blob: 3b0b43e18ecfa6676c7a45f5b1c3c450ad7c4ec6 [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
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) || \
Pankaj Bharadiyac5936422019-12-09 10:31:43 -080048 (a) >= (sizeof_field(struct integrity_iint_cache, measured_pcrs) * 8))
Eric Richter02606432016-06-01 13:14:01 -050049
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 */
Tyler Hicksaa0c0222020-07-09 01:19:08 -050078 char *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;
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -080082 char *keyrings; /* Measure keys added to these keyrings */
Matthew Garrett19453ce02019-06-19 15:46:11 -070083 struct ima_template_desc *template;
Mimi Zohar3323eec2009-02-04 09:06:58 -050084};
85
Eric Paris5789ba32009-05-21 15:47:06 -040086/*
87 * Without LSM specific knowledge, the default policy can only be
Mimi Zohar07f6a792011-03-09 22:25:48 -050088 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
Mimi Zohar4af46622009-02-04 09:07:00 -050089 */
Eric Paris5789ba32009-05-21 15:47:06 -040090
91/*
92 * The minimum rule set to allow for full TCB coverage. Measures all files
93 * opened or mmap for exec and everything read by root. Dangerous because
94 * normal users can easily run the machine out of memory simply building
95 * and running executables.
96 */
James Morrisbad44172017-02-13 16:34:35 +110097static struct ima_rule_entry dont_measure_rules[] __ro_after_init = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020098 {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
99 {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
100 {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
101 {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
102 {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
103 {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
104 {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
105 {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
Martin Townsend1c070b12018-03-22 13:46:01 +0000106 {.action = DONT_MEASURE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
Roberto Sassu6438de92015-04-11 17:13:06 +0200107 {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
108 .flags = IMA_FSMAGIC},
Laura Abbott82e3bb42017-05-09 11:25:27 -0700109 {.action = DONT_MEASURE, .fsmagic = CGROUP2_SUPER_MAGIC,
110 .flags = IMA_FSMAGIC},
Mimi Zohar060190f2018-11-14 17:24:13 -0500111 {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
112 {.action = DONT_MEASURE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC}
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400113};
114
James Morrisbad44172017-02-13 16:34:35 +1100115static struct ima_rule_entry original_measurement_rules[] __ro_after_init = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200116 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500117 .flags = IMA_FUNC | IMA_MASK},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200118 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500119 .flags = IMA_FUNC | IMA_MASK},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400120 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300121 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
122 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400123 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
124 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
125};
126
James Morrisbad44172017-02-13 16:34:35 +1100127static struct ima_rule_entry default_measurement_rules[] __ro_after_init = {
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400128 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
129 .flags = IMA_FUNC | IMA_MASK},
130 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
131 .flags = IMA_FUNC | IMA_MASK},
132 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300133 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
134 .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400135 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300136 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
137 .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200138 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400139 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
Mimi Zohar19f8a842016-01-15 10:17:12 -0500140 {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC},
Mimi Zohar3323eec2009-02-04 09:06:58 -0500141};
142
James Morrisbad44172017-02-13 16:34:35 +1100143static struct ima_rule_entry default_appraise_rules[] __ro_after_init = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200144 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
145 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
146 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
147 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
148 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
149 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
150 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
151 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
152 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
Martin Townsend1c070b12018-03-22 13:46:01 +0000153 {.action = DONT_APPRAISE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
Mimi Zoharcd025f72015-04-21 16:54:24 -0400154 {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
Mimi Zohar060190f2018-11-14 17:24:13 -0500155 {.action = DONT_APPRAISE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200156 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
Laura Abbott82e3bb42017-05-09 11:25:27 -0700157 {.action = DONT_APPRAISE, .fsmagic = CGROUP2_SUPER_MAGIC, .flags = IMA_FSMAGIC},
Mimi Zohar95ee08f2015-12-07 15:08:01 -0500158#ifdef CONFIG_IMA_WRITE_POLICY
159 {.action = APPRAISE, .func = POLICY_CHECK,
160 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
161#endif
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200162#ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300163 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
164 .flags = IMA_FOWNER},
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200165#else
166 /* force signature */
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300167 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200168 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
169#endif
Mimi Zohar07f6a792011-03-09 22:25:48 -0500170};
Mimi Zohar3323eec2009-02-04 09:06:58 -0500171
Mimi Zoharef968372018-07-13 14:06:01 -0400172static struct ima_rule_entry build_appraise_rules[] __ro_after_init = {
173#ifdef CONFIG_IMA_APPRAISE_REQUIRE_MODULE_SIGS
174 {.action = APPRAISE, .func = MODULE_CHECK,
175 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
176#endif
177#ifdef CONFIG_IMA_APPRAISE_REQUIRE_FIRMWARE_SIGS
178 {.action = APPRAISE, .func = FIRMWARE_CHECK,
179 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
180#endif
181#ifdef CONFIG_IMA_APPRAISE_REQUIRE_KEXEC_SIGS
182 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
183 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
184#endif
185#ifdef CONFIG_IMA_APPRAISE_REQUIRE_POLICY_SIGS
186 {.action = APPRAISE, .func = POLICY_CHECK,
187 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
188#endif
189};
190
Mimi Zohar503ceae2017-04-21 18:58:27 -0400191static struct ima_rule_entry secure_boot_rules[] __ro_after_init = {
192 {.action = APPRAISE, .func = MODULE_CHECK,
193 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
194 {.action = APPRAISE, .func = FIRMWARE_CHECK,
195 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
196 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
197 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
198 {.action = APPRAISE, .func = POLICY_CHECK,
199 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
200};
201
Nayna Jain61917062018-10-09 23:00:36 +0530202/* An array of architecture specific rules */
YueHaibing68f25292019-06-11 21:40:32 +0800203static struct ima_rule_entry *arch_policy_entry __ro_after_init;
Nayna Jain61917062018-10-09 23:00:36 +0530204
Mimi Zohar07f6a792011-03-09 22:25:48 -0500205static LIST_HEAD(ima_default_rules);
206static LIST_HEAD(ima_policy_rules);
Petko Manolov38d859f2015-12-02 17:47:54 +0200207static LIST_HEAD(ima_temp_rules);
Roberto Sassu067a4362020-06-03 17:08:20 +0200208static struct list_head *ima_rules = &ima_default_rules;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500209
Lakshmi Ramasubramanian5c7bac92020-01-16 18:18:21 -0800210/* Pre-allocated buffer used for matching keyrings. */
211static char *ima_keyrings;
212static size_t ima_keyrings_len;
213
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400214static int ima_policy __initdata;
Petko Manolov38d859f2015-12-02 17:47:54 +0200215
Mimi Zohar07f6a792011-03-09 22:25:48 -0500216static int __init default_measure_policy_setup(char *str)
Eric Paris5789ba32009-05-21 15:47:06 -0400217{
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400218 if (ima_policy)
219 return 1;
220
221 ima_policy = ORIGINAL_TCB;
Eric Paris5789ba32009-05-21 15:47:06 -0400222 return 1;
223}
Mimi Zohar07f6a792011-03-09 22:25:48 -0500224__setup("ima_tcb", default_measure_policy_setup);
225
Mimi Zohar33ce9542017-04-24 12:04:09 -0400226static bool ima_use_appraise_tcb __initdata;
Mimi Zohar503ceae2017-04-21 18:58:27 -0400227static bool ima_use_secure_boot __initdata;
Mimi Zohar9e670282018-02-21 11:36:32 -0500228static bool ima_fail_unverifiable_sigs __ro_after_init;
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400229static int __init policy_setup(char *str)
230{
Mimi Zohar33ce9542017-04-24 12:04:09 -0400231 char *p;
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400232
Mimi Zohar33ce9542017-04-24 12:04:09 -0400233 while ((p = strsep(&str, " |\n")) != NULL) {
234 if (*p == ' ')
235 continue;
236 if ((strcmp(p, "tcb") == 0) && !ima_policy)
237 ima_policy = DEFAULT_TCB;
238 else if (strcmp(p, "appraise_tcb") == 0)
Thomas Meyer39adb922017-10-07 16:02:21 +0200239 ima_use_appraise_tcb = true;
Mimi Zohar503ceae2017-04-21 18:58:27 -0400240 else if (strcmp(p, "secure_boot") == 0)
Thomas Meyer39adb922017-10-07 16:02:21 +0200241 ima_use_secure_boot = true;
Mimi Zohar9e670282018-02-21 11:36:32 -0500242 else if (strcmp(p, "fail_securely") == 0)
243 ima_fail_unverifiable_sigs = true;
Mimi Zohar33ce9542017-04-24 12:04:09 -0400244 }
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400245
246 return 1;
247}
248__setup("ima_policy=", policy_setup);
249
Mimi Zohar07f6a792011-03-09 22:25:48 -0500250static int __init default_appraise_policy_setup(char *str)
251{
Thomas Meyer39adb922017-10-07 16:02:21 +0200252 ima_use_appraise_tcb = true;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500253 return 1;
254}
255__setup("ima_appraise_tcb", default_appraise_policy_setup);
Eric Paris5789ba32009-05-21 15:47:06 -0400256
Janne Karhunenb1694242019-06-14 15:20:15 +0300257static void ima_lsm_free_rule(struct ima_rule_entry *entry)
258{
259 int i;
260
261 for (i = 0; i < MAX_LSM_RULES; i++) {
Tyler Hicksb8867ee2020-07-10 15:37:50 -0500262 ima_filter_rule_free(entry->lsm[i].rule);
Janne Karhunenb1694242019-06-14 15:20:15 +0300263 kfree(entry->lsm[i].args_p);
264 }
Tyler Hicks465aee72020-07-09 01:19:01 -0500265}
266
267static void ima_free_rule(struct ima_rule_entry *entry)
268{
269 if (!entry)
270 return;
271
272 /*
273 * entry->template->fields may be allocated in ima_parse_rule() but that
274 * reference is owned by the corresponding ima_template_desc element in
275 * the defined_templates list and cannot be freed here
276 */
277 kfree(entry->fsname);
278 kfree(entry->keyrings);
279 ima_lsm_free_rule(entry);
Janne Karhunenb1694242019-06-14 15:20:15 +0300280 kfree(entry);
281}
282
283static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry)
284{
285 struct ima_rule_entry *nentry;
Janne Karhunen483ec262020-01-15 17:42:30 +0200286 int i;
Janne Karhunenb1694242019-06-14 15:20:15 +0300287
288 nentry = kmalloc(sizeof(*nentry), GFP_KERNEL);
289 if (!nentry)
290 return NULL;
291
292 /*
293 * Immutable elements are copied over as pointers and data; only
294 * lsm rules can change
295 */
296 memcpy(nentry, entry, sizeof(*nentry));
Pankaj Bharadiyac5936422019-12-09 10:31:43 -0800297 memset(nentry->lsm, 0, sizeof_field(struct ima_rule_entry, lsm));
Janne Karhunenb1694242019-06-14 15:20:15 +0300298
299 for (i = 0; i < MAX_LSM_RULES; i++) {
Janne Karhunen483ec262020-01-15 17:42:30 +0200300 if (!entry->lsm[i].args_p)
Janne Karhunenb1694242019-06-14 15:20:15 +0300301 continue;
302
303 nentry->lsm[i].type = entry->lsm[i].type;
Tyler Hicks39e59932020-07-09 01:19:07 -0500304 nentry->lsm[i].args_p = entry->lsm[i].args_p;
305 /*
306 * Remove the reference from entry so that the associated
307 * memory will not be freed during a later call to
308 * ima_lsm_free_rule(entry).
309 */
310 entry->lsm[i].args_p = NULL;
Janne Karhunenb1694242019-06-14 15:20:15 +0300311
Tyler Hicksb8867ee2020-07-10 15:37:50 -0500312 ima_filter_rule_init(nentry->lsm[i].type, Audit_equal,
313 nentry->lsm[i].args_p,
314 &nentry->lsm[i].rule);
Janne Karhunen483ec262020-01-15 17:42:30 +0200315 if (!nentry->lsm[i].rule)
316 pr_warn("rule for LSM \'%s\' is undefined\n",
Tyler Hicksaa0c0222020-07-09 01:19:08 -0500317 nentry->lsm[i].args_p);
Janne Karhunenb1694242019-06-14 15:20:15 +0300318 }
319 return nentry;
Janne Karhunenb1694242019-06-14 15:20:15 +0300320}
321
322static int ima_lsm_update_rule(struct ima_rule_entry *entry)
323{
324 struct ima_rule_entry *nentry;
325
326 nentry = ima_lsm_copy_rule(entry);
327 if (!nentry)
328 return -ENOMEM;
329
330 list_replace_rcu(&entry->list, &nentry->list);
331 synchronize_rcu();
Tyler Hicks465aee72020-07-09 01:19:01 -0500332 /*
333 * ima_lsm_copy_rule() shallow copied all references, except for the
334 * LSM references, from entry to nentry so we only want to free the LSM
335 * references and the entry itself. All other memory refrences will now
336 * be owned by nentry.
337 */
Janne Karhunenb1694242019-06-14 15:20:15 +0300338 ima_lsm_free_rule(entry);
Tyler Hicks465aee72020-07-09 01:19:01 -0500339 kfree(entry);
Janne Karhunenb1694242019-06-14 15:20:15 +0300340
341 return 0;
342}
343
Tyler Hicksdb2045f2020-07-09 01:19:04 -0500344static bool ima_rule_contains_lsm_cond(struct ima_rule_entry *entry)
345{
346 int i;
347
348 for (i = 0; i < MAX_LSM_RULES; i++)
349 if (entry->lsm[i].args_p)
350 return true;
351
352 return false;
353}
354
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200355/*
Petko Manolov38d859f2015-12-02 17:47:54 +0200356 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
357 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
Janne Karhunenb1694242019-06-14 15:20:15 +0300358 * the reloaded LSM policy.
Mimi Zohar7163a992013-01-03 14:19:09 -0500359 */
360static void ima_lsm_update_rules(void)
361{
Janne Karhunenb1694242019-06-14 15:20:15 +0300362 struct ima_rule_entry *entry, *e;
Tyler Hicks592b24c2020-07-09 01:19:10 -0500363 int result;
Mimi Zohar7163a992013-01-03 14:19:09 -0500364
Janne Karhunenb1694242019-06-14 15:20:15 +0300365 list_for_each_entry_safe(entry, e, &ima_policy_rules, list) {
Tyler Hicks592b24c2020-07-09 01:19:10 -0500366 if (!ima_rule_contains_lsm_cond(entry))
Janne Karhunenb1694242019-06-14 15:20:15 +0300367 continue;
368
369 result = ima_lsm_update_rule(entry);
370 if (result) {
Janne Karhunen483ec262020-01-15 17:42:30 +0200371 pr_err("lsm rule update error %d\n", result);
Janne Karhunenb1694242019-06-14 15:20:15 +0300372 return;
Mimi Zohar7163a992013-01-03 14:19:09 -0500373 }
374 }
Mimi Zohar7163a992013-01-03 14:19:09 -0500375}
376
Janne Karhunenb1694242019-06-14 15:20:15 +0300377int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
378 void *lsm_data)
379{
380 if (event != LSM_POLICY_CHANGE)
381 return NOTIFY_DONE;
382
383 ima_lsm_update_rules();
384 return NOTIFY_OK;
385}
386
Mimi Zohar3323eec2009-02-04 09:06:58 -0500387/**
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800388 * ima_match_keyring - determine whether the keyring matches the measure rule
389 * @rule: a pointer to a rule
390 * @keyring: name of the keyring to match against the measure rule
391 * @cred: a pointer to a credentials structure for user validation
392 *
393 * Returns true if keyring matches one in the rule, false otherwise.
394 */
395static bool ima_match_keyring(struct ima_rule_entry *rule,
396 const char *keyring, const struct cred *cred)
397{
Lakshmi Ramasubramanian5c7bac92020-01-16 18:18:21 -0800398 char *next_keyring, *keyrings_ptr;
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800399 bool matched = false;
400
401 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
402 return false;
403
404 if (!rule->keyrings)
405 return true;
406
407 if (!keyring)
408 return false;
409
Lakshmi Ramasubramanian5c7bac92020-01-16 18:18:21 -0800410 strcpy(ima_keyrings, rule->keyrings);
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800411
412 /*
413 * "keyrings=" is specified in the policy in the format below:
414 * keyrings=.builtin_trusted_keys|.ima|.evm
415 */
Lakshmi Ramasubramanian5c7bac92020-01-16 18:18:21 -0800416 keyrings_ptr = ima_keyrings;
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800417 while ((next_keyring = strsep(&keyrings_ptr, "|")) != NULL) {
418 if (!strcmp(next_keyring, keyring)) {
419 matched = true;
420 break;
421 }
422 }
423
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800424 return matched;
425}
426
427/**
Janne Karhunen483ec262020-01-15 17:42:30 +0200428 * ima_match_rules - determine whether an inode matches the policy rule.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500429 * @rule: a pointer to a rule
430 * @inode: a pointer to an inode
Matthew Garrettd906c102018-01-08 13:36:20 -0800431 * @cred: a pointer to a credentials structure for user validation
432 * @secid: the secid of the task to be validated
Mimi Zohar3323eec2009-02-04 09:06:58 -0500433 * @func: LIM hook identifier
434 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800435 * @keyring: keyring name to check in policy for KEY_CHECK func
Mimi Zohar3323eec2009-02-04 09:06:58 -0500436 *
437 * Returns true on rule match, false on failure.
438 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500439static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,
Matthew Garrettd906c102018-01-08 13:36:20 -0800440 const struct cred *cred, u32 secid,
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800441 enum ima_hooks func, int mask,
442 const char *keyring)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500443{
Mimi Zohar4af46622009-02-04 09:07:00 -0500444 int i;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500445
Tyler Hicks48341772020-07-09 01:19:11 -0500446 if (func == KEY_CHECK) {
447 return (rule->flags & IMA_FUNC) && (rule->func == func) &&
448 ima_match_keyring(rule, keyring, cred);
Prakhar Srivastavab0935122019-06-23 23:23:29 -0700449 }
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200450 if ((rule->flags & IMA_FUNC) &&
451 (rule->func != func && func != POST_SETATTR))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500452 return false;
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200453 if ((rule->flags & IMA_MASK) &&
454 (rule->mask != mask && func != POST_SETATTR))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500455 return false;
Mimi Zohar4351c292014-11-05 07:53:55 -0500456 if ((rule->flags & IMA_INMASK) &&
457 (!(rule->mask & mask) && func != POST_SETATTR))
458 return false;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500459 if ((rule->flags & IMA_FSMAGIC)
460 && rule->fsmagic != inode->i_sb->s_magic)
461 return false;
Mimi Zoharf1b08bb2018-01-15 11:20:36 -0500462 if ((rule->flags & IMA_FSNAME)
463 && strcmp(rule->fsname, inode->i_sb->s_type->name))
464 return false;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300465 if ((rule->flags & IMA_FSUUID) &&
Christoph Hellwig85787092017-05-10 15:06:33 +0200466 !uuid_equal(&rule->fsuuid, &inode->i_sb->s_uuid))
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300467 return false;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300468 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500469 return false;
Mimi Zohar139069e2014-11-05 07:48:36 -0500470 if (rule->flags & IMA_EUID) {
471 if (has_capability_noaudit(current, CAP_SETUID)) {
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300472 if (!rule->uid_op(cred->euid, rule->uid)
473 && !rule->uid_op(cred->suid, rule->uid)
474 && !rule->uid_op(cred->uid, rule->uid))
Mimi Zohar139069e2014-11-05 07:48:36 -0500475 return false;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300476 } else if (!rule->uid_op(cred->euid, rule->uid))
Mimi Zohar139069e2014-11-05 07:48:36 -0500477 return false;
478 }
479
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300480 if ((rule->flags & IMA_FOWNER) &&
481 !rule->fowner_op(inode->i_uid, rule->fowner))
Mimi Zohar07f6a792011-03-09 22:25:48 -0500482 return false;
Mimi Zohar4af46622009-02-04 09:07:00 -0500483 for (i = 0; i < MAX_LSM_RULES; i++) {
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400484 int rc = 0;
Matthew Garrettd906c102018-01-08 13:36:20 -0800485 u32 osid;
Mimi Zohar4af46622009-02-04 09:07:00 -0500486
Janne Karhunen483ec262020-01-15 17:42:30 +0200487 if (!rule->lsm[i].rule) {
488 if (!rule->lsm[i].args_p)
489 continue;
490 else
491 return false;
492 }
Mimi Zohar4af46622009-02-04 09:07:00 -0500493 switch (i) {
494 case LSM_OBJ_USER:
495 case LSM_OBJ_ROLE:
496 case LSM_OBJ_TYPE:
497 security_inode_getsecid(inode, &osid);
Tyler Hicksb8867ee2020-07-10 15:37:50 -0500498 rc = ima_filter_rule_match(osid, rule->lsm[i].type,
499 Audit_equal,
500 rule->lsm[i].rule);
Mimi Zohar4af46622009-02-04 09:07:00 -0500501 break;
502 case LSM_SUBJ_USER:
503 case LSM_SUBJ_ROLE:
504 case LSM_SUBJ_TYPE:
Tyler Hicksb8867ee2020-07-10 15:37:50 -0500505 rc = ima_filter_rule_match(secid, rule->lsm[i].type,
506 Audit_equal,
507 rule->lsm[i].rule);
Mimi Zohar4af46622009-02-04 09:07:00 -0500508 default:
509 break;
510 }
511 if (!rc)
512 return false;
513 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500514 return true;
515}
516
Mimi Zohard79d72e2012-12-03 17:08:11 -0500517/*
518 * In addition to knowing that we need to appraise the file in general,
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500519 * we need to differentiate between calling hooks, for hook specific rules.
Mimi Zohard79d72e2012-12-03 17:08:11 -0500520 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500521static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
Mimi Zohard79d72e2012-12-03 17:08:11 -0500522{
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500523 if (!(rule->flags & IMA_FUNC))
524 return IMA_FILE_APPRAISE;
525
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200526 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500527 case MMAP_CHECK:
528 return IMA_MMAP_APPRAISE;
529 case BPRM_CHECK:
530 return IMA_BPRM_APPRAISE;
Matthew Garrettd906c102018-01-08 13:36:20 -0800531 case CREDS_CHECK:
532 return IMA_CREDS_APPRAISE;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500533 case FILE_CHECK:
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500534 case POST_SETATTR:
Mimi Zohard79d72e2012-12-03 17:08:11 -0500535 return IMA_FILE_APPRAISE;
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500536 case MODULE_CHECK ... MAX_CHECK - 1:
537 default:
538 return IMA_READ_APPRAISE;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500539 }
540}
541
Mimi Zohar3323eec2009-02-04 09:06:58 -0500542/**
543 * ima_match_policy - decision based on LSM and other conditions
544 * @inode: pointer to an inode for which the policy decision is being made
Matthew Garrettd906c102018-01-08 13:36:20 -0800545 * @cred: pointer to a credentials structure for which the policy decision is
546 * being made
547 * @secid: LSM secid of the task to be validated
Mimi Zohar3323eec2009-02-04 09:06:58 -0500548 * @func: IMA hook identifier
549 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
Eric Richter725de7f2016-06-01 13:14:02 -0500550 * @pcr: set the pcr to extend
Matthew Garrett19453ce02019-06-19 15:46:11 -0700551 * @template_desc: the template that should be used for this rule
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800552 * @keyring: the keyring name, if given, to be used to check in the policy.
553 * keyring can be NULL if func is anything other than KEY_CHECK.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500554 *
555 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
556 * conditions.
557 *
Petko Manolov38d859f2015-12-02 17:47:54 +0200558 * Since the IMA policy may be updated multiple times we need to lock the
559 * list when walking it. Reads are many orders of magnitude more numerous
560 * than writes so ima_match_policy() is classical RCU candidate.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500561 */
Matthew Garrettd906c102018-01-08 13:36:20 -0800562int ima_match_policy(struct inode *inode, const struct cred *cred, u32 secid,
Matthew Garrett19453ce02019-06-19 15:46:11 -0700563 enum ima_hooks func, int mask, int flags, int *pcr,
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800564 struct ima_template_desc **template_desc,
565 const char *keyring)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500566{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500567 struct ima_rule_entry *entry;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500568 int action = 0, actmask = flags | (flags << 1);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500569
Mimi Zoharb36f2812019-07-19 07:16:57 -0400570 if (template_desc)
571 *template_desc = ima_template_desc_current();
572
Petko Manolov38d859f2015-12-02 17:47:54 +0200573 rcu_read_lock();
574 list_for_each_entry_rcu(entry, ima_rules, list) {
Mimi Zohar3323eec2009-02-04 09:06:58 -0500575
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500576 if (!(entry->action & actmask))
577 continue;
578
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800579 if (!ima_match_rules(entry, inode, cred, secid, func, mask,
580 keyring))
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500581 continue;
582
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300583 action |= entry->flags & IMA_ACTION_FLAGS;
584
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300585 action |= entry->action & IMA_DO_MASK;
Mimi Zoharda1b0022016-09-29 10:04:52 -0400586 if (entry->action & IMA_APPRAISE) {
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500587 action |= get_subaction(entry, func);
Mimi Zohara9a49352018-03-10 23:07:34 -0500588 action &= ~IMA_HASH;
Mimi Zohar9e670282018-02-21 11:36:32 -0500589 if (ima_fail_unverifiable_sigs)
590 action |= IMA_FAIL_UNVERIFIABLE_SIGS;
Mimi Zoharda1b0022016-09-29 10:04:52 -0400591 }
Mimi Zohard79d72e2012-12-03 17:08:11 -0500592
Mimi Zoharb36f2812019-07-19 07:16:57 -0400593
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300594 if (entry->action & IMA_DO_MASK)
595 actmask &= ~(entry->action | entry->action << 1);
596 else
597 actmask &= ~(entry->action | entry->action >> 1);
598
Eric Richter725de7f2016-06-01 13:14:02 -0500599 if ((pcr) && (entry->flags & IMA_PCR))
600 *pcr = entry->pcr;
601
Matthew Garrett19453ce02019-06-19 15:46:11 -0700602 if (template_desc && entry->template)
603 *template_desc = entry->template;
Matthew Garrett19453ce02019-06-19 15:46:11 -0700604
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500605 if (!actmask)
606 break;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500607 }
Petko Manolov38d859f2015-12-02 17:47:54 +0200608 rcu_read_unlock();
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500609
610 return action;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500611}
612
Roberto Sassua7560242014-09-12 19:35:54 +0200613/*
614 * Initialize the ima_policy_flag variable based on the currently
615 * loaded policy. Based on this flag, the decision to short circuit
616 * out of a function or not call the function in the first place
617 * can be made earlier.
618 */
619void ima_update_policy_flag(void)
620{
621 struct ima_rule_entry *entry;
622
Roberto Sassua7560242014-09-12 19:35:54 +0200623 list_for_each_entry(entry, ima_rules, list) {
624 if (entry->action & IMA_DO_MASK)
625 ima_policy_flag |= entry->action;
626 }
627
Mimi Zoharef968372018-07-13 14:06:01 -0400628 ima_appraise |= (build_ima_appraise | temp_ima_appraise);
Roberto Sassua7560242014-09-12 19:35:54 +0200629 if (!ima_appraise)
630 ima_policy_flag &= ~IMA_APPRAISE;
631}
632
Mimi Zohar6f0911a2018-04-12 00:15:22 -0400633static int ima_appraise_flag(enum ima_hooks func)
634{
635 if (func == MODULE_CHECK)
636 return IMA_APPRAISE_MODULES;
637 else if (func == FIRMWARE_CHECK)
638 return IMA_APPRAISE_FIRMWARE;
639 else if (func == POLICY_CHECK)
640 return IMA_APPRAISE_POLICY;
Mimi Zohar16c267a2018-07-13 14:05:58 -0400641 else if (func == KEXEC_KERNEL_CHECK)
642 return IMA_APPRAISE_KEXEC;
Mimi Zohar6f0911a2018-04-12 00:15:22 -0400643 return 0;
644}
645
Nayna Jainc52657d2018-10-09 23:00:35 +0530646static void add_rules(struct ima_rule_entry *entries, int count,
647 enum policy_rule_list policy_rule)
648{
649 int i = 0;
650
651 for (i = 0; i < count; i++) {
652 struct ima_rule_entry *entry;
653
654 if (policy_rule & IMA_DEFAULT_POLICY)
655 list_add_tail(&entries[i].list, &ima_default_rules);
656
657 if (policy_rule & IMA_CUSTOM_POLICY) {
658 entry = kmemdup(&entries[i], sizeof(*entry),
659 GFP_KERNEL);
660 if (!entry)
661 continue;
662
663 list_add_tail(&entry->list, &ima_policy_rules);
664 }
Krzysztof Struczynskib59fda42020-04-27 12:28:59 +0200665 if (entries[i].action == APPRAISE) {
666 if (entries != build_appraise_rules)
667 temp_ima_appraise |=
668 ima_appraise_flag(entries[i].func);
669 else
670 build_ima_appraise |=
671 ima_appraise_flag(entries[i].func);
672 }
Nayna Jainc52657d2018-10-09 23:00:35 +0530673 }
674}
675
Nayna Jain61917062018-10-09 23:00:36 +0530676static int ima_parse_rule(char *rule, struct ima_rule_entry *entry);
677
678static int __init ima_init_arch_policy(void)
679{
680 const char * const *arch_rules;
681 const char * const *rules;
682 int arch_entries = 0;
683 int i = 0;
684
685 arch_rules = arch_get_ima_policy();
686 if (!arch_rules)
687 return arch_entries;
688
689 /* Get number of rules */
690 for (rules = arch_rules; *rules != NULL; rules++)
691 arch_entries++;
692
693 arch_policy_entry = kcalloc(arch_entries + 1,
694 sizeof(*arch_policy_entry), GFP_KERNEL);
695 if (!arch_policy_entry)
696 return 0;
697
698 /* Convert each policy string rules to struct ima_rule_entry format */
699 for (rules = arch_rules, i = 0; *rules != NULL; rules++) {
700 char rule[255];
701 int result;
702
703 result = strlcpy(rule, *rules, sizeof(rule));
704
705 INIT_LIST_HEAD(&arch_policy_entry[i].list);
706 result = ima_parse_rule(rule, &arch_policy_entry[i]);
707 if (result) {
708 pr_warn("Skipping unknown architecture policy rule: %s\n",
709 rule);
710 memset(&arch_policy_entry[i], 0,
711 sizeof(*arch_policy_entry));
712 continue;
713 }
714 i++;
715 }
716 return i;
717}
718
Mimi Zohar3323eec2009-02-04 09:06:58 -0500719/**
720 * ima_init_policy - initialize the default measure rules.
721 *
Mimi Zohar07f6a792011-03-09 22:25:48 -0500722 * ima_rules points to either the ima_default_rules or the
723 * the new ima_policy_rules.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500724 */
Eric Paris932995f2009-05-21 15:43:32 -0400725void __init ima_init_policy(void)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500726{
Nayna Jain61917062018-10-09 23:00:36 +0530727 int build_appraise_entries, arch_entries;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500728
Nayna Jainc52657d2018-10-09 23:00:35 +0530729 /* if !ima_policy, we load NO default rules */
730 if (ima_policy)
731 add_rules(dont_measure_rules, ARRAY_SIZE(dont_measure_rules),
732 IMA_DEFAULT_POLICY);
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400733
734 switch (ima_policy) {
735 case ORIGINAL_TCB:
Nayna Jainc52657d2018-10-09 23:00:35 +0530736 add_rules(original_measurement_rules,
737 ARRAY_SIZE(original_measurement_rules),
738 IMA_DEFAULT_POLICY);
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400739 break;
740 case DEFAULT_TCB:
Nayna Jainc52657d2018-10-09 23:00:35 +0530741 add_rules(default_measurement_rules,
742 ARRAY_SIZE(default_measurement_rules),
743 IMA_DEFAULT_POLICY);
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400744 default:
745 break;
746 }
Eric Paris5789ba32009-05-21 15:47:06 -0400747
Mimi Zohar503ceae2017-04-21 18:58:27 -0400748 /*
Nayna Jain61917062018-10-09 23:00:36 +0530749 * Based on runtime secure boot flags, insert arch specific measurement
750 * and appraise rules requiring file signatures for both the initial
751 * and custom policies, prior to other appraise rules.
752 * (Highest priority)
Mimi Zohar503ceae2017-04-21 18:58:27 -0400753 */
Nayna Jain61917062018-10-09 23:00:36 +0530754 arch_entries = ima_init_arch_policy();
755 if (!arch_entries)
756 pr_info("No architecture policies found\n");
757 else
758 add_rules(arch_policy_entry, arch_entries,
759 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
760
761 /*
Mimi Zohar503ceae2017-04-21 18:58:27 -0400762 * Insert the builtin "secure_boot" policy rules requiring file
Nayna Jain61917062018-10-09 23:00:36 +0530763 * signatures, prior to other appraise rules.
Mimi Zohar503ceae2017-04-21 18:58:27 -0400764 */
Nayna Jainc52657d2018-10-09 23:00:35 +0530765 if (ima_use_secure_boot)
766 add_rules(secure_boot_rules, ARRAY_SIZE(secure_boot_rules),
767 IMA_DEFAULT_POLICY);
Mimi Zohar503ceae2017-04-21 18:58:27 -0400768
Mimi Zoharef968372018-07-13 14:06:01 -0400769 /*
770 * Insert the build time appraise rules requiring file signatures
771 * for both the initial and custom policies, prior to other appraise
Nayna Jainc52657d2018-10-09 23:00:35 +0530772 * rules. As the secure boot rules includes all of the build time
773 * rules, include either one or the other set of rules, but not both.
Mimi Zoharef968372018-07-13 14:06:01 -0400774 */
Nayna Jainc52657d2018-10-09 23:00:35 +0530775 build_appraise_entries = ARRAY_SIZE(build_appraise_rules);
776 if (build_appraise_entries) {
777 if (ima_use_secure_boot)
778 add_rules(build_appraise_rules, build_appraise_entries,
779 IMA_CUSTOM_POLICY);
780 else
781 add_rules(build_appraise_rules, build_appraise_entries,
782 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
Mimi Zoharef968372018-07-13 14:06:01 -0400783 }
784
Nayna Jainc52657d2018-10-09 23:00:35 +0530785 if (ima_use_appraise_tcb)
786 add_rules(default_appraise_rules,
787 ARRAY_SIZE(default_appraise_rules),
788 IMA_DEFAULT_POLICY);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500789
Mimi Zohar95ee08f2015-12-07 15:08:01 -0500790 ima_update_policy_flag();
Mimi Zohar3323eec2009-02-04 09:06:58 -0500791}
Mimi Zohar4af46622009-02-04 09:07:00 -0500792
Sasha Levin01127212015-12-22 08:51:23 -0500793/* Make sure we have a valid policy, at least containing some rules. */
Colin Ian Kingc75d8e92016-01-20 11:13:46 +0000794int ima_check_policy(void)
Sasha Levin01127212015-12-22 08:51:23 -0500795{
796 if (list_empty(&ima_temp_rules))
797 return -EINVAL;
798 return 0;
799}
800
Mimi Zohar4af46622009-02-04 09:07:00 -0500801/**
802 * ima_update_policy - update default_rules with new measure rules
803 *
804 * Called on file .release to update the default rules with a complete new
Petko Manolov38d859f2015-12-02 17:47:54 +0200805 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
806 * they make a queue. The policy may be updated multiple times and this is the
807 * RCU updater.
808 *
809 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
810 * we switch from the default policy to user defined.
Mimi Zohar4af46622009-02-04 09:07:00 -0500811 */
812void ima_update_policy(void)
813{
Petko Manolov53b626f2018-05-22 17:06:55 +0300814 struct list_head *policy = &ima_policy_rules;
Petko Manolov38d859f2015-12-02 17:47:54 +0200815
Petko Manolov53b626f2018-05-22 17:06:55 +0300816 list_splice_tail_init_rcu(&ima_temp_rules, policy, synchronize_rcu);
Petko Manolov38d859f2015-12-02 17:47:54 +0200817
818 if (ima_rules != policy) {
819 ima_policy_flag = 0;
820 ima_rules = policy;
Nayna Jain61917062018-10-09 23:00:36 +0530821
822 /*
823 * IMA architecture specific policy rules are specified
824 * as strings and converted to an array of ima_entry_rules
825 * on boot. After loading a custom policy, free the
826 * architecture specific rules stored as an array.
827 */
828 kfree(arch_policy_entry);
Petko Manolov38d859f2015-12-02 17:47:54 +0200829 }
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300830 ima_update_policy_flag();
Lakshmi Ramasubramanian450d0fd2020-01-22 17:32:05 -0800831
832 /* Custom IMA policy has been loaded */
833 ima_process_queued_keys();
Mimi Zohar4af46622009-02-04 09:07:00 -0500834}
835
Mimi Zohar1a9430d2018-12-17 19:14:49 -0500836/* Keep the enumeration in sync with the policy_tokens! */
Mimi Zohar4af46622009-02-04 09:07:00 -0500837enum {
Mimi Zohar1a9430d2018-12-17 19:14:49 -0500838 Opt_measure, Opt_dont_measure,
Mimi Zohar07f6a792011-03-09 22:25:48 -0500839 Opt_appraise, Opt_dont_appraise,
Mimi Zoharda1b0022016-09-29 10:04:52 -0400840 Opt_audit, Opt_hash, Opt_dont_hash,
Mimi Zohar4af46622009-02-04 09:07:00 -0500841 Opt_obj_user, Opt_obj_role, Opt_obj_type,
842 Opt_subj_user, Opt_subj_role, Opt_subj_type,
Mimi Zoharf1b08bb2018-01-15 11:20:36 -0500843 Opt_func, Opt_mask, Opt_fsmagic, Opt_fsname,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300844 Opt_fsuuid, Opt_uid_eq, Opt_euid_eq, Opt_fowner_eq,
845 Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt,
846 Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt,
Nayna Jain273df862019-10-30 23:31:32 -0400847 Opt_appraise_type, Opt_appraise_flag,
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -0800848 Opt_permit_directio, Opt_pcr, Opt_template, Opt_keyrings,
849 Opt_err
Mimi Zohar4af46622009-02-04 09:07:00 -0500850};
851
Mimi Zohar1a9430d2018-12-17 19:14:49 -0500852static const match_table_t policy_tokens = {
Mimi Zohar4af46622009-02-04 09:07:00 -0500853 {Opt_measure, "measure"},
854 {Opt_dont_measure, "dont_measure"},
Mimi Zohar07f6a792011-03-09 22:25:48 -0500855 {Opt_appraise, "appraise"},
856 {Opt_dont_appraise, "dont_appraise"},
Peter Moodye7c568e2012-06-14 10:04:36 -0700857 {Opt_audit, "audit"},
Mimi Zoharda1b0022016-09-29 10:04:52 -0400858 {Opt_hash, "hash"},
859 {Opt_dont_hash, "dont_hash"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500860 {Opt_obj_user, "obj_user=%s"},
861 {Opt_obj_role, "obj_role=%s"},
862 {Opt_obj_type, "obj_type=%s"},
863 {Opt_subj_user, "subj_user=%s"},
864 {Opt_subj_role, "subj_role=%s"},
865 {Opt_subj_type, "subj_type=%s"},
866 {Opt_func, "func=%s"},
867 {Opt_mask, "mask=%s"},
868 {Opt_fsmagic, "fsmagic=%s"},
Mimi Zoharf1b08bb2018-01-15 11:20:36 -0500869 {Opt_fsname, "fsname=%s"},
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300870 {Opt_fsuuid, "fsuuid=%s"},
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300871 {Opt_uid_eq, "uid=%s"},
872 {Opt_euid_eq, "euid=%s"},
873 {Opt_fowner_eq, "fowner=%s"},
874 {Opt_uid_gt, "uid>%s"},
875 {Opt_euid_gt, "euid>%s"},
876 {Opt_fowner_gt, "fowner>%s"},
877 {Opt_uid_lt, "uid<%s"},
878 {Opt_euid_lt, "euid<%s"},
879 {Opt_fowner_lt, "fowner<%s"},
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300880 {Opt_appraise_type, "appraise_type=%s"},
Nayna Jain273df862019-10-30 23:31:32 -0400881 {Opt_appraise_flag, "appraise_flag=%s"},
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400882 {Opt_permit_directio, "permit_directio"},
Eric Richter02606432016-06-01 13:14:01 -0500883 {Opt_pcr, "pcr=%s"},
Matthew Garrett19453ce02019-06-19 15:46:11 -0700884 {Opt_template, "template=%s"},
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -0800885 {Opt_keyrings, "keyrings=%s"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500886 {Opt_err, NULL}
887};
888
Mimi Zohar07f6a792011-03-09 22:25:48 -0500889static int ima_lsm_rule_init(struct ima_rule_entry *entry,
Mimi Zohar7163a992013-01-03 14:19:09 -0500890 substring_t *args, int lsm_rule, int audit_type)
Mimi Zohar4af46622009-02-04 09:07:00 -0500891{
892 int result;
893
Eric Paris7b62e162010-04-20 10:21:01 -0400894 if (entry->lsm[lsm_rule].rule)
895 return -EINVAL;
896
Mimi Zohar7163a992013-01-03 14:19:09 -0500897 entry->lsm[lsm_rule].args_p = match_strdup(args);
898 if (!entry->lsm[lsm_rule].args_p)
899 return -ENOMEM;
900
Mimi Zohar4af46622009-02-04 09:07:00 -0500901 entry->lsm[lsm_rule].type = audit_type;
Tyler Hicksb8867ee2020-07-10 15:37:50 -0500902 result = ima_filter_rule_init(entry->lsm[lsm_rule].type, Audit_equal,
903 entry->lsm[lsm_rule].args_p,
904 &entry->lsm[lsm_rule].rule);
Mimi Zohar7163a992013-01-03 14:19:09 -0500905 if (!entry->lsm[lsm_rule].rule) {
Janne Karhunen483ec262020-01-15 17:42:30 +0200906 pr_warn("rule for LSM \'%s\' is undefined\n",
Tyler Hicksaa0c0222020-07-09 01:19:08 -0500907 entry->lsm[lsm_rule].args_p);
Janne Karhunen483ec262020-01-15 17:42:30 +0200908
909 if (ima_rules == &ima_default_rules) {
910 kfree(entry->lsm[lsm_rule].args_p);
Tyler Hicks2bdd7372020-07-09 01:19:02 -0500911 entry->lsm[lsm_rule].args_p = NULL;
Janne Karhunen483ec262020-01-15 17:42:30 +0200912 result = -EINVAL;
913 } else
914 result = 0;
Mimi Zohar7163a992013-01-03 14:19:09 -0500915 }
916
Mimi Zohar4af46622009-02-04 09:07:00 -0500917 return result;
918}
919
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300920static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value,
921 bool (*rule_operator)(kuid_t, kuid_t))
Eric Paris2f1506c2010-04-20 10:21:30 -0400922{
Stefan Berger2afd0202018-06-04 16:54:54 -0400923 if (!ab)
924 return;
925
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300926 if (rule_operator == &uid_gt)
927 audit_log_format(ab, "%s>", key);
928 else if (rule_operator == &uid_lt)
929 audit_log_format(ab, "%s<", key);
930 else
931 audit_log_format(ab, "%s=", key);
Stefan Berger3d2859d2018-06-04 16:54:53 -0400932 audit_log_format(ab, "%s ", value);
Eric Paris2f1506c2010-04-20 10:21:30 -0400933}
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300934static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
935{
936 ima_log_string_op(ab, key, value, NULL);
937}
Eric Paris2f1506c2010-04-20 10:21:30 -0400938
Thiago Jung Bauermann3878d502019-06-27 23:19:32 -0300939/*
940 * Validating the appended signature included in the measurement list requires
941 * the file hash calculated without the appended signature (i.e., the 'd-modsig'
942 * field). Therefore, notify the user if they have the 'modsig' field but not
943 * the 'd-modsig' field in the template.
944 */
945static void check_template_modsig(const struct ima_template_desc *template)
946{
947#define MSG "template with 'modsig' field also needs 'd-modsig' field\n"
948 bool has_modsig, has_dmodsig;
949 static bool checked;
950 int i;
951
952 /* We only need to notify the user once. */
953 if (checked)
954 return;
955
956 has_modsig = has_dmodsig = false;
957 for (i = 0; i < template->num_fields; i++) {
958 if (!strcmp(template->fields[i]->field_id, "modsig"))
959 has_modsig = true;
960 else if (!strcmp(template->fields[i]->field_id, "d-modsig"))
961 has_dmodsig = true;
962 }
963
964 if (has_modsig && !has_dmodsig)
965 pr_notice(MSG);
966
967 checked = true;
968#undef MSG
969}
970
Tyler Hicks712183432020-07-09 01:19:03 -0500971static bool ima_validate_rule(struct ima_rule_entry *entry)
972{
Tyler Hicks30031b02020-07-09 01:19:09 -0500973 /* Ensure that the action is set and is compatible with the flags */
Tyler Hicks712183432020-07-09 01:19:03 -0500974 if (entry->action == UNKNOWN)
975 return false;
976
Tyler Hicks30031b02020-07-09 01:19:09 -0500977 if (entry->action != MEASURE && entry->flags & IMA_PCR)
978 return false;
979
980 if (entry->action != APPRAISE &&
981 entry->flags & (IMA_DIGSIG_REQUIRED | IMA_MODSIG_ALLOWED | IMA_CHECK_BLACKLIST))
982 return false;
983
984 /*
985 * The IMA_FUNC bit must be set if and only if there's a valid hook
986 * function specified, and vice versa. Enforcing this property allows
987 * for the NONE case below to validate a rule without an explicit hook
988 * function.
989 */
990 if (((entry->flags & IMA_FUNC) && entry->func == NONE) ||
991 (!(entry->flags & IMA_FUNC) && entry->func != NONE))
992 return false;
993
Tyler Hicks712183432020-07-09 01:19:03 -0500994 /*
995 * Ensure that the hook function is compatible with the other
996 * components of the rule
997 */
998 switch (entry->func) {
999 case NONE:
1000 case FILE_CHECK:
1001 case MMAP_CHECK:
1002 case BPRM_CHECK:
1003 case CREDS_CHECK:
1004 case POST_SETATTR:
Tyler Hicks712183432020-07-09 01:19:03 -05001005 case FIRMWARE_CHECK:
Tyler Hicks30031b02020-07-09 01:19:09 -05001006 case POLICY_CHECK:
1007 if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
1008 IMA_UID | IMA_FOWNER | IMA_FSUUID |
1009 IMA_INMASK | IMA_EUID | IMA_PCR |
1010 IMA_FSNAME | IMA_DIGSIG_REQUIRED |
1011 IMA_PERMIT_DIRECTIO))
1012 return false;
1013
1014 break;
1015 case MODULE_CHECK:
Tyler Hicks712183432020-07-09 01:19:03 -05001016 case KEXEC_KERNEL_CHECK:
1017 case KEXEC_INITRAMFS_CHECK:
Tyler Hicks30031b02020-07-09 01:19:09 -05001018 if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
1019 IMA_UID | IMA_FOWNER | IMA_FSUUID |
1020 IMA_INMASK | IMA_EUID | IMA_PCR |
1021 IMA_FSNAME | IMA_DIGSIG_REQUIRED |
1022 IMA_PERMIT_DIRECTIO | IMA_MODSIG_ALLOWED |
1023 IMA_CHECK_BLACKLIST))
1024 return false;
1025
Tyler Hicks712183432020-07-09 01:19:03 -05001026 break;
1027 case KEXEC_CMDLINE:
Tyler Hicksdb2045f2020-07-09 01:19:04 -05001028 if (entry->action & ~(MEASURE | DONT_MEASURE))
1029 return false;
1030
Tyler Hicks48341772020-07-09 01:19:11 -05001031 if (entry->flags & ~(IMA_FUNC | IMA_FSMAGIC | IMA_UID |
1032 IMA_FOWNER | IMA_FSUUID | IMA_EUID |
1033 IMA_PCR | IMA_FSNAME))
Tyler Hicksdb2045f2020-07-09 01:19:04 -05001034 return false;
1035
1036 break;
Tyler Hicks712183432020-07-09 01:19:03 -05001037 case KEY_CHECK:
1038 if (entry->action & ~(MEASURE | DONT_MEASURE))
1039 return false;
1040
Tyler Hickseb624fe2020-07-09 01:19:05 -05001041 if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_PCR |
1042 IMA_KEYRINGS))
1043 return false;
1044
1045 if (ima_rule_contains_lsm_cond(entry))
1046 return false;
1047
Tyler Hicks712183432020-07-09 01:19:03 -05001048 break;
1049 default:
1050 return false;
1051 }
1052
Tyler Hicks5f3e9262020-07-09 01:19:06 -05001053 /* Ensure that combinations of flags are compatible with each other */
1054 if (entry->flags & IMA_CHECK_BLACKLIST &&
1055 !(entry->flags & IMA_MODSIG_ALLOWED))
1056 return false;
1057
Tyler Hicks712183432020-07-09 01:19:03 -05001058 return true;
1059}
1060
Mimi Zohar07f6a792011-03-09 22:25:48 -05001061static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
Mimi Zohar4af46622009-02-04 09:07:00 -05001062{
1063 struct audit_buffer *ab;
Mimi Zohar4351c292014-11-05 07:53:55 -05001064 char *from;
Mimi Zohar4af46622009-02-04 09:07:00 -05001065 char *p;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001066 bool uid_token;
Matthew Garrett19453ce02019-06-19 15:46:11 -07001067 struct ima_template_desc *template_desc;
Mimi Zohar4af46622009-02-04 09:07:00 -05001068 int result = 0;
Lakshmi Ramasubramanian5c7bac92020-01-16 18:18:21 -08001069 size_t keyrings_len;
Mimi Zohar4af46622009-02-04 09:07:00 -05001070
Stefan Bergerdba31ee2018-06-04 16:54:55 -04001071 ab = integrity_audit_log_start(audit_context(), GFP_KERNEL,
1072 AUDIT_INTEGRITY_POLICY_RULE);
Mimi Zohar4af46622009-02-04 09:07:00 -05001073
Eric W. Biederman8b94eea2012-05-25 18:24:12 -06001074 entry->uid = INVALID_UID;
Linus Torvalds882653222012-10-02 21:38:48 -07001075 entry->fowner = INVALID_UID;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001076 entry->uid_op = &uid_eq;
1077 entry->fowner_op = &uid_eq;
Eric Parisb9035b12010-04-20 10:21:07 -04001078 entry->action = UNKNOWN;
Eric Paris28ef4002010-04-20 10:21:18 -04001079 while ((p = strsep(&rule, " \t")) != NULL) {
Mimi Zohar4af46622009-02-04 09:07:00 -05001080 substring_t args[MAX_OPT_ARGS];
1081 int token;
1082 unsigned long lnum;
1083
1084 if (result < 0)
1085 break;
Eric Paris28ef4002010-04-20 10:21:18 -04001086 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
1087 continue;
Mimi Zohar4af46622009-02-04 09:07:00 -05001088 token = match_token(p, policy_tokens, args);
1089 switch (token) {
1090 case Opt_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -04001091 ima_log_string(ab, "action", "measure");
Eric Paris7b62e162010-04-20 10:21:01 -04001092
1093 if (entry->action != UNKNOWN)
1094 result = -EINVAL;
1095
Mimi Zohar4af46622009-02-04 09:07:00 -05001096 entry->action = MEASURE;
1097 break;
1098 case Opt_dont_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -04001099 ima_log_string(ab, "action", "dont_measure");
Eric Paris7b62e162010-04-20 10:21:01 -04001100
1101 if (entry->action != UNKNOWN)
1102 result = -EINVAL;
1103
Mimi Zohar4af46622009-02-04 09:07:00 -05001104 entry->action = DONT_MEASURE;
1105 break;
Mimi Zohar07f6a792011-03-09 22:25:48 -05001106 case Opt_appraise:
1107 ima_log_string(ab, "action", "appraise");
1108
1109 if (entry->action != UNKNOWN)
1110 result = -EINVAL;
1111
1112 entry->action = APPRAISE;
1113 break;
1114 case Opt_dont_appraise:
1115 ima_log_string(ab, "action", "dont_appraise");
1116
1117 if (entry->action != UNKNOWN)
1118 result = -EINVAL;
1119
1120 entry->action = DONT_APPRAISE;
1121 break;
Peter Moodye7c568e2012-06-14 10:04:36 -07001122 case Opt_audit:
1123 ima_log_string(ab, "action", "audit");
1124
1125 if (entry->action != UNKNOWN)
1126 result = -EINVAL;
1127
1128 entry->action = AUDIT;
1129 break;
Mimi Zoharda1b0022016-09-29 10:04:52 -04001130 case Opt_hash:
1131 ima_log_string(ab, "action", "hash");
1132
1133 if (entry->action != UNKNOWN)
1134 result = -EINVAL;
1135
1136 entry->action = HASH;
1137 break;
1138 case Opt_dont_hash:
1139 ima_log_string(ab, "action", "dont_hash");
1140
1141 if (entry->action != UNKNOWN)
1142 result = -EINVAL;
1143
1144 entry->action = DONT_HASH;
1145 break;
Mimi Zohar4af46622009-02-04 09:07:00 -05001146 case Opt_func:
Eric Paris2f1506c2010-04-20 10:21:30 -04001147 ima_log_string(ab, "func", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -04001148
1149 if (entry->func)
Mimi Zohar07f6a792011-03-09 22:25:48 -05001150 result = -EINVAL;
Eric Paris7b62e162010-04-20 10:21:01 -04001151
Mimi Zohar1e93d002010-01-26 17:02:41 -05001152 if (strcmp(args[0].from, "FILE_CHECK") == 0)
1153 entry->func = FILE_CHECK;
1154 /* PATH_CHECK is for backwards compat */
1155 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
1156 entry->func = FILE_CHECK;
Mimi Zoharfdf90722012-10-16 12:40:08 +10301157 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
1158 entry->func = MODULE_CHECK;
Mimi Zohar5a9196d2014-07-22 10:39:48 -04001159 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
1160 entry->func = FIRMWARE_CHECK;
Mimi Zohar16cac492012-12-13 11:15:04 -05001161 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
1162 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
1163 entry->func = MMAP_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -05001164 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
1165 entry->func = BPRM_CHECK;
Matthew Garrettd906c102018-01-08 13:36:20 -08001166 else if (strcmp(args[0].from, "CREDS_CHECK") == 0)
1167 entry->func = CREDS_CHECK;
Mimi Zohard9ddf072016-01-14 20:59:14 -05001168 else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
1169 0)
1170 entry->func = KEXEC_KERNEL_CHECK;
1171 else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
1172 == 0)
1173 entry->func = KEXEC_INITRAMFS_CHECK;
Mimi Zohar19f8a842016-01-15 10:17:12 -05001174 else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
1175 entry->func = POLICY_CHECK;
Prakhar Srivastavab0935122019-06-23 23:23:29 -07001176 else if (strcmp(args[0].from, "KEXEC_CMDLINE") == 0)
1177 entry->func = KEXEC_CMDLINE;
Lakshmi Ramasubramanian58086112019-12-11 08:47:03 -08001178 else if (strcmp(args[0].from, "KEY_CHECK") == 0)
1179 entry->func = KEY_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -05001180 else
1181 result = -EINVAL;
1182 if (!result)
1183 entry->flags |= IMA_FUNC;
1184 break;
1185 case Opt_mask:
Eric Paris2f1506c2010-04-20 10:21:30 -04001186 ima_log_string(ab, "mask", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -04001187
1188 if (entry->mask)
1189 result = -EINVAL;
1190
Mimi Zohar4351c292014-11-05 07:53:55 -05001191 from = args[0].from;
1192 if (*from == '^')
1193 from++;
1194
1195 if ((strcmp(from, "MAY_EXEC")) == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -05001196 entry->mask = MAY_EXEC;
Mimi Zohar4351c292014-11-05 07:53:55 -05001197 else if (strcmp(from, "MAY_WRITE") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -05001198 entry->mask = MAY_WRITE;
Mimi Zohar4351c292014-11-05 07:53:55 -05001199 else if (strcmp(from, "MAY_READ") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -05001200 entry->mask = MAY_READ;
Mimi Zohar4351c292014-11-05 07:53:55 -05001201 else if (strcmp(from, "MAY_APPEND") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -05001202 entry->mask = MAY_APPEND;
1203 else
1204 result = -EINVAL;
1205 if (!result)
Mimi Zohar4351c292014-11-05 07:53:55 -05001206 entry->flags |= (*args[0].from == '^')
1207 ? IMA_INMASK : IMA_MASK;
Mimi Zohar4af46622009-02-04 09:07:00 -05001208 break;
1209 case Opt_fsmagic:
Eric Paris2f1506c2010-04-20 10:21:30 -04001210 ima_log_string(ab, "fsmagic", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -04001211
1212 if (entry->fsmagic) {
1213 result = -EINVAL;
1214 break;
1215 }
1216
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +02001217 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
Mimi Zohar4af46622009-02-04 09:07:00 -05001218 if (!result)
1219 entry->flags |= IMA_FSMAGIC;
1220 break;
Mimi Zoharf1b08bb2018-01-15 11:20:36 -05001221 case Opt_fsname:
1222 ima_log_string(ab, "fsname", args[0].from);
1223
1224 entry->fsname = kstrdup(args[0].from, GFP_KERNEL);
1225 if (!entry->fsname) {
1226 result = -ENOMEM;
1227 break;
1228 }
1229 result = 0;
1230 entry->flags |= IMA_FSNAME;
1231 break;
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -08001232 case Opt_keyrings:
1233 ima_log_string(ab, "keyrings", args[0].from);
1234
Lakshmi Ramasubramanian5c7bac92020-01-16 18:18:21 -08001235 keyrings_len = strlen(args[0].from) + 1;
1236
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -08001237 if ((entry->keyrings) ||
Lakshmi Ramasubramanian5c7bac92020-01-16 18:18:21 -08001238 (keyrings_len < 2)) {
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -08001239 result = -EINVAL;
1240 break;
1241 }
Lakshmi Ramasubramanian5c7bac92020-01-16 18:18:21 -08001242
1243 if (keyrings_len > ima_keyrings_len) {
1244 char *tmpbuf;
1245
1246 tmpbuf = krealloc(ima_keyrings, keyrings_len,
1247 GFP_KERNEL);
1248 if (!tmpbuf) {
1249 result = -ENOMEM;
1250 break;
1251 }
1252
1253 ima_keyrings = tmpbuf;
1254 ima_keyrings_len = keyrings_len;
1255 }
1256
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -08001257 entry->keyrings = kstrdup(args[0].from, GFP_KERNEL);
1258 if (!entry->keyrings) {
Lakshmi Ramasubramanian5c7bac92020-01-16 18:18:21 -08001259 kfree(ima_keyrings);
1260 ima_keyrings = NULL;
1261 ima_keyrings_len = 0;
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -08001262 result = -ENOMEM;
1263 break;
1264 }
1265 result = 0;
1266 entry->flags |= IMA_KEYRINGS;
1267 break;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +03001268 case Opt_fsuuid:
1269 ima_log_string(ab, "fsuuid", args[0].from);
1270
Mike Rapoport36447452018-01-17 20:27:11 +02001271 if (!uuid_is_null(&entry->fsuuid)) {
Dmitry Kasatkin85865c12012-09-03 23:23:13 +03001272 result = -EINVAL;
1273 break;
1274 }
1275
Christoph Hellwig787d8c52017-06-01 07:00:26 +02001276 result = uuid_parse(args[0].from, &entry->fsuuid);
Mimi Zohar446d64e2013-02-24 23:42:37 -05001277 if (!result)
1278 entry->flags |= IMA_FSUUID;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +03001279 break;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001280 case Opt_uid_gt:
1281 case Opt_euid_gt:
1282 entry->uid_op = &uid_gt;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001283 fallthrough;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001284 case Opt_uid_lt:
1285 case Opt_euid_lt:
1286 if ((token == Opt_uid_lt) || (token == Opt_euid_lt))
1287 entry->uid_op = &uid_lt;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001288 fallthrough;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001289 case Opt_uid_eq:
1290 case Opt_euid_eq:
1291 uid_token = (token == Opt_uid_eq) ||
1292 (token == Opt_uid_gt) ||
1293 (token == Opt_uid_lt);
1294
1295 ima_log_string_op(ab, uid_token ? "uid" : "euid",
1296 args[0].from, entry->uid_op);
Eric Paris7b62e162010-04-20 10:21:01 -04001297
Eric W. Biederman8b94eea2012-05-25 18:24:12 -06001298 if (uid_valid(entry->uid)) {
Eric Paris7b62e162010-04-20 10:21:01 -04001299 result = -EINVAL;
1300 break;
1301 }
1302
Jingoo Han29707b22014-02-05 15:13:14 +09001303 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar4af46622009-02-04 09:07:00 -05001304 if (!result) {
Mimi Zohar139069e2014-11-05 07:48:36 -05001305 entry->uid = make_kuid(current_user_ns(),
1306 (uid_t) lnum);
1307 if (!uid_valid(entry->uid) ||
1308 (uid_t)lnum != lnum)
Mimi Zohar4af46622009-02-04 09:07:00 -05001309 result = -EINVAL;
1310 else
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001311 entry->flags |= uid_token
Mimi Zohar139069e2014-11-05 07:48:36 -05001312 ? IMA_UID : IMA_EUID;
Mimi Zohar4af46622009-02-04 09:07:00 -05001313 }
1314 break;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001315 case Opt_fowner_gt:
1316 entry->fowner_op = &uid_gt;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001317 fallthrough;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001318 case Opt_fowner_lt:
1319 if (token == Opt_fowner_lt)
1320 entry->fowner_op = &uid_lt;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001321 fallthrough;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001322 case Opt_fowner_eq:
1323 ima_log_string_op(ab, "fowner", args[0].from,
1324 entry->fowner_op);
Mimi Zohar07f6a792011-03-09 22:25:48 -05001325
Linus Torvalds882653222012-10-02 21:38:48 -07001326 if (uid_valid(entry->fowner)) {
Mimi Zohar07f6a792011-03-09 22:25:48 -05001327 result = -EINVAL;
1328 break;
1329 }
1330
Jingoo Han29707b22014-02-05 15:13:14 +09001331 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar07f6a792011-03-09 22:25:48 -05001332 if (!result) {
Linus Torvalds882653222012-10-02 21:38:48 -07001333 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
1334 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
Mimi Zohar07f6a792011-03-09 22:25:48 -05001335 result = -EINVAL;
1336 else
1337 entry->flags |= IMA_FOWNER;
1338 }
1339 break;
Mimi Zohar4af46622009-02-04 09:07:00 -05001340 case Opt_obj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -04001341 ima_log_string(ab, "obj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -05001342 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -05001343 LSM_OBJ_USER,
1344 AUDIT_OBJ_USER);
1345 break;
1346 case Opt_obj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -04001347 ima_log_string(ab, "obj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -05001348 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -05001349 LSM_OBJ_ROLE,
1350 AUDIT_OBJ_ROLE);
1351 break;
1352 case Opt_obj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -04001353 ima_log_string(ab, "obj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -05001354 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -05001355 LSM_OBJ_TYPE,
1356 AUDIT_OBJ_TYPE);
1357 break;
1358 case Opt_subj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -04001359 ima_log_string(ab, "subj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -05001360 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -05001361 LSM_SUBJ_USER,
1362 AUDIT_SUBJ_USER);
1363 break;
1364 case Opt_subj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -04001365 ima_log_string(ab, "subj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -05001366 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -05001367 LSM_SUBJ_ROLE,
1368 AUDIT_SUBJ_ROLE);
1369 break;
1370 case Opt_subj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -04001371 ima_log_string(ab, "subj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -05001372 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -05001373 LSM_SUBJ_TYPE,
1374 AUDIT_SUBJ_TYPE);
1375 break;
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +03001376 case Opt_appraise_type:
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +03001377 ima_log_string(ab, "appraise_type", args[0].from);
1378 if ((strcmp(args[0].from, "imasig")) == 0)
1379 entry->flags |= IMA_DIGSIG_REQUIRED;
Tyler Hicks30031b02020-07-09 01:19:09 -05001380 else if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
Thiago Jung Bauermann9044d622019-06-27 23:19:28 -03001381 strcmp(args[0].from, "imasig|modsig") == 0)
1382 entry->flags |= IMA_DIGSIG_REQUIRED |
1383 IMA_MODSIG_ALLOWED;
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +03001384 else
1385 result = -EINVAL;
1386 break;
Nayna Jain273df862019-10-30 23:31:32 -04001387 case Opt_appraise_flag:
1388 ima_log_string(ab, "appraise_flag", args[0].from);
Tyler Hicks5f3e9262020-07-09 01:19:06 -05001389 if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
1390 strstr(args[0].from, "blacklist"))
Nayna Jain273df862019-10-30 23:31:32 -04001391 entry->flags |= IMA_CHECK_BLACKLIST;
Tyler Hicks5f3e9262020-07-09 01:19:06 -05001392 else
1393 result = -EINVAL;
Nayna Jain273df862019-10-30 23:31:32 -04001394 break;
Mimi Zoharf9b2a732014-05-12 09:28:11 -04001395 case Opt_permit_directio:
1396 entry->flags |= IMA_PERMIT_DIRECTIO;
1397 break;
Eric Richter02606432016-06-01 13:14:01 -05001398 case Opt_pcr:
Eric Richter02606432016-06-01 13:14:01 -05001399 ima_log_string(ab, "pcr", args[0].from);
1400
1401 result = kstrtoint(args[0].from, 10, &entry->pcr);
1402 if (result || INVALID_PCR(entry->pcr))
1403 result = -EINVAL;
1404 else
1405 entry->flags |= IMA_PCR;
1406
1407 break;
Matthew Garrett19453ce02019-06-19 15:46:11 -07001408 case Opt_template:
1409 ima_log_string(ab, "template", args[0].from);
1410 if (entry->action != MEASURE) {
1411 result = -EINVAL;
1412 break;
1413 }
1414 template_desc = lookup_template_desc(args[0].from);
1415 if (!template_desc || entry->template) {
1416 result = -EINVAL;
1417 break;
1418 }
1419
1420 /*
1421 * template_desc_init_fields() does nothing if
1422 * the template is already initialised, so
1423 * it's safe to do this unconditionally
1424 */
1425 template_desc_init_fields(template_desc->fmt,
1426 &(template_desc->fields),
1427 &(template_desc->num_fields));
1428 entry->template = template_desc;
1429 break;
Mimi Zohar4af46622009-02-04 09:07:00 -05001430 case Opt_err:
Eric Paris2f1506c2010-04-20 10:21:30 -04001431 ima_log_string(ab, "UNKNOWN", p);
Eric Parise9d393b2010-04-20 10:21:13 -04001432 result = -EINVAL;
Mimi Zohar4af46622009-02-04 09:07:00 -05001433 break;
1434 }
1435 }
Tyler Hicks712183432020-07-09 01:19:03 -05001436 if (!result && !ima_validate_rule(entry))
Mimi Zohar4af46622009-02-04 09:07:00 -05001437 result = -EINVAL;
Mimi Zohar6f0911a2018-04-12 00:15:22 -04001438 else if (entry->action == APPRAISE)
1439 temp_ima_appraise |= ima_appraise_flag(entry->func);
1440
Thiago Jung Bauermann3878d502019-06-27 23:19:32 -03001441 if (!result && entry->flags & IMA_MODSIG_ALLOWED) {
1442 template_desc = entry->template ? entry->template :
1443 ima_template_desc_current();
1444 check_template_modsig(template_desc);
1445 }
1446
Eric Parisb0d5de42012-02-14 17:11:07 -05001447 audit_log_format(ab, "res=%d", !result);
Mimi Zohar4af46622009-02-04 09:07:00 -05001448 audit_log_end(ab);
1449 return result;
1450}
1451
1452/**
Mimi Zohar07f6a792011-03-09 22:25:48 -05001453 * ima_parse_add_rule - add a rule to ima_policy_rules
Mimi Zohar4af46622009-02-04 09:07:00 -05001454 * @rule - ima measurement policy rule
1455 *
Petko Manolov38d859f2015-12-02 17:47:54 +02001456 * Avoid locking by allowing just one writer at a time in ima_write_policy()
Eric Paris6ccd0452010-04-20 10:20:54 -04001457 * Returns the length of the rule parsed, an error code on failure
Mimi Zohar4af46622009-02-04 09:07:00 -05001458 */
Eric Paris6ccd0452010-04-20 10:20:54 -04001459ssize_t ima_parse_add_rule(char *rule)
Mimi Zohar4af46622009-02-04 09:07:00 -05001460{
Mimi Zohar52a13282013-12-11 14:44:04 -05001461 static const char op[] = "update_policy";
Eric Paris6ccd0452010-04-20 10:20:54 -04001462 char *p;
Mimi Zohar07f6a792011-03-09 22:25:48 -05001463 struct ima_rule_entry *entry;
Eric Paris6ccd0452010-04-20 10:20:54 -04001464 ssize_t result, len;
Mimi Zohar4af46622009-02-04 09:07:00 -05001465 int audit_info = 0;
1466
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +03001467 p = strsep(&rule, "\n");
1468 len = strlen(p) + 1;
Dmitry Kasatkin71787842014-10-03 14:40:20 +03001469 p += strspn(p, " \t");
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +03001470
Dmitry Kasatkin71787842014-10-03 14:40:20 +03001471 if (*p == '#' || *p == '\0')
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +03001472 return len;
1473
Mimi Zohar4af46622009-02-04 09:07:00 -05001474 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1475 if (!entry) {
1476 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1477 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
1478 return -ENOMEM;
1479 }
1480
1481 INIT_LIST_HEAD(&entry->list);
1482
Eric Paris6ccd0452010-04-20 10:20:54 -04001483 result = ima_parse_rule(p, entry);
Eric Paris7233e3e2010-04-20 10:21:24 -04001484 if (result) {
Tyler Hicks2bdd7372020-07-09 01:19:02 -05001485 ima_free_rule(entry);
Mimi Zohar523979a2009-02-11 11:12:28 -05001486 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
Richard Guy Briggs7e9001f2014-06-16 15:52:07 -04001487 NULL, op, "invalid-policy", result,
Mimi Zohar523979a2009-02-11 11:12:28 -05001488 audit_info);
Eric Paris7233e3e2010-04-20 10:21:24 -04001489 return result;
Mimi Zohar523979a2009-02-11 11:12:28 -05001490 }
Eric Paris7233e3e2010-04-20 10:21:24 -04001491
Petko Manolov38d859f2015-12-02 17:47:54 +02001492 list_add_tail(&entry->list, &ima_temp_rules);
Eric Paris7233e3e2010-04-20 10:21:24 -04001493
1494 return len;
Mimi Zohar4af46622009-02-04 09:07:00 -05001495}
1496
Petko Manolov38d859f2015-12-02 17:47:54 +02001497/**
1498 * ima_delete_rules() called to cleanup invalid in-flight policy.
1499 * We don't need locking as we operate on the temp list, which is
1500 * different from the active one. There is also only one user of
1501 * ima_delete_rules() at a time.
1502 */
James Morris64c61d82009-02-05 09:28:26 +11001503void ima_delete_rules(void)
Mimi Zohar4af46622009-02-04 09:07:00 -05001504{
Mimi Zohar07f6a792011-03-09 22:25:48 -05001505 struct ima_rule_entry *entry, *tmp;
Mimi Zohar4af46622009-02-04 09:07:00 -05001506
Mimi Zohar6ad6afa2015-12-07 14:35:47 -05001507 temp_ima_appraise = 0;
Petko Manolov38d859f2015-12-02 17:47:54 +02001508 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
Mimi Zohar4af46622009-02-04 09:07:00 -05001509 list_del(&entry->list);
Tyler Hicks465aee72020-07-09 01:19:01 -05001510 ima_free_rule(entry);
Mimi Zohar4af46622009-02-04 09:07:00 -05001511 }
Mimi Zohar4af46622009-02-04 09:07:00 -05001512}
Petko Manolov80eae202015-12-02 17:47:56 +02001513
Lakshmi Ramasubramanian34e980b2020-06-18 14:10:12 -07001514#define __ima_hook_stringify(func, str) (#func),
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -03001515
1516const char *const func_tokens[] = {
1517 __ima_hooks(__ima_hook_stringify)
1518};
1519
Petko Manolov80eae202015-12-02 17:47:56 +02001520#ifdef CONFIG_IMA_READ_POLICY
1521enum {
1522 mask_exec = 0, mask_write, mask_read, mask_append
1523};
1524
Thiago Jung Bauermannbb543e32017-06-07 22:49:10 -03001525static const char *const mask_tokens[] = {
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02001526 "^MAY_EXEC",
1527 "^MAY_WRITE",
1528 "^MAY_READ",
1529 "^MAY_APPEND"
Petko Manolov80eae202015-12-02 17:47:56 +02001530};
1531
Petko Manolov80eae202015-12-02 17:47:56 +02001532void *ima_policy_start(struct seq_file *m, loff_t *pos)
1533{
1534 loff_t l = *pos;
1535 struct ima_rule_entry *entry;
1536
1537 rcu_read_lock();
1538 list_for_each_entry_rcu(entry, ima_rules, list) {
1539 if (!l--) {
1540 rcu_read_unlock();
1541 return entry;
1542 }
1543 }
1544 rcu_read_unlock();
1545 return NULL;
1546}
1547
1548void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
1549{
1550 struct ima_rule_entry *entry = v;
1551
1552 rcu_read_lock();
1553 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
1554 rcu_read_unlock();
1555 (*pos)++;
1556
1557 return (&entry->list == ima_rules) ? NULL : entry;
1558}
1559
1560void ima_policy_stop(struct seq_file *m, void *v)
1561{
1562}
1563
Mimi Zohar1a9430d2018-12-17 19:14:49 -05001564#define pt(token) policy_tokens[token].pattern
Petko Manolov80eae202015-12-02 17:47:56 +02001565#define mt(token) mask_tokens[token]
Petko Manolov80eae202015-12-02 17:47:56 +02001566
Mimi Zoharb5269ab2016-01-28 13:10:36 -05001567/*
1568 * policy_func_show - display the ima_hooks policy rule
1569 */
1570static void policy_func_show(struct seq_file *m, enum ima_hooks func)
1571{
Thiago Jung Bauermann26632182017-06-07 22:49:11 -03001572 if (func > 0 && func < MAX_CHECK)
1573 seq_printf(m, "func=%s ", func_tokens[func]);
1574 else
1575 seq_printf(m, "func=%d ", func);
Mimi Zoharb5269ab2016-01-28 13:10:36 -05001576}
1577
Petko Manolov80eae202015-12-02 17:47:56 +02001578int ima_policy_show(struct seq_file *m, void *v)
1579{
1580 struct ima_rule_entry *entry = v;
Andy Shevchenkob8b57272016-05-20 17:00:57 -07001581 int i;
Petko Manolov80eae202015-12-02 17:47:56 +02001582 char tbuf[64] = {0,};
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02001583 int offset = 0;
Petko Manolov80eae202015-12-02 17:47:56 +02001584
1585 rcu_read_lock();
1586
1587 if (entry->action & MEASURE)
1588 seq_puts(m, pt(Opt_measure));
1589 if (entry->action & DONT_MEASURE)
1590 seq_puts(m, pt(Opt_dont_measure));
1591 if (entry->action & APPRAISE)
1592 seq_puts(m, pt(Opt_appraise));
1593 if (entry->action & DONT_APPRAISE)
1594 seq_puts(m, pt(Opt_dont_appraise));
1595 if (entry->action & AUDIT)
1596 seq_puts(m, pt(Opt_audit));
Mimi Zoharda1b0022016-09-29 10:04:52 -04001597 if (entry->action & HASH)
1598 seq_puts(m, pt(Opt_hash));
1599 if (entry->action & DONT_HASH)
1600 seq_puts(m, pt(Opt_dont_hash));
Petko Manolov80eae202015-12-02 17:47:56 +02001601
1602 seq_puts(m, " ");
1603
Mimi Zoharb5269ab2016-01-28 13:10:36 -05001604 if (entry->flags & IMA_FUNC)
1605 policy_func_show(m, entry->func);
Petko Manolov80eae202015-12-02 17:47:56 +02001606
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02001607 if ((entry->flags & IMA_MASK) || (entry->flags & IMA_INMASK)) {
1608 if (entry->flags & IMA_MASK)
1609 offset = 1;
Petko Manolov80eae202015-12-02 17:47:56 +02001610 if (entry->mask & MAY_EXEC)
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02001611 seq_printf(m, pt(Opt_mask), mt(mask_exec) + offset);
Petko Manolov80eae202015-12-02 17:47:56 +02001612 if (entry->mask & MAY_WRITE)
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02001613 seq_printf(m, pt(Opt_mask), mt(mask_write) + offset);
Petko Manolov80eae202015-12-02 17:47:56 +02001614 if (entry->mask & MAY_READ)
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02001615 seq_printf(m, pt(Opt_mask), mt(mask_read) + offset);
Petko Manolov80eae202015-12-02 17:47:56 +02001616 if (entry->mask & MAY_APPEND)
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02001617 seq_printf(m, pt(Opt_mask), mt(mask_append) + offset);
Petko Manolov80eae202015-12-02 17:47:56 +02001618 seq_puts(m, " ");
1619 }
1620
1621 if (entry->flags & IMA_FSMAGIC) {
1622 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
1623 seq_printf(m, pt(Opt_fsmagic), tbuf);
1624 seq_puts(m, " ");
1625 }
1626
Mimi Zoharf1b08bb2018-01-15 11:20:36 -05001627 if (entry->flags & IMA_FSNAME) {
1628 snprintf(tbuf, sizeof(tbuf), "%s", entry->fsname);
1629 seq_printf(m, pt(Opt_fsname), tbuf);
1630 seq_puts(m, " ");
1631 }
1632
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -08001633 if (entry->flags & IMA_KEYRINGS) {
1634 if (entry->keyrings != NULL)
1635 snprintf(tbuf, sizeof(tbuf), "%s", entry->keyrings);
1636 seq_printf(m, pt(Opt_keyrings), tbuf);
1637 seq_puts(m, " ");
1638 }
1639
Eric Richter02606432016-06-01 13:14:01 -05001640 if (entry->flags & IMA_PCR) {
1641 snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
1642 seq_printf(m, pt(Opt_pcr), tbuf);
1643 seq_puts(m, " ");
1644 }
1645
Petko Manolov80eae202015-12-02 17:47:56 +02001646 if (entry->flags & IMA_FSUUID) {
Christoph Hellwig787d8c52017-06-01 07:00:26 +02001647 seq_printf(m, "fsuuid=%pU", &entry->fsuuid);
Petko Manolov80eae202015-12-02 17:47:56 +02001648 seq_puts(m, " ");
1649 }
1650
1651 if (entry->flags & IMA_UID) {
1652 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001653 if (entry->uid_op == &uid_gt)
1654 seq_printf(m, pt(Opt_uid_gt), tbuf);
1655 else if (entry->uid_op == &uid_lt)
1656 seq_printf(m, pt(Opt_uid_lt), tbuf);
1657 else
1658 seq_printf(m, pt(Opt_uid_eq), tbuf);
Petko Manolov80eae202015-12-02 17:47:56 +02001659 seq_puts(m, " ");
1660 }
1661
1662 if (entry->flags & IMA_EUID) {
1663 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001664 if (entry->uid_op == &uid_gt)
1665 seq_printf(m, pt(Opt_euid_gt), tbuf);
1666 else if (entry->uid_op == &uid_lt)
1667 seq_printf(m, pt(Opt_euid_lt), tbuf);
1668 else
1669 seq_printf(m, pt(Opt_euid_eq), tbuf);
Petko Manolov80eae202015-12-02 17:47:56 +02001670 seq_puts(m, " ");
1671 }
1672
1673 if (entry->flags & IMA_FOWNER) {
1674 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001675 if (entry->fowner_op == &uid_gt)
1676 seq_printf(m, pt(Opt_fowner_gt), tbuf);
1677 else if (entry->fowner_op == &uid_lt)
1678 seq_printf(m, pt(Opt_fowner_lt), tbuf);
1679 else
1680 seq_printf(m, pt(Opt_fowner_eq), tbuf);
Petko Manolov80eae202015-12-02 17:47:56 +02001681 seq_puts(m, " ");
1682 }
1683
1684 for (i = 0; i < MAX_LSM_RULES; i++) {
1685 if (entry->lsm[i].rule) {
1686 switch (i) {
1687 case LSM_OBJ_USER:
1688 seq_printf(m, pt(Opt_obj_user),
Tyler Hicksaa0c0222020-07-09 01:19:08 -05001689 entry->lsm[i].args_p);
Petko Manolov80eae202015-12-02 17:47:56 +02001690 break;
1691 case LSM_OBJ_ROLE:
1692 seq_printf(m, pt(Opt_obj_role),
Tyler Hicksaa0c0222020-07-09 01:19:08 -05001693 entry->lsm[i].args_p);
Petko Manolov80eae202015-12-02 17:47:56 +02001694 break;
1695 case LSM_OBJ_TYPE:
1696 seq_printf(m, pt(Opt_obj_type),
Tyler Hicksaa0c0222020-07-09 01:19:08 -05001697 entry->lsm[i].args_p);
Petko Manolov80eae202015-12-02 17:47:56 +02001698 break;
1699 case LSM_SUBJ_USER:
1700 seq_printf(m, pt(Opt_subj_user),
Tyler Hicksaa0c0222020-07-09 01:19:08 -05001701 entry->lsm[i].args_p);
Petko Manolov80eae202015-12-02 17:47:56 +02001702 break;
1703 case LSM_SUBJ_ROLE:
1704 seq_printf(m, pt(Opt_subj_role),
Tyler Hicksaa0c0222020-07-09 01:19:08 -05001705 entry->lsm[i].args_p);
Petko Manolov80eae202015-12-02 17:47:56 +02001706 break;
1707 case LSM_SUBJ_TYPE:
1708 seq_printf(m, pt(Opt_subj_type),
Tyler Hicksaa0c0222020-07-09 01:19:08 -05001709 entry->lsm[i].args_p);
Petko Manolov80eae202015-12-02 17:47:56 +02001710 break;
1711 }
Clay Chang5350ceb2020-01-05 09:18:13 +08001712 seq_puts(m, " ");
Petko Manolov80eae202015-12-02 17:47:56 +02001713 }
1714 }
Matthew Garrett19453ce02019-06-19 15:46:11 -07001715 if (entry->template)
1716 seq_printf(m, "template=%s ", entry->template->name);
Thiago Jung Bauermann9044d622019-06-27 23:19:28 -03001717 if (entry->flags & IMA_DIGSIG_REQUIRED) {
1718 if (entry->flags & IMA_MODSIG_ALLOWED)
1719 seq_puts(m, "appraise_type=imasig|modsig ");
1720 else
1721 seq_puts(m, "appraise_type=imasig ");
1722 }
Nayna Jain273df862019-10-30 23:31:32 -04001723 if (entry->flags & IMA_CHECK_BLACKLIST)
1724 seq_puts(m, "appraise_flag=check_blacklist ");
Petko Manolov80eae202015-12-02 17:47:56 +02001725 if (entry->flags & IMA_PERMIT_DIRECTIO)
1726 seq_puts(m, "permit_directio ");
1727 rcu_read_unlock();
1728 seq_puts(m, "\n");
1729 return 0;
1730}
1731#endif /* CONFIG_IMA_READ_POLICY */
Matthew Garrett29d3c1c2019-08-19 17:18:01 -07001732
1733#if defined(CONFIG_IMA_APPRAISE) && defined(CONFIG_INTEGRITY_TRUSTED_KEYRING)
1734/*
1735 * ima_appraise_signature: whether IMA will appraise a given function using
1736 * an IMA digital signature. This is restricted to cases where the kernel
1737 * has a set of built-in trusted keys in order to avoid an attacker simply
1738 * loading additional keys.
1739 */
1740bool ima_appraise_signature(enum kernel_read_file_id id)
1741{
1742 struct ima_rule_entry *entry;
1743 bool found = false;
1744 enum ima_hooks func;
1745
1746 if (id >= READING_MAX_ID)
1747 return false;
1748
1749 func = read_idmap[id] ?: FILE_CHECK;
1750
1751 rcu_read_lock();
1752 list_for_each_entry_rcu(entry, ima_rules, list) {
1753 if (entry->action != APPRAISE)
1754 continue;
1755
1756 /*
1757 * A generic entry will match, but otherwise require that it
1758 * match the func we're looking for
1759 */
1760 if (entry->func && entry->func != func)
1761 continue;
1762
1763 /*
1764 * We require this to be a digital signature, not a raw IMA
1765 * hash.
1766 */
1767 if (entry->flags & IMA_DIGSIG_REQUIRED)
1768 found = true;
1769
1770 /*
1771 * We've found a rule that matches, so break now even if it
1772 * didn't require a digital signature - a later rule that does
1773 * won't override it, so would be a false positive.
1774 */
1775 break;
1776 }
1777
1778 rcu_read_unlock();
1779 return found;
1780}
1781#endif /* CONFIG_IMA_APPRAISE && CONFIG_INTEGRITY_TRUSTED_KEYRING */