Skip to content

Commit 6dcd5d7

Browse files
a13xp0p0vmchehab
authored andcommitted
media: vivid: Fix wrong locking that causes race conditions on streaming stop
There is the same incorrect approach to locking implemented in vivid_stop_generating_vid_cap(), vivid_stop_generating_vid_out() and sdr_cap_stop_streaming(). These functions are called during streaming stopping with vivid_dev.mutex locked. And they all do the same mistake while stopping their kthreads, which need to lock this mutex as well. See the example from vivid_stop_generating_vid_cap(): /* shutdown control thread */ vivid_grab_controls(dev, false); mutex_unlock(&dev->mutex); kthread_stop(dev->kthread_vid_cap); dev->kthread_vid_cap = NULL; mutex_lock(&dev->mutex); But when this mutex is unlocked, another vb2_fop_read() can lock it instead of vivid_thread_vid_cap() and manipulate the buffer queue. That causes a use-after-free access later. To fix those issues let's: 1. avoid unlocking the mutex in vivid_stop_generating_vid_cap(), vivid_stop_generating_vid_out() and sdr_cap_stop_streaming(); 2. use mutex_trylock() with schedule_timeout_uninterruptible() in the loops of the vivid kthread handlers. Signed-off-by: Alexander Popov <alex.popov@linux.com> Acked-by: Linus Torvalds <torvalds@linux-foundation.org> Tested-by: Hans Verkuil <hverkuil-cisco@xs4all.nl> Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl> Cc: <stable@vger.kernel.org> # for v3.18 and up Signed-off-by: Mauro Carvalho Chehab <mchehab@kernel.org>
1 parent 0c90f64 commit 6dcd5d7

3 files changed

Lines changed: 15 additions & 9 deletions

File tree

‎drivers/media/platform/vivid/vivid-kthread-cap.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,11 @@ static int vivid_thread_vid_cap(void *data)
818818
if (kthread_should_stop())
819819
break;
820820

821-
mutex_lock(&dev->mutex);
821+
if (!mutex_trylock(&dev->mutex)) {
822+
schedule_timeout_uninterruptible(1);
823+
continue;
824+
}
825+
822826
cur_jiffies = jiffies;
823827
if (dev->cap_seq_resync) {
824828
dev->jiffies_vid_cap = cur_jiffies;
@@ -998,8 +1002,6 @@ void vivid_stop_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming)
9981002

9991003
/* shutdown control thread */
10001004
vivid_grab_controls(dev, false);
1001-
mutex_unlock(&dev->mutex);
10021005
kthread_stop(dev->kthread_vid_cap);
10031006
dev->kthread_vid_cap = NULL;
1004-
mutex_lock(&dev->mutex);
10051007
}

‎drivers/media/platform/vivid/vivid-kthread-out.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,11 @@ static int vivid_thread_vid_out(void *data)
166166
if (kthread_should_stop())
167167
break;
168168

169-
mutex_lock(&dev->mutex);
169+
if (!mutex_trylock(&dev->mutex)) {
170+
schedule_timeout_uninterruptible(1);
171+
continue;
172+
}
173+
170174
cur_jiffies = jiffies;
171175
if (dev->out_seq_resync) {
172176
dev->jiffies_vid_out = cur_jiffies;
@@ -344,8 +348,6 @@ void vivid_stop_generating_vid_out(struct vivid_dev *dev, bool *pstreaming)
344348

345349
/* shutdown control thread */
346350
vivid_grab_controls(dev, false);
347-
mutex_unlock(&dev->mutex);
348351
kthread_stop(dev->kthread_vid_out);
349352
dev->kthread_vid_out = NULL;
350-
mutex_lock(&dev->mutex);
351353
}

‎drivers/media/platform/vivid/vivid-sdr-cap.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,11 @@ static int vivid_thread_sdr_cap(void *data)
141141
if (kthread_should_stop())
142142
break;
143143

144-
mutex_lock(&dev->mutex);
144+
if (!mutex_trylock(&dev->mutex)) {
145+
schedule_timeout_uninterruptible(1);
146+
continue;
147+
}
148+
145149
cur_jiffies = jiffies;
146150
if (dev->sdr_cap_seq_resync) {
147151
dev->jiffies_sdr_cap = cur_jiffies;
@@ -303,10 +307,8 @@ static void sdr_cap_stop_streaming(struct vb2_queue *vq)
303307
}
304308

305309
/* shutdown control thread */
306-
mutex_unlock(&dev->mutex);
307310
kthread_stop(dev->kthread_sdr_cap);
308311
dev->kthread_sdr_cap = NULL;
309-
mutex_lock(&dev->mutex);
310312
}
311313

312314
static void sdr_cap_buf_request_complete(struct vb2_buffer *vb)

0 commit comments

Comments
 (0)