* [PATCH bpf-next v4 1/4] selftests/bpf: Update ima_setup.sh for busybox
2020-12-03 19:14 [PATCH bpf-next v4 0/4] Fixes for ima selftest KP Singh
@ 2020-12-03 19:14 ` KP Singh
2020-12-03 19:14 ` [PATCH bpf-next v4 2/4] selftests/bpf: Ensure securityfs mount before writing ima policy KP Singh
` (4 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: KP Singh @ 2020-12-03 19:14 UTC (permalink / raw)
To: bpf; +Cc: Andrii Nakryiko, Alexei Starovoitov, Daniel Borkmann
From: KP Singh <kpsingh@google.com>
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.
Fixes: 34b82d3ac105 ("bpf: Add a selftest for bpf_ima_inode_hash")
Reported-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: KP Singh <kpsingh@google.com>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
---
tools/testing/selftests/bpf/ima_setup.sh | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/tools/testing/selftests/bpf/ima_setup.sh b/tools/testing/selftests/bpf/ima_setup.sh
index 15490ccc5e55..137f2d32598f 100755
--- a/tools/testing/selftests/bpf/ima_setup.sh
+++ b/tools/testing/selftests/bpf/ima_setup.sh
@@ -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
--
2.29.2.576.ga3fc446d84-goog
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH bpf-next v4 2/4] selftests/bpf: Ensure securityfs mount before writing ima policy
2020-12-03 19:14 [PATCH bpf-next v4 0/4] Fixes for ima selftest KP Singh
2020-12-03 19:14 ` [PATCH bpf-next v4 1/4] selftests/bpf: Update ima_setup.sh for busybox KP Singh
@ 2020-12-03 19:14 ` KP Singh
2020-12-03 19:14 ` [PATCH bpf-next v4 3/4] selftests/bpf: Add config dependency on BLK_DEV_LOOP KP Singh
` (3 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: KP Singh @ 2020-12-03 19:14 UTC (permalink / raw)
To: bpf; +Cc: Andrii Nakryiko, Alexei Starovoitov, Daniel Borkmann
From: KP Singh <kpsingh@google.com>
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.
Fixes: 34b82d3ac105 ("bpf: Add a selftest for bpf_ima_inode_hash")
Reported-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: KP Singh <kpsingh@google.com>
---
tools/testing/selftests/bpf/ima_setup.sh | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/tools/testing/selftests/bpf/ima_setup.sh b/tools/testing/selftests/bpf/ima_setup.sh
index 137f2d32598f..b1ee4bf06996 100755
--- a/tools/testing/selftests/bpf/ima_setup.sh
+++ b/tools/testing/selftests/bpf/ima_setup.sh
@@ -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}
}
--
2.29.2.576.ga3fc446d84-goog
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH bpf-next v4 3/4] selftests/bpf: Add config dependency on BLK_DEV_LOOP
2020-12-03 19:14 [PATCH bpf-next v4 0/4] Fixes for ima selftest KP Singh
2020-12-03 19:14 ` [PATCH bpf-next v4 1/4] selftests/bpf: Update ima_setup.sh for busybox KP Singh
2020-12-03 19:14 ` [PATCH bpf-next v4 2/4] selftests/bpf: Ensure securityfs mount before writing ima policy KP Singh
@ 2020-12-03 19:14 ` KP Singh
2020-12-03 19:14 ` [PATCH bpf-next v4 4/4] selftests/bpf: Indent ima_setup.sh with tabs KP Singh
` (2 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: KP Singh @ 2020-12-03 19:14 UTC (permalink / raw)
To: bpf; +Cc: Andrii Nakryiko, Alexei Starovoitov, Daniel Borkmann
From: KP Singh <kpsingh@google.com>
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")
Reported-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: KP Singh <kpsingh@google.com>
---
tools/testing/selftests/bpf/config | 1 +
1 file changed, 1 insertion(+)
diff --git a/tools/testing/selftests/bpf/config b/tools/testing/selftests/bpf/config
index 365bf9771b07..37e1f303fc11 100644
--- a/tools/testing/selftests/bpf/config
+++ b/tools/testing/selftests/bpf/config
@@ -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
--
2.29.2.576.ga3fc446d84-goog
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH bpf-next v4 4/4] selftests/bpf: Indent ima_setup.sh with tabs.
2020-12-03 19:14 [PATCH bpf-next v4 0/4] Fixes for ima selftest KP Singh
` (2 preceding siblings ...)
2020-12-03 19:14 ` [PATCH bpf-next v4 3/4] selftests/bpf: Add config dependency on BLK_DEV_LOOP KP Singh
@ 2020-12-03 19:14 ` KP Singh
2020-12-03 19:26 ` [PATCH bpf-next v4 0/4] Fixes for ima selftest Andrii Nakryiko
2020-12-03 19:30 ` patchwork-bot+netdevbpf
5 siblings, 0 replies; 8+ messages in thread
From: KP Singh @ 2020-12-03 19:14 UTC (permalink / raw)
To: bpf; +Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
From: KP Singh <kpsingh@google.com>
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.
Signed-off-by: KP Singh <kpsingh@google.com>
---
tools/testing/selftests/bpf/ima_setup.sh | 108 +++++++++++------------
1 file changed, 54 insertions(+), 54 deletions(-)
diff --git a/tools/testing/selftests/bpf/ima_setup.sh b/tools/testing/selftests/bpf/ima_setup.sh
index b1ee4bf06996..2bfc646bc230 100755
--- a/tools/testing/selftests/bpf/ima_setup.sh
+++ b/tools/testing/selftests/bpf/ima_setup.sh
@@ -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 "$@"
--
2.29.2.576.ga3fc446d84-goog
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH bpf-next v4 0/4] Fixes for ima selftest
2020-12-03 19:14 [PATCH bpf-next v4 0/4] Fixes for ima selftest KP Singh
` (3 preceding siblings ...)
2020-12-03 19:14 ` [PATCH bpf-next v4 4/4] selftests/bpf: Indent ima_setup.sh with tabs KP Singh
@ 2020-12-03 19:26 ` Andrii Nakryiko
2020-12-04 0:53 ` KP Singh
2020-12-03 19:30 ` patchwork-bot+netdevbpf
5 siblings, 1 reply; 8+ messages in thread
From: Andrii Nakryiko @ 2020-12-03 19:26 UTC (permalink / raw)
To: KP Singh; +Cc: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
On Thu, Dec 3, 2020 at 11:14 AM KP Singh <kpsingh@chromium.org> wrote:
>
> From: KP Singh <kpsingh@google.com>
>
> # 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.
>
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.
> 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(-)
>
> --
> 2.29.2.576.ga3fc446d84-goog
>
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH bpf-next v4 0/4] Fixes for ima selftest
2020-12-03 19:26 ` [PATCH bpf-next v4 0/4] Fixes for ima selftest Andrii Nakryiko
@ 2020-12-04 0:53 ` KP Singh
0 siblings, 0 replies; 8+ messages in thread
From: KP Singh @ 2020-12-04 0:53 UTC (permalink / raw)
To: Andrii Nakryiko; +Cc: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
[...]
> 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.
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.
[...]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH bpf-next v4 0/4] Fixes for ima selftest
2020-12-03 19:14 [PATCH bpf-next v4 0/4] Fixes for ima selftest KP Singh
` (4 preceding siblings ...)
2020-12-03 19:26 ` [PATCH bpf-next v4 0/4] Fixes for ima selftest Andrii Nakryiko
@ 2020-12-03 19:30 ` patchwork-bot+netdevbpf
5 siblings, 0 replies; 8+ messages in thread
From: patchwork-bot+netdevbpf @ 2020-12-03 19:30 UTC (permalink / raw)
To: KP Singh; +Cc: bpf, ast, daniel, andrii
Hello:
This series was applied to bpf/bpf-next.git (refs/heads/master):
On Thu, 3 Dec 2020 19:14:33 +0000 you wrote:
> From: KP Singh <kpsingh@google.com>
>
> # 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
^ permalink raw reply [flat|nested] 8+ messages in thread