-
Describe the bug file openat.bpf.c //go:build ignore
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 10240);
__type(key, u32);
__type(value, struct openat_args);
} start SEC(".maps");
#define NAME_MAX 255
struct openat_args {
int flags;
const char *fname_ptr;
char fname[NAME_MAX];
};
SEC("tp/syscalls/sys_enter_openat")
int enter_openat(struct trace_event_raw_sys_enter* ctx)
{
u64 id = bpf_get_current_pid_tgid();
struct openat_args arg ={};
arg.flags = (int)ctx->args[2];
arg.fname_ptr = (const char *)ctx->args[1];
bpf_map_update_elem(&start, &id, &arg, BPF_ANY);
return 0;
}
SEC("tp/syscalls/sys_exit_openat")
int exit_openat(struct trace_event_raw_sys_exit* ctx)
{
u64 id = bpf_get_current_pid_tgid();
struct openat_args *arg = bpf_map_lookup_elem(&start, &id);
if (!arg) {
return 0;
}
// Read filename on syscall exit
bpf_probe_read_user(&arg->fname, sizeof(arg->fname), (void *)arg->fname_ptr);
bpf_printk("openat: filename: %s flags: %d\n",arg->fname,arg->flags);
bpf_map_delete_elem(&start, &id);
return 0;
}
char LICENSE[] SEC("license") = "GPL"; doc.go
error
Expected behavior |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
struct openat_args {
int flags;
const char *fname_ptr;
char fname[NAME_MAX];
}; The problem here is struct openat_args {
int flags;
uintptr_t fname_ptr;
char fname[NAME_MAX];
}; Instead of a pointer you store the raw value of the pointer. In userspace you can retrieve that value and do with it what you want (maybe via ptrace?), but you can't stuff it into a Go pointer. |
Beta Was this translation helpful? Give feedback.
The problem here is
fname_ptr
as you've probably figured out. Pointers are not supported because we can't put C pointer values into Go pointer types. Taking a pointer from kernel space and doing something with it in user space in general is complicated. The way to work around this is by changing the type declaration:Instead of a pointer you store the raw value of the pointer. In userspace you can retrieve that value and do with it what you want (maybe via ptrace?), but you can't stuff it into a Go pointer.