Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp4206473ybl; Mon, 13 Jan 2020 09:36:38 -0800 (PST) X-Google-Smtp-Source: APXvYqzjWQUqC6HZBVwrQI9d2r7r7vogRSMr7uuyb3pNfpM6TG8xfbgvToznAYMWyGOn8goxP5Kx X-Received: by 2002:a9d:367:: with SMTP id 94mr13147363otv.329.1578936997920; Mon, 13 Jan 2020 09:36:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578936997; cv=none; d=google.com; s=arc-20160816; b=Me6PZho3XvkoSiauHB2Ir8nP84akGYt/drf42pYf1Ie5ljjd+T8Ng+EEpgx0Llenhm 2Xd0vHvw3LEWtOi7d5kr9DS8BdW2l5S328fjppfYslVTCNzzap0XZQmRtov58pnd+i9u oy5FUFtiehCyqyEXJ1Ilgq6CTFIz2kDCUd7keJT9dhxaHMINgs+FcIYnV2kWxOaFz4tB NGVfk1qswbdV3F8g5OjHNko3xWo3D5LXm7u2fxT4Z7tG8iZtNShUZfkpRD6swFV0kn69 mcF7GZiX0lhyoHUMLSXXkAVhJ7nU7DUGiUxnsOhXi1VwxqHjf9wX0T0bEuy6TPeqin++ r9QQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=1PJ13D+KFCkNQkN2EIudaVpEqGNZODR++Sj1BPuo/ZE=; b=sKS7JjnSaoHwDyW/EKm8TK6A9idHsPVq/M9Y6IFUlzhxT9LNxZv1Dl14ywSU0dcshi P5QcjirfNVMXsIpUxo/nZLwcgOvXQW0TIENnODW594Q9VdRCtbKbxA738LNwLF+kP0qf 48r9mrPKx9aVn+strzepxNN2XX0xsuWbO3JQiK62sy8nBFz9b0LF2vFmb4hDpMuWVdYh CwCZ3I9T9vkdZ1+04BGTjqMxpJJM0pMq+sraoUOnj10pln+MIEBl7entx4P+GCx8/TBw NaGYMj5n2LFs5LN+BtEwiozNNTi0iKOUcl2aovqWn03fiIkjT9AsQ11j//xt/iXnUude aFeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=bHUxH4h7; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 92si7127823otr.25.2020.01.13.09.36.26; Mon, 13 Jan 2020 09:36:37 -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=@google.com header.s=20161025 header.b=bHUxH4h7; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728778AbgAMRej (ORCPT + 99 others); Mon, 13 Jan 2020 12:34:39 -0500 Received: from mail-pl1-f195.google.com ([209.85.214.195]:45569 "EHLO mail-pl1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728664AbgAMRej (ORCPT ); Mon, 13 Jan 2020 12:34:39 -0500 Received: by mail-pl1-f195.google.com with SMTP id b22so4067892pls.12 for ; Mon, 13 Jan 2020 09:34:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=1PJ13D+KFCkNQkN2EIudaVpEqGNZODR++Sj1BPuo/ZE=; b=bHUxH4h7szTdb1IgkxMSHl1LpvDPXrw+WDyMWAPf7c3MB7w02xunVZWtUZCdADY4Hj d4D1Pu0lWUi+wPPmAy3uzD0euIaNsbCAPIz8+Cagp4fmYrOq0UhEk87VexH5DYwPKmAw /Pjdwsn1VaK9QJNhO0HysvG3+1jGwUqp8hNiu+oHH+A/Yx4ohJ/GUdBmu+TzZ8LH4ggx tUUbFCBVCMQPhKtP2uMS/a/VSRMvzKpB7bHmBGAj1zq3ru2OFCsSfI2MhGcHDlpOeQ25 j5RTtLChMAGY9IKx/APGv0aBRW+NqAclwtjgFbkPAyYnEmHvjmSZG2qkQsndOH+MLqeC XYaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=1PJ13D+KFCkNQkN2EIudaVpEqGNZODR++Sj1BPuo/ZE=; b=ALgOExJtZ4bABsd1+tFe91q3H5bR5Qn4eXWe6/48GhnG1el0X9MCuWtyRZCl5Kkx1Y IWOoLDw0b8L+ZyiTq5HedrhOrAjT29oFdLSrfTq2AIyEbt6YGh5XmHLWX+Pw4v+CmFvv mL6UcRR31hl2bY8RrNhwrM1SQdBZ8jxRILbZlYmy54ETOE+wqJE4rohlgTfqzKMIujs1 7ceftT6d8Kuar/WmmsS1vkFSI/BM0G1+szfCR1+La2SXrGVUSEAtMT+9WrkpqHK7IGNy MoGbSntmD42UQh1juYxhRqXR1U4EWm8ya++kaVGo796wVJzs0zDELfaR7iWw02mCS1hR ffzg== X-Gm-Message-State: APjAAAXM8jO4QLSqEbjpnWfJSLQwrH+XxHg//+dMNN8sZomhGM1FO9fm QWSPEaRhGtl4UVrJo1iZ1cv85eCPLtZ2soL2XhlBJA== X-Received: by 2002:a17:902:9889:: with SMTP id s9mr7931281plp.252.1578936878099; Mon, 13 Jan 2020 09:34:38 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Andrey Konovalov Date: Mon, 13 Jan 2020 18:34:27 +0100 Message-ID: Subject: Re: [PATCH v4 1/1] usb: gadget: add raw-gadget interface To: Alan Stern Cc: Greg Kroah-Hartman , Felipe Balbi , USB list , LKML , Jonathan Corbet , Dmitry Vyukov , Alexander Potapenko , Marco Elver Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jan 13, 2020 at 5:50 PM Alan Stern wrote: > > On Mon, 13 Jan 2020, Andrey Konovalov wrote: > > > I've also found an issue, but I'm not sure if that is the bug in Raw > > Gadget, or in the gadget layer (in the former case I'll add this fix > > to v5 as well). What I believe I'm seeing is > > __fput()->usb_gadget_unregister_driver()->usb_gadget_remove_driver()->gadget_unbind() > > racing with dummy_timer()->gadget_setup(). In my case it results in > > gadget_unbind() doing set_gadget_data(gadget, NULL), and then > > gadget_setup() dereferencing get_gadget_data(gadget). > > > > Alan, does it look possible for those two functions to race? Should > > this be prevented by the gadget layer, or should I use some kind of > > locking in my gadget driver to prevent this? > > In your situation this race shouldn't happen, because before > udc->driver->unbind() is invoked we call usb_gadget_disconnect(). If > that routine succeeds -- which it always does under dummy-hcd -- then > there can't be any more setup callbacks, because find_endpoint() will > always return NULL (the is_active() test fails; see the various > set_link_state* routines). So I don't see how you could have ended up > with the race you describe. I've managed to reproduce the race by adding an mdelay() into the beginning of the setup() callback. AFAIU what happens is setup() gets called (and waits on the mdelay()), then unbind() comes in and does set_gadget_data(NULL), and then setup() proceeds, gets NULL through get_gadget_data() and crashes on null-ptr-deref. I've got the same crash a few times after many days of fuzzing, so I assume it can happen without the mdelay() as well. > However, a real UDC might not be able to perform a disconnect under > software control. In that case usb_gadget_disconnect() would not > change the pullup state, and there would be a real possibility of a > setup callback racing with an unbind callback. This seems like a > genuine problem and I can't think of a solution offhand. > > What we would need is a way to tell the UDC driver to stop invoking > gadget callbacks, _before_ the UDC driver's stop callback gets called. > Maybe this should be merged into the pullup callback somehow. > > Alan Stern >