Linux Audio

Check our new training course

Loading...
  1/*
  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
 10 * 	- initialize default measure policy rules
 11 *
 12 */
 13#include <linux/module.h>
 14#include <linux/list.h>
 15#include <linux/security.h>
 16#include <linux/magic.h>
 17#include <linux/parser.h>
 18#include <linux/slab.h>
 
 19
 20#include "ima.h"
 21
 22/* flags definitions */
 23#define IMA_FUNC 	0x0001
 24#define IMA_MASK 	0x0002
 25#define IMA_FSMAGIC	0x0004
 26#define IMA_UID		0x0008
 
 
 27
 28enum ima_action { UNKNOWN = -1, DONT_MEASURE = 0, MEASURE };
 
 
 
 
 
 29
 30#define MAX_LSM_RULES 6
 31enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
 32	LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
 33};
 34
 35struct ima_measure_rule_entry {
 36	struct list_head list;
 37	enum ima_action action;
 38	unsigned int flags;
 39	enum ima_hooks func;
 40	int mask;
 41	unsigned long fsmagic;
 42	uid_t uid;
 
 
 43	struct {
 44		void *rule;	/* LSM file metadata specific */
 
 45		int type;	/* audit type */
 46	} lsm[MAX_LSM_RULES];
 47};
 48
 49/*
 50 * Without LSM specific knowledge, the default policy can only be
 51 * written in terms of .action, .func, .mask, .fsmagic, and .uid
 52 */
 53
 54/*
 55 * The minimum rule set to allow for full TCB coverage.  Measures all files
 56 * opened or mmap for exec and everything read by root.  Dangerous because
 57 * normal users can easily run the machine out of memory simply building
 58 * and running executables.
 59 */
 60static struct ima_measure_rule_entry default_rules[] = {
 61	{.action = DONT_MEASURE,.fsmagic = PROC_SUPER_MAGIC,.flags = IMA_FSMAGIC},
 62	{.action = DONT_MEASURE,.fsmagic = SYSFS_MAGIC,.flags = IMA_FSMAGIC},
 63	{.action = DONT_MEASURE,.fsmagic = DEBUGFS_MAGIC,.flags = IMA_FSMAGIC},
 64	{.action = DONT_MEASURE,.fsmagic = TMPFS_MAGIC,.flags = IMA_FSMAGIC},
 65	{.action = DONT_MEASURE,.fsmagic = RAMFS_MAGIC,.flags = IMA_FSMAGIC},
 66	{.action = DONT_MEASURE,.fsmagic = SECURITYFS_MAGIC,.flags = IMA_FSMAGIC},
 67	{.action = DONT_MEASURE,.fsmagic = SELINUX_MAGIC,.flags = IMA_FSMAGIC},
 68	{.action = MEASURE,.func = FILE_MMAP,.mask = MAY_EXEC,
 
 69	 .flags = IMA_FUNC | IMA_MASK},
 70	{.action = MEASURE,.func = BPRM_CHECK,.mask = MAY_EXEC,
 71	 .flags = IMA_FUNC | IMA_MASK},
 72	{.action = MEASURE,.func = FILE_CHECK,.mask = MAY_READ,.uid = 0,
 73	 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
 
 74};
 75
 76static LIST_HEAD(measure_default_rules);
 77static LIST_HEAD(measure_policy_rules);
 78static struct list_head *ima_measure;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 79
 80static DEFINE_MUTEX(ima_measure_mutex);
 81
 82static bool ima_use_tcb __initdata;
 83static int __init default_policy_setup(char *str)
 84{
 85	ima_use_tcb = 1;
 86	return 1;
 87}
 88__setup("ima_tcb", default_policy_setup);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 89
 90/**
 91 * ima_match_rules - determine whether an inode matches the measure rule.
 92 * @rule: a pointer to a rule
 93 * @inode: a pointer to an inode
 94 * @func: LIM hook identifier
 95 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
 96 *
 97 * Returns true on rule match, false on failure.
 98 */
 99static bool ima_match_rules(struct ima_measure_rule_entry *rule,
100			    struct inode *inode, enum ima_hooks func, int mask)
101{
102	struct task_struct *tsk = current;
103	const struct cred *cred = current_cred();
104	int i;
105
106	if ((rule->flags & IMA_FUNC) && rule->func != func)
 
107		return false;
108	if ((rule->flags & IMA_MASK) && rule->mask != mask)
 
109		return false;
110	if ((rule->flags & IMA_FSMAGIC)
111	    && rule->fsmagic != inode->i_sb->s_magic)
112		return false;
113	if ((rule->flags & IMA_UID) && rule->uid != cred->uid)
 
 
 
 
 
114		return false;
115	for (i = 0; i < MAX_LSM_RULES; i++) {
116		int rc = 0;
117		u32 osid, sid;
 
118
119		if (!rule->lsm[i].rule)
120			continue;
121
122		switch (i) {
123		case LSM_OBJ_USER:
124		case LSM_OBJ_ROLE:
125		case LSM_OBJ_TYPE:
126			security_inode_getsecid(inode, &osid);
127			rc = security_filter_rule_match(osid,
128							rule->lsm[i].type,
129							Audit_equal,
130							rule->lsm[i].rule,
131							NULL);
132			break;
133		case LSM_SUBJ_USER:
134		case LSM_SUBJ_ROLE:
135		case LSM_SUBJ_TYPE:
136			security_task_getsecid(tsk, &sid);
137			rc = security_filter_rule_match(sid,
138							rule->lsm[i].type,
139							Audit_equal,
140							rule->lsm[i].rule,
141							NULL);
142		default:
143			break;
144		}
 
 
 
 
 
145		if (!rc)
146			return false;
147	}
148	return true;
149}
150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151/**
152 * ima_match_policy - decision based on LSM and other conditions
153 * @inode: pointer to an inode for which the policy decision is being made
154 * @func: IMA hook identifier
155 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
156 *
157 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
158 * conditions.
159 *
160 * (There is no need for locking when walking the policy list,
161 * as elements in the list are never deleted, nor does the list
162 * change.)
163 */
164int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask)
 
165{
166	struct ima_measure_rule_entry *entry;
 
 
 
 
 
 
 
 
 
167
168	list_for_each_entry(entry, ima_measure, list) {
169		bool rc;
170
171		rc = ima_match_rules(entry, inode, func, mask);
172		if (rc)
173			return entry->action;
 
 
 
 
 
 
 
 
174	}
175	return 0;
 
176}
177
178/**
179 * ima_init_policy - initialize the default measure rules.
180 *
181 * ima_measure points to either the measure_default_rules or the
182 * the new measure_policy_rules.
183 */
184void __init ima_init_policy(void)
185{
186	int i, entries;
187
188	/* if !ima_use_tcb set entries = 0 so we load NO default rules */
189	if (ima_use_tcb)
190		entries = ARRAY_SIZE(default_rules);
191	else
192		entries = 0;
193
194	for (i = 0; i < entries; i++)
195		list_add_tail(&default_rules[i].list, &measure_default_rules);
196	ima_measure = &measure_default_rules;
 
 
 
 
 
 
 
 
 
197}
198
199/**
200 * ima_update_policy - update default_rules with new measure rules
201 *
202 * Called on file .release to update the default rules with a complete new
203 * policy.  Once updated, the policy is locked, no additional rules can be
204 * added to the policy.
205 */
206void ima_update_policy(void)
207{
208	const char *op = "policy_update";
209	const char *cause = "already exists";
210	int result = 1;
211	int audit_info = 0;
212
213	if (ima_measure == &measure_default_rules) {
214		ima_measure = &measure_policy_rules;
215		cause = "complete";
216		result = 0;
217	}
218	integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
219			    NULL, op, cause, result, audit_info);
220}
221
222enum {
223	Opt_err = -1,
224	Opt_measure = 1, Opt_dont_measure,
 
 
225	Opt_obj_user, Opt_obj_role, Opt_obj_type,
226	Opt_subj_user, Opt_subj_role, Opt_subj_type,
227	Opt_func, Opt_mask, Opt_fsmagic, Opt_uid
 
228};
229
230static match_table_t policy_tokens = {
231	{Opt_measure, "measure"},
232	{Opt_dont_measure, "dont_measure"},
 
 
 
233	{Opt_obj_user, "obj_user=%s"},
234	{Opt_obj_role, "obj_role=%s"},
235	{Opt_obj_type, "obj_type=%s"},
236	{Opt_subj_user, "subj_user=%s"},
237	{Opt_subj_role, "subj_role=%s"},
238	{Opt_subj_type, "subj_type=%s"},
239	{Opt_func, "func=%s"},
240	{Opt_mask, "mask=%s"},
241	{Opt_fsmagic, "fsmagic=%s"},
 
242	{Opt_uid, "uid=%s"},
 
 
243	{Opt_err, NULL}
244};
245
246static int ima_lsm_rule_init(struct ima_measure_rule_entry *entry,
247			     char *args, int lsm_rule, int audit_type)
248{
249	int result;
250
251	if (entry->lsm[lsm_rule].rule)
252		return -EINVAL;
253
 
 
 
 
254	entry->lsm[lsm_rule].type = audit_type;
255	result = security_filter_rule_init(entry->lsm[lsm_rule].type,
256					   Audit_equal, args,
 
257					   &entry->lsm[lsm_rule].rule);
258	if (!entry->lsm[lsm_rule].rule)
 
259		return -EINVAL;
 
 
260	return result;
261}
262
263static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
264{
265	audit_log_format(ab, "%s=", key);
266	audit_log_untrustedstring(ab, value);
267	audit_log_format(ab, " ");
268}
269
270static int ima_parse_rule(char *rule, struct ima_measure_rule_entry *entry)
271{
272	struct audit_buffer *ab;
273	char *p;
274	int result = 0;
275
276	ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
277
278	entry->uid = -1;
 
279	entry->action = UNKNOWN;
280	while ((p = strsep(&rule, " \t")) != NULL) {
281		substring_t args[MAX_OPT_ARGS];
282		int token;
283		unsigned long lnum;
284
285		if (result < 0)
286			break;
287		if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
288			continue;
289		token = match_token(p, policy_tokens, args);
290		switch (token) {
291		case Opt_measure:
292			ima_log_string(ab, "action", "measure");
293
294			if (entry->action != UNKNOWN)
295				result = -EINVAL;
296
297			entry->action = MEASURE;
298			break;
299		case Opt_dont_measure:
300			ima_log_string(ab, "action", "dont_measure");
301
302			if (entry->action != UNKNOWN)
303				result = -EINVAL;
304
305			entry->action = DONT_MEASURE;
306			break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
307		case Opt_func:
308			ima_log_string(ab, "func", args[0].from);
309
310			if (entry->func)
311				result  = -EINVAL;
312
313			if (strcmp(args[0].from, "FILE_CHECK") == 0)
314				entry->func = FILE_CHECK;
315			/* PATH_CHECK is for backwards compat */
316			else if (strcmp(args[0].from, "PATH_CHECK") == 0)
317				entry->func = FILE_CHECK;
318			else if (strcmp(args[0].from, "FILE_MMAP") == 0)
319				entry->func = FILE_MMAP;
 
 
 
320			else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
321				entry->func = BPRM_CHECK;
322			else
323				result = -EINVAL;
324			if (!result)
325				entry->flags |= IMA_FUNC;
326			break;
327		case Opt_mask:
328			ima_log_string(ab, "mask", args[0].from);
329
330			if (entry->mask)
331				result = -EINVAL;
332
333			if ((strcmp(args[0].from, "MAY_EXEC")) == 0)
334				entry->mask = MAY_EXEC;
335			else if (strcmp(args[0].from, "MAY_WRITE") == 0)
336				entry->mask = MAY_WRITE;
337			else if (strcmp(args[0].from, "MAY_READ") == 0)
338				entry->mask = MAY_READ;
339			else if (strcmp(args[0].from, "MAY_APPEND") == 0)
340				entry->mask = MAY_APPEND;
341			else
342				result = -EINVAL;
343			if (!result)
344				entry->flags |= IMA_MASK;
345			break;
346		case Opt_fsmagic:
347			ima_log_string(ab, "fsmagic", args[0].from);
348
349			if (entry->fsmagic) {
350				result = -EINVAL;
351				break;
352			}
353
354			result = strict_strtoul(args[0].from, 16,
355						&entry->fsmagic);
356			if (!result)
357				entry->flags |= IMA_FSMAGIC;
358			break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
359		case Opt_uid:
360			ima_log_string(ab, "uid", args[0].from);
361
362			if (entry->uid != -1) {
363				result = -EINVAL;
364				break;
365			}
366
367			result = strict_strtoul(args[0].from, 10, &lnum);
368			if (!result) {
369				entry->uid = (uid_t) lnum;
370				if (entry->uid != lnum)
371					result = -EINVAL;
372				else
373					entry->flags |= IMA_UID;
374			}
375			break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
376		case Opt_obj_user:
377			ima_log_string(ab, "obj_user", args[0].from);
378			result = ima_lsm_rule_init(entry, args[0].from,
379						   LSM_OBJ_USER,
380						   AUDIT_OBJ_USER);
381			break;
382		case Opt_obj_role:
383			ima_log_string(ab, "obj_role", args[0].from);
384			result = ima_lsm_rule_init(entry, args[0].from,
385						   LSM_OBJ_ROLE,
386						   AUDIT_OBJ_ROLE);
387			break;
388		case Opt_obj_type:
389			ima_log_string(ab, "obj_type", args[0].from);
390			result = ima_lsm_rule_init(entry, args[0].from,
391						   LSM_OBJ_TYPE,
392						   AUDIT_OBJ_TYPE);
393			break;
394		case Opt_subj_user:
395			ima_log_string(ab, "subj_user", args[0].from);
396			result = ima_lsm_rule_init(entry, args[0].from,
397						   LSM_SUBJ_USER,
398						   AUDIT_SUBJ_USER);
399			break;
400		case Opt_subj_role:
401			ima_log_string(ab, "subj_role", args[0].from);
402			result = ima_lsm_rule_init(entry, args[0].from,
403						   LSM_SUBJ_ROLE,
404						   AUDIT_SUBJ_ROLE);
405			break;
406		case Opt_subj_type:
407			ima_log_string(ab, "subj_type", args[0].from);
408			result = ima_lsm_rule_init(entry, args[0].from,
409						   LSM_SUBJ_TYPE,
410						   AUDIT_SUBJ_TYPE);
411			break;
 
 
 
 
 
 
 
 
 
 
 
 
412		case Opt_err:
413			ima_log_string(ab, "UNKNOWN", p);
414			result = -EINVAL;
415			break;
416		}
417	}
418	if (!result && (entry->action == UNKNOWN))
419		result = -EINVAL;
420
 
421	audit_log_format(ab, "res=%d", !result);
422	audit_log_end(ab);
423	return result;
424}
425
426/**
427 * ima_parse_add_rule - add a rule to measure_policy_rules
428 * @rule - ima measurement policy rule
429 *
430 * Uses a mutex to protect the policy list from multiple concurrent writers.
431 * Returns the length of the rule parsed, an error code on failure
432 */
433ssize_t ima_parse_add_rule(char *rule)
434{
435	const char *op = "update_policy";
436	char *p;
437	struct ima_measure_rule_entry *entry;
438	ssize_t result, len;
439	int audit_info = 0;
440
441	/* Prevent installed policy from changing */
442	if (ima_measure != &measure_default_rules) {
443		integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
444				    NULL, op, "already exists",
445				    -EACCES, audit_info);
446		return -EACCES;
447	}
448
449	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
450	if (!entry) {
451		integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
452				    NULL, op, "-ENOMEM", -ENOMEM, audit_info);
453		return -ENOMEM;
454	}
455
456	INIT_LIST_HEAD(&entry->list);
457
458	p = strsep(&rule, "\n");
459	len = strlen(p) + 1;
460
461	if (*p == '#') {
462		kfree(entry);
463		return len;
464	}
465
466	result = ima_parse_rule(p, entry);
467	if (result) {
468		kfree(entry);
469		integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
470				    NULL, op, "invalid policy", result,
471				    audit_info);
472		return result;
473	}
474
475	mutex_lock(&ima_measure_mutex);
476	list_add_tail(&entry->list, &measure_policy_rules);
477	mutex_unlock(&ima_measure_mutex);
478
479	return len;
480}
481
482/* ima_delete_rules called to cleanup invalid policy */
483void ima_delete_rules(void)
484{
485	struct ima_measure_rule_entry *entry, *tmp;
 
 
 
 
 
 
486
487	mutex_lock(&ima_measure_mutex);
488	list_for_each_entry_safe(entry, tmp, &measure_policy_rules, list) {
489		list_del(&entry->list);
490		kfree(entry);
491	}
492	mutex_unlock(&ima_measure_mutex);
493}
  1/*
  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
 10 *	- initialize default measure policy rules
 11 *
 12 */
 13#include <linux/module.h>
 14#include <linux/list.h>
 15#include <linux/security.h>
 16#include <linux/magic.h>
 17#include <linux/parser.h>
 18#include <linux/slab.h>
 19#include <linux/genhd.h>
 20
 21#include "ima.h"
 22
 23/* flags definitions */
 24#define IMA_FUNC	0x0001
 25#define IMA_MASK	0x0002
 26#define IMA_FSMAGIC	0x0004
 27#define IMA_UID		0x0008
 28#define IMA_FOWNER	0x0010
 29#define IMA_FSUUID	0x0020
 30
 31#define UNKNOWN		0
 32#define MEASURE		0x0001	/* same as IMA_MEASURE */
 33#define DONT_MEASURE	0x0002
 34#define APPRAISE	0x0004	/* same as IMA_APPRAISE */
 35#define DONT_APPRAISE	0x0008
 36#define AUDIT		0x0040
 37
 38#define MAX_LSM_RULES 6
 39enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
 40	LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
 41};
 42
 43struct ima_rule_entry {
 44	struct list_head list;
 45	int action;
 46	unsigned int flags;
 47	enum ima_hooks func;
 48	int mask;
 49	unsigned long fsmagic;
 50	u8 fsuuid[16];
 51	kuid_t uid;
 52	kuid_t fowner;
 53	struct {
 54		void *rule;	/* LSM file metadata specific */
 55		void *args_p;	/* audit value */
 56		int type;	/* audit type */
 57	} lsm[MAX_LSM_RULES];
 58};
 59
 60/*
 61 * Without LSM specific knowledge, the default policy can only be
 62 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
 63 */
 64
 65/*
 66 * The minimum rule set to allow for full TCB coverage.  Measures all files
 67 * opened or mmap for exec and everything read by root.  Dangerous because
 68 * normal users can easily run the machine out of memory simply building
 69 * and running executables.
 70 */
 71static struct ima_rule_entry default_rules[] = {
 72	{.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
 73	{.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
 74	{.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
 75	{.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
 76	{.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
 77	{.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
 78	{.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
 79	{.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
 80	{.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
 81	 .flags = IMA_FUNC | IMA_MASK},
 82	{.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
 83	 .flags = IMA_FUNC | IMA_MASK},
 84	{.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ, .uid = GLOBAL_ROOT_UID,
 85	 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
 86	{.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
 87};
 88
 89static struct ima_rule_entry default_appraise_rules[] = {
 90	{.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
 91	{.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
 92	{.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
 93	{.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
 94	{.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
 95	{.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
 96	{.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
 97	{.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
 98	{.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
 99	{.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
100	{.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .flags = IMA_FOWNER},
101};
102
103static LIST_HEAD(ima_default_rules);
104static LIST_HEAD(ima_policy_rules);
105static struct list_head *ima_rules;
106
107static DEFINE_MUTEX(ima_rules_mutex);
108
109static bool ima_use_tcb __initdata;
110static int __init default_measure_policy_setup(char *str)
111{
112	ima_use_tcb = 1;
113	return 1;
114}
115__setup("ima_tcb", default_measure_policy_setup);
116
117static bool ima_use_appraise_tcb __initdata;
118static int __init default_appraise_policy_setup(char *str)
119{
120	ima_use_appraise_tcb = 1;
121	return 1;
122}
123__setup("ima_appraise_tcb", default_appraise_policy_setup);
124
125/*
126 * Although the IMA policy does not change, the LSM policy can be
127 * reloaded, leaving the IMA LSM based rules referring to the old,
128 * stale LSM policy.
129 *
130 * Update the IMA LSM based rules to reflect the reloaded LSM policy.
131 * We assume the rules still exist; and BUG_ON() if they don't.
132 */
133static void ima_lsm_update_rules(void)
134{
135	struct ima_rule_entry *entry, *tmp;
136	int result;
137	int i;
138
139	mutex_lock(&ima_rules_mutex);
140	list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
141		for (i = 0; i < MAX_LSM_RULES; i++) {
142			if (!entry->lsm[i].rule)
143				continue;
144			result = security_filter_rule_init(entry->lsm[i].type,
145							   Audit_equal,
146							   entry->lsm[i].args_p,
147							   &entry->lsm[i].rule);
148			BUG_ON(!entry->lsm[i].rule);
149		}
150	}
151	mutex_unlock(&ima_rules_mutex);
152}
153
154/**
155 * ima_match_rules - determine whether an inode matches the measure rule.
156 * @rule: a pointer to a rule
157 * @inode: a pointer to an inode
158 * @func: LIM hook identifier
159 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
160 *
161 * Returns true on rule match, false on failure.
162 */
163static bool ima_match_rules(struct ima_rule_entry *rule,
164			    struct inode *inode, enum ima_hooks func, int mask)
165{
166	struct task_struct *tsk = current;
167	const struct cred *cred = current_cred();
168	int i;
169
170	if ((rule->flags & IMA_FUNC) &&
171	    (rule->func != func && func != POST_SETATTR))
172		return false;
173	if ((rule->flags & IMA_MASK) &&
174	    (rule->mask != mask && func != POST_SETATTR))
175		return false;
176	if ((rule->flags & IMA_FSMAGIC)
177	    && rule->fsmagic != inode->i_sb->s_magic)
178		return false;
179	if ((rule->flags & IMA_FSUUID) &&
180	    memcmp(rule->fsuuid, inode->i_sb->s_uuid, sizeof(rule->fsuuid)))
181		return false;
182	if ((rule->flags & IMA_UID) && !uid_eq(rule->uid, cred->uid))
183		return false;
184	if ((rule->flags & IMA_FOWNER) && !uid_eq(rule->fowner, inode->i_uid))
185		return false;
186	for (i = 0; i < MAX_LSM_RULES; i++) {
187		int rc = 0;
188		u32 osid, sid;
189		int retried = 0;
190
191		if (!rule->lsm[i].rule)
192			continue;
193retry:
194		switch (i) {
195		case LSM_OBJ_USER:
196		case LSM_OBJ_ROLE:
197		case LSM_OBJ_TYPE:
198			security_inode_getsecid(inode, &osid);
199			rc = security_filter_rule_match(osid,
200							rule->lsm[i].type,
201							Audit_equal,
202							rule->lsm[i].rule,
203							NULL);
204			break;
205		case LSM_SUBJ_USER:
206		case LSM_SUBJ_ROLE:
207		case LSM_SUBJ_TYPE:
208			security_task_getsecid(tsk, &sid);
209			rc = security_filter_rule_match(sid,
210							rule->lsm[i].type,
211							Audit_equal,
212							rule->lsm[i].rule,
213							NULL);
214		default:
215			break;
216		}
217		if ((rc < 0) && (!retried)) {
218			retried = 1;
219			ima_lsm_update_rules();
220			goto retry;
221		}
222		if (!rc)
223			return false;
224	}
225	return true;
226}
227
228/*
229 * In addition to knowing that we need to appraise the file in general,
230 * we need to differentiate between calling hooks, for hook specific rules.
231 */
232static int get_subaction(struct ima_rule_entry *rule, int func)
233{
234	if (!(rule->flags & IMA_FUNC))
235		return IMA_FILE_APPRAISE;
236
237	switch (func) {
238	case MMAP_CHECK:
239		return IMA_MMAP_APPRAISE;
240	case BPRM_CHECK:
241		return IMA_BPRM_APPRAISE;
242	case MODULE_CHECK:
243		return IMA_MODULE_APPRAISE;
244	case FILE_CHECK:
245	default:
246		return IMA_FILE_APPRAISE;
247	}
248}
249
250/**
251 * ima_match_policy - decision based on LSM and other conditions
252 * @inode: pointer to an inode for which the policy decision is being made
253 * @func: IMA hook identifier
254 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
255 *
256 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
257 * conditions.
258 *
259 * (There is no need for locking when walking the policy list,
260 * as elements in the list are never deleted, nor does the list
261 * change.)
262 */
263int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
264		     int flags)
265{
266	struct ima_rule_entry *entry;
267	int action = 0, actmask = flags | (flags << 1);
268
269	list_for_each_entry(entry, ima_rules, list) {
270
271		if (!(entry->action & actmask))
272			continue;
273
274		if (!ima_match_rules(entry, inode, func, mask))
275			continue;
276
277		action |= entry->flags & IMA_ACTION_FLAGS;
 
278
279		action |= entry->action & IMA_DO_MASK;
280		if (entry->action & IMA_APPRAISE)
281			action |= get_subaction(entry, func);
282
283		if (entry->action & IMA_DO_MASK)
284			actmask &= ~(entry->action | entry->action << 1);
285		else
286			actmask &= ~(entry->action | entry->action >> 1);
287
288		if (!actmask)
289			break;
290	}
291
292	return action;
293}
294
295/**
296 * ima_init_policy - initialize the default measure rules.
297 *
298 * ima_rules points to either the ima_default_rules or the
299 * the new ima_policy_rules.
300 */
301void __init ima_init_policy(void)
302{
303	int i, measure_entries, appraise_entries;
304
305	/* if !ima_use_tcb set entries = 0 so we load NO default rules */
306	measure_entries = ima_use_tcb ? ARRAY_SIZE(default_rules) : 0;
307	appraise_entries = ima_use_appraise_tcb ?
308			 ARRAY_SIZE(default_appraise_rules) : 0;
309
310	for (i = 0; i < measure_entries + appraise_entries; i++) {
311		if (i < measure_entries)
312			list_add_tail(&default_rules[i].list,
313				      &ima_default_rules);
314		else {
315			int j = i - measure_entries;
316
317			list_add_tail(&default_appraise_rules[j].list,
318				      &ima_default_rules);
319		}
320	}
321
322	ima_rules = &ima_default_rules;
323}
324
325/**
326 * ima_update_policy - update default_rules with new measure rules
327 *
328 * Called on file .release to update the default rules with a complete new
329 * policy.  Once updated, the policy is locked, no additional rules can be
330 * added to the policy.
331 */
332void ima_update_policy(void)
333{
334	static const char op[] = "policy_update";
335	const char *cause = "already exists";
336	int result = 1;
337	int audit_info = 0;
338
339	if (ima_rules == &ima_default_rules) {
340		ima_rules = &ima_policy_rules;
341		cause = "complete";
342		result = 0;
343	}
344	integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
345			    NULL, op, cause, result, audit_info);
346}
347
348enum {
349	Opt_err = -1,
350	Opt_measure = 1, Opt_dont_measure,
351	Opt_appraise, Opt_dont_appraise,
352	Opt_audit,
353	Opt_obj_user, Opt_obj_role, Opt_obj_type,
354	Opt_subj_user, Opt_subj_role, Opt_subj_type,
355	Opt_func, Opt_mask, Opt_fsmagic, Opt_uid, Opt_fowner,
356	Opt_appraise_type, Opt_fsuuid
357};
358
359static match_table_t policy_tokens = {
360	{Opt_measure, "measure"},
361	{Opt_dont_measure, "dont_measure"},
362	{Opt_appraise, "appraise"},
363	{Opt_dont_appraise, "dont_appraise"},
364	{Opt_audit, "audit"},
365	{Opt_obj_user, "obj_user=%s"},
366	{Opt_obj_role, "obj_role=%s"},
367	{Opt_obj_type, "obj_type=%s"},
368	{Opt_subj_user, "subj_user=%s"},
369	{Opt_subj_role, "subj_role=%s"},
370	{Opt_subj_type, "subj_type=%s"},
371	{Opt_func, "func=%s"},
372	{Opt_mask, "mask=%s"},
373	{Opt_fsmagic, "fsmagic=%s"},
374	{Opt_fsuuid, "fsuuid=%s"},
375	{Opt_uid, "uid=%s"},
376	{Opt_fowner, "fowner=%s"},
377	{Opt_appraise_type, "appraise_type=%s"},
378	{Opt_err, NULL}
379};
380
381static int ima_lsm_rule_init(struct ima_rule_entry *entry,
382			     substring_t *args, int lsm_rule, int audit_type)
383{
384	int result;
385
386	if (entry->lsm[lsm_rule].rule)
387		return -EINVAL;
388
389	entry->lsm[lsm_rule].args_p = match_strdup(args);
390	if (!entry->lsm[lsm_rule].args_p)
391		return -ENOMEM;
392
393	entry->lsm[lsm_rule].type = audit_type;
394	result = security_filter_rule_init(entry->lsm[lsm_rule].type,
395					   Audit_equal,
396					   entry->lsm[lsm_rule].args_p,
397					   &entry->lsm[lsm_rule].rule);
398	if (!entry->lsm[lsm_rule].rule) {
399		kfree(entry->lsm[lsm_rule].args_p);
400		return -EINVAL;
401	}
402
403	return result;
404}
405
406static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
407{
408	audit_log_format(ab, "%s=", key);
409	audit_log_untrustedstring(ab, value);
410	audit_log_format(ab, " ");
411}
412
413static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
414{
415	struct audit_buffer *ab;
416	char *p;
417	int result = 0;
418
419	ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
420
421	entry->uid = INVALID_UID;
422	entry->fowner = INVALID_UID;
423	entry->action = UNKNOWN;
424	while ((p = strsep(&rule, " \t")) != NULL) {
425		substring_t args[MAX_OPT_ARGS];
426		int token;
427		unsigned long lnum;
428
429		if (result < 0)
430			break;
431		if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
432			continue;
433		token = match_token(p, policy_tokens, args);
434		switch (token) {
435		case Opt_measure:
436			ima_log_string(ab, "action", "measure");
437
438			if (entry->action != UNKNOWN)
439				result = -EINVAL;
440
441			entry->action = MEASURE;
442			break;
443		case Opt_dont_measure:
444			ima_log_string(ab, "action", "dont_measure");
445
446			if (entry->action != UNKNOWN)
447				result = -EINVAL;
448
449			entry->action = DONT_MEASURE;
450			break;
451		case Opt_appraise:
452			ima_log_string(ab, "action", "appraise");
453
454			if (entry->action != UNKNOWN)
455				result = -EINVAL;
456
457			entry->action = APPRAISE;
458			break;
459		case Opt_dont_appraise:
460			ima_log_string(ab, "action", "dont_appraise");
461
462			if (entry->action != UNKNOWN)
463				result = -EINVAL;
464
465			entry->action = DONT_APPRAISE;
466			break;
467		case Opt_audit:
468			ima_log_string(ab, "action", "audit");
469
470			if (entry->action != UNKNOWN)
471				result = -EINVAL;
472
473			entry->action = AUDIT;
474			break;
475		case Opt_func:
476			ima_log_string(ab, "func", args[0].from);
477
478			if (entry->func)
479				result = -EINVAL;
480
481			if (strcmp(args[0].from, "FILE_CHECK") == 0)
482				entry->func = FILE_CHECK;
483			/* PATH_CHECK is for backwards compat */
484			else if (strcmp(args[0].from, "PATH_CHECK") == 0)
485				entry->func = FILE_CHECK;
486			else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
487				entry->func = MODULE_CHECK;
488			else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
489				|| (strcmp(args[0].from, "MMAP_CHECK") == 0))
490				entry->func = MMAP_CHECK;
491			else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
492				entry->func = BPRM_CHECK;
493			else
494				result = -EINVAL;
495			if (!result)
496				entry->flags |= IMA_FUNC;
497			break;
498		case Opt_mask:
499			ima_log_string(ab, "mask", args[0].from);
500
501			if (entry->mask)
502				result = -EINVAL;
503
504			if ((strcmp(args[0].from, "MAY_EXEC")) == 0)
505				entry->mask = MAY_EXEC;
506			else if (strcmp(args[0].from, "MAY_WRITE") == 0)
507				entry->mask = MAY_WRITE;
508			else if (strcmp(args[0].from, "MAY_READ") == 0)
509				entry->mask = MAY_READ;
510			else if (strcmp(args[0].from, "MAY_APPEND") == 0)
511				entry->mask = MAY_APPEND;
512			else
513				result = -EINVAL;
514			if (!result)
515				entry->flags |= IMA_MASK;
516			break;
517		case Opt_fsmagic:
518			ima_log_string(ab, "fsmagic", args[0].from);
519
520			if (entry->fsmagic) {
521				result = -EINVAL;
522				break;
523			}
524
525			result = kstrtoul(args[0].from, 16, &entry->fsmagic);
 
526			if (!result)
527				entry->flags |= IMA_FSMAGIC;
528			break;
529		case Opt_fsuuid:
530			ima_log_string(ab, "fsuuid", args[0].from);
531
532			if (memchr_inv(entry->fsuuid, 0x00,
533				       sizeof(entry->fsuuid))) {
534				result = -EINVAL;
535				break;
536			}
537
538			result = blk_part_pack_uuid(args[0].from,
539						    entry->fsuuid);
540			if (!result)
541				entry->flags |= IMA_FSUUID;
542			break;
543		case Opt_uid:
544			ima_log_string(ab, "uid", args[0].from);
545
546			if (uid_valid(entry->uid)) {
547				result = -EINVAL;
548				break;
549			}
550
551			result = kstrtoul(args[0].from, 10, &lnum);
552			if (!result) {
553				entry->uid = make_kuid(current_user_ns(), (uid_t)lnum);
554				if (!uid_valid(entry->uid) || (((uid_t)lnum) != lnum))
555					result = -EINVAL;
556				else
557					entry->flags |= IMA_UID;
558			}
559			break;
560		case Opt_fowner:
561			ima_log_string(ab, "fowner", args[0].from);
562
563			if (uid_valid(entry->fowner)) {
564				result = -EINVAL;
565				break;
566			}
567
568			result = kstrtoul(args[0].from, 10, &lnum);
569			if (!result) {
570				entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
571				if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
572					result = -EINVAL;
573				else
574					entry->flags |= IMA_FOWNER;
575			}
576			break;
577		case Opt_obj_user:
578			ima_log_string(ab, "obj_user", args[0].from);
579			result = ima_lsm_rule_init(entry, args,
580						   LSM_OBJ_USER,
581						   AUDIT_OBJ_USER);
582			break;
583		case Opt_obj_role:
584			ima_log_string(ab, "obj_role", args[0].from);
585			result = ima_lsm_rule_init(entry, args,
586						   LSM_OBJ_ROLE,
587						   AUDIT_OBJ_ROLE);
588			break;
589		case Opt_obj_type:
590			ima_log_string(ab, "obj_type", args[0].from);
591			result = ima_lsm_rule_init(entry, args,
592						   LSM_OBJ_TYPE,
593						   AUDIT_OBJ_TYPE);
594			break;
595		case Opt_subj_user:
596			ima_log_string(ab, "subj_user", args[0].from);
597			result = ima_lsm_rule_init(entry, args,
598						   LSM_SUBJ_USER,
599						   AUDIT_SUBJ_USER);
600			break;
601		case Opt_subj_role:
602			ima_log_string(ab, "subj_role", args[0].from);
603			result = ima_lsm_rule_init(entry, args,
604						   LSM_SUBJ_ROLE,
605						   AUDIT_SUBJ_ROLE);
606			break;
607		case Opt_subj_type:
608			ima_log_string(ab, "subj_type", args[0].from);
609			result = ima_lsm_rule_init(entry, args,
610						   LSM_SUBJ_TYPE,
611						   AUDIT_SUBJ_TYPE);
612			break;
613		case Opt_appraise_type:
614			if (entry->action != APPRAISE) {
615				result = -EINVAL;
616				break;
617			}
618
619			ima_log_string(ab, "appraise_type", args[0].from);
620			if ((strcmp(args[0].from, "imasig")) == 0)
621				entry->flags |= IMA_DIGSIG_REQUIRED;
622			else
623				result = -EINVAL;
624			break;
625		case Opt_err:
626			ima_log_string(ab, "UNKNOWN", p);
627			result = -EINVAL;
628			break;
629		}
630	}
631	if (!result && (entry->action == UNKNOWN))
632		result = -EINVAL;
633	else if (entry->func == MODULE_CHECK)
634		ima_appraise |= IMA_APPRAISE_MODULES;
635	audit_log_format(ab, "res=%d", !result);
636	audit_log_end(ab);
637	return result;
638}
639
640/**
641 * ima_parse_add_rule - add a rule to ima_policy_rules
642 * @rule - ima measurement policy rule
643 *
644 * Uses a mutex to protect the policy list from multiple concurrent writers.
645 * Returns the length of the rule parsed, an error code on failure
646 */
647ssize_t ima_parse_add_rule(char *rule)
648{
649	static const char op[] = "update_policy";
650	char *p;
651	struct ima_rule_entry *entry;
652	ssize_t result, len;
653	int audit_info = 0;
654
655	/* Prevent installed policy from changing */
656	if (ima_rules != &ima_default_rules) {
657		integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
658				    NULL, op, "already exists",
659				    -EACCES, audit_info);
660		return -EACCES;
661	}
662
663	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
664	if (!entry) {
665		integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
666				    NULL, op, "-ENOMEM", -ENOMEM, audit_info);
667		return -ENOMEM;
668	}
669
670	INIT_LIST_HEAD(&entry->list);
671
672	p = strsep(&rule, "\n");
673	len = strlen(p) + 1;
674
675	if (*p == '#') {
676		kfree(entry);
677		return len;
678	}
679
680	result = ima_parse_rule(p, entry);
681	if (result) {
682		kfree(entry);
683		integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
684				    NULL, op, "invalid policy", result,
685				    audit_info);
686		return result;
687	}
688
689	mutex_lock(&ima_rules_mutex);
690	list_add_tail(&entry->list, &ima_policy_rules);
691	mutex_unlock(&ima_rules_mutex);
692
693	return len;
694}
695
696/* ima_delete_rules called to cleanup invalid policy */
697void ima_delete_rules(void)
698{
699	struct ima_rule_entry *entry, *tmp;
700	int i;
701
702	mutex_lock(&ima_rules_mutex);
703	list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
704		for (i = 0; i < MAX_LSM_RULES; i++)
705			kfree(entry->lsm[i].args_p);
706
 
 
707		list_del(&entry->list);
708		kfree(entry);
709	}
710	mutex_unlock(&ima_rules_mutex);
711}