Fixes for ima selftest
Subsystems (3)
v4 cover Fixes for ima selftest
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.
KP Singh (4):
selftests/bpf: Update ima_setup.sh for busybox selftests/bpf: Ensure securityfs mount before writing ima policy selftests/bpf: Add config dependency on BLK_DEV_LOOP selftests/bpf: Indent ima_setup.sh with tabs.
tools/testing/selftests/bpf/config | 1 + tools/testing/selftests/bpf/ima_setup.sh | 107 +++++++++++++---------- 2 files changed, 64 insertions(+), 44 deletions(-)
Signature · 1 line
2.29.2.576.ga3fc446d84-goog
PATCH v4 1/4 selftests/bpf: Update ima_setup.sh for busybox
From: KP Singh <[email protected]>
losetup on busybox does not output the name of loop device on using -f with --show. It also doesn't support -j to find the loop devices for a given backing file. losetup is updated to use "-a" which is available on busybox.
blkid does not support options (-s and -o) to only display the uuid, so parse the output instead.
Not all environments have mkfs.ext4, the test requires a loop device with a backing image file which could formatted with any filesystem. Update to using mkfs.ext2 which is available on busybox.
--- tools/testing/selftests/bpf/ima_setup.sh | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/ima_setup.sh +8 -4
@@ -3,6 +3,7 @@ set -e set -u +set -o pipefail IMA_POLICY_FILE="/sys/kernel/security/ima/policy" TEST_BINARY="/bin/true"
@@ -23,13 +24,15 @@ setup() dd if=/dev/zero of="${mount_img}" bs=1M count=10 - local loop_device="$(losetup --find --show ${mount_img})" + losetup -f "${mount_img}" + local loop_device=$(losetup -a | grep ${mount_img:?} | cut -d ":" -f1) - mkfs.ext4 "${loop_device}" + mkfs.ext2 "${loop_device:?}" mount "${loop_device}" "${mount_dir}" cp "${TEST_BINARY}" "${mount_dir}" - local mount_uuid="$(blkid -s UUID -o value ${loop_device})" + local mount_uuid="$(blkid ${loop_device} | sed 's/.*UUID="\([^"]*\)".*/\1/')" + echo "measure func=BPRM_CHECK fsuuid=${mount_uuid}" > ${IMA_POLICY_FILE} }
@@ -38,7 +41,8 @@ cleanup() { local mount_img="${tmp_dir}/test.img" local mount_dir="${tmp_dir}/mnt" - local loop_devices=$(losetup -j ${mount_img} -O NAME --noheadings) + local loop_devices=$(losetup -a | grep ${mount_img:?} | cut -d ":" -f1) + for loop_dev in "${loop_devices}"; do losetup -d $loop_dev done
Signature · 1 line
2.29.2.576.ga3fc446d84-goog
PATCH v4 2/4 selftests/bpf: Ensure securityfs mount before writing ima policy
In reply to KP SinghFrom: KP Singh <[email protected]>
SecurityFS may not be mounted even if it is enabled in the kernel config. So, check if the mount exists in /proc/mounts by parsing the file and, if not, mount it on /sys/kernel/security.
--- tools/testing/selftests/bpf/ima_setup.sh | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
tools/testing/selftests/bpf/ima_setup.sh +15 -0
@@ -14,6 +14,20 @@ usage() exit 1 } +ensure_mount_securityfs() +{ + local securityfs_dir=$(grep "securityfs" /proc/mounts | awk '{print $2}') + + if [ -z "${securityfs_dir}" ]; then + securityfs_dir=/sys/kernel/security + mount -t securityfs security "${securityfs_dir}" + fi + + if [ ! -d "${securityfs_dir}" ]; then + echo "${securityfs_dir}: securityfs is not mounted" && exit 1 + fi +} + setup() { local tmp_dir="$1"
@@ -33,6 +47,7 @@ setup() cp "${TEST_BINARY}" "${mount_dir}" local mount_uuid="$(blkid ${loop_device} | sed 's/.*UUID="\([^"]*\)".*/\1/')" + ensure_mount_securityfs echo "measure func=BPRM_CHECK fsuuid=${mount_uuid}" > ${IMA_POLICY_FILE} }
Signature · 1 line
2.29.2.576.ga3fc446d84-goog
PATCH v4 3/4 selftests/bpf: Add config dependency on BLK_DEV_LOOP
In reply to KP SinghFrom: KP Singh <[email protected]>
The ima selftest restricts its scope to a test filesystem image mounted on a loop device and prevents permanent ima policy changes for the whole system.
--- tools/testing/selftests/bpf/config | 1 + 1 file changed, 1 insertion(+)
tools/testing/selftests/bpf/config +1 -0
@@ -43,3 +43,4 @@ CONFIG_IMA=y CONFIG_SECURITYFS=y CONFIG_IMA_WRITE_POLICY=y CONFIG_IMA_READ_POLICY=y +CONFIG_BLK_DEV_LOOP=y
Signature · 1 line
2.29.2.576.ga3fc446d84-goog
PATCH v4 4/4 selftests/bpf: Indent ima_setup.sh with tabs.
In reply to KP SinghFrom: KP Singh <[email protected]>
The file was formatted with spaces instead of tabs and went unnoticed as checkpatch.pl did not complain (probably because this is a shell script). Re-indent it with tabs to be consistent with other scripts.
--- tools/testing/selftests/bpf/ima_setup.sh | 108 +++++++++++------------ 1 file changed, 54 insertions(+), 54 deletions(-)
tools/testing/selftests/bpf/ima_setup.sh +54 -54
@@ -10,90 +10,90 @@ TEST_BINARY="/bin/true" usage() { - echo "Usage: $0 <setup|cleanup|run> <existing_tmp_dir>" - exit 1 + echo "Usage: $0 <setup|cleanup|run> <existing_tmp_dir>" + exit 1 } ensure_mount_securityfs() { - local securityfs_dir=$(grep "securityfs" /proc/mounts | awk '{print $2}') + local securityfs_dir=$(grep "securityfs" /proc/mounts | awk '{print $2}') - if [ -z "${securityfs_dir}" ]; then - securityfs_dir=/sys/kernel/security - mount -t securityfs security "${securityfs_dir}" - fi + if [ -z "${securityfs_dir}" ]; then + securityfs_dir=/sys/kernel/security + mount -t securityfs security "${securityfs_dir}" + fi - if [ ! -d "${securityfs_dir}" ]; then - echo "${securityfs_dir}: securityfs is not mounted" && exit 1 - fi + if [ ! -d "${securityfs_dir}" ]; then + echo "${securityfs_dir}: securityfs is not mounted" && exit 1 + fi } setup() { - local tmp_dir="$1" - local mount_img="${tmp_dir}/test.img" - local mount_dir="${tmp_dir}/mnt" - local copied_bin_path="${mount_dir}/$(basename ${TEST_BINARY})" - mkdir -p ${mount_dir} + local tmp_dir="$1" + local mount_img="${tmp_dir}/test.img" + local mount_dir="${tmp_dir}/mnt" + local copied_bin_path="${mount_dir}/$(basename ${TEST_BINARY})" + mkdir -p ${mount_dir} - dd if=/dev/zero of="${mount_img}" bs=1M count=10 + dd if=/dev/zero of="${mount_img}" bs=1M count=10 - losetup -f "${mount_img}" - local loop_device=$(losetup -a | grep ${mount_img:?} | cut -d ":" -f1) + losetup -f "${mount_img}" + local loop_device=$(losetup -a | grep ${mount_img:?} | cut -d ":" -f1) - mkfs.ext2 "${loop_device:?}" - mount "${loop_device}" "${mount_dir}" + mkfs.ext2 "${loop_device:?}" + mount "${loop_device}" "${mount_dir}" - cp "${TEST_BINARY}" "${mount_dir}" - local mount_uuid="$(blkid ${loop_device} | sed 's/.*UUID="\([^"]*\)".*/\1/')" + cp "${TEST_BINARY}" "${mount_dir}" + local mount_uuid="$(blkid ${loop_device} | sed 's/.*UUID="\([^"]*\)".*/\1/')" - ensure_mount_securityfs - echo "measure func=BPRM_CHECK fsuuid=${mount_uuid}" > ${IMA_POLICY_FILE} + ensure_mount_securityfs + echo "measure func=BPRM_CHECK fsuuid=${mount_uuid}" > ${IMA_POLICY_FILE} } cleanup() { - local tmp_dir="$1" - local mount_img="${tmp_dir}/test.img" - local mount_dir="${tmp_dir}/mnt" + local tmp_dir="$1" + local mount_img="${tmp_dir}/test.img" + local mount_dir="${tmp_dir}/mnt" - local loop_devices=$(losetup -a | grep ${mount_img:?} | cut -d ":" -f1) + local loop_devices=$(losetup -a | grep ${mount_img:?} | cut -d ":" -f1) - for loop_dev in "${loop_devices}"; do - losetup -d $loop_dev - done + for loop_dev in "${loop_devices}"; do + losetup -d $loop_dev + done - umount ${mount_dir} - rm -rf ${tmp_dir} + umount ${mount_dir} + rm -rf ${tmp_dir} } run() { - local tmp_dir="$1" - local mount_dir="${tmp_dir}/mnt" - local copied_bin_path="${mount_dir}/$(basename ${TEST_BINARY})" + local tmp_dir="$1" + local mount_dir="${tmp_dir}/mnt" + local copied_bin_path="${mount_dir}/$(basename ${TEST_BINARY})" - exec "${copied_bin_path}" + exec "${copied_bin_path}" } main() { - [[ $# -ne 2 ]] && usage - - local action="$1" - local tmp_dir="$2" - - [[ ! -d "${tmp_dir}" ]] && echo "Directory ${tmp_dir} doesn't exist" && exit 1 - - if [[ "${action}" == "setup" ]]; then - setup "${tmp_dir}" - elif [[ "${action}" == "cleanup" ]]; then - cleanup "${tmp_dir}" - elif [[ "${action}" == "run" ]]; then - run "${tmp_dir}" - else - echo "Unknown action: ${action}" - exit 1 - fi + [[ $# -ne 2 ]] && usage + + local action="$1" + local tmp_dir="$2" + + [[ ! -d "${tmp_dir}" ]] && echo "Directory ${tmp_dir} doesn't exist" && exit 1 + + if [[ "${action}" == "setup" ]]; then + setup "${tmp_dir}" + elif [[ "${action}" == "cleanup" ]]; then + cleanup "${tmp_dir}" + elif [[ "${action}" == "run" ]]; then + run "${tmp_dir}" + else + echo "Unknown action: ${action}" + exit 1 + fi } main "$@"
Signature · 1 line
2.29.2.576.ga3fc446d84-goog
Quoted 20 lines · KP Singh
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_hashhelper 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.
Quoted 8 lines
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/bpfand, lastly, changes the indentation ofima_setup.shto use tabs.
Thanks, I think this reads much better. And a few months from now it would be easier to understand and page back the context, if necessary.
I've pushed your fixes. ima selftest still emits a bunch of extra output from dd and maybe other commands:
10+0 records in
10+0 records out
10485760 bytes (10.0MB) copied, 0.037096 seconds, 269.6MB/s Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
2560 inodes, 10240 blocks
512 blocks (5%) reserved for the super user
First data block=1
Maximum filesystem blocks=262144
2 block groups
8192 blocks per group, 8192 fragments per group 1280 inodes per group
Superblock backups stored on blocks:
8193
Please follow up at your earliest convenience to silence those in default (non-verbose) mode.
Quoted 13 lines
KP Singh (4):
selftests/bpf: Updateima_setup.shfor busybox selftests/bpf: Ensure securityfs mount before writing ima policy selftests/bpf: Add config dependency onBLK_DEV_LOOPselftests/bpf: Indentima_setup.shwith tabs.tools/testing/selftests/bpf/config | 1 + tools/testing/selftests/bpf/ima_setup.sh | 107 +++++++++++++---------- 2 files changed, 64 insertions(+), 44 deletions(-)--
2.29.2.576.ga3fc446d84-goog
[...]
Quoted 17 lines
output from dd and maybe other commands:
10+0 records in
10+0 records out
10485760 bytes (10.0MB) copied, 0.037096 seconds, 269.6MB/s Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
2560 inodes, 10240 blocks
512 blocks (5%) reserved for the super user
First data block=1
Maximum filesystem blocks=262144
2 block groups
8192 blocks per group, 8192 fragments per group 1280 inodes per group
Superblock backups stored on blocks:
Quoted 4 lines
8193Please follow up at your earliest convenience to silence those in default (non-verbose) mode.
Thanks, fixed. I added a verbosity flag to ima_setup.sh and am using env.verbosity to toggle this extra output. I will send in my patch tomorrow.
[...]
This series was applied to bpf/bpf-next.git (refs/heads/master):
Hello:
This series was applied to bpf/bpf-next.git (refs/heads/master):
Quoted 9 lines · On Thu, 3 Dec 2020 19:14:33 +0000 you wrote:
From: KP Singh <[email protected]>
# v3 -> v4* Fix typos.
* Update commit message for the indentation patch.
* Added Andrii's acks.[...]
Here is the summary with links:
- [bpf-next,v4,1/4] selftests/bpf: Update ima_setup.sh for busybox
https://git.kernel.org/bpf/bpf-next/c/3db980449bc3 - [bpf-next,v4,2/4] selftests/bpf: Ensure securityfs mount before writing ima policy https://git.kernel.org/bpf/bpf-next/c/1ee076719d4e - [bpf-next,v4,3/4] selftests/bpf: Add config dependency on BLK_DEV_LOOP https://git.kernel.org/bpf/bpf-next/c/d932e043b9d6 - [bpf-next,v4,4/4] selftests/bpf: Indent ima_setup.sh with tabs. https://git.kernel.org/bpf/bpf-next/c/ffebecd9d495
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html