Skip to content

Commit d0be889

Browse files
committed
Add setuid C example
1 parent dda05bd commit d0be889

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

examples/c-form/ex7.bpf.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <linux/bpf.h>
3+
#include <bpf/bpf_helpers.h>
4+
#include <bpf/bpf_tracing.h>
5+
6+
7+
struct event {
8+
__u32 pid;
9+
__u32 uid;
10+
__u64 ts;
11+
};
12+
13+
struct {
14+
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
15+
__uint(key_size, sizeof(int));
16+
__uint(value_size, sizeof(int));
17+
} events SEC(".maps");
18+
19+
SEC("tp/syscalls/sys_enter_setuid")
20+
int handle_setuid_entry(struct trace_event_raw_sys_enter *ctx)
21+
{
22+
struct event data = {};
23+
24+
// Extract UID from the syscall arguments
25+
data.uid = (unsigned int)ctx->args[0];
26+
data.ts = bpf_ktime_get_ns();
27+
data.pid = bpf_get_current_pid_tgid() >> 32;
28+
29+
bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, &data, sizeof(data));
30+
31+
return 0;
32+
}
33+
34+
char LICENSE[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)