blob: be09e2cacf828d32c75028ccf04f31e88657da70 [file] [log] [blame]
Mimi Zohar3323eec92009-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 Zohar3323eec92009-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 Zohar3323eec92009-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 Zohar3323eec92009-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 Zohar3323eec92009-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 Zohar3323eec92009-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 Zohar3323eec92009-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 Zohar3323eec92009-02-04 09:06:58 -050054 struct list_head list;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050055 int action;
Mimi Zohar3323eec92009-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 Zohar3323eec92009-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 Zohar3323eec92009-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 Zohar3323eec92009-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 Zohar3323eec92009-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},
Mimi Zohar95ee08f2015-12-07 15:08:01 -0500132#ifdef CONFIG_IMA_WRITE_POLICY
133 {.action = APPRAISE, .func = POLICY_CHECK,
134 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
135#endif
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200136#ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200137 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .flags = IMA_FOWNER},
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200138#else
139 /* force signature */
140 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID,
141 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
142#endif
Mimi Zohar07f6a792011-03-09 22:25:48 -0500143};
Mimi Zohar3323eec92009-02-04 09:06:58 -0500144
Mimi Zohar07f6a792011-03-09 22:25:48 -0500145static LIST_HEAD(ima_default_rules);
146static LIST_HEAD(ima_policy_rules);
Petko Manolov38d859f2015-12-02 17:47:54 +0200147static LIST_HEAD(ima_temp_rules);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500148static struct list_head *ima_rules;
149
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400150static int ima_policy __initdata;
Petko Manolov38d859f2015-12-02 17:47:54 +0200151
Mimi Zohar07f6a792011-03-09 22:25:48 -0500152static int __init default_measure_policy_setup(char *str)
Eric Paris5789ba32009-05-21 15:47:06 -0400153{
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400154 if (ima_policy)
155 return 1;
156
157 ima_policy = ORIGINAL_TCB;
Eric Paris5789ba32009-05-21 15:47:06 -0400158 return 1;
159}
Mimi Zohar07f6a792011-03-09 22:25:48 -0500160__setup("ima_tcb", default_measure_policy_setup);
161
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400162static int __init policy_setup(char *str)
163{
164 if (ima_policy)
165 return 1;
166
167 if (strcmp(str, "tcb") == 0)
168 ima_policy = DEFAULT_TCB;
169
170 return 1;
171}
172__setup("ima_policy=", policy_setup);
173
Mimi Zohar07f6a792011-03-09 22:25:48 -0500174static bool ima_use_appraise_tcb __initdata;
175static int __init default_appraise_policy_setup(char *str)
176{
177 ima_use_appraise_tcb = 1;
178 return 1;
179}
180__setup("ima_appraise_tcb", default_appraise_policy_setup);
Eric Paris5789ba32009-05-21 15:47:06 -0400181
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200182/*
Petko Manolov38d859f2015-12-02 17:47:54 +0200183 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
184 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
185 * the reloaded LSM policy. We assume the rules still exist; and BUG_ON() if
186 * they don't.
Mimi Zohar7163a992013-01-03 14:19:09 -0500187 */
188static void ima_lsm_update_rules(void)
189{
Petko Manolov38d859f2015-12-02 17:47:54 +0200190 struct ima_rule_entry *entry;
Mimi Zohar7163a992013-01-03 14:19:09 -0500191 int result;
192 int i;
193
Petko Manolov38d859f2015-12-02 17:47:54 +0200194 list_for_each_entry(entry, &ima_policy_rules, list) {
Mimi Zohar7163a992013-01-03 14:19:09 -0500195 for (i = 0; i < MAX_LSM_RULES; i++) {
196 if (!entry->lsm[i].rule)
197 continue;
198 result = security_filter_rule_init(entry->lsm[i].type,
199 Audit_equal,
200 entry->lsm[i].args_p,
201 &entry->lsm[i].rule);
202 BUG_ON(!entry->lsm[i].rule);
203 }
204 }
Mimi Zohar7163a992013-01-03 14:19:09 -0500205}
206
Mimi Zohar3323eec92009-02-04 09:06:58 -0500207/**
208 * ima_match_rules - determine whether an inode matches the measure rule.
209 * @rule: a pointer to a rule
210 * @inode: a pointer to an inode
211 * @func: LIM hook identifier
212 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
213 *
214 * Returns true on rule match, false on failure.
215 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500216static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,
217 enum ima_hooks func, int mask)
Mimi Zohar3323eec92009-02-04 09:06:58 -0500218{
219 struct task_struct *tsk = current;
Mimi Zohar3db59dd2012-01-17 22:11:28 -0500220 const struct cred *cred = current_cred();
Mimi Zohar4af46622009-02-04 09:07:00 -0500221 int i;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500222
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200223 if ((rule->flags & IMA_FUNC) &&
224 (rule->func != func && func != POST_SETATTR))
Mimi Zohar3323eec92009-02-04 09:06:58 -0500225 return false;
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200226 if ((rule->flags & IMA_MASK) &&
227 (rule->mask != mask && func != POST_SETATTR))
Mimi Zohar3323eec92009-02-04 09:06:58 -0500228 return false;
Mimi Zohar4351c292014-11-05 07:53:55 -0500229 if ((rule->flags & IMA_INMASK) &&
230 (!(rule->mask & mask) && func != POST_SETATTR))
231 return false;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500232 if ((rule->flags & IMA_FSMAGIC)
233 && rule->fsmagic != inode->i_sb->s_magic)
234 return false;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300235 if ((rule->flags & IMA_FSUUID) &&
Mimi Zohar446d64e2013-02-24 23:42:37 -0500236 memcmp(rule->fsuuid, inode->i_sb->s_uuid, sizeof(rule->fsuuid)))
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300237 return false;
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600238 if ((rule->flags & IMA_UID) && !uid_eq(rule->uid, cred->uid))
Mimi Zohar3323eec92009-02-04 09:06:58 -0500239 return false;
Mimi Zohar139069e2014-11-05 07:48:36 -0500240 if (rule->flags & IMA_EUID) {
241 if (has_capability_noaudit(current, CAP_SETUID)) {
242 if (!uid_eq(rule->uid, cred->euid)
243 && !uid_eq(rule->uid, cred->suid)
244 && !uid_eq(rule->uid, cred->uid))
245 return false;
246 } else if (!uid_eq(rule->uid, cred->euid))
247 return false;
248 }
249
Linus Torvalds882653222012-10-02 21:38:48 -0700250 if ((rule->flags & IMA_FOWNER) && !uid_eq(rule->fowner, inode->i_uid))
Mimi Zohar07f6a792011-03-09 22:25:48 -0500251 return false;
Mimi Zohar4af46622009-02-04 09:07:00 -0500252 for (i = 0; i < MAX_LSM_RULES; i++) {
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400253 int rc = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500254 u32 osid, sid;
Mimi Zohar7163a992013-01-03 14:19:09 -0500255 int retried = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500256
257 if (!rule->lsm[i].rule)
258 continue;
Mimi Zohar7163a992013-01-03 14:19:09 -0500259retry:
Mimi Zohar4af46622009-02-04 09:07:00 -0500260 switch (i) {
261 case LSM_OBJ_USER:
262 case LSM_OBJ_ROLE:
263 case LSM_OBJ_TYPE:
264 security_inode_getsecid(inode, &osid);
265 rc = security_filter_rule_match(osid,
266 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400267 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500268 rule->lsm[i].rule,
269 NULL);
270 break;
271 case LSM_SUBJ_USER:
272 case LSM_SUBJ_ROLE:
273 case LSM_SUBJ_TYPE:
274 security_task_getsecid(tsk, &sid);
275 rc = security_filter_rule_match(sid,
276 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400277 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500278 rule->lsm[i].rule,
279 NULL);
280 default:
281 break;
282 }
Mimi Zohar7163a992013-01-03 14:19:09 -0500283 if ((rc < 0) && (!retried)) {
284 retried = 1;
285 ima_lsm_update_rules();
286 goto retry;
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200287 }
Mimi Zohar4af46622009-02-04 09:07:00 -0500288 if (!rc)
289 return false;
290 }
Mimi Zohar3323eec92009-02-04 09:06:58 -0500291 return true;
292}
293
Mimi Zohard79d72e2012-12-03 17:08:11 -0500294/*
295 * In addition to knowing that we need to appraise the file in general,
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500296 * we need to differentiate between calling hooks, for hook specific rules.
Mimi Zohard79d72e2012-12-03 17:08:11 -0500297 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500298static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
Mimi Zohard79d72e2012-12-03 17:08:11 -0500299{
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500300 if (!(rule->flags & IMA_FUNC))
301 return IMA_FILE_APPRAISE;
302
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200303 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500304 case MMAP_CHECK:
305 return IMA_MMAP_APPRAISE;
306 case BPRM_CHECK:
307 return IMA_BPRM_APPRAISE;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500308 case FILE_CHECK:
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500309 case POST_SETATTR:
Mimi Zohard79d72e2012-12-03 17:08:11 -0500310 return IMA_FILE_APPRAISE;
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500311 case MODULE_CHECK ... MAX_CHECK - 1:
312 default:
313 return IMA_READ_APPRAISE;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500314 }
315}
316
Mimi Zohar3323eec92009-02-04 09:06:58 -0500317/**
318 * ima_match_policy - decision based on LSM and other conditions
319 * @inode: pointer to an inode for which the policy decision is being made
320 * @func: IMA hook identifier
321 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
322 *
323 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
324 * conditions.
325 *
Petko Manolov38d859f2015-12-02 17:47:54 +0200326 * Since the IMA policy may be updated multiple times we need to lock the
327 * list when walking it. Reads are many orders of magnitude more numerous
328 * than writes so ima_match_policy() is classical RCU candidate.
Mimi Zohar3323eec92009-02-04 09:06:58 -0500329 */
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500330int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
331 int flags)
Mimi Zohar3323eec92009-02-04 09:06:58 -0500332{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500333 struct ima_rule_entry *entry;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500334 int action = 0, actmask = flags | (flags << 1);
Mimi Zohar3323eec92009-02-04 09:06:58 -0500335
Petko Manolov38d859f2015-12-02 17:47:54 +0200336 rcu_read_lock();
337 list_for_each_entry_rcu(entry, ima_rules, list) {
Mimi Zohar3323eec92009-02-04 09:06:58 -0500338
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500339 if (!(entry->action & actmask))
340 continue;
341
342 if (!ima_match_rules(entry, inode, func, mask))
343 continue;
344
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300345 action |= entry->flags & IMA_ACTION_FLAGS;
346
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300347 action |= entry->action & IMA_DO_MASK;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500348 if (entry->action & IMA_APPRAISE)
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500349 action |= get_subaction(entry, func);
Mimi Zohard79d72e2012-12-03 17:08:11 -0500350
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300351 if (entry->action & IMA_DO_MASK)
352 actmask &= ~(entry->action | entry->action << 1);
353 else
354 actmask &= ~(entry->action | entry->action >> 1);
355
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500356 if (!actmask)
357 break;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500358 }
Petko Manolov38d859f2015-12-02 17:47:54 +0200359 rcu_read_unlock();
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500360
361 return action;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500362}
363
Roberto Sassua7560242014-09-12 19:35:54 +0200364/*
365 * Initialize the ima_policy_flag variable based on the currently
366 * loaded policy. Based on this flag, the decision to short circuit
367 * out of a function or not call the function in the first place
368 * can be made earlier.
369 */
370void ima_update_policy_flag(void)
371{
372 struct ima_rule_entry *entry;
373
Roberto Sassua7560242014-09-12 19:35:54 +0200374 list_for_each_entry(entry, ima_rules, list) {
375 if (entry->action & IMA_DO_MASK)
376 ima_policy_flag |= entry->action;
377 }
378
Mimi Zohar6ad6afa2015-12-07 14:35:47 -0500379 ima_appraise |= temp_ima_appraise;
Roberto Sassua7560242014-09-12 19:35:54 +0200380 if (!ima_appraise)
381 ima_policy_flag &= ~IMA_APPRAISE;
382}
383
Mimi Zohar3323eec92009-02-04 09:06:58 -0500384/**
385 * ima_init_policy - initialize the default measure rules.
386 *
Mimi Zohar07f6a792011-03-09 22:25:48 -0500387 * ima_rules points to either the ima_default_rules or the
388 * the new ima_policy_rules.
Mimi Zohar3323eec92009-02-04 09:06:58 -0500389 */
Eric Paris932995f2009-05-21 15:43:32 -0400390void __init ima_init_policy(void)
Mimi Zohar3323eec92009-02-04 09:06:58 -0500391{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500392 int i, measure_entries, appraise_entries;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500393
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400394 /* if !ima_policy set entries = 0 so we load NO default rules */
395 measure_entries = ima_policy ? ARRAY_SIZE(dont_measure_rules) : 0;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500396 appraise_entries = ima_use_appraise_tcb ?
397 ARRAY_SIZE(default_appraise_rules) : 0;
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200398
Dan Carpenter55778572015-04-07 12:22:11 +0300399 for (i = 0; i < measure_entries; i++)
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400400 list_add_tail(&dont_measure_rules[i].list, &ima_default_rules);
401
402 switch (ima_policy) {
403 case ORIGINAL_TCB:
404 for (i = 0; i < ARRAY_SIZE(original_measurement_rules); i++)
405 list_add_tail(&original_measurement_rules[i].list,
406 &ima_default_rules);
407 break;
408 case DEFAULT_TCB:
409 for (i = 0; i < ARRAY_SIZE(default_measurement_rules); i++)
410 list_add_tail(&default_measurement_rules[i].list,
411 &ima_default_rules);
412 default:
413 break;
414 }
Eric Paris5789ba32009-05-21 15:47:06 -0400415
Dan Carpenter55778572015-04-07 12:22:11 +0300416 for (i = 0; i < appraise_entries; i++) {
417 list_add_tail(&default_appraise_rules[i].list,
418 &ima_default_rules);
Mimi Zohar95ee08f2015-12-07 15:08:01 -0500419 if (default_appraise_rules[i].func == POLICY_CHECK)
420 temp_ima_appraise |= IMA_APPRAISE_POLICY;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500421 }
422
423 ima_rules = &ima_default_rules;
Mimi Zohar95ee08f2015-12-07 15:08:01 -0500424 ima_update_policy_flag();
Mimi Zohar3323eec92009-02-04 09:06:58 -0500425}
Mimi Zohar4af46622009-02-04 09:07:00 -0500426
Sasha Levin01127212015-12-22 08:51:23 -0500427/* Make sure we have a valid policy, at least containing some rules. */
Colin Ian Kingc75d8e92016-01-20 11:13:46 +0000428int ima_check_policy(void)
Sasha Levin01127212015-12-22 08:51:23 -0500429{
430 if (list_empty(&ima_temp_rules))
431 return -EINVAL;
432 return 0;
433}
434
Mimi Zohar4af46622009-02-04 09:07:00 -0500435/**
436 * ima_update_policy - update default_rules with new measure rules
437 *
438 * Called on file .release to update the default rules with a complete new
Petko Manolov38d859f2015-12-02 17:47:54 +0200439 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
440 * they make a queue. The policy may be updated multiple times and this is the
441 * RCU updater.
442 *
443 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
444 * we switch from the default policy to user defined.
Mimi Zohar4af46622009-02-04 09:07:00 -0500445 */
446void ima_update_policy(void)
447{
Petko Manolov38d859f2015-12-02 17:47:54 +0200448 struct list_head *first, *last, *policy;
449
450 /* append current policy with the new rules */
451 first = (&ima_temp_rules)->next;
452 last = (&ima_temp_rules)->prev;
453 policy = &ima_policy_rules;
454
455 synchronize_rcu();
456
457 last->next = policy;
458 rcu_assign_pointer(list_next_rcu(policy->prev), first);
459 first->prev = policy->prev;
460 policy->prev = last;
461
462 /* prepare for the next policy rules addition */
463 INIT_LIST_HEAD(&ima_temp_rules);
464
465 if (ima_rules != policy) {
466 ima_policy_flag = 0;
467 ima_rules = policy;
468 }
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300469 ima_update_policy_flag();
Mimi Zohar4af46622009-02-04 09:07:00 -0500470}
471
472enum {
473 Opt_err = -1,
474 Opt_measure = 1, Opt_dont_measure,
Mimi Zohar07f6a792011-03-09 22:25:48 -0500475 Opt_appraise, Opt_dont_appraise,
Peter Moodye7c568e2012-06-14 10:04:36 -0700476 Opt_audit,
Mimi Zohar4af46622009-02-04 09:07:00 -0500477 Opt_obj_user, Opt_obj_role, Opt_obj_type,
478 Opt_subj_user, Opt_subj_role, Opt_subj_type,
Mimi Zohar139069e2014-11-05 07:48:36 -0500479 Opt_func, Opt_mask, Opt_fsmagic,
Petko Manolov80eae202015-12-02 17:47:56 +0200480 Opt_fsuuid, Opt_uid, Opt_euid, Opt_fowner,
481 Opt_appraise_type, Opt_permit_directio
Mimi Zohar4af46622009-02-04 09:07:00 -0500482};
483
484static match_table_t policy_tokens = {
485 {Opt_measure, "measure"},
486 {Opt_dont_measure, "dont_measure"},
Mimi Zohar07f6a792011-03-09 22:25:48 -0500487 {Opt_appraise, "appraise"},
488 {Opt_dont_appraise, "dont_appraise"},
Peter Moodye7c568e2012-06-14 10:04:36 -0700489 {Opt_audit, "audit"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500490 {Opt_obj_user, "obj_user=%s"},
491 {Opt_obj_role, "obj_role=%s"},
492 {Opt_obj_type, "obj_type=%s"},
493 {Opt_subj_user, "subj_user=%s"},
494 {Opt_subj_role, "subj_role=%s"},
495 {Opt_subj_type, "subj_type=%s"},
496 {Opt_func, "func=%s"},
497 {Opt_mask, "mask=%s"},
498 {Opt_fsmagic, "fsmagic=%s"},
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300499 {Opt_fsuuid, "fsuuid=%s"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500500 {Opt_uid, "uid=%s"},
Mimi Zohar139069e2014-11-05 07:48:36 -0500501 {Opt_euid, "euid=%s"},
Mimi Zohar07f6a792011-03-09 22:25:48 -0500502 {Opt_fowner, "fowner=%s"},
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300503 {Opt_appraise_type, "appraise_type=%s"},
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400504 {Opt_permit_directio, "permit_directio"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500505 {Opt_err, NULL}
506};
507
Mimi Zohar07f6a792011-03-09 22:25:48 -0500508static int ima_lsm_rule_init(struct ima_rule_entry *entry,
Mimi Zohar7163a992013-01-03 14:19:09 -0500509 substring_t *args, int lsm_rule, int audit_type)
Mimi Zohar4af46622009-02-04 09:07:00 -0500510{
511 int result;
512
Eric Paris7b62e162010-04-20 10:21:01 -0400513 if (entry->lsm[lsm_rule].rule)
514 return -EINVAL;
515
Mimi Zohar7163a992013-01-03 14:19:09 -0500516 entry->lsm[lsm_rule].args_p = match_strdup(args);
517 if (!entry->lsm[lsm_rule].args_p)
518 return -ENOMEM;
519
Mimi Zohar4af46622009-02-04 09:07:00 -0500520 entry->lsm[lsm_rule].type = audit_type;
521 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
Mimi Zohar7163a992013-01-03 14:19:09 -0500522 Audit_equal,
523 entry->lsm[lsm_rule].args_p,
Mimi Zohar4af46622009-02-04 09:07:00 -0500524 &entry->lsm[lsm_rule].rule);
Mimi Zohar7163a992013-01-03 14:19:09 -0500525 if (!entry->lsm[lsm_rule].rule) {
526 kfree(entry->lsm[lsm_rule].args_p);
Mimi Zohar867c2022011-01-03 14:59:10 -0800527 return -EINVAL;
Mimi Zohar7163a992013-01-03 14:19:09 -0500528 }
529
Mimi Zohar4af46622009-02-04 09:07:00 -0500530 return result;
531}
532
Eric Paris2f1506c2010-04-20 10:21:30 -0400533static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
534{
535 audit_log_format(ab, "%s=", key);
536 audit_log_untrustedstring(ab, value);
537 audit_log_format(ab, " ");
538}
539
Mimi Zohar07f6a792011-03-09 22:25:48 -0500540static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
Mimi Zohar4af46622009-02-04 09:07:00 -0500541{
542 struct audit_buffer *ab;
Mimi Zohar4351c292014-11-05 07:53:55 -0500543 char *from;
Mimi Zohar4af46622009-02-04 09:07:00 -0500544 char *p;
545 int result = 0;
546
Mimi Zohar523979a2009-02-11 11:12:28 -0500547 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
Mimi Zohar4af46622009-02-04 09:07:00 -0500548
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600549 entry->uid = INVALID_UID;
Linus Torvalds882653222012-10-02 21:38:48 -0700550 entry->fowner = INVALID_UID;
Eric Parisb9035b12010-04-20 10:21:07 -0400551 entry->action = UNKNOWN;
Eric Paris28ef4002010-04-20 10:21:18 -0400552 while ((p = strsep(&rule, " \t")) != NULL) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500553 substring_t args[MAX_OPT_ARGS];
554 int token;
555 unsigned long lnum;
556
557 if (result < 0)
558 break;
Eric Paris28ef4002010-04-20 10:21:18 -0400559 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
560 continue;
Mimi Zohar4af46622009-02-04 09:07:00 -0500561 token = match_token(p, policy_tokens, args);
562 switch (token) {
563 case Opt_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400564 ima_log_string(ab, "action", "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 = MEASURE;
570 break;
571 case Opt_dont_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400572 ima_log_string(ab, "action", "dont_measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400573
574 if (entry->action != UNKNOWN)
575 result = -EINVAL;
576
Mimi Zohar4af46622009-02-04 09:07:00 -0500577 entry->action = DONT_MEASURE;
578 break;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500579 case Opt_appraise:
580 ima_log_string(ab, "action", "appraise");
581
582 if (entry->action != UNKNOWN)
583 result = -EINVAL;
584
585 entry->action = APPRAISE;
586 break;
587 case Opt_dont_appraise:
588 ima_log_string(ab, "action", "dont_appraise");
589
590 if (entry->action != UNKNOWN)
591 result = -EINVAL;
592
593 entry->action = DONT_APPRAISE;
594 break;
Peter Moodye7c568e2012-06-14 10:04:36 -0700595 case Opt_audit:
596 ima_log_string(ab, "action", "audit");
597
598 if (entry->action != UNKNOWN)
599 result = -EINVAL;
600
601 entry->action = AUDIT;
602 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500603 case Opt_func:
Eric Paris2f1506c2010-04-20 10:21:30 -0400604 ima_log_string(ab, "func", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400605
606 if (entry->func)
Mimi Zohar07f6a792011-03-09 22:25:48 -0500607 result = -EINVAL;
Eric Paris7b62e162010-04-20 10:21:01 -0400608
Mimi Zohar1e93d002010-01-26 17:02:41 -0500609 if (strcmp(args[0].from, "FILE_CHECK") == 0)
610 entry->func = FILE_CHECK;
611 /* PATH_CHECK is for backwards compat */
612 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
613 entry->func = FILE_CHECK;
Mimi Zoharfdf90722012-10-16 12:40:08 +1030614 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
615 entry->func = MODULE_CHECK;
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400616 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
617 entry->func = FIRMWARE_CHECK;
Mimi Zohar16cac492012-12-13 11:15:04 -0500618 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
619 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
620 entry->func = MMAP_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500621 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
622 entry->func = BPRM_CHECK;
Mimi Zohard9ddf072016-01-14 20:59:14 -0500623 else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
624 0)
625 entry->func = KEXEC_KERNEL_CHECK;
626 else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
627 == 0)
628 entry->func = KEXEC_INITRAMFS_CHECK;
Mimi Zohar19f8a842016-01-15 10:17:12 -0500629 else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
630 entry->func = POLICY_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500631 else
632 result = -EINVAL;
633 if (!result)
634 entry->flags |= IMA_FUNC;
635 break;
636 case Opt_mask:
Eric Paris2f1506c2010-04-20 10:21:30 -0400637 ima_log_string(ab, "mask", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400638
639 if (entry->mask)
640 result = -EINVAL;
641
Mimi Zohar4351c292014-11-05 07:53:55 -0500642 from = args[0].from;
643 if (*from == '^')
644 from++;
645
646 if ((strcmp(from, "MAY_EXEC")) == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500647 entry->mask = MAY_EXEC;
Mimi Zohar4351c292014-11-05 07:53:55 -0500648 else if (strcmp(from, "MAY_WRITE") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500649 entry->mask = MAY_WRITE;
Mimi Zohar4351c292014-11-05 07:53:55 -0500650 else if (strcmp(from, "MAY_READ") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500651 entry->mask = MAY_READ;
Mimi Zohar4351c292014-11-05 07:53:55 -0500652 else if (strcmp(from, "MAY_APPEND") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500653 entry->mask = MAY_APPEND;
654 else
655 result = -EINVAL;
656 if (!result)
Mimi Zohar4351c292014-11-05 07:53:55 -0500657 entry->flags |= (*args[0].from == '^')
658 ? IMA_INMASK : IMA_MASK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500659 break;
660 case Opt_fsmagic:
Eric Paris2f1506c2010-04-20 10:21:30 -0400661 ima_log_string(ab, "fsmagic", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400662
663 if (entry->fsmagic) {
664 result = -EINVAL;
665 break;
666 }
667
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200668 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
Mimi Zohar4af46622009-02-04 09:07:00 -0500669 if (!result)
670 entry->flags |= IMA_FSMAGIC;
671 break;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300672 case Opt_fsuuid:
673 ima_log_string(ab, "fsuuid", args[0].from);
674
675 if (memchr_inv(entry->fsuuid, 0x00,
Mimi Zohar446d64e2013-02-24 23:42:37 -0500676 sizeof(entry->fsuuid))) {
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300677 result = -EINVAL;
678 break;
679 }
680
Mimi Zohar446d64e2013-02-24 23:42:37 -0500681 result = blk_part_pack_uuid(args[0].from,
682 entry->fsuuid);
683 if (!result)
684 entry->flags |= IMA_FSUUID;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300685 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500686 case Opt_uid:
Eric Paris2f1506c2010-04-20 10:21:30 -0400687 ima_log_string(ab, "uid", args[0].from);
Mimi Zohar139069e2014-11-05 07:48:36 -0500688 case Opt_euid:
689 if (token == Opt_euid)
690 ima_log_string(ab, "euid", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400691
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600692 if (uid_valid(entry->uid)) {
Eric Paris7b62e162010-04-20 10:21:01 -0400693 result = -EINVAL;
694 break;
695 }
696
Jingoo Han29707b22014-02-05 15:13:14 +0900697 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar4af46622009-02-04 09:07:00 -0500698 if (!result) {
Mimi Zohar139069e2014-11-05 07:48:36 -0500699 entry->uid = make_kuid(current_user_ns(),
700 (uid_t) lnum);
701 if (!uid_valid(entry->uid) ||
702 (uid_t)lnum != lnum)
Mimi Zohar4af46622009-02-04 09:07:00 -0500703 result = -EINVAL;
704 else
Mimi Zohar139069e2014-11-05 07:48:36 -0500705 entry->flags |= (token == Opt_uid)
706 ? IMA_UID : IMA_EUID;
Mimi Zohar4af46622009-02-04 09:07:00 -0500707 }
708 break;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500709 case Opt_fowner:
710 ima_log_string(ab, "fowner", args[0].from);
711
Linus Torvalds882653222012-10-02 21:38:48 -0700712 if (uid_valid(entry->fowner)) {
Mimi Zohar07f6a792011-03-09 22:25:48 -0500713 result = -EINVAL;
714 break;
715 }
716
Jingoo Han29707b22014-02-05 15:13:14 +0900717 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500718 if (!result) {
Linus Torvalds882653222012-10-02 21:38:48 -0700719 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
720 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
Mimi Zohar07f6a792011-03-09 22:25:48 -0500721 result = -EINVAL;
722 else
723 entry->flags |= IMA_FOWNER;
724 }
725 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500726 case Opt_obj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400727 ima_log_string(ab, "obj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500728 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500729 LSM_OBJ_USER,
730 AUDIT_OBJ_USER);
731 break;
732 case Opt_obj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400733 ima_log_string(ab, "obj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500734 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500735 LSM_OBJ_ROLE,
736 AUDIT_OBJ_ROLE);
737 break;
738 case Opt_obj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400739 ima_log_string(ab, "obj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500740 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500741 LSM_OBJ_TYPE,
742 AUDIT_OBJ_TYPE);
743 break;
744 case Opt_subj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400745 ima_log_string(ab, "subj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500746 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500747 LSM_SUBJ_USER,
748 AUDIT_SUBJ_USER);
749 break;
750 case Opt_subj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400751 ima_log_string(ab, "subj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500752 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500753 LSM_SUBJ_ROLE,
754 AUDIT_SUBJ_ROLE);
755 break;
756 case Opt_subj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400757 ima_log_string(ab, "subj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500758 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500759 LSM_SUBJ_TYPE,
760 AUDIT_SUBJ_TYPE);
761 break;
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300762 case Opt_appraise_type:
763 if (entry->action != APPRAISE) {
764 result = -EINVAL;
765 break;
766 }
767
768 ima_log_string(ab, "appraise_type", args[0].from);
769 if ((strcmp(args[0].from, "imasig")) == 0)
770 entry->flags |= IMA_DIGSIG_REQUIRED;
771 else
772 result = -EINVAL;
773 break;
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400774 case Opt_permit_directio:
775 entry->flags |= IMA_PERMIT_DIRECTIO;
776 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500777 case Opt_err:
Eric Paris2f1506c2010-04-20 10:21:30 -0400778 ima_log_string(ab, "UNKNOWN", p);
Eric Parise9d393b2010-04-20 10:21:13 -0400779 result = -EINVAL;
Mimi Zohar4af46622009-02-04 09:07:00 -0500780 break;
781 }
782 }
Eric Paris7b62e162010-04-20 10:21:01 -0400783 if (!result && (entry->action == UNKNOWN))
Mimi Zohar4af46622009-02-04 09:07:00 -0500784 result = -EINVAL;
Mimi Zohara7f2a362012-12-21 08:34:21 -0500785 else if (entry->func == MODULE_CHECK)
Mimi Zohar6ad6afa2015-12-07 14:35:47 -0500786 temp_ima_appraise |= IMA_APPRAISE_MODULES;
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400787 else if (entry->func == FIRMWARE_CHECK)
Mimi Zohar6ad6afa2015-12-07 14:35:47 -0500788 temp_ima_appraise |= IMA_APPRAISE_FIRMWARE;
Mimi Zohar19f8a842016-01-15 10:17:12 -0500789 else if (entry->func == POLICY_CHECK)
790 temp_ima_appraise |= IMA_APPRAISE_POLICY;
Eric Parisb0d5de42012-02-14 17:11:07 -0500791 audit_log_format(ab, "res=%d", !result);
Mimi Zohar4af46622009-02-04 09:07:00 -0500792 audit_log_end(ab);
793 return result;
794}
795
796/**
Mimi Zohar07f6a792011-03-09 22:25:48 -0500797 * ima_parse_add_rule - add a rule to ima_policy_rules
Mimi Zohar4af46622009-02-04 09:07:00 -0500798 * @rule - ima measurement policy rule
799 *
Petko Manolov38d859f2015-12-02 17:47:54 +0200800 * Avoid locking by allowing just one writer at a time in ima_write_policy()
Eric Paris6ccd0452010-04-20 10:20:54 -0400801 * Returns the length of the rule parsed, an error code on failure
Mimi Zohar4af46622009-02-04 09:07:00 -0500802 */
Eric Paris6ccd0452010-04-20 10:20:54 -0400803ssize_t ima_parse_add_rule(char *rule)
Mimi Zohar4af46622009-02-04 09:07:00 -0500804{
Mimi Zohar52a13282013-12-11 14:44:04 -0500805 static const char op[] = "update_policy";
Eric Paris6ccd0452010-04-20 10:20:54 -0400806 char *p;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500807 struct ima_rule_entry *entry;
Eric Paris6ccd0452010-04-20 10:20:54 -0400808 ssize_t result, len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500809 int audit_info = 0;
810
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +0300811 p = strsep(&rule, "\n");
812 len = strlen(p) + 1;
Dmitry Kasatkin71787842014-10-03 14:40:20 +0300813 p += strspn(p, " \t");
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +0300814
Dmitry Kasatkin71787842014-10-03 14:40:20 +0300815 if (*p == '#' || *p == '\0')
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +0300816 return len;
817
Mimi Zohar4af46622009-02-04 09:07:00 -0500818 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
819 if (!entry) {
820 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
821 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
822 return -ENOMEM;
823 }
824
825 INIT_LIST_HEAD(&entry->list);
826
Eric Paris6ccd0452010-04-20 10:20:54 -0400827 result = ima_parse_rule(p, entry);
Eric Paris7233e3e2010-04-20 10:21:24 -0400828 if (result) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500829 kfree(entry);
Mimi Zohar523979a2009-02-11 11:12:28 -0500830 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
Richard Guy Briggs7e9001f2014-06-16 15:52:07 -0400831 NULL, op, "invalid-policy", result,
Mimi Zohar523979a2009-02-11 11:12:28 -0500832 audit_info);
Eric Paris7233e3e2010-04-20 10:21:24 -0400833 return result;
Mimi Zohar523979a2009-02-11 11:12:28 -0500834 }
Eric Paris7233e3e2010-04-20 10:21:24 -0400835
Petko Manolov38d859f2015-12-02 17:47:54 +0200836 list_add_tail(&entry->list, &ima_temp_rules);
Eric Paris7233e3e2010-04-20 10:21:24 -0400837
838 return len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500839}
840
Petko Manolov38d859f2015-12-02 17:47:54 +0200841/**
842 * ima_delete_rules() called to cleanup invalid in-flight policy.
843 * We don't need locking as we operate on the temp list, which is
844 * different from the active one. There is also only one user of
845 * ima_delete_rules() at a time.
846 */
James Morris64c61d82009-02-05 09:28:26 +1100847void ima_delete_rules(void)
Mimi Zohar4af46622009-02-04 09:07:00 -0500848{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500849 struct ima_rule_entry *entry, *tmp;
Mimi Zohar7163a992013-01-03 14:19:09 -0500850 int i;
Mimi Zohar4af46622009-02-04 09:07:00 -0500851
Mimi Zohar6ad6afa2015-12-07 14:35:47 -0500852 temp_ima_appraise = 0;
Petko Manolov38d859f2015-12-02 17:47:54 +0200853 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
Mimi Zohar7163a992013-01-03 14:19:09 -0500854 for (i = 0; i < MAX_LSM_RULES; i++)
855 kfree(entry->lsm[i].args_p);
856
Mimi Zohar4af46622009-02-04 09:07:00 -0500857 list_del(&entry->list);
858 kfree(entry);
859 }
Mimi Zohar4af46622009-02-04 09:07:00 -0500860}
Petko Manolov80eae202015-12-02 17:47:56 +0200861
862#ifdef CONFIG_IMA_READ_POLICY
863enum {
864 mask_exec = 0, mask_write, mask_read, mask_append
865};
866
867static char *mask_tokens[] = {
868 "MAY_EXEC",
869 "MAY_WRITE",
870 "MAY_READ",
871 "MAY_APPEND"
872};
873
874enum {
875 func_file = 0, func_mmap, func_bprm,
Mimi Zohard9ddf072016-01-14 20:59:14 -0500876 func_module, func_firmware, func_post,
Mimi Zohar19f8a842016-01-15 10:17:12 -0500877 func_kexec_kernel, func_kexec_initramfs,
878 func_policy
Petko Manolov80eae202015-12-02 17:47:56 +0200879};
880
881static char *func_tokens[] = {
882 "FILE_CHECK",
883 "MMAP_CHECK",
884 "BPRM_CHECK",
885 "MODULE_CHECK",
886 "FIRMWARE_CHECK",
Mimi Zohard9ddf072016-01-14 20:59:14 -0500887 "KEXEC_KERNEL_CHECK",
888 "KEXEC_INITRAMFS_CHECK",
Mimi Zohar19f8a842016-01-15 10:17:12 -0500889 "POLICY_CHECK",
Petko Manolov80eae202015-12-02 17:47:56 +0200890 "POST_SETATTR"
891};
892
893void *ima_policy_start(struct seq_file *m, loff_t *pos)
894{
895 loff_t l = *pos;
896 struct ima_rule_entry *entry;
897
898 rcu_read_lock();
899 list_for_each_entry_rcu(entry, ima_rules, list) {
900 if (!l--) {
901 rcu_read_unlock();
902 return entry;
903 }
904 }
905 rcu_read_unlock();
906 return NULL;
907}
908
909void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
910{
911 struct ima_rule_entry *entry = v;
912
913 rcu_read_lock();
914 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
915 rcu_read_unlock();
916 (*pos)++;
917
918 return (&entry->list == ima_rules) ? NULL : entry;
919}
920
921void ima_policy_stop(struct seq_file *m, void *v)
922{
923}
924
925#define pt(token) policy_tokens[token + Opt_err].pattern
926#define mt(token) mask_tokens[token]
927#define ft(token) func_tokens[token]
928
Mimi Zoharb5269ab2016-01-28 13:10:36 -0500929/*
930 * policy_func_show - display the ima_hooks policy rule
931 */
932static void policy_func_show(struct seq_file *m, enum ima_hooks func)
933{
934 char tbuf[64] = {0,};
935
936 switch (func) {
937 case FILE_CHECK:
938 seq_printf(m, pt(Opt_func), ft(func_file));
939 break;
940 case MMAP_CHECK:
941 seq_printf(m, pt(Opt_func), ft(func_mmap));
942 break;
943 case BPRM_CHECK:
944 seq_printf(m, pt(Opt_func), ft(func_bprm));
945 break;
946 case MODULE_CHECK:
947 seq_printf(m, pt(Opt_func), ft(func_module));
948 break;
949 case FIRMWARE_CHECK:
950 seq_printf(m, pt(Opt_func), ft(func_firmware));
951 break;
952 case POST_SETATTR:
953 seq_printf(m, pt(Opt_func), ft(func_post));
954 break;
Mimi Zohard9ddf072016-01-14 20:59:14 -0500955 case KEXEC_KERNEL_CHECK:
956 seq_printf(m, pt(Opt_func), ft(func_kexec_kernel));
957 break;
958 case KEXEC_INITRAMFS_CHECK:
959 seq_printf(m, pt(Opt_func), ft(func_kexec_initramfs));
960 break;
Mimi Zohar19f8a842016-01-15 10:17:12 -0500961 case POLICY_CHECK:
962 seq_printf(m, pt(Opt_func), ft(func_policy));
963 break;
Mimi Zoharb5269ab2016-01-28 13:10:36 -0500964 default:
965 snprintf(tbuf, sizeof(tbuf), "%d", func);
966 seq_printf(m, pt(Opt_func), tbuf);
967 break;
968 }
969 seq_puts(m, " ");
970}
971
Petko Manolov80eae202015-12-02 17:47:56 +0200972int ima_policy_show(struct seq_file *m, void *v)
973{
974 struct ima_rule_entry *entry = v;
975 int i = 0;
976 char tbuf[64] = {0,};
977
978 rcu_read_lock();
979
980 if (entry->action & MEASURE)
981 seq_puts(m, pt(Opt_measure));
982 if (entry->action & DONT_MEASURE)
983 seq_puts(m, pt(Opt_dont_measure));
984 if (entry->action & APPRAISE)
985 seq_puts(m, pt(Opt_appraise));
986 if (entry->action & DONT_APPRAISE)
987 seq_puts(m, pt(Opt_dont_appraise));
988 if (entry->action & AUDIT)
989 seq_puts(m, pt(Opt_audit));
990
991 seq_puts(m, " ");
992
Mimi Zoharb5269ab2016-01-28 13:10:36 -0500993 if (entry->flags & IMA_FUNC)
994 policy_func_show(m, entry->func);
Petko Manolov80eae202015-12-02 17:47:56 +0200995
996 if (entry->flags & IMA_MASK) {
997 if (entry->mask & MAY_EXEC)
998 seq_printf(m, pt(Opt_mask), mt(mask_exec));
999 if (entry->mask & MAY_WRITE)
1000 seq_printf(m, pt(Opt_mask), mt(mask_write));
1001 if (entry->mask & MAY_READ)
1002 seq_printf(m, pt(Opt_mask), mt(mask_read));
1003 if (entry->mask & MAY_APPEND)
1004 seq_printf(m, pt(Opt_mask), mt(mask_append));
1005 seq_puts(m, " ");
1006 }
1007
1008 if (entry->flags & IMA_FSMAGIC) {
1009 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
1010 seq_printf(m, pt(Opt_fsmagic), tbuf);
1011 seq_puts(m, " ");
1012 }
1013
1014 if (entry->flags & IMA_FSUUID) {
1015 seq_puts(m, "fsuuid=");
1016 for (i = 0; i < ARRAY_SIZE(entry->fsuuid); ++i) {
1017 switch (i) {
1018 case 4:
1019 case 6:
1020 case 8:
1021 case 10:
1022 seq_puts(m, "-");
1023 }
1024 seq_printf(m, "%x", entry->fsuuid[i]);
1025 }
1026 seq_puts(m, " ");
1027 }
1028
1029 if (entry->flags & IMA_UID) {
1030 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1031 seq_printf(m, pt(Opt_uid), tbuf);
1032 seq_puts(m, " ");
1033 }
1034
1035 if (entry->flags & IMA_EUID) {
1036 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1037 seq_printf(m, pt(Opt_euid), tbuf);
1038 seq_puts(m, " ");
1039 }
1040
1041 if (entry->flags & IMA_FOWNER) {
1042 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
1043 seq_printf(m, pt(Opt_fowner), tbuf);
1044 seq_puts(m, " ");
1045 }
1046
1047 for (i = 0; i < MAX_LSM_RULES; i++) {
1048 if (entry->lsm[i].rule) {
1049 switch (i) {
1050 case LSM_OBJ_USER:
1051 seq_printf(m, pt(Opt_obj_user),
1052 (char *)entry->lsm[i].args_p);
1053 break;
1054 case LSM_OBJ_ROLE:
1055 seq_printf(m, pt(Opt_obj_role),
1056 (char *)entry->lsm[i].args_p);
1057 break;
1058 case LSM_OBJ_TYPE:
1059 seq_printf(m, pt(Opt_obj_type),
1060 (char *)entry->lsm[i].args_p);
1061 break;
1062 case LSM_SUBJ_USER:
1063 seq_printf(m, pt(Opt_subj_user),
1064 (char *)entry->lsm[i].args_p);
1065 break;
1066 case LSM_SUBJ_ROLE:
1067 seq_printf(m, pt(Opt_subj_role),
1068 (char *)entry->lsm[i].args_p);
1069 break;
1070 case LSM_SUBJ_TYPE:
1071 seq_printf(m, pt(Opt_subj_type),
1072 (char *)entry->lsm[i].args_p);
1073 break;
1074 }
1075 }
1076 }
1077 if (entry->flags & IMA_DIGSIG_REQUIRED)
1078 seq_puts(m, "appraise_type=imasig ");
1079 if (entry->flags & IMA_PERMIT_DIRECTIO)
1080 seq_puts(m, "permit_directio ");
1081 rcu_read_unlock();
1082 seq_puts(m, "\n");
1083 return 0;
1084}
1085#endif /* CONFIG_IMA_READ_POLICY */