blob: 2f4e0f5f31e282610b25a408cedb93f957a6628c [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>
Petko Manolov38d859f2015-12-02 17:47:54 +020019#include <linux/rculist.h>
Dmitry Kasatkin85865c12012-09-03 23:23:13 +030020#include <linux/genhd.h>
Petko Manolov80eae202015-12-02 17:47:56 +020021#include <linux/seq_file.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050022
23#include "ima.h"
24
25/* flags definitions */
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020026#define IMA_FUNC 0x0001
27#define IMA_MASK 0x0002
Mimi Zohar3323eec2009-02-04 09:06:58 -050028#define IMA_FSMAGIC 0x0004
29#define IMA_UID 0x0008
Mimi Zohar07f6a792011-03-09 22:25:48 -050030#define IMA_FOWNER 0x0010
Dmitry Kasatkin85865c12012-09-03 23:23:13 +030031#define IMA_FSUUID 0x0020
Mimi Zohar4351c292014-11-05 07:53:55 -050032#define IMA_INMASK 0x0040
Mimi Zohar139069e2014-11-05 07:48:36 -050033#define IMA_EUID 0x0080
Mimi Zohar3323eec2009-02-04 09:06:58 -050034
Dmitry Kasatkin45e24722012-09-12 20:51:32 +030035#define UNKNOWN 0
36#define MEASURE 0x0001 /* same as IMA_MEASURE */
37#define DONT_MEASURE 0x0002
38#define APPRAISE 0x0004 /* same as IMA_APPRAISE */
39#define DONT_APPRAISE 0x0008
Peter Moodye7c568e2012-06-14 10:04:36 -070040#define AUDIT 0x0040
Mimi Zohar4af46622009-02-04 09:07:00 -050041
Roberto Sassua7560242014-09-12 19:35:54 +020042int ima_policy_flag;
43
Mimi Zohar4af46622009-02-04 09:07:00 -050044#define MAX_LSM_RULES 6
45enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
46 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
47};
Mimi Zohar3323eec2009-02-04 09:06:58 -050048
Mimi Zohar24fd03c2015-06-11 20:48:33 -040049enum policy_types { ORIGINAL_TCB = 1, DEFAULT_TCB };
50
Mimi Zohar07f6a792011-03-09 22:25:48 -050051struct ima_rule_entry {
Mimi Zohar3323eec2009-02-04 09:06:58 -050052 struct list_head list;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050053 int action;
Mimi Zohar3323eec2009-02-04 09:06:58 -050054 unsigned int flags;
55 enum ima_hooks func;
56 int mask;
57 unsigned long fsmagic;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +030058 u8 fsuuid[16];
Eric W. Biederman8b94eea2012-05-25 18:24:12 -060059 kuid_t uid;
Linus Torvalds882653222012-10-02 21:38:48 -070060 kuid_t fowner;
Mimi Zohar4af46622009-02-04 09:07:00 -050061 struct {
62 void *rule; /* LSM file metadata specific */
Mimi Zohar7163a992013-01-03 14:19:09 -050063 void *args_p; /* audit value */
Mimi Zohar4af46622009-02-04 09:07:00 -050064 int type; /* audit type */
65 } lsm[MAX_LSM_RULES];
Mimi Zohar3323eec2009-02-04 09:06:58 -050066};
67
Eric Paris5789ba32009-05-21 15:47:06 -040068/*
69 * Without LSM specific knowledge, the default policy can only be
Mimi Zohar07f6a792011-03-09 22:25:48 -050070 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
Mimi Zohar4af46622009-02-04 09:07:00 -050071 */
Eric Paris5789ba32009-05-21 15:47:06 -040072
73/*
74 * The minimum rule set to allow for full TCB coverage. Measures all files
75 * opened or mmap for exec and everything read by root. Dangerous because
76 * normal users can easily run the machine out of memory simply building
77 * and running executables.
78 */
Mimi Zohar24fd03c2015-06-11 20:48:33 -040079static struct ima_rule_entry dont_measure_rules[] = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020080 {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
81 {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
82 {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
83 {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
84 {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
85 {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
86 {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
87 {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
Roberto Sassu6438de92015-04-11 17:13:06 +020088 {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
89 .flags = IMA_FSMAGIC},
Mimi Zohar24fd03c2015-06-11 20:48:33 -040090 {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC}
91};
92
93static struct ima_rule_entry original_measurement_rules[] = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020094 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
Mimi Zohar3323eec2009-02-04 09:06:58 -050095 .flags = IMA_FUNC | IMA_MASK},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020096 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
Mimi Zohar3323eec2009-02-04 09:06:58 -050097 .flags = IMA_FUNC | IMA_MASK},
Mimi Zohar24fd03c2015-06-11 20:48:33 -040098 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
99 .uid = GLOBAL_ROOT_UID, .flags = IMA_FUNC | IMA_MASK | IMA_UID},
100 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
101 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
102};
103
104static struct ima_rule_entry default_measurement_rules[] = {
105 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
106 .flags = IMA_FUNC | IMA_MASK},
107 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
108 .flags = IMA_FUNC | IMA_MASK},
109 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
110 .uid = GLOBAL_ROOT_UID, .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
111 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
112 .uid = GLOBAL_ROOT_UID, .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200113 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400114 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
Mimi Zohar3323eec2009-02-04 09:06:58 -0500115};
116
Mimi Zohar07f6a792011-03-09 22:25:48 -0500117static struct ima_rule_entry default_appraise_rules[] = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200118 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
119 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
120 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
121 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
122 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
123 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
124 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
125 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
126 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
Mimi Zoharcd025f72015-04-21 16:54:24 -0400127 {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200128 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200129#ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200130 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .flags = IMA_FOWNER},
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200131#else
132 /* force signature */
133 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID,
134 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
135#endif
Mimi Zohar07f6a792011-03-09 22:25:48 -0500136};
Mimi Zohar3323eec2009-02-04 09:06:58 -0500137
Mimi Zohar07f6a792011-03-09 22:25:48 -0500138static LIST_HEAD(ima_default_rules);
139static LIST_HEAD(ima_policy_rules);
Petko Manolov38d859f2015-12-02 17:47:54 +0200140static LIST_HEAD(ima_temp_rules);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500141static struct list_head *ima_rules;
142
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400143static int ima_policy __initdata;
Petko Manolov38d859f2015-12-02 17:47:54 +0200144
Mimi Zohar07f6a792011-03-09 22:25:48 -0500145static int __init default_measure_policy_setup(char *str)
Eric Paris5789ba32009-05-21 15:47:06 -0400146{
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400147 if (ima_policy)
148 return 1;
149
150 ima_policy = ORIGINAL_TCB;
Eric Paris5789ba32009-05-21 15:47:06 -0400151 return 1;
152}
Mimi Zohar07f6a792011-03-09 22:25:48 -0500153__setup("ima_tcb", default_measure_policy_setup);
154
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400155static int __init policy_setup(char *str)
156{
157 if (ima_policy)
158 return 1;
159
160 if (strcmp(str, "tcb") == 0)
161 ima_policy = DEFAULT_TCB;
162
163 return 1;
164}
165__setup("ima_policy=", policy_setup);
166
Mimi Zohar07f6a792011-03-09 22:25:48 -0500167static bool ima_use_appraise_tcb __initdata;
168static int __init default_appraise_policy_setup(char *str)
169{
170 ima_use_appraise_tcb = 1;
171 return 1;
172}
173__setup("ima_appraise_tcb", default_appraise_policy_setup);
Eric Paris5789ba32009-05-21 15:47:06 -0400174
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200175/*
Petko Manolov38d859f2015-12-02 17:47:54 +0200176 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
177 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
178 * the reloaded LSM policy. We assume the rules still exist; and BUG_ON() if
179 * they don't.
Mimi Zohar7163a992013-01-03 14:19:09 -0500180 */
181static void ima_lsm_update_rules(void)
182{
Petko Manolov38d859f2015-12-02 17:47:54 +0200183 struct ima_rule_entry *entry;
Mimi Zohar7163a992013-01-03 14:19:09 -0500184 int result;
185 int i;
186
Petko Manolov38d859f2015-12-02 17:47:54 +0200187 list_for_each_entry(entry, &ima_policy_rules, list) {
Mimi Zohar7163a992013-01-03 14:19:09 -0500188 for (i = 0; i < MAX_LSM_RULES; i++) {
189 if (!entry->lsm[i].rule)
190 continue;
191 result = security_filter_rule_init(entry->lsm[i].type,
192 Audit_equal,
193 entry->lsm[i].args_p,
194 &entry->lsm[i].rule);
195 BUG_ON(!entry->lsm[i].rule);
196 }
197 }
Mimi Zohar7163a992013-01-03 14:19:09 -0500198}
199
Mimi Zohar3323eec2009-02-04 09:06:58 -0500200/**
201 * ima_match_rules - determine whether an inode matches the measure rule.
202 * @rule: a pointer to a rule
203 * @inode: a pointer to an inode
204 * @func: LIM hook identifier
205 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
206 *
207 * Returns true on rule match, false on failure.
208 */
Mimi Zohar07f6a792011-03-09 22:25:48 -0500209static bool ima_match_rules(struct ima_rule_entry *rule,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500210 struct inode *inode, enum ima_hooks func, int mask)
211{
212 struct task_struct *tsk = current;
Mimi Zohar3db59dd2012-01-17 22:11:28 -0500213 const struct cred *cred = current_cred();
Mimi Zohar4af46622009-02-04 09:07:00 -0500214 int i;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500215
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200216 if ((rule->flags & IMA_FUNC) &&
217 (rule->func != func && func != POST_SETATTR))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500218 return false;
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200219 if ((rule->flags & IMA_MASK) &&
220 (rule->mask != mask && func != POST_SETATTR))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500221 return false;
Mimi Zohar4351c292014-11-05 07:53:55 -0500222 if ((rule->flags & IMA_INMASK) &&
223 (!(rule->mask & mask) && func != POST_SETATTR))
224 return false;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500225 if ((rule->flags & IMA_FSMAGIC)
226 && rule->fsmagic != inode->i_sb->s_magic)
227 return false;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300228 if ((rule->flags & IMA_FSUUID) &&
Mimi Zohar446d64e2013-02-24 23:42:37 -0500229 memcmp(rule->fsuuid, inode->i_sb->s_uuid, sizeof(rule->fsuuid)))
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300230 return false;
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600231 if ((rule->flags & IMA_UID) && !uid_eq(rule->uid, cred->uid))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500232 return false;
Mimi Zohar139069e2014-11-05 07:48:36 -0500233 if (rule->flags & IMA_EUID) {
234 if (has_capability_noaudit(current, CAP_SETUID)) {
235 if (!uid_eq(rule->uid, cred->euid)
236 && !uid_eq(rule->uid, cred->suid)
237 && !uid_eq(rule->uid, cred->uid))
238 return false;
239 } else if (!uid_eq(rule->uid, cred->euid))
240 return false;
241 }
242
Linus Torvalds882653222012-10-02 21:38:48 -0700243 if ((rule->flags & IMA_FOWNER) && !uid_eq(rule->fowner, inode->i_uid))
Mimi Zohar07f6a792011-03-09 22:25:48 -0500244 return false;
Mimi Zohar4af46622009-02-04 09:07:00 -0500245 for (i = 0; i < MAX_LSM_RULES; i++) {
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400246 int rc = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500247 u32 osid, sid;
Mimi Zohar7163a992013-01-03 14:19:09 -0500248 int retried = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500249
250 if (!rule->lsm[i].rule)
251 continue;
Mimi Zohar7163a992013-01-03 14:19:09 -0500252retry:
Mimi Zohar4af46622009-02-04 09:07:00 -0500253 switch (i) {
254 case LSM_OBJ_USER:
255 case LSM_OBJ_ROLE:
256 case LSM_OBJ_TYPE:
257 security_inode_getsecid(inode, &osid);
258 rc = security_filter_rule_match(osid,
259 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400260 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500261 rule->lsm[i].rule,
262 NULL);
263 break;
264 case LSM_SUBJ_USER:
265 case LSM_SUBJ_ROLE:
266 case LSM_SUBJ_TYPE:
267 security_task_getsecid(tsk, &sid);
268 rc = security_filter_rule_match(sid,
269 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400270 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500271 rule->lsm[i].rule,
272 NULL);
273 default:
274 break;
275 }
Mimi Zohar7163a992013-01-03 14:19:09 -0500276 if ((rc < 0) && (!retried)) {
277 retried = 1;
278 ima_lsm_update_rules();
279 goto retry;
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200280 }
Mimi Zohar4af46622009-02-04 09:07:00 -0500281 if (!rc)
282 return false;
283 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500284 return true;
285}
286
Mimi Zohard79d72e2012-12-03 17:08:11 -0500287/*
288 * In addition to knowing that we need to appraise the file in general,
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500289 * we need to differentiate between calling hooks, for hook specific rules.
Mimi Zohard79d72e2012-12-03 17:08:11 -0500290 */
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500291static int get_subaction(struct ima_rule_entry *rule, int func)
Mimi Zohard79d72e2012-12-03 17:08:11 -0500292{
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500293 if (!(rule->flags & IMA_FUNC))
294 return IMA_FILE_APPRAISE;
295
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200296 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500297 case MMAP_CHECK:
298 return IMA_MMAP_APPRAISE;
299 case BPRM_CHECK:
300 return IMA_BPRM_APPRAISE;
301 case MODULE_CHECK:
302 return IMA_MODULE_APPRAISE;
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400303 case FIRMWARE_CHECK:
304 return IMA_FIRMWARE_APPRAISE;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500305 case FILE_CHECK:
306 default:
307 return IMA_FILE_APPRAISE;
308 }
309}
310
Mimi Zohar3323eec2009-02-04 09:06:58 -0500311/**
312 * ima_match_policy - decision based on LSM and other conditions
313 * @inode: pointer to an inode for which the policy decision is being made
314 * @func: IMA hook identifier
315 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
316 *
317 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
318 * conditions.
319 *
Petko Manolov38d859f2015-12-02 17:47:54 +0200320 * Since the IMA policy may be updated multiple times we need to lock the
321 * list when walking it. Reads are many orders of magnitude more numerous
322 * than writes so ima_match_policy() is classical RCU candidate.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500323 */
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500324int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
325 int flags)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500326{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500327 struct ima_rule_entry *entry;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500328 int action = 0, actmask = flags | (flags << 1);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500329
Petko Manolov38d859f2015-12-02 17:47:54 +0200330 rcu_read_lock();
331 list_for_each_entry_rcu(entry, ima_rules, list) {
Mimi Zohar3323eec2009-02-04 09:06:58 -0500332
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500333 if (!(entry->action & actmask))
334 continue;
335
336 if (!ima_match_rules(entry, inode, func, mask))
337 continue;
338
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300339 action |= entry->flags & IMA_ACTION_FLAGS;
340
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300341 action |= entry->action & IMA_DO_MASK;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500342 if (entry->action & IMA_APPRAISE)
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500343 action |= get_subaction(entry, func);
Mimi Zohard79d72e2012-12-03 17:08:11 -0500344
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300345 if (entry->action & IMA_DO_MASK)
346 actmask &= ~(entry->action | entry->action << 1);
347 else
348 actmask &= ~(entry->action | entry->action >> 1);
349
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500350 if (!actmask)
351 break;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500352 }
Petko Manolov38d859f2015-12-02 17:47:54 +0200353 rcu_read_unlock();
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500354
355 return action;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500356}
357
Roberto Sassua7560242014-09-12 19:35:54 +0200358/*
359 * Initialize the ima_policy_flag variable based on the currently
360 * loaded policy. Based on this flag, the decision to short circuit
361 * out of a function or not call the function in the first place
362 * can be made earlier.
363 */
364void ima_update_policy_flag(void)
365{
366 struct ima_rule_entry *entry;
367
Roberto Sassua7560242014-09-12 19:35:54 +0200368 list_for_each_entry(entry, ima_rules, list) {
369 if (entry->action & IMA_DO_MASK)
370 ima_policy_flag |= entry->action;
371 }
372
373 if (!ima_appraise)
374 ima_policy_flag &= ~IMA_APPRAISE;
375}
376
Mimi Zohar3323eec2009-02-04 09:06:58 -0500377/**
378 * ima_init_policy - initialize the default measure rules.
379 *
Mimi Zohar07f6a792011-03-09 22:25:48 -0500380 * ima_rules points to either the ima_default_rules or the
381 * the new ima_policy_rules.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500382 */
Eric Paris932995f2009-05-21 15:43:32 -0400383void __init ima_init_policy(void)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500384{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500385 int i, measure_entries, appraise_entries;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500386
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400387 /* if !ima_policy set entries = 0 so we load NO default rules */
388 measure_entries = ima_policy ? ARRAY_SIZE(dont_measure_rules) : 0;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500389 appraise_entries = ima_use_appraise_tcb ?
390 ARRAY_SIZE(default_appraise_rules) : 0;
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200391
Dan Carpenter55778572015-04-07 12:22:11 +0300392 for (i = 0; i < measure_entries; i++)
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400393 list_add_tail(&dont_measure_rules[i].list, &ima_default_rules);
394
395 switch (ima_policy) {
396 case ORIGINAL_TCB:
397 for (i = 0; i < ARRAY_SIZE(original_measurement_rules); i++)
398 list_add_tail(&original_measurement_rules[i].list,
399 &ima_default_rules);
400 break;
401 case DEFAULT_TCB:
402 for (i = 0; i < ARRAY_SIZE(default_measurement_rules); i++)
403 list_add_tail(&default_measurement_rules[i].list,
404 &ima_default_rules);
405 default:
406 break;
407 }
Eric Paris5789ba32009-05-21 15:47:06 -0400408
Dan Carpenter55778572015-04-07 12:22:11 +0300409 for (i = 0; i < appraise_entries; i++) {
410 list_add_tail(&default_appraise_rules[i].list,
411 &ima_default_rules);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500412 }
413
414 ima_rules = &ima_default_rules;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500415}
Mimi Zohar4af46622009-02-04 09:07:00 -0500416
417/**
418 * ima_update_policy - update default_rules with new measure rules
419 *
420 * Called on file .release to update the default rules with a complete new
Petko Manolov38d859f2015-12-02 17:47:54 +0200421 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
422 * they make a queue. The policy may be updated multiple times and this is the
423 * RCU updater.
424 *
425 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
426 * we switch from the default policy to user defined.
Mimi Zohar4af46622009-02-04 09:07:00 -0500427 */
428void ima_update_policy(void)
429{
Petko Manolov38d859f2015-12-02 17:47:54 +0200430 struct list_head *first, *last, *policy;
431
432 /* append current policy with the new rules */
433 first = (&ima_temp_rules)->next;
434 last = (&ima_temp_rules)->prev;
435 policy = &ima_policy_rules;
436
437 synchronize_rcu();
438
439 last->next = policy;
440 rcu_assign_pointer(list_next_rcu(policy->prev), first);
441 first->prev = policy->prev;
442 policy->prev = last;
443
444 /* prepare for the next policy rules addition */
445 INIT_LIST_HEAD(&ima_temp_rules);
446
447 if (ima_rules != policy) {
448 ima_policy_flag = 0;
449 ima_rules = policy;
450 }
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300451 ima_update_policy_flag();
Mimi Zohar4af46622009-02-04 09:07:00 -0500452}
453
454enum {
455 Opt_err = -1,
456 Opt_measure = 1, Opt_dont_measure,
Mimi Zohar07f6a792011-03-09 22:25:48 -0500457 Opt_appraise, Opt_dont_appraise,
Peter Moodye7c568e2012-06-14 10:04:36 -0700458 Opt_audit,
Mimi Zohar4af46622009-02-04 09:07:00 -0500459 Opt_obj_user, Opt_obj_role, Opt_obj_type,
460 Opt_subj_user, Opt_subj_role, Opt_subj_type,
Mimi Zohar139069e2014-11-05 07:48:36 -0500461 Opt_func, Opt_mask, Opt_fsmagic,
Petko Manolov80eae202015-12-02 17:47:56 +0200462 Opt_fsuuid, Opt_uid, Opt_euid, Opt_fowner,
463 Opt_appraise_type, Opt_permit_directio
Mimi Zohar4af46622009-02-04 09:07:00 -0500464};
465
466static match_table_t policy_tokens = {
467 {Opt_measure, "measure"},
468 {Opt_dont_measure, "dont_measure"},
Mimi Zohar07f6a792011-03-09 22:25:48 -0500469 {Opt_appraise, "appraise"},
470 {Opt_dont_appraise, "dont_appraise"},
Peter Moodye7c568e2012-06-14 10:04:36 -0700471 {Opt_audit, "audit"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500472 {Opt_obj_user, "obj_user=%s"},
473 {Opt_obj_role, "obj_role=%s"},
474 {Opt_obj_type, "obj_type=%s"},
475 {Opt_subj_user, "subj_user=%s"},
476 {Opt_subj_role, "subj_role=%s"},
477 {Opt_subj_type, "subj_type=%s"},
478 {Opt_func, "func=%s"},
479 {Opt_mask, "mask=%s"},
480 {Opt_fsmagic, "fsmagic=%s"},
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300481 {Opt_fsuuid, "fsuuid=%s"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500482 {Opt_uid, "uid=%s"},
Mimi Zohar139069e2014-11-05 07:48:36 -0500483 {Opt_euid, "euid=%s"},
Mimi Zohar07f6a792011-03-09 22:25:48 -0500484 {Opt_fowner, "fowner=%s"},
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300485 {Opt_appraise_type, "appraise_type=%s"},
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400486 {Opt_permit_directio, "permit_directio"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500487 {Opt_err, NULL}
488};
489
Mimi Zohar07f6a792011-03-09 22:25:48 -0500490static int ima_lsm_rule_init(struct ima_rule_entry *entry,
Mimi Zohar7163a992013-01-03 14:19:09 -0500491 substring_t *args, int lsm_rule, int audit_type)
Mimi Zohar4af46622009-02-04 09:07:00 -0500492{
493 int result;
494
Eric Paris7b62e162010-04-20 10:21:01 -0400495 if (entry->lsm[lsm_rule].rule)
496 return -EINVAL;
497
Mimi Zohar7163a992013-01-03 14:19:09 -0500498 entry->lsm[lsm_rule].args_p = match_strdup(args);
499 if (!entry->lsm[lsm_rule].args_p)
500 return -ENOMEM;
501
Mimi Zohar4af46622009-02-04 09:07:00 -0500502 entry->lsm[lsm_rule].type = audit_type;
503 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
Mimi Zohar7163a992013-01-03 14:19:09 -0500504 Audit_equal,
505 entry->lsm[lsm_rule].args_p,
Mimi Zohar4af46622009-02-04 09:07:00 -0500506 &entry->lsm[lsm_rule].rule);
Mimi Zohar7163a992013-01-03 14:19:09 -0500507 if (!entry->lsm[lsm_rule].rule) {
508 kfree(entry->lsm[lsm_rule].args_p);
Mimi Zohar867c2022011-01-03 14:59:10 -0800509 return -EINVAL;
Mimi Zohar7163a992013-01-03 14:19:09 -0500510 }
511
Mimi Zohar4af46622009-02-04 09:07:00 -0500512 return result;
513}
514
Eric Paris2f1506c2010-04-20 10:21:30 -0400515static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
516{
517 audit_log_format(ab, "%s=", key);
518 audit_log_untrustedstring(ab, value);
519 audit_log_format(ab, " ");
520}
521
Mimi Zohar07f6a792011-03-09 22:25:48 -0500522static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
Mimi Zohar4af46622009-02-04 09:07:00 -0500523{
524 struct audit_buffer *ab;
Mimi Zohar4351c292014-11-05 07:53:55 -0500525 char *from;
Mimi Zohar4af46622009-02-04 09:07:00 -0500526 char *p;
527 int result = 0;
528
Mimi Zohar523979a2009-02-11 11:12:28 -0500529 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
Mimi Zohar4af46622009-02-04 09:07:00 -0500530
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600531 entry->uid = INVALID_UID;
Linus Torvalds882653222012-10-02 21:38:48 -0700532 entry->fowner = INVALID_UID;
Eric Parisb9035b12010-04-20 10:21:07 -0400533 entry->action = UNKNOWN;
Eric Paris28ef4002010-04-20 10:21:18 -0400534 while ((p = strsep(&rule, " \t")) != NULL) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500535 substring_t args[MAX_OPT_ARGS];
536 int token;
537 unsigned long lnum;
538
539 if (result < 0)
540 break;
Eric Paris28ef4002010-04-20 10:21:18 -0400541 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
542 continue;
Mimi Zohar4af46622009-02-04 09:07:00 -0500543 token = match_token(p, policy_tokens, args);
544 switch (token) {
545 case Opt_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400546 ima_log_string(ab, "action", "measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400547
548 if (entry->action != UNKNOWN)
549 result = -EINVAL;
550
Mimi Zohar4af46622009-02-04 09:07:00 -0500551 entry->action = MEASURE;
552 break;
553 case Opt_dont_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400554 ima_log_string(ab, "action", "dont_measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400555
556 if (entry->action != UNKNOWN)
557 result = -EINVAL;
558
Mimi Zohar4af46622009-02-04 09:07:00 -0500559 entry->action = DONT_MEASURE;
560 break;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500561 case Opt_appraise:
562 ima_log_string(ab, "action", "appraise");
563
564 if (entry->action != UNKNOWN)
565 result = -EINVAL;
566
567 entry->action = APPRAISE;
568 break;
569 case Opt_dont_appraise:
570 ima_log_string(ab, "action", "dont_appraise");
571
572 if (entry->action != UNKNOWN)
573 result = -EINVAL;
574
575 entry->action = DONT_APPRAISE;
576 break;
Peter Moodye7c568e2012-06-14 10:04:36 -0700577 case Opt_audit:
578 ima_log_string(ab, "action", "audit");
579
580 if (entry->action != UNKNOWN)
581 result = -EINVAL;
582
583 entry->action = AUDIT;
584 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500585 case Opt_func:
Eric Paris2f1506c2010-04-20 10:21:30 -0400586 ima_log_string(ab, "func", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400587
588 if (entry->func)
Mimi Zohar07f6a792011-03-09 22:25:48 -0500589 result = -EINVAL;
Eric Paris7b62e162010-04-20 10:21:01 -0400590
Mimi Zohar1e93d002010-01-26 17:02:41 -0500591 if (strcmp(args[0].from, "FILE_CHECK") == 0)
592 entry->func = FILE_CHECK;
593 /* PATH_CHECK is for backwards compat */
594 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
595 entry->func = FILE_CHECK;
Mimi Zoharfdf90722012-10-16 12:40:08 +1030596 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
597 entry->func = MODULE_CHECK;
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400598 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
599 entry->func = FIRMWARE_CHECK;
Mimi Zohar16cac492012-12-13 11:15:04 -0500600 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
601 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
602 entry->func = MMAP_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500603 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
604 entry->func = BPRM_CHECK;
605 else
606 result = -EINVAL;
607 if (!result)
608 entry->flags |= IMA_FUNC;
609 break;
610 case Opt_mask:
Eric Paris2f1506c2010-04-20 10:21:30 -0400611 ima_log_string(ab, "mask", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400612
613 if (entry->mask)
614 result = -EINVAL;
615
Mimi Zohar4351c292014-11-05 07:53:55 -0500616 from = args[0].from;
617 if (*from == '^')
618 from++;
619
620 if ((strcmp(from, "MAY_EXEC")) == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500621 entry->mask = MAY_EXEC;
Mimi Zohar4351c292014-11-05 07:53:55 -0500622 else if (strcmp(from, "MAY_WRITE") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500623 entry->mask = MAY_WRITE;
Mimi Zohar4351c292014-11-05 07:53:55 -0500624 else if (strcmp(from, "MAY_READ") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500625 entry->mask = MAY_READ;
Mimi Zohar4351c292014-11-05 07:53:55 -0500626 else if (strcmp(from, "MAY_APPEND") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500627 entry->mask = MAY_APPEND;
628 else
629 result = -EINVAL;
630 if (!result)
Mimi Zohar4351c292014-11-05 07:53:55 -0500631 entry->flags |= (*args[0].from == '^')
632 ? IMA_INMASK : IMA_MASK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500633 break;
634 case Opt_fsmagic:
Eric Paris2f1506c2010-04-20 10:21:30 -0400635 ima_log_string(ab, "fsmagic", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400636
637 if (entry->fsmagic) {
638 result = -EINVAL;
639 break;
640 }
641
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200642 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
Mimi Zohar4af46622009-02-04 09:07:00 -0500643 if (!result)
644 entry->flags |= IMA_FSMAGIC;
645 break;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300646 case Opt_fsuuid:
647 ima_log_string(ab, "fsuuid", args[0].from);
648
649 if (memchr_inv(entry->fsuuid, 0x00,
Mimi Zohar446d64e2013-02-24 23:42:37 -0500650 sizeof(entry->fsuuid))) {
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300651 result = -EINVAL;
652 break;
653 }
654
Mimi Zohar446d64e2013-02-24 23:42:37 -0500655 result = blk_part_pack_uuid(args[0].from,
656 entry->fsuuid);
657 if (!result)
658 entry->flags |= IMA_FSUUID;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300659 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500660 case Opt_uid:
Eric Paris2f1506c2010-04-20 10:21:30 -0400661 ima_log_string(ab, "uid", args[0].from);
Mimi Zohar139069e2014-11-05 07:48:36 -0500662 case Opt_euid:
663 if (token == Opt_euid)
664 ima_log_string(ab, "euid", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400665
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600666 if (uid_valid(entry->uid)) {
Eric Paris7b62e162010-04-20 10:21:01 -0400667 result = -EINVAL;
668 break;
669 }
670
Jingoo Han29707b22014-02-05 15:13:14 +0900671 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar4af46622009-02-04 09:07:00 -0500672 if (!result) {
Mimi Zohar139069e2014-11-05 07:48:36 -0500673 entry->uid = make_kuid(current_user_ns(),
674 (uid_t) lnum);
675 if (!uid_valid(entry->uid) ||
676 (uid_t)lnum != lnum)
Mimi Zohar4af46622009-02-04 09:07:00 -0500677 result = -EINVAL;
678 else
Mimi Zohar139069e2014-11-05 07:48:36 -0500679 entry->flags |= (token == Opt_uid)
680 ? IMA_UID : IMA_EUID;
Mimi Zohar4af46622009-02-04 09:07:00 -0500681 }
682 break;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500683 case Opt_fowner:
684 ima_log_string(ab, "fowner", args[0].from);
685
Linus Torvalds882653222012-10-02 21:38:48 -0700686 if (uid_valid(entry->fowner)) {
Mimi Zohar07f6a792011-03-09 22:25:48 -0500687 result = -EINVAL;
688 break;
689 }
690
Jingoo Han29707b22014-02-05 15:13:14 +0900691 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500692 if (!result) {
Linus Torvalds882653222012-10-02 21:38:48 -0700693 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
694 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
Mimi Zohar07f6a792011-03-09 22:25:48 -0500695 result = -EINVAL;
696 else
697 entry->flags |= IMA_FOWNER;
698 }
699 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500700 case Opt_obj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400701 ima_log_string(ab, "obj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500702 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500703 LSM_OBJ_USER,
704 AUDIT_OBJ_USER);
705 break;
706 case Opt_obj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400707 ima_log_string(ab, "obj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500708 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500709 LSM_OBJ_ROLE,
710 AUDIT_OBJ_ROLE);
711 break;
712 case Opt_obj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400713 ima_log_string(ab, "obj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500714 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500715 LSM_OBJ_TYPE,
716 AUDIT_OBJ_TYPE);
717 break;
718 case Opt_subj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400719 ima_log_string(ab, "subj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500720 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500721 LSM_SUBJ_USER,
722 AUDIT_SUBJ_USER);
723 break;
724 case Opt_subj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400725 ima_log_string(ab, "subj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500726 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500727 LSM_SUBJ_ROLE,
728 AUDIT_SUBJ_ROLE);
729 break;
730 case Opt_subj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400731 ima_log_string(ab, "subj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500732 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500733 LSM_SUBJ_TYPE,
734 AUDIT_SUBJ_TYPE);
735 break;
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300736 case Opt_appraise_type:
737 if (entry->action != APPRAISE) {
738 result = -EINVAL;
739 break;
740 }
741
742 ima_log_string(ab, "appraise_type", args[0].from);
743 if ((strcmp(args[0].from, "imasig")) == 0)
744 entry->flags |= IMA_DIGSIG_REQUIRED;
745 else
746 result = -EINVAL;
747 break;
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400748 case Opt_permit_directio:
749 entry->flags |= IMA_PERMIT_DIRECTIO;
750 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500751 case Opt_err:
Eric Paris2f1506c2010-04-20 10:21:30 -0400752 ima_log_string(ab, "UNKNOWN", p);
Eric Parise9d393b2010-04-20 10:21:13 -0400753 result = -EINVAL;
Mimi Zohar4af46622009-02-04 09:07:00 -0500754 break;
755 }
756 }
Eric Paris7b62e162010-04-20 10:21:01 -0400757 if (!result && (entry->action == UNKNOWN))
Mimi Zohar4af46622009-02-04 09:07:00 -0500758 result = -EINVAL;
Mimi Zohara7f2a362012-12-21 08:34:21 -0500759 else if (entry->func == MODULE_CHECK)
760 ima_appraise |= IMA_APPRAISE_MODULES;
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400761 else if (entry->func == FIRMWARE_CHECK)
762 ima_appraise |= IMA_APPRAISE_FIRMWARE;
Eric Parisb0d5de42012-02-14 17:11:07 -0500763 audit_log_format(ab, "res=%d", !result);
Mimi Zohar4af46622009-02-04 09:07:00 -0500764 audit_log_end(ab);
765 return result;
766}
767
768/**
Mimi Zohar07f6a792011-03-09 22:25:48 -0500769 * ima_parse_add_rule - add a rule to ima_policy_rules
Mimi Zohar4af46622009-02-04 09:07:00 -0500770 * @rule - ima measurement policy rule
771 *
Petko Manolov38d859f2015-12-02 17:47:54 +0200772 * Avoid locking by allowing just one writer at a time in ima_write_policy()
Eric Paris6ccd0452010-04-20 10:20:54 -0400773 * Returns the length of the rule parsed, an error code on failure
Mimi Zohar4af46622009-02-04 09:07:00 -0500774 */
Eric Paris6ccd0452010-04-20 10:20:54 -0400775ssize_t ima_parse_add_rule(char *rule)
Mimi Zohar4af46622009-02-04 09:07:00 -0500776{
Mimi Zohar52a13282013-12-11 14:44:04 -0500777 static const char op[] = "update_policy";
Eric Paris6ccd0452010-04-20 10:20:54 -0400778 char *p;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500779 struct ima_rule_entry *entry;
Eric Paris6ccd0452010-04-20 10:20:54 -0400780 ssize_t result, len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500781 int audit_info = 0;
782
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +0300783 p = strsep(&rule, "\n");
784 len = strlen(p) + 1;
Dmitry Kasatkin71787842014-10-03 14:40:20 +0300785 p += strspn(p, " \t");
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +0300786
Dmitry Kasatkin71787842014-10-03 14:40:20 +0300787 if (*p == '#' || *p == '\0')
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +0300788 return len;
789
Mimi Zohar4af46622009-02-04 09:07:00 -0500790 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
791 if (!entry) {
792 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
793 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
794 return -ENOMEM;
795 }
796
797 INIT_LIST_HEAD(&entry->list);
798
Eric Paris6ccd0452010-04-20 10:20:54 -0400799 result = ima_parse_rule(p, entry);
Eric Paris7233e3e2010-04-20 10:21:24 -0400800 if (result) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500801 kfree(entry);
Mimi Zohar523979a2009-02-11 11:12:28 -0500802 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
Richard Guy Briggs7e9001f2014-06-16 15:52:07 -0400803 NULL, op, "invalid-policy", result,
Mimi Zohar523979a2009-02-11 11:12:28 -0500804 audit_info);
Eric Paris7233e3e2010-04-20 10:21:24 -0400805 return result;
Mimi Zohar523979a2009-02-11 11:12:28 -0500806 }
Eric Paris7233e3e2010-04-20 10:21:24 -0400807
Petko Manolov38d859f2015-12-02 17:47:54 +0200808 list_add_tail(&entry->list, &ima_temp_rules);
Eric Paris7233e3e2010-04-20 10:21:24 -0400809
810 return len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500811}
812
Petko Manolov38d859f2015-12-02 17:47:54 +0200813/**
814 * ima_delete_rules() called to cleanup invalid in-flight policy.
815 * We don't need locking as we operate on the temp list, which is
816 * different from the active one. There is also only one user of
817 * ima_delete_rules() at a time.
818 */
James Morris64c61d82009-02-05 09:28:26 +1100819void ima_delete_rules(void)
Mimi Zohar4af46622009-02-04 09:07:00 -0500820{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500821 struct ima_rule_entry *entry, *tmp;
Mimi Zohar7163a992013-01-03 14:19:09 -0500822 int i;
Mimi Zohar4af46622009-02-04 09:07:00 -0500823
Petko Manolov38d859f2015-12-02 17:47:54 +0200824 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
Mimi Zohar7163a992013-01-03 14:19:09 -0500825 for (i = 0; i < MAX_LSM_RULES; i++)
826 kfree(entry->lsm[i].args_p);
827
Mimi Zohar4af46622009-02-04 09:07:00 -0500828 list_del(&entry->list);
829 kfree(entry);
830 }
Mimi Zohar4af46622009-02-04 09:07:00 -0500831}
Petko Manolov80eae202015-12-02 17:47:56 +0200832
833#ifdef CONFIG_IMA_READ_POLICY
834enum {
835 mask_exec = 0, mask_write, mask_read, mask_append
836};
837
838static char *mask_tokens[] = {
839 "MAY_EXEC",
840 "MAY_WRITE",
841 "MAY_READ",
842 "MAY_APPEND"
843};
844
845enum {
846 func_file = 0, func_mmap, func_bprm,
847 func_module, func_firmware, func_post
848};
849
850static char *func_tokens[] = {
851 "FILE_CHECK",
852 "MMAP_CHECK",
853 "BPRM_CHECK",
854 "MODULE_CHECK",
855 "FIRMWARE_CHECK",
856 "POST_SETATTR"
857};
858
859void *ima_policy_start(struct seq_file *m, loff_t *pos)
860{
861 loff_t l = *pos;
862 struct ima_rule_entry *entry;
863
864 rcu_read_lock();
865 list_for_each_entry_rcu(entry, ima_rules, list) {
866 if (!l--) {
867 rcu_read_unlock();
868 return entry;
869 }
870 }
871 rcu_read_unlock();
872 return NULL;
873}
874
875void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
876{
877 struct ima_rule_entry *entry = v;
878
879 rcu_read_lock();
880 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
881 rcu_read_unlock();
882 (*pos)++;
883
884 return (&entry->list == ima_rules) ? NULL : entry;
885}
886
887void ima_policy_stop(struct seq_file *m, void *v)
888{
889}
890
891#define pt(token) policy_tokens[token + Opt_err].pattern
892#define mt(token) mask_tokens[token]
893#define ft(token) func_tokens[token]
894
895int ima_policy_show(struct seq_file *m, void *v)
896{
897 struct ima_rule_entry *entry = v;
898 int i = 0;
899 char tbuf[64] = {0,};
900
901 rcu_read_lock();
902
903 if (entry->action & MEASURE)
904 seq_puts(m, pt(Opt_measure));
905 if (entry->action & DONT_MEASURE)
906 seq_puts(m, pt(Opt_dont_measure));
907 if (entry->action & APPRAISE)
908 seq_puts(m, pt(Opt_appraise));
909 if (entry->action & DONT_APPRAISE)
910 seq_puts(m, pt(Opt_dont_appraise));
911 if (entry->action & AUDIT)
912 seq_puts(m, pt(Opt_audit));
913
914 seq_puts(m, " ");
915
916 if (entry->flags & IMA_FUNC) {
917 switch (entry->func) {
918 case FILE_CHECK:
919 seq_printf(m, pt(Opt_func), ft(func_file));
920 break;
921 case MMAP_CHECK:
922 seq_printf(m, pt(Opt_func), ft(func_mmap));
923 break;
924 case BPRM_CHECK:
925 seq_printf(m, pt(Opt_func), ft(func_bprm));
926 break;
927 case MODULE_CHECK:
928 seq_printf(m, pt(Opt_func), ft(func_module));
929 break;
930 case FIRMWARE_CHECK:
931 seq_printf(m, pt(Opt_func), ft(func_firmware));
932 break;
933 case POST_SETATTR:
934 seq_printf(m, pt(Opt_func), ft(func_post));
935 break;
936 default:
937 snprintf(tbuf, sizeof(tbuf), "%d", entry->func);
938 seq_printf(m, pt(Opt_func), tbuf);
939 break;
940 }
941 seq_puts(m, " ");
942 }
943
944 if (entry->flags & IMA_MASK) {
945 if (entry->mask & MAY_EXEC)
946 seq_printf(m, pt(Opt_mask), mt(mask_exec));
947 if (entry->mask & MAY_WRITE)
948 seq_printf(m, pt(Opt_mask), mt(mask_write));
949 if (entry->mask & MAY_READ)
950 seq_printf(m, pt(Opt_mask), mt(mask_read));
951 if (entry->mask & MAY_APPEND)
952 seq_printf(m, pt(Opt_mask), mt(mask_append));
953 seq_puts(m, " ");
954 }
955
956 if (entry->flags & IMA_FSMAGIC) {
957 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
958 seq_printf(m, pt(Opt_fsmagic), tbuf);
959 seq_puts(m, " ");
960 }
961
962 if (entry->flags & IMA_FSUUID) {
963 seq_puts(m, "fsuuid=");
964 for (i = 0; i < ARRAY_SIZE(entry->fsuuid); ++i) {
965 switch (i) {
966 case 4:
967 case 6:
968 case 8:
969 case 10:
970 seq_puts(m, "-");
971 }
972 seq_printf(m, "%x", entry->fsuuid[i]);
973 }
974 seq_puts(m, " ");
975 }
976
977 if (entry->flags & IMA_UID) {
978 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
979 seq_printf(m, pt(Opt_uid), tbuf);
980 seq_puts(m, " ");
981 }
982
983 if (entry->flags & IMA_EUID) {
984 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
985 seq_printf(m, pt(Opt_euid), tbuf);
986 seq_puts(m, " ");
987 }
988
989 if (entry->flags & IMA_FOWNER) {
990 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
991 seq_printf(m, pt(Opt_fowner), tbuf);
992 seq_puts(m, " ");
993 }
994
995 for (i = 0; i < MAX_LSM_RULES; i++) {
996 if (entry->lsm[i].rule) {
997 switch (i) {
998 case LSM_OBJ_USER:
999 seq_printf(m, pt(Opt_obj_user),
1000 (char *)entry->lsm[i].args_p);
1001 break;
1002 case LSM_OBJ_ROLE:
1003 seq_printf(m, pt(Opt_obj_role),
1004 (char *)entry->lsm[i].args_p);
1005 break;
1006 case LSM_OBJ_TYPE:
1007 seq_printf(m, pt(Opt_obj_type),
1008 (char *)entry->lsm[i].args_p);
1009 break;
1010 case LSM_SUBJ_USER:
1011 seq_printf(m, pt(Opt_subj_user),
1012 (char *)entry->lsm[i].args_p);
1013 break;
1014 case LSM_SUBJ_ROLE:
1015 seq_printf(m, pt(Opt_subj_role),
1016 (char *)entry->lsm[i].args_p);
1017 break;
1018 case LSM_SUBJ_TYPE:
1019 seq_printf(m, pt(Opt_subj_type),
1020 (char *)entry->lsm[i].args_p);
1021 break;
1022 }
1023 }
1024 }
1025 if (entry->flags & IMA_DIGSIG_REQUIRED)
1026 seq_puts(m, "appraise_type=imasig ");
1027 if (entry->flags & IMA_PERMIT_DIRECTIO)
1028 seq_puts(m, "permit_directio ");
1029 rcu_read_unlock();
1030 seq_puts(m, "\n");
1031 return 0;
1032}
1033#endif /* CONFIG_IMA_READ_POLICY */