Skip to content

Commit ab38283

Browse files
edumazetgregkh
authored andcommitted
net: af_packet: fix race in PACKET_{R|T}X_RING
[ Upstream commit 5171b37 ] In order to remove the race caught by syzbot [1], we need to lock the socket before using po->tp_version as this could change under us otherwise. This means lock_sock() and release_sock() must be done by packet_set_ring() callers. [1] : BUG: KMSAN: uninit-value in packet_set_ring+0x1254/0x3870 net/packet/af_packet.c:4249 CPU: 0 PID: 20195 Comm: syzkaller707632 Not tainted 4.16.0+ hardkernel#83 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Call Trace: __dump_stack lib/dump_stack.c:17 [inline] dump_stack+0x185/0x1d0 lib/dump_stack.c:53 kmsan_report+0x142/0x240 mm/kmsan/kmsan.c:1067 __msan_warning_32+0x6c/0xb0 mm/kmsan/kmsan_instr.c:676 packet_set_ring+0x1254/0x3870 net/packet/af_packet.c:4249 packet_setsockopt+0x12c6/0x5a90 net/packet/af_packet.c:3662 SYSC_setsockopt+0x4b8/0x570 net/socket.c:1849 SyS_setsockopt+0x76/0xa0 net/socket.c:1828 do_syscall_64+0x309/0x430 arch/x86/entry/common.c:287 entry_SYSCALL_64_after_hwframe+0x3d/0xa2 RIP: 0033:0x449099 RSP: 002b:00007f42b5307ce8 EFLAGS: 00000246 ORIG_RAX: 0000000000000036 RAX: ffffffffffffffda RBX: 000000000070003c RCX: 0000000000449099 RDX: 0000000000000005 RSI: 0000000000000107 RDI: 0000000000000003 RBP: 0000000000700038 R08: 000000000000001c R09: 0000000000000000 R10: 00000000200000c0 R11: 0000000000000246 R12: 0000000000000000 R13: 000000000080eecf R14: 00007f42b53089c0 R15: 0000000000000001 Local variable description: ----req_u@packet_setsockopt Variable was created at: packet_setsockopt+0x13f/0x5a90 net/packet/af_packet.c:3612 SYSC_setsockopt+0x4b8/0x570 net/socket.c:1849 Fixes: f6fb8f1 ("af-packet: TPACKET_V3 flexible buffer implementation.") Signed-off-by: Eric Dumazet <[email protected]> Reported-by: syzbot <[email protected]> Signed-off-by: David S. Miller <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 8be2d38 commit ab38283

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

net/packet/af_packet.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2899,6 +2899,7 @@ static int packet_release(struct socket *sock)
28992899

29002900
packet_flush_mclist(sk);
29012901

2902+
lock_sock(sk);
29022903
if (po->rx_ring.pg_vec) {
29032904
memset(&req_u, 0, sizeof(req_u));
29042905
packet_set_ring(sk, &req_u, 1, 0);
@@ -2908,6 +2909,7 @@ static int packet_release(struct socket *sock)
29082909
memset(&req_u, 0, sizeof(req_u));
29092910
packet_set_ring(sk, &req_u, 1, 1);
29102911
}
2912+
release_sock(sk);
29112913

29122914
f = fanout_release(sk);
29132915

@@ -3577,6 +3579,7 @@ packet_setsockopt(struct socket *sock, int level, int optname, char __user *optv
35773579
union tpacket_req_u req_u;
35783580
int len;
35793581

3582+
lock_sock(sk);
35803583
switch (po->tp_version) {
35813584
case TPACKET_V1:
35823585
case TPACKET_V2:
@@ -3587,14 +3590,21 @@ packet_setsockopt(struct socket *sock, int level, int optname, char __user *optv
35873590
len = sizeof(req_u.req3);
35883591
break;
35893592
}
3590-
if (optlen < len)
3591-
return -EINVAL;
3592-
if (pkt_sk(sk)->has_vnet_hdr)
3593-
return -EINVAL;
3594-
if (copy_from_user(&req_u.req, optval, len))
3595-
return -EFAULT;
3596-
return packet_set_ring(sk, &req_u, 0,
3597-
optname == PACKET_TX_RING);
3593+
if (optlen < len) {
3594+
ret = -EINVAL;
3595+
} else {
3596+
if (pkt_sk(sk)->has_vnet_hdr) {
3597+
ret = -EINVAL;
3598+
} else {
3599+
if (copy_from_user(&req_u.req, optval, len))
3600+
ret = -EFAULT;
3601+
else
3602+
ret = packet_set_ring(sk, &req_u, 0,
3603+
optname == PACKET_TX_RING);
3604+
}
3605+
}
3606+
release_sock(sk);
3607+
return ret;
35983608
}
35993609
case PACKET_COPY_THRESH:
36003610
{
@@ -4144,7 +4154,6 @@ static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
41444154
/* Added to avoid minimal code churn */
41454155
struct tpacket_req *req = &req_u->req;
41464156

4147-
lock_sock(sk);
41484157
/* Opening a Tx-ring is NOT supported in TPACKET_V3 */
41494158
if (!closing && tx_ring && (po->tp_version > TPACKET_V2)) {
41504159
WARN(1, "Tx-ring is not supported.\n");
@@ -4280,7 +4289,6 @@ static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
42804289
if (pg_vec)
42814290
free_pg_vec(pg_vec, order, req->tp_block_nr);
42824291
out:
4283-
release_sock(sk);
42844292
return err;
42854293
}
42864294

0 commit comments

Comments
 (0)