1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2020 Google LLC.
9 #include <bpf/bpf_helpers.h>
10 #include <bpf/bpf_tracing.h>
12 u32 monitored_pid = 0;
15 __uint(type, BPF_MAP_TYPE_RINGBUF);
16 __uint(max_entries, 1 << 12);
17 } ringbuf SEC(".maps");
19 char _license[] SEC("license") = "GPL";
21 bool use_ima_file_hash;
22 bool enable_bprm_creds_for_exec;
23 bool enable_kernel_read_file;
26 static void ima_test_common(struct file *file)
33 pid = bpf_get_current_pid_tgid() >> 32;
34 if (pid == monitored_pid) {
35 if (!use_ima_file_hash)
36 ret = bpf_ima_inode_hash(file->f_inode, &ima_hash,
39 ret = bpf_ima_file_hash(file, &ima_hash,
41 if (ret < 0 || ima_hash == 0)
44 sample = bpf_ringbuf_reserve(&ringbuf, sizeof(u64), 0);
49 bpf_ringbuf_submit(sample, 0);
55 static int ima_test_deny(void)
59 pid = bpf_get_current_pid_tgid() >> 32;
60 if (pid == monitored_pid && test_deny)
66 SEC("lsm.s/bprm_committed_creds")
67 void BPF_PROG(bprm_committed_creds, struct linux_binprm *bprm)
69 ima_test_common(bprm->file);
72 SEC("lsm.s/bprm_creds_for_exec")
73 int BPF_PROG(bprm_creds_for_exec, struct linux_binprm *bprm)
75 if (!enable_bprm_creds_for_exec)
78 ima_test_common(bprm->file);
82 SEC("lsm.s/kernel_read_file")
83 int BPF_PROG(kernel_read_file, struct file *file, enum kernel_read_file_id id,
88 if (!enable_kernel_read_file)
94 if (id != READING_POLICY)
97 ret = ima_test_deny();
101 ima_test_common(file);