Debian Codegraph @

tests/_test_tpm2_file_permissions

raw
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-file-permissions"
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 PIDFILE=${TPM_PATH}/swtpm.pid
15 LOGFILE=${TPM_PATH}/swtpm.log
16 PWDFILE=${TPM_PATH}/pwdfile.txt
17 SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
18
19 function cleanup()
20 {
21 if [ -n "${SWTPM_PID}" ]; then
22 kill_quiet -9 "${SWTPM_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 SWTPM_SETUP_CONF=${TPM_PATH}/swtpm_setup.conf
32
33 cat <<_EOF_ > "${SWTPM_SETUP_CONF}"
34 create_certs_tool=unused
35 create_certs_tool_config=/dev/null
36 create_certs_tool_options=/dev/null
37 _EOF_
38
39 # We need to copy swtpm and libswtpm_libtpms.so to the workdir
40 # so that swtpm_setup can access it as TESTUSER
41 if ! file "${SWTPM_EXE}" | grep -q ELF; then
42 directory="$(dirname "${SWTPM_EXE}")/.libs"
43 if [ -d "${directory}" ]; then
44 cp "${directory}/swtpm" "${TPM_PATH}"
45 cp "${directory}"/libswtpm_libtpms.so* "${TPM_PATH}"
46 else
47 echo "Could not find .libs directory to copy swtpm from."
48 exit 77
49 fi
50 MY_SWTPM_EXE="${TPM_PATH}/swtpm"
51 SWTPM_LD_LIBRARY_PATH="${TPM_PATH}"
52 else
53 MY_SWTPM_EXE="${SWTPM_EXE}"
54 SWTPM_LD_LIBRARY_PATH=""
55 fi
56
57 # Minimum permissions on directory
58 chmod 0700 "${TPM_PATH}"
59 chown "${TESTUSER}:${TESTGROUP}" "${TPM_PATH}"
60 # empty pid file that swtpm must be able to overwrite
61 touch "${PIDFILE}"
62 # A logfile that swtpm must be able to write to
63 echo "TestTest" > "${LOGFILE}"
64 # minimum permission on state files, pidfile, password file, and logfile
65 cp "${TESTDIR}"/data/tpm2state2/* "${TPM_PATH}"
66 chmod 0600 "${TPM_PATH}"/tpm2-00* "${PIDFILE}" "${PWDFILE}" "${LOGFILE}" "${SWTPM_SETUP_CONF}"
67 chown "${TESTUSER}:${TESTGROUP}" "${TPM_PATH}"/*
68
69 # Test-execute the swtpm program as $TESTUSER
70 if ! tmp=$(su -m "${TESTUSER}" -c "LD_LIBRARY_PATH=\"${SWTPM_LD_LIBRARY_PATH}\" \"${MY_SWTPM_EXE}\" --help 2>&1"); then
71 echo "Could not run '${MY_SWTPM_EXE}' as ${TESTUSER}. Skipping swtpm_setup tests."
72 echo "Error: ${tmp}"
73 exit 77
74 fi
75
76
77 logsize=$(get_filesize "${LOGFILE}")
78
79 TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \
80 --pid "file=${PIDFILE}" \
81 --log "file=${LOGFILE},level=20" \
82 --runas "${TESTUSER}" \
83 --tpm2 \
84 --key "pwdfile=${PWDFILE},kdf=sha512"
85
86 if ! kill -0 "${SWTPM_PID}"; then
87 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
88 exit 1
89 fi
90
91 # Init the TPM
92 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
93 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
94 exit 1
95 fi
96
97 # Volatile state file must be gone now
98 if [ -f "${VOLATILE_STATE_FILE}" ]; then
99 echo "Error: Volatile state file has not been removed."
100 exit 1
101 fi
102
103 # There should be a log file now owned by the ${TESTUSER}
104 # Since the CUSE TPM must be started as root root ownership is allowed for log and pid file
105 if [ "${SWTPM_INTERFACE}" != "cuse" ]; then
106 fileowner="$(get_fileowner_names "${LOGFILE}")"
107 if [ "${fileowner}" != "${TESTUSER} ${TESTGROUP}" ]; then
108 echo "File ownership for logfile is wrong."
109 echo "Expected: ${TESTUSER} ${TESTGROUP}"
110 echo "Actual : ${fileowner}"
111 fi
112
113 fileowner="$(get_fileowner_names "${PIDFILE}")"
114 if [ "${fileowner}" != "${TESTUSER} ${TESTGROUP}" ]; then
115 echo "File ownership for pidfile is wrong."
116 echo "Expected: ${TESTUSER} ${TESTGROUP}"
117 echo "Actual : ${fileowner}"
118 fi
119 fi
120
121 # The log file must have grown
122 if ! grep -q "TestTest" "${LOGFILE}"; then
123 echo "Error: First line 'TestTest' missing in logfile."
124 exit 1
125 fi
126 if [ "$(get_filesize "${LOGFILE}")" -le "${logsize}" ]; then
127 echo "Error: Log file did not grow!"
128 exit 1
129 fi
130
131 # The PID file must contain the PID
132 if [ "$(cat "${PIDFILE}")" != "${SWTPM_PID}" ]; then
133 echo "Error: PID file does not have the PID!"
134 echo "Expected: ${SWTPM_PID}"
135 echo "Actual : $(cat "${PIDFILE}")"
136 exit 1
137 fi
138
139 # Read PCR 10 (from pcrextend -ha 10 -ic test)
140 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x04\x00')
141 exp=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 16 00 00 00 01 00 0b 03 00 04 00 00 00 00 01 00 20 f6 85 98 e5 86 8d e6 8b 97 29 99 60 f2 71 7d 17 67 89 a4 2f 9a ae a8 c7 b7 aa 79 a8 62 56 c1 de'
142 if [ "$RES" != "$exp" ]; then
143 echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
144 echo "expected: $exp"
145 echo "received: $RES"
146 exit 1
147 fi
148
149 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
150 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
151 exit 1
152 fi
153
154 if wait_process_gone "${SWTPM_PID}" 4; then
155 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
156 exit 1
157 fi
158
159 if [ -f "${PIDFILE}" ]; then
160 echo "Error: PID file should have been removed."
161 ls -l "${TPM_PATH}"
162 exit 1
163 fi
164
165 echo "Test 1: OK"
166
167 logsize=$(get_filesize "${LOGFILE}")
168 statefilehash=$(get_sha1_file "${STATE_FILE}")
169
170 if LD_LIBRARY_PATH="${SWTPM_LD_LIBRARY_PATH}" ${SWTPM_SETUP} \
171 --tpm2 \
172 --tpmstate "${TPM_PATH}" \
173 --logfile "${LOGFILE}" \
174 --runas "${TESTUSER}" \
175 --config "${SWTPM_SETUP_CONF}" \
176 --tpm "${MY_SWTPM_EXE} socket ${SWTPM_TEST_SECCOMP_OPT}"; then
177 echo "Error: ${SWTPM_SETUP} should have refused to overwrite existing state."
178 exit 1
179 fi
180
181 if ! LD_LIBRARY_PATH="${SWTPM_LD_LIBRARY_PATH}" ${SWTPM_SETUP} \
182 --tpm2 \
183 --tpmstate "${TPM_PATH}" \
184 --overwrite \
185 --logfile "${LOGFILE}" \
186 --runas "${TESTUSER}" \
187 --config "${SWTPM_SETUP_CONF}" \
188 --tpm "${MY_SWTPM_EXE} socket ${SWTPM_TEST_SECCOMP_OPT}"; then
189 echo "Error: ${SWTPM_SETUP} could not overwrite existing state. (1)"
190 cat "${LOGFILE}"
191 exit 1
192 fi
193 if [ ! -f "${STATE_FILE}" ]; then
194 echo "Error: TPM 2 state file doesn't exist."
195 exit 1
196 fi
197 if [ "$(get_sha1_file "${STATE_FILE}")" = "${statefilehash}" ]; then
198 echo "Error: State file was not changed."
199 exit 1
200 fi
201 if ! grep -q "TestTest" "${LOGFILE}"; then
202 echo "Error: First line 'TestTest' missing in logfile."
203 exit 1
204 fi
205 if [ "$(get_filesize "${LOGFILE}")" -le "${logsize}" ]; then
206 echo "Error: ${SWTPM_SETUP} did not append to existing log."
207 exit 1
208 fi
209
210 echo "Test 2: OK"
211
212 # Make sure it can access the pwdfile also
213 statefilehash=$(get_sha1_file "${STATE_FILE}")
214 logsize=$(get_filesize "${LOGFILE}")
215
216 if ! LD_LIBRARY_PATH="${SWTPM_LD_LIBRARY_PATH}" ${SWTPM_SETUP} \
217 --tpmstate "${TPM_PATH}" \
218 --overwrite \
219 --tpm2 \
220 --logfile "${LOGFILE}" \
221 --runas "${TESTUSER}" \
222 --config "${SWTPM_SETUP_CONF}" \
223 --tpm "${MY_SWTPM_EXE} socket ${SWTPM_TEST_SECCOMP_OPT}" \
224 --pwdfile "${PWDFILE}"; then
225 echo "Error: ${SWTPM_SETUP} could not overwrite existing state."
226 exit 1
227 fi
228 if [ ! -f "${STATE_FILE}" ]; then
229 echo "Error: TPM 2 state file '${STATE_FILE}' doesn't exist."
230 exit 1
231 fi
232 if [ "$(get_sha1_file "${STATE_FILE}")" = "${statefilehash}" ]; then
233 echo "Error: State file was not changed."
234 exit 1
235 fi
236 if [ "$(get_filesize "${LOGFILE}")" -le "${logsize}" ]; then
237 echo "Error: ${SWTPM_SETUP} did not append to existing log."
238 exit 1
239 fi
240
241 echo "Test 3: OK"
242
243 exit 0