* [PATCH v2 net 1/5] tcp: Restart iteration after removing reqsk in inet_twsk_purge().
2024-02-27 1:10 [PATCH v2 net 0/5] tcp/rds: Fix use-after-free around kernel TCP reqsk Kuniyuki Iwashima
@ 2024-02-27 1:10 ` Kuniyuki Iwashima
2024-02-27 1:10 ` [PATCH v2 net 2/5] Revert "tcp: Clean up kernel listener's reqsk in inet_twsk_purge()" Kuniyuki Iwashima
` (3 subsequent siblings)
4 siblings, 0 replies; 9+ messages in thread
From: Kuniyuki Iwashima @ 2024-02-27 1:10 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Allison Henderson
Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev, linux-rdma,
rds-devel
Commit 740ea3c4a0b2 ("tcp: Clean up kernel listener's reqsk in
inet_twsk_purge()") added changes in inet_twsk_purge() to purge
reqsk in per-netns ehash during netns dismantle.
inet_csk_reqsk_queue_drop_and_put() will remove reqsk from per-netns
ehash, but the iteration uses sk_nulls_for_each_rcu(), which is not
safe.
After removing reqsk, we need to restart iteration.
Note that we need not check net->ns.count here because per-netns
ehash does not have reqsk in other live netns.
This change will be removed by the following patch.
Fixes: 740ea3c4a0b2 ("tcp: Clean up kernel listener's reqsk in inet_twsk_purge()")
Reported-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
net/ipv4/inet_timewait_sock.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/net/ipv4/inet_timewait_sock.c b/net/ipv4/inet_timewait_sock.c
index 5befa4de5b24..00cbebaa2c68 100644
--- a/net/ipv4/inet_timewait_sock.c
+++ b/net/ipv4/inet_timewait_sock.c
@@ -287,6 +287,8 @@ void inet_twsk_purge(struct inet_hashinfo *hashinfo, int family)
struct request_sock *req = inet_reqsk(sk);
inet_csk_reqsk_queue_drop_and_put(req->rsk_listener, req);
+
+ goto restart;
}
continue;
--
2.30.2
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH v2 net 2/5] Revert "tcp: Clean up kernel listener's reqsk in inet_twsk_purge()"
2024-02-27 1:10 [PATCH v2 net 0/5] tcp/rds: Fix use-after-free around kernel TCP reqsk Kuniyuki Iwashima
2024-02-27 1:10 ` [PATCH v2 net 1/5] tcp: Restart iteration after removing reqsk in inet_twsk_purge() Kuniyuki Iwashima
@ 2024-02-27 1:10 ` Kuniyuki Iwashima
2024-02-27 1:10 ` [PATCH v2 net 3/5] net: Convert @kern of __sock_create() to enum Kuniyuki Iwashima
` (2 subsequent siblings)
4 siblings, 0 replies; 9+ messages in thread
From: Kuniyuki Iwashima @ 2024-02-27 1:10 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Allison Henderson
Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev, linux-rdma,
rds-devel
This reverts commit 740ea3c4a0b2e326b23d7cdf05472a0e92aa39bc.
The change actually fixed a use-after-free of struct net by kernel
listener's reqsk in per-netns ehash.
However, the fix was incomplete, as the same issue exists for the
global ehash.
We should have fixed it on the RDS side without slowing down netns
dismantle for the normal TCP use case.
The following patches will fix the issue on the RDS side.
Fixes: 740ea3c4a0b2 ("tcp: Clean up kernel listener's reqsk in inet_twsk_purge()")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
net/ipv4/inet_timewait_sock.c | 16 +---------------
net/ipv4/tcp_minisocks.c | 9 ++++-----
2 files changed, 5 insertions(+), 20 deletions(-)
diff --git a/net/ipv4/inet_timewait_sock.c b/net/ipv4/inet_timewait_sock.c
index 00cbebaa2c68..6b65f5f97478 100644
--- a/net/ipv4/inet_timewait_sock.c
+++ b/net/ipv4/inet_timewait_sock.c
@@ -277,22 +277,8 @@ void inet_twsk_purge(struct inet_hashinfo *hashinfo, int family)
rcu_read_lock();
restart:
sk_nulls_for_each_rcu(sk, node, &head->chain) {
- if (sk->sk_state != TCP_TIME_WAIT) {
- /* A kernel listener socket might not hold refcnt for net,
- * so reqsk_timer_handler() could be fired after net is
- * freed. Userspace listener and reqsk never exist here.
- */
- if (unlikely(sk->sk_state == TCP_NEW_SYN_RECV &&
- hashinfo->pernet)) {
- struct request_sock *req = inet_reqsk(sk);
-
- inet_csk_reqsk_queue_drop_and_put(req->rsk_listener, req);
-
- goto restart;
- }
-
+ if (sk->sk_state != TCP_TIME_WAIT)
continue;
- }
tw = inet_twsk(sk);
if ((tw->tw_family != family) ||
diff --git a/net/ipv4/tcp_minisocks.c b/net/ipv4/tcp_minisocks.c
index 9e85f2a0bddd..baecfa4c70ef 100644
--- a/net/ipv4/tcp_minisocks.c
+++ b/net/ipv4/tcp_minisocks.c
@@ -394,14 +394,13 @@ void tcp_twsk_purge(struct list_head *net_exit_list, int family)
struct net *net;
list_for_each_entry(net, net_exit_list, exit_list) {
+ /* The last refcount is decremented in tcp_sk_exit_batch() */
+ if (refcount_read(&net->ipv4.tcp_death_row.tw_refcount) == 1)
+ continue;
+
if (net->ipv4.tcp_death_row.hashinfo->pernet) {
- /* Even if tw_refcount == 1, we must clean up kernel reqsk */
inet_twsk_purge(net->ipv4.tcp_death_row.hashinfo, family);
} else if (!purged_once) {
- /* The last refcount is decremented in tcp_sk_exit_batch() */
- if (refcount_read(&net->ipv4.tcp_death_row.tw_refcount) == 1)
- continue;
-
inet_twsk_purge(&tcp_hashinfo, family);
purged_once = true;
}
--
2.30.2
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH v2 net 3/5] net: Convert @kern of __sock_create() to enum.
2024-02-27 1:10 [PATCH v2 net 0/5] tcp/rds: Fix use-after-free around kernel TCP reqsk Kuniyuki Iwashima
2024-02-27 1:10 ` [PATCH v2 net 1/5] tcp: Restart iteration after removing reqsk in inet_twsk_purge() Kuniyuki Iwashima
2024-02-27 1:10 ` [PATCH v2 net 2/5] Revert "tcp: Clean up kernel listener's reqsk in inet_twsk_purge()" Kuniyuki Iwashima
@ 2024-02-27 1:10 ` Kuniyuki Iwashima
2024-02-29 21:51 ` David Laight
2024-02-27 1:10 ` [PATCH v2 net 4/5] rds: tcp: Fix use-after-free of net in reqsk_timer_handler() Kuniyuki Iwashima
2024-02-27 1:10 ` [PATCH v2 net 5/5] tcp: Add assertion for reqsk->rsk_listener->sk_net_refcnt Kuniyuki Iwashima
4 siblings, 1 reply; 9+ messages in thread
From: Kuniyuki Iwashima @ 2024-02-27 1:10 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Allison Henderson
Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev, linux-rdma,
rds-devel
Historically, syzbot has reported many use-after-free of struct
net by kernel sockets.
In most cases, the root cause was a timer kicked by a kernel socket
which does not hold netns refcount nor clean it up during netns
dismantle.
This patch converts the @kern argument of __sock_create() to enum
so that we can pass SOCKET_KERN_NET_REF and later sk_alloc() can
hold refcount of net for kernel sockets.
We pass !!kern to security_socket(_post)?_create() but kern as is
to pf->create() because 3 functions (atalk_create(), inet_create(),
inet6_create()) use it for the following check:
if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
The conversion for rest of the callers of __sock_create() and
sk_alloc() will be completed in net-next.git as the change is
too large to backport.
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
include/linux/net.h | 6 ++++++
net/core/sock.c | 2 +-
net/socket.c | 11 ++++++-----
3 files changed, 13 insertions(+), 6 deletions(-)
diff --git a/include/linux/net.h b/include/linux/net.h
index c9b4a63791a4..62ef0954be75 100644
--- a/include/linux/net.h
+++ b/include/linux/net.h
@@ -245,6 +245,12 @@ enum {
SOCK_WAKE_URG,
};
+enum socket_user {
+ SOCKET_USER,
+ SOCKET_KERN,
+ SOCKET_KERN_NET_REF,
+};
+
int sock_wake_async(struct socket_wq *sk_wq, int how, int band);
int sock_register(const struct net_proto_family *fam);
void sock_unregister(int family);
diff --git a/net/core/sock.c b/net/core/sock.c
index 5e78798456fd..6f417cdbcf50 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -2138,7 +2138,7 @@ struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
sk->sk_prot = sk->sk_prot_creator = prot;
sk->sk_kern_sock = kern;
sock_lock_init(sk);
- sk->sk_net_refcnt = kern ? 0 : 1;
+ sk->sk_net_refcnt = kern != SOCKET_KERN;
if (likely(sk->sk_net_refcnt)) {
get_net_track(net, &sk->ns_tracker, priority);
sock_inuse_add(net, 1);
diff --git a/net/socket.c b/net/socket.c
index ed3df2f749bf..f5ec613d9e3b 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -1489,7 +1489,7 @@ EXPORT_SYMBOL(sock_wake_async);
* @type: communication type (SOCK_STREAM, ...)
* @protocol: protocol (0, ...)
* @res: new socket
- * @kern: boolean for kernel space sockets
+ * @kern: enum for kernel space sockets
*
* Creates a new socket and assigns it to @res, passing through LSM.
* Returns 0 or an error. On failure @res is set to %NULL. @kern must
@@ -1523,7 +1523,7 @@ int __sock_create(struct net *net, int family, int type, int protocol,
family = PF_PACKET;
}
- err = security_socket_create(family, type, protocol, kern);
+ err = security_socket_create(family, type, protocol, !!kern);
if (err)
return err;
@@ -1584,7 +1584,7 @@ int __sock_create(struct net *net, int family, int type, int protocol,
* module can have its refcnt decremented
*/
module_put(pf->owner);
- err = security_socket_post_create(sock, family, type, protocol, kern);
+ err = security_socket_post_create(sock, family, type, protocol, !!kern);
if (err)
goto out_sock_release;
*res = sock;
@@ -1619,7 +1619,8 @@ EXPORT_SYMBOL(__sock_create);
int sock_create(int family, int type, int protocol, struct socket **res)
{
- return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
+ return __sock_create(current->nsproxy->net_ns, family, type, protocol,
+ res, SOCKET_USER);
}
EXPORT_SYMBOL(sock_create);
@@ -1637,7 +1638,7 @@ EXPORT_SYMBOL(sock_create);
int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res)
{
- return __sock_create(net, family, type, protocol, res, 1);
+ return __sock_create(net, family, type, protocol, res, SOCKET_KERN);
}
EXPORT_SYMBOL(sock_create_kern);
--
2.30.2
^ permalink raw reply related [flat|nested] 9+ messages in thread* RE: [PATCH v2 net 3/5] net: Convert @kern of __sock_create() to enum.
2024-02-27 1:10 ` [PATCH v2 net 3/5] net: Convert @kern of __sock_create() to enum Kuniyuki Iwashima
@ 2024-02-29 21:51 ` David Laight
0 siblings, 0 replies; 9+ messages in thread
From: David Laight @ 2024-02-29 21:51 UTC (permalink / raw)
To: 'Kuniyuki Iwashima', David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Allison Henderson
Cc: Kuniyuki Iwashima, netdev@vger.kernel.org,
linux-rdma@vger.kernel.org, rds-devel@oss.oracle.com
From: Kuniyuki Iwashima
> Sent: 27 February 2024 01:11
> Subject: [PATCH v2 net 3/5] net: Convert @kern of __sock_create() to enum.
Should probably be (something like):
Allow __sock_create() create kernel sockets that hold a reference
to the network namespace.
> Historically, syzbot has reported many use-after-free of struct
> net by kernel sockets.
>
> In most cases, the root cause was a timer kicked by a kernel socket
> which does not hold netns refcount nor clean it up during netns
> dismantle.
>
> This patch converts the @kern argument of __sock_create() to enum
> so that we can pass SOCKET_KERN_NET_REF and later sk_alloc() can
> hold refcount of net for kernel sockets.
I think you should add a 'hold netns' parameter to sock_create_kern().
Indeed, that is likely to be used for a real connection
(which would need the 'hold netns') and code that doesn't need it
(because the socket is some internal housekeeping socket) could
directly call __sock_create().
Fortunately both functions are exported non-gpl.
I've this comment in a driver...
/* sock_create_kern() creates a socket that doesn't hold a reference
* to the namespace (they get used for sockets needed by the protocol
* stack code itself).
* We need a socket that holds a reference to the namespace, so create
* a 'user' socket in a specific namespace.
* This adds an extra security check which we should pass because all the
* sockets are created by kernel threads.
*/
rval = __sock_create(net, family, type, protocol, sockp, 0);
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2 net 4/5] rds: tcp: Fix use-after-free of net in reqsk_timer_handler().
2024-02-27 1:10 [PATCH v2 net 0/5] tcp/rds: Fix use-after-free around kernel TCP reqsk Kuniyuki Iwashima
` (2 preceding siblings ...)
2024-02-27 1:10 ` [PATCH v2 net 3/5] net: Convert @kern of __sock_create() to enum Kuniyuki Iwashima
@ 2024-02-27 1:10 ` Kuniyuki Iwashima
2024-02-27 12:06 ` Eric Dumazet
2024-02-27 1:10 ` [PATCH v2 net 5/5] tcp: Add assertion for reqsk->rsk_listener->sk_net_refcnt Kuniyuki Iwashima
4 siblings, 1 reply; 9+ messages in thread
From: Kuniyuki Iwashima @ 2024-02-27 1:10 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Allison Henderson
Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev, linux-rdma,
rds-devel, syzkaller
syzkaller reported a warning of netns tracker [0] followed by KASAN
splat [1] and another ref tracker warning [1].
syzkaller could not find a repro, but in the log, the only suspicious
sequence was as follows:
18:26:22 executing program 1:
r0 = socket$inet6_mptcp(0xa, 0x1, 0x106)
...
connect$inet6(r0, &(0x7f0000000080)={0xa, 0x4001, 0x0, @loopback}, 0x1c) (async)
The notable thing here is 0x4001 in connect(), which is RDS_TCP_PORT.
So, the scenario would be:
1. unshare(CLONE_NEWNET) creates a per netns tcp listener in
rds_tcp_listen_init().
2. syz-executor connect()s to it and creates a reqsk.
3. syz-executor exit()s immediately.
4. netns is dismantled. [0]
5. reqsk timer is fired, and UAF happens while freeing reqsk. [1]
6. listener is freed after RCU grace period. [2]
Basically, reqsk assumes that the listener guarantees netns safety
until all reqsk timers are expired by holding the listener's refcount.
However, this was not the case for kernel sockets.
Commit 740ea3c4a0b2 ("tcp: Clean up kernel listener's reqsk in
inet_twsk_purge()") fixed this issue only for per-netns ehash, but
the issue still exists for the global ehash.
We can apply the same fix, but this issue is specific to RDS.
Instead of iterating ehash and purging reqsk during netns dismantle,
let's hold netns refcount for the kernel listener.
[0]:
ref_tracker: net notrefcnt@0000000065449cc3 has 1/1 users at
sk_alloc (./include/net/net_namespace.h:337 net/core/sock.c:2146)
inet6_create (net/ipv6/af_inet6.c:192 net/ipv6/af_inet6.c:119)
__sock_create (net/socket.c:1572)
rds_tcp_listen_init (net/rds/tcp_listen.c:279)
rds_tcp_init_net (net/rds/tcp.c:577)
ops_init (net/core/net_namespace.c:137)
setup_net (net/core/net_namespace.c:340)
copy_net_ns (net/core/net_namespace.c:497)
create_new_namespaces (kernel/nsproxy.c:110)
unshare_nsproxy_namespaces (kernel/nsproxy.c:228 (discriminator 4))
ksys_unshare (kernel/fork.c:3429)
__x64_sys_unshare (kernel/fork.c:3496)
do_syscall_64 (arch/x86/entry/common.c:52 arch/x86/entry/common.c:83)
entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:129)
...
WARNING: CPU: 0 PID: 27 at lib/ref_tracker.c:179 ref_tracker_dir_exit (lib/ref_tracker.c:179)
[1]:
BUG: KASAN: slab-use-after-free in inet_csk_reqsk_queue_drop (./include/net/inet_hashtables.h:180 net/ipv4/inet_connection_sock.c:952 net/ipv4/inet_connection_sock.c:966)
Read of size 8 at addr ffff88801b370400 by task swapper/0/0
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.16.0-0-gd239552ce722-prebuilt.qemu.org 04/01/2014
Call Trace:
<IRQ>
dump_stack_lvl (lib/dump_stack.c:107 (discriminator 1))
print_report (mm/kasan/report.c:378 mm/kasan/report.c:488)
kasan_report (mm/kasan/report.c:603)
inet_csk_reqsk_queue_drop (./include/net/inet_hashtables.h:180 net/ipv4/inet_connection_sock.c:952 net/ipv4/inet_connection_sock.c:966)
reqsk_timer_handler (net/ipv4/inet_connection_sock.c:979 net/ipv4/inet_connection_sock.c:1092)
call_timer_fn (./arch/x86/include/asm/jump_label.h:27 ./include/linux/jump_label.h:207 ./include/trace/events/timer.h:127 kernel/time/timer.c:1701)
__run_timers.part.0 (kernel/time/timer.c:1752 kernel/time/timer.c:2038)
run_timer_softirq (kernel/time/timer.c:2053)
__do_softirq (./arch/x86/include/asm/jump_label.h:27 ./include/linux/jump_label.h:207 ./include/trace/events/irq.h:142 kernel/softirq.c:554)
irq_exit_rcu (kernel/softirq.c:427 kernel/softirq.c:632 kernel/softirq.c:644)
sysvec_apic_timer_interrupt (arch/x86/kernel/apic/apic.c:1076 (discriminator 14))
</IRQ>
Allocated by task 258 on cpu 0 at 83.612050s:
kasan_save_stack (mm/kasan/common.c:48)
kasan_save_track (mm/kasan/common.c:68)
__kasan_slab_alloc (mm/kasan/common.c:343)
kmem_cache_alloc (mm/slub.c:3813 mm/slub.c:3860 mm/slub.c:3867)
copy_net_ns (./include/linux/slab.h:701 net/core/net_namespace.c:421 net/core/net_namespace.c:480)
create_new_namespaces (kernel/nsproxy.c:110)
unshare_nsproxy_namespaces (kernel/nsproxy.c:228 (discriminator 4))
ksys_unshare (kernel/fork.c:3429)
__x64_sys_unshare (kernel/fork.c:3496)
do_syscall_64 (arch/x86/entry/common.c:52 arch/x86/entry/common.c:83)
entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:129)
Freed by task 27 on cpu 0 at 329.158864s:
kasan_save_stack (mm/kasan/common.c:48)
kasan_save_track (mm/kasan/common.c:68)
kasan_save_free_info (mm/kasan/generic.c:643)
__kasan_slab_free (mm/kasan/common.c:265)
kmem_cache_free (mm/slub.c:4299 mm/slub.c:4363)
cleanup_net (net/core/net_namespace.c:456 net/core/net_namespace.c:446 net/core/net_namespace.c:639)
process_one_work (kernel/workqueue.c:2638)
worker_thread (kernel/workqueue.c:2700 kernel/workqueue.c:2787)
kthread (kernel/kthread.c:388)
ret_from_fork (arch/x86/kernel/process.c:153)
ret_from_fork_asm (arch/x86/entry/entry_64.S:250)
The buggy address belongs to the object at ffff88801b370000
which belongs to the cache net_namespace of size 4352
The buggy address is located 1024 bytes inside of
freed 4352-byte region [ffff88801b370000, ffff88801b371100)
[2]:
WARNING: CPU: 0 PID: 95 at lib/ref_tracker.c:228 ref_tracker_free (lib/ref_tracker.c:228 (discriminator 1))
Modules linked in:
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.16.0-0-gd239552ce722-prebuilt.qemu.org 04/01/2014
RIP: 0010:ref_tracker_free (lib/ref_tracker.c:228 (discriminator 1))
...
Call Trace:
<IRQ>
__sk_destruct (./include/net/net_namespace.h:353 net/core/sock.c:2204)
rcu_core (./arch/x86/include/asm/preempt.h:26 kernel/rcu/tree.c:2165 kernel/rcu/tree.c:2433)
__do_softirq (./arch/x86/include/asm/jump_label.h:27 ./include/linux/jump_label.h:207 ./include/trace/events/irq.h:142 kernel/softirq.c:554)
irq_exit_rcu (kernel/softirq.c:427 kernel/softirq.c:632 kernel/softirq.c:644)
sysvec_apic_timer_interrupt (arch/x86/kernel/apic/apic.c:1076 (discriminator 14))
</IRQ>
Reported-by: syzkaller <syzkaller@googlegroups.com>
Suggested-by: Eric Dumazet <edumazet@google.com>
Fixes: 467fa15356ac ("RDS-TCP: Support multiple RDS-TCP listen endpoints, one per netns.")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
net/rds/tcp_listen.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/net/rds/tcp_listen.c b/net/rds/tcp_listen.c
index 05008ce5c421..2d40e523322c 100644
--- a/net/rds/tcp_listen.c
+++ b/net/rds/tcp_listen.c
@@ -274,8 +274,8 @@ struct socket *rds_tcp_listen_init(struct net *net, bool isv6)
int addr_len;
int ret;
- ret = sock_create_kern(net, isv6 ? PF_INET6 : PF_INET, SOCK_STREAM,
- IPPROTO_TCP, &sock);
+ ret = __sock_create(net, isv6 ? PF_INET6 : PF_INET, SOCK_STREAM,
+ IPPROTO_TCP, &sock, SOCKET_KERN_NET_REF);
if (ret < 0) {
rdsdebug("could not create %s listener socket: %d\n",
isv6 ? "IPv6" : "IPv4", ret);
--
2.30.2
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH v2 net 4/5] rds: tcp: Fix use-after-free of net in reqsk_timer_handler().
2024-02-27 1:10 ` [PATCH v2 net 4/5] rds: tcp: Fix use-after-free of net in reqsk_timer_handler() Kuniyuki Iwashima
@ 2024-02-27 12:06 ` Eric Dumazet
2024-02-28 2:24 ` Kuniyuki Iwashima
0 siblings, 1 reply; 9+ messages in thread
From: Eric Dumazet @ 2024-02-27 12:06 UTC (permalink / raw)
To: Kuniyuki Iwashima
Cc: David S. Miller, Jakub Kicinski, Paolo Abeni, Allison Henderson,
Kuniyuki Iwashima, netdev, linux-rdma, rds-devel, syzkaller
On Tue, Feb 27, 2024 at 2:12 AM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
>
> syzkaller reported a warning of netns tracker [0] followed by KASAN
> splat [1] and another ref tracker warning [1].
>
> syzkaller could not find a repro, but in the log, the only suspicious
> sequence was as follows:
>
> 18:26:22 executing program 1:
> r0 = socket$inet6_mptcp(0xa, 0x1, 0x106)
> ...
> connect$inet6(r0, &(0x7f0000000080)={0xa, 0x4001, 0x0, @loopback}, 0x1c) (async)
>
> The notable thing here is 0x4001 in connect(), which is RDS_TCP_PORT.
>
> So, the scenario would be:
>
> 1. unshare(CLONE_NEWNET) creates a per netns tcp listener in
> rds_tcp_listen_init().
> 2. syz-executor connect()s to it and creates a reqsk.
> 3. syz-executor exit()s immediately.
> 4. netns is dismantled. [0]
> 5. reqsk timer is fired, and UAF happens while freeing reqsk. [1]
> 6. listener is freed after RCU grace period. [2]
>
> Basically, reqsk assumes that the listener guarantees netns safety
> until all reqsk timers are expired by holding the listener's refcount.
> However, this was not the case for kernel sockets.
>
> Commit 740ea3c4a0b2 ("tcp: Clean up kernel listener's reqsk in
> inet_twsk_purge()") fixed this issue only for per-netns ehash, but
> the issue still exists for the global ehash.
>
> We can apply the same fix, but this issue is specific to RDS.
>
> Instead of iterating ehash and purging reqsk during netns dismantle,
> let's hold netns refcount for the kernel listener.
>
>
> Reported-by: syzkaller <syzkaller@googlegroups.com>
> Suggested-by: Eric Dumazet <edumazet@google.com>
> Fixes: 467fa15356ac ("RDS-TCP: Support multiple RDS-TCP listen endpoints, one per netns.")
> Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
> ---
> net/rds/tcp_listen.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/net/rds/tcp_listen.c b/net/rds/tcp_listen.c
> index 05008ce5c421..2d40e523322c 100644
> --- a/net/rds/tcp_listen.c
> +++ b/net/rds/tcp_listen.c
> @@ -274,8 +274,8 @@ struct socket *rds_tcp_listen_init(struct net *net, bool isv6)
> int addr_len;
> int ret;
>
> - ret = sock_create_kern(net, isv6 ? PF_INET6 : PF_INET, SOCK_STREAM,
> - IPPROTO_TCP, &sock);
> + ret = __sock_create(net, isv6 ? PF_INET6 : PF_INET, SOCK_STREAM,
> + IPPROTO_TCP, &sock, SOCKET_KERN_NET_REF);
> if (ret < 0) {
> rdsdebug("could not create %s listener socket: %d\n",
> isv6 ? "IPv6" : "IPv4", ret);
If RDS module keeps a listener alive, not attached to a user process,
netns dismantle will never occur.
I think we have to cleanup SYN_RECV sockets in inet_twsk_purge()
Yes, it removes one optimization you did.
Perhaps add a counter of all kernel sockets that were ever attached to
a netns in order to decide to apply the optimization.
(keeping a precise count of SYN_RECV would be too expensive)
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH v2 net 4/5] rds: tcp: Fix use-after-free of net in reqsk_timer_handler().
2024-02-27 12:06 ` Eric Dumazet
@ 2024-02-28 2:24 ` Kuniyuki Iwashima
0 siblings, 0 replies; 9+ messages in thread
From: Kuniyuki Iwashima @ 2024-02-28 2:24 UTC (permalink / raw)
To: edumazet
Cc: allison.henderson, davem, kuba, kuni1840, kuniyu, linux-rdma,
netdev, pabeni, rds-devel, syzkaller
From: Eric Dumazet <edumazet@google.com>
Date: Tue, 27 Feb 2024 13:06:07 +0100
> On Tue, Feb 27, 2024 at 2:12 AM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
> >
> > syzkaller reported a warning of netns tracker [0] followed by KASAN
> > splat [1] and another ref tracker warning [1].
> >
> > syzkaller could not find a repro, but in the log, the only suspicious
> > sequence was as follows:
> >
> > 18:26:22 executing program 1:
> > r0 = socket$inet6_mptcp(0xa, 0x1, 0x106)
> > ...
> > connect$inet6(r0, &(0x7f0000000080)={0xa, 0x4001, 0x0, @loopback}, 0x1c) (async)
> >
> > The notable thing here is 0x4001 in connect(), which is RDS_TCP_PORT.
> >
> > So, the scenario would be:
> >
> > 1. unshare(CLONE_NEWNET) creates a per netns tcp listener in
> > rds_tcp_listen_init().
> > 2. syz-executor connect()s to it and creates a reqsk.
> > 3. syz-executor exit()s immediately.
> > 4. netns is dismantled. [0]
> > 5. reqsk timer is fired, and UAF happens while freeing reqsk. [1]
> > 6. listener is freed after RCU grace period. [2]
> >
> > Basically, reqsk assumes that the listener guarantees netns safety
> > until all reqsk timers are expired by holding the listener's refcount.
> > However, this was not the case for kernel sockets.
> >
> > Commit 740ea3c4a0b2 ("tcp: Clean up kernel listener's reqsk in
> > inet_twsk_purge()") fixed this issue only for per-netns ehash, but
> > the issue still exists for the global ehash.
> >
> > We can apply the same fix, but this issue is specific to RDS.
> >
> > Instead of iterating ehash and purging reqsk during netns dismantle,
> > let's hold netns refcount for the kernel listener.
> >
> >
>
> > Reported-by: syzkaller <syzkaller@googlegroups.com>
> > Suggested-by: Eric Dumazet <edumazet@google.com>
> > Fixes: 467fa15356ac ("RDS-TCP: Support multiple RDS-TCP listen endpoints, one per netns.")
> > Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
> > ---
> > net/rds/tcp_listen.c | 4 ++--
> > 1 file changed, 2 insertions(+), 2 deletions(-)
> >
> > diff --git a/net/rds/tcp_listen.c b/net/rds/tcp_listen.c
> > index 05008ce5c421..2d40e523322c 100644
> > --- a/net/rds/tcp_listen.c
> > +++ b/net/rds/tcp_listen.c
> > @@ -274,8 +274,8 @@ struct socket *rds_tcp_listen_init(struct net *net, bool isv6)
> > int addr_len;
> > int ret;
> >
> > - ret = sock_create_kern(net, isv6 ? PF_INET6 : PF_INET, SOCK_STREAM,
> > - IPPROTO_TCP, &sock);
> > + ret = __sock_create(net, isv6 ? PF_INET6 : PF_INET, SOCK_STREAM,
> > + IPPROTO_TCP, &sock, SOCKET_KERN_NET_REF);
> > if (ret < 0) {
> > rdsdebug("could not create %s listener socket: %d\n",
> > isv6 ? "IPv6" : "IPv4", ret);
>
> If RDS module keeps a listener alive, not attached to a user process,
> netns dismantle will never occur.
>
> I think we have to cleanup SYN_RECV sockets in inet_twsk_purge()
Ah.. yes, __init_net ops hook must not take net ref..
I'll go that way in v3.
>
> Yes, it removes one optimization you did.
>
> Perhaps add a counter of all kernel sockets that were ever attached to
> a netns in order to decide to apply the optimization.
> (keeping a precise count of SYN_RECV would be too expensive)
I'll work on the follow-up for net-next after the right fix is merged.
Thanks!
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2 net 5/5] tcp: Add assertion for reqsk->rsk_listener->sk_net_refcnt.
2024-02-27 1:10 [PATCH v2 net 0/5] tcp/rds: Fix use-after-free around kernel TCP reqsk Kuniyuki Iwashima
` (3 preceding siblings ...)
2024-02-27 1:10 ` [PATCH v2 net 4/5] rds: tcp: Fix use-after-free of net in reqsk_timer_handler() Kuniyuki Iwashima
@ 2024-02-27 1:10 ` Kuniyuki Iwashima
4 siblings, 0 replies; 9+ messages in thread
From: Kuniyuki Iwashima @ 2024-02-27 1:10 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Allison Henderson
Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev, linux-rdma,
rds-devel
syzbot demonstrated that a reqsk timer could be fired after netns
dismantle if the timer was kicked by kernel TCP listener.
Regardless of the owner of the socket, TCP listener always has to
hold netns refcount.
Let's make sure that new user will not create kernel TCP listener
without holding netns refcount.
Suggested-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
net/ipv4/tcp_input.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index df7b13f0e5e0..341dd5bb3fd1 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -6972,6 +6972,8 @@ struct request_sock *inet_reqsk_alloc(const struct request_sock_ops *ops,
if (req) {
struct inet_request_sock *ireq = inet_rsk(req);
+ DEBUG_NET_WARN_ON_ONCE(!sk_listener->sk_net_refcnt);
+
ireq->ireq_opt = NULL;
#if IS_ENABLED(CONFIG_IPV6)
ireq->pktopts = NULL;
--
2.30.2
^ permalink raw reply related [flat|nested] 9+ messages in thread