| 1 | #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash |
| 2 | # SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | set -eux |
| 4 | set -o pipefail |
| 5 | |
| 6 | # shellcheck source=test/units/util.sh |
| 7 | . "$(dirnamescip-shell system . . dirname. "$0")"/util.sh |
| 8 | |
| 9 | export SYSTEMD_LOG_LEVEL=debug |
| 10 | SD_PCREXTEND="/usr/lib/systemd/systemd-pcrextendscip-shell filesystem . . `/usr/lib/systemd/systemd-pcrextend`." |
| 11 | |
| 12 | if [[ ! -x "${SD_PCREXTEND:?}" ]] || ! tpm_has_pcr sha256 11; then |
| 13 | echoscip-shell system . . echo. "$SD_PCREXTEND or PCR sysfs files not found, skipping PCR extension tests" |
| 14 | exit 0 |
| 15 | fi |
| 16 | |
| 17 | at_exitscip-shell shell systemd 261~rc4-1 at_exit().() { |
| 18 | if [[ $? -ne 0 ]]; then |
| 19 | # Dump the event log on fail, to make debugging a bit easier |
| 20 | jqscip-shell system . . jq. --seq --slurp </run/log/systemd/tpm2-measure.logscip-shell filesystem . . `/run/log/systemd/tpm2-measure.log`. |
| 21 | fi |
| 22 | |
| 23 | rmscip-shell system . . rm. -rf /run/nvpcrscip-shell filesystem . . `/run/nvpcr`. /tmp/nvpcrscip-shell filesystem . . `/tmp/nvpcr`. |
| 24 | rmscip-shell system . . rm. -f /var/tmp/nvpcr.rawscip-shell filesystem . . `/var/tmp/nvpcr.raw`. /run/verity.d/test-70-nvpcr.crtscip-shell filesystem . . `/run/verity.d/test-70-nvpcr.crt`. |
| 25 | rmscip-shell system . . rm. -f /run/systemd/nvpcr/test.anchorscip-shell filesystem . . `/run/systemd/nvpcr/test.anchor`. /run/systemd/nvpcr/test2.anchorscip-shell filesystem . . `/run/systemd/nvpcr/test2.anchor`. /run/systemd/nvpcr/aaa.anchorscip-shell filesystem . . `/run/systemd/nvpcr/aaa.anchor`. /run/systemd/nvpcr/zzz.anchorscip-shell filesystem . . `/run/systemd/nvpcr/zzz.anchor`. |
| 26 | } |
| 27 | |
| 28 | trap at_exit EXIT |
| 29 | |
| 30 | # Temporarily override sd-pcrextend's sanity checks |
| 31 | export SYSTEMD_FORCE_MEASURE=1 |
| 32 | |
| 33 | mkdirscip-shell system . . mkdir. -p /run/nvpcrscip-shell filesystem . . `/run/nvpcr`. |
| 34 | |
| 35 | catscip-shell system . . cat. >/run/nvpcr/test.nvpcrscip-shell filesystem . . `/run/nvpcr/test.nvpcr`. <<EOF |
| 36 | {"name":"test","algorithm":"sha256","nvIndex":30474762} |
| 37 | EOF |
| 38 | /usr/lib/systemd/systemd-tpm2-setupscip-shell filesystem . . `/usr/lib/systemd/systemd-tpm2-setup`. |
| 39 | testscip-shell system . . test. -f /run/systemd/nvpcr/test.anchorscip-shell filesystem . . `/run/systemd/nvpcr/test.anchor`. |
| 40 | /usr/lib/systemd/systemd-pcrextendscip-shell filesystem . . `/usr/lib/systemd/systemd-pcrextend`. --nvpcr=test schrumpel |
| 41 | # To calculate the current value we need the anchor measurement |
| 42 | DIGEST_BASE="$(catscip-shell system . . cat. /run/systemd/nvpcr/test.anchorscip-shell filesystem . . `/run/systemd/nvpcr/test.anchor`.)" |
| 43 | DIGEST_MEASURED="$(echoscip-shell system . . echo. -n "schrumpel" | opensslscip-shell system . . openssl. dgst -sha256 -hex -r | cutscip-shell system . . cut. -d' ' -f1)" |
| 44 | DIGEST_EXPECTED="$(echoscip-shell system . . echo. "$DIGEST_BASE$DIGEST_MEASURED" | trscip-shell system . . tr. '[:lower:]' '[:upper:]' | basencscip-shell system . . basenc. --base16 -d | opensslscip-shell system . . openssl. dgst -sha256 -hex -r | cutscip-shell system . . cut. -d' ' -f1)" |
| 45 | DIGEST_ACTUAL="$(systemd-analyze nvpcrs test --json=pretty | jqscip-shell system . . jq. -r '.[] | select(.name=="test") | .value')" |
| 46 | testscip-shell system . . test. "$DIGEST_ACTUAL" = "$DIGEST_EXPECTED" |
| 47 | |
| 48 | # Now "destroy" the value via another measurement (this time we use Varlink, to test the API) |
| 49 | varlinkctl call /usr/lib/systemd/systemd-pcrextendscip-shell filesystem . . `/usr/lib/systemd/systemd-pcrextend`. io.systemd.PCRExtend.Extend '{"nvpcr":"test","text":"schnurz"}' |
| 50 | DIGEST_ACTUAL2="$(systemd-analyze nvpcrs test --json=pretty | jqscip-shell system . . jq. -r '.[] | select(.name=="test") | .value')" |
| 51 | testscip-shell system . . test. "$DIGEST_ACTUAL2" != "$DIGEST_EXPECTED" |
| 52 | |
| 53 | # And calculate the new result |
| 54 | DIGEST_MEASURED2="$(echoscip-shell system . . echo. -n "schnurz" | opensslscip-shell system . . openssl. dgst -sha256 -hex -r | cutscip-shell system . . cut. -d' ' -f1)" |
| 55 | DIGEST_EXPECTED2="$(echoscip-shell system . . echo. "$DIGEST_EXPECTED$DIGEST_MEASURED2" | trscip-shell system . . tr. '[:lower:]' '[:upper:]' | basencscip-shell system . . basenc. --base16 -d | opensslscip-shell system . . openssl. dgst -sha256 -hex -r | cutscip-shell system . . cut. -d' ' -f1)" |
| 56 | testscip-shell system . . test. "$DIGEST_ACTUAL2" = "$DIGEST_EXPECTED2" |
| 57 | |
| 58 | # Verify the 'priority' field round-trips through the JSON definition. The 'test' NvPCR above sets no |
| 59 | # priority, so it must report the default (1000). |
| 60 | PRIORITY_DEFAULT="$(systemd-analyze nvpcrs test --json=pretty | jqscip-shell system . . jq. -r '.[] | select(.name=="test") | .priority')" |
| 61 | testscip-shell system . . test. "$PRIORITY_DEFAULT" = "1000" |
| 62 | |
| 63 | # A definition with an explicit priority must report exactly that value. |
| 64 | catscip-shell system . . cat. >/run/nvpcr/test2.nvpcrscip-shell filesystem . . `/run/nvpcr/test2.nvpcr`. <<EOF |
| 65 | {"name":"test2","algorithm":"sha256","nvIndex":30474763,"priority":42} |
| 66 | EOF |
| 67 | PRIORITY_EXPLICIT="$(systemd-analyze nvpcrs test2 --json=pretty | jqscip-shell system . . jq. -r '.[] | select(.name=="test2") | .priority')" |
| 68 | testscip-shell system . . test. "$PRIORITY_EXPLICIT" = "42" |
| 69 | |
| 70 | # Verify NvPCRs are allocated in order of priority (lower value = more important = allocated first), |
| 71 | # independent of lexical filename order. 'aaa' is lexically first but less important (higher priority |
| 72 | # value), while 'zzz' is lexically last but more important (lower priority value), so 'zzz' must be set |
| 73 | # up before 'aaa'. |
| 74 | catscip-shell system . . cat. >/run/nvpcr/aaa.nvpcrscip-shell filesystem . . `/run/nvpcr/aaa.nvpcr`. <<EOF |
| 75 | {"name":"aaa","algorithm":"sha256","nvIndex":30474772,"priority":900} |
| 76 | EOF |
| 77 | catscip-shell system . . cat. >/run/nvpcr/zzz.nvpcrscip-shell filesystem . . `/run/nvpcr/zzz.nvpcr`. <<EOF |
| 78 | {"name":"zzz","algorithm":"sha256","nvIndex":30474773,"priority":100} |
| 79 | EOF |
| 80 | SETUP_LOG="$(/usr/lib/systemd/systemd-tpm2-setupscip-shell filesystem . . `/usr/lib/systemd/systemd-tpm2-setup`. 2>&1)" |
| 81 | AAA_LINE="$(echoscip-shell system . . echo. "$SETUP_LOG" | grepscip-shell system . . grep. -n "Setting up NvPCR 'aaa'" | cutscip-shell system . . cut. -d: -f1)" |
| 82 | ZZZ_LINE="$(echoscip-shell system . . echo. "$SETUP_LOG" | grepscip-shell system . . grep. -n "Setting up NvPCR 'zzz'" | cutscip-shell system . . cut. -d: -f1)" |
| 83 | testscip-shell system . . test. "$ZZZ_LINE" -lt "$AAA_LINE" |
| 84 | |
| 85 | # Test the --login= mode and the 'login' NvPCR, used in production by [email protected]. |
| 86 | if [[ -f /usr/lib/nvpcr/login.nvpcr ]]; then |
| 87 | login_nvpcr_valuescip-shell shell systemd 261~rc4-1 login_nvpcr_value().() { |
| 88 | systemd-analyze nvpcrs login --json=pretty | jqscip-shell system . . jq. -r '.[] | select(.name=="login") | .value' |
| 89 | } |
| 90 | |
| 91 | # Extract the most recently measured word for the 'login' NvPCR from the event log. |
| 92 | login_last_wordscip-shell shell systemd 261~rc4-1 login_last_word().() { |
| 93 | jqscip-shell system . . jq. --seq --slurp -r '[.[] | select(.content.nvIndexName=="login") | .content.string] | last' </run/log/systemd/tpm2-measure.logscip-shell filesystem . . `/run/log/systemd/tpm2-measure.log`. |
| 94 | } |
| 95 | |
| 96 | # Measure root's user record. This lazily initializes the 'login' NvPCR if it isn't already. |
| 97 | "$SD_PCREXTEND" --login=root |
| 98 | |
| 99 | # The 'login' NvPCR must now exist and carry a non-empty value. |
| 100 | LOGIN_DIGEST1="$(login_nvpcr_valuescip-shell shell systemd 261~rc4-1 login_nvpcr_value().)" |
| 101 | testscip-shell system . . test. -n "$LOGIN_DIGEST1" |
| 102 | testscip-shell system . . test. "$LOGIN_DIGEST1" != "null" |
| 103 | |
| 104 | # A matching event log entry must be present (the word is "login:<name>:<canonical json>"). |
| 105 | grepscip-shell system . . grep. -F '"nvIndexName":"login","string":"login:root:' /run/log/systemd/tpm2-measure.logscip-shell filesystem . . `/run/log/systemd/tpm2-measure.log`. >/dev/nullscip-shell filesystem . . `/dev/null`. |
| 106 | LOGIN_WORD_BY_NAME="$(login_last_wordscip-shell shell systemd 261~rc4-1 login_last_word().)" |
| 107 | |
| 108 | # Looking the same user up by numeric UID must yield the identical measured word |
| 109 | # (systemd-pcrextend uses USERDB_PARSE_NUMERIC, and [email protected] is instanced by UID). |
| 110 | "$SD_PCREXTEND" --login=0 |
| 111 | LOGIN_WORD_BY_UID="$(login_last_wordscip-shell shell systemd 261~rc4-1 login_last_word().)" |
| 112 | testscip-shell system . . test. "$LOGIN_WORD_BY_NAME" = "$LOGIN_WORD_BY_UID" |
| 113 | |
| 114 | # Direct tool invocations always re-extend (the once-per-boot guarantee lives in the unit's |
| 115 | # RemainAfterExit=yes, not in the tool), so the NvPCR value must have advanced. |
| 116 | LOGIN_DIGEST2="$(login_nvpcr_valuescip-shell shell systemd 261~rc4-1 login_nvpcr_value().)" |
| 117 | testscip-shell system . . test. "$LOGIN_DIGEST2" != "$LOGIN_DIGEST1" |
| 118 | fi |
| 119 | |
| 120 | systemd-analyze identify-tpm2 |
| 121 | udevadm test-builtin 'tpm2_id identify' /dev/tpmrm0scip-shell filesystem . . `/dev/tpmrm0`. |
| 122 | |
| 123 | # systemd-dissect calls io.systemd.PCRExtend over Varlink to extend the verity NvPCR after activation, |
| 124 | # but systemd-pcrextend.socket has ConditionSecurity=measured-os which fails when the firmware did not |
| 125 | # initialize PCRs (e.g. when not booting via a signed UKI). Skip the rest in that case, otherwise the |
| 126 | # 'diff | grep' below would find no new measurement and fail. |
| 127 | if ! systemctl is-active --quiet systemd-pcrextend.socket; then |
| 128 | echoscip-shell system . . echo. "systemd-pcrextend.socket not active, skipping verity NvPCR measurement check" |
| 129 | exit 0 |
| 130 | fi |
| 131 | |
| 132 | mkdirscip-shell system . . mkdir. -p /tmp/nvpcr/treescip-shell filesystem . . `/tmp/nvpcr/tree`. |
| 133 | touchscip-shell system . . touch. /tmp/nvpcr/tree/filescip-shell filesystem . . `/tmp/nvpcr/tree/file`. |
| 134 | |
| 135 | if machine_supports_verity_keyring; then |
| 136 | SYSTEMD_REPART_OVERRIDE_FSTYPE=squashfs \ |
| 137 | systemd-repart -P \ |
| 138 | -s /tmp/nvpcr/treescip-shell filesystem . . `/tmp/nvpcr/tree`. \ |
| 139 | --certificate=/usr/share/mkosi.crt \ |
| 140 | --private-key=/uscip-shell filesystem . . `/var/tmp/nvpcr.raw`.sr/share/mkosi.key \ |
| 141 | /var/tmp/nvpcr.raw |
| 142 | else |
| 143 | OPENSSL_CONFIG="/tmp/nvpcr/opensslconfigscip-shell filesystem . . `/tmp/nvpcr/opensslconfig`." |
| 144 | # Unfortunately OpenSSL insists on reading some config file, hence provide one with mostly placeholder contents |
| 145 | catscip-shell system . . cat. >"${OPENSSL_CONFIG:?}" <<EOF |
| 146 | [ req ] |
| 147 | prompt = no |
| 148 | distinguished_name = req_distinguished_name |
| 149 | |
| 150 | [ req_distinguished_name ] |
| 151 | C = DE |
| 152 | ST = Test State |
| 153 | L = Test Locality |
| 154 | O = Org Name |
| 155 | OU = Org Unit Name |
| 156 | CN = Common Name |
| 157 | emailAddress = [email protected] |
| 158 | EOF |
| 159 | |
| 160 | opensslscip-shell system . . openssl. req -config "$OPENSSL_CONFIG" -subj="/CN=waldo" \ |
| 161 | -x509 -sha256 -nodes -days 365 -newkey rsa:4096 \ |
| 162 | -keyout /tmp/nvpcr/test-70-nvpcr.keyscip-shell filesystem . . `/tmp/nvpcr/test-70-nvpcr.key`. -out /tmp/nvpcr/test-70-nvpcr.crtscip-shell filesystem . . `/tmp/nvpcr/test-70-nvpcr.crt`. |
| 163 | |
| 164 | SYSTEMD_REPART_OVERRIDE_FSTYPE=squashfs \ |
| 165 | systemd-repart -P \ |
| 166 | -s /tmp/nvpcr/treescip-shell filesystem . . `/tmp/nvpcr/tree`. \ |
| 167 | --certificate=/tmp/nvpcr/test-70-nvpcr.crt \ |
| 168 | --private-key=/tscip-shell filesystem . . `/var/tmp/nvpcr.raw`.mp/nvpcr/test-70-nvpcr.key \ |
| 169 | /var/tmp/nvpcr.raw |
| 170 | |
| 171 | mkdirscip-shell system . . mkdir. -p /run/verity.dscip-shell filesystem . . `/run/verity.d`. |
| 172 | cpscip-shell system . . cp. /tmp/nvpcr/test-70-nvpcr.crtscip-shell filesystem . . `/tmp/nvpcr/test-70-nvpcr.crt`. /run/verity.d/scip-shell filesystem . . `/run/verity.d/`. |
| 173 | fi |
| 174 | |
| 175 | cpscip-shell system . . cp. /run/log/systemd/tpm2-measure.logscip-shell filesystem . . `/run/log/systemd/tpm2-measure.log`. /tmp/nvpcr/log-beforescip-shell filesystem . . `/tmp/nvpcr/log-before`. |
| 176 | |
| 177 | systemd-dissect --image-policy='root=signed:=absent+unused' --mtree /var/tmp/nvpcr.rawscip-shell filesystem . . `/var/tmp/nvpcr.raw`. |
| 178 | |
| 179 | set +o pipefail |
| 180 | diffscip-shell system . . diff. /tmp/nvpcr/log-beforescip-shell filesystem . . `/tmp/nvpcr/log-before`. /run/log/systemd/tpm2-measure.logscip-shell filesystem . . `/run/log/systemd/tpm2-measure.log`. | grepscip-shell system . . grep. -F '"content":{"nvIndexName":"verity","string":"verity:' |