Debian Codegraph @

tests/_test_tpm2_init

raw symbols SCIP
1 #!/bin/bash
2
3 # For the license, see the LICENSE file in the root directory.
4 #set -x
5
6 ROOT=${abs_top_builddir:-$(pwd)/..}
7 TESTDIR=${abs_top_testdir:-$(dirname "$0")}
8
9 VTPM_NAME="vtpm-test-tpm2-init"
10 SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
11 TPM_PATH="$(mktemp -d)" || exit 1
12 STATE_FILE=$TPM_PATH/tpm2-00.permall
13 VOLATILE_STATE_FILE=$TPM_PATH/tpm2-00.volatilestate
14 SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
15 SWTPM_CMD_UNIX_PATH=${TPM_PATH}/unix-cmd.sock
16 SWTPM_CTRL_UNIX_PATH=${TPM_PATH}/unix-ctrl.sock
17
18 function cleanup()
19 {
20 pid=${SWTPM_PID}
21 if [ -n "$pid" ]; then
22 kill_quiet -9 "$pid"
23 fi
24 rm -rf "$TPM_PATH"
25 }
26
27 trap "cleanup" EXIT
28
29 source "${TESTDIR}/common"
30 [ "${SWTPM_INTERFACE}" == "cuse" ] && source "${TESTDIR}/test_cuse"
31
32 rm -f "$STATE_FILE" "$VOLATILE_STATE_FILE" 2>/dev/null
33
34 if has_seccomp_support "${SWTPM_EXE}"; then
35 SWTPM_TEST_SECCOMP_OPT="--seccomp action=none"
36 fi
37
38 TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" --tpm2
39
40 display_processes_by_name "swtpm"
41
42 if ! kill_quiet -0 "${SWTPM_PID}"; then
43 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
44 exit 1
45 fi
46
47 # Init the TPM
48 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
49 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
50 exit 1
51 fi
52
53 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
54 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
55 exit 1
56 fi
57
58 # Init the TPM again but make its state file inaccessible; this only
59 # works if the TPM runs as non-rootchmod 000 "${STATE_FILE}"
60 if [ "$(id -u)" != "0" ]; then
61 chmod 000 "${STATE_FILE}"
62 if run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
63 echo "Error: Unexpected initialization success of the ${SWTPM_INTERFACE} TPM."
64 exit 1
65 fi
66
67 sleep 0.5
68
69 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
70 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after failed INIT."
71 exit 1
72 fi
73 chmod 664 "${STATE_FILE}"
74
75 # Init the TPM again; now with state file accessible again
76 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
77 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
78 exit 1
79 fi
80
81 sleep 0.5
82
83 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
84 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
85 exit 1
86 fi
87 fi
88
89 if ! check_seccomp_profile "${SWTPM_EXE}" "${SWTPM_PID}" 0; then
90 exit 1
91 fi
92
93 # Shut down
94 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
95 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
96 exit 1
97 fi
98
99 if wait_process_gone "${SWTPM_PID}" 4; then
100 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
101 exit 1
102 fi
103
104 if [ ! -e "$STATE_FILE" ]; then
105 echo "Error: TPM state file $STATE_FILE does not exist."
106 exit 1
107 fi
108
109 echo "OK"
110
111 exit 0