Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp1064486rdd; Wed, 10 Jan 2024 07:40:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IFLSOdOJvlaQeffJRFQEuAf8SzR3kgY91xmpIVaz96bOrAW7pKzKJDuklFTuv/gX/kK4kvD X-Received: by 2002:a17:907:98c:b0:a23:1e0d:565b with SMTP id bf12-20020a170907098c00b00a231e0d565bmr967826ejc.1.1704901225349; Wed, 10 Jan 2024 07:40:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704901225; cv=none; d=google.com; s=arc-20160816; b=Hs9yfVik1CVmPT0K1WPTLv382jbz4I67fjXajVJcaaBKRMHMDH0aBeufD2HI8cEfRa WbBpv0H2l3YSzpHuwZqRQbBfKOfKHP1VWpUalBgL2dr1hRPqifAoniBV1LyxxwdM/Plb 4VnRyppjBf+vbeJhcv29WcsiYANXZpiPOTYKgIxbyeSvXDtXy3ihP0p7VClKS4kHuhx+ AHpitCivHltvbPrH8pTiVVAocGVZ4G4lQzUm7dC3QLee911054VLv1F6iZs5pAZawrMF XHI3cszhLIntpwCQ5pQk6eBNbbX8yP4XHkiIzcIhIHY8bzxU0hrrfbm+lC5fTV1bXit1 ZUwg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date; bh=/7rVYfKdeDplt//SlNNLB38m5bb9JvF+j9mew8zXA3U=; fh=yuhdH7jjz3rKaI6Ennwc71I4ftzHIkDe+4RTsSvKrmo=; b=1AZF3sMAJOK8H2oJKvYsRHKpWjw+zqfff+elFY0r0JhcbtpKpmYlQP1fkikcuhAW/R 4w9GRv5fa1i7AawK3cN+cMlpZBVP5UuWn7j2BB9kRELscA0aIo7lqf+EqI488UKM5Ver VcoZC8iynloYaZE7PBM9VOpo2zJ7yQ3Q7X7CKui34rMwqEYqfvE+Tbu5aNfmEWzI+6D/ kxoObeI3b1qwZ+OSABV4j6Zgw/CJhlMUQFdWmP21WemCWU9cTww5q1H4eGJmgLUwuIAm NrNPlOsvRJSoOz2cb+8iraRmN4tXTNatDV5H3GQQK5QMLoealLq4tXkUwZAMe/3aMPCs RVNQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-22436-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-22436-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=harvard.edu Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id k26-20020a170906055a00b00a2ad1086a3dsi1822121eja.90.2024.01.10.07.40.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 07:40:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-22436-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-22436-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-22436-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=harvard.edu Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id CD24E1F277A7 for ; Wed, 10 Jan 2024 15:40:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6C31C4C3D4; Wed, 10 Jan 2024 15:39:23 +0000 (UTC) Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by smtp.subspace.kernel.org (Postfix) with SMTP id 2250A4C3C4 for ; Wed, 10 Jan 2024 15:39:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=rowland.harvard.edu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=netrider.rowland.org Received: (qmail 568340 invoked by uid 1000); 10 Jan 2024 10:39:13 -0500 Date: Wed, 10 Jan 2024 10:39:13 -0500 From: Alan Stern To: Udipto Goswami Cc: Greg Kroah-Hartman , Krishna Kurapati , Sergei Shtylyov , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH v5] usb: core: Prevent null pointer dereference in update_port_device_state Message-ID: <20abaa31-9558-4a56-9d34-4407f175cada@rowland.harvard.edu> References: <20240110095814.7626-1-quic_ugoswami@quicinc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240110095814.7626-1-quic_ugoswami@quicinc.com> On Wed, Jan 10, 2024 at 03:28:14PM +0530, Udipto Goswami wrote: > Currently, the function update_port_device_state gets the usb_hub from > udev->parent by calling usb_hub_to_struct_hub. > However, in case the actconfig or the maxchild is 0, the usb_hub would > be NULL and upon further accessing to get port_dev would result in null > pointer dereference. > > Fix this by introducing an if check after the usb_hub is populated. > > Fixes: 83cb2604f641 ("usb: core: add sysfs entry for usb device state") > Cc: stable@vger.kernel.org > Signed-off-by: Udipto Goswami > --- > v5: Addressed nit picks in commit and the comment. > v4: Fixed minor mistakes in the comment. > v3: Re-wrote the comment for better context. > v2: Introduced comment for the if check & CC'ed stable. Reviewed-by: Alan Stern > drivers/usb/core/hub.c | 16 +++++++++++++--- > 1 file changed, 13 insertions(+), 3 deletions(-) > > diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c > index ffd7c99e24a3..48409d51ea43 100644 > --- a/drivers/usb/core/hub.c > +++ b/drivers/usb/core/hub.c > @@ -2053,9 +2053,19 @@ static void update_port_device_state(struct usb_device *udev) > > if (udev->parent) { > hub = usb_hub_to_struct_hub(udev->parent); > - port_dev = hub->ports[udev->portnum - 1]; > - WRITE_ONCE(port_dev->state, udev->state); > - sysfs_notify_dirent(port_dev->state_kn); > + > + /* > + * The Link Layer Validation System Driver (lvstest) > + * has a test step to unbind the hub before running the > + * rest of the procedure. This triggers hub_disconnect > + * which will set the hub's maxchild to 0, further > + * resulting in usb_hub_to_struct_hub returning NULL. > + */ > + if (hub) { > + port_dev = hub->ports[udev->portnum - 1]; > + WRITE_ONCE(port_dev->state, udev->state); > + sysfs_notify_dirent(port_dev->state_kn); > + } > } > } > > -- > 2.17.1 >