* [bug report] rtnetlink: Return correct error on changing device netns
@ 2021-08-30 9:04 Dan Carpenter
2021-08-31 7:06 ` Andrey Ignatov
0 siblings, 1 reply; 3+ messages in thread
From: Dan Carpenter @ 2021-08-30 9:04 UTC (permalink / raw)
To: rdna; +Cc: kernel-janitors
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?
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 }
2630
2631 if (tb[IFLA_MAP]) {
2632 struct rtnl_link_ifmap *u_map;
2633 struct ifmap k_map;
2634
2635 if (!ops->ndo_set_config) {
2636 err = -EOPNOTSUPP;
2637 goto errout;
2638 }
2639
2640 if (!netif_device_present(dev)) {
2641 err = -ENODEV;
2642 goto errout;
2643 }
2644
2645 u_map = nla_data(tb[IFLA_MAP]);
2646 k_map.mem_start = (unsigned long) u_map->mem_start;
2647 k_map.mem_end = (unsigned long) u_map->mem_end;
2648 k_map.base_addr = (unsigned short) u_map->base_addr;
2649 k_map.irq = (unsigned char) u_map->irq;
2650 k_map.dma = (unsigned char) u_map->dma;
2651 k_map.port = (unsigned char) u_map->port;
2652
2653 err = ops->ndo_set_config(dev, &k_map);
2654 if (err < 0)
2655 goto errout;
2656
2657 status |= DO_SETLINK_NOTIFY;
2658 }
2659
2660 if (tb[IFLA_ADDRESS]) {
2661 struct sockaddr *sa;
2662 int len;
2663
2664 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2665 sizeof(*sa));
2666 sa = kmalloc(len, GFP_KERNEL);
2667 if (!sa) {
2668 err = -ENOMEM;
2669 goto errout;
2670 }
2671 sa->sa_family = dev->type;
2672 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2673 dev->addr_len);
2674 err = dev_set_mac_address_user(dev, sa, extack);
2675 kfree(sa);
2676 if (err)
2677 goto errout;
2678 status |= DO_SETLINK_MODIFIED;
2679 }
2680
2681 if (tb[IFLA_MTU]) {
2682 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
2683 if (err < 0)
2684 goto errout;
2685 status |= DO_SETLINK_MODIFIED;
2686 }
2687
2688 if (tb[IFLA_GROUP]) {
2689 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2690 status |= DO_SETLINK_NOTIFY;
2691 }
2692
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.
2699 err = dev_change_name(dev, ifname);
2700 if (err < 0)
regards,
dan carpenter
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [bug report] rtnetlink: Return correct error on changing device netns
2021-08-30 9:04 [bug report] rtnetlink: Return correct error on changing device netns Dan Carpenter
@ 2021-08-31 7:06 ` Andrey Ignatov
2021-08-31 9:03 ` Dan Carpenter
0 siblings, 1 reply; 3+ messages in thread
From: Andrey Ignatov @ 2021-08-31 7:06 UTC (permalink / raw)
To: Dan Carpenter; +Cc: kernel-janitors, Jakub Kicinski
Dan Carpenter <dan.carpenter@oracle.com> [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@kicinski-fedora-pc1c0hjn.dhcp.thefacebook.com/
--
Andrey Ignatov
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [bug report] rtnetlink: Return correct error on changing device netns
2021-08-31 7:06 ` Andrey Ignatov
@ 2021-08-31 9:03 ` Dan Carpenter
0 siblings, 0 replies; 3+ messages in thread
From: Dan Carpenter @ 2021-08-31 9:03 UTC (permalink / raw)
To: Andrey Ignatov; +Cc: kernel-janitors, Jakub Kicinski
Thanks. In theory, Smatch is supposed to be able to parse this
correctly but do_setlink() is too complicated for Smatch.
net/core/rtnetlink.c:2824 do_setlink() warn: Function too hairy. No more merges.
So then the callers think that ifm->ifi_index is not used and they don't
pass that information. It's hard to know how to fix this...
regards,
dan carpenter
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2021-08-31 9:03 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-30 9:04 [bug report] rtnetlink: Return correct error on changing device netns Dan Carpenter
2021-08-31 7:06 ` Andrey Ignatov
2021-08-31 9:03 ` Dan Carpenter
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).