blob: 3ab1067db624d860f9303a5e3ea8587ebbed699b [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 Zohar3323eec2009-02-04 09:06:58 -050036
Dmitry Kasatkin45e24722012-09-12 20:51:32 +030037#define UNKNOWN 0
38#define MEASURE 0x0001 /* same as IMA_MEASURE */
39#define DONT_MEASURE 0x0002
40#define APPRAISE 0x0004 /* same as IMA_APPRAISE */
41#define DONT_APPRAISE 0x0008
Peter Moodye7c568e2012-06-14 10:04:36 -070042#define AUDIT 0x0040
Mimi Zohar4af46622009-02-04 09:07:00 -050043
Eric Richter02606432016-06-01 13:14:01 -050044#define INVALID_PCR(a) (((a) < 0) || \
45 (a) >= (FIELD_SIZEOF(struct integrity_iint_cache, measured_pcrs) * 8))
46
Roberto Sassua7560242014-09-12 19:35:54 +020047int ima_policy_flag;
Mimi Zohar6ad6afa2015-12-07 14:35:47 -050048static int temp_ima_appraise;
Roberto Sassua7560242014-09-12 19:35:54 +020049
Mimi Zohar4af46622009-02-04 09:07:00 -050050#define MAX_LSM_RULES 6
51enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
52 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
53};
Mimi Zohar3323eec2009-02-04 09:06:58 -050054
Mimi Zohar24fd03c2015-06-11 20:48:33 -040055enum policy_types { ORIGINAL_TCB = 1, DEFAULT_TCB };
56
Mimi Zohar07f6a792011-03-09 22:25:48 -050057struct ima_rule_entry {
Mimi Zohar3323eec2009-02-04 09:06:58 -050058 struct list_head list;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050059 int action;
Mimi Zohar3323eec2009-02-04 09:06:58 -050060 unsigned int flags;
61 enum ima_hooks func;
62 int mask;
63 unsigned long fsmagic;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +030064 u8 fsuuid[16];
Eric W. Biederman8b94eea2012-05-25 18:24:12 -060065 kuid_t uid;
Linus Torvalds882653222012-10-02 21:38:48 -070066 kuid_t fowner;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +030067 bool (*uid_op)(kuid_t, kuid_t); /* Handlers for operators */
68 bool (*fowner_op)(kuid_t, kuid_t); /* uid_eq(), uid_gt(), uid_lt() */
Eric Richter02606432016-06-01 13:14:01 -050069 int pcr;
Mimi Zohar4af46622009-02-04 09:07:00 -050070 struct {
71 void *rule; /* LSM file metadata specific */
Mimi Zohar7163a992013-01-03 14:19:09 -050072 void *args_p; /* audit value */
Mimi Zohar4af46622009-02-04 09:07:00 -050073 int type; /* audit type */
74 } lsm[MAX_LSM_RULES];
Mimi Zohar3323eec2009-02-04 09:06:58 -050075};
76
Eric Paris5789ba32009-05-21 15:47:06 -040077/*
78 * Without LSM specific knowledge, the default policy can only be
Mimi Zohar07f6a792011-03-09 22:25:48 -050079 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
Mimi Zohar4af46622009-02-04 09:07:00 -050080 */
Eric Paris5789ba32009-05-21 15:47:06 -040081
82/*
83 * The minimum rule set to allow for full TCB coverage. Measures all files
84 * opened or mmap for exec and everything read by root. Dangerous because
85 * normal users can easily run the machine out of memory simply building
86 * and running executables.
87 */
James Morrisbad44172017-02-13 16:34:35 +110088static struct ima_rule_entry dont_measure_rules[] __ro_after_init = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020089 {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
90 {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
91 {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
92 {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
93 {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
94 {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
95 {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
96 {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
Roberto Sassu6438de92015-04-11 17:13:06 +020097 {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
98 .flags = IMA_FSMAGIC},
Mimi Zohar24fd03c2015-06-11 20:48:33 -040099 {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC}
100};
101
James Morrisbad44172017-02-13 16:34:35 +1100102static struct ima_rule_entry original_measurement_rules[] __ro_after_init = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200103 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500104 .flags = IMA_FUNC | IMA_MASK},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200105 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500106 .flags = IMA_FUNC | IMA_MASK},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400107 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300108 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
109 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400110 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
111 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
112};
113
James Morrisbad44172017-02-13 16:34:35 +1100114static struct ima_rule_entry default_measurement_rules[] __ro_after_init = {
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400115 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
116 .flags = IMA_FUNC | IMA_MASK},
117 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
118 .flags = IMA_FUNC | IMA_MASK},
119 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300120 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
121 .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400122 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300123 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
124 .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200125 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400126 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
Mimi Zohar19f8a842016-01-15 10:17:12 -0500127 {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC},
Mimi Zohar3323eec2009-02-04 09:06:58 -0500128};
129
James Morrisbad44172017-02-13 16:34:35 +1100130static struct ima_rule_entry default_appraise_rules[] __ro_after_init = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200131 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
132 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
133 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
134 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
135 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
136 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
137 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
138 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
139 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
Mimi Zoharcd025f72015-04-21 16:54:24 -0400140 {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200141 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
Mimi Zohar95ee08f2015-12-07 15:08:01 -0500142#ifdef CONFIG_IMA_WRITE_POLICY
143 {.action = APPRAISE, .func = POLICY_CHECK,
144 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
145#endif
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200146#ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300147 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
148 .flags = IMA_FOWNER},
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200149#else
150 /* force signature */
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300151 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200152 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
153#endif
Mimi Zohar07f6a792011-03-09 22:25:48 -0500154};
Mimi Zohar3323eec2009-02-04 09:06:58 -0500155
Mimi Zohar07f6a792011-03-09 22:25:48 -0500156static LIST_HEAD(ima_default_rules);
157static LIST_HEAD(ima_policy_rules);
Petko Manolov38d859f2015-12-02 17:47:54 +0200158static LIST_HEAD(ima_temp_rules);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500159static struct list_head *ima_rules;
160
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400161static int ima_policy __initdata;
Petko Manolov38d859f2015-12-02 17:47:54 +0200162
Mimi Zohar07f6a792011-03-09 22:25:48 -0500163static int __init default_measure_policy_setup(char *str)
Eric Paris5789ba32009-05-21 15:47:06 -0400164{
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400165 if (ima_policy)
166 return 1;
167
168 ima_policy = ORIGINAL_TCB;
Eric Paris5789ba32009-05-21 15:47:06 -0400169 return 1;
170}
Mimi Zohar07f6a792011-03-09 22:25:48 -0500171__setup("ima_tcb", default_measure_policy_setup);
172
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400173static int __init policy_setup(char *str)
174{
175 if (ima_policy)
176 return 1;
177
178 if (strcmp(str, "tcb") == 0)
179 ima_policy = DEFAULT_TCB;
180
181 return 1;
182}
183__setup("ima_policy=", policy_setup);
184
Mimi Zohar07f6a792011-03-09 22:25:48 -0500185static bool ima_use_appraise_tcb __initdata;
186static int __init default_appraise_policy_setup(char *str)
187{
188 ima_use_appraise_tcb = 1;
189 return 1;
190}
191__setup("ima_appraise_tcb", default_appraise_policy_setup);
Eric Paris5789ba32009-05-21 15:47:06 -0400192
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200193/*
Petko Manolov38d859f2015-12-02 17:47:54 +0200194 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
195 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
196 * the reloaded LSM policy. We assume the rules still exist; and BUG_ON() if
197 * they don't.
Mimi Zohar7163a992013-01-03 14:19:09 -0500198 */
199static void ima_lsm_update_rules(void)
200{
Petko Manolov38d859f2015-12-02 17:47:54 +0200201 struct ima_rule_entry *entry;
Mimi Zohar7163a992013-01-03 14:19:09 -0500202 int result;
203 int i;
204
Petko Manolov38d859f2015-12-02 17:47:54 +0200205 list_for_each_entry(entry, &ima_policy_rules, list) {
Mimi Zohar7163a992013-01-03 14:19:09 -0500206 for (i = 0; i < MAX_LSM_RULES; i++) {
207 if (!entry->lsm[i].rule)
208 continue;
209 result = security_filter_rule_init(entry->lsm[i].type,
210 Audit_equal,
211 entry->lsm[i].args_p,
212 &entry->lsm[i].rule);
213 BUG_ON(!entry->lsm[i].rule);
214 }
215 }
Mimi Zohar7163a992013-01-03 14:19:09 -0500216}
217
Mimi Zohar3323eec2009-02-04 09:06:58 -0500218/**
219 * ima_match_rules - determine whether an inode matches the measure rule.
220 * @rule: a pointer to a rule
221 * @inode: a pointer to an inode
222 * @func: LIM hook identifier
223 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
224 *
225 * Returns true on rule match, false on failure.
226 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500227static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,
228 enum ima_hooks func, int mask)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500229{
230 struct task_struct *tsk = current;
Mimi Zohar3db59dd2012-01-17 22:11:28 -0500231 const struct cred *cred = current_cred();
Mimi Zohar4af46622009-02-04 09:07:00 -0500232 int i;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500233
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200234 if ((rule->flags & IMA_FUNC) &&
235 (rule->func != func && func != POST_SETATTR))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500236 return false;
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200237 if ((rule->flags & IMA_MASK) &&
238 (rule->mask != mask && func != POST_SETATTR))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500239 return false;
Mimi Zohar4351c292014-11-05 07:53:55 -0500240 if ((rule->flags & IMA_INMASK) &&
241 (!(rule->mask & mask) && func != POST_SETATTR))
242 return false;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500243 if ((rule->flags & IMA_FSMAGIC)
244 && rule->fsmagic != inode->i_sb->s_magic)
245 return false;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300246 if ((rule->flags & IMA_FSUUID) &&
Mimi Zohar446d64e2013-02-24 23:42:37 -0500247 memcmp(rule->fsuuid, inode->i_sb->s_uuid, sizeof(rule->fsuuid)))
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300248 return false;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300249 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500250 return false;
Mimi Zohar139069e2014-11-05 07:48:36 -0500251 if (rule->flags & IMA_EUID) {
252 if (has_capability_noaudit(current, CAP_SETUID)) {
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300253 if (!rule->uid_op(cred->euid, rule->uid)
254 && !rule->uid_op(cred->suid, rule->uid)
255 && !rule->uid_op(cred->uid, rule->uid))
Mimi Zohar139069e2014-11-05 07:48:36 -0500256 return false;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300257 } else if (!rule->uid_op(cred->euid, rule->uid))
Mimi Zohar139069e2014-11-05 07:48:36 -0500258 return false;
259 }
260
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300261 if ((rule->flags & IMA_FOWNER) &&
262 !rule->fowner_op(inode->i_uid, rule->fowner))
Mimi Zohar07f6a792011-03-09 22:25:48 -0500263 return false;
Mimi Zohar4af46622009-02-04 09:07:00 -0500264 for (i = 0; i < MAX_LSM_RULES; i++) {
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400265 int rc = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500266 u32 osid, sid;
Mimi Zohar7163a992013-01-03 14:19:09 -0500267 int retried = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500268
269 if (!rule->lsm[i].rule)
270 continue;
Mimi Zohar7163a992013-01-03 14:19:09 -0500271retry:
Mimi Zohar4af46622009-02-04 09:07:00 -0500272 switch (i) {
273 case LSM_OBJ_USER:
274 case LSM_OBJ_ROLE:
275 case LSM_OBJ_TYPE:
276 security_inode_getsecid(inode, &osid);
277 rc = security_filter_rule_match(osid,
278 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400279 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500280 rule->lsm[i].rule,
281 NULL);
282 break;
283 case LSM_SUBJ_USER:
284 case LSM_SUBJ_ROLE:
285 case LSM_SUBJ_TYPE:
286 security_task_getsecid(tsk, &sid);
287 rc = security_filter_rule_match(sid,
288 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400289 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500290 rule->lsm[i].rule,
291 NULL);
292 default:
293 break;
294 }
Mimi Zohar7163a992013-01-03 14:19:09 -0500295 if ((rc < 0) && (!retried)) {
296 retried = 1;
297 ima_lsm_update_rules();
298 goto retry;
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200299 }
Mimi Zohar4af46622009-02-04 09:07:00 -0500300 if (!rc)
301 return false;
302 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500303 return true;
304}
305
Mimi Zohard79d72e2012-12-03 17:08:11 -0500306/*
307 * In addition to knowing that we need to appraise the file in general,
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500308 * we need to differentiate between calling hooks, for hook specific rules.
Mimi Zohard79d72e2012-12-03 17:08:11 -0500309 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500310static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
Mimi Zohard79d72e2012-12-03 17:08:11 -0500311{
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500312 if (!(rule->flags & IMA_FUNC))
313 return IMA_FILE_APPRAISE;
314
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200315 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500316 case MMAP_CHECK:
317 return IMA_MMAP_APPRAISE;
318 case BPRM_CHECK:
319 return IMA_BPRM_APPRAISE;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500320 case FILE_CHECK:
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500321 case POST_SETATTR:
Mimi Zohard79d72e2012-12-03 17:08:11 -0500322 return IMA_FILE_APPRAISE;
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500323 case MODULE_CHECK ... MAX_CHECK - 1:
324 default:
325 return IMA_READ_APPRAISE;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500326 }
327}
328
Mimi Zohar3323eec2009-02-04 09:06:58 -0500329/**
330 * ima_match_policy - decision based on LSM and other conditions
331 * @inode: pointer to an inode for which the policy decision is being made
332 * @func: IMA hook identifier
333 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
Eric Richter725de7f2016-06-01 13:14:02 -0500334 * @pcr: set the pcr to extend
Mimi Zohar3323eec2009-02-04 09:06:58 -0500335 *
336 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
337 * conditions.
338 *
Petko Manolov38d859f2015-12-02 17:47:54 +0200339 * Since the IMA policy may be updated multiple times we need to lock the
340 * list when walking it. Reads are many orders of magnitude more numerous
341 * than writes so ima_match_policy() is classical RCU candidate.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500342 */
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500343int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
Eric Richter725de7f2016-06-01 13:14:02 -0500344 int flags, int *pcr)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500345{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500346 struct ima_rule_entry *entry;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500347 int action = 0, actmask = flags | (flags << 1);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500348
Petko Manolov38d859f2015-12-02 17:47:54 +0200349 rcu_read_lock();
350 list_for_each_entry_rcu(entry, ima_rules, list) {
Mimi Zohar3323eec2009-02-04 09:06:58 -0500351
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500352 if (!(entry->action & actmask))
353 continue;
354
355 if (!ima_match_rules(entry, inode, func, mask))
356 continue;
357
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300358 action |= entry->flags & IMA_ACTION_FLAGS;
359
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300360 action |= entry->action & IMA_DO_MASK;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500361 if (entry->action & IMA_APPRAISE)
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500362 action |= get_subaction(entry, func);
Mimi Zohard79d72e2012-12-03 17:08:11 -0500363
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300364 if (entry->action & IMA_DO_MASK)
365 actmask &= ~(entry->action | entry->action << 1);
366 else
367 actmask &= ~(entry->action | entry->action >> 1);
368
Eric Richter725de7f2016-06-01 13:14:02 -0500369 if ((pcr) && (entry->flags & IMA_PCR))
370 *pcr = entry->pcr;
371
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500372 if (!actmask)
373 break;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500374 }
Petko Manolov38d859f2015-12-02 17:47:54 +0200375 rcu_read_unlock();
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500376
377 return action;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500378}
379
Roberto Sassua7560242014-09-12 19:35:54 +0200380/*
381 * Initialize the ima_policy_flag variable based on the currently
382 * loaded policy. Based on this flag, the decision to short circuit
383 * out of a function or not call the function in the first place
384 * can be made earlier.
385 */
386void ima_update_policy_flag(void)
387{
388 struct ima_rule_entry *entry;
389
Roberto Sassua7560242014-09-12 19:35:54 +0200390 list_for_each_entry(entry, ima_rules, list) {
391 if (entry->action & IMA_DO_MASK)
392 ima_policy_flag |= entry->action;
393 }
394
Mimi Zohar6ad6afa2015-12-07 14:35:47 -0500395 ima_appraise |= temp_ima_appraise;
Roberto Sassua7560242014-09-12 19:35:54 +0200396 if (!ima_appraise)
397 ima_policy_flag &= ~IMA_APPRAISE;
398}
399
Mimi Zohar3323eec2009-02-04 09:06:58 -0500400/**
401 * ima_init_policy - initialize the default measure rules.
402 *
Mimi Zohar07f6a792011-03-09 22:25:48 -0500403 * ima_rules points to either the ima_default_rules or the
404 * the new ima_policy_rules.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500405 */
Eric Paris932995f2009-05-21 15:43:32 -0400406void __init ima_init_policy(void)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500407{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500408 int i, measure_entries, appraise_entries;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500409
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400410 /* if !ima_policy set entries = 0 so we load NO default rules */
411 measure_entries = ima_policy ? ARRAY_SIZE(dont_measure_rules) : 0;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500412 appraise_entries = ima_use_appraise_tcb ?
413 ARRAY_SIZE(default_appraise_rules) : 0;
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200414
Dan Carpenter55778572015-04-07 12:22:11 +0300415 for (i = 0; i < measure_entries; i++)
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400416 list_add_tail(&dont_measure_rules[i].list, &ima_default_rules);
417
418 switch (ima_policy) {
419 case ORIGINAL_TCB:
420 for (i = 0; i < ARRAY_SIZE(original_measurement_rules); i++)
421 list_add_tail(&original_measurement_rules[i].list,
422 &ima_default_rules);
423 break;
424 case DEFAULT_TCB:
425 for (i = 0; i < ARRAY_SIZE(default_measurement_rules); i++)
426 list_add_tail(&default_measurement_rules[i].list,
427 &ima_default_rules);
428 default:
429 break;
430 }
Eric Paris5789ba32009-05-21 15:47:06 -0400431
Dan Carpenter55778572015-04-07 12:22:11 +0300432 for (i = 0; i < appraise_entries; i++) {
433 list_add_tail(&default_appraise_rules[i].list,
434 &ima_default_rules);
Mimi Zohar95ee08f2015-12-07 15:08:01 -0500435 if (default_appraise_rules[i].func == POLICY_CHECK)
436 temp_ima_appraise |= IMA_APPRAISE_POLICY;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500437 }
438
439 ima_rules = &ima_default_rules;
Mimi Zohar95ee08f2015-12-07 15:08:01 -0500440 ima_update_policy_flag();
Mimi Zohar3323eec2009-02-04 09:06:58 -0500441}
Mimi Zohar4af46622009-02-04 09:07:00 -0500442
Sasha Levin01127212015-12-22 08:51:23 -0500443/* Make sure we have a valid policy, at least containing some rules. */
Colin Ian Kingc75d8e92016-01-20 11:13:46 +0000444int ima_check_policy(void)
Sasha Levin01127212015-12-22 08:51:23 -0500445{
446 if (list_empty(&ima_temp_rules))
447 return -EINVAL;
448 return 0;
449}
450
Mimi Zohar4af46622009-02-04 09:07:00 -0500451/**
452 * ima_update_policy - update default_rules with new measure rules
453 *
454 * Called on file .release to update the default rules with a complete new
Petko Manolov38d859f2015-12-02 17:47:54 +0200455 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
456 * they make a queue. The policy may be updated multiple times and this is the
457 * RCU updater.
458 *
459 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
460 * we switch from the default policy to user defined.
Mimi Zohar4af46622009-02-04 09:07:00 -0500461 */
462void ima_update_policy(void)
463{
Petko Manolov38d859f2015-12-02 17:47:54 +0200464 struct list_head *first, *last, *policy;
465
466 /* append current policy with the new rules */
467 first = (&ima_temp_rules)->next;
468 last = (&ima_temp_rules)->prev;
469 policy = &ima_policy_rules;
470
471 synchronize_rcu();
472
473 last->next = policy;
474 rcu_assign_pointer(list_next_rcu(policy->prev), first);
475 first->prev = policy->prev;
476 policy->prev = last;
477
478 /* prepare for the next policy rules addition */
479 INIT_LIST_HEAD(&ima_temp_rules);
480
481 if (ima_rules != policy) {
482 ima_policy_flag = 0;
483 ima_rules = policy;
484 }
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300485 ima_update_policy_flag();
Mimi Zohar4af46622009-02-04 09:07:00 -0500486}
487
488enum {
489 Opt_err = -1,
490 Opt_measure = 1, Opt_dont_measure,
Mimi Zohar07f6a792011-03-09 22:25:48 -0500491 Opt_appraise, Opt_dont_appraise,
Peter Moodye7c568e2012-06-14 10:04:36 -0700492 Opt_audit,
Mimi Zohar4af46622009-02-04 09:07:00 -0500493 Opt_obj_user, Opt_obj_role, Opt_obj_type,
494 Opt_subj_user, Opt_subj_role, Opt_subj_type,
Mimi Zohar139069e2014-11-05 07:48:36 -0500495 Opt_func, Opt_mask, Opt_fsmagic,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300496 Opt_fsuuid, Opt_uid_eq, Opt_euid_eq, Opt_fowner_eq,
497 Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt,
498 Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt,
Eric Richter02606432016-06-01 13:14:01 -0500499 Opt_appraise_type, Opt_permit_directio,
500 Opt_pcr
Mimi Zohar4af46622009-02-04 09:07:00 -0500501};
502
503static match_table_t policy_tokens = {
504 {Opt_measure, "measure"},
505 {Opt_dont_measure, "dont_measure"},
Mimi Zohar07f6a792011-03-09 22:25:48 -0500506 {Opt_appraise, "appraise"},
507 {Opt_dont_appraise, "dont_appraise"},
Peter Moodye7c568e2012-06-14 10:04:36 -0700508 {Opt_audit, "audit"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500509 {Opt_obj_user, "obj_user=%s"},
510 {Opt_obj_role, "obj_role=%s"},
511 {Opt_obj_type, "obj_type=%s"},
512 {Opt_subj_user, "subj_user=%s"},
513 {Opt_subj_role, "subj_role=%s"},
514 {Opt_subj_type, "subj_type=%s"},
515 {Opt_func, "func=%s"},
516 {Opt_mask, "mask=%s"},
517 {Opt_fsmagic, "fsmagic=%s"},
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300518 {Opt_fsuuid, "fsuuid=%s"},
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300519 {Opt_uid_eq, "uid=%s"},
520 {Opt_euid_eq, "euid=%s"},
521 {Opt_fowner_eq, "fowner=%s"},
522 {Opt_uid_gt, "uid>%s"},
523 {Opt_euid_gt, "euid>%s"},
524 {Opt_fowner_gt, "fowner>%s"},
525 {Opt_uid_lt, "uid<%s"},
526 {Opt_euid_lt, "euid<%s"},
527 {Opt_fowner_lt, "fowner<%s"},
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300528 {Opt_appraise_type, "appraise_type=%s"},
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400529 {Opt_permit_directio, "permit_directio"},
Eric Richter02606432016-06-01 13:14:01 -0500530 {Opt_pcr, "pcr=%s"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500531 {Opt_err, NULL}
532};
533
Mimi Zohar07f6a792011-03-09 22:25:48 -0500534static int ima_lsm_rule_init(struct ima_rule_entry *entry,
Mimi Zohar7163a992013-01-03 14:19:09 -0500535 substring_t *args, int lsm_rule, int audit_type)
Mimi Zohar4af46622009-02-04 09:07:00 -0500536{
537 int result;
538
Eric Paris7b62e162010-04-20 10:21:01 -0400539 if (entry->lsm[lsm_rule].rule)
540 return -EINVAL;
541
Mimi Zohar7163a992013-01-03 14:19:09 -0500542 entry->lsm[lsm_rule].args_p = match_strdup(args);
543 if (!entry->lsm[lsm_rule].args_p)
544 return -ENOMEM;
545
Mimi Zohar4af46622009-02-04 09:07:00 -0500546 entry->lsm[lsm_rule].type = audit_type;
547 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
Mimi Zohar7163a992013-01-03 14:19:09 -0500548 Audit_equal,
549 entry->lsm[lsm_rule].args_p,
Mimi Zohar4af46622009-02-04 09:07:00 -0500550 &entry->lsm[lsm_rule].rule);
Mimi Zohar7163a992013-01-03 14:19:09 -0500551 if (!entry->lsm[lsm_rule].rule) {
552 kfree(entry->lsm[lsm_rule].args_p);
Mimi Zohar867c2022011-01-03 14:59:10 -0800553 return -EINVAL;
Mimi Zohar7163a992013-01-03 14:19:09 -0500554 }
555
Mimi Zohar4af46622009-02-04 09:07:00 -0500556 return result;
557}
558
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300559static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value,
560 bool (*rule_operator)(kuid_t, kuid_t))
Eric Paris2f1506c2010-04-20 10:21:30 -0400561{
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300562 if (rule_operator == &uid_gt)
563 audit_log_format(ab, "%s>", key);
564 else if (rule_operator == &uid_lt)
565 audit_log_format(ab, "%s<", key);
566 else
567 audit_log_format(ab, "%s=", key);
Eric Paris2f1506c2010-04-20 10:21:30 -0400568 audit_log_untrustedstring(ab, value);
569 audit_log_format(ab, " ");
570}
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300571static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
572{
573 ima_log_string_op(ab, key, value, NULL);
574}
Eric Paris2f1506c2010-04-20 10:21:30 -0400575
Mimi Zohar07f6a792011-03-09 22:25:48 -0500576static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
Mimi Zohar4af46622009-02-04 09:07:00 -0500577{
578 struct audit_buffer *ab;
Mimi Zohar4351c292014-11-05 07:53:55 -0500579 char *from;
Mimi Zohar4af46622009-02-04 09:07:00 -0500580 char *p;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300581 bool uid_token;
Mimi Zohar4af46622009-02-04 09:07:00 -0500582 int result = 0;
583
Mimi Zohar523979a2009-02-11 11:12:28 -0500584 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
Mimi Zohar4af46622009-02-04 09:07:00 -0500585
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600586 entry->uid = INVALID_UID;
Linus Torvalds882653222012-10-02 21:38:48 -0700587 entry->fowner = INVALID_UID;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300588 entry->uid_op = &uid_eq;
589 entry->fowner_op = &uid_eq;
Eric Parisb9035b12010-04-20 10:21:07 -0400590 entry->action = UNKNOWN;
Eric Paris28ef4002010-04-20 10:21:18 -0400591 while ((p = strsep(&rule, " \t")) != NULL) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500592 substring_t args[MAX_OPT_ARGS];
593 int token;
594 unsigned long lnum;
595
596 if (result < 0)
597 break;
Eric Paris28ef4002010-04-20 10:21:18 -0400598 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
599 continue;
Mimi Zohar4af46622009-02-04 09:07:00 -0500600 token = match_token(p, policy_tokens, args);
601 switch (token) {
602 case Opt_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400603 ima_log_string(ab, "action", "measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400604
605 if (entry->action != UNKNOWN)
606 result = -EINVAL;
607
Mimi Zohar4af46622009-02-04 09:07:00 -0500608 entry->action = MEASURE;
609 break;
610 case Opt_dont_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400611 ima_log_string(ab, "action", "dont_measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400612
613 if (entry->action != UNKNOWN)
614 result = -EINVAL;
615
Mimi Zohar4af46622009-02-04 09:07:00 -0500616 entry->action = DONT_MEASURE;
617 break;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500618 case Opt_appraise:
619 ima_log_string(ab, "action", "appraise");
620
621 if (entry->action != UNKNOWN)
622 result = -EINVAL;
623
624 entry->action = APPRAISE;
625 break;
626 case Opt_dont_appraise:
627 ima_log_string(ab, "action", "dont_appraise");
628
629 if (entry->action != UNKNOWN)
630 result = -EINVAL;
631
632 entry->action = DONT_APPRAISE;
633 break;
Peter Moodye7c568e2012-06-14 10:04:36 -0700634 case Opt_audit:
635 ima_log_string(ab, "action", "audit");
636
637 if (entry->action != UNKNOWN)
638 result = -EINVAL;
639
640 entry->action = AUDIT;
641 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500642 case Opt_func:
Eric Paris2f1506c2010-04-20 10:21:30 -0400643 ima_log_string(ab, "func", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400644
645 if (entry->func)
Mimi Zohar07f6a792011-03-09 22:25:48 -0500646 result = -EINVAL;
Eric Paris7b62e162010-04-20 10:21:01 -0400647
Mimi Zohar1e93d002010-01-26 17:02:41 -0500648 if (strcmp(args[0].from, "FILE_CHECK") == 0)
649 entry->func = FILE_CHECK;
650 /* PATH_CHECK is for backwards compat */
651 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
652 entry->func = FILE_CHECK;
Mimi Zoharfdf90722012-10-16 12:40:08 +1030653 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
654 entry->func = MODULE_CHECK;
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400655 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
656 entry->func = FIRMWARE_CHECK;
Mimi Zohar16cac492012-12-13 11:15:04 -0500657 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
658 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
659 entry->func = MMAP_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500660 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
661 entry->func = BPRM_CHECK;
Mimi Zohard9ddf072016-01-14 20:59:14 -0500662 else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
663 0)
664 entry->func = KEXEC_KERNEL_CHECK;
665 else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
666 == 0)
667 entry->func = KEXEC_INITRAMFS_CHECK;
Mimi Zohar19f8a842016-01-15 10:17:12 -0500668 else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
669 entry->func = POLICY_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500670 else
671 result = -EINVAL;
672 if (!result)
673 entry->flags |= IMA_FUNC;
674 break;
675 case Opt_mask:
Eric Paris2f1506c2010-04-20 10:21:30 -0400676 ima_log_string(ab, "mask", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400677
678 if (entry->mask)
679 result = -EINVAL;
680
Mimi Zohar4351c292014-11-05 07:53:55 -0500681 from = args[0].from;
682 if (*from == '^')
683 from++;
684
685 if ((strcmp(from, "MAY_EXEC")) == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500686 entry->mask = MAY_EXEC;
Mimi Zohar4351c292014-11-05 07:53:55 -0500687 else if (strcmp(from, "MAY_WRITE") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500688 entry->mask = MAY_WRITE;
Mimi Zohar4351c292014-11-05 07:53:55 -0500689 else if (strcmp(from, "MAY_READ") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500690 entry->mask = MAY_READ;
Mimi Zohar4351c292014-11-05 07:53:55 -0500691 else if (strcmp(from, "MAY_APPEND") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500692 entry->mask = MAY_APPEND;
693 else
694 result = -EINVAL;
695 if (!result)
Mimi Zohar4351c292014-11-05 07:53:55 -0500696 entry->flags |= (*args[0].from == '^')
697 ? IMA_INMASK : IMA_MASK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500698 break;
699 case Opt_fsmagic:
Eric Paris2f1506c2010-04-20 10:21:30 -0400700 ima_log_string(ab, "fsmagic", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400701
702 if (entry->fsmagic) {
703 result = -EINVAL;
704 break;
705 }
706
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200707 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
Mimi Zohar4af46622009-02-04 09:07:00 -0500708 if (!result)
709 entry->flags |= IMA_FSMAGIC;
710 break;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300711 case Opt_fsuuid:
712 ima_log_string(ab, "fsuuid", args[0].from);
713
714 if (memchr_inv(entry->fsuuid, 0x00,
Mimi Zohar446d64e2013-02-24 23:42:37 -0500715 sizeof(entry->fsuuid))) {
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300716 result = -EINVAL;
717 break;
718 }
719
Mimi Zohar446d64e2013-02-24 23:42:37 -0500720 result = blk_part_pack_uuid(args[0].from,
721 entry->fsuuid);
722 if (!result)
723 entry->flags |= IMA_FSUUID;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300724 break;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300725 case Opt_uid_gt:
726 case Opt_euid_gt:
727 entry->uid_op = &uid_gt;
728 case Opt_uid_lt:
729 case Opt_euid_lt:
730 if ((token == Opt_uid_lt) || (token == Opt_euid_lt))
731 entry->uid_op = &uid_lt;
732 case Opt_uid_eq:
733 case Opt_euid_eq:
734 uid_token = (token == Opt_uid_eq) ||
735 (token == Opt_uid_gt) ||
736 (token == Opt_uid_lt);
737
738 ima_log_string_op(ab, uid_token ? "uid" : "euid",
739 args[0].from, entry->uid_op);
Eric Paris7b62e162010-04-20 10:21:01 -0400740
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600741 if (uid_valid(entry->uid)) {
Eric Paris7b62e162010-04-20 10:21:01 -0400742 result = -EINVAL;
743 break;
744 }
745
Jingoo Han29707b22014-02-05 15:13:14 +0900746 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar4af46622009-02-04 09:07:00 -0500747 if (!result) {
Mimi Zohar139069e2014-11-05 07:48:36 -0500748 entry->uid = make_kuid(current_user_ns(),
749 (uid_t) lnum);
750 if (!uid_valid(entry->uid) ||
751 (uid_t)lnum != lnum)
Mimi Zohar4af46622009-02-04 09:07:00 -0500752 result = -EINVAL;
753 else
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300754 entry->flags |= uid_token
Mimi Zohar139069e2014-11-05 07:48:36 -0500755 ? IMA_UID : IMA_EUID;
Mimi Zohar4af46622009-02-04 09:07:00 -0500756 }
757 break;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300758 case Opt_fowner_gt:
759 entry->fowner_op = &uid_gt;
760 case Opt_fowner_lt:
761 if (token == Opt_fowner_lt)
762 entry->fowner_op = &uid_lt;
763 case Opt_fowner_eq:
764 ima_log_string_op(ab, "fowner", args[0].from,
765 entry->fowner_op);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500766
Linus Torvalds882653222012-10-02 21:38:48 -0700767 if (uid_valid(entry->fowner)) {
Mimi Zohar07f6a792011-03-09 22:25:48 -0500768 result = -EINVAL;
769 break;
770 }
771
Jingoo Han29707b22014-02-05 15:13:14 +0900772 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500773 if (!result) {
Linus Torvalds882653222012-10-02 21:38:48 -0700774 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
775 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
Mimi Zohar07f6a792011-03-09 22:25:48 -0500776 result = -EINVAL;
777 else
778 entry->flags |= IMA_FOWNER;
779 }
780 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500781 case Opt_obj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400782 ima_log_string(ab, "obj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500783 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500784 LSM_OBJ_USER,
785 AUDIT_OBJ_USER);
786 break;
787 case Opt_obj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400788 ima_log_string(ab, "obj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500789 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500790 LSM_OBJ_ROLE,
791 AUDIT_OBJ_ROLE);
792 break;
793 case Opt_obj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400794 ima_log_string(ab, "obj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500795 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500796 LSM_OBJ_TYPE,
797 AUDIT_OBJ_TYPE);
798 break;
799 case Opt_subj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400800 ima_log_string(ab, "subj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500801 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500802 LSM_SUBJ_USER,
803 AUDIT_SUBJ_USER);
804 break;
805 case Opt_subj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400806 ima_log_string(ab, "subj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500807 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500808 LSM_SUBJ_ROLE,
809 AUDIT_SUBJ_ROLE);
810 break;
811 case Opt_subj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400812 ima_log_string(ab, "subj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500813 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500814 LSM_SUBJ_TYPE,
815 AUDIT_SUBJ_TYPE);
816 break;
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300817 case Opt_appraise_type:
818 if (entry->action != APPRAISE) {
819 result = -EINVAL;
820 break;
821 }
822
823 ima_log_string(ab, "appraise_type", args[0].from);
824 if ((strcmp(args[0].from, "imasig")) == 0)
825 entry->flags |= IMA_DIGSIG_REQUIRED;
826 else
827 result = -EINVAL;
828 break;
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400829 case Opt_permit_directio:
830 entry->flags |= IMA_PERMIT_DIRECTIO;
831 break;
Eric Richter02606432016-06-01 13:14:01 -0500832 case Opt_pcr:
833 if (entry->action != MEASURE) {
834 result = -EINVAL;
835 break;
836 }
837 ima_log_string(ab, "pcr", args[0].from);
838
839 result = kstrtoint(args[0].from, 10, &entry->pcr);
840 if (result || INVALID_PCR(entry->pcr))
841 result = -EINVAL;
842 else
843 entry->flags |= IMA_PCR;
844
845 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500846 case Opt_err:
Eric Paris2f1506c2010-04-20 10:21:30 -0400847 ima_log_string(ab, "UNKNOWN", p);
Eric Parise9d393b2010-04-20 10:21:13 -0400848 result = -EINVAL;
Mimi Zohar4af46622009-02-04 09:07:00 -0500849 break;
850 }
851 }
Eric Paris7b62e162010-04-20 10:21:01 -0400852 if (!result && (entry->action == UNKNOWN))
Mimi Zohar4af46622009-02-04 09:07:00 -0500853 result = -EINVAL;
Mimi Zohara7f2a362012-12-21 08:34:21 -0500854 else if (entry->func == MODULE_CHECK)
Mimi Zohar6ad6afa2015-12-07 14:35:47 -0500855 temp_ima_appraise |= IMA_APPRAISE_MODULES;
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400856 else if (entry->func == FIRMWARE_CHECK)
Mimi Zohar6ad6afa2015-12-07 14:35:47 -0500857 temp_ima_appraise |= IMA_APPRAISE_FIRMWARE;
Mimi Zohar19f8a842016-01-15 10:17:12 -0500858 else if (entry->func == POLICY_CHECK)
859 temp_ima_appraise |= IMA_APPRAISE_POLICY;
Eric Parisb0d5de42012-02-14 17:11:07 -0500860 audit_log_format(ab, "res=%d", !result);
Mimi Zohar4af46622009-02-04 09:07:00 -0500861 audit_log_end(ab);
862 return result;
863}
864
865/**
Mimi Zohar07f6a792011-03-09 22:25:48 -0500866 * ima_parse_add_rule - add a rule to ima_policy_rules
Mimi Zohar4af46622009-02-04 09:07:00 -0500867 * @rule - ima measurement policy rule
868 *
Petko Manolov38d859f2015-12-02 17:47:54 +0200869 * Avoid locking by allowing just one writer at a time in ima_write_policy()
Eric Paris6ccd0452010-04-20 10:20:54 -0400870 * Returns the length of the rule parsed, an error code on failure
Mimi Zohar4af46622009-02-04 09:07:00 -0500871 */
Eric Paris6ccd0452010-04-20 10:20:54 -0400872ssize_t ima_parse_add_rule(char *rule)
Mimi Zohar4af46622009-02-04 09:07:00 -0500873{
Mimi Zohar52a13282013-12-11 14:44:04 -0500874 static const char op[] = "update_policy";
Eric Paris6ccd0452010-04-20 10:20:54 -0400875 char *p;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500876 struct ima_rule_entry *entry;
Eric Paris6ccd0452010-04-20 10:20:54 -0400877 ssize_t result, len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500878 int audit_info = 0;
879
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +0300880 p = strsep(&rule, "\n");
881 len = strlen(p) + 1;
Dmitry Kasatkin71787842014-10-03 14:40:20 +0300882 p += strspn(p, " \t");
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +0300883
Dmitry Kasatkin71787842014-10-03 14:40:20 +0300884 if (*p == '#' || *p == '\0')
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +0300885 return len;
886
Mimi Zohar4af46622009-02-04 09:07:00 -0500887 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
888 if (!entry) {
889 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
890 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
891 return -ENOMEM;
892 }
893
894 INIT_LIST_HEAD(&entry->list);
895
Eric Paris6ccd0452010-04-20 10:20:54 -0400896 result = ima_parse_rule(p, entry);
Eric Paris7233e3e2010-04-20 10:21:24 -0400897 if (result) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500898 kfree(entry);
Mimi Zohar523979a2009-02-11 11:12:28 -0500899 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
Richard Guy Briggs7e9001f2014-06-16 15:52:07 -0400900 NULL, op, "invalid-policy", result,
Mimi Zohar523979a2009-02-11 11:12:28 -0500901 audit_info);
Eric Paris7233e3e2010-04-20 10:21:24 -0400902 return result;
Mimi Zohar523979a2009-02-11 11:12:28 -0500903 }
Eric Paris7233e3e2010-04-20 10:21:24 -0400904
Petko Manolov38d859f2015-12-02 17:47:54 +0200905 list_add_tail(&entry->list, &ima_temp_rules);
Eric Paris7233e3e2010-04-20 10:21:24 -0400906
907 return len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500908}
909
Petko Manolov38d859f2015-12-02 17:47:54 +0200910/**
911 * ima_delete_rules() called to cleanup invalid in-flight policy.
912 * We don't need locking as we operate on the temp list, which is
913 * different from the active one. There is also only one user of
914 * ima_delete_rules() at a time.
915 */
James Morris64c61d82009-02-05 09:28:26 +1100916void ima_delete_rules(void)
Mimi Zohar4af46622009-02-04 09:07:00 -0500917{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500918 struct ima_rule_entry *entry, *tmp;
Mimi Zohar7163a992013-01-03 14:19:09 -0500919 int i;
Mimi Zohar4af46622009-02-04 09:07:00 -0500920
Mimi Zohar6ad6afa2015-12-07 14:35:47 -0500921 temp_ima_appraise = 0;
Petko Manolov38d859f2015-12-02 17:47:54 +0200922 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
Mimi Zohar7163a992013-01-03 14:19:09 -0500923 for (i = 0; i < MAX_LSM_RULES; i++)
924 kfree(entry->lsm[i].args_p);
925
Mimi Zohar4af46622009-02-04 09:07:00 -0500926 list_del(&entry->list);
927 kfree(entry);
928 }
Mimi Zohar4af46622009-02-04 09:07:00 -0500929}
Petko Manolov80eae202015-12-02 17:47:56 +0200930
931#ifdef CONFIG_IMA_READ_POLICY
932enum {
933 mask_exec = 0, mask_write, mask_read, mask_append
934};
935
936static char *mask_tokens[] = {
937 "MAY_EXEC",
938 "MAY_WRITE",
939 "MAY_READ",
940 "MAY_APPEND"
941};
942
943enum {
944 func_file = 0, func_mmap, func_bprm,
Mimi Zohard9ddf072016-01-14 20:59:14 -0500945 func_module, func_firmware, func_post,
Mimi Zohar19f8a842016-01-15 10:17:12 -0500946 func_kexec_kernel, func_kexec_initramfs,
947 func_policy
Petko Manolov80eae202015-12-02 17:47:56 +0200948};
949
950static char *func_tokens[] = {
951 "FILE_CHECK",
952 "MMAP_CHECK",
953 "BPRM_CHECK",
954 "MODULE_CHECK",
955 "FIRMWARE_CHECK",
Mimi Zoharcf90ea92016-04-19 17:42:43 -0400956 "POST_SETATTR",
Mimi Zohard9ddf072016-01-14 20:59:14 -0500957 "KEXEC_KERNEL_CHECK",
958 "KEXEC_INITRAMFS_CHECK",
Mimi Zoharcf90ea92016-04-19 17:42:43 -0400959 "POLICY_CHECK"
Petko Manolov80eae202015-12-02 17:47:56 +0200960};
961
962void *ima_policy_start(struct seq_file *m, loff_t *pos)
963{
964 loff_t l = *pos;
965 struct ima_rule_entry *entry;
966
967 rcu_read_lock();
968 list_for_each_entry_rcu(entry, ima_rules, list) {
969 if (!l--) {
970 rcu_read_unlock();
971 return entry;
972 }
973 }
974 rcu_read_unlock();
975 return NULL;
976}
977
978void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
979{
980 struct ima_rule_entry *entry = v;
981
982 rcu_read_lock();
983 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
984 rcu_read_unlock();
985 (*pos)++;
986
987 return (&entry->list == ima_rules) ? NULL : entry;
988}
989
990void ima_policy_stop(struct seq_file *m, void *v)
991{
992}
993
994#define pt(token) policy_tokens[token + Opt_err].pattern
995#define mt(token) mask_tokens[token]
996#define ft(token) func_tokens[token]
997
Mimi Zoharb5269ab2016-01-28 13:10:36 -0500998/*
999 * policy_func_show - display the ima_hooks policy rule
1000 */
1001static void policy_func_show(struct seq_file *m, enum ima_hooks func)
1002{
1003 char tbuf[64] = {0,};
1004
1005 switch (func) {
1006 case FILE_CHECK:
1007 seq_printf(m, pt(Opt_func), ft(func_file));
1008 break;
1009 case MMAP_CHECK:
1010 seq_printf(m, pt(Opt_func), ft(func_mmap));
1011 break;
1012 case BPRM_CHECK:
1013 seq_printf(m, pt(Opt_func), ft(func_bprm));
1014 break;
1015 case MODULE_CHECK:
1016 seq_printf(m, pt(Opt_func), ft(func_module));
1017 break;
1018 case FIRMWARE_CHECK:
1019 seq_printf(m, pt(Opt_func), ft(func_firmware));
1020 break;
1021 case POST_SETATTR:
1022 seq_printf(m, pt(Opt_func), ft(func_post));
1023 break;
Mimi Zohard9ddf072016-01-14 20:59:14 -05001024 case KEXEC_KERNEL_CHECK:
1025 seq_printf(m, pt(Opt_func), ft(func_kexec_kernel));
1026 break;
1027 case KEXEC_INITRAMFS_CHECK:
1028 seq_printf(m, pt(Opt_func), ft(func_kexec_initramfs));
1029 break;
Mimi Zohar19f8a842016-01-15 10:17:12 -05001030 case POLICY_CHECK:
1031 seq_printf(m, pt(Opt_func), ft(func_policy));
1032 break;
Mimi Zoharb5269ab2016-01-28 13:10:36 -05001033 default:
1034 snprintf(tbuf, sizeof(tbuf), "%d", func);
1035 seq_printf(m, pt(Opt_func), tbuf);
1036 break;
1037 }
1038 seq_puts(m, " ");
1039}
1040
Petko Manolov80eae202015-12-02 17:47:56 +02001041int ima_policy_show(struct seq_file *m, void *v)
1042{
1043 struct ima_rule_entry *entry = v;
Andy Shevchenkob8b57272016-05-20 17:00:57 -07001044 int i;
Petko Manolov80eae202015-12-02 17:47:56 +02001045 char tbuf[64] = {0,};
1046
1047 rcu_read_lock();
1048
1049 if (entry->action & MEASURE)
1050 seq_puts(m, pt(Opt_measure));
1051 if (entry->action & DONT_MEASURE)
1052 seq_puts(m, pt(Opt_dont_measure));
1053 if (entry->action & APPRAISE)
1054 seq_puts(m, pt(Opt_appraise));
1055 if (entry->action & DONT_APPRAISE)
1056 seq_puts(m, pt(Opt_dont_appraise));
1057 if (entry->action & AUDIT)
1058 seq_puts(m, pt(Opt_audit));
1059
1060 seq_puts(m, " ");
1061
Mimi Zoharb5269ab2016-01-28 13:10:36 -05001062 if (entry->flags & IMA_FUNC)
1063 policy_func_show(m, entry->func);
Petko Manolov80eae202015-12-02 17:47:56 +02001064
1065 if (entry->flags & IMA_MASK) {
1066 if (entry->mask & MAY_EXEC)
1067 seq_printf(m, pt(Opt_mask), mt(mask_exec));
1068 if (entry->mask & MAY_WRITE)
1069 seq_printf(m, pt(Opt_mask), mt(mask_write));
1070 if (entry->mask & MAY_READ)
1071 seq_printf(m, pt(Opt_mask), mt(mask_read));
1072 if (entry->mask & MAY_APPEND)
1073 seq_printf(m, pt(Opt_mask), mt(mask_append));
1074 seq_puts(m, " ");
1075 }
1076
1077 if (entry->flags & IMA_FSMAGIC) {
1078 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
1079 seq_printf(m, pt(Opt_fsmagic), tbuf);
1080 seq_puts(m, " ");
1081 }
1082
Eric Richter02606432016-06-01 13:14:01 -05001083 if (entry->flags & IMA_PCR) {
1084 snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
1085 seq_printf(m, pt(Opt_pcr), tbuf);
1086 seq_puts(m, " ");
1087 }
1088
Petko Manolov80eae202015-12-02 17:47:56 +02001089 if (entry->flags & IMA_FSUUID) {
Andy Shevchenkob8b57272016-05-20 17:00:57 -07001090 seq_printf(m, "fsuuid=%pU", entry->fsuuid);
Petko Manolov80eae202015-12-02 17:47:56 +02001091 seq_puts(m, " ");
1092 }
1093
1094 if (entry->flags & IMA_UID) {
1095 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001096 if (entry->uid_op == &uid_gt)
1097 seq_printf(m, pt(Opt_uid_gt), tbuf);
1098 else if (entry->uid_op == &uid_lt)
1099 seq_printf(m, pt(Opt_uid_lt), tbuf);
1100 else
1101 seq_printf(m, pt(Opt_uid_eq), tbuf);
Petko Manolov80eae202015-12-02 17:47:56 +02001102 seq_puts(m, " ");
1103 }
1104
1105 if (entry->flags & IMA_EUID) {
1106 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001107 if (entry->uid_op == &uid_gt)
1108 seq_printf(m, pt(Opt_euid_gt), tbuf);
1109 else if (entry->uid_op == &uid_lt)
1110 seq_printf(m, pt(Opt_euid_lt), tbuf);
1111 else
1112 seq_printf(m, pt(Opt_euid_eq), tbuf);
Petko Manolov80eae202015-12-02 17:47:56 +02001113 seq_puts(m, " ");
1114 }
1115
1116 if (entry->flags & IMA_FOWNER) {
1117 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001118 if (entry->fowner_op == &uid_gt)
1119 seq_printf(m, pt(Opt_fowner_gt), tbuf);
1120 else if (entry->fowner_op == &uid_lt)
1121 seq_printf(m, pt(Opt_fowner_lt), tbuf);
1122 else
1123 seq_printf(m, pt(Opt_fowner_eq), tbuf);
Petko Manolov80eae202015-12-02 17:47:56 +02001124 seq_puts(m, " ");
1125 }
1126
1127 for (i = 0; i < MAX_LSM_RULES; i++) {
1128 if (entry->lsm[i].rule) {
1129 switch (i) {
1130 case LSM_OBJ_USER:
1131 seq_printf(m, pt(Opt_obj_user),
1132 (char *)entry->lsm[i].args_p);
1133 break;
1134 case LSM_OBJ_ROLE:
1135 seq_printf(m, pt(Opt_obj_role),
1136 (char *)entry->lsm[i].args_p);
1137 break;
1138 case LSM_OBJ_TYPE:
1139 seq_printf(m, pt(Opt_obj_type),
1140 (char *)entry->lsm[i].args_p);
1141 break;
1142 case LSM_SUBJ_USER:
1143 seq_printf(m, pt(Opt_subj_user),
1144 (char *)entry->lsm[i].args_p);
1145 break;
1146 case LSM_SUBJ_ROLE:
1147 seq_printf(m, pt(Opt_subj_role),
1148 (char *)entry->lsm[i].args_p);
1149 break;
1150 case LSM_SUBJ_TYPE:
1151 seq_printf(m, pt(Opt_subj_type),
1152 (char *)entry->lsm[i].args_p);
1153 break;
1154 }
1155 }
1156 }
1157 if (entry->flags & IMA_DIGSIG_REQUIRED)
1158 seq_puts(m, "appraise_type=imasig ");
1159 if (entry->flags & IMA_PERMIT_DIRECTIO)
1160 seq_puts(m, "permit_directio ");
1161 rcu_read_unlock();
1162 seq_puts(m, "\n");
1163 return 0;
1164}
1165#endif /* CONFIG_IMA_READ_POLICY */