|
3 | 3 |
|
4 | 4 | set -e |
5 | 5 | set -u |
| 6 | +set -o pipefail |
6 | 7 |
|
7 | 8 | IMA_POLICY_FILE="/sys/kernel/security/ima/policy" |
8 | 9 | TEST_BINARY="/bin/true" |
9 | 10 |
|
10 | 11 | usage() |
11 | 12 | { |
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 |
14 | 29 | } |
15 | 30 |
|
16 | 31 | setup() |
17 | 32 | { |
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 |
23 | 40 |
|
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) |
25 | 43 |
|
26 | | - local loop_device="$(losetup --find --show ${mount_img})" |
| 44 | + mkfs.ext2 "${loop_device:?}" |
| 45 | + mount "${loop_device}" "${mount_dir}" |
27 | 46 |
|
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/')" |
30 | 49 |
|
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} |
34 | 52 | } |
35 | 53 |
|
36 | 54 | 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) |
40 | 60 |
|
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 |
45 | 64 |
|
46 | | - umount ${mount_dir} |
47 | | - rm -rf ${tmp_dir} |
| 65 | + umount ${mount_dir} |
| 66 | + rm -rf ${tmp_dir} |
48 | 67 | } |
49 | 68 |
|
50 | 69 | run() |
51 | 70 | { |
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})" |
55 | 74 |
|
56 | | - exec "${copied_bin_path}" |
| 75 | + exec "${copied_bin_path}" |
57 | 76 | } |
58 | 77 |
|
59 | 78 | main() |
60 | 79 | { |
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 |
78 | 97 | } |
79 | 98 |
|
80 | 99 | main "$@" |
0 commit comments