Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp6022414ybc; Wed, 27 Nov 2019 13:33:44 -0800 (PST) X-Google-Smtp-Source: APXvYqxl3i6kJfFMKM0VJ7qJeLutr59+qfwLAmJoTDObFP8+VbMuGZqf0hN/636RH07mYOmZX/Dh X-Received: by 2002:a17:906:360a:: with SMTP id q10mr8427720ejb.131.1574890424700; Wed, 27 Nov 2019 13:33:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574890424; cv=none; d=google.com; s=arc-20160816; b=onpKzwQGoDM+27nHCOT2UV+miyokYsQrsF3SoCF9cPct680frldabqdnzAau4MliCl 28oiYUDj9paTIZjmP5JtzpkZxCUt4gUy4SnWxa83BL5UgZR23gr5lI7D7OKxpf7ufHXt ZPC3nVNr1rWprovZ7uNoHPpWjWz88lGz/ffZxdgxEwpU1sqzNjqE3sKnXOQXHC/wO3sx Rffox0jzTuavCE/HzgLdOAkL8RxUAQj3g7EHEzUL166rBAUUkekNJWjHr5DCMawST65g EeitQaCFZAyK8LTRy3Ku4iapbRw8htpTw6xXcRaN41mkoN8ItSvSvAYNgP2MrgTcSSJE VVEQ== 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=CnTXGnGWAjjKzEwdf/sSZWyfXyHHU1jU88za6/Dzb7o=; b=PDELOsVdhxCSfF8AXzmESho1VSQ1reuJf5f2NfmK6GSiGXQ4orV626heOTN69isP79 ntBcEwOhc0kZagXbqVQTfy1ZByadyDqFiQwTw7aYGuROlqYAOHuVtArU6yZHyVYp+E9u Z8oyq4BLcJ2QDV2sCK6rehq9ottX9dYjMULPG12rox6xLeEnOKgsaoV5Ed7TQ9EHpeZB Ks+rOzkuu0wrZ9bgQ26R/5EhxswRE8v9SJ08aBcrXKbgCdpHGa2Wlnjh7doSzkN/A5UO DiaWmpkxztom/RhW/ttCdU9ENcDeJlb3SV1VoTSV2vY76IL76A6D7JB8HPBie1AT3giH gsWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=01NAvmDo; 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 w2si11397432edf.32.2019.11.27.13.33.21; Wed, 27 Nov 2019 13:33:44 -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=01NAvmDo; 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 S1730982AbfK0VbG (ORCPT + 99 others); Wed, 27 Nov 2019 16:31:06 -0500 Received: from mail.kernel.org ([198.145.29.99]:45506 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730916AbfK0UzG (ORCPT ); Wed, 27 Nov 2019 15:55:06 -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 5BA422084D; Wed, 27 Nov 2019 20:55:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1574888105; bh=bg068m6eQEBEESa5p5w78Mq6U6ACRkCkcOhx0Dbo9xs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=01NAvmDoxEFQiZTn933OcFMmGMR4L3JwuH/TAcWILAUD3TUHByHphwvSSfX5/qT0j Vesa012r/2wk6ThBtRYtZAgtj1ibTT40Sst1cNiMli07VzdrV6t05pZHOBaSIvnrsU qkd4VJXOtEPBGxUakqfkf2qaExE9sp2ndPEIQScs= 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.14 189/211] media: vivid: Fix wrong locking that causes race conditions on streaming stop Date: Wed, 27 Nov 2019 21:32:02 +0100 Message-Id: <20191127203111.557215149@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191127203049.431810767@linuxfoundation.org> References: <20191127203049.431810767@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 @@ -777,7 +777,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; @@ -930,8 +934,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 @@ -147,7 +147,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; @@ -301,8 +305,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 @@ -149,7 +149,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; @@ -309,10 +313,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 = {