Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp1091656ybb; Fri, 10 Apr 2020 17:12:41 -0700 (PDT) X-Google-Smtp-Source: APiQypKbPovaVaDrxPN1x6dM/p1H70LMpP69tZ+0CdBjk/2dWn+AaekjpXKhLT3pxF4IeyosuLoT X-Received: by 2002:ac8:480d:: with SMTP id g13mr1716808qtq.29.1586563961724; Fri, 10 Apr 2020 17:12:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586563961; cv=none; d=google.com; s=arc-20160816; b=k3RHrPUiAgdg9XqqiKK0ln213fjdZyNaNhBGPXJXZ/ChR6QwvKj3rKtv3+Urpuw307 Xzc/Dk4natrxOto0ixBIeu+Daazl11gX6i6nkUFR9JsqnFG++IVh56Qog8f+THomvfab oF9lwjY8qBfstYxQrkb0HueQT2CLQ4ZUPw3QkrEreQbKOv0sYTB4WKwsRfMyxj7EiYTb xM1lLWPUjDSL7ysdtOJXfjU6YSJJ4OGEymOQ3nzoWtukKKdNo8CNZhYoaCgkmcvxPR3g /k9GKhZ4KuEVVbh9dyHqIJee3jn661rt+V37g0t657GOLxG4hO0UO7iAK+OHDrcBdkqT tuCg== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:autocrypt:references:cc:to:from:subject:reply-to; bh=ScHIcIB77dQcUEBf39WWnnD1yi6BECX6mpqWSLTHIR4=; b=z82Yz6qVLMA9o8HmCiJz6kCK8QstZ/F4JDFBtljKM5BWmA9hb0DTdJdAPCdul0KyGG AglRAECPSSOAAdQyRo2kXg/eGKabEVY6U6YJUGTVjb5LgKAqM7f2fSsVAZRCcNzxZIDM 1ReVbT2KzDjeJh1pepArPCXd2n3liIX1rdJs7iVzaDk6wVw79J5wd/hCIWvm1DPb4eOQ uBnPk0IGB/DNWTbP8scYgFwmFaap17ZFwvCl/a8x1f6ElnpbYJC/bvegGwPm6NBR0PGu BTZ2h8Q/Qun4pMvc1kzKsjnBWpNzRa2PtEeBj/A+KbxNo5aJ7ld9yS3VIayocYgcoXmV gD9w== 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 s5si1988410qtb.246.2020.04.10.17.12.14; Fri, 10 Apr 2020 17:12:41 -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 S1726702AbgDKAKw (ORCPT + 99 others); Fri, 10 Apr 2020 20:10:52 -0400 Received: from mail-lf1-f49.google.com ([209.85.167.49]:37326 "EHLO mail-lf1-f49.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726641AbgDKAKw (ORCPT ); Fri, 10 Apr 2020 20:10:52 -0400 Received: by mail-lf1-f49.google.com with SMTP id t11so2450276lfe.4; Fri, 10 Apr 2020 17:10:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:subject:from:to:cc:references:autocrypt :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=ScHIcIB77dQcUEBf39WWnnD1yi6BECX6mpqWSLTHIR4=; b=gby0fvPNrNftvfwpdaYdPfSw2L59MNK3Wvn2v+p66Uw/V71GSSIiniIecHK17cnu4x M1uEsqKkFZsIco4fr4IaCxgwiUqX42BkglSabBi/3GXnxBHmWiNFT5dPvorGhJD7txi/ wVSnTq2tOXOHUQHYNF8VmImaGFgU+l88PA/43SRd9MhWPiMKA22NKdk+AruHpE20iI/m v3WfEji7JNI8ucnkf8I7IxNwWZqb7+XRbHrnjBSFxNIOTPp4M7gF4rUih8HQsAYrhghW U+FlQYhlf2kZiFP0Oy+LHJzoJ3/bXkzloj3yJjpDmGGKgb9Ialc+V5i945PAAAMnjEJD 87hA== X-Gm-Message-State: AGi0PuYeg2+5yA5Qf7XOQkfe4bgbGAGY/c56p8GJPmZFBLqKCOoZmXpG vGsKShiVcfXXhAd2UOaug9M= X-Received: by 2002:ac2:43c6:: with SMTP id u6mr4016963lfl.170.1586563850394; Fri, 10 Apr 2020 17:10:50 -0700 (PDT) Received: from [192.168.1.183] ([176.15.215.153]) by smtp.gmail.com with ESMTPSA id w24sm2257297lfe.58.2020.04.10.17.10.48 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 10 Apr 2020 17:10:49 -0700 (PDT) Reply-To: alex.popov@linux.com Subject: Re: Coccinelle rule for CVE-2019-18683 From: Alexander Popov To: Jann Horn Cc: Julia Lawall , Gilles Muller , Nicolas Palix , Michal Marek , cocci@systeme.lip6.fr, "kernel-hardening@lists.openwall.com" , Kees Cook , Hans Verkuil , Mauro Carvalho Chehab , Linux Media Mailing List , LKML , Markus Elfring References: Autocrypt: addr=alex.popov@linux.com; prefer-encrypt=mutual; keydata= mQINBFX15q4BEADZartsIW3sQ9R+9TOuCFRIW+RDCoBWNHhqDLu+Tzf2mZevVSF0D5AMJW4f UB1QigxOuGIeSngfmgLspdYe2Kl8+P8qyfrnBcS4hLFyLGjaP7UVGtpUl7CUxz2Hct3yhsPz ID/rnCSd0Q+3thrJTq44b2kIKqM1swt/F2Er5Bl0B4o5WKx4J9k6Dz7bAMjKD8pHZJnScoP4 dzKPhrytN/iWM01eRZRc1TcIdVsRZC3hcVE6OtFoamaYmePDwWTRhmDtWYngbRDVGe3Tl8bT 7BYN7gv7Ikt7Nq2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mgyCajVOhUhln4IuwvZke1CLfXe1i3ZtlaIUrxfXqfYpeijfM/JSmliPxwW BRnQRcgS85xpC1pBUMrraxajaVPwu7hCTke03v6bu8zSZlgA1rd9E6KHu2VNS46VzUPjbR77 kO7u6H5PgQPKcuJwQQ+d3qa+5ZeKmoVkc2SuHVrCd1yKtAMmKBoJtSku1evXPwyBzqHFOInk mLMtrWuUhj+wtcnOWxaP+n4ODgUwc/uvyuamo0L2Gp3V5ItdIUDO/7ZpZ/3JxvERF3Yc1md8 5kfflpLzpxyl2fKaRdvxr48ZLv9XLUQ4qNuADDmJArq/+foORAX4BBFWvqZQKe8a9ZMAvGSh uoGUVg4Ks0uC4IeG7iNtd+csmBj5dNf91C7zV4bsKt0JjiJ9a4D85dtCOPmOeNuusK7xaDZc gzBW8J8RW+nUJcTpudX4TC2SGeAOyxnM5O4XJ8yZyDUY334seDRJWtS4wRHxpfYcHKTewR96 IsP1USE+9ndu6lrMXQ3aFsd1n1m1pfa/y8hiqsSYHy7JQ9Iuo9DxysOj22UNOmOE+OYPK48D j3lCqPk= Message-ID: Date: Sat, 11 Apr 2020 03:10:48 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-GB Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 09.04.2020 22:41, Alexander Popov wrote: > On 09.04.2020 01:26, Jann Horn wrote: >> On Thu, Apr 9, 2020 at 12:01 AM Alexander Popov wrote: >>> 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. >> [...] >>> mutex_unlock@unlock_p(E) >>> ... >>> kthread_stop@stop_p(...) >>> ... >>> mutex_lock@lock_p(E) >> >> Is the kthread_stop() really special here? It seems to me like it's >> pretty much just a normal instance of the "temporarily dropping a >> lock" pattern - which does tend to go wrong quite often, but can also >> be correct. > > Right, searching without kthread_stop() gives more cases. > >> I think it would be interesting though to have a list of places that >> drop and then re-acquire a mutex/spinlock/... that was not originally >> acquired in the same block of code (but was instead originally >> acquired in an outer block, or by a parent function, or something like >> that). So things like this: The following rule reported 146 matching cases, which might be interesting. ``` virtual report virtual context @race exists@ expression E; position unlock_p; position lock_p; @@ ... when != mutex_lock(E) * mutex_unlock@unlock_p(E) ... when != schedule() when != schedule_timeout(...) when != cond_resched() when != wait_event(...) when != wait_event_timeout(...) when != wait_event_interruptible_timeout(...) when != wait_event_interruptible(...) when != msleep() when != msleep_interruptible(...) * mutex_lock@lock_p(E) @script:python@ unlock_p << race.unlock_p; lock_p << race.lock_p; E << race.E; @@ coccilib.report.print_report(unlock_p[0], 'see mutex_unlock(' + E + ') here') coccilib.report.print_report(lock_p[0], 'see mutex_lock(' + E + ') here\n') ``` Analysing each matching case would take a lot of time. However, I'm focused on searching kernel security issues. So I will filter out the code that: - is not enabled in popular kernel configurations, - doesn't create additional attack surface. Then I'll take the time to analyse the rest of reported cases. I'll inform you if I find any bug. Best regards, Alexander