Skip to contentSkip to conversation

selftests/bpf: Update ima_setup.sh for busybox

PATCH Superseded KP Singh 2 participants 9 messages Dec 2, 2020 – Dec 3, 2020

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

KP Singh

author
#0 lore raw

PATCH v2 1/3 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 dosn'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.
* 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.

Fixes 34b82d3ac105 ("bpf: Add a selftest for bpf_ima_inode_hash")
---
 tools/testing/selftests/bpf/ima_setup.sh | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/ima_setup.sh +8 -4
index 15490ccc5e55..d864c62c1207 100755
@@ -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
author
#1 lore raw

PATCH v2 2/3 selftests/bpf: Ensure securityfs mount before writing ima policy

From: KP Singh <[email protected]>

SecurityFS may not be mounted even if it is enabled in the kernel config.

---
 tools/testing/selftests/bpf/ima_setup.sh | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
tools/testing/selftests/bpf/ima_setup.sh +15 -0
index d864c62c1207..1da2d97a0e72 100755
@@ -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
#3 lore raw

Re: [PATCH bpf-next v2 2/3] selftests/bpf: Ensure securityfs mount before writing ima policy

In reply to KP Singh
Quoted 8 lines · KP Singh

From: KP Singh <[email protected]>

SecurityFS may not be mounted even if it is enabled in the kernel config.

---

please add those tags in v2 :)

Quoted 9 lines
 tools/testing/selftests/bpf/ima_setup.sh | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
tools/testing/selftests/bpf/ima_setup.sh +0 -0
index d864c62c1207..1da2d97a0e72 100755
@@ -14,6 +14,20 @@ usage()
         exit 1

tools/testing/selftests/bpf/ima_setup.sh usage() · line 24

Quoted 10 lines
tools/testing/selftests/bpf/ima_setup.sh +8 -0
 }
 
+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

again, something is off with indentation here

tools/testing/selftests/bpf/ima_setup.sh usage() · line 27

Quoted 3 lines
tools/testing/selftests/bpf/ima_setup.sh +3 -0
+
+       if [ ! -d "${securityfs_dir}" ]; then
+               echo "${securityfs_dir} :securityfs is not mounted" && exit 1

nit: " :" => ": " or it was intended this way?

Quoted 17 lines
tools/testing/selftests/bpf/ima_setup.sh +4 -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}
 }
 
--

2.29.2.576.ga3fc446d84-goog

author
#4 lore raw

Re: [PATCH bpf-next v2 2/3] selftests/bpf: Ensure securityfs mount before writing ima policy

Quoted 5 lines · Andrii Nakryiko
Quoted 3 lines · KP Singh

From: KP Singh <[email protected]>

Quoted 7 lines
Quoted 5 lines

SecurityFS may not be mounted even if it is enabled in the kernel config.

---

please add those tags in v2 :)

Done.

Quoted 12 lines
Quoted 12 lines
 tools/testing/selftests/bpf/ima_setup.sh | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
tools/testing/selftests/bpf/ima_setup.sh +0 -0
index d864c62c1207..1da2d97a0e72 100755
@@ -14,6 +14,20 @@ usage()
         exit 1
 }
 
Quoted 10 lines

tools/testing/selftests/bpf/ima_setup.sh usage() · line 24

Quoted 8 lines
tools/testing/selftests/bpf/ima_setup.sh +8 -0
+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

again, something is off with indentation here

Yeah, changed it to spaces and added a patch at the end which changes it to tabs.
Checkpatch did not catch it, maybe it ignores shell scripts :(

Quoted 6 lines

tools/testing/selftests/bpf/ima_setup.sh usage() · line 27

Quoted 4 lines
tools/testing/selftests/bpf/ima_setup.sh +3 -0
+
+       if [ ! -d "${securityfs_dir}" ]; then
+               echo "${securityfs_dir} :securityfs is not mounted" && exit 1

nit: " :" => ": " or it was intended this way?

Fixes. Thanks.

Quoted 19 lines
Quoted 19 lines
tools/testing/selftests/bpf/ima_setup.sh +4 -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}
 }
 
--

2.29.2.576.ga3fc446d84-goog

author
#5 lore raw

PATCH v2 3/3 selftests/bpf: Add config dependency on BLK_DEV_LOOP

In reply to KP Singh

From: 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
index 365bf9771b07..37e1f303fc11 100644
@@ -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
author
#6 lore raw

Re: [PATCH bpf-next v2 3/3] selftests/bpf: Add config dependency on BLK_DEV_LOOP

Quoted 8 lines · KP Singh

From: 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.

Fixes 34b82d3ac105 ("bpf: Add a selftest for bpf_ima_inode_hash")
#7 lore raw
In reply to KP Singh
Quoted 12 lines · KP Singh

From: KP Singh <[email protected]>

* losetup on busybox does not output the name of loop device on using -f with --show. It also dosn'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.
* 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.

Quoted 4 lines
Fixes 34b82d3ac105 ("bpf: Add a selftest for bpf_ima_inode_hash")

---

Thanks for the fixes!

You have three related patches, please add a cover letter to the patch set as well.

Quoted 20 lines
 tools/testing/selftests/bpf/ima_setup.sh | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/ima_setup.sh +1 -1
index 15490ccc5e55..d864c62c1207 100755
@@ -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})"

tools/testing/selftests/bpf/ima_setup.sh setup() · line 34

Quoted 10 lines
tools/testing/selftests/bpf/ima_setup.sh +4 -2
+        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/')"

tabs/spaces mix up here?

tools/testing/selftests/bpf/ima_setup.sh cleanup() { · line 44

Quoted 10 lines
tools/testing/selftests/bpf/ima_setup.sh +2 -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)

didn't know about :?, fancy

Quoted 7 lines
tools/testing/selftests/bpf/ima_setup.sh +1 -1
+
         for loop_dev in "${loop_devices}"; do
                 losetup -d $loop_dev
         done
--

2.29.2.576.ga3fc446d84-goog

author
#8 lore raw
Quoted 19 lines · Andrii Nakryiko
Quoted 12 lines · KP Singh

From: KP Singh <[email protected]>

* losetup on busybox does not output the name of loop device on using -f with --show. It also dosn'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.
* 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.

Quoted 4 lines
Fixes 34b82d3ac105 ("bpf: Add a selftest for bpf_ima_inode_hash")

---

Quoted 4 lines

Thanks for the fixes!

You have three related patches, please add a cover letter to the patch set as well.

Will do.

Quoted 28 lines
Quoted 28 lines
 tools/testing/selftests/bpf/ima_setup.sh | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/ima_setup.sh +4 -2
index 15490ccc5e55..d864c62c1207 100755
@@ -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}"
 
Quoted 5 lines

tools/testing/selftests/bpf/ima_setup.sh setup() · line 34

Quoted 3 lines
tools/testing/selftests/bpf/ima_setup.sh +1 -1
         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/')"

tabs/spaces mix up here?

Yeah, it seems like the whole file is formatted with spaces and I added tabs here. Added a patch
at the end of the series which reindents this. I have not added the "Fixes" tag since it's not
a fix, do feel free to add it, if you think that's the norm for these kinds of indentation fixes.

Quoted 5 lines
Quoted 5 lines
tools/testing/selftests/bpf/ima_setup.sh +1 -0
+
         echo "measure func=BPRM_CHECK fsuuid=${mount_uuid}" > ${IMA_POLICY_FILE}
 }
 
Quoted 8 lines

tools/testing/selftests/bpf/ima_setup.sh cleanup() { · line 44

Quoted 6 lines
tools/testing/selftests/bpf/ima_setup.sh +1 -1
@@ -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)

didn't know about :?, fancy

Thanks :) It has helped me in rm -rf ${dir:?}/${file:?}

Quoted 9 lines
Quoted 9 lines
tools/testing/selftests/bpf/ima_setup.sh +1 -1
+
         for loop_dev in "${loop_devices}"; do
                 losetup -d $loop_dev
         done
--

2.29.2.576.ga3fc446d84-goog