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