From patchwork Mon Dec 11 15:32:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Doug Anderson X-Patchwork-Id: 176744 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7136155vqy; Mon, 11 Dec 2023 07:33:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IExgdPaRniu503+gSE17vGh9vYwEiiBP2pHqA2n66ZOsmjMVFqpWuns/2sQhhqjaoxA5Xwl X-Received: by 2002:a17:902:e746:b0:1d0:6ffd:ce9b with SMTP id p6-20020a170902e74600b001d06ffdce9bmr2148494plf.84.1702308805551; Mon, 11 Dec 2023 07:33:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702308805; cv=none; d=google.com; s=arc-20160816; b=IWd/6tKTJmwutdPnKdZznXZZQvqtJBl8cnjymoWUAnhEtqu4Jgsq59bjQLenAIc08d WRYer64uDzkiICR9d14zSG/r5mf4tI9GIEmJzz4/ZK8cWguqYCL2Z016KawoNm6Poczp JGmCD6upA2WD9RRLGfLC1iebi+pj0jnp2ryCOseIKStr4FysY8VwNN9MDa3h53n4H5te Gepk5UtgZF15HUJD/AFMx6Aed1Eu2WGvz3hOdI475xLXgw5BNrHFQwB9+AUTod1K7kdR 2mqfKdGMvPSbxH8csqpLusRymaEMJqEaDYtELj3/U3I7iN4ByvZQf9FDx4pTfGtwG3Jm g3Zw== 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=u9nyIKtamCpicw2WPRHA84FcbnZ7MvQb17dWhMR1seM=; fh=CoITJ6rEvFgcB4eETgq33y/ggQIHnKrIQdMLu147+pg=; b=OgsNqR+KhyrC5xTceCgcL25LnuwyvkCMEOT1CpuULkAC9Y+VvZ0G2AN7HrMlA+5wz4 MCvXV7R1LV5IHWw1skvV3K8Cfcbv8fEv321xjHyKcDRFcwfdVLUd699i+/rTBtF3Ys7q CVX4tDrMJB3xw07Utm8HYH8zARFJKW9VO04gxymstOTYlW6lSHazK1WjnBC03qqjYMEW iEZ47BrTfmjbpZtpprmh87GHceZWavobKdnGXykEaYM18c7vvOMMl/qZriS0WbIraUah vHY216HbeYFZtrLIGNkuZT5SH2MeffDUWGeV3KGcY4IC1yo2LWRkEXOEraqv99PAzzBZ /FEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=bHOc7iw1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id v4-20020a17090331c400b001d1db5e39b4si6109835ple.172.2023.12.11.07.33.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 07:33:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=bHOc7iw1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (Postfix) with ESMTP id 01AAE8096BAE; Mon, 11 Dec 2023 07:33:23 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343898AbjLKPdE (ORCPT + 99 others); Mon, 11 Dec 2023 10:33:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343865AbjLKPdE (ORCPT ); Mon, 11 Dec 2023 10:33:04 -0500 Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8616ADF for ; Mon, 11 Dec 2023 07:33:10 -0800 (PST) Received: by mail-pg1-x532.google.com with SMTP id 41be03b00d2f7-5be30d543c4so2577181a12.2 for ; Mon, 11 Dec 2023 07:33:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1702308790; x=1702913590; 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=u9nyIKtamCpicw2WPRHA84FcbnZ7MvQb17dWhMR1seM=; b=bHOc7iw1f4LInPU0QY/OUI5mmSiPL9kHRlaIduOgjj64lFRxdRmMH3bCzmKG2qpTS+ jcg99u6XCshn+QB6m/gKVtGCLws1aYJKdq26J0SgtYK5Q2bSPGLnqos6P2Q6XQYENYwB 57KZA6CfkPopVR7wkD0qLH1LSTscBL6xIYqeo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702308790; x=1702913590; 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=u9nyIKtamCpicw2WPRHA84FcbnZ7MvQb17dWhMR1seM=; b=skQ6YGg2HzUk6Qn62HttzpzTVJ0t1/et1HvuGIxvbSPNQ7Ec4VS1uSbFH8SZXPAy83 aoSv/M+TcpYMMoUyITQIbnTEBwS7vuXgGeW/yDEw3ButRcBsdNw3LBkVStddT/+0eczh qgBNQJpM8dFNco9t75+PfUGGALuq//sSMrkQoWHS2qFAjbFcux+598v9Ib+tkk9F7V41 CL7xmrDKAc3eSIKj1Xheo1Z1umLnhCzBSg3puCyp2QvcJWtUDs/7mhl2FAVpsgGGwWju 4kN+Tn/iG/SnRydapFaFuuzdqx1+UtcuhUCSYf6ZaMe4BMf0yKHxVUJwI9fFpAe9h+39 m01g== X-Gm-Message-State: AOJu0Ywo9tk+uCAyCC4qyyiftnUjMz8DywhhTvq5NWNckjycg3sttQjq MSutt795t7q+JBHmJKpwqUIsWQ== X-Received: by 2002:a05:6a20:8f08:b0:18c:3260:e20f with SMTP id b8-20020a056a208f0800b0018c3260e20fmr2612497pzk.33.1702308789981; Mon, 11 Dec 2023 07:33:09 -0800 (PST) Received: from tictac2.mtv.corp.google.com ([2620:15c:9d:2:5c08:e1ed:d922:d30c]) by smtp.gmail.com with ESMTPSA id x1-20020a63cc01000000b005742092c211sm6299572pgf.64.2023.12.11.07.33.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 07:33:09 -0800 (PST) From: Douglas Anderson To: linux-usb@vger.kernel.org, Greg Kroah-Hartman Cc: Eric Dumazet , netdev@vger.kernel.org, "David S . Miller" , Simon Horman , Jakub Kicinski , Hayes Wang , =?utf-8?q?Bj=C3=B8rn_Mork?= , Paolo Abeni , Brian Geffon , Grant Grundler , Alan Stern , Douglas Anderson , linux-kernel@vger.kernel.org Subject: [PATCH v3] usb: core: Fix crash w/ usb_choose_configuration() if no driver Date: Mon, 11 Dec 2023 07:32:41 -0800 Message-ID: <20231211073237.v3.1.If27eb3bf7812f91ab83810f232292f032f4203e0@changeid> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog MIME-Version: 1.0 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 fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Mon, 11 Dec 2023 07:33:23 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785000157760958821 X-GMAIL-MSGID: 1785000157760958821 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()") Reviewed-by: Alan Stern Signed-off-by: Douglas Anderson --- Changes in v3: - Add comment in code, as per Alan. Changes in v2: - Return immediately if no driver, as per Alan. drivers/usb/core/generic.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/drivers/usb/core/generic.c b/drivers/usb/core/generic.c index dcb897158228..b134bff5c3fe 100644 --- a/drivers/usb/core/generic.c +++ b/drivers/usb/core/generic.c @@ -59,7 +59,16 @@ 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 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. + */ + if (!udev->dev.driver) + return -1; + udriver = to_usb_device_driver(udev->dev.driver); if (usb_device_is_owned(udev)) return 0;