From patchwork Fri Sep 25 17:44:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Vorel X-Patchwork-Id: 1371497 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.cz Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4ByfSW2B24z9sSJ for ; Sat, 26 Sep 2020 03:44:59 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 9EC873C59E7 for ; Fri, 25 Sep 2020 19:44:56 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-4.smtp.seeweb.it (in-4.smtp.seeweb.it [217.194.8.4]) by picard.linux.it (Postfix) with ESMTP id 32BA83C2C38 for ; Fri, 25 Sep 2020 19:44:46 +0200 (CEST) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-4.smtp.seeweb.it (Postfix) with ESMTPS id CDA961000A5B for ; Fri, 25 Sep 2020 19:44:45 +0200 (CEST) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 53D28B2A2; Fri, 25 Sep 2020 17:44:45 +0000 (UTC) From: Petr Vorel To: ltp@lists.linux.it Date: Fri, 25 Sep 2020 19:44:36 +0200 Message-Id: <20200925174439.9534-2-pvorel@suse.cz> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200925174439.9534-1-pvorel@suse.cz> References: <20200925174439.9534-1-pvorel@suse.cz> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.102.4 at in-4.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.0 required=7.0 tests=SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-4.smtp.seeweb.it Subject: [LTP] [PATCH 1/4] IMA: Move get_algorithm_digest(), set_digest_index() to ima_setup.sh X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-integrity@vger.kernel.org, Mimi Zohar , Jarkko Sakkinen , Lakshmi Ramasubramanian Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" 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. Signed-off-by: Petr Vorel --- .../integrity/ima/tests/ima_measurements.sh | 62 ++-------------- .../security/integrity/ima/tests/ima_setup.sh | 70 +++++++++++++++++++ 2 files changed, 76 insertions(+), 56 deletions(-) diff --git a/testcases/kernel/security/integrity/ima/tests/ima_measurements.sh b/testcases/kernel/security/integrity/ima/tests/ima_measurements.sh index 9a7500c76..1927e937c 100755 --- a/testcases/kernel/security/integrity/ima/tests/ima_measurements.sh +++ b/testcases/kernel/security/integrity/ima/tests/ima_measurements.sh @@ -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" diff --git a/testcases/kernel/security/integrity/ima/tests/ima_setup.sh b/testcases/kernel/security/integrity/ima/tests/ima_setup.sh index 1f17aa707..83ea62d4f 100644 --- a/testcases/kernel/security/integrity/ima/tests/ima_setup.sh +++ b/testcases/kernel/security/integrity/ima/tests/ima_setup.sh @@ -191,6 +191,76 @@ ima_cleanup() fi } +set_digest_index() +{ + DIGEST_INDEX= + + local template="$(tail -1 $ASCII_MEASUREMENTS | cut -d' ' -f 3)" + local i word + + # 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 ;; + *) + # 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')" +} + +get_algorithm_digest() +{ + local line="$1" + local delimiter=':' + local algorithm digest + + if [ -z "$line" ]; then + echo "measurement record not found" + return 1 + fi + + [ -z "$DIGEST_INDEX" ] && set_digest_index + digest=$(echo "$line" | cut -d' ' -f $DIGEST_INDEX) + if [ -z "$digest" ]; then + echo "digest not found (index: $DIGEST_INDEX, line: '$line')" + return 1 + fi + + if [ "${digest#*$delimiter}" != "$digest" ]; then + algorithm=$(echo "$digest" | cut -d $delimiter -f 1) + digest=$(echo "$digest" | cut -d $delimiter -f 2) + else + case "${#digest}" in + 32) algorithm="md5" ;; + 40) algorithm="sha1" ;; + *) + echo "algorithm must be either md5 or sha1 (digest: '$digest')" + return 1 ;; + esac + fi + if [ -z "$algorithm" ]; then + echo "algorithm not found" + return 1 + fi + if [ -z "$digest" ]; then + echo "digest not found" + return 1 + fi + + echo "$algorithm|$digest" +} + # loop device is needed to use only for tmpfs TMPDIR="${TMPDIR:-/tmp}" if [ "$(df -T $TMPDIR | tail -1 | awk '{print $2}')" != "tmpfs" -a -n "$TST_NEEDS_DEVICE" ]; then From patchwork Fri Sep 25 17:44:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Vorel X-Patchwork-Id: 1371498 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.cz Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4ByfSh2N1rz9sPB for ; Sat, 26 Sep 2020 03:45:08 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 6F8793C4D12 for ; Fri, 25 Sep 2020 19:45:05 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-7.smtp.seeweb.it (in-7.smtp.seeweb.it [217.194.8.7]) by picard.linux.it (Postfix) with ESMTP id 71F9A3C2991 for ; Fri, 25 Sep 2020 19:44:46 +0200 (CEST) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-7.smtp.seeweb.it (Postfix) with ESMTPS id 0B780200275 for ; Fri, 25 Sep 2020 19:44:46 +0200 (CEST) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 97CE9B2A1; Fri, 25 Sep 2020 17:44:45 +0000 (UTC) From: Petr Vorel To: ltp@lists.linux.it Date: Fri, 25 Sep 2020 19:44:37 +0200 Message-Id: <20200925174439.9534-3-pvorel@suse.cz> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200925174439.9534-1-pvorel@suse.cz> References: <20200925174439.9534-1-pvorel@suse.cz> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.102.4 at in-7.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.0 required=7.0 tests=SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-7.smtp.seeweb.it Subject: [LTP] [PATCH 2/4] IMA: Rewrite ima_boot_aggregate.c to new API X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-integrity@vger.kernel.org, Mimi Zohar , Jarkko Sakkinen , Lakshmi Ramasubramanian Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" 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. Signed-off-by: Petr Vorel --- .../integrity/ima/src/ima_boot_aggregate.c | 110 +++++++++--------- 1 file changed, 54 insertions(+), 56 deletions(-) diff --git a/testcases/kernel/security/integrity/ima/src/ima_boot_aggregate.c b/testcases/kernel/security/integrity/ima/src/ima_boot_aggregate.c index 98893b99a..318d9c299 100644 --- a/testcases/kernel/security/integrity/ima/src/ima_boot_aggregate.c +++ b/testcases/kernel/security/integrity/ima/src/ima_boot_aggregate.c @@ -1,19 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* -* Copyright (c) International Business Machines Corp., 2009 -* -* Authors: -* Mimi Zohar -* -* 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 + * + * Authors: Mimi Zohar + * + * Calculate a SHA1 boot aggregate value based on the TPM 1.2 + * binary_bios_measurements. + */ + +#include "config.h" #include #include #include @@ -23,10 +19,8 @@ #include #include -#include "config.h" -#include "test.h" - -char *TCID = "ima_boot_aggregate"; +#include "tst_test.h" +#include "tst_safe_stdio.h" #if HAVE_LIBCRYPTO #include @@ -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 */ @@ -128,12 +119,19 @@ int main(int argc, char *argv[]) printf("boot_aggregate:"); display_sha1_digest(boot_aggregate); - tst_exit(); } +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 From patchwork Fri Sep 25 17:44:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Vorel X-Patchwork-Id: 1371499 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.cz Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4ByfSv2GZGz9sPB for ; Sat, 26 Sep 2020 03:45:19 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 84B8A3C2F39 for ; Fri, 25 Sep 2020 19:45:16 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-4.smtp.seeweb.it (in-4.smtp.seeweb.it [IPv6:2001:4b78:1:20::4]) by picard.linux.it (Postfix) with ESMTP id 87F073C2C38 for ; Fri, 25 Sep 2020 19:44:46 +0200 (CEST) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-4.smtp.seeweb.it (Postfix) with ESMTPS id 33C401000DF1 for ; Fri, 25 Sep 2020 19:44:46 +0200 (CEST) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id C2A5CB2A3; Fri, 25 Sep 2020 17:44:45 +0000 (UTC) From: Petr Vorel To: ltp@lists.linux.it Date: Fri, 25 Sep 2020 19:44:38 +0200 Message-Id: <20200925174439.9534-4-pvorel@suse.cz> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200925174439.9534-1-pvorel@suse.cz> References: <20200925174439.9534-1-pvorel@suse.cz> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.102.4 at in-4.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.0 required=7.0 tests=SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-4.smtp.seeweb.it Subject: [LTP] [PATCH 3/4] ima_tpm.sh: Fix calculating boot aggregate X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-integrity@vger.kernel.org, Mimi Zohar , Jarkko Sakkinen , Lakshmi Ramasubramanian Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" 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 evmctl is recommended, to support older distros, when event log (/sys/kernel/security/tpm0/binary_bios_measurements) is not exported and only sha1 used, get boot agregate with old our legacy ima_boot_aggregate.c. Signed-off-by: Petr Vorel --- .../security/integrity/ima/tests/ima_tpm.sh | 101 ++++++++++++++---- 1 file changed, 83 insertions(+), 18 deletions(-) diff --git a/testcases/kernel/security/integrity/ima/tests/ima_tpm.sh b/testcases/kernel/security/integrity/ima/tests/ima_tpm.sh index c69f891f1..f4fa043c1 100755 --- a/testcases/kernel/security/integrity/ima/tests/ima_tpm.sh +++ b/testcases/kernel/security/integrity/ima/tests/ima_tpm.sh @@ -7,38 +7,103 @@ # 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 +EVMCTL_REQUIRED='1.3' +ERRMSG_EVMCTL="install evmctl >= $EVMCTL_REQUIRED" + +setup() +{ + local line tmp + + 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 ! check_evmctl $EVMCTL_REQUIRED; then + MISSING_EVMCTL=1 + if [ "$ALGORITHM" = "sha1" ]; then + tst_brk TCONF "algorithm not sha1 ($ALGORITHM), $ERRMSG_EVMCTL" + fi + fi +} + +# check_evmctl REQUIRED_TPM_VERSION +# return: 0: evmctl is new enough, 1: too old version +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)" + 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 -o $v2 -lt $r2 -o $v3 -lt $r3 ]; then + tst_res TCONF "evmctl >= $required required ($v)" + return 1 + fi + return 0 +} + test1() { tst_res TINFO "verify boot aggregate" - local zero="0000000000000000000000000000000000000000" local tpm_bios="$SECURITYFS/tpm0/binary_bios_measurements" - local ima_measurements="$ASCII_MEASUREMENTS" - local boot_aggregate boot_hash line + local cmd="evmctl ima_boot_aggregate" + local boot_aggregate cmd zero - # IMA boot aggregate - read line < $ima_measurements - boot_hash=$(echo $line | awk '{print $(NF-1)}' | cut -d':' -f2) + if [ "$MISSING_EVMCTL" = 1 ]; then + if [ -f "$tpm_bios" ]; then + tst_res TCONF "missing $tpm_bios, $ERRMSG_EVMCTL" + return + fi + cmd="ima_boot_aggregate" + fi + tst_res TINFO "using command: $cmd" - if [ ! -f "$tpm_bios" ]; then - tst_res TINFO "TPM Hardware Support not enabled in kernel or no TPM chip found" + boot_aggregate=$($cmd | grep "$ALGORITHM:" | cut -d':' -f2) + if [ -z "$boot_aggregate" ]; then + tst_res TINFO "TPM hardware support not enabled in kernel or no TPM chip found" - if [ "$boot_hash" = "$zero" ]; then - tst_res TPASS "bios boot aggregate is 0" + zero=$(echo $DIGEST | awk '{gsub(/./, "0")}; {print}') + if [ "$DIGEST" = "$zero" ]; then + tst_res TPASS "bios boot aggregate is $zero" else - tst_res TFAIL "bios boot aggregate is not 0" + tst_res TFAIL "bios boot aggregate is not $zero ($DIGEST)" fi + 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 - boot_aggregate=$(ima_boot_aggregate $tpm_bios | grep "boot_aggregate:" | 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" - fi + tst_res TFAIL "bios boot aggregate does not match IMA boot aggregate ($DIGEST)" fi } From patchwork Fri Sep 25 17:44:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Vorel X-Patchwork-Id: 1371500 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.cz Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4ByfT43Ddwz9sPB for ; Sat, 26 Sep 2020 03:45:28 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id D1A2D3C5FD1 for ; Fri, 25 Sep 2020 19:45:25 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-4.smtp.seeweb.it (in-4.smtp.seeweb.it [IPv6:2001:4b78:1:20::4]) by picard.linux.it (Postfix) with ESMTP id B378B3C2991 for ; Fri, 25 Sep 2020 19:44:46 +0200 (CEST) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-4.smtp.seeweb.it (Postfix) with ESMTPS id 5ACA51000F69 for ; Fri, 25 Sep 2020 19:44:46 +0200 (CEST) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id F0C06B2A5; Fri, 25 Sep 2020 17:44:45 +0000 (UTC) From: Petr Vorel To: ltp@lists.linux.it Date: Fri, 25 Sep 2020 19:44:39 +0200 Message-Id: <20200925174439.9534-5-pvorel@suse.cz> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200925174439.9534-1-pvorel@suse.cz> References: <20200925174439.9534-1-pvorel@suse.cz> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.102.4 at in-4.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.0 required=7.0 tests=SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-4.smtp.seeweb.it Subject: [LTP] [PATCH 4/4] ima_tpm.sh: Fix calculating PCR aggregate X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-integrity@vger.kernel.org, Mimi Zohar , Jarkko Sakkinen , Lakshmi Ramasubramanian Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" for TPM 2.0 and never 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.1 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 tpm pcrs file (tss1pcrread could be also used, but it's not yet packaged by distros). Add support for never evmctl versions (>= 1.3) and very old versions, which has different output format for ima_measurement command: * 1.3 adds prefix for algorithm and TPM, e.g.: "sha256: TPM PCR-10:" * old versions (v1.0, v0.9) printed only "PCR-10:" Previously supported "HW PCR-10:" was format for ima_measurement for evmctl 1.1. [1] https://patchwork.kernel.org/patch/11759729/ Signed-off-by: Petr Vorel --- .../security/integrity/ima/tests/ima_tpm.sh | 145 ++++++++++++------ 1 file changed, 102 insertions(+), 43 deletions(-) diff --git a/testcases/kernel/security/integrity/ima/tests/ima_tpm.sh b/testcases/kernel/security/integrity/ima/tests/ima_tpm.sh index f4fa043c1..300c96f64 100755 --- a/testcases/kernel/security/integrity/ima/tests/ima_tpm.sh +++ b/testcases/kernel/security/integrity/ima/tests/ima_tpm.sh @@ -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() { @@ -68,6 +69,83 @@ check_evmctl() return 0 } +# prints major version 1: TPM 1.2, 2: TPM 2.0 +# or nothing when version not detected +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 + + 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 +} + +read_pcr_tpm1() +{ + local pcr_path="/sys/class/tpm/tpm0/device/pcrs" + local pcr hash + + if [ ! -f "$pcrs_path" ]; then + pcrs_path="/sys/class/misc/tpm0/device/pcrs" + 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 + + if ! evmctl -v ima_measurement $BINARY_MEASUREMENTS > hash.txt 2>&1; then + tst_res TBROK "evmctl failed:" + cat hash.txt >&2 + return 1 + fi + pcr=$(grep -E "^($ALGORITHM: TPM |HW )*PCR-10:" hash.txt \ + | awk '{print $NF}') + + echo "$pcr" +} + test1() { tst_res TINFO "verify boot aggregate" @@ -87,7 +165,7 @@ test1() boot_aggregate=$($cmd | grep "$ALGORITHM:" | cut -d':' -f2) if [ -z "$boot_aggregate" ]; then - tst_res TINFO "TPM hardware support not enabled in kernel or no TPM chip found" + tst_res TINFO "$ERRMSG_TPM" zero=$(echo $DIGEST | awk '{gsub(/./, "0")}; {print}') if [ "$DIGEST" = "$zero" ]; then @@ -107,57 +185,38 @@ test1() fi } -# 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 + tpm_version="$(get_tpm_version)" + if [ -z "$tpm_version" ]; then + tst_brk TCONF "TMP version not detected ($ERRMSG_TPM)" fi + tst_res TINFO "TMP major version: $tpm_version" - 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 - - 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 + tst_res TFAIL "failed to get aggregate PCR-10" + 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 }