Skip to content

Commit f526ddd

Browse files
AliGhaffariandylandreimerink
authored andcommitted
helpers: add example for bpf_ima_[inode/file]_hash
1 parent 55b3bde commit f526ddd

2 files changed

Lines changed: 102 additions & 4 deletions

File tree

docs/linux/helper-function/bpf_ima_file_hash.md

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,57 @@ 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+
58+
SEC("lsm.s/bprm_creds_for_exec")
59+
int BPF_PROG(test_func, struct linux_binprm *b)
60+
{
61+
// We are expecting SHA-256
62+
__u8 buf[32 / sizeof(__u8)] = {0};
63+
enum hash_algo algo = 0;
64+
65+
algo = bpf_ima_file_hash(b->file, buf, sizeof(buf));
66+
67+
if(algo < 0)
68+
return 0;
69+
/*just to showcase enum hash_algo*/
70+
if(algo != HASH_ALGO_SHA256){
71+
bpf_printk("algo mismatch");
72+
return 0;
73+
}
74+
75+
bpf_printk("%s", b->filename);
76+
print_sha256(buf);
77+
78+
return 0;
79+
}
80+
81+
char __license[] SEC("license") = "GPL";
82+
```
83+
84+
Output should be something like this:
85+
```
86+
<...>-18169 [004] ...11 8969.860732: bpf_trace_printk: /usr/bin/cat
87+
<...>-18169 [004] ...11 8969.860738: bpf_trace_printk: IMA Hash Part 1: 8a5c20c3400a4058a487cd80
88+
<...>-18169 [004] ...11 8969.860739: bpf_trace_printk: IMA Hash Part 2: 6111cc5138ef4d0fbc6714ff
89+
<...>-18169 [004] ...11 8969.860739: bpf_trace_printk: IMA Hash Part 3: 67c9432e38c2705a
90+
<...>-18171 [011] ...11 8969.861704: bpf_trace_printk: /usr/bin/glow
91+
<...>-18171 [011] ...11 8969.861708: bpf_trace_printk: IMA Hash Part 1: aed777d7f19376fefe2d0f3d
92+
<...>-18171 [011] ...11 8969.861709: bpf_trace_printk: IMA Hash Part 2: cd52d2f981d08c579b598b6d
93+
<...>-18171 [011] ...11 8969.861709: bpf_trace_printk: IMA Hash Part 3: 9cb6af4c3234e5ff
94+
```

docs/linux/helper-function/bpf_ima_inode_hash.md

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)