Debian Codegraph @

tests/test_tpm2_chroot_cuse

raw
1 #!/usr/bin/env bash
2
3 # For the license, see the LICENSE file in the root directory.
4
5 if [ "$(id -u)" -ne 0 ]; then
6 echo "Need to be root to run this test."
7 exit 77
8 fi
9
10 if [ "$(uname -s)" != "Linux" ]; then
11 # Due to using /proc/<pid>/root
12 echo "This test only runs only Linux."
13 exit 77
14 fi
15
16 if [ -z "$(type -P df)" ]; then
17 echo "This test requires the 'df' tool."
18 exit 77
19 fi
20
21 ROOT=${abs_top_builddir:-$(dirname "$0")/..}
22 TESTDIR=${abs_top_testdir:-$(dirname "$0")}
23
24 PID_FILE=/swtpm.pid
25 VTPM_NAME="vtpm-test-chroot"
26 SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
27
28 source "${TESTDIR}/common"
29 source "${TESTDIR}/test_common"
30 source "${TESTDIR}/test_cuse"
31
32 skip_test_no_tpm20 "${SWTPM_EXE}"
33
34 trap "cleanup" SIGTERM EXIT
35
36 function cleanup()
37 {
38 rm -rf "$TPMDIR"
39 if [ -n "$PID" ]; then
40 kill_quiet -SIGTERM "$PID" 2>/dev/null
41 fi
42 }
43
44 for OPTION in --chroot -R; do
45 TPMDIR="$(mktemp -d)" || exit 1
46
47 # CUSE TPM will only work if the filesystem does not have 'nodev' option
48 mnt=$(df "$TPMDIR" | tail -n 1 | gawk '{print $1" "$6}')
49 if [ -z "${mnt}" ]; then
50 echo " Error: Could not determine filesystem and mount point of $TPMDIR"
51 exit 1
52 fi
53 nodev="$(grep -E "^${mnt} " /proc/mounts |
54 gawk '{print ","$4","}' |
55 grep ",nodev,")"
56 if [ -n "${nodev}" ]; then
57 echo " Error: '${mnt}' is mounted with nodev option. Skipping test."
58 exit 77
59 fi
60
61 mkdir "$TPMDIR/dev"
62 mknod -m 0666 "$TPMDIR/dev/urandom" c 1 9
63 mknod -m 0666 "$TPMDIR/dev/cuse" c 10 203
64
65 $SWTPM_EXE cuse \
66 -n "$SWTPM_DEV_NAME" \
67 "$OPTION" "$TPMDIR" \
68 --tpmstate dir=/ \
69 --pid "file=$PID_FILE" \
70 --tpm2 \
71 --flags not-need-init \
72 ${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} &>/dev/null &
73
74 if wait_for_file "$TPMDIR/$PID_FILE" 3; then
75 echo "Error: CUSE TPM did not write pidfile."
76 exit 1
77 fi
78
79 PID=$(ps aux |
80 grep "cuse" |
81 grep " ${SWTPM_DEV_NAME}" |
82 grep -v grep |
83 gawk '{print $2}')
84
85 validate_pidfile "$PID" "$TPMDIR/$PID_FILE"
86
87 if [ "$(readlink "/proc/$PID/root")" != "$TPMDIR" ]; then
88 echo "Test 1 failed: Unexpected chroot dir"
89 exit 1
90 fi
91
92 if [ ! -f "${TPMDIR}/tpm2-00.permall" ]; then
93 echo "Missing state file"
94 exit 1
95 fi
96
97 echo "Test $OPTION passed"
98 cleanup
99 done