Skip to content

Commit 34b82d3

Browse files
sinkapborkmann
authored andcommitted
bpf: Add a selftest for bpf_ima_inode_hash
The test does the following: - Mounts a loopback filesystem and appends the IMA policy to measure executions only on this file-system. Restricting the IMA policy to a particular filesystem prevents a system-wide IMA policy change. - Executes an executable copied to this loopback filesystem. - Calls the bpf_ima_inode_hash in the bprm_committed_creds hook and checks if the call succeeded and checks if a hash was calculated. The test shells out to the added ima_setup.sh script as the setup is better handled in a shell script and is more complicated to do in the test program or even shelling out individual commands from C. The list of required configs (i.e. IMA, SECURITYFS, IMA_{WRITE,READ}_POLICY) for running this test are also updated. Suggested-by: Mimi Zohar <[email protected]> (limit policy rule to loopback mount) Signed-off-by: KP Singh <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 27672f0 commit 34b82d3

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed

tools/testing/selftests/bpf/config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,7 @@ CONFIG_BPF_JIT=y
3939
CONFIG_BPF_LSM=y
4040
CONFIG_SECURITY=y
4141
CONFIG_LIRC=y
42+
CONFIG_IMA=y
43+
CONFIG_SECURITYFS=y
44+
CONFIG_IMA_WRITE_POLICY=y
45+
CONFIG_IMA_READ_POLICY=y
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-2.0
3+
4+
set -e
5+
set -u
6+
7+
IMA_POLICY_FILE="/sys/kernel/security/ima/policy"
8+
TEST_BINARY="/bin/true"
9+
10+
usage()
11+
{
12+
echo "Usage: $0 <setup|cleanup|run> <existing_tmp_dir>"
13+
exit 1
14+
}
15+
16+
setup()
17+
{
18+
local tmp_dir="$1"
19+
local mount_img="${tmp_dir}/test.img"
20+
local mount_dir="${tmp_dir}/mnt"
21+
local copied_bin_path="${mount_dir}/$(basename ${TEST_BINARY})"
22+
mkdir -p ${mount_dir}
23+
24+
dd if=/dev/zero of="${mount_img}" bs=1M count=10
25+
26+
local loop_device="$(losetup --find --show ${mount_img})"
27+
28+
mkfs.ext4 "${loop_device}"
29+
mount "${loop_device}" "${mount_dir}"
30+
31+
cp "${TEST_BINARY}" "${mount_dir}"
32+
local mount_uuid="$(blkid -s UUID -o value ${loop_device})"
33+
echo "measure func=BPRM_CHECK fsuuid=${mount_uuid}" > ${IMA_POLICY_FILE}
34+
}
35+
36+
cleanup() {
37+
local tmp_dir="$1"
38+
local mount_img="${tmp_dir}/test.img"
39+
local mount_dir="${tmp_dir}/mnt"
40+
41+
local loop_devices=$(losetup -j ${mount_img} -O NAME --noheadings)
42+
for loop_dev in "${loop_devices}"; do
43+
losetup -d $loop_dev
44+
done
45+
46+
umount ${mount_dir}
47+
rm -rf ${tmp_dir}
48+
}
49+
50+
run()
51+
{
52+
local tmp_dir="$1"
53+
local mount_dir="${tmp_dir}/mnt"
54+
local copied_bin_path="${mount_dir}/$(basename ${TEST_BINARY})"
55+
56+
exec "${copied_bin_path}"
57+
}
58+
59+
main()
60+
{
61+
[[ $# -ne 2 ]] && usage
62+
63+
local action="$1"
64+
local tmp_dir="$2"
65+
66+
[[ ! -d "${tmp_dir}" ]] && echo "Directory ${tmp_dir} doesn't exist" && exit 1
67+
68+
if [[ "${action}" == "setup" ]]; then
69+
setup "${tmp_dir}"
70+
elif [[ "${action}" == "cleanup" ]]; then
71+
cleanup "${tmp_dir}"
72+
elif [[ "${action}" == "run" ]]; then
73+
run "${tmp_dir}"
74+
else
75+
echo "Unknown action: ${action}"
76+
exit 1
77+
fi
78+
}
79+
80+
main "$@"
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
/*
4+
* Copyright (C) 2020 Google LLC.
5+
*/
6+
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
#include <unistd.h>
10+
#include <sys/wait.h>
11+
#include <test_progs.h>
12+
13+
#include "ima.skel.h"
14+
15+
static int run_measured_process(const char *measured_dir, u32 *monitored_pid)
16+
{
17+
int child_pid, child_status;
18+
19+
child_pid = fork();
20+
if (child_pid == 0) {
21+
*monitored_pid = getpid();
22+
execlp("./ima_setup.sh", "./ima_setup.sh", "run", measured_dir,
23+
NULL);
24+
exit(errno);
25+
26+
} else if (child_pid > 0) {
27+
waitpid(child_pid, &child_status, 0);
28+
return WEXITSTATUS(child_status);
29+
}
30+
31+
return -EINVAL;
32+
}
33+
34+
void test_test_ima(void)
35+
{
36+
char measured_dir_template[] = "/tmp/ima_measuredXXXXXX";
37+
const char *measured_dir;
38+
char cmd[256];
39+
40+
int err, duration = 0;
41+
struct ima *skel = NULL;
42+
43+
skel = ima__open_and_load();
44+
if (CHECK(!skel, "skel_load", "skeleton failed\n"))
45+
goto close_prog;
46+
47+
err = ima__attach(skel);
48+
if (CHECK(err, "attach", "attach failed: %d\n", err))
49+
goto close_prog;
50+
51+
measured_dir = mkdtemp(measured_dir_template);
52+
if (CHECK(measured_dir == NULL, "mkdtemp", "err %d\n", errno))
53+
goto close_prog;
54+
55+
snprintf(cmd, sizeof(cmd), "./ima_setup.sh setup %s", measured_dir);
56+
if (CHECK_FAIL(system(cmd)))
57+
goto close_clean;
58+
59+
err = run_measured_process(measured_dir, &skel->bss->monitored_pid);
60+
if (CHECK(err, "run_measured_process", "err = %d\n", err))
61+
goto close_clean;
62+
63+
CHECK(skel->data->ima_hash_ret < 0, "ima_hash_ret",
64+
"ima_hash_ret = %ld\n", skel->data->ima_hash_ret);
65+
66+
CHECK(skel->bss->ima_hash == 0, "ima_hash",
67+
"ima_hash = %lu\n", skel->bss->ima_hash);
68+
69+
close_clean:
70+
snprintf(cmd, sizeof(cmd), "./ima_setup.sh cleanup %s", measured_dir);
71+
CHECK_FAIL(system(cmd));
72+
close_prog:
73+
ima__destroy(skel);
74+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
/*
4+
* Copyright 2020 Google LLC.
5+
*/
6+
7+
#include "vmlinux.h"
8+
#include <errno.h>
9+
#include <bpf/bpf_helpers.h>
10+
#include <bpf/bpf_tracing.h>
11+
12+
long ima_hash_ret = -1;
13+
u64 ima_hash = 0;
14+
u32 monitored_pid = 0;
15+
16+
char _license[] SEC("license") = "GPL";
17+
18+
SEC("lsm.s/bprm_committed_creds")
19+
int BPF_PROG(ima, struct linux_binprm *bprm)
20+
{
21+
u32 pid = bpf_get_current_pid_tgid() >> 32;
22+
23+
if (pid == monitored_pid)
24+
ima_hash_ret = bpf_ima_inode_hash(bprm->file->f_inode,
25+
&ima_hash, sizeof(ima_hash));
26+
27+
return 0;
28+
}

0 commit comments

Comments
 (0)