Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp871828rdd; Wed, 10 Jan 2024 01:58:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IGtD6WpV5xc9j1K/iJiXZKpcCMA/d+Pnm+DManJiltwxas2UahrqTHC7ntzydmjUnvWzTLf X-Received: by 2002:a05:6a21:1f30:b0:199:a2a9:4150 with SMTP id ry48-20020a056a211f3000b00199a2a94150mr548829pzb.121.1704880733438; Wed, 10 Jan 2024 01:58:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704880733; cv=none; d=google.com; s=arc-20160816; b=xw815ZrUCtqrtQ83sZh1OL57g0s5ECZyAgzImP/O66YMQQNVZLCoPCknUZWJGl4AgU ORD4PZ0QJDQdLefwGYhvp7NytgOL3Osh73JHPzx3k4odhdR92eml0QwdHNF9PX+FFaTE 7ksGqkAnqnIHtoZ8RzzQg0/3LGpQAEbWT1E33o2jD3O0xg7ohHqQX1TM11rPQLn2ios/ f4ShZvPAVCIAP+zikHhFneMwPVREalA6e+YsovynM2k0qtwW6IImi6INdAhsLu7xyhqt tZKMB7cfX5SBgNqcLEte1cWRMyiCcFH1gyzq+2Hat1BR8d3mhoDaQXgWHPHY2XoHFb63 Gacw== 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=227RG3/s9zkktxfBSvl+wRn+OQ3YUe+UnkVxb7D7O/4=; fh=Zwr7txChiMiJF5Zz2x9S5nWNYXcADsNSHoeb6wab6EQ=; b=t1UIsHICEkWDtO6594iu0JDUOX/HQNBpyqOVqBsNh3YXuJnWOg4TjOWiIQQ/8zAU9f r+Dz6O/sj/iCJkGu+kc64JkJbvWlAcN13XcSzwcXa9En/akXfdUtautsXT2/wSAgJ1H+ Plro00cbfzAWovXmvww5jI5lx+siNh3IRAkHLTcvyoc3pZIS24/kOGO8nYHo8FcME1Ci hH3fCrCqAuGpX7doKrcToexFCxwccWWwiPhaoKUMWcCXyRIPPbJWrcsqgv8B5eiafIOi sTrN9RnWQO7ZxAPozjwWgp5fIzHdhaH60Yh+nADsffccEz5ja0g7TFk1ROmJ0jkQD+j7 zJew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=HBA9Xwqn; spf=pass (google.com: domain of linux-kernel+bounces-21932-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21932-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id cp24-20020a17090afb9800b0028c23fd2935si1019590pjb.150.2024.01.10.01.58.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 01:58:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-21932-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=HBA9Xwqn; spf=pass (google.com: domain of linux-kernel+bounces-21932-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21932-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 5FE4CB20843 for ; Wed, 10 Jan 2024 09:58:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 18BED3F8FB; Wed, 10 Jan 2024 09:58:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=quicinc.com header.i=@quicinc.com header.b="HBA9Xwqn" 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 D3C4B3F8D2; Wed, 10 Jan 2024 09:58: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 (m0279872.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.24/8.17.1.24) with ESMTP id 40A8e59P022347; Wed, 10 Jan 2024 09:58:35 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=227RG3/s9zkktxfBSvl+wRn+OQ3YUe+UnkVxb7D7O/4=; b=HB A9XwqnTOOnn2/kZfUAydF4vZ861zRRIw9XAwcXFdN8c59m0vjmC3G3i+VHtkGeCC 1RIgjm3jO/IPHcRRDC7JFIW0FTn7gzFR5UsVirJ/gnCJTROyIgfhTanRAex8z0jI rL/MwxW778xXmC5y+tyrG6ypSDUAddYTEIasTYt7BXk4kcB6LFi1B/ylfmKapMPY LTynE8aReqXklH/fbp4IueGT0TQXcy7LPY6wJkZmjCInDcUsgWU86I4Ex69bxr5i XwXIuUzMnfXWY1441uGbckCHXxvX6Q4i2mHwLW/wp8XgBtUCbnGOyhR0kQnR6J8p EJrB6zJp2hd7F+FMA6Tw== Received: from nalasppmta02.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3vh85t26rr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 10 Jan 2024 09:58:35 +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 40A9wX0a012035 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 10 Jan 2024 09:58:34 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:58:30 -0800 From: Udipto Goswami To: Greg Kroah-Hartman , Alan Stern CC: Krishna Kurapati , Sergei Shtylyov , , , , Udipto Goswami Subject: [PATCH v5] usb: core: Prevent null pointer dereference in update_port_device_state Date: Wed, 10 Jan 2024 15:28:14 +0530 Message-ID: <20240110095814.7626-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-ORIG-GUID: 5eHEwRHheskovyIeQv2JtkuD3PkE7HjZ X-Proofpoint-GUID: 5eHEwRHheskovyIeQv2JtkuD3PkE7HjZ 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_01,2023-12-07_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 bulkscore=0 malwarescore=0 suspectscore=0 mlxlogscore=793 clxscore=1015 priorityscore=1501 adultscore=0 impostorscore=0 phishscore=0 spamscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2311290000 definitions=main-2401100081 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. 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