[Kernel bug] KASAN: slab-out-of-bounds Read in in4_pton

From: Sam Sun
Date: Thu Feb 29 2024 - 21:41:47 EST


Dear developers and maintainers,

We found a bug through our modified Syzkaller. Kernel version is b401b621758,
Linux 6.8-rc5. The C reproducer and kernel config are attached.

KASAN report is listed below

```
==================================================================
BUG: KASAN: slab-out-of-bounds in strlen+0x7d/0xa0 lib/string.c:418 Read of size 1 at addr ffff8881119c4781 by task syz-executor665/8107 CPU: 1 PID: 8107 Comm: syz-executor665 Not tainted 6.7.0-rc7 #1 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014 Call Trace: <TASK> __dump_stack lib/dump_stack.c:88 [inline] dump_stack_lvl+0xd9/0x150 lib/dump_stack.c:106 print_address_description mm/kasan/report.c:364 [inline] print_report+0xc1/0x5e0 mm/kasan/report.c:475 kasan_report+0xbe/0xf0 mm/kasan/report.c:588 strlen+0x7d/0xa0 lib/string.c:418 __fortify_strlen include/linux/fortify-string.h:210 [inline] in4_pton+0xa3/0x3f0 net/core/utils.c:130 bond_option_arp_ip_targets_set+0xc2/0x910 drivers/net/bonding/bond_optionsc:1201 __bond_opt_set+0x2a4/0x1030 drivers/net/bonding/bond_options.c:767 __bond_opt_set_notify+0x48/0x150 drivers/net/bonding/bond_options.c:792 bond_opt_tryset_rtnl+0xda/0x160 drivers/net/bonding/bond_options.c:817 bonding_sysfs_store_option+0xa1/0x120 drivers/net/bonding/bond_sysfs.c:156 dev_attr_store+0x54/0x80 drivers/base/core.c:2366 sysfs_kf_write+0x114/0x170 fs/sysfs/file.c:136 kernfs_fop_write_iter+0x337/0x500 fs/kernfs/file.c:334 call_write_iter include/linux/fs.h:2020 [inline] new_sync_write fs/read_write.c:491 [inline] vfs_write+0x96a/0xd80 fs/read_write.c:584 ksys_write+0x122/0x250 fs/read_write.c:637 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0x40/0x110 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x63/0x6b RIP: 0033:0x7f797835cfcd Code: 28 c3 e8 46 1e 00 00 66 0f 1f 44 00 00 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 b8 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007ffff464ffb8 EFLAGS: 00000246 ORIG_RAX: 0000000000000001 RAX: ffffffffffffffda RBX: 00007ffff46501b8 RCX: 00007f797835cfcd RDX: 00000000000000f5 RSI: 0000000020000140 RDI: 0000000000000003 RBP: 0000000000000001 R08: 0000000000000000 R09: 00007ffff46501b8 R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000001 R13: 00007ffff46501a8 R14: 00007f79783da530 R15: 0000000000000001 </TASK> Allocated by task 8107: kasan_save_stack+0x22/0x40 mm/kasan/common.c:45 kasan_set_track+0x25/0x30 mm/kasan/common.c:52 ____kasan_kmalloc mm/kasan/common.c:374 [inline] ____kasan_kmalloc mm/kasan/common.c:333 [inline] __kasan_kmalloc+0xa3/0xb0 mm/kasan/common.c:383 kasan_kmalloc include/linux/kasan.h:198 [inline] __do_kmalloc_node mm/slab_common.c:1007 [inline] __kmalloc_node_track_caller+0x5e/0xd0 mm/slab_common.c:1027 kstrndup+0x72/0x110 mm/util.c:108 bonding_sysfs_store_option+0x66/0x120 drivers/net/bonding/bond_sysfs.c:153 dev_attr_store+0x54/0x80 drivers/base/core.c:2366 sysfs_kf_write+0x114/0x170 fs/sysfs/file.c:136 kernfs_fop_write_iter+0x337/0x500 fs/kernfs/file.c:334 call_write_iter include/linux/fs.h:2020 [inline] new_sync_write fs/read_write.c:491 [inline] vfs_write+0x96a/0xd80 fs/read_write.c:584 ksys_write+0x122/0x250 fs/read_write.c:637 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0x40/0x110 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x63/0x6b The buggy address belongs to the object at ffff8881119c4780 which belongs to the cache kmalloc-32 of size 32 The buggy address is located 0 bytes to the right of allocated 1-byte region [ffff8881119c4780, ffff8881119c4781) The buggy address belongs to the physical page: page:ffffea0004467100 refcount:1 mapcount:0 mapping:0000000000000000 index:0xffff8881119c4fc1 pfn:0x1119c4 flags: 0x57ff00000000800(slab|node=1|zone=2|lastcpupid=0x7ff) page_type: 0x3f() raw: 057ff00000000800 ffff888013040100 ffffea000405a650 ffffea0004156b90 raw: ffff8881119c4fc1 ffff8881119c4000 000000010000003f 0000000000000000 page dumped because: kasan: bad access detected page_owner tracks the page as allocated page last allocated via order 0, migratetype Unmovable, gfp_mask 0x2420c0(__GFP_IO|__GFP_FS|__GFP_NOWARN|__GFP_COMP|__GFP_THISNODE), pid 25, tgid 25 (kworker/u6:0), ts 73787966798, free_ts 73774684635 set_page_owner include/linux/page_owner.h:31 [inline] post_alloc_hook+0x2d9/0x350 mm/page_alloc.c:1537 prep_new_page mm/page_alloc.c:1544 [inline] get_page_from_freelist+0xd38/0x2fa0 mm/page_alloc.c:3312 __alloc_pages+0x21d/0x21f0 mm/page_alloc.c:4568 __alloc_pages_node include/linux/gfp.h:238 [inline] kmem_getpages mm/slab.c:1356 [inline] cache_grow_begin+0x9b/0x3c0 mm/slab.c:2550 cache_alloc_refill+0x289/0x3a0 mm/slab.c:2923 ____cache_alloc mm/slab.c:2999 [inline] ____cache_alloc mm/slab.c:2982 [inline] __do_cache_alloc mm/slab.c:3185 [inline] slab_alloc_node mm/slab.c:3230 [inline] __kmem_cache_alloc_node+0x374/0x420 mm/slab.c:3521 __do_kmalloc_node mm/slab_common.c:1006 [inline] __kmalloc_node+0x50/0xd0 mm/slab_common.c:1014 kmalloc_array_node include/linux/slab.h:698 [inline] kcalloc_node include/linux/slab.h:703 [inline] memcg_alloc_slab_cgroups+0x10e/0x210 mm/memcontrol.c:2968 account_slab mm/slab.h:637 [inline] kmem_getpages mm/slab.c:1364 [inline] cache_grow_begin+0x349/0x3c0 mm/slab.c:2550 cache_alloc_refill+0x289/0x3a0 mm/slab.c:2923 ____cache_alloc mm/slab.c:2999 [inline] ____cache_alloc mm/slab.c:2982 [inline] __do_cache_alloc mm/slab.c:3182 [inline] slab_alloc_node mm/slab.c:3230 [inline] kmem_cache_alloc_node+0x3e1/0x4a0 mm/slab.c:3509 alloc_task_struct_node kernel/fork.c:173 [inline] dup_task_struct kernel/fork.c:1110 [inline] copy_process+0x3f6/0x73e0 kernel/fork.c:2332 kernel_clone+0xeb/0x8c0 kernel/fork.c:2907 user_mode_thread+0xb4/0xf0 kernel/fork.c:2985 call_usermodehelper_exec_work kernel/umh.c:172 [inline] call_usermodehelper_exec_work+0xd0/0x180 kernel/umh.c:158 process_one_work+0x878/0x15c0 kernel/workqueue.c:2627 page last free stack trace: reset_page_owner include/linux/page_owner.h:24 [inline] free_pages_prepare mm/page_alloc.c:1137 [inline] free_unref_page_prepare+0x4c5/0xa60 mm/page_alloc.c:2347 free_unref_page+0x33/0x3d0 mm/page_alloc.c:2487 rcu_do_batch kernel/rcu/tree.c:2158 [inline] rcu_core+0x817/0x1670 kernel/rcu/tree.c:2431 __do_softirq+0x1d4/0x85e kernel/softirq.c:553 Memory state around the buggy address: ffff8881119c4680: fa fb fb fb fc fc fc fc 00 01 fc fc fc fc fc fc ffff8881119c4700: 00 05 fc fc fc fc fc fc 00 00 00 fc fc fc fc fc >ffff8881119c4780: 01 fc fc fc fc fc fc fc fb fb fb fb fc fc fc fc ^ ffff8881119c4800: 00 01 fc fc fc fc fc fc fb fb fb fb fc fc fc fc ffff8881119c4880: 00 fc fc fc fc fc fc fc 00 01 fc fc fc fc fc fc ==================================================================
```
We analyzed the cause of this bug. In the function "bond_option_arp_ip_targets_set" in 
drivers/net/bonding/bond_options.c, newval->string can be controlled by users. If string
is empty, newval->string+1 points to the address after newval->string, causing potential 
info leak.
One possible fix is to check before calling in4_pton, whether strnlen(newval->string) > 1.

If you have any questions or require more information, please feel
free to contact us.
Reported by Yue Sun <samsun1006219@xxxxxxxxx>

Best Regards,
Yue
#define _GNU_SOURCE

#include <endian.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>

uint64_t r[1] = {0xffffffffffffffff};

int main(void)
{
syscall(__NR_mmap, /*addr=*/0x1ffff000ul, /*len=*/0x1000ul, /*prot=*/0ul,
/*flags=*/0x32ul, /*fd=*/-1, /*offset=*/0ul);
syscall(__NR_mmap, /*addr=*/0x20000000ul, /*len=*/0x1000000ul, /*prot=*/7ul,
/*flags=*/0x32ul, /*fd=*/-1, /*offset=*/0ul);
syscall(__NR_mmap, /*addr=*/0x21000000ul, /*len=*/0x1000ul, /*prot=*/0ul,
/*flags=*/0x32ul, /*fd=*/-1, /*offset=*/0ul);
intptr_t res = 0;
memcpy((void*)0x20000080,
"/sys/devices/virtual/net/bond0/bonding/arp_ip_target\000", 53);
res = syscall(__NR_openat, /*fd=*/0xffffffffffffff9cul, /*file=*/0x20000080ul,
/*flags=*/2ul, /*mode=*/0ul);
if (res != -1)
r[0] = res;
memcpy(
(void*)0x20000140,
"\x00\xdc\x39\x49\xf4\x87\xd1\xc0\x35\xf7\x35\x07\xd7\x73\x6a\x4a\x68\x3b"
"\xe7\x1d\x54\x49\x8a\x75\x6d\xb5\x16\xa4\xf9\xd2\x7c\xe2\x99\x57\x56\xc0"
"\xe8\x75\xd9\x6d\x9e\xc0\x19\x8e\xfb\x4c\x75\x5c\x79\xd2\x71\x40\x15\x52"
"\xab\x41\xd4\xdd\x13\x42\xda\xa5\xf8\xc7\x19\x2f\xbf\x4e\x9a\x74\xec\x5b"
"\x1e\x47\xbc\xca\x9d\x3e\x3b\x0e\x7a\x43\x90\x66\x85\x4c\xb5\x1f\xc8\x35"
"\x6b\x1f\x51\x1d\x97\x17\xb7\xd5\x8e\x5a\xc5\x6b\xd0\xeb\x73\x1f\xd8\xc3"
"\x9d\xc4\xff\x0a\x5d\x14\x65\x3e\xe0\x7d\x3b\x72\x26\xef\x39\x2b\x7e\x84"
"\xc1\x82\x21\x4d\x2a\x1e\x9f\xf1\x06\x1a\x8e\x14\xec\xfe\xc4\x94\x82\x1f"
"\xcf\xe9\x01\x34\x29\x6f\x2b\x9b\x21\x12\x8a\x41\xde\x34\x9a\xab\xfe\x94"
"\x7c\xb9\xe4\x6c\x16\x9a\x49\xd6\xee\x22\x14\xf7\xef\xbb\xa5\xe1\xad\x79"
"\x61\xaa\x0c\xc4\xca\xa0\x38\xc4\x20\xbf\x59\x65\x8f\xae\xd7\xdf\x44\x5d"
"\xd6\x16\x7f\x08\x0b\xaa\x7d\x6b\x47\xb4\xa6\x13\x78\x91\x87\x77\x0a\x91"
"\x41\x35\x9a\xba\x3e\x6a\x67\x03\x79\x04\xb4\xc0\x09\xc5\x50\xd6\x6d\x2b"
"\x74\x31\xc7\xe0\x3c\x10\xad\xff\xfe\x71\xc7",
245);
syscall(__NR_write, /*fd=*/r[0], /*buf=*/0x20000140ul, /*count=*/0xf5ul);
return 0;
}

Attachment: config
Description: Binary data