BPF List
 help / color / mirror / Atom feed
* ebpf program can cause verifier crash in check_return_code()
@ 2026-11-23 19:56 rtm
  2026-11-24  3:27 ` Alexei Starovoitov
  0 siblings, 1 reply; 2+ messages in thread
From: rtm @ 2026-11-23 19:56 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, John Fastabend; +Cc: bpf

[-- Attachment #1: Type: text/plain, Size: 4003 bytes --]

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
rtm@mit.edu


[-- Attachment #2: io3.bpf.c --]
[-- Type: application/octet-stream, Size: 1903 bytes --]


//
// to be runin libbpf-bootstrap/examples/c
//

#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

struct iou_loop_params {
	__u32			cq_wait_idx;
};

struct iou_ctx { };

extern int bpf_io_uring_submit_sqes(struct iou_ctx *loop_ctx, __u32 nr) __ksym;
extern __u8 *bpf_io_uring_get_region(struct iou_ctx *loop_ctx, __u32 region_id,
                                     const size_t rdwr_buf_size) __ksym;

enum {
  IOU_REGION_MEM = 0,
  IOU_REGION_CQ  = 1,
  IOU_REGION_SQ  = 2,
};

enum {
  IOU_LOOP_CONTINUE = 0,
  IOU_LOOP_STOP     = 1,
};

SEC("struct_ops.s/loop_step")
int BPF_PROG(my_loop_step, struct iou_ctx *loop_ctx, struct iou_loop_params *lp)
{

  __u8 *mem = bpf_io_uring_get_region(loop_ctx, IOU_REGION_MEM, 64);
  if (!mem){
  }

  register volatile void *r1 asm("r1");
  register volatile void *r2 asm("r2");
  register volatile void *r3 asm("r3");

  asm volatile("if w6 s< 0x40000000 goto +19");
  asm volatile("w2 = -w2");
  asm volatile("r0 = *(s16 *)(r5 + 0)");

  struct io_uring_sqe *sqes =
    (struct io_uring_sqe *)bpf_io_uring_get_region(r1,
                                                   (long)r2,
                                                   (long)r3);

  if (!sqes){
  }

  int submitted = bpf_io_uring_submit_sqes(loop_ctx, 1);
  if (submitted < 0){
  }

  if (submitted > 0) {
    lp->cq_wait_idx = 1; /* Wait for at least 1 CQE before next step */
    return IOU_LOOP_CONTINUE;
  }

  bpf_printk("io3 #2");

  /* Return control to userspace io_uring_enter() */
  return IOU_LOOP_STOP;
}

struct io_uring_bpf_ops {
	int (*loop_step)(struct iou_ctx *, struct iou_loop_params *lp);

	__u32 ring_fd;
	void *priv;
};

SEC(".struct_ops.link")
struct io_uring_bpf_ops my_uring_ops = {
  .loop_step = (void *)my_loop_step,
  /* .ring_fd is populated from userspace before loading/attaching */
};

char _license[] SEC("license") = "GPL";

[-- Attachment #3: io3.c --]
[-- Type: application/octet-stream, Size: 1471 bytes --]

//
// to be run in libbpf-bootstrap/examples/c
//

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <liburing.h>
#include <bpf/libbpf.h>
#include "io3.skel.h"

int main(void)
{
  struct io_uring ring;
  struct io_uring_params params = {
    .flags = IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN,
  };
  struct io3_bpf *skel;
  struct bpf_link *link;
  int err;

  system("echo 1 > /proc/sys/net/core/bpf_jit_enable");

  err = io_uring_queue_init_params(32, &ring, &params);
  if (err < 0) {
    fprintf(stderr, "io_uring_queue_init_params failed: %d\n", err);
    return 1;
  }

  skel = io3_bpf__open();
  if (!skel) {
    fprintf(stderr, "Failed to open BPF skeleton\n");
    goto out_ring;
  }

  skel->struct_ops.my_uring_ops->ring_fd = ring.ring_fd;

  err = io3_bpf__load(skel);
  if (err) {
    fprintf(stderr, "Failed to load BPF skeleton: %d\n", err);
    goto out_skel;
  }

  link = bpf_map__attach_struct_ops(skel->maps.my_uring_ops);
  if (!link) {
    fprintf(stderr, "Failed to attach struct_ops\n");
    goto out_skel;
  }

  struct io_uring_sqe *sqe = io_uring_get_sqe(&ring);
  io_uring_prep_nop(sqe);
  io_uring_submit_and_wait(&ring, 1);

  struct io_uring_cqe *cqe;
  if (io_uring_peek_cqe(&ring, &cqe) == 0) {
    printf("Completed CQE res: %d\n", cqe->res);
    io_uring_cqe_seen(&ring, cqe);
  }

  bpf_link__destroy(link);
 out_skel:
  io3_bpf__destroy(skel);
 out_ring:
  io_uring_queue_exit(&ring);
  return 0;
}

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-24  3:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 19:56 ebpf program can cause verifier crash in check_return_code() rtm
2026-11-24  3:27 ` Alexei Starovoitov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox