blob: b089ebef66486c2f6f910138b40ed921afe66c23 [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;
Mimi Zohar6ad6afa2015-12-07 14:35:47 -050043static int temp_ima_appraise;
Roberto Sassua7560242014-09-12 19:35:54 +020044
Mimi Zohar4af46622009-02-04 09:07:00 -050045#define MAX_LSM_RULES 6
46enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
47 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
48};
Mimi Zohar3323eec2009-02-04 09:06:58 -050049
Mimi Zohar24fd03c2015-06-11 20:48:33 -040050enum policy_types { ORIGINAL_TCB = 1, DEFAULT_TCB };
51
Mimi Zohar07f6a792011-03-09 22:25:48 -050052struct ima_rule_entry {
Mimi Zohar3323eec2009-02-04 09:06:58 -050053 struct list_head list;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050054 int action;
Mimi Zohar3323eec2009-02-04 09:06:58 -050055 unsigned int flags;
56 enum ima_hooks func;
57 int mask;
58 unsigned long fsmagic;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +030059 u8 fsuuid[16];
Eric W. Biederman8b94eea2012-05-25 18:24:12 -060060 kuid_t uid;
Linus Torvalds882653222012-10-02 21:38:48 -070061 kuid_t fowner;
Mimi Zohar4af46622009-02-04 09:07:00 -050062 struct {
63 void *rule; /* LSM file metadata specific */
Mimi Zohar7163a992013-01-03 14:19:09 -050064 void *args_p; /* audit value */
Mimi Zohar4af46622009-02-04 09:07:00 -050065 int type; /* audit type */
66 } lsm[MAX_LSM_RULES];
Mimi Zohar3323eec2009-02-04 09:06:58 -050067};
68
Eric Paris5789ba32009-05-21 15:47:06 -040069/*
70 * Without LSM specific knowledge, the default policy can only be
Mimi Zohar07f6a792011-03-09 22:25:48 -050071 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
Mimi Zohar4af46622009-02-04 09:07:00 -050072 */
Eric Paris5789ba32009-05-21 15:47:06 -040073
74/*
75 * The minimum rule set to allow for full TCB coverage. Measures all files
76 * opened or mmap for exec and everything read by root. Dangerous because
77 * normal users can easily run the machine out of memory simply building
78 * and running executables.
79 */
Mimi Zohar24fd03c2015-06-11 20:48:33 -040080static struct ima_rule_entry dont_measure_rules[] = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020081 {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
82 {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
83 {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
84 {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
85 {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
86 {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
87 {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
88 {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
Roberto Sassu6438de92015-04-11 17:13:06 +020089 {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
90 .flags = IMA_FSMAGIC},
Mimi Zohar24fd03c2015-06-11 20:48:33 -040091 {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC}
92};
93
94static struct ima_rule_entry original_measurement_rules[] = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020095 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
Mimi Zohar3323eec2009-02-04 09:06:58 -050096 .flags = IMA_FUNC | IMA_MASK},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020097 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
Mimi Zohar3323eec2009-02-04 09:06:58 -050098 .flags = IMA_FUNC | IMA_MASK},
Mimi Zohar24fd03c2015-06-11 20:48:33 -040099 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
100 .uid = GLOBAL_ROOT_UID, .flags = IMA_FUNC | IMA_MASK | IMA_UID},
101 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
102 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
103};
104
105static struct ima_rule_entry default_measurement_rules[] = {
106 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
107 .flags = IMA_FUNC | IMA_MASK},
108 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
109 .flags = IMA_FUNC | IMA_MASK},
110 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
111 .uid = GLOBAL_ROOT_UID, .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
112 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
113 .uid = GLOBAL_ROOT_UID, .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200114 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400115 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
Mimi Zohar3323eec2009-02-04 09:06:58 -0500116};
117
Mimi Zohar07f6a792011-03-09 22:25:48 -0500118static struct ima_rule_entry default_appraise_rules[] = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200119 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
120 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
121 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
122 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
123 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
124 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
125 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
126 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
127 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
Mimi Zoharcd025f72015-04-21 16:54:24 -0400128 {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200129 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200130#ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200131 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .flags = IMA_FOWNER},
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200132#else
133 /* force signature */
134 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID,
135 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
136#endif
Mimi Zohar07f6a792011-03-09 22:25:48 -0500137};
Mimi Zohar3323eec2009-02-04 09:06:58 -0500138
Mimi Zohar07f6a792011-03-09 22:25:48 -0500139static LIST_HEAD(ima_default_rules);
140static LIST_HEAD(ima_policy_rules);
Petko Manolov38d859f2015-12-02 17:47:54 +0200141static LIST_HEAD(ima_temp_rules);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500142static struct list_head *ima_rules;
143
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400144static int ima_policy __initdata;
Petko Manolov38d859f2015-12-02 17:47:54 +0200145
Mimi Zohar07f6a792011-03-09 22:25:48 -0500146static int __init default_measure_policy_setup(char *str)
Eric Paris5789ba32009-05-21 15:47:06 -0400147{
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400148 if (ima_policy)
149 return 1;
150
151 ima_policy = ORIGINAL_TCB;
Eric Paris5789ba32009-05-21 15:47:06 -0400152 return 1;
153}
Mimi Zohar07f6a792011-03-09 22:25:48 -0500154__setup("ima_tcb", default_measure_policy_setup);
155
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400156static int __init policy_setup(char *str)
157{
158 if (ima_policy)
159 return 1;
160
161 if (strcmp(str, "tcb") == 0)
162 ima_policy = DEFAULT_TCB;
163
164 return 1;
165}
166__setup("ima_policy=", policy_setup);
167
Mimi Zohar07f6a792011-03-09 22:25:48 -0500168static bool ima_use_appraise_tcb __initdata;
169static int __init default_appraise_policy_setup(char *str)
170{
171 ima_use_appraise_tcb = 1;
172 return 1;
173}
174__setup("ima_appraise_tcb", default_appraise_policy_setup);
Eric Paris5789ba32009-05-21 15:47:06 -0400175
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200176/*
Petko Manolov38d859f2015-12-02 17:47:54 +0200177 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
178 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
179 * the reloaded LSM policy. We assume the rules still exist; and BUG_ON() if
180 * they don't.
Mimi Zohar7163a992013-01-03 14:19:09 -0500181 */
182static void ima_lsm_update_rules(void)
183{
Petko Manolov38d859f2015-12-02 17:47:54 +0200184 struct ima_rule_entry *entry;
Mimi Zohar7163a992013-01-03 14:19:09 -0500185 int result;
186 int i;
187
Petko Manolov38d859f2015-12-02 17:47:54 +0200188 list_for_each_entry(entry, &ima_policy_rules, list) {
Mimi Zohar7163a992013-01-03 14:19:09 -0500189 for (i = 0; i < MAX_LSM_RULES; i++) {
190 if (!entry->lsm[i].rule)
191 continue;
192 result = security_filter_rule_init(entry->lsm[i].type,
193 Audit_equal,
194 entry->lsm[i].args_p,
195 &entry->lsm[i].rule);
196 BUG_ON(!entry->lsm[i].rule);
197 }
198 }
Mimi Zohar7163a992013-01-03 14:19:09 -0500199}
200
Mimi Zohar3323eec2009-02-04 09:06:58 -0500201/**
202 * ima_match_rules - determine whether an inode matches the measure rule.
203 * @rule: a pointer to a rule
204 * @inode: a pointer to an inode
205 * @func: LIM hook identifier
206 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
207 *
208 * Returns true on rule match, false on failure.
209 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500210static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,
211 enum ima_hooks func, int mask)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500212{
213 struct task_struct *tsk = current;
Mimi Zohar3db59dd2012-01-17 22:11:28 -0500214 const struct cred *cred = current_cred();
Mimi Zohar4af46622009-02-04 09:07:00 -0500215 int i;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500216
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200217 if ((rule->flags & IMA_FUNC) &&
218 (rule->func != func && func != POST_SETATTR))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500219 return false;
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200220 if ((rule->flags & IMA_MASK) &&
221 (rule->mask != mask && func != POST_SETATTR))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500222 return false;
Mimi Zohar4351c292014-11-05 07:53:55 -0500223 if ((rule->flags & IMA_INMASK) &&
224 (!(rule->mask & mask) && func != POST_SETATTR))
225 return false;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500226 if ((rule->flags & IMA_FSMAGIC)
227 && rule->fsmagic != inode->i_sb->s_magic)
228 return false;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300229 if ((rule->flags & IMA_FSUUID) &&
Mimi Zohar446d64e2013-02-24 23:42:37 -0500230 memcmp(rule->fsuuid, inode->i_sb->s_uuid, sizeof(rule->fsuuid)))
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300231 return false;
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600232 if ((rule->flags & IMA_UID) && !uid_eq(rule->uid, cred->uid))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500233 return false;
Mimi Zohar139069e2014-11-05 07:48:36 -0500234 if (rule->flags & IMA_EUID) {
235 if (has_capability_noaudit(current, CAP_SETUID)) {
236 if (!uid_eq(rule->uid, cred->euid)
237 && !uid_eq(rule->uid, cred->suid)
238 && !uid_eq(rule->uid, cred->uid))
239 return false;
240 } else if (!uid_eq(rule->uid, cred->euid))
241 return false;
242 }
243
Linus Torvalds882653222012-10-02 21:38:48 -0700244 if ((rule->flags & IMA_FOWNER) && !uid_eq(rule->fowner, inode->i_uid))
Mimi Zohar07f6a792011-03-09 22:25:48 -0500245 return false;
Mimi Zohar4af46622009-02-04 09:07:00 -0500246 for (i = 0; i < MAX_LSM_RULES; i++) {
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400247 int rc = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500248 u32 osid, sid;
Mimi Zohar7163a992013-01-03 14:19:09 -0500249 int retried = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500250
251 if (!rule->lsm[i].rule)
252 continue;
Mimi Zohar7163a992013-01-03 14:19:09 -0500253retry:
Mimi Zohar4af46622009-02-04 09:07:00 -0500254 switch (i) {
255 case LSM_OBJ_USER:
256 case LSM_OBJ_ROLE:
257 case LSM_OBJ_TYPE:
258 security_inode_getsecid(inode, &osid);
259 rc = security_filter_rule_match(osid,
260 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400261 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500262 rule->lsm[i].rule,
263 NULL);
264 break;
265 case LSM_SUBJ_USER:
266 case LSM_SUBJ_ROLE:
267 case LSM_SUBJ_TYPE:
268 security_task_getsecid(tsk, &sid);
269 rc = security_filter_rule_match(sid,
270 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400271 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500272 rule->lsm[i].rule,
273 NULL);
274 default:
275 break;
276 }
Mimi Zohar7163a992013-01-03 14:19:09 -0500277 if ((rc < 0) && (!retried)) {
278 retried = 1;
279 ima_lsm_update_rules();
280 goto retry;
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200281 }
Mimi Zohar4af46622009-02-04 09:07:00 -0500282 if (!rc)
283 return false;
284 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500285 return true;
286}
287
Mimi Zohard79d72e2012-12-03 17:08:11 -0500288/*
289 * In addition to knowing that we need to appraise the file in general,
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500290 * we need to differentiate between calling hooks, for hook specific rules.
Mimi Zohard79d72e2012-12-03 17:08:11 -0500291 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500292static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
Mimi Zohard79d72e2012-12-03 17:08:11 -0500293{
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500294 if (!(rule->flags & IMA_FUNC))
295 return IMA_FILE_APPRAISE;
296
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200297 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500298 case MMAP_CHECK:
299 return IMA_MMAP_APPRAISE;
300 case BPRM_CHECK:
301 return IMA_BPRM_APPRAISE;
302 case MODULE_CHECK:
303 return IMA_MODULE_APPRAISE;
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400304 case FIRMWARE_CHECK:
305 return IMA_FIRMWARE_APPRAISE;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500306 case FILE_CHECK:
307 default:
308 return IMA_FILE_APPRAISE;
309 }
310}
311
Mimi Zohar3323eec2009-02-04 09:06:58 -0500312/**
313 * ima_match_policy - decision based on LSM and other conditions
314 * @inode: pointer to an inode for which the policy decision is being made
315 * @func: IMA hook identifier
316 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
317 *
318 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
319 * conditions.
320 *
Petko Manolov38d859f2015-12-02 17:47:54 +0200321 * Since the IMA policy may be updated multiple times we need to lock the
322 * list when walking it. Reads are many orders of magnitude more numerous
323 * than writes so ima_match_policy() is classical RCU candidate.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500324 */
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500325int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
326 int flags)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500327{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500328 struct ima_rule_entry *entry;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500329 int action = 0, actmask = flags | (flags << 1);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500330
Petko Manolov38d859f2015-12-02 17:47:54 +0200331 rcu_read_lock();
332 list_for_each_entry_rcu(entry, ima_rules, list) {
Mimi Zohar3323eec2009-02-04 09:06:58 -0500333
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500334 if (!(entry->action & actmask))
335 continue;
336
337 if (!ima_match_rules(entry, inode, func, mask))
338 continue;
339
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300340 action |= entry->flags & IMA_ACTION_FLAGS;
341
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300342 action |= entry->action & IMA_DO_MASK;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500343 if (entry->action & IMA_APPRAISE)
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500344 action |= get_subaction(entry, func);
Mimi Zohard79d72e2012-12-03 17:08:11 -0500345
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300346 if (entry->action & IMA_DO_MASK)
347 actmask &= ~(entry->action | entry->action << 1);
348 else
349 actmask &= ~(entry->action | entry->action >> 1);
350
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500351 if (!actmask)
352 break;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500353 }
Petko Manolov38d859f2015-12-02 17:47:54 +0200354 rcu_read_unlock();
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500355
356 return action;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500357}
358
Roberto Sassua7560242014-09-12 19:35:54 +0200359/*
360 * Initialize the ima_policy_flag variable based on the currently
361 * loaded policy. Based on this flag, the decision to short circuit
362 * out of a function or not call the function in the first place
363 * can be made earlier.
364 */
365void ima_update_policy_flag(void)
366{
367 struct ima_rule_entry *entry;
368
Roberto Sassua7560242014-09-12 19:35:54 +0200369 list_for_each_entry(entry, ima_rules, list) {
370 if (entry->action & IMA_DO_MASK)
371 ima_policy_flag |= entry->action;
372 }
373
Mimi Zohar6ad6afa2015-12-07 14:35:47 -0500374 ima_appraise |= temp_ima_appraise;
Roberto Sassua7560242014-09-12 19:35:54 +0200375 if (!ima_appraise)
376 ima_policy_flag &= ~IMA_APPRAISE;
377}
378
Mimi Zohar3323eec2009-02-04 09:06:58 -0500379/**
380 * ima_init_policy - initialize the default measure rules.
381 *
Mimi Zohar07f6a792011-03-09 22:25:48 -0500382 * ima_rules points to either the ima_default_rules or the
383 * the new ima_policy_rules.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500384 */
Eric Paris932995f2009-05-21 15:43:32 -0400385void __init ima_init_policy(void)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500386{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500387 int i, measure_entries, appraise_entries;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500388
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400389 /* if !ima_policy set entries = 0 so we load NO default rules */
390 measure_entries = ima_policy ? ARRAY_SIZE(dont_measure_rules) : 0;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500391 appraise_entries = ima_use_appraise_tcb ?
392 ARRAY_SIZE(default_appraise_rules) : 0;
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200393
Dan Carpenter55778572015-04-07 12:22:11 +0300394 for (i = 0; i < measure_entries; i++)
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400395 list_add_tail(&dont_measure_rules[i].list, &ima_default_rules);
396
397 switch (ima_policy) {
398 case ORIGINAL_TCB:
399 for (i = 0; i < ARRAY_SIZE(original_measurement_rules); i++)
400 list_add_tail(&original_measurement_rules[i].list,
401 &ima_default_rules);
402 break;
403 case DEFAULT_TCB:
404 for (i = 0; i < ARRAY_SIZE(default_measurement_rules); i++)
405 list_add_tail(&default_measurement_rules[i].list,
406 &ima_default_rules);
407 default:
408 break;
409 }
Eric Paris5789ba32009-05-21 15:47:06 -0400410
Dan Carpenter55778572015-04-07 12:22:11 +0300411 for (i = 0; i < appraise_entries; i++) {
412 list_add_tail(&default_appraise_rules[i].list,
413 &ima_default_rules);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500414 }
415
416 ima_rules = &ima_default_rules;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500417}
Mimi Zohar4af46622009-02-04 09:07:00 -0500418
Sasha Levin01127212015-12-22 08:51:23 -0500419/* Make sure we have a valid policy, at least containing some rules. */
Colin Ian Kingc75d8e92016-01-20 11:13:46 +0000420int ima_check_policy(void)
Sasha Levin01127212015-12-22 08:51:23 -0500421{
422 if (list_empty(&ima_temp_rules))
423 return -EINVAL;
424 return 0;
425}
426
Mimi Zohar4af46622009-02-04 09:07:00 -0500427/**
428 * ima_update_policy - update default_rules with new measure rules
429 *
430 * Called on file .release to update the default rules with a complete new
Petko Manolov38d859f2015-12-02 17:47:54 +0200431 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
432 * they make a queue. The policy may be updated multiple times and this is the
433 * RCU updater.
434 *
435 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
436 * we switch from the default policy to user defined.
Mimi Zohar4af46622009-02-04 09:07:00 -0500437 */
438void ima_update_policy(void)
439{
Petko Manolov38d859f2015-12-02 17:47:54 +0200440 struct list_head *first, *last, *policy;
441
442 /* append current policy with the new rules */
443 first = (&ima_temp_rules)->next;
444 last = (&ima_temp_rules)->prev;
445 policy = &ima_policy_rules;
446
447 synchronize_rcu();
448
449 last->next = policy;
450 rcu_assign_pointer(list_next_rcu(policy->prev), first);
451 first->prev = policy->prev;
452 policy->prev = last;
453
454 /* prepare for the next policy rules addition */
455 INIT_LIST_HEAD(&ima_temp_rules);
456
457 if (ima_rules != policy) {
458 ima_policy_flag = 0;
459 ima_rules = policy;
460 }
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300461 ima_update_policy_flag();
Mimi Zohar4af46622009-02-04 09:07:00 -0500462}
463
464enum {
465 Opt_err = -1,
466 Opt_measure = 1, Opt_dont_measure,
Mimi Zohar07f6a792011-03-09 22:25:48 -0500467 Opt_appraise, Opt_dont_appraise,
Peter Moodye7c568e2012-06-14 10:04:36 -0700468 Opt_audit,
Mimi Zohar4af46622009-02-04 09:07:00 -0500469 Opt_obj_user, Opt_obj_role, Opt_obj_type,
470 Opt_subj_user, Opt_subj_role, Opt_subj_type,
Mimi Zohar139069e2014-11-05 07:48:36 -0500471 Opt_func, Opt_mask, Opt_fsmagic,
Petko Manolov80eae202015-12-02 17:47:56 +0200472 Opt_fsuuid, Opt_uid, Opt_euid, Opt_fowner,
473 Opt_appraise_type, Opt_permit_directio
Mimi Zohar4af46622009-02-04 09:07:00 -0500474};
475
476static match_table_t policy_tokens = {
477 {Opt_measure, "measure"},
478 {Opt_dont_measure, "dont_measure"},
Mimi Zohar07f6a792011-03-09 22:25:48 -0500479 {Opt_appraise, "appraise"},
480 {Opt_dont_appraise, "dont_appraise"},
Peter Moodye7c568e2012-06-14 10:04:36 -0700481 {Opt_audit, "audit"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500482 {Opt_obj_user, "obj_user=%s"},
483 {Opt_obj_role, "obj_role=%s"},
484 {Opt_obj_type, "obj_type=%s"},
485 {Opt_subj_user, "subj_user=%s"},
486 {Opt_subj_role, "subj_role=%s"},
487 {Opt_subj_type, "subj_type=%s"},
488 {Opt_func, "func=%s"},
489 {Opt_mask, "mask=%s"},
490 {Opt_fsmagic, "fsmagic=%s"},
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300491 {Opt_fsuuid, "fsuuid=%s"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500492 {Opt_uid, "uid=%s"},
Mimi Zohar139069e2014-11-05 07:48:36 -0500493 {Opt_euid, "euid=%s"},
Mimi Zohar07f6a792011-03-09 22:25:48 -0500494 {Opt_fowner, "fowner=%s"},
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300495 {Opt_appraise_type, "appraise_type=%s"},
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400496 {Opt_permit_directio, "permit_directio"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500497 {Opt_err, NULL}
498};
499
Mimi Zohar07f6a792011-03-09 22:25:48 -0500500static int ima_lsm_rule_init(struct ima_rule_entry *entry,
Mimi Zohar7163a992013-01-03 14:19:09 -0500501 substring_t *args, int lsm_rule, int audit_type)
Mimi Zohar4af46622009-02-04 09:07:00 -0500502{
503 int result;
504
Eric Paris7b62e162010-04-20 10:21:01 -0400505 if (entry->lsm[lsm_rule].rule)
506 return -EINVAL;
507
Mimi Zohar7163a992013-01-03 14:19:09 -0500508 entry->lsm[lsm_rule].args_p = match_strdup(args);
509 if (!entry->lsm[lsm_rule].args_p)
510 return -ENOMEM;
511
Mimi Zohar4af46622009-02-04 09:07:00 -0500512 entry->lsm[lsm_rule].type = audit_type;
513 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
Mimi Zohar7163a992013-01-03 14:19:09 -0500514 Audit_equal,
515 entry->lsm[lsm_rule].args_p,
Mimi Zohar4af46622009-02-04 09:07:00 -0500516 &entry->lsm[lsm_rule].rule);
Mimi Zohar7163a992013-01-03 14:19:09 -0500517 if (!entry->lsm[lsm_rule].rule) {
518 kfree(entry->lsm[lsm_rule].args_p);
Mimi Zohar867c2022011-01-03 14:59:10 -0800519 return -EINVAL;
Mimi Zohar7163a992013-01-03 14:19:09 -0500520 }
521
Mimi Zohar4af46622009-02-04 09:07:00 -0500522 return result;
523}
524
Eric Paris2f1506c2010-04-20 10:21:30 -0400525static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
526{
527 audit_log_format(ab, "%s=", key);
528 audit_log_untrustedstring(ab, value);
529 audit_log_format(ab, " ");
530}
531
Mimi Zohar07f6a792011-03-09 22:25:48 -0500532static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
Mimi Zohar4af46622009-02-04 09:07:00 -0500533{
534 struct audit_buffer *ab;
Mimi Zohar4351c292014-11-05 07:53:55 -0500535 char *from;
Mimi Zohar4af46622009-02-04 09:07:00 -0500536 char *p;
537 int result = 0;
538
Mimi Zohar523979a2009-02-11 11:12:28 -0500539 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
Mimi Zohar4af46622009-02-04 09:07:00 -0500540
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600541 entry->uid = INVALID_UID;
Linus Torvalds882653222012-10-02 21:38:48 -0700542 entry->fowner = INVALID_UID;
Eric Parisb9035b12010-04-20 10:21:07 -0400543 entry->action = UNKNOWN;
Eric Paris28ef4002010-04-20 10:21:18 -0400544 while ((p = strsep(&rule, " \t")) != NULL) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500545 substring_t args[MAX_OPT_ARGS];
546 int token;
547 unsigned long lnum;
548
549 if (result < 0)
550 break;
Eric Paris28ef4002010-04-20 10:21:18 -0400551 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
552 continue;
Mimi Zohar4af46622009-02-04 09:07:00 -0500553 token = match_token(p, policy_tokens, args);
554 switch (token) {
555 case Opt_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400556 ima_log_string(ab, "action", "measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400557
558 if (entry->action != UNKNOWN)
559 result = -EINVAL;
560
Mimi Zohar4af46622009-02-04 09:07:00 -0500561 entry->action = MEASURE;
562 break;
563 case Opt_dont_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400564 ima_log_string(ab, "action", "dont_measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400565
566 if (entry->action != UNKNOWN)
567 result = -EINVAL;
568
Mimi Zohar4af46622009-02-04 09:07:00 -0500569 entry->action = DONT_MEASURE;
570 break;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500571 case Opt_appraise:
572 ima_log_string(ab, "action", "appraise");
573
574 if (entry->action != UNKNOWN)
575 result = -EINVAL;
576
577 entry->action = APPRAISE;
578 break;
579 case Opt_dont_appraise:
580 ima_log_string(ab, "action", "dont_appraise");
581
582 if (entry->action != UNKNOWN)
583 result = -EINVAL;
584
585 entry->action = DONT_APPRAISE;
586 break;
Peter Moodye7c568e2012-06-14 10:04:36 -0700587 case Opt_audit:
588 ima_log_string(ab, "action", "audit");
589
590 if (entry->action != UNKNOWN)
591 result = -EINVAL;
592
593 entry->action = AUDIT;
594 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500595 case Opt_func:
Eric Paris2f1506c2010-04-20 10:21:30 -0400596 ima_log_string(ab, "func", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400597
598 if (entry->func)
Mimi Zohar07f6a792011-03-09 22:25:48 -0500599 result = -EINVAL;
Eric Paris7b62e162010-04-20 10:21:01 -0400600
Mimi Zohar1e93d002010-01-26 17:02:41 -0500601 if (strcmp(args[0].from, "FILE_CHECK") == 0)
602 entry->func = FILE_CHECK;
603 /* PATH_CHECK is for backwards compat */
604 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
605 entry->func = FILE_CHECK;
Mimi Zoharfdf90722012-10-16 12:40:08 +1030606 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
607 entry->func = MODULE_CHECK;
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400608 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
609 entry->func = FIRMWARE_CHECK;
Mimi Zohar16cac492012-12-13 11:15:04 -0500610 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
611 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
612 entry->func = MMAP_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500613 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
614 entry->func = BPRM_CHECK;
615 else
616 result = -EINVAL;
617 if (!result)
618 entry->flags |= IMA_FUNC;
619 break;
620 case Opt_mask:
Eric Paris2f1506c2010-04-20 10:21:30 -0400621 ima_log_string(ab, "mask", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400622
623 if (entry->mask)
624 result = -EINVAL;
625
Mimi Zohar4351c292014-11-05 07:53:55 -0500626 from = args[0].from;
627 if (*from == '^')
628 from++;
629
630 if ((strcmp(from, "MAY_EXEC")) == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500631 entry->mask = MAY_EXEC;
Mimi Zohar4351c292014-11-05 07:53:55 -0500632 else if (strcmp(from, "MAY_WRITE") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500633 entry->mask = MAY_WRITE;
Mimi Zohar4351c292014-11-05 07:53:55 -0500634 else if (strcmp(from, "MAY_READ") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500635 entry->mask = MAY_READ;
Mimi Zohar4351c292014-11-05 07:53:55 -0500636 else if (strcmp(from, "MAY_APPEND") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500637 entry->mask = MAY_APPEND;
638 else
639 result = -EINVAL;
640 if (!result)
Mimi Zohar4351c292014-11-05 07:53:55 -0500641 entry->flags |= (*args[0].from == '^')
642 ? IMA_INMASK : IMA_MASK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500643 break;
644 case Opt_fsmagic:
Eric Paris2f1506c2010-04-20 10:21:30 -0400645 ima_log_string(ab, "fsmagic", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400646
647 if (entry->fsmagic) {
648 result = -EINVAL;
649 break;
650 }
651
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200652 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
Mimi Zohar4af46622009-02-04 09:07:00 -0500653 if (!result)
654 entry->flags |= IMA_FSMAGIC;
655 break;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300656 case Opt_fsuuid:
657 ima_log_string(ab, "fsuuid", args[0].from);
658
659 if (memchr_inv(entry->fsuuid, 0x00,
Mimi Zohar446d64e2013-02-24 23:42:37 -0500660 sizeof(entry->fsuuid))) {
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300661 result = -EINVAL;
662 break;
663 }
664
Mimi Zohar446d64e2013-02-24 23:42:37 -0500665 result = blk_part_pack_uuid(args[0].from,
666 entry->fsuuid);
667 if (!result)
668 entry->flags |= IMA_FSUUID;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300669 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500670 case Opt_uid:
Eric Paris2f1506c2010-04-20 10:21:30 -0400671 ima_log_string(ab, "uid", args[0].from);
Mimi Zohar139069e2014-11-05 07:48:36 -0500672 case Opt_euid:
673 if (token == Opt_euid)
674 ima_log_string(ab, "euid", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400675
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600676 if (uid_valid(entry->uid)) {
Eric Paris7b62e162010-04-20 10:21:01 -0400677 result = -EINVAL;
678 break;
679 }
680
Jingoo Han29707b22014-02-05 15:13:14 +0900681 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar4af46622009-02-04 09:07:00 -0500682 if (!result) {
Mimi Zohar139069e2014-11-05 07:48:36 -0500683 entry->uid = make_kuid(current_user_ns(),
684 (uid_t) lnum);
685 if (!uid_valid(entry->uid) ||
686 (uid_t)lnum != lnum)
Mimi Zohar4af46622009-02-04 09:07:00 -0500687 result = -EINVAL;
688 else
Mimi Zohar139069e2014-11-05 07:48:36 -0500689 entry->flags |= (token == Opt_uid)
690 ? IMA_UID : IMA_EUID;
Mimi Zohar4af46622009-02-04 09:07:00 -0500691 }
692 break;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500693 case Opt_fowner:
694 ima_log_string(ab, "fowner", args[0].from);
695
Linus Torvalds882653222012-10-02 21:38:48 -0700696 if (uid_valid(entry->fowner)) {
Mimi Zohar07f6a792011-03-09 22:25:48 -0500697 result = -EINVAL;
698 break;
699 }
700
Jingoo Han29707b22014-02-05 15:13:14 +0900701 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500702 if (!result) {
Linus Torvalds882653222012-10-02 21:38:48 -0700703 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
704 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
Mimi Zohar07f6a792011-03-09 22:25:48 -0500705 result = -EINVAL;
706 else
707 entry->flags |= IMA_FOWNER;
708 }
709 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500710 case Opt_obj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400711 ima_log_string(ab, "obj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500712 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500713 LSM_OBJ_USER,
714 AUDIT_OBJ_USER);
715 break;
716 case Opt_obj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400717 ima_log_string(ab, "obj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500718 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500719 LSM_OBJ_ROLE,
720 AUDIT_OBJ_ROLE);
721 break;
722 case Opt_obj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400723 ima_log_string(ab, "obj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500724 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500725 LSM_OBJ_TYPE,
726 AUDIT_OBJ_TYPE);
727 break;
728 case Opt_subj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400729 ima_log_string(ab, "subj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500730 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500731 LSM_SUBJ_USER,
732 AUDIT_SUBJ_USER);
733 break;
734 case Opt_subj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400735 ima_log_string(ab, "subj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500736 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500737 LSM_SUBJ_ROLE,
738 AUDIT_SUBJ_ROLE);
739 break;
740 case Opt_subj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400741 ima_log_string(ab, "subj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500742 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500743 LSM_SUBJ_TYPE,
744 AUDIT_SUBJ_TYPE);
745 break;
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300746 case Opt_appraise_type:
747 if (entry->action != APPRAISE) {
748 result = -EINVAL;
749 break;
750 }
751
752 ima_log_string(ab, "appraise_type", args[0].from);
753 if ((strcmp(args[0].from, "imasig")) == 0)
754 entry->flags |= IMA_DIGSIG_REQUIRED;
755 else
756 result = -EINVAL;
757 break;
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400758 case Opt_permit_directio:
759 entry->flags |= IMA_PERMIT_DIRECTIO;
760 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500761 case Opt_err:
Eric Paris2f1506c2010-04-20 10:21:30 -0400762 ima_log_string(ab, "UNKNOWN", p);
Eric Parise9d393b2010-04-20 10:21:13 -0400763 result = -EINVAL;
Mimi Zohar4af46622009-02-04 09:07:00 -0500764 break;
765 }
766 }
Eric Paris7b62e162010-04-20 10:21:01 -0400767 if (!result && (entry->action == UNKNOWN))
Mimi Zohar4af46622009-02-04 09:07:00 -0500768 result = -EINVAL;
Mimi Zohara7f2a362012-12-21 08:34:21 -0500769 else if (entry->func == MODULE_CHECK)
Mimi Zohar6ad6afa2015-12-07 14:35:47 -0500770 temp_ima_appraise |= IMA_APPRAISE_MODULES;
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400771 else if (entry->func == FIRMWARE_CHECK)
Mimi Zohar6ad6afa2015-12-07 14:35:47 -0500772 temp_ima_appraise |= IMA_APPRAISE_FIRMWARE;
Eric Parisb0d5de42012-02-14 17:11:07 -0500773 audit_log_format(ab, "res=%d", !result);
Mimi Zohar4af46622009-02-04 09:07:00 -0500774 audit_log_end(ab);
775 return result;
776}
777
778/**
Mimi Zohar07f6a792011-03-09 22:25:48 -0500779 * ima_parse_add_rule - add a rule to ima_policy_rules
Mimi Zohar4af46622009-02-04 09:07:00 -0500780 * @rule - ima measurement policy rule
781 *
Petko Manolov38d859f2015-12-02 17:47:54 +0200782 * Avoid locking by allowing just one writer at a time in ima_write_policy()
Eric Paris6ccd0452010-04-20 10:20:54 -0400783 * Returns the length of the rule parsed, an error code on failure
Mimi Zohar4af46622009-02-04 09:07:00 -0500784 */
Eric Paris6ccd0452010-04-20 10:20:54 -0400785ssize_t ima_parse_add_rule(char *rule)
Mimi Zohar4af46622009-02-04 09:07:00 -0500786{
Mimi Zohar52a13282013-12-11 14:44:04 -0500787 static const char op[] = "update_policy";
Eric Paris6ccd0452010-04-20 10:20:54 -0400788 char *p;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500789 struct ima_rule_entry *entry;
Eric Paris6ccd0452010-04-20 10:20:54 -0400790 ssize_t result, len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500791 int audit_info = 0;
792
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +0300793 p = strsep(&rule, "\n");
794 len = strlen(p) + 1;
Dmitry Kasatkin71787842014-10-03 14:40:20 +0300795 p += strspn(p, " \t");
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +0300796
Dmitry Kasatkin71787842014-10-03 14:40:20 +0300797 if (*p == '#' || *p == '\0')
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +0300798 return len;
799
Mimi Zohar4af46622009-02-04 09:07:00 -0500800 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
801 if (!entry) {
802 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
803 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
804 return -ENOMEM;
805 }
806
807 INIT_LIST_HEAD(&entry->list);
808
Eric Paris6ccd0452010-04-20 10:20:54 -0400809 result = ima_parse_rule(p, entry);
Eric Paris7233e3e2010-04-20 10:21:24 -0400810 if (result) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500811 kfree(entry);
Mimi Zohar523979a2009-02-11 11:12:28 -0500812 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
Richard Guy Briggs7e9001f2014-06-16 15:52:07 -0400813 NULL, op, "invalid-policy", result,
Mimi Zohar523979a2009-02-11 11:12:28 -0500814 audit_info);
Eric Paris7233e3e2010-04-20 10:21:24 -0400815 return result;
Mimi Zohar523979a2009-02-11 11:12:28 -0500816 }
Eric Paris7233e3e2010-04-20 10:21:24 -0400817
Petko Manolov38d859f2015-12-02 17:47:54 +0200818 list_add_tail(&entry->list, &ima_temp_rules);
Eric Paris7233e3e2010-04-20 10:21:24 -0400819
820 return len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500821}
822
Petko Manolov38d859f2015-12-02 17:47:54 +0200823/**
824 * ima_delete_rules() called to cleanup invalid in-flight policy.
825 * We don't need locking as we operate on the temp list, which is
826 * different from the active one. There is also only one user of
827 * ima_delete_rules() at a time.
828 */
James Morris64c61d82009-02-05 09:28:26 +1100829void ima_delete_rules(void)
Mimi Zohar4af46622009-02-04 09:07:00 -0500830{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500831 struct ima_rule_entry *entry, *tmp;
Mimi Zohar7163a992013-01-03 14:19:09 -0500832 int i;
Mimi Zohar4af46622009-02-04 09:07:00 -0500833
Mimi Zohar6ad6afa2015-12-07 14:35:47 -0500834 temp_ima_appraise = 0;
Petko Manolov38d859f2015-12-02 17:47:54 +0200835 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
Mimi Zohar7163a992013-01-03 14:19:09 -0500836 for (i = 0; i < MAX_LSM_RULES; i++)
837 kfree(entry->lsm[i].args_p);
838
Mimi Zohar4af46622009-02-04 09:07:00 -0500839 list_del(&entry->list);
840 kfree(entry);
841 }
Mimi Zohar4af46622009-02-04 09:07:00 -0500842}
Petko Manolov80eae202015-12-02 17:47:56 +0200843
844#ifdef CONFIG_IMA_READ_POLICY
845enum {
846 mask_exec = 0, mask_write, mask_read, mask_append
847};
848
849static char *mask_tokens[] = {
850 "MAY_EXEC",
851 "MAY_WRITE",
852 "MAY_READ",
853 "MAY_APPEND"
854};
855
856enum {
857 func_file = 0, func_mmap, func_bprm,
858 func_module, func_firmware, func_post
859};
860
861static char *func_tokens[] = {
862 "FILE_CHECK",
863 "MMAP_CHECK",
864 "BPRM_CHECK",
865 "MODULE_CHECK",
866 "FIRMWARE_CHECK",
867 "POST_SETATTR"
868};
869
870void *ima_policy_start(struct seq_file *m, loff_t *pos)
871{
872 loff_t l = *pos;
873 struct ima_rule_entry *entry;
874
875 rcu_read_lock();
876 list_for_each_entry_rcu(entry, ima_rules, list) {
877 if (!l--) {
878 rcu_read_unlock();
879 return entry;
880 }
881 }
882 rcu_read_unlock();
883 return NULL;
884}
885
886void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
887{
888 struct ima_rule_entry *entry = v;
889
890 rcu_read_lock();
891 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
892 rcu_read_unlock();
893 (*pos)++;
894
895 return (&entry->list == ima_rules) ? NULL : entry;
896}
897
898void ima_policy_stop(struct seq_file *m, void *v)
899{
900}
901
902#define pt(token) policy_tokens[token + Opt_err].pattern
903#define mt(token) mask_tokens[token]
904#define ft(token) func_tokens[token]
905
Mimi Zoharb5269ab2016-01-28 13:10:36 -0500906/*
907 * policy_func_show - display the ima_hooks policy rule
908 */
909static void policy_func_show(struct seq_file *m, enum ima_hooks func)
910{
911 char tbuf[64] = {0,};
912
913 switch (func) {
914 case FILE_CHECK:
915 seq_printf(m, pt(Opt_func), ft(func_file));
916 break;
917 case MMAP_CHECK:
918 seq_printf(m, pt(Opt_func), ft(func_mmap));
919 break;
920 case BPRM_CHECK:
921 seq_printf(m, pt(Opt_func), ft(func_bprm));
922 break;
923 case MODULE_CHECK:
924 seq_printf(m, pt(Opt_func), ft(func_module));
925 break;
926 case FIRMWARE_CHECK:
927 seq_printf(m, pt(Opt_func), ft(func_firmware));
928 break;
929 case POST_SETATTR:
930 seq_printf(m, pt(Opt_func), ft(func_post));
931 break;
932 default:
933 snprintf(tbuf, sizeof(tbuf), "%d", func);
934 seq_printf(m, pt(Opt_func), tbuf);
935 break;
936 }
937 seq_puts(m, " ");
938}
939
Petko Manolov80eae202015-12-02 17:47:56 +0200940int ima_policy_show(struct seq_file *m, void *v)
941{
942 struct ima_rule_entry *entry = v;
943 int i = 0;
944 char tbuf[64] = {0,};
945
946 rcu_read_lock();
947
948 if (entry->action & MEASURE)
949 seq_puts(m, pt(Opt_measure));
950 if (entry->action & DONT_MEASURE)
951 seq_puts(m, pt(Opt_dont_measure));
952 if (entry->action & APPRAISE)
953 seq_puts(m, pt(Opt_appraise));
954 if (entry->action & DONT_APPRAISE)
955 seq_puts(m, pt(Opt_dont_appraise));
956 if (entry->action & AUDIT)
957 seq_puts(m, pt(Opt_audit));
958
959 seq_puts(m, " ");
960
Mimi Zoharb5269ab2016-01-28 13:10:36 -0500961 if (entry->flags & IMA_FUNC)
962 policy_func_show(m, entry->func);
Petko Manolov80eae202015-12-02 17:47:56 +0200963
964 if (entry->flags & IMA_MASK) {
965 if (entry->mask & MAY_EXEC)
966 seq_printf(m, pt(Opt_mask), mt(mask_exec));
967 if (entry->mask & MAY_WRITE)
968 seq_printf(m, pt(Opt_mask), mt(mask_write));
969 if (entry->mask & MAY_READ)
970 seq_printf(m, pt(Opt_mask), mt(mask_read));
971 if (entry->mask & MAY_APPEND)
972 seq_printf(m, pt(Opt_mask), mt(mask_append));
973 seq_puts(m, " ");
974 }
975
976 if (entry->flags & IMA_FSMAGIC) {
977 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
978 seq_printf(m, pt(Opt_fsmagic), tbuf);
979 seq_puts(m, " ");
980 }
981
982 if (entry->flags & IMA_FSUUID) {
983 seq_puts(m, "fsuuid=");
984 for (i = 0; i < ARRAY_SIZE(entry->fsuuid); ++i) {
985 switch (i) {
986 case 4:
987 case 6:
988 case 8:
989 case 10:
990 seq_puts(m, "-");
991 }
992 seq_printf(m, "%x", entry->fsuuid[i]);
993 }
994 seq_puts(m, " ");
995 }
996
997 if (entry->flags & IMA_UID) {
998 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
999 seq_printf(m, pt(Opt_uid), tbuf);
1000 seq_puts(m, " ");
1001 }
1002
1003 if (entry->flags & IMA_EUID) {
1004 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1005 seq_printf(m, pt(Opt_euid), tbuf);
1006 seq_puts(m, " ");
1007 }
1008
1009 if (entry->flags & IMA_FOWNER) {
1010 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
1011 seq_printf(m, pt(Opt_fowner), tbuf);
1012 seq_puts(m, " ");
1013 }
1014
1015 for (i = 0; i < MAX_LSM_RULES; i++) {
1016 if (entry->lsm[i].rule) {
1017 switch (i) {
1018 case LSM_OBJ_USER:
1019 seq_printf(m, pt(Opt_obj_user),
1020 (char *)entry->lsm[i].args_p);
1021 break;
1022 case LSM_OBJ_ROLE:
1023 seq_printf(m, pt(Opt_obj_role),
1024 (char *)entry->lsm[i].args_p);
1025 break;
1026 case LSM_OBJ_TYPE:
1027 seq_printf(m, pt(Opt_obj_type),
1028 (char *)entry->lsm[i].args_p);
1029 break;
1030 case LSM_SUBJ_USER:
1031 seq_printf(m, pt(Opt_subj_user),
1032 (char *)entry->lsm[i].args_p);
1033 break;
1034 case LSM_SUBJ_ROLE:
1035 seq_printf(m, pt(Opt_subj_role),
1036 (char *)entry->lsm[i].args_p);
1037 break;
1038 case LSM_SUBJ_TYPE:
1039 seq_printf(m, pt(Opt_subj_type),
1040 (char *)entry->lsm[i].args_p);
1041 break;
1042 }
1043 }
1044 }
1045 if (entry->flags & IMA_DIGSIG_REQUIRED)
1046 seq_puts(m, "appraise_type=imasig ");
1047 if (entry->flags & IMA_PERMIT_DIRECTIO)
1048 seq_puts(m, "permit_directio ");
1049 rcu_read_unlock();
1050 seq_puts(m, "\n");
1051 return 0;
1052}
1053#endif /* CONFIG_IMA_READ_POLICY */