From nobody Sat Sep 13 22:03:07 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F1B26C76186 for ; Mon, 30 Jan 2023 22:59:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231182AbjA3W7H (ORCPT ); Mon, 30 Jan 2023 17:59:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230091AbjA3W6s (ORCPT ); Mon, 30 Jan 2023 17:58:48 -0500 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id AB89B20D1A; Mon, 30 Jan 2023 14:58:45 -0800 (PST) Received: by linux.microsoft.com (Postfix, from userid 1052) id BA5F220EA219; Mon, 30 Jan 2023 14:58:42 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com BA5F220EA219 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1675119522; bh=Kx48hiiDSWzOoufluPZBRSxdRqmQO6sj1TtE+pEDBTo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ptpgwa1eoH+Bao3UzUTFYOJb8yu2qloYWMOIMNvWxctUvv1XpQdzN4YhWkdAPwuVE 9EiIVLsZIIEOvfNSbAv/j6SxraBhbK3rqeK6R7aNxRUxhe0UjGIISbg5N9QbnT1T2r Ihol8apa7hd96REMRyuB3fnqkbuSQE6b/4gbhDUY= From: Fan Wu To: corbet@lwn.net, zohar@linux.ibm.com, jmorris@namei.org, serge@hallyn.com, tytso@mit.edu, ebiggers@kernel.org, axboe@kernel.dk, agk@redhat.com, snitzer@kernel.org, eparis@redhat.com, paul@paul-moore.com Cc: linux-doc@vger.kernel.org, linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, linux-fscrypt@vger.kernel.org, linux-block@vger.kernel.org, dm-devel@redhat.com, linux-audit@redhat.com, roberto.sassu@huawei.com, linux-kernel@vger.kernel.org, Deven Bowers , Fan Wu Subject: [RFC PATCH v9 15/16] ipe: kunit test for parser Date: Mon, 30 Jan 2023 14:57:30 -0800 Message-Id: <1675119451-23180-16-git-send-email-wufan@linux.microsoft.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1675119451-23180-1-git-send-email-wufan@linux.microsoft.com> References: <1675119451-23180-1-git-send-email-wufan@linux.microsoft.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Deven Bowers Add various happy/unhappy unit tests for both IPE's parser. Signed-off-by: Deven Bowers Signed-off-by: Fan Wu --- v1-v6: + Not present v7: Introduced v8: + Remove the kunit tests with respect to the fsverity digest, as these require significant changes to work with the new method of acquiring the digest at runtime. v9: + Remove the kunit tests related to ipe_context --- security/ipe/Kconfig | 17 +++ security/ipe/Makefile | 3 + security/ipe/policy_tests.c | 294 ++++++++++++++++++++++++++++++++++++ 3 files changed, 314 insertions(+) create mode 100644 security/ipe/policy_tests.c diff --git a/security/ipe/Kconfig b/security/ipe/Kconfig index 691fdb9ae60e..79e4fd677c19 100644 --- a/security/ipe/Kconfig +++ b/security/ipe/Kconfig @@ -55,4 +55,21 @@ config IPE_PROP_FS_VERITY =20 endmenu =20 +config SECURITY_IPE_KUNIT_TEST + bool "Build KUnit tests for IPE" if !KUNIT_ALL_TESTS + depends on KUNIT=3Dy + default KUNIT_ALL_TESTS + help + This builds the IPE KUnit tests. + + KUnit tests run during boot and output the results to the debug log + in TAP format (https://testanything.org/). Only useful for kernel devs + running KUnit test harness and are not for inclusion into a + production build. + + For more information on KUnit and unit tests in general please refer + to the KUnit documentation in Documentation/dev-tools/kunit/. + + If unsure, say N. + endif diff --git a/security/ipe/Makefile b/security/ipe/Makefile index e6d5176bc20b..285e0949db25 100644 --- a/security/ipe/Makefile +++ b/security/ipe/Makefile @@ -27,3 +27,6 @@ obj-$(CONFIG_SECURITY_IPE) +=3D \ audit.o \ =20 clean-files :=3D boot-policy.c \ + +obj-$(CONFIG_SECURITY_IPE_KUNIT_TEST) +=3D \ + policy_tests.o \ diff --git a/security/ipe/policy_tests.c b/security/ipe/policy_tests.c new file mode 100644 index 000000000000..d09a1aca863d --- /dev/null +++ b/security/ipe/policy_tests.c @@ -0,0 +1,294 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) Microsoft Corporation. All rights reserved. + */ + +#include +#include +#include +#include +#include "policy.h" +struct policy_case { + const char *const policy; + int errno; + const char *const desc; +}; + +static const struct policy_case policy_cases[] =3D { + { + "policy_name=3Dallowall policy_version=3D0.0.0\n" + "DEFAULT action=3DALLOW", + 0, + "basic", + }, + { + "policy_name=3Dtrailing_comment policy_version=3D152.0.0 #This is commen= t\n" + "DEFAULT action=3DALLOW", + 0, + "trailing comment", + }, + { + "policy_name=3Dallowallnewline policy_version=3D0.2.0\n" + "DEFAULT action=3DALLOW\n" + "\n", + 0, + "trailing newline", + }, + { + "policy_name=3Dcarriagereturnlinefeed policy_version=3D0.0.1\n" + "DEFAULT action=3DALLOW\n" + "\r\n", + 0, + "clrf newline", + }, + { + "policy_name=3Dwhitespace policy_version=3D0.0.0\n" + "DEFAULT\taction=3DALLOW\n" + " \t DEFAULT \t op=3DEXECUTE action=3DDENY\n" + "op=3DEXECUTE boot_verified=3DTRUE action=3DALLOW\n" + "# this is a\tcomment\t\t\t\t\n" + "DEFAULT \t op=3DKMODULE\t\t\t action=3DDENY\r\n" + "op=3DKMODULE boot_verified=3DTRUE action=3DALLOW\n", + 0, + "various whitespaces and nested default", + }, + { + "policy_name=3Dboot_verified policy_version=3D-1236.0.0\n" + "DEFAULT\taction=3DALLOW\n", + -EINVAL, + "negative version", + }, + { + "policy_name=3D$@!*&^%%\\:;{}() policy_version=3D0.0.0\n" + "DEFAULT action=3DALLOW", + 0, + "special characters", + }, + { + "policy_name=3Dtest policy_version=3D999999.0.0\n" + "DEFAULT action=3DALLOW", + -ERANGE, + "overflow version", + }, + { + "policy_name=3Dtest policy_version=3D255.0\n" + "DEFAULT action=3DALLOW", + -EBADMSG, + "incomplete version", + }, + { + "policy_name=3Dtest policy_version=3D111.0.0.0\n" + "DEFAULT action=3DALLOW", + -EBADMSG, + "extra version", + }, + { + "", + -EBADMSG, + "0-length policy", + }, + { + "policy_name=3Dtest\0policy_version=3D0.0.0\n" + "DEFAULT action=3DALLOW", + -EBADMSG, + "random null in header", + }, + { + "policy_name=3Dtest policy_version=3D0.0.0\n" + "\0DEFAULT action=3DALLOW", + -EBADMSG, + "incomplete policy from NULL", + }, + { + "policy_name=3Dtest policy_version=3D0.0.0\n" + "DEFAULT action=3DDENY\n\0" + "op=3DEXECUTE dmverity_signature=3DTRUE action=3DALLOW\n", + 0, + "NULL truncates policy", + }, + { + "policy_name=3Dtest policy_version=3D0.0.0\n" + "DEFAULT action=3DALLOW\n" + "op=3DEXECUTE dmverity_signature=3Dabc action=3DALLOW", + -EBADMSG, + "invalid property type", + }, + { + "DEFAULT action=3DALLOW", + -EBADMSG, + "missing policy header", + }, + { + "policy_name=3Dtest policy_version=3D0.0.0\n", + -EBADMSG, + "missing default definition", + }, + { + "policy_name=3Dtest policy_version=3D0.0.0\n" + "DEFAULT action=3DALLOW\n" + "dmverity_signature=3DTRUE op=3DEXECUTE action=3DALLOW", + -EBADMSG, + "invalid rule ordering" + }, + { + "policy_name=3Dtest policy_version=3D0.0.0\n" + "DEFAULT action=3DALLOW\n" + "action=3DALLOW op=3DEXECUTE dmverity_signature=3DTRUE", + -EBADMSG, + "invalid rule ordering (2)", + }, + { + "policy_name=3Dtest policy_version=3D0.0\n" + "DEFAULT action=3DALLOW\n" + "op=3DEXECUTE dmverity_signature=3DTRUE action=3DALLOW", + -EBADMSG, + "invalid version", + }, + { + "policy_name=3Dtest policy_version=3D0.0.0\n" + "DEFAULT action=3DALLOW\n" + "op=3DUNKNOWN dmverity_signature=3DTRUE action=3DALLOW", + -EBADMSG, + "unknown operation", + }, + { + "policy_name=3Dasdvpolicy_version=3D0.0.0\n" + "DEFAULT action=3DALLOW\n", + -EBADMSG, + "missing space after policy name", + }, + { + "policy_name=3Dtest\xFF\xEF policy_version=3D0.0.0\n" + "DEFAULT action=3DALLOW\n" + "op=3DEXECUTE dmverity_signature=3DTRUE action=3DALLOW", + 0, + "expanded ascii", + }, + { + "policy_name=3Dtest\xFF\xEF policy_version=3D0.0.0\n" + "DEFAULT action=3DALLOW\n" + "op=3DEXECUTE dmverity_roothash=3DGOOD_DOG action=3DALLOW", + -EBADMSG, + "invalid property value (2)", + }, + { + "policy_name=3Dtest policy_version=3D0.0.0\n" + "policy_name=3Dtest policy_version=3D0.1.0\n" + "DEFAULT action=3DALLOW", + -EBADMSG, + "double header" + }, + { + "policy_name=3Dtest policy_version=3D0.0.0\n" + "DEFAULT action=3DALLOW\n" + "DEFAULT action=3DALLOW\n", + -EBADMSG, + "double default" + }, + { + "policy_name=3Dtest policy_version=3D0.0.0\n" + "DEFAULT action=3DALLOW\n" + "DEFAULT op=3DEXECUTE action=3DDENY\n" + "DEFAULT op=3DEXECUTE action=3DALLOW\n", + -EBADMSG, + "double operation default" + }, + { + "policy_name=3Dtest policy_version=3D0.0.0\n" + "DEFAULT action=3DALLOW\n" + "DEFAULT op=3DEXECUTE action=3DDEN\n", + -EBADMSG, + "invalid action value" + }, + { + "policy_name=3Dtest policy_version=3D0.0.0\n" + "DEFAULT action=3DALLOW\n" + "DEFAULT op=3DEXECUTE action\n", + -EBADMSG, + "invalid action value (2)" + }, + { + "policy_name=3Dtest policy_version=3D0.0.0\n" + "DEFAULT action=3DALLOW\n" + "UNKNOWN value=3Dtrue\n", + -EBADMSG, + "unrecognized statement" + }, + { + "policy_name=3Dtest policy_version=3D0.0.0\n" + "DEFAULT action=3DALLOW\n" + "op=3DEXECUTE dmverity_roothash=3D1c0d7ee1f8343b7fbe418378e8eb22c061d7de= c7 action=3DDENY\n", + -EBADMSG, + "old-style digest" + }, + { + "policy_name=3Dtest policy_version=3D0.0.0\n" + "DEFAULT action=3DALLOW\n" + "op=3DEXECUTE fsverity_digest=3D1c0d7ee1f8343b7fbe418378e8eb22c061d7dec7= action=3DDENY\n", + -EBADMSG, + "old-style digest" + } +}; + +static void pol_to_desc(const struct policy_case *c, char *desc) +{ + strncpy(desc, c->desc, KUNIT_PARAM_DESC_SIZE); +} + +KUNIT_ARRAY_PARAM(ipe_policies, policy_cases, pol_to_desc); + +/** + * ipe_parser_unsigned_test - Test the paser by passing unsigned policies. + * @test: Supplies a pointer to a kunit structure. + * + * This is called by the kunit harness. This test does not check the corre= ctness + * of the policy, but ensures that errors are handled correctly. + */ +static void ipe_parser_unsigned_test(struct kunit *test) +{ + const struct policy_case *p =3D test->param_value; + struct ipe_policy *pol =3D ipe_new_policy(p->policy, strlen(p->policy), N= ULL, 0); + + if (p->errno) { + KUNIT_EXPECT_EQ(test, PTR_ERR(pol), p->errno); + return; + } + + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pol); + KUNIT_EXPECT_NOT_ERR_OR_NULL(test, pol->parsed); + KUNIT_EXPECT_STREQ(test, pol->text, p->policy); + KUNIT_EXPECT_PTR_EQ(test, NULL, pol->pkcs7); + KUNIT_EXPECT_EQ(test, 0, pol->pkcs7len); + + ipe_free_policy(pol); +} + +/** + * ipe_parser_widestring_test - Ensure parser fail on a wide string policy. + * @test: Supplies a pointer to a kunit structure. + * + * This is called by the kunit harness. + */ +static void ipe_parser_widestring_test(struct kunit *test) +{ + struct ipe_policy *pol =3D NULL; + const unsigned short policy[] =3D L"policy_name=3DTest policy_version=3D0= .0.0\n" + L"DEFAULT action=3DALLOW"; + + pol =3D ipe_new_policy((const char *)policy, (ARRAY_SIZE(policy) - 1) * 2= , NULL, 0); + KUNIT_EXPECT_TRUE(test, IS_ERR_OR_NULL(pol)); + + ipe_free_policy(pol); +} + +static struct kunit_case ipe_parser_test_cases[] =3D { + KUNIT_CASE_PARAM(ipe_parser_unsigned_test, ipe_policies_gen_params), + KUNIT_CASE(ipe_parser_widestring_test), +}; + +static struct kunit_suite ipe_parser_test_suite =3D { + .name =3D "ipe-parser", + .test_cases =3D ipe_parser_test_cases, +}; + +kunit_test_suite(ipe_parser_test_suite); --=20 2.39.0