ima_setup.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. set -e
  4. set -u
  5. set -o pipefail
  6. IMA_POLICY_FILE="/sys/kernel/security/ima/policy"
  7. TEST_BINARY="/bin/true"
  8. VERBOSE="${SELFTESTS_VERBOSE:=0}"
  9. LOG_FILE="$(mktemp /tmp/ima_setup.XXXX.log)"
  10. usage()
  11. {
  12. echo "Usage: $0 <setup|cleanup|run|modify-bin|restore-bin|load-policy> <existing_tmp_dir>"
  13. exit 1
  14. }
  15. ensure_mount_securityfs()
  16. {
  17. local securityfs_dir=$(grep "securityfs" /proc/mounts | awk '{print $2}')
  18. if [ -z "${securityfs_dir}" ]; then
  19. securityfs_dir=/sys/kernel/security
  20. mount -t securityfs security "${securityfs_dir}"
  21. fi
  22. if [ ! -d "${securityfs_dir}" ]; then
  23. echo "${securityfs_dir}: securityfs is not mounted" && exit 1
  24. fi
  25. }
  26. setup()
  27. {
  28. local tmp_dir="$1"
  29. local mount_img="${tmp_dir}/test.img"
  30. local mount_dir="${tmp_dir}/mnt"
  31. local copied_bin_path="${mount_dir}/$(basename ${TEST_BINARY})"
  32. mkdir -p ${mount_dir}
  33. dd if=/dev/zero of="${mount_img}" bs=1M count=10
  34. losetup -f "${mount_img}"
  35. local loop_device=$(losetup -a | grep ${mount_img:?} | cut -d ":" -f1)
  36. mkfs.ext2 "${loop_device:?}"
  37. mount "${loop_device}" "${mount_dir}"
  38. cp "${TEST_BINARY}" "${mount_dir}"
  39. local mount_uuid="$(blkid ${loop_device} | sed 's/.*UUID="\([^"]*\)".*/\1/')"
  40. ensure_mount_securityfs
  41. echo "measure func=BPRM_CHECK fsuuid=${mount_uuid}" > ${IMA_POLICY_FILE}
  42. echo "measure func=BPRM_CHECK fsuuid=${mount_uuid}" > ${mount_dir}/policy_test
  43. }
  44. cleanup() {
  45. local tmp_dir="$1"
  46. local mount_img="${tmp_dir}/test.img"
  47. local mount_dir="${tmp_dir}/mnt"
  48. local loop_devices=$(losetup -a | grep ${mount_img:?} | cut -d ":" -f1)
  49. for loop_dev in "${loop_devices}"; do
  50. losetup -d $loop_dev
  51. done
  52. umount ${mount_dir}
  53. rm -rf ${tmp_dir}
  54. }
  55. run()
  56. {
  57. local tmp_dir="$1"
  58. local mount_dir="${tmp_dir}/mnt"
  59. local copied_bin_path="${mount_dir}/$(basename ${TEST_BINARY})"
  60. exec "${copied_bin_path}"
  61. }
  62. modify_bin()
  63. {
  64. local tmp_dir="$1"
  65. local mount_dir="${tmp_dir}/mnt"
  66. local copied_bin_path="${mount_dir}/$(basename ${TEST_BINARY})"
  67. echo "mod" >> "${copied_bin_path}"
  68. }
  69. restore_bin()
  70. {
  71. local tmp_dir="$1"
  72. local mount_dir="${tmp_dir}/mnt"
  73. local copied_bin_path="${mount_dir}/$(basename ${TEST_BINARY})"
  74. truncate -s -4 "${copied_bin_path}"
  75. }
  76. load_policy()
  77. {
  78. local tmp_dir="$1"
  79. local mount_dir="${tmp_dir}/mnt"
  80. echo ${mount_dir}/policy_test > ${IMA_POLICY_FILE} 2> /dev/null
  81. }
  82. catch()
  83. {
  84. local exit_code="$1"
  85. local log_file="$2"
  86. if [[ "${exit_code}" -ne 0 ]]; then
  87. cat "${log_file}" >&3
  88. fi
  89. rm -f "${log_file}"
  90. exit ${exit_code}
  91. }
  92. main()
  93. {
  94. [[ $# -ne 2 ]] && usage
  95. local action="$1"
  96. local tmp_dir="$2"
  97. [[ ! -d "${tmp_dir}" ]] && echo "Directory ${tmp_dir} doesn't exist" && exit 1
  98. if [[ "${action}" == "setup" ]]; then
  99. setup "${tmp_dir}"
  100. elif [[ "${action}" == "cleanup" ]]; then
  101. cleanup "${tmp_dir}"
  102. elif [[ "${action}" == "run" ]]; then
  103. run "${tmp_dir}"
  104. elif [[ "${action}" == "modify-bin" ]]; then
  105. modify_bin "${tmp_dir}"
  106. elif [[ "${action}" == "restore-bin" ]]; then
  107. restore_bin "${tmp_dir}"
  108. elif [[ "${action}" == "load-policy" ]]; then
  109. load_policy "${tmp_dir}"
  110. else
  111. echo "Unknown action: ${action}"
  112. exit 1
  113. fi
  114. }
  115. trap 'catch "$?" "${LOG_FILE}"' EXIT
  116. if [[ "${VERBOSE}" -eq 0 ]]; then
  117. # Save the stderr to 3 so that we can output back to
  118. # it incase of an error.
  119. exec 3>&2 1>"${LOG_FILE}" 2>&1
  120. fi
  121. main "$@"
  122. rm -f "${LOG_FILE}"