Message ID | 20231211070808.v2.1.If27eb3bf7812f91ab83810f232292f032f4203e0@changeid |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7119817vqy; Mon, 11 Dec 2023 07:11:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IE9qRTJkycXAMr4+9KsFMqO3Xf6bPkXl2Ju9rTCe0JhMOUUcnEDSl4RrP/k0ROo5Lxt75kC X-Received: by 2002:a17:90b:890:b0:286:6cc0:b90b with SMTP id bj16-20020a17090b089000b002866cc0b90bmr1572320pjb.66.1702307485439; Mon, 11 Dec 2023 07:11:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702307485; cv=none; d=google.com; s=arc-20160816; b=i4TCQue4kPpZHnSYfY/2PhNnh2ErXbodm97cAGPkCpNM25LufcYoEOJplDIkTwfSn3 UhbKFMkTwM1qHBHRhISa/TXqLefI4vBYwRaSghg/BM4hzIZghl/4jBq/jswTCS8UqDEv JrLfi3DexvjJAMxQM1TnFRYdKqkKBRBUpO1lykd1+snvxkzLWvvgQU0W7LreQTl0KlIy mRIJhIh0pdJzZbhiAA+mNEQXgB171uzTtppBDdKNVmq70FPYkGxdcVOtu7d3Ck32GdZq SpVzetD46EqdiS6iXrTV7LR4yvrKTe2WNVS+XC6WzFEM/7tXH5ejvjh/1DiW9ExNOFIU PgCw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=QjZ1Q3i+wwEmoaOAEivvc7ulbH4UVbA72KsZePqHS/s=; fh=1w0lmpZhxgl0SCp3cGam5hTE8fGx1yJbWfxvhAAYzWc=; b=t3gmffNsYcELQNyIAHvPqM78YBEZ0XR/6ch4vrOV7HiUcrqpWwEADIBlB6iY32BGJn 0GYRa8CxXYeIEGZrQyOBsso0Xfz5mJlUKvUxeGnr6YeKaoeeROVTCx7kVpTNtXeP67aF Odwrin02Yk9z0PXpSyj3//XTCbmIOHKGOBBpaqVKAdmqILYCpUwFand4WA/9uswW0jt5 cF9MCcbwB62gzLwsat1APgj24iofkLKDmuJyICvWWzpRoN0FE8aWpY3Fk5jyeLmjrxGf ezBs7rmuZmA/csQ97njgY2yfxGQIOPAAt6w9sM6rHedPJ3PpXl6Fyko0hyLUhKtrIUu5 b+pg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=HOuflSDF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id n12-20020a65450c000000b00565f0e91894si5884293pgq.394.2023.12.11.07.11.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 07:11:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=HOuflSDF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 354BF80A9AA6; Mon, 11 Dec 2023 07:11:22 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343992AbjLKPLN (ORCPT <rfc822;dexuan.linux@gmail.com> + 99 others); Mon, 11 Dec 2023 10:11:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343701AbjLKPLK (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 11 Dec 2023 10:11:10 -0500 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03E3ED8 for <linux-kernel@vger.kernel.org>; Mon, 11 Dec 2023 07:11:14 -0800 (PST) Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1d0bcc0c313so24686615ad.3 for <linux-kernel@vger.kernel.org>; Mon, 11 Dec 2023 07:11:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1702307473; x=1702912273; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=QjZ1Q3i+wwEmoaOAEivvc7ulbH4UVbA72KsZePqHS/s=; b=HOuflSDFImwQKauYTvTgDYF3QpXRZVnrThFvyJyMCZ1o4kJUJnqppZbYcddW2t+cGE +Q8mqfPOdC5SmVxey9C1YiXiL2Ova2ht6hGA6rnX74X3GJCTCEEWW+lGv9tJu/HR7L3M /LWmrEb42+9zVbmB/HvszMEjHqWoYb4G/jX/I= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702307473; x=1702912273; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=QjZ1Q3i+wwEmoaOAEivvc7ulbH4UVbA72KsZePqHS/s=; b=sOGBIIr3C1PkU98qKkcW5F90vq50hFru2jcGstD4usYCq+VYdaOv/NpIqKeBhD3F0s PDi+MIr3PMOevKSJKR558F51y7G34PCIVRGozK1PuIamBWlOLpRBZUxYqL26z0KnZ6A+ EXJvMkglHGSny5YX4nGH/9QewVVthMJ20rBjLLji3MyW4r88TaZpUU9hXqWEMmx9YVzI D9V0ZKvPxzN3w/REUa4N2I+/dBY19CKVBdg+5Xs9I3qIUb9Ijh0bC6RalphwxK6EF3GE nY1lYyvNmVfnl0RzVUlYdZdv4ch8ov8Ip7Gp85HcdJPsUIKaBlg3b/b2dqwm3DDz9R3K Cs9w== X-Gm-Message-State: AOJu0Yy9c++mNB0iHvenvbQ/+fovJy+iy4IDT75LgaPKo7phFrZiYAig hfFPJbMJgpPM1GI4c+voWk3OYg== X-Received: by 2002:a17:902:e74e:b0:1d0:6d5d:5e4d with SMTP id p14-20020a170902e74e00b001d06d5d5e4dmr1735616plf.59.1702307473507; Mon, 11 Dec 2023 07:11:13 -0800 (PST) Received: from tictac2.mtv.corp.google.com ([2620:15c:9d:2:5c08:e1ed:d922:d30c]) by smtp.gmail.com with ESMTPSA id a18-20020a170902ecd200b001d058ad8770sm6787897plh.306.2023.12.11.07.11.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 07:11:12 -0800 (PST) From: Douglas Anderson <dianders@chromium.org> To: linux-usb@vger.kernel.org, Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: netdev@vger.kernel.org, =?utf-8?q?Bj=C3=B8rn_Mork?= <bjorn@mork.no>, Eric Dumazet <edumazet@google.com>, Hayes Wang <hayeswang@realtek.com>, Brian Geffon <bgeffon@google.com>, "David S . Miller" <davem@davemloft.net>, Jakub Kicinski <kuba@kernel.org>, Simon Horman <horms@kernel.org>, Alan Stern <stern@rowland.harvard.edu>, Grant Grundler <grundler@chromium.org>, Paolo Abeni <pabeni@redhat.com>, Douglas Anderson <dianders@chromium.org>, linux-kernel@vger.kernel.org Subject: [PATCH v2] usb: core: Fix crash w/ usb_choose_configuration() if no driver Date: Mon, 11 Dec 2023 07:08:14 -0800 Message-ID: <20231211070808.v2.1.If27eb3bf7812f91ab83810f232292f032f4203e0@changeid> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Mon, 11 Dec 2023 07:11:22 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784998773775670612 X-GMAIL-MSGID: 1784998773775670612 |
Series |
[v2] usb: core: Fix crash w/ usb_choose_configuration() if no driver
|
|
Commit Message
Doug Anderson
Dec. 11, 2023, 3:08 p.m. UTC
It's possible that usb_choose_configuration() can get called when a
USB device has no driver. In this case the recent commit a87b8e3be926
("usb: core: Allow subclassed USB drivers to override
usb_choose_configuration()") can cause a crash since it dereferenced
the driver structure without checking for NULL. Let's add a check.
A USB device with no driver is an anomaly, so make
usb_choose_configuration() return immediately if there is no driver.
This was seen in the real world when usbguard got ahold of a r8152
device at the wrong time. It can also be simulated via this on a
computer with one r8152-based USB Ethernet adapter:
cd /sys/bus/usb/drivers/r8152-cfgselector
to_unbind="$(ls -d *-*)"
real_dir="$(readlink -f "${to_unbind}")"
echo "${to_unbind}" > unbind
cd "${real_dir}"
echo 0 > authorized
echo 1 > authorized
Fixes: a87b8e3be926 ("usb: core: Allow subclassed USB drivers to override usb_choose_configuration()")
Signed-off-by: Douglas Anderson <dianders@chromium.org>
---
Changes in v2:
- Return immediately if no driver, as per Alan.
drivers/usb/core/generic.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
Comments
On Mon, Dec 11, 2023 at 07:08:14AM -0800, Douglas Anderson wrote: > It's possible that usb_choose_configuration() can get called when a > USB device has no driver. In this case the recent commit a87b8e3be926 > ("usb: core: Allow subclassed USB drivers to override > usb_choose_configuration()") can cause a crash since it dereferenced > the driver structure without checking for NULL. Let's add a check. > > A USB device with no driver is an anomaly, so make > usb_choose_configuration() return immediately if there is no driver. > > This was seen in the real world when usbguard got ahold of a r8152 > device at the wrong time. It can also be simulated via this on a > computer with one r8152-based USB Ethernet adapter: > cd /sys/bus/usb/drivers/r8152-cfgselector > to_unbind="$(ls -d *-*)" > real_dir="$(readlink -f "${to_unbind}")" > echo "${to_unbind}" > unbind > cd "${real_dir}" > echo 0 > authorized > echo 1 > authorized > > Fixes: a87b8e3be926 ("usb: core: Allow subclassed USB drivers to override usb_choose_configuration()") > Signed-off-by: Douglas Anderson <dianders@chromium.org> > --- > > Changes in v2: > - Return immediately if no driver, as per Alan. > > drivers/usb/core/generic.c | 6 +++++- > 1 file changed, 5 insertions(+), 1 deletion(-) > > diff --git a/drivers/usb/core/generic.c b/drivers/usb/core/generic.c > index dcb897158228..2be1e8901e2f 100644 > --- a/drivers/usb/core/generic.c > +++ b/drivers/usb/core/generic.c > @@ -59,7 +59,11 @@ int usb_choose_configuration(struct usb_device *udev) > int num_configs; > int insufficient_power = 0; > struct usb_host_config *c, *best; > - struct usb_device_driver *udriver = to_usb_device_driver(udev->dev.driver); > + struct usb_device_driver *udriver; > + > + if (!udev->dev.driver) > + return -1; This is a rather unusual condition. It would be good to put a comment just before the test, explaining that if a USB device (not an interface) doesn't have a driver then the kernel has no business trying to select or install a configuration for it. Along with the comment, feel free to add: Reviewed-by: Alan Stern <stern@rowland.harvard.edu> Alan Stern > + udriver = to_usb_device_driver(udev->dev.driver); > > if (usb_device_is_owned(udev)) > return 0; > -- > 2.43.0.472.g3155946c3a-goog >
Hi, On Mon, Dec 11, 2023 at 7:27 AM Alan Stern <stern@rowland.harvard.edu> wrote: > > On Mon, Dec 11, 2023 at 07:08:14AM -0800, Douglas Anderson wrote: > > It's possible that usb_choose_configuration() can get called when a > > USB device has no driver. In this case the recent commit a87b8e3be926 > > ("usb: core: Allow subclassed USB drivers to override > > usb_choose_configuration()") can cause a crash since it dereferenced > > the driver structure without checking for NULL. Let's add a check. > > > > A USB device with no driver is an anomaly, so make > > usb_choose_configuration() return immediately if there is no driver. > > > > This was seen in the real world when usbguard got ahold of a r8152 > > device at the wrong time. It can also be simulated via this on a > > computer with one r8152-based USB Ethernet adapter: > > cd /sys/bus/usb/drivers/r8152-cfgselector > > to_unbind="$(ls -d *-*)" > > real_dir="$(readlink -f "${to_unbind}")" > > echo "${to_unbind}" > unbind > > cd "${real_dir}" > > echo 0 > authorized > > echo 1 > authorized > > > > Fixes: a87b8e3be926 ("usb: core: Allow subclassed USB drivers to override usb_choose_configuration()") > > Signed-off-by: Douglas Anderson <dianders@chromium.org> > > --- > > > > Changes in v2: > > - Return immediately if no driver, as per Alan. > > > > drivers/usb/core/generic.c | 6 +++++- > > 1 file changed, 5 insertions(+), 1 deletion(-) > > > > diff --git a/drivers/usb/core/generic.c b/drivers/usb/core/generic.c > > index dcb897158228..2be1e8901e2f 100644 > > --- a/drivers/usb/core/generic.c > > +++ b/drivers/usb/core/generic.c > > @@ -59,7 +59,11 @@ int usb_choose_configuration(struct usb_device *udev) > > int num_configs; > > int insufficient_power = 0; > > struct usb_host_config *c, *best; > > - struct usb_device_driver *udriver = to_usb_device_driver(udev->dev.driver); > > + struct usb_device_driver *udriver; > > + > > + if (!udev->dev.driver) > > + return -1; > > This is a rather unusual condition. It would be good to put a comment > just before the test, explaining that if a USB device (not an interface) > doesn't have a driver then the kernel has no business trying to select > or install a configuration for it. > > Along with the comment, feel free to add: > > Reviewed-by: Alan Stern <stern@rowland.harvard.edu> Thanks for the quick reply. I've added your comment almost verbatim to the code and sent out a quick v3 with your tag: https://lore.kernel.org/r/20231211073237.v3.1.If27eb3bf7812f91ab83810f232292f032f4203e0@changeid
diff --git a/drivers/usb/core/generic.c b/drivers/usb/core/generic.c index dcb897158228..2be1e8901e2f 100644 --- a/drivers/usb/core/generic.c +++ b/drivers/usb/core/generic.c @@ -59,7 +59,11 @@ int usb_choose_configuration(struct usb_device *udev) int num_configs; int insufficient_power = 0; struct usb_host_config *c, *best; - struct usb_device_driver *udriver = to_usb_device_driver(udev->dev.driver); + struct usb_device_driver *udriver; + + if (!udev->dev.driver) + return -1; + udriver = to_usb_device_driver(udev->dev.driver); if (usb_device_is_owned(udev)) return 0;