Skip to content

Commit bae141f

Browse files
committed
bpf: Emit audit messages upon successful prog load and unload
Allow for audit messages to be emitted upon BPF program load and unload for having a timeline of events. The load itself is in syscall context, so additional info about the process initiating the BPF prog creation can be logged and later directly correlated to the unload event. The only info really needed from BPF side is the globally unique prog ID where then audit user space tooling can query / dump all info needed about the specific BPF program right upon load event and enrich the record, thus these changes needed here can be kept small and non-intrusive to the core. Raw example output: # auditctl -D # auditctl -a always,exit -F arch=x86_64 -S bpf # ausearch --start recent -m 1334 ... ---- time->Wed Nov 27 16:04:13 2019 type=PROCTITLE msg=audit(1574867053.120:84664): proctitle="./bpf" type=SYSCALL msg=audit(1574867053.120:84664): arch=c000003e syscall=321 \ success=yes exit=3 a0=5 a1=7ffea484fbe0 a2=70 a3=0 items=0 ppid=7477 \ pid=12698 auid=1001 uid=1001 gid=1001 euid=1001 suid=1001 fsuid=1001 \ egid=1001 sgid=1001 fsgid=1001 tty=pts2 ses=4 comm="bpf" \ exe="/home/jolsa/auditd/audit-testsuite/tests/bpf/bpf" \ subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 key=(null) type=UNKNOWN[1334] msg=audit(1574867053.120:84664): prog-id=76 op=LOAD ---- time->Wed Nov 27 16:04:13 2019 type=UNKNOWN[1334] msg=audit(1574867053.120:84665): prog-id=76 op=UNLOAD ... Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Co-developed-by: Jiri Olsa <jolsa@kernel.org> Signed-off-by: Jiri Olsa <jolsa@kernel.org> Acked-by: Paul Moore <paul@paul-moore.com> Link: https://lore.kernel.org/bpf/20191206214934.11319-1-jolsa@kernel.org
1 parent b590cb5 commit bae141f

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

‎include/uapi/linux/audit.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
#define AUDIT_FANOTIFY 1331 /* Fanotify access decision */
117117
#define AUDIT_TIME_INJOFFSET 1332 /* Timekeeping offset injected */
118118
#define AUDIT_TIME_ADJNTPVAL 1333 /* NTP value adjustment */
119+
#define AUDIT_BPF 1334 /* BPF subsystem */
119120

120121
#define AUDIT_AVC 1400 /* SE Linux avc denial or grant */
121122
#define AUDIT_SELINUX_ERR 1401 /* Internal SE Linux Errors */

‎kernel/bpf/syscall.c‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/timekeeping.h>
2424
#include <linux/ctype.h>
2525
#include <linux/nospec.h>
26+
#include <linux/audit.h>
2627
#include <uapi/linux/btf.h>
2728

2829
#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
@@ -1306,6 +1307,36 @@ static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
13061307
return 0;
13071308
}
13081309

1310+
enum bpf_audit {
1311+
BPF_AUDIT_LOAD,
1312+
BPF_AUDIT_UNLOAD,
1313+
BPF_AUDIT_MAX,
1314+
};
1315+
1316+
static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1317+
[BPF_AUDIT_LOAD] = "LOAD",
1318+
[BPF_AUDIT_UNLOAD] = "UNLOAD",
1319+
};
1320+
1321+
static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1322+
{
1323+
struct audit_context *ctx = NULL;
1324+
struct audit_buffer *ab;
1325+
1326+
if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1327+
return;
1328+
if (audit_enabled == AUDIT_OFF)
1329+
return;
1330+
if (op == BPF_AUDIT_LOAD)
1331+
ctx = audit_context();
1332+
ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1333+
if (unlikely(!ab))
1334+
return;
1335+
audit_log_format(ab, "prog-id=%u op=%s",
1336+
prog->aux->id, bpf_audit_str[op]);
1337+
audit_log_end(ab);
1338+
}
1339+
13091340
int __bpf_prog_charge(struct user_struct *user, u32 pages)
13101341
{
13111342
unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
@@ -1421,6 +1452,7 @@ static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
14211452
{
14221453
if (atomic64_dec_and_test(&prog->aux->refcnt)) {
14231454
perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1455+
bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
14241456
/* bpf_prog_free_id() must be called first */
14251457
bpf_prog_free_id(prog, do_idr_lock);
14261458
__bpf_prog_put_noref(prog, true);
@@ -1830,6 +1862,7 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
18301862
*/
18311863
bpf_prog_kallsyms_add(prog);
18321864
perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
1865+
bpf_audit_prog(prog, BPF_AUDIT_LOAD);
18331866

18341867
err = bpf_prog_new_fd(prog);
18351868
if (err < 0)

0 commit comments

Comments
 (0)