| 1 | #!/bin/bash |
| 2 | |
| 3 | # Encrypted and trusted keyring test; trusted keys can be emulated by TPM2. |
| 4 | # DO NOT use this in combination with a real TPM data, it can destroy them. |
| 5 | # |
| 6 | # Note that if trusted or tpm kernel module is built-in, emulated TPM2 cannot |
| 7 | # provide trusted keys (only encrypted keys will be tested then). |
| 8 | |
| 9 | [ -z "$CRYPTSETUP_PATH" ] && CRYPTSETUP_PATH=".." |
| 10 | CRYPTSETUP=$CRYPTSETUP_PATH/cryptsetup |
| 11 | |
| 12 | IMG=keyring_trusted_test.img |
| 13 | MAP="trusted_test" |
| 14 | KEYNAME=testxxx |
| 15 | |
| 16 | bin_check() |
| 17 | { |
| 18 | command -v $1 >/dev/null || skip "WARNING: test require $1 binary, test skipped." |
| 19 | } |
| 20 | |
| 21 | cleanup() { |
| 22 | clear_keys |
| 23 | |
| 24 | [ -S $SWTPM_STATE_DIR/ctrl.sock ] && { |
| 25 | # shutdown TPM via control socket |
| 26 | swtpm_ioctl -s --unix $SWTPM_STATE_DIR/ctrl.sock |
| 27 | sleep 1 |
| 28 | } |
| 29 | |
| 30 | # if graceful shutdown was successful, pidfile should be deleted |
| 31 | # if it is still present, we forcefully kill the process |
| 32 | [ -f "$SWTPM_PIDFILE" ] && { |
| 33 | kill -9 $(cat $SWTPM_PIDFILE) >/dev/null 2>&1 |
| 34 | } |
| 35 | |
| 36 | [ -b /dev/mapper/$MAP ] && dmsetup remove --retry $MAP |
| 37 | |
| 38 | [ -n "$SWTPM_PIDFILE" ] && rm -f $SWTPM_PIDFILE >/dev/null 2>&1 |
| 39 | [ -n "$SWTPM_STATE_DIR" ] && rm -rf $SWTPM_STATE_DIR >/dev/null 2>&1 |
| 40 | rm -f $IMG >/dev/null 2>&1 |
| 41 | |
| 42 | cleanup_modules |
| 43 | } |
| 44 | |
| 45 | cleanup_modules() |
| 46 | { |
| 47 | [ -n "$SKIP_MODULE_UNLOAD" ] && return |
| 48 | |
| 49 | # This is a hack to avoid linking trusted key to another TPM. |
| 50 | # Without clean load tests do not work reliably. |
| 51 | modprobe -r dm-crypt 2>/dev/null |
| 52 | modprobe -r encrypted-keys 2>/dev/null |
| 53 | modprobe -r trusted 2>/dev/null |
| 54 | modprobe -r tpm_vtpm_proxy 2>/dev/null |
| 55 | modprobe -r tpm 2>/dev/null |
| 56 | } |
| 57 | |
| 58 | fail() |
| 59 | { |
| 60 | echo "[FAILED]" |
| 61 | [ -n "$1" ] && echo "$1" |
| 62 | echo "FAILED backtrace:" |
| 63 | while caller $frame; do ((frame++)); done |
| 64 | cleanup |
| 65 | exit 2 |
| 66 | } |
| 67 | |
| 68 | _sigchld() { local c=$?; [ $c -eq 139 ] && fail "Segfault"; [ $c -eq 134 ] && fail "Aborted"; } |
| 69 | trap _sigchld CHLD |
| 70 | |
| 71 | skip() |
| 72 | { |
| 73 | [ -n "$1" ] && echo "$1" |
| 74 | cleanup |
| 75 | exit 77 |
| 76 | } |
| 77 | |
| 78 | prepare_tpm() |
| 79 | { |
| 80 | SWTPM_STATE_DIR=$(mktemp -d /tmp/systemd_swtpm_state.XXXXXX) |
| 81 | SWTPM_SEAL_KEY="$SWTPM_STATE_DIR/sealkey.ctxt" |
| 82 | SWTPM_PERSISTENT_HANDLE=0x81000001 |
| 83 | |
| 84 | if [ -z "$TPM_PATH" ]; then |
| 85 | bin_check swtpm |
| 86 | bin_check swtpm_ioctl |
| 87 | |
| 88 | SWTPM_PIDFILE=$(mktemp /tmp/systemd_swtpm_pid.XXXXXX) |
| 89 | |
| 90 | cleanup_modules |
| 91 | modprobe tpm_vtpm_proxy || fail "Failed to load tpm_vtpm_proxy kernel module, required for emulated TPM." |
| 92 | |
| 93 | SWTPM_LOG=$(swtpm chardev --vtpm-proxy --tpm2 --tpmstate dir=$SWTPM_STATE_DIR -d --pid file=$SWTPM_PIDFILE --ctrl type=unixio,path=$SWTPM_STATE_DIR/ctrl.sock) |
| 94 | TPM_PATH=$(echo $SWTPM_LOG | grep -Eo /dev/tpm\([0-9]\)+ | sed s/tpm/tpmrm/) |
| 95 | |
| 96 | [ -z "$TPM_PATH" ] && fail "No TPM_PATH set and swtpm failed, test skipped." |
| 97 | |
| 98 | echo "Virtual TPM set up at $TPM_PATH" |
| 99 | |
| 100 | # Trusted module needs to see TPM above. |
| 101 | sleep 1 |
| 102 | modprobe trusted 2>/dev/null || echo "Failed to load trusted keys kernel module." |
| 103 | else |
| 104 | SKIP_MODULE_UNLOAD=1 |
| 105 | echo "Preconfigured TPM set up at $TPM_PATH" |
| 106 | fi |
| 107 | # Create persistent store |
| 108 | tpm2_createprimary -Q -T device:$TPM_PATH --hierarchy o -G rsa2048 -c $SWTPM_SEAL_KEY || fail |
| 109 | tpm2_evictcontrol -Q -T device:$TPM_PATH -c $SWTPM_SEAL_KEY $SWTPM_PERSISTENT_HANDLE || fail |
| 110 | } |
| 111 | |
| 112 | prepare_vk_keyring() |
| 113 | { |
| 114 | local s_desc=$(keyctl rdescribe @s | cut -d';' -f5) |
| 115 | local us_desc=$(keyctl rdescribe @us | cut -d';' -f5) |
| 116 | |
| 117 | if [ "$s_desc" = "$us_desc" -a -n "$s_desc" ]; then |
| 118 | echo "Session keyring is missing, initializing new one." |
| 119 | keyctl new_session > /dev/null || fail |
| 120 | fi |
| 121 | } |
| 122 | |
| 123 | clear_keys() |
| 124 | { |
| 125 | keyctl revoke %encrypted:$KEYNAME 2>/dev/null |
| 126 | keyctl revoke %user:$KEYNAME 2>/dev/null |
| 127 | |
| 128 | if [ -n "$TRUSTED_SUPPORTED" ]; then |
| 129 | keyctl revoke %trusted:$KEYNAME 2>/dev/null |
| 130 | tpm2_evictcontrol -Q -T device:$TPM_PATH -C o -c $SWTPM_PERSISTENT_HANDLE 2>/dev/null |
| 131 | fi |
| 132 | } |
| 133 | |
| 134 | [ $(id -u) != 0 ] && skip "WARNING: You must be root to run this test, test skipped." |
| 135 | bin_check keyctl |
| 136 | |
| 137 | prepare_vk_keyring |
| 138 | |
| 139 | # Prevent using TPM by default |
| 140 | if [ -n "$RUN_KEYRING_TRUSTED_TEST" ]; then |
| 141 | prepare_tpm |
| 142 | # Trusted keyring key |
| 143 | keyctl add trusted $KEYNAME "new 32 keyhandle=$SWTPM_PERSISTENT_HANDLE" @s >/dev/null 2>&1 && TRUSTED_SUPPORTED=1 |
| 144 | if [ -n "$TRUSTED_SUPPORTED" ]; then |
| 145 | keyctl print %trusted:$KEYNAME >/dev/null || fail "Cannot read trusted key blob." |
| 146 | fi |
| 147 | else |
| 148 | echo "WARNING: Variable RUN_KEYRING_TRUSTED_TEST must be defined, TPM trusted test skipped." |
| 149 | TRUSTED_SUPPORTED= |
| 150 | fi |
| 151 | |
| 152 | # Prepare encrypted key |
| 153 | modprobe encrypted-keys 2>/dev/null || skip "Failed to load encrypted keys kernel module." |
| 154 | |
| 155 | # Encrypted keyring key (encrypted with user key) |
| 156 | # User key needed for encrypted key |
| 157 | keyctl add -x user $KEYNAME bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a @s >/dev/null 2>&1 || skip "User key load failed, test skipped." |
| 158 | keyctl print %user:$KEYNAME >/dev/null || fail "Cannot read user key." |
| 159 | |
| 160 | keyctl add encrypted $KEYNAME "new user:$KEYNAME 32" @s >/dev/null || fail "Encrypted keys are not supported." |
| 161 | keyctl print %encrypted:$KEYNAME >/dev/null || fail "Cannot read encrypted key blob." |
| 162 | |
| 163 | dd if=/dev/zero of=$IMG bs=1M count=32 >/dev/null 2>&1 |
| 164 | |
| 165 | echo -n "Plain with trusted volume key " |
| 166 | if [ -n "$TRUSTED_SUPPORTED" ]; then |
| 167 | $CRYPTSETUP open --type plain $IMG $MAP --key-size=256 --cipher=aes-xts-plain64 --volume-key-keyring=%trusted:$KEYNAME || fail |
| 168 | dmsetup table --showkeys $MAP | grep -q ":32:trusted:$KEYNAME" || fail |
| 169 | $CRYPTSETUP status $MAP | grep -q "key location: keyring" || fail |
| 170 | $CRYPTSETUP close $MAP || fail |
| 171 | echo "[OK]" |
| 172 | else |
| 173 | echo "[N/A]" |
| 174 | fi |
| 175 | |
| 176 | echo -n "Plain with encrypted volume key " |
| 177 | $CRYPTSETUP open --type plain $IMG $MAP --key-size=256 --cipher=aes-xts-plain64 --volume-key-keyring=%encrypted:$KEYNAME ||fail |
| 178 | dmsetup table --showkeys $MAP | grep -q ":32:encrypted:$KEYNAME" || fail |
| 179 | $CRYPTSETUP status $MAP | grep -q "key location: keyring" || fail |
| 180 | $CRYPTSETUP resize $MAP --size 100 || fail |
| 181 | $CRYPTSETUP status $MAP | grep "size:" | grep -q "100 \[512-byte units\]" || fail |
| 182 | $CRYPTSETUP resize $MAP || fail |
| 183 | $CRYPTSETUP close $MAP || fail |
| 184 | echo "[OK]" |
| 185 | |
| 186 | cleanup |
| 187 | exit 0 |