Skip to contentSkip to conversation

TPM 2.0 fixes in IMA tests

PATCH Superseded Petr Vorel 4 participants 20 messages Sep 29, 2020 – Oct 6, 2020 Keys-trusted

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Petr Vorel

author
#0 lore raw

v3 cover TPM 2.0 fixes in IMA tests

Hi,

few more fixes, mostly touching older kernels or evmctl versions. Changes in 3rd and 4th commit.

Kind regards,
Petr

Petr Vorel (4):
IMA: Move get_algorithm_digest(), set_digest_index() to ima_setup.sh
IMA: Rewrite ima_boot_aggregate.c to new API
ima_tpm.sh: Fix calculating boot aggregate
ima_tpm.sh: Fix calculating PCR aggregate

 .../integrity/ima/src/ima_boot_aggregate.c    | 113 ++++----
 .../integrity/ima/tests/ima_measurements.sh   |  62 +---
 .../security/integrity/ima/tests/ima_setup.sh |  70 +++++
 .../security/integrity/ima/tests/ima_tpm.sh   | 265 ++++++++++++++----
 4 files changed, 341 insertions(+), 169 deletions(-)
Signature · 1 line
2.28.0
author
#1 lore raw

PATCH v3 1/4 IMA: Move get_algorithm_digest(), set_digest_index() to ima_setup.sh

To be reusable by more tests (preparation for next commit).

Call set_digest_index() inside get_algorithm_digest() if needed instead of expecting get_algorithm_digest() caller to call set_digest_index() before.

---
Changes v2->v3:
none

 .../integrity/ima/tests/ima_measurements.sh   | 62 ++--------------
 .../security/integrity/ima/tests/ima_setup.sh | 70 +++++++++++++++++++
 2 files changed, 76 insertions(+), 56 deletions(-)
testcases/kernel/security/integrity/ima/tests/ima_measurements.sh +6 -56
index 9a7500c76..1927e937c 100755
@@ -6,7 +6,7 @@
 #
 # Verify that measurements are added to the measurement list based on policy.
 
-TST_NEEDS_CMDS="awk cut"
+TST_NEEDS_CMDS="awk cut sed"
 TST_SETUP="setup"
 TST_CNT=3
 TST_NEEDS_DEVICE=1
@@ -20,72 +20,22 @@ setup()
 	TEST_FILE="$PWD/test.txt"
 	POLICY="$IMA_DIR/policy"
 	[ -f "$POLICY" ] || tst_res TINFO "not using default policy"
-	DIGEST_INDEX=
-
-	local template="$(tail -1 $ASCII_MEASUREMENTS | cut -d' ' -f 3)"
-	local i
-
-	# parse digest index
-	# https://www.kernel.org/doc/html/latest/security/IMA-templates.html#use
-	case "$template" in
-	ima|ima-ng|ima-sig|ima-buf) DIGEST_INDEX=4 ;;
-	*)
-		# using ima_template_fmt kernel parameter
-		local IFS="|"
-		i=4
-		for word in $template; do
-			if [ "$word" = 'd' -o "$word" = 'd-ng' ]; then
-				DIGEST_INDEX=$i
-				break
-			fi
-			i=$((i+1))
-		done
-	esac
-
-	[ -z "$DIGEST_INDEX" ] && tst_brk TCONF \
-		"Cannot find digest index (template: '$template')"
 }
 
 ima_check()
 {
-	local delimiter=':'
-	local algorithm digest expected_digest line
+	local algorithm digest expected_digest line tmp
 
 	# need to read file to get updated $ASCII_MEASUREMENTS
 	cat $TEST_FILE > /dev/null
 
 	line="$(grep $TEST_FILE $ASCII_MEASUREMENTS | tail -1)"
-	if [ -z "$line" ]; then
-		tst_res TFAIL "cannot find measurement record for '$TEST_FILE'"
-		return
-	fi
-	tst_res TINFO "measurement record: '$line'"
 
-	digest=$(echo "$line" | cut -d' ' -f $DIGEST_INDEX)
-	if [ -z "$digest" ]; then
-		tst_res TFAIL "cannot find digest (index: $DIGEST_INDEX)"
-		return
-	fi
-
-	if [ "${digest#*$delimiter}" != "$digest" ]; then
-		algorithm=$(echo "$digest" | cut -d $delimiter -f 1)
-		digest=$(echo "$digest" | cut -d $delimiter -f 2)
+	if tmp=$(get_algorithm_digest "$line"); then
+		algorithm=$(echo "$tmp" | cut -d'|' -f1)
+		digest=$(echo "$tmp" | cut -d'|' -f2)
 	else
-		case "${#digest}" in
-		32) algorithm="md5" ;;
-		40) algorithm="sha1" ;;
-		*)
-			tst_res TFAIL "algorithm must be either md5 or sha1 (digest: '$digest')"
-			return ;;
-		esac
-	fi
-	if [ -z "$algorithm" ]; then
-		tst_res TFAIL "cannot find algorithm"
-		return
-	fi
-	if [ -z "$digest" ]; then
-		tst_res TFAIL "cannot find digest"
-		return
+		tst_res TBROK "failed to get algorithm/digest for '$TEST_FILE': $tmp"
 	fi
 
 	tst_res TINFO "computing digest for $algorithm algorithm"
testcases/kernel/security/integrity/ima/tests/ima_setup.sh +70 -0

76 lines not on this page · raw message

Signature · 1 line
2.28.0
author
#2 lore raw

PATCH v3 2/4 IMA: Rewrite ima_boot_aggregate.c to new API

In reply to Petr Vorel

The main reason was to see TCONF messages, which are printed into stderr in new API (but to stdout in legacy API) and thus visible as the output is redirected into the variable.

Changing boot_aggregate: to sha1: to be compatible with evmctl ima_boot_aggregate.

---
Changes v2->v3:
none

 .../integrity/ima/src/ima_boot_aggregate.c    | 113 +++++++++---------
 .../security/integrity/ima/tests/ima_tpm.sh   |   2 +-
 2 files changed, 57 insertions(+), 58 deletions(-)
testcases/kernel/security/integrity/ima/src/ima_boot_aggregate.c +56 -57
index 98893b99a..67f97ea04 100644
@@ -1,19 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
-* Copyright (c) International Business Machines  Corp., 2009
-*
-* Authors:
-* Mimi Zohar <[email protected]>
-*
-* This program is free software; you can redistribute it and/or
-* modify it under the terms of the GNU General Public License as
-* published by the Free Software Foundation, version 2 of the
-* License.
-*
-* File: ima_boot_aggregate.c
-*
-* Calculate a SHA1 boot aggregate value based on the TPM
-* binary_bios_measurements.
-*/
+ * Copyright (c) International Business Machines  Corp., 2009
+ * Copyright (c) 2016-2019 Petr Vorel <[email protected]>
+ *
+ * Authors: Mimi Zohar <[email protected]>
+ *
+ * Calculate a SHA1 boot aggregate value based on the TPM 1.2
+ * binary_bios_measurements.
+ */
+
+#include "config.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
@@ -23,10 +19,8 @@
 #include <unistd.h>
 #include <limits.h>
 
-#include "config.h"
-#include "test.h"
-
-char *TCID = "ima_boot_aggregate";
+#include "tst_test.h"
+#include "tst_safe_stdio.h"
 
 #if HAVE_LIBCRYPTO
 #include <openssl/sha.h>
@@ -36,7 +30,24 @@ char *TCID = "ima_boot_aggregate";
 #define MAX_EVENT_DATA_SIZE (MAX_EVENT_SIZE - EVENT_HEADER_SIZE)
 #define NUM_PCRS 8		/*  PCR registers 0-7 in boot aggregate */
 
-int TST_TOTAL = 1;
+static char *debug;
+static char *file;
+
+static unsigned char boot_aggregate[SHA_DIGEST_LENGTH];
+
+static struct {
+	struct {
+		u_int32_t pcr;
+		u_int32_t type;
+		u_int8_t digest[SHA_DIGEST_LENGTH];
+		u_int32_t len;
+	} header __attribute__ ((packed));
+	char *data;
+} event;
+
+static struct {
+	unsigned char digest[SHA_DIGEST_LENGTH];
+} pcr[NUM_PCRS];
 
 static void display_sha1_digest(unsigned char *pcr)
 {
@@ -47,45 +58,24 @@ static void display_sha1_digest(unsigned char *pcr)
 	printf("\n");
 }
 
-int main(int argc, char *argv[])
+static void do_test(void)
 {
-	unsigned char boot_aggregate[SHA_DIGEST_LENGTH];
-	struct {
-		struct {
-			u_int32_t pcr;
-			u_int32_t type;
-			u_int8_t digest[SHA_DIGEST_LENGTH];
-			u_int32_t len;
-		} header __attribute__ ((packed));
-		char *data;
-	} event;
-	struct {
-		unsigned char digest[SHA_DIGEST_LENGTH];
-	} pcr[NUM_PCRS];
 	FILE *fp;
-	int i;
-	int debug = 0;
 	SHA_CTX c;
+	int i;
 
-	if (argc != 2) {
-		printf("format: %s binary_bios_measurement file\n", argv[0]);
-		return 1;
-	}
-	fp = fopen(argv[1], "r");
-	if (!fp) {
-		perror("unable to open pcr file\n");
-		return 1;
-	}
+	if (!file)
+		tst_brk(TBROK, "missing binary_bios_measurement file, specify with -f");
+
+	fp = SAFE_FOPEN(file, "r");
 
 	/* Initialize psuedo PCR registers 0 - 7 */
 	for (i = 0; i < NUM_PCRS; i++)
 		memset(&pcr[i].digest, 0, SHA_DIGEST_LENGTH);
 
 	event.data = malloc(MAX_EVENT_DATA_SIZE);
-	if (!event.data) {
-		printf("Cannot allocate memory\n");
-		return 1;
-	}
+	if (!event.data)
+		tst_brk(TBROK, "cannot allocate memory");
 
 	/* Extend the pseudo PCRs with the event digest */
 	while (fread(&event, sizeof(event.header), 1, fp)) {
@@ -105,13 +95,14 @@ int main(int argc, char *argv[])
 
 #if MAX_EVENT_DATA_SIZE < USHRT_MAX
 		if (event.header.len > MAX_EVENT_DATA_SIZE) {
-			printf("Error event too long\n");
+			tst_res(TWARN, "error event too long");
 			break;
 		}
 #endif
 		fread(event.data, event.header.len, 1, fp);
 	}
-	fclose(fp);
+
+	SAFE_FCLOSE(fp);
 	free(event.data);
 
 	/* Extend the boot aggregate with the pseudo PCR digest values */
@@ -126,14 +117,22 @@ int main(int argc, char *argv[])
 	}
 	SHA1_Final(boot_aggregate, &c);
 
-	printf("boot_aggregate:");
+	printf("sha1:");
 	display_sha1_digest(boot_aggregate);
-	tst_exit();
+	tst_res(TPASS, "found sha1 hash");
 }
 
+static struct tst_option options[] = {
+	{"d", &debug, "-d       enable debug"},
+	{"f:", &file, "-f x     binary_bios_measurement file (required)\n"},
+	{NULL, NULL, NULL}
+};
+
+static struct tst_test test = {
+	.test_all = do_test,
+	.options = options,
+};
+
 #else
-int main(void)
-{
-	tst_brkm(TCONF, NULL, "test requires libcrypto and openssl development packages");
-}
+TST_TEST_TCONF("libcrypto and openssl development packages required");
 #endif
testcases/kernel/security/integrity/ima/tests/ima_tpm.sh +1 -1
index c69f891f1..dc958eb5c 100755
@@ -33,7 +33,7 @@ test1()
 			tst_res TFAIL "bios boot aggregate is not 0"
 		fi
 	else
-		boot_aggregate=$(ima_boot_aggregate $tpm_bios | grep "boot_aggregate:" | cut -d':' -f2)
+		boot_aggregate=$(ima_boot_aggregate -f $tpm_bios | grep "sha1:" | cut -d':' -f2)
 		if [ "$boot_hash" = "$boot_aggregate" ]; then
 			tst_res TPASS "bios aggregate matches IMA boot aggregate"
 		else
Signature · 1 line
2.28.0
maintainer
#3 lore raw

Re: [PATCH v3 2/4] IMA: Rewrite ima_boot_aggregate.c to new API

Quoted 9 lines · Petr Vorel

The main reason was to see TCONF messages, which are printed into stderr in new API (but to stdout in legacy API) and thus visible as the output is redirected into the variable.

Changing boot_aggregate: to sha1: to be compatible with evmctl ima_boot_aggregate.

---

<snip>

testcases/kernel/security/integrity/ima/tests/ima_tpm.sh test1() · line 39

Quoted 13 lines
testcases/kernel/security/integrity/ima/tests/ima_tpm.sh +1 -1
index c69f891f1..dc958eb5c 100755
@@ -33,7 +33,7 @@ test1()
 			tst_res TFAIL "bios boot aggregate is not 0"
 		fi
 	else
-		boot_aggregate=$(ima_boot_aggregate $tpm_bios | grep "boot_aggregate:" | cut -d':' -f2)
+		boot_aggregate=$(ima_boot_aggregate -f $tpm_bios | grep "sha1:" | cut -d':' -f2)
 		if [ "$boot_hash" = "$boot_aggregate" ]; then
 			tst_res TPASS "bios aggregate matches IMA boot aggregate"
 		else

The original "ima" template is just the hash digest, without the algorithm.

Mimi

author
#4 lore raw

Re: [PATCH v3 2/4] IMA: Rewrite ima_boot_aggregate.c to new API

Hi Mimi,

Quoted 18 lines

<snip>

testcases/kernel/security/integrity/ima/tests/ima_tpm.sh test1() · line 39

Quoted 13 lines
testcases/kernel/security/integrity/ima/tests/ima_tpm.sh +1 -1
index c69f891f1..dc958eb5c 100755
@@ -33,7 +33,7 @@ test1()
 			tst_res TFAIL "bios boot aggregate is not 0"
 		fi
 	else
-		boot_aggregate=$(ima_boot_aggregate $tpm_bios | grep "boot_aggregate:" | cut -d':' -f2)
+		boot_aggregate=$(ima_boot_aggregate -f $tpm_bios | grep "sha1:" | cut -d':' -f2)
 		if [ "$boot_hash" = "$boot_aggregate" ]; then
 			tst_res TPASS "bios aggregate matches IMA boot aggregate"
 		else

The original "ima" template is just the hash digest, without the algorithm.

Yes, but this code is output of ima_boot_aggregate.c. And code detecting old format is still working (verified on ima_measurements.sh with ima_tcb kernel parameter on 3.10).

Kind regards,
Petr

author
#5 lore raw

PATCH v3 3/4 ima_tpm.sh: Fix calculating boot aggregate

In reply to Petr Vorel

for TPM 2.0 or kernel >= v5.8-rc1:
6f1a1d103b48 ima: ("Switch to ima_hash_algo for boot aggregate")

Test still fails with TPM 2.0 on kernel < v5.8-rc1.

Test was failing, because it expect SHA1 hash, but for TPM 2.0 is now used IMA default hash algorithm (by default default SHA256). This is similar for entries in IMA measurement list so we can reuse already existing code.

Reading other algorithms than sha1 or TPM 2.0 requires evmctl >= 1.3. Although recent evmctl is recommended, to support older kernels when sha1 is used, get boot aggregate with old our legacy ima_boot_aggregate.c.

Also fixed test without TPM device (when IMA TPM-bypass is tested) as some TPM 2.0 devices does not export event log (/sys/kernel/security/tpm0/binary_bios_measurements). This does not require evmctl at all.

---
Changes v2->v3:
* separate testing real HW and IMA TPM-bypass into it's own functions
* fixed checking in check_evmctl
* improve get_tpm_version to not depend on having dmesg on IMA TPM-bypass

Kind regards,
Petr

 .../security/integrity/ima/tests/ima_tpm.sh   | 153 +++++++++++++++---
 1 file changed, 131 insertions(+), 22 deletions(-)
testcases/kernel/security/integrity/ima/tests/ima_tpm.sh +131 -22
index dc958eb5c..7eb3a9409 100755
@@ -7,41 +7,150 @@
 # Verify the boot and PCR aggregates.
 
 TST_CNT=2
-TST_NEEDS_CMDS="awk cut ima_boot_aggregate"
+TST_NEEDS_CMDS="awk cut"
+TST_SETUP="setup"
 
 . ima_setup.sh
 
-test1()
-{
-	tst_res TINFO "verify boot aggregate"
+EVMCTL_REQUIRED='1.3'
+ERRMSG_EVMCTL="install evmctl >= $EVMCTL_REQUIRED"
 
-	local zero="0000000000000000000000000000000000000000"
-	local tpm_bios="$SECURITYFS/tpm0/binary_bios_measurements"
-	local ima_measurements="$ASCII_MEASUREMENTS"
-	local boot_aggregate boot_hash line
+setup()
+{
+	local line tmp
 
-	# IMA boot aggregate
-	read line < $ima_measurements
-	boot_hash=$(echo $line | awk '{print $(NF-1)}' | cut -d':' -f2)
+	read line < $ASCII_MEASUREMENTS
+	if tmp=$(get_algorithm_digest "$line"); then
+		ALGORITHM=$(echo "$tmp" | cut -d'|' -f1)
+		DIGEST=$(echo "$tmp" | cut -d'|' -f2)
+	else
+		tst_res TBROK "failed to get algorithm/digest: $tmp"
+	fi
+	tst_res TINFO "used algorithm: $ALGORITHM"
 
-	if [ ! -f "$tpm_bios" ]; then
-		tst_res TINFO "TPM Hardware Support not enabled in kernel or no TPM chip found"
+	TPM_VERSION="$(get_tpm_version)"
+	if [ "$TPM_VERSION" ]; then
+		tst_res TINFO "TMP major version: $TPM_VERSION"
+	fi
 
-		if [ "$boot_hash" = "$zero" ]; then
-			tst_res TPASS "bios boot aggregate is 0"
-		else
-			tst_res TFAIL "bios boot aggregate is not 0"
+	if ! check_evmctl $EVMCTL_REQUIRED; then
+		if [ "$ALGORITHM" != "sha1" ]; then
+			tst_brk TCONF "algorithm not sha1 ($ALGORITHM), $ERRMSG_EVMCTL"
 		fi
+		MISSING_EVMCTL=1
+	fi
+}
+
+# check_evmctl REQUIRED_TPM_VERSION
+# return: 0: evmctl is new enough, 1: too old version (or version > v0.9)
+check_evmctl()
+{
+	local required="$1"
+
+	local r1="$(echo $required | cut -d. -f1)"
+	local r2="$(echo $required | cut -d. -f2)"
+	local r3="$(echo $required | cut -d. -f3)"
+	[ -z "$r3" ] && r3=0
+
+	tst_is_int "$r1" || tst_brk TBROK "required major version not int ($v1)"
+	tst_is_int "$r2" || tst_brk TBROK "required minor version not int ($v2)"
+	tst_is_int "$r3" || tst_brk TBROK "required patch version not int ($v3)"
+
+	tst_check_cmds evmctl || return 1
+
+	local v="$(evmctl --version | cut -d' ' -f2)"
+	[ -z "$v" ] && return 1
+	tst_res TINFO "evmctl version: $v"
+
+	local v1="$(echo $v | cut -d. -f1)"
+	local v2="$(echo $v | cut -d. -f2)"
+	local v3="$(echo $v | cut -d. -f3)"
+	[ -z "$v3" ] && v3=0
+
+	if [ $v1 -lt $r1 ] || [ $v1 -eq $r1 -a $v2 -lt $r2 ] || \
+		[ $v1 -eq $r1 -a $v2 -eq $r2 -a $v3 -lt $r3 ]; then
+		return 1
+	fi
+	return 0
+}
+
+# prints major version 1: TPM 1.2, 2: TPM 2.0
+# or nothing when version not detected (no TPM device)
+get_tpm_version()
+{
+	if [ -f /sys/class/tpm/tpm0/tpm_version_major ]; then
+		cat /sys/class/tpm/tpm0/tpm_version_major
+		return
+	fi
+
+	if [ -f /sys/class/tpm/tpm0/device/caps -o \
+		-f /sys/class/misc/tpm0/device/caps ]; then
+		echo 1
+		return
+	fi
+
+	if [ ! -d /sys/class/tpm/tpm0/ -a ! -d /sys/class/misc/tpm0/ ]; then
+		return
+	fi
+
+	tst_check_cmds dmesg || return
+	if dmesg | grep -q '1\.2 TPM (device-id'; then
+		echo 1
+	elif dmesg | grep -q '2\.0 TPM (device-id'; then
+		echo 2
+	fi
+}
+
+test1_virtual_tpm()
+{
+	local zero=$(echo $DIGEST | awk '{gsub(/./, "0")}; {print}')
+
+	tst_res TINFO "TPM hardware support not enabled in kernel or no TPM chip found, testing TPM-bypass"
+
+	if [ "$DIGEST" = "$zero" ]; then
+		tst_res TPASS "bios boot aggregate is $zero"
 	else
-		boot_aggregate=$(ima_boot_aggregate -f $tpm_bios | grep "sha1:" | cut -d':' -f2)
-		if [ "$boot_hash" = "$boot_aggregate" ]; then
-			tst_res TPASS "bios aggregate matches IMA boot aggregate"
-		else
-			tst_res TFAIL "bios aggregate does not match IMA boot aggregate"
+		tst_res TFAIL "bios boot aggregate is not $zero ($DIGEST)"
+	fi
+}
+
+test1_hw_tpm()
+{
+	local tpm_bios="$SECURITYFS/tpm0/binary_bios_measurements"
+	local cmd="evmctl ima_boot_aggregate"
+	local boot_aggregate
+
+	if [ "$MISSING_EVMCTL" = 1 ]; then
+		if [ ! -f "$tpm_bios" ]; then
+			tst_res TCONF "missing $tpm_bios, $ERRMSG_EVMCTL"
+			return
 		fi
+		tst_check_cmds ima_boot_aggregate || return
+		cmd="ima_boot_aggregate -f $tpm_bios"
+	fi
+	tst_res TINFO "using command: $cmd"
+
+	boot_aggregate=$($cmd | grep "$ALGORITHM:" | cut -d':' -f2)
+	if [ -z "$boot_aggregate" ]; then
+		tst_res TBROK "failed to get boot aggregate"
+		return
+	fi
+	tst_res TINFO "IMA boot aggregate: '$boot_aggregate'"
+
+	if [ "$DIGEST" = "$boot_aggregate" ]; then
+		tst_res TPASS "bios boot aggregate matches IMA boot aggregate"
+	else
+		tst_res TFAIL "bios boot aggregate does not match IMA boot aggregate ($DIGEST)"
 	fi
 }
 
+test1()
+{
+	tst_res TINFO "verify boot aggregate"
+
+	[ -z "$TPM_VERSION" ] && test1_virtual_tpm || test1_hw_tpm
+}
+
 # Probably cleaner to programmatically read the PCR values directly
 # from the TPM, but that would require a TPM library. For now, use
 # the PCR values from /sys/devices.
Signature · 1 line
2.28.0
maintainer
#6 lore raw

Re: [PATCH v3 3/4] ima_tpm.sh: Fix calculating boot aggregate

Quoted 7 lines · Petr Vorel

for TPM 2.0 or kernel >= v5.8-rc1:
6f1a1d103b48 ima: ("Switch to ima_hash_algo for boot aggregate")

Test still fails with TPM 2.0 on kernel < v5.8-rc1.

Test was failing, because it expect SHA1 hash, but for TPM 2.0 is now used IMA default hash algorithm (by default default SHA256).

TPM 1.2 only supported MD5 and SHA1, at least the IMA measurement list did. (This is probably the reason it searched for "boot_aggregate" and not "sha1".) Nobody should be using MD5, so the boot_aggregate for TPM 1.2 should always be SHA1.

For TPM 2.0, which supports multiple algorithms, it's a bit more complicated. The hash algorithm used for calculating the "boot_aggregate" is dependent on which TPM bank is enabled. For example, even if the default IMA measurement hash is defined as SHA256, but the TPM 2.0 SHA256 bank isn't enabled, it will fall back to calculating a SHA1 "boot_aggregate".

For backwards compatibility, the SHA1 boot_aggregate is based on PCRs 0 -7, while all others include PCRs 0 - 9.

Quoted 6 lines

This is similar for entries in IMA measurement list so we can reuse already existing code.

Reading other algorithms than sha1 or TPM 2.0 requires evmctl >= 1.3. Although recent evmctl is recommended, to support older kernels when sha1 is used, get boot aggregate with old our legacy ima_boot_aggregate.c.

For example, some PTT firmware (TPM 2.0) only supports SHA1. (I've
seen some TPM 2.0's using the TPM 1.2 event log format.) Instead of
saying, "to support older kernels when sha1 is used, get boot_aggregate ...", it should say "to support older TPMs, calculate the "boot_aggregate" with ...cat

One of the major differences is that the original TPM 1.2 support walked the TPM event log, extending the PCRs. Only after re- calculating the PCRs, would it calculate the "boot_aggregate". The TPM 2.0 support assumes that some other mechanism exists for verifying the PCRs against the TPM event log* and just calculates the "boot_aggregate" based on the existing TPM PCR values.

* tsseventextend -sim -if /sys/kernel/security/tpm0/binary_bios_measurements -v -ns

Mimi

Quoted 15 lines

Also fixed test without TPM device (when IMA TPM-bypass is tested) as some TPM 2.0 devices does not export event log (/sys/kernel/security/tpm0/binary_bios_measurements). This does not require evmctl at all.

---
Changes v2->v3:
* separate testing real HW and IMA TPM-bypass into it's own functions
* fixed checking in check_evmctl
* improve get_tpm_version to not depend on having dmesg on IMA TPM-bypass

Kind regards,
Petr

author
#7 lore raw

PATCH v3 4/4 ima_tpm.sh: Fix calculating PCR aggregate

In reply to Petr Vorel

for TPM 2.0 and never or very old evmctl versions.

Because exporting PCR registers for TPM 2.0 has not been upstreamed [1], we use user space code, which requires evmctl >= 1.3 and tsspcrread. Using evmctl allows to test for TPM devices which does not export event log (/sys/kernel/security/tpm0/binary_bios_measurements).

For TPM 1.2 read tpm0 device pcrs file from kernel. (tss1pcrread could be also used, but it's not yet packaged by distros.)

For old kernels which use sha1, any evmctl version is required (evmctl ima_measurement was introduced in very old v0.7, but newer sysctl path /sys/class/tpm/tpm0/device/pcrs requires evmctl 1.1)

We now support output format of ima_measurement command for various evmctl versions:
* 1.3: "sha256: TPM PCR-10:" (or other algorithm, e.g. "sha1")
* 1.1-1.2.1: "HW PCR-10:" (the only previously supported format)
* 0.7-1.0: "PCR-10:"
NOTE: we ignore evmctl failure, because evmctl < 1.1 fails with "PCRAgg does not match PCR-10".

[1] https://patchwork.kernel.org/patch/11759729/

---
Changes v2->v3:
* grep for PCRAgg (compatible with older versions)
* do not check evmctl ima_measurement failure due "PCRAgg does not match PCR-10" on evmctl < 1.1
* require evmctl 1.1 for new /sys/class/tpm/tpm0/device/pcrs

Kind regards,
Petr

 .../security/integrity/ima/tests/ima_tpm.sh   | 128 ++++++++++++------
 1 file changed, 86 insertions(+), 42 deletions(-)
testcases/kernel/security/integrity/ima/tests/ima_tpm.sh +86 -42
index 7eb3a9409..b878467ed 100755
@@ -14,6 +14,7 @@ TST_SETUP="setup"
 
 EVMCTL_REQUIRED='1.3'
 ERRMSG_EVMCTL="install evmctl >= $EVMCTL_REQUIRED"
+ERRMSG_TPM="TPM hardware support not enabled in kernel or no TPM chip found"
 
 setup()
 {
@@ -101,11 +102,72 @@ get_tpm_version()
 	fi
 }
 
+read_pcr_tpm1()
+{
+	local pcr_path="/sys/class/tpm/tpm0/device/pcrs"
+	local evmctl_required="1.1"
+	local pcr hash
+
+	if [ ! -f "$pcrs_path" ]; then
+		pcrs_path="/sys/class/misc/tpm0/device/pcrs"
+	else
+		check_evmctl $evmctl_required || \
+			tst_brk TCONF "evmctl >= $evmctl_required required"
+	fi
+
+	if [ ! -f "$pcr_path" ]; then
+		tst_brk TCONF "missing PCR file $pcrs_path ($ERRMSG_TPM)"
+	fi
+
+	while read line; do
+		pcr="$(echo $line | cut -d':' -f1)"
+		hash="$(echo $line | cut -d':' -f2 | awk '{ gsub (" ", "", $0); print tolower($0) }')"
+		echo "$pcr: $hash"
+	done < $pcr_path
+}
+
+# NOTE: TPM 1.2 would require to use tss1pcrread which is not fully adopted
+# by distros yet.
+read_pcr_tpm2()
+{
+	local pcrmax=23
+	local pcrread="tsspcrread -halg $ALGORITHM"
+	local i pcr
+
+	tst_check_cmds tsspcrread || return 1
+
+	for i in $(seq 0 $pcrmax); do
+		pcr=$($pcrread -ha "$i" -ns)
+		if [ $? -ne 0 ]; then
+			tst_brk TBROK "tsspcrread failed: $pcr"
+		fi
+		printf "PCR-%02d: %s\n" $i "$pcr"
+	done
+}
+
+get_pcr10_aggregate()
+{
+	local pcr
+
+	evmctl -v ima_measurement $BINARY_MEASUREMENTS > hash.txt 2>&1
+
+	pcr=$(grep -E "^($ALGORITHM: )*PCRAgg.*:" hash.txt \
+		| awk '{print $NF}')
+
+	if [ -z "$pcr" ]; then
+		tst_res TFAIL "evmctl failed to get aggregate PCR-10"
+		cat hash.txt >&2
+		return
+	fi
+
+	echo "$pcr"
+}
+
 test1_virtual_tpm()
 {
 	local zero=$(echo $DIGEST | awk '{gsub(/./, "0")}; {print}')
 
-	tst_res TINFO "TPM hardware support not enabled in kernel or no TPM chip found, testing TPM-bypass"
+	tst_res TINFO "$ERRMSG_TPM, testing TPM-bypass"
 
 	if [ "$DIGEST" = "$zero" ]; then
 		tst_res TPASS "bios boot aggregate is $zero"
@@ -151,57 +213,39 @@ test1()
 	[ -z "$TPM_VERSION" ] && test1_virtual_tpm || test1_hw_tpm
 }
 
-# Probably cleaner to programmatically read the PCR values directly
-# from the TPM, but that would require a TPM library. For now, use
-# the PCR values from /sys/devices.
-validate_pcr()
+test2()
 {
-	tst_res TINFO "verify PCR (Process Control Register)"
+	local hash pcr_aggregate
 
-	local dev_pcrs="$1"
-	local pcr hash aggregate_pcr
+	tst_res TINFO "verify PCR values"
 
-	aggregate_pcr="$(evmctl -v ima_measurement $BINARY_MEASUREMENTS 2>&1 | \
-		grep 'HW PCR-10:' | awk '{print $3}')"
-	if [ -z "$aggregate_pcr" ]; then
-		tst_res TFAIL "failed to get PCR-10"
-		return 1
+	if [ -z "$TPM_VERSION" ]; then
+		tst_brk TCONF "TMP version not detected ($ERRMSG_TPM)"
 	fi
 
-	while read line; do
-		pcr="$(echo $line | cut -d':' -f1)"
-		if [ "$pcr" = "PCR-10" ]; then
-			hash="$(echo $line | cut -d':' -f2 | awk '{ gsub (" ", "", $0); print tolower($0) }')"
-			[ "$hash" = "$aggregate_pcr" ]
-			return $?
-		fi
-	done < $dev_pcrs
-	return 1
-}
-
-test2()
-{
-	tst_res TINFO "verify PCR values"
-	tst_check_cmds evmctl || return
+	if [ "$ALGORITHM" = "sha1" ]; then
+		tst_check_cmds evmctl || return 1
+	fi
 
-	tst_res TINFO "evmctl version: $(evmctl --version)"
+	read_pcr_tpm$TPM_VERSION > pcr.txt
+	hash=$(grep "^PCR-10" pcr.txt | cut -d' ' -f2)
+	if [ -z "$hash" ]; then
+		tst_res TBROK "PCR-10 hash not found"
+		cat pcr.txt
+		return 1
+	fi
+	tst_res TINFO "real PCR-10: '$hash'"
 
-	local pcrs_path="/sys/class/tpm/tpm0/device/pcrs"
-	if [ -f "$pcrs_path" ]; then
-		tst_res TINFO "new PCRS path, evmctl >= 1.1 required"
-	else
-		pcrs_path="/sys/class/misc/tpm0/device/pcrs"
+	pcr_aggregate="$(get_pcr10_aggregate)"
+	if [ -z "$pcr_aggregate" ]; then
+		return
 	fi
+	tst_res TINFO "aggregate PCR-10: '$hash'"
 
-	if [ -f "$pcrs_path" ]; then
-		validate_pcr $pcrs_path
-		if [ $? -eq 0 ]; then
-			tst_res TPASS "aggregate PCR value matches real PCR value"
-		else
-			tst_res TFAIL "aggregate PCR value does not match real PCR value"
-		fi
+	if [ "$hash" = "$pcr_aggregate" ]; then
+		tst_res TPASS "aggregate PCR value matches real PCR value"
 	else
-		tst_res TCONF "TPM Hardware Support not enabled in kernel or no TPM chip found"
+		tst_res TFAIL "aggregate PCR value does not match real PCR value"
 	fi
 }
 
Signature · 1 line
2.28.0
maintainer
#8 lore raw

Re: [PATCH v3 4/4] ima_tpm.sh: Fix calculating PCR aggregate

Hi Petr,

Quoted 8 lines · Petr Vorel
testcases/kernel/security/integrity/ima/tests/ima_tpm.sh +8 -0
+get_pcr10_aggregate()
+{
+       local pcr
+
+       evmctl -v ima_measurement $BINARY_MEASUREMENTS > hash.txt 2>&1
+
+       pcr=$(grep -E "^($ALGORITHM: )*PCRAgg.*:" hash.txt \
+               | awk '{print $NF}')

testcases/kernel/security/integrity/ima/tests/ima_tpm.sh get_tpm_version() · line 165

Quoted 10 lines
testcases/kernel/security/integrity/ima/tests/ima_tpm.sh +10 -0
+
+       if [ -z "$pcr" ]; then
+               tst_res TFAIL "evmctl failed to get aggregate PCR-10"
+               cat hash.txt >&2
+               return
+       fi
+
+       echo "$pcr"
+}
+

I'm seeing the following output:

10 a528ab7a7096e0187aa5c154502f467a0f931873 ima-ng sha1:75bf81bc120313f6aa61430fad4a47afceea3e7c /usr/local/lib/libimaevm.so.2.0.0
Failed to match per TPM bank or SHA1 padded TPM digest(s).
errno: No such file or directory (2)
ima_tpm 2 TBROK: Test didn't report any results
ima_tpm 2 TINFO: SELinux enabled in enforcing mode, this may affect test results
ima_tpm 2 TINFO: it can be disabled with TST_DISABLE_SELINUX=1 (requires super/root)
ima_tpm 2 TINFO: install seinfo to find used SELinux profiles ima_tpm 2 TINFO: loaded SELinux profiles: none

SELinux is blamed enough for different things. Let's not add verifying the IMA measurement list. A more likely reason for failing to validate the measurement list is that it contains violations. Normally this is because the builtin "ima_policy=tcb" policy has not been replaced with a custom policy, based on LSM labels.

Test2 should fail when the measurement list contains violations, but it should retry validating the measurement list with the "--ignore- violations" option to provide additional context.

thanks,

Mimi

author
#9 lore raw

v3 cover TPM 2.0 fixes in IMA tests

In reply to Petr Vorel

Hi Mimi, Lakshmi,

sorry for late version. FYI Cyril is planning to release LTP tomorrow evening, thus To get it into this release require review and testing till tomorrow lunch or something.
Thus understand if you don't have time for it.

NOTE: crazy support for old versions was important to get support for older SLES versions.

Kind regards,
Petr

Quoted 19 lines

Hi,

few more fixes, mostly touching older kernels or evmctl versions. Changes in 3rd and 4th commit.

Kind regards,
Petr

Petr Vorel (4):
IMA: Move get_algorithm_digest(), set_digest_index() to ima_setup.sh
IMA: Rewrite ima_boot_aggregate.c to new API
ima_tpm.sh: Fix calculating boot aggregate
ima_tpm.sh: Fix calculating PCR aggregate

 .../integrity/ima/src/ima_boot_aggregate.c    | 113 ++++----
 .../integrity/ima/tests/ima_measurements.sh   |  62 +---
 .../security/integrity/ima/tests/ima_setup.sh |  70 +++++
 .../security/integrity/ima/tests/ima_tpm.sh   | 265 ++++++++++++++----
 4 files changed, 341 insertions(+), 169 deletions(-)
maintainer
#10 lore raw

v3 cover TPM 2.0 fixes in IMA tests

Hi Petr,

Quoted 9 lines · Petr Vorel

Hi Mimi, Lakshmi,

sorry for late version. FYI Cyril is planning to release LTP tomorrow evening, thus To get it into this release require review and testing till tomorrow lunch or something.
Thus understand if you don't have time for it.

NOTE: crazy support for old versions was important to get support for older SLES versions.

Thank you so much for updating the ima_tpm.sh test. Of all the comments, it would be nice to re-verify the measurement list with " --ignore-violations" to provide more context. Anyone running with just the ima_policy=tcb, would have violations. You should be able to test that yourself with the logs, PCRs, and directions, I sent you.

In terms of supporting the "ima" template, I think it would only be for old, existing systems, but then I doubt they would be running ltp. The "boot_aggregate" to "sha1" change works on a system with TPM 1.2.

Mimi

#11 lore raw
In reply to Petr Vorel
Quoted 14 lines · Petr Vorel

Hi,

few more fixes, mostly touching older kernels or evmctl versions. Changes in 3rd and 4th commit.

Kind regards,
Petr

Petr Vorel (4):
IMA: Move get_algorithm_digest(), set_digest_index() to ima_setup.sh
IMA: Rewrite ima_boot_aggregate.c to new API
ima_tpm.sh: Fix calculating boot aggregate
ima_tpm.sh: Fix calculating PCR aggregate

Quoted 9 lines
 .../integrity/ima/src/ima_boot_aggregate.c    | 113 ++++----
 .../integrity/ima/tests/ima_measurements.sh   |  62 +---
 .../security/integrity/ima/tests/ima_setup.sh |  70 +++++
 .../security/integrity/ima/tests/ima_tpm.sh   | 265 ++++++++++++++----
 4 files changed, 341 insertions(+), 169 deletions(-)
Signature · 1 line
2.28.0

Hi, is there something specific I should look at in this patch set?

/Jarkko

author
#12 lore raw

Hi Jarkko,

Quoted 1 line

Hi, is there something specific I should look at in this patch set?

I'm sorry to bother you with LTP specific code. Can you have a quick look if I didn't overlook anything obvious in reading PCR files (read_pcr_tpm*())?

I'm surprised that it's working on my TPM 2.0 which does not export /sys/kernel/security/tpm0/binary_bios_measurements (using evmctl).

Quoted 1 line

/Jarkko

Kind regards,
Petr

#13 lore raw
Quoted 9 lines · Petr Vorel

Hi Jarkko,

Quoted 1 line

Hi, is there something specific I should look at in this patch set?

I'm sorry to bother you with LTP specific code. Can you have a quick look if I didn't overlook anything obvious in reading PCR files (read_pcr_tpm*())?

I'm surprised that it's working on my TPM 2.0 which does not export /sys/kernel/security/tpm0/binary_bios_measurements (using evmctl).

Thank you, this was actually really important remark and reminder.

OK so I think James' patch is stuck because of me, i.e.

https://lore.kernel.org/linux-integrity/[email protected]/

I'm sorry about this. The final final conclusion is that the way it exports PCRs is just fine.

Can you test this version?

https://patchwork.kernel.org/patch/11759729/

I can then add reviewd-by and apply it and you don't have to do any sort of stupid hacks.

/Jarkkko

author
#14 lore raw

Hi Jarkko,

Quoted 11 lines · Petr Vorel
Quoted 9 lines

Hi Jarkko,

Quoted 1 line

Hi, is there something specific I should look at in this patch set?

I'm sorry to bother you with LTP specific code. Can you have a quick look if I didn't overlook anything obvious in reading PCR files (read_pcr_tpm*())?

I'm surprised that it's working on my TPM 2.0 which does not export /sys/kernel/security/tpm0/binary_bios_measurements (using evmctl).

Quoted 8 lines

Thank you, this was actually really important remark and reminder.

OK so I think James' patch is stuck because of me, i.e.

https://lore.kernel.org/linux-integrity/[email protected]/

I'm sorry about this. The final final conclusion is that the way it exports PCRs is just fine.

That's a great, thank you for going to upstream James' patch. James, thanks for implementing it!

Quoted 1 line

Can you test this version?

Sure, I'll test it next week.

Quoted 5 lines

https://patchwork.kernel.org/patch/11759729/

I can then add reviewd-by and apply it and you don't have to do any sort of stupid hacks.

I'll need to keep these hacks for older kernels, but it's great that there is a better solution.

Other thing: do you know anybody practically uses more TPM devices in single machine? I'm asking that I work with tpm0 in ima_tpm.sh, but maybe I should allow user to redefine it to choose different device (or even run tests for all available devices).

Kind regards,
Petr

Quoted 1 line

/Jarkkko

#15 lore raw
Quoted 7 lines · Petr Vorel

I'll need to keep these hacks for older kernels, but it's great that there is a better solution.

Other thing: do you know anybody practically uses more TPM devices in single machine? I'm asking that I work with tpm0 in ima_tpm.sh, but maybe I should allow user to redefine it to choose different device (or even run tests for all available devices).

You can create a proxy TPM device for a TPM emulator or a software TPM (e.g. could be an SGX enclave) by using ioctl interface /dev/vtpmx, provided by tpm_vtpm_proxy driver. QEMU provides a passthrough interface from TPM devices to the VM, which can be utilized for this.

This one I know at least.

Quoted 2 lines

Kind regards,
Petr

/Jarkko

author
#16 lore raw

Hi Jarkko,

can rely on /dev/tpm0 and /dev/tpmrm0 for TPM detection? i.e.:
/dev/tpmrm0 => TPM 2.0
/dev/tpm0 => both TPM 1.2 or 2.0
none of them => No TPM device

I'm trying to get reliably TPM version (1, 2 or no TPM) for various kernel versions (including very old ones like 3.x):

So far I have:

cat /sys/class/tpm/tpm0/tpm_version_major

[ -f /sys/class/tpm/tpm0/device/caps -o -f /sys/class/misc/tpm0/device/caps ] => TPM 1.2

[ ! -d /sys/class/tpm/tpm0/ -a ! -d /sys/class/misc/tpm0/ ] => no TPM

Then I grep

dmesg | grep -q 'activating TPM-bypass' => no TPM
dmesg | grep -q '1\.2 TPM (device-id' => TPM 1.2
dmesg | grep -q '2\.0 TPM (device-id' => TPM 2.0

But according to Mimi there are still some TPM 2.0 devices which does not export sysfs files.

And I'd prefer avoid using dmesg (not on some embedded targets and not sure if reliable for all TPM devices).

If I understand James's commit fdc915f7f719 ("tpm: expose spaces via a device link /dev/tpmrm<n>") from v4.12-rc1 correctly /dev/tpmrm0 is really for TPM 2.0. But how to detect it for v4.0 - v4.11 when /sys/class/tpm detection fails?
I haven't found anything obvious in drivers/char/tpm/tpm2-cmd.c from v4.0.

Thanks for info.

Kind regards,
Petr

#17 lore raw
Quoted 7 lines · Petr Vorel

Hi Jarkko,

can rely on /dev/tpm0 and /dev/tpmrm0 for TPM detection? i.e.:
/dev/tpmrm0 => TPM 2.0
/dev/tpm0 => both TPM 1.2 or 2.0
none of them => No TPM device

I think that should work as tpmrm0 is unconditionally available for TPM devices.

Since Linux v5.6 there has been a sysfs file called tpm_version_major available too.

/Jarkko

#18 lore raw
In reply to Petr Vorel
Quoted 3 lines · Petr Vorel

If I understand James's commit fdc915f7f719 ("tpm: expose spaces via a device link /dev/tpmrm<n>") from v4.12-rc1 correctly /dev/tpmrm0 is really for TPM 2.0. But how to detect it for v4.0 - v4.11 when /sys/class/tpm detection fails?

I'd send an idempotent TPM2 command to /dev/tpm0. I.e. if it works, it does not change the state. If it doesn't, you know that it is not TPM2 device.

See the kernel function tpm2_probe() as an example of this approach.

/Jarkko

#19 lore raw
Quoted 11 lines · Jarkko Sakkinen
Quoted 3 lines · Petr Vorel

If I understand James's commit fdc915f7f719 ("tpm: expose spaces via a device link /dev/tpmrm<n>") from v4.12-rc1 correctly /dev/tpmrm0 is really for TPM 2.0. But how to detect it for v4.0 - v4.11 when /sys/class/tpm detection fails?

I'd send an idempotent TPM2 command to /dev/tpm0. I.e. if it works, it does not change the state. If it doesn't, you know that it is not TPM2 device.

See the kernel function tpm2_probe() as an example of this approach.

As good candidate is getcapability, because it is guaranteed never to require authorization.