blob: 50ead724ba23ed8cd69116659370397460cfee75 [file] [log] [blame]
Mimi Zohar3323eec2009-02-04 09:06:58 -05001/*
2 * Copyright (C) 2008 IBM Corporation
3 * Author: Mimi Zohar <zohar@us.ibm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
8 *
9 * ima_policy.c
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020010 * - initialize default measure policy rules
Mimi Zohar3323eec2009-02-04 09:06:58 -050011 *
12 */
13#include <linux/module.h>
14#include <linux/list.h>
Mimi Zoharcf222212016-01-14 17:57:47 -050015#include <linux/fs.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050016#include <linux/security.h>
17#include <linux/magic.h>
Mimi Zohar4af46622009-02-04 09:07:00 -050018#include <linux/parser.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Petko Manolov38d859f2015-12-02 17:47:54 +020020#include <linux/rculist.h>
Dmitry Kasatkin85865c12012-09-03 23:23:13 +030021#include <linux/genhd.h>
Petko Manolov80eae202015-12-02 17:47:56 +020022#include <linux/seq_file.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
Mimi Zohar07f6a792011-03-09 22:25:48 -050061struct ima_rule_entry {
Mimi Zohar3323eec2009-02-04 09:06:58 -050062 struct list_head list;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050063 int action;
Mimi Zohar3323eec2009-02-04 09:06:58 -050064 unsigned int flags;
65 enum ima_hooks func;
66 int mask;
67 unsigned long fsmagic;
Christoph Hellwig787d8c52017-06-01 07:00:26 +020068 uuid_t fsuuid;
Eric W. Biederman8b94eea2012-05-25 18:24:12 -060069 kuid_t uid;
Linus Torvalds882653222012-10-02 21:38:48 -070070 kuid_t fowner;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +030071 bool (*uid_op)(kuid_t, kuid_t); /* Handlers for operators */
72 bool (*fowner_op)(kuid_t, kuid_t); /* uid_eq(), uid_gt(), uid_lt() */
Eric Richter02606432016-06-01 13:14:01 -050073 int pcr;
Mimi Zohar4af46622009-02-04 09:07:00 -050074 struct {
75 void *rule; /* LSM file metadata specific */
Mimi Zohar7163a992013-01-03 14:19:09 -050076 void *args_p; /* audit value */
Mimi Zohar4af46622009-02-04 09:07:00 -050077 int type; /* audit type */
78 } lsm[MAX_LSM_RULES];
Mimi Zoharf1b08bb2018-01-15 11:20:36 -050079 char *fsname;
Mimi Zohar3323eec2009-02-04 09:06:58 -050080};
81
Eric Paris5789ba32009-05-21 15:47:06 -040082/*
83 * Without LSM specific knowledge, the default policy can only be
Mimi Zohar07f6a792011-03-09 22:25:48 -050084 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
Mimi Zohar4af46622009-02-04 09:07:00 -050085 */
Eric Paris5789ba32009-05-21 15:47:06 -040086
87/*
88 * The minimum rule set to allow for full TCB coverage. Measures all files
89 * opened or mmap for exec and everything read by root. Dangerous because
90 * normal users can easily run the machine out of memory simply building
91 * and running executables.
92 */
James Morrisbad44172017-02-13 16:34:35 +110093static struct ima_rule_entry dont_measure_rules[] __ro_after_init = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020094 {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
95 {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
96 {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
97 {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
98 {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
99 {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
100 {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
101 {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
Martin Townsend1c070b12018-03-22 13:46:01 +0000102 {.action = DONT_MEASURE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
Roberto Sassu6438de92015-04-11 17:13:06 +0200103 {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
104 .flags = IMA_FSMAGIC},
Laura Abbott82e3bb42017-05-09 11:25:27 -0700105 {.action = DONT_MEASURE, .fsmagic = CGROUP2_SUPER_MAGIC,
106 .flags = IMA_FSMAGIC},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400107 {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC}
108};
109
James Morrisbad44172017-02-13 16:34:35 +1100110static struct ima_rule_entry original_measurement_rules[] __ro_after_init = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200111 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500112 .flags = IMA_FUNC | IMA_MASK},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200113 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500114 .flags = IMA_FUNC | IMA_MASK},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400115 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300116 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
117 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400118 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
119 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
120};
121
James Morrisbad44172017-02-13 16:34:35 +1100122static struct ima_rule_entry default_measurement_rules[] __ro_after_init = {
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400123 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
124 .flags = IMA_FUNC | IMA_MASK},
125 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
126 .flags = IMA_FUNC | IMA_MASK},
127 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300128 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
129 .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400130 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300131 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
132 .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200133 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400134 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
Mimi Zohar19f8a842016-01-15 10:17:12 -0500135 {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC},
Mimi Zohar3323eec2009-02-04 09:06:58 -0500136};
137
James Morrisbad44172017-02-13 16:34:35 +1100138static struct ima_rule_entry default_appraise_rules[] __ro_after_init = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200139 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
140 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
141 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
142 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
143 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
144 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
145 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
146 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
147 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
Martin Townsend1c070b12018-03-22 13:46:01 +0000148 {.action = DONT_APPRAISE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
Mimi Zoharcd025f72015-04-21 16:54:24 -0400149 {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200150 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
Laura Abbott82e3bb42017-05-09 11:25:27 -0700151 {.action = DONT_APPRAISE, .fsmagic = CGROUP2_SUPER_MAGIC, .flags = IMA_FSMAGIC},
Mimi Zohar95ee08f2015-12-07 15:08:01 -0500152#ifdef CONFIG_IMA_WRITE_POLICY
153 {.action = APPRAISE, .func = POLICY_CHECK,
154 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
155#endif
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200156#ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300157 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
158 .flags = IMA_FOWNER},
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200159#else
160 /* force signature */
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300161 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200162 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
163#endif
Mimi Zohar07f6a792011-03-09 22:25:48 -0500164};
Mimi Zohar3323eec2009-02-04 09:06:58 -0500165
Mimi Zoharef968372018-07-13 14:06:01 -0400166static struct ima_rule_entry build_appraise_rules[] __ro_after_init = {
167#ifdef CONFIG_IMA_APPRAISE_REQUIRE_MODULE_SIGS
168 {.action = APPRAISE, .func = MODULE_CHECK,
169 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
170#endif
171#ifdef CONFIG_IMA_APPRAISE_REQUIRE_FIRMWARE_SIGS
172 {.action = APPRAISE, .func = FIRMWARE_CHECK,
173 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
174#endif
175#ifdef CONFIG_IMA_APPRAISE_REQUIRE_KEXEC_SIGS
176 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
177 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
178#endif
179#ifdef CONFIG_IMA_APPRAISE_REQUIRE_POLICY_SIGS
180 {.action = APPRAISE, .func = POLICY_CHECK,
181 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
182#endif
183};
184
Mimi Zohar503ceae2017-04-21 18:58:27 -0400185static struct ima_rule_entry secure_boot_rules[] __ro_after_init = {
186 {.action = APPRAISE, .func = MODULE_CHECK,
187 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
188 {.action = APPRAISE, .func = FIRMWARE_CHECK,
189 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
190 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
191 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
192 {.action = APPRAISE, .func = POLICY_CHECK,
193 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
194};
195
Mimi Zohar07f6a792011-03-09 22:25:48 -0500196static LIST_HEAD(ima_default_rules);
197static LIST_HEAD(ima_policy_rules);
Petko Manolov38d859f2015-12-02 17:47:54 +0200198static LIST_HEAD(ima_temp_rules);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500199static struct list_head *ima_rules;
200
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400201static int ima_policy __initdata;
Petko Manolov38d859f2015-12-02 17:47:54 +0200202
Mimi Zohar07f6a792011-03-09 22:25:48 -0500203static int __init default_measure_policy_setup(char *str)
Eric Paris5789ba32009-05-21 15:47:06 -0400204{
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400205 if (ima_policy)
206 return 1;
207
208 ima_policy = ORIGINAL_TCB;
Eric Paris5789ba32009-05-21 15:47:06 -0400209 return 1;
210}
Mimi Zohar07f6a792011-03-09 22:25:48 -0500211__setup("ima_tcb", default_measure_policy_setup);
212
Mimi Zohar33ce9542017-04-24 12:04:09 -0400213static bool ima_use_appraise_tcb __initdata;
Mimi Zohar503ceae2017-04-21 18:58:27 -0400214static bool ima_use_secure_boot __initdata;
Mimi Zohar9e670282018-02-21 11:36:32 -0500215static bool ima_fail_unverifiable_sigs __ro_after_init;
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400216static int __init policy_setup(char *str)
217{
Mimi Zohar33ce9542017-04-24 12:04:09 -0400218 char *p;
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400219
Mimi Zohar33ce9542017-04-24 12:04:09 -0400220 while ((p = strsep(&str, " |\n")) != NULL) {
221 if (*p == ' ')
222 continue;
223 if ((strcmp(p, "tcb") == 0) && !ima_policy)
224 ima_policy = DEFAULT_TCB;
225 else if (strcmp(p, "appraise_tcb") == 0)
Thomas Meyer39adb922017-10-07 16:02:21 +0200226 ima_use_appraise_tcb = true;
Mimi Zohar503ceae2017-04-21 18:58:27 -0400227 else if (strcmp(p, "secure_boot") == 0)
Thomas Meyer39adb922017-10-07 16:02:21 +0200228 ima_use_secure_boot = true;
Mimi Zohar9e670282018-02-21 11:36:32 -0500229 else if (strcmp(p, "fail_securely") == 0)
230 ima_fail_unverifiable_sigs = true;
Mimi Zohar33ce9542017-04-24 12:04:09 -0400231 }
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400232
233 return 1;
234}
235__setup("ima_policy=", policy_setup);
236
Mimi Zohar07f6a792011-03-09 22:25:48 -0500237static int __init default_appraise_policy_setup(char *str)
238{
Thomas Meyer39adb922017-10-07 16:02:21 +0200239 ima_use_appraise_tcb = true;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500240 return 1;
241}
242__setup("ima_appraise_tcb", default_appraise_policy_setup);
Eric Paris5789ba32009-05-21 15:47:06 -0400243
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200244/*
Petko Manolov38d859f2015-12-02 17:47:54 +0200245 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
246 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
247 * the reloaded LSM policy. We assume the rules still exist; and BUG_ON() if
248 * they don't.
Mimi Zohar7163a992013-01-03 14:19:09 -0500249 */
250static void ima_lsm_update_rules(void)
251{
Petko Manolov38d859f2015-12-02 17:47:54 +0200252 struct ima_rule_entry *entry;
Mimi Zohar7163a992013-01-03 14:19:09 -0500253 int result;
254 int i;
255
Petko Manolov38d859f2015-12-02 17:47:54 +0200256 list_for_each_entry(entry, &ima_policy_rules, list) {
Mimi Zohar7163a992013-01-03 14:19:09 -0500257 for (i = 0; i < MAX_LSM_RULES; i++) {
258 if (!entry->lsm[i].rule)
259 continue;
260 result = security_filter_rule_init(entry->lsm[i].type,
261 Audit_equal,
262 entry->lsm[i].args_p,
263 &entry->lsm[i].rule);
264 BUG_ON(!entry->lsm[i].rule);
265 }
266 }
Mimi Zohar7163a992013-01-03 14:19:09 -0500267}
268
Mimi Zohar3323eec2009-02-04 09:06:58 -0500269/**
270 * ima_match_rules - determine whether an inode matches the measure rule.
271 * @rule: a pointer to a rule
272 * @inode: a pointer to an inode
Matthew Garrettd906c102018-01-08 13:36:20 -0800273 * @cred: a pointer to a credentials structure for user validation
274 * @secid: the secid of the task to be validated
Mimi Zohar3323eec2009-02-04 09:06:58 -0500275 * @func: LIM hook identifier
276 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
277 *
278 * Returns true on rule match, false on failure.
279 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500280static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,
Matthew Garrettd906c102018-01-08 13:36:20 -0800281 const struct cred *cred, u32 secid,
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500282 enum ima_hooks func, int mask)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500283{
Mimi Zohar4af46622009-02-04 09:07:00 -0500284 int i;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500285
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200286 if ((rule->flags & IMA_FUNC) &&
287 (rule->func != func && func != POST_SETATTR))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500288 return false;
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200289 if ((rule->flags & IMA_MASK) &&
290 (rule->mask != mask && func != POST_SETATTR))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500291 return false;
Mimi Zohar4351c292014-11-05 07:53:55 -0500292 if ((rule->flags & IMA_INMASK) &&
293 (!(rule->mask & mask) && func != POST_SETATTR))
294 return false;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500295 if ((rule->flags & IMA_FSMAGIC)
296 && rule->fsmagic != inode->i_sb->s_magic)
297 return false;
Mimi Zoharf1b08bb2018-01-15 11:20:36 -0500298 if ((rule->flags & IMA_FSNAME)
299 && strcmp(rule->fsname, inode->i_sb->s_type->name))
300 return false;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300301 if ((rule->flags & IMA_FSUUID) &&
Christoph Hellwig85787092017-05-10 15:06:33 +0200302 !uuid_equal(&rule->fsuuid, &inode->i_sb->s_uuid))
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300303 return false;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300304 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500305 return false;
Mimi Zohar139069e2014-11-05 07:48:36 -0500306 if (rule->flags & IMA_EUID) {
307 if (has_capability_noaudit(current, CAP_SETUID)) {
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300308 if (!rule->uid_op(cred->euid, rule->uid)
309 && !rule->uid_op(cred->suid, rule->uid)
310 && !rule->uid_op(cred->uid, rule->uid))
Mimi Zohar139069e2014-11-05 07:48:36 -0500311 return false;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300312 } else if (!rule->uid_op(cred->euid, rule->uid))
Mimi Zohar139069e2014-11-05 07:48:36 -0500313 return false;
314 }
315
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300316 if ((rule->flags & IMA_FOWNER) &&
317 !rule->fowner_op(inode->i_uid, rule->fowner))
Mimi Zohar07f6a792011-03-09 22:25:48 -0500318 return false;
Mimi Zohar4af46622009-02-04 09:07:00 -0500319 for (i = 0; i < MAX_LSM_RULES; i++) {
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400320 int rc = 0;
Matthew Garrettd906c102018-01-08 13:36:20 -0800321 u32 osid;
Mimi Zohar7163a992013-01-03 14:19:09 -0500322 int retried = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500323
324 if (!rule->lsm[i].rule)
325 continue;
Mimi Zohar7163a992013-01-03 14:19:09 -0500326retry:
Mimi Zohar4af46622009-02-04 09:07:00 -0500327 switch (i) {
328 case LSM_OBJ_USER:
329 case LSM_OBJ_ROLE:
330 case LSM_OBJ_TYPE:
331 security_inode_getsecid(inode, &osid);
332 rc = security_filter_rule_match(osid,
333 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400334 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500335 rule->lsm[i].rule,
336 NULL);
337 break;
338 case LSM_SUBJ_USER:
339 case LSM_SUBJ_ROLE:
340 case LSM_SUBJ_TYPE:
Matthew Garrettd906c102018-01-08 13:36:20 -0800341 rc = security_filter_rule_match(secid,
Mimi Zohar4af46622009-02-04 09:07:00 -0500342 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400343 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500344 rule->lsm[i].rule,
345 NULL);
346 default:
347 break;
348 }
Mimi Zohar7163a992013-01-03 14:19:09 -0500349 if ((rc < 0) && (!retried)) {
350 retried = 1;
351 ima_lsm_update_rules();
352 goto retry;
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200353 }
Mimi Zohar4af46622009-02-04 09:07:00 -0500354 if (!rc)
355 return false;
356 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500357 return true;
358}
359
Mimi Zohard79d72e2012-12-03 17:08:11 -0500360/*
361 * In addition to knowing that we need to appraise the file in general,
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500362 * we need to differentiate between calling hooks, for hook specific rules.
Mimi Zohard79d72e2012-12-03 17:08:11 -0500363 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500364static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
Mimi Zohard79d72e2012-12-03 17:08:11 -0500365{
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500366 if (!(rule->flags & IMA_FUNC))
367 return IMA_FILE_APPRAISE;
368
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200369 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500370 case MMAP_CHECK:
371 return IMA_MMAP_APPRAISE;
372 case BPRM_CHECK:
373 return IMA_BPRM_APPRAISE;
Matthew Garrettd906c102018-01-08 13:36:20 -0800374 case CREDS_CHECK:
375 return IMA_CREDS_APPRAISE;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500376 case FILE_CHECK:
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500377 case POST_SETATTR:
Mimi Zohard79d72e2012-12-03 17:08:11 -0500378 return IMA_FILE_APPRAISE;
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500379 case MODULE_CHECK ... MAX_CHECK - 1:
380 default:
381 return IMA_READ_APPRAISE;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500382 }
383}
384
Mimi Zohar3323eec2009-02-04 09:06:58 -0500385/**
386 * ima_match_policy - decision based on LSM and other conditions
387 * @inode: pointer to an inode for which the policy decision is being made
Matthew Garrettd906c102018-01-08 13:36:20 -0800388 * @cred: pointer to a credentials structure for which the policy decision is
389 * being made
390 * @secid: LSM secid of the task to be validated
Mimi Zohar3323eec2009-02-04 09:06:58 -0500391 * @func: IMA hook identifier
392 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
Eric Richter725de7f2016-06-01 13:14:02 -0500393 * @pcr: set the pcr to extend
Mimi Zohar3323eec2009-02-04 09:06:58 -0500394 *
395 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
396 * conditions.
397 *
Petko Manolov38d859f2015-12-02 17:47:54 +0200398 * Since the IMA policy may be updated multiple times we need to lock the
399 * list when walking it. Reads are many orders of magnitude more numerous
400 * than writes so ima_match_policy() is classical RCU candidate.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500401 */
Matthew Garrettd906c102018-01-08 13:36:20 -0800402int ima_match_policy(struct inode *inode, const struct cred *cred, u32 secid,
403 enum ima_hooks func, int mask, int flags, int *pcr)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500404{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500405 struct ima_rule_entry *entry;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500406 int action = 0, actmask = flags | (flags << 1);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500407
Petko Manolov38d859f2015-12-02 17:47:54 +0200408 rcu_read_lock();
409 list_for_each_entry_rcu(entry, ima_rules, list) {
Mimi Zohar3323eec2009-02-04 09:06:58 -0500410
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500411 if (!(entry->action & actmask))
412 continue;
413
Matthew Garrettd906c102018-01-08 13:36:20 -0800414 if (!ima_match_rules(entry, inode, cred, secid, func, mask))
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500415 continue;
416
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300417 action |= entry->flags & IMA_ACTION_FLAGS;
418
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300419 action |= entry->action & IMA_DO_MASK;
Mimi Zoharda1b0022016-09-29 10:04:52 -0400420 if (entry->action & IMA_APPRAISE) {
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500421 action |= get_subaction(entry, func);
Mimi Zohara9a49352018-03-10 23:07:34 -0500422 action &= ~IMA_HASH;
Mimi Zohar9e670282018-02-21 11:36:32 -0500423 if (ima_fail_unverifiable_sigs)
424 action |= IMA_FAIL_UNVERIFIABLE_SIGS;
Mimi Zoharda1b0022016-09-29 10:04:52 -0400425 }
Mimi Zohard79d72e2012-12-03 17:08:11 -0500426
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300427 if (entry->action & IMA_DO_MASK)
428 actmask &= ~(entry->action | entry->action << 1);
429 else
430 actmask &= ~(entry->action | entry->action >> 1);
431
Eric Richter725de7f2016-06-01 13:14:02 -0500432 if ((pcr) && (entry->flags & IMA_PCR))
433 *pcr = entry->pcr;
434
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500435 if (!actmask)
436 break;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500437 }
Petko Manolov38d859f2015-12-02 17:47:54 +0200438 rcu_read_unlock();
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500439
440 return action;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500441}
442
Roberto Sassua7560242014-09-12 19:35:54 +0200443/*
444 * Initialize the ima_policy_flag variable based on the currently
445 * loaded policy. Based on this flag, the decision to short circuit
446 * out of a function or not call the function in the first place
447 * can be made earlier.
448 */
449void ima_update_policy_flag(void)
450{
451 struct ima_rule_entry *entry;
452
Roberto Sassua7560242014-09-12 19:35:54 +0200453 list_for_each_entry(entry, ima_rules, list) {
454 if (entry->action & IMA_DO_MASK)
455 ima_policy_flag |= entry->action;
456 }
457
Mimi Zoharef968372018-07-13 14:06:01 -0400458 ima_appraise |= (build_ima_appraise | temp_ima_appraise);
Roberto Sassua7560242014-09-12 19:35:54 +0200459 if (!ima_appraise)
460 ima_policy_flag &= ~IMA_APPRAISE;
461}
462
Mimi Zohar6f0911a2018-04-12 00:15:22 -0400463static int ima_appraise_flag(enum ima_hooks func)
464{
465 if (func == MODULE_CHECK)
466 return IMA_APPRAISE_MODULES;
467 else if (func == FIRMWARE_CHECK)
468 return IMA_APPRAISE_FIRMWARE;
469 else if (func == POLICY_CHECK)
470 return IMA_APPRAISE_POLICY;
Mimi Zohar16c267a2018-07-13 14:05:58 -0400471 else if (func == KEXEC_KERNEL_CHECK)
472 return IMA_APPRAISE_KEXEC;
Mimi Zohar6f0911a2018-04-12 00:15:22 -0400473 return 0;
474}
475
Mimi Zohar3323eec2009-02-04 09:06:58 -0500476/**
477 * ima_init_policy - initialize the default measure rules.
478 *
Mimi Zohar07f6a792011-03-09 22:25:48 -0500479 * ima_rules points to either the ima_default_rules or the
480 * the new ima_policy_rules.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500481 */
Eric Paris932995f2009-05-21 15:43:32 -0400482void __init ima_init_policy(void)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500483{
Mimi Zohar503ceae2017-04-21 18:58:27 -0400484 int i, measure_entries, appraise_entries, secure_boot_entries;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500485
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400486 /* if !ima_policy set entries = 0 so we load NO default rules */
487 measure_entries = ima_policy ? ARRAY_SIZE(dont_measure_rules) : 0;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500488 appraise_entries = ima_use_appraise_tcb ?
489 ARRAY_SIZE(default_appraise_rules) : 0;
Mimi Zohar503ceae2017-04-21 18:58:27 -0400490 secure_boot_entries = ima_use_secure_boot ?
491 ARRAY_SIZE(secure_boot_rules) : 0;
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200492
Dan Carpenter55778572015-04-07 12:22:11 +0300493 for (i = 0; i < measure_entries; i++)
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400494 list_add_tail(&dont_measure_rules[i].list, &ima_default_rules);
495
496 switch (ima_policy) {
497 case ORIGINAL_TCB:
498 for (i = 0; i < ARRAY_SIZE(original_measurement_rules); i++)
499 list_add_tail(&original_measurement_rules[i].list,
500 &ima_default_rules);
501 break;
502 case DEFAULT_TCB:
503 for (i = 0; i < ARRAY_SIZE(default_measurement_rules); i++)
504 list_add_tail(&default_measurement_rules[i].list,
505 &ima_default_rules);
506 default:
507 break;
508 }
Eric Paris5789ba32009-05-21 15:47:06 -0400509
Mimi Zohar503ceae2017-04-21 18:58:27 -0400510 /*
Mimi Zoharef968372018-07-13 14:06:01 -0400511 * Insert the builtin "secure_boot" policy rules requiring file
512 * signatures, prior to any other appraise rules.
Mimi Zohar503ceae2017-04-21 18:58:27 -0400513 */
Mimi Zohar6f0911a2018-04-12 00:15:22 -0400514 for (i = 0; i < secure_boot_entries; i++) {
515 list_add_tail(&secure_boot_rules[i].list, &ima_default_rules);
516 temp_ima_appraise |=
517 ima_appraise_flag(secure_boot_rules[i].func);
518 }
Mimi Zohar503ceae2017-04-21 18:58:27 -0400519
Mimi Zoharef968372018-07-13 14:06:01 -0400520 /*
521 * Insert the build time appraise rules requiring file signatures
522 * for both the initial and custom policies, prior to other appraise
523 * rules.
524 */
525 for (i = 0; i < ARRAY_SIZE(build_appraise_rules); i++) {
526 struct ima_rule_entry *entry;
527
528 if (!secure_boot_entries)
529 list_add_tail(&build_appraise_rules[i].list,
530 &ima_default_rules);
531
532 entry = kmemdup(&build_appraise_rules[i], sizeof(*entry),
533 GFP_KERNEL);
534 if (entry)
535 list_add_tail(&entry->list, &ima_policy_rules);
536 build_ima_appraise |=
537 ima_appraise_flag(build_appraise_rules[i].func);
538 }
539
Dan Carpenter55778572015-04-07 12:22:11 +0300540 for (i = 0; i < appraise_entries; i++) {
541 list_add_tail(&default_appraise_rules[i].list,
542 &ima_default_rules);
Mimi Zohar95ee08f2015-12-07 15:08:01 -0500543 if (default_appraise_rules[i].func == POLICY_CHECK)
544 temp_ima_appraise |= IMA_APPRAISE_POLICY;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500545 }
546
547 ima_rules = &ima_default_rules;
Mimi Zohar95ee08f2015-12-07 15:08:01 -0500548 ima_update_policy_flag();
Mimi Zohar3323eec2009-02-04 09:06:58 -0500549}
Mimi Zohar4af46622009-02-04 09:07:00 -0500550
Sasha Levin01127212015-12-22 08:51:23 -0500551/* Make sure we have a valid policy, at least containing some rules. */
Colin Ian Kingc75d8e92016-01-20 11:13:46 +0000552int ima_check_policy(void)
Sasha Levin01127212015-12-22 08:51:23 -0500553{
554 if (list_empty(&ima_temp_rules))
555 return -EINVAL;
556 return 0;
557}
558
Mimi Zohar4af46622009-02-04 09:07:00 -0500559/**
560 * ima_update_policy - update default_rules with new measure rules
561 *
562 * Called on file .release to update the default rules with a complete new
Petko Manolov38d859f2015-12-02 17:47:54 +0200563 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
564 * they make a queue. The policy may be updated multiple times and this is the
565 * RCU updater.
566 *
567 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
568 * we switch from the default policy to user defined.
Mimi Zohar4af46622009-02-04 09:07:00 -0500569 */
570void ima_update_policy(void)
571{
Petko Manolov53b626f2018-05-22 17:06:55 +0300572 struct list_head *policy = &ima_policy_rules;
Petko Manolov38d859f2015-12-02 17:47:54 +0200573
Petko Manolov53b626f2018-05-22 17:06:55 +0300574 list_splice_tail_init_rcu(&ima_temp_rules, policy, synchronize_rcu);
Petko Manolov38d859f2015-12-02 17:47:54 +0200575
576 if (ima_rules != policy) {
577 ima_policy_flag = 0;
578 ima_rules = policy;
579 }
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300580 ima_update_policy_flag();
Mimi Zohar4af46622009-02-04 09:07:00 -0500581}
582
583enum {
584 Opt_err = -1,
585 Opt_measure = 1, Opt_dont_measure,
Mimi Zohar07f6a792011-03-09 22:25:48 -0500586 Opt_appraise, Opt_dont_appraise,
Mimi Zoharda1b0022016-09-29 10:04:52 -0400587 Opt_audit, Opt_hash, Opt_dont_hash,
Mimi Zohar4af46622009-02-04 09:07:00 -0500588 Opt_obj_user, Opt_obj_role, Opt_obj_type,
589 Opt_subj_user, Opt_subj_role, Opt_subj_type,
Mimi Zoharf1b08bb2018-01-15 11:20:36 -0500590 Opt_func, Opt_mask, Opt_fsmagic, Opt_fsname,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300591 Opt_fsuuid, Opt_uid_eq, Opt_euid_eq, Opt_fowner_eq,
592 Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt,
593 Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt,
Eric Richter02606432016-06-01 13:14:01 -0500594 Opt_appraise_type, Opt_permit_directio,
595 Opt_pcr
Mimi Zohar4af46622009-02-04 09:07:00 -0500596};
597
598static match_table_t policy_tokens = {
599 {Opt_measure, "measure"},
600 {Opt_dont_measure, "dont_measure"},
Mimi Zohar07f6a792011-03-09 22:25:48 -0500601 {Opt_appraise, "appraise"},
602 {Opt_dont_appraise, "dont_appraise"},
Peter Moodye7c568e2012-06-14 10:04:36 -0700603 {Opt_audit, "audit"},
Mimi Zoharda1b0022016-09-29 10:04:52 -0400604 {Opt_hash, "hash"},
605 {Opt_dont_hash, "dont_hash"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500606 {Opt_obj_user, "obj_user=%s"},
607 {Opt_obj_role, "obj_role=%s"},
608 {Opt_obj_type, "obj_type=%s"},
609 {Opt_subj_user, "subj_user=%s"},
610 {Opt_subj_role, "subj_role=%s"},
611 {Opt_subj_type, "subj_type=%s"},
612 {Opt_func, "func=%s"},
613 {Opt_mask, "mask=%s"},
614 {Opt_fsmagic, "fsmagic=%s"},
Mimi Zoharf1b08bb2018-01-15 11:20:36 -0500615 {Opt_fsname, "fsname=%s"},
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300616 {Opt_fsuuid, "fsuuid=%s"},
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300617 {Opt_uid_eq, "uid=%s"},
618 {Opt_euid_eq, "euid=%s"},
619 {Opt_fowner_eq, "fowner=%s"},
620 {Opt_uid_gt, "uid>%s"},
621 {Opt_euid_gt, "euid>%s"},
622 {Opt_fowner_gt, "fowner>%s"},
623 {Opt_uid_lt, "uid<%s"},
624 {Opt_euid_lt, "euid<%s"},
625 {Opt_fowner_lt, "fowner<%s"},
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300626 {Opt_appraise_type, "appraise_type=%s"},
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400627 {Opt_permit_directio, "permit_directio"},
Eric Richter02606432016-06-01 13:14:01 -0500628 {Opt_pcr, "pcr=%s"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500629 {Opt_err, NULL}
630};
631
Mimi Zohar07f6a792011-03-09 22:25:48 -0500632static int ima_lsm_rule_init(struct ima_rule_entry *entry,
Mimi Zohar7163a992013-01-03 14:19:09 -0500633 substring_t *args, int lsm_rule, int audit_type)
Mimi Zohar4af46622009-02-04 09:07:00 -0500634{
635 int result;
636
Eric Paris7b62e162010-04-20 10:21:01 -0400637 if (entry->lsm[lsm_rule].rule)
638 return -EINVAL;
639
Mimi Zohar7163a992013-01-03 14:19:09 -0500640 entry->lsm[lsm_rule].args_p = match_strdup(args);
641 if (!entry->lsm[lsm_rule].args_p)
642 return -ENOMEM;
643
Mimi Zohar4af46622009-02-04 09:07:00 -0500644 entry->lsm[lsm_rule].type = audit_type;
645 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
Mimi Zohar7163a992013-01-03 14:19:09 -0500646 Audit_equal,
647 entry->lsm[lsm_rule].args_p,
Mimi Zohar4af46622009-02-04 09:07:00 -0500648 &entry->lsm[lsm_rule].rule);
Mimi Zohar7163a992013-01-03 14:19:09 -0500649 if (!entry->lsm[lsm_rule].rule) {
650 kfree(entry->lsm[lsm_rule].args_p);
Mimi Zohar867c2022011-01-03 14:59:10 -0800651 return -EINVAL;
Mimi Zohar7163a992013-01-03 14:19:09 -0500652 }
653
Mimi Zohar4af46622009-02-04 09:07:00 -0500654 return result;
655}
656
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300657static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value,
658 bool (*rule_operator)(kuid_t, kuid_t))
Eric Paris2f1506c2010-04-20 10:21:30 -0400659{
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300660 if (rule_operator == &uid_gt)
661 audit_log_format(ab, "%s>", key);
662 else if (rule_operator == &uid_lt)
663 audit_log_format(ab, "%s<", key);
664 else
665 audit_log_format(ab, "%s=", key);
Stefan Berger3d2859d2018-06-04 16:54:53 -0400666 audit_log_format(ab, "%s ", value);
Eric Paris2f1506c2010-04-20 10:21:30 -0400667}
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300668static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
669{
670 ima_log_string_op(ab, key, value, NULL);
671}
Eric Paris2f1506c2010-04-20 10:21:30 -0400672
Mimi Zohar07f6a792011-03-09 22:25:48 -0500673static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
Mimi Zohar4af46622009-02-04 09:07:00 -0500674{
675 struct audit_buffer *ab;
Mimi Zohar4351c292014-11-05 07:53:55 -0500676 char *from;
Mimi Zohar4af46622009-02-04 09:07:00 -0500677 char *p;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300678 bool uid_token;
Mimi Zohar4af46622009-02-04 09:07:00 -0500679 int result = 0;
680
Mimi Zohar523979a2009-02-11 11:12:28 -0500681 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
Mimi Zohar4af46622009-02-04 09:07:00 -0500682
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600683 entry->uid = INVALID_UID;
Linus Torvalds882653222012-10-02 21:38:48 -0700684 entry->fowner = INVALID_UID;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300685 entry->uid_op = &uid_eq;
686 entry->fowner_op = &uid_eq;
Eric Parisb9035b12010-04-20 10:21:07 -0400687 entry->action = UNKNOWN;
Eric Paris28ef4002010-04-20 10:21:18 -0400688 while ((p = strsep(&rule, " \t")) != NULL) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500689 substring_t args[MAX_OPT_ARGS];
690 int token;
691 unsigned long lnum;
692
693 if (result < 0)
694 break;
Eric Paris28ef4002010-04-20 10:21:18 -0400695 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
696 continue;
Mimi Zohar4af46622009-02-04 09:07:00 -0500697 token = match_token(p, policy_tokens, args);
698 switch (token) {
699 case Opt_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400700 ima_log_string(ab, "action", "measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400701
702 if (entry->action != UNKNOWN)
703 result = -EINVAL;
704
Mimi Zohar4af46622009-02-04 09:07:00 -0500705 entry->action = MEASURE;
706 break;
707 case Opt_dont_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400708 ima_log_string(ab, "action", "dont_measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400709
710 if (entry->action != UNKNOWN)
711 result = -EINVAL;
712
Mimi Zohar4af46622009-02-04 09:07:00 -0500713 entry->action = DONT_MEASURE;
714 break;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500715 case Opt_appraise:
716 ima_log_string(ab, "action", "appraise");
717
718 if (entry->action != UNKNOWN)
719 result = -EINVAL;
720
721 entry->action = APPRAISE;
722 break;
723 case Opt_dont_appraise:
724 ima_log_string(ab, "action", "dont_appraise");
725
726 if (entry->action != UNKNOWN)
727 result = -EINVAL;
728
729 entry->action = DONT_APPRAISE;
730 break;
Peter Moodye7c568e2012-06-14 10:04:36 -0700731 case Opt_audit:
732 ima_log_string(ab, "action", "audit");
733
734 if (entry->action != UNKNOWN)
735 result = -EINVAL;
736
737 entry->action = AUDIT;
738 break;
Mimi Zoharda1b0022016-09-29 10:04:52 -0400739 case Opt_hash:
740 ima_log_string(ab, "action", "hash");
741
742 if (entry->action != UNKNOWN)
743 result = -EINVAL;
744
745 entry->action = HASH;
746 break;
747 case Opt_dont_hash:
748 ima_log_string(ab, "action", "dont_hash");
749
750 if (entry->action != UNKNOWN)
751 result = -EINVAL;
752
753 entry->action = DONT_HASH;
754 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500755 case Opt_func:
Eric Paris2f1506c2010-04-20 10:21:30 -0400756 ima_log_string(ab, "func", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400757
758 if (entry->func)
Mimi Zohar07f6a792011-03-09 22:25:48 -0500759 result = -EINVAL;
Eric Paris7b62e162010-04-20 10:21:01 -0400760
Mimi Zohar1e93d002010-01-26 17:02:41 -0500761 if (strcmp(args[0].from, "FILE_CHECK") == 0)
762 entry->func = FILE_CHECK;
763 /* PATH_CHECK is for backwards compat */
764 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
765 entry->func = FILE_CHECK;
Mimi Zoharfdf90722012-10-16 12:40:08 +1030766 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
767 entry->func = MODULE_CHECK;
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400768 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
769 entry->func = FIRMWARE_CHECK;
Mimi Zohar16cac492012-12-13 11:15:04 -0500770 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
771 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
772 entry->func = MMAP_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500773 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
774 entry->func = BPRM_CHECK;
Matthew Garrettd906c102018-01-08 13:36:20 -0800775 else if (strcmp(args[0].from, "CREDS_CHECK") == 0)
776 entry->func = CREDS_CHECK;
Mimi Zohard9ddf072016-01-14 20:59:14 -0500777 else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
778 0)
779 entry->func = KEXEC_KERNEL_CHECK;
780 else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
781 == 0)
782 entry->func = KEXEC_INITRAMFS_CHECK;
Mimi Zohar19f8a842016-01-15 10:17:12 -0500783 else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
784 entry->func = POLICY_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500785 else
786 result = -EINVAL;
787 if (!result)
788 entry->flags |= IMA_FUNC;
789 break;
790 case Opt_mask:
Eric Paris2f1506c2010-04-20 10:21:30 -0400791 ima_log_string(ab, "mask", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400792
793 if (entry->mask)
794 result = -EINVAL;
795
Mimi Zohar4351c292014-11-05 07:53:55 -0500796 from = args[0].from;
797 if (*from == '^')
798 from++;
799
800 if ((strcmp(from, "MAY_EXEC")) == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500801 entry->mask = MAY_EXEC;
Mimi Zohar4351c292014-11-05 07:53:55 -0500802 else if (strcmp(from, "MAY_WRITE") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500803 entry->mask = MAY_WRITE;
Mimi Zohar4351c292014-11-05 07:53:55 -0500804 else if (strcmp(from, "MAY_READ") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500805 entry->mask = MAY_READ;
Mimi Zohar4351c292014-11-05 07:53:55 -0500806 else if (strcmp(from, "MAY_APPEND") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500807 entry->mask = MAY_APPEND;
808 else
809 result = -EINVAL;
810 if (!result)
Mimi Zohar4351c292014-11-05 07:53:55 -0500811 entry->flags |= (*args[0].from == '^')
812 ? IMA_INMASK : IMA_MASK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500813 break;
814 case Opt_fsmagic:
Eric Paris2f1506c2010-04-20 10:21:30 -0400815 ima_log_string(ab, "fsmagic", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400816
817 if (entry->fsmagic) {
818 result = -EINVAL;
819 break;
820 }
821
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200822 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
Mimi Zohar4af46622009-02-04 09:07:00 -0500823 if (!result)
824 entry->flags |= IMA_FSMAGIC;
825 break;
Mimi Zoharf1b08bb2018-01-15 11:20:36 -0500826 case Opt_fsname:
827 ima_log_string(ab, "fsname", args[0].from);
828
829 entry->fsname = kstrdup(args[0].from, GFP_KERNEL);
830 if (!entry->fsname) {
831 result = -ENOMEM;
832 break;
833 }
834 result = 0;
835 entry->flags |= IMA_FSNAME;
836 break;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300837 case Opt_fsuuid:
838 ima_log_string(ab, "fsuuid", args[0].from);
839
Mike Rapoport36447452018-01-17 20:27:11 +0200840 if (!uuid_is_null(&entry->fsuuid)) {
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300841 result = -EINVAL;
842 break;
843 }
844
Christoph Hellwig787d8c52017-06-01 07:00:26 +0200845 result = uuid_parse(args[0].from, &entry->fsuuid);
Mimi Zohar446d64e2013-02-24 23:42:37 -0500846 if (!result)
847 entry->flags |= IMA_FSUUID;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300848 break;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300849 case Opt_uid_gt:
850 case Opt_euid_gt:
851 entry->uid_op = &uid_gt;
852 case Opt_uid_lt:
853 case Opt_euid_lt:
854 if ((token == Opt_uid_lt) || (token == Opt_euid_lt))
855 entry->uid_op = &uid_lt;
856 case Opt_uid_eq:
857 case Opt_euid_eq:
858 uid_token = (token == Opt_uid_eq) ||
859 (token == Opt_uid_gt) ||
860 (token == Opt_uid_lt);
861
862 ima_log_string_op(ab, uid_token ? "uid" : "euid",
863 args[0].from, entry->uid_op);
Eric Paris7b62e162010-04-20 10:21:01 -0400864
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600865 if (uid_valid(entry->uid)) {
Eric Paris7b62e162010-04-20 10:21:01 -0400866 result = -EINVAL;
867 break;
868 }
869
Jingoo Han29707b22014-02-05 15:13:14 +0900870 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar4af46622009-02-04 09:07:00 -0500871 if (!result) {
Mimi Zohar139069e2014-11-05 07:48:36 -0500872 entry->uid = make_kuid(current_user_ns(),
873 (uid_t) lnum);
874 if (!uid_valid(entry->uid) ||
875 (uid_t)lnum != lnum)
Mimi Zohar4af46622009-02-04 09:07:00 -0500876 result = -EINVAL;
877 else
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300878 entry->flags |= uid_token
Mimi Zohar139069e2014-11-05 07:48:36 -0500879 ? IMA_UID : IMA_EUID;
Mimi Zohar4af46622009-02-04 09:07:00 -0500880 }
881 break;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300882 case Opt_fowner_gt:
883 entry->fowner_op = &uid_gt;
884 case Opt_fowner_lt:
885 if (token == Opt_fowner_lt)
886 entry->fowner_op = &uid_lt;
887 case Opt_fowner_eq:
888 ima_log_string_op(ab, "fowner", args[0].from,
889 entry->fowner_op);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500890
Linus Torvalds882653222012-10-02 21:38:48 -0700891 if (uid_valid(entry->fowner)) {
Mimi Zohar07f6a792011-03-09 22:25:48 -0500892 result = -EINVAL;
893 break;
894 }
895
Jingoo Han29707b22014-02-05 15:13:14 +0900896 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500897 if (!result) {
Linus Torvalds882653222012-10-02 21:38:48 -0700898 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
899 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
Mimi Zohar07f6a792011-03-09 22:25:48 -0500900 result = -EINVAL;
901 else
902 entry->flags |= IMA_FOWNER;
903 }
904 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500905 case Opt_obj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400906 ima_log_string(ab, "obj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500907 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500908 LSM_OBJ_USER,
909 AUDIT_OBJ_USER);
910 break;
911 case Opt_obj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400912 ima_log_string(ab, "obj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500913 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500914 LSM_OBJ_ROLE,
915 AUDIT_OBJ_ROLE);
916 break;
917 case Opt_obj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400918 ima_log_string(ab, "obj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500919 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500920 LSM_OBJ_TYPE,
921 AUDIT_OBJ_TYPE);
922 break;
923 case Opt_subj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400924 ima_log_string(ab, "subj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500925 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500926 LSM_SUBJ_USER,
927 AUDIT_SUBJ_USER);
928 break;
929 case Opt_subj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400930 ima_log_string(ab, "subj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500931 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500932 LSM_SUBJ_ROLE,
933 AUDIT_SUBJ_ROLE);
934 break;
935 case Opt_subj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400936 ima_log_string(ab, "subj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500937 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500938 LSM_SUBJ_TYPE,
939 AUDIT_SUBJ_TYPE);
940 break;
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300941 case Opt_appraise_type:
942 if (entry->action != APPRAISE) {
943 result = -EINVAL;
944 break;
945 }
946
947 ima_log_string(ab, "appraise_type", args[0].from);
948 if ((strcmp(args[0].from, "imasig")) == 0)
949 entry->flags |= IMA_DIGSIG_REQUIRED;
950 else
951 result = -EINVAL;
952 break;
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400953 case Opt_permit_directio:
954 entry->flags |= IMA_PERMIT_DIRECTIO;
955 break;
Eric Richter02606432016-06-01 13:14:01 -0500956 case Opt_pcr:
957 if (entry->action != MEASURE) {
958 result = -EINVAL;
959 break;
960 }
961 ima_log_string(ab, "pcr", args[0].from);
962
963 result = kstrtoint(args[0].from, 10, &entry->pcr);
964 if (result || INVALID_PCR(entry->pcr))
965 result = -EINVAL;
966 else
967 entry->flags |= IMA_PCR;
968
969 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500970 case Opt_err:
Eric Paris2f1506c2010-04-20 10:21:30 -0400971 ima_log_string(ab, "UNKNOWN", p);
Eric Parise9d393b2010-04-20 10:21:13 -0400972 result = -EINVAL;
Mimi Zohar4af46622009-02-04 09:07:00 -0500973 break;
974 }
975 }
Eric Paris7b62e162010-04-20 10:21:01 -0400976 if (!result && (entry->action == UNKNOWN))
Mimi Zohar4af46622009-02-04 09:07:00 -0500977 result = -EINVAL;
Mimi Zohar6f0911a2018-04-12 00:15:22 -0400978 else if (entry->action == APPRAISE)
979 temp_ima_appraise |= ima_appraise_flag(entry->func);
980
Eric Parisb0d5de42012-02-14 17:11:07 -0500981 audit_log_format(ab, "res=%d", !result);
Mimi Zohar4af46622009-02-04 09:07:00 -0500982 audit_log_end(ab);
983 return result;
984}
985
986/**
Mimi Zohar07f6a792011-03-09 22:25:48 -0500987 * ima_parse_add_rule - add a rule to ima_policy_rules
Mimi Zohar4af46622009-02-04 09:07:00 -0500988 * @rule - ima measurement policy rule
989 *
Petko Manolov38d859f2015-12-02 17:47:54 +0200990 * Avoid locking by allowing just one writer at a time in ima_write_policy()
Eric Paris6ccd0452010-04-20 10:20:54 -0400991 * Returns the length of the rule parsed, an error code on failure
Mimi Zohar4af46622009-02-04 09:07:00 -0500992 */
Eric Paris6ccd0452010-04-20 10:20:54 -0400993ssize_t ima_parse_add_rule(char *rule)
Mimi Zohar4af46622009-02-04 09:07:00 -0500994{
Mimi Zohar52a13282013-12-11 14:44:04 -0500995 static const char op[] = "update_policy";
Eric Paris6ccd0452010-04-20 10:20:54 -0400996 char *p;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500997 struct ima_rule_entry *entry;
Eric Paris6ccd0452010-04-20 10:20:54 -0400998 ssize_t result, len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500999 int audit_info = 0;
1000
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +03001001 p = strsep(&rule, "\n");
1002 len = strlen(p) + 1;
Dmitry Kasatkin71787842014-10-03 14:40:20 +03001003 p += strspn(p, " \t");
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +03001004
Dmitry Kasatkin71787842014-10-03 14:40:20 +03001005 if (*p == '#' || *p == '\0')
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +03001006 return len;
1007
Mimi Zohar4af46622009-02-04 09:07:00 -05001008 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1009 if (!entry) {
1010 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1011 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
1012 return -ENOMEM;
1013 }
1014
1015 INIT_LIST_HEAD(&entry->list);
1016
Eric Paris6ccd0452010-04-20 10:20:54 -04001017 result = ima_parse_rule(p, entry);
Eric Paris7233e3e2010-04-20 10:21:24 -04001018 if (result) {
Mimi Zohar4af46622009-02-04 09:07:00 -05001019 kfree(entry);
Mimi Zohar523979a2009-02-11 11:12:28 -05001020 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
Richard Guy Briggs7e9001f2014-06-16 15:52:07 -04001021 NULL, op, "invalid-policy", result,
Mimi Zohar523979a2009-02-11 11:12:28 -05001022 audit_info);
Eric Paris7233e3e2010-04-20 10:21:24 -04001023 return result;
Mimi Zohar523979a2009-02-11 11:12:28 -05001024 }
Eric Paris7233e3e2010-04-20 10:21:24 -04001025
Petko Manolov38d859f2015-12-02 17:47:54 +02001026 list_add_tail(&entry->list, &ima_temp_rules);
Eric Paris7233e3e2010-04-20 10:21:24 -04001027
1028 return len;
Mimi Zohar4af46622009-02-04 09:07:00 -05001029}
1030
Petko Manolov38d859f2015-12-02 17:47:54 +02001031/**
1032 * ima_delete_rules() called to cleanup invalid in-flight policy.
1033 * We don't need locking as we operate on the temp list, which is
1034 * different from the active one. There is also only one user of
1035 * ima_delete_rules() at a time.
1036 */
James Morris64c61d82009-02-05 09:28:26 +11001037void ima_delete_rules(void)
Mimi Zohar4af46622009-02-04 09:07:00 -05001038{
Mimi Zohar07f6a792011-03-09 22:25:48 -05001039 struct ima_rule_entry *entry, *tmp;
Mimi Zohar7163a992013-01-03 14:19:09 -05001040 int i;
Mimi Zohar4af46622009-02-04 09:07:00 -05001041
Mimi Zohar6ad6afa2015-12-07 14:35:47 -05001042 temp_ima_appraise = 0;
Petko Manolov38d859f2015-12-02 17:47:54 +02001043 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
Mimi Zohar7163a992013-01-03 14:19:09 -05001044 for (i = 0; i < MAX_LSM_RULES; i++)
1045 kfree(entry->lsm[i].args_p);
1046
Mimi Zohar4af46622009-02-04 09:07:00 -05001047 list_del(&entry->list);
1048 kfree(entry);
1049 }
Mimi Zohar4af46622009-02-04 09:07:00 -05001050}
Petko Manolov80eae202015-12-02 17:47:56 +02001051
1052#ifdef CONFIG_IMA_READ_POLICY
1053enum {
1054 mask_exec = 0, mask_write, mask_read, mask_append
1055};
1056
Thiago Jung Bauermannbb543e32017-06-07 22:49:10 -03001057static const char *const mask_tokens[] = {
Petko Manolov80eae202015-12-02 17:47:56 +02001058 "MAY_EXEC",
1059 "MAY_WRITE",
1060 "MAY_READ",
1061 "MAY_APPEND"
1062};
1063
Thiago Jung Bauermann26632182017-06-07 22:49:11 -03001064#define __ima_hook_stringify(str) (#str),
Petko Manolov80eae202015-12-02 17:47:56 +02001065
Thiago Jung Bauermannbb543e32017-06-07 22:49:10 -03001066static const char *const func_tokens[] = {
Thiago Jung Bauermann26632182017-06-07 22:49:11 -03001067 __ima_hooks(__ima_hook_stringify)
Petko Manolov80eae202015-12-02 17:47:56 +02001068};
1069
1070void *ima_policy_start(struct seq_file *m, loff_t *pos)
1071{
1072 loff_t l = *pos;
1073 struct ima_rule_entry *entry;
1074
1075 rcu_read_lock();
1076 list_for_each_entry_rcu(entry, ima_rules, list) {
1077 if (!l--) {
1078 rcu_read_unlock();
1079 return entry;
1080 }
1081 }
1082 rcu_read_unlock();
1083 return NULL;
1084}
1085
1086void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
1087{
1088 struct ima_rule_entry *entry = v;
1089
1090 rcu_read_lock();
1091 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
1092 rcu_read_unlock();
1093 (*pos)++;
1094
1095 return (&entry->list == ima_rules) ? NULL : entry;
1096}
1097
1098void ima_policy_stop(struct seq_file *m, void *v)
1099{
1100}
1101
1102#define pt(token) policy_tokens[token + Opt_err].pattern
1103#define mt(token) mask_tokens[token]
Petko Manolov80eae202015-12-02 17:47:56 +02001104
Mimi Zoharb5269ab2016-01-28 13:10:36 -05001105/*
1106 * policy_func_show - display the ima_hooks policy rule
1107 */
1108static void policy_func_show(struct seq_file *m, enum ima_hooks func)
1109{
Thiago Jung Bauermann26632182017-06-07 22:49:11 -03001110 if (func > 0 && func < MAX_CHECK)
1111 seq_printf(m, "func=%s ", func_tokens[func]);
1112 else
1113 seq_printf(m, "func=%d ", func);
Mimi Zoharb5269ab2016-01-28 13:10:36 -05001114}
1115
Petko Manolov80eae202015-12-02 17:47:56 +02001116int ima_policy_show(struct seq_file *m, void *v)
1117{
1118 struct ima_rule_entry *entry = v;
Andy Shevchenkob8b57272016-05-20 17:00:57 -07001119 int i;
Petko Manolov80eae202015-12-02 17:47:56 +02001120 char tbuf[64] = {0,};
1121
1122 rcu_read_lock();
1123
1124 if (entry->action & MEASURE)
1125 seq_puts(m, pt(Opt_measure));
1126 if (entry->action & DONT_MEASURE)
1127 seq_puts(m, pt(Opt_dont_measure));
1128 if (entry->action & APPRAISE)
1129 seq_puts(m, pt(Opt_appraise));
1130 if (entry->action & DONT_APPRAISE)
1131 seq_puts(m, pt(Opt_dont_appraise));
1132 if (entry->action & AUDIT)
1133 seq_puts(m, pt(Opt_audit));
Mimi Zoharda1b0022016-09-29 10:04:52 -04001134 if (entry->action & HASH)
1135 seq_puts(m, pt(Opt_hash));
1136 if (entry->action & DONT_HASH)
1137 seq_puts(m, pt(Opt_dont_hash));
Petko Manolov80eae202015-12-02 17:47:56 +02001138
1139 seq_puts(m, " ");
1140
Mimi Zoharb5269ab2016-01-28 13:10:36 -05001141 if (entry->flags & IMA_FUNC)
1142 policy_func_show(m, entry->func);
Petko Manolov80eae202015-12-02 17:47:56 +02001143
1144 if (entry->flags & IMA_MASK) {
1145 if (entry->mask & MAY_EXEC)
1146 seq_printf(m, pt(Opt_mask), mt(mask_exec));
1147 if (entry->mask & MAY_WRITE)
1148 seq_printf(m, pt(Opt_mask), mt(mask_write));
1149 if (entry->mask & MAY_READ)
1150 seq_printf(m, pt(Opt_mask), mt(mask_read));
1151 if (entry->mask & MAY_APPEND)
1152 seq_printf(m, pt(Opt_mask), mt(mask_append));
1153 seq_puts(m, " ");
1154 }
1155
1156 if (entry->flags & IMA_FSMAGIC) {
1157 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
1158 seq_printf(m, pt(Opt_fsmagic), tbuf);
1159 seq_puts(m, " ");
1160 }
1161
Mimi Zoharf1b08bb2018-01-15 11:20:36 -05001162 if (entry->flags & IMA_FSNAME) {
1163 snprintf(tbuf, sizeof(tbuf), "%s", entry->fsname);
1164 seq_printf(m, pt(Opt_fsname), tbuf);
1165 seq_puts(m, " ");
1166 }
1167
Eric Richter02606432016-06-01 13:14:01 -05001168 if (entry->flags & IMA_PCR) {
1169 snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
1170 seq_printf(m, pt(Opt_pcr), tbuf);
1171 seq_puts(m, " ");
1172 }
1173
Petko Manolov80eae202015-12-02 17:47:56 +02001174 if (entry->flags & IMA_FSUUID) {
Christoph Hellwig787d8c52017-06-01 07:00:26 +02001175 seq_printf(m, "fsuuid=%pU", &entry->fsuuid);
Petko Manolov80eae202015-12-02 17:47:56 +02001176 seq_puts(m, " ");
1177 }
1178
1179 if (entry->flags & IMA_UID) {
1180 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001181 if (entry->uid_op == &uid_gt)
1182 seq_printf(m, pt(Opt_uid_gt), tbuf);
1183 else if (entry->uid_op == &uid_lt)
1184 seq_printf(m, pt(Opt_uid_lt), tbuf);
1185 else
1186 seq_printf(m, pt(Opt_uid_eq), tbuf);
Petko Manolov80eae202015-12-02 17:47:56 +02001187 seq_puts(m, " ");
1188 }
1189
1190 if (entry->flags & IMA_EUID) {
1191 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001192 if (entry->uid_op == &uid_gt)
1193 seq_printf(m, pt(Opt_euid_gt), tbuf);
1194 else if (entry->uid_op == &uid_lt)
1195 seq_printf(m, pt(Opt_euid_lt), tbuf);
1196 else
1197 seq_printf(m, pt(Opt_euid_eq), tbuf);
Petko Manolov80eae202015-12-02 17:47:56 +02001198 seq_puts(m, " ");
1199 }
1200
1201 if (entry->flags & IMA_FOWNER) {
1202 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001203 if (entry->fowner_op == &uid_gt)
1204 seq_printf(m, pt(Opt_fowner_gt), tbuf);
1205 else if (entry->fowner_op == &uid_lt)
1206 seq_printf(m, pt(Opt_fowner_lt), tbuf);
1207 else
1208 seq_printf(m, pt(Opt_fowner_eq), tbuf);
Petko Manolov80eae202015-12-02 17:47:56 +02001209 seq_puts(m, " ");
1210 }
1211
1212 for (i = 0; i < MAX_LSM_RULES; i++) {
1213 if (entry->lsm[i].rule) {
1214 switch (i) {
1215 case LSM_OBJ_USER:
1216 seq_printf(m, pt(Opt_obj_user),
1217 (char *)entry->lsm[i].args_p);
1218 break;
1219 case LSM_OBJ_ROLE:
1220 seq_printf(m, pt(Opt_obj_role),
1221 (char *)entry->lsm[i].args_p);
1222 break;
1223 case LSM_OBJ_TYPE:
1224 seq_printf(m, pt(Opt_obj_type),
1225 (char *)entry->lsm[i].args_p);
1226 break;
1227 case LSM_SUBJ_USER:
1228 seq_printf(m, pt(Opt_subj_user),
1229 (char *)entry->lsm[i].args_p);
1230 break;
1231 case LSM_SUBJ_ROLE:
1232 seq_printf(m, pt(Opt_subj_role),
1233 (char *)entry->lsm[i].args_p);
1234 break;
1235 case LSM_SUBJ_TYPE:
1236 seq_printf(m, pt(Opt_subj_type),
1237 (char *)entry->lsm[i].args_p);
1238 break;
1239 }
1240 }
1241 }
1242 if (entry->flags & IMA_DIGSIG_REQUIRED)
1243 seq_puts(m, "appraise_type=imasig ");
1244 if (entry->flags & IMA_PERMIT_DIRECTIO)
1245 seq_puts(m, "permit_directio ");
1246 rcu_read_unlock();
1247 seq_puts(m, "\n");
1248 return 0;
1249}
1250#endif /* CONFIG_IMA_READ_POLICY */