Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp141978rdd; Mon, 8 Jan 2024 22:17:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IEEJdBlV0qXNVt1q6HV7Ia/bHt/5YK2zxlsHSY/kdst1lWLCFUaFUuNnpjrXBVklBF1KaP5 X-Received: by 2002:a17:90a:8c0a:b0:28d:42c4:d3e6 with SMTP id a10-20020a17090a8c0a00b0028d42c4d3e6mr2083249pjo.73.1704781072191; Mon, 08 Jan 2024 22:17:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704781072; cv=none; d=google.com; s=arc-20160816; b=KXlW/pT7pUElqU383dfyIslvUOVbBCvYnO6PXgO5G1WHjjWQidK0s5yx1e9rX0l7qa IQ+NW+gFs1NT20UoXyn/rshP7pW3zEoI524MblsgbgqUcNemHDLga2gvY5VaoyLytPrW DR/rEkIi25uLon15FjOTpqzBs1a59s+KIBD621JU3y9N3uALYjIfiwjEHWmpf9GHhH0F w/h0ZuHrMeLvbfYFgLame7MBCgy8yOelXCjZs3FkrH4515YJ9piGHQJlt2wV+5lBsLMY vVburq72AK0+mD8EHrJ0Q2u40mn+EVJjgpFIHNNb9fxVi8MFjx57LWeCg5CXmd33mNlB kULg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :message-id:date:subject:cc:to:from:dkim-signature; bh=FmGVh3W2g4mUkpYE5hHFAFoo4UsVNaeXCU8Z582FdZQ=; fh=jTrtV/6Lyz50M3+prG1j0ffKbMd0roN0oVseUTTINJI=; b=ndr8TB25xRpo7/83qI8cM+6n7nI7ihBY2CkjlRopTWormDi07A0orzovrwAMgMyDs5 xOIY2A1PVHG1Lq0YC+BXWZviV5p3GudnCvbqPVD+hrQKJ6vNltExEJ4mZcrdmCe72UpM 6Wu0N5vw6ooHlmyE0dqtXPHjINfa43Bq17qydx7Mxmf5//EhK+POq0IJC3fVMWz3YsPu QKsDhMj8esg6/CDyrgKCIFG7SVBbm1Y9Gad5GSAeosE/nkPxRfvcp9CDTQ1ktn/bk7iB JjarteK+LgGUe2giwowVD4O+362GsmVJpmFfbbsUFE71tMP3i9e6GWQyidJ+QwuOYEVy 9Tjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=BzLKO1XI; spf=pass (google.com: domain of linux-kernel+bounces-20427-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-20427-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id e3-20020a17090a9a8300b0028bf7ab5a92si1062840pjp.147.2024.01.08.22.17.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Jan 2024 22:17:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-20427-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=BzLKO1XI; spf=pass (google.com: domain of linux-kernel+bounces-20427-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-20427-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id D53B5285003 for ; Tue, 9 Jan 2024 06:17:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 91A148F40; Tue, 9 Jan 2024 06:17:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=quicinc.com header.i=@quicinc.com header.b="BzLKO1XI" Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 38F5717C2; Tue, 9 Jan 2024 06:17:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=quicinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=quicinc.com Received: from pps.filterd (m0279869.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.24/8.17.1.24) with ESMTP id 4093LIp0019957; Tue, 9 Jan 2024 06:17:28 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= from:to:cc:subject:date:message-id:mime-version:content-type; s= qcppdkim1; bh=FmGVh3W2g4mUkpYE5hHFAFoo4UsVNaeXCU8Z582FdZQ=; b=Bz LKO1XI5i1aSz0Y3Ngdv2pdIZlQ9++EztwQnAZgLEuQaPqM0wSEl1EobAsM1yFgEV t7DT66rc5GGG+Zj8vd8dDxaEcMJqDUy+NeBF0hk2Cr/wjWqwS97RmbPt87yjZ7UF d1xrPm/EJ9a2KAtLhIjoFv58mXE3Zb3jRlL+US2pCxNYF2G5FgqlNq73te0d/DhC Fr4drzodfGqeBzB9bAh8EYHPtjXMbDGbI2Mn2KCjk1BqI43WShNRlkkUtDEYZBIG tpA8j+EVe7b0Ie7o4/Tirtxqi7xFWT5RGWxaZ5MqUd64bccpTYT61ykrnUyLOWjt Lq9jf9AJUsxjSRDLj6Kg== Received: from nalasppmta04.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3vgq2ys23f-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 09 Jan 2024 06:17:27 +0000 (GMT) Received: from nalasex01a.na.qualcomm.com (nalasex01a.na.qualcomm.com [10.47.209.196]) by NALASPPMTA04.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 4096HRkC019581 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 9 Jan 2024 06:17:27 GMT Received: from hu-ugoswami-hyd.qualcomm.com (10.80.80.8) by nalasex01a.na.qualcomm.com (10.47.209.196) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Mon, 8 Jan 2024 22:17:24 -0800 From: Udipto Goswami To: Greg Kroah-Hartman , Alan Stern CC: Krishna Kurapati , , , , Udipto Goswami Subject: [PATCH v3] usb: core: Prevent null pointer dereference in update_port_device_state Date: Tue, 9 Jan 2024 11:47:08 +0530 Message-ID: <20240109061708.26288-1-quic_ugoswami@quicinc.com> X-Mailer: git-send-email 2.17.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain X-ClientProxiedBy: nasanex01a.na.qualcomm.com (10.52.223.231) To nalasex01a.na.qualcomm.com (10.47.209.196) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-GUID: j7V46y80BPb3l08IrWMq5e68WHn60HEV X-Proofpoint-ORIG-GUID: j7V46y80BPb3l08IrWMq5e68WHn60HEV X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.997,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-12-09_02,2023-12-07_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 adultscore=0 lowpriorityscore=0 impostorscore=0 bulkscore=0 malwarescore=0 mlxlogscore=718 clxscore=1015 priorityscore=1501 phishscore=0 mlxscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2311290000 definitions=main-2401090045 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 --- v3: Re-wrote the comment for better context. v2: Introduced comment for the if check & CC'ed stable. drivers/usb/core/hub.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c index ffd7c99e24a3..6b514546e59b 100644 --- a/drivers/usb/core/hub.c +++ b/drivers/usb/core/hub.c @@ -2053,9 +2053,23 @@ 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 procedure of unbinding the hub before running + * the rest of the procedure. This triggers + * hub_disconnect will set the hub's maxchild to 0. + * This would result usb_hub_to_struct_hub in this + * function to return NULL. + * + * Add if check to avoid running into NULL pointer + * de-reference. + */ + 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