Skip to content

Commit 482521d

Browse files
edumazetdavem330
authored andcommitted
udp: annotate data-races around up->pending
up->pending can be read without holding the socket lock, as pointed out by syzbot [1] Add READ_ONCE() in lockless contexts, and WRITE_ONCE() on write side. [1] BUG: KCSAN: data-race in udpv6_sendmsg / udpv6_sendmsg write to 0xffff88814e5eadf0 of 4 bytes by task 15547 on cpu 1: udpv6_sendmsg+0x1405/0x1530 net/ipv6/udp.c:1596 inet6_sendmsg+0x63/0x80 net/ipv6/af_inet6.c:657 sock_sendmsg_nosec net/socket.c:730 [inline] __sock_sendmsg net/socket.c:745 [inline] __sys_sendto+0x257/0x310 net/socket.c:2192 __do_sys_sendto net/socket.c:2204 [inline] __se_sys_sendto net/socket.c:2200 [inline] __x64_sys_sendto+0x78/0x90 net/socket.c:2200 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0x44/0x110 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x63/0x6b read to 0xffff88814e5eadf0 of 4 bytes by task 15551 on cpu 0: udpv6_sendmsg+0x22c/0x1530 net/ipv6/udp.c:1373 inet6_sendmsg+0x63/0x80 net/ipv6/af_inet6.c:657 sock_sendmsg_nosec net/socket.c:730 [inline] __sock_sendmsg net/socket.c:745 [inline] ____sys_sendmsg+0x37c/0x4d0 net/socket.c:2586 ___sys_sendmsg net/socket.c:2640 [inline] __sys_sendmmsg+0x269/0x500 net/socket.c:2726 __do_sys_sendmmsg net/socket.c:2755 [inline] __se_sys_sendmmsg net/socket.c:2752 [inline] __x64_sys_sendmmsg+0x57/0x60 net/socket.c:2752 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0x44/0x110 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x63/0x6b value changed: 0x00000000 -> 0x0000000a Reported by Kernel Concurrency Sanitizer on: CPU: 0 PID: 15551 Comm: syz-executor.1 Tainted: G W 6.7.0-syzkaller #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/17/2023 Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Reported-by: [email protected] Link: https://lore.kernel.org/netdev/[email protected]/ Signed-off-by: Eric Dumazet <[email protected]> Reviewed-by: Jiri Pirko <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 08300ad commit 482521d

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

net/ipv4/udp.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ void udp_flush_pending_frames(struct sock *sk)
805805

806806
if (up->pending) {
807807
up->len = 0;
808-
up->pending = 0;
808+
WRITE_ONCE(up->pending, 0);
809809
ip_flush_pending_frames(sk);
810810
}
811811
}
@@ -993,7 +993,7 @@ int udp_push_pending_frames(struct sock *sk)
993993

994994
out:
995995
up->len = 0;
996-
up->pending = 0;
996+
WRITE_ONCE(up->pending, 0);
997997
return err;
998998
}
999999
EXPORT_SYMBOL(udp_push_pending_frames);
@@ -1070,7 +1070,7 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
10701070
getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
10711071

10721072
fl4 = &inet->cork.fl.u.ip4;
1073-
if (up->pending) {
1073+
if (READ_ONCE(up->pending)) {
10741074
/*
10751075
* There are pending frames.
10761076
* The socket lock must be held while it's corked.
@@ -1269,7 +1269,7 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
12691269
fl4->saddr = saddr;
12701270
fl4->fl4_dport = dport;
12711271
fl4->fl4_sport = inet->inet_sport;
1272-
up->pending = AF_INET;
1272+
WRITE_ONCE(up->pending, AF_INET);
12731273

12741274
do_append_data:
12751275
up->len += ulen;
@@ -1281,7 +1281,7 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
12811281
else if (!corkreq)
12821282
err = udp_push_pending_frames(sk);
12831283
else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1284-
up->pending = 0;
1284+
WRITE_ONCE(up->pending, 0);
12851285
release_sock(sk);
12861286

12871287
out:
@@ -1319,7 +1319,7 @@ void udp_splice_eof(struct socket *sock)
13191319
struct sock *sk = sock->sk;
13201320
struct udp_sock *up = udp_sk(sk);
13211321

1322-
if (!up->pending || udp_test_bit(CORK, sk))
1322+
if (!READ_ONCE(up->pending) || udp_test_bit(CORK, sk))
13231323
return;
13241324

13251325
lock_sock(sk);

net/ipv6/udp.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,7 @@ static void udp_v6_flush_pending_frames(struct sock *sk)
11351135
udp_flush_pending_frames(sk);
11361136
else if (up->pending) {
11371137
up->len = 0;
1138-
up->pending = 0;
1138+
WRITE_ONCE(up->pending, 0);
11391139
ip6_flush_pending_frames(sk);
11401140
}
11411141
}
@@ -1313,7 +1313,7 @@ static int udp_v6_push_pending_frames(struct sock *sk)
13131313
&inet_sk(sk)->cork.base);
13141314
out:
13151315
up->len = 0;
1316-
up->pending = 0;
1316+
WRITE_ONCE(up->pending, 0);
13171317
return err;
13181318
}
13191319

@@ -1370,7 +1370,7 @@ int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
13701370
default:
13711371
return -EINVAL;
13721372
}
1373-
} else if (!up->pending) {
1373+
} else if (!READ_ONCE(up->pending)) {
13741374
if (sk->sk_state != TCP_ESTABLISHED)
13751375
return -EDESTADDRREQ;
13761376
daddr = &sk->sk_v6_daddr;
@@ -1401,8 +1401,8 @@ int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
14011401
return -EMSGSIZE;
14021402

14031403
getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
1404-
if (up->pending) {
1405-
if (up->pending == AF_INET)
1404+
if (READ_ONCE(up->pending)) {
1405+
if (READ_ONCE(up->pending) == AF_INET)
14061406
return udp_sendmsg(sk, msg, len);
14071407
/*
14081408
* There are pending frames.
@@ -1593,7 +1593,7 @@ int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
15931593
goto out;
15941594
}
15951595

1596-
up->pending = AF_INET6;
1596+
WRITE_ONCE(up->pending, AF_INET6);
15971597

15981598
do_append_data:
15991599
if (ipc6.dontfrag < 0)
@@ -1607,7 +1607,7 @@ int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
16071607
else if (!corkreq)
16081608
err = udp_v6_push_pending_frames(sk);
16091609
else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1610-
up->pending = 0;
1610+
WRITE_ONCE(up->pending, 0);
16111611

16121612
if (err > 0)
16131613
err = inet6_test_bit(RECVERR6, sk) ? net_xmit_errno(err) : 0;
@@ -1648,7 +1648,7 @@ static void udpv6_splice_eof(struct socket *sock)
16481648
struct sock *sk = sock->sk;
16491649
struct udp_sock *up = udp_sk(sk);
16501650

1651-
if (!up->pending || udp_test_bit(CORK, sk))
1651+
if (!READ_ONCE(up->pending) || udp_test_bit(CORK, sk))
16521652
return;
16531653

16541654
lock_sock(sk);

0 commit comments

Comments
 (0)