@@ -38,6 +38,53 @@ This helper call can be used in the following program types:
3838<!-- [/HELPER_FUNC_PROG_REF] -->
3939
4040### Example
41+ The following program prints hash of files just before they are being executed.
42+ Kernel command line is ` ima_policy=tcb ima_hash=sha256 ` .
4143
42- !!! example "Docs could be improved"
43- This part of the docs is incomplete, contributions are very welcome
44+ ``` c
45+ #include " vmlinux.h"
46+ #include < bpf/bpf_helpers.h>
47+ #include < bpf/bpf_tracing.h>
48+
49+ static void print_sha256 (__ u8 * buf) {
50+ bpf_printk("IMA Hash Part 1: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
51+ buf[ 0] , buf[ 1] , buf[ 2] , buf[ 3] , buf[ 4] , buf[ 5] , buf[ 6] , buf[ 7] , buf[ 8] , buf[ 9] , buf[ 10] , buf[ 11] );
52+ bpf_printk("IMA Hash Part 2: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
53+ buf[ 12] , buf[ 13] , buf[ 14] , buf[ 15] , buf[ 16] , buf[ 17] , buf[ 18] , buf[ 19] , buf[ 20] , buf[ 21] , buf[ 22] , buf[ 23] );
54+ bpf_printk("IMA Hash Part 3: %02x%02x%02x%02x%02x%02x%02x%02x",
55+ buf[ 24] , buf[ 25] , buf[ 26] , buf[ 27] , buf[ 28] , buf[ 29] , buf[ 30] , buf[ 31] );
56+ }
57+ SEC("lsm.s/bprm_creds_for_exec")
58+ int BPF_PROG(test_func, struct linux_binprm * b)
59+ {
60+ // We are expecting SHA-256
61+ __ u8 buf[ 32 / sizeof(__ u8)] = {0};
62+ enum hash_algo algo = 0;
63+
64+ algo = bpf_ima_inode_hash(b->file->f_inode, buf, sizeof(buf));
65+ if (algo < 0)
66+ return 0;
67+ /*just to showcase enum hash_algo*/
68+ if(algo != HASH_ALGO_SHA256){
69+ bpf_printk("algo mismatch");
70+ return 0;
71+ }
72+ bpf_printk("%s", b->filename);
73+ print_sha256(buf);
74+ return 0;
75+ }
76+
77+ char __ license[ ] SEC("license") = "GPL";
78+ ```
79+
80+ Output should be something like this:
81+ ```
82+ <...>-20230 [ 008] ...11 9707.708954: bpf_trace_printk: /usr/bin/figlet
83+ <...>-20230 [ 008] ...11 9707.708957: bpf_trace_printk: IMA Hash Part 1: 1748eeb53c9479fb923fb772
84+ <...>-20230 [ 008] ...11 9707.708957: bpf_trace_printk: IMA Hash Part 2: c21bd9c9f5c27aa4e81c66cd
85+ <...>-20230 [ 008] ...11 9707.708957: bpf_trace_printk: IMA Hash Part 3: 59886d7b339e70d0
86+ <...>-20231 [ 000] ...11 9707.709873: bpf_trace_printk: /usr/bin/python3
87+ <...>-20231 [ 000] ...11 9707.709876: bpf_trace_printk: IMA Hash Part 1: e59d0124ff06c248546876e0
88+ <...>-20231 [ 000] ...11 9707.709876: bpf_trace_printk: IMA Hash Part 2: 1fcfb1ea3cda63534949f94a
89+ <...>-20231 [ 000] ...11 9707.709877: bpf_trace_printk: IMA Hash Part 3: 9372bfcfe3bfc3f5
90+ ```
0 commit comments