Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp2102015ybx; Sat, 2 Nov 2019 12:04:41 -0700 (PDT) X-Google-Smtp-Source: APXvYqwy2FWn6HOv2Di+VfwJfxAJzsB8c7hFZ9IgRLNmTCBQWTYD7Qalyl1qB0c00WPD6+HFULxU X-Received: by 2002:a17:906:a446:: with SMTP id cb6mr8745951ejb.48.1572721480918; Sat, 02 Nov 2019 12:04:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572721480; cv=none; d=google.com; s=arc-20160816; b=zhRRzBmevRGlbcivQFM3WhUrGARU1R4JRT3IMQMSdGFvfrqpL0q6JCd2VEnmrW3cJJ MV54bQwLEopDDAKK6b5IyRQFPNkgtHeSPTU0u5wPyJgltxNJhVfCHgqQ/UyO6RpTO8dO fMXUviecpDPQ05C6NfM4cDdI/WejVKk5LfcB94mf8J20+kUm51bTdGUaA1QMDLjm4cdQ YAjQVWOFb6TkaM8hh3BXoqgD+1ykRSfHAzE/gdrfLUb0dF3Gn53l7HvCy+BiX1Yh5TgR glwbdpt5gHWSIcMqwbQzMPJbjkDF09b8I2thr26kryc2S6Y6zQBSl7BEDMfqUZkYaEh5 jsow== 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 :message-id:date:subject:to:from; bh=OwXkGlWC6FY9JCKWqS9fhbxPEPEvk7bQIiDtUDxVeTs=; b=TtQPGikxEbaAfcQ9bVcuQERSIQHBdGNPIUvabULDMnFjZzSYoKbKp7LlrCpuhOtaim r1YibQOX1Ju5SlCobW7z9d86yIJoJ3yPOXMnQ1S39zIzB5eRKlDnvWWLVEPvUxz0XzEe nteHnroRi7qdrQ/luJwkwJDAuzX5VAN+HjRyvtxkBSIdGHvMw5lKnjRtW970roZ0Dv6y FAOMH0pk1nSvsWXaf5FtjJB5gbSAG8AigfYuk6CPcnilMtUMbxWN1QJRKOXDD/VeDRO3 Dc/+W8SVrQOWfK1YJavyzAZvxAlVgg7INF93U6QtYpKTW79ghc71Zc2DjQmJb1IONaPt TqTA== ARC-Authentication-Results: i=1; mx.google.com; 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 m17si8502596eja.415.2019.11.02.12.04.13; Sat, 02 Nov 2019 12:04:40 -0700 (PDT) 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; 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 S1726771AbfKBTDn (ORCPT + 99 others); Sat, 2 Nov 2019 15:03:43 -0400 Received: from mail-lf1-f67.google.com ([209.85.167.67]:36822 "EHLO mail-lf1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726396AbfKBTDn (ORCPT ); Sat, 2 Nov 2019 15:03:43 -0400 Received: by mail-lf1-f67.google.com with SMTP id a6so6053396lfo.3; Sat, 02 Nov 2019 12:03:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=OwXkGlWC6FY9JCKWqS9fhbxPEPEvk7bQIiDtUDxVeTs=; b=O1Yz+RC1ehoKp70tqg6wqH1fSa6eyUIOrof1nvCJ9y/LZkPhD/rS8QGJHmkKsXsIhS 59gIRb3K95NrVGUpCnoHgV+NlBQCP4VaMwyztFh0Ev+wvBlJTyvchkZI4SNzv3ZdGamp WrwJES+qhnfCUcNctyvDR5C72Yn0SQeL3HnWqK8jK5taGZVUzKogtA04PPt6B7gAQKAZ gIZMjebysofhKV3iF+7PpF/TlQ02exGC8KB11az9G0UQnCzxHZeiKKKWckOUjGcdowEe NMSt5etbqyFAI6ChIraWL5pvIchNZdO1rzTGjlhYdGoRAKYNLjBjIXQPyJkCDzQJ17X+ nlXQ== X-Gm-Message-State: APjAAAU6Q7etxEC1gZQh/haxWjejOMUOtscZ4+/ZkXRsVof1lMtnKfMY CHxetH1eAc+3WSb/879RIDA= X-Received: by 2002:a19:41c8:: with SMTP id o191mr11564028lfa.101.1572721421272; Sat, 02 Nov 2019 12:03:41 -0700 (PDT) Received: from hackbase.lan ([213.234.229.88]) by smtp.gmail.com with ESMTPSA id c3sm3989149lfi.32.2019.11.02.12.03.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Nov 2019 12:03:40 -0700 (PDT) From: Alexander Popov To: Linus Torvalds , Hans Verkuil , Mauro Carvalho Chehab , Security Officers , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Alexander Popov Subject: [PATCH v3 1/1] media: vivid: Fix wrong locking that causes race conditions on streaming stop Date: Sat, 2 Nov 2019 22:03:27 +0300 Message-Id: <20191102190327.24903-1-alex.popov@linux.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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() in the loops of the vivid kthread handlers. Signed-off-by: Alexander Popov Acked-by: Linus Torvalds --- 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(-) diff --git a/drivers/media/platform/vivid/vivid-kthread-cap.c b/drivers/media/platform/vivid/vivid-kthread-cap.c index 003319d7816d..27b9c78d2d05 100644 --- a/drivers/media/platform/vivid/vivid-kthread-cap.c +++ b/drivers/media/platform/vivid/vivid-kthread-cap.c @@ -796,7 +796,11 @@ static int vivid_thread_vid_cap(void *data) if (kthread_should_stop()) break; - mutex_lock(&dev->mutex); + if (!mutex_trylock(&dev->mutex)) { + schedule_timeout(1); + continue; + } + cur_jiffies = jiffies; if (dev->cap_seq_resync) { dev->jiffies_vid_cap = cur_jiffies; @@ -956,8 +960,6 @@ void vivid_stop_generating_vid_cap(struct vivid_dev *dev, bool *pstreaming) /* 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); } diff --git a/drivers/media/platform/vivid/vivid-kthread-out.c b/drivers/media/platform/vivid/vivid-kthread-out.c index ce5bcda2348c..a657b0d20e2f 100644 --- a/drivers/media/platform/vivid/vivid-kthread-out.c +++ b/drivers/media/platform/vivid/vivid-kthread-out.c @@ -143,7 +143,11 @@ static int vivid_thread_vid_out(void *data) if (kthread_should_stop()) break; - mutex_lock(&dev->mutex); + if (!mutex_trylock(&dev->mutex)) { + schedule_timeout(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(struct vivid_dev *dev, bool *pstreaming) /* 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); } diff --git a/drivers/media/platform/vivid/vivid-sdr-cap.c b/drivers/media/platform/vivid/vivid-sdr-cap.c index 9acc709b0740..590080716962 100644 --- a/drivers/media/platform/vivid/vivid-sdr-cap.c +++ b/drivers/media/platform/vivid/vivid-sdr-cap.c @@ -141,7 +141,11 @@ static int vivid_thread_sdr_cap(void *data) if (kthread_should_stop()) break; - mutex_lock(&dev->mutex); + if (!mutex_trylock(&dev->mutex)) { + schedule_timeout(1); + continue; + } + cur_jiffies = jiffies; if (dev->sdr_cap_seq_resync) { dev->jiffies_sdr_cap = cur_jiffies; @@ -303,10 +307,8 @@ static void sdr_cap_stop_streaming(struct vb2_queue *vq) } /* shutdown control thread */ - mutex_unlock(&dev->mutex); kthread_stop(dev->kthread_sdr_cap); dev->kthread_sdr_cap = NULL; - mutex_lock(&dev->mutex); } static void sdr_cap_buf_request_complete(struct vb2_buffer *vb) -- 2.21.0