Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2ffd261

Browse files
edumazetgregkh
authored andcommitted
tcp/dccp: fix ireq->opt races
[ Upstream commit c92e8c0 ] syzkaller found another bug in DCCP/TCP stacks [1] For the reasons explained in commit ce10500 ("tcp/dccp: fix ireq->pktopts race"), we need to make sure we do not access ireq->opt unless we own the request sock. Note the opt field is renamed to ireq_opt to ease grep games. [1] BUG: KASAN: use-after-free in ip_queue_xmit+0x1687/0x18e0 net/ipv4/ip_output.c:474 Read of size 1 at addr ffff8801c951039c by task syz-executor5/3295 CPU: 1 PID: 3295 Comm: syz-executor5 Not tainted 4.14.0-rc4+ hardkernel#80 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Call Trace: __dump_stack lib/dump_stack.c:16 [inline] dump_stack+0x194/0x257 lib/dump_stack.c:52 print_address_description+0x73/0x250 mm/kasan/report.c:252 kasan_report_error mm/kasan/report.c:351 [inline] kasan_report+0x25b/0x340 mm/kasan/report.c:409 __asan_report_load1_noabort+0x14/0x20 mm/kasan/report.c:427 ip_queue_xmit+0x1687/0x18e0 net/ipv4/ip_output.c:474 tcp_transmit_skb+0x1ab7/0x3840 net/ipv4/tcp_output.c:1135 tcp_send_ack.part.37+0x3bb/0x650 net/ipv4/tcp_output.c:3587 tcp_send_ack+0x49/0x60 net/ipv4/tcp_output.c:3557 __tcp_ack_snd_check+0x2c6/0x4b0 net/ipv4/tcp_input.c:5072 tcp_ack_snd_check net/ipv4/tcp_input.c:5085 [inline] tcp_rcv_state_process+0x2eff/0x4850 net/ipv4/tcp_input.c:6071 tcp_child_process+0x342/0x990 net/ipv4/tcp_minisocks.c:816 tcp_v4_rcv+0x1827/0x2f80 net/ipv4/tcp_ipv4.c:1682 ip_local_deliver_finish+0x2e2/0xba0 net/ipv4/ip_input.c:216 NF_HOOK include/linux/netfilter.h:249 [inline] ip_local_deliver+0x1ce/0x6e0 net/ipv4/ip_input.c:257 dst_input include/net/dst.h:464 [inline] ip_rcv_finish+0x887/0x19a0 net/ipv4/ip_input.c:397 NF_HOOK include/linux/netfilter.h:249 [inline] ip_rcv+0xc3f/0x1820 net/ipv4/ip_input.c:493 __netif_receive_skb_core+0x1a3e/0x34b0 net/core/dev.c:4476 __netif_receive_skb+0x2c/0x1b0 net/core/dev.c:4514 netif_receive_skb_internal+0x10b/0x670 net/core/dev.c:4587 netif_receive_skb+0xae/0x390 net/core/dev.c:4611 tun_rx_batched.isra.50+0x5ed/0x860 drivers/net/tun.c:1372 tun_get_user+0x249c/0x36d0 drivers/net/tun.c:1766 tun_chr_write_iter+0xbf/0x160 drivers/net/tun.c:1792 call_write_iter include/linux/fs.h:1770 [inline] new_sync_write fs/read_write.c:468 [inline] __vfs_write+0x68a/0x970 fs/read_write.c:481 vfs_write+0x18f/0x510 fs/read_write.c:543 SYSC_write fs/read_write.c:588 [inline] SyS_write+0xef/0x220 fs/read_write.c:580 entry_SYSCALL_64_fastpath+0x1f/0xbe RIP: 0033:0x40c341 RSP: 002b:00007f469523ec10 EFLAGS: 00000293 ORIG_RAX: 0000000000000001 RAX: ffffffffffffffda RBX: 0000000000718000 RCX: 000000000040c341 RDX: 0000000000000037 RSI: 0000000020004000 RDI: 0000000000000015 RBP: 0000000000000086 R08: 0000000000000000 R09: 0000000000000000 R10: 00000000000f4240 R11: 0000000000000293 R12: 00000000004b7fd1 R13: 00000000ffffffff R14: 0000000020000000 R15: 0000000000025000 Allocated by task 3295: save_stack_trace+0x16/0x20 arch/x86/kernel/stacktrace.c:59 save_stack+0x43/0xd0 mm/kasan/kasan.c:447 set_track mm/kasan/kasan.c:459 [inline] kasan_kmalloc+0xad/0xe0 mm/kasan/kasan.c:551 __do_kmalloc mm/slab.c:3725 [inline] __kmalloc+0x162/0x760 mm/slab.c:3734 kmalloc include/linux/slab.h:498 [inline] tcp_v4_save_options include/net/tcp.h:1962 [inline] tcp_v4_init_req+0x2d3/0x3e0 net/ipv4/tcp_ipv4.c:1271 tcp_conn_request+0xf6d/0x3410 net/ipv4/tcp_input.c:6283 tcp_v4_conn_request+0x157/0x210 net/ipv4/tcp_ipv4.c:1313 tcp_rcv_state_process+0x8ea/0x4850 net/ipv4/tcp_input.c:5857 tcp_v4_do_rcv+0x55c/0x7d0 net/ipv4/tcp_ipv4.c:1482 tcp_v4_rcv+0x2d10/0x2f80 net/ipv4/tcp_ipv4.c:1711 ip_local_deliver_finish+0x2e2/0xba0 net/ipv4/ip_input.c:216 NF_HOOK include/linux/netfilter.h:249 [inline] ip_local_deliver+0x1ce/0x6e0 net/ipv4/ip_input.c:257 dst_input include/net/dst.h:464 [inline] ip_rcv_finish+0x887/0x19a0 net/ipv4/ip_input.c:397 NF_HOOK include/linux/netfilter.h:249 [inline] ip_rcv+0xc3f/0x1820 net/ipv4/ip_input.c:493 __netif_receive_skb_core+0x1a3e/0x34b0 net/core/dev.c:4476 __netif_receive_skb+0x2c/0x1b0 net/core/dev.c:4514 netif_receive_skb_internal+0x10b/0x670 net/core/dev.c:4587 netif_receive_skb+0xae/0x390 net/core/dev.c:4611 tun_rx_batched.isra.50+0x5ed/0x860 drivers/net/tun.c:1372 tun_get_user+0x249c/0x36d0 drivers/net/tun.c:1766 tun_chr_write_iter+0xbf/0x160 drivers/net/tun.c:1792 call_write_iter include/linux/fs.h:1770 [inline] new_sync_write fs/read_write.c:468 [inline] __vfs_write+0x68a/0x970 fs/read_write.c:481 vfs_write+0x18f/0x510 fs/read_write.c:543 SYSC_write fs/read_write.c:588 [inline] SyS_write+0xef/0x220 fs/read_write.c:580 entry_SYSCALL_64_fastpath+0x1f/0xbe Freed by task 3306: save_stack_trace+0x16/0x20 arch/x86/kernel/stacktrace.c:59 save_stack+0x43/0xd0 mm/kasan/kasan.c:447 set_track mm/kasan/kasan.c:459 [inline] kasan_slab_free+0x71/0xc0 mm/kasan/kasan.c:524 __cache_free mm/slab.c:3503 [inline] kfree+0xca/0x250 mm/slab.c:3820 inet_sock_destruct+0x59d/0x950 net/ipv4/af_inet.c:157 __sk_destruct+0xfd/0x910 net/core/sock.c:1560 sk_destruct+0x47/0x80 net/core/sock.c:1595 __sk_free+0x57/0x230 net/core/sock.c:1603 sk_free+0x2a/0x40 net/core/sock.c:1614 sock_put include/net/sock.h:1652 [inline] inet_csk_complete_hashdance+0xd5/0xf0 net/ipv4/inet_connection_sock.c:959 tcp_check_req+0xf4d/0x1620 net/ipv4/tcp_minisocks.c:765 tcp_v4_rcv+0x17f6/0x2f80 net/ipv4/tcp_ipv4.c:1675 ip_local_deliver_finish+0x2e2/0xba0 net/ipv4/ip_input.c:216 NF_HOOK include/linux/netfilter.h:249 [inline] ip_local_deliver+0x1ce/0x6e0 net/ipv4/ip_input.c:257 dst_input include/net/dst.h:464 [inline] ip_rcv_finish+0x887/0x19a0 net/ipv4/ip_input.c:397 NF_HOOK include/linux/netfilter.h:249 [inline] ip_rcv+0xc3f/0x1820 net/ipv4/ip_input.c:493 __netif_receive_skb_core+0x1a3e/0x34b0 net/core/dev.c:4476 __netif_receive_skb+0x2c/0x1b0 net/core/dev.c:4514 netif_receive_skb_internal+0x10b/0x670 net/core/dev.c:4587 netif_receive_skb+0xae/0x390 net/core/dev.c:4611 tun_rx_batched.isra.50+0x5ed/0x860 drivers/net/tun.c:1372 tun_get_user+0x249c/0x36d0 drivers/net/tun.c:1766 tun_chr_write_iter+0xbf/0x160 drivers/net/tun.c:1792 call_write_iter include/linux/fs.h:1770 [inline] new_sync_write fs/read_write.c:468 [inline] __vfs_write+0x68a/0x970 fs/read_write.c:481 vfs_write+0x18f/0x510 fs/read_write.c:543 SYSC_write fs/read_write.c:588 [inline] SyS_write+0xef/0x220 fs/read_write.c:580 entry_SYSCALL_64_fastpath+0x1f/0xbe Fixes: e994b2f ("tcp: do not lock listener to process SYN packets") Fixes: 079096f ("tcp/dccp: install syn_recv requests into ehash table") Signed-off-by: Eric Dumazet <[email protected]> Signed-off-by: David S. Miller <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent bcb9ced commit 2ffd261

File tree

7 files changed

+33
-39
lines changed

7 files changed

+33
-39
lines changed

include/net/inet_sock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ struct inet_request_sock {
9696
kmemcheck_bitfield_end(flags);
9797
u32 ir_mark;
9898
union {
99-
struct ip_options_rcu *opt;
99+
struct ip_options_rcu __rcu *ireq_opt;
100100
#if IS_ENABLED(CONFIG_IPV6)
101101
struct {
102102
struct ipv6_txoptions *ipv6_opt;

net/dccp/ipv4.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,7 @@ struct sock *dccp_v4_request_recv_sock(const struct sock *sk,
414414
sk_daddr_set(newsk, ireq->ir_rmt_addr);
415415
sk_rcv_saddr_set(newsk, ireq->ir_loc_addr);
416416
newinet->inet_saddr = ireq->ir_loc_addr;
417-
newinet->inet_opt = ireq->opt;
418-
ireq->opt = NULL;
417+
RCU_INIT_POINTER(newinet->inet_opt, rcu_dereference(ireq->ireq_opt));
419418
newinet->mc_index = inet_iif(skb);
420419
newinet->mc_ttl = ip_hdr(skb)->ttl;
421420
newinet->inet_id = jiffies;
@@ -430,7 +429,10 @@ struct sock *dccp_v4_request_recv_sock(const struct sock *sk,
430429
if (__inet_inherit_port(sk, newsk) < 0)
431430
goto put_and_exit;
432431
*own_req = inet_ehash_nolisten(newsk, req_to_sk(req_unhash));
433-
432+
if (*own_req)
433+
ireq->ireq_opt = NULL;
434+
else
435+
newinet->inet_opt = NULL;
434436
return newsk;
435437

436438
exit_overflow:
@@ -441,6 +443,7 @@ struct sock *dccp_v4_request_recv_sock(const struct sock *sk,
441443
__NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENDROPS);
442444
return NULL;
443445
put_and_exit:
446+
newinet->inet_opt = NULL;
444447
inet_csk_prepare_forced_close(newsk);
445448
dccp_done(newsk);
446449
goto exit;
@@ -492,7 +495,7 @@ static int dccp_v4_send_response(const struct sock *sk, struct request_sock *req
492495
ireq->ir_rmt_addr);
493496
err = ip_build_and_send_pkt(skb, sk, ireq->ir_loc_addr,
494497
ireq->ir_rmt_addr,
495-
ireq->opt);
498+
rcu_dereference(ireq->ireq_opt));
496499
err = net_xmit_eval(err);
497500
}
498501

@@ -548,7 +551,7 @@ static void dccp_v4_ctl_send_reset(const struct sock *sk, struct sk_buff *rxskb)
548551
static void dccp_v4_reqsk_destructor(struct request_sock *req)
549552
{
550553
dccp_feat_list_purge(&dccp_rsk(req)->dreq_featneg);
551-
kfree(inet_rsk(req)->opt);
554+
kfree(rcu_dereference_protected(inet_rsk(req)->ireq_opt, 1));
552555
}
553556

554557
void dccp_syn_ack_timeout(const struct request_sock *req)

net/ipv4/cipso_ipv4.c

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,7 +1943,7 @@ int cipso_v4_req_setattr(struct request_sock *req,
19431943
buf = NULL;
19441944

19451945
req_inet = inet_rsk(req);
1946-
opt = xchg(&req_inet->opt, opt);
1946+
opt = xchg((__force struct ip_options_rcu **)&req_inet->ireq_opt, opt);
19471947
if (opt)
19481948
kfree_rcu(opt, rcu);
19491949

@@ -1965,11 +1965,13 @@ int cipso_v4_req_setattr(struct request_sock *req,
19651965
* values on failure.
19661966
*
19671967
*/
1968-
static int cipso_v4_delopt(struct ip_options_rcu **opt_ptr)
1968+
static int cipso_v4_delopt(struct ip_options_rcu __rcu **opt_ptr)
19691969
{
1970+
struct ip_options_rcu *opt = rcu_dereference_protected(*opt_ptr, 1);
19701971
int hdr_delta = 0;
1971-
struct ip_options_rcu *opt = *opt_ptr;
19721972

1973+
if (!opt || opt->opt.cipso == 0)
1974+
return 0;
19731975
if (opt->opt.srr || opt->opt.rr || opt->opt.ts || opt->opt.router_alert) {
19741976
u8 cipso_len;
19751977
u8 cipso_off;
@@ -2031,14 +2033,10 @@ static int cipso_v4_delopt(struct ip_options_rcu **opt_ptr)
20312033
*/
20322034
void cipso_v4_sock_delattr(struct sock *sk)
20332035
{
2034-
int hdr_delta;
2035-
struct ip_options_rcu *opt;
20362036
struct inet_sock *sk_inet;
2037+
int hdr_delta;
20372038

20382039
sk_inet = inet_sk(sk);
2039-
opt = rcu_dereference_protected(sk_inet->inet_opt, 1);
2040-
if (!opt || opt->opt.cipso == 0)
2041-
return;
20422040

20432041
hdr_delta = cipso_v4_delopt(&sk_inet->inet_opt);
20442042
if (sk_inet->is_icsk && hdr_delta > 0) {
@@ -2058,15 +2056,7 @@ void cipso_v4_sock_delattr(struct sock *sk)
20582056
*/
20592057
void cipso_v4_req_delattr(struct request_sock *req)
20602058
{
2061-
struct ip_options_rcu *opt;
2062-
struct inet_request_sock *req_inet;
2063-
2064-
req_inet = inet_rsk(req);
2065-
opt = req_inet->opt;
2066-
if (!opt || opt->opt.cipso == 0)
2067-
return;
2068-
2069-
cipso_v4_delopt(&req_inet->opt);
2059+
cipso_v4_delopt(&inet_rsk(req)->ireq_opt);
20702060
}
20712061

20722062
/**

net/ipv4/inet_connection_sock.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,10 @@ struct dst_entry *inet_csk_route_req(const struct sock *sk,
407407
{
408408
const struct inet_request_sock *ireq = inet_rsk(req);
409409
struct net *net = read_pnet(&ireq->ireq_net);
410-
struct ip_options_rcu *opt = ireq->opt;
410+
struct ip_options_rcu *opt;
411411
struct rtable *rt;
412412

413+
opt = rcu_dereference(ireq->ireq_opt);
413414
flowi4_init_output(fl4, ireq->ir_iif, ireq->ir_mark,
414415
RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE,
415416
sk->sk_protocol, inet_sk_flowi_flags(sk),
@@ -443,10 +444,9 @@ struct dst_entry *inet_csk_route_child_sock(const struct sock *sk,
443444
struct flowi4 *fl4;
444445
struct rtable *rt;
445446

447+
opt = rcu_dereference(ireq->ireq_opt);
446448
fl4 = &newinet->cork.fl.u.ip4;
447449

448-
rcu_read_lock();
449-
opt = rcu_dereference(newinet->inet_opt);
450450
flowi4_init_output(fl4, ireq->ir_iif, ireq->ir_mark,
451451
RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE,
452452
sk->sk_protocol, inet_sk_flowi_flags(sk),
@@ -459,13 +459,11 @@ struct dst_entry *inet_csk_route_child_sock(const struct sock *sk,
459459
goto no_route;
460460
if (opt && opt->opt.is_strictroute && rt->rt_uses_gateway)
461461
goto route_err;
462-
rcu_read_unlock();
463462
return &rt->dst;
464463

465464
route_err:
466465
ip_rt_put(rt);
467466
no_route:
468-
rcu_read_unlock();
469467
__IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
470468
return NULL;
471469
}

net/ipv4/syncookies.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
354354
/* We throwed the options of the initial SYN away, so we hope
355355
* the ACK carries the same options again (see RFC1122 4.2.3.8)
356356
*/
357-
ireq->opt = tcp_v4_save_options(skb);
357+
RCU_INIT_POINTER(ireq->ireq_opt, tcp_v4_save_options(skb));
358358

359359
if (security_inet_conn_request(sk, skb, req)) {
360360
reqsk_free(req);

net/ipv4/tcp_input.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6237,7 +6237,7 @@ struct request_sock *inet_reqsk_alloc(const struct request_sock_ops *ops,
62376237
struct inet_request_sock *ireq = inet_rsk(req);
62386238

62396239
kmemcheck_annotate_bitfield(ireq, flags);
6240-
ireq->opt = NULL;
6240+
ireq->ireq_opt = NULL;
62416241
#if IS_ENABLED(CONFIG_IPV6)
62426242
ireq->pktopts = NULL;
62436243
#endif

net/ipv4/tcp_ipv4.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ static int tcp_v4_send_synack(const struct sock *sk, struct dst_entry *dst,
861861

862862
err = ip_build_and_send_pkt(skb, sk, ireq->ir_loc_addr,
863863
ireq->ir_rmt_addr,
864-
ireq->opt);
864+
rcu_dereference(ireq->ireq_opt));
865865
err = net_xmit_eval(err);
866866
}
867867

@@ -873,7 +873,7 @@ static int tcp_v4_send_synack(const struct sock *sk, struct dst_entry *dst,
873873
*/
874874
static void tcp_v4_reqsk_destructor(struct request_sock *req)
875875
{
876-
kfree(inet_rsk(req)->opt);
876+
kfree(rcu_dereference_protected(inet_rsk(req)->ireq_opt, 1));
877877
}
878878

879879
#ifdef CONFIG_TCP_MD5SIG
@@ -1199,7 +1199,7 @@ static void tcp_v4_init_req(struct request_sock *req,
11991199

12001200
sk_rcv_saddr_set(req_to_sk(req), ip_hdr(skb)->daddr);
12011201
sk_daddr_set(req_to_sk(req), ip_hdr(skb)->saddr);
1202-
ireq->opt = tcp_v4_save_options(skb);
1202+
RCU_INIT_POINTER(ireq->ireq_opt, tcp_v4_save_options(skb));
12031203
}
12041204

12051205
static struct dst_entry *tcp_v4_route_req(const struct sock *sk,
@@ -1295,10 +1295,9 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,
12951295
sk_daddr_set(newsk, ireq->ir_rmt_addr);
12961296
sk_rcv_saddr_set(newsk, ireq->ir_loc_addr);
12971297
newsk->sk_bound_dev_if = ireq->ir_iif;
1298-
newinet->inet_saddr = ireq->ir_loc_addr;
1299-
inet_opt = ireq->opt;
1300-
rcu_assign_pointer(newinet->inet_opt, inet_opt);
1301-
ireq->opt = NULL;
1298+
newinet->inet_saddr = ireq->ir_loc_addr;
1299+
inet_opt = rcu_dereference(ireq->ireq_opt);
1300+
RCU_INIT_POINTER(newinet->inet_opt, inet_opt);
13021301
newinet->mc_index = inet_iif(skb);
13031302
newinet->mc_ttl = ip_hdr(skb)->ttl;
13041303
newinet->rcv_tos = ip_hdr(skb)->tos;
@@ -1346,9 +1345,12 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,
13461345
if (__inet_inherit_port(sk, newsk) < 0)
13471346
goto put_and_exit;
13481347
*own_req = inet_ehash_nolisten(newsk, req_to_sk(req_unhash));
1349-
if (*own_req)
1348+
if (likely(*own_req)) {
13501349
tcp_move_syn(newtp, req);
1351-
1350+
ireq->ireq_opt = NULL;
1351+
} else {
1352+
newinet->inet_opt = NULL;
1353+
}
13521354
return newsk;
13531355

13541356
exit_overflow:
@@ -1359,6 +1361,7 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,
13591361
tcp_listendrop(sk);
13601362
return NULL;
13611363
put_and_exit:
1364+
newinet->inet_opt = NULL;
13621365
inet_csk_prepare_forced_close(newsk);
13631366
tcp_done(newsk);
13641367
goto exit;

0 commit comments

Comments
 (0)