Skip to contentSkip to conversation

ebpf program can cause verifier crash in check_return_code()

DISCUSSION [email protected] 2 participants 2 messages Nov 23 – Nov 24

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

I've attached a demo struct_ops / io_uring program that causes the ebpf verifier to dereference a bad pointer. The disassembled instructions are below. I believe what's happening is:

* instruction 6 is a call to bpf_io_uring_get_region(), causing the

    verifier to set r0's reg->type to PTR_TO_MEM|PTR_MAYBE_NULL, with
    reg->mem_size set to the call's 3rd argument (64 in the demo).

* the conditional jump at instruction 7 goes to the exit.

* the verifier's check_return_code() executes this without a check

    of r0's reg->type:

        if (prog_type == BPF_PROG_TYPE_STRUCT_OPS) {
                /* Allow a struct_ops program to return a referenced kptr if it
                 * matches the operator's return type and is in its unmodified
                 * form. A scalar zero (i.e., a null pointer) is also allowed.
                 */
                reg_type = reg->btf ? btf_type_by_id(reg->btf, reg->btf_id) : NULL;

* reg->btf shares a slot in the bpf_reg_state union with mem_size, so

    reg->btf is 64, and check_return_code() calls btf_type_by_id(64, ...),
    which crashes.

The attached demo code is suitable for libbpf-bootstrap/examples/c; here are the bpf instructions (you may need to fiddle with instruction 7's jump offset; +19 works on one of my setups, while +16 is required on another):

io3.bpf.o: file format elf64-bpf
Disassembly of section struct_ops.s/loop_step:
0000000000000000 <my_loop_step>:

       0:       79 18 08 00 00 00 00 00 r8 = *(u64 *)(r1 + 0x8)
       1:       79 17 00 00 00 00 00 00 r7 = *(u64 *)(r1 + 0x0)
       2:       b7 06 00 00 00 00 00 00 r6 = 0x0
       3:       bf 71 00 00 00 00 00 00 r1 = r7
       4:       b7 02 00 00 00 00 00 00 r2 = 0x0
       5:       b7 03 00 00 40 00 00 00 r3 = 0x40
       6:       85 10 00 00 ff ff ff ff call -0x1
       7:       c6 06 13 00 00 00 00 40 if w6 s< 0x40000000 goto +0x13 <my_loop_step+0xd8>
       8:       84 02 00 00 00 00 00 00 w2 = -w2
       9:       89 50 00 00 00 00 00 00 r0 = *(s16 *)(r5 + 0x0)
      10:       85 10 00 00 ff ff ff ff call -0x1
      11:       b7 09 00 00 01 00 00 00 r9 = 0x1
      12:       bf 71 00 00 00 00 00 00 r1 = r7
      13:       b7 02 00 00 01 00 00 00 r2 = 0x1
      14:       85 10 00 00 ff ff ff ff call -0x1
      15:       67 00 00 00 20 00 00 00 r0 <<= 0x20
      16:       c7 00 00 00 20 00 00 00 r0 s>>= 0x20
      17:       6d 09 03 00 00 00 00 00 if r9 s> r0 goto +0x3 <my_loop_step+0xa8>
      18:       b7 01 00 00 01 00 00 00 r1 = 0x1
      19:       63 18 00 00 00 00 00 00 *(u32 *)(r8 + 0x0) = r1
      20:       05 00 05 00 00 00 00 00 goto +0x5 <my_loop_step+0xd0>
      21:       18 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 r1 = 0x0 ll
      23:       b7 02 00 00 07 00 00 00 r2 = 0x7
      24:       85 00 00 00 06 00 00 00 call 0x6
      25:       b7 06 00 00 01 00 00 00 r6 = 0x1
      26:       bf 60 00 00 00 00 00 00 r0 = r6
      27:       95 00 00 00 00 00 00 00 exit

The crash backtrace:

Unable to handle kernel access to user memory without uaccess routines at virtual address 00000000000000e8
Current io3 pgtable: 4K pagesize, 39-bit VAs, pgdp=0x0000000084ca3000
[00000000000000e8] pgd=0000000000000000, p4d=0000000000000000, pud=0000000000000000
Oops [#1]
Modules linked in:
CPU: 0 UID: 0 PID: 70 Comm: io3 Not tainted 7.4.0-rc3-00441-g539a78300c41-dirty #843 PREEMPTLAZY 
Hardware name: riscv-virtio,qemu (DT)
epc : btf_type_by_id+0x6/0x40
 ra : check_return_code+0x22e/0x448
status: 0000000200000120 badaddr: 00000000000000e8 cause: 000000000000000d
[<ffffffff80222746>] btf_type_by_id+0x6/0x40
[<ffffffff801c4ddc>] check_return_code+0x22e/0x448
[<ffffffff801dd3f0>] do_check_common+0x2474/0x2af4
[<ffffffff801e0d7c>] bpf_check+0x28f0/0x2b2c
[<ffffffff801b5806>] bpf_prog_load+0x4bc/0x95a
[<ffffffff801b7fda>] __sys_bpf+0x181c/0x25d6
[<ffffffff801b8fc6>] __riscv_sys_bpf+0x1e/0x26
[<ffffffff80b88fbe>] do_trap_ecall_u+0x14a/0x324
[<ffffffff80b97788>] handle_exception+0x168/0x174

Robert Morris
[email protected]

#1 lore raw

The following should fix it. Pls test it and send an official patch.

kernel/bpf/verifier.c +2 -1
index dea18409f480..5c8e84c80a9e 100644
@@ -19928,7 +19928,8 @@ static int check_return_code(struct bpf_verifier_env *env, int regno, const char
 		 * matches the operator's return type and is in its unmodified
 		 * form. A scalar zero (i.e., a null pointer) is also allowed.
 		 */
-		reg_type = reg->btf ? btf_type_by_id(reg->btf, reg->btf_id) : NULL;
+		reg_type = base_type(reg->type) == PTR_TO_BTF_ID ?
+			   btf_type_by_id(reg->btf, reg->btf_id) : NULL;
 		ret_type = btf_type_resolve_ptr(prog->aux->attach_btf,
 						prog->aux->attach_func_proto->type,
 						NULL);