From patchwork Wed Jul 28 13:21:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: THOBY Simon X-Patchwork-Id: 12405621 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=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 886E4C4320A for ; Wed, 28 Jul 2021 13:21:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 717F060FED for ; Wed, 28 Jul 2021 13:21:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233315AbhG1NVe (ORCPT ); Wed, 28 Jul 2021 09:21:34 -0400 Received: from mail-eopbgr00100.outbound.protection.outlook.com ([40.107.0.100]:63456 "EHLO EUR02-AM5-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S236259AbhG1NVb (ORCPT ); Wed, 28 Jul 2021 09:21:31 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HHYMSTHc9NyNajbSEbWbEMsQS5cxbmEnSsyCv7LUa8rLzcU5Zbx+tQpI1ptT3TWsm7GKjc5wu8dJ2ntHX0+ZJNbSlcbJ6+5QLP1m1cxQgh/altoX+aZbT8UUWXnyHVfziImyp95ApatmBJPAev4u9ktWXFiPyXF920WPHqIiG8H/rXO/dIdtDPrd3KcCrH0/LapWzmoc56YGj8ZT4RzI6WeGYMTxKT32SCtPlaa01GFfereqLvqCVu8M1y6Irbec1YqGoS+gdbjXwJXVVDovZOxVgF+0nvmmci3jX8SMTe3Ytt9+62o2xcR+Inrmww3S1Di2DGzEiL+yn+kbrYq0+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=t9HR3FpasfmEXN9zKXbw9/BDA2cEMOli7CYojW0/xr4=; b=obPqQVtnRRQAlMvPAGQ1J1rm7PvrumxgnDnoBG969zc5m50nQ3m+IMZ7lmxzOIVeLGybebfFo70VUkRsG3vY03BVwzvNo2nfl5tuwbi9gWkcmLeAccWPrwNuvUiypmfiNJqmWxTIi9PhbeUyOp6FWP+3fTyfAwX86eJ4lGQMZztW9aDCjhSYQ+d2aou0SRF/DyhB0FJlVAtOmcEdc93HcSsTnBCLfgq3XXcmKayG1umXzyWMVtX8ALW2hcQnvWjX9pqwR5h3kslZdPvaVRhZrNxoEeqqEWtwl8GQP0kTXcvtgVTtMN43Xz+uFAin7xNsFsMMt7Acvd/2pzpfs3utjQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=viveris.fr; dmarc=pass action=none header.from=viveris.fr; dkim=pass header.d=viveris.fr; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=viverislicensing.onmicrosoft.com; s=selector2-viverislicensing-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=t9HR3FpasfmEXN9zKXbw9/BDA2cEMOli7CYojW0/xr4=; b=mGJcNV1b+SO0Dh+n70/LAq6TxnlK8bvQgxOiMQGe5g9oy0I+Tkwi+0ZpoPBShu9baVgoSwswuLyhCv9zDMfPKmxukOeGd8BTxzuEeN/7Rb2t1YciOsS+g2ZLMOebMKcEIyMhys3Sa9WqQAan9srsMs/ea6Ik2PgvOVor02dqSTA= Received: from AM4PR0902MB1748.eurprd09.prod.outlook.com (2603:10a6:200:96::21) by AM0PR09MB3361.eurprd09.prod.outlook.com (2603:10a6:208:170::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4352.28; Wed, 28 Jul 2021 13:21:25 +0000 Received: from AM4PR0902MB1748.eurprd09.prod.outlook.com ([fe80::84a0:780d:1c5c:4432]) by AM4PR0902MB1748.eurprd09.prod.outlook.com ([fe80::84a0:780d:1c5c:4432%9]) with mapi id 15.20.4352.031; Wed, 28 Jul 2021 13:21:25 +0000 From: THOBY Simon To: "zohar@linux.ibm.com" , "dmitry.kasatkin@gmail.com" , "linux-integrity@vger.kernel.org" , BARVAUX Didier CC: THOBY Simon Subject: [PATCH v5 4/5] IMA: add a policy option to restrict xattr hash algorithms on appraisal Thread-Topic: [PATCH v5 4/5] IMA: add a policy option to restrict xattr hash algorithms on appraisal Thread-Index: AQHXg7N3um+oBXQ92U6hndVjgnx0Bg== Date: Wed, 28 Jul 2021 13:21:25 +0000 Message-ID: <20210728132112.258606-5-simon.thoby@viveris.fr> References: <20210728132112.258606-1-simon.thoby@viveris.fr> In-Reply-To: <20210728132112.258606-1-simon.thoby@viveris.fr> Accept-Language: fr-FR, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: linux.ibm.com; dkim=none (message not signed) header.d=none;linux.ibm.com; dmarc=none action=none header.from=viveris.fr; x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: ea6b98e4-162d-47b5-a06c-08d951ca99b7 x-ms-traffictypediagnostic: AM0PR09MB3361: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:1824; x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: SBS/j7jzgklh4H6NNN90D4hAEBYWPzz43mzxsoItFYai80FoCp7RldG/NBa+dd4yIG6BLkY9Zts/eAkJIBuZD17GaxJgRRQY19Mmio7XhPfMCtKc/fDrmqMgM5JwNQluqeKknRt4DezQlS0vcUDeGwoEmYWLXmvAwCF6rwDBxUouoiYHZIHpIGbKjs69JHld+ySEDnFVUO2yX/agbUZnIutgORZv7qcrK3Nq24R3jB792qqIqTvBnKCLf+eVC6WlSyhSKGq6mfmDXi2c6rOU7HKSu9G6p/Ug56qI/VGeLzk2l8sYQ9rwzR0dQCD3y5csCgsvmNT3WfhscdfgKj7AErnApJO5sVrDtwJQY8kHf6PEIRqn0ifhjDWGvVytL8iCJ2K5937ah+Z+6f0IB6lwKSrkSVr071fjX8/3FcQjCKraezaWV5gB0MM5iLP4qP0TEzSzmShInf1a8P9/Xf+K5IiaN8N5HdPkCTQkOrG/DWgJ04zj/v02AI8VwlUW8WWyj7XL0idZoLbc9GqI2gFS0cKvxink9BFx4pz3IiZ+1Avj14g4S0WwnQXBij44GblilpLKM0StU5em/y66f9gFVl8JEFS9w2pF/OCtR1zc0k0qrHsreWlvChWNW5S3OVHqtsTWTu41vn4Ik+Tuuk3hQQMmkBOJ8kuI02uvzdotHL0+okxHQcb1clwxEyvlQZObPglOX/V0p1AAdxcbv9fnIA== x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:AM4PR0902MB1748.eurprd09.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(39840400004)(396003)(136003)(346002)(376002)(8676002)(6512007)(8936002)(6486002)(110136005)(76116006)(186003)(71200400001)(66446008)(26005)(91956017)(83380400001)(6506007)(64756008)(478600001)(66946007)(66476007)(66556008)(38100700002)(316002)(122000001)(86362001)(1076003)(4326008)(2906002)(5660300002)(6636002)(107886003)(38070700005)(36756003)(2616005);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?ypBfI7jT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859-1?q?ceyAmxD/7yx4KjyYFY1MjP4O2oaO2TEHM/OTrj5cU5OyUZwYSPIHfabCZS/I?= =?iso-8859-1?q?SkZkACI1RF6/OFYjcd+8qLzvXPntL3CTc5t8eZxjm+nuempyoBEcTzlgo6Bf?= =?iso-8859-1?q?3prY8dqmfjEdACYMsZGKy31Q+DqfcwT6komhIJEnaytMATwevDa3J+Cxh6Wt?= =?iso-8859-1?q?XCbRzxcaQTvXJFOgvoypEMEFR8zO3631obGOWOdI+OL4gsaG9COoovWlbOXZ?= =?iso-8859-1?q?uAuwlrNqdZLo0GOM+e0epdPFrige7NLTG0EKQGwHUaIO+F3JJxw2H2lQhgWQ?= =?iso-8859-1?q?3/XE6gdCMU/Dqp5pTK5tbBzASnQTUq9xy602S6A4RJEBPIrNyN/5Oqkn3I0Y?= =?iso-8859-1?q?Dz2KT8dcWuPmUsJVMnRInPq2NCkUfP+OV5kvPTobXs2LzAOl0swYTYolBYeK?= =?iso-8859-1?q?ju3WzWcC2XQA4uk/mglSgDZI9Kv616hkHY5k39nByXO1SK2EIbeMJ8byPiyR?= =?iso-8859-1?q?H?= MIME-Version: 1.0 X-OriginatorOrg: viveris.fr X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: AM4PR0902MB1748.eurprd09.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: ea6b98e4-162d-47b5-a06c-08d951ca99b7 X-MS-Exchange-CrossTenant-originalarrivaltime: 28 Jul 2021 13:21:25.1028 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 34bab81c-945c-43f1-ad13-592b97e11b40 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: mXi1uwOPwvzlKi3tKCufPhVcU0Iap3iO3AgkxdTeraKwuJDuFgekxUHFVOJjdmCfhl4/eAVYxbAlBbwuDZMjzw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR09MB3361 Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org The kernel has the ability to restrict the set of hash algorithms it accepts for the security.ima xattr when it appraises files. Define a new IMA policy rule option "appraise_hash=", using the mentioned mechanism to expose a user-toggable policy knob to opt-in to that restriction and select the desired set of algorithms that must be accepted. When a policy rule uses the 'appraise_hash' option, appraisal of a file referenced by that rule will now fail if the digest algorithm employed to hash the file was not one of those explicitly listed in the option. In its absence, any hash algorithm compiled in the kernel will be accepted. For example, on a system where SELinux is properly deployed, the rule appraise func=BPRM_CHECK obj_type=iptables_exec_t appraise_hash=sha256,sha384 will block the execution of iptables if the xattr security.ima of its executables were not hashed with either sha256 or sha384. Signed-off-by: Simon Thoby Reviewed-by: Mimi Zohar --- Documentation/ABI/testing/ima_policy | 6 ++- security/integrity/ima/ima_policy.c | 74 ++++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/Documentation/ABI/testing/ima_policy b/Documentation/ABI/testing/ima_policy index 070779e8d836..aeb622698047 100644 --- a/Documentation/ABI/testing/ima_policy +++ b/Documentation/ABI/testing/ima_policy @@ -27,7 +27,7 @@ Description: lsm: [[subj_user=] [subj_role=] [subj_type=] [obj_user=] [obj_role=] [obj_type=]] option: [[appraise_type=]] [template=] [permit_directio] - [appraise_flag=] [keyrings=] + [appraise_flag=] [appraise_hash=] [keyrings=] base: func:= [BPRM_CHECK][MMAP_CHECK][CREDS_CHECK][FILE_CHECK][MODULE_CHECK] [FIRMWARE_CHECK] @@ -55,6 +55,10 @@ Description: label:= [selinux]|[kernel_info]|[data_label] data_label:= a unique string used for grouping and limiting critical data. For example, "selinux" to measure critical data for SELinux. + appraise_hash:= comma-separated list of hash algorithms + For example, "sha256,sha512" to only accept to appraise + files where the security.ima xattr was hashed with one + of these two algorithms. default policy: # PROC_SUPER_MAGIC diff --git a/security/integrity/ima/ima_policy.c b/security/integrity/ima/ima_policy.c index 344b5b0dc1a1..f944c3e7f340 100644 --- a/security/integrity/ima/ima_policy.c +++ b/security/integrity/ima/ima_policy.c @@ -960,7 +960,7 @@ enum { Opt_fsuuid, Opt_uid_eq, Opt_euid_eq, Opt_fowner_eq, Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt, Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt, - Opt_appraise_type, Opt_appraise_flag, + Opt_appraise_type, Opt_appraise_flag, Opt_appraise_hash, Opt_permit_directio, Opt_pcr, Opt_template, Opt_keyrings, Opt_label, Opt_err }; @@ -995,6 +995,7 @@ static const match_table_t policy_tokens = { {Opt_fowner_lt, "fowner<%s"}, {Opt_appraise_type, "appraise_type=%s"}, {Opt_appraise_flag, "appraise_flag=%s"}, + {Opt_appraise_hash, "appraise_hash=%s"}, {Opt_permit_directio, "permit_directio"}, {Opt_pcr, "pcr=%s"}, {Opt_template, "template=%s"}, @@ -1095,7 +1096,8 @@ static bool ima_validate_rule(struct ima_rule_entry *entry) return false; if (entry->action != APPRAISE && - entry->flags & (IMA_DIGSIG_REQUIRED | IMA_MODSIG_ALLOWED | IMA_CHECK_BLACKLIST)) + entry->flags & (IMA_DIGSIG_REQUIRED | IMA_MODSIG_ALLOWED | + IMA_CHECK_BLACKLIST | IMA_VALIDATE_HASH)) return false; /* @@ -1125,7 +1127,7 @@ static bool ima_validate_rule(struct ima_rule_entry *entry) IMA_UID | IMA_FOWNER | IMA_FSUUID | IMA_INMASK | IMA_EUID | IMA_PCR | IMA_FSNAME | IMA_DIGSIG_REQUIRED | - IMA_PERMIT_DIRECTIO)) + IMA_PERMIT_DIRECTIO | IMA_VALIDATE_HASH)) return false; break; @@ -1137,7 +1139,7 @@ static bool ima_validate_rule(struct ima_rule_entry *entry) IMA_INMASK | IMA_EUID | IMA_PCR | IMA_FSNAME | IMA_DIGSIG_REQUIRED | IMA_PERMIT_DIRECTIO | IMA_MODSIG_ALLOWED | - IMA_CHECK_BLACKLIST)) + IMA_CHECK_BLACKLIST | IMA_VALIDATE_HASH)) return false; break; @@ -1187,6 +1189,28 @@ static bool ima_validate_rule(struct ima_rule_entry *entry) return true; } +static unsigned int ima_parse_appraise_hash(char *arg) +{ + unsigned int res = 0; + int idx; + char *token; + + while ((token = strsep(&arg, ",")) != NULL) { + idx = match_string(hash_algo_name, HASH_ALGO__LAST, token); + + if (idx < 0) { + pr_err("unknown hash algorithm \"%s\", ignoring", + token); + continue; + } + + /* Add the hash algorithm to the 'allowed' bitfield */ + res |= (1U << idx); + } + + return res; +} + static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) { struct audit_buffer *ab; @@ -1522,6 +1546,25 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) else result = -EINVAL; break; + case Opt_appraise_hash: + ima_log_string(ab, "appraise_hash", args[0].from); + + if (entry->allowed_hashes) { + result = -EINVAL; + break; + } + + entry->allowed_hashes = ima_parse_appraise_hash(args[0].from); + + /* invalid or empty list of algorithms */ + if (!entry->allowed_hashes) { + result = -EINVAL; + break; + } + + entry->flags |= IMA_VALIDATE_HASH; + + break; case Opt_permit_directio: entry->flags |= IMA_PERMIT_DIRECTIO; break; @@ -1714,6 +1757,23 @@ static void ima_show_rule_opt_list(struct seq_file *m, seq_printf(m, "%s%s", i ? "|" : "", opt_list->items[i]); } +static void ima_policy_show_appraise_hash(struct seq_file *m, + unsigned int allowed_hashes) +{ + int idx, list_size = 0; + + for (idx = 0; idx < HASH_ALGO__LAST; idx++) { + if (!(allowed_hashes & (1U << idx))) + continue; + + /* only add commas if the list contains multiple entries */ + if (list_size++) + seq_puts(m, ","); + + seq_puts(m, hash_algo_name[idx]); + } +} + int ima_policy_show(struct seq_file *m, void *v) { struct ima_rule_entry *entry = v; @@ -1825,6 +1885,12 @@ int ima_policy_show(struct seq_file *m, void *v) seq_puts(m, " "); } + if (entry->flags & IMA_VALIDATE_HASH) { + seq_puts(m, "appraise_hash="); + ima_policy_show_appraise_hash(m, entry->allowed_hashes); + seq_puts(m, " "); + } + for (i = 0; i < MAX_LSM_RULES; i++) { if (entry->lsm[i].rule) { switch (i) {