blob: d1eefb9d65fb2bb457c280103ed9913fc4ade255 [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 Zohar3323eec2009-02-04 09:06:58 -050015#include <linux/security.h>
16#include <linux/magic.h>
Mimi Zohar4af46622009-02-04 09:07:00 -050017#include <linux/parser.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Dmitry Kasatkin85865c12012-09-03 23:23:13 +030019#include <linux/genhd.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050020
21#include "ima.h"
22
23/* flags definitions */
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020024#define IMA_FUNC 0x0001
25#define IMA_MASK 0x0002
Mimi Zohar3323eec2009-02-04 09:06:58 -050026#define IMA_FSMAGIC 0x0004
27#define IMA_UID 0x0008
Mimi Zohar07f6a792011-03-09 22:25:48 -050028#define IMA_FOWNER 0x0010
Dmitry Kasatkin85865c12012-09-03 23:23:13 +030029#define IMA_FSUUID 0x0020
Mimi Zohar3323eec2009-02-04 09:06:58 -050030
Dmitry Kasatkin45e24722012-09-12 20:51:32 +030031#define UNKNOWN 0
32#define MEASURE 0x0001 /* same as IMA_MEASURE */
33#define DONT_MEASURE 0x0002
34#define APPRAISE 0x0004 /* same as IMA_APPRAISE */
35#define DONT_APPRAISE 0x0008
Peter Moodye7c568e2012-06-14 10:04:36 -070036#define AUDIT 0x0040
Mimi Zohar4af46622009-02-04 09:07:00 -050037
Roberto Sassua7560242014-09-12 19:35:54 +020038int ima_policy_flag;
39
Mimi Zohar4af46622009-02-04 09:07:00 -050040#define MAX_LSM_RULES 6
41enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
42 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
43};
Mimi Zohar3323eec2009-02-04 09:06:58 -050044
Mimi Zohar07f6a792011-03-09 22:25:48 -050045struct ima_rule_entry {
Mimi Zohar3323eec2009-02-04 09:06:58 -050046 struct list_head list;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050047 int action;
Mimi Zohar3323eec2009-02-04 09:06:58 -050048 unsigned int flags;
49 enum ima_hooks func;
50 int mask;
51 unsigned long fsmagic;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +030052 u8 fsuuid[16];
Eric W. Biederman8b94eea2012-05-25 18:24:12 -060053 kuid_t uid;
Linus Torvalds882653222012-10-02 21:38:48 -070054 kuid_t fowner;
Mimi Zohar4af46622009-02-04 09:07:00 -050055 struct {
56 void *rule; /* LSM file metadata specific */
Mimi Zohar7163a992013-01-03 14:19:09 -050057 void *args_p; /* audit value */
Mimi Zohar4af46622009-02-04 09:07:00 -050058 int type; /* audit type */
59 } lsm[MAX_LSM_RULES];
Mimi Zohar3323eec2009-02-04 09:06:58 -050060};
61
Eric Paris5789ba32009-05-21 15:47:06 -040062/*
63 * Without LSM specific knowledge, the default policy can only be
Mimi Zohar07f6a792011-03-09 22:25:48 -050064 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
Mimi Zohar4af46622009-02-04 09:07:00 -050065 */
Eric Paris5789ba32009-05-21 15:47:06 -040066
67/*
68 * The minimum rule set to allow for full TCB coverage. Measures all files
69 * opened or mmap for exec and everything read by root. Dangerous because
70 * normal users can easily run the machine out of memory simply building
71 * and running executables.
72 */
Mimi Zohar07f6a792011-03-09 22:25:48 -050073static struct ima_rule_entry default_rules[] = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020074 {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
75 {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
76 {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
77 {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
78 {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
79 {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
80 {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
81 {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
82 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
Mimi Zohar3323eec2009-02-04 09:06:58 -050083 .flags = IMA_FUNC | IMA_MASK},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020084 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
Mimi Zohar3323eec2009-02-04 09:06:58 -050085 .flags = IMA_FUNC | IMA_MASK},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020086 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ, .uid = GLOBAL_ROOT_UID,
Eric Paris5789ba32009-05-21 15:47:06 -040087 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020088 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
Mimi Zohar5a9196d2014-07-22 10:39:48 -040089 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
Mimi Zohar3323eec2009-02-04 09:06:58 -050090};
91
Mimi Zohar07f6a792011-03-09 22:25:48 -050092static struct ima_rule_entry default_appraise_rules[] = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020093 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
94 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
95 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
96 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
97 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
98 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
99 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
100 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
101 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
102 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200103#ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200104 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .flags = IMA_FOWNER},
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200105#else
106 /* force signature */
107 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID,
108 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
109#endif
Mimi Zohar07f6a792011-03-09 22:25:48 -0500110};
Mimi Zohar3323eec2009-02-04 09:06:58 -0500111
Mimi Zohar07f6a792011-03-09 22:25:48 -0500112static LIST_HEAD(ima_default_rules);
113static LIST_HEAD(ima_policy_rules);
114static struct list_head *ima_rules;
115
116static DEFINE_MUTEX(ima_rules_mutex);
Mimi Zohar4af46622009-02-04 09:07:00 -0500117
Eric Paris5789ba32009-05-21 15:47:06 -0400118static bool ima_use_tcb __initdata;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500119static int __init default_measure_policy_setup(char *str)
Eric Paris5789ba32009-05-21 15:47:06 -0400120{
121 ima_use_tcb = 1;
122 return 1;
123}
Mimi Zohar07f6a792011-03-09 22:25:48 -0500124__setup("ima_tcb", default_measure_policy_setup);
125
126static bool ima_use_appraise_tcb __initdata;
127static int __init default_appraise_policy_setup(char *str)
128{
129 ima_use_appraise_tcb = 1;
130 return 1;
131}
132__setup("ima_appraise_tcb", default_appraise_policy_setup);
Eric Paris5789ba32009-05-21 15:47:06 -0400133
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200134/*
Mimi Zohar7163a992013-01-03 14:19:09 -0500135 * Although the IMA policy does not change, the LSM policy can be
136 * reloaded, leaving the IMA LSM based rules referring to the old,
137 * stale LSM policy.
138 *
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200139 * Update the IMA LSM based rules to reflect the reloaded LSM policy.
Mimi Zohar7163a992013-01-03 14:19:09 -0500140 * We assume the rules still exist; and BUG_ON() if they don't.
141 */
142static void ima_lsm_update_rules(void)
143{
144 struct ima_rule_entry *entry, *tmp;
145 int result;
146 int i;
147
148 mutex_lock(&ima_rules_mutex);
149 list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
150 for (i = 0; i < MAX_LSM_RULES; i++) {
151 if (!entry->lsm[i].rule)
152 continue;
153 result = security_filter_rule_init(entry->lsm[i].type,
154 Audit_equal,
155 entry->lsm[i].args_p,
156 &entry->lsm[i].rule);
157 BUG_ON(!entry->lsm[i].rule);
158 }
159 }
160 mutex_unlock(&ima_rules_mutex);
161}
162
Mimi Zohar3323eec2009-02-04 09:06:58 -0500163/**
164 * ima_match_rules - determine whether an inode matches the measure rule.
165 * @rule: a pointer to a rule
166 * @inode: a pointer to an inode
167 * @func: LIM hook identifier
168 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
169 *
170 * Returns true on rule match, false on failure.
171 */
Mimi Zohar07f6a792011-03-09 22:25:48 -0500172static bool ima_match_rules(struct ima_rule_entry *rule,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500173 struct inode *inode, enum ima_hooks func, int mask)
174{
175 struct task_struct *tsk = current;
Mimi Zohar3db59dd2012-01-17 22:11:28 -0500176 const struct cred *cred = current_cred();
Mimi Zohar4af46622009-02-04 09:07:00 -0500177 int i;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500178
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200179 if ((rule->flags & IMA_FUNC) &&
180 (rule->func != func && func != POST_SETATTR))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500181 return false;
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200182 if ((rule->flags & IMA_MASK) &&
183 (rule->mask != mask && func != POST_SETATTR))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500184 return false;
185 if ((rule->flags & IMA_FSMAGIC)
186 && rule->fsmagic != inode->i_sb->s_magic)
187 return false;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300188 if ((rule->flags & IMA_FSUUID) &&
Mimi Zohar446d64e2013-02-24 23:42:37 -0500189 memcmp(rule->fsuuid, inode->i_sb->s_uuid, sizeof(rule->fsuuid)))
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300190 return false;
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600191 if ((rule->flags & IMA_UID) && !uid_eq(rule->uid, cred->uid))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500192 return false;
Linus Torvalds882653222012-10-02 21:38:48 -0700193 if ((rule->flags & IMA_FOWNER) && !uid_eq(rule->fowner, inode->i_uid))
Mimi Zohar07f6a792011-03-09 22:25:48 -0500194 return false;
Mimi Zohar4af46622009-02-04 09:07:00 -0500195 for (i = 0; i < MAX_LSM_RULES; i++) {
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400196 int rc = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500197 u32 osid, sid;
Mimi Zohar7163a992013-01-03 14:19:09 -0500198 int retried = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500199
200 if (!rule->lsm[i].rule)
201 continue;
Mimi Zohar7163a992013-01-03 14:19:09 -0500202retry:
Mimi Zohar4af46622009-02-04 09:07:00 -0500203 switch (i) {
204 case LSM_OBJ_USER:
205 case LSM_OBJ_ROLE:
206 case LSM_OBJ_TYPE:
207 security_inode_getsecid(inode, &osid);
208 rc = security_filter_rule_match(osid,
209 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400210 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500211 rule->lsm[i].rule,
212 NULL);
213 break;
214 case LSM_SUBJ_USER:
215 case LSM_SUBJ_ROLE:
216 case LSM_SUBJ_TYPE:
217 security_task_getsecid(tsk, &sid);
218 rc = security_filter_rule_match(sid,
219 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400220 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500221 rule->lsm[i].rule,
222 NULL);
223 default:
224 break;
225 }
Mimi Zohar7163a992013-01-03 14:19:09 -0500226 if ((rc < 0) && (!retried)) {
227 retried = 1;
228 ima_lsm_update_rules();
229 goto retry;
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200230 }
Mimi Zohar4af46622009-02-04 09:07:00 -0500231 if (!rc)
232 return false;
233 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500234 return true;
235}
236
Mimi Zohard79d72e2012-12-03 17:08:11 -0500237/*
238 * In addition to knowing that we need to appraise the file in general,
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500239 * we need to differentiate between calling hooks, for hook specific rules.
Mimi Zohard79d72e2012-12-03 17:08:11 -0500240 */
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500241static int get_subaction(struct ima_rule_entry *rule, int func)
Mimi Zohard79d72e2012-12-03 17:08:11 -0500242{
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500243 if (!(rule->flags & IMA_FUNC))
244 return IMA_FILE_APPRAISE;
245
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200246 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500247 case MMAP_CHECK:
248 return IMA_MMAP_APPRAISE;
249 case BPRM_CHECK:
250 return IMA_BPRM_APPRAISE;
251 case MODULE_CHECK:
252 return IMA_MODULE_APPRAISE;
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400253 case FIRMWARE_CHECK:
254 return IMA_FIRMWARE_APPRAISE;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500255 case FILE_CHECK:
256 default:
257 return IMA_FILE_APPRAISE;
258 }
259}
260
Mimi Zohar3323eec2009-02-04 09:06:58 -0500261/**
262 * ima_match_policy - decision based on LSM and other conditions
263 * @inode: pointer to an inode for which the policy decision is being made
264 * @func: IMA hook identifier
265 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
266 *
267 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
268 * conditions.
269 *
270 * (There is no need for locking when walking the policy list,
271 * as elements in the list are never deleted, nor does the list
272 * change.)
273 */
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500274int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
275 int flags)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500276{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500277 struct ima_rule_entry *entry;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500278 int action = 0, actmask = flags | (flags << 1);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500279
Mimi Zohar07f6a792011-03-09 22:25:48 -0500280 list_for_each_entry(entry, ima_rules, list) {
Mimi Zohar3323eec2009-02-04 09:06:58 -0500281
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500282 if (!(entry->action & actmask))
283 continue;
284
285 if (!ima_match_rules(entry, inode, func, mask))
286 continue;
287
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300288 action |= entry->flags & IMA_ACTION_FLAGS;
289
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300290 action |= entry->action & IMA_DO_MASK;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500291 if (entry->action & IMA_APPRAISE)
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500292 action |= get_subaction(entry, func);
Mimi Zohard79d72e2012-12-03 17:08:11 -0500293
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300294 if (entry->action & IMA_DO_MASK)
295 actmask &= ~(entry->action | entry->action << 1);
296 else
297 actmask &= ~(entry->action | entry->action >> 1);
298
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500299 if (!actmask)
300 break;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500301 }
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500302
303 return action;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500304}
305
Roberto Sassua7560242014-09-12 19:35:54 +0200306/*
307 * Initialize the ima_policy_flag variable based on the currently
308 * loaded policy. Based on this flag, the decision to short circuit
309 * out of a function or not call the function in the first place
310 * can be made earlier.
311 */
312void ima_update_policy_flag(void)
313{
314 struct ima_rule_entry *entry;
315
316 ima_policy_flag = 0;
317 list_for_each_entry(entry, ima_rules, list) {
318 if (entry->action & IMA_DO_MASK)
319 ima_policy_flag |= entry->action;
320 }
321
322 if (!ima_appraise)
323 ima_policy_flag &= ~IMA_APPRAISE;
324}
325
Mimi Zohar3323eec2009-02-04 09:06:58 -0500326/**
327 * ima_init_policy - initialize the default measure rules.
328 *
Mimi Zohar07f6a792011-03-09 22:25:48 -0500329 * ima_rules points to either the ima_default_rules or the
330 * the new ima_policy_rules.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500331 */
Eric Paris932995f2009-05-21 15:43:32 -0400332void __init ima_init_policy(void)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500333{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500334 int i, measure_entries, appraise_entries;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500335
Eric Paris5789ba32009-05-21 15:47:06 -0400336 /* if !ima_use_tcb set entries = 0 so we load NO default rules */
Mimi Zohar07f6a792011-03-09 22:25:48 -0500337 measure_entries = ima_use_tcb ? ARRAY_SIZE(default_rules) : 0;
338 appraise_entries = ima_use_appraise_tcb ?
339 ARRAY_SIZE(default_appraise_rules) : 0;
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200340
Mimi Zohar07f6a792011-03-09 22:25:48 -0500341 for (i = 0; i < measure_entries + appraise_entries; i++) {
342 if (i < measure_entries)
343 list_add_tail(&default_rules[i].list,
344 &ima_default_rules);
345 else {
346 int j = i - measure_entries;
Eric Paris5789ba32009-05-21 15:47:06 -0400347
Mimi Zohar07f6a792011-03-09 22:25:48 -0500348 list_add_tail(&default_appraise_rules[j].list,
349 &ima_default_rules);
350 }
351 }
352
353 ima_rules = &ima_default_rules;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500354}
Mimi Zohar4af46622009-02-04 09:07:00 -0500355
356/**
357 * ima_update_policy - update default_rules with new measure rules
358 *
359 * Called on file .release to update the default rules with a complete new
360 * policy. Once updated, the policy is locked, no additional rules can be
361 * added to the policy.
362 */
363void ima_update_policy(void)
364{
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300365 ima_rules = &ima_policy_rules;
366 ima_update_policy_flag();
Mimi Zohar4af46622009-02-04 09:07:00 -0500367}
368
369enum {
370 Opt_err = -1,
371 Opt_measure = 1, Opt_dont_measure,
Mimi Zohar07f6a792011-03-09 22:25:48 -0500372 Opt_appraise, Opt_dont_appraise,
Peter Moodye7c568e2012-06-14 10:04:36 -0700373 Opt_audit,
Mimi Zohar4af46622009-02-04 09:07:00 -0500374 Opt_obj_user, Opt_obj_role, Opt_obj_type,
375 Opt_subj_user, Opt_subj_role, Opt_subj_type,
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300376 Opt_func, Opt_mask, Opt_fsmagic, Opt_uid, Opt_fowner,
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400377 Opt_appraise_type, Opt_fsuuid, Opt_permit_directio
Mimi Zohar4af46622009-02-04 09:07:00 -0500378};
379
380static match_table_t policy_tokens = {
381 {Opt_measure, "measure"},
382 {Opt_dont_measure, "dont_measure"},
Mimi Zohar07f6a792011-03-09 22:25:48 -0500383 {Opt_appraise, "appraise"},
384 {Opt_dont_appraise, "dont_appraise"},
Peter Moodye7c568e2012-06-14 10:04:36 -0700385 {Opt_audit, "audit"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500386 {Opt_obj_user, "obj_user=%s"},
387 {Opt_obj_role, "obj_role=%s"},
388 {Opt_obj_type, "obj_type=%s"},
389 {Opt_subj_user, "subj_user=%s"},
390 {Opt_subj_role, "subj_role=%s"},
391 {Opt_subj_type, "subj_type=%s"},
392 {Opt_func, "func=%s"},
393 {Opt_mask, "mask=%s"},
394 {Opt_fsmagic, "fsmagic=%s"},
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300395 {Opt_fsuuid, "fsuuid=%s"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500396 {Opt_uid, "uid=%s"},
Mimi Zohar07f6a792011-03-09 22:25:48 -0500397 {Opt_fowner, "fowner=%s"},
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300398 {Opt_appraise_type, "appraise_type=%s"},
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400399 {Opt_permit_directio, "permit_directio"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500400 {Opt_err, NULL}
401};
402
Mimi Zohar07f6a792011-03-09 22:25:48 -0500403static int ima_lsm_rule_init(struct ima_rule_entry *entry,
Mimi Zohar7163a992013-01-03 14:19:09 -0500404 substring_t *args, int lsm_rule, int audit_type)
Mimi Zohar4af46622009-02-04 09:07:00 -0500405{
406 int result;
407
Eric Paris7b62e162010-04-20 10:21:01 -0400408 if (entry->lsm[lsm_rule].rule)
409 return -EINVAL;
410
Mimi Zohar7163a992013-01-03 14:19:09 -0500411 entry->lsm[lsm_rule].args_p = match_strdup(args);
412 if (!entry->lsm[lsm_rule].args_p)
413 return -ENOMEM;
414
Mimi Zohar4af46622009-02-04 09:07:00 -0500415 entry->lsm[lsm_rule].type = audit_type;
416 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
Mimi Zohar7163a992013-01-03 14:19:09 -0500417 Audit_equal,
418 entry->lsm[lsm_rule].args_p,
Mimi Zohar4af46622009-02-04 09:07:00 -0500419 &entry->lsm[lsm_rule].rule);
Mimi Zohar7163a992013-01-03 14:19:09 -0500420 if (!entry->lsm[lsm_rule].rule) {
421 kfree(entry->lsm[lsm_rule].args_p);
Mimi Zohar867c2022011-01-03 14:59:10 -0800422 return -EINVAL;
Mimi Zohar7163a992013-01-03 14:19:09 -0500423 }
424
Mimi Zohar4af46622009-02-04 09:07:00 -0500425 return result;
426}
427
Eric Paris2f1506c2010-04-20 10:21:30 -0400428static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
429{
430 audit_log_format(ab, "%s=", key);
431 audit_log_untrustedstring(ab, value);
432 audit_log_format(ab, " ");
433}
434
Mimi Zohar07f6a792011-03-09 22:25:48 -0500435static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
Mimi Zohar4af46622009-02-04 09:07:00 -0500436{
437 struct audit_buffer *ab;
438 char *p;
439 int result = 0;
440
Mimi Zohar523979a2009-02-11 11:12:28 -0500441 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
Mimi Zohar4af46622009-02-04 09:07:00 -0500442
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600443 entry->uid = INVALID_UID;
Linus Torvalds882653222012-10-02 21:38:48 -0700444 entry->fowner = INVALID_UID;
Eric Parisb9035b12010-04-20 10:21:07 -0400445 entry->action = UNKNOWN;
Eric Paris28ef4002010-04-20 10:21:18 -0400446 while ((p = strsep(&rule, " \t")) != NULL) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500447 substring_t args[MAX_OPT_ARGS];
448 int token;
449 unsigned long lnum;
450
451 if (result < 0)
452 break;
Eric Paris28ef4002010-04-20 10:21:18 -0400453 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
454 continue;
Mimi Zohar4af46622009-02-04 09:07:00 -0500455 token = match_token(p, policy_tokens, args);
456 switch (token) {
457 case Opt_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400458 ima_log_string(ab, "action", "measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400459
460 if (entry->action != UNKNOWN)
461 result = -EINVAL;
462
Mimi Zohar4af46622009-02-04 09:07:00 -0500463 entry->action = MEASURE;
464 break;
465 case Opt_dont_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400466 ima_log_string(ab, "action", "dont_measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400467
468 if (entry->action != UNKNOWN)
469 result = -EINVAL;
470
Mimi Zohar4af46622009-02-04 09:07:00 -0500471 entry->action = DONT_MEASURE;
472 break;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500473 case Opt_appraise:
474 ima_log_string(ab, "action", "appraise");
475
476 if (entry->action != UNKNOWN)
477 result = -EINVAL;
478
479 entry->action = APPRAISE;
480 break;
481 case Opt_dont_appraise:
482 ima_log_string(ab, "action", "dont_appraise");
483
484 if (entry->action != UNKNOWN)
485 result = -EINVAL;
486
487 entry->action = DONT_APPRAISE;
488 break;
Peter Moodye7c568e2012-06-14 10:04:36 -0700489 case Opt_audit:
490 ima_log_string(ab, "action", "audit");
491
492 if (entry->action != UNKNOWN)
493 result = -EINVAL;
494
495 entry->action = AUDIT;
496 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500497 case Opt_func:
Eric Paris2f1506c2010-04-20 10:21:30 -0400498 ima_log_string(ab, "func", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400499
500 if (entry->func)
Mimi Zohar07f6a792011-03-09 22:25:48 -0500501 result = -EINVAL;
Eric Paris7b62e162010-04-20 10:21:01 -0400502
Mimi Zohar1e93d002010-01-26 17:02:41 -0500503 if (strcmp(args[0].from, "FILE_CHECK") == 0)
504 entry->func = FILE_CHECK;
505 /* PATH_CHECK is for backwards compat */
506 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
507 entry->func = FILE_CHECK;
Mimi Zoharfdf90722012-10-16 12:40:08 +1030508 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
509 entry->func = MODULE_CHECK;
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400510 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
511 entry->func = FIRMWARE_CHECK;
Mimi Zohar16cac492012-12-13 11:15:04 -0500512 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
513 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
514 entry->func = MMAP_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500515 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
516 entry->func = BPRM_CHECK;
517 else
518 result = -EINVAL;
519 if (!result)
520 entry->flags |= IMA_FUNC;
521 break;
522 case Opt_mask:
Eric Paris2f1506c2010-04-20 10:21:30 -0400523 ima_log_string(ab, "mask", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400524
525 if (entry->mask)
526 result = -EINVAL;
527
Mimi Zohar4af46622009-02-04 09:07:00 -0500528 if ((strcmp(args[0].from, "MAY_EXEC")) == 0)
529 entry->mask = MAY_EXEC;
530 else if (strcmp(args[0].from, "MAY_WRITE") == 0)
531 entry->mask = MAY_WRITE;
532 else if (strcmp(args[0].from, "MAY_READ") == 0)
533 entry->mask = MAY_READ;
534 else if (strcmp(args[0].from, "MAY_APPEND") == 0)
535 entry->mask = MAY_APPEND;
536 else
537 result = -EINVAL;
538 if (!result)
539 entry->flags |= IMA_MASK;
540 break;
541 case Opt_fsmagic:
Eric Paris2f1506c2010-04-20 10:21:30 -0400542 ima_log_string(ab, "fsmagic", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400543
544 if (entry->fsmagic) {
545 result = -EINVAL;
546 break;
547 }
548
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200549 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
Mimi Zohar4af46622009-02-04 09:07:00 -0500550 if (!result)
551 entry->flags |= IMA_FSMAGIC;
552 break;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300553 case Opt_fsuuid:
554 ima_log_string(ab, "fsuuid", args[0].from);
555
556 if (memchr_inv(entry->fsuuid, 0x00,
Mimi Zohar446d64e2013-02-24 23:42:37 -0500557 sizeof(entry->fsuuid))) {
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300558 result = -EINVAL;
559 break;
560 }
561
Mimi Zohar446d64e2013-02-24 23:42:37 -0500562 result = blk_part_pack_uuid(args[0].from,
563 entry->fsuuid);
564 if (!result)
565 entry->flags |= IMA_FSUUID;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300566 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500567 case Opt_uid:
Eric Paris2f1506c2010-04-20 10:21:30 -0400568 ima_log_string(ab, "uid", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400569
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600570 if (uid_valid(entry->uid)) {
Eric Paris7b62e162010-04-20 10:21:01 -0400571 result = -EINVAL;
572 break;
573 }
574
Jingoo Han29707b22014-02-05 15:13:14 +0900575 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar4af46622009-02-04 09:07:00 -0500576 if (!result) {
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600577 entry->uid = make_kuid(current_user_ns(), (uid_t)lnum);
578 if (!uid_valid(entry->uid) || (((uid_t)lnum) != lnum))
Mimi Zohar4af46622009-02-04 09:07:00 -0500579 result = -EINVAL;
580 else
581 entry->flags |= IMA_UID;
582 }
583 break;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500584 case Opt_fowner:
585 ima_log_string(ab, "fowner", args[0].from);
586
Linus Torvalds882653222012-10-02 21:38:48 -0700587 if (uid_valid(entry->fowner)) {
Mimi Zohar07f6a792011-03-09 22:25:48 -0500588 result = -EINVAL;
589 break;
590 }
591
Jingoo Han29707b22014-02-05 15:13:14 +0900592 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500593 if (!result) {
Linus Torvalds882653222012-10-02 21:38:48 -0700594 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
595 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
Mimi Zohar07f6a792011-03-09 22:25:48 -0500596 result = -EINVAL;
597 else
598 entry->flags |= IMA_FOWNER;
599 }
600 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500601 case Opt_obj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400602 ima_log_string(ab, "obj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500603 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500604 LSM_OBJ_USER,
605 AUDIT_OBJ_USER);
606 break;
607 case Opt_obj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400608 ima_log_string(ab, "obj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500609 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500610 LSM_OBJ_ROLE,
611 AUDIT_OBJ_ROLE);
612 break;
613 case Opt_obj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400614 ima_log_string(ab, "obj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500615 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500616 LSM_OBJ_TYPE,
617 AUDIT_OBJ_TYPE);
618 break;
619 case Opt_subj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400620 ima_log_string(ab, "subj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500621 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500622 LSM_SUBJ_USER,
623 AUDIT_SUBJ_USER);
624 break;
625 case Opt_subj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400626 ima_log_string(ab, "subj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500627 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500628 LSM_SUBJ_ROLE,
629 AUDIT_SUBJ_ROLE);
630 break;
631 case Opt_subj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400632 ima_log_string(ab, "subj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500633 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500634 LSM_SUBJ_TYPE,
635 AUDIT_SUBJ_TYPE);
636 break;
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300637 case Opt_appraise_type:
638 if (entry->action != APPRAISE) {
639 result = -EINVAL;
640 break;
641 }
642
643 ima_log_string(ab, "appraise_type", args[0].from);
644 if ((strcmp(args[0].from, "imasig")) == 0)
645 entry->flags |= IMA_DIGSIG_REQUIRED;
646 else
647 result = -EINVAL;
648 break;
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400649 case Opt_permit_directio:
650 entry->flags |= IMA_PERMIT_DIRECTIO;
651 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500652 case Opt_err:
Eric Paris2f1506c2010-04-20 10:21:30 -0400653 ima_log_string(ab, "UNKNOWN", p);
Eric Parise9d393b2010-04-20 10:21:13 -0400654 result = -EINVAL;
Mimi Zohar4af46622009-02-04 09:07:00 -0500655 break;
656 }
657 }
Eric Paris7b62e162010-04-20 10:21:01 -0400658 if (!result && (entry->action == UNKNOWN))
Mimi Zohar4af46622009-02-04 09:07:00 -0500659 result = -EINVAL;
Mimi Zohara7f2a362012-12-21 08:34:21 -0500660 else if (entry->func == MODULE_CHECK)
661 ima_appraise |= IMA_APPRAISE_MODULES;
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400662 else if (entry->func == FIRMWARE_CHECK)
663 ima_appraise |= IMA_APPRAISE_FIRMWARE;
Eric Parisb0d5de42012-02-14 17:11:07 -0500664 audit_log_format(ab, "res=%d", !result);
Mimi Zohar4af46622009-02-04 09:07:00 -0500665 audit_log_end(ab);
666 return result;
667}
668
669/**
Mimi Zohar07f6a792011-03-09 22:25:48 -0500670 * ima_parse_add_rule - add a rule to ima_policy_rules
Mimi Zohar4af46622009-02-04 09:07:00 -0500671 * @rule - ima measurement policy rule
672 *
673 * Uses a mutex to protect the policy list from multiple concurrent writers.
Eric Paris6ccd0452010-04-20 10:20:54 -0400674 * Returns the length of the rule parsed, an error code on failure
Mimi Zohar4af46622009-02-04 09:07:00 -0500675 */
Eric Paris6ccd0452010-04-20 10:20:54 -0400676ssize_t ima_parse_add_rule(char *rule)
Mimi Zohar4af46622009-02-04 09:07:00 -0500677{
Mimi Zohar52a13282013-12-11 14:44:04 -0500678 static const char op[] = "update_policy";
Eric Paris6ccd0452010-04-20 10:20:54 -0400679 char *p;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500680 struct ima_rule_entry *entry;
Eric Paris6ccd0452010-04-20 10:20:54 -0400681 ssize_t result, len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500682 int audit_info = 0;
683
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +0300684 p = strsep(&rule, "\n");
685 len = strlen(p) + 1;
Dmitry Kasatkin71787842014-10-03 14:40:20 +0300686 p += strspn(p, " \t");
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +0300687
Dmitry Kasatkin71787842014-10-03 14:40:20 +0300688 if (*p == '#' || *p == '\0')
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +0300689 return len;
690
Mimi Zohar4af46622009-02-04 09:07:00 -0500691 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
692 if (!entry) {
693 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
694 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
695 return -ENOMEM;
696 }
697
698 INIT_LIST_HEAD(&entry->list);
699
Eric Paris6ccd0452010-04-20 10:20:54 -0400700 result = ima_parse_rule(p, entry);
Eric Paris7233e3e2010-04-20 10:21:24 -0400701 if (result) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500702 kfree(entry);
Mimi Zohar523979a2009-02-11 11:12:28 -0500703 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
Richard Guy Briggs7e9001f2014-06-16 15:52:07 -0400704 NULL, op, "invalid-policy", result,
Mimi Zohar523979a2009-02-11 11:12:28 -0500705 audit_info);
Eric Paris7233e3e2010-04-20 10:21:24 -0400706 return result;
Mimi Zohar523979a2009-02-11 11:12:28 -0500707 }
Eric Paris7233e3e2010-04-20 10:21:24 -0400708
Mimi Zohar07f6a792011-03-09 22:25:48 -0500709 mutex_lock(&ima_rules_mutex);
710 list_add_tail(&entry->list, &ima_policy_rules);
711 mutex_unlock(&ima_rules_mutex);
Eric Paris7233e3e2010-04-20 10:21:24 -0400712
713 return len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500714}
715
716/* ima_delete_rules called to cleanup invalid policy */
James Morris64c61d82009-02-05 09:28:26 +1100717void ima_delete_rules(void)
Mimi Zohar4af46622009-02-04 09:07:00 -0500718{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500719 struct ima_rule_entry *entry, *tmp;
Mimi Zohar7163a992013-01-03 14:19:09 -0500720 int i;
Mimi Zohar4af46622009-02-04 09:07:00 -0500721
Mimi Zohar07f6a792011-03-09 22:25:48 -0500722 mutex_lock(&ima_rules_mutex);
723 list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
Mimi Zohar7163a992013-01-03 14:19:09 -0500724 for (i = 0; i < MAX_LSM_RULES; i++)
725 kfree(entry->lsm[i].args_p);
726
Mimi Zohar4af46622009-02-04 09:07:00 -0500727 list_del(&entry->list);
728 kfree(entry);
729 }
Mimi Zohar07f6a792011-03-09 22:25:48 -0500730 mutex_unlock(&ima_rules_mutex);
Mimi Zohar4af46622009-02-04 09:07:00 -0500731}