Re: [bug report] rtnetlink: Return correct error on changing device netns

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Dan Carpenter <dan.carpenter@xxxxxxxxxx> [Mon, 2021-08-30 02:04 -0700]:
> Hello Andrey Ignatov,
> 
> This is a semi-automatic email about new static checker warnings.
> 
> The patch 96a6b93b6988: "rtnetlink: Return correct error on changing
> device netns" from Aug 25, 2021, leads to the following Smatch
> complaint:
> 
>     net/core/rtnetlink.c:2698 do_setlink()
>     error: we previously assumed 'ifname' could be null (see line 2608)
> 
> net/core/rtnetlink.c
>   2607		if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
>   2608			const char *pat = ifname && ifname[0] ? ifname : NULL;
>                                           ^^^^^^
> The patch adds a new check for if "ifname" is NULL.  Is this required?

Yes. There is one do_setlink() call that passes NULL as ifname:

net/core/rtnetlink.c

static int rtnl_group_changelink(const struct sk_buff *skb,
...
			err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);

It handles IFLA_GROUP that can be used with these netns attributes.

W/o the NULL check something like `ip link set group GROUP netns NETNS`
would cause a panic like this:

# ./ifname.sh
+ ip netns add ns0
+ ip link add g1 group 1 type dummy
+ ip link show group 1
10: g1: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN mode DEFAULT group 1 qlen 1000
    link/ether ba:3c:5e:9d:8e:ab brd ff:ff:ff:ff:ff:ff
+ ip link set group 1 netns ns0
[    7.566918] BUG: kernel NULL pointer dereference, address: 0000000000000000
[    7.567746] #PF: supervisor read access in kernel mode
[    7.568299] #PF: error_code(0x0000) - not-present page
[    7.568853] PGD 8000000107758067 P4D 8000000107758067 PUD 107757067 PMD 0
[    7.569638] Oops: 0000 [#1] SMP PTI
[    7.570007] CPU: 2 PID: 242 Comm: ip Not tainted 5.14.0-rc7-00093-g1a6436f37512-dirty #533
[    7.570918] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.14.0-0-g155821a1990b-prebuilt.qemu.org 04/01/2014
[    7.572126] RIP: 0010:do_setlink+0x69/0x1070
...

>   2609			struct net *net;
>   2610			int new_ifindex;
>   2611	
>   2612			net = rtnl_link_get_net_capable(skb, dev_net(dev),
>   2613							tb, CAP_NET_ADMIN);
>   2614			if (IS_ERR(net)) {
>   2615				err = PTR_ERR(net);
>   2616				goto errout;
>   2617			}
>   2618	
>   2619			if (tb[IFLA_NEW_IFINDEX])
>   2620				new_ifindex = nla_get_s32(tb[IFLA_NEW_IFINDEX]);
>   2621			else
>   2622				new_ifindex = 0;
>   2623	
>   2624			err = __dev_change_net_namespace(dev, net, pat, new_ifindex);
>   2625			put_net(net);
>   2626			if (err)
>   2627				goto errout;
>   2628			status |= DO_SETLINK_MODIFIED;
>   2629		}
...
>   2693		/*
>   2694		 * Interface selected by interface index but interface
>   2695		 * name provided implies that a name change has been
>   2696		 * requested.
>   2697		 */
>   2698		if (ifm->ifi_index > 0 && ifname[0]) {
>                                           ^^^^^^^^^
> The existing code does not check.

+Jakub

As Jakub explained in [0] this code can not be called with ifname=NULL
because the only do_setlink() call with ifname=NULL is done only when
`ifm->ifi_index == 0` so it looks like false positive.

[0]
https://lore.kernel.org/netdev/20210830094301.4f6ada72@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/


-- 
Andrey Ignatov



[Index of Archives]     [Kernel Development]     [Kernel Announce]     [Kernel Newbies]     [Linux Networking Development]     [Share Photos]     [IDE]     [Security]     [Git]     [Netfilter]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Device Mapper]

  Powered by Linux