Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp213700rdd; Tue, 9 Jan 2024 01:31:37 -0800 (PST) X-Google-Smtp-Source: AGHT+IHtUzCMIlrwPE6vYHKbKEWa78Ury9FZROAgLRzt3ANUVCAdirB/jo5AmcJoDizzfMFkZfYp X-Received: by 2002:a17:903:18c:b0:1d5:107f:7ba4 with SMTP id z12-20020a170903018c00b001d5107f7ba4mr554047plg.28.1704792696844; Tue, 09 Jan 2024 01:31:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704792696; cv=none; d=google.com; s=arc-20160816; b=ODAe8ieiDMWl+k0ygU4fT7gMb1lWGLtYYTgvkIcCp8ZZRN/9JrFAiBGh8vBy8XOi4F Pezihy1uHxUjsCKUabtFB0YpJh9ehMwh6Py8AZM0kRZ8MMZ1e68Yov5KmZUsNZ53rpZX K6ueoDpz2nQtXEmWkozcQBr5gcum5sg1d+KWK3CNrKaB7AzqCpjfmYyfrDZVShEXRepz PPyXSfjY88nze3f90VWNWycHLpHkQNLh17szgWResPiSGHBFw08KfvvKdoMJL0f8uW6p V/JkYlzNnxWPot8u5ge9GI4xdUAVbCYsksb4gKgALxunZCYMyTq8IdfRddP5fU2SQecs rUqw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:content-language:in-reply-to:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=fdolU0EUOihiv4P24pkLi7UHCL7sN9mCnSKzJVpdC60=; fh=oR6XNLry3q0M9P4ja0Fz8XhBESr9G0/c7tL+Q6bWDt4=; b=kAwvZ5ycYkxp1ZuBFoHtBabwwUQHOFuruiA2bxsXIr6rDJHhRjP2WL5bNSHvwNb5Ou aDUZIcaEndy6rNemCMMZnka305nWKhRxOJb0p+kYBBAUARERMqkYyeimaSYUskIFESYD Ac9yIOh8NZNYojqO7y3LaDlnDHgobCOxkvlCImv0M8hQD1ywaSZ3TTZzHLiod4oNzRXW y7UY9+VHGlXCz7jGn5K59BbHvB7peNV4iFTvkcKFXtGRgWIu/inXputMv/gV07Z2EKQY IqTdK9St9DVNY9qAbZ9a5mvBcsRhlZKO+ycuAAsCFbGyolbKuMycCZFVakGxgfPXyjOt 4S3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=OhB5fRQ5; spf=pass (google.com: domain of linux-kernel+bounces-20666-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-20666-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id u22-20020a63f656000000b005c65ed0f692si1213704pgj.141.2024.01.09.01.31.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 01:31:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-20666-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=OhB5fRQ5; spf=pass (google.com: domain of linux-kernel+bounces-20666-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-20666-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.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 80C9E283686 for ; Tue, 9 Jan 2024 09:31:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BB76133CFF; Tue, 9 Jan 2024 09:31:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="OhB5fRQ5" Received: from mail-lf1-f50.google.com (mail-lf1-f50.google.com [209.85.167.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 666A42E650; Tue, 9 Jan 2024 09:31:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lf1-f50.google.com with SMTP id 2adb3069b0e04-50e741123acso2805933e87.0; Tue, 09 Jan 2024 01:31:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704792682; x=1705397482; darn=vger.kernel.org; h=content-transfer-encoding:content-language:in-reply-to:mime-version :user-agent:date:message-id:from:references:cc:to:subject:from:to:cc :subject:date:message-id:reply-to; bh=fdolU0EUOihiv4P24pkLi7UHCL7sN9mCnSKzJVpdC60=; b=OhB5fRQ5LguOXWFMeVnEZISpWGj4ao7Xy3+KOeHoMauumDau18q979T+bs2woHCSx4 oNYV4jWID1U7sxDTgvyStaCccNsCLpWIQnovJbS7MRMhpMeiUp/nm/PlbbzaMOfx7Sva 96o1ejdJo+e2GAyA5+t2OT+FK1PQ6EpC5PDksVpcJWEUToedNBzk6GCtpyZtpWOUxX0B GTPvdzhhuTibBw482rvdrNijg9A4KCdKbEN22U7jXwvBZg9JTvgmh73qT5pVkfXQ1cIe g6BxmD0RMDh/BxUVS/N1+LPtL3xhPP+vr5RXbxyV742gcRnVZZ/OsBKoG6wqx0J1vcrQ Vr8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704792682; x=1705397482; h=content-transfer-encoding:content-language:in-reply-to:mime-version :user-agent:date:message-id:from:references:cc:to:subject :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=fdolU0EUOihiv4P24pkLi7UHCL7sN9mCnSKzJVpdC60=; b=B8KRyXW56RM7py0N95olGuIgq5bYRJZzfXPloIdZM/lY6d8YhJVBuTcIiIT232ULDW 1Jv5ahS9NVwv4SIL0iHqwqpMLFKJ61xgjSnMn+IpVI0BJ5W7eriVlHbmXXxPryDzRPwP CJSaoNQfgwJy0acWqo41Vdc4RN3C9muQN8JkbVi5xeR9zt7+oob4NYoWXtvUrfRum2fr akoDtvfILCehRZgJC+LYBsq3PrCGtBSYPjwLaWpsMuPBBJ/LASJsoJJwalLUoH4zdlOR UDEoxxjFpsCJUYWbM7KTxAIZGBZSm+y1Mts1yF0jq/FKybW9hO+704myGUe4KkmQxyHr GF2g== X-Gm-Message-State: AOJu0YxQkjKM+MHWwlXqCxVJNneJXhpSwKSKGrDOuINJV/LWspVUX4Ls S2hlaAeTiSEmN0sJInSM8I1gk/2IoOQ= X-Received: by 2002:ac2:58d6:0:b0:50e:696e:150e with SMTP id u22-20020ac258d6000000b0050e696e150emr1755069lfo.7.1704792681797; Tue, 09 Jan 2024 01:31:21 -0800 (PST) Received: from [192.168.1.105] ([178.176.74.61]) by smtp.gmail.com with ESMTPSA id n30-20020ac2491e000000b0050e76a5624csm253765lfi.242.2024.01.09.01.31.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 09 Jan 2024 01:31:21 -0800 (PST) Subject: Re: [PATCH v3] usb: core: Prevent null pointer dereference in update_port_device_state To: Udipto Goswami , Greg Kroah-Hartman , Alan Stern Cc: Krishna Kurapati , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org References: <20240109061708.26288-1-quic_ugoswami@quicinc.com> From: Sergei Shtylyov Message-ID: <3bb51617-81e1-7d19-598d-2b57164320e1@gmail.com> Date: Tue, 9 Jan 2024 12:31:19 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In-Reply-To: <20240109061708.26288-1-quic_ugoswami@quicinc.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit On 1/9/24 9:17 AM, 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 > --- > 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. I can't parse this sentence, s/th is missing... > + * This would result usb_hub_to_struct_hub in this > + * function to return NULL. "This would result in usb_hub_to_struct_hub in this function returning NULL", perhaps? > + * > + * 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); > + } > } > } > MBR, Sergey