Re: WARNING: kmalloc bug in bpf_uprobe_multi_link_attach

From: Hou Tao
Date: Mon Dec 11 2023 - 06:29:54 EST


Hi,

On 12/11/2023 4:12 PM, xingwei lee wrote:
> Sorry for containing HTML part, repeat the mail
> Hello I found a bug in net/bpf in the lastest upstream linux and
> lastest net tree.
> WARNING: kmalloc bug in bpf_uprobe_multi_link_attach
>
> kernel: net 28a7cb045ab700de5554193a1642917602787784
> Kernel config: https://github.com/google/syzkaller/commits/fc59b78e3174009510ed15f20665e7ab2435ebee
>
> in the lastest net tree, the crash like:
>
> [ 68.363836][ T8223] ------------[ cut here ]------------
> [ 68.364967][ T8223] WARNING: CPU: 2 PID: 8223 at mm/util.c:632
> kvmalloc_node+0x18a/0x1a0
> [ 68.366527][ T8223] Modules linked in:
> [ 68.367882][ T8223] CPU: 2 PID: 8223 Comm: 36d Not tainted
> 6.7.0-rc4-00146-g28a7cb045ab7 #2
> [ 68.369260][ T8223] Hardware name: QEMU Standard PC (i440FX + PIIX,
> 1996), BIOS 1.16.2-1.fc38 04/014
> [ 68.370811][ T8223] RIP: 0010:kvmalloc_node+0x18a/0x1a0
> [ 68.371689][ T8223] Code: dc 1c 00 eb aa e8 86 33 c6 ff 41 81 e4 00
> 20 00 00 31 ff 44 89 e6 e8 e5 20
> [ 68.375001][ T8223] RSP: 0018:ffffc9001088fb68 EFLAGS: 00010293
> [ 68.375989][ T8223] RAX: 0000000000000000 RBX: 00000037ffffcec8
> RCX: ffffffff81c1a32b
> [ 68.377154][ T8223] RDX: ffff88802cc00040 RSI: ffffffff81c1a339
> RDI: 0000000000000005
> [ 68.377950][ T8223] RBP: 0000000000000400 R08: 0000000000000005
> R09: 0000000000000000
> [ 68.378744][ T8223] R10: 0000000000000000 R11: 0000000000000000
> R12: 0000000000000000
> [ 68.379523][ T8223] R13: 00000000ffffffff R14: ffff888017eb4a28
> R15: 0000000000000000
> [ 68.380307][ T8223] FS: 0000000000827380(0000)
> GS:ffff8880b9900000(0000) knlGS:0000000000000000
> [ 68.381185][ T8223] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
> [ 68.381843][ T8223] CR2: 0000000020000140 CR3: 00000000204d2000
> CR4: 0000000000750ef0
> [ 68.382624][ T8223] PKRU: 55555554
> [ 68.382978][ T8223] Call Trace:
> [ 68.383312][ T8223] <TASK>
> [ 68.383608][ T8223] ? show_regs+0x8f/0xa0
> [ 68.384052][ T8223] ? __warn+0xe6/0x390
> [ 68.384470][ T8223] ? kvmalloc_node+0x18a/0x1a0
> [ 68.385111][ T8223] ? report_bug+0x3b9/0x580
> [ 68.385585][ T8223] ? handle_bug+0x67/0x90
> [ 68.386032][ T8223] ? exc_invalid_op+0x17/0x40
> [ 68.386503][ T8223] ? asm_exc_invalid_op+0x1a/0x20
> [ 68.387065][ T8223] ? kvmalloc_node+0x17b/0x1a0
> [ 68.387551][ T8223] ? kvmalloc_node+0x189/0x1a0
> [ 68.388051][ T8223] ? kvmalloc_node+0x18a/0x1a0
> [ 68.388537][ T8223] ? kvmalloc_node+0x189/0x1a0
> [ 68.389038][ T8223] bpf_uprobe_multi_link_attach+0x436/0xfb0

It seems a big attr->link_create.uprobe_multi.cnt is passed to
bpf_uprobe_multi_link_attach(). Could you please try the first patch in
the following patch set ?

https://lore.kernel.org/bpf/20231211112843.4147157-1-houtao@xxxxxxxxxxxxxxx/T/#t
> [ 68.389633][ T8223] ? __might_fault+0x13f/0x1a0
> [ 68.390129][ T8223] ? bpf_kprobe_multi_link_attach+0x10/0x10

SNIP
> res = syscall(__NR_bpf, /*cmd=*/5ul, /*arg=*/0x20000140ul, /*size=*/0x90ul);
> if (res != -1) r[0] = res;
> memcpy((void*)0x20000000, "./file0\000", 8);
> syscall(__NR_creat, /*file=*/0x20000000ul, /*mode=*/0ul);
> *(uint32_t*)0x20000340 = r[0];
> *(uint32_t*)0x20000344 = 0;
> *(uint32_t*)0x20000348 = 0x30;
> *(uint32_t*)0x2000034c = 0;
> *(uint64_t*)0x20000350 = 0x20000080;
> memcpy((void*)0x20000080, "./file0\000", 8);

0x20000350 is the address of attr->link_create.uprobe_multi.path.
> *(uint64_t*)0x20000358 = 0x200000c0;
> *(uint64_t*)0x200000c0 = 0;
> *(uint64_t*)0x20000360 = 0;
> *(uint64_t*)0x20000368 = 0;
> *(uint32_t*)0x20000370 = 0xffffff1f;

The value of attr->link_create.uprobe_multi.cnt is 0xffffff1f, so 
0xffffff1f * sizeof(bpf_uprobe) will be greater than INT_MAX, and
triggers the warning in mm/util.c:

        /* Don't even allow crazy sizes */
        if (unlikely(size > INT_MAX)) {
                WARN_ON_ONCE(!(flags & __GFP_NOWARN));
                return NULL;
        }

Adding __GFP_NOWARN when doing kvcalloc() can fix the warning.
> *(uint32_t*)0x20000374 = 0;
> *(uint32_t*)0x20000378 = 0;
> syscall(__NR_bpf, /*cmd=*/0x1cul, /*arg=*/0x20000340ul, /*size=*/0x40ul);
> return 0;
> }
>
> =* repro.txt =*
> r0 = bpf$PROG_LOAD(0x5, &(0x7f0000000140)={0x2, 0x3,
> &(0x7f0000000200)=@framed, &(0x7f0000000240)='GPL\x00', 0x0, 0x0, 0x0,
> 0x0, 0x0, '\x00', 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
> 0x0, 0x0, 0x0, 0x0, 0x0}, 0x90)
> creat(&(0x7f0000000000)='./file0\x00', 0x0)
> bpf$BPF_LINK_CREATE_XDP(0x1c, &(0x7f0000000340)={r0, 0x0, 0x30, 0x0,
> @val=@uprobe_multi={&(0x7f0000000080)='./file0\x00',
> &(0x7f00000000c0)=[0x0], 0x0, 0x0, 0xffffff1f}}, 0x40
>
>
> See aslo https://gist.github.com/xrivendell7/15d43946c73aa13247b4b20b68798aaa
>
> .