Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp1521030ybb; Thu, 9 Apr 2020 03:56:15 -0700 (PDT) X-Google-Smtp-Source: APiQypLOaakbDH7KlmfM//4wQJJK+91wiWlmsMGx1UMKjqKN9xBr/+4sBZrdWRFicPcLGyYs/2AH X-Received: by 2002:a9d:610e:: with SMTP id i14mr6614071otj.256.1586429774923; Thu, 09 Apr 2020 03:56:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586429774; cv=none; d=google.com; s=arc-20160816; b=RJZ5o/nG+/OHdvcd0Xb69sAKxjpx+faZnoc5Ewt8dBNF3+B7ynLnIGPSHZyPHx79M5 MwqgInP421G7ijfzlh1tglKzxz6NNzzI4poVSFH21bM5jspbZf1Q7eN8akoICEYfqIje wouP3C5EZwNHewzvhfRfgI/Wt+RL0FgKBdcJBGOrLB1RgUdp0Om9o3qu3LnfUkb3IISx stwFOBXVvKcf4Youb6zqmtpejAcZbuji809QpPxmdvwpcBsuce0MfDBGWfxz5MtFZXWN Wbap02o+9a5GwIW0svkk6u6PkLa2DcBoBjBvTsgCLE+TqUyHuM7UgXSpIm7nk7adbMDK sh+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date; bh=bruAAtc1uIe1QCsTkU0OUXxsiu6i80S1jm65g3WH6uE=; b=GE/bzLrvlbV/VyrMuyvnLXULqF+w4TrSMw2dwO2dMm7QvE5PgEDmCr8UjtIq1elJlA 3Q638OA8AR7d/4TcBC+Ta0Wbk3Vo/1W4lc9j7xXy/tIsaKz11d8VBg4pr1huqhgl/Xgr Lf+D4FKj9QMTUV3ahCb+4m0RPQEHImR38UV3Y7OC9jPcDjK/YpcekkjnDwnS0dHjNsK3 M8bfv8dE8CWmGMEH5vXg69sLfOSIG/UXACO0mGqhHCAqaeAeRW5VzMis8R6qRFas5k2N uvHW/uperccV690uJiQc9As0qGn03aXjveS66t9JZML2KWEraWUVBDNcB4zSiQlXpPrk pAvQ== 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 w15si3983908oth.84.2020.04.09.03.56.00; Thu, 09 Apr 2020 03:56:14 -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 S1726652AbgDIKx4 (ORCPT + 99 others); Thu, 9 Apr 2020 06:53:56 -0400 Received: from mail2-relais-roc.national.inria.fr ([192.134.164.83]:36477 "EHLO mail2-relais-roc.national.inria.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725828AbgDIKx4 (ORCPT ); Thu, 9 Apr 2020 06:53:56 -0400 X-IronPort-AV: E=Sophos;i="5.72,362,1580770800"; d="scan'208";a="444554909" Received: from abo-173-121-68.mrs.modulonet.fr (HELO hadrien) ([85.68.121.173]) by mail2-relais-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 09 Apr 2020 12:53:54 +0200 Date: Thu, 9 Apr 2020 12:53:54 +0200 (CEST) From: Julia Lawall X-X-Sender: jll@hadrien To: Alexander Popov cc: Gilles Muller , Nicolas Palix , Michal Marek , cocci@systeme.lip6.fr, "kernel-hardening@lists.openwall.com" , Jann Horn , Kees Cook , Hans Verkuil , Mauro Carvalho Chehab , Linux Media Mailing List , LKML , jannh@google.com Subject: Re: [Cocci] Coccinelle rule for CVE-2019-18683 In-Reply-To: Message-ID: References: User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 9 Apr 2020, Alexander Popov wrote: > Hello! > > Some time ago I fixed CVE-2019-18683 in the V4L2 subsystem of the Linux kernel. > > I created a Coccinelle rule that detects that bug pattern. Let me show it. Thanks for the discussion :) > > > Bug pattern > =========== > > CVE-2019-18683 refers to three similar vulnerabilities caused by the same > incorrect approach to locking that is used in vivid_stop_generating_vid_cap(), > vivid_stop_generating_vid_out(), and sdr_cap_stop_streaming(). > > For fixes please see the commit 6dcd5d7a7a29c1e4 (media: vivid: Fix wrong > locking that causes race conditions on streaming stop). > > These three 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 > the kthread and manipulate the buffer queue. That causes use-after-free. > > I created a Coccinelle rule that detects mutex_unlock+kthread_stop+mutex_lock > within one function. > > > Coccinelle rule > =============== > > virtual report > > @race exists@ > expression E; > position stop_p; > position unlock_p; > position lock_p; > @@ > > mutex_unlock@unlock_p(E) > ... It would be good to put when != mutex_lock(E) after the ... above. Your rule doesn't actually prevent the lock from being retaken. > kthread_stop@stop_p(...) > ... > mutex_lock@lock_p(E) > > @script:python@ > stop_p << race.stop_p; > unlock_p << race.unlock_p; > lock_p << race.lock_p; > E << race.E; > @@ > > coccilib.report.print_report(unlock_p[0], 'mutex_unlock(' + E + ') here') > coccilib.report.print_report(stop_p[0], 'kthread_stop here') > coccilib.report.print_report(lock_p[0], 'mutex_lock(' + E + ') here\n') > > > Testing the rule > ================ > > I reverted the commit 6dcd5d7a7a29c1e4 and called: > COCCI=./scripts/coccinelle/kthread_race.cocci make coccicheck MODE=report > > The result: > > ./drivers/media/platform/vivid/vivid-kthread-out.c:347:1-13: mutex_unlock(& dev > -> mutex) here > ./drivers/media/platform/vivid/vivid-kthread-out.c:348:1-13: kthread_stop here > ./drivers/media/platform/vivid/vivid-kthread-out.c:350:1-11: mutex_lock(& dev -> > mutex) here > > ./drivers/media/platform/vivid/vivid-sdr-cap.c:306:1-13: mutex_unlock(& dev -> > mutex) here > ./drivers/media/platform/vivid/vivid-sdr-cap.c:307:1-13: kthread_stop here > ./drivers/media/platform/vivid/vivid-sdr-cap.c:309:1-11: mutex_lock(& dev -> > mutex) here > > ./drivers/media/platform/vivid/vivid-kthread-cap.c:1001:1-13: mutex_unlock(& dev > -> mutex) here > ./drivers/media/platform/vivid/vivid-kthread-cap.c:1002:1-13: kthread_stop here > ./drivers/media/platform/vivid/vivid-kthread-cap.c:1004:1-11: mutex_lock(& dev > -> mutex) here > > There are no other bugs detected. > > Do you have any idea how to improve it? > Do we need that rule for regression testing in the upstream? Based on Jann's suggestion, it seem like it could be interesting to find these locking pauses, and then collect functions that are used in locks and in lock pauses. If a function is mostly used with locks held, then using it in a lock pause could be a sign of a bug. I will see if it turns up anything interesting. julia