Skip to content

Commit 802a7dc

Browse files
edumazetummakynes
authored andcommitted
netfilter: conntrack: annotate data-races around ct->timeout
(struct nf_conn)->timeout can be read/written locklessly, add READ_ONCE()/WRITE_ONCE() to prevent load/store tearing. BUG: KCSAN: data-race in __nf_conntrack_alloc / __nf_conntrack_find_get write to 0xffff888132e78c08 of 4 bytes by task 6029 on cpu 0: __nf_conntrack_alloc+0x158/0x280 net/netfilter/nf_conntrack_core.c:1563 init_conntrack+0x1da/0xb30 net/netfilter/nf_conntrack_core.c:1635 resolve_normal_ct+0x502/0x610 net/netfilter/nf_conntrack_core.c:1746 nf_conntrack_in+0x1c5/0x88f net/netfilter/nf_conntrack_core.c:1901 ipv6_conntrack_local+0x19/0x20 net/netfilter/nf_conntrack_proto.c:414 nf_hook_entry_hookfn include/linux/netfilter.h:142 [inline] nf_hook_slow+0x72/0x170 net/netfilter/core.c:619 nf_hook include/linux/netfilter.h:262 [inline] NF_HOOK include/linux/netfilter.h:305 [inline] ip6_xmit+0xa3a/0xa60 net/ipv6/ip6_output.c:324 inet6_csk_xmit+0x1a2/0x1e0 net/ipv6/inet6_connection_sock.c:135 __tcp_transmit_skb+0x132a/0x1840 net/ipv4/tcp_output.c:1402 tcp_transmit_skb net/ipv4/tcp_output.c:1420 [inline] tcp_write_xmit+0x1450/0x4460 net/ipv4/tcp_output.c:2680 __tcp_push_pending_frames+0x68/0x1c0 net/ipv4/tcp_output.c:2864 tcp_push_pending_frames include/net/tcp.h:1897 [inline] tcp_data_snd_check+0x62/0x2e0 net/ipv4/tcp_input.c:5452 tcp_rcv_established+0x880/0x10e0 net/ipv4/tcp_input.c:5947 tcp_v6_do_rcv+0x36e/0xa50 net/ipv6/tcp_ipv6.c:1521 sk_backlog_rcv include/net/sock.h:1030 [inline] __release_sock+0xf2/0x270 net/core/sock.c:2768 release_sock+0x40/0x110 net/core/sock.c:3300 sk_stream_wait_memory+0x435/0x700 net/core/stream.c:145 tcp_sendmsg_locked+0xb85/0x25a0 net/ipv4/tcp.c:1402 tcp_sendmsg+0x2c/0x40 net/ipv4/tcp.c:1440 inet6_sendmsg+0x5f/0x80 net/ipv6/af_inet6.c:644 sock_sendmsg_nosec net/socket.c:704 [inline] sock_sendmsg net/socket.c:724 [inline] __sys_sendto+0x21e/0x2c0 net/socket.c:2036 __do_sys_sendto net/socket.c:2048 [inline] __se_sys_sendto net/socket.c:2044 [inline] __x64_sys_sendto+0x74/0x90 net/socket.c:2044 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 read to 0xffff888132e78c08 of 4 bytes by task 17446 on cpu 1: nf_ct_is_expired include/net/netfilter/nf_conntrack.h:286 [inline] ____nf_conntrack_find net/netfilter/nf_conntrack_core.c:776 [inline] __nf_conntrack_find_get+0x1c7/0xac0 net/netfilter/nf_conntrack_core.c:807 resolve_normal_ct+0x273/0x610 net/netfilter/nf_conntrack_core.c:1734 nf_conntrack_in+0x1c5/0x88f net/netfilter/nf_conntrack_core.c:1901 ipv6_conntrack_local+0x19/0x20 net/netfilter/nf_conntrack_proto.c:414 nf_hook_entry_hookfn include/linux/netfilter.h:142 [inline] nf_hook_slow+0x72/0x170 net/netfilter/core.c:619 nf_hook include/linux/netfilter.h:262 [inline] NF_HOOK include/linux/netfilter.h:305 [inline] ip6_xmit+0xa3a/0xa60 net/ipv6/ip6_output.c:324 inet6_csk_xmit+0x1a2/0x1e0 net/ipv6/inet6_connection_sock.c:135 __tcp_transmit_skb+0x132a/0x1840 net/ipv4/tcp_output.c:1402 __tcp_send_ack+0x1fd/0x300 net/ipv4/tcp_output.c:3956 tcp_send_ack+0x23/0x30 net/ipv4/tcp_output.c:3962 __tcp_ack_snd_check+0x2d8/0x510 net/ipv4/tcp_input.c:5478 tcp_ack_snd_check net/ipv4/tcp_input.c:5523 [inline] tcp_rcv_established+0x8c2/0x10e0 net/ipv4/tcp_input.c:5948 tcp_v6_do_rcv+0x36e/0xa50 net/ipv6/tcp_ipv6.c:1521 sk_backlog_rcv include/net/sock.h:1030 [inline] __release_sock+0xf2/0x270 net/core/sock.c:2768 release_sock+0x40/0x110 net/core/sock.c:3300 tcp_sendpage+0x94/0xb0 net/ipv4/tcp.c:1114 inet_sendpage+0x7f/0xc0 net/ipv4/af_inet.c:833 rds_tcp_xmit+0x376/0x5f0 net/rds/tcp_send.c:118 rds_send_xmit+0xbed/0x1500 net/rds/send.c:367 rds_send_worker+0x43/0x200 net/rds/threads.c:200 process_one_work+0x3fc/0x980 kernel/workqueue.c:2298 worker_thread+0x616/0xa70 kernel/workqueue.c:2445 kthread+0x2c7/0x2e0 kernel/kthread.c:327 ret_from_fork+0x1f/0x30 value changed: 0x00027cc2 -> 0x00000000 Reported by Kernel Concurrency Sanitizer on: CPU: 1 PID: 17446 Comm: kworker/u4:5 Tainted: G W 5.16.0-rc4-syzkaller #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Workqueue: krdsd rds_send_worker Note: I chose an arbitrary commit for the Fixes: tag, because I do not think we need to backport this fix to very old kernels. Fixes: e37542b ("netfilter: conntrack: avoid possible false sharing") Signed-off-by: Eric Dumazet <[email protected]> Reported-by: syzbot <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent d46cea0 commit 802a7dc

File tree

4 files changed

+9
-9
lines changed

4 files changed

+9
-9
lines changed

include/net/netfilter/nf_conntrack.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,14 +276,14 @@ static inline bool nf_is_loopback_packet(const struct sk_buff *skb)
276276
/* jiffies until ct expires, 0 if already expired */
277277
static inline unsigned long nf_ct_expires(const struct nf_conn *ct)
278278
{
279-
s32 timeout = ct->timeout - nfct_time_stamp;
279+
s32 timeout = READ_ONCE(ct->timeout) - nfct_time_stamp;
280280

281281
return timeout > 0 ? timeout : 0;
282282
}
283283

284284
static inline bool nf_ct_is_expired(const struct nf_conn *ct)
285285
{
286-
return (__s32)(ct->timeout - nfct_time_stamp) <= 0;
286+
return (__s32)(READ_ONCE(ct->timeout) - nfct_time_stamp) <= 0;
287287
}
288288

289289
/* use after obtaining a reference count */
@@ -302,7 +302,7 @@ static inline bool nf_ct_should_gc(const struct nf_conn *ct)
302302
static inline void nf_ct_offload_timeout(struct nf_conn *ct)
303303
{
304304
if (nf_ct_expires(ct) < NF_CT_DAY / 2)
305-
ct->timeout = nfct_time_stamp + NF_CT_DAY;
305+
WRITE_ONCE(ct->timeout, nfct_time_stamp + NF_CT_DAY);
306306
}
307307

308308
struct kernel_param;

net/netfilter/nf_conntrack_core.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ bool nf_ct_delete(struct nf_conn *ct, u32 portid, int report)
684684

685685
tstamp = nf_conn_tstamp_find(ct);
686686
if (tstamp) {
687-
s32 timeout = ct->timeout - nfct_time_stamp;
687+
s32 timeout = READ_ONCE(ct->timeout) - nfct_time_stamp;
688688

689689
tstamp->stop = ktime_get_real_ns();
690690
if (timeout < 0)
@@ -1036,7 +1036,7 @@ static int nf_ct_resolve_clash_harder(struct sk_buff *skb, u32 repl_idx)
10361036
}
10371037

10381038
/* We want the clashing entry to go away real soon: 1 second timeout. */
1039-
loser_ct->timeout = nfct_time_stamp + HZ;
1039+
WRITE_ONCE(loser_ct->timeout, nfct_time_stamp + HZ);
10401040

10411041
/* IPS_NAT_CLASH removes the entry automatically on the first
10421042
* reply. Also prevents UDP tracker from moving the entry to
@@ -1560,7 +1560,7 @@ __nf_conntrack_alloc(struct net *net,
15601560
/* save hash for reusing when confirming */
15611561
*(unsigned long *)(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev) = hash;
15621562
ct->status = 0;
1563-
ct->timeout = 0;
1563+
WRITE_ONCE(ct->timeout, 0);
15641564
write_pnet(&ct->ct_net, net);
15651565
memset(&ct->__nfct_init_offset, 0,
15661566
offsetof(struct nf_conn, proto) -

net/netfilter/nf_conntrack_netlink.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1998,7 +1998,7 @@ static int ctnetlink_change_timeout(struct nf_conn *ct,
19981998

19991999
if (timeout > INT_MAX)
20002000
timeout = INT_MAX;
2001-
ct->timeout = nfct_time_stamp + (u32)timeout;
2001+
WRITE_ONCE(ct->timeout, nfct_time_stamp + (u32)timeout);
20022002

20032003
if (test_bit(IPS_DYING_BIT, &ct->status))
20042004
return -ETIME;

net/netfilter/nf_flow_table_core.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ static void flow_offload_fixup_ct_timeout(struct nf_conn *ct)
201201
if (timeout < 0)
202202
timeout = 0;
203203

204-
if (nf_flow_timeout_delta(ct->timeout) > (__s32)timeout)
205-
ct->timeout = nfct_time_stamp + timeout;
204+
if (nf_flow_timeout_delta(READ_ONCE(ct->timeout)) > (__s32)timeout)
205+
WRITE_ONCE(ct->timeout, nfct_time_stamp + timeout);
206206
}
207207

208208
static void flow_offload_fixup_ct_state(struct nf_conn *ct)

0 commit comments

Comments
 (0)