Debian Codegraph @

tests/test_vtpm_proxy

raw symbols SCIP
1 #!/usr/bin/env bash
2
3 # For the license, see the LICENSE file in the root directory.
4 #set -x
5
6 if [ "$(id -u)" -ne 0 ]; then
7 echo "Need to be root to run this test."
8 exit 77
9 fi
10
11 ROOT=${abs_top_builddir:-$(dirname "$0")/..}
12 TESTDIR=${abs_top_testdir:-$(dirname "$0")}
13
14 TPM_PATH="$(mktemp -d)" || exit 1
15 STATE_FILE=$TPM_PATH/tpm-00.permall
16 VOLATILE_STATE_FILE=$TPM_PATH/tpm-00.volatilestate
17 PID_FILE=$TPM_PATH/swtpm.pid
18 SOCK_PATH=$TPM_PATH/sock
19 CMD_PATH=$TPM_PATH/cmd
20 RESP_PATH=$TPM_PATH/resp
21 LOGFILE=$TPM_PATH/logfile
22
23 function cleanup()
24 {
25 pid=$(ps aux | grep "swtpm" | grep -E " file=${PID_FILE}\$" | gawk '{print $2}')
26 if [ -n "$pid" ]; then
27 kill_quiet -9 "$pid"
28 fi
29 rm -rf "$TPM_PATH"
30 }
31
32 trap "cleanup" EXIT
33
34 source "${TESTDIR}/common"
35 skip_test_no_tpm12 "${SWTPM_EXE}"
36
37 source "${TESTDIR}/load_vtpm_proxy"
38
39 rm -f "$STATE_FILE" "$VOLATILE_STATE_FILE" 2>/dev/null
40
41 $SWTPM_EXE chardev --vtpm-proxy \
42 --tpmstate "dir=$TPM_PATH" \
43 --ctrl "type=unixio,path=$SOCK_PATH" \
44 ${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} \
45 --pid "file=$PID_FILE" &>"$LOGFILE" &
46 sleep 0.5
47 PID=$(ps aux | grep swtpm | grep -E " file=${PID_FILE}\$" | gawk '{print $2}')
48
49 display_processes_by_name "swtpm"
50
51 if ! kill_quiet -0 "$PID"; then
52 echo "Error: Chardev TPM did not start."
53 exit 1
54 fi
55
56 if wait_for_file "$PID_FILE" 3; then
57 echo "Error: Chardev TPM did not write pidfile."
58 exit 1
59 fi
60
61 # Wait for chardev to appear; TPM 1.2 may take a long time to self-test
62 # with valgrind
63 for ((i = 0; i < 200; i ++)); do
64 if [ -z "${TPM_DEVICE}" ]; then
65 TPM_DEVICE=$(sed -n 's,.*\(/dev/tpm[0-9]\+\).*,\1,p' "$LOGFILE")
66 if [ -n "${TPM_DEVICE}" ]; then
67 echo "Using ${TPM_DEVICE}."
68 fi
69 fi
70 if [ -n "${TPM_DEVICE}" ]; then
71 [ -c "${TPM_DEVICE}" ] && break
72 fi
73 sleep 0.1
74 done
75 if ! [ -c "${TPM_DEVICE}" ]; then
76 echo "Error: Chardev ${TPM_DEVICE} did not appear"
77 exit 1
78 fi
79
80 # Open access to the TPM
81 if ! exec 100<>"$TPM_DEVICE"; then
82 echo "Error: Could not open $TPM_DEVICE"
83 exit 1
84 fi
85
86 # Read PCR 17 -- this should give a fatal error response
87 echo -en '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11' >&100
88 #RES=$(cat <&100 | od -t x1 -A n -w128)
89 RES=$(od -t x1 -A n -w128 <&100)
90 exp=' 00 c4 00 00 00 1e 00 00 00 00 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff'
91 if [ "$RES" != "$exp" ]; then
92 echo "Error: Did not get expected result from TPM_PCRRead(17)"
93 echo "expected: $exp"
94 echo "received: $RES"
95 exit 1
96 fi
97
98 exec 100>&-
99
100 if ! kill_quiet -0 "$PID"; then
101 echo "Error: Chardev TPM must have crashed."
102 exit 1
103 fi
104
105 if [ ! -e "$STATE_FILE" ]; then
106 echo "Error: TPM state file $STATE_FILE does not exist."
107 exit 1
108 fi
109
110 # Send shutdown command to the TPM: CMD_SHUTDOWN = 00 00 00 03
111 echo -en '\x00\x00\x00\x03' > "$CMD_PATH"
112 socat -x -t10 "FILE:$CMD_PATH,rdonly" "UNIX-CONNECT:$SOCK_PATH" 2>&1 | \
113 sed -n '/^ /p' | \
114 tail -n1 > "$RESP_PATH"
115 res="$(cat "$RESP_PATH")"
116 exp=" 00 00 00 00"
117 if [ "$res" != "$exp" ]; then
118 echo "Error: Unexpected response from CMD_SHUTDOWN:"
119 echo " actual : $res"
120 echo " expected: $exp"
121 exit 1
122 fi
123
124 if wait_file_gone "$PID_FILE" 2; then
125 echo "Error: TPM should have removed PID file by now."
126 exit 1
127 fi
128
129 if wait_process_gone "${PID}" 4; then
130 echo "Error: TPM should not be running anymore."
131 exit 1
132 fi
133
134 echo "OK"
135
136 exit 0