Skip to contentSkip to conversation

IMA: Verify measurement of certificates

PATCH Superseded Lachlan Sneff 4 participants 11 messages Jun 17, 2020 – Jul 14, 2020

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover IMA: Verify measurement of certificates

The IMA subsystem is capable of importing and measuring certificates. This set of patches adds tests for verifying that keys are imported and measured correctly.

Changelog:

v3
- Document requirements for running the ima key tests and provide resources for generating keys.

v2
- Un-linebreak a few strings
- Enforce that some commands are available before running
- Move compute_digest function to ima_setup.sh
- Fix file permissions on ima_key.sh
- Move IMA_POLICY variable to ima_setup.sh
- Add keycheck.policy datafile

v1
- The following patchsets should be applied in that order.
- Add tests that verify measurement of keys and importing certificates.

Lachlan Sneff (2):
IMA: Add a test to verify measurment of keys
IMA: Add a test to verify importing a certificate into keyring

 runtest/ima                                   |   1 +
 .../kernel/security/integrity/ima/README.md   |  21 ++++
 .../integrity/ima/datafiles/keycheck.policy   |   1 +
 .../security/integrity/ima/tests/ima_keys.sh  | 110 ++++++++++++++++++
 .../integrity/ima/tests/ima_measurements.sh   |  36 +-----
 .../integrity/ima/tests/ima_policy.sh         |   1 -
 .../security/integrity/ima/tests/ima_setup.sh |  35 ++++++
 7 files changed, 169 insertions(+), 36 deletions(-)
 create mode 100644 testcases/kernel/security/integrity/ima/datafiles/keycheck.policy
 create mode 100755 testcases/kernel/security/integrity/ima/tests/ima_keys.sh
Signature · 1 line
2.25.1
#1 lore raw

PATCH v3 1/2 IMA: Add a test to verify measurment of keys

Add a testcase that verifies that the IMA subsystem has correctly measured keys added to keyrings specified in the IMA policy file.

Additionally, add support for handling a new IMA template descriptor, namely ima-buf[1], in the IMA measurement tests.

[1]: https://www.kernel.org/doc/html/latest/security/IMA-templates.html#use

---
 runtest/ima                                   |  1 +
 .../integrity/ima/datafiles/keycheck.policy   |  1 +
 .../security/integrity/ima/tests/ima_keys.sh  | 67 +++++++++++++++++++
 .../integrity/ima/tests/ima_measurements.sh   | 36 +---------
 .../integrity/ima/tests/ima_policy.sh         |  1 -
 .../security/integrity/ima/tests/ima_setup.sh | 35 ++++++++++
 6 files changed, 105 insertions(+), 36 deletions(-)
 create mode 100644 testcases/kernel/security/integrity/ima/datafiles/keycheck.policy
 create mode 100755 testcases/kernel/security/integrity/ima/tests/ima_keys.sh
runtest/ima +1 -0
index f3ea88cf0..309d47420 100644
@@ -3,4 +3,5 @@ ima_measurements ima_measurements.sh
 ima_policy ima_policy.sh
 ima_tpm ima_tpm.sh
 ima_violations ima_violations.sh
+ima_keys ima_keys.sh
 evm_overlay evm_overlay.sh
testcases/kernel/security/integrity/ima/datafiles/keycheck.policy new file +1 -0
new file mode 100644
index 000000000..3f1934a3d
@@ -0,0 +1 @@
+measure func=KEY_CHECK keyrings=.ima|.evm|.builtin_trusted_keys|.blacklist template=ima-buf
testcases/kernel/security/integrity/ima/tests/ima_keys.sh new file +67 -0

67 lines not on this page · raw message

testcases/kernel/security/integrity/ima/tests/ima_measurements.sh +1 -35
index 54237d688..04d8e6353 100755
@@ -28,7 +28,7 @@ setup()
 	# parse digest index
 	# https://www.kernel.org/doc/html/latest/security/IMA-templates.html#use
 	case "$template" in
-	ima|ima-ng|ima-sig) DIGEST_INDEX=4 ;;
+	ima|ima-ng|ima-sig|ima-buf) DIGEST_INDEX=4 ;;
 	*)
 		# using ima_template_fmt kernel parameter
 		local IFS="|"
@@ -46,40 +46,6 @@ setup()
 		"Cannot find digest index (template: '$template')"
 }
 
-# TODO: find support for rmd128 rmd256 rmd320 wp256 wp384 tgr128 tgr160
-compute_digest()
-{
-	local algorithm="$1"
-	local file="$2"
-	local digest
-
-	digest="$(${algorithm}sum $file 2>/dev/null | cut -f1 -d ' ')"
-	if [ -n "$digest" ]; then
-		echo "$digest"
-		return 0
-	fi
-
-	digest="$(openssl $algorithm $file 2>/dev/null | cut -f2 -d ' ')"
-	if [ -n "$digest" ]; then
-		echo "$digest"
-		return 0
-	fi
-
-	# uncommon ciphers
-	local arg="$algorithm"
-	case "$algorithm" in
-	tgr192) arg="tiger" ;;
-	wp512) arg="whirlpool" ;;
-	esac
-
-	digest="$(rdigest --$arg $file 2>/dev/null | cut -f1 -d ' ')"
-	if [ -n "$digest" ]; then
-		echo "$digest"
-		return 0
-	fi
-	return 1
-}
-
 ima_check()
 {
 	local delimiter=':'
testcases/kernel/security/integrity/ima/tests/ima_policy.sh +0 -1
index 6286277b4..244cf081d 100755
@@ -23,7 +23,6 @@ check_policy_writable()
 
 setup()
 {
-	IMA_POLICY="$IMA_DIR/policy"
 	check_policy_writable
 
 	VALID_POLICY="$TST_DATAROOT/measure.policy"
testcases/kernel/security/integrity/ima/tests/ima_setup.sh +35 -0
index 58a12eda3..8ae477c1c 100644
@@ -20,6 +20,40 @@ SYSFS="/sys"
 UMOUNT=
 TST_FS_TYPE="ext3"
 
+# TODO: find support for rmd128 rmd256 rmd320 wp256 wp384 tgr128 tgr160
+compute_digest()
+{
+	local algorithm="$1"
+	local file="$2"
+	local digest
+
+	digest="$(${algorithm}sum $file 2>/dev/null | cut -f1 -d ' ')"
+	if [ -n "$digest" ]; then
+		echo "$digest"
+		return 0
+	fi
+
+	digest="$(openssl $algorithm $file 2>/dev/null | cut -f2 -d ' ')"
+	if [ -n "$digest" ]; then
+		echo "$digest"
+		return 0
+	fi
+
+	# uncommon ciphers
+	local arg="$algorithm"
+	case "$algorithm" in
+	tgr192) arg="tiger" ;;
+	wp512) arg="whirlpool" ;;
+	esac
+
+	digest="$(rdigest --$arg $file 2>/dev/null | cut -f1 -d ' ')"
+	if [ -n "$digest" ]; then
+		echo "$digest"
+		return 0
+	fi
+	return 1
+}
+
 check_ima_policy()
 {
 	local policy="$1"
@@ -85,6 +119,7 @@ ima_setup()
 	[ -d "$IMA_DIR" ] || tst_brk TCONF "IMA not enabled in kernel"
 	ASCII_MEASUREMENTS="$IMA_DIR/ascii_runtime_measurements"
 	BINARY_MEASUREMENTS="$IMA_DIR/binary_runtime_measurements"
+	IMA_POLICY="$IMA_DIR/policy"
 
 	print_ima_config
 
Signature · 1 line
2.25.1
#2 lore raw

PATCH v3 1/2 IMA: Add a test to verify measurment of keys

Hi Lachlan,

testcases/kernel/security/integrity/ima/tests/ima_keys.sh line 8

Quoted 10 lines

+++ b/testcases/kernel/security/integrity/ima/tests/ima_keys.sh

testcases/kernel/security/integrity/ima/tests/ima_keys.sh new file +8 -0
@@ -0,0 +1,67 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) 2020 Microsoft Corporation
+# Author: Lachlan Sneff <[email protected]>
+#
+# Verify that keys are measured correctly based on policy.
+
+TST_NEEDS_CMDS="grep mktemp cut sed tr"

This is already a dependency for tst_test.sh, but it does not harm to have it here (in case we remove the dependency from tst_test.sh).

testcases/kernel/security/integrity/ima/tests/ima_keys.sh line 18

Quoted 10 lines
testcases/kernel/security/integrity/ima/tests/ima_keys.sh new file +10 -0
+TST_CNT=1
+TST_NEEDS_DEVICE=1
+
+. ima_setup.sh
+
+# Based on https://lkml.org/lkml/2019/12/13/564.
+# (450d0fd51564 - "IMA: Call workqueue functions to measure queued keys")
+test1()
+{
+	local keyrings keycheck_line templates test_file=$(mktemp)

Do we need mktemp? Can't it be just:
local keyrings keycheck_line templates test_file="file.txt"

...

testcases/kernel/security/integrity/ima/tests/ima_keys.sh line 52

Quoted 1 line
testcases/kernel/security/integrity/ima/tests/ima_keys.sh new file +1 -0
+		echo "$line" | cut -d' ' -f6 | xxd -r -p > $test_file

Because you later just overwrite the file (simplicity).

I also try to keep shell dependencies low so it's possible to run it with in dracut initramfs with rapido [1] without too many dependencies (although mktemp is already tst_test.sh dependency).

testcases/kernel/security/integrity/ima/tests/ima_keys.sh line 62

Quoted 10 lines
testcases/kernel/security/integrity/ima/tests/ima_keys.sh new file +10 -0
+
+		expected_digest="$(compute_digest $algorithm $test_file)" || \
+			tst_brk TCONF "cannot compute digest for $algorithm"
+
+		if [ "$digest" != "$expected_digest" ]; then
+			tst_res TFAIL "incorrect digest was found for the ($keyring) keyring"
+		fi
+	done
+
+	rm $test_file

Again, IMHO no need to delete the file.

[1] https://github.com/rapido-linux/rapido

Review line 31

Quoted 2 lines
(quoted diff) +2 -0
+
+	tst_res TPASS "specified keyrings were measured correctly"

This TPASS will be called even if there is previous TFAIL "incorrect digest was found for the ($keyring) keyring". We should either exit testing with return, or have variable to detect failure and not call this (not sure what makes more sense).

Kind regards,
Petr

#3 lore raw

PATCH v3 1/2 IMA: Add a test to verify measurment of keys

In reply to Lachlan Sneff

Hi Lachian,

Quoted 22 lines
testcases/kernel/security/integrity/ima/tests/ima_keys.sh new file +22 -0
+
+# Based on https://lkml.org/lkml/2019/12/13/564.
+# (450d0fd51564 - "IMA: Call workqueue functions to measure queued keys")
+test1()
+{
+	local keyrings keycheck_line templates test_file=$(mktemp)
+
+	tst_res TINFO "verifying key measurement for keyrings and templates specified in IMA policy file"
+
+	[ -f $IMA_POLICY ] || tst_brk TCONF "missing $IMA_POLICY"
+
+	[ -r $IMA_POLICY ] || tst_brk TCONF "cannot read IMA policy (CONFIG_IMA_READ_POLICY=y required)"
+
+	keycheck_line=$(grep "func=KEY_CHECK" $IMA_POLICY)
+	if [ -z "$keycheck_line" ]; then
+		tst_brk TCONF "ima policy does not specify \"func=KEY_CHECK\""
+	fi
+
+	if echo "$keycheck_line" | grep -q "*keyrings*"; then
+		tst_brk TCONF "ima policy does not specify a keyrings to check"
+	fi
+

testcases/kernel/security/integrity/ima/tests/ima_keys.sh line 44

Quoted 10 lines
testcases/kernel/security/integrity/ima/tests/ima_keys.sh new file +10 -0
+	keyrings=$(echo "$keycheck_line" | tr " " "\n" | grep "keyrings" | \
+		sed "s/\./\\\./g" | cut -d'=' -f2)
+	if [ -z "$keyrings" ]; then
+		tst_brk TCONF "ima policy has a keyring key-value specifier, but no specified keyrings"
+	fi
+
+	templates=$(echo "$keycheck_line" | tr " " "\n" | grep "template" | \
+		cut -d'=' -f2)
+
+	grep -E "($templates)*($keyrings)" $ASCII_MEASUREMENTS | while read line

Probably because I have multiple KEY_CHECK rules, this is failing:

grep: Unmatched ( or \(

And then it continues merrily alongs its way.

ima_keys 1 TPASS: specified keyrings were measured correctly ima_keys 2 TCONF: missing /etc/keys/x509_ima.der

Mimi

Quoted 21 lines
testcases/kernel/security/integrity/ima/tests/ima_keys.sh new file +21 -0
+	do
+		local digest expected_digest algorithm
+
+		digest=$(echo "$line" | cut -d' ' -f4 | cut -d':' -f2)
+		algorithm=$(echo "$line" | cut -d' ' -f4 | cut -d':' -f1)
+		keyring=$(echo "$line" | cut -d' ' -f5)
+
+		echo "$line" | cut -d' ' -f6 | xxd -r -p > $test_file
+
+		expected_digest="$(compute_digest $algorithm $test_file)" || \
+			tst_brk TCONF "cannot compute digest for $algorithm"
+
+		if [ "$digest" != "$expected_digest" ]; then
+			tst_res TFAIL "incorrect digest was found for the ($keyring) keyring"
+		fi
+	done
+
+	rm $test_file
+
+	tst_res TPASS "specified keyrings were measured correctly"
+}
#4 lore raw

PATCH v3 1/2 IMA: Add a test to verify measurment of keys

[Resending due to mailer issues]

Quoted 67 lines · Mimi Zohar

Hi Lachian,

Quoted 22 lines
testcases/kernel/security/integrity/ima/tests/ima_keys.sh new file +22 -0
+
+# Based on https://lkml.org/lkml/2019/12/13/564.
+# (450d0fd51564 - "IMA: Call workqueue functions to measure queued keys")
+test1()
+{
+	local keyrings keycheck_line templates test_file=$(mktemp)
+
+	tst_res TINFO "verifying key measurement for keyrings and templates specified in IMA policy file"
+
+	[ -f $IMA_POLICY ] || tst_brk TCONF "missing $IMA_POLICY"
+
+	[ -r $IMA_POLICY ] || tst_brk TCONF "cannot read IMA policy (CONFIG_IMA_READ_POLICY=y required)"
+
+	keycheck_line=$(grep "func=KEY_CHECK" $IMA_POLICY)
+	if [ -z "$keycheck_line" ]; then
+		tst_brk TCONF "ima policy does not specify \"func=KEY_CHECK\""
+	fi
+
+	if echo "$keycheck_line" | grep -q "*keyrings*"; then
+		tst_brk TCONF "ima policy does not specify a keyrings to check"
+	fi
+

testcases/kernel/security/integrity/ima/tests/ima_keys.sh line 44

Quoted 10 lines
testcases/kernel/security/integrity/ima/tests/ima_keys.sh new file +10 -0
+	keyrings=$(echo "$keycheck_line" | tr " " "\n" | grep "keyrings" | \
+		sed "s/\./\\\./g" | cut -d'=' -f2)
+	if [ -z "$keyrings" ]; then
+		tst_brk TCONF "ima policy has a keyring key-value specifier, but no specified keyrings"
+	fi
+
+	templates=$(echo "$keycheck_line" | tr " " "\n" | grep "template" | \
+		cut -d'=' -f2)
+
+	grep -E "($templates)*($keyrings)" $ASCII_MEASUREMENTS | while read line

Probably because I have multiple KEY_CHECK rules, this is failing:

grep: Unmatched ( or \(

And then it continues merrily alongs its way.

ima_keys 1 TPASS: specified keyrings were measured correctly ima_keys 2 TCONF: missing /etc/keys/x509_ima.der

Mimi

Quoted 21 lines
testcases/kernel/security/integrity/ima/tests/ima_keys.sh new file +21 -0
+	do
+		local digest expected_digest algorithm
+
+		digest=$(echo "$line" | cut -d' ' -f4 | cut -d':' -f2)
+		algorithm=$(echo "$line" | cut -d' ' -f4 | cut -d':' -f1)
+		keyring=$(echo "$line" | cut -d' ' -f5)
+
+		echo "$line" | cut -d' ' -f6 | xxd -r -p > $test_file
+
+		expected_digest="$(compute_digest $algorithm $test_file)" || \
+			tst_brk TCONF "cannot compute digest for $algorithm"
+
+		if [ "$digest" != "$expected_digest" ]; then
+			tst_res TFAIL "incorrect digest was found for the ($keyring) keyring"
+		fi
+	done
+
+	rm $test_file
+
+	tst_res TPASS "specified keyrings were measured correctly"
+}
#5 lore raw

PATCH v3 2/2 IMA: Add a test to verify importing a certificate into keyring

In reply to Lachlan Sneff

Add an IMA measurement test that verifies that an x509 certificate can be imported into the .ima keyring and measured correctly.

---
 .../kernel/security/integrity/ima/README.md   | 21 +++++++++
 .../security/integrity/ima/tests/ima_keys.sh  | 47 ++++++++++++++++++-
 2 files changed, 66 insertions(+), 2 deletions(-)
testcases/kernel/security/integrity/ima/README.md +21 -0
index 16a1f48c3..e41f7b570 100644
@@ -16,6 +16,27 @@ CONFIG_INTEGRITY=y
 CONFIG_IMA=y
 ```
 
+IMA Key Import test
+-------------
+
+`ima_keys.sh` requires an x509 key to be generated and placed
+at `/etc/keys/x509_ima.der`.
+
+The x509 public key key must be signed by the private key you generate.
+Follow these instructions:
+https://manpages.ubuntu.com/manpages/disco/man1/evmctl.1.html#generate%20trusted%20keys.
+
+The test cannot be set-up automatically because the kernel must be built
+with one of the keys you generate.
+
+As well as what's required for the IMA tests, the following are also required
+in the kernel configuration:
+```
+CONFIG_IMA_READ_POLICY=y
+CONFIG_SYSTEM_TRUSTED_KEYRING=y
+CONFIG_SYSTEM_TRUSTED_KEYS="/etc/keys/ima-local-ca.pem"
+```
+
 EVM tests
 ---------
 
testcases/kernel/security/integrity/ima/tests/ima_keys.sh +45 -2

59 lines not on this page · raw message

Signature · 1 line
2.25.1
#6 lore raw

PATCH v3 2/2 IMA: Add a test to verify importing a certificate into keyring

Hi Lachlan,

LGTM, I'd just like to do some tests. That's what prevents me from merging (my notes below are just nits, I'll fix them before merging).
@Mimi: would you have time to have look into these tests?

Quoted 14 lines

Add an IMA measurement test that verifies that an x509 certificate can be imported into the .ima keyring and measured correctly.

---
 .../kernel/security/integrity/ima/README.md   | 21 +++++++++
 .../security/integrity/ima/tests/ima_keys.sh  | 47 ++++++++++++++++++-
 2 files changed, 66 insertions(+), 2 deletions(-)
testcases/kernel/security/integrity/ima/README.md +0 -0
index 16a1f48c3..e41f7b570 100644

testcases/kernel/security/integrity/ima/README.md CONFIG_INTEGRITY=y · line 17

Quoted 2 lines
testcases/kernel/security/integrity/ima/README.md +0 -0
@@ -16,6 +16,27 @@ CONFIG_INTEGRITY=y
 CONFIG_IMA=y
 ```

Thanks for a docs, I'll move it to the first commit.

testcases/kernel/security/integrity/ima/README.md CONFIG_INTEGRITY=y · line 19

Quoted 1 line
testcases/kernel/security/integrity/ima/README.md +1 -0
+IMA Key Import test

IMA Key Import tests

testcases/kernel/security/integrity/ima/README.md CONFIG_INTEGRITY=y · line 23

Quoted 4 lines
testcases/kernel/security/integrity/ima/README.md +4 -0
+-------------
+
+`ima_keys.sh` requires an x509 key to be generated and placed
+at `/etc/keys/x509_ima.der`.

ima_keys.sh requires an x509 public key to be generated and placed

testcases/kernel/security/integrity/ima/README.md CONFIG_INTEGRITY=y · line 27

Quoted 6 lines
testcases/kernel/security/integrity/ima/README.md +5 -0
+at `/etc/keys/x509_ima.der`.
 
+
+The x509 public key key must be signed by the private key you generate.
+Follow these instructions:
+https://manpages.ubuntu.com/manpages/disco/man1/evmctl.1.html#generate%20trusted%20keys.

I was thinking to use non-distro link:
https://www.mankier.com/1/evmctl#Generate_Trusted_Keys
as Ubuntu docs is tied to certain evmctl version, but on the other hand it document what you used when wrote tests. And Ubuntu URL is probably is probably safer to use (mankier.com can vanish in the future). Thus keep this one.

Quoted 66 lines
testcases/kernel/security/integrity/ima/README.md +12 -0
+
+The test cannot be set-up automatically because the kernel must be built
+with one of the keys you generate.
+
+As well as what's required for the IMA tests, the following are also required
+in the kernel configuration:
+```
+CONFIG_IMA_READ_POLICY=y
+CONFIG_SYSTEM_TRUSTED_KEYRING=y
+CONFIG_SYSTEM_TRUSTED_KEYS="/etc/keys/ima-local-ca.pem"
+```
+
 EVM tests
 ---------
testcases/kernel/security/integrity/ima/tests/ima_keys.sh +33 -2
index 2b5324dbf..1d9824aba 100755
@@ -5,10 +5,12 @@
 
 # Verify that keys are measured correctly based on policy.
 
-TST_NEEDS_CMDS="grep mktemp cut sed tr"
-TST_CNT=1
+TST_NEEDS_CMDS="grep mktemp cut sed tr xxd keyctl evmctl openssl cmp"
+TST_CNT=2
 TST_NEEDS_DEVICE=1
 
+CERT_FILE="${CERT_FILE:-/etc/keys/x509_ima.der}"
+
 . ima_setup.sh
 
 # Based on https://lkml.org/lkml/2019/12/13/564.
@@ -64,4 +66,45 @@ test1()
 	tst_res TPASS "specified keyrings were measured correctly"
 }
 
+
+# Test that a cert can be imported into the ".ima" keyring correctly.
+test2() {
+	local keyring_id key_id test_file=$(mktemp)
+
+	[ -f $CERT_FILE ] || tst_brk TCONF "missing $CERT_FILE"
+
+	if ! openssl x509 -in $CERT_FILE -inform der > /dev/null; then
+		tst_brk TCONF "The suppled cert file ($CERT_FILE) is not a valid x509 certificate"
+	fi
+
+	tst_res TINFO "adding a cert to the .ima keyring ($CERT_FILE)"
+
+	keyring_id=$(keyctl show %:.ima | sed -n 2p | \
+		sed 's/^[[:space:]]*//' | cut -d' ' -f1) || \
+		tst_btk TCONF "unable to retrieve .ima keyring id"
+
+	if ! tst_is_num	"$keyring_id"; then
+		tst_brk TCONF "unable to parse keyring id from keyring"
+	fi
+
+	evmctl import $CERT_FILE "$keyring_id" > /dev/null || \
+		tst_brk TCONF "unable to import a cert into the .ima keyring"
+
+	grep -F ".ima" "$ASCII_MEASUREMENTS" | tail -n1 | cut -d' ' -f6 | \
+		xxd -r -p > $test_file || \
+		tst_brk TCONF "cert not found in ascii_runtime_measurements log"
+
+	if ! openssl x509 -in $test_file -inform der > /dev/null; then

testcases/kernel/security/integrity/ima/tests/ima_keys.sh test1() · line 107

Quoted 10 lines
testcases/kernel/security/integrity/ima/tests/ima_keys.sh +10 -0
+		tst_brk TCONF "The cert logged in ascii_runtime_measurements is not a valid x509 certificate"
+	fi
+
+	if cmp -s "$test_file" $CERT_FILE; then
+		tst_res TPASS "logged cert matches original cert"
+	else
+		tst_res TFAIL "logged cert does not match original cert"
+	fi
+
+	rm $test_file

I guess you can avoid deleting this file. There is automatic cleanup of the test directory and even if the test is run with -i (number of iterations), it'll be unique as it's using using mktemp.

testcases/kernel/security/integrity/ima/tests/ima_keys.sh test1() · line 110

Quoted 3 lines
testcases/kernel/security/integrity/ima/tests/ima_keys.sh +2 -0
+}
+
 tst_run

Kind regards,
Petr

#7 lore raw

PATCH v3 2/2 IMA: Add a test to verify importing a certificate into keyring

In reply to Lachlan Sneff

Hi Lachlan,

Quoted 2 lines · Lachlan Sneff

Add an IMA measurement test that verifies that an x509 certificate can be imported into the .ima keyring and measured correctly.

Please expand this, explaining that the x509 certificate needs to be signed by a key on one of the trusted keyrings.

Once there is a reliable way of adding a key to the IMA keyring, this opens up a lot of other testing possibilities.

Quoted 12 lines
---
 .../kernel/security/integrity/ima/README.md   | 21 +++++++++
 .../security/integrity/ima/tests/ima_keys.sh  | 47 ++++++++++++++++++-
 2 files changed, 66 insertions(+), 2 deletions(-)
testcases/kernel/security/integrity/ima/README.md +0 -0
index 16a1f48c3..e41f7b570 100644

testcases/kernel/security/integrity/ima/README.md CONFIG_INTEGRITY=y · line 23

Quoted 8 lines
testcases/kernel/security/integrity/ima/README.md +5 -0
@@ -16,6 +16,27 @@ CONFIG_INTEGRITY=y
 CONFIG_IMA=y
 ```
 
+IMA Key Import test
+-------------
+
+`ima_keys.sh` requires an x509 key to be generated and placed
+at `/etc/keys/x509_ima.der`.

The filename "/etc/keys/x509_ima.der" is configurable. ?It's based on CONFIG_IMA_X509_PATH Kconfig option. ?Perhaps extract it from the running kernel's Kconfig?

testcases/kernel/security/integrity/ima/README.md CONFIG_INTEGRITY=y · line 30

Quoted 7 lines
testcases/kernel/security/integrity/ima/README.md +7 -0
+
+The x509 public key key must be signed by the private key you generate.
+Follow these instructions:
+https://manpages.ubuntu.com/manpages/disco/man1/evmctl.1.html#generate%20trusted%20keys.
+
+The test cannot be set-up automatically because the kernel must be built
+with one of the keys you generate.

Please reword this to convey that the public key must be built into the kernel and loaded onto a trusted keyring (eg. .builtin_trusted_keys, .secondary_trusted_keyring)

Quoted 41 lines
testcases/kernel/security/integrity/ima/README.md +9 -0
+
+As well as what's required for the IMA tests, the following are also required
+in the kernel configuration:
+```
+CONFIG_IMA_READ_POLICY=y
+CONFIG_SYSTEM_TRUSTED_KEYRING=y
+CONFIG_SYSTEM_TRUSTED_KEYS="/etc/keys/ima-local-ca.pem"
+```
+
 EVM tests
 ---------
 
testcases/kernel/security/integrity/ima/tests/ima_keys.sh +10 -2
index 2b5324dbf..1d9824aba 100755
@@ -5,10 +5,12 @@
 #
 # Verify that keys are measured correctly based on policy.
 
-TST_NEEDS_CMDS="grep mktemp cut sed tr"
-TST_CNT=1
+TST_NEEDS_CMDS="grep mktemp cut sed tr xxd keyctl evmctl openssl cmp"
+TST_CNT=2
 TST_NEEDS_DEVICE=1
 
+CERT_FILE="${CERT_FILE:-/etc/keys/x509_ima.der}"
+
 . ima_setup.sh
 
 # Based on https://lkml.org/lkml/2019/12/13/564.
@@ -64,4 +66,45 @@ test1()
 	tst_res TPASS "specified keyrings were measured correctly"
 }
 
+
+# Test that a cert can be imported into the ".ima" keyring correctly.
+test2() {
+	local keyring_id key_id test_file=$(mktemp)
+
+	[ -f $CERT_FILE ] || tst_brk TCONF "missing $CERT_FILE"

testcases/kernel/security/integrity/ima/tests/ima_keys.sh test1() · line 84

Quoted 10 lines
testcases/kernel/security/integrity/ima/tests/ima_keys.sh +10 -0
+
+	if ! openssl x509 -in $CERT_FILE -inform der > /dev/null; then
+		tst_brk TCONF "The suppled cert file ($CERT_FILE) is not a valid x509 certificate"
+	fi
+
+	tst_res TINFO "adding a cert to the .ima keyring ($CERT_FILE)"
+
+	keyring_id=$(keyctl show %:.ima | sed -n 2p | \
+		sed 's/^[[:space:]]*//' | cut -d' ' -f1) || \
+		tst_btk TCONF "unable to retrieve .ima keyring id"

Using "keyctl describe" returns the keyring id as the first token, making it simpler to parse.

Mimi

Quoted 26 lines
testcases/kernel/security/integrity/ima/tests/ima_keys.sh +25 -0
+
+	if ! tst_is_num	"$keyring_id"; then
+		tst_brk TCONF "unable to parse keyring id from keyring"
+	fi
+
+	evmctl import $CERT_FILE "$keyring_id" > /dev/null || \
+		tst_brk TCONF "unable to import a cert into the .ima keyring"
+
+	grep -F ".ima" "$ASCII_MEASUREMENTS" | tail -n1 | cut -d' ' -f6 | \
+		xxd -r -p > $test_file || \
+		tst_brk TCONF "cert not found in ascii_runtime_measurements log"
+
+	if ! openssl x509 -in $test_file -inform der > /dev/null; then
+		tst_brk TCONF "The cert logged in ascii_runtime_measurements is not a valid x509 certificate"
+	fi
+
+	if cmp -s "$test_file" $CERT_FILE; then
+		tst_res TPASS "logged cert matches original cert"
+	else
+		tst_res TFAIL "logged cert does not match original cert"
+	fi
+
+	rm $test_file
+}
+
 tst_run
#8 lore raw

PATCH v3 2/2 IMA: Add a test to verify importing a certificate into keyring

Thank you for the review, Mimi!

Quoted 10 lines · Mimi Zohar

Hi Lachlan,

Quoted 2 lines · Lachlan Sneff

Add an IMA measurement test that verifies that an x509 certificate can be imported into the .ima keyring and measured correctly.

Please expand this, explaining that the x509 certificate needs to be signed by a key on one of the trusted keyrings.

Once there is a reliable way of adding a key to the IMA keyring, this opens up a lot of other testing possibilities.

This is a great idea. I definitely wasn't clear enough here.

Quoted 14 lines
Quoted 14 lines
---
  .../kernel/security/integrity/ima/README.md   | 21 +++++++++
  .../security/integrity/ima/tests/ima_keys.sh  | 47 ++++++++++++++++++-
  2 files changed, 66 insertions(+), 2 deletions(-)
testcases/kernel/security/integrity/ima/README.md +0 -0
index 16a1f48c3..e41f7b570 100644
@@ -16,6 +16,27 @@ CONFIG_INTEGRITY=y
  CONFIG_IMA=y
  ```
 
Quoted 8 lines

testcases/kernel/security/integrity/ima/README.md CONFIG_INTEGRITY=y · line 23

Quoted 5 lines
testcases/kernel/security/integrity/ima/README.md +5 -0
+IMA Key Import test
+-------------
+
+`ima_keys.sh` requires an x509 key to be generated and placed
+at `/etc/keys/x509_ima.der`.

The filename "/etc/keys/x509_ima.der" is configurable. ?It's based on CONFIG_IMA_X509_PATH Kconfig option. ?Perhaps extract it from the running kernel's Kconfig?

I didn't think pulling it from the kernel config. Will try this. I assume grep "..." /boot/config-$(uname -r) is the right way to grab a line from the config?

Quoted 10 lines

testcases/kernel/security/integrity/ima/README.md CONFIG_INTEGRITY=y · line 30

Quoted 7 lines
testcases/kernel/security/integrity/ima/README.md +7 -0
+
+The x509 public key key must be signed by the private key you generate.
+Follow these instructions:
+https://manpages.ubuntu.com/manpages/disco/man1/evmctl.1.html#generate%20trusted%20keys.
+
+The test cannot be set-up automatically because the kernel must be built
+with one of the keys you generate.

Please reword this to convey that the public key must be built into the kernel and loaded onto a trusted keyring (eg. .builtin_trusted_keys, .secondary_trusted_keyring)

Sounds good.

Quoted 45 lines
Quoted 45 lines
testcases/kernel/security/integrity/ima/README.md +9 -0
+
+As well as what's required for the IMA tests, the following are also required
+in the kernel configuration:
+```
+CONFIG_IMA_READ_POLICY=y
+CONFIG_SYSTEM_TRUSTED_KEYRING=y
+CONFIG_SYSTEM_TRUSTED_KEYS="/etc/keys/ima-local-ca.pem"
+```
+
  EVM tests
  ---------
 
testcases/kernel/security/integrity/ima/tests/ima_keys.sh +14 -2
index 2b5324dbf..1d9824aba 100755
@@ -5,10 +5,12 @@
  #
  # Verify that keys are measured correctly based on policy.
 
-TST_NEEDS_CMDS="grep mktemp cut sed tr"
-TST_CNT=1
+TST_NEEDS_CMDS="grep mktemp cut sed tr xxd keyctl evmctl openssl cmp"
+TST_CNT=2
  TST_NEEDS_DEVICE=1
 
+CERT_FILE="${CERT_FILE:-/etc/keys/x509_ima.der}"
+
  . ima_setup.sh
 
  # Based on https://lkml.org/lkml/2019/12/13/564.
@@ -64,4 +66,45 @@ test1()
  	tst_res TPASS "specified keyrings were measured correctly"
  }
 
+
+# Test that a cert can be imported into the ".ima" keyring correctly.
+test2() {
+	local keyring_id key_id test_file=$(mktemp)
+
+	[ -f $CERT_FILE ] || tst_brk TCONF "missing $CERT_FILE"
+
+	if ! openssl x509 -in $CERT_FILE -inform der > /dev/null; then
+		tst_brk TCONF "The suppled cert file ($CERT_FILE) is not a valid x509 certificate"
+	fi
Quoted 8 lines

testcases/kernel/security/integrity/ima/tests/ima_keys.sh test1() · line 84

Quoted 6 lines
testcases/kernel/security/integrity/ima/tests/ima_keys.sh +6 -0
+
+	tst_res TINFO "adding a cert to the .ima keyring ($CERT_FILE)"
+
+	keyring_id=$(keyctl show %:.ima | sed -n 2p | \
+		sed 's/^[[:space:]]*//' | cut -d' ' -f1) || \
+		tst_btk TCONF "unable to retrieve .ima keyring id"

Using "keyctl describe" returns the keyring id as the first token, making it simpler to parse.

Didn't realize this, will simplify the code here.

Quoted 2 lines

Mimi

Thanks again! Will get a patchset out with the changes asap.

Quoted 27 lines
Quoted 27 lines
testcases/kernel/security/integrity/ima/tests/ima_keys.sh +25 -0
+
+	if ! tst_is_num	"$keyring_id"; then
+		tst_brk TCONF "unable to parse keyring id from keyring"
+	fi
+
+	evmctl import $CERT_FILE "$keyring_id" > /dev/null || \
+		tst_brk TCONF "unable to import a cert into the .ima keyring"
+
+	grep -F ".ima" "$ASCII_MEASUREMENTS" | tail -n1 | cut -d' ' -f6 | \
+		xxd -r -p > $test_file || \
+		tst_brk TCONF "cert not found in ascii_runtime_measurements log"
+
+	if ! openssl x509 -in $test_file -inform der > /dev/null; then
+		tst_brk TCONF "The cert logged in ascii_runtime_measurements is not a valid x509 certificate"
+	fi
+
+	if cmp -s "$test_file" $CERT_FILE; then
+		tst_res TPASS "logged cert matches original cert"
+	else
+		tst_res TFAIL "logged cert does not match original cert"
+	fi
+
+	rm $test_file
+}
+
  tst_run
#9 lore raw

PATCH v3 2/2 IMA: Add a test to verify importing a certificate into keyring

Quoted 17 lines · Lachlan Sneff
Quoted 9 lines
Quoted 9 lines
testcases/kernel/security/integrity/ima/README.md +0 -0
index 16a1f48c3..e41f7b570 100644
@@ -16,6 +16,27 @@ CONFIG_INTEGRITY=y
  CONFIG_IMA=y
  ```
 
Quoted 8 lines

testcases/kernel/security/integrity/ima/README.md CONFIG_INTEGRITY=y · line 23

Quoted 5 lines
testcases/kernel/security/integrity/ima/README.md +5 -0
+IMA Key Import test
+-------------
+
+`ima_keys.sh` requires an x509 key to be generated and placed
+at `/etc/keys/x509_ima.der`.

The filename "/etc/keys/x509_ima.der" is configurable. ?It's based on CONFIG_IMA_X509_PATH Kconfig option. ?Perhaps extract it from the running kernel's Kconfig?

Quoted 3 lines

I didn't think pulling it from the kernel config. Will try this. I assume grep "..." /boot/config-$(uname -r) is the right way to grab a line from the config?

Try using scripts/extract-ikconfig.

Mimi

#10 lore raw

PATCH v3 2/2 IMA: Add a test to verify importing a certificate into keyring

Hi Mimi, Lachlan,

Quoted 10 lines
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

+ima_keys.sh requires an x509 key to be generated and placed +at /etc/keys/x509_ima.der.

The filename "/etc/keys/x509_ima.der" is configurable. ?It's based on CONFIG_IMA_X509_PATH Kconfig option. ?Perhaps extract it from the running kernel's Kconfig?

I didn't think pulling it from the kernel config. Will try this. I assume grep "..." /boot/config-$(uname -r) is the right way to grab a line from the config?

Try using scripts/extract-ikconfig.

For now I'd just try to grep /boot/config-$(uname -r), but allow to run the test with the default value if kconfig not presented / readable (when running without root).

I'm not sure if extract-ikconfig as external dependency would be suitable for LTP (understand it's great for kselftest as it's already presented).

BTW there is a ticket for adding kernel config related helpers into the LTP shell API [1], I'll also note extract-ikconfig there.

LTP refused for long time working with kernel config, because it it's requirement meant that SUT without it could not be tested. Always try to not make kernel config as hard dependency (various embedded or old android will be disabled; some linux distros require root for reading the config). Design in [1] also suggest to have possibility to run the test even without config.

[1] https://github.com/linux-test-project/ltp/issues/700

Quoted 1 line

Mimi

Kind regards,
Petr