Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp4211579ybl; Mon, 13 Jan 2020 09:41:56 -0800 (PST) X-Google-Smtp-Source: APXvYqx8thAiRKKypYB7ymLBpE8nXB5vjxd12ZPrLWDzs8d/H/kXiyyJtbPkaUJVGH0hVj3l0I7I X-Received: by 2002:a05:6808:486:: with SMTP id z6mr13933734oid.117.1578937316115; Mon, 13 Jan 2020 09:41:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578937316; cv=none; d=google.com; s=arc-20160816; b=N6MV8cBzTKygq5ALYC1KRMG3lklMuJznLO3SuzQyPw411Ifbw7THiJTAWCw41kI6K1 m/x7dtzsOUZJcefkCvtaZkLQtLCVgpNoLc3PriQLF/dENbnF9EgPkQobYbUoFXXy0/Xi JTO6aIyUBmqznuxpl6Ea1WqPgrEVm57LwLpsHd4Ff08G0iVyWNCK9p7KBUshqG5Wi4tQ wT+ZEOl4Dah/V/p7lB3mAnyrtUbZm6fzaUEMyt+eVYcm+uoeeb5sedkz35mhpNG8TMNb 62GzPHQsRlE1hholGXQXEK5x4ZIEZfUcxPqbTiBucD0XKgAR61EBSeM9dIafrmWQ0P9Y Mo4w== 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=BVAS/5gDM35E+vwqcWj4iTNingff3wxRTXoRrLJ2q00=; b=kjQDErhY5fOsW3L1BUfHrjGecdDt8qtT16tw6qni5HZU13Efe5swgES++PYbQMzJfV p/RYhD/g/pP8BL7R3CiEJm/dVOXV6s6Z2b8ObtVjdKoPo6/1Ek5HVDMu2BtYnBhJmOkp /i3hAsaOTIadSm5F6FcM7yhAnkcP8+H1cNnY9E3BvpA25BigY8OIqTC84+MJFrsqjzzP fLGOQnqrM+kw7DaY5DaeCrUksxP/2MEQ0mtmKCB4QtZaNJDuhsh+hy5wjdfQPgDUyjBd VlgE4JrPVRRiIEHnhrsRTGiPitxPRpjucAvsk+HcJcrYlNbmNxip62GsBUrceWZXBW4H Nn7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=PbUozQIi; 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 i198si6392589oib.111.2020.01.13.09.41.43; Mon, 13 Jan 2020 09:41:56 -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=PbUozQIi; 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 S1728731AbgAMRko (ORCPT + 99 others); Mon, 13 Jan 2020 12:40:44 -0500 Received: from mail-pf1-f193.google.com ([209.85.210.193]:44172 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726109AbgAMRkn (ORCPT ); Mon, 13 Jan 2020 12:40:43 -0500 Received: by mail-pf1-f193.google.com with SMTP id 195so5183825pfw.11 for ; Mon, 13 Jan 2020 09:40:43 -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=BVAS/5gDM35E+vwqcWj4iTNingff3wxRTXoRrLJ2q00=; b=PbUozQIiNWcYiivH2d8I/Q2/mmC03ElGYL8BeMeEiv7rT62VOBMRJhYp6DUZdsTLzB i2aruOVld4neB7dXlGb+PPyKcrcD2QBO7p1lE3z6d08KbBBzfy/of7wNgpej6eO2NiRH qyJAc0F8pnU6MMVWkY81NPm7xe2OA+QsZbE4LIIRcqIBn0OPKdrPC1Q8XPCsPpyspboN sUr3+8leOwyY89F/oSFv3cGrNSHmUlynYX2bOUbH+MV0uOTI5ztlTTSvZTB7f47EB940 9BYe61kQnH0QOdtBTCSmRfuc3h358BlzcWmxeCAZgWOqtNXs9cMTpai7VPRj19osxYe3 hHUw== 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=BVAS/5gDM35E+vwqcWj4iTNingff3wxRTXoRrLJ2q00=; b=Vd7Ih9nz5QfrdOIM/GMAE91/MY7CqJwzgnuBKxtDLP+jYdEY/53EWdQjUk+M8bOieK lUqJ7pu7GfonjsPcnDIUSns7a9F4ppP6klxj8SBJ9FGe7Bs+TvJ1/pfJcJ29fCZVZELM hWzexLbSBLdU+TrVOnD2qA5kA+93qATzOZCBiSijZwqKHztn+CFkLfcWhIzGgpblpsDx vGQNKFcfmbtkw+4ZaX3T7/QnJS+J9pgjoxjWo6QjOZINUt/FkVT7kZ18Wl7RXaLohbFH OKS0epg3vVA8EdwIP+0vxqvhLLWXrLfJmdPCi8Eb3KHQVIyrDerqJBTCfqDIunnb2N9V jU8A== X-Gm-Message-State: APjAAAV9GcGdCOs3iRYR1sCnfrwgymWfEkIJY9Mh73dtniPIVhUipIie enBdkmsFiSKtwsJdo3gY3ujq8SZgPoYZnepkDGhrQQ== X-Received: by 2002:a63:d906:: with SMTP id r6mr22112095pgg.440.1578937242731; Mon, 13 Jan 2020 09:40:42 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Andrey Konovalov Date: Mon, 13 Jan 2020 18:40:31 +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 6:34 PM Andrey Konovalov wrote: > > 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. Perhaps for the dummy driver we need to wait for setup() to finish if it's being executed and then stop the dummy timer in dummy_pullup()?