Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp855436rdd; Wed, 10 Jan 2024 01:15:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IEjd7Iv4iAENnhz9xP84Uy5hrbjhjPj/TqKkuPPIK6I/WvGGv3/DNA0j7vV5RLyeh/+ZLab X-Received: by 2002:a05:622a:104e:b0:429:996a:6ad7 with SMTP id f14-20020a05622a104e00b00429996a6ad7mr1002835qte.3.1704878117878; Wed, 10 Jan 2024 01:15:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704878117; cv=none; d=google.com; s=arc-20160816; b=RrNUU+6M/fE/gHUppsB0970UMHZXgHAsCWyhAtLI6BC7yR5ELlzP6aqdmnRrje7S2k VHOcpb4CKSgDW21F8aOPcACri9rCZT4PilAYb2DYVHWoPLCZ0lZ3XXR6qCcG9X3Y1ml4 ZFE3vOqV4ffkJi1njrQPccK/R4LGqjhx1eluWVAC6/XkYuRqQO0R0bsgLke+NrPj9Apq amEH+Uv0sT8zBsVnXwvuXQH28m6JMbezeoJwP8xDRzIrMI6GSfMcmpIChetG56xo3cTv KYOAxoGEzgWrHfpff8AbPJ/I7YIAAjqFV8lW7m+U6odQqprSwN/DuJQtFXUm/hVTuE/3 hhEQ== 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=yAhA8Oceyu1djk0D/e5KpYlLpin5FLpfjCsMWaRJTFw=; fh=Zwr7txChiMiJF5Zz2x9S5nWNYXcADsNSHoeb6wab6EQ=; b=R9Ig1QlnoGGACELcxXtKdMDspMxIggfStqQOsUbtv8XvCYTmfjW42ajBVVODGm+Hj/ hnPS1j+lnBsN+fhJkyGAZWooExmbC0cDBoXvz1bZ0TBYXuL9jiry8YM6NPm/hrX4Xxe4 Ho8BbBOM5URPWswxf5OhUDEpxgmc+guh+2u4BPWIbmSVeUfgPKM8YhgMwY0MjxGLZilB SjiFh8XJNZdG9vKNia9njRW9pb+0FWaXA/F06NN5COyw3nCbLtxsVso6BfFc3w+ybjQr +BVoq0dX6/PEUqfs3wrQ3RJNaGF+2+4ikeRSdHKNycO9x/HN1CN3bRBnCUT7W5Uw1nQo PXJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=niAPxXdT; spf=pass (google.com: domain of linux-kernel+bounces-21884-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21884-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id 7-20020ac84e87000000b00429b6d3ab93si451513qtp.484.2024.01.10.01.15.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 01:15:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-21884-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=niAPxXdT; spf=pass (google.com: domain of linux-kernel+bounces-21884-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21884-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id A24F01C20D7E for ; Wed, 10 Jan 2024 09:15:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 247723EA85; Wed, 10 Jan 2024 09:14:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=quicinc.com header.i=@quicinc.com header.b="niAPxXdT" Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.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 EAFE73D57E; Wed, 10 Jan 2024 09:14:46 +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 (m0279867.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.24/8.17.1.24) with ESMTP id 40A8cYUm020369; Wed, 10 Jan 2024 09:14:43 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=yAhA8Oceyu1djk0D/e5KpYlLpin5FLpfjCsMWaRJTFw=; b=ni APxXdT8ExeUBnMttdN4GJMHaeL1ytoT8dE2Hil1QiyEFOsgWG6wfPu1HyOMV48sq qLk9AV9SbDpVRCLRzzcY0U0JCCQc53/QSkHKTm7D/Xuz8XV21BCrYfnBiSB2Ja4Y Z18IhdV4/buwRsFevYK7iyyxCqmFdqRvLiMuWko+/Dhwek9jrn4gwDkVahaagf4y jhJy50CBSdi1BaUQEgW74TiGUCavPU2VjSaaN48SatDjT5oV5H3/aL4M4o06evGv AWWeXa+O6OdNTG6CY/G8XZ5Zk9YbIJPx3VKv595xj8fMaaI0ByaGe4SvrczB+2is uEVzC9534q4SZ+h2jQ0A== Received: from nalasppmta02.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3vhjh2rq4y-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 10 Jan 2024 09:14:43 +0000 (GMT) Received: from nalasex01a.na.qualcomm.com (nalasex01a.na.qualcomm.com [10.47.209.196]) by NALASPPMTA02.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 40A9Eg0Z014725 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 10 Jan 2024 09:14:42 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; Wed, 10 Jan 2024 01:14:39 -0800 From: Udipto Goswami To: Greg Kroah-Hartman , Alan Stern CC: Krishna Kurapati , Sergei Shtylyov , , , , Udipto Goswami Subject: [PATCH v4] usb: core: Prevent null pointer dereference in update_port_device_state Date: Wed, 10 Jan 2024 14:44:22 +0530 Message-ID: <20240110091422.25347-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: nasanex01b.na.qualcomm.com (10.46.141.250) 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-ORIG-GUID: Z-F7FYVwYTcqWfTAMF69RFMQ-nVvd2_I X-Proofpoint-GUID: Z-F7FYVwYTcqWfTAMF69RFMQ-nVvd2_I 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 lowpriorityscore=0 mlxlogscore=659 clxscore=1015 bulkscore=0 malwarescore=0 spamscore=0 phishscore=0 priorityscore=1501 adultscore=0 impostorscore=0 suspectscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2311290000 definitions=main-2401100075 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 --- 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. drivers/usb/core/hub.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c index ffd7c99e24a3..5ba1875e6bf4 100644 --- a/drivers/usb/core/hub.c +++ b/drivers/usb/core/hub.c @@ -2053,9 +2053,22 @@ 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 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 + * usb_hub_to_struct_hub returning 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