IMA: Key Measurement + Certificate Measurement Tests
v2 cover IMA: Key Measurement + Certificate Measurement Tests
Changeslog:
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
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 + .../integrity/ima/datafiles/keycheck.policy | 1 + .../security/integrity/ima/tests/ima_keys.sh | 106 ++++++++++++++++++ .../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, 144 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
PATCH v2 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 | 65 +++++++++++++++++++ .../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, 103 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
@@ -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
@@ -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 +65 -0
65 lines not on this page · raw message
testcases/kernel/security/integrity/ima/tests/ima_measurements.sh +1 -35
@@ -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
@@ -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
@@ -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
PATCH v2 1/2 IMA: Add a test to verify measurment of keys
Hi Lachlan,
..
testcases/kernel/security/integrity/ima/datafiles/keycheck.policy line 1
Quoted 3 lines
+++ b/testcases/kernel/security/integrity/ima/datafiles/keycheck.policy
testcases/kernel/security/integrity/ima/datafiles/keycheck.policy new file +1
-0@@ -0,0 +1 @@ +measure func=KEY_CHECK keyrings=.ima|.evm|.builtin_trusted_keys|.blacklist template=ima-buf
Thanks for this!
You don't use it, but that's ok, I'll add that policy handling myself after merging. I have some notes about documentation / setup for both commits.
It would be nice to mention CONFIG_IMA_READ_POLICY=y in testcases/kernel/security/integrity/ima/README.md as it's required. That trivial thing I could do myself, but it'd help to add more info for setup needed (and it's always preferred to do the setup, if possible, but for some tests e.g. EVM testing in evm_overlay.sh it must be during the installation).
testcases/kernel/security/integrity/ima/tests/ima_keys.sh line 4
Quoted 10 lines
testcases/kernel/security/integrity/ima/tests/ima_keys.sh new file +4
-0@@ -0,0 +1,65 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0-or-later +# Copyright (c) 2020 Microsoft Corporation +# Author: Lachlan Sneff <[email protected]>
testcases/kernel/security/integrity/ima/tests/ima_keys.sh line 8
Quoted 4 lines
testcases/kernel/security/integrity/ima/tests/ima_keys.sh new file +4
-0+# +# Verify that keys are measured correctly based on policy. + +TST_NEEDS_CMDS="awk cut xxd"
nit: actually sed was meant to be added in this commit (I reported in previous one).
Kind regards,
Petr
PATCH v2 1/2 IMA: Add a test to verify measurment of keys
In reply to Lachlan SneffHi Lachlan,
...
Review
Quoted 6 lines
(quoted diff) +6
-0+ 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
I guess "*keyrings*" as grep parameter is wrong. * for regexp should be .* If you meant to grep for keyrings, it should be:
if ! echo "$keycheck_line" | grep -q "keyrings"; then
tst_brk TCONF "ima policy does not specify a keyrings to check"
fi
Few more changes (mostly nits), is that ok for you?
Kind regards,
Petr
kernel/security/integrity/ima/README.md +18 -0
@@ -16,6 +16,24 @@ CONFIG_INTEGRITY=y CONFIG_IMA=y ``` +IMA Key Import tests +~~~~~~~~~~~~~~~~~~~~ + +`ima_keys.sh` requires a x509 public key, by default in `/etc/keys/x509_ima.der`. +The 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 ---------
kernel/security/integrity/ima/tests/ima_keys.sh +11 -10
@@ -5,17 +5,18 @@ # # Verify that keys are measured correctly based on policy. -TST_NEEDS_CMDS="grep mktemp cut sed tr" +TST_NEEDS_CMDS="cut grep sed tr xxd" TST_CNT=1 TST_NEEDS_DEVICE=1 . ima_setup.sh -# Based on https://lkml.org/lkml/2019/12/13/564. +# 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) + local err keycheck_line keyrings line templates + local test_file="file.txt" tst_res TINFO "verifying key measurement for keyrings and templates specified in IMA policy file"
@@ -28,7 +29,7 @@ test1() tst_brk TCONF "ima policy does not specify \"func=KEY_CHECK\"" fi - if echo "$keycheck_line" | grep -q "*keyrings*"; then + if ! echo "$keycheck_line" | grep -q "keyrings"; then tst_brk TCONF "ima policy does not specify a keyrings to check" fi
@@ -41,12 +42,12 @@ test1() templates=$(echo "$keycheck_line" | tr " " "\n" | grep "template" | \ cut -d'=' -f2) - grep -E "($templates)*($keyrings)" $ASCII_MEASUREMENTS | while read line + grep -E "($templates)*$keyrings" $ASCII_MEASUREMENTS | while read line do - local digest expected_digest algorithm + local algorithm digest expected_digest keyring - digest=$(echo "$line" | cut -d' ' -f4 | cut -d':' -f2) algorithm=$(echo "$line" | cut -d' ' -f4 | cut -d':' -f1) + digest=$(echo "$line" | cut -d' ' -f4 | cut -d':' -f2) keyring=$(echo "$line" | cut -d' ' -f5) echo "$line" | cut -d' ' -f6 | xxd -r -p > $test_file
@@ -56,12 +57,12 @@ test1() if [ "$digest" != "$expected_digest" ]; then tst_res TFAIL "incorrect digest was found for the ($keyring) keyring" + err=1 fi done - rm $test_file - - tst_res TPASS "specified keyrings were measured correctly" + [ -z "$err" ] && \ + tst_res TPASS "specified keyrings were measured correctly" } tst_run
PATCH v2 2/2 IMA: Add a test to verify importing a certificate into keyring
In reply to Lachlan SneffAdd an IMA measurement test that verifies that an x509 certificate can be imported into the .ima keyring and measured correctly.
--- .../security/integrity/ima/tests/ima_keys.sh | 45 ++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-)
testcases/kernel/security/integrity/ima/tests/ima_keys.sh +43 -2
@@ -5,10 +5,12 @@ # # Verify that keys are measured correctly based on policy. -TST_NEEDS_CMDS="awk cut xxd" -TST_CNT=1 +TST_NEEDS_CMDS="awk cut 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.
@@ -62,4 +64,43 @@ 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="$PWD/test.txt" + + [ -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 + 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 +} + tst_run
Signature · 1 line
2.25.1
PATCH v2 2/2 IMA: Add a test to verify importing a certificate into keyring
Hi Lachlan,
few details (all can be fixed by me before merge, no need to repost).
...
testcases/kernel/security/integrity/ima/tests/ima_keys.sh line 8
Quoted 8 lines
+++
b/testcases/kernel/security/integrity/ima/tests/ima_keys.shtestcases/kernel/security/integrity/ima/tests/ima_keys.sh +1
-2@@ -5,10 +5,12 @@ # Verify that keys are measured correctly based on policy. -TST_NEEDS_CMDS="awk cut xxd" -TST_CNT=1 +TST_NEEDS_CMDS="awk cut xxd keyctl evmctl openssl cmp"
TST_NEEDS_CMDS="awk cmp cut evmctl keyctl openssl sed xxd" (I ignore tail, if there is cut, sed and openssl it should be there, the same rule as for grep).
testcases/kernel/security/integrity/ima/tests/ima_keys.sh line 12
Quoted 4 lines
testcases/kernel/security/integrity/ima/tests/ima_keys.sh +2
-0+TST_CNT=2 TST_NEEDS_DEVICE=1 +CERT_FILE="${CERT_FILE:-}/etc/keys/x509_ima.der"
I'm sorry, I was wrong, this must be:
CERT_FILE="${CERT_FILE:-/etc/keys/x509_ima.der}"
testcases/kernel/security/integrity/ima/tests/ima_keys.sh test1() · line 70
Quoted 12 lines
testcases/kernel/security/integrity/ima/tests/ima_keys.sh +5
-0+ . ima_setup.sh # Based on https://lkml.org/lkml/2019/12/13/564.@@ -62,4 +64,43 @@ 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="$PWD/test.txt"
nit: Can test_file use relative path?
local test_file="test.txt"
Kind regards,
Petr
PATCH v2 2/2 IMA: Add a test to verify importing a certificate into keyring
In reply to Lachlan SneffQuoted 13 lines
Add an IMA measurement test that verifies that an x509 certificate can be imported into the .ima keyring and measured correctly.
Signed-off-by Lachlan Sneff <[email protected]>--- .../security/integrity/ima/tests/ima_keys.sh | 45 ++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-)testcases/kernel/security/integrity/ima/tests/ima_keys.sh +0
-0
testcases/kernel/security/integrity/ima/tests/ima_keys.sh line 12
Quoted 10 lines
testcases/kernel/security/integrity/ima/tests/ima_keys.sh +3
-2@@ -5,10 +5,12 @@ # Verify that keys are measured correctly based on policy. -TST_NEEDS_CMDS="awk cut xxd" -TST_CNT=1 +TST_NEEDS_CMDS="awk cut xxd keyctl evmctl openssl cmp" +TST_CNT=2 TST_NEEDS_DEVICE=1 +CERT_FILE="${CERT_FILE:-}/etc/keys/x509_ima.der"
Key setup is something what I'd like to be either set automatically (ideally, but maybe too hard) or documented in testcases/kernel/security/integrity/ima/README.md.
ima_keys 1 TINFO: verifying key measurement for keyrings and templates specified in IMA policy file ima_keys 1 TPASS: specified keyrings were measured correctlyima_keys 2 TCONF: missing /etc/keys/x509_ima.der
=> many uses will TCONF, which is not what we want.
Running these scripts from examples/ in ima-evm-utils repository:
./ima-gen-local-ca.sh && ./ima-genkey-self.sh && ./ima-genkey.sh
is obviously not enough:
ima_keys 1 TINFO: verifying key measurement for keyrings and templates specified in IMA policy file ima_keys 1 TPASS: specified keyrings were measured correctlyima_keys 2 TINFO: adding a cert to the .ima keyring (/etc/keys/x509_ima.der) add_key failed
errno: Required key not available (126)ima_keys 2 TCONF: unable to import a cert into the .ima keyring
Does it make sense to copy these scripts into LTP (most distros ship them in development packages, but we cannot depend on it) and run them in the test setup? If not, we should really document that.
+ feel free to add anything relevant to README.md :)
Kind regards,
Petr
PATCH v2 2/2 IMA: Add a test to verify importing a certificate into keyring
Quoted 39 lines · Petr Vorel
Quoted 11 lines
Add an IMA measurement test that verifies that an x509 certificate can be imported into the .ima keyring and measured correctly.
Signed-off-by Lachlan Sneff <[email protected]>--- .../security/integrity/ima/tests/ima_keys.sh | 45 ++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-)testcases/kernel/security/integrity/ima/tests/ima_keys.sh +0
-0testcases/kernel/security/integrity/ima/tests/ima_keys.sh line 9
Quoted 7 lines
testcases/kernel/security/integrity/ima/tests/ima_keys.sh +3
-2@@ -5,10 +5,12 @@ # Verify that keys are measured correctly based on policy. -TST_NEEDS_CMDS="awk cut xxd" -TST_CNT=1 +TST_NEEDS_CMDS="awk cut xxd keyctl evmctl openssl cmp" +TST_CNT=2 TST_NEEDS_DEVICE=1 +CERT_FILE="${CERT_FILE:-}/etc/keys/x509_ima.der"Key setup is something what I'd like to be either set automatically (ideally, but maybe too hard) or documented in
testcases/kernel/security/integrity/ima/README.md.
ima_keys1 TINFO: verifying key measurement for keyrings and templates specified in IMA policy fileima_keys1 TPASS: specified keyrings were measured correctlyima_keys2 TCONF: missing/etc/keys/x509_ima.der
=> many uses will TCONF, which is not what we want.Running these scripts from examples/ in ima-evm-utils repository:
./ima-gen-local-ca.sh && ./ima-genkey-self.sh && ./ima-genkey.shis obviously not enough:
ima_keys1 TINFO: verifying key measurement for keyrings and templates specified in IMA policy fileima_keys1 TPASS: specified keyrings were measured correctlyima_keys2 TINFO: adding a cert to the .ima keyring (/etc/keys/x509_ima.der)add_keyfailed
errno: Required key not available (126)ima_keys2 TCONF: unable to import a cert into the .ima keyring
Quoted 7 lines
Does it make sense to copy these scripts into LTP (most distros ship them in development packages, but we cannot depend on it) and run them in the test setup? If not, we should really document that.
+ feel free to add anything relevant to README.md :)Kind regards,
Petr
Hi Petr,
I'll look into whether it's doable to automatically generate keys. I suspect that including the scripts to generate the key
is the best way forward with this, as well as noting in the README how to generate your own key and use that instead.
Expect a further patch in the next few days.
Thank you reviewing this further!
Lachlan
PATCH v2 2/2 IMA: Add a test to verify importing a certificate into keyring
In reply to Lachlan SneffHi Lachlan,
Just a 2 nit changes for second test:
testcases/kernel/security/integrity/ima/tests/ima_keys.sh test1() · line 70
Quoted 2 lines
testcases/kernel/security/integrity/ima/tests/ima_keys.sh +2
-0+test2() { + local keyring_id key_id test_file="$PWD/test.txt"
local keyring_id key_id
local test_file="test.txt"
testcases/kernel/security/integrity/ima/tests/ima_keys.sh test1() · line 72
Quoted 1 line
testcases/kernel/security/integrity/ima/tests/ima_keys.sh +1
-0+ [ -f $CERT_FILE ] || tst_brk TCONF "missing $CERT_FILE"
[ -f $CERT_FILE ] || tst_brk TCONF "missing x509 certificate ($CERT_FILE)"
...
plus removing
rm $test_file
It's in my branch
https://github.com/pevik/ltp/tree/Lachlan_Sneff/ima_keys.sh.v3.fixes/
Can you please have a look, so I can push it?
BTW could you please send me your IMA files (ascii_runtime_measurements should be enough)?
Kind regards,
Petr