From patchwork Sat Apr 4 14:18:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Taehee Yoo X-Patchwork-Id: 1266413 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=hpBie/Na; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 48vf7D3lqVz9sSK for ; Sun, 5 Apr 2020 01:19:04 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726246AbgDDOTB (ORCPT ); Sat, 4 Apr 2020 10:19:01 -0400 Received: from mail-pj1-f68.google.com ([209.85.216.68]:53107 "EHLO mail-pj1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725730AbgDDOTA (ORCPT ); Sat, 4 Apr 2020 10:19:00 -0400 Received: by mail-pj1-f68.google.com with SMTP id ng8so4398829pjb.2; Sat, 04 Apr 2020 07:18:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=QJ9NcQbuYWxp/fWpC6kxDelETomxeggFMk3kc/GiT3w=; b=hpBie/Na48tBzqI/4U3LkzdVWiyl9ohEWuOyWf2iWNxl59ffCTcUe/kZ+IHO9Z/r/K HagSNmW47aSHU8YApchp3iRqQ/FrD7bE7TYE95Dk+9QIxyMokBqPmQHLTeL1MQHaGF25 JyIVjpY3gGL3t0yXxdue5svobta1AOl2hVMUM6JfIFXeCWlN+4xAwNHALIVhna9/+D2D O6zPrtdqP/WKQW31sKmb7wDMJ3yIBTJOc+I0dnDjUZrgoNX8O4SfnnS+SQXe1SsBmpP7 PkK0gLt9voe7gVbjNJ80rD9dRH04OXQdF5boeJ4Z39avMxlFyBGSoME9ifM4Sc4ZvSNS ym1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=QJ9NcQbuYWxp/fWpC6kxDelETomxeggFMk3kc/GiT3w=; b=RyJapa23xmL4vOWzXf90CbCe2lHbg/+ZAzarmFLvQwLSWpI9qzT5ztdrj6gwzDtTKB kG6nTG5MP7wAdmyvIV0JvmrvxBtxVi7DXDzgJgPI6rm81N/w+j5ruHw7XADMf0L++X0g Jyt5R6lg6zESj5K+dEZxyPYfCgETQpvF7Vequz8OGQmMhuHXj8FHT8OSLg5js4vJWtn0 zYPHMX7yqNrkDdz4uj40GLVeyVhi3d/G8NPD52YMaibUx3pyB2P7qqxo9zdHXS69B61Y Xz+pn4kcf8cYowrKXJ6PS11SAyujmJ4wMNSMu1sC6SA5Trlkoq+OrcJ8Kxsv6jSTiron eegA== X-Gm-Message-State: AGi0PubT0umiAgqFX7eMPU6Ekb+1CgBZ+xDxiizn3GW48T5/Xq3qzoOl HwVO7388v47I/0Rpoacbo/w17rWL X-Google-Smtp-Source: APiQypIMC6nsNFD/3rZQDf7SQJjt1NuRNNoDR7BqAjPZEjoXhIqxuJcbPLbhae1jR34g5VUe4gHUMw== X-Received: by 2002:a17:90b:4d0b:: with SMTP id mw11mr16067328pjb.45.1586009939247; Sat, 04 Apr 2020 07:18:59 -0700 (PDT) Received: from localhost.localdomain ([180.70.143.152]) by smtp.gmail.com with ESMTPSA id o11sm7104730pgh.78.2020.04.04.07.18.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Apr 2020 07:18:58 -0700 (PDT) From: Taehee Yoo To: davem@davemloft.net, kuba@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, j.vosburgh@gmail.com, vfalico@gmail.com, andy@greyhouse.net, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: ap420073@gmail.com, mitch.a.williams@intel.com Subject: [PATCH net v2 1/3] class: add class_has_file_ns() helper function Date: Sat, 4 Apr 2020 14:18:27 +0000 Message-Id: <20200404141827.26255-1-ap420073@gmail.com> X-Mailer: git-send-email 2.17.1 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org The new helper function is to check whether the class file is existing or not. This function will be used by networking stack to check "/sys/class/net/*" file. Reported-by: syzbot+830c6dbfc71edc4f0b8f@syzkaller.appspotmail.com Fixes: b76cdba9cdb2 ("[PATCH] bonding: add sysfs functionality to bonding (large)") Signed-off-by: Taehee Yoo --- v1 -> v2: - Implement class_has_file_ns() instead of class_find_and_get_file_ns(). - Change headline. - Add kernel documentation comment. drivers/base/class.c | 22 ++++++++++++++++++++++ include/linux/device/class.h | 3 ++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/drivers/base/class.c b/drivers/base/class.c index bcd410e6d70a..a2f2787f6aa7 100644 --- a/drivers/base/class.c +++ b/drivers/base/class.c @@ -105,6 +105,28 @@ void class_remove_file_ns(struct class *cls, const struct class_attribute *attr, sysfs_remove_file_ns(&cls->p->subsys.kobj, &attr->attr, ns); } +/** + * class_has_file_ns - check whether file is existing or not + * @cls: the compatibility class + * @name: name to look for + * @ns: the namespace tag to use + */ +bool class_has_file_ns(struct class *cls, const char *name, + const void *ns) +{ + struct kernfs_node *kn = NULL; + + if (cls) { + kn = kernfs_find_and_get_ns(cls->p->subsys.kobj.sd, name, ns); + if (kn) { + kernfs_put(kn); + return true; + } + } + return false; +} +EXPORT_SYMBOL_GPL(class_has_file_ns); + static struct class *class_get(struct class *cls) { if (cls) diff --git a/include/linux/device/class.h b/include/linux/device/class.h index e8d470c457d1..b3d43658b201 100644 --- a/include/linux/device/class.h +++ b/include/linux/device/class.h @@ -209,7 +209,8 @@ extern int __must_check class_create_file_ns(struct class *class, extern void class_remove_file_ns(struct class *class, const struct class_attribute *attr, const void *ns); - +bool class_has_file_ns(struct class *cls, const char *name, + const void *ns); static inline int __must_check class_create_file(struct class *class, const struct class_attribute *attr) { From patchwork Sat Apr 4 14:19:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Taehee Yoo X-Patchwork-Id: 1266414 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=P6SMp6q0; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 48vf7c03VFz9sSK for ; Sun, 5 Apr 2020 01:19:24 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726402AbgDDOTU (ORCPT ); Sat, 4 Apr 2020 10:19:20 -0400 Received: from mail-pg1-f195.google.com ([209.85.215.195]:36117 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725730AbgDDOTT (ORCPT ); Sat, 4 Apr 2020 10:19:19 -0400 Received: by mail-pg1-f195.google.com with SMTP id c23so5111314pgj.3; Sat, 04 Apr 2020 07:19:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=2qcBk2Kee3c2H8KonoYQptPwts2tMtEwFN7KzhaQ2kA=; b=P6SMp6q09+//0MLio/ZPPVTPzvoaeIu3FNBLqVPC2JwNe9fyUVtAFuJeFQFKBNwVZi nmmiG8tMe46Y/snCkXoP0G/99FeMCDgNImcMpuZW1A4R/BE7cQx1zhSsmmcFWvMZNnxG BkRJGVNX4XJXrn1FFl5FECG7exdk/DTHEh2ZjV4Q8hWfFf6D6eMeyNJo3AY4rSu+/gfb MtOZbNnGWc2fLCgJGZrDlo6rIRmYkeiR2V5hdbUA0+gWRs/lT12A6XrBd0Zn12tl5FCB Ftm9ITYKuEwPuqB6j+gjQp1IA5cEPZPC7YK01SU7Jox0iZB4VM4V3MiiGDu5wlnxx0pV mnZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=2qcBk2Kee3c2H8KonoYQptPwts2tMtEwFN7KzhaQ2kA=; b=lM5O0AmEOAL/7teknPKNeQFC0n4woL3RhhCo41/ugf0PFip/vuBx5cJcnKqHrQncnV UDS0OpTTer2J/YjzbVHHKGr82W4PCJXyBLYQoVDxv4LznbkCgL7JfWeZBv/gB/z6DQxT dNZDMnF/qhYu0jB1MUb5DaPPYuoYEoWqBuea43HK8gQezP71VwPxs9eSfAVENh/8me2s OOHRT0aQs19URQlMoM0UFjKvatL/rg0twjDrCyZIns341zDHoXroo/DH7sR15xD83zAY /NPs7/kz7Ql5RgQKWLgFYgn0vl/hUMDkefG8WE84fy9jkyihcyVyMbgIbIIA3k1JzjeD RVmw== X-Gm-Message-State: AGi0Pub+0OP0+NQSAbENPAfE42BYP1AUfeRrGaf9onQ6n+FGkXJHTBNF gx9m0jqPwoHLs9616AjZ9c8= X-Google-Smtp-Source: APiQypIlxvSawXoB+jieWeDwhpjcHlo0Wly8PQrE2MKJQn+0hWfY6iH5a17kaWCZ/i80AsCGjWVHZQ== X-Received: by 2002:a62:76d1:: with SMTP id r200mr13447849pfc.298.1586009958356; Sat, 04 Apr 2020 07:19:18 -0700 (PDT) Received: from localhost.localdomain ([180.70.143.152]) by smtp.gmail.com with ESMTPSA id d5sm7853360pfa.59.2020.04.04.07.19.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Apr 2020 07:19:17 -0700 (PDT) From: Taehee Yoo To: davem@davemloft.net, kuba@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, j.vosburgh@gmail.com, vfalico@gmail.com, andy@greyhouse.net, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: ap420073@gmail.com, mitch.a.williams@intel.com Subject: [PATCH net v2 2/3] net: core: add netdev_class_has_file_ns() helper function Date: Sat, 4 Apr 2020 14:19:09 +0000 Message-Id: <20200404141909.26399-1-ap420073@gmail.com> X-Mailer: git-send-email 2.17.1 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org This helper function is to check whether the class file "/sys/class/net/*" is existing or not. In the next patch, this helper function will be used. Reported-by: syzbot+830c6dbfc71edc4f0b8f@syzkaller.appspotmail.com Fixes: b76cdba9cdb2 ("[PATCH] bonding: add sysfs functionality to bonding (large)") Signed-off-by: Taehee Yoo --- v1 -> v2: - use class_has_file_ns(), which is introduced by the first patch. include/linux/netdevice.h | 2 +- net/core/net-sysfs.c | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h index 130a668049ab..a04c487c0975 100644 --- a/include/linux/netdevice.h +++ b/include/linux/netdevice.h @@ -4555,7 +4555,7 @@ int netdev_class_create_file_ns(const struct class_attribute *class_attr, const void *ns); void netdev_class_remove_file_ns(const struct class_attribute *class_attr, const void *ns); - +bool netdev_class_has_file_ns(const char *name, const void *ns); static inline int netdev_class_create_file(const struct class_attribute *class_attr) { return netdev_class_create_file_ns(class_attr, NULL); diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c index cf0215734ceb..8a20d658eff0 100644 --- a/net/core/net-sysfs.c +++ b/net/core/net-sysfs.c @@ -1914,6 +1914,12 @@ void netdev_class_remove_file_ns(const struct class_attribute *class_attr, } EXPORT_SYMBOL(netdev_class_remove_file_ns); +bool netdev_class_has_file_ns(const char *name, const void *ns) +{ + return class_has_file_ns(&net_class, name, ns); +} +EXPORT_SYMBOL(netdev_class_has_file_ns); + int __init netdev_kobject_init(void) { kobj_ns_type_register(&net_ns_type_operations); From patchwork Sat Apr 4 14:19:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Taehee Yoo X-Patchwork-Id: 1266415 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=p3m27AyD; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 48vf7w3zF9z9sPF for ; Sun, 5 Apr 2020 01:19:40 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726461AbgDDOTf (ORCPT ); Sat, 4 Apr 2020 10:19:35 -0400 Received: from mail-pl1-f194.google.com ([209.85.214.194]:34360 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725730AbgDDOTe (ORCPT ); Sat, 4 Apr 2020 10:19:34 -0400 Received: by mail-pl1-f194.google.com with SMTP id a23so4008559plm.1; Sat, 04 Apr 2020 07:19:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=FQZ87IqMgNSbWHXUc3DVaj/f2JBKgxMopHxIVMyYK98=; b=p3m27AyDHmdxfRJhrqvH57lz+PzJDwICr5tmnu7wifkJKXW5TigDxWVr4K9AJba6qc HrTISnsEFOvjlMlT24tCAk5pwX2XmtRl9d94Vbx6h1h9uoSRywAb9JS7arigyBE7SIpv JiG/yOrPJgLFRSML80N6B5KI4wuJeH65NcWWOl1A1JeSRWHcAutJiKk1y8jvMbt6Bj1a tYF3hCg1CS4nIFRWjVIHloAUZJ537Axjvx+P0vJ1rwlyVvj+BZ2WuYZ0NpEpNyW3JnKX Nj4JxX/LMfIYC8epl1MIQvYapfePF5+HVldnQ0TijbjWJVAVg3OirW17putz2lgVJvyG AesQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=FQZ87IqMgNSbWHXUc3DVaj/f2JBKgxMopHxIVMyYK98=; b=ewcb/d0Xt5xeAu6y9+OKASo74HhpUdxzwDnf0sXlVeh1JTxzkKdHVNOAPxMkkwAqpN NYK79+gq6ZhOQy/WqJA8j1DiZQo4io0l+Wiv1jekRcQF4niDMz5IiEJJ8mP9HzGkLetC XQQMS2ro9s2JAfhv7gS69kcozztWIrgcmosdIMqvQy83RZokdh0svhatzrXNwVVdXOyO grbmrpRNfBdeGy1A4tK9/+PrckMEg8+phW4bxCuVUiCXkJJsC7v2+4qK6cnySM/51B0x eU97omo4Yavf2FfFWB/z8cSIRnB5xURHIJeRdtJYyGrk1UHaSc/DYy9l3/8D35lOlaq+ AAOw== X-Gm-Message-State: AGi0PuYCzEB6TEkseznLFbO48ij1GbKfzPyxBLGpFuzqHrc+7+VcNyqa KnWlj6PfFrJZ9KNmoT+cfMQ= X-Google-Smtp-Source: APiQypJGttGtx8hTXs2pI9Mfqxpd3HNJmLrKFx1NYY3Yuk6Zhkp8KchsYjqOdDiXleRtFjmCw4QIjQ== X-Received: by 2002:a17:902:8c94:: with SMTP id t20mr12392555plo.170.1586009971820; Sat, 04 Apr 2020 07:19:31 -0700 (PDT) Received: from localhost.localdomain ([180.70.143.152]) by smtp.gmail.com with ESMTPSA id t66sm7876837pjb.45.2020.04.04.07.19.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Apr 2020 07:19:30 -0700 (PDT) From: Taehee Yoo To: davem@davemloft.net, kuba@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, j.vosburgh@gmail.com, vfalico@gmail.com, andy@greyhouse.net, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: ap420073@gmail.com, mitch.a.williams@intel.com Subject: [PATCH net v2 3/3] net: core: avoid warning in dev_change_net_namespace() Date: Sat, 4 Apr 2020 14:19:22 +0000 Message-Id: <20200404141922.26492-1-ap420073@gmail.com> X-Mailer: git-send-email 2.17.1 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org When interface's namespace is being changed, dev_change_net_namespace() is called. This removes and re-allocates many resources that include sysfs files. The "/net/class/net/" is one of them. If the sysfs creation routine(device_rename()) found duplicate sysfs file name, it warns about it and fails. But unfortunately, at that point, dev_change_net_namespace() doesn't return fail because rollback cost is too high. So, the interface can't have a sysfs file. The approach of this patch is to find the duplicate sysfs file as fast as possible. If it found that, dev_change_net_namespace() returns fail immediately with zero rollback cost. This patch includes two other things. a) Acquire rtnl_lock() in both bond_create_sysfs() and bond_destroy_sysfs() to avoid race condition. b) Do not remove "/sys/class/net/bonding_masters" sysfs file by bond_destroy_sysfs() if the file wasn't created by bond_create_sysfs(). Test commands: ip netns add nst ip link add bonding_masters type dummy modprobe bonding ip link set bonding_masters netns nst Splat looks like: [ 6396.214052] WARNING: CPU: 2 PID: 1167 at net/core/dev.c:10119 dev_change_net_namespace+0xa13/0xcb0 [ 6396.215087] Modules linked in: bonding dummy bareudp openvswitch nsh nf_conncount nf_nat nf_conntrack nf_defrag_ipv6 nf_defrag_ipv4 af_packet e1000 sch_fq_codel nf_tables nfnetlink ip_tables x_tables unix [ 6396.217064] CPU: 2 PID: 1167 Comm: ip Not tainted 5.6.0+ #492 [ 6396.217885] Hardware name: innotek GmbH VirtualBox/VirtualBox, BIOS VirtualBox 12/01/2006 [ 6396.219046] RIP: 0010:dev_change_net_namespace+0xa13/0xcb0 [ 6396.219845] Code: e9 bc f6 ff ff 4c 8b 64 24 28 e9 a5 fc ff ff b8 ea ff ff ff e9 65 fb ff ff e8 f9 00 27 ff e9 f5 f6 ff ff 0f 0b e9 0b fb ff ff <0f> 0b e9 ec fa ff ff b8 ef ff ff ff e9 43 fb ff ff 4c 89 ff 48 89 [ 6396.222336] RSP: 0018:ffff8880518a6dd0 EFLAGS: 00010282 [ 6396.223114] RAX: 00000000ffffffef RBX: ffff888057049000 RCX: ffff888051a8c000 [ 6396.224128] RDX: 0000000000000000 RSI: 0000000000000000 RDI: ffffffffb8dc0074 [ 6396.225265] RBP: ffff8880570490a0 R08: fffffbfff7166089 R09: fffffbfff7166089 [ 6396.226307] R10: 0000000000000001 R11: fffffbfff7166088 R12: ffff8880570490b8 [ 6396.227333] R13: ffff888057398040 R14: ffff888057398040 R15: ffff888057049090 [ 6396.228350] FS: 00007fdb2d3710c0(0000) GS:ffff88806c800000(0000) knlGS:0000000000000000 [ 6396.229514] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 6396.230403] CR2: 000055ec001c4910 CR3: 000000006591c001 CR4: 00000000000606e0 [ 6396.231414] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 [ 6396.232420] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 [ 6396.233463] Call Trace: [ 6396.233907] ? do_dup2+0x450/0x450 [ 6396.234452] ? deref_stack_reg+0x9c/0xd0 [ 6396.235078] ? dev_get_valid_name+0xc0/0xc0 [ 6396.235718] ? ns_capable_common+0x5c/0xd0 [ 6396.236342] ? __netlink_ns_capable+0xc3/0xf0 [ 6396.237022] do_setlink+0x171/0x2f70 [ 6396.237663] ? check_flags.part.41+0x450/0x450 [ 6396.238393] ? unwind_next_frame+0xb77/0x1c30 [ 6396.239113] ? rtnl_getlink+0x8a0/0x8a0 [ 6396.239751] ? is_bpf_text_address+0x81/0xe0 [ 6396.240447] ? kernel_text_address+0x111/0x120 [ 6396.241177] ? __kernel_text_address+0xe/0x30 [ 6396.241860] ? unwind_get_return_address+0x5f/0xa0 [ 6396.242571] ? create_prof_cpu_mask+0x20/0x20 [ 6396.243239] ? arch_stack_walk+0x83/0xb0 [ 6396.243842] ? pskb_expand_head+0x25f/0xe10 [ 6396.244479] ? stack_trace_save+0x82/0xb0 [ 6396.245103] ? memset+0x1f/0x40 [ 6396.245616] ? __nla_validate_parse+0x98/0x1ab0 [ 6396.246297] ? register_lock_class+0x19e0/0x19e0 [ 6396.246986] ? __kasan_slab_free+0x111/0x150 [ 6396.247629] ? kfree+0xce/0x2f0 [ 6396.248129] ? netlink_trim+0x196/0x1f0 [ 6396.248715] ? nla_memcpy+0x90/0x90 [ 6396.249258] ? register_lock_class+0x19e0/0x19e0 [ 6396.249958] ? deref_stack_reg+0x9c/0xd0 [ 6396.250556] ? __read_once_size_nocheck.constprop.6+0x10/0x10 [ 6396.251387] ? entry_SYSCALL_64_after_hwframe+0x49/0xb3 [ 6396.252154] __rtnl_newlink+0x9c5/0x1270 [ ... ] Reported-by: syzbot+830c6dbfc71edc4f0b8f@syzkaller.appspotmail.com Fixes: b76cdba9cdb2 ("[PATCH] bonding: add sysfs functionality to bonding (large)") Signed-off-by: Taehee Yoo --- v1 -> v2: - Change commit log. drivers/net/bonding/bond_sysfs.c | 13 ++++++++++++- include/net/bonding.h | 1 + net/core/dev.c | 4 ++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/drivers/net/bonding/bond_sysfs.c b/drivers/net/bonding/bond_sysfs.c index 2d615a93685e..02c0ae58b9db 100644 --- a/drivers/net/bonding/bond_sysfs.c +++ b/drivers/net/bonding/bond_sysfs.c @@ -772,9 +772,13 @@ int bond_create_sysfs(struct bond_net *bn) { int ret; + bn->skip_destroy_class = false; bn->class_attr_bonding_masters = class_attr_bonding_masters; sysfs_attr_init(&bn->class_attr_bonding_masters.attr); + rtnl_lock(); + if (netdev_class_has_file_ns("bonding_masters", bn->net)) + goto err; ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters, bn->net); /* Permit multiple loads of the module by ignoring failures to @@ -788,13 +792,16 @@ int bond_create_sysfs(struct bond_net *bn) * configure multiple bonding devices. */ if (ret == -EEXIST) { +err: /* Is someone being kinky and naming a device bonding_master? */ if (__dev_get_by_name(bn->net, class_attr_bonding_masters.attr.name)) pr_err("network device named %s already exists in sysfs\n", class_attr_bonding_masters.attr.name); ret = 0; + bn->skip_destroy_class = true; } + rtnl_unlock(); return ret; @@ -803,7 +810,11 @@ int bond_create_sysfs(struct bond_net *bn) /* Remove /sys/class/net/bonding_masters. */ void bond_destroy_sysfs(struct bond_net *bn) { - netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, bn->net); + rtnl_lock(); + if (!bn->skip_destroy_class) + netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, + bn->net); + rtnl_unlock(); } /* Initialize sysfs for each bond. This sets up and registers diff --git a/include/net/bonding.h b/include/net/bonding.h index dc2ce31a1f52..c04b0a0ac767 100644 --- a/include/net/bonding.h +++ b/include/net/bonding.h @@ -607,6 +607,7 @@ struct bond_net { struct proc_dir_entry *proc_dir; #endif struct class_attribute class_attr_bonding_masters; + bool skip_destroy_class; }; int bond_arp_rcv(const struct sk_buff *skb, struct bonding *bond, struct slave *slave); diff --git a/net/core/dev.c b/net/core/dev.c index 9c9e763bfe0e..a3a151142bc8 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -10058,6 +10058,10 @@ int dev_change_net_namespace(struct net_device *dev, struct net *net, const char goto out; } + err = -EEXIST; + if (netdev_class_has_file_ns(dev->name, net)) + goto out; + /* * And now a mini version of register_netdevice unregister_netdevice. */