|
23 | 23 | #include <linux/timekeeping.h> |
24 | 24 | #include <linux/ctype.h> |
25 | 25 | #include <linux/nospec.h> |
| 26 | +#include <linux/audit.h> |
26 | 27 | #include <uapi/linux/btf.h> |
27 | 28 |
|
28 | 29 | #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \ |
@@ -1318,6 +1319,34 @@ static void free_used_maps(struct bpf_prog_aux *aux) |
1318 | 1319 | kfree(aux->used_maps); |
1319 | 1320 | } |
1320 | 1321 |
|
| 1322 | +enum bpf_event { |
| 1323 | + BPF_EVENT_LOAD, |
| 1324 | + BPF_EVENT_UNLOAD, |
| 1325 | +}; |
| 1326 | + |
| 1327 | +static const char * const bpf_event_audit_str[] = { |
| 1328 | + [BPF_EVENT_LOAD] = "LOAD", |
| 1329 | + [BPF_EVENT_UNLOAD] = "UNLOAD", |
| 1330 | +}; |
| 1331 | + |
| 1332 | +static void bpf_audit_prog(const struct bpf_prog *prog, enum bpf_event event) |
| 1333 | +{ |
| 1334 | + bool has_task_context = event == BPF_EVENT_LOAD; |
| 1335 | + struct audit_buffer *ab; |
| 1336 | + |
| 1337 | + if (audit_enabled == AUDIT_OFF) |
| 1338 | + return; |
| 1339 | + ab = audit_log_start(audit_context(), GFP_ATOMIC, AUDIT_BPF); |
| 1340 | + if (unlikely(!ab)) |
| 1341 | + return; |
| 1342 | + if (has_task_context) |
| 1343 | + audit_log_task(ab); |
| 1344 | + audit_log_format(ab, "%sprog-id=%u event=%s", |
| 1345 | + has_task_context ? " " : "", |
| 1346 | + prog->aux->id, bpf_event_audit_str[event]); |
| 1347 | + audit_log_end(ab); |
| 1348 | +} |
| 1349 | + |
1321 | 1350 | int __bpf_prog_charge(struct user_struct *user, u32 pages) |
1322 | 1351 | { |
1323 | 1352 | unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
@@ -1434,6 +1463,7 @@ static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock) |
1434 | 1463 | { |
1435 | 1464 | if (atomic64_dec_and_test(&prog->aux->refcnt)) { |
1436 | 1465 | perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0); |
| 1466 | + bpf_audit_prog(prog, BPF_EVENT_UNLOAD); |
1437 | 1467 | /* bpf_prog_free_id() must be called first */ |
1438 | 1468 | bpf_prog_free_id(prog, do_idr_lock); |
1439 | 1469 | __bpf_prog_put_noref(prog, true); |
@@ -1843,6 +1873,7 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr) |
1843 | 1873 | */ |
1844 | 1874 | bpf_prog_kallsyms_add(prog); |
1845 | 1875 | perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0); |
| 1876 | + bpf_audit_prog(prog, BPF_EVENT_LOAD); |
1846 | 1877 |
|
1847 | 1878 | err = bpf_prog_new_fd(prog); |
1848 | 1879 | if (err < 0) |
|
0 commit comments