-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserspace_helper.c
More file actions
134 lines (130 loc) · 4.43 KB
/
userspace_helper.c
File metadata and controls
134 lines (130 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//go:build ignore
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#define NUM_RINGS 16
#define PATH_MAX 4096
s32 pid;
#define DEFINE_USERSPACE(id, request_type, response_type) \
struct context_##id { \
__u32 lock; \
__u64 pid_tgid; \
struct bpf_stack_build_id stack[80]; \
int stack_n; \
unsigned char comm[TASK_COMM_LEN]; \
unsigned char exe_path[PATH_MAX]; \
__u32 n; \
request_type value; \
}; \
struct { \
__uint(type, BPF_MAP_TYPE_ARRAY); \
__type(key, __u32); \
__type(value, struct context_##id); \
__uint(max_entries, NUM_RINGS); \
} request_array_##id SEC(".maps"); \
struct { \
__uint(type, BPF_MAP_TYPE_ARRAY); \
__type(key, __u32); \
__type(value, response_type); \
__uint(max_entries, NUM_RINGS); \
} response_array_##id SEC(".maps"); \
__u32 counter_##id = 0; \
static __always_inline const char *__get_dynamic_attr_##id(__u32 n) { \
switch (n % NUM_RINGS) { \
case 0: \
return "user." #id ".0"; \
case 1: \
return "user." #id ".1"; \
case 2: \
return "user." #id ".2"; \
case 3: \
return "user." #id ".3"; \
case 4: \
return "user." #id ".4"; \
case 5: \
return "user." #id ".5"; \
case 6: \
return "user." #id ".6"; \
case 7: \
return "user." #id ".7"; \
case 8: \
return "user." #id ".8"; \
case 9: \
return "user." #id ".9"; \
case 10: \
return "user." #id ".10"; \
case 11: \
return "user." #id ".11"; \
case 12: \
return "user." #id ".12"; \
case 13: \
return "user." #id ".13"; \
case 14: \
return "user." #id ".14"; \
case 15: \
return "user." #id ".15"; \
default: \
return "unreachable"; \
} \
} \
static __always_inline struct context_##id *userspace_blocking_reserve_##id() { \
__u32 n = __sync_fetch_and_add(&counter_##id, 1) % NUM_RINGS; \
struct context_##id *c = bpf_map_lookup_elem(&request_array_##id, &n); \
if (!c) \
return NULL; \
if (__sync_val_compare_and_swap(&c->lock, 0, 1)) \
return NULL; \
c->n = n; \
c->pid_tgid = bpf_get_current_pid_tgid(); \
struct task_struct *task = bpf_get_current_task_btf(); \
c->stack_n = bpf_get_task_stack(task, c->stack, sizeof(c->stack), BPF_F_USER_STACK | BPF_F_USER_BUILD_ID); \
bpf_get_current_comm(c->comm, sizeof(c->comm)); \
struct file *exe_file = bpf_get_task_exe_file(task); \
if (!exe_file) \
return NULL; \
bpf_path_d_path(&exe_file->f_path, (char *)c->exe_path, sizeof(c->exe_path)); \
bpf_put_file(exe_file); \
return c; \
} \
static __always_inline response_type *userspace_blocking_##id(struct context_##id *req) { \
struct task_struct *task = bpf_task_from_pid(pid); \
if (!task) { \
req->lock = 0; \
return NULL; \
} \
struct file *file = bpf_get_task_exe_file(task); \
if (!file) { \
bpf_task_release(task); \
req->lock = 0; \
return NULL; \
} \
response_type *res = bpf_map_lookup_elem(&response_array_##id, &req->n); \
if (!res) { \
bpf_put_file(file); \
bpf_task_release(task); \
req->lock = 0; \
return NULL; \
} \
struct bpf_dynptr dynp; \
long err = bpf_dynptr_from_mem(res, sizeof(response_type), 0, &dynp); \
if (err < 0) { \
bpf_printk("C n=%d err=%d", req->n, err); \
bpf_put_file(file); \
bpf_task_release(task); \
req->lock = 0; \
return NULL; \
} \
err = bpf_get_file_xattr(file, __get_dynamic_attr_##id(req->n), &dynp); \
if (err < 0) { \
bpf_printk("D n=%d err=%d", req->n, err); \
bpf_put_file(file); \
bpf_task_release(task); \
req->lock = 0; \
return NULL; \
} \
bpf_put_file(file); \
bpf_task_release(task); \
return res; \
} \
static __always_inline void userspace_blocking_end_##id(struct context_##id *req) { \
req->lock = 0; \
}