blob: 87b9b71cb8201ab7840120da7f7241d30cfbbb35 [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Mimi Zohar3323eec2009-02-04 09:06:58 -05002/*
3 * Copyright (C) 2008 IBM Corporation
4 * Author: Mimi Zohar <zohar@us.ibm.com>
5 *
Mimi Zohar3323eec2009-02-04 09:06:58 -05006 * ima_policy.c
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +02007 * - initialize default measure policy rules
Mimi Zohar3323eec2009-02-04 09:06:58 -05008 */
Thiago Jung Bauermann3878d502019-06-27 23:19:32 -03009
Paul Gortmaker876979c2018-12-09 15:36:29 -050010#include <linux/init.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050011#include <linux/list.h>
Scott Brandenb89999d02020-10-02 10:38:15 -070012#include <linux/kernel_read_file.h>
Mimi Zoharcf222212016-01-14 17:57:47 -050013#include <linux/fs.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050014#include <linux/security.h>
15#include <linux/magic.h>
Mimi Zohar4af46622009-02-04 09:07:00 -050016#include <linux/parser.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Petko Manolov38d859f2015-12-02 17:47:54 +020018#include <linux/rculist.h>
Dmitry Kasatkin85865c12012-09-03 23:23:13 +030019#include <linux/genhd.h>
Petko Manolov80eae202015-12-02 17:47:56 +020020#include <linux/seq_file.h>
Nayna Jain61917062018-10-09 23:00:36 +053021#include <linux/ima.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050022
23#include "ima.h"
24
25/* flags definitions */
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020026#define IMA_FUNC 0x0001
27#define IMA_MASK 0x0002
Mimi Zohar3323eec2009-02-04 09:06:58 -050028#define IMA_FSMAGIC 0x0004
29#define IMA_UID 0x0008
Mimi Zohar07f6a792011-03-09 22:25:48 -050030#define IMA_FOWNER 0x0010
Dmitry Kasatkin85865c12012-09-03 23:23:13 +030031#define IMA_FSUUID 0x0020
Mimi Zohar4351c292014-11-05 07:53:55 -050032#define IMA_INMASK 0x0040
Mimi Zohar139069e2014-11-05 07:48:36 -050033#define IMA_EUID 0x0080
Eric Richter02606432016-06-01 13:14:01 -050034#define IMA_PCR 0x0100
Mimi Zoharf1b08bb2018-01-15 11:20:36 -050035#define IMA_FSNAME 0x0200
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -080036#define IMA_KEYRINGS 0x0400
Tushar Sugandhi47d76a42021-01-07 20:07:05 -080037#define IMA_LABEL 0x0800
THOBY Simon1624dc02021-08-16 08:11:00 +000038#define IMA_VALIDATE_ALGOS 0x1000
Mimi Zohar3323eec2009-02-04 09:06:58 -050039
Dmitry Kasatkin45e24722012-09-12 20:51:32 +030040#define UNKNOWN 0
41#define MEASURE 0x0001 /* same as IMA_MEASURE */
42#define DONT_MEASURE 0x0002
43#define APPRAISE 0x0004 /* same as IMA_APPRAISE */
44#define DONT_APPRAISE 0x0008
Peter Moodye7c568e2012-06-14 10:04:36 -070045#define AUDIT 0x0040
Mimi Zoharda1b0022016-09-29 10:04:52 -040046#define HASH 0x0100
47#define DONT_HASH 0x0200
Mimi Zohar4af46622009-02-04 09:07:00 -050048
Eric Richter02606432016-06-01 13:14:01 -050049#define INVALID_PCR(a) (((a) < 0) || \
Pankaj Bharadiyac5936422019-12-09 10:31:43 -080050 (a) >= (sizeof_field(struct integrity_iint_cache, measured_pcrs) * 8))
Eric Richter02606432016-06-01 13:14:01 -050051
Roberto Sassua7560242014-09-12 19:35:54 +020052int ima_policy_flag;
Mimi Zohar6ad6afa2015-12-07 14:35:47 -050053static int temp_ima_appraise;
Mimi Zoharef968372018-07-13 14:06:01 -040054static int build_ima_appraise __ro_after_init;
Roberto Sassua7560242014-09-12 19:35:54 +020055
THOBY Simon4f2946a2021-08-16 08:11:01 +000056atomic_t ima_setxattr_allowed_hash_algorithms;
57
Mimi Zohar4af46622009-02-04 09:07:00 -050058#define MAX_LSM_RULES 6
59enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
60 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
61};
Mimi Zohar3323eec2009-02-04 09:06:58 -050062
Mimi Zohar24fd03c2015-06-11 20:48:33 -040063enum policy_types { ORIGINAL_TCB = 1, DEFAULT_TCB };
64
Nayna Jainc52657d2018-10-09 23:00:35 +053065enum policy_rule_list { IMA_DEFAULT_POLICY = 1, IMA_CUSTOM_POLICY };
66
Tyler Hicks176377d2020-08-11 14:26:20 -050067struct ima_rule_opt_list {
68 size_t count;
69 char *items[];
70};
71
Mimi Zohar07f6a792011-03-09 22:25:48 -050072struct ima_rule_entry {
Mimi Zohar3323eec2009-02-04 09:06:58 -050073 struct list_head list;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050074 int action;
Mimi Zohar3323eec2009-02-04 09:06:58 -050075 unsigned int flags;
76 enum ima_hooks func;
77 int mask;
78 unsigned long fsmagic;
Christoph Hellwig787d8c52017-06-01 07:00:26 +020079 uuid_t fsuuid;
Eric W. Biederman8b94eea2012-05-25 18:24:12 -060080 kuid_t uid;
Linus Torvalds882653222012-10-02 21:38:48 -070081 kuid_t fowner;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +030082 bool (*uid_op)(kuid_t, kuid_t); /* Handlers for operators */
83 bool (*fowner_op)(kuid_t, kuid_t); /* uid_eq(), uid_gt(), uid_lt() */
Eric Richter02606432016-06-01 13:14:01 -050084 int pcr;
THOBY Simon1624dc02021-08-16 08:11:00 +000085 unsigned int allowed_algos; /* bitfield of allowed hash algorithms */
Mimi Zohar4af46622009-02-04 09:07:00 -050086 struct {
87 void *rule; /* LSM file metadata specific */
Tyler Hicksaa0c0222020-07-09 01:19:08 -050088 char *args_p; /* audit value */
Mimi Zohar4af46622009-02-04 09:07:00 -050089 int type; /* audit type */
90 } lsm[MAX_LSM_RULES];
Mimi Zoharf1b08bb2018-01-15 11:20:36 -050091 char *fsname;
Tyler Hicks176377d2020-08-11 14:26:20 -050092 struct ima_rule_opt_list *keyrings; /* Measure keys added to these keyrings */
Tushar Sugandhi47d76a42021-01-07 20:07:05 -080093 struct ima_rule_opt_list *label; /* Measure data grouped under this label */
Matthew Garrett19453ce02019-06-19 15:46:11 -070094 struct ima_template_desc *template;
Mimi Zohar3323eec2009-02-04 09:06:58 -050095};
96
Eric Paris5789ba32009-05-21 15:47:06 -040097/*
THOBY Simon1624dc02021-08-16 08:11:00 +000098 * sanity check in case the kernels gains more hash algorithms that can
99 * fit in an unsigned int
100 */
101static_assert(
102 8 * sizeof(unsigned int) >= HASH_ALGO__LAST,
103 "The bitfield allowed_algos in ima_rule_entry is too small to contain all the supported hash algorithms, consider using a bigger type");
104
105/*
Eric Paris5789ba32009-05-21 15:47:06 -0400106 * Without LSM specific knowledge, the default policy can only be
Mimi Zohar07f6a792011-03-09 22:25:48 -0500107 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
Mimi Zohar4af46622009-02-04 09:07:00 -0500108 */
Eric Paris5789ba32009-05-21 15:47:06 -0400109
110/*
111 * The minimum rule set to allow for full TCB coverage. Measures all files
112 * opened or mmap for exec and everything read by root. Dangerous because
113 * normal users can easily run the machine out of memory simply building
114 * and running executables.
115 */
James Morrisbad44172017-02-13 16:34:35 +1100116static struct ima_rule_entry dont_measure_rules[] __ro_after_init = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200117 {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
118 {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
119 {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
120 {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
121 {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
122 {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
123 {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
124 {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
Martin Townsend1c070b12018-03-22 13:46:01 +0000125 {.action = DONT_MEASURE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
Roberto Sassu6438de92015-04-11 17:13:06 +0200126 {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
127 .flags = IMA_FSMAGIC},
Laura Abbott82e3bb42017-05-09 11:25:27 -0700128 {.action = DONT_MEASURE, .fsmagic = CGROUP2_SUPER_MAGIC,
129 .flags = IMA_FSMAGIC},
Mimi Zohar060190f2018-11-14 17:24:13 -0500130 {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
131 {.action = DONT_MEASURE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC}
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400132};
133
James Morrisbad44172017-02-13 16:34:35 +1100134static struct ima_rule_entry original_measurement_rules[] __ro_after_init = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200135 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500136 .flags = IMA_FUNC | IMA_MASK},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200137 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500138 .flags = IMA_FUNC | IMA_MASK},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400139 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300140 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
141 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400142 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
143 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
144};
145
James Morrisbad44172017-02-13 16:34:35 +1100146static struct ima_rule_entry default_measurement_rules[] __ro_after_init = {
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400147 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
148 .flags = IMA_FUNC | IMA_MASK},
149 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
150 .flags = IMA_FUNC | IMA_MASK},
151 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300152 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
153 .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400154 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300155 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
156 .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200157 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400158 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
Mimi Zohar19f8a842016-01-15 10:17:12 -0500159 {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC},
Mimi Zohar3323eec2009-02-04 09:06:58 -0500160};
161
James Morrisbad44172017-02-13 16:34:35 +1100162static struct ima_rule_entry default_appraise_rules[] __ro_after_init = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200163 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
164 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
165 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
166 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
167 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
168 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
169 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
170 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
171 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
Martin Townsend1c070b12018-03-22 13:46:01 +0000172 {.action = DONT_APPRAISE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
Mimi Zoharcd025f72015-04-21 16:54:24 -0400173 {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
Mimi Zohar060190f2018-11-14 17:24:13 -0500174 {.action = DONT_APPRAISE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200175 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
Laura Abbott82e3bb42017-05-09 11:25:27 -0700176 {.action = DONT_APPRAISE, .fsmagic = CGROUP2_SUPER_MAGIC, .flags = IMA_FSMAGIC},
Mimi Zohar95ee08f2015-12-07 15:08:01 -0500177#ifdef CONFIG_IMA_WRITE_POLICY
178 {.action = APPRAISE, .func = POLICY_CHECK,
179 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
180#endif
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200181#ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300182 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
183 .flags = IMA_FOWNER},
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200184#else
185 /* force signature */
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300186 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200187 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
188#endif
Mimi Zohar07f6a792011-03-09 22:25:48 -0500189};
Mimi Zohar3323eec2009-02-04 09:06:58 -0500190
Mimi Zoharef968372018-07-13 14:06:01 -0400191static struct ima_rule_entry build_appraise_rules[] __ro_after_init = {
192#ifdef CONFIG_IMA_APPRAISE_REQUIRE_MODULE_SIGS
193 {.action = APPRAISE, .func = MODULE_CHECK,
194 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
195#endif
196#ifdef CONFIG_IMA_APPRAISE_REQUIRE_FIRMWARE_SIGS
197 {.action = APPRAISE, .func = FIRMWARE_CHECK,
198 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
199#endif
200#ifdef CONFIG_IMA_APPRAISE_REQUIRE_KEXEC_SIGS
201 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
202 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
203#endif
204#ifdef CONFIG_IMA_APPRAISE_REQUIRE_POLICY_SIGS
205 {.action = APPRAISE, .func = POLICY_CHECK,
206 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
207#endif
208};
209
Mimi Zohar503ceae2017-04-21 18:58:27 -0400210static struct ima_rule_entry secure_boot_rules[] __ro_after_init = {
211 {.action = APPRAISE, .func = MODULE_CHECK,
212 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
213 {.action = APPRAISE, .func = FIRMWARE_CHECK,
214 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
215 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
216 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
217 {.action = APPRAISE, .func = POLICY_CHECK,
218 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
219};
220
Lakshmi Ramasubramanian03cee162021-01-07 20:07:07 -0800221static struct ima_rule_entry critical_data_rules[] __ro_after_init = {
222 {.action = MEASURE, .func = CRITICAL_DATA, .flags = IMA_FUNC},
223};
224
Nayna Jain61917062018-10-09 23:00:36 +0530225/* An array of architecture specific rules */
YueHaibing68f25292019-06-11 21:40:32 +0800226static struct ima_rule_entry *arch_policy_entry __ro_after_init;
Nayna Jain61917062018-10-09 23:00:36 +0530227
Mimi Zohar07f6a792011-03-09 22:25:48 -0500228static LIST_HEAD(ima_default_rules);
229static LIST_HEAD(ima_policy_rules);
Petko Manolov38d859f2015-12-02 17:47:54 +0200230static LIST_HEAD(ima_temp_rules);
Roberto Sassu067a4362020-06-03 17:08:20 +0200231static struct list_head *ima_rules = &ima_default_rules;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500232
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400233static int ima_policy __initdata;
Petko Manolov38d859f2015-12-02 17:47:54 +0200234
Mimi Zohar07f6a792011-03-09 22:25:48 -0500235static int __init default_measure_policy_setup(char *str)
Eric Paris5789ba32009-05-21 15:47:06 -0400236{
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400237 if (ima_policy)
238 return 1;
239
240 ima_policy = ORIGINAL_TCB;
Eric Paris5789ba32009-05-21 15:47:06 -0400241 return 1;
242}
Mimi Zohar07f6a792011-03-09 22:25:48 -0500243__setup("ima_tcb", default_measure_policy_setup);
244
Mimi Zohar33ce9542017-04-24 12:04:09 -0400245static bool ima_use_appraise_tcb __initdata;
Mimi Zohar503ceae2017-04-21 18:58:27 -0400246static bool ima_use_secure_boot __initdata;
Lakshmi Ramasubramanian03cee162021-01-07 20:07:07 -0800247static bool ima_use_critical_data __initdata;
Mimi Zohar9e670282018-02-21 11:36:32 -0500248static bool ima_fail_unverifiable_sigs __ro_after_init;
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400249static int __init policy_setup(char *str)
250{
Mimi Zohar33ce9542017-04-24 12:04:09 -0400251 char *p;
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400252
Mimi Zohar33ce9542017-04-24 12:04:09 -0400253 while ((p = strsep(&str, " |\n")) != NULL) {
254 if (*p == ' ')
255 continue;
256 if ((strcmp(p, "tcb") == 0) && !ima_policy)
257 ima_policy = DEFAULT_TCB;
258 else if (strcmp(p, "appraise_tcb") == 0)
Thomas Meyer39adb922017-10-07 16:02:21 +0200259 ima_use_appraise_tcb = true;
Mimi Zohar503ceae2017-04-21 18:58:27 -0400260 else if (strcmp(p, "secure_boot") == 0)
Thomas Meyer39adb922017-10-07 16:02:21 +0200261 ima_use_secure_boot = true;
Lakshmi Ramasubramanian03cee162021-01-07 20:07:07 -0800262 else if (strcmp(p, "critical_data") == 0)
263 ima_use_critical_data = true;
Mimi Zohar9e670282018-02-21 11:36:32 -0500264 else if (strcmp(p, "fail_securely") == 0)
265 ima_fail_unverifiable_sigs = true;
Bruno Meneguele7fe2bb72020-09-04 16:40:58 -0300266 else
267 pr_err("policy \"%s\" not found", p);
Mimi Zohar33ce9542017-04-24 12:04:09 -0400268 }
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400269
270 return 1;
271}
272__setup("ima_policy=", policy_setup);
273
Mimi Zohar07f6a792011-03-09 22:25:48 -0500274static int __init default_appraise_policy_setup(char *str)
275{
Thomas Meyer39adb922017-10-07 16:02:21 +0200276 ima_use_appraise_tcb = true;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500277 return 1;
278}
279__setup("ima_appraise_tcb", default_appraise_policy_setup);
Eric Paris5789ba32009-05-21 15:47:06 -0400280
Tyler Hicks176377d2020-08-11 14:26:20 -0500281static struct ima_rule_opt_list *ima_alloc_rule_opt_list(const substring_t *src)
282{
283 struct ima_rule_opt_list *opt_list;
284 size_t count = 0;
285 char *src_copy;
286 char *cur, *next;
287 size_t i;
288
289 src_copy = match_strdup(src);
290 if (!src_copy)
291 return ERR_PTR(-ENOMEM);
292
293 next = src_copy;
294 while ((cur = strsep(&next, "|"))) {
295 /* Don't accept an empty list item */
296 if (!(*cur)) {
297 kfree(src_copy);
298 return ERR_PTR(-EINVAL);
299 }
300 count++;
301 }
302
303 /* Don't accept an empty list */
304 if (!count) {
305 kfree(src_copy);
306 return ERR_PTR(-EINVAL);
307 }
308
309 opt_list = kzalloc(struct_size(opt_list, items, count), GFP_KERNEL);
310 if (!opt_list) {
311 kfree(src_copy);
312 return ERR_PTR(-ENOMEM);
313 }
314
315 /*
316 * strsep() has already replaced all instances of '|' with '\0',
317 * leaving a byte sequence of NUL-terminated strings. Reference each
318 * string with the array of items.
319 *
320 * IMPORTANT: Ownership of the allocated buffer is transferred from
321 * src_copy to the first element in the items array. To free the
322 * buffer, kfree() must only be called on the first element of the
323 * array.
324 */
325 for (i = 0, cur = src_copy; i < count; i++) {
326 opt_list->items[i] = cur;
327 cur = strchr(cur, '\0') + 1;
328 }
329 opt_list->count = count;
330
331 return opt_list;
332}
333
334static void ima_free_rule_opt_list(struct ima_rule_opt_list *opt_list)
335{
336 if (!opt_list)
337 return;
338
339 if (opt_list->count) {
340 kfree(opt_list->items[0]);
341 opt_list->count = 0;
342 }
343
344 kfree(opt_list);
345}
346
Janne Karhunenb1694242019-06-14 15:20:15 +0300347static void ima_lsm_free_rule(struct ima_rule_entry *entry)
348{
349 int i;
350
351 for (i = 0; i < MAX_LSM_RULES; i++) {
Tyler Hicksb8867ee2020-07-10 15:37:50 -0500352 ima_filter_rule_free(entry->lsm[i].rule);
Janne Karhunenb1694242019-06-14 15:20:15 +0300353 kfree(entry->lsm[i].args_p);
354 }
Tyler Hicks465aee72020-07-09 01:19:01 -0500355}
356
357static void ima_free_rule(struct ima_rule_entry *entry)
358{
359 if (!entry)
360 return;
361
362 /*
363 * entry->template->fields may be allocated in ima_parse_rule() but that
364 * reference is owned by the corresponding ima_template_desc element in
365 * the defined_templates list and cannot be freed here
366 */
367 kfree(entry->fsname);
Tyler Hicks176377d2020-08-11 14:26:20 -0500368 ima_free_rule_opt_list(entry->keyrings);
Tyler Hicks465aee72020-07-09 01:19:01 -0500369 ima_lsm_free_rule(entry);
Janne Karhunenb1694242019-06-14 15:20:15 +0300370 kfree(entry);
371}
372
373static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry)
374{
375 struct ima_rule_entry *nentry;
Janne Karhunen483ec262020-01-15 17:42:30 +0200376 int i;
Janne Karhunenb1694242019-06-14 15:20:15 +0300377
Janne Karhunenb1694242019-06-14 15:20:15 +0300378 /*
379 * Immutable elements are copied over as pointers and data; only
380 * lsm rules can change
381 */
Alex Dewarf60c8262020-09-09 20:09:06 +0100382 nentry = kmemdup(entry, sizeof(*nentry), GFP_KERNEL);
383 if (!nentry)
384 return NULL;
385
Pankaj Bharadiyac5936422019-12-09 10:31:43 -0800386 memset(nentry->lsm, 0, sizeof_field(struct ima_rule_entry, lsm));
Janne Karhunenb1694242019-06-14 15:20:15 +0300387
388 for (i = 0; i < MAX_LSM_RULES; i++) {
Janne Karhunen483ec262020-01-15 17:42:30 +0200389 if (!entry->lsm[i].args_p)
Janne Karhunenb1694242019-06-14 15:20:15 +0300390 continue;
391
392 nentry->lsm[i].type = entry->lsm[i].type;
Tyler Hicks39e59932020-07-09 01:19:07 -0500393 nentry->lsm[i].args_p = entry->lsm[i].args_p;
394 /*
395 * Remove the reference from entry so that the associated
396 * memory will not be freed during a later call to
397 * ima_lsm_free_rule(entry).
398 */
399 entry->lsm[i].args_p = NULL;
Janne Karhunenb1694242019-06-14 15:20:15 +0300400
Tyler Hicksb8867ee2020-07-10 15:37:50 -0500401 ima_filter_rule_init(nentry->lsm[i].type, Audit_equal,
402 nentry->lsm[i].args_p,
403 &nentry->lsm[i].rule);
Janne Karhunen483ec262020-01-15 17:42:30 +0200404 if (!nentry->lsm[i].rule)
405 pr_warn("rule for LSM \'%s\' is undefined\n",
Tyler Hicksaa0c0222020-07-09 01:19:08 -0500406 nentry->lsm[i].args_p);
Janne Karhunenb1694242019-06-14 15:20:15 +0300407 }
408 return nentry;
Janne Karhunenb1694242019-06-14 15:20:15 +0300409}
410
411static int ima_lsm_update_rule(struct ima_rule_entry *entry)
412{
413 struct ima_rule_entry *nentry;
414
415 nentry = ima_lsm_copy_rule(entry);
416 if (!nentry)
417 return -ENOMEM;
418
419 list_replace_rcu(&entry->list, &nentry->list);
420 synchronize_rcu();
Tyler Hicks465aee72020-07-09 01:19:01 -0500421 /*
422 * ima_lsm_copy_rule() shallow copied all references, except for the
423 * LSM references, from entry to nentry so we only want to free the LSM
424 * references and the entry itself. All other memory refrences will now
425 * be owned by nentry.
426 */
Janne Karhunenb1694242019-06-14 15:20:15 +0300427 ima_lsm_free_rule(entry);
Tyler Hicks465aee72020-07-09 01:19:01 -0500428 kfree(entry);
Janne Karhunenb1694242019-06-14 15:20:15 +0300429
430 return 0;
431}
432
Tyler Hicksdb2045f2020-07-09 01:19:04 -0500433static bool ima_rule_contains_lsm_cond(struct ima_rule_entry *entry)
434{
435 int i;
436
437 for (i = 0; i < MAX_LSM_RULES; i++)
438 if (entry->lsm[i].args_p)
439 return true;
440
441 return false;
442}
443
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200444/*
Petko Manolov38d859f2015-12-02 17:47:54 +0200445 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
446 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
Janne Karhunenb1694242019-06-14 15:20:15 +0300447 * the reloaded LSM policy.
Mimi Zohar7163a992013-01-03 14:19:09 -0500448 */
449static void ima_lsm_update_rules(void)
450{
Janne Karhunenb1694242019-06-14 15:20:15 +0300451 struct ima_rule_entry *entry, *e;
Tyler Hicks592b24c2020-07-09 01:19:10 -0500452 int result;
Mimi Zohar7163a992013-01-03 14:19:09 -0500453
Janne Karhunenb1694242019-06-14 15:20:15 +0300454 list_for_each_entry_safe(entry, e, &ima_policy_rules, list) {
Tyler Hicks592b24c2020-07-09 01:19:10 -0500455 if (!ima_rule_contains_lsm_cond(entry))
Janne Karhunenb1694242019-06-14 15:20:15 +0300456 continue;
457
458 result = ima_lsm_update_rule(entry);
459 if (result) {
Janne Karhunen483ec262020-01-15 17:42:30 +0200460 pr_err("lsm rule update error %d\n", result);
Janne Karhunenb1694242019-06-14 15:20:15 +0300461 return;
Mimi Zohar7163a992013-01-03 14:19:09 -0500462 }
463 }
Mimi Zohar7163a992013-01-03 14:19:09 -0500464}
465
Janne Karhunenb1694242019-06-14 15:20:15 +0300466int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
467 void *lsm_data)
468{
469 if (event != LSM_POLICY_CHANGE)
470 return NOTIFY_DONE;
471
472 ima_lsm_update_rules();
473 return NOTIFY_OK;
474}
475
Mimi Zohar3323eec2009-02-04 09:06:58 -0500476/**
Tushar Sugandhi2b4a2472021-01-07 20:07:01 -0800477 * ima_match_rule_data - determine whether func_data matches the policy rule
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800478 * @rule: a pointer to a rule
Tushar Sugandhi2b4a2472021-01-07 20:07:01 -0800479 * @func_data: data to match against the measure rule data
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800480 * @cred: a pointer to a credentials structure for user validation
481 *
Tushar Sugandhi2b4a2472021-01-07 20:07:01 -0800482 * Returns true if func_data matches one in the rule, false otherwise.
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800483 */
Tushar Sugandhi2b4a2472021-01-07 20:07:01 -0800484static bool ima_match_rule_data(struct ima_rule_entry *rule,
485 const char *func_data,
486 const struct cred *cred)
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800487{
Tushar Sugandhi2b4a2472021-01-07 20:07:01 -0800488 const struct ima_rule_opt_list *opt_list = NULL;
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800489 bool matched = false;
Tyler Hicks176377d2020-08-11 14:26:20 -0500490 size_t i;
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800491
492 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
493 return false;
494
Tushar Sugandhi2b4a2472021-01-07 20:07:01 -0800495 switch (rule->func) {
496 case KEY_CHECK:
497 if (!rule->keyrings)
498 return true;
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800499
Tushar Sugandhi2b4a2472021-01-07 20:07:01 -0800500 opt_list = rule->keyrings;
501 break;
Tushar Sugandhic4e43aa2021-01-07 20:07:04 -0800502 case CRITICAL_DATA:
Tushar Sugandhi47d76a42021-01-07 20:07:05 -0800503 if (!rule->label)
504 return true;
505
506 opt_list = rule->label;
507 break;
Tushar Sugandhi2b4a2472021-01-07 20:07:01 -0800508 default:
509 return false;
510 }
511
512 if (!func_data)
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800513 return false;
514
Tushar Sugandhi2b4a2472021-01-07 20:07:01 -0800515 for (i = 0; i < opt_list->count; i++) {
516 if (!strcmp(opt_list->items[i], func_data)) {
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800517 matched = true;
518 break;
519 }
520 }
521
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800522 return matched;
523}
524
525/**
Janne Karhunen483ec262020-01-15 17:42:30 +0200526 * ima_match_rules - determine whether an inode matches the policy rule.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500527 * @rule: a pointer to a rule
Christian Braunera2d23292021-01-21 14:19:45 +0100528 * @mnt_userns: user namespace of the mount the inode was found from
Mimi Zohar3323eec2009-02-04 09:06:58 -0500529 * @inode: a pointer to an inode
Matthew Garrettd906c102018-01-08 13:36:20 -0800530 * @cred: a pointer to a credentials structure for user validation
531 * @secid: the secid of the task to be validated
Mimi Zohar3323eec2009-02-04 09:06:58 -0500532 * @func: LIM hook identifier
533 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
Tushar Sugandhi2b4a2472021-01-07 20:07:01 -0800534 * @func_data: func specific data, may be NULL
Mimi Zohar3323eec2009-02-04 09:06:58 -0500535 *
536 * Returns true on rule match, false on failure.
537 */
Christian Braunera2d23292021-01-21 14:19:45 +0100538static bool ima_match_rules(struct ima_rule_entry *rule,
539 struct user_namespace *mnt_userns,
540 struct inode *inode, const struct cred *cred,
541 u32 secid, enum ima_hooks func, int mask,
Tushar Sugandhi2b4a2472021-01-07 20:07:01 -0800542 const char *func_data)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500543{
Mimi Zohar4af46622009-02-04 09:07:00 -0500544 int i;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500545
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200546 if ((rule->flags & IMA_FUNC) &&
547 (rule->func != func && func != POST_SETATTR))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500548 return false;
Tushar Sugandhic4e43aa2021-01-07 20:07:04 -0800549
550 switch (func) {
551 case KEY_CHECK:
552 case CRITICAL_DATA:
553 return ((rule->func == func) &&
554 ima_match_rule_data(rule, func_data, cred));
555 default:
556 break;
557 }
558
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200559 if ((rule->flags & IMA_MASK) &&
560 (rule->mask != mask && func != POST_SETATTR))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500561 return false;
Mimi Zohar4351c292014-11-05 07:53:55 -0500562 if ((rule->flags & IMA_INMASK) &&
563 (!(rule->mask & mask) && func != POST_SETATTR))
564 return false;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500565 if ((rule->flags & IMA_FSMAGIC)
566 && rule->fsmagic != inode->i_sb->s_magic)
567 return false;
Mimi Zoharf1b08bb2018-01-15 11:20:36 -0500568 if ((rule->flags & IMA_FSNAME)
569 && strcmp(rule->fsname, inode->i_sb->s_type->name))
570 return false;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300571 if ((rule->flags & IMA_FSUUID) &&
Christoph Hellwig85787092017-05-10 15:06:33 +0200572 !uuid_equal(&rule->fsuuid, &inode->i_sb->s_uuid))
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300573 return false;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300574 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500575 return false;
Mimi Zohar139069e2014-11-05 07:48:36 -0500576 if (rule->flags & IMA_EUID) {
577 if (has_capability_noaudit(current, CAP_SETUID)) {
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300578 if (!rule->uid_op(cred->euid, rule->uid)
579 && !rule->uid_op(cred->suid, rule->uid)
580 && !rule->uid_op(cred->uid, rule->uid))
Mimi Zohar139069e2014-11-05 07:48:36 -0500581 return false;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300582 } else if (!rule->uid_op(cred->euid, rule->uid))
Mimi Zohar139069e2014-11-05 07:48:36 -0500583 return false;
584 }
585
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300586 if ((rule->flags & IMA_FOWNER) &&
Christian Braunera2d23292021-01-21 14:19:45 +0100587 !rule->fowner_op(i_uid_into_mnt(mnt_userns, inode), rule->fowner))
Mimi Zohar07f6a792011-03-09 22:25:48 -0500588 return false;
Mimi Zohar4af46622009-02-04 09:07:00 -0500589 for (i = 0; i < MAX_LSM_RULES; i++) {
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400590 int rc = 0;
Matthew Garrettd906c102018-01-08 13:36:20 -0800591 u32 osid;
Mimi Zohar4af46622009-02-04 09:07:00 -0500592
Janne Karhunen483ec262020-01-15 17:42:30 +0200593 if (!rule->lsm[i].rule) {
594 if (!rule->lsm[i].args_p)
595 continue;
596 else
597 return false;
598 }
Mimi Zohar4af46622009-02-04 09:07:00 -0500599 switch (i) {
600 case LSM_OBJ_USER:
601 case LSM_OBJ_ROLE:
602 case LSM_OBJ_TYPE:
603 security_inode_getsecid(inode, &osid);
Tyler Hicksb8867ee2020-07-10 15:37:50 -0500604 rc = ima_filter_rule_match(osid, rule->lsm[i].type,
605 Audit_equal,
606 rule->lsm[i].rule);
Mimi Zohar4af46622009-02-04 09:07:00 -0500607 break;
608 case LSM_SUBJ_USER:
609 case LSM_SUBJ_ROLE:
610 case LSM_SUBJ_TYPE:
Tyler Hicksb8867ee2020-07-10 15:37:50 -0500611 rc = ima_filter_rule_match(secid, rule->lsm[i].type,
612 Audit_equal,
613 rule->lsm[i].rule);
Gustavo A. R. Silva28073eb2020-11-20 12:25:46 -0600614 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500615 default:
616 break;
617 }
618 if (!rc)
619 return false;
620 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500621 return true;
622}
623
Mimi Zohard79d72e2012-12-03 17:08:11 -0500624/*
625 * In addition to knowing that we need to appraise the file in general,
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500626 * we need to differentiate between calling hooks, for hook specific rules.
Mimi Zohard79d72e2012-12-03 17:08:11 -0500627 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500628static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
Mimi Zohard79d72e2012-12-03 17:08:11 -0500629{
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500630 if (!(rule->flags & IMA_FUNC))
631 return IMA_FILE_APPRAISE;
632
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200633 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500634 case MMAP_CHECK:
635 return IMA_MMAP_APPRAISE;
636 case BPRM_CHECK:
637 return IMA_BPRM_APPRAISE;
Matthew Garrettd906c102018-01-08 13:36:20 -0800638 case CREDS_CHECK:
639 return IMA_CREDS_APPRAISE;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500640 case FILE_CHECK:
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500641 case POST_SETATTR:
Mimi Zohard79d72e2012-12-03 17:08:11 -0500642 return IMA_FILE_APPRAISE;
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500643 case MODULE_CHECK ... MAX_CHECK - 1:
644 default:
645 return IMA_READ_APPRAISE;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500646 }
647}
648
Mimi Zohar3323eec2009-02-04 09:06:58 -0500649/**
650 * ima_match_policy - decision based on LSM and other conditions
Christian Braunera2d23292021-01-21 14:19:45 +0100651 * @mnt_userns: user namespace of the mount the inode was found from
Mimi Zohar3323eec2009-02-04 09:06:58 -0500652 * @inode: pointer to an inode for which the policy decision is being made
Matthew Garrettd906c102018-01-08 13:36:20 -0800653 * @cred: pointer to a credentials structure for which the policy decision is
654 * being made
655 * @secid: LSM secid of the task to be validated
Mimi Zohar3323eec2009-02-04 09:06:58 -0500656 * @func: IMA hook identifier
657 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
Eric Richter725de7f2016-06-01 13:14:02 -0500658 * @pcr: set the pcr to extend
Matthew Garrett19453ce02019-06-19 15:46:11 -0700659 * @template_desc: the template that should be used for this rule
Tushar Sugandhi2b4a2472021-01-07 20:07:01 -0800660 * @func_data: func specific data, may be NULL
THOBY Simon1624dc02021-08-16 08:11:00 +0000661 * @allowed_algos: allowlist of hash algorithms for the IMA xattr
Mimi Zohar3323eec2009-02-04 09:06:58 -0500662 *
663 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
664 * conditions.
665 *
Petko Manolov38d859f2015-12-02 17:47:54 +0200666 * Since the IMA policy may be updated multiple times we need to lock the
667 * list when walking it. Reads are many orders of magnitude more numerous
668 * than writes so ima_match_policy() is classical RCU candidate.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500669 */
Christian Braunera2d23292021-01-21 14:19:45 +0100670int ima_match_policy(struct user_namespace *mnt_userns, struct inode *inode,
671 const struct cred *cred, u32 secid, enum ima_hooks func,
672 int mask, int flags, int *pcr,
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800673 struct ima_template_desc **template_desc,
THOBY Simon1624dc02021-08-16 08:11:00 +0000674 const char *func_data, unsigned int *allowed_algos)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500675{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500676 struct ima_rule_entry *entry;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500677 int action = 0, actmask = flags | (flags << 1);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500678
Lakshmi Ramasubramaniandea87d02020-11-12 12:39:59 -0800679 if (template_desc && !*template_desc)
Mimi Zoharb36f2812019-07-19 07:16:57 -0400680 *template_desc = ima_template_desc_current();
681
Petko Manolov38d859f2015-12-02 17:47:54 +0200682 rcu_read_lock();
683 list_for_each_entry_rcu(entry, ima_rules, list) {
Mimi Zohar3323eec2009-02-04 09:06:58 -0500684
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500685 if (!(entry->action & actmask))
686 continue;
687
Christian Braunera2d23292021-01-21 14:19:45 +0100688 if (!ima_match_rules(entry, mnt_userns, inode, cred, secid,
Linus Torvalds7d6beb72021-02-23 13:39:45 -0800689 func, mask, func_data))
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500690 continue;
691
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300692 action |= entry->flags & IMA_ACTION_FLAGS;
693
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300694 action |= entry->action & IMA_DO_MASK;
Mimi Zoharda1b0022016-09-29 10:04:52 -0400695 if (entry->action & IMA_APPRAISE) {
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500696 action |= get_subaction(entry, func);
Mimi Zohara9a49352018-03-10 23:07:34 -0500697 action &= ~IMA_HASH;
Mimi Zohar9e670282018-02-21 11:36:32 -0500698 if (ima_fail_unverifiable_sigs)
699 action |= IMA_FAIL_UNVERIFIABLE_SIGS;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500700
THOBY Simon1624dc02021-08-16 08:11:00 +0000701 if (allowed_algos &&
702 entry->flags & IMA_VALIDATE_ALGOS)
703 *allowed_algos = entry->allowed_algos;
704 }
Mimi Zoharb36f2812019-07-19 07:16:57 -0400705
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300706 if (entry->action & IMA_DO_MASK)
707 actmask &= ~(entry->action | entry->action << 1);
708 else
709 actmask &= ~(entry->action | entry->action >> 1);
710
Eric Richter725de7f2016-06-01 13:14:02 -0500711 if ((pcr) && (entry->flags & IMA_PCR))
712 *pcr = entry->pcr;
713
Matthew Garrett19453ce02019-06-19 15:46:11 -0700714 if (template_desc && entry->template)
715 *template_desc = entry->template;
Matthew Garrett19453ce02019-06-19 15:46:11 -0700716
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500717 if (!actmask)
718 break;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500719 }
Petko Manolov38d859f2015-12-02 17:47:54 +0200720 rcu_read_unlock();
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500721
722 return action;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500723}
724
THOBY Simon4f2946a2021-08-16 08:11:01 +0000725/**
726 * ima_update_policy_flags() - Update global IMA variables
727 *
728 * Update ima_policy_flag and ima_setxattr_allowed_hash_algorithms
729 * based on the currently loaded policy.
730 *
731 * With ima_policy_flag, the decision to short circuit out of a function
732 * or not call the function in the first place can be made earlier.
733 *
734 * With ima_setxattr_allowed_hash_algorithms, the policy can restrict the
735 * set of hash algorithms accepted when updating the security.ima xattr of
736 * a file.
737 *
738 * Context: called after a policy update and at system initialization.
Roberto Sassua7560242014-09-12 19:35:54 +0200739 */
THOBY Simon4f2946a2021-08-16 08:11:01 +0000740void ima_update_policy_flags(void)
Roberto Sassua7560242014-09-12 19:35:54 +0200741{
742 struct ima_rule_entry *entry;
THOBY Simon4f2946a2021-08-16 08:11:01 +0000743 int new_policy_flag = 0;
Roberto Sassua7560242014-09-12 19:35:54 +0200744
THOBY Simon4f2946a2021-08-16 08:11:01 +0000745 rcu_read_lock();
Roberto Sassua7560242014-09-12 19:35:54 +0200746 list_for_each_entry(entry, ima_rules, list) {
THOBY Simon4f2946a2021-08-16 08:11:01 +0000747 /*
748 * SETXATTR_CHECK rules do not implement a full policy check
749 * because rule checking would probably have an important
750 * performance impact on setxattr(). As a consequence, only one
751 * SETXATTR_CHECK can be active at a given time.
752 * Because we want to preserve that property, we set out to use
753 * atomic_cmpxchg. Either:
754 * - the atomic was non-zero: a setxattr hash policy is
755 * already enforced, we do nothing
756 * - the atomic was zero: no setxattr policy was set, enable
757 * the setxattr hash policy
758 */
759 if (entry->func == SETXATTR_CHECK) {
760 atomic_cmpxchg(&ima_setxattr_allowed_hash_algorithms,
761 0, entry->allowed_algos);
762 /* SETXATTR_CHECK doesn't impact ima_policy_flag */
763 continue;
764 }
765
Roberto Sassua7560242014-09-12 19:35:54 +0200766 if (entry->action & IMA_DO_MASK)
THOBY Simon4f2946a2021-08-16 08:11:01 +0000767 new_policy_flag |= entry->action;
Roberto Sassua7560242014-09-12 19:35:54 +0200768 }
THOBY Simon4f2946a2021-08-16 08:11:01 +0000769 rcu_read_unlock();
Roberto Sassua7560242014-09-12 19:35:54 +0200770
Mimi Zoharef968372018-07-13 14:06:01 -0400771 ima_appraise |= (build_ima_appraise | temp_ima_appraise);
Roberto Sassua7560242014-09-12 19:35:54 +0200772 if (!ima_appraise)
THOBY Simon4f2946a2021-08-16 08:11:01 +0000773 new_policy_flag &= ~IMA_APPRAISE;
774
775 ima_policy_flag = new_policy_flag;
Roberto Sassua7560242014-09-12 19:35:54 +0200776}
777
Mimi Zohar6f0911a2018-04-12 00:15:22 -0400778static int ima_appraise_flag(enum ima_hooks func)
779{
780 if (func == MODULE_CHECK)
781 return IMA_APPRAISE_MODULES;
782 else if (func == FIRMWARE_CHECK)
783 return IMA_APPRAISE_FIRMWARE;
784 else if (func == POLICY_CHECK)
785 return IMA_APPRAISE_POLICY;
Mimi Zohar16c267a2018-07-13 14:05:58 -0400786 else if (func == KEXEC_KERNEL_CHECK)
787 return IMA_APPRAISE_KEXEC;
Mimi Zohar6f0911a2018-04-12 00:15:22 -0400788 return 0;
789}
790
Nayna Jainc52657d2018-10-09 23:00:35 +0530791static void add_rules(struct ima_rule_entry *entries, int count,
792 enum policy_rule_list policy_rule)
793{
794 int i = 0;
795
796 for (i = 0; i < count; i++) {
797 struct ima_rule_entry *entry;
798
799 if (policy_rule & IMA_DEFAULT_POLICY)
800 list_add_tail(&entries[i].list, &ima_default_rules);
801
802 if (policy_rule & IMA_CUSTOM_POLICY) {
803 entry = kmemdup(&entries[i], sizeof(*entry),
804 GFP_KERNEL);
805 if (!entry)
806 continue;
807
808 list_add_tail(&entry->list, &ima_policy_rules);
809 }
Krzysztof Struczynskib59fda42020-04-27 12:28:59 +0200810 if (entries[i].action == APPRAISE) {
811 if (entries != build_appraise_rules)
812 temp_ima_appraise |=
813 ima_appraise_flag(entries[i].func);
814 else
815 build_ima_appraise |=
816 ima_appraise_flag(entries[i].func);
817 }
Nayna Jainc52657d2018-10-09 23:00:35 +0530818 }
819}
820
Nayna Jain61917062018-10-09 23:00:36 +0530821static int ima_parse_rule(char *rule, struct ima_rule_entry *entry);
822
823static int __init ima_init_arch_policy(void)
824{
825 const char * const *arch_rules;
826 const char * const *rules;
827 int arch_entries = 0;
828 int i = 0;
829
830 arch_rules = arch_get_ima_policy();
831 if (!arch_rules)
832 return arch_entries;
833
834 /* Get number of rules */
835 for (rules = arch_rules; *rules != NULL; rules++)
836 arch_entries++;
837
838 arch_policy_entry = kcalloc(arch_entries + 1,
839 sizeof(*arch_policy_entry), GFP_KERNEL);
840 if (!arch_policy_entry)
841 return 0;
842
843 /* Convert each policy string rules to struct ima_rule_entry format */
844 for (rules = arch_rules, i = 0; *rules != NULL; rules++) {
845 char rule[255];
846 int result;
847
848 result = strlcpy(rule, *rules, sizeof(rule));
849
850 INIT_LIST_HEAD(&arch_policy_entry[i].list);
851 result = ima_parse_rule(rule, &arch_policy_entry[i]);
852 if (result) {
853 pr_warn("Skipping unknown architecture policy rule: %s\n",
854 rule);
855 memset(&arch_policy_entry[i], 0,
856 sizeof(*arch_policy_entry));
857 continue;
858 }
859 i++;
860 }
861 return i;
862}
863
Mimi Zohar3323eec2009-02-04 09:06:58 -0500864/**
865 * ima_init_policy - initialize the default measure rules.
866 *
Mimi Zohar07f6a792011-03-09 22:25:48 -0500867 * ima_rules points to either the ima_default_rules or the
868 * the new ima_policy_rules.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500869 */
Eric Paris932995f2009-05-21 15:43:32 -0400870void __init ima_init_policy(void)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500871{
Nayna Jain61917062018-10-09 23:00:36 +0530872 int build_appraise_entries, arch_entries;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500873
Nayna Jainc52657d2018-10-09 23:00:35 +0530874 /* if !ima_policy, we load NO default rules */
875 if (ima_policy)
876 add_rules(dont_measure_rules, ARRAY_SIZE(dont_measure_rules),
877 IMA_DEFAULT_POLICY);
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400878
879 switch (ima_policy) {
880 case ORIGINAL_TCB:
Nayna Jainc52657d2018-10-09 23:00:35 +0530881 add_rules(original_measurement_rules,
882 ARRAY_SIZE(original_measurement_rules),
883 IMA_DEFAULT_POLICY);
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400884 break;
885 case DEFAULT_TCB:
Nayna Jainc52657d2018-10-09 23:00:35 +0530886 add_rules(default_measurement_rules,
887 ARRAY_SIZE(default_measurement_rules),
888 IMA_DEFAULT_POLICY);
Gustavo A. R. Silva28073eb2020-11-20 12:25:46 -0600889 break;
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400890 default:
891 break;
892 }
Eric Paris5789ba32009-05-21 15:47:06 -0400893
Mimi Zohar503ceae2017-04-21 18:58:27 -0400894 /*
Nayna Jain61917062018-10-09 23:00:36 +0530895 * Based on runtime secure boot flags, insert arch specific measurement
896 * and appraise rules requiring file signatures for both the initial
897 * and custom policies, prior to other appraise rules.
898 * (Highest priority)
Mimi Zohar503ceae2017-04-21 18:58:27 -0400899 */
Nayna Jain61917062018-10-09 23:00:36 +0530900 arch_entries = ima_init_arch_policy();
901 if (!arch_entries)
902 pr_info("No architecture policies found\n");
903 else
904 add_rules(arch_policy_entry, arch_entries,
905 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
906
907 /*
Mimi Zohar503ceae2017-04-21 18:58:27 -0400908 * Insert the builtin "secure_boot" policy rules requiring file
Nayna Jain61917062018-10-09 23:00:36 +0530909 * signatures, prior to other appraise rules.
Mimi Zohar503ceae2017-04-21 18:58:27 -0400910 */
Nayna Jainc52657d2018-10-09 23:00:35 +0530911 if (ima_use_secure_boot)
912 add_rules(secure_boot_rules, ARRAY_SIZE(secure_boot_rules),
913 IMA_DEFAULT_POLICY);
Mimi Zohar503ceae2017-04-21 18:58:27 -0400914
Mimi Zoharef968372018-07-13 14:06:01 -0400915 /*
916 * Insert the build time appraise rules requiring file signatures
917 * for both the initial and custom policies, prior to other appraise
Nayna Jainc52657d2018-10-09 23:00:35 +0530918 * rules. As the secure boot rules includes all of the build time
919 * rules, include either one or the other set of rules, but not both.
Mimi Zoharef968372018-07-13 14:06:01 -0400920 */
Nayna Jainc52657d2018-10-09 23:00:35 +0530921 build_appraise_entries = ARRAY_SIZE(build_appraise_rules);
922 if (build_appraise_entries) {
923 if (ima_use_secure_boot)
924 add_rules(build_appraise_rules, build_appraise_entries,
925 IMA_CUSTOM_POLICY);
926 else
927 add_rules(build_appraise_rules, build_appraise_entries,
928 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
Mimi Zoharef968372018-07-13 14:06:01 -0400929 }
930
Nayna Jainc52657d2018-10-09 23:00:35 +0530931 if (ima_use_appraise_tcb)
932 add_rules(default_appraise_rules,
933 ARRAY_SIZE(default_appraise_rules),
934 IMA_DEFAULT_POLICY);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500935
Lakshmi Ramasubramanian03cee162021-01-07 20:07:07 -0800936 if (ima_use_critical_data)
937 add_rules(critical_data_rules,
938 ARRAY_SIZE(critical_data_rules),
939 IMA_DEFAULT_POLICY);
940
THOBY Simon4f2946a2021-08-16 08:11:01 +0000941 atomic_set(&ima_setxattr_allowed_hash_algorithms, 0);
942
943 ima_update_policy_flags();
Mimi Zohar3323eec2009-02-04 09:06:58 -0500944}
Mimi Zohar4af46622009-02-04 09:07:00 -0500945
Sasha Levin01127212015-12-22 08:51:23 -0500946/* Make sure we have a valid policy, at least containing some rules. */
Colin Ian Kingc75d8e92016-01-20 11:13:46 +0000947int ima_check_policy(void)
Sasha Levin01127212015-12-22 08:51:23 -0500948{
949 if (list_empty(&ima_temp_rules))
950 return -EINVAL;
951 return 0;
952}
953
Mimi Zohar4af46622009-02-04 09:07:00 -0500954/**
955 * ima_update_policy - update default_rules with new measure rules
956 *
957 * Called on file .release to update the default rules with a complete new
Petko Manolov38d859f2015-12-02 17:47:54 +0200958 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
959 * they make a queue. The policy may be updated multiple times and this is the
960 * RCU updater.
961 *
962 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
963 * we switch from the default policy to user defined.
Mimi Zohar4af46622009-02-04 09:07:00 -0500964 */
965void ima_update_policy(void)
966{
Petko Manolov53b626f2018-05-22 17:06:55 +0300967 struct list_head *policy = &ima_policy_rules;
Petko Manolov38d859f2015-12-02 17:47:54 +0200968
Petko Manolov53b626f2018-05-22 17:06:55 +0300969 list_splice_tail_init_rcu(&ima_temp_rules, policy, synchronize_rcu);
Petko Manolov38d859f2015-12-02 17:47:54 +0200970
971 if (ima_rules != policy) {
972 ima_policy_flag = 0;
973 ima_rules = policy;
Nayna Jain61917062018-10-09 23:00:36 +0530974
975 /*
976 * IMA architecture specific policy rules are specified
977 * as strings and converted to an array of ima_entry_rules
978 * on boot. After loading a custom policy, free the
979 * architecture specific rules stored as an array.
980 */
981 kfree(arch_policy_entry);
Petko Manolov38d859f2015-12-02 17:47:54 +0200982 }
THOBY Simon4f2946a2021-08-16 08:11:01 +0000983 ima_update_policy_flags();
Lakshmi Ramasubramanian450d0fd2020-01-22 17:32:05 -0800984
985 /* Custom IMA policy has been loaded */
986 ima_process_queued_keys();
Mimi Zohar4af46622009-02-04 09:07:00 -0500987}
988
Mimi Zohar1a9430d2018-12-17 19:14:49 -0500989/* Keep the enumeration in sync with the policy_tokens! */
Mimi Zohar4af46622009-02-04 09:07:00 -0500990enum {
Mimi Zohar1a9430d2018-12-17 19:14:49 -0500991 Opt_measure, Opt_dont_measure,
Mimi Zohar07f6a792011-03-09 22:25:48 -0500992 Opt_appraise, Opt_dont_appraise,
Mimi Zoharda1b0022016-09-29 10:04:52 -0400993 Opt_audit, Opt_hash, Opt_dont_hash,
Mimi Zohar4af46622009-02-04 09:07:00 -0500994 Opt_obj_user, Opt_obj_role, Opt_obj_type,
995 Opt_subj_user, Opt_subj_role, Opt_subj_type,
Mimi Zoharf1b08bb2018-01-15 11:20:36 -0500996 Opt_func, Opt_mask, Opt_fsmagic, Opt_fsname,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300997 Opt_fsuuid, Opt_uid_eq, Opt_euid_eq, Opt_fowner_eq,
998 Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt,
999 Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt,
THOBY Simon583a80a2021-08-16 08:11:00 +00001000 Opt_appraise_type, Opt_appraise_flag, Opt_appraise_algos,
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -08001001 Opt_permit_directio, Opt_pcr, Opt_template, Opt_keyrings,
Tushar Sugandhi47d76a42021-01-07 20:07:05 -08001002 Opt_label, Opt_err
Mimi Zohar4af46622009-02-04 09:07:00 -05001003};
1004
Mimi Zohar1a9430d2018-12-17 19:14:49 -05001005static const match_table_t policy_tokens = {
Mimi Zohar4af46622009-02-04 09:07:00 -05001006 {Opt_measure, "measure"},
1007 {Opt_dont_measure, "dont_measure"},
Mimi Zohar07f6a792011-03-09 22:25:48 -05001008 {Opt_appraise, "appraise"},
1009 {Opt_dont_appraise, "dont_appraise"},
Peter Moodye7c568e2012-06-14 10:04:36 -07001010 {Opt_audit, "audit"},
Mimi Zoharda1b0022016-09-29 10:04:52 -04001011 {Opt_hash, "hash"},
1012 {Opt_dont_hash, "dont_hash"},
Mimi Zohar4af46622009-02-04 09:07:00 -05001013 {Opt_obj_user, "obj_user=%s"},
1014 {Opt_obj_role, "obj_role=%s"},
1015 {Opt_obj_type, "obj_type=%s"},
1016 {Opt_subj_user, "subj_user=%s"},
1017 {Opt_subj_role, "subj_role=%s"},
1018 {Opt_subj_type, "subj_type=%s"},
1019 {Opt_func, "func=%s"},
1020 {Opt_mask, "mask=%s"},
1021 {Opt_fsmagic, "fsmagic=%s"},
Mimi Zoharf1b08bb2018-01-15 11:20:36 -05001022 {Opt_fsname, "fsname=%s"},
Dmitry Kasatkin85865c12012-09-03 23:23:13 +03001023 {Opt_fsuuid, "fsuuid=%s"},
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001024 {Opt_uid_eq, "uid=%s"},
1025 {Opt_euid_eq, "euid=%s"},
1026 {Opt_fowner_eq, "fowner=%s"},
1027 {Opt_uid_gt, "uid>%s"},
1028 {Opt_euid_gt, "euid>%s"},
1029 {Opt_fowner_gt, "fowner>%s"},
1030 {Opt_uid_lt, "uid<%s"},
1031 {Opt_euid_lt, "euid<%s"},
1032 {Opt_fowner_lt, "fowner<%s"},
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +03001033 {Opt_appraise_type, "appraise_type=%s"},
Nayna Jain273df862019-10-30 23:31:32 -04001034 {Opt_appraise_flag, "appraise_flag=%s"},
THOBY Simon583a80a2021-08-16 08:11:00 +00001035 {Opt_appraise_algos, "appraise_algos=%s"},
Mimi Zoharf9b2a732014-05-12 09:28:11 -04001036 {Opt_permit_directio, "permit_directio"},
Eric Richter02606432016-06-01 13:14:01 -05001037 {Opt_pcr, "pcr=%s"},
Matthew Garrett19453ce02019-06-19 15:46:11 -07001038 {Opt_template, "template=%s"},
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -08001039 {Opt_keyrings, "keyrings=%s"},
Tushar Sugandhi47d76a42021-01-07 20:07:05 -08001040 {Opt_label, "label=%s"},
Mimi Zohar4af46622009-02-04 09:07:00 -05001041 {Opt_err, NULL}
1042};
1043
Mimi Zohar07f6a792011-03-09 22:25:48 -05001044static int ima_lsm_rule_init(struct ima_rule_entry *entry,
Mimi Zohar7163a992013-01-03 14:19:09 -05001045 substring_t *args, int lsm_rule, int audit_type)
Mimi Zohar4af46622009-02-04 09:07:00 -05001046{
1047 int result;
1048
Eric Paris7b62e162010-04-20 10:21:01 -04001049 if (entry->lsm[lsm_rule].rule)
1050 return -EINVAL;
1051
Mimi Zohar7163a992013-01-03 14:19:09 -05001052 entry->lsm[lsm_rule].args_p = match_strdup(args);
1053 if (!entry->lsm[lsm_rule].args_p)
1054 return -ENOMEM;
1055
Mimi Zohar4af46622009-02-04 09:07:00 -05001056 entry->lsm[lsm_rule].type = audit_type;
Tyler Hicksb8867ee2020-07-10 15:37:50 -05001057 result = ima_filter_rule_init(entry->lsm[lsm_rule].type, Audit_equal,
1058 entry->lsm[lsm_rule].args_p,
1059 &entry->lsm[lsm_rule].rule);
Mimi Zohar7163a992013-01-03 14:19:09 -05001060 if (!entry->lsm[lsm_rule].rule) {
Janne Karhunen483ec262020-01-15 17:42:30 +02001061 pr_warn("rule for LSM \'%s\' is undefined\n",
Tyler Hicksaa0c0222020-07-09 01:19:08 -05001062 entry->lsm[lsm_rule].args_p);
Janne Karhunen483ec262020-01-15 17:42:30 +02001063
1064 if (ima_rules == &ima_default_rules) {
1065 kfree(entry->lsm[lsm_rule].args_p);
Tyler Hicks2bdd7372020-07-09 01:19:02 -05001066 entry->lsm[lsm_rule].args_p = NULL;
Janne Karhunen483ec262020-01-15 17:42:30 +02001067 result = -EINVAL;
1068 } else
1069 result = 0;
Mimi Zohar7163a992013-01-03 14:19:09 -05001070 }
1071
Mimi Zohar4af46622009-02-04 09:07:00 -05001072 return result;
1073}
1074
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001075static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value,
1076 bool (*rule_operator)(kuid_t, kuid_t))
Eric Paris2f1506c2010-04-20 10:21:30 -04001077{
Stefan Berger2afd0202018-06-04 16:54:54 -04001078 if (!ab)
1079 return;
1080
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001081 if (rule_operator == &uid_gt)
1082 audit_log_format(ab, "%s>", key);
1083 else if (rule_operator == &uid_lt)
1084 audit_log_format(ab, "%s<", key);
1085 else
1086 audit_log_format(ab, "%s=", key);
Stefan Berger3d2859d2018-06-04 16:54:53 -04001087 audit_log_format(ab, "%s ", value);
Eric Paris2f1506c2010-04-20 10:21:30 -04001088}
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001089static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
1090{
1091 ima_log_string_op(ab, key, value, NULL);
1092}
Eric Paris2f1506c2010-04-20 10:21:30 -04001093
Thiago Jung Bauermann3878d502019-06-27 23:19:32 -03001094/*
1095 * Validating the appended signature included in the measurement list requires
1096 * the file hash calculated without the appended signature (i.e., the 'd-modsig'
1097 * field). Therefore, notify the user if they have the 'modsig' field but not
1098 * the 'd-modsig' field in the template.
1099 */
1100static void check_template_modsig(const struct ima_template_desc *template)
1101{
1102#define MSG "template with 'modsig' field also needs 'd-modsig' field\n"
1103 bool has_modsig, has_dmodsig;
1104 static bool checked;
1105 int i;
1106
1107 /* We only need to notify the user once. */
1108 if (checked)
1109 return;
1110
1111 has_modsig = has_dmodsig = false;
1112 for (i = 0; i < template->num_fields; i++) {
1113 if (!strcmp(template->fields[i]->field_id, "modsig"))
1114 has_modsig = true;
1115 else if (!strcmp(template->fields[i]->field_id, "d-modsig"))
1116 has_dmodsig = true;
1117 }
1118
1119 if (has_modsig && !has_dmodsig)
1120 pr_notice(MSG);
1121
1122 checked = true;
1123#undef MSG
1124}
1125
Tyler Hicks712183432020-07-09 01:19:03 -05001126static bool ima_validate_rule(struct ima_rule_entry *entry)
1127{
Tyler Hicks30031b02020-07-09 01:19:09 -05001128 /* Ensure that the action is set and is compatible with the flags */
Tyler Hicks712183432020-07-09 01:19:03 -05001129 if (entry->action == UNKNOWN)
1130 return false;
1131
Tyler Hicks30031b02020-07-09 01:19:09 -05001132 if (entry->action != MEASURE && entry->flags & IMA_PCR)
1133 return false;
1134
1135 if (entry->action != APPRAISE &&
THOBY Simon583a80a2021-08-16 08:11:00 +00001136 entry->flags & (IMA_DIGSIG_REQUIRED | IMA_MODSIG_ALLOWED |
1137 IMA_CHECK_BLACKLIST | IMA_VALIDATE_ALGOS))
Tyler Hicks30031b02020-07-09 01:19:09 -05001138 return false;
1139
1140 /*
1141 * The IMA_FUNC bit must be set if and only if there's a valid hook
1142 * function specified, and vice versa. Enforcing this property allows
1143 * for the NONE case below to validate a rule without an explicit hook
1144 * function.
1145 */
1146 if (((entry->flags & IMA_FUNC) && entry->func == NONE) ||
1147 (!(entry->flags & IMA_FUNC) && entry->func != NONE))
1148 return false;
1149
Tyler Hicks712183432020-07-09 01:19:03 -05001150 /*
1151 * Ensure that the hook function is compatible with the other
1152 * components of the rule
1153 */
1154 switch (entry->func) {
1155 case NONE:
1156 case FILE_CHECK:
1157 case MMAP_CHECK:
1158 case BPRM_CHECK:
1159 case CREDS_CHECK:
1160 case POST_SETATTR:
Tyler Hicks712183432020-07-09 01:19:03 -05001161 case FIRMWARE_CHECK:
Tyler Hicks30031b02020-07-09 01:19:09 -05001162 case POLICY_CHECK:
1163 if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
1164 IMA_UID | IMA_FOWNER | IMA_FSUUID |
1165 IMA_INMASK | IMA_EUID | IMA_PCR |
1166 IMA_FSNAME | IMA_DIGSIG_REQUIRED |
THOBY Simon583a80a2021-08-16 08:11:00 +00001167 IMA_PERMIT_DIRECTIO | IMA_VALIDATE_ALGOS))
Tyler Hicks30031b02020-07-09 01:19:09 -05001168 return false;
1169
1170 break;
1171 case MODULE_CHECK:
Tyler Hicks712183432020-07-09 01:19:03 -05001172 case KEXEC_KERNEL_CHECK:
1173 case KEXEC_INITRAMFS_CHECK:
Tyler Hicks30031b02020-07-09 01:19:09 -05001174 if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
1175 IMA_UID | IMA_FOWNER | IMA_FSUUID |
1176 IMA_INMASK | IMA_EUID | IMA_PCR |
1177 IMA_FSNAME | IMA_DIGSIG_REQUIRED |
1178 IMA_PERMIT_DIRECTIO | IMA_MODSIG_ALLOWED |
THOBY Simon583a80a2021-08-16 08:11:00 +00001179 IMA_CHECK_BLACKLIST | IMA_VALIDATE_ALGOS))
Tyler Hicks30031b02020-07-09 01:19:09 -05001180 return false;
1181
Tyler Hicks712183432020-07-09 01:19:03 -05001182 break;
1183 case KEXEC_CMDLINE:
Tyler Hicksdb2045f2020-07-09 01:19:04 -05001184 if (entry->action & ~(MEASURE | DONT_MEASURE))
1185 return false;
1186
Tyler Hicks48341772020-07-09 01:19:11 -05001187 if (entry->flags & ~(IMA_FUNC | IMA_FSMAGIC | IMA_UID |
1188 IMA_FOWNER | IMA_FSUUID | IMA_EUID |
1189 IMA_PCR | IMA_FSNAME))
Tyler Hicksdb2045f2020-07-09 01:19:04 -05001190 return false;
1191
1192 break;
Tyler Hicks712183432020-07-09 01:19:03 -05001193 case KEY_CHECK:
1194 if (entry->action & ~(MEASURE | DONT_MEASURE))
1195 return false;
1196
Tyler Hickseb624fe2020-07-09 01:19:05 -05001197 if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_PCR |
1198 IMA_KEYRINGS))
1199 return false;
1200
1201 if (ima_rule_contains_lsm_cond(entry))
1202 return false;
1203
Tyler Hicks712183432020-07-09 01:19:03 -05001204 break;
Tushar Sugandhic4e43aa2021-01-07 20:07:04 -08001205 case CRITICAL_DATA:
1206 if (entry->action & ~(MEASURE | DONT_MEASURE))
1207 return false;
1208
Tushar Sugandhi47d76a42021-01-07 20:07:05 -08001209 if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_PCR |
1210 IMA_LABEL))
Tushar Sugandhic4e43aa2021-01-07 20:07:04 -08001211 return false;
1212
1213 if (ima_rule_contains_lsm_cond(entry))
1214 return false;
1215
1216 break;
THOBY Simon4f2946a2021-08-16 08:11:01 +00001217 case SETXATTR_CHECK:
1218 /* any action other than APPRAISE is unsupported */
1219 if (entry->action != APPRAISE)
1220 return false;
1221
1222 /* SETXATTR_CHECK requires an appraise_algos parameter */
1223 if (!(entry->flags & IMA_VALIDATE_ALGOS))
1224 return false;
1225
1226 /*
1227 * full policies are not supported, they would have too
1228 * much of a performance impact
1229 */
1230 if (entry->flags & ~(IMA_FUNC | IMA_VALIDATE_ALGOS))
1231 return false;
1232
1233 break;
Tyler Hicks712183432020-07-09 01:19:03 -05001234 default:
1235 return false;
1236 }
1237
Tyler Hicks5f3e9262020-07-09 01:19:06 -05001238 /* Ensure that combinations of flags are compatible with each other */
1239 if (entry->flags & IMA_CHECK_BLACKLIST &&
1240 !(entry->flags & IMA_MODSIG_ALLOWED))
1241 return false;
1242
Tyler Hicks712183432020-07-09 01:19:03 -05001243 return true;
1244}
1245
THOBY Simon583a80a2021-08-16 08:11:00 +00001246static unsigned int ima_parse_appraise_algos(char *arg)
1247{
1248 unsigned int res = 0;
1249 int idx;
1250 char *token;
1251
1252 while ((token = strsep(&arg, ",")) != NULL) {
1253 idx = match_string(hash_algo_name, HASH_ALGO__LAST, token);
1254
1255 if (idx < 0) {
1256 pr_err("unknown hash algorithm \"%s\"",
1257 token);
1258 return 0;
1259 }
1260
THOBY Simon8ecd39c2021-08-16 08:11:01 +00001261 if (!crypto_has_alg(hash_algo_name[idx], 0, 0)) {
1262 pr_err("unavailable hash algorithm \"%s\", check your kernel configuration",
1263 token);
1264 return 0;
1265 }
1266
THOBY Simon583a80a2021-08-16 08:11:00 +00001267 /* Add the hash algorithm to the 'allowed' bitfield */
1268 res |= (1U << idx);
1269 }
1270
1271 return res;
1272}
1273
Mimi Zohar07f6a792011-03-09 22:25:48 -05001274static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
Mimi Zohar4af46622009-02-04 09:07:00 -05001275{
1276 struct audit_buffer *ab;
Mimi Zohar4351c292014-11-05 07:53:55 -05001277 char *from;
Mimi Zohar4af46622009-02-04 09:07:00 -05001278 char *p;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001279 bool uid_token;
Matthew Garrett19453ce02019-06-19 15:46:11 -07001280 struct ima_template_desc *template_desc;
Mimi Zohar4af46622009-02-04 09:07:00 -05001281 int result = 0;
1282
Stefan Bergerdba31ee2018-06-04 16:54:55 -04001283 ab = integrity_audit_log_start(audit_context(), GFP_KERNEL,
1284 AUDIT_INTEGRITY_POLICY_RULE);
Mimi Zohar4af46622009-02-04 09:07:00 -05001285
Eric W. Biederman8b94eea2012-05-25 18:24:12 -06001286 entry->uid = INVALID_UID;
Linus Torvalds882653222012-10-02 21:38:48 -07001287 entry->fowner = INVALID_UID;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001288 entry->uid_op = &uid_eq;
1289 entry->fowner_op = &uid_eq;
Eric Parisb9035b12010-04-20 10:21:07 -04001290 entry->action = UNKNOWN;
Eric Paris28ef4002010-04-20 10:21:18 -04001291 while ((p = strsep(&rule, " \t")) != NULL) {
Mimi Zohar4af46622009-02-04 09:07:00 -05001292 substring_t args[MAX_OPT_ARGS];
1293 int token;
1294 unsigned long lnum;
1295
1296 if (result < 0)
1297 break;
Eric Paris28ef4002010-04-20 10:21:18 -04001298 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
1299 continue;
Mimi Zohar4af46622009-02-04 09:07:00 -05001300 token = match_token(p, policy_tokens, args);
1301 switch (token) {
1302 case Opt_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -04001303 ima_log_string(ab, "action", "measure");
Eric Paris7b62e162010-04-20 10:21:01 -04001304
1305 if (entry->action != UNKNOWN)
1306 result = -EINVAL;
1307
Mimi Zohar4af46622009-02-04 09:07:00 -05001308 entry->action = MEASURE;
1309 break;
1310 case Opt_dont_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -04001311 ima_log_string(ab, "action", "dont_measure");
Eric Paris7b62e162010-04-20 10:21:01 -04001312
1313 if (entry->action != UNKNOWN)
1314 result = -EINVAL;
1315
Mimi Zohar4af46622009-02-04 09:07:00 -05001316 entry->action = DONT_MEASURE;
1317 break;
Mimi Zohar07f6a792011-03-09 22:25:48 -05001318 case Opt_appraise:
1319 ima_log_string(ab, "action", "appraise");
1320
1321 if (entry->action != UNKNOWN)
1322 result = -EINVAL;
1323
1324 entry->action = APPRAISE;
1325 break;
1326 case Opt_dont_appraise:
1327 ima_log_string(ab, "action", "dont_appraise");
1328
1329 if (entry->action != UNKNOWN)
1330 result = -EINVAL;
1331
1332 entry->action = DONT_APPRAISE;
1333 break;
Peter Moodye7c568e2012-06-14 10:04:36 -07001334 case Opt_audit:
1335 ima_log_string(ab, "action", "audit");
1336
1337 if (entry->action != UNKNOWN)
1338 result = -EINVAL;
1339
1340 entry->action = AUDIT;
1341 break;
Mimi Zoharda1b0022016-09-29 10:04:52 -04001342 case Opt_hash:
1343 ima_log_string(ab, "action", "hash");
1344
1345 if (entry->action != UNKNOWN)
1346 result = -EINVAL;
1347
1348 entry->action = HASH;
1349 break;
1350 case Opt_dont_hash:
1351 ima_log_string(ab, "action", "dont_hash");
1352
1353 if (entry->action != UNKNOWN)
1354 result = -EINVAL;
1355
1356 entry->action = DONT_HASH;
1357 break;
Mimi Zohar4af46622009-02-04 09:07:00 -05001358 case Opt_func:
Eric Paris2f1506c2010-04-20 10:21:30 -04001359 ima_log_string(ab, "func", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -04001360
1361 if (entry->func)
Mimi Zohar07f6a792011-03-09 22:25:48 -05001362 result = -EINVAL;
Eric Paris7b62e162010-04-20 10:21:01 -04001363
Mimi Zohar1e93d002010-01-26 17:02:41 -05001364 if (strcmp(args[0].from, "FILE_CHECK") == 0)
1365 entry->func = FILE_CHECK;
1366 /* PATH_CHECK is for backwards compat */
1367 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
1368 entry->func = FILE_CHECK;
Mimi Zoharfdf90722012-10-16 12:40:08 +10301369 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
1370 entry->func = MODULE_CHECK;
Mimi Zohar5a9196d2014-07-22 10:39:48 -04001371 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
1372 entry->func = FIRMWARE_CHECK;
Mimi Zohar16cac492012-12-13 11:15:04 -05001373 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
1374 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
1375 entry->func = MMAP_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -05001376 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
1377 entry->func = BPRM_CHECK;
Matthew Garrettd906c102018-01-08 13:36:20 -08001378 else if (strcmp(args[0].from, "CREDS_CHECK") == 0)
1379 entry->func = CREDS_CHECK;
Mimi Zohard9ddf072016-01-14 20:59:14 -05001380 else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
1381 0)
1382 entry->func = KEXEC_KERNEL_CHECK;
1383 else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
1384 == 0)
1385 entry->func = KEXEC_INITRAMFS_CHECK;
Mimi Zohar19f8a842016-01-15 10:17:12 -05001386 else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
1387 entry->func = POLICY_CHECK;
Prakhar Srivastavab0935122019-06-23 23:23:29 -07001388 else if (strcmp(args[0].from, "KEXEC_CMDLINE") == 0)
1389 entry->func = KEXEC_CMDLINE;
Tyler Hicks48ce1dd2020-08-11 14:26:21 -05001390 else if (IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) &&
1391 strcmp(args[0].from, "KEY_CHECK") == 0)
Lakshmi Ramasubramanian58086112019-12-11 08:47:03 -08001392 entry->func = KEY_CHECK;
Tushar Sugandhic4e43aa2021-01-07 20:07:04 -08001393 else if (strcmp(args[0].from, "CRITICAL_DATA") == 0)
1394 entry->func = CRITICAL_DATA;
THOBY Simon4f2946a2021-08-16 08:11:01 +00001395 else if (strcmp(args[0].from, "SETXATTR_CHECK") == 0)
1396 entry->func = SETXATTR_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -05001397 else
1398 result = -EINVAL;
1399 if (!result)
1400 entry->flags |= IMA_FUNC;
1401 break;
1402 case Opt_mask:
Eric Paris2f1506c2010-04-20 10:21:30 -04001403 ima_log_string(ab, "mask", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -04001404
1405 if (entry->mask)
1406 result = -EINVAL;
1407
Mimi Zohar4351c292014-11-05 07:53:55 -05001408 from = args[0].from;
1409 if (*from == '^')
1410 from++;
1411
1412 if ((strcmp(from, "MAY_EXEC")) == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -05001413 entry->mask = MAY_EXEC;
Mimi Zohar4351c292014-11-05 07:53:55 -05001414 else if (strcmp(from, "MAY_WRITE") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -05001415 entry->mask = MAY_WRITE;
Mimi Zohar4351c292014-11-05 07:53:55 -05001416 else if (strcmp(from, "MAY_READ") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -05001417 entry->mask = MAY_READ;
Mimi Zohar4351c292014-11-05 07:53:55 -05001418 else if (strcmp(from, "MAY_APPEND") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -05001419 entry->mask = MAY_APPEND;
1420 else
1421 result = -EINVAL;
1422 if (!result)
Mimi Zohar4351c292014-11-05 07:53:55 -05001423 entry->flags |= (*args[0].from == '^')
1424 ? IMA_INMASK : IMA_MASK;
Mimi Zohar4af46622009-02-04 09:07:00 -05001425 break;
1426 case Opt_fsmagic:
Eric Paris2f1506c2010-04-20 10:21:30 -04001427 ima_log_string(ab, "fsmagic", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -04001428
1429 if (entry->fsmagic) {
1430 result = -EINVAL;
1431 break;
1432 }
1433
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +02001434 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
Mimi Zohar4af46622009-02-04 09:07:00 -05001435 if (!result)
1436 entry->flags |= IMA_FSMAGIC;
1437 break;
Mimi Zoharf1b08bb2018-01-15 11:20:36 -05001438 case Opt_fsname:
1439 ima_log_string(ab, "fsname", args[0].from);
1440
1441 entry->fsname = kstrdup(args[0].from, GFP_KERNEL);
1442 if (!entry->fsname) {
1443 result = -ENOMEM;
1444 break;
1445 }
1446 result = 0;
1447 entry->flags |= IMA_FSNAME;
1448 break;
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -08001449 case Opt_keyrings:
1450 ima_log_string(ab, "keyrings", args[0].from);
1451
Tyler Hicks48ce1dd2020-08-11 14:26:21 -05001452 if (!IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) ||
1453 entry->keyrings) {
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -08001454 result = -EINVAL;
1455 break;
1456 }
Lakshmi Ramasubramanian5c7bac92020-01-16 18:18:21 -08001457
Tyler Hicks176377d2020-08-11 14:26:20 -05001458 entry->keyrings = ima_alloc_rule_opt_list(args);
1459 if (IS_ERR(entry->keyrings)) {
1460 result = PTR_ERR(entry->keyrings);
1461 entry->keyrings = NULL;
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -08001462 break;
1463 }
Tyler Hicks176377d2020-08-11 14:26:20 -05001464
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -08001465 entry->flags |= IMA_KEYRINGS;
1466 break;
Tushar Sugandhi47d76a42021-01-07 20:07:05 -08001467 case Opt_label:
1468 ima_log_string(ab, "label", args[0].from);
1469
1470 if (entry->label) {
1471 result = -EINVAL;
1472 break;
1473 }
1474
1475 entry->label = ima_alloc_rule_opt_list(args);
1476 if (IS_ERR(entry->label)) {
1477 result = PTR_ERR(entry->label);
1478 entry->label = NULL;
1479 break;
1480 }
1481
1482 entry->flags |= IMA_LABEL;
1483 break;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +03001484 case Opt_fsuuid:
1485 ima_log_string(ab, "fsuuid", args[0].from);
1486
Mike Rapoport36447452018-01-17 20:27:11 +02001487 if (!uuid_is_null(&entry->fsuuid)) {
Dmitry Kasatkin85865c12012-09-03 23:23:13 +03001488 result = -EINVAL;
1489 break;
1490 }
1491
Christoph Hellwig787d8c52017-06-01 07:00:26 +02001492 result = uuid_parse(args[0].from, &entry->fsuuid);
Mimi Zohar446d64e2013-02-24 23:42:37 -05001493 if (!result)
1494 entry->flags |= IMA_FSUUID;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +03001495 break;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001496 case Opt_uid_gt:
1497 case Opt_euid_gt:
1498 entry->uid_op = &uid_gt;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001499 fallthrough;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001500 case Opt_uid_lt:
1501 case Opt_euid_lt:
1502 if ((token == Opt_uid_lt) || (token == Opt_euid_lt))
1503 entry->uid_op = &uid_lt;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001504 fallthrough;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001505 case Opt_uid_eq:
1506 case Opt_euid_eq:
1507 uid_token = (token == Opt_uid_eq) ||
1508 (token == Opt_uid_gt) ||
1509 (token == Opt_uid_lt);
1510
1511 ima_log_string_op(ab, uid_token ? "uid" : "euid",
1512 args[0].from, entry->uid_op);
Eric Paris7b62e162010-04-20 10:21:01 -04001513
Eric W. Biederman8b94eea2012-05-25 18:24:12 -06001514 if (uid_valid(entry->uid)) {
Eric Paris7b62e162010-04-20 10:21:01 -04001515 result = -EINVAL;
1516 break;
1517 }
1518
Jingoo Han29707b22014-02-05 15:13:14 +09001519 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar4af46622009-02-04 09:07:00 -05001520 if (!result) {
Mimi Zohar139069e2014-11-05 07:48:36 -05001521 entry->uid = make_kuid(current_user_ns(),
1522 (uid_t) lnum);
1523 if (!uid_valid(entry->uid) ||
1524 (uid_t)lnum != lnum)
Mimi Zohar4af46622009-02-04 09:07:00 -05001525 result = -EINVAL;
1526 else
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001527 entry->flags |= uid_token
Mimi Zohar139069e2014-11-05 07:48:36 -05001528 ? IMA_UID : IMA_EUID;
Mimi Zohar4af46622009-02-04 09:07:00 -05001529 }
1530 break;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001531 case Opt_fowner_gt:
1532 entry->fowner_op = &uid_gt;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001533 fallthrough;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001534 case Opt_fowner_lt:
1535 if (token == Opt_fowner_lt)
1536 entry->fowner_op = &uid_lt;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001537 fallthrough;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001538 case Opt_fowner_eq:
1539 ima_log_string_op(ab, "fowner", args[0].from,
1540 entry->fowner_op);
Mimi Zohar07f6a792011-03-09 22:25:48 -05001541
Linus Torvalds882653222012-10-02 21:38:48 -07001542 if (uid_valid(entry->fowner)) {
Mimi Zohar07f6a792011-03-09 22:25:48 -05001543 result = -EINVAL;
1544 break;
1545 }
1546
Jingoo Han29707b22014-02-05 15:13:14 +09001547 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar07f6a792011-03-09 22:25:48 -05001548 if (!result) {
Linus Torvalds882653222012-10-02 21:38:48 -07001549 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
1550 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
Mimi Zohar07f6a792011-03-09 22:25:48 -05001551 result = -EINVAL;
1552 else
1553 entry->flags |= IMA_FOWNER;
1554 }
1555 break;
Mimi Zohar4af46622009-02-04 09:07:00 -05001556 case Opt_obj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -04001557 ima_log_string(ab, "obj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -05001558 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -05001559 LSM_OBJ_USER,
1560 AUDIT_OBJ_USER);
1561 break;
1562 case Opt_obj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -04001563 ima_log_string(ab, "obj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -05001564 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -05001565 LSM_OBJ_ROLE,
1566 AUDIT_OBJ_ROLE);
1567 break;
1568 case Opt_obj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -04001569 ima_log_string(ab, "obj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -05001570 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -05001571 LSM_OBJ_TYPE,
1572 AUDIT_OBJ_TYPE);
1573 break;
1574 case Opt_subj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -04001575 ima_log_string(ab, "subj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -05001576 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -05001577 LSM_SUBJ_USER,
1578 AUDIT_SUBJ_USER);
1579 break;
1580 case Opt_subj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -04001581 ima_log_string(ab, "subj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -05001582 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -05001583 LSM_SUBJ_ROLE,
1584 AUDIT_SUBJ_ROLE);
1585 break;
1586 case Opt_subj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -04001587 ima_log_string(ab, "subj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -05001588 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -05001589 LSM_SUBJ_TYPE,
1590 AUDIT_SUBJ_TYPE);
1591 break;
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +03001592 case Opt_appraise_type:
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +03001593 ima_log_string(ab, "appraise_type", args[0].from);
1594 if ((strcmp(args[0].from, "imasig")) == 0)
1595 entry->flags |= IMA_DIGSIG_REQUIRED;
Tyler Hicks30031b02020-07-09 01:19:09 -05001596 else if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
Thiago Jung Bauermann9044d622019-06-27 23:19:28 -03001597 strcmp(args[0].from, "imasig|modsig") == 0)
1598 entry->flags |= IMA_DIGSIG_REQUIRED |
1599 IMA_MODSIG_ALLOWED;
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +03001600 else
1601 result = -EINVAL;
1602 break;
Nayna Jain273df862019-10-30 23:31:32 -04001603 case Opt_appraise_flag:
1604 ima_log_string(ab, "appraise_flag", args[0].from);
Tyler Hicks5f3e9262020-07-09 01:19:06 -05001605 if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
1606 strstr(args[0].from, "blacklist"))
Nayna Jain273df862019-10-30 23:31:32 -04001607 entry->flags |= IMA_CHECK_BLACKLIST;
Tyler Hicks5f3e9262020-07-09 01:19:06 -05001608 else
1609 result = -EINVAL;
Nayna Jain273df862019-10-30 23:31:32 -04001610 break;
THOBY Simon583a80a2021-08-16 08:11:00 +00001611 case Opt_appraise_algos:
1612 ima_log_string(ab, "appraise_algos", args[0].from);
1613
1614 if (entry->allowed_algos) {
1615 result = -EINVAL;
1616 break;
1617 }
1618
1619 entry->allowed_algos =
1620 ima_parse_appraise_algos(args[0].from);
1621 /* invalid or empty list of algorithms */
1622 if (!entry->allowed_algos) {
1623 result = -EINVAL;
1624 break;
1625 }
1626
1627 entry->flags |= IMA_VALIDATE_ALGOS;
1628
1629 break;
Mimi Zoharf9b2a732014-05-12 09:28:11 -04001630 case Opt_permit_directio:
1631 entry->flags |= IMA_PERMIT_DIRECTIO;
1632 break;
Eric Richter02606432016-06-01 13:14:01 -05001633 case Opt_pcr:
Eric Richter02606432016-06-01 13:14:01 -05001634 ima_log_string(ab, "pcr", args[0].from);
1635
1636 result = kstrtoint(args[0].from, 10, &entry->pcr);
1637 if (result || INVALID_PCR(entry->pcr))
1638 result = -EINVAL;
1639 else
1640 entry->flags |= IMA_PCR;
1641
1642 break;
Matthew Garrett19453ce02019-06-19 15:46:11 -07001643 case Opt_template:
1644 ima_log_string(ab, "template", args[0].from);
1645 if (entry->action != MEASURE) {
1646 result = -EINVAL;
1647 break;
1648 }
1649 template_desc = lookup_template_desc(args[0].from);
1650 if (!template_desc || entry->template) {
1651 result = -EINVAL;
1652 break;
1653 }
1654
1655 /*
1656 * template_desc_init_fields() does nothing if
1657 * the template is already initialised, so
1658 * it's safe to do this unconditionally
1659 */
1660 template_desc_init_fields(template_desc->fmt,
1661 &(template_desc->fields),
1662 &(template_desc->num_fields));
1663 entry->template = template_desc;
1664 break;
Mimi Zohar4af46622009-02-04 09:07:00 -05001665 case Opt_err:
Eric Paris2f1506c2010-04-20 10:21:30 -04001666 ima_log_string(ab, "UNKNOWN", p);
Eric Parise9d393b2010-04-20 10:21:13 -04001667 result = -EINVAL;
Mimi Zohar4af46622009-02-04 09:07:00 -05001668 break;
1669 }
1670 }
Tyler Hicks712183432020-07-09 01:19:03 -05001671 if (!result && !ima_validate_rule(entry))
Mimi Zohar4af46622009-02-04 09:07:00 -05001672 result = -EINVAL;
Mimi Zohar6f0911a2018-04-12 00:15:22 -04001673 else if (entry->action == APPRAISE)
1674 temp_ima_appraise |= ima_appraise_flag(entry->func);
1675
Thiago Jung Bauermann3878d502019-06-27 23:19:32 -03001676 if (!result && entry->flags & IMA_MODSIG_ALLOWED) {
1677 template_desc = entry->template ? entry->template :
1678 ima_template_desc_current();
1679 check_template_modsig(template_desc);
1680 }
1681
Eric Parisb0d5de42012-02-14 17:11:07 -05001682 audit_log_format(ab, "res=%d", !result);
Mimi Zohar4af46622009-02-04 09:07:00 -05001683 audit_log_end(ab);
1684 return result;
1685}
1686
1687/**
Mimi Zohar07f6a792011-03-09 22:25:48 -05001688 * ima_parse_add_rule - add a rule to ima_policy_rules
Mimi Zohar4af46622009-02-04 09:07:00 -05001689 * @rule - ima measurement policy rule
1690 *
Petko Manolov38d859f2015-12-02 17:47:54 +02001691 * Avoid locking by allowing just one writer at a time in ima_write_policy()
Eric Paris6ccd0452010-04-20 10:20:54 -04001692 * Returns the length of the rule parsed, an error code on failure
Mimi Zohar4af46622009-02-04 09:07:00 -05001693 */
Eric Paris6ccd0452010-04-20 10:20:54 -04001694ssize_t ima_parse_add_rule(char *rule)
Mimi Zohar4af46622009-02-04 09:07:00 -05001695{
Mimi Zohar52a13282013-12-11 14:44:04 -05001696 static const char op[] = "update_policy";
Eric Paris6ccd0452010-04-20 10:20:54 -04001697 char *p;
Mimi Zohar07f6a792011-03-09 22:25:48 -05001698 struct ima_rule_entry *entry;
Eric Paris6ccd0452010-04-20 10:20:54 -04001699 ssize_t result, len;
Mimi Zohar4af46622009-02-04 09:07:00 -05001700 int audit_info = 0;
1701
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +03001702 p = strsep(&rule, "\n");
1703 len = strlen(p) + 1;
Dmitry Kasatkin71787842014-10-03 14:40:20 +03001704 p += strspn(p, " \t");
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +03001705
Dmitry Kasatkin71787842014-10-03 14:40:20 +03001706 if (*p == '#' || *p == '\0')
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +03001707 return len;
1708
Mimi Zohar4af46622009-02-04 09:07:00 -05001709 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1710 if (!entry) {
1711 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1712 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
1713 return -ENOMEM;
1714 }
1715
1716 INIT_LIST_HEAD(&entry->list);
1717
Eric Paris6ccd0452010-04-20 10:20:54 -04001718 result = ima_parse_rule(p, entry);
Eric Paris7233e3e2010-04-20 10:21:24 -04001719 if (result) {
Tyler Hicks2bdd7372020-07-09 01:19:02 -05001720 ima_free_rule(entry);
Mimi Zohar523979a2009-02-11 11:12:28 -05001721 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
Richard Guy Briggs7e9001f2014-06-16 15:52:07 -04001722 NULL, op, "invalid-policy", result,
Mimi Zohar523979a2009-02-11 11:12:28 -05001723 audit_info);
Eric Paris7233e3e2010-04-20 10:21:24 -04001724 return result;
Mimi Zohar523979a2009-02-11 11:12:28 -05001725 }
Eric Paris7233e3e2010-04-20 10:21:24 -04001726
Petko Manolov38d859f2015-12-02 17:47:54 +02001727 list_add_tail(&entry->list, &ima_temp_rules);
Eric Paris7233e3e2010-04-20 10:21:24 -04001728
1729 return len;
Mimi Zohar4af46622009-02-04 09:07:00 -05001730}
1731
Petko Manolov38d859f2015-12-02 17:47:54 +02001732/**
1733 * ima_delete_rules() called to cleanup invalid in-flight policy.
1734 * We don't need locking as we operate on the temp list, which is
1735 * different from the active one. There is also only one user of
1736 * ima_delete_rules() at a time.
1737 */
James Morris64c61d82009-02-05 09:28:26 +11001738void ima_delete_rules(void)
Mimi Zohar4af46622009-02-04 09:07:00 -05001739{
Mimi Zohar07f6a792011-03-09 22:25:48 -05001740 struct ima_rule_entry *entry, *tmp;
Mimi Zohar4af46622009-02-04 09:07:00 -05001741
Mimi Zohar6ad6afa2015-12-07 14:35:47 -05001742 temp_ima_appraise = 0;
Petko Manolov38d859f2015-12-02 17:47:54 +02001743 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
Mimi Zohar4af46622009-02-04 09:07:00 -05001744 list_del(&entry->list);
Tyler Hicks465aee72020-07-09 01:19:01 -05001745 ima_free_rule(entry);
Mimi Zohar4af46622009-02-04 09:07:00 -05001746 }
Mimi Zohar4af46622009-02-04 09:07:00 -05001747}
Petko Manolov80eae202015-12-02 17:47:56 +02001748
Lakshmi Ramasubramanian34e980b2020-06-18 14:10:12 -07001749#define __ima_hook_stringify(func, str) (#func),
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -03001750
1751const char *const func_tokens[] = {
1752 __ima_hooks(__ima_hook_stringify)
1753};
1754
Petko Manolov80eae202015-12-02 17:47:56 +02001755#ifdef CONFIG_IMA_READ_POLICY
1756enum {
1757 mask_exec = 0, mask_write, mask_read, mask_append
1758};
1759
Thiago Jung Bauermannbb543e32017-06-07 22:49:10 -03001760static const char *const mask_tokens[] = {
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02001761 "^MAY_EXEC",
1762 "^MAY_WRITE",
1763 "^MAY_READ",
1764 "^MAY_APPEND"
Petko Manolov80eae202015-12-02 17:47:56 +02001765};
1766
Petko Manolov80eae202015-12-02 17:47:56 +02001767void *ima_policy_start(struct seq_file *m, loff_t *pos)
1768{
1769 loff_t l = *pos;
1770 struct ima_rule_entry *entry;
1771
1772 rcu_read_lock();
1773 list_for_each_entry_rcu(entry, ima_rules, list) {
1774 if (!l--) {
1775 rcu_read_unlock();
1776 return entry;
1777 }
1778 }
1779 rcu_read_unlock();
1780 return NULL;
1781}
1782
1783void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
1784{
1785 struct ima_rule_entry *entry = v;
1786
1787 rcu_read_lock();
1788 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
1789 rcu_read_unlock();
1790 (*pos)++;
1791
1792 return (&entry->list == ima_rules) ? NULL : entry;
1793}
1794
1795void ima_policy_stop(struct seq_file *m, void *v)
1796{
1797}
1798
Mimi Zohar1a9430d2018-12-17 19:14:49 -05001799#define pt(token) policy_tokens[token].pattern
Petko Manolov80eae202015-12-02 17:47:56 +02001800#define mt(token) mask_tokens[token]
Petko Manolov80eae202015-12-02 17:47:56 +02001801
Mimi Zoharb5269ab2016-01-28 13:10:36 -05001802/*
1803 * policy_func_show - display the ima_hooks policy rule
1804 */
1805static void policy_func_show(struct seq_file *m, enum ima_hooks func)
1806{
Thiago Jung Bauermann26632182017-06-07 22:49:11 -03001807 if (func > 0 && func < MAX_CHECK)
1808 seq_printf(m, "func=%s ", func_tokens[func]);
1809 else
1810 seq_printf(m, "func=%d ", func);
Mimi Zoharb5269ab2016-01-28 13:10:36 -05001811}
1812
Tyler Hicks176377d2020-08-11 14:26:20 -05001813static void ima_show_rule_opt_list(struct seq_file *m,
1814 const struct ima_rule_opt_list *opt_list)
1815{
1816 size_t i;
1817
1818 for (i = 0; i < opt_list->count; i++)
1819 seq_printf(m, "%s%s", i ? "|" : "", opt_list->items[i]);
1820}
1821
THOBY Simon583a80a2021-08-16 08:11:00 +00001822static void ima_policy_show_appraise_algos(struct seq_file *m,
1823 unsigned int allowed_hashes)
1824{
1825 int idx, list_size = 0;
1826
1827 for (idx = 0; idx < HASH_ALGO__LAST; idx++) {
1828 if (!(allowed_hashes & (1U << idx)))
1829 continue;
1830
1831 /* only add commas if the list contains multiple entries */
1832 if (list_size++)
1833 seq_puts(m, ",");
1834
1835 seq_puts(m, hash_algo_name[idx]);
1836 }
1837}
1838
Petko Manolov80eae202015-12-02 17:47:56 +02001839int ima_policy_show(struct seq_file *m, void *v)
1840{
1841 struct ima_rule_entry *entry = v;
Andy Shevchenkob8b57272016-05-20 17:00:57 -07001842 int i;
Petko Manolov80eae202015-12-02 17:47:56 +02001843 char tbuf[64] = {0,};
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02001844 int offset = 0;
Petko Manolov80eae202015-12-02 17:47:56 +02001845
1846 rcu_read_lock();
1847
1848 if (entry->action & MEASURE)
1849 seq_puts(m, pt(Opt_measure));
1850 if (entry->action & DONT_MEASURE)
1851 seq_puts(m, pt(Opt_dont_measure));
1852 if (entry->action & APPRAISE)
1853 seq_puts(m, pt(Opt_appraise));
1854 if (entry->action & DONT_APPRAISE)
1855 seq_puts(m, pt(Opt_dont_appraise));
1856 if (entry->action & AUDIT)
1857 seq_puts(m, pt(Opt_audit));
Mimi Zoharda1b0022016-09-29 10:04:52 -04001858 if (entry->action & HASH)
1859 seq_puts(m, pt(Opt_hash));
1860 if (entry->action & DONT_HASH)
1861 seq_puts(m, pt(Opt_dont_hash));
Petko Manolov80eae202015-12-02 17:47:56 +02001862
1863 seq_puts(m, " ");
1864
Mimi Zoharb5269ab2016-01-28 13:10:36 -05001865 if (entry->flags & IMA_FUNC)
1866 policy_func_show(m, entry->func);
Petko Manolov80eae202015-12-02 17:47:56 +02001867
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02001868 if ((entry->flags & IMA_MASK) || (entry->flags & IMA_INMASK)) {
1869 if (entry->flags & IMA_MASK)
1870 offset = 1;
Petko Manolov80eae202015-12-02 17:47:56 +02001871 if (entry->mask & MAY_EXEC)
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02001872 seq_printf(m, pt(Opt_mask), mt(mask_exec) + offset);
Petko Manolov80eae202015-12-02 17:47:56 +02001873 if (entry->mask & MAY_WRITE)
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02001874 seq_printf(m, pt(Opt_mask), mt(mask_write) + offset);
Petko Manolov80eae202015-12-02 17:47:56 +02001875 if (entry->mask & MAY_READ)
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02001876 seq_printf(m, pt(Opt_mask), mt(mask_read) + offset);
Petko Manolov80eae202015-12-02 17:47:56 +02001877 if (entry->mask & MAY_APPEND)
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02001878 seq_printf(m, pt(Opt_mask), mt(mask_append) + offset);
Petko Manolov80eae202015-12-02 17:47:56 +02001879 seq_puts(m, " ");
1880 }
1881
1882 if (entry->flags & IMA_FSMAGIC) {
1883 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
1884 seq_printf(m, pt(Opt_fsmagic), tbuf);
1885 seq_puts(m, " ");
1886 }
1887
Mimi Zoharf1b08bb2018-01-15 11:20:36 -05001888 if (entry->flags & IMA_FSNAME) {
1889 snprintf(tbuf, sizeof(tbuf), "%s", entry->fsname);
1890 seq_printf(m, pt(Opt_fsname), tbuf);
1891 seq_puts(m, " ");
1892 }
1893
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -08001894 if (entry->flags & IMA_KEYRINGS) {
Tyler Hicks176377d2020-08-11 14:26:20 -05001895 seq_puts(m, "keyrings=");
1896 ima_show_rule_opt_list(m, entry->keyrings);
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -08001897 seq_puts(m, " ");
1898 }
1899
Tushar Sugandhi47d76a42021-01-07 20:07:05 -08001900 if (entry->flags & IMA_LABEL) {
1901 seq_puts(m, "label=");
1902 ima_show_rule_opt_list(m, entry->label);
1903 seq_puts(m, " ");
1904 }
1905
Eric Richter02606432016-06-01 13:14:01 -05001906 if (entry->flags & IMA_PCR) {
1907 snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
1908 seq_printf(m, pt(Opt_pcr), tbuf);
1909 seq_puts(m, " ");
1910 }
1911
Petko Manolov80eae202015-12-02 17:47:56 +02001912 if (entry->flags & IMA_FSUUID) {
Christoph Hellwig787d8c52017-06-01 07:00:26 +02001913 seq_printf(m, "fsuuid=%pU", &entry->fsuuid);
Petko Manolov80eae202015-12-02 17:47:56 +02001914 seq_puts(m, " ");
1915 }
1916
1917 if (entry->flags & IMA_UID) {
1918 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001919 if (entry->uid_op == &uid_gt)
1920 seq_printf(m, pt(Opt_uid_gt), tbuf);
1921 else if (entry->uid_op == &uid_lt)
1922 seq_printf(m, pt(Opt_uid_lt), tbuf);
1923 else
1924 seq_printf(m, pt(Opt_uid_eq), tbuf);
Petko Manolov80eae202015-12-02 17:47:56 +02001925 seq_puts(m, " ");
1926 }
1927
1928 if (entry->flags & IMA_EUID) {
1929 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001930 if (entry->uid_op == &uid_gt)
1931 seq_printf(m, pt(Opt_euid_gt), tbuf);
1932 else if (entry->uid_op == &uid_lt)
1933 seq_printf(m, pt(Opt_euid_lt), tbuf);
1934 else
1935 seq_printf(m, pt(Opt_euid_eq), tbuf);
Petko Manolov80eae202015-12-02 17:47:56 +02001936 seq_puts(m, " ");
1937 }
1938
1939 if (entry->flags & IMA_FOWNER) {
1940 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001941 if (entry->fowner_op == &uid_gt)
1942 seq_printf(m, pt(Opt_fowner_gt), tbuf);
1943 else if (entry->fowner_op == &uid_lt)
1944 seq_printf(m, pt(Opt_fowner_lt), tbuf);
1945 else
1946 seq_printf(m, pt(Opt_fowner_eq), tbuf);
Petko Manolov80eae202015-12-02 17:47:56 +02001947 seq_puts(m, " ");
1948 }
1949
THOBY Simon583a80a2021-08-16 08:11:00 +00001950 if (entry->flags & IMA_VALIDATE_ALGOS) {
1951 seq_puts(m, "appraise_algos=");
1952 ima_policy_show_appraise_algos(m, entry->allowed_algos);
1953 seq_puts(m, " ");
1954 }
1955
Petko Manolov80eae202015-12-02 17:47:56 +02001956 for (i = 0; i < MAX_LSM_RULES; i++) {
1957 if (entry->lsm[i].rule) {
1958 switch (i) {
1959 case LSM_OBJ_USER:
1960 seq_printf(m, pt(Opt_obj_user),
Tyler Hicksaa0c0222020-07-09 01:19:08 -05001961 entry->lsm[i].args_p);
Petko Manolov80eae202015-12-02 17:47:56 +02001962 break;
1963 case LSM_OBJ_ROLE:
1964 seq_printf(m, pt(Opt_obj_role),
Tyler Hicksaa0c0222020-07-09 01:19:08 -05001965 entry->lsm[i].args_p);
Petko Manolov80eae202015-12-02 17:47:56 +02001966 break;
1967 case LSM_OBJ_TYPE:
1968 seq_printf(m, pt(Opt_obj_type),
Tyler Hicksaa0c0222020-07-09 01:19:08 -05001969 entry->lsm[i].args_p);
Petko Manolov80eae202015-12-02 17:47:56 +02001970 break;
1971 case LSM_SUBJ_USER:
1972 seq_printf(m, pt(Opt_subj_user),
Tyler Hicksaa0c0222020-07-09 01:19:08 -05001973 entry->lsm[i].args_p);
Petko Manolov80eae202015-12-02 17:47:56 +02001974 break;
1975 case LSM_SUBJ_ROLE:
1976 seq_printf(m, pt(Opt_subj_role),
Tyler Hicksaa0c0222020-07-09 01:19:08 -05001977 entry->lsm[i].args_p);
Petko Manolov80eae202015-12-02 17:47:56 +02001978 break;
1979 case LSM_SUBJ_TYPE:
1980 seq_printf(m, pt(Opt_subj_type),
Tyler Hicksaa0c0222020-07-09 01:19:08 -05001981 entry->lsm[i].args_p);
Petko Manolov80eae202015-12-02 17:47:56 +02001982 break;
1983 }
Clay Chang5350ceb2020-01-05 09:18:13 +08001984 seq_puts(m, " ");
Petko Manolov80eae202015-12-02 17:47:56 +02001985 }
1986 }
Matthew Garrett19453ce02019-06-19 15:46:11 -07001987 if (entry->template)
1988 seq_printf(m, "template=%s ", entry->template->name);
Thiago Jung Bauermann9044d622019-06-27 23:19:28 -03001989 if (entry->flags & IMA_DIGSIG_REQUIRED) {
1990 if (entry->flags & IMA_MODSIG_ALLOWED)
1991 seq_puts(m, "appraise_type=imasig|modsig ");
1992 else
1993 seq_puts(m, "appraise_type=imasig ");
1994 }
Nayna Jain273df862019-10-30 23:31:32 -04001995 if (entry->flags & IMA_CHECK_BLACKLIST)
1996 seq_puts(m, "appraise_flag=check_blacklist ");
Petko Manolov80eae202015-12-02 17:47:56 +02001997 if (entry->flags & IMA_PERMIT_DIRECTIO)
1998 seq_puts(m, "permit_directio ");
1999 rcu_read_unlock();
2000 seq_puts(m, "\n");
2001 return 0;
2002}
2003#endif /* CONFIG_IMA_READ_POLICY */
Matthew Garrett29d3c1c2019-08-19 17:18:01 -07002004
2005#if defined(CONFIG_IMA_APPRAISE) && defined(CONFIG_INTEGRITY_TRUSTED_KEYRING)
2006/*
2007 * ima_appraise_signature: whether IMA will appraise a given function using
2008 * an IMA digital signature. This is restricted to cases where the kernel
2009 * has a set of built-in trusted keys in order to avoid an attacker simply
2010 * loading additional keys.
2011 */
2012bool ima_appraise_signature(enum kernel_read_file_id id)
2013{
2014 struct ima_rule_entry *entry;
2015 bool found = false;
2016 enum ima_hooks func;
2017
2018 if (id >= READING_MAX_ID)
2019 return false;
2020
2021 func = read_idmap[id] ?: FILE_CHECK;
2022
2023 rcu_read_lock();
2024 list_for_each_entry_rcu(entry, ima_rules, list) {
2025 if (entry->action != APPRAISE)
2026 continue;
2027
2028 /*
2029 * A generic entry will match, but otherwise require that it
2030 * match the func we're looking for
2031 */
2032 if (entry->func && entry->func != func)
2033 continue;
2034
2035 /*
2036 * We require this to be a digital signature, not a raw IMA
2037 * hash.
2038 */
2039 if (entry->flags & IMA_DIGSIG_REQUIRED)
2040 found = true;
2041
2042 /*
2043 * We've found a rule that matches, so break now even if it
2044 * didn't require a digital signature - a later rule that does
2045 * won't override it, so would be a false positive.
2046 */
2047 break;
2048 }
2049
2050 rcu_read_unlock();
2051 return found;
2052}
2053#endif /* CONFIG_IMA_APPRAISE && CONFIG_INTEGRITY_TRUSTED_KEYRING */