Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp913439pxb; Wed, 1 Sep 2021 12:33:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwnu1SwYPsINNIazOmlxPVQx3XEtthNauL4ig7Cu0w+zdECdw1AuQ0UuGCmlX4jJ831i0JA X-Received: by 2002:a6b:b512:: with SMTP id e18mr1009415iof.98.1630524789325; Wed, 01 Sep 2021 12:33:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630524789; cv=none; d=google.com; s=arc-20160816; b=xWetWvy0VuK18Xij3DFw/W5QQ6uwrza8vILyLogKPir2Obq2JDgIOXpJ162NKpSBBJ +zcH6FuFyapu03dNq9AYixh8jx5jVlqpBdv3jSA1bvbVK0xGvl5vCurr42erQRI/sK39 XT78l5UlYmhUHHzWmsFD66Y27rLufAtjERGKX5/hJGtFFBvqP+bosMDLqA8VSg1GGPTz 6Od43xNaHv+5ECy4+X2YJYY+WLeWETuPdpdsMyqng6ui43uPiLbAVPkLIjOXDkxCiYGm kAwlz1RBGrsLlNTuILh0q7hoKl8y1JYckKBTw0GuezOfBlCW3hVp26usoYvhbDHFLera y20A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=yvUpqRcf47Qc42uLg5gsvHW4yj3AuBk5SzF2Z7Npk2U=; b=juMQvJF2836WtwsYOiiSZfROcPLn4XYwliXHFDn4RKuVT2zxbjhXU7qGZwwSopP6ET pOJPcj8HdRbEJvi0rpspjA5IHhQ5uWeLwFaWthtYvuY5UCxvdPDyrOWZoLEmc2RQZRmR 8j2fKwRNczSQzIIsTe6wQqCYrP7yKK8PjX168BFboMU9EX3F1AIjDRBCY47eoflAU6qc vAiWLonh6v4OBKm0VsrtszWYYRVBjJKcY8+lk7iwPBL2xLIMLkM9ItX/C2D2ztMMnvnv k8LSADUm49ksGnhtt3eLLm2cfkoconKbYMU0W0E8fQPo/PDQ6lleX8YWfNZsK0h/Kdr0 srUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=YwSkHzDy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n20si404785iod.5.2021.09.01.12.32.57; Wed, 01 Sep 2021 12:33:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=YwSkHzDy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343607AbhIAMeI (ORCPT + 99 others); Wed, 1 Sep 2021 08:34:08 -0400 Received: from mail.kernel.org ([198.145.29.99]:35798 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245340AbhIAMcc (ORCPT ); Wed, 1 Sep 2021 08:32:32 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8DA2A610C7; Wed, 1 Sep 2021 12:31:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1630499496; bh=QkEI0WCue3HLhyKwEt1fpN6496pjf5f9hYPCJVuQCH8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YwSkHzDy2/iqBm6Tk3yLTZycbMzxQVT+oc7ZuNDm1gmdmzjPsuaDr7UViVhAXENIs aPwtOFc9+yzVHnsLNMwVBBzUXJVyIoJwv+ojyQIQQovBl92kF4oCSSdWW+wjvkVA7d OTR6v2S988ZirG2LSjdihWRMtigXmHr0HHPwAkC0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andrey Ignatov , "David S. Miller" , Sasha Levin Subject: [PATCH 5.4 21/48] rtnetlink: Return correct error on changing device netns Date: Wed, 1 Sep 2021 14:28:11 +0200 Message-Id: <20210901122254.100821660@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210901122253.388326997@linuxfoundation.org> References: <20210901122253.388326997@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andrey Ignatov [ Upstream commit 96a6b93b69880b2c978e1b2be9cae6970b605008 ] Currently when device is moved between network namespaces using RTM_NEWLINK message type and one of netns attributes (FLA_NET_NS_PID, IFLA_NET_NS_FD, IFLA_TARGET_NETNSID) but w/o specifying IFLA_IFNAME, and target namespace already has device with same name, userspace will get EINVAL what is confusing and makes debugging harder. Fix it so that userspace gets more appropriate EEXIST instead what makes debugging much easier. Before: # ./ifname.sh + ip netns add ns0 + ip netns exec ns0 ip link add l0 type dummy + ip netns exec ns0 ip link show l0 8: l0: mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 66:90:b5:d5:78:69 brd ff:ff:ff:ff:ff:ff + ip link add l0 type dummy + ip link show l0 10: l0: mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 6e:c6:1f:15:20:8d brd ff:ff:ff:ff:ff:ff + ip link set l0 netns ns0 RTNETLINK answers: Invalid argument After: # ./ifname.sh + ip netns add ns0 + ip netns exec ns0 ip link add l0 type dummy + ip netns exec ns0 ip link show l0 8: l0: mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 1e:4a:72:e3:e3:8f brd ff:ff:ff:ff:ff:ff + ip link add l0 type dummy + ip link show l0 10: l0: mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether f2:fc:fe:2b:7d:a6 brd ff:ff:ff:ff:ff:ff + ip link set l0 netns ns0 RTNETLINK answers: File exists The problem is that do_setlink() passes its `char *ifname` argument, that it gets from a caller, to __dev_change_net_namespace() as is (as `const char *pat`), but semantics of ifname and pat can be different. For example, __rtnl_newlink() does this: net/core/rtnetlink.c 3270 char ifname[IFNAMSIZ]; ... 3286 if (tb[IFLA_IFNAME]) 3287 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 3288 else 3289 ifname[0] = '\0'; ... 3364 if (dev) { ... 3394 return do_setlink(skb, dev, ifm, extack, tb, ifname, status); 3395 } , i.e. do_setlink() gets ifname pointer that is always valid no matter if user specified IFLA_IFNAME or not and then do_setlink() passes this ifname pointer as is to __dev_change_net_namespace() as pat argument. But the pat (pattern) in __dev_change_net_namespace() is used as: net/core/dev.c 11198 err = -EEXIST; 11199 if (__dev_get_by_name(net, dev->name)) { 11200 /* We get here if we can't use the current device name */ 11201 if (!pat) 11202 goto out; 11203 err = dev_get_valid_name(net, dev, pat); 11204 if (err < 0) 11205 goto out; 11206 } As the result the `goto out` path on line 11202 is neven taken and instead of returning EEXIST defined on line 11198, __dev_change_net_namespace() returns an error from dev_get_valid_name() and this, in turn, will be EINVAL for ifname[0] = '\0' set earlier. Fixes: d8a5ec672768 ("[NET]: netlink support for moving devices between network namespaces.") Signed-off-by: Andrey Ignatov Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/core/rtnetlink.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c index 0bad5db23129..6fbc9cb09dc0 100644 --- a/net/core/rtnetlink.c +++ b/net/core/rtnetlink.c @@ -2414,6 +2414,7 @@ static int do_setlink(const struct sk_buff *skb, return err; if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) { + const char *pat = ifname && ifname[0] ? ifname : NULL; struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev), tb, CAP_NET_ADMIN); if (IS_ERR(net)) { @@ -2421,7 +2422,7 @@ static int do_setlink(const struct sk_buff *skb, goto errout; } - err = dev_change_net_namespace(dev, net, ifname); + err = dev_change_net_namespace(dev, net, pat); put_net(net); if (err) goto errout; -- 2.30.2