Debian Codegraph @

tests/_test_tpm2_init

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2
3 # For the license, see the LICENSE file in the root directory.
4 #set -x
5
6 ROOT=${abs_top_builddir:-$(pwdscip-shell system . . pwd.)/..}
7 TESTDIR=${abs_top_testdir:-$(dirnamescip-shell system . . dirname. "$0")}
8
9 VTPM_NAME="vtpm-test-tpm2-init"
10 SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
11 TPM_PATH="$(mktempscip-shell system . . 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 cleanupscip-shell shell swtpm 0.10.1+ds-1 cleanup().()
19 {
20 pid=${SWTPM_PID}
21 if [scip-shell system . . `[`. -n "$pid" ]; then
22 kill_quiet -9 "$pid"
23 fi
24 rmscip-shell system . . rm. -rf "$TPM_PATH"
25 }
26
27 trap "cleanup" EXIT
28
29 source "${TESTDIR}/common"
30 [scip-shell system . . `[`. "${SWTPM_INTERFACE}" == "cuse" ] && source "${TESTDIR}/test_cuse"
31
32 rmscip-shell system . . rm. -f "$STATE_FILE" "$VOLATILE_STATE_FILE" 2>/dev/nullscip-shell filesystem . . `/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 echoscip-shell system . . 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 echoscip-shell system . . echo. "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
50 exit 1
51 fi
52
53 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/nullscip-shell filesystem . . `/dev/null`.; then
54 echoscip-shell system . . 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 [scip-shell system . . `[`. "$(idscip-shell system . . id. -u)" != "0" ]; then
61 chmodscip-shell system . . chmod. 000 "${STATE_FILE}"
62 if run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
63 echoscip-shell system . . echo. "Error: Unexpected initialization success of the ${SWTPM_INTERFACE} TPM."
64 exit 1
65 fi
66
67 sleepscip-shell system . . sleep. 0.5
68
69 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/nullscip-shell filesystem . . `/dev/null`.; then
70 echoscip-shell system . . echo. "Error: ${SWTPM_INTERFACE} TPM not running anymore after failed INIT."
71 exit 1
72 fi
73 chmodscip-shell system . . 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 echoscip-shell system . . echo. "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
78 exit 1
79 fi
80
81 sleepscip-shell system . . sleep. 0.5
82
83 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/nullscip-shell filesystem . . `/dev/null`.; then
84 echoscip-shell system . . 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 echoscip-shell system . . 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 echoscip-shell system . . echo. "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
101 exit 1
102 fi
103
104 if [scip-shell system . . `[`. ! -e "$STATE_FILE" ]; then
105 echoscip-shell system . . echo. "Error: TPM state file $STATE_FILE does not exist."
106 exit 1
107 fi
108
109 echoscip-shell system . . echo. "OK"
110
111 exit 0