Skip to content

Commit 9ceaf6f

Browse files
edumazetdavem330
authored andcommitted
bonding: fix data-races around agg_select_timer
syzbot reported that two threads might write over agg_select_timer at the same time. Make agg_select_timer atomic to fix the races. BUG: KCSAN: data-race in bond_3ad_initiate_agg_selection / bond_3ad_state_machine_handler read to 0xffff8881242aea90 of 4 bytes by task 1846 on cpu 1: bond_3ad_state_machine_handler+0x99/0x2810 drivers/net/bonding/bond_3ad.c:2317 process_one_work+0x3f6/0x960 kernel/workqueue.c:2307 worker_thread+0x616/0xa70 kernel/workqueue.c:2454 kthread+0x1bf/0x1e0 kernel/kthread.c:377 ret_from_fork+0x1f/0x30 write to 0xffff8881242aea90 of 4 bytes by task 25910 on cpu 0: bond_3ad_initiate_agg_selection+0x18/0x30 drivers/net/bonding/bond_3ad.c:1998 bond_open+0x658/0x6f0 drivers/net/bonding/bond_main.c:3967 __dev_open+0x274/0x3a0 net/core/dev.c:1407 dev_open+0x54/0x190 net/core/dev.c:1443 bond_enslave+0xcef/0x3000 drivers/net/bonding/bond_main.c:1937 do_set_master net/core/rtnetlink.c:2532 [inline] do_setlink+0x94f/0x2500 net/core/rtnetlink.c:2736 __rtnl_newlink net/core/rtnetlink.c:3414 [inline] rtnl_newlink+0xfeb/0x13e0 net/core/rtnetlink.c:3529 rtnetlink_rcv_msg+0x745/0x7e0 net/core/rtnetlink.c:5594 netlink_rcv_skb+0x14e/0x250 net/netlink/af_netlink.c:2494 rtnetlink_rcv+0x18/0x20 net/core/rtnetlink.c:5612 netlink_unicast_kernel net/netlink/af_netlink.c:1317 [inline] netlink_unicast+0x602/0x6d0 net/netlink/af_netlink.c:1343 netlink_sendmsg+0x728/0x850 net/netlink/af_netlink.c:1919 sock_sendmsg_nosec net/socket.c:705 [inline] sock_sendmsg net/socket.c:725 [inline] ____sys_sendmsg+0x39a/0x510 net/socket.c:2413 ___sys_sendmsg net/socket.c:2467 [inline] __sys_sendmsg+0x195/0x230 net/socket.c:2496 __do_sys_sendmsg net/socket.c:2505 [inline] __se_sys_sendmsg net/socket.c:2503 [inline] __x64_sys_sendmsg+0x42/0x50 net/socket.c:2503 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x44/0xd0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x44/0xae value changed: 0x00000050 -> 0x0000004f Reported by Kernel Concurrency Sanitizer on: CPU: 0 PID: 25910 Comm: syz-executor.1 Tainted: G W 5.17.0-rc4-syzkaller-dirty #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Eric Dumazet <edumazet@google.com> Reported-by: syzbot <syzkaller@googlegroups.com> Cc: Jay Vosburgh <j.vosburgh@gmail.com> Cc: Veaceslav Falico <vfalico@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 07dd448 commit 9ceaf6f

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

drivers/net/bonding/bond_3ad.c

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ static inline int __check_agg_selection_timer(struct port *port)
225225
if (bond == NULL)
226226
return 0;
227227

228-
return BOND_AD_INFO(bond).agg_select_timer ? 1 : 0;
228+
return atomic_read(&BOND_AD_INFO(bond).agg_select_timer) ? 1 : 0;
229229
}
230230

231231
/**
@@ -1995,7 +1995,7 @@ static void ad_marker_response_received(struct bond_marker *marker,
19951995
*/
19961996
void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout)
19971997
{
1998-
BOND_AD_INFO(bond).agg_select_timer = timeout;
1998+
atomic_set(&BOND_AD_INFO(bond).agg_select_timer, timeout);
19991999
}
20002000

20012001
/**
@@ -2278,6 +2278,28 @@ void bond_3ad_update_ad_actor_settings(struct bonding *bond)
22782278
spin_unlock_bh(&bond->mode_lock);
22792279
}
22802280

2281+
/**
2282+
* bond_agg_timer_advance - advance agg_select_timer
2283+
* @bond: bonding structure
2284+
*
2285+
* Return true when agg_select_timer reaches 0.
2286+
*/
2287+
static bool bond_agg_timer_advance(struct bonding *bond)
2288+
{
2289+
int val, nval;
2290+
2291+
while (1) {
2292+
val = atomic_read(&BOND_AD_INFO(bond).agg_select_timer);
2293+
if (!val)
2294+
return false;
2295+
nval = val - 1;
2296+
if (atomic_cmpxchg(&BOND_AD_INFO(bond).agg_select_timer,
2297+
val, nval) == val)
2298+
break;
2299+
}
2300+
return nval == 0;
2301+
}
2302+
22812303
/**
22822304
* bond_3ad_state_machine_handler - handle state machines timeout
22832305
* @work: work context to fetch bonding struct to work on from
@@ -2313,9 +2335,7 @@ void bond_3ad_state_machine_handler(struct work_struct *work)
23132335
if (!bond_has_slaves(bond))
23142336
goto re_arm;
23152337

2316-
/* check if agg_select_timer timer after initialize is timed out */
2317-
if (BOND_AD_INFO(bond).agg_select_timer &&
2318-
!(--BOND_AD_INFO(bond).agg_select_timer)) {
2338+
if (bond_agg_timer_advance(bond)) {
23192339
slave = bond_first_slave_rcu(bond);
23202340
port = slave ? &(SLAVE_AD_INFO(slave)->port) : NULL;
23212341

include/net/bond_3ad.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ struct ad_system {
262262
struct ad_bond_info {
263263
struct ad_system system; /* 802.3ad system structure */
264264
struct bond_3ad_stats stats;
265-
u32 agg_select_timer; /* Timer to select aggregator after all adapter's hand shakes */
265+
atomic_t agg_select_timer; /* Timer to select aggregator after all adapter's hand shakes */
266266
u16 aggregator_identifier;
267267
};
268268

0 commit comments

Comments
 (0)