blob: c1b5d00fe5d4cdd8efdbaaad2d6ca72eb3bc8a4f [file] [log] [blame]
Mimi Zohar3323eec2009-02-04 09:06:58 -05001/*
2 * Copyright (C) 2008 IBM Corporation
3 * Author: Mimi Zohar <zohar@us.ibm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
8 *
9 * ima_policy.c
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020010 * - initialize default measure policy rules
Mimi Zohar3323eec2009-02-04 09:06:58 -050011 *
12 */
13#include <linux/module.h>
14#include <linux/list.h>
Mimi Zoharcf222212016-01-14 17:57:47 -050015#include <linux/fs.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050016#include <linux/security.h>
17#include <linux/magic.h>
Mimi Zohar4af46622009-02-04 09:07:00 -050018#include <linux/parser.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Petko Manolov38d859f2015-12-02 17:47:54 +020020#include <linux/rculist.h>
Dmitry Kasatkin85865c12012-09-03 23:23:13 +030021#include <linux/genhd.h>
Petko Manolov80eae202015-12-02 17:47:56 +020022#include <linux/seq_file.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050023
24#include "ima.h"
25
26/* flags definitions */
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020027#define IMA_FUNC 0x0001
28#define IMA_MASK 0x0002
Mimi Zohar3323eec2009-02-04 09:06:58 -050029#define IMA_FSMAGIC 0x0004
30#define IMA_UID 0x0008
Mimi Zohar07f6a792011-03-09 22:25:48 -050031#define IMA_FOWNER 0x0010
Dmitry Kasatkin85865c12012-09-03 23:23:13 +030032#define IMA_FSUUID 0x0020
Mimi Zohar4351c292014-11-05 07:53:55 -050033#define IMA_INMASK 0x0040
Mimi Zohar139069e2014-11-05 07:48:36 -050034#define IMA_EUID 0x0080
Mimi Zohar3323eec2009-02-04 09:06:58 -050035
Dmitry Kasatkin45e24722012-09-12 20:51:32 +030036#define UNKNOWN 0
37#define MEASURE 0x0001 /* same as IMA_MEASURE */
38#define DONT_MEASURE 0x0002
39#define APPRAISE 0x0004 /* same as IMA_APPRAISE */
40#define DONT_APPRAISE 0x0008
Peter Moodye7c568e2012-06-14 10:04:36 -070041#define AUDIT 0x0040
Mimi Zohar4af46622009-02-04 09:07:00 -050042
Roberto Sassua7560242014-09-12 19:35:54 +020043int ima_policy_flag;
Mimi Zohar6ad6afa2015-12-07 14:35:47 -050044static int temp_ima_appraise;
Roberto Sassua7560242014-09-12 19:35:54 +020045
Mimi Zohar4af46622009-02-04 09:07:00 -050046#define MAX_LSM_RULES 6
47enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
48 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
49};
Mimi Zohar3323eec2009-02-04 09:06:58 -050050
Mimi Zohar24fd03c2015-06-11 20:48:33 -040051enum policy_types { ORIGINAL_TCB = 1, DEFAULT_TCB };
52
Mimi Zohar07f6a792011-03-09 22:25:48 -050053struct ima_rule_entry {
Mimi Zohar3323eec2009-02-04 09:06:58 -050054 struct list_head list;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050055 int action;
Mimi Zohar3323eec2009-02-04 09:06:58 -050056 unsigned int flags;
57 enum ima_hooks func;
58 int mask;
59 unsigned long fsmagic;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +030060 u8 fsuuid[16];
Eric W. Biederman8b94eea2012-05-25 18:24:12 -060061 kuid_t uid;
Linus Torvalds882653222012-10-02 21:38:48 -070062 kuid_t fowner;
Mimi Zohar4af46622009-02-04 09:07:00 -050063 struct {
64 void *rule; /* LSM file metadata specific */
Mimi Zohar7163a992013-01-03 14:19:09 -050065 void *args_p; /* audit value */
Mimi Zohar4af46622009-02-04 09:07:00 -050066 int type; /* audit type */
67 } lsm[MAX_LSM_RULES];
Mimi Zohar3323eec2009-02-04 09:06:58 -050068};
69
Eric Paris5789ba32009-05-21 15:47:06 -040070/*
71 * Without LSM specific knowledge, the default policy can only be
Mimi Zohar07f6a792011-03-09 22:25:48 -050072 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
Mimi Zohar4af46622009-02-04 09:07:00 -050073 */
Eric Paris5789ba32009-05-21 15:47:06 -040074
75/*
76 * The minimum rule set to allow for full TCB coverage. Measures all files
77 * opened or mmap for exec and everything read by root. Dangerous because
78 * normal users can easily run the machine out of memory simply building
79 * and running executables.
80 */
Mimi Zohar24fd03c2015-06-11 20:48:33 -040081static struct ima_rule_entry dont_measure_rules[] = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020082 {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
83 {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
84 {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
85 {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
86 {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
87 {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
88 {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
89 {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
Roberto Sassu6438de92015-04-11 17:13:06 +020090 {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
91 .flags = IMA_FSMAGIC},
Mimi Zohar24fd03c2015-06-11 20:48:33 -040092 {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC}
93};
94
95static struct ima_rule_entry original_measurement_rules[] = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020096 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
Mimi Zohar3323eec2009-02-04 09:06:58 -050097 .flags = IMA_FUNC | IMA_MASK},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020098 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
Mimi Zohar3323eec2009-02-04 09:06:58 -050099 .flags = IMA_FUNC | IMA_MASK},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400100 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
101 .uid = GLOBAL_ROOT_UID, .flags = IMA_FUNC | IMA_MASK | IMA_UID},
102 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
103 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
104};
105
106static struct ima_rule_entry default_measurement_rules[] = {
107 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
108 .flags = IMA_FUNC | IMA_MASK},
109 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
110 .flags = IMA_FUNC | IMA_MASK},
111 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
112 .uid = GLOBAL_ROOT_UID, .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
113 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
114 .uid = GLOBAL_ROOT_UID, .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200115 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400116 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
Mimi Zohar19f8a842016-01-15 10:17:12 -0500117 {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC},
Mimi Zohar3323eec2009-02-04 09:06:58 -0500118};
119
Mimi Zohar07f6a792011-03-09 22:25:48 -0500120static struct ima_rule_entry default_appraise_rules[] = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200121 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
122 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
123 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
124 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
125 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
126 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
127 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
128 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
129 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
Mimi Zoharcd025f72015-04-21 16:54:24 -0400130 {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200131 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200132#ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200133 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .flags = IMA_FOWNER},
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200134#else
135 /* force signature */
136 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID,
137 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
138#endif
Mimi Zohar07f6a792011-03-09 22:25:48 -0500139};
Mimi Zohar3323eec2009-02-04 09:06:58 -0500140
Mimi Zohar07f6a792011-03-09 22:25:48 -0500141static LIST_HEAD(ima_default_rules);
142static LIST_HEAD(ima_policy_rules);
Petko Manolov38d859f2015-12-02 17:47:54 +0200143static LIST_HEAD(ima_temp_rules);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500144static struct list_head *ima_rules;
145
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400146static int ima_policy __initdata;
Petko Manolov38d859f2015-12-02 17:47:54 +0200147
Mimi Zohar07f6a792011-03-09 22:25:48 -0500148static int __init default_measure_policy_setup(char *str)
Eric Paris5789ba32009-05-21 15:47:06 -0400149{
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400150 if (ima_policy)
151 return 1;
152
153 ima_policy = ORIGINAL_TCB;
Eric Paris5789ba32009-05-21 15:47:06 -0400154 return 1;
155}
Mimi Zohar07f6a792011-03-09 22:25:48 -0500156__setup("ima_tcb", default_measure_policy_setup);
157
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400158static int __init policy_setup(char *str)
159{
160 if (ima_policy)
161 return 1;
162
163 if (strcmp(str, "tcb") == 0)
164 ima_policy = DEFAULT_TCB;
165
166 return 1;
167}
168__setup("ima_policy=", policy_setup);
169
Mimi Zohar07f6a792011-03-09 22:25:48 -0500170static bool ima_use_appraise_tcb __initdata;
171static int __init default_appraise_policy_setup(char *str)
172{
173 ima_use_appraise_tcb = 1;
174 return 1;
175}
176__setup("ima_appraise_tcb", default_appraise_policy_setup);
Eric Paris5789ba32009-05-21 15:47:06 -0400177
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200178/*
Petko Manolov38d859f2015-12-02 17:47:54 +0200179 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
180 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
181 * the reloaded LSM policy. We assume the rules still exist; and BUG_ON() if
182 * they don't.
Mimi Zohar7163a992013-01-03 14:19:09 -0500183 */
184static void ima_lsm_update_rules(void)
185{
Petko Manolov38d859f2015-12-02 17:47:54 +0200186 struct ima_rule_entry *entry;
Mimi Zohar7163a992013-01-03 14:19:09 -0500187 int result;
188 int i;
189
Petko Manolov38d859f2015-12-02 17:47:54 +0200190 list_for_each_entry(entry, &ima_policy_rules, list) {
Mimi Zohar7163a992013-01-03 14:19:09 -0500191 for (i = 0; i < MAX_LSM_RULES; i++) {
192 if (!entry->lsm[i].rule)
193 continue;
194 result = security_filter_rule_init(entry->lsm[i].type,
195 Audit_equal,
196 entry->lsm[i].args_p,
197 &entry->lsm[i].rule);
198 BUG_ON(!entry->lsm[i].rule);
199 }
200 }
Mimi Zohar7163a992013-01-03 14:19:09 -0500201}
202
Mimi Zohar3323eec2009-02-04 09:06:58 -0500203/**
204 * ima_match_rules - determine whether an inode matches the measure rule.
205 * @rule: a pointer to a rule
206 * @inode: a pointer to an inode
207 * @func: LIM hook identifier
208 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
209 *
210 * Returns true on rule match, false on failure.
211 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500212static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,
213 enum ima_hooks func, int mask)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500214{
215 struct task_struct *tsk = current;
Mimi Zohar3db59dd2012-01-17 22:11:28 -0500216 const struct cred *cred = current_cred();
Mimi Zohar4af46622009-02-04 09:07:00 -0500217 int i;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500218
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200219 if ((rule->flags & IMA_FUNC) &&
220 (rule->func != func && func != POST_SETATTR))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500221 return false;
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200222 if ((rule->flags & IMA_MASK) &&
223 (rule->mask != mask && func != POST_SETATTR))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500224 return false;
Mimi Zohar4351c292014-11-05 07:53:55 -0500225 if ((rule->flags & IMA_INMASK) &&
226 (!(rule->mask & mask) && func != POST_SETATTR))
227 return false;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500228 if ((rule->flags & IMA_FSMAGIC)
229 && rule->fsmagic != inode->i_sb->s_magic)
230 return false;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300231 if ((rule->flags & IMA_FSUUID) &&
Mimi Zohar446d64e2013-02-24 23:42:37 -0500232 memcmp(rule->fsuuid, inode->i_sb->s_uuid, sizeof(rule->fsuuid)))
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300233 return false;
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600234 if ((rule->flags & IMA_UID) && !uid_eq(rule->uid, cred->uid))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500235 return false;
Mimi Zohar139069e2014-11-05 07:48:36 -0500236 if (rule->flags & IMA_EUID) {
237 if (has_capability_noaudit(current, CAP_SETUID)) {
238 if (!uid_eq(rule->uid, cred->euid)
239 && !uid_eq(rule->uid, cred->suid)
240 && !uid_eq(rule->uid, cred->uid))
241 return false;
242 } else if (!uid_eq(rule->uid, cred->euid))
243 return false;
244 }
245
Linus Torvalds882653222012-10-02 21:38:48 -0700246 if ((rule->flags & IMA_FOWNER) && !uid_eq(rule->fowner, inode->i_uid))
Mimi Zohar07f6a792011-03-09 22:25:48 -0500247 return false;
Mimi Zohar4af46622009-02-04 09:07:00 -0500248 for (i = 0; i < MAX_LSM_RULES; i++) {
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400249 int rc = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500250 u32 osid, sid;
Mimi Zohar7163a992013-01-03 14:19:09 -0500251 int retried = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500252
253 if (!rule->lsm[i].rule)
254 continue;
Mimi Zohar7163a992013-01-03 14:19:09 -0500255retry:
Mimi Zohar4af46622009-02-04 09:07:00 -0500256 switch (i) {
257 case LSM_OBJ_USER:
258 case LSM_OBJ_ROLE:
259 case LSM_OBJ_TYPE:
260 security_inode_getsecid(inode, &osid);
261 rc = security_filter_rule_match(osid,
262 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400263 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500264 rule->lsm[i].rule,
265 NULL);
266 break;
267 case LSM_SUBJ_USER:
268 case LSM_SUBJ_ROLE:
269 case LSM_SUBJ_TYPE:
270 security_task_getsecid(tsk, &sid);
271 rc = security_filter_rule_match(sid,
272 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400273 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500274 rule->lsm[i].rule,
275 NULL);
276 default:
277 break;
278 }
Mimi Zohar7163a992013-01-03 14:19:09 -0500279 if ((rc < 0) && (!retried)) {
280 retried = 1;
281 ima_lsm_update_rules();
282 goto retry;
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200283 }
Mimi Zohar4af46622009-02-04 09:07:00 -0500284 if (!rc)
285 return false;
286 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500287 return true;
288}
289
Mimi Zohard79d72e2012-12-03 17:08:11 -0500290/*
291 * In addition to knowing that we need to appraise the file in general,
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500292 * we need to differentiate between calling hooks, for hook specific rules.
Mimi Zohard79d72e2012-12-03 17:08:11 -0500293 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500294static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
Mimi Zohard79d72e2012-12-03 17:08:11 -0500295{
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500296 if (!(rule->flags & IMA_FUNC))
297 return IMA_FILE_APPRAISE;
298
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200299 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500300 case MMAP_CHECK:
301 return IMA_MMAP_APPRAISE;
302 case BPRM_CHECK:
303 return IMA_BPRM_APPRAISE;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500304 case FILE_CHECK:
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500305 case POST_SETATTR:
Mimi Zohard79d72e2012-12-03 17:08:11 -0500306 return IMA_FILE_APPRAISE;
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500307 case MODULE_CHECK ... MAX_CHECK - 1:
308 default:
309 return IMA_READ_APPRAISE;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500310 }
311}
312
Mimi Zohar3323eec2009-02-04 09:06:58 -0500313/**
314 * ima_match_policy - decision based on LSM and other conditions
315 * @inode: pointer to an inode for which the policy decision is being made
316 * @func: IMA hook identifier
317 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
318 *
319 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
320 * conditions.
321 *
Petko Manolov38d859f2015-12-02 17:47:54 +0200322 * Since the IMA policy may be updated multiple times we need to lock the
323 * list when walking it. Reads are many orders of magnitude more numerous
324 * than writes so ima_match_policy() is classical RCU candidate.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500325 */
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500326int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
327 int flags)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500328{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500329 struct ima_rule_entry *entry;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500330 int action = 0, actmask = flags | (flags << 1);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500331
Petko Manolov38d859f2015-12-02 17:47:54 +0200332 rcu_read_lock();
333 list_for_each_entry_rcu(entry, ima_rules, list) {
Mimi Zohar3323eec2009-02-04 09:06:58 -0500334
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500335 if (!(entry->action & actmask))
336 continue;
337
338 if (!ima_match_rules(entry, inode, func, mask))
339 continue;
340
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300341 action |= entry->flags & IMA_ACTION_FLAGS;
342
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300343 action |= entry->action & IMA_DO_MASK;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500344 if (entry->action & IMA_APPRAISE)
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500345 action |= get_subaction(entry, func);
Mimi Zohard79d72e2012-12-03 17:08:11 -0500346
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300347 if (entry->action & IMA_DO_MASK)
348 actmask &= ~(entry->action | entry->action << 1);
349 else
350 actmask &= ~(entry->action | entry->action >> 1);
351
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500352 if (!actmask)
353 break;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500354 }
Petko Manolov38d859f2015-12-02 17:47:54 +0200355 rcu_read_unlock();
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500356
357 return action;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500358}
359
Roberto Sassua7560242014-09-12 19:35:54 +0200360/*
361 * Initialize the ima_policy_flag variable based on the currently
362 * loaded policy. Based on this flag, the decision to short circuit
363 * out of a function or not call the function in the first place
364 * can be made earlier.
365 */
366void ima_update_policy_flag(void)
367{
368 struct ima_rule_entry *entry;
369
Roberto Sassua7560242014-09-12 19:35:54 +0200370 list_for_each_entry(entry, ima_rules, list) {
371 if (entry->action & IMA_DO_MASK)
372 ima_policy_flag |= entry->action;
373 }
374
Mimi Zohar6ad6afa2015-12-07 14:35:47 -0500375 ima_appraise |= temp_ima_appraise;
Roberto Sassua7560242014-09-12 19:35:54 +0200376 if (!ima_appraise)
377 ima_policy_flag &= ~IMA_APPRAISE;
378}
379
Mimi Zohar3323eec2009-02-04 09:06:58 -0500380/**
381 * ima_init_policy - initialize the default measure rules.
382 *
Mimi Zohar07f6a792011-03-09 22:25:48 -0500383 * ima_rules points to either the ima_default_rules or the
384 * the new ima_policy_rules.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500385 */
Eric Paris932995f2009-05-21 15:43:32 -0400386void __init ima_init_policy(void)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500387{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500388 int i, measure_entries, appraise_entries;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500389
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400390 /* if !ima_policy set entries = 0 so we load NO default rules */
391 measure_entries = ima_policy ? ARRAY_SIZE(dont_measure_rules) : 0;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500392 appraise_entries = ima_use_appraise_tcb ?
393 ARRAY_SIZE(default_appraise_rules) : 0;
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200394
Dan Carpenter55778572015-04-07 12:22:11 +0300395 for (i = 0; i < measure_entries; i++)
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400396 list_add_tail(&dont_measure_rules[i].list, &ima_default_rules);
397
398 switch (ima_policy) {
399 case ORIGINAL_TCB:
400 for (i = 0; i < ARRAY_SIZE(original_measurement_rules); i++)
401 list_add_tail(&original_measurement_rules[i].list,
402 &ima_default_rules);
403 break;
404 case DEFAULT_TCB:
405 for (i = 0; i < ARRAY_SIZE(default_measurement_rules); i++)
406 list_add_tail(&default_measurement_rules[i].list,
407 &ima_default_rules);
408 default:
409 break;
410 }
Eric Paris5789ba32009-05-21 15:47:06 -0400411
Dan Carpenter55778572015-04-07 12:22:11 +0300412 for (i = 0; i < appraise_entries; i++) {
413 list_add_tail(&default_appraise_rules[i].list,
414 &ima_default_rules);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500415 }
416
417 ima_rules = &ima_default_rules;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500418}
Mimi Zohar4af46622009-02-04 09:07:00 -0500419
Sasha Levin01127212015-12-22 08:51:23 -0500420/* Make sure we have a valid policy, at least containing some rules. */
Colin Ian Kingc75d8e92016-01-20 11:13:46 +0000421int ima_check_policy(void)
Sasha Levin01127212015-12-22 08:51:23 -0500422{
423 if (list_empty(&ima_temp_rules))
424 return -EINVAL;
425 return 0;
426}
427
Mimi Zohar4af46622009-02-04 09:07:00 -0500428/**
429 * ima_update_policy - update default_rules with new measure rules
430 *
431 * Called on file .release to update the default rules with a complete new
Petko Manolov38d859f2015-12-02 17:47:54 +0200432 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
433 * they make a queue. The policy may be updated multiple times and this is the
434 * RCU updater.
435 *
436 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
437 * we switch from the default policy to user defined.
Mimi Zohar4af46622009-02-04 09:07:00 -0500438 */
439void ima_update_policy(void)
440{
Petko Manolov38d859f2015-12-02 17:47:54 +0200441 struct list_head *first, *last, *policy;
442
443 /* append current policy with the new rules */
444 first = (&ima_temp_rules)->next;
445 last = (&ima_temp_rules)->prev;
446 policy = &ima_policy_rules;
447
448 synchronize_rcu();
449
450 last->next = policy;
451 rcu_assign_pointer(list_next_rcu(policy->prev), first);
452 first->prev = policy->prev;
453 policy->prev = last;
454
455 /* prepare for the next policy rules addition */
456 INIT_LIST_HEAD(&ima_temp_rules);
457
458 if (ima_rules != policy) {
459 ima_policy_flag = 0;
460 ima_rules = policy;
461 }
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300462 ima_update_policy_flag();
Mimi Zohar4af46622009-02-04 09:07:00 -0500463}
464
465enum {
466 Opt_err = -1,
467 Opt_measure = 1, Opt_dont_measure,
Mimi Zohar07f6a792011-03-09 22:25:48 -0500468 Opt_appraise, Opt_dont_appraise,
Peter Moodye7c568e2012-06-14 10:04:36 -0700469 Opt_audit,
Mimi Zohar4af46622009-02-04 09:07:00 -0500470 Opt_obj_user, Opt_obj_role, Opt_obj_type,
471 Opt_subj_user, Opt_subj_role, Opt_subj_type,
Mimi Zohar139069e2014-11-05 07:48:36 -0500472 Opt_func, Opt_mask, Opt_fsmagic,
Petko Manolov80eae202015-12-02 17:47:56 +0200473 Opt_fsuuid, Opt_uid, Opt_euid, Opt_fowner,
474 Opt_appraise_type, Opt_permit_directio
Mimi Zohar4af46622009-02-04 09:07:00 -0500475};
476
477static match_table_t policy_tokens = {
478 {Opt_measure, "measure"},
479 {Opt_dont_measure, "dont_measure"},
Mimi Zohar07f6a792011-03-09 22:25:48 -0500480 {Opt_appraise, "appraise"},
481 {Opt_dont_appraise, "dont_appraise"},
Peter Moodye7c568e2012-06-14 10:04:36 -0700482 {Opt_audit, "audit"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500483 {Opt_obj_user, "obj_user=%s"},
484 {Opt_obj_role, "obj_role=%s"},
485 {Opt_obj_type, "obj_type=%s"},
486 {Opt_subj_user, "subj_user=%s"},
487 {Opt_subj_role, "subj_role=%s"},
488 {Opt_subj_type, "subj_type=%s"},
489 {Opt_func, "func=%s"},
490 {Opt_mask, "mask=%s"},
491 {Opt_fsmagic, "fsmagic=%s"},
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300492 {Opt_fsuuid, "fsuuid=%s"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500493 {Opt_uid, "uid=%s"},
Mimi Zohar139069e2014-11-05 07:48:36 -0500494 {Opt_euid, "euid=%s"},
Mimi Zohar07f6a792011-03-09 22:25:48 -0500495 {Opt_fowner, "fowner=%s"},
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300496 {Opt_appraise_type, "appraise_type=%s"},
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400497 {Opt_permit_directio, "permit_directio"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500498 {Opt_err, NULL}
499};
500
Mimi Zohar07f6a792011-03-09 22:25:48 -0500501static int ima_lsm_rule_init(struct ima_rule_entry *entry,
Mimi Zohar7163a992013-01-03 14:19:09 -0500502 substring_t *args, int lsm_rule, int audit_type)
Mimi Zohar4af46622009-02-04 09:07:00 -0500503{
504 int result;
505
Eric Paris7b62e162010-04-20 10:21:01 -0400506 if (entry->lsm[lsm_rule].rule)
507 return -EINVAL;
508
Mimi Zohar7163a992013-01-03 14:19:09 -0500509 entry->lsm[lsm_rule].args_p = match_strdup(args);
510 if (!entry->lsm[lsm_rule].args_p)
511 return -ENOMEM;
512
Mimi Zohar4af46622009-02-04 09:07:00 -0500513 entry->lsm[lsm_rule].type = audit_type;
514 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
Mimi Zohar7163a992013-01-03 14:19:09 -0500515 Audit_equal,
516 entry->lsm[lsm_rule].args_p,
Mimi Zohar4af46622009-02-04 09:07:00 -0500517 &entry->lsm[lsm_rule].rule);
Mimi Zohar7163a992013-01-03 14:19:09 -0500518 if (!entry->lsm[lsm_rule].rule) {
519 kfree(entry->lsm[lsm_rule].args_p);
Mimi Zohar867c2022011-01-03 14:59:10 -0800520 return -EINVAL;
Mimi Zohar7163a992013-01-03 14:19:09 -0500521 }
522
Mimi Zohar4af46622009-02-04 09:07:00 -0500523 return result;
524}
525
Eric Paris2f1506c2010-04-20 10:21:30 -0400526static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
527{
528 audit_log_format(ab, "%s=", key);
529 audit_log_untrustedstring(ab, value);
530 audit_log_format(ab, " ");
531}
532
Mimi Zohar07f6a792011-03-09 22:25:48 -0500533static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
Mimi Zohar4af46622009-02-04 09:07:00 -0500534{
535 struct audit_buffer *ab;
Mimi Zohar4351c292014-11-05 07:53:55 -0500536 char *from;
Mimi Zohar4af46622009-02-04 09:07:00 -0500537 char *p;
538 int result = 0;
539
Mimi Zohar523979a2009-02-11 11:12:28 -0500540 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
Mimi Zohar4af46622009-02-04 09:07:00 -0500541
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600542 entry->uid = INVALID_UID;
Linus Torvalds882653222012-10-02 21:38:48 -0700543 entry->fowner = INVALID_UID;
Eric Parisb9035b12010-04-20 10:21:07 -0400544 entry->action = UNKNOWN;
Eric Paris28ef4002010-04-20 10:21:18 -0400545 while ((p = strsep(&rule, " \t")) != NULL) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500546 substring_t args[MAX_OPT_ARGS];
547 int token;
548 unsigned long lnum;
549
550 if (result < 0)
551 break;
Eric Paris28ef4002010-04-20 10:21:18 -0400552 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
553 continue;
Mimi Zohar4af46622009-02-04 09:07:00 -0500554 token = match_token(p, policy_tokens, args);
555 switch (token) {
556 case Opt_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400557 ima_log_string(ab, "action", "measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400558
559 if (entry->action != UNKNOWN)
560 result = -EINVAL;
561
Mimi Zohar4af46622009-02-04 09:07:00 -0500562 entry->action = MEASURE;
563 break;
564 case Opt_dont_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400565 ima_log_string(ab, "action", "dont_measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400566
567 if (entry->action != UNKNOWN)
568 result = -EINVAL;
569
Mimi Zohar4af46622009-02-04 09:07:00 -0500570 entry->action = DONT_MEASURE;
571 break;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500572 case Opt_appraise:
573 ima_log_string(ab, "action", "appraise");
574
575 if (entry->action != UNKNOWN)
576 result = -EINVAL;
577
578 entry->action = APPRAISE;
579 break;
580 case Opt_dont_appraise:
581 ima_log_string(ab, "action", "dont_appraise");
582
583 if (entry->action != UNKNOWN)
584 result = -EINVAL;
585
586 entry->action = DONT_APPRAISE;
587 break;
Peter Moodye7c568e2012-06-14 10:04:36 -0700588 case Opt_audit:
589 ima_log_string(ab, "action", "audit");
590
591 if (entry->action != UNKNOWN)
592 result = -EINVAL;
593
594 entry->action = AUDIT;
595 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500596 case Opt_func:
Eric Paris2f1506c2010-04-20 10:21:30 -0400597 ima_log_string(ab, "func", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400598
599 if (entry->func)
Mimi Zohar07f6a792011-03-09 22:25:48 -0500600 result = -EINVAL;
Eric Paris7b62e162010-04-20 10:21:01 -0400601
Mimi Zohar1e93d002010-01-26 17:02:41 -0500602 if (strcmp(args[0].from, "FILE_CHECK") == 0)
603 entry->func = FILE_CHECK;
604 /* PATH_CHECK is for backwards compat */
605 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
606 entry->func = FILE_CHECK;
Mimi Zoharfdf90722012-10-16 12:40:08 +1030607 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
608 entry->func = MODULE_CHECK;
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400609 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
610 entry->func = FIRMWARE_CHECK;
Mimi Zohar16cac492012-12-13 11:15:04 -0500611 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
612 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
613 entry->func = MMAP_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500614 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
615 entry->func = BPRM_CHECK;
Mimi Zohard9ddf072016-01-14 20:59:14 -0500616 else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
617 0)
618 entry->func = KEXEC_KERNEL_CHECK;
619 else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
620 == 0)
621 entry->func = KEXEC_INITRAMFS_CHECK;
Mimi Zohar19f8a842016-01-15 10:17:12 -0500622 else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
623 entry->func = POLICY_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500624 else
625 result = -EINVAL;
626 if (!result)
627 entry->flags |= IMA_FUNC;
628 break;
629 case Opt_mask:
Eric Paris2f1506c2010-04-20 10:21:30 -0400630 ima_log_string(ab, "mask", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400631
632 if (entry->mask)
633 result = -EINVAL;
634
Mimi Zohar4351c292014-11-05 07:53:55 -0500635 from = args[0].from;
636 if (*from == '^')
637 from++;
638
639 if ((strcmp(from, "MAY_EXEC")) == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500640 entry->mask = MAY_EXEC;
Mimi Zohar4351c292014-11-05 07:53:55 -0500641 else if (strcmp(from, "MAY_WRITE") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500642 entry->mask = MAY_WRITE;
Mimi Zohar4351c292014-11-05 07:53:55 -0500643 else if (strcmp(from, "MAY_READ") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500644 entry->mask = MAY_READ;
Mimi Zohar4351c292014-11-05 07:53:55 -0500645 else if (strcmp(from, "MAY_APPEND") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500646 entry->mask = MAY_APPEND;
647 else
648 result = -EINVAL;
649 if (!result)
Mimi Zohar4351c292014-11-05 07:53:55 -0500650 entry->flags |= (*args[0].from == '^')
651 ? IMA_INMASK : IMA_MASK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500652 break;
653 case Opt_fsmagic:
Eric Paris2f1506c2010-04-20 10:21:30 -0400654 ima_log_string(ab, "fsmagic", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400655
656 if (entry->fsmagic) {
657 result = -EINVAL;
658 break;
659 }
660
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200661 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
Mimi Zohar4af46622009-02-04 09:07:00 -0500662 if (!result)
663 entry->flags |= IMA_FSMAGIC;
664 break;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300665 case Opt_fsuuid:
666 ima_log_string(ab, "fsuuid", args[0].from);
667
668 if (memchr_inv(entry->fsuuid, 0x00,
Mimi Zohar446d64e2013-02-24 23:42:37 -0500669 sizeof(entry->fsuuid))) {
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300670 result = -EINVAL;
671 break;
672 }
673
Mimi Zohar446d64e2013-02-24 23:42:37 -0500674 result = blk_part_pack_uuid(args[0].from,
675 entry->fsuuid);
676 if (!result)
677 entry->flags |= IMA_FSUUID;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300678 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500679 case Opt_uid:
Eric Paris2f1506c2010-04-20 10:21:30 -0400680 ima_log_string(ab, "uid", args[0].from);
Mimi Zohar139069e2014-11-05 07:48:36 -0500681 case Opt_euid:
682 if (token == Opt_euid)
683 ima_log_string(ab, "euid", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400684
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600685 if (uid_valid(entry->uid)) {
Eric Paris7b62e162010-04-20 10:21:01 -0400686 result = -EINVAL;
687 break;
688 }
689
Jingoo Han29707b22014-02-05 15:13:14 +0900690 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar4af46622009-02-04 09:07:00 -0500691 if (!result) {
Mimi Zohar139069e2014-11-05 07:48:36 -0500692 entry->uid = make_kuid(current_user_ns(),
693 (uid_t) lnum);
694 if (!uid_valid(entry->uid) ||
695 (uid_t)lnum != lnum)
Mimi Zohar4af46622009-02-04 09:07:00 -0500696 result = -EINVAL;
697 else
Mimi Zohar139069e2014-11-05 07:48:36 -0500698 entry->flags |= (token == Opt_uid)
699 ? IMA_UID : IMA_EUID;
Mimi Zohar4af46622009-02-04 09:07:00 -0500700 }
701 break;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500702 case Opt_fowner:
703 ima_log_string(ab, "fowner", args[0].from);
704
Linus Torvalds882653222012-10-02 21:38:48 -0700705 if (uid_valid(entry->fowner)) {
Mimi Zohar07f6a792011-03-09 22:25:48 -0500706 result = -EINVAL;
707 break;
708 }
709
Jingoo Han29707b22014-02-05 15:13:14 +0900710 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500711 if (!result) {
Linus Torvalds882653222012-10-02 21:38:48 -0700712 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
713 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
Mimi Zohar07f6a792011-03-09 22:25:48 -0500714 result = -EINVAL;
715 else
716 entry->flags |= IMA_FOWNER;
717 }
718 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500719 case Opt_obj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400720 ima_log_string(ab, "obj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500721 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500722 LSM_OBJ_USER,
723 AUDIT_OBJ_USER);
724 break;
725 case Opt_obj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400726 ima_log_string(ab, "obj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500727 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500728 LSM_OBJ_ROLE,
729 AUDIT_OBJ_ROLE);
730 break;
731 case Opt_obj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400732 ima_log_string(ab, "obj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500733 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500734 LSM_OBJ_TYPE,
735 AUDIT_OBJ_TYPE);
736 break;
737 case Opt_subj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400738 ima_log_string(ab, "subj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500739 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500740 LSM_SUBJ_USER,
741 AUDIT_SUBJ_USER);
742 break;
743 case Opt_subj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400744 ima_log_string(ab, "subj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500745 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500746 LSM_SUBJ_ROLE,
747 AUDIT_SUBJ_ROLE);
748 break;
749 case Opt_subj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400750 ima_log_string(ab, "subj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500751 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500752 LSM_SUBJ_TYPE,
753 AUDIT_SUBJ_TYPE);
754 break;
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300755 case Opt_appraise_type:
756 if (entry->action != APPRAISE) {
757 result = -EINVAL;
758 break;
759 }
760
761 ima_log_string(ab, "appraise_type", args[0].from);
762 if ((strcmp(args[0].from, "imasig")) == 0)
763 entry->flags |= IMA_DIGSIG_REQUIRED;
764 else
765 result = -EINVAL;
766 break;
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400767 case Opt_permit_directio:
768 entry->flags |= IMA_PERMIT_DIRECTIO;
769 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500770 case Opt_err:
Eric Paris2f1506c2010-04-20 10:21:30 -0400771 ima_log_string(ab, "UNKNOWN", p);
Eric Parise9d393b2010-04-20 10:21:13 -0400772 result = -EINVAL;
Mimi Zohar4af46622009-02-04 09:07:00 -0500773 break;
774 }
775 }
Eric Paris7b62e162010-04-20 10:21:01 -0400776 if (!result && (entry->action == UNKNOWN))
Mimi Zohar4af46622009-02-04 09:07:00 -0500777 result = -EINVAL;
Mimi Zohara7f2a362012-12-21 08:34:21 -0500778 else if (entry->func == MODULE_CHECK)
Mimi Zohar6ad6afa2015-12-07 14:35:47 -0500779 temp_ima_appraise |= IMA_APPRAISE_MODULES;
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400780 else if (entry->func == FIRMWARE_CHECK)
Mimi Zohar6ad6afa2015-12-07 14:35:47 -0500781 temp_ima_appraise |= IMA_APPRAISE_FIRMWARE;
Mimi Zohar19f8a842016-01-15 10:17:12 -0500782 else if (entry->func == POLICY_CHECK)
783 temp_ima_appraise |= IMA_APPRAISE_POLICY;
Eric Parisb0d5de42012-02-14 17:11:07 -0500784 audit_log_format(ab, "res=%d", !result);
Mimi Zohar4af46622009-02-04 09:07:00 -0500785 audit_log_end(ab);
786 return result;
787}
788
789/**
Mimi Zohar07f6a792011-03-09 22:25:48 -0500790 * ima_parse_add_rule - add a rule to ima_policy_rules
Mimi Zohar4af46622009-02-04 09:07:00 -0500791 * @rule - ima measurement policy rule
792 *
Petko Manolov38d859f2015-12-02 17:47:54 +0200793 * Avoid locking by allowing just one writer at a time in ima_write_policy()
Eric Paris6ccd0452010-04-20 10:20:54 -0400794 * Returns the length of the rule parsed, an error code on failure
Mimi Zohar4af46622009-02-04 09:07:00 -0500795 */
Eric Paris6ccd0452010-04-20 10:20:54 -0400796ssize_t ima_parse_add_rule(char *rule)
Mimi Zohar4af46622009-02-04 09:07:00 -0500797{
Mimi Zohar52a13282013-12-11 14:44:04 -0500798 static const char op[] = "update_policy";
Eric Paris6ccd0452010-04-20 10:20:54 -0400799 char *p;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500800 struct ima_rule_entry *entry;
Eric Paris6ccd0452010-04-20 10:20:54 -0400801 ssize_t result, len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500802 int audit_info = 0;
803
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +0300804 p = strsep(&rule, "\n");
805 len = strlen(p) + 1;
Dmitry Kasatkin71787842014-10-03 14:40:20 +0300806 p += strspn(p, " \t");
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +0300807
Dmitry Kasatkin71787842014-10-03 14:40:20 +0300808 if (*p == '#' || *p == '\0')
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +0300809 return len;
810
Mimi Zohar4af46622009-02-04 09:07:00 -0500811 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
812 if (!entry) {
813 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
814 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
815 return -ENOMEM;
816 }
817
818 INIT_LIST_HEAD(&entry->list);
819
Eric Paris6ccd0452010-04-20 10:20:54 -0400820 result = ima_parse_rule(p, entry);
Eric Paris7233e3e2010-04-20 10:21:24 -0400821 if (result) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500822 kfree(entry);
Mimi Zohar523979a2009-02-11 11:12:28 -0500823 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
Richard Guy Briggs7e9001f2014-06-16 15:52:07 -0400824 NULL, op, "invalid-policy", result,
Mimi Zohar523979a2009-02-11 11:12:28 -0500825 audit_info);
Eric Paris7233e3e2010-04-20 10:21:24 -0400826 return result;
Mimi Zohar523979a2009-02-11 11:12:28 -0500827 }
Eric Paris7233e3e2010-04-20 10:21:24 -0400828
Petko Manolov38d859f2015-12-02 17:47:54 +0200829 list_add_tail(&entry->list, &ima_temp_rules);
Eric Paris7233e3e2010-04-20 10:21:24 -0400830
831 return len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500832}
833
Petko Manolov38d859f2015-12-02 17:47:54 +0200834/**
835 * ima_delete_rules() called to cleanup invalid in-flight policy.
836 * We don't need locking as we operate on the temp list, which is
837 * different from the active one. There is also only one user of
838 * ima_delete_rules() at a time.
839 */
James Morris64c61d82009-02-05 09:28:26 +1100840void ima_delete_rules(void)
Mimi Zohar4af46622009-02-04 09:07:00 -0500841{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500842 struct ima_rule_entry *entry, *tmp;
Mimi Zohar7163a992013-01-03 14:19:09 -0500843 int i;
Mimi Zohar4af46622009-02-04 09:07:00 -0500844
Mimi Zohar6ad6afa2015-12-07 14:35:47 -0500845 temp_ima_appraise = 0;
Petko Manolov38d859f2015-12-02 17:47:54 +0200846 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
Mimi Zohar7163a992013-01-03 14:19:09 -0500847 for (i = 0; i < MAX_LSM_RULES; i++)
848 kfree(entry->lsm[i].args_p);
849
Mimi Zohar4af46622009-02-04 09:07:00 -0500850 list_del(&entry->list);
851 kfree(entry);
852 }
Mimi Zohar4af46622009-02-04 09:07:00 -0500853}
Petko Manolov80eae202015-12-02 17:47:56 +0200854
855#ifdef CONFIG_IMA_READ_POLICY
856enum {
857 mask_exec = 0, mask_write, mask_read, mask_append
858};
859
860static char *mask_tokens[] = {
861 "MAY_EXEC",
862 "MAY_WRITE",
863 "MAY_READ",
864 "MAY_APPEND"
865};
866
867enum {
868 func_file = 0, func_mmap, func_bprm,
Mimi Zohard9ddf072016-01-14 20:59:14 -0500869 func_module, func_firmware, func_post,
Mimi Zohar19f8a842016-01-15 10:17:12 -0500870 func_kexec_kernel, func_kexec_initramfs,
871 func_policy
Petko Manolov80eae202015-12-02 17:47:56 +0200872};
873
874static char *func_tokens[] = {
875 "FILE_CHECK",
876 "MMAP_CHECK",
877 "BPRM_CHECK",
878 "MODULE_CHECK",
879 "FIRMWARE_CHECK",
Mimi Zohard9ddf072016-01-14 20:59:14 -0500880 "KEXEC_KERNEL_CHECK",
881 "KEXEC_INITRAMFS_CHECK",
Mimi Zohar19f8a842016-01-15 10:17:12 -0500882 "POLICY_CHECK",
Petko Manolov80eae202015-12-02 17:47:56 +0200883 "POST_SETATTR"
884};
885
886void *ima_policy_start(struct seq_file *m, loff_t *pos)
887{
888 loff_t l = *pos;
889 struct ima_rule_entry *entry;
890
891 rcu_read_lock();
892 list_for_each_entry_rcu(entry, ima_rules, list) {
893 if (!l--) {
894 rcu_read_unlock();
895 return entry;
896 }
897 }
898 rcu_read_unlock();
899 return NULL;
900}
901
902void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
903{
904 struct ima_rule_entry *entry = v;
905
906 rcu_read_lock();
907 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
908 rcu_read_unlock();
909 (*pos)++;
910
911 return (&entry->list == ima_rules) ? NULL : entry;
912}
913
914void ima_policy_stop(struct seq_file *m, void *v)
915{
916}
917
918#define pt(token) policy_tokens[token + Opt_err].pattern
919#define mt(token) mask_tokens[token]
920#define ft(token) func_tokens[token]
921
Mimi Zoharb5269ab2016-01-28 13:10:36 -0500922/*
923 * policy_func_show - display the ima_hooks policy rule
924 */
925static void policy_func_show(struct seq_file *m, enum ima_hooks func)
926{
927 char tbuf[64] = {0,};
928
929 switch (func) {
930 case FILE_CHECK:
931 seq_printf(m, pt(Opt_func), ft(func_file));
932 break;
933 case MMAP_CHECK:
934 seq_printf(m, pt(Opt_func), ft(func_mmap));
935 break;
936 case BPRM_CHECK:
937 seq_printf(m, pt(Opt_func), ft(func_bprm));
938 break;
939 case MODULE_CHECK:
940 seq_printf(m, pt(Opt_func), ft(func_module));
941 break;
942 case FIRMWARE_CHECK:
943 seq_printf(m, pt(Opt_func), ft(func_firmware));
944 break;
945 case POST_SETATTR:
946 seq_printf(m, pt(Opt_func), ft(func_post));
947 break;
Mimi Zohard9ddf072016-01-14 20:59:14 -0500948 case KEXEC_KERNEL_CHECK:
949 seq_printf(m, pt(Opt_func), ft(func_kexec_kernel));
950 break;
951 case KEXEC_INITRAMFS_CHECK:
952 seq_printf(m, pt(Opt_func), ft(func_kexec_initramfs));
953 break;
Mimi Zohar19f8a842016-01-15 10:17:12 -0500954 case POLICY_CHECK:
955 seq_printf(m, pt(Opt_func), ft(func_policy));
956 break;
Mimi Zoharb5269ab2016-01-28 13:10:36 -0500957 default:
958 snprintf(tbuf, sizeof(tbuf), "%d", func);
959 seq_printf(m, pt(Opt_func), tbuf);
960 break;
961 }
962 seq_puts(m, " ");
963}
964
Petko Manolov80eae202015-12-02 17:47:56 +0200965int ima_policy_show(struct seq_file *m, void *v)
966{
967 struct ima_rule_entry *entry = v;
968 int i = 0;
969 char tbuf[64] = {0,};
970
971 rcu_read_lock();
972
973 if (entry->action & MEASURE)
974 seq_puts(m, pt(Opt_measure));
975 if (entry->action & DONT_MEASURE)
976 seq_puts(m, pt(Opt_dont_measure));
977 if (entry->action & APPRAISE)
978 seq_puts(m, pt(Opt_appraise));
979 if (entry->action & DONT_APPRAISE)
980 seq_puts(m, pt(Opt_dont_appraise));
981 if (entry->action & AUDIT)
982 seq_puts(m, pt(Opt_audit));
983
984 seq_puts(m, " ");
985
Mimi Zoharb5269ab2016-01-28 13:10:36 -0500986 if (entry->flags & IMA_FUNC)
987 policy_func_show(m, entry->func);
Petko Manolov80eae202015-12-02 17:47:56 +0200988
989 if (entry->flags & IMA_MASK) {
990 if (entry->mask & MAY_EXEC)
991 seq_printf(m, pt(Opt_mask), mt(mask_exec));
992 if (entry->mask & MAY_WRITE)
993 seq_printf(m, pt(Opt_mask), mt(mask_write));
994 if (entry->mask & MAY_READ)
995 seq_printf(m, pt(Opt_mask), mt(mask_read));
996 if (entry->mask & MAY_APPEND)
997 seq_printf(m, pt(Opt_mask), mt(mask_append));
998 seq_puts(m, " ");
999 }
1000
1001 if (entry->flags & IMA_FSMAGIC) {
1002 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
1003 seq_printf(m, pt(Opt_fsmagic), tbuf);
1004 seq_puts(m, " ");
1005 }
1006
1007 if (entry->flags & IMA_FSUUID) {
1008 seq_puts(m, "fsuuid=");
1009 for (i = 0; i < ARRAY_SIZE(entry->fsuuid); ++i) {
1010 switch (i) {
1011 case 4:
1012 case 6:
1013 case 8:
1014 case 10:
1015 seq_puts(m, "-");
1016 }
1017 seq_printf(m, "%x", entry->fsuuid[i]);
1018 }
1019 seq_puts(m, " ");
1020 }
1021
1022 if (entry->flags & IMA_UID) {
1023 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1024 seq_printf(m, pt(Opt_uid), tbuf);
1025 seq_puts(m, " ");
1026 }
1027
1028 if (entry->flags & IMA_EUID) {
1029 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1030 seq_printf(m, pt(Opt_euid), tbuf);
1031 seq_puts(m, " ");
1032 }
1033
1034 if (entry->flags & IMA_FOWNER) {
1035 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
1036 seq_printf(m, pt(Opt_fowner), tbuf);
1037 seq_puts(m, " ");
1038 }
1039
1040 for (i = 0; i < MAX_LSM_RULES; i++) {
1041 if (entry->lsm[i].rule) {
1042 switch (i) {
1043 case LSM_OBJ_USER:
1044 seq_printf(m, pt(Opt_obj_user),
1045 (char *)entry->lsm[i].args_p);
1046 break;
1047 case LSM_OBJ_ROLE:
1048 seq_printf(m, pt(Opt_obj_role),
1049 (char *)entry->lsm[i].args_p);
1050 break;
1051 case LSM_OBJ_TYPE:
1052 seq_printf(m, pt(Opt_obj_type),
1053 (char *)entry->lsm[i].args_p);
1054 break;
1055 case LSM_SUBJ_USER:
1056 seq_printf(m, pt(Opt_subj_user),
1057 (char *)entry->lsm[i].args_p);
1058 break;
1059 case LSM_SUBJ_ROLE:
1060 seq_printf(m, pt(Opt_subj_role),
1061 (char *)entry->lsm[i].args_p);
1062 break;
1063 case LSM_SUBJ_TYPE:
1064 seq_printf(m, pt(Opt_subj_type),
1065 (char *)entry->lsm[i].args_p);
1066 break;
1067 }
1068 }
1069 }
1070 if (entry->flags & IMA_DIGSIG_REQUIRED)
1071 seq_puts(m, "appraise_type=imasig ");
1072 if (entry->flags & IMA_PERMIT_DIRECTIO)
1073 seq_puts(m, "permit_directio ");
1074 rcu_read_unlock();
1075 seq_puts(m, "\n");
1076 return 0;
1077}
1078#endif /* CONFIG_IMA_READ_POLICY */