From patchwork Thu Jul 22 17:34:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roberto Sassu X-Patchwork-Id: 12394425 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-21.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 45657C63799 for ; Thu, 22 Jul 2021 17:34:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2C9626101B for ; Thu, 22 Jul 2021 17:34:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229826AbhGVQyO (ORCPT ); Thu, 22 Jul 2021 12:54:14 -0400 Received: from frasgout.his.huawei.com ([185.176.79.56]:3452 "EHLO frasgout.his.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230040AbhGVQyJ (ORCPT ); Thu, 22 Jul 2021 12:54:09 -0400 Received: from fraeml714-chm.china.huawei.com (unknown [172.18.147.200]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4GVzj54Txfz6D8XB; Fri, 23 Jul 2021 01:19:53 +0800 (CST) Received: from roberto-ThinkStation-P620.huawei.com (10.204.63.22) by fraeml714-chm.china.huawei.com (10.206.15.33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Thu, 22 Jul 2021 19:34:42 +0200 From: Roberto Sassu To: CC: , , , Roberto Sassu Subject: [RFC][PATCH ima-evm-utils 1/7] Download UML kernel and signing key Date: Thu, 22 Jul 2021 19:34:08 +0200 Message-ID: <20210722173414.1738041-2-roberto.sassu@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210722173414.1738041-1-roberto.sassu@huawei.com> References: <20210722173414.1738041-1-roberto.sassu@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.204.63.22] X-ClientProxiedBy: lhreml753-chm.china.huawei.com (10.201.108.203) To fraeml714-chm.china.huawei.com (10.206.15.33) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org Testing kernel functionality is more difficult than testing user space software, as it requires support from the testing platform to create a more specific environment (e.g. a virtual machine). User space software instead could be simply run in a container. Fortunately, a kernel architecture named UML (User Mode Linux) allows the kernel to be executed as a user space process, which would be suitable also for testing platforms such as Github Actions and Travis. This patch simply downloads (errors are ignored) the UML kernel binary and its signing key as artifacts from a URL in the format: $LINUX_URL/-test/ The LINUX_URL environment variable must be set in the configuration of the testing platform (for Github Actions, the variable must be created in an environment named 'test'). LINUX_URL could be for example: https://github.com/robertosassu/linux/releases/download/ If Github Releases is used, a workflow should build the kernel, tagged as '-test', and publish the artifacts. The UML kernel binary should be named 'linux-[.]' and the signing key 'signing_key.pem-', where the '.' suffix appears only if the current arch is not x86_64 and separator characters in the container name are replaced with '.'. Finally, the patch also adds curl and ca-certificates as software dependencies when necesssary. Signed-off-by: Roberto Sassu --- .github/workflows/ci.yml | 16 ++++++++++++++++ .travis.yml | 12 ++++++++++++ ci/alpine.sh | 3 ++- ci/alt.sh | 1 + ci/debian.sh | 2 ++ ci/fedora.sh | 3 ++- ci/tumbleweed.sh | 3 ++- 7 files changed, 37 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 088c04188682..51f7dbe0aaa6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -101,6 +101,9 @@ jobs: image: ${{ matrix.container }} env: ${{ matrix.env }} + environment: + name: test + steps: - name: Show OS run: cat /etc/os-release @@ -125,6 +128,19 @@ jobs: fi fi + - name: Download UML kernel and signing key + run: | + asset_name_suffix=${{ matrix.container }} + asset_name_suffix="$(echo $asset_name_suffix | sed 's/[:\/]/./')" + if [ -n "$ARCH" ]; then + asset_name_suffix="$asset_name_suffix.$ARCH" + fi + curl -L ${{ secrets.LINUX_URL }}/${GITHUB_REF##*/}-test/linux-$asset_name_suffix -s -f --output linux || echo + curl -L ${{ secrets.LINUX_URL }}/${GITHUB_REF##*/}-test/signing_key.pem-$asset_name_suffix -s -f --output signing_key.pem || echo + if [ -f linux ]; then + chmod +x linux + fi + - name: Compiler version run: $CC --version diff --git a/.travis.yml b/.travis.yml index 7a7627323724..23c220e857b6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -87,11 +87,23 @@ before_install: sudo curl -L https://github.com/opencontainers/runc/releases/download/v1.0.0-rc93/runc.amd64 -o /usr/bin/runc sudo chmod +x /usr/bin/runc fi + - asset_name_suffix=$DISTRO + - asset_name_suffix="$(echo $asset_name_suffix | sed 's/[:\/]/./')" + - > + if [ -n "$ARCH" ]; then + asset_name_suffix="$asset_name_suffix.$ARCH" + fi - $CONTAINER info - DIR="/usr/src/ima-evm-utils" - printf "FROM $DISTRO\nRUN mkdir -p $DIR\nWORKDIR $DIR\nCOPY . $DIR\n" > Dockerfile - cat Dockerfile + - curl -L $LINUX_URL/$TRAVIS_BRANCH-test/linux-$asset_name_suffix -s -f --output linux || echo + - curl -L $LINUX_URL/$TRAVIS_BRANCH-test/signing_key.pem-$asset_name_suffix -s -f --output signing_key.pem || echo + - > + if [ -f "linux" ]; then + chmod +x linux + fi - $CONTAINER build $CONTAINER_ARGS -t ima-evm-utils . script: diff --git a/ci/alpine.sh b/ci/alpine.sh index 63d79546c0dd..588f450bdb5f 100755 --- a/ci/alpine.sh +++ b/ci/alpine.sh @@ -42,7 +42,8 @@ apk add \ sudo \ wget \ which \ - xxd + xxd \ + curl if [ ! "$TSS" ]; then apk add git diff --git a/ci/alt.sh b/ci/alt.sh index 884c9951ca98..6b2aa08bd228 100755 --- a/ci/alt.sh +++ b/ci/alt.sh @@ -21,4 +21,5 @@ apt-get install -y \ wget \ xsltproc \ xxd \ + curl \ && control openssl-gost enabled diff --git a/ci/debian.sh b/ci/debian.sh index ad7d2c026735..07ef28c3a2f0 100755 --- a/ci/debian.sh +++ b/ci/debian.sh @@ -49,6 +49,8 @@ $apt \ sudo \ wget \ xsltproc \ + curl \ + ca-certificates $apt xxd || $apt vim-common $apt libengine-gost-openssl1.1$ARCH || true diff --git a/ci/fedora.sh b/ci/fedora.sh index 2d80915ecd77..f07c678130ae 100755 --- a/ci/fedora.sh +++ b/ci/fedora.sh @@ -38,7 +38,8 @@ yum -y install \ sudo \ vim-common \ wget \ - which + which \ + curl yum -y install docbook5-style-xsl || true yum -y install swtpm || true diff --git a/ci/tumbleweed.sh b/ci/tumbleweed.sh index dfc478bed5dd..b6a42df7bfca 100755 --- a/ci/tumbleweed.sh +++ b/ci/tumbleweed.sh @@ -40,7 +40,8 @@ zypper --non-interactive install --force-resolution --no-recommends \ vim \ wget \ which \ - xsltproc + xsltproc \ + curl if [ -f /usr/lib/ibmtss/tpm_server -a ! -e /usr/local/bin/tpm_server ]; then ln -s /usr/lib/ibmtss/tpm_server /usr/local/bin From patchwork Thu Jul 22 17:34:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roberto Sassu X-Patchwork-Id: 12394419 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A4F83C6377D for ; Thu, 22 Jul 2021 17:34:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 878156101B for ; Thu, 22 Jul 2021 17:34:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230149AbhGVQyN (ORCPT ); Thu, 22 Jul 2021 12:54:13 -0400 Received: from frasgout.his.huawei.com ([185.176.79.56]:3453 "EHLO frasgout.his.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230054AbhGVQyK (ORCPT ); Thu, 22 Jul 2021 12:54:10 -0400 Received: from fraeml714-chm.china.huawei.com (unknown [172.18.147.201]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4GVzqw0Wdtz6DGqg; Fri, 23 Jul 2021 01:25:48 +0800 (CST) Received: from roberto-ThinkStation-P620.huawei.com (10.204.63.22) by fraeml714-chm.china.huawei.com (10.206.15.33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Thu, 22 Jul 2021 19:34:43 +0200 From: Roberto Sassu To: CC: , , , Roberto Sassu Subject: [RFC][PATCH ima-evm-utils 2/7] Download mount-idmapped Date: Thu, 22 Jul 2021 19:34:09 +0200 Message-ID: <20210722173414.1738041-3-roberto.sassu@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210722173414.1738041-1-roberto.sassu@huawei.com> References: <20210722173414.1738041-1-roberto.sassu@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.204.63.22] X-ClientProxiedBy: lhreml753-chm.china.huawei.com (10.201.108.203) To fraeml714-chm.china.huawei.com (10.206.15.33) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org mount-idmapped is a tool to create idmapped mounts, a feature recently integrated in kernel 5.12 and that allows processes to see a different UID and GID on that mount. This patch downloads (errors are ignored) an artifact from $MOUNT_IDMAPPED_URL/mount-idmapped, where MOUNT_IDMAPPED_URL is an environment variable to be set in the configuration of the testing platform (for Github Actions, the variable should be added in an environment named 'test'). Signed-off-by: Roberto Sassu --- .github/workflows/ci.yml | 4 ++++ .travis.yml | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 51f7dbe0aaa6..4cfffbbd85f7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -140,6 +140,10 @@ jobs: if [ -f linux ]; then chmod +x linux fi + curl -L ${{ secrets.MOUNT_IDMAPPED_URL }}/mount-idmapped -s -f --output mount-idmapped || echo + if [ -f mount-idmapped ]; then + chmod +x mount-idmapped + fi - name: Compiler version run: $CC --version diff --git a/.travis.yml b/.travis.yml index 23c220e857b6..f3e3d93e1907 100644 --- a/.travis.yml +++ b/.travis.yml @@ -104,6 +104,11 @@ before_install: if [ -f "linux" ]; then chmod +x linux fi + - curl -L $MOUNT_IDMAPPED_URL/mount-idmapped -s -f --output mount-idmapped || echo + - > + if [ -f "mount-idmapped" ]; then + chmod +x mount-idmapped + fi - $CONTAINER build $CONTAINER_ARGS -t ima-evm-utils . script: From patchwork Thu Jul 22 17:34:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roberto Sassu X-Patchwork-Id: 12394421 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CF8FDC63797 for ; Thu, 22 Jul 2021 17:34:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B94D4611C1 for ; Thu, 22 Jul 2021 17:34:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229585AbhGVQyO (ORCPT ); Thu, 22 Jul 2021 12:54:14 -0400 Received: from frasgout.his.huawei.com ([185.176.79.56]:3454 "EHLO frasgout.his.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230056AbhGVQyK (ORCPT ); Thu, 22 Jul 2021 12:54:10 -0400 Received: from fraeml714-chm.china.huawei.com (unknown [172.18.147.201]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4GVzj64H8nz6D8Zq; Fri, 23 Jul 2021 01:19:54 +0800 (CST) Received: from roberto-ThinkStation-P620.huawei.com (10.204.63.22) by fraeml714-chm.china.huawei.com (10.206.15.33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Thu, 22 Jul 2021 19:34:43 +0200 From: Roberto Sassu To: CC: , , , Roberto Sassu Subject: [RFC][PATCH ima-evm-utils 3/7] Add additional options to the container engine Date: Thu, 22 Jul 2021 19:34:10 +0200 Message-ID: <20210722173414.1738041-4-roberto.sassu@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210722173414.1738041-1-roberto.sassu@huawei.com> References: <20210722173414.1738041-1-roberto.sassu@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.204.63.22] X-ClientProxiedBy: lhreml753-chm.china.huawei.com (10.201.108.203) To fraeml714-chm.china.huawei.com (10.206.15.33) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org This patch adds additional options to the command line of the container engine, necessary to run the UML kernel. The additional options are: --cap-add=SYS_PTRACE -v /dev/shm:/dev/shm The first adds the PTRACE capability to the container, and the second adds a bind mount for /dev/shm. Signed-off-by: Roberto Sassu --- .github/workflows/ci.yml | 1 + .travis.yml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4cfffbbd85f7..c3964a01cf3a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -100,6 +100,7 @@ jobs: container: image: ${{ matrix.container }} env: ${{ matrix.env }} + options: --cap-add=SYS_PTRACE -v /dev/shm:/dev/shm environment: name: test diff --git a/.travis.yml b/.travis.yml index f3e3d93e1907..b26b57637914 100644 --- a/.travis.yml +++ b/.travis.yml @@ -114,4 +114,4 @@ before_install: script: - INSTALL="${DISTRO%%:*}" - INSTALL="${INSTALL%%/*}" - - $CONTAINER run $CONTAINER_ARGS -t ima-evm-utils /bin/sh -c "if [ \"$VARIANT\" ]; then ARCH=\"$ARCH\" ./ci/$INSTALL.$VARIANT.sh; fi && ARCH=\"$ARCH\" CC=\"$CC\" TSS=\"$TSS\" ./ci/$INSTALL.sh && if [ ! \"$VARIANT\" ]; then which tpm_server || which swtpm || if which tssstartup; then ./tests/install-swtpm.sh; fi; fi && CC=\"$CC\" VARIANT=\"$VARIANT\" ./build.sh" + - $CONTAINER run $CONTAINER_ARGS --cap-add=SYS_PTRACE -v /dev/shm:/dev/shm -t ima-evm-utils /bin/sh -c "if [ \"$VARIANT\" ]; then ARCH=\"$ARCH\" ./ci/$INSTALL.$VARIANT.sh; fi && ARCH=\"$ARCH\" CC=\"$CC\" TSS=\"$TSS\" ./ci/$INSTALL.sh && if [ ! \"$VARIANT\" ]; then which tpm_server || which swtpm || if which tssstartup; then ./tests/install-swtpm.sh; fi; fi && CC=\"$CC\" VARIANT=\"$VARIANT\" ./build.sh" From patchwork Thu Jul 22 17:34:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roberto Sassu X-Patchwork-Id: 12394427 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 79126C6379A for ; Thu, 22 Jul 2021 17:34:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 61D6961287 for ; Thu, 22 Jul 2021 17:34:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230017AbhGVQyO (ORCPT ); Thu, 22 Jul 2021 12:54:14 -0400 Received: from frasgout.his.huawei.com ([185.176.79.56]:3455 "EHLO frasgout.his.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230074AbhGVQyL (ORCPT ); Thu, 22 Jul 2021 12:54:11 -0400 Received: from fraeml714-chm.china.huawei.com (unknown [172.18.147.226]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4GVzqx03rbz6DH0p; Fri, 23 Jul 2021 01:25:49 +0800 (CST) Received: from roberto-ThinkStation-P620.huawei.com (10.204.63.22) by fraeml714-chm.china.huawei.com (10.206.15.33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Thu, 22 Jul 2021 19:34:44 +0200 From: Roberto Sassu To: CC: , , , Roberto Sassu Subject: [RFC][PATCH ima-evm-utils 4/7] Add functions to the testing library to run a test script with UML Date: Thu, 22 Jul 2021 19:34:11 +0200 Message-ID: <20210722173414.1738041-5-roberto.sassu@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210722173414.1738041-1-roberto.sassu@huawei.com> References: <20210722173414.1738041-1-roberto.sassu@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.204.63.22] X-ClientProxiedBy: lhreml753-chm.china.huawei.com (10.201.108.203) To fraeml714-chm.china.huawei.com (10.206.15.33) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org This patch introduces four new functions to execute a test script with a UML kernel. _run_user_mode It executes the UML kernel passed as first argument, with the init program specified as second argument. Additional kernel parameters can be passed through the third argument. This function is used to re-execute the script calling this function, so that the tests will be executed in the new environment rather than in the launching environment. This behavior is similar to doing a fork() in C. _exit_user_mode This function terminates the process that launched the UML kernel, so that the following commands in the script are executed by the UML kernel. _init_user_mode This function performs some initialization tasks, such as mounting sysfs, securityfs and procfs, and launching haveged to initialize the random device in the UML kernel. _cleanup_user_mode This function cleans the environment by unmounting the filesystems mounted by _init_user_mode. A typical structure of a test script to be launched by the UML kernel is: --- trap cleanup EXIT cleanup() { < cleanup commands > _cleanup_user_mode _report_exit } < commands before launching the UML kernel > _run_user_mode _exit_user_mode _init_user_mode < tests executed by the UML kernel > -- Finally, this patch adds haveged as software dependency. Signed-off-by: Roberto Sassu --- ci/alpine.sh | 3 ++- ci/debian.sh | 3 ++- ci/fedora.sh | 10 ++++++++- ci/tumbleweed.sh | 3 ++- tests/functions.sh | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 4 deletions(-) diff --git a/ci/alpine.sh b/ci/alpine.sh index 588f450bdb5f..a6db9271b28f 100755 --- a/ci/alpine.sh +++ b/ci/alpine.sh @@ -43,7 +43,8 @@ apk add \ wget \ which \ xxd \ - curl + curl \ + haveged if [ ! "$TSS" ]; then apk add git diff --git a/ci/debian.sh b/ci/debian.sh index 07ef28c3a2f0..13127b16d2d8 100755 --- a/ci/debian.sh +++ b/ci/debian.sh @@ -50,7 +50,8 @@ $apt \ wget \ xsltproc \ curl \ - ca-certificates + ca-certificates \ + haveged $apt xxd || $apt vim-common $apt libengine-gost-openssl1.1$ARCH || true diff --git a/ci/fedora.sh b/ci/fedora.sh index f07c678130ae..5808e65fde3a 100755 --- a/ci/fedora.sh +++ b/ci/fedora.sh @@ -17,6 +17,13 @@ esac # ibmswtpm2 requires gcc [ "$CC" = "gcc" ] || CC="gcc $CC" +. /etc/os-release + +# EPEL required for haveged +if [ "$PRETTY_NAME" = "CentOS Linux 8" ]; then + yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm +fi + yum -y install \ $CC $TSS \ asciidoc \ @@ -39,7 +46,8 @@ yum -y install \ vim-common \ wget \ which \ - curl + curl \ + haveged yum -y install docbook5-style-xsl || true yum -y install swtpm || true diff --git a/ci/tumbleweed.sh b/ci/tumbleweed.sh index b6a42df7bfca..f12c41c43e1a 100755 --- a/ci/tumbleweed.sh +++ b/ci/tumbleweed.sh @@ -41,7 +41,8 @@ zypper --non-interactive install --force-resolution --no-recommends \ wget \ which \ xsltproc \ - curl + curl \ + haveged if [ -f /usr/lib/ibmtss/tpm_server -a ! -e /usr/local/bin/tpm_server ]; then ln -s /usr/lib/ibmtss/tpm_server /usr/local/bin diff --git a/tests/functions.sh b/tests/functions.sh index 91cd5d96ddc4..5893e6dc4931 100755 --- a/tests/functions.sh +++ b/tests/functions.sh @@ -272,3 +272,54 @@ _report_exit() { fi } +# Syntax: _run_user_mode +_run_user_mode() { + if [ ! -f "$1" ]; then + return + fi + + if [ $$ -eq 1 ]; then + return + fi + + expect_pass $1 rootfstype=hostfs rw init=$2 quiet mem=256M $3 +} + +# Syntax: _exit_user_mode +_exit_user_mode() { + if [ $$ -eq 1 ]; then + return + fi + + if [ -f "$1" ]; then + exit $OK + fi +} + +# Syntax: _init_user_mode +_init_user_mode() { + if [ $$ -ne 1 ]; then + return + fi + + mount -t proc proc /proc + mount -t sysfs sysfs /sys + mount -t securityfs securityfs /sys/kernel/security + + if [ -n "$(which haveged 2> /dev/null)" ]; then + $(which haveged) -w 1024 &> /dev/null + fi + + pushd $PWD > /dev/null +} + +# Syntax: _cleanup_user_mode +_cleanup_user_mode() { + if [ $$ -ne 1 ]; then + return + fi + + umount /sys/kernel/security + umount /sys + umount /proc +} From patchwork Thu Jul 22 17:34:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roberto Sassu X-Patchwork-Id: 12394429 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B6890C63797 for ; Thu, 22 Jul 2021 17:35:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9063F611C1 for ; Thu, 22 Jul 2021 17:35:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229826AbhGVQzY (ORCPT ); Thu, 22 Jul 2021 12:55:24 -0400 Received: from frasgout.his.huawei.com ([185.176.79.56]:3456 "EHLO frasgout.his.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229536AbhGVQzX (ORCPT ); Thu, 22 Jul 2021 12:55:23 -0400 Received: from fraeml714-chm.china.huawei.com (unknown [172.18.147.200]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4GVzsK5DRKz6DHLN; Fri, 23 Jul 2021 01:27:01 +0800 (CST) Received: from roberto-ThinkStation-P620.huawei.com (10.204.63.22) by fraeml714-chm.china.huawei.com (10.206.15.33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Thu, 22 Jul 2021 19:35:57 +0200 From: Roberto Sassu To: CC: , , , Roberto Sassu Subject: [RFC][PATCH ima-evm-utils 5/7] Signal failures of tests executed by UML kernel with unclean shutdown Date: Thu, 22 Jul 2021 19:34:12 +0200 Message-ID: <20210722173414.1738041-6-roberto.sassu@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210722173414.1738041-1-roberto.sassu@huawei.com> References: <20210722173414.1738041-1-roberto.sassu@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.204.63.22] X-ClientProxiedBy: lhreml753-chm.china.huawei.com (10.201.108.203) To fraeml714-chm.china.huawei.com (10.206.15.33) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org With a UML kernel, test errors must be handled in a different way. Since the tests are executed by the UML kernel, the parent does not know which exit code it should return. The solution is to consider the executions of the UML kernel as tests (by using the existing testing API, expect_pass and expect_fail), and to signal to the launching environment, with the exit code of the UML kernel, whether or not the tests executed by the UML kernel were successful. With a clean shutdown, the UML kernel returns zero, with an unclean shutdown the UML kernel returns a non-zero exit code. This patch checks if the number of tests failed is greater than zero and, in this case, it does not perform a clean shutdown. By adding expect_pass to the command line of the UML kernel, the testing infrastructure in the launching environment will know if one or multiple tests in an execution of the UML kernel failed. This can be seen in the following output: --- Test: check_ima_sig_appraisal (evm_value: 0) [...] PASS: 9 SKIP: 1 FAIL: 0 Powering off. reboot: System halted [...] Test: check_evm_revalidate (evm_value: 6) PASS: 1 SKIP: 9 FAIL: 0 Powering off. reboot: System halted PASS: 2 SKIP: 0 FAIL: 0 PASS portable_signatures.test (exit status: 0) -- Two groups of tests, launched by the UML kernel, have been executed successfully and a clean shutdown has been performed for each group. 'PASS: 2 SKIP: 0 FAIL: 0' is the summary of the UML kernel executions, not of the tests. -- Kernel panic - not syncing: Attempted to kill init! exitcode=0x0000000b CPU: 0 PID: 1 Comm: portable_signat Not tainted 5.14.0-rc2-dont-use #1 [...] Kernel panic - not syncing: Attempted to kill init! exitcode=0x0000000b CPU: 0 PID: 1 Comm: portable_signat Not tainted 5.14.0-rc2-dont-use #1 [...] PASS: 0 SKIP: 0 FAIL: 2 FAIL portable_signatures.test (exit status: 1) -- In this case, the two groups of tests both failed and the message 'PASS: 0 SKIP: 0 FAIL: 2' reflects that. Lastly, this patch adds the package containing the poweroff command as software dependency. Signed-off-by: Roberto Sassu --- ci/alpine.sh | 3 ++- ci/debian.sh | 3 ++- ci/fedora.sh | 3 ++- ci/tumbleweed.sh | 3 ++- tests/functions.sh | 4 ++++ 5 files changed, 12 insertions(+), 4 deletions(-) diff --git a/ci/alpine.sh b/ci/alpine.sh index a6db9271b28f..a074ea0e841c 100755 --- a/ci/alpine.sh +++ b/ci/alpine.sh @@ -44,7 +44,8 @@ apk add \ which \ xxd \ curl \ - haveged + haveged \ + openrc if [ ! "$TSS" ]; then apk add git diff --git a/ci/debian.sh b/ci/debian.sh index 13127b16d2d8..58004a0bc028 100755 --- a/ci/debian.sh +++ b/ci/debian.sh @@ -51,7 +51,8 @@ $apt \ xsltproc \ curl \ ca-certificates \ - haveged + haveged \ + systemd-sysv $apt xxd || $apt vim-common $apt libengine-gost-openssl1.1$ARCH || true diff --git a/ci/fedora.sh b/ci/fedora.sh index 5808e65fde3a..6cc3cb46fb56 100755 --- a/ci/fedora.sh +++ b/ci/fedora.sh @@ -47,7 +47,8 @@ yum -y install \ wget \ which \ curl \ - haveged + haveged \ + systemd yum -y install docbook5-style-xsl || true yum -y install swtpm || true diff --git a/ci/tumbleweed.sh b/ci/tumbleweed.sh index f12c41c43e1a..a7039129d02f 100755 --- a/ci/tumbleweed.sh +++ b/ci/tumbleweed.sh @@ -42,7 +42,8 @@ zypper --non-interactive install --force-resolution --no-recommends \ which \ xsltproc \ curl \ - haveged + haveged \ + systemd-sysvinit if [ -f /usr/lib/ibmtss/tpm_server -a ! -e /usr/local/bin/tpm_server ]; then ln -s /usr/lib/ibmtss/tpm_server /usr/local/bin diff --git a/tests/functions.sh b/tests/functions.sh index 5893e6dc4931..9f05429d47ce 100755 --- a/tests/functions.sh +++ b/tests/functions.sh @@ -263,6 +263,10 @@ _report_exit() { [ $testsfail -gt 0 ] && echo -n "$RED" || echo -n "$NORM" echo " FAIL: $testsfail" echo "$NORM" + # Signal failure to UML caller with an unclean shutdown. + if [ $$ -eq 1 ] && [ "$(which poweroff)" ] && [ $testsfail -eq 0 ]; then + poweroff -f + fi if [ $testsfail -gt 0 ]; then exit "$FAIL" elif [ $testspass -gt 0 ]; then From patchwork Thu Jul 22 17:34:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roberto Sassu X-Patchwork-Id: 12394431 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EC695C63798 for ; Thu, 22 Jul 2021 17:35:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C6D7661249 for ; Thu, 22 Jul 2021 17:35:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229536AbhGVQzY (ORCPT ); Thu, 22 Jul 2021 12:55:24 -0400 Received: from frasgout.his.huawei.com ([185.176.79.56]:3457 "EHLO frasgout.his.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229710AbhGVQzX (ORCPT ); Thu, 22 Jul 2021 12:55:23 -0400 Received: from fraeml714-chm.china.huawei.com (unknown [172.18.147.200]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4GVzkX1zfsz6D8XB; Fri, 23 Jul 2021 01:21:08 +0800 (CST) Received: from roberto-ThinkStation-P620.huawei.com (10.204.63.22) by fraeml714-chm.china.huawei.com (10.206.15.33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Thu, 22 Jul 2021 19:35:57 +0200 From: Roberto Sassu To: CC: , , , Roberto Sassu Subject: [RFC][PATCH ima-evm-utils 6/7] Introduce TST_LIST variable to select a test to execute Date: Thu, 22 Jul 2021 19:34:13 +0200 Message-ID: <20210722173414.1738041-7-roberto.sassu@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210722173414.1738041-1-roberto.sassu@huawei.com> References: <20210722173414.1738041-1-roberto.sassu@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.204.63.22] X-ClientProxiedBy: lhreml753-chm.china.huawei.com (10.201.108.203) To fraeml714-chm.china.huawei.com (10.206.15.33) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org It might be desirable, due to restrictions in the testing environment, to execute tests individually. This patch introduces the TST_LIST variable, which can be set with the name of the test to execute. If the variable is set, expect_pass and expect_fail automatically skip the tests when the first argument of those functions does not match the value of TST_LIST. TST_LIST can be also used in conjunction with the UML kernel. It is sufficient to add it to the kernel command line. Signed-off-by: Roberto Sassu --- tests/functions.sh | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/functions.sh b/tests/functions.sh index 9f05429d47ce..2602b4699f5c 100755 --- a/tests/functions.sh +++ b/tests/functions.sh @@ -72,6 +72,12 @@ declare -i TNESTED=0 # just for sanity checking expect_pass() { local -i ret + if [ -n "$TST_LIST" ] && [ "${TST_LIST/$1/}" = $TST_LIST ]; then + [ "$VERBOSE" -gt 1 ] && echo "____ SKIP test: $*" + testsskip+=1 + return $SKIP + fi + if [ $TNESTED -gt 0 ]; then echo $RED"expect_pass should not be run nested"$NORM testsfail+=1 @@ -98,6 +104,12 @@ expect_pass() { expect_fail() { local ret + if [ -n "$TST_LIST" ] && [ "${TST_LIST/$1/}" = $TST_LIST ]; then + [ "$VERBOSE" -gt 1 ] && echo "____ SKIP test: $*" + testsskip+=1 + return $SKIP + fi + if [ $TNESTED -gt 0 ]; then echo $RED"expect_fail should not be run nested"$NORM testsfail+=1 From patchwork Thu Jul 22 17:34:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roberto Sassu X-Patchwork-Id: 12394433 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 08030C6377D for ; Thu, 22 Jul 2021 17:36:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D59E361249 for ; Thu, 22 Jul 2021 17:36:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229837AbhGVQz0 (ORCPT ); Thu, 22 Jul 2021 12:55:26 -0400 Received: from frasgout.his.huawei.com ([185.176.79.56]:3458 "EHLO frasgout.his.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229710AbhGVQzZ (ORCPT ); Thu, 22 Jul 2021 12:55:25 -0400 Received: from fraeml714-chm.china.huawei.com (unknown [172.18.147.207]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4GVzpH6vm4z6H7WK; Fri, 23 Jul 2021 01:24:23 +0800 (CST) Received: from roberto-ThinkStation-P620.huawei.com (10.204.63.22) by fraeml714-chm.china.huawei.com (10.206.15.33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Thu, 22 Jul 2021 19:35:57 +0200 From: Roberto Sassu To: CC: , , , Roberto Sassu Subject: [RFC][PATCH ima-evm-utils 7/7] Add tests for EVM portable signatures Date: Thu, 22 Jul 2021 19:34:14 +0200 Message-ID: <20210722173414.1738041-8-roberto.sassu@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210722173414.1738041-1-roberto.sassu@huawei.com> References: <20210722173414.1738041-1-roberto.sassu@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.204.63.22] X-ClientProxiedBy: lhreml753-chm.china.huawei.com (10.201.108.203) To fraeml714-chm.china.huawei.com (10.206.15.33) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org EVM portable signatures are particularly suitable for the protection of metadata of immutable files where metadata is signed by a software vendor. They can be used for example in conjunction with an IMA policy that appraises only executed and memory mapped files. However, some usability issues are still unsolved, especially when EVM is used without loading an HMAC key. The kernel patch set 'evm: Improve usability of portable signatures' attempts to fix the open issues. The purpose of the new tests is to verify that operations on files with EVM portable signatures succeed and that the new kernel patch set does not break the existing kernel integrity expectations. To run the tests, it is necessary to pass the path of the kernel private key with the TST_KEY_PATH environment variable. If not provided, the script searches the key in /lib/modules/$(uname -r)/source/certs/signing_key.pem and in the current directory. Root privileges are required to mount the image, configure IMA/EVM and set xattrs. The script attempts to launch a UML kernel named 'linux' if it is found in the ima-evm-utils root directory. Only the tests with a compatible EVM mode will be executed, unless the TST_EVM_CHANGE_MODE variable is set to 1 (if the tests are executed by a UML kernel TST_EVM_CHANGE_MODE is always set to 1). In this case, the script attempts to change the current EVM mode. The check_evm_revalidate() test can be executed only if EVM_ALLOW_METADATA_WRITES is set in advance before running the tests. To do it, it is sufficient to execute: echo 4 > /sys/kernel/security/evm This step is not necessary if the script launches a UML kernel. Signed-off-by: Roberto Sassu --- build.sh | 1 + ci/alpine.sh | 5 +- ci/debian.sh | 6 +- ci/fedora.sh | 6 +- ci/tumbleweed.sh | 6 +- tests/Makefile.am | 3 +- tests/portable_signatures.test | 1157 ++++++++++++++++++++++++++++++++ 7 files changed, 1179 insertions(+), 5 deletions(-) create mode 100755 tests/portable_signatures.test diff --git a/build.sh b/build.sh index c4d28f1302c6..4f002445a68b 100755 --- a/build.sh +++ b/build.sh @@ -90,6 +90,7 @@ if [ $ret -eq 0 ]; then grep "skipped" tests/sign_verify.log | wc -l fi tail -20 tests/boot_aggregate.log + tail -100 tests/portable_signatures.log exit 0 fi diff --git a/ci/alpine.sh b/ci/alpine.sh index a074ea0e841c..8cac021fd0de 100755 --- a/ci/alpine.sh +++ b/ci/alpine.sh @@ -45,7 +45,10 @@ apk add \ xxd \ curl \ haveged \ - openrc + openrc \ + acl \ + e2fsprogs \ + keyutils if [ ! "$TSS" ]; then apk add git diff --git a/ci/debian.sh b/ci/debian.sh index 58004a0bc028..8e86f051d481 100755 --- a/ci/debian.sh +++ b/ci/debian.sh @@ -52,7 +52,11 @@ $apt \ curl \ ca-certificates \ haveged \ - systemd-sysv + systemd-sysv \ + acl \ + e2fsprogs \ + keyutils \ + libcap2-bin $apt xxd || $apt vim-common $apt libengine-gost-openssl1.1$ARCH || true diff --git a/ci/fedora.sh b/ci/fedora.sh index 6cc3cb46fb56..cffea38ca690 100755 --- a/ci/fedora.sh +++ b/ci/fedora.sh @@ -48,7 +48,11 @@ yum -y install \ which \ curl \ haveged \ - systemd + systemd \ + keyutils \ + e2fsprogs \ + acl \ + libcap yum -y install docbook5-style-xsl || true yum -y install swtpm || true diff --git a/ci/tumbleweed.sh b/ci/tumbleweed.sh index a7039129d02f..3b78a1edf501 100755 --- a/ci/tumbleweed.sh +++ b/ci/tumbleweed.sh @@ -43,7 +43,11 @@ zypper --non-interactive install --force-resolution --no-recommends \ xsltproc \ curl \ haveged \ - systemd-sysvinit + systemd-sysvinit \ + e2fsprogs \ + keyutils \ + acl \ + libcap-progs if [ -f /usr/lib/ibmtss/tpm_server -a ! -e /usr/local/bin/tpm_server ]; then ln -s /usr/lib/ibmtss/tpm_server /usr/local/bin diff --git a/tests/Makefile.am b/tests/Makefile.am index ff928e177406..c3f6a151e101 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,7 +1,8 @@ check_SCRIPTS = TESTS = $(check_SCRIPTS) -check_SCRIPTS += ima_hash.test sign_verify.test boot_aggregate.test +check_SCRIPTS += ima_hash.test sign_verify.test boot_aggregate.test \ + portable_signatures.test clean-local: -rm -f *.txt *.out *.sig *.sig2 diff --git a/tests/portable_signatures.test b/tests/portable_signatures.test new file mode 100755 index 000000000000..cacbf7a9bd14 --- /dev/null +++ b/tests/portable_signatures.test @@ -0,0 +1,1157 @@ +#!/bin/bash + +# +# Check if operations on files with EVM portable signatures succeed. + +trap cleanup SIGINT SIGTERM SIGSEGV EXIT + +# Base VERBOSE on the environment variable, if set. +VERBOSE="${VERBOSE:-0}" +TST_EVM_CHANGE_MODE="${TST_EVM_CHANGE_MODE:-0}" + +# From security/integrity/evm/evm.h in kernel source directory. +let "EVM_INIT_HMAC=0x0001" +let "EVM_INIT_X509=0x0002" +let "EVM_ALLOW_METADATA_WRITES=0x0004" +let "EVM_SETUP_COMPLETE=0x80000000" + +cd "$(dirname "$0")" +export PATH=$PWD/../src:$PATH +export LD_LIBRARY_PATH=$LD_LIBRARY_PATH +. ./functions.sh +_require evmctl + +cleanup() { + if [ "$loop_mounted" = "1" ]; then + popd > /dev/null + + if [ -n "$mountpoint_idmapped" ]; then + umount $mountpoint_idmapped + fi + + umount $mountpoint + fi + + if [ -n "$dev" ]; then + losetup -d $dev + fi + + rm -f $image + rm -f $key_path_der + rm -Rf $mountpoint + + if [ -n "$mountpoint_idmapped" ]; then + rm -Rf $mountpoint_idmapped + fi + + _cleanup_user_mode + _report_exit +} + +get_xattr() { + format="hex" + + if [ "$1" = "security.selinux" ]; then + format="text" + fi + + getfattr -n $1 -e $format -d $2 2> /dev/null | awk -F "=" '$1 == "'$1'" {if ("'$format'" == "hex") v=substr($2, 3); else { split($2, temp, "\""); v=temp[2] }; print v}' +} + +IMA_UUID="28b23254-9467-44c0-b6ba-34b12e85a26d" +APPRAISE_DIGSIG_FOWNER=2000 +APPRAISE_DIGSIG_RULE="appraise fsuuid=$IMA_UUID fowner=$APPRAISE_DIGSIG_FOWNER appraise_type=imasig" +MEASURE_FOWNER=2001 +MEASURE_RULE="measure fsuuid=$IMA_UUID fowner=$MEASURE_FOWNER template=ima-sig" +APPRAISE_FOWNER=2002 +APPRAISE_RULE="appraise fsuuid=$IMA_UUID fowner=$APPRAISE_FOWNER" +METADATA_CHANGE_FOWNER=3001 +METADATA_CHANGE_FOWNER_2=3002 + +check_load_ima_rule() { + rule_loaded=$(cat /sys/kernel/security/ima/policy | grep "$1") + if [ -z "$rule_loaded" ]; then + new_policy=$(mktemp -p $mountpoint) + echo $1 > $new_policy + evmctl sign -o -a sha256 --imasig --key $key_path $new_policy &> /dev/null + echo $new_policy > /sys/kernel/security/ima/policy + result=$? + rm -f $new_policy + + if [ $result -ne 0 ]; then + echo "${RED}Failed to set IMA policy${NORM}" + return $FAIL + fi + fi + + return $OK +} + +# The purpose of this test is to verify that the patch 'ima: Allow imasig +# requirement to be satisfied by EVM portable signatures' didn't break the +# current behavior (IMA signatures still satisfy the imasig requirement). +check_ima_sig_appraisal() { + echo "Test: ${FUNCNAME[0]} (evm_value: $evm_value)" + + if [ $((evm_value & (EVM_INIT_X509 | EVM_INIT_HMAC))) -ne 0 ]; then + echo "${CYAN}EVM mode 0 required${NORM}" + return $SKIP + fi + + echo "test" > test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot write test-file${NORM}" + return $FAIL + fi + + evmctl ima_sign -a sha256 --key $key_path test-file &> /dev/null + if [ $? -ne 0 ]; then + echo "${RED}Cannot sign test-file${NORM}" + return $FAIL + fi + + chown $APPRAISE_DIGSIG_FOWNER test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot change owner of test-file${NORM}" + return $FAIL + fi + + check_load_ima_rule "$APPRAISE_DIGSIG_RULE" + result=$? + if [ $result -ne $OK ]; then + return $result + fi + + # Check if appraisal works. + cat test-file > /dev/null + if [ $? -ne 0 ]; then + echo "${RED}Cannot read test-file${NORM}" + return $FAIL + fi + + # Ensure that files with IMA signature cannot be updated (immutable). + echo "test" 2> /dev/null >> test-file + if [ $? -eq 0 ]; then + echo "${RED}Write to test-file should not succeed (immutable file)${NORM}" + return $FAIL + fi + + return $OK +} + +cleanup_ima_sig_appraisal() { + rm -f test-file +} + +# Requires: +# - ima: Don't remove security.ima if file must not be appraised +# +# The purpose of this test is to verify that the patch 'ima: Introduce template +# field evmsig and write to field sig as fallback' still allows IMA signatures +# to be displayed in the measurement list. +check_ima_sig_ima_measurement_list() { + echo "Test: ${FUNCNAME[0]} (evm_value: $evm_value)" + + echo "test" > test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot write test-file${NORM}" + return $FAIL + fi + + evmctl ima_sign -a sha256 --imasig --key $key_path test-file &> /dev/null + if [ $? -ne 0 ]; then + echo "${RED}Cannot sign test-file${NORM}" + return $FAIL + fi + + chown $MEASURE_FOWNER test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot change owner of test-file${NORM}" + return $FAIL + fi + + check_load_ima_rule "$MEASURE_RULE" + result=$? + if [ $result -ne $OK ]; then + return $result + fi + + # Read the file to add it to the measurement list. + cat test-file > /dev/null + if [ $? -ne 0 ]; then + echo "${RED}Cannot read test-file${NORM}" + return $FAIL + fi + + ima_sig_fs=$(get_xattr security.ima test-file) + if [ -z "$ima_sig_fs" ]; then + echo "${RED}security.ima not found${NORM}" + return $FAIL + fi + + # Search security.ima in the measurement list. + ima_sig_list=$(cat /sys/kernel/security/ima/ascii_runtime_measurements | awk '$6 == "'$ima_sig_fs'"') + if [ -z "$ima_sig_list" ]; then + echo "${RED}security.ima mismatch (xattr != measurement list)${NORM}" + return $FAIL + fi + + return $OK +} + +cleanup_ima_sig_ima_measurement_list() { + rm -f test-file +} + +# Requires: +# - evm: Execute evm_inode_init_security() only when an HMAC key is loaded +# +# The purpose of this test is to verify that new files can be created when EVM +# is initialized only with a public key. +check_create_file() { + echo "Test: ${FUNCNAME[0]} (evm_value: $evm_value)" + + # To trigger the bug we need to enable public key verification without HMAC key loaded. + if [ $((evm_value & $EVM_INIT_X509)) -ne $EVM_INIT_X509 ]; then + echo "${CYAN}EVM mode $EVM_INIT_X509 required${NORM}" + return $SKIP + fi + + if [ $((evm_value & $EVM_INIT_HMAC)) -eq $EVM_INIT_HMAC ]; then + echo "${CYAN}EVM mode $EVM_INIT_HMAC must be disabled${NORM}" + return $SKIP + fi + + echo "test" > test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot write test-file${NORM}" + return $FAIL + fi + + return $OK +} + +cleanup_create_file() { + rm -f test-file +} + +# Requires: +# - evm: Introduce evm_hmac_disabled() to safely ignore verification errors +# - evm: Allow xattr/attr operations for portable signatures +# - evm: Execute evm_inode_init_security() only when an HMAC key is loaded +# +# The purpose of this test is to verify that EVM with the patches above allows +# metadata to copied one by one, even if the portable signature verification +# temporarily fails until the copy is completed. +check_cp_preserve_xattrs() { + echo "Test: ${FUNCNAME[0]} (evm_value: $evm_value)" + + if [ $evm_value -ne $EVM_INIT_X509 ]; then + echo "${CYAN}EVM mode $EVM_INIT_X509 required${NORM}" + return $SKIP + fi + + echo "test" > test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot write test-file${NORM}" + return $FAIL + fi + + evmctl sign -o -a sha256 --imahash --key $key_path test-file &> /dev/null + if [ $? -ne 0 ]; then + echo "${RED}Cannot sign test-file${NORM}" + return $FAIL + fi + + # Check if cp is allowed to set metadata for the new file. + cp -a test-file test-file.copy + if [ $? -ne 0 ]; then + echo "${RED}Cannot copy test-file with attrs/xattrs preserved${NORM}" + return $FAIL + fi + + return $OK +} + +cleanup_cp_preserve_xattrs() { + rm -f test-file test-file.copy +} + +# Requires: +# - evm: Introduce evm_hmac_disabled() to safely ignore verification errors +# - evm: Allow xattr/attr operations for portable signatures +# - evm: Execute evm_inode_init_security() only when an HMAC key is loaded +# - ima: Don't remove security.ima if file must not be appraised +# +# The purpose of this test is similar to that of the previous test, with the +# difference that tar is used instead of cp. One remark is that the owner is +# intentionally different (or it should be) from the current owner, to +# incrementally test the patches without 'evm: Allow setxattr() and setattr() +# for unmodified metadata'. +check_tar_extract_xattrs_different_owner() { + echo "Test: ${FUNCNAME[0]} (evm_value: $evm_value)" + + if [ $evm_value -ne $EVM_INIT_X509 ]; then + echo "${CYAN}EVM mode $EVM_INIT_X509 required${NORM}" + return $SKIP + fi + + mkdir in out + if [ $? -ne 0 ]; then + echo "${RED}Cannot create directories${NORM}" + return $FAIL + fi + + echo "test" > in/test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot write test-file${NORM}" + return $FAIL + fi + + chown 3000 in/test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot change owner of test-file${NORM}" + return $FAIL + fi + + chmod 600 in/test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot change mode of test-file${NORM}" + return $FAIL + fi + + evmctl sign -o -a sha256 --imahash --key $key_path in/test-file &> /dev/null + if [ $? -ne 0 ]; then + echo "${RED}Cannot sign test-file${NORM}" + return $FAIL + fi + + tar --xattrs-include=* -cf test-archive.tar in/test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot create archive with xattrs${NORM}" + return $FAIL + fi + + # Check if tar is allowed to set metadata for the extracted file. + # Ensure that the owner from the archive is different from the + # owner of the extracted file to avoid that portable signature + # verification succeeds before restoring original metadata + # (a patch allows modification of immutable metadata if portable + # signature verification fails). + tar --xattrs-include=* -xf test-archive.tar -C out + if [ $? -ne 0 ]; then + echo "${RED}Cannot extract archive with xattrs${NORM}" + return $FAIL + fi + + return $OK +} + +cleanup_tar_extract_xattrs_different_owner() { + rm -Rf in out test-archive.tar +} + +# Requires: +# - evm: Introduce evm_hmac_disabled() to safely ignore verification errors +# - evm: Allow xattr/attr operations for portable signatures +# - evm: Pass user namespace to set/remove xattr hooks +# - evm: Allow setxattr() and setattr() for unmodified metadata +# - evm: Execute evm_inode_init_security() only when an HMAC key is loaded +# - ima: Don't remove security.ima if file must not be appraised +# +# The purpose of this test is similar to that of the previous two tests. The +# difference is that tar is used instead of cp, and the extracted files have +# the same owner as the current one. Thus, this test requires 'evm: Allow +# setxattr() and setattr() for unmodified metadata'. +check_tar_extract_xattrs_same_owner() { + echo "Test: ${FUNCNAME[0]} (evm_value: $evm_value)" + + if [ $evm_value -ne $EVM_INIT_X509 ]; then + echo "${CYAN}EVM mode $EVM_INIT_X509 required${NORM}" + return $SKIP + fi + + mkdir in out + if [ $? -ne 0 ]; then + echo "${RED}Cannot create directories${NORM}" + return $FAIL + fi + + echo "test" > in/test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot write test-file${NORM}" + return $FAIL + fi + + evmctl sign -o -a sha256 --imahash --key $key_path in/test-file &> /dev/null + if [ $? -ne 0 ]; then + echo "${RED}Cannot sign test-file${NORM}" + return $FAIL + fi + + tar --xattrs-include=* -cf test-archive.tar in/test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot create archive with xattrs${NORM}" + return $FAIL + fi + + # Check if tar is allowed to set metadata for the extracted file. + # This test is different from the previous one, as the owner + # from the archive is the same of the owner of the extracted + # file. tar will attempt anyway to restore the original owner but + # unlike the previous test, portable signature verification already + # succeeds at the time the owner is set (another patch allows + # metadata operations if those operations don't modify current + # values). + tar --xattrs-include=* -xf test-archive.tar -C out + if [ $? -ne 0 ]; then + echo "${RED}Cannot extract archive with xattrs${NORM}" + return $FAIL + fi + + return $OK +} + +cleanup_tar_extract_xattrs_same_owner() { + rm -Rf in out test-archive.tar +} + +# Requires: +# - evm: Introduce evm_hmac_disabled() to safely ignore verification errors +# - evm: Allow xattr/attr operations for portable signatures +# - evm: Pass user namespace to set/remove xattr hooks +# - evm: Allow setxattr() and setattr() for unmodified metadata +# - ima: Don't remove security.ima if file must not be appraised +# - evm: Execute evm_inode_init_security() only when an HMAC key is loaded +# +# The purpose of this test is to further verify the patches above, by executing +# commands to set the same or different metadata. Setting the same metadata +# should be allowed, setting different metadata should be denied. +check_metadata_change() { + echo "Test: ${FUNCNAME[0]} (evm_value: $evm_value)" + + if [ $evm_value -ne $EVM_INIT_X509 ]; then + echo "${CYAN}EVM mode $EVM_INIT_X509 required${NORM}" + return $SKIP + fi + + echo "test" > test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot write test-file${NORM}" + return $FAIL + fi + + chown $METADATA_CHANGE_FOWNER test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot change owner of test-file${NORM}" + return $FAIL + fi + + chgrp $METADATA_CHANGE_FOWNER test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot change group of test-file${NORM}" + return $FAIL + fi + + chmod 2644 test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot change mode of test-file${NORM}" + return $FAIL + fi + + evmctl sign -o -a sha256 --imahash --key $key_path test-file &> /dev/null + if [ $? -ne 0 ]; then + echo "${RED}Cannot sign test-file${NORM}" + return $FAIL + fi + + # If metadata modification is not allowed, EVM should deny any + # operation that modifies metadata. Check if setting the same + # value is allowed. + chown $METADATA_CHANGE_FOWNER test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot set same owner for test-file${NORM}" + return $FAIL + fi + + # Setting a different value should not be allowed. + chown $METADATA_CHANGE_FOWNER_2 test-file 2> /dev/null + if [ $? -eq 0 ]; then + echo "${RED}Owner change for test-file should not be allowed (immutable metadata)${NORM}" + return $FAIL + fi + + # Repeat the test for the file mode. + chmod 2644 test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot set same mode for test-file${NORM}" + return $FAIL + fi + + chmod 2666 test-file 2> /dev/null + if [ $? -eq 0 ]; then + echo "${RED}Mode change for test-file should not be allowed (immutable metadata)${NORM}" + return $FAIL + fi + + if [ -n "$(which chcon 2> /dev/null)" ] && [ -n "$(which getenforce 2> /dev/null)" ] && [ "$(getenforce 2> /dev/null)" != "Disabled" ]; then + # Repeat the test for the SELinux label. + label=$(get_xattr security.selinux test-file) + + if [ -n "$label" ]; then + chcon $label test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot set same security.selinux for test-file${NORM}" + return $FAIL + fi + fi + + chcon unconfined_u:object_r:null_device_t:s0 test-file 2> /dev/null + if [ $? -eq 0 ]; then + echo "${RED}security.selinux change for test file should not be allowed (immutable metadata)${NORM}" + return $FAIL + fi + fi + + # Repeat the test for the IMA signature. + ima_xattr=$(get_xattr security.ima test-file) + if [ -z "$ima_xattr" ]; then + echo "${RED}security.ima not found${NORM}" + return $FAIL + fi + + setfattr -n security.ima -v 0x$ima_xattr test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot set same security.ima for test-file${NORM}" + return $FAIL + fi + + last_char=${ima_xattr: -1} + ((last_char += 1)) + ((last_char %= 10)) + ima_xattr=${ima_xattr:0:-1}$last_char + + setfattr -n security.ima -v 0x$ima_xattr test-file 2> /dev/null + if [ $? -eq 0 ]; then + echo "${RED}Change of security.ima for test-file should not be allowed (immutable metadata)${NORM}" + return $FAIL + fi + + # Repeat the test for ACLs. + msg=$(exec 2>&1 && setfacl --set u::rw,g::r,o::r,m:r test-file) + if [ $? -ne 0 ]; then + if [ "${msg%not supported}" != "$msg" ]; then + return $OK + fi + + echo "${RED}Cannot preserve system.posix_acl_access for test-file${NORM}" + return $FAIL + fi + + setfacl --set u::rw,g::r,o::r,m:rw test-file 2> /dev/null + if [ $? -eq 0 ]; then + echo "${RED}Change of system.posix_acl_access for test-file should not be allowed (immutable metadata)${NORM}" + return $FAIL + fi + + if [ -n "$mountpoint_idmapped" ]; then + pushd $mountpoint_idmapped > /dev/null + + # Repeat the test for ACLs on an idmapped mount. + # + # This test relies on the fact that the caller of this script (root) is in + # the same owning group of test-file (in the idmapped mount the group is + # root, not $METADATA_CHANGE_FOWNER and, for this reason, the S_ISGID bit + # is not cleared. If EVM was not aware of the mapping, it would have + # determined that root is not in the owning group of test-file and given + # that also CAP_FSETID is cleared, the S_ISGID bit would have been cleared + # and thus the operation would fail (file metadata changed). + capsh --drop='cap_fsetid' -- -c 'setfacl --set u::rw,g::r,o::r test-file' + if [ $? -ne 0 ]; then + echo "${RED}Cannot preserve system.posix_acl_access for test-file${NORM}" + popd + return $FAIL + fi + + popd > /dev/null + fi + + return $OK +} + +cleanup_metadata_change() { + rm -f test-file +} + +# Requires: +# - evm: Introduce evm_revalidate_status() +# - evm: Execute evm_inode_init_security() only when an HMAC key is loaded +# +# Note: +# This test can be run if EVM_ALLOW_METADATA_WRITES is set in advance +# before running this script. If it is not set before, this script sets +# EVM_SETUP_COMPLETE, disabling further EVM mode modifications until reboot. +# +# Without EVM_ALLOW_METADATA_WRITES, EVM_SETUP_COMPLETE is necessary to ignore +# the INTEGRITY_NOLABEL and INTEGRITY_NOXATTRS errors. +# +# The purpose of this test is to verify that IMA detected a metadata change +# when EVM_ALLOW_METADATA_WRITES is set (metadata operations are always +# allowed). After the first successful appraisal, the test intentionally changes +# metadata and verifies that IMA revoked access to the file. The test also +# verifies that IMA grants access again to the file after restoring the correct +# metadata. +check_evm_revalidate() { + echo "Test: ${FUNCNAME[0]} (evm_value: $evm_value)" + + if [ $evm_value -ne $(($EVM_INIT_X509 | $EVM_ALLOW_METADATA_WRITES)) ]; then + echo "${CYAN}EVM mode $(($EVM_INIT_X509 | $EVM_ALLOW_METADATA_WRITES)) required, execute echo 4 > /sys/kernel/security/evm before running this test${NORM}" + return $SKIP + fi + + echo "test" > test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot write test-file${NORM}" + return $FAIL + fi + + chmod 600 test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot change mode of test-file${NORM}" + return $FAIL + fi + + # We need to defer setting the correct owner, as there could be + # already an IMA policy rule preventing evmctl from reading the + # file to calculate the digest. + evmctl sign -o -a sha256 --imahash --uid $APPRAISE_FOWNER --key $key_path test-file &> /dev/null + if [ $? -ne 0 ]; then + echo "${RED}Cannot sign test-file${NORM}" + return $FAIL + fi + + chown $APPRAISE_FOWNER test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot change owner of test-file${NORM}" + return $FAIL + fi + + check_load_ima_rule "$APPRAISE_RULE" + result=$? + if [ $result -ne $OK ]; then + return $result + fi + + # Read the file so that IMA would not re-appraise it next time. + cat test-file &> /dev/null + if [ $? -ne 0 ]; then + echo "${RED}Cannot read test-file${NORM}" + return $FAIL + fi + + # After enabling metadata modification, operations should succeed even + # if the file has a portable signature. However, the previously cached + # appraisal status should be invalidated. + chmod 644 test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot change mode of test-file${NORM}" + return $FAIL + fi + + # Here check if IMA re-appraised the file. The read should fail + # since now file metadata is invalid. + cat test-file &> /dev/null + if [ $? -eq 0 ]; then + echo "${RED}Read of test-file should not succeed (invalid mode)${NORM}" + return $FAIL + fi + + # Restore metadata back to the original value. + chmod 600 test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot restore original mode of test-file${NORM}" + return $FAIL + fi + + # Ensure that now IMA appraisal succeeds. + cat test-file > /dev/null + if [ $? -ne 0 ]; then + echo "${RED}Cannot read test-file after restoring correct mode${NORM}" + return $FAIL + fi + + if [ -n "$(which chcon 2> /dev/null)" ] && [ -n "$(which getenforce 2> /dev/null)" ] && [ "$(getenforce 2> /dev/null)" != "Disabled" ]; then + # Repeat the test for the SELinux label. + label=$(get_xattr security.selinux test-file) + + chcon unconfined_u:object_r:null_device_t:s0 test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot change security.selinux of test-file${NORM}" + return $FAIL + fi + + cat test-file &> /dev/null + if [ $? -eq 0 ]; then + echo "${RED}Read of test-file should not succeed (invalid security.selinux)${NORM}" + return $FAIL + fi + + if [ -n "$label" ]; then + chcon $label test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot restore original security.selinux of test-file${NORM}" + return $FAIL + fi + else + attr -S -r selinux test-file + fi + + cat test-file > /dev/null + if [ $? -ne 0 ]; then + echo "${RED}Cannot read test-file after restoring correct security.selinux${NORM}" + return $FAIL + fi + fi + + # Repeat the test for the IMA signature. + ima_xattr=$(get_xattr security.ima test-file) + if [ -z "$ima_xattr" ]; then + echo "${RED}security.ima not found${NORM}" + return $FAIL + fi + + last_char=${ima_xattr: -1} + ((last_char += 1)) + ((last_char %= 10)) + ima_xattr_new=${ima_xattr:0:-1}$last_char + + setfattr -n security.ima -v 0x$ima_xattr_new test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot set security.ima of test-file${NORM}" + return $FAIL + fi + + cat test-file &> /dev/null + if [ $? -eq 0 ]; then + echo "${RED}Read of test-file should not succeed (invalid security.ima)${NORM}" + return $FAIL + fi + + setfattr -n security.ima -v 0x$ima_xattr test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot restore original security.ima of test-file${NORM}" + return $FAIL + fi + + cat test-file > /dev/null + if [ $? -ne 0 ]; then + echo "${RED}Cannot read test-file after restoring correct security.ima${NORM}" + return $FAIL + fi + + # Repeat the test for the EVM signature. + evm_xattr=$(get_xattr security.evm test-file) + if [ -z "$evm_xattr" ]; then + echo "${RED}security.evm not found${NORM}" + return $FAIL + fi + + last_char=${evm_xattr: -1} + ((last_char += 1)) + ((last_char %= 10)) + evm_xattr_new=${evm_xattr:0:-1}$last_char + + setfattr -n security.evm -v 0x$evm_xattr_new test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot set security.evm of test-file${NORM}" + return $FAIL + fi + + cat test-file &> /dev/null + if [ $? -eq 0 ]; then + echo "${RED}Read of test-file should not succeed (invalid security.evm)${NORM}" + return $FAIL + fi + + setfattr -n security.evm -v 0x$evm_xattr test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot restore original security.evm of test-file${NORM}" + return $FAIL + fi + + cat test-file > /dev/null + if [ $? -ne 0 ]; then + echo "${RED}Cannot read test-file after restoring correct security.evm${NORM}" + return $FAIL + fi + + # Repeat the test for ACLs. + setfacl -m u::rwx test-file 2> /dev/null + if [ $? -ne 0 ]; then + echo "${RED}Cannot change system.posix_acl_access${NORM}" + return $FAIL + fi + + cat test-file &> /dev/null + if [ $? -eq 0 ]; then + echo "${RED}Read of test-file should not succeed (invalid system.posix_acl_access)${NORM}" + return $FAIL + fi + + setfacl -m u::rw test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot restore original system.posix_acl_access for test-file${NORM}" + return $FAIL + fi + + cat test-file > /dev/null + if [ $? -ne 0 ]; then + echo "${RED}Cannot read test-file after restoring correct system.posix_acl_access${NORM}" + return $FAIL + fi + + return $OK +} + +cleanup_evm_revalidate() { + rm -f test-file +} + +# Requires: +# - evm: Introduce evm_hmac_disabled() to safely ignore verification errors +# - evm: Introduce evm_revalidate_status() +# - ima: Allow imasig requirement to be satisfied by EVM portable signatures +# - evm: Execute evm_inode_init_security() only when an HMAC key is loaded +# +# The purpose of this test is to verify that IMA manages files with an EVM +# portable signature similarly to those with an IMA signature: content can be +# written to new files after adding the signature and files can be accessed +# when the imasig requirement is specified in the IMA policy. +check_evm_portable_sig_ima_appraisal() { + echo "Test: ${FUNCNAME[0]} (evm_value: $evm_value)" + + if [ $((evm_value & EVM_INIT_X509)) -ne $EVM_INIT_X509 ]; then + echo "${CYAN}EVM flag $EVM_INIT_X509 required${NORM}" + return $SKIP + fi + + echo "test" > test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot write test-file${NORM}" + return $FAIL + fi + + chmod 600 test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot change mode of test-file${NORM}" + return $FAIL + fi + + # We need to defer setting the correct owner, as there could be + # already an IMA policy rule preventing evmctl from reading the + # file to calculate the digest. + evmctl sign -o -a sha256 --imahash --uid $APPRAISE_DIGSIG_FOWNER --key $key_path test-file &> /dev/null + if [ $? -ne 0 ]; then + echo "${RED}Cannot sign test-file${NORM}" + return $FAIL + fi + + chown $APPRAISE_DIGSIG_FOWNER test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot change owner of test-file${NORM}" + return $FAIL + fi + + check_load_ima_rule "$APPRAISE_DIGSIG_RULE" + result=$? + if [ $result -ne $OK ]; then + return $result + fi + + # Ensure that a file with a portable signature satisfies the + # appraise_type=imasig requirement specified in the IMA policy. + cat test-file > /dev/null + if [ $? -ne 0 ]; then + echo "${RED}Cannot read test-file${NORM}" + return $FAIL + fi + + # Even files with a portable signature should be considered as + # immutable by IMA. Write should fail. + echo "test" 2> /dev/null >> test-file + if [ $? -eq 0 ]; then + echo "${RED}Write to test-file should not succeed (immutable metadata)${NORM}" + return $FAIL + fi + + tar --xattrs-include=* -cf test-archive.tar test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot create archive with xattrs${NORM}" + return $FAIL + fi + + mkdir out + + # Appraisal of the new file, extracted by tar, should succeed + # not only if the new file has an IMA signature but also if + # it has a portable signature. + tar --xattrs-include=* -xf test-archive.tar -C out + if [ $? -ne 0 ]; then + echo "${RED}Cannot extract archive with xattrs${NORM}" + return $FAIL + fi + + # Check if xattrs have been correctly set. + xattr_orig=$(get_xattr security.selinux test-file) + xattr=$(get_xattr security.selinux out/test-file) + if [ "$xattr" != "$xattr_orig" ]; then + echo "${RED}security.selinux mismatch between original and extracted file${NORM}" + return $FAIL + fi + + xattr_orig=$(get_xattr security.ima test-file) + xattr=$(get_xattr security.ima out/test-file) + if [ "$xattr" != "$xattr_orig" ]; then + echo "${RED}security.ima mismatch between original and extracted file${NORM}" + return $FAIL + fi + + xattr_orig=$(get_xattr security.evm test-file) + xattr=$(get_xattr security.evm out/test-file) + if [ "$xattr" != "$xattr_orig" ]; then + echo "${RED}security.evm mismatch between original and extracted file${NORM}" + return $FAIL + fi + + # Check if attrs have been correctly set. + owner=$(stat -c "%u" out/test-file) + if [ "$owner" != "$APPRAISE_DIGSIG_FOWNER" ]; then + echo "${RED}owner mismatch between original and extracted file${NORM}" + return $FAIL + fi + + mode=$(stat -c "%a" out/test-file) + if [ "$mode" != "600" ]; then + echo "${RED}mode mismatch between original and extracted file${NORM}" + return $FAIL + fi + + return $OK +} + +cleanup_evm_portable_sig_ima_appraisal() { + rm -f test-file test-archive.tar + rm -Rf out +} + +# Requires: +# - ima: Introduce template field evmsig and write to field sig as fallback +# - evm: Execute evm_inode_init_security() only when an HMAC key is loaded +# - ima: Don't remove security.ima if file must not be appraised +# +# The purpose of this test is to verify that the EVM portable signature is +# displayed in the measurement list. +check_evm_portable_sig_ima_measurement_list() { + echo "Test: ${FUNCNAME[0]} (evm_value: $evm_value)" + + echo "test" > test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot write test-file${NORM}" + return $FAIL + fi + + chown $MEASURE_FOWNER test-file + if [ $? -ne 0 ]; then + echo "${RED}Cannot change owner of test-file${NORM}" + return $FAIL + fi + + evmctl sign -o -a sha256 --imahash --key $key_path test-file &> /dev/null + if [ $? -ne 0 ]; then + echo "${RED}Cannot sign test-file${NORM}" + return $FAIL + fi + + check_load_ima_rule "$MEASURE_RULE" + result=$? + if [ $result -ne $OK ]; then + return $result + fi + + # Invalidate previous measurement to add new entry + touch test-file + + # Read the file to add it to the measurement list. + cat test-file > /dev/null + if [ $? -ne 0 ]; then + echo "${RED}Cannot read test-file${NORM}" + return $FAIL + fi + + evm_sig_fs=$(get_xattr security.evm test-file) + if [ -z "$evm_sig_fs" ]; then + echo "${RED}security.evm not found${NORM}" + return $FAIL + fi + + # Search security.evm in the measurement list. + evm_sig_list=$(cat /sys/kernel/security/ima/ascii_runtime_measurements | awk '$6 == "'$evm_sig_fs'"') + if [ -z "$evm_sig_list" ]; then + echo "${RED}security.evm mismatch (xattr != measurement list)${NORM}" + return $FAIL + fi + + return $OK +} + +cleanup_evm_portable_sig_ima_measurement_list() { + rm -f test-file +} + +# Run in User Mode Linux. +_run_user_mode ../linux $PWD/$(basename $0) "PATH=$PATH LD_LIBRARY_PATH=$LD_LIBRARY_PATH VERBOSE=$VERBOSE TST_EVM_CHANGE_MODE=$TST_EVM_CHANGE_MODE TST_KEY_PATH=$TST_KEY_PATH" + +# Run in User Mode Linux (skipped test). +_run_user_mode ../linux $PWD/$(basename $0) "PATH=$PATH LD_LIBRARY_PATH=$LD_LIBRARY_PATH VERBOSE=$VERBOSE TST_EVM_CHANGE_MODE=$TST_EVM_CHANGE_MODE TST_KEY_PATH=$TST_KEY_PATH TST_LIST=check_evm_revalidate" + +# Exit from the parent if UML was used. +_exit_user_mode ../linux + +# Mount filesystems in UML environment. +_init_user_mode + +mountpoint=$(mktemp -d) +image=$(mktemp) + +if [ -z "$mountpoint" ]; then + echo "${RED}Mountpoint directory not created${NORM}" + exit $FAIL +fi + +if [ $(whoami) != "root" ]; then + echo "${CYAN}This script must be executed as root${NORM}" + exit $SKIP +fi + +key_path="/lib/modules/$(uname -r)/source/certs/signing_key.pem" +if [ -f "$PWD/../signing_key.pem" ]; then + key_path=$PWD/../signing_key.pem +fi + +if [ -n "$TST_KEY_PATH" ]; then + key_path=$TST_KEY_PATH +fi + +if [ ${key_path:0:1} != "/" ]; then + echo "${RED}Absolute path required for the signing key${NORM}" + exit $FAIL +fi + +key_path_der=$(mktemp) + +if [ ! -f $key_path ]; then + echo "${CYAN}Kernel signing key not found in $key_path${NORM}" + exit $SKIP +fi + +if [ ! -f "/sys/kernel/security/evm" ]; then + echo "${CYAN}EVM support in the kernel disabled${NORM}" + exit $SKIP +fi + +# Assume that the EVM mode can be changed in a UML kernel +if [ -f $PWD/../linux ]; then + TST_EVM_CHANGE_MODE=1 +fi + +evm_value=$(cat /sys/kernel/security/evm) + +openssl x509 -in $key_path -out $key_path_der -outform der +cat $key_path_der | keyctl padd asymmetric pubkey %keyring:.ima &> /dev/null +if [ $? -ne 0 ]; then + echo "${RED}Public key cannot be added to the IMA keyring${NORM}" + exit $FAIL +fi + +dd if=/dev/zero of=$image bs=1M count=20 &> /dev/null +if [ $? -ne 0 ]; then + echo "${RED}Cannot create test image${NORM}" + exit $FAIL +fi + +dev=$(losetup -f $image --show) +if [ -z "$dev" ]; then + echo "${RED}Cannot create loop device${NORM}" + exit $FAIL +fi + +mkfs.ext4 -U $IMA_UUID -b 4096 $dev &> /dev/null +if [ $? -ne 0 ]; then + echo "${RED}Cannot format $dev${NORM}" + exit $FAIL +fi + +mount -o i_version $dev $mountpoint +if [ $? -ne 0 ]; then + echo "${RED}Cannot mount loop device${NORM}" + exit $FAIL +fi + +if [ -f ../mount-idmapped ]; then + mountpoint_idmapped=$(mktemp -d) + ../mount-idmapped --map-mount b:$METADATA_CHANGE_FOWNER:0:1 $mountpoint $mountpoint_idmapped + if [ $? -ne 0 ]; then + echo "${RED}mount-idmapped failed${NORM}" + exit $FAIL + fi +fi + +loop_mounted=1 +pushd $mountpoint > /dev/null + +expect_pass check_ima_sig_appraisal +cleanup_ima_sig_appraisal +expect_pass check_ima_sig_ima_measurement_list +cleanup_ima_sig_ima_measurement_list + +if [ $(echo -e "$(uname -r)\n5.12" | sort -V | head -n 1) != "5.12" ]; then + exit $OK +fi + +if [ $((evm_value & EVM_INIT_X509)) -ne $EVM_INIT_X509 ] && [ "$TST_EVM_CHANGE_MODE" -eq 1 ]; then + cat $key_path_der | keyctl padd asymmetric pubkey %keyring:.evm &> /dev/null + if [ $? -ne 0 ]; then + echo "${RED}Public key cannot be added to the EVM keyring${NORM}" + exit $FAIL + fi + + echo $EVM_INIT_X509 > /sys/kernel/security/evm 2> /dev/null +fi + +if [ $(expr index "$TST_LIST" "check_evm_revalidate") -gt 0 ] && [ "$TST_EVM_CHANGE_MODE" -eq 1 ]; then + echo $EVM_ALLOW_METADATA_WRITES > /sys/kernel/security/evm 2> /dev/null +fi + +# We cannot determine from securityfs if EVM_SETUP_COMPLETE is set, so we set it unless EVM_ALLOW_METADATA_WRITES is set. +if [ $((evm_value & EVM_ALLOW_METADATA_WRITES)) -ne $EVM_ALLOW_METADATA_WRITES ] && [ "$TST_EVM_CHANGE_MODE" -eq 1 ]; then + echo $EVM_SETUP_COMPLETE > /sys/kernel/security/evm 2> /dev/null +fi + +evm_value=$(cat /sys/kernel/security/evm) + +expect_pass check_create_file +cleanup_create_file +expect_pass check_cp_preserve_xattrs +cleanup_cp_preserve_xattrs +expect_pass check_tar_extract_xattrs_different_owner +cleanup_tar_extract_xattrs_different_owner +expect_pass check_tar_extract_xattrs_same_owner +cleanup_tar_extract_xattrs_same_owner +expect_pass check_metadata_change +cleanup_metadata_change +expect_pass check_evm_revalidate +cleanup_evm_revalidate +expect_pass check_evm_portable_sig_ima_appraisal +cleanup_evm_portable_sig_ima_appraisal +expect_pass check_evm_portable_sig_ima_measurement_list +cleanup_evm_portable_sig_ima_measurement_list