Received: by 2002:ab2:788f:0:b0:1ee:8f2e:70ae with SMTP id b15csp675361lqi; Thu, 7 Mar 2024 08:19:25 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUNxG0oB2u6xSEqVDTdmqdBRSWPMA39UWRu8EuZDClqMDJyJgJhKZ+8gN6co2tSl2Dm/v9OZ8g3cDN3NZQI9Dhug+gnJDEYl1mh15Mrnw== X-Google-Smtp-Source: AGHT+IHw5ZMgni/v6z7L6GKqPbYf81Zp1tKZc/wGXTkIXtVW8XZgFSQvES7scesN/nBGWW24Js0s X-Received: by 2002:a05:6a20:2583:b0:1a1:6ed2:838a with SMTP id k3-20020a056a20258300b001a16ed2838amr3201166pzd.57.1709828365645; Thu, 07 Mar 2024 08:19:25 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709828365; cv=pass; d=google.com; s=arc-20160816; b=jg6CaqX9h5jMhjsgT0DxtTSKoYAsR9rAu3dKR7AX61D/Lyc0RvcxaxN7nH2MiYx4Qb 1dzmEyagQYJZx7PNeuEzJ09MXoE25qBK0jtDkn8BcvclbUh64K316OUNNQqKXgKHc8G2 2ClKh9slcrAQ4ZhbtJDpeY+OFkri3W5x7atVPb+sS81XPBzoKyG7aFiHDBqEXuLkqCW0 +/doQ/vkeqsumV5rlbMSpbx3l/+tTS7+ogswQRfpNWTDilQ8TleFexB/KLCnoGYwl4me UG/7HPzhzdBgMjQlHWjVIbPxXNcBgjqS3vhoPMS5vAtIncACgocI2zKcu4c/9iaqUWgD g4Cw== ARC-Message-Signature: i=2; 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; bh=9aC2KEpFxGSKmW4wmwJg5CjABQ00W2Cr1p8EdfQwe24=; fh=PjXA/4NezLGCM/bEX7bmMPYUIWsfpR8kRfQYxj60pF4=; b=vAp45w7jbbVWQivbo616xg7T0CSiVCZEsXATtQxjQ4T4aT4yLP7NJbulstHE0kODuw iuv4eD+rqglxE8iTk++fD2+UcBFgwc4RERJdvwc54ukJtYz9HE1GUF95BkzIXFNh5a40 8JdeCpptkzVc1raB66ZTezu+wWFKILGp9E0FF3j4R3oZbGcz0cz1AoALbaIEHJ2tQSBt ou3BjnkF8tOf1p5FULO8xxdl3JhFy3xADeC4w70+8NkPSYBvwInxtGFJ6senIRK+Xs8y W8+c+46CKYKfeT0LU6AiK96VUtP+hnfJEn/NZgmAeqXl6xi9iOvl2Y0Q/WlSymANMgLe nYqQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=de.adit-jv.com); spf=pass (google.com: domain of linux-kernel+bounces-95852-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-95852-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id o25-20020a637319000000b005d7a13d0be6si14247278pgc.232.2024.03.07.08.19.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Mar 2024 08:19:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-95852-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; arc=pass (i=1 spf=pass spfdomain=de.adit-jv.com); spf=pass (google.com: domain of linux-kernel+bounces-95852-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-95852-linux.lists.archive=gmail.com@vger.kernel.org" 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 51E4828268A for ; Thu, 7 Mar 2024 16:19:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 87AC712FB16; Thu, 7 Mar 2024 16:19:17 +0000 (UTC) Received: from hi1smtp01.de.adit-jv.com (smtp1.de.adit-jv.com [93.241.18.167]) (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 5684812F5AE; Thu, 7 Mar 2024 16:19:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=93.241.18.167 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709828357; cv=none; b=u0xkZ8GmSuKD3coqisYdjuuRwQMr9vtEQs5wvo1a0CDSD1HOIjXVOep6A8ec4UXWVpsBkNf+9MJ3DKG6+faYZc+mfOMQeKfkzcX4uxoVEGKCNqny3pnS86ePPOFdZ+9im2uUOn/JldZpGIt1HFrBfBEOGizMm1BHdN3T/dx2+GY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709828357; c=relaxed/simple; bh=wkiTOsXoIX4LFXBExDAi0YiMCiTavv3BcfFKVDYFrbA=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=Tdi0s/ztDRBnYGa9s3P5+uYkyKeJvee+r1jSE6MMUktSjVujwSmkVcYhnqRuxwI6GWMpe3RCWPHrmcLBMrlWrtg82sUFPrqJSFb+B4N0DDq6FSaw3HzlHbzKRQkNeh6UJ+Ufj+VBZyMkTV0Iz82R2T1cawQ042SwKoOCnxwg2jo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=de.adit-jv.com; spf=pass smtp.mailfrom=de.adit-jv.com; arc=none smtp.client-ip=93.241.18.167 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=de.adit-jv.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=de.adit-jv.com Received: from hi2exch02.adit-jv.com (hi2exch02.adit-jv.com [10.72.92.28]) by hi1smtp01.de.adit-jv.com (Postfix) with ESMTP id A9789520363; Thu, 7 Mar 2024 17:19:03 +0100 (CET) Received: from vmlxhi-118.adit-jv.com (10.72.93.77) by hi2exch02.adit-jv.com (10.72.92.28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Thu, 7 Mar 2024 17:19:03 +0100 From: Hardik Gajjar To: , , , CC: , , , , Subject: [PATCH] usb: gadget: f_ncm: Fix Kernel Panic due to access of invalid gadget ptr Date: Thu, 7 Mar 2024 17:18:49 +0100 Message-ID: <20240307161849.9145-1-hgajjar@de.adit-jv.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: hi2exch02.adit-jv.com (10.72.92.28) To hi2exch02.adit-jv.com (10.72.92.28) In the scenario where the system enters suspend to RAM mode (STR) triggers the disconnection of Dual Role USB Hub, and the UDC platform driver calls usb_del_gadget_udc() to cleanup and delete the associated gadget. However, at this point, the usb0 interface is not yet deleted, leading to a race condition with the TCP/IP stack attempting to access the network device parent (gadget pointer), through operations like the GETLINK net message. This patch addresses the issue by clearing the netdevice's parent device pointer when the ncm unbinds, effectively preventing the race condition during this critical phase. Followinfg is the backtrace of such race condition [ 3566.105792] Call trace: [ 3566.105984] if_nlmsg_size+0x48/0x3b0 [ 3566.107497] rtnetlink_rcv_msg+0x1cc/0x408 [ 3566.107905] netlink_rcv_skb+0x12c/0x164 [ 3566.108264] rtnetlink_rcv+0x18/0x24 [ 3566.108851] netlink_unicast_kernel+0xc4/0x14c [ 3566.109192] netlink_unicast+0x210/0x2b0 [ 3566.109606] netlink_sendmsg+0x2ec/0x360 [ 3566.110046] __sys_sendto+0x1b8/0x25c [ 3566.111594] __arm64_sys_sendto+0x28/0x38 [ 3566.112599] el0_svc_common+0xb4/0x19c [ 3566.112978] el0_svc_handler+0x74/0x98 [ 3566.113269] el0_svc+0x8/0xc - code: if_nlmsg_size call the following function static inline int rtnl_vfinfo_size(const struct net_device *dev, u32 ext_filter_mask) { // dev->dev.parent is not NULL if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) { // dev_num_vf use the dev->dev.parent->bus lead to kernel panic. int num_vfs = dev_num_vf(dev->dev.parent); size_t size = nla_total_size(0); size += num_vfs * (nla_total_size(0) + nla_total_size(sizeof(struct ifla_vf_mac)) + nla_total_size(sizeof(struct ifla_vf_vlan)) + nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST * Signed-off-by: Hardik Gajjar --- drivers/usb/gadget/function/f_ncm.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/drivers/usb/gadget/function/f_ncm.c b/drivers/usb/gadget/function/f_ncm.c index e2a059cfda2c..fdfb5b3460c7 100644 --- a/drivers/usb/gadget/function/f_ncm.c +++ b/drivers/usb/gadget/function/f_ncm.c @@ -1728,9 +1728,12 @@ static void ncm_free(struct usb_function *f) static void ncm_unbind(struct usb_configuration *c, struct usb_function *f) { struct f_ncm *ncm = func_to_ncm(f); + struct f_ncm_opts *ncm_opts; DBG(c->cdev, "ncm unbind\n"); + ncm_opts = container_of(f->fi, struct f_ncm_opts, func_inst); + hrtimer_cancel(&ncm->task_timer); kfree(f->os_desc_table); @@ -1746,6 +1749,10 @@ static void ncm_unbind(struct usb_configuration *c, struct usb_function *f) kfree(ncm->notify_req->buf); usb_ep_free_request(ncm->notify, ncm->notify_req); + + mutex_lock(&ncm_opts->lock); + SET_NETDEV_DEV(ncm_opts->net, NULL); + mutex_unlock(&ncm_opts->lock); } static struct usb_function *ncm_alloc(struct usb_function_instance *fi) -- 2.17.1