Skip to content

Commit 4888e1d

Browse files
Thadeu Lima de Souza Cascardogregkh
authored andcommitted
net: Remove RTNL dance for SIOCBRADDIF and SIOCBRDELIF.
commit ed3ba9b upstream. SIOCBRDELIF is passed to dev_ioctl() first and later forwarded to br_ioctl_call(), which causes unnecessary RTNL dance and the splat below [0] under RTNL pressure. Let's say Thread A is trying to detach a device from a bridge and Thread B is trying to remove the bridge. In dev_ioctl(), Thread A bumps the bridge device's refcnt by netdev_hold() and releases RTNL because the following br_ioctl_call() also re-acquires RTNL. In the race window, Thread B could acquire RTNL and try to remove the bridge device. Then, rtnl_unlock() by Thread B will release RTNL and wait for netdev_put() by Thread A. Thread A, however, must hold RTNL after the unlock in dev_ifsioc(), which may take long under RTNL pressure, resulting in the splat by Thread B. Thread A (SIOCBRDELIF) Thread B (SIOCBRDELBR) ---------------------- ---------------------- sock_ioctl sock_ioctl `- sock_do_ioctl `- br_ioctl_call `- dev_ioctl `- br_ioctl_stub |- rtnl_lock | |- dev_ifsioc ' ' |- dev = __dev_get_by_name(...) |- netdev_hold(dev, ...) . / |- rtnl_unlock ------. | | |- br_ioctl_call `---> |- rtnl_lock Race | | `- br_ioctl_stub |- br_del_bridge Window | | | |- dev = __dev_get_by_name(...) | | | May take long | `- br_dev_delete(dev, ...) | | | under RTNL pressure | `- unregister_netdevice_queue(dev, ...) | | | | `- rtnl_unlock \ | |- rtnl_lock <-' `- netdev_run_todo | |- ... `- netdev_run_todo | `- rtnl_unlock |- __rtnl_unlock | |- netdev_wait_allrefs_any |- netdev_put(dev, ...) <----------------' Wait refcnt decrement and log splat below To avoid blocking SIOCBRDELBR unnecessarily, let's not call dev_ioctl() for SIOCBRADDIF and SIOCBRDELIF. In the dev_ioctl() path, we do the following: 1. Copy struct ifreq by get_user_ifreq in sock_do_ioctl() 2. Check CAP_NET_ADMIN in dev_ioctl() 3. Call dev_load() in dev_ioctl() 4. Fetch the master dev from ifr.ifr_name in dev_ifsioc() 3. can be done by request_module() in br_ioctl_call(), so we move 1., 2., and 4. to br_ioctl_stub(). Note that 2. is also checked later in add_del_if(), but it's better performed before RTNL. SIOCBRADDIF and SIOCBRDELIF have been processed in dev_ioctl() since the pre-git era, and there seems to be no specific reason to process them there. [0]: unregister_netdevice: waiting for wpan3 to become free. Usage count = 2 ref_tracker: wpan3@ffff8880662d8608 has 1/1 users at __netdev_tracker_alloc include/linux/netdevice.h:4282 [inline] netdev_hold include/linux/netdevice.h:4311 [inline] dev_ifsioc+0xc6a/0x1160 net/core/dev_ioctl.c:624 dev_ioctl+0x255/0x10c0 net/core/dev_ioctl.c:826 sock_do_ioctl+0x1ca/0x260 net/socket.c:1213 sock_ioctl+0x23a/0x6c0 net/socket.c:1318 vfs_ioctl fs/ioctl.c:51 [inline] __do_sys_ioctl fs/ioctl.c:906 [inline] __se_sys_ioctl fs/ioctl.c:892 [inline] __x64_sys_ioctl+0x1a4/0x210 fs/ioctl.c:892 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0xcb/0x250 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x77/0x7f Fixes: 893b195 ("net: bridge: fix ioctl locking") Reported-by: syzkaller <syzkaller@googlegroups.com> Reported-by: yan kang <kangyan91@outlook.com> Reported-by: yue sun <samsun1006219@gmail.com> Closes: https://lore.kernel.org/netdev/SY8P300MB0421225D54EB92762AE8F0F2A1D32@SY8P300MB0421.AUSP300.PROD.OUTLOOK.COM/ Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com> Acked-by: Stanislav Fomichev <sdf@fomichev.me> Reviewed-by: Ido Schimmel <idosch@nvidia.com> Acked-by: Nikolay Aleksandrov <razor@blackwall.org> Link: https://patch.msgid.link/20250316192851.19781-1-kuniyu@amazon.com Signed-off-by: Paolo Abeni <pabeni@redhat.com> [cascardo: fixed conflict at dev_ifsioc] Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@igalia.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent b031365 commit 4888e1d

5 files changed

Lines changed: 45 additions & 35 deletions

File tree

include/linux/if_bridge.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,9 @@ struct br_ip_list {
6565
#define BR_DEFAULT_AGEING_TIME (300 * HZ)
6666

6767
struct net_bridge;
68-
void brioctl_set(int (*hook)(struct net *net, struct net_bridge *br,
69-
unsigned int cmd, struct ifreq *ifr,
68+
void brioctl_set(int (*hook)(struct net *net, unsigned int cmd,
7069
void __user *uarg));
71-
int br_ioctl_call(struct net *net, struct net_bridge *br, unsigned int cmd,
72-
struct ifreq *ifr, void __user *uarg);
70+
int br_ioctl_call(struct net *net, unsigned int cmd, void __user *uarg);
7371

7472
#if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING)
7573
int br_multicast_list_adjacent(struct net_device *dev,

net/bridge/br_ioctl.c

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,26 @@ static int old_deviceless(struct net *net, void __user *data)
394394
return -EOPNOTSUPP;
395395
}
396396

397-
int br_ioctl_stub(struct net *net, struct net_bridge *br, unsigned int cmd,
398-
struct ifreq *ifr, void __user *uarg)
397+
int br_ioctl_stub(struct net *net, unsigned int cmd, void __user *uarg)
399398
{
400399
int ret = -EOPNOTSUPP;
400+
struct ifreq ifr;
401+
402+
if (cmd == SIOCBRADDIF || cmd == SIOCBRDELIF) {
403+
void __user *data;
404+
char *colon;
405+
406+
if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
407+
return -EPERM;
408+
409+
if (get_user_ifreq(&ifr, &data, uarg))
410+
return -EFAULT;
411+
412+
ifr.ifr_name[IFNAMSIZ - 1] = 0;
413+
colon = strchr(ifr.ifr_name, ':');
414+
if (colon)
415+
*colon = 0;
416+
}
401417

402418
rtnl_lock();
403419

@@ -430,7 +446,21 @@ int br_ioctl_stub(struct net *net, struct net_bridge *br, unsigned int cmd,
430446
break;
431447
case SIOCBRADDIF:
432448
case SIOCBRDELIF:
433-
ret = add_del_if(br, ifr->ifr_ifindex, cmd == SIOCBRADDIF);
449+
{
450+
struct net_device *dev;
451+
452+
dev = __dev_get_by_name(net, ifr.ifr_name);
453+
if (!dev || !netif_device_present(dev)) {
454+
ret = -ENODEV;
455+
break;
456+
}
457+
if (!netif_is_bridge_master(dev)) {
458+
ret = -EOPNOTSUPP;
459+
break;
460+
}
461+
462+
ret = add_del_if(netdev_priv(dev), ifr.ifr_ifindex, cmd == SIOCBRADDIF);
463+
}
434464
break;
435465
}
436466

net/bridge/br_private.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -953,8 +953,7 @@ br_port_get_check_rtnl(const struct net_device *dev)
953953
/* br_ioctl.c */
954954
int br_dev_siocdevprivate(struct net_device *dev, struct ifreq *rq,
955955
void __user *data, int cmd);
956-
int br_ioctl_stub(struct net *net, struct net_bridge *br, unsigned int cmd,
957-
struct ifreq *ifr, void __user *uarg);
956+
int br_ioctl_stub(struct net *net, unsigned int cmd, void __user *uarg);
958957

959958
/* br_multicast.c */
960959
#ifdef CONFIG_BRIDGE_IGMP_SNOOPING

net/core/dev_ioctl.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,6 @@ static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data,
514514
int err;
515515
struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
516516
const struct net_device_ops *ops;
517-
netdevice_tracker dev_tracker;
518517

519518
if (!dev)
520519
return -ENODEV;
@@ -577,19 +576,6 @@ static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data,
577576
case SIOCWANDEV:
578577
return dev_siocwandev(dev, &ifr->ifr_settings);
579578

580-
case SIOCBRADDIF:
581-
case SIOCBRDELIF:
582-
if (!netif_device_present(dev))
583-
return -ENODEV;
584-
if (!netif_is_bridge_master(dev))
585-
return -EOPNOTSUPP;
586-
netdev_hold(dev, &dev_tracker, GFP_KERNEL);
587-
rtnl_unlock();
588-
err = br_ioctl_call(net, netdev_priv(dev), cmd, ifr, NULL);
589-
netdev_put(dev, &dev_tracker);
590-
rtnl_lock();
591-
return err;
592-
593579
case SIOCDEVPRIVATE ... SIOCDEVPRIVATE + 15:
594580
return dev_siocdevprivate(dev, ifr, data, cmd);
595581

@@ -770,8 +756,6 @@ int dev_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr,
770756
case SIOCBONDRELEASE:
771757
case SIOCBONDSETHWADDR:
772758
case SIOCBONDCHANGEACTIVE:
773-
case SIOCBRADDIF:
774-
case SIOCBRDELIF:
775759
case SIOCSHWTSTAMP:
776760
if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
777761
return -EPERM;

net/socket.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,12 +1173,10 @@ static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
11731173
*/
11741174

11751175
static DEFINE_MUTEX(br_ioctl_mutex);
1176-
static int (*br_ioctl_hook)(struct net *net, struct net_bridge *br,
1177-
unsigned int cmd, struct ifreq *ifr,
1176+
static int (*br_ioctl_hook)(struct net *net, unsigned int cmd,
11781177
void __user *uarg);
11791178

1180-
void brioctl_set(int (*hook)(struct net *net, struct net_bridge *br,
1181-
unsigned int cmd, struct ifreq *ifr,
1179+
void brioctl_set(int (*hook)(struct net *net, unsigned int cmd,
11821180
void __user *uarg))
11831181
{
11841182
mutex_lock(&br_ioctl_mutex);
@@ -1187,8 +1185,7 @@ void brioctl_set(int (*hook)(struct net *net, struct net_bridge *br,
11871185
}
11881186
EXPORT_SYMBOL(brioctl_set);
11891187

1190-
int br_ioctl_call(struct net *net, struct net_bridge *br, unsigned int cmd,
1191-
struct ifreq *ifr, void __user *uarg)
1188+
int br_ioctl_call(struct net *net, unsigned int cmd, void __user *uarg)
11921189
{
11931190
int err = -ENOPKG;
11941191

@@ -1197,7 +1194,7 @@ int br_ioctl_call(struct net *net, struct net_bridge *br, unsigned int cmd,
11971194

11981195
mutex_lock(&br_ioctl_mutex);
11991196
if (br_ioctl_hook)
1200-
err = br_ioctl_hook(net, br, cmd, ifr, uarg);
1197+
err = br_ioctl_hook(net, cmd, uarg);
12011198
mutex_unlock(&br_ioctl_mutex);
12021199

12031200
return err;
@@ -1297,7 +1294,9 @@ static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
12971294
case SIOCSIFBR:
12981295
case SIOCBRADDBR:
12991296
case SIOCBRDELBR:
1300-
err = br_ioctl_call(net, NULL, cmd, NULL, argp);
1297+
case SIOCBRADDIF:
1298+
case SIOCBRDELIF:
1299+
err = br_ioctl_call(net, cmd, argp);
13011300
break;
13021301
case SIOCGIFVLAN:
13031302
case SIOCSIFVLAN:
@@ -3466,6 +3465,8 @@ static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
34663465
case SIOCGPGRP:
34673466
case SIOCBRADDBR:
34683467
case SIOCBRDELBR:
3468+
case SIOCBRADDIF:
3469+
case SIOCBRDELIF:
34693470
case SIOCGIFVLAN:
34703471
case SIOCSIFVLAN:
34713472
case SIOCGSKNS:
@@ -3505,8 +3506,6 @@ static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
35053506
case SIOCGIFPFLAGS:
35063507
case SIOCGIFTXQLEN:
35073508
case SIOCSIFTXQLEN:
3508-
case SIOCBRADDIF:
3509-
case SIOCBRDELIF:
35103509
case SIOCGIFNAME:
35113510
case SIOCSIFNAME:
35123511
case SIOCGMIIPHY:

0 commit comments

Comments
 (0)