blob: 4badc4fcda9850b3695e758e1489ec6121c6d993 [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
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Paul Gortmaker876979c2018-12-09 15:36:29 -050012#include <linux/init.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050013#include <linux/list.h>
Mimi Zoharcf222212016-01-14 17:57:47 -050014#include <linux/fs.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050015#include <linux/security.h>
16#include <linux/magic.h>
Mimi Zohar4af46622009-02-04 09:07:00 -050017#include <linux/parser.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Petko Manolov38d859f2015-12-02 17:47:54 +020019#include <linux/rculist.h>
Dmitry Kasatkin85865c12012-09-03 23:23:13 +030020#include <linux/genhd.h>
Petko Manolov80eae202015-12-02 17:47:56 +020021#include <linux/seq_file.h>
Nayna Jain61917062018-10-09 23:00:36 +053022#include <linux/ima.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050023
24#include "ima.h"
25
26/* flags definitions */
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020027#define IMA_FUNC 0x0001
28#define IMA_MASK 0x0002
Mimi Zohar3323eec2009-02-04 09:06:58 -050029#define IMA_FSMAGIC 0x0004
30#define IMA_UID 0x0008
Mimi Zohar07f6a792011-03-09 22:25:48 -050031#define IMA_FOWNER 0x0010
Dmitry Kasatkin85865c12012-09-03 23:23:13 +030032#define IMA_FSUUID 0x0020
Mimi Zohar4351c292014-11-05 07:53:55 -050033#define IMA_INMASK 0x0040
Mimi Zohar139069e2014-11-05 07:48:36 -050034#define IMA_EUID 0x0080
Eric Richter02606432016-06-01 13:14:01 -050035#define IMA_PCR 0x0100
Mimi Zoharf1b08bb2018-01-15 11:20:36 -050036#define IMA_FSNAME 0x0200
Mimi Zohar3323eec2009-02-04 09:06:58 -050037
Dmitry Kasatkin45e24722012-09-12 20:51:32 +030038#define UNKNOWN 0
39#define MEASURE 0x0001 /* same as IMA_MEASURE */
40#define DONT_MEASURE 0x0002
41#define APPRAISE 0x0004 /* same as IMA_APPRAISE */
42#define DONT_APPRAISE 0x0008
Peter Moodye7c568e2012-06-14 10:04:36 -070043#define AUDIT 0x0040
Mimi Zoharda1b0022016-09-29 10:04:52 -040044#define HASH 0x0100
45#define DONT_HASH 0x0200
Mimi Zohar4af46622009-02-04 09:07:00 -050046
Eric Richter02606432016-06-01 13:14:01 -050047#define INVALID_PCR(a) (((a) < 0) || \
48 (a) >= (FIELD_SIZEOF(struct integrity_iint_cache, measured_pcrs) * 8))
49
Roberto Sassua7560242014-09-12 19:35:54 +020050int ima_policy_flag;
Mimi Zohar6ad6afa2015-12-07 14:35:47 -050051static int temp_ima_appraise;
Mimi Zoharef968372018-07-13 14:06:01 -040052static int build_ima_appraise __ro_after_init;
Roberto Sassua7560242014-09-12 19:35:54 +020053
Mimi Zohar4af46622009-02-04 09:07:00 -050054#define MAX_LSM_RULES 6
55enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
56 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
57};
Mimi Zohar3323eec2009-02-04 09:06:58 -050058
Mimi Zohar24fd03c2015-06-11 20:48:33 -040059enum policy_types { ORIGINAL_TCB = 1, DEFAULT_TCB };
60
Nayna Jainc52657d2018-10-09 23:00:35 +053061enum policy_rule_list { IMA_DEFAULT_POLICY = 1, IMA_CUSTOM_POLICY };
62
Mimi Zohar07f6a792011-03-09 22:25:48 -050063struct ima_rule_entry {
Mimi Zohar3323eec2009-02-04 09:06:58 -050064 struct list_head list;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050065 int action;
Mimi Zohar3323eec2009-02-04 09:06:58 -050066 unsigned int flags;
67 enum ima_hooks func;
68 int mask;
69 unsigned long fsmagic;
Christoph Hellwig787d8c52017-06-01 07:00:26 +020070 uuid_t fsuuid;
Eric W. Biederman8b94eea2012-05-25 18:24:12 -060071 kuid_t uid;
Linus Torvalds882653222012-10-02 21:38:48 -070072 kuid_t fowner;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +030073 bool (*uid_op)(kuid_t, kuid_t); /* Handlers for operators */
74 bool (*fowner_op)(kuid_t, kuid_t); /* uid_eq(), uid_gt(), uid_lt() */
Eric Richter02606432016-06-01 13:14:01 -050075 int pcr;
Mimi Zohar4af46622009-02-04 09:07:00 -050076 struct {
77 void *rule; /* LSM file metadata specific */
Mimi Zohar7163a992013-01-03 14:19:09 -050078 void *args_p; /* audit value */
Mimi Zohar4af46622009-02-04 09:07:00 -050079 int type; /* audit type */
80 } lsm[MAX_LSM_RULES];
Mimi Zoharf1b08bb2018-01-15 11:20:36 -050081 char *fsname;
Matthew Garrett19453ce02019-06-19 15:46:11 -070082 struct ima_template_desc *template;
Mimi Zohar3323eec2009-02-04 09:06:58 -050083};
84
Eric Paris5789ba32009-05-21 15:47:06 -040085/*
86 * Without LSM specific knowledge, the default policy can only be
Mimi Zohar07f6a792011-03-09 22:25:48 -050087 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
Mimi Zohar4af46622009-02-04 09:07:00 -050088 */
Eric Paris5789ba32009-05-21 15:47:06 -040089
90/*
91 * The minimum rule set to allow for full TCB coverage. Measures all files
92 * opened or mmap for exec and everything read by root. Dangerous because
93 * normal users can easily run the machine out of memory simply building
94 * and running executables.
95 */
James Morrisbad44172017-02-13 16:34:35 +110096static struct ima_rule_entry dont_measure_rules[] __ro_after_init = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020097 {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
98 {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
99 {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
100 {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
101 {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
102 {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
103 {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
104 {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
Martin Townsend1c070b12018-03-22 13:46:01 +0000105 {.action = DONT_MEASURE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
Roberto Sassu6438de92015-04-11 17:13:06 +0200106 {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
107 .flags = IMA_FSMAGIC},
Laura Abbott82e3bb42017-05-09 11:25:27 -0700108 {.action = DONT_MEASURE, .fsmagic = CGROUP2_SUPER_MAGIC,
109 .flags = IMA_FSMAGIC},
Mimi Zohar060190f2018-11-14 17:24:13 -0500110 {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
111 {.action = DONT_MEASURE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC}
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400112};
113
James Morrisbad44172017-02-13 16:34:35 +1100114static struct ima_rule_entry original_measurement_rules[] __ro_after_init = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200115 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500116 .flags = IMA_FUNC | IMA_MASK},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200117 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500118 .flags = IMA_FUNC | IMA_MASK},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400119 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300120 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
121 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400122 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
123 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
124};
125
James Morrisbad44172017-02-13 16:34:35 +1100126static struct ima_rule_entry default_measurement_rules[] __ro_after_init = {
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400127 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
128 .flags = IMA_FUNC | IMA_MASK},
129 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
130 .flags = IMA_FUNC | IMA_MASK},
131 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300132 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
133 .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400134 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300135 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
136 .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200137 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400138 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
Mimi Zohar19f8a842016-01-15 10:17:12 -0500139 {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC},
Mimi Zohar3323eec2009-02-04 09:06:58 -0500140};
141
James Morrisbad44172017-02-13 16:34:35 +1100142static struct ima_rule_entry default_appraise_rules[] __ro_after_init = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200143 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
144 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
145 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
146 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
147 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
148 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
149 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
150 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
151 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
Martin Townsend1c070b12018-03-22 13:46:01 +0000152 {.action = DONT_APPRAISE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
Mimi Zoharcd025f72015-04-21 16:54:24 -0400153 {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
Mimi Zohar060190f2018-11-14 17:24:13 -0500154 {.action = DONT_APPRAISE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200155 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
Laura Abbott82e3bb42017-05-09 11:25:27 -0700156 {.action = DONT_APPRAISE, .fsmagic = CGROUP2_SUPER_MAGIC, .flags = IMA_FSMAGIC},
Mimi Zohar95ee08f2015-12-07 15:08:01 -0500157#ifdef CONFIG_IMA_WRITE_POLICY
158 {.action = APPRAISE, .func = POLICY_CHECK,
159 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
160#endif
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200161#ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300162 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
163 .flags = IMA_FOWNER},
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200164#else
165 /* force signature */
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300166 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200167 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
168#endif
Mimi Zohar07f6a792011-03-09 22:25:48 -0500169};
Mimi Zohar3323eec2009-02-04 09:06:58 -0500170
Mimi Zoharef968372018-07-13 14:06:01 -0400171static struct ima_rule_entry build_appraise_rules[] __ro_after_init = {
172#ifdef CONFIG_IMA_APPRAISE_REQUIRE_MODULE_SIGS
173 {.action = APPRAISE, .func = MODULE_CHECK,
174 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
175#endif
176#ifdef CONFIG_IMA_APPRAISE_REQUIRE_FIRMWARE_SIGS
177 {.action = APPRAISE, .func = FIRMWARE_CHECK,
178 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
179#endif
180#ifdef CONFIG_IMA_APPRAISE_REQUIRE_KEXEC_SIGS
181 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
182 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
183#endif
184#ifdef CONFIG_IMA_APPRAISE_REQUIRE_POLICY_SIGS
185 {.action = APPRAISE, .func = POLICY_CHECK,
186 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
187#endif
188};
189
Mimi Zohar503ceae2017-04-21 18:58:27 -0400190static struct ima_rule_entry secure_boot_rules[] __ro_after_init = {
191 {.action = APPRAISE, .func = MODULE_CHECK,
192 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
193 {.action = APPRAISE, .func = FIRMWARE_CHECK,
194 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
195 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
196 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
197 {.action = APPRAISE, .func = POLICY_CHECK,
198 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
199};
200
Nayna Jain61917062018-10-09 23:00:36 +0530201/* An array of architecture specific rules */
YueHaibing68f25292019-06-11 21:40:32 +0800202static struct ima_rule_entry *arch_policy_entry __ro_after_init;
Nayna Jain61917062018-10-09 23:00:36 +0530203
Mimi Zohar07f6a792011-03-09 22:25:48 -0500204static LIST_HEAD(ima_default_rules);
205static LIST_HEAD(ima_policy_rules);
Petko Manolov38d859f2015-12-02 17:47:54 +0200206static LIST_HEAD(ima_temp_rules);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500207static struct list_head *ima_rules;
208
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400209static int ima_policy __initdata;
Petko Manolov38d859f2015-12-02 17:47:54 +0200210
Mimi Zohar07f6a792011-03-09 22:25:48 -0500211static int __init default_measure_policy_setup(char *str)
Eric Paris5789ba32009-05-21 15:47:06 -0400212{
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400213 if (ima_policy)
214 return 1;
215
216 ima_policy = ORIGINAL_TCB;
Eric Paris5789ba32009-05-21 15:47:06 -0400217 return 1;
218}
Mimi Zohar07f6a792011-03-09 22:25:48 -0500219__setup("ima_tcb", default_measure_policy_setup);
220
Mimi Zohar33ce9542017-04-24 12:04:09 -0400221static bool ima_use_appraise_tcb __initdata;
Mimi Zohar503ceae2017-04-21 18:58:27 -0400222static bool ima_use_secure_boot __initdata;
Mimi Zohar9e670282018-02-21 11:36:32 -0500223static bool ima_fail_unverifiable_sigs __ro_after_init;
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400224static int __init policy_setup(char *str)
225{
Mimi Zohar33ce9542017-04-24 12:04:09 -0400226 char *p;
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400227
Mimi Zohar33ce9542017-04-24 12:04:09 -0400228 while ((p = strsep(&str, " |\n")) != NULL) {
229 if (*p == ' ')
230 continue;
231 if ((strcmp(p, "tcb") == 0) && !ima_policy)
232 ima_policy = DEFAULT_TCB;
233 else if (strcmp(p, "appraise_tcb") == 0)
Thomas Meyer39adb922017-10-07 16:02:21 +0200234 ima_use_appraise_tcb = true;
Mimi Zohar503ceae2017-04-21 18:58:27 -0400235 else if (strcmp(p, "secure_boot") == 0)
Thomas Meyer39adb922017-10-07 16:02:21 +0200236 ima_use_secure_boot = true;
Mimi Zohar9e670282018-02-21 11:36:32 -0500237 else if (strcmp(p, "fail_securely") == 0)
238 ima_fail_unverifiable_sigs = true;
Mimi Zohar33ce9542017-04-24 12:04:09 -0400239 }
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400240
241 return 1;
242}
243__setup("ima_policy=", policy_setup);
244
Mimi Zohar07f6a792011-03-09 22:25:48 -0500245static int __init default_appraise_policy_setup(char *str)
246{
Thomas Meyer39adb922017-10-07 16:02:21 +0200247 ima_use_appraise_tcb = true;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500248 return 1;
249}
250__setup("ima_appraise_tcb", default_appraise_policy_setup);
Eric Paris5789ba32009-05-21 15:47:06 -0400251
Janne Karhunenb1694242019-06-14 15:20:15 +0300252static void ima_lsm_free_rule(struct ima_rule_entry *entry)
253{
254 int i;
255
256 for (i = 0; i < MAX_LSM_RULES; i++) {
257 kfree(entry->lsm[i].rule);
258 kfree(entry->lsm[i].args_p);
259 }
260 kfree(entry);
261}
262
263static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry)
264{
265 struct ima_rule_entry *nentry;
266 int i, result;
267
268 nentry = kmalloc(sizeof(*nentry), GFP_KERNEL);
269 if (!nentry)
270 return NULL;
271
272 /*
273 * Immutable elements are copied over as pointers and data; only
274 * lsm rules can change
275 */
276 memcpy(nentry, entry, sizeof(*nentry));
277 memset(nentry->lsm, 0, FIELD_SIZEOF(struct ima_rule_entry, lsm));
278
279 for (i = 0; i < MAX_LSM_RULES; i++) {
280 if (!entry->lsm[i].rule)
281 continue;
282
283 nentry->lsm[i].type = entry->lsm[i].type;
284 nentry->lsm[i].args_p = kstrdup(entry->lsm[i].args_p,
285 GFP_KERNEL);
286 if (!nentry->lsm[i].args_p)
287 goto out_err;
288
289 result = security_filter_rule_init(nentry->lsm[i].type,
290 Audit_equal,
291 nentry->lsm[i].args_p,
292 &nentry->lsm[i].rule);
293 if (result == -EINVAL)
294 pr_warn("ima: rule for LSM \'%d\' is undefined\n",
295 entry->lsm[i].type);
296 }
297 return nentry;
298
299out_err:
300 ima_lsm_free_rule(nentry);
301 return NULL;
302}
303
304static int ima_lsm_update_rule(struct ima_rule_entry *entry)
305{
306 struct ima_rule_entry *nentry;
307
308 nentry = ima_lsm_copy_rule(entry);
309 if (!nentry)
310 return -ENOMEM;
311
312 list_replace_rcu(&entry->list, &nentry->list);
313 synchronize_rcu();
314 ima_lsm_free_rule(entry);
315
316 return 0;
317}
318
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200319/*
Petko Manolov38d859f2015-12-02 17:47:54 +0200320 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
321 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
Janne Karhunenb1694242019-06-14 15:20:15 +0300322 * the reloaded LSM policy.
Mimi Zohar7163a992013-01-03 14:19:09 -0500323 */
324static void ima_lsm_update_rules(void)
325{
Janne Karhunenb1694242019-06-14 15:20:15 +0300326 struct ima_rule_entry *entry, *e;
327 int i, result, needs_update;
Mimi Zohar7163a992013-01-03 14:19:09 -0500328
Janne Karhunenb1694242019-06-14 15:20:15 +0300329 list_for_each_entry_safe(entry, e, &ima_policy_rules, list) {
330 needs_update = 0;
Mimi Zohar7163a992013-01-03 14:19:09 -0500331 for (i = 0; i < MAX_LSM_RULES; i++) {
Janne Karhunenb1694242019-06-14 15:20:15 +0300332 if (entry->lsm[i].rule) {
333 needs_update = 1;
334 break;
335 }
336 }
337 if (!needs_update)
338 continue;
339
340 result = ima_lsm_update_rule(entry);
341 if (result) {
342 pr_err("ima: lsm rule update error %d\n",
343 result);
344 return;
Mimi Zohar7163a992013-01-03 14:19:09 -0500345 }
346 }
Mimi Zohar7163a992013-01-03 14:19:09 -0500347}
348
Janne Karhunenb1694242019-06-14 15:20:15 +0300349int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
350 void *lsm_data)
351{
352 if (event != LSM_POLICY_CHANGE)
353 return NOTIFY_DONE;
354
355 ima_lsm_update_rules();
356 return NOTIFY_OK;
357}
358
Mimi Zohar3323eec2009-02-04 09:06:58 -0500359/**
360 * ima_match_rules - determine whether an inode matches the measure rule.
361 * @rule: a pointer to a rule
362 * @inode: a pointer to an inode
Matthew Garrettd906c102018-01-08 13:36:20 -0800363 * @cred: a pointer to a credentials structure for user validation
364 * @secid: the secid of the task to be validated
Mimi Zohar3323eec2009-02-04 09:06:58 -0500365 * @func: LIM hook identifier
366 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
367 *
368 * Returns true on rule match, false on failure.
369 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500370static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,
Matthew Garrettd906c102018-01-08 13:36:20 -0800371 const struct cred *cred, u32 secid,
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500372 enum ima_hooks func, int mask)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500373{
Mimi Zohar4af46622009-02-04 09:07:00 -0500374 int i;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500375
Prakhar Srivastavab0935122019-06-23 23:23:29 -0700376 if (func == KEXEC_CMDLINE) {
377 if ((rule->flags & IMA_FUNC) && (rule->func == func))
378 return true;
379 return false;
380 }
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200381 if ((rule->flags & IMA_FUNC) &&
382 (rule->func != func && func != POST_SETATTR))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500383 return false;
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200384 if ((rule->flags & IMA_MASK) &&
385 (rule->mask != mask && func != POST_SETATTR))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500386 return false;
Mimi Zohar4351c292014-11-05 07:53:55 -0500387 if ((rule->flags & IMA_INMASK) &&
388 (!(rule->mask & mask) && func != POST_SETATTR))
389 return false;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500390 if ((rule->flags & IMA_FSMAGIC)
391 && rule->fsmagic != inode->i_sb->s_magic)
392 return false;
Mimi Zoharf1b08bb2018-01-15 11:20:36 -0500393 if ((rule->flags & IMA_FSNAME)
394 && strcmp(rule->fsname, inode->i_sb->s_type->name))
395 return false;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300396 if ((rule->flags & IMA_FSUUID) &&
Christoph Hellwig85787092017-05-10 15:06:33 +0200397 !uuid_equal(&rule->fsuuid, &inode->i_sb->s_uuid))
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300398 return false;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300399 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500400 return false;
Mimi Zohar139069e2014-11-05 07:48:36 -0500401 if (rule->flags & IMA_EUID) {
402 if (has_capability_noaudit(current, CAP_SETUID)) {
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300403 if (!rule->uid_op(cred->euid, rule->uid)
404 && !rule->uid_op(cred->suid, rule->uid)
405 && !rule->uid_op(cred->uid, rule->uid))
Mimi Zohar139069e2014-11-05 07:48:36 -0500406 return false;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300407 } else if (!rule->uid_op(cred->euid, rule->uid))
Mimi Zohar139069e2014-11-05 07:48:36 -0500408 return false;
409 }
410
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300411 if ((rule->flags & IMA_FOWNER) &&
412 !rule->fowner_op(inode->i_uid, rule->fowner))
Mimi Zohar07f6a792011-03-09 22:25:48 -0500413 return false;
Mimi Zohar4af46622009-02-04 09:07:00 -0500414 for (i = 0; i < MAX_LSM_RULES; i++) {
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400415 int rc = 0;
Matthew Garrettd906c102018-01-08 13:36:20 -0800416 u32 osid;
Mimi Zohar4af46622009-02-04 09:07:00 -0500417
418 if (!rule->lsm[i].rule)
419 continue;
Janne Karhunenb1694242019-06-14 15:20:15 +0300420
Mimi Zohar4af46622009-02-04 09:07:00 -0500421 switch (i) {
422 case LSM_OBJ_USER:
423 case LSM_OBJ_ROLE:
424 case LSM_OBJ_TYPE:
425 security_inode_getsecid(inode, &osid);
426 rc = security_filter_rule_match(osid,
427 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400428 Audit_equal,
Richard Guy Briggs90462a52019-01-31 11:52:11 -0500429 rule->lsm[i].rule);
Mimi Zohar4af46622009-02-04 09:07:00 -0500430 break;
431 case LSM_SUBJ_USER:
432 case LSM_SUBJ_ROLE:
433 case LSM_SUBJ_TYPE:
Matthew Garrettd906c102018-01-08 13:36:20 -0800434 rc = security_filter_rule_match(secid,
Mimi Zohar4af46622009-02-04 09:07:00 -0500435 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400436 Audit_equal,
Richard Guy Briggs90462a52019-01-31 11:52:11 -0500437 rule->lsm[i].rule);
Mimi Zohar4af46622009-02-04 09:07:00 -0500438 default:
439 break;
440 }
441 if (!rc)
442 return false;
443 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500444 return true;
445}
446
Mimi Zohard79d72e2012-12-03 17:08:11 -0500447/*
448 * In addition to knowing that we need to appraise the file in general,
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500449 * we need to differentiate between calling hooks, for hook specific rules.
Mimi Zohard79d72e2012-12-03 17:08:11 -0500450 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500451static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
Mimi Zohard79d72e2012-12-03 17:08:11 -0500452{
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500453 if (!(rule->flags & IMA_FUNC))
454 return IMA_FILE_APPRAISE;
455
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200456 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500457 case MMAP_CHECK:
458 return IMA_MMAP_APPRAISE;
459 case BPRM_CHECK:
460 return IMA_BPRM_APPRAISE;
Matthew Garrettd906c102018-01-08 13:36:20 -0800461 case CREDS_CHECK:
462 return IMA_CREDS_APPRAISE;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500463 case FILE_CHECK:
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500464 case POST_SETATTR:
Mimi Zohard79d72e2012-12-03 17:08:11 -0500465 return IMA_FILE_APPRAISE;
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500466 case MODULE_CHECK ... MAX_CHECK - 1:
467 default:
468 return IMA_READ_APPRAISE;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500469 }
470}
471
Mimi Zohar3323eec2009-02-04 09:06:58 -0500472/**
473 * ima_match_policy - decision based on LSM and other conditions
474 * @inode: pointer to an inode for which the policy decision is being made
Matthew Garrettd906c102018-01-08 13:36:20 -0800475 * @cred: pointer to a credentials structure for which the policy decision is
476 * being made
477 * @secid: LSM secid of the task to be validated
Mimi Zohar3323eec2009-02-04 09:06:58 -0500478 * @func: IMA hook identifier
479 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
Eric Richter725de7f2016-06-01 13:14:02 -0500480 * @pcr: set the pcr to extend
Matthew Garrett19453ce02019-06-19 15:46:11 -0700481 * @template_desc: the template that should be used for this rule
Mimi Zohar3323eec2009-02-04 09:06:58 -0500482 *
483 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
484 * conditions.
485 *
Petko Manolov38d859f2015-12-02 17:47:54 +0200486 * Since the IMA policy may be updated multiple times we need to lock the
487 * list when walking it. Reads are many orders of magnitude more numerous
488 * than writes so ima_match_policy() is classical RCU candidate.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500489 */
Matthew Garrettd906c102018-01-08 13:36:20 -0800490int ima_match_policy(struct inode *inode, const struct cred *cred, u32 secid,
Matthew Garrett19453ce02019-06-19 15:46:11 -0700491 enum ima_hooks func, int mask, int flags, int *pcr,
492 struct ima_template_desc **template_desc)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500493{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500494 struct ima_rule_entry *entry;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500495 int action = 0, actmask = flags | (flags << 1);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500496
Mimi Zoharb36f2812019-07-19 07:16:57 -0400497 if (template_desc)
498 *template_desc = ima_template_desc_current();
499
Petko Manolov38d859f2015-12-02 17:47:54 +0200500 rcu_read_lock();
501 list_for_each_entry_rcu(entry, ima_rules, list) {
Mimi Zohar3323eec2009-02-04 09:06:58 -0500502
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500503 if (!(entry->action & actmask))
504 continue;
505
Matthew Garrettd906c102018-01-08 13:36:20 -0800506 if (!ima_match_rules(entry, inode, cred, secid, func, mask))
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500507 continue;
508
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300509 action |= entry->flags & IMA_ACTION_FLAGS;
510
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300511 action |= entry->action & IMA_DO_MASK;
Mimi Zoharda1b0022016-09-29 10:04:52 -0400512 if (entry->action & IMA_APPRAISE) {
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500513 action |= get_subaction(entry, func);
Mimi Zohara9a49352018-03-10 23:07:34 -0500514 action &= ~IMA_HASH;
Mimi Zohar9e670282018-02-21 11:36:32 -0500515 if (ima_fail_unverifiable_sigs)
516 action |= IMA_FAIL_UNVERIFIABLE_SIGS;
Mimi Zoharda1b0022016-09-29 10:04:52 -0400517 }
Mimi Zohard79d72e2012-12-03 17:08:11 -0500518
Mimi Zoharb36f2812019-07-19 07:16:57 -0400519
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300520 if (entry->action & IMA_DO_MASK)
521 actmask &= ~(entry->action | entry->action << 1);
522 else
523 actmask &= ~(entry->action | entry->action >> 1);
524
Eric Richter725de7f2016-06-01 13:14:02 -0500525 if ((pcr) && (entry->flags & IMA_PCR))
526 *pcr = entry->pcr;
527
Matthew Garrett19453ce02019-06-19 15:46:11 -0700528 if (template_desc && entry->template)
529 *template_desc = entry->template;
Matthew Garrett19453ce02019-06-19 15:46:11 -0700530
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500531 if (!actmask)
532 break;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500533 }
Petko Manolov38d859f2015-12-02 17:47:54 +0200534 rcu_read_unlock();
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500535
536 return action;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500537}
538
Roberto Sassua7560242014-09-12 19:35:54 +0200539/*
540 * Initialize the ima_policy_flag variable based on the currently
541 * loaded policy. Based on this flag, the decision to short circuit
542 * out of a function or not call the function in the first place
543 * can be made earlier.
544 */
545void ima_update_policy_flag(void)
546{
547 struct ima_rule_entry *entry;
548
Roberto Sassua7560242014-09-12 19:35:54 +0200549 list_for_each_entry(entry, ima_rules, list) {
550 if (entry->action & IMA_DO_MASK)
551 ima_policy_flag |= entry->action;
552 }
553
Mimi Zoharef968372018-07-13 14:06:01 -0400554 ima_appraise |= (build_ima_appraise | temp_ima_appraise);
Roberto Sassua7560242014-09-12 19:35:54 +0200555 if (!ima_appraise)
556 ima_policy_flag &= ~IMA_APPRAISE;
557}
558
Mimi Zohar6f0911a2018-04-12 00:15:22 -0400559static int ima_appraise_flag(enum ima_hooks func)
560{
561 if (func == MODULE_CHECK)
562 return IMA_APPRAISE_MODULES;
563 else if (func == FIRMWARE_CHECK)
564 return IMA_APPRAISE_FIRMWARE;
565 else if (func == POLICY_CHECK)
566 return IMA_APPRAISE_POLICY;
Mimi Zohar16c267a2018-07-13 14:05:58 -0400567 else if (func == KEXEC_KERNEL_CHECK)
568 return IMA_APPRAISE_KEXEC;
Mimi Zohar6f0911a2018-04-12 00:15:22 -0400569 return 0;
570}
571
Nayna Jainc52657d2018-10-09 23:00:35 +0530572static void add_rules(struct ima_rule_entry *entries, int count,
573 enum policy_rule_list policy_rule)
574{
575 int i = 0;
576
577 for (i = 0; i < count; i++) {
578 struct ima_rule_entry *entry;
579
580 if (policy_rule & IMA_DEFAULT_POLICY)
581 list_add_tail(&entries[i].list, &ima_default_rules);
582
583 if (policy_rule & IMA_CUSTOM_POLICY) {
584 entry = kmemdup(&entries[i], sizeof(*entry),
585 GFP_KERNEL);
586 if (!entry)
587 continue;
588
589 list_add_tail(&entry->list, &ima_policy_rules);
590 }
Petr Vorelf4001942019-05-15 08:18:07 +0200591 if (entries[i].action == APPRAISE) {
Nayna Jainc52657d2018-10-09 23:00:35 +0530592 temp_ima_appraise |= ima_appraise_flag(entries[i].func);
Petr Vorelf4001942019-05-15 08:18:07 +0200593 if (entries[i].func == POLICY_CHECK)
594 temp_ima_appraise |= IMA_APPRAISE_POLICY;
595 }
Nayna Jainc52657d2018-10-09 23:00:35 +0530596 }
597}
598
Nayna Jain61917062018-10-09 23:00:36 +0530599static int ima_parse_rule(char *rule, struct ima_rule_entry *entry);
600
601static int __init ima_init_arch_policy(void)
602{
603 const char * const *arch_rules;
604 const char * const *rules;
605 int arch_entries = 0;
606 int i = 0;
607
608 arch_rules = arch_get_ima_policy();
609 if (!arch_rules)
610 return arch_entries;
611
612 /* Get number of rules */
613 for (rules = arch_rules; *rules != NULL; rules++)
614 arch_entries++;
615
616 arch_policy_entry = kcalloc(arch_entries + 1,
617 sizeof(*arch_policy_entry), GFP_KERNEL);
618 if (!arch_policy_entry)
619 return 0;
620
621 /* Convert each policy string rules to struct ima_rule_entry format */
622 for (rules = arch_rules, i = 0; *rules != NULL; rules++) {
623 char rule[255];
624 int result;
625
626 result = strlcpy(rule, *rules, sizeof(rule));
627
628 INIT_LIST_HEAD(&arch_policy_entry[i].list);
629 result = ima_parse_rule(rule, &arch_policy_entry[i]);
630 if (result) {
631 pr_warn("Skipping unknown architecture policy rule: %s\n",
632 rule);
633 memset(&arch_policy_entry[i], 0,
634 sizeof(*arch_policy_entry));
635 continue;
636 }
637 i++;
638 }
639 return i;
640}
641
Mimi Zohar3323eec2009-02-04 09:06:58 -0500642/**
643 * ima_init_policy - initialize the default measure rules.
644 *
Mimi Zohar07f6a792011-03-09 22:25:48 -0500645 * ima_rules points to either the ima_default_rules or the
646 * the new ima_policy_rules.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500647 */
Eric Paris932995f2009-05-21 15:43:32 -0400648void __init ima_init_policy(void)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500649{
Nayna Jain61917062018-10-09 23:00:36 +0530650 int build_appraise_entries, arch_entries;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500651
Nayna Jainc52657d2018-10-09 23:00:35 +0530652 /* if !ima_policy, we load NO default rules */
653 if (ima_policy)
654 add_rules(dont_measure_rules, ARRAY_SIZE(dont_measure_rules),
655 IMA_DEFAULT_POLICY);
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400656
657 switch (ima_policy) {
658 case ORIGINAL_TCB:
Nayna Jainc52657d2018-10-09 23:00:35 +0530659 add_rules(original_measurement_rules,
660 ARRAY_SIZE(original_measurement_rules),
661 IMA_DEFAULT_POLICY);
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400662 break;
663 case DEFAULT_TCB:
Nayna Jainc52657d2018-10-09 23:00:35 +0530664 add_rules(default_measurement_rules,
665 ARRAY_SIZE(default_measurement_rules),
666 IMA_DEFAULT_POLICY);
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400667 default:
668 break;
669 }
Eric Paris5789ba32009-05-21 15:47:06 -0400670
Mimi Zohar503ceae2017-04-21 18:58:27 -0400671 /*
Nayna Jain61917062018-10-09 23:00:36 +0530672 * Based on runtime secure boot flags, insert arch specific measurement
673 * and appraise rules requiring file signatures for both the initial
674 * and custom policies, prior to other appraise rules.
675 * (Highest priority)
Mimi Zohar503ceae2017-04-21 18:58:27 -0400676 */
Nayna Jain61917062018-10-09 23:00:36 +0530677 arch_entries = ima_init_arch_policy();
678 if (!arch_entries)
679 pr_info("No architecture policies found\n");
680 else
681 add_rules(arch_policy_entry, arch_entries,
682 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
683
684 /*
Mimi Zohar503ceae2017-04-21 18:58:27 -0400685 * Insert the builtin "secure_boot" policy rules requiring file
Nayna Jain61917062018-10-09 23:00:36 +0530686 * signatures, prior to other appraise rules.
Mimi Zohar503ceae2017-04-21 18:58:27 -0400687 */
Nayna Jainc52657d2018-10-09 23:00:35 +0530688 if (ima_use_secure_boot)
689 add_rules(secure_boot_rules, ARRAY_SIZE(secure_boot_rules),
690 IMA_DEFAULT_POLICY);
Mimi Zohar503ceae2017-04-21 18:58:27 -0400691
Mimi Zoharef968372018-07-13 14:06:01 -0400692 /*
693 * Insert the build time appraise rules requiring file signatures
694 * for both the initial and custom policies, prior to other appraise
Nayna Jainc52657d2018-10-09 23:00:35 +0530695 * rules. As the secure boot rules includes all of the build time
696 * rules, include either one or the other set of rules, but not both.
Mimi Zoharef968372018-07-13 14:06:01 -0400697 */
Nayna Jainc52657d2018-10-09 23:00:35 +0530698 build_appraise_entries = ARRAY_SIZE(build_appraise_rules);
699 if (build_appraise_entries) {
700 if (ima_use_secure_boot)
701 add_rules(build_appraise_rules, build_appraise_entries,
702 IMA_CUSTOM_POLICY);
703 else
704 add_rules(build_appraise_rules, build_appraise_entries,
705 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
Mimi Zoharef968372018-07-13 14:06:01 -0400706 }
707
Nayna Jainc52657d2018-10-09 23:00:35 +0530708 if (ima_use_appraise_tcb)
709 add_rules(default_appraise_rules,
710 ARRAY_SIZE(default_appraise_rules),
711 IMA_DEFAULT_POLICY);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500712
713 ima_rules = &ima_default_rules;
Mimi Zohar95ee08f2015-12-07 15:08:01 -0500714 ima_update_policy_flag();
Mimi Zohar3323eec2009-02-04 09:06:58 -0500715}
Mimi Zohar4af46622009-02-04 09:07:00 -0500716
Sasha Levin01127212015-12-22 08:51:23 -0500717/* Make sure we have a valid policy, at least containing some rules. */
Colin Ian Kingc75d8e92016-01-20 11:13:46 +0000718int ima_check_policy(void)
Sasha Levin01127212015-12-22 08:51:23 -0500719{
720 if (list_empty(&ima_temp_rules))
721 return -EINVAL;
722 return 0;
723}
724
Mimi Zohar4af46622009-02-04 09:07:00 -0500725/**
726 * ima_update_policy - update default_rules with new measure rules
727 *
728 * Called on file .release to update the default rules with a complete new
Petko Manolov38d859f2015-12-02 17:47:54 +0200729 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
730 * they make a queue. The policy may be updated multiple times and this is the
731 * RCU updater.
732 *
733 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
734 * we switch from the default policy to user defined.
Mimi Zohar4af46622009-02-04 09:07:00 -0500735 */
736void ima_update_policy(void)
737{
Petko Manolov53b626f2018-05-22 17:06:55 +0300738 struct list_head *policy = &ima_policy_rules;
Petko Manolov38d859f2015-12-02 17:47:54 +0200739
Petko Manolov53b626f2018-05-22 17:06:55 +0300740 list_splice_tail_init_rcu(&ima_temp_rules, policy, synchronize_rcu);
Petko Manolov38d859f2015-12-02 17:47:54 +0200741
742 if (ima_rules != policy) {
743 ima_policy_flag = 0;
744 ima_rules = policy;
Nayna Jain61917062018-10-09 23:00:36 +0530745
746 /*
747 * IMA architecture specific policy rules are specified
748 * as strings and converted to an array of ima_entry_rules
749 * on boot. After loading a custom policy, free the
750 * architecture specific rules stored as an array.
751 */
752 kfree(arch_policy_entry);
Petko Manolov38d859f2015-12-02 17:47:54 +0200753 }
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300754 ima_update_policy_flag();
Mimi Zohar4af46622009-02-04 09:07:00 -0500755}
756
Mimi Zohar1a9430d2018-12-17 19:14:49 -0500757/* Keep the enumeration in sync with the policy_tokens! */
Mimi Zohar4af46622009-02-04 09:07:00 -0500758enum {
Mimi Zohar1a9430d2018-12-17 19:14:49 -0500759 Opt_measure, Opt_dont_measure,
Mimi Zohar07f6a792011-03-09 22:25:48 -0500760 Opt_appraise, Opt_dont_appraise,
Mimi Zoharda1b0022016-09-29 10:04:52 -0400761 Opt_audit, Opt_hash, Opt_dont_hash,
Mimi Zohar4af46622009-02-04 09:07:00 -0500762 Opt_obj_user, Opt_obj_role, Opt_obj_type,
763 Opt_subj_user, Opt_subj_role, Opt_subj_type,
Mimi Zoharf1b08bb2018-01-15 11:20:36 -0500764 Opt_func, Opt_mask, Opt_fsmagic, Opt_fsname,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300765 Opt_fsuuid, Opt_uid_eq, Opt_euid_eq, Opt_fowner_eq,
766 Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt,
767 Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt,
Eric Richter02606432016-06-01 13:14:01 -0500768 Opt_appraise_type, Opt_permit_directio,
Matthew Garrett19453ce02019-06-19 15:46:11 -0700769 Opt_pcr, Opt_template, Opt_err
Mimi Zohar4af46622009-02-04 09:07:00 -0500770};
771
Mimi Zohar1a9430d2018-12-17 19:14:49 -0500772static const match_table_t policy_tokens = {
Mimi Zohar4af46622009-02-04 09:07:00 -0500773 {Opt_measure, "measure"},
774 {Opt_dont_measure, "dont_measure"},
Mimi Zohar07f6a792011-03-09 22:25:48 -0500775 {Opt_appraise, "appraise"},
776 {Opt_dont_appraise, "dont_appraise"},
Peter Moodye7c568e2012-06-14 10:04:36 -0700777 {Opt_audit, "audit"},
Mimi Zoharda1b0022016-09-29 10:04:52 -0400778 {Opt_hash, "hash"},
779 {Opt_dont_hash, "dont_hash"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500780 {Opt_obj_user, "obj_user=%s"},
781 {Opt_obj_role, "obj_role=%s"},
782 {Opt_obj_type, "obj_type=%s"},
783 {Opt_subj_user, "subj_user=%s"},
784 {Opt_subj_role, "subj_role=%s"},
785 {Opt_subj_type, "subj_type=%s"},
786 {Opt_func, "func=%s"},
787 {Opt_mask, "mask=%s"},
788 {Opt_fsmagic, "fsmagic=%s"},
Mimi Zoharf1b08bb2018-01-15 11:20:36 -0500789 {Opt_fsname, "fsname=%s"},
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300790 {Opt_fsuuid, "fsuuid=%s"},
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300791 {Opt_uid_eq, "uid=%s"},
792 {Opt_euid_eq, "euid=%s"},
793 {Opt_fowner_eq, "fowner=%s"},
794 {Opt_uid_gt, "uid>%s"},
795 {Opt_euid_gt, "euid>%s"},
796 {Opt_fowner_gt, "fowner>%s"},
797 {Opt_uid_lt, "uid<%s"},
798 {Opt_euid_lt, "euid<%s"},
799 {Opt_fowner_lt, "fowner<%s"},
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300800 {Opt_appraise_type, "appraise_type=%s"},
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400801 {Opt_permit_directio, "permit_directio"},
Eric Richter02606432016-06-01 13:14:01 -0500802 {Opt_pcr, "pcr=%s"},
Matthew Garrett19453ce02019-06-19 15:46:11 -0700803 {Opt_template, "template=%s"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500804 {Opt_err, NULL}
805};
806
Mimi Zohar07f6a792011-03-09 22:25:48 -0500807static int ima_lsm_rule_init(struct ima_rule_entry *entry,
Mimi Zohar7163a992013-01-03 14:19:09 -0500808 substring_t *args, int lsm_rule, int audit_type)
Mimi Zohar4af46622009-02-04 09:07:00 -0500809{
810 int result;
811
Eric Paris7b62e162010-04-20 10:21:01 -0400812 if (entry->lsm[lsm_rule].rule)
813 return -EINVAL;
814
Mimi Zohar7163a992013-01-03 14:19:09 -0500815 entry->lsm[lsm_rule].args_p = match_strdup(args);
816 if (!entry->lsm[lsm_rule].args_p)
817 return -ENOMEM;
818
Mimi Zohar4af46622009-02-04 09:07:00 -0500819 entry->lsm[lsm_rule].type = audit_type;
820 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
Mimi Zohar7163a992013-01-03 14:19:09 -0500821 Audit_equal,
822 entry->lsm[lsm_rule].args_p,
Mimi Zohar4af46622009-02-04 09:07:00 -0500823 &entry->lsm[lsm_rule].rule);
Mimi Zohar7163a992013-01-03 14:19:09 -0500824 if (!entry->lsm[lsm_rule].rule) {
825 kfree(entry->lsm[lsm_rule].args_p);
Mimi Zohar867c2022011-01-03 14:59:10 -0800826 return -EINVAL;
Mimi Zohar7163a992013-01-03 14:19:09 -0500827 }
828
Mimi Zohar4af46622009-02-04 09:07:00 -0500829 return result;
830}
831
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300832static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value,
833 bool (*rule_operator)(kuid_t, kuid_t))
Eric Paris2f1506c2010-04-20 10:21:30 -0400834{
Stefan Berger2afd0202018-06-04 16:54:54 -0400835 if (!ab)
836 return;
837
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300838 if (rule_operator == &uid_gt)
839 audit_log_format(ab, "%s>", key);
840 else if (rule_operator == &uid_lt)
841 audit_log_format(ab, "%s<", key);
842 else
843 audit_log_format(ab, "%s=", key);
Stefan Berger3d2859d2018-06-04 16:54:53 -0400844 audit_log_format(ab, "%s ", value);
Eric Paris2f1506c2010-04-20 10:21:30 -0400845}
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300846static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
847{
848 ima_log_string_op(ab, key, value, NULL);
849}
Eric Paris2f1506c2010-04-20 10:21:30 -0400850
Thiago Jung Bauermann3878d502019-06-27 23:19:32 -0300851/*
852 * Validating the appended signature included in the measurement list requires
853 * the file hash calculated without the appended signature (i.e., the 'd-modsig'
854 * field). Therefore, notify the user if they have the 'modsig' field but not
855 * the 'd-modsig' field in the template.
856 */
857static void check_template_modsig(const struct ima_template_desc *template)
858{
859#define MSG "template with 'modsig' field also needs 'd-modsig' field\n"
860 bool has_modsig, has_dmodsig;
861 static bool checked;
862 int i;
863
864 /* We only need to notify the user once. */
865 if (checked)
866 return;
867
868 has_modsig = has_dmodsig = false;
869 for (i = 0; i < template->num_fields; i++) {
870 if (!strcmp(template->fields[i]->field_id, "modsig"))
871 has_modsig = true;
872 else if (!strcmp(template->fields[i]->field_id, "d-modsig"))
873 has_dmodsig = true;
874 }
875
876 if (has_modsig && !has_dmodsig)
877 pr_notice(MSG);
878
879 checked = true;
880#undef MSG
881}
882
Mimi Zohar07f6a792011-03-09 22:25:48 -0500883static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
Mimi Zohar4af46622009-02-04 09:07:00 -0500884{
885 struct audit_buffer *ab;
Mimi Zohar4351c292014-11-05 07:53:55 -0500886 char *from;
Mimi Zohar4af46622009-02-04 09:07:00 -0500887 char *p;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300888 bool uid_token;
Matthew Garrett19453ce02019-06-19 15:46:11 -0700889 struct ima_template_desc *template_desc;
Mimi Zohar4af46622009-02-04 09:07:00 -0500890 int result = 0;
891
Stefan Bergerdba31ee2018-06-04 16:54:55 -0400892 ab = integrity_audit_log_start(audit_context(), GFP_KERNEL,
893 AUDIT_INTEGRITY_POLICY_RULE);
Mimi Zohar4af46622009-02-04 09:07:00 -0500894
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600895 entry->uid = INVALID_UID;
Linus Torvalds882653222012-10-02 21:38:48 -0700896 entry->fowner = INVALID_UID;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300897 entry->uid_op = &uid_eq;
898 entry->fowner_op = &uid_eq;
Eric Parisb9035b12010-04-20 10:21:07 -0400899 entry->action = UNKNOWN;
Eric Paris28ef4002010-04-20 10:21:18 -0400900 while ((p = strsep(&rule, " \t")) != NULL) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500901 substring_t args[MAX_OPT_ARGS];
902 int token;
903 unsigned long lnum;
904
905 if (result < 0)
906 break;
Eric Paris28ef4002010-04-20 10:21:18 -0400907 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
908 continue;
Mimi Zohar4af46622009-02-04 09:07:00 -0500909 token = match_token(p, policy_tokens, args);
910 switch (token) {
911 case Opt_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400912 ima_log_string(ab, "action", "measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400913
914 if (entry->action != UNKNOWN)
915 result = -EINVAL;
916
Mimi Zohar4af46622009-02-04 09:07:00 -0500917 entry->action = MEASURE;
918 break;
919 case Opt_dont_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400920 ima_log_string(ab, "action", "dont_measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400921
922 if (entry->action != UNKNOWN)
923 result = -EINVAL;
924
Mimi Zohar4af46622009-02-04 09:07:00 -0500925 entry->action = DONT_MEASURE;
926 break;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500927 case Opt_appraise:
928 ima_log_string(ab, "action", "appraise");
929
930 if (entry->action != UNKNOWN)
931 result = -EINVAL;
932
933 entry->action = APPRAISE;
934 break;
935 case Opt_dont_appraise:
936 ima_log_string(ab, "action", "dont_appraise");
937
938 if (entry->action != UNKNOWN)
939 result = -EINVAL;
940
941 entry->action = DONT_APPRAISE;
942 break;
Peter Moodye7c568e2012-06-14 10:04:36 -0700943 case Opt_audit:
944 ima_log_string(ab, "action", "audit");
945
946 if (entry->action != UNKNOWN)
947 result = -EINVAL;
948
949 entry->action = AUDIT;
950 break;
Mimi Zoharda1b0022016-09-29 10:04:52 -0400951 case Opt_hash:
952 ima_log_string(ab, "action", "hash");
953
954 if (entry->action != UNKNOWN)
955 result = -EINVAL;
956
957 entry->action = HASH;
958 break;
959 case Opt_dont_hash:
960 ima_log_string(ab, "action", "dont_hash");
961
962 if (entry->action != UNKNOWN)
963 result = -EINVAL;
964
965 entry->action = DONT_HASH;
966 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500967 case Opt_func:
Eric Paris2f1506c2010-04-20 10:21:30 -0400968 ima_log_string(ab, "func", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400969
970 if (entry->func)
Mimi Zohar07f6a792011-03-09 22:25:48 -0500971 result = -EINVAL;
Eric Paris7b62e162010-04-20 10:21:01 -0400972
Mimi Zohar1e93d002010-01-26 17:02:41 -0500973 if (strcmp(args[0].from, "FILE_CHECK") == 0)
974 entry->func = FILE_CHECK;
975 /* PATH_CHECK is for backwards compat */
976 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
977 entry->func = FILE_CHECK;
Mimi Zoharfdf90722012-10-16 12:40:08 +1030978 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
979 entry->func = MODULE_CHECK;
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400980 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
981 entry->func = FIRMWARE_CHECK;
Mimi Zohar16cac492012-12-13 11:15:04 -0500982 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
983 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
984 entry->func = MMAP_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500985 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
986 entry->func = BPRM_CHECK;
Matthew Garrettd906c102018-01-08 13:36:20 -0800987 else if (strcmp(args[0].from, "CREDS_CHECK") == 0)
988 entry->func = CREDS_CHECK;
Mimi Zohard9ddf072016-01-14 20:59:14 -0500989 else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
990 0)
991 entry->func = KEXEC_KERNEL_CHECK;
992 else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
993 == 0)
994 entry->func = KEXEC_INITRAMFS_CHECK;
Mimi Zohar19f8a842016-01-15 10:17:12 -0500995 else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
996 entry->func = POLICY_CHECK;
Prakhar Srivastavab0935122019-06-23 23:23:29 -0700997 else if (strcmp(args[0].from, "KEXEC_CMDLINE") == 0)
998 entry->func = KEXEC_CMDLINE;
Mimi Zohar4af46622009-02-04 09:07:00 -0500999 else
1000 result = -EINVAL;
1001 if (!result)
1002 entry->flags |= IMA_FUNC;
1003 break;
1004 case Opt_mask:
Eric Paris2f1506c2010-04-20 10:21:30 -04001005 ima_log_string(ab, "mask", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -04001006
1007 if (entry->mask)
1008 result = -EINVAL;
1009
Mimi Zohar4351c292014-11-05 07:53:55 -05001010 from = args[0].from;
1011 if (*from == '^')
1012 from++;
1013
1014 if ((strcmp(from, "MAY_EXEC")) == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -05001015 entry->mask = MAY_EXEC;
Mimi Zohar4351c292014-11-05 07:53:55 -05001016 else if (strcmp(from, "MAY_WRITE") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -05001017 entry->mask = MAY_WRITE;
Mimi Zohar4351c292014-11-05 07:53:55 -05001018 else if (strcmp(from, "MAY_READ") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -05001019 entry->mask = MAY_READ;
Mimi Zohar4351c292014-11-05 07:53:55 -05001020 else if (strcmp(from, "MAY_APPEND") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -05001021 entry->mask = MAY_APPEND;
1022 else
1023 result = -EINVAL;
1024 if (!result)
Mimi Zohar4351c292014-11-05 07:53:55 -05001025 entry->flags |= (*args[0].from == '^')
1026 ? IMA_INMASK : IMA_MASK;
Mimi Zohar4af46622009-02-04 09:07:00 -05001027 break;
1028 case Opt_fsmagic:
Eric Paris2f1506c2010-04-20 10:21:30 -04001029 ima_log_string(ab, "fsmagic", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -04001030
1031 if (entry->fsmagic) {
1032 result = -EINVAL;
1033 break;
1034 }
1035
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +02001036 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
Mimi Zohar4af46622009-02-04 09:07:00 -05001037 if (!result)
1038 entry->flags |= IMA_FSMAGIC;
1039 break;
Mimi Zoharf1b08bb2018-01-15 11:20:36 -05001040 case Opt_fsname:
1041 ima_log_string(ab, "fsname", args[0].from);
1042
1043 entry->fsname = kstrdup(args[0].from, GFP_KERNEL);
1044 if (!entry->fsname) {
1045 result = -ENOMEM;
1046 break;
1047 }
1048 result = 0;
1049 entry->flags |= IMA_FSNAME;
1050 break;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +03001051 case Opt_fsuuid:
1052 ima_log_string(ab, "fsuuid", args[0].from);
1053
Mike Rapoport36447452018-01-17 20:27:11 +02001054 if (!uuid_is_null(&entry->fsuuid)) {
Dmitry Kasatkin85865c12012-09-03 23:23:13 +03001055 result = -EINVAL;
1056 break;
1057 }
1058
Christoph Hellwig787d8c52017-06-01 07:00:26 +02001059 result = uuid_parse(args[0].from, &entry->fsuuid);
Mimi Zohar446d64e2013-02-24 23:42:37 -05001060 if (!result)
1061 entry->flags |= IMA_FSUUID;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +03001062 break;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001063 case Opt_uid_gt:
1064 case Opt_euid_gt:
1065 entry->uid_op = &uid_gt;
Gustavo A. R. Silva09186e52019-02-08 14:54:53 -06001066 /* fall through */
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001067 case Opt_uid_lt:
1068 case Opt_euid_lt:
1069 if ((token == Opt_uid_lt) || (token == Opt_euid_lt))
1070 entry->uid_op = &uid_lt;
Gustavo A. R. Silva09186e52019-02-08 14:54:53 -06001071 /* fall through */
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001072 case Opt_uid_eq:
1073 case Opt_euid_eq:
1074 uid_token = (token == Opt_uid_eq) ||
1075 (token == Opt_uid_gt) ||
1076 (token == Opt_uid_lt);
1077
1078 ima_log_string_op(ab, uid_token ? "uid" : "euid",
1079 args[0].from, entry->uid_op);
Eric Paris7b62e162010-04-20 10:21:01 -04001080
Eric W. Biederman8b94eea2012-05-25 18:24:12 -06001081 if (uid_valid(entry->uid)) {
Eric Paris7b62e162010-04-20 10:21:01 -04001082 result = -EINVAL;
1083 break;
1084 }
1085
Jingoo Han29707b22014-02-05 15:13:14 +09001086 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar4af46622009-02-04 09:07:00 -05001087 if (!result) {
Mimi Zohar139069e2014-11-05 07:48:36 -05001088 entry->uid = make_kuid(current_user_ns(),
1089 (uid_t) lnum);
1090 if (!uid_valid(entry->uid) ||
1091 (uid_t)lnum != lnum)
Mimi Zohar4af46622009-02-04 09:07:00 -05001092 result = -EINVAL;
1093 else
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001094 entry->flags |= uid_token
Mimi Zohar139069e2014-11-05 07:48:36 -05001095 ? IMA_UID : IMA_EUID;
Mimi Zohar4af46622009-02-04 09:07:00 -05001096 }
1097 break;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001098 case Opt_fowner_gt:
1099 entry->fowner_op = &uid_gt;
Gustavo A. R. Silva09186e52019-02-08 14:54:53 -06001100 /* fall through */
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001101 case Opt_fowner_lt:
1102 if (token == Opt_fowner_lt)
1103 entry->fowner_op = &uid_lt;
Gustavo A. R. Silva09186e52019-02-08 14:54:53 -06001104 /* fall through */
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001105 case Opt_fowner_eq:
1106 ima_log_string_op(ab, "fowner", args[0].from,
1107 entry->fowner_op);
Mimi Zohar07f6a792011-03-09 22:25:48 -05001108
Linus Torvalds882653222012-10-02 21:38:48 -07001109 if (uid_valid(entry->fowner)) {
Mimi Zohar07f6a792011-03-09 22:25:48 -05001110 result = -EINVAL;
1111 break;
1112 }
1113
Jingoo Han29707b22014-02-05 15:13:14 +09001114 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar07f6a792011-03-09 22:25:48 -05001115 if (!result) {
Linus Torvalds882653222012-10-02 21:38:48 -07001116 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
1117 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
Mimi Zohar07f6a792011-03-09 22:25:48 -05001118 result = -EINVAL;
1119 else
1120 entry->flags |= IMA_FOWNER;
1121 }
1122 break;
Mimi Zohar4af46622009-02-04 09:07:00 -05001123 case Opt_obj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -04001124 ima_log_string(ab, "obj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -05001125 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -05001126 LSM_OBJ_USER,
1127 AUDIT_OBJ_USER);
1128 break;
1129 case Opt_obj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -04001130 ima_log_string(ab, "obj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -05001131 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -05001132 LSM_OBJ_ROLE,
1133 AUDIT_OBJ_ROLE);
1134 break;
1135 case Opt_obj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -04001136 ima_log_string(ab, "obj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -05001137 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -05001138 LSM_OBJ_TYPE,
1139 AUDIT_OBJ_TYPE);
1140 break;
1141 case Opt_subj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -04001142 ima_log_string(ab, "subj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -05001143 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -05001144 LSM_SUBJ_USER,
1145 AUDIT_SUBJ_USER);
1146 break;
1147 case Opt_subj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -04001148 ima_log_string(ab, "subj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -05001149 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -05001150 LSM_SUBJ_ROLE,
1151 AUDIT_SUBJ_ROLE);
1152 break;
1153 case Opt_subj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -04001154 ima_log_string(ab, "subj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -05001155 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -05001156 LSM_SUBJ_TYPE,
1157 AUDIT_SUBJ_TYPE);
1158 break;
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +03001159 case Opt_appraise_type:
1160 if (entry->action != APPRAISE) {
1161 result = -EINVAL;
1162 break;
1163 }
1164
1165 ima_log_string(ab, "appraise_type", args[0].from);
1166 if ((strcmp(args[0].from, "imasig")) == 0)
1167 entry->flags |= IMA_DIGSIG_REQUIRED;
Thiago Jung Bauermann9044d622019-06-27 23:19:28 -03001168 else if (ima_hook_supports_modsig(entry->func) &&
1169 strcmp(args[0].from, "imasig|modsig") == 0)
1170 entry->flags |= IMA_DIGSIG_REQUIRED |
1171 IMA_MODSIG_ALLOWED;
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +03001172 else
1173 result = -EINVAL;
1174 break;
Mimi Zoharf9b2a732014-05-12 09:28:11 -04001175 case Opt_permit_directio:
1176 entry->flags |= IMA_PERMIT_DIRECTIO;
1177 break;
Eric Richter02606432016-06-01 13:14:01 -05001178 case Opt_pcr:
1179 if (entry->action != MEASURE) {
1180 result = -EINVAL;
1181 break;
1182 }
1183 ima_log_string(ab, "pcr", args[0].from);
1184
1185 result = kstrtoint(args[0].from, 10, &entry->pcr);
1186 if (result || INVALID_PCR(entry->pcr))
1187 result = -EINVAL;
1188 else
1189 entry->flags |= IMA_PCR;
1190
1191 break;
Matthew Garrett19453ce02019-06-19 15:46:11 -07001192 case Opt_template:
1193 ima_log_string(ab, "template", args[0].from);
1194 if (entry->action != MEASURE) {
1195 result = -EINVAL;
1196 break;
1197 }
1198 template_desc = lookup_template_desc(args[0].from);
1199 if (!template_desc || entry->template) {
1200 result = -EINVAL;
1201 break;
1202 }
1203
1204 /*
1205 * template_desc_init_fields() does nothing if
1206 * the template is already initialised, so
1207 * it's safe to do this unconditionally
1208 */
1209 template_desc_init_fields(template_desc->fmt,
1210 &(template_desc->fields),
1211 &(template_desc->num_fields));
1212 entry->template = template_desc;
1213 break;
Mimi Zohar4af46622009-02-04 09:07:00 -05001214 case Opt_err:
Eric Paris2f1506c2010-04-20 10:21:30 -04001215 ima_log_string(ab, "UNKNOWN", p);
Eric Parise9d393b2010-04-20 10:21:13 -04001216 result = -EINVAL;
Mimi Zohar4af46622009-02-04 09:07:00 -05001217 break;
1218 }
1219 }
Eric Paris7b62e162010-04-20 10:21:01 -04001220 if (!result && (entry->action == UNKNOWN))
Mimi Zohar4af46622009-02-04 09:07:00 -05001221 result = -EINVAL;
Mimi Zohar6f0911a2018-04-12 00:15:22 -04001222 else if (entry->action == APPRAISE)
1223 temp_ima_appraise |= ima_appraise_flag(entry->func);
1224
Thiago Jung Bauermann3878d502019-06-27 23:19:32 -03001225 if (!result && entry->flags & IMA_MODSIG_ALLOWED) {
1226 template_desc = entry->template ? entry->template :
1227 ima_template_desc_current();
1228 check_template_modsig(template_desc);
1229 }
1230
Eric Parisb0d5de42012-02-14 17:11:07 -05001231 audit_log_format(ab, "res=%d", !result);
Mimi Zohar4af46622009-02-04 09:07:00 -05001232 audit_log_end(ab);
1233 return result;
1234}
1235
1236/**
Mimi Zohar07f6a792011-03-09 22:25:48 -05001237 * ima_parse_add_rule - add a rule to ima_policy_rules
Mimi Zohar4af46622009-02-04 09:07:00 -05001238 * @rule - ima measurement policy rule
1239 *
Petko Manolov38d859f2015-12-02 17:47:54 +02001240 * Avoid locking by allowing just one writer at a time in ima_write_policy()
Eric Paris6ccd0452010-04-20 10:20:54 -04001241 * Returns the length of the rule parsed, an error code on failure
Mimi Zohar4af46622009-02-04 09:07:00 -05001242 */
Eric Paris6ccd0452010-04-20 10:20:54 -04001243ssize_t ima_parse_add_rule(char *rule)
Mimi Zohar4af46622009-02-04 09:07:00 -05001244{
Mimi Zohar52a13282013-12-11 14:44:04 -05001245 static const char op[] = "update_policy";
Eric Paris6ccd0452010-04-20 10:20:54 -04001246 char *p;
Mimi Zohar07f6a792011-03-09 22:25:48 -05001247 struct ima_rule_entry *entry;
Eric Paris6ccd0452010-04-20 10:20:54 -04001248 ssize_t result, len;
Mimi Zohar4af46622009-02-04 09:07:00 -05001249 int audit_info = 0;
1250
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +03001251 p = strsep(&rule, "\n");
1252 len = strlen(p) + 1;
Dmitry Kasatkin71787842014-10-03 14:40:20 +03001253 p += strspn(p, " \t");
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +03001254
Dmitry Kasatkin71787842014-10-03 14:40:20 +03001255 if (*p == '#' || *p == '\0')
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +03001256 return len;
1257
Mimi Zohar4af46622009-02-04 09:07:00 -05001258 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1259 if (!entry) {
1260 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1261 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
1262 return -ENOMEM;
1263 }
1264
1265 INIT_LIST_HEAD(&entry->list);
1266
Eric Paris6ccd0452010-04-20 10:20:54 -04001267 result = ima_parse_rule(p, entry);
Eric Paris7233e3e2010-04-20 10:21:24 -04001268 if (result) {
Mimi Zohar4af46622009-02-04 09:07:00 -05001269 kfree(entry);
Mimi Zohar523979a2009-02-11 11:12:28 -05001270 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
Richard Guy Briggs7e9001f2014-06-16 15:52:07 -04001271 NULL, op, "invalid-policy", result,
Mimi Zohar523979a2009-02-11 11:12:28 -05001272 audit_info);
Eric Paris7233e3e2010-04-20 10:21:24 -04001273 return result;
Mimi Zohar523979a2009-02-11 11:12:28 -05001274 }
Eric Paris7233e3e2010-04-20 10:21:24 -04001275
Petko Manolov38d859f2015-12-02 17:47:54 +02001276 list_add_tail(&entry->list, &ima_temp_rules);
Eric Paris7233e3e2010-04-20 10:21:24 -04001277
1278 return len;
Mimi Zohar4af46622009-02-04 09:07:00 -05001279}
1280
Petko Manolov38d859f2015-12-02 17:47:54 +02001281/**
1282 * ima_delete_rules() called to cleanup invalid in-flight policy.
1283 * We don't need locking as we operate on the temp list, which is
1284 * different from the active one. There is also only one user of
1285 * ima_delete_rules() at a time.
1286 */
James Morris64c61d82009-02-05 09:28:26 +11001287void ima_delete_rules(void)
Mimi Zohar4af46622009-02-04 09:07:00 -05001288{
Mimi Zohar07f6a792011-03-09 22:25:48 -05001289 struct ima_rule_entry *entry, *tmp;
Mimi Zohar7163a992013-01-03 14:19:09 -05001290 int i;
Mimi Zohar4af46622009-02-04 09:07:00 -05001291
Mimi Zohar6ad6afa2015-12-07 14:35:47 -05001292 temp_ima_appraise = 0;
Petko Manolov38d859f2015-12-02 17:47:54 +02001293 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
Mimi Zohar7163a992013-01-03 14:19:09 -05001294 for (i = 0; i < MAX_LSM_RULES; i++)
1295 kfree(entry->lsm[i].args_p);
1296
Mimi Zohar4af46622009-02-04 09:07:00 -05001297 list_del(&entry->list);
1298 kfree(entry);
1299 }
Mimi Zohar4af46622009-02-04 09:07:00 -05001300}
Petko Manolov80eae202015-12-02 17:47:56 +02001301
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -03001302#define __ima_hook_stringify(str) (#str),
1303
1304const char *const func_tokens[] = {
1305 __ima_hooks(__ima_hook_stringify)
1306};
1307
Petko Manolov80eae202015-12-02 17:47:56 +02001308#ifdef CONFIG_IMA_READ_POLICY
1309enum {
1310 mask_exec = 0, mask_write, mask_read, mask_append
1311};
1312
Thiago Jung Bauermannbb543e32017-06-07 22:49:10 -03001313static const char *const mask_tokens[] = {
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02001314 "^MAY_EXEC",
1315 "^MAY_WRITE",
1316 "^MAY_READ",
1317 "^MAY_APPEND"
Petko Manolov80eae202015-12-02 17:47:56 +02001318};
1319
Petko Manolov80eae202015-12-02 17:47:56 +02001320void *ima_policy_start(struct seq_file *m, loff_t *pos)
1321{
1322 loff_t l = *pos;
1323 struct ima_rule_entry *entry;
1324
1325 rcu_read_lock();
1326 list_for_each_entry_rcu(entry, ima_rules, list) {
1327 if (!l--) {
1328 rcu_read_unlock();
1329 return entry;
1330 }
1331 }
1332 rcu_read_unlock();
1333 return NULL;
1334}
1335
1336void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
1337{
1338 struct ima_rule_entry *entry = v;
1339
1340 rcu_read_lock();
1341 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
1342 rcu_read_unlock();
1343 (*pos)++;
1344
1345 return (&entry->list == ima_rules) ? NULL : entry;
1346}
1347
1348void ima_policy_stop(struct seq_file *m, void *v)
1349{
1350}
1351
Mimi Zohar1a9430d2018-12-17 19:14:49 -05001352#define pt(token) policy_tokens[token].pattern
Petko Manolov80eae202015-12-02 17:47:56 +02001353#define mt(token) mask_tokens[token]
Petko Manolov80eae202015-12-02 17:47:56 +02001354
Mimi Zoharb5269ab2016-01-28 13:10:36 -05001355/*
1356 * policy_func_show - display the ima_hooks policy rule
1357 */
1358static void policy_func_show(struct seq_file *m, enum ima_hooks func)
1359{
Thiago Jung Bauermann26632182017-06-07 22:49:11 -03001360 if (func > 0 && func < MAX_CHECK)
1361 seq_printf(m, "func=%s ", func_tokens[func]);
1362 else
1363 seq_printf(m, "func=%d ", func);
Mimi Zoharb5269ab2016-01-28 13:10:36 -05001364}
1365
Petko Manolov80eae202015-12-02 17:47:56 +02001366int ima_policy_show(struct seq_file *m, void *v)
1367{
1368 struct ima_rule_entry *entry = v;
Andy Shevchenkob8b57272016-05-20 17:00:57 -07001369 int i;
Petko Manolov80eae202015-12-02 17:47:56 +02001370 char tbuf[64] = {0,};
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02001371 int offset = 0;
Petko Manolov80eae202015-12-02 17:47:56 +02001372
1373 rcu_read_lock();
1374
1375 if (entry->action & MEASURE)
1376 seq_puts(m, pt(Opt_measure));
1377 if (entry->action & DONT_MEASURE)
1378 seq_puts(m, pt(Opt_dont_measure));
1379 if (entry->action & APPRAISE)
1380 seq_puts(m, pt(Opt_appraise));
1381 if (entry->action & DONT_APPRAISE)
1382 seq_puts(m, pt(Opt_dont_appraise));
1383 if (entry->action & AUDIT)
1384 seq_puts(m, pt(Opt_audit));
Mimi Zoharda1b0022016-09-29 10:04:52 -04001385 if (entry->action & HASH)
1386 seq_puts(m, pt(Opt_hash));
1387 if (entry->action & DONT_HASH)
1388 seq_puts(m, pt(Opt_dont_hash));
Petko Manolov80eae202015-12-02 17:47:56 +02001389
1390 seq_puts(m, " ");
1391
Mimi Zoharb5269ab2016-01-28 13:10:36 -05001392 if (entry->flags & IMA_FUNC)
1393 policy_func_show(m, entry->func);
Petko Manolov80eae202015-12-02 17:47:56 +02001394
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02001395 if ((entry->flags & IMA_MASK) || (entry->flags & IMA_INMASK)) {
1396 if (entry->flags & IMA_MASK)
1397 offset = 1;
Petko Manolov80eae202015-12-02 17:47:56 +02001398 if (entry->mask & MAY_EXEC)
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02001399 seq_printf(m, pt(Opt_mask), mt(mask_exec) + offset);
Petko Manolov80eae202015-12-02 17:47:56 +02001400 if (entry->mask & MAY_WRITE)
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02001401 seq_printf(m, pt(Opt_mask), mt(mask_write) + offset);
Petko Manolov80eae202015-12-02 17:47:56 +02001402 if (entry->mask & MAY_READ)
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02001403 seq_printf(m, pt(Opt_mask), mt(mask_read) + offset);
Petko Manolov80eae202015-12-02 17:47:56 +02001404 if (entry->mask & MAY_APPEND)
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02001405 seq_printf(m, pt(Opt_mask), mt(mask_append) + offset);
Petko Manolov80eae202015-12-02 17:47:56 +02001406 seq_puts(m, " ");
1407 }
1408
1409 if (entry->flags & IMA_FSMAGIC) {
1410 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
1411 seq_printf(m, pt(Opt_fsmagic), tbuf);
1412 seq_puts(m, " ");
1413 }
1414
Mimi Zoharf1b08bb2018-01-15 11:20:36 -05001415 if (entry->flags & IMA_FSNAME) {
1416 snprintf(tbuf, sizeof(tbuf), "%s", entry->fsname);
1417 seq_printf(m, pt(Opt_fsname), tbuf);
1418 seq_puts(m, " ");
1419 }
1420
Eric Richter02606432016-06-01 13:14:01 -05001421 if (entry->flags & IMA_PCR) {
1422 snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
1423 seq_printf(m, pt(Opt_pcr), tbuf);
1424 seq_puts(m, " ");
1425 }
1426
Petko Manolov80eae202015-12-02 17:47:56 +02001427 if (entry->flags & IMA_FSUUID) {
Christoph Hellwig787d8c52017-06-01 07:00:26 +02001428 seq_printf(m, "fsuuid=%pU", &entry->fsuuid);
Petko Manolov80eae202015-12-02 17:47:56 +02001429 seq_puts(m, " ");
1430 }
1431
1432 if (entry->flags & IMA_UID) {
1433 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001434 if (entry->uid_op == &uid_gt)
1435 seq_printf(m, pt(Opt_uid_gt), tbuf);
1436 else if (entry->uid_op == &uid_lt)
1437 seq_printf(m, pt(Opt_uid_lt), tbuf);
1438 else
1439 seq_printf(m, pt(Opt_uid_eq), tbuf);
Petko Manolov80eae202015-12-02 17:47:56 +02001440 seq_puts(m, " ");
1441 }
1442
1443 if (entry->flags & IMA_EUID) {
1444 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001445 if (entry->uid_op == &uid_gt)
1446 seq_printf(m, pt(Opt_euid_gt), tbuf);
1447 else if (entry->uid_op == &uid_lt)
1448 seq_printf(m, pt(Opt_euid_lt), tbuf);
1449 else
1450 seq_printf(m, pt(Opt_euid_eq), tbuf);
Petko Manolov80eae202015-12-02 17:47:56 +02001451 seq_puts(m, " ");
1452 }
1453
1454 if (entry->flags & IMA_FOWNER) {
1455 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001456 if (entry->fowner_op == &uid_gt)
1457 seq_printf(m, pt(Opt_fowner_gt), tbuf);
1458 else if (entry->fowner_op == &uid_lt)
1459 seq_printf(m, pt(Opt_fowner_lt), tbuf);
1460 else
1461 seq_printf(m, pt(Opt_fowner_eq), tbuf);
Petko Manolov80eae202015-12-02 17:47:56 +02001462 seq_puts(m, " ");
1463 }
1464
1465 for (i = 0; i < MAX_LSM_RULES; i++) {
1466 if (entry->lsm[i].rule) {
1467 switch (i) {
1468 case LSM_OBJ_USER:
1469 seq_printf(m, pt(Opt_obj_user),
1470 (char *)entry->lsm[i].args_p);
1471 break;
1472 case LSM_OBJ_ROLE:
1473 seq_printf(m, pt(Opt_obj_role),
1474 (char *)entry->lsm[i].args_p);
1475 break;
1476 case LSM_OBJ_TYPE:
1477 seq_printf(m, pt(Opt_obj_type),
1478 (char *)entry->lsm[i].args_p);
1479 break;
1480 case LSM_SUBJ_USER:
1481 seq_printf(m, pt(Opt_subj_user),
1482 (char *)entry->lsm[i].args_p);
1483 break;
1484 case LSM_SUBJ_ROLE:
1485 seq_printf(m, pt(Opt_subj_role),
1486 (char *)entry->lsm[i].args_p);
1487 break;
1488 case LSM_SUBJ_TYPE:
1489 seq_printf(m, pt(Opt_subj_type),
1490 (char *)entry->lsm[i].args_p);
1491 break;
1492 }
1493 }
1494 }
Matthew Garrett19453ce02019-06-19 15:46:11 -07001495 if (entry->template)
1496 seq_printf(m, "template=%s ", entry->template->name);
Thiago Jung Bauermann9044d622019-06-27 23:19:28 -03001497 if (entry->flags & IMA_DIGSIG_REQUIRED) {
1498 if (entry->flags & IMA_MODSIG_ALLOWED)
1499 seq_puts(m, "appraise_type=imasig|modsig ");
1500 else
1501 seq_puts(m, "appraise_type=imasig ");
1502 }
Petko Manolov80eae202015-12-02 17:47:56 +02001503 if (entry->flags & IMA_PERMIT_DIRECTIO)
1504 seq_puts(m, "permit_directio ");
1505 rcu_read_unlock();
1506 seq_puts(m, "\n");
1507 return 0;
1508}
1509#endif /* CONFIG_IMA_READ_POLICY */