dcavalca / rpms / systemd

Forked from rpms/systemd 4 months ago
Clone
594167
From 6a31abd9fdfe9b08c169b315dd3bc18abbe200b0 Mon Sep 17 00:00:00 2001
594167
From: Grigori Goronzy <greg@chown.ath.cx>
594167
Date: Thu, 24 Feb 2022 01:28:29 +0100
594167
Subject: [PATCH] cryptenroll: add tests for TPM2 unlocking
594167
594167
Add tests for enrolling and unlocking. Various cases are tested:
594167
594167
- Default PCR 7 policy w/o PIN, good and bad cases (wrong PCR)
594167
- PCR 7 + PIN policy, good and bad cases (wrong PCR, wrong PIN)
594167
- Non-default PCR 0+7 policy w/o PIN, good and bad cases (wrong PCR 0)
594167
594167
v2: rename test, fix tss2 library installation, fix CI failures
594167
v3: fix ppc64, load module
594167
(cherry picked from commit fd8b9248206734b655de503f8bb16c2d154934ed)Q
594167
594167
Related: #2087652
594167
---
594167
 test/TEST-70-TPM2/Makefile      |  6 +++++
594167
 test/TEST-70-TPM2/test.sh       | 40 +++++++++++++++++++++++++++
594167
 test/test-functions             |  2 +-
594167
 test/units/testsuite-70.service |  7 +++++
594167
 test/units/testsuite-70.sh      | 48 +++++++++++++++++++++++++++++++++
594167
 5 files changed, 102 insertions(+), 1 deletion(-)
594167
 create mode 100644 test/TEST-70-TPM2/Makefile
594167
 create mode 100755 test/TEST-70-TPM2/test.sh
594167
 create mode 100644 test/units/testsuite-70.service
594167
 create mode 100755 test/units/testsuite-70.sh
594167
594167
diff --git a/test/TEST-70-TPM2/Makefile b/test/TEST-70-TPM2/Makefile
594167
new file mode 100644
594167
index 0000000000..9f65d4ca4f
594167
--- /dev/null
594167
+++ b/test/TEST-70-TPM2/Makefile
594167
@@ -0,0 +1,6 @@
594167
+# SPDX-License-Identifier: LGPL-2.1-or-later
594167
+
594167
+all setup run clean clean-again:
594167
+	@TEST_BASE_DIR=../ ./test.sh --$@
594167
+
594167
+.PHONY: all setup run clean clean-again
594167
diff --git a/test/TEST-70-TPM2/test.sh b/test/TEST-70-TPM2/test.sh
594167
new file mode 100755
594167
index 0000000000..d716614bcf
594167
--- /dev/null
594167
+++ b/test/TEST-70-TPM2/test.sh
594167
@@ -0,0 +1,40 @@
594167
+#!/usr/bin/env bash
594167
+# SPDX-License-Identifier: LGPL-2.1-or-later
594167
+set -e
594167
+
594167
+TEST_DESCRIPTION="cryptenroll/cryptsetup with TPM2 devices"
594167
+IMAGE_NAME="tpm2"
594167
+TEST_NO_NSPAWN=1
594167
+TEST_REQUIRE_INSTALL_TESTS=0
594167
+
594167
+# shellcheck source=test/test-functions
594167
+. "${TEST_BASE_DIR:?}/test-functions"
594167
+
594167
+command -v swtpm >/dev/null 2>&1 || exit 0
594167
+command -v tpm2_pcrextend >/dev/null 2>&1 || exit 0
594167
+
594167
+test_append_files() {
594167
+    (
594167
+        local workspace="${1:?}"
594167
+
594167
+        instmods tpm tpm_tis tpm_ibmvtpm
594167
+        install_dmevent
594167
+        generate_module_dependencies
594167
+        inst_binary tpm2_pcrextend
594167
+    )
594167
+}
594167
+
594167
+machine="$(uname -m)"
594167
+tpmdevice="tpm-tis"
594167
+if [ "$machine" = "ppc64le" ]; then
594167
+    # tpm-spapr support was introduced in qemu 5.0.0. Skip test for old qemu versions.
594167
+    qemu_min_version "5.0.0" || exit 0
594167
+    tpmdevice="tpm-spapr"
594167
+fi
594167
+
594167
+tpmstate=$(mktemp -d)
594167
+swtpm socket --tpm2 --tpmstate dir="$tpmstate" --ctrl type=unixio,path="$tpmstate/sock" &
594167
+trap 'kill %%; rm -rf $tpmstate' SIGINT EXIT
594167
+QEMU_OPTIONS="-chardev socket,id=chrtpm,path=$tpmstate/sock -tpmdev emulator,id=tpm0,chardev=chrtpm -device $tpmdevice,tpmdev=tpm0"
594167
+
594167
+do_test "$@"
594167
diff --git a/test/test-functions b/test/test-functions
594167
index 4a6436a74b..050fefaf1b 100644
594167
--- a/test/test-functions
594167
+++ b/test/test-functions
594167
@@ -1198,7 +1198,7 @@ install_missing_libraries() {
594167
     local lib path
594167
     # A number of dependencies is now optional via dlopen, so the install
594167
     # script will not pick them up, since it looks at linkage.
594167
-    for lib in libcryptsetup libidn libidn2 pwquality libqrencode tss2-esys tss2-rc tss2-mu libfido2 libbpf libelf libdw; do
594167
+    for lib in libcryptsetup libidn libidn2 pwquality libqrencode tss2-esys tss2-rc tss2-mu tss2-tcti-device libfido2 libbpf libelf libdw; do
594167
         ddebug "Searching for $lib via pkg-config"
594167
         if pkg-config --exists "$lib"; then
594167
                 path="$(pkg-config --variable=libdir "$lib")"
594167
diff --git a/test/units/testsuite-70.service b/test/units/testsuite-70.service
594167
new file mode 100644
594167
index 0000000000..c13c2d51a3
594167
--- /dev/null
594167
+++ b/test/units/testsuite-70.service
594167
@@ -0,0 +1,7 @@
594167
+# SPDX-License-Identifier: LGPL-2.1-or-later
594167
+[Unit]
594167
+Description=TEST-70-TPM2
594167
+
594167
+[Service]
594167
+Type=oneshot
594167
+ExecStart=/usr/lib/systemd/tests/testdata/units/%N.sh
594167
diff --git a/test/units/testsuite-70.sh b/test/units/testsuite-70.sh
594167
new file mode 100755
594167
index 0000000000..f395ef4e5e
594167
--- /dev/null
594167
+++ b/test/units/testsuite-70.sh
594167
@@ -0,0 +1,48 @@
594167
+#!/usr/bin/env bash
594167
+# SPDX-License-Identifier: LGPL-2.1-or-later
594167
+set -ex
594167
+
594167
+export SYSTEMD_LOG_LEVEL=debug
594167
+
594167
+
594167
+# Prepare fresh disk image
594167
+img="/var/tmp/test.img"
594167
+dd if=/dev/zero of=$img bs=1024k count=20 status=none
594167
+echo -n passphrase >/tmp/passphrase
594167
+cryptsetup luksFormat -q --use-urandom $img /tmp/passphrase
594167
+
594167
+# Enroll unlock with default PCR policy
594167
+env PASSWORD=passphrase systemd-cryptenroll --tpm2-device=auto $img
594167
+/usr/lib/systemd/systemd-cryptsetup attach test-volume $img - tpm2-device=auto,headless=1
594167
+/usr/lib/systemd/systemd-cryptsetup detach test-volume
594167
+
594167
+# Check with wrong PCR
594167
+tpm2_pcrextend 7:sha256=0000000000000000000000000000000000000000000000000000000000000000
594167
+/usr/lib/systemd/systemd-cryptsetup attach test-volume $img - tpm2-device=auto,headless=1 && { echo 'unexpected success'; exit 1; }
594167
+
594167
+# Enroll unlock with PCR+PIN policy
594167
+systemd-cryptenroll --wipe-slot=tpm2 $img
594167
+env PASSWORD=passphrase NEWPIN=123456 systemd-cryptenroll --tpm2-device=auto --tpm2-with-pin=true $img
594167
+env PIN=123456 /usr/lib/systemd/systemd-cryptsetup attach test-volume $img - tpm2-device=auto,headless=1
594167
+/usr/lib/systemd/systemd-cryptsetup detach test-volume
594167
+
594167
+# Check failure with wrong PIN
594167
+env PIN=123457 /usr/lib/systemd/systemd-cryptsetup attach test-volume $img - tpm2-device=auto,headless=1 && { echo 'unexpected success'; exit 1; }
594167
+
594167
+# Check failure with wrong PCR (and correct PIN)
594167
+tpm2_pcrextend 7:sha256=0000000000000000000000000000000000000000000000000000000000000000
594167
+env PIN=123456 /usr/lib/systemd/systemd-cryptsetup attach test-volume $img - tpm2-device=auto,headless=1 && { echo 'unexpected success'; exit 1; }
594167
+
594167
+# Enroll unlock with PCR 0+7
594167
+systemd-cryptenroll --wipe-slot=tpm2 $img
594167
+env PASSWORD=passphrase systemd-cryptenroll --tpm2-device=auto --tpm2-pcrs=0+7 $img
594167
+/usr/lib/systemd/systemd-cryptsetup attach test-volume $img - tpm2-device=auto,headless=1
594167
+/usr/lib/systemd/systemd-cryptsetup detach test-volume
594167
+
594167
+# Check with wrong PCR 0
594167
+tpm2_pcrextend 0:sha256=0000000000000000000000000000000000000000000000000000000000000000
594167
+/usr/lib/systemd/systemd-cryptsetup attach test-volume $img - tpm2-device=auto,headless=1 && exit 1
594167
+
594167
+echo OK >/testok
594167
+
594167
+exit 0