Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp6013194ybc; Wed, 27 Nov 2019 13:23:31 -0800 (PST) X-Google-Smtp-Source: APXvYqyu0dYHo5fCZK8PQn1/rPKIDgd0OyNf/EM2z5q5nZGqEyiJSynX6daCQaFCCrBnVx6kx0lC X-Received: by 2002:aa7:c402:: with SMTP id j2mr15396591edq.249.1574889811640; Wed, 27 Nov 2019 13:23:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574889811; cv=none; d=google.com; s=arc-20160816; b=HWThtDmts4az83vc8yq7H/xHMCDM54LUSEWCYSzEbQswcbiNT4EXx4QLN/+4RTNjYO FJ0SXG17L/IgQb2xmMyEgElhYmMFyzdHZf3Pnyacf/0BbYSY2LQkNBOpXnjN/9M14djX c8o0M16CiwvRAOQkGq/JWZh023JLms2dBTiXu92HOrErzx+pN5KzNdivLf0BAlD2GPm4 W6ONzSak1IOe92XB9zIhzG1pyClP174t2L4ynyV2QhrorbkBKYh61pe6U4waWYI1Ht6O unBOhkoAm5oZv4aj0q7g7FE60zBrhWIHgIo6OqKpG1Hn3VdG5TzngQcz5nsIwrVPhhek iL2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=guH2S5muBU/Sacc8OcPvEltSMhl6CfFVHdMLArfyqP0=; b=FIoZYnY61kB0TxK9PLGdV4IAVVg8tbOr9zo5POZF12jUkPhVAmA9vd7/x+mFxpZbHG aIK2v1BGVFp7XDslSBwHQ/3+1QtT1wBbbjLUHN11xQhLTcN1nyenja1V9QEn9uSkB44q fahrrGAB0zxSMGx5IDiiFiXVmV8Nvrssif/dWTtYbDpcalio03BFjGZFwpGkjXufZ+0B JckKyHedS67QF/UFr/eqVPBYMUOhrjQ4SmNzuhzPil7ikvdosqet2NvgHHG+4KeNLbB3 XNJtSvHQAFfzCM4Dwnt9Mt3XDHW41qZ5G5qUXEnCrdr1h0ZxriulnL5/BabeH8SwV1pQ m1LQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=OcMFsiXJ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y14si11058356edu.250.2019.11.27.13.23.07; Wed, 27 Nov 2019 13:23:31 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=OcMFsiXJ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732721AbfK0VIP (ORCPT + 99 others); Wed, 27 Nov 2019 16:08:15 -0500 Received: from mail.kernel.org ([198.145.29.99]:34624 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731764AbfK0VIM (ORCPT ); Wed, 27 Nov 2019 16:08:12 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D034121775; Wed, 27 Nov 2019 21:08:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1574888892; bh=/P8hmbq+AUTR087NgJfoly6UxB9jxkqQeYB/Vda1iQA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OcMFsiXJeHitxrommzfbnGmZBZpDJxlXaC9XVoJk3b4wnAXl+SrIoQJ7b54sm1jlL i1WYxaP0fGgbD4VLMQCTFwClxFN91FaOBbPVVHUSHsPOB02HdZ/3ok/dHJS4HOR51f Zf/L5QH5PPEo6Oq5F18Np4k2OwV8umYzwk16AWXQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Alexander Popov , Linus Torvalds , Hans Verkuil , Mauro Carvalho Chehab Subject: [PATCH 4.19 284/306] media: vivid: Fix wrong locking that causes race conditions on streaming stop Date: Wed, 27 Nov 2019 21:32:14 +0100 Message-Id: <20191127203135.520127969@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191127203114.766709977@linuxfoundation.org> References: <20191127203114.766709977@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alexander Popov commit 6dcd5d7a7a29c1e4b8016a06aed78cd650cd8c27 upstream. 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 Acked-by: Linus Torvalds Tested-by: Hans Verkuil Signed-off-by: Hans Verkuil Cc: # for v3.18 and up Signed-off-by: Mauro Carvalho Chehab Signed-off-by: Greg Kroah-Hartman --- drivers/media/platform/vivid/vivid-kthread-cap.c | 8 +++++--- drivers/media/platform/vivid/vivid-kthread-out.c | 8 +++++--- drivers/media/platform/vivid/vivid-sdr-cap.c | 8 +++++--- 3 files changed, 15 insertions(+), 9 deletions(-) --- a/drivers/media/platform/vivid/vivid-kthread-cap.c +++ b/drivers/media/platform/vivid/vivid-kthread-cap.c @@ -765,7 +765,11 @@ static int vivid_thread_vid_cap(void *da if (kthread_should_stop()) break; - mutex_lock(&dev->mutex); + if (!mutex_trylock(&dev->mutex)) { + schedule_timeout_uninterruptible(1); + continue; + } + cur_jiffies = jiffies; if (dev->cap_seq_resync) { dev->jiffies_vid_cap = cur_jiffies; @@ -918,8 +922,6 @@ void vivid_stop_generating_vid_cap(struc /* 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); } --- a/drivers/media/platform/vivid/vivid-kthread-out.c +++ b/drivers/media/platform/vivid/vivid-kthread-out.c @@ -135,7 +135,11 @@ static int vivid_thread_vid_out(void *da if (kthread_should_stop()) break; - mutex_lock(&dev->mutex); + if (!mutex_trylock(&dev->mutex)) { + schedule_timeout_uninterruptible(1); + continue; + } + cur_jiffies = jiffies; if (dev->out_seq_resync) { dev->jiffies_vid_out = cur_jiffies; @@ -289,8 +293,6 @@ void vivid_stop_generating_vid_out(struc /* shutdown control thread */ vivid_grab_controls(dev, false); - mutex_unlock(&dev->mutex); kthread_stop(dev->kthread_vid_out); dev->kthread_vid_out = NULL; - mutex_lock(&dev->mutex); } --- a/drivers/media/platform/vivid/vivid-sdr-cap.c +++ b/drivers/media/platform/vivid/vivid-sdr-cap.c @@ -137,7 +137,11 @@ static int vivid_thread_sdr_cap(void *da if (kthread_should_stop()) break; - mutex_lock(&dev->mutex); + if (!mutex_trylock(&dev->mutex)) { + schedule_timeout_uninterruptible(1); + continue; + } + cur_jiffies = jiffies; if (dev->sdr_cap_seq_resync) { dev->jiffies_sdr_cap = cur_jiffies; @@ -297,10 +301,8 @@ static void sdr_cap_stop_streaming(struc } /* shutdown control thread */ - mutex_unlock(&dev->mutex); kthread_stop(dev->kthread_sdr_cap); dev->kthread_sdr_cap = NULL; - mutex_lock(&dev->mutex); } const struct vb2_ops vivid_sdr_cap_qops = {