Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp6974204rwb; Mon, 12 Dec 2022 08:32:08 -0800 (PST) X-Google-Smtp-Source: AA0mqf7mDBM042kQIdfUok+b5I8gNVISzIE5Fa0t7O/TAw7zpFVTP7lsA14WVw7X76yRUZ4DZiiv X-Received: by 2002:a17:90a:e147:b0:219:17bb:b854 with SMTP id ez7-20020a17090ae14700b0021917bbb854mr17071064pjb.29.1670862728282; Mon, 12 Dec 2022 08:32:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670862728; cv=none; d=google.com; s=arc-20160816; b=ugGO5MwyVeUPE+S3KfrKk+8jkbWrf3bPn/pht1haLYtXXzpSfu+UM57Q43HXm7Sqa6 GuuBbinjgHtRdhWjMwsXitf2SN0+F+sahBb9AKWzkOHKz7naUxwQHwLwY8uTT8jTvTCp 1CzVAo5/M5N7ufpjqBbxizm6ZX0ipz3PTEvTl9E8msxZmgOzhsfQy/5SDdt0p4dmUlKC lojVpABJH1tg8Muhm/Gx+yKLiJvKUU2rJv9/nQ/1dVRRh2M4cnT3IpMKBn/Q9dBenY3I AagxRmQENk8VgD2MpjrW5btsI4Kw+HSKGFLKVEpb7Aw3V/jVCRVYqQ4AO7md3KleOTeb l9xQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=nAIZvy2GHC+aPE3w+aZumyMTj4ABKFPu4soNxMPIi6Q=; b=DSwsHsZb5v5lkHt/tGIyAsxWkhFnJbKhiWPEH4ilyoMIsMlPa1YawLC/SsrTJPYdXg jg5EuYPH/HQdWqNmCpIRo+oEgSWftte/hcDq1h0MOKfXMdZIyGyYQyU3dkJsaH8BMMft KiAt9xn8RcbjyFHofsFxmvhJpEn0SY5bmsP1c4g+Z1Mv3kScGRoC1xSwE7Gjq7CFHbdK 5lJWk6ZtgxiPK26AVjHTknAKsyaRuLD73YGOxajJvgPT201xkEZ7YHlaDfTB7RSBeEuM KwzLH6pgCNundLI+48c0LdbeQ6o7e3c1YTv3ALBk1RiYYor12bn4JHlcadAPhZxBHpaS 0uRg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id pw2-20020a17090b278200b0021918bc9a47si9861712pjb.174.2022.12.12.08.31.59; Mon, 12 Dec 2022 08:32:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232599AbiLLPwX (ORCPT + 74 others); Mon, 12 Dec 2022 10:52:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232328AbiLLPwW (ORCPT ); Mon, 12 Dec 2022 10:52:22 -0500 Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by lindbergh.monkeyblade.net (Postfix) with SMTP id 2B23763E6 for ; Mon, 12 Dec 2022 07:52:21 -0800 (PST) Received: (qmail 856042 invoked by uid 1000); 12 Dec 2022 10:52:20 -0500 Date: Mon, 12 Dec 2022 10:52:20 -0500 From: Alan Stern To: Oliver Neukum Cc: syzbot , WeitaoWang-oc@zhaoxin.com, arnd@arndb.de, gregkh@linuxfoundation.org, khalid.masum.92@gmail.com, kishon@ti.com, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, syzkaller-bugs@googlegroups.com Subject: Re: [syzbot] KASAN: use-after-free Read in __usb_hcd_giveback_urb (2) Message-ID: References: <0000000000002fc8dc05ef267a9f@google.com> <8e60fa70-15f5-e438-cb49-d3d2281bc975@suse.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <8e60fa70-15f5-e438-cb49-d3d2281bc975@suse.com> X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Dec 12, 2022 at 01:29:24PM +0100, Oliver Neukum wrote: > > > On 08.12.22 18:40, Alan Stern wrote: > > On Thu, Dec 08, 2022 at 03:36:45PM +0100, Oliver Neukum wrote: > > > On 06.12.22 16:38, Alan Stern wrote: > > > It's hard to tell what's really going on. Looking at > > xpad_stop_output(), you see that it doesn't do anything if xpad->type is > > XTYPE_UNKNOWN. Is that what happened here? > > The output anchor in xpad was used. So I have to answer that in the negative. > > I can't figure out where the underlying race is. Maybe it's not > > directly connected with anchors after all. > > > > > As far as I can tell the order we decrease use_count is correct. But: > > > > > > 6ec4147e7bdbd (Hans de Goede 2013-10-09 17:01:41 +0200 1674) usb_anchor_resume_wakeups(anchor); > > > 94dfd7edfd5c9 (Ming Lei 2013-07-03 22:53:07 +0800 1675) atomic_dec(&urb->use_count); > > > > > > Do we need to guarantee memory ordering here? > > > > I don't think we need to do anything more. usb_kill_urb() is careful to > > wait for completion handlers to finish, and we already have > > By checking use_count > > > smp_mb__after_atomic() barriers in the appropriate places to ensure > > proper memory ordering. > > Do we? Looking at __usb_hcd_giveback_urb(): > > usb_unanchor_urb(urb); > > This is an implicit memory barrier > > if (likely(status == 0)) > usb_led_activity(USB_LED_EVENT_HOST); > > /* pass ownership to the completion handler */ > urb->status = status; > /* > * This function can be called in task context inside another remote > * coverage collection section, but kcov doesn't support that kind of > * recursion yet. Only collect coverage in softirq context for now. > */ > kcov_remote_start_usb_softirq((u64)urb->dev->bus->busnum); > urb->complete(urb); > kcov_remote_stop_softirq(); > > usb_anchor_resume_wakeups(anchor); > atomic_dec(&urb->use_count); > /* > * Order the write of urb->use_count above before the read > * of urb->reject below. Pairs with the memory barriers in > * usb_kill_urb() and usb_poison_urb(). > */ > smp_mb__after_atomic(); > > That is the latest time use_count can go to zero. > But what is the earliest time the CPU could reorder setting use_count to zero? > Try as I might the last certain memory barrier I can find in this function > is usb_unanchor_urb(). > That means another CPU can complete usb_kill_urb() before usb_anchor_resume_wakeups() > runs. > > usb_anchor_resume_wakeups(anchor); > > I think we need a memory barrier here, too. > > atomic_dec(&urb->use_count); Okay, how about if this is changed to atomic_dec_return? That puts a full memory barrier both before and after the atomic decrement, so as a bonus we could remove the smp_mb__after_atomic call. Alan Stern