Skip to content

Commit a8b415c

Browse files
committed
Merge branch 'Fixes for ima selftest'
KP Singh says: ==================== From: KP Singh <[email protected]> # v3 -> v4 * Fix typos. * Update commit message for the indentation patch. * Added Andrii's acks. # v2 -> v3 * Added missing tags. * Indentation fixes + some other fixes suggested by Andrii. * Re-indent file to tabs. The selftest for the bpf_ima_inode_hash helper uses a shell script to setup the system for ima. While this worked without an issue on recent desktop distros, it failed on environments with stripped out shells like busybox which is also used by the bpf CI. This series fixes the assumptions made on the availablity of certain command line switches and the expectation that securityfs being mounted by default. It also adds the missing kernel config dependencies in tools/testing/selftests/bpf and, lastly, changes the indentation of ima_setup.sh to use tabs. ==================== Signed-off-by: Andrii Nakryiko <[email protected]>
2 parents 61b7594 + ffebecd commit a8b415c

File tree

2 files changed

+64
-44
lines changed

2 files changed

+64
-44
lines changed

tools/testing/selftests/bpf/config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ CONFIG_IMA=y
4343
CONFIG_SECURITYFS=y
4444
CONFIG_IMA_WRITE_POLICY=y
4545
CONFIG_IMA_READ_POLICY=y
46+
CONFIG_BLK_DEV_LOOP=y

tools/testing/selftests/bpf/ima_setup.sh

Lines changed: 63 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,78 +3,97 @@
33

44
set -e
55
set -u
6+
set -o pipefail
67

78
IMA_POLICY_FILE="/sys/kernel/security/ima/policy"
89
TEST_BINARY="/bin/true"
910

1011
usage()
1112
{
12-
echo "Usage: $0 <setup|cleanup|run> <existing_tmp_dir>"
13-
exit 1
13+
echo "Usage: $0 <setup|cleanup|run> <existing_tmp_dir>"
14+
exit 1
15+
}
16+
17+
ensure_mount_securityfs()
18+
{
19+
local securityfs_dir=$(grep "securityfs" /proc/mounts | awk '{print $2}')
20+
21+
if [ -z "${securityfs_dir}" ]; then
22+
securityfs_dir=/sys/kernel/security
23+
mount -t securityfs security "${securityfs_dir}"
24+
fi
25+
26+
if [ ! -d "${securityfs_dir}" ]; then
27+
echo "${securityfs_dir}: securityfs is not mounted" && exit 1
28+
fi
1429
}
1530

1631
setup()
1732
{
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}
33+
local tmp_dir="$1"
34+
local mount_img="${tmp_dir}/test.img"
35+
local mount_dir="${tmp_dir}/mnt"
36+
local copied_bin_path="${mount_dir}/$(basename ${TEST_BINARY})"
37+
mkdir -p ${mount_dir}
38+
39+
dd if=/dev/zero of="${mount_img}" bs=1M count=10
2340

24-
dd if=/dev/zero of="${mount_img}" bs=1M count=10
41+
losetup -f "${mount_img}"
42+
local loop_device=$(losetup -a | grep ${mount_img:?} | cut -d ":" -f1)
2543

26-
local loop_device="$(losetup --find --show ${mount_img})"
44+
mkfs.ext2 "${loop_device:?}"
45+
mount "${loop_device}" "${mount_dir}"
2746

28-
mkfs.ext4 "${loop_device}"
29-
mount "${loop_device}" "${mount_dir}"
47+
cp "${TEST_BINARY}" "${mount_dir}"
48+
local mount_uuid="$(blkid ${loop_device} | sed 's/.*UUID="\([^"]*\)".*/\1/')"
3049

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}
50+
ensure_mount_securityfs
51+
echo "measure func=BPRM_CHECK fsuuid=${mount_uuid}" > ${IMA_POLICY_FILE}
3452
}
3553

3654
cleanup() {
37-
local tmp_dir="$1"
38-
local mount_img="${tmp_dir}/test.img"
39-
local mount_dir="${tmp_dir}/mnt"
55+
local tmp_dir="$1"
56+
local mount_img="${tmp_dir}/test.img"
57+
local mount_dir="${tmp_dir}/mnt"
58+
59+
local loop_devices=$(losetup -a | grep ${mount_img:?} | cut -d ":" -f1)
4060

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
61+
for loop_dev in "${loop_devices}"; do
62+
losetup -d $loop_dev
63+
done
4564

46-
umount ${mount_dir}
47-
rm -rf ${tmp_dir}
65+
umount ${mount_dir}
66+
rm -rf ${tmp_dir}
4867
}
4968

5069
run()
5170
{
52-
local tmp_dir="$1"
53-
local mount_dir="${tmp_dir}/mnt"
54-
local copied_bin_path="${mount_dir}/$(basename ${TEST_BINARY})"
71+
local tmp_dir="$1"
72+
local mount_dir="${tmp_dir}/mnt"
73+
local copied_bin_path="${mount_dir}/$(basename ${TEST_BINARY})"
5574

56-
exec "${copied_bin_path}"
75+
exec "${copied_bin_path}"
5776
}
5877

5978
main()
6079
{
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
80+
[[ $# -ne 2 ]] && usage
81+
82+
local action="$1"
83+
local tmp_dir="$2"
84+
85+
[[ ! -d "${tmp_dir}" ]] && echo "Directory ${tmp_dir} doesn't exist" && exit 1
86+
87+
if [[ "${action}" == "setup" ]]; then
88+
setup "${tmp_dir}"
89+
elif [[ "${action}" == "cleanup" ]]; then
90+
cleanup "${tmp_dir}"
91+
elif [[ "${action}" == "run" ]]; then
92+
run "${tmp_dir}"
93+
else
94+
echo "Unknown action: ${action}"
95+
exit 1
96+
fi
7897
}
7998

8099
main "$@"

0 commit comments

Comments
 (0)