| 1 | #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash |
| 2 | |
| 3 | # For the license, see the LICENSE file in the root directory. |
| 4 | #set -x |
| 5 | |
| 6 | if [scip-shell system . . `[`. "$(idscip-shell system . . id. -u)" -ne 0 ]; then |
| 7 | echoscip-shell system . . echo. "Need to be root to run this test." |
| 8 | exit 77 |
| 9 | fi |
| 10 | |
| 11 | ROOT=${abs_top_builddir:-$(dirnamescip-shell system . . dirname. "$0")/..} |
| 12 | TESTDIR=${abs_top_testdir:-$(dirnamescip-shell system . . dirname. "$0")} |
| 13 | |
| 14 | TPM_PATH="$(mktempscip-shell system . . 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 cleanupscip-shell shell swtpm 0.10.1+ds-1 cleanup().() |
| 24 | { |
| 25 | pid=$(psscip-shell system . . ps. aux | grepscip-shell system . . grep. "swtpm" | grepscip-shell system . . grep. -E " file=${PID_FILE}\$" | gawkscip-shell system . . gawk. '{print $2}') |
| 26 | if [scip-shell system . . `[`. -n "$pid" ]; then |
| 27 | kill_quiet -9 "$pid" |
| 28 | fi |
| 29 | rmscip-shell system . . 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 | rmscip-shell system . . rm. -f "$STATE_FILE" "$VOLATILE_STATE_FILE" 2>/dev/nullscip-shell filesystem . . `/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 | sleepscip-shell system . . sleep. 0.5 |
| 47 | PID=$(psscip-shell system . . ps. aux | grepscip-shell system . . grep. swtpm | grepscip-shell system . . grep. -E " file=${PID_FILE}\$" | gawkscip-shell system . . gawk. '{print $2}') |
| 48 | |
| 49 | display_processes_by_name "swtpm" |
| 50 | |
| 51 | if ! kill_quiet -0 "$PID"; then |
| 52 | echoscip-shell system . . echo. "Error: Chardev TPM did not start." |
| 53 | exit 1 |
| 54 | fi |
| 55 | |
| 56 | if wait_for_file "$PID_FILE" 3; then |
| 57 | echoscip-shell system . . 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 ! [scip-shell system . . `[`. -c "${TPM_DEVICE}" ]; then |
| 76 | echoscip-shell system . . 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 | echoscip-shell system . . 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 | echoscip-shell system . . 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=$(odscip-shell system . . 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 [scip-shell system . . `[`. "$RES" != "$exp" ]; then |
| 92 | echoscip-shell system . . echo. "Error: Did not get expected result from TPM_PCRRead(17)" |
| 93 | echoscip-shell system . . echo. "expected: $exp" |
| 94 | echoscip-shell system . . echo. "received: $RES" |
| 95 | exit 1 |
| 96 | fi |
| 97 | |
| 98 | exec 100>&- |
| 99 | |
| 100 | if ! kill_quiet -0 "$PID"; then |
| 101 | echoscip-shell system . . echo. "Error: Chardev TPM must have crashed." |
| 102 | exit 1 |
| 103 | fi |
| 104 | |
| 105 | if [scip-shell system . . `[`. ! -e "$STATE_FILE" ]; then |
| 106 | echoscip-shell system . . 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 | echoscip-shell system . . 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 '/^ /pscip-shell filesystem . . `/^ /p`.' | \ |
| 114 | tail -n1 > "$RESP_PATH" |
| 115 | res="$(catscip-shell system . . cat. "$RESP_PATH")" |
| 116 | exp=" 00 00 00 00" |
| 117 | if [scip-shell system . . `[`. "$res" != "$exp" ]; then |
| 118 | echoscip-shell system . . echo. "Error: Unexpected response from CMD_SHUTDOWN:" |
| 119 | echoscip-shell system . . echo. " actual : $res" |
| 120 | echoscip-shell system . . echo. " expected: $exp" |
| 121 | exit 1 |
| 122 | fi |
| 123 | |
| 124 | if wait_file_gone "$PID_FILE" 2; then |
| 125 | echoscip-shell system . . 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 | echoscip-shell system . . echo. "Error: TPM should not be running anymore." |
| 131 | exit 1 |
| 132 | fi |
| 133 | |
| 134 | echoscip-shell system . . echo. "OK" |
| 135 | |
| 136 | exit 0 |