Ignat Korchagin
454c454ed6
net: do not leave a dangling sk pointer, when socket creation fails
...
commit 6cd4a78d962bebbaf8beb7d2ead3f34120e3f7b2 upstream.
It is possible to trigger a use-after-free by:
* attaching an fentry probe to __sock_release() and the probe calling the
bpf_get_socket_cookie() helper
* running traceroute -I 1.1.1.1 on a freshly booted VM
A KASAN enabled kernel will log something like below (decoded and stripped):
==================================================================
BUG: KASAN: slab-use-after-free in __sock_gen_cookie (./arch/x86/include/asm/atomic64_64.h:15 ./include/linux/atomic/atomic-arch-fallback.h:2583 ./include/linux/atomic/atomic-instrumented.h:1611 net/core/sock_diag.c:29)
Read of size 8 at addr ffff888007110dd8 by task traceroute/299
CPU: 2 PID: 299 Comm: traceroute Tainted: G E 6.10.0-rc2+ #2
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.2-debian-1.16.2-1 04/01/2014
Call Trace:
<TASK>
dump_stack_lvl (lib/dump_stack.c:117 (discriminator 1))
print_report (mm/kasan/report.c:378 mm/kasan/report.c:488)
? __sock_gen_cookie (./arch/x86/include/asm/atomic64_64.h:15 ./include/linux/atomic/atomic-arch-fallback.h:2583 ./include/linux/atomic/atomic-instrumented.h:1611 net/core/sock_diag.c:29)
kasan_report (mm/kasan/report.c:603)
? __sock_gen_cookie (./arch/x86/include/asm/atomic64_64.h:15 ./include/linux/atomic/atomic-arch-fallback.h:2583 ./include/linux/atomic/atomic-instrumented.h:1611 net/core/sock_diag.c:29)
kasan_check_range (mm/kasan/generic.c:183 mm/kasan/generic.c:189)
__sock_gen_cookie (./arch/x86/include/asm/atomic64_64.h:15 ./include/linux/atomic/atomic-arch-fallback.h:2583 ./include/linux/atomic/atomic-instrumented.h:1611 net/core/sock_diag.c:29)
bpf_get_socket_ptr_cookie (./arch/x86/include/asm/preempt.h:94 ./include/linux/sock_diag.h:42 net/core/filter.c:5094 net/core/filter.c:5092)
bpf_prog_875642cf11f1d139___sock_release+0x6e/0x8e
bpf_trampoline_6442506592+0x47/0xaf
__sock_release (net/socket.c:652)
__sock_create (net/socket.c:1601)
...
Allocated by task 299 on cpu 2 at 78.328492s:
kasan_save_stack (mm/kasan/common.c:48)
kasan_save_track (mm/kasan/common.c:68)
__kasan_slab_alloc (mm/kasan/common.c:312 mm/kasan/common.c:338)
kmem_cache_alloc_noprof (mm/slub.c:3941 mm/slub.c:4000 mm/slub.c:4007)
sk_prot_alloc (net/core/sock.c:2075)
sk_alloc (net/core/sock.c:2134)
inet_create (net/ipv4/af_inet.c:327 net/ipv4/af_inet.c:252)
__sock_create (net/socket.c:1572)
__sys_socket (net/socket.c:1660 net/socket.c:1644 net/socket.c:1706)
__x64_sys_socket (net/socket.c:1718)
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:130)
Freed by task 299 on cpu 2 at 78.328502s:
kasan_save_stack (mm/kasan/common.c:48)
kasan_save_track (mm/kasan/common.c:68)
kasan_save_free_info (mm/kasan/generic.c:582)
poison_slab_object (mm/kasan/common.c:242)
__kasan_slab_free (mm/kasan/common.c:256)
kmem_cache_free (mm/slub.c:4437 mm/slub.c:4511)
__sk_destruct (net/core/sock.c:2117 net/core/sock.c:2208)
inet_create (net/ipv4/af_inet.c:397 net/ipv4/af_inet.c:252)
__sock_create (net/socket.c:1572)
__sys_socket (net/socket.c:1660 net/socket.c:1644 net/socket.c:1706)
__x64_sys_socket (net/socket.c:1718)
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:130)
Fix this by clearing the struct socket reference in sk_common_release() to cover
all protocol families create functions, which may already attached the
reference to the sk object with sock_init_data().
Fixes: c5dbb89fc2ac ("bpf: Expose bpf_get_socket_cookie to tracing programs")
Suggested-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Signed-off-by: Ignat Korchagin <ignat@cloudflare.com>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/netdev/20240613194047.36478-1-kuniyu@amazon.com/T/
Reviewed-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Reviewed-by: D. Wythe <alibuda@linux.alibaba.com>
Link: https://lore.kernel.org/r/20240617210205.67311-1-ignat@cloudflare.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2024-06-27 13:49:11 +02:00
..
2024-06-16 13:47:41 +02:00
2024-05-17 12:02:07 +02:00
2023-12-20 17:01:50 +01:00
2023-12-20 17:01:48 +01:00
2024-06-21 14:38:14 +02:00
2024-06-27 13:49:01 +02:00
2024-06-21 14:38:34 +02:00
2024-06-21 14:38:16 +02:00
2024-06-21 14:38:36 +02:00
2023-06-24 15:50:13 -07:00
2024-02-23 09:25:17 +01:00
2024-03-01 13:34:56 +01:00
2024-06-27 13:49:11 +02:00
2023-08-01 21:07:46 -07:00
2023-11-20 11:59:35 +01:00
2024-03-26 18:20:11 -04:00
2024-01-25 15:35:41 -08:00
2023-08-09 13:08:09 -07:00
2024-05-02 16:32:46 +02:00
2024-06-21 14:38:20 +02:00
2024-02-23 09:24:50 +01:00
2024-05-17 12:02:24 +02:00
2023-08-29 17:39:15 -07:00
2024-01-01 12:42:30 +00:00
2024-06-27 13:49:06 +02:00
2024-06-27 13:49:08 +02:00
2024-03-26 18:19:12 -04:00
2024-03-26 18:19:40 -04:00
2023-08-18 12:44:56 -07:00
2024-05-17 12:02:02 +02:00
2024-02-05 20:14:36 +00:00
2024-06-21 14:38:13 +02:00
2024-04-03 15:28:27 +02:00
2024-03-26 18:19:34 -04:00
2024-04-13 13:07:41 +02:00
2024-06-21 14:38:38 +02:00
2024-06-21 14:38:14 +02:00
2024-06-27 13:49:08 +02:00
2024-01-25 15:35:14 -08:00
2024-03-06 14:48:34 +00:00
2024-06-27 13:49:06 +02:00
2024-06-12 11:12:51 +02:00
2024-05-17 12:02:02 +02:00
2024-06-12 11:12:49 +02:00
2024-06-27 13:49:01 +02:00
2024-05-17 12:02:22 +02:00
2023-12-13 18:45:10 +01:00
2024-06-12 11:12:12 +02:00
2024-04-10 16:35:49 +02:00
2024-01-01 12:42:41 +00:00
2024-01-01 12:42:31 +00:00
2024-05-17 12:02:23 +02:00
2024-06-27 13:49:07 +02:00
2024-01-25 15:35:30 -08:00
2024-06-21 14:38:16 +02:00
2024-06-21 14:38:28 +02:00
2024-03-01 13:35:06 +01:00
2024-06-27 13:49:07 +02:00
2024-06-12 11:12:50 +02:00
2024-06-21 14:38:36 +02:00
2024-04-10 16:35:50 +02:00
2024-06-21 14:38:12 +02:00
2024-03-26 18:19:41 -04:00
2024-04-17 11:19:28 +02:00
2024-06-16 13:47:44 +02:00
2023-07-19 10:07:27 -07:00
2024-01-10 17:16:51 +01:00
2023-08-15 15:26:17 -07:00