2 * Copyright (C) 2008 IBM Corporation
3 * Author: Mimi Zohar <zohar@us.ibm.com>
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.
10 * - initialize default measure policy rules
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>
22 /* flags definitions */
23 #define IMA_FUNC 0x0001
24 #define IMA_MASK 0x0002
25 #define IMA_FSMAGIC 0x0004
26 #define IMA_UID 0x0008
28 enum ima_action
{ UNKNOWN
= -1, DONT_MEASURE
= 0, MEASURE
};
30 #define MAX_LSM_RULES 6
31 enum lsm_rule_types
{ LSM_OBJ_USER
, LSM_OBJ_ROLE
, LSM_OBJ_TYPE
,
32 LSM_SUBJ_USER
, LSM_SUBJ_ROLE
, LSM_SUBJ_TYPE
35 struct ima_measure_rule_entry
{
36 struct list_head list
;
37 enum ima_action action
;
41 unsigned long fsmagic
;
44 void *rule
; /* LSM file metadata specific */
45 int type
; /* audit type */
50 * Without LSM specific knowledge, the default policy can only be
51 * written in terms of .action, .func, .mask, .fsmagic, and .uid
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.
60 static 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
= SECURITYFS_MAGIC
,.flags
= IMA_FSMAGIC
},
66 {.action
= DONT_MEASURE
,.fsmagic
= SELINUX_MAGIC
,.flags
= IMA_FSMAGIC
},
67 {.action
= MEASURE
,.func
= FILE_MMAP
,.mask
= MAY_EXEC
,
68 .flags
= IMA_FUNC
| IMA_MASK
},
69 {.action
= MEASURE
,.func
= BPRM_CHECK
,.mask
= MAY_EXEC
,
70 .flags
= IMA_FUNC
| IMA_MASK
},
71 {.action
= MEASURE
,.func
= FILE_CHECK
,.mask
= MAY_READ
,.uid
= 0,
72 .flags
= IMA_FUNC
| IMA_MASK
| IMA_UID
},
75 static LIST_HEAD(measure_default_rules
);
76 static LIST_HEAD(measure_policy_rules
);
77 static struct list_head
*ima_measure
;
79 static DEFINE_MUTEX(ima_measure_mutex
);
81 static bool ima_use_tcb __initdata
;
82 static int __init
default_policy_setup(char *str
)
87 __setup("ima_tcb", default_policy_setup
);
90 * ima_match_rules - determine whether an inode matches the measure rule.
91 * @rule: a pointer to a rule
92 * @inode: a pointer to an inode
93 * @func: LIM hook identifier
94 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
96 * Returns true on rule match, false on failure.
98 static bool ima_match_rules(struct ima_measure_rule_entry
*rule
,
99 struct inode
*inode
, enum ima_hooks func
, int mask
)
101 struct task_struct
*tsk
= current
;
104 if ((rule
->flags
& IMA_FUNC
) && rule
->func
!= func
)
106 if ((rule
->flags
& IMA_MASK
) && rule
->mask
!= mask
)
108 if ((rule
->flags
& IMA_FSMAGIC
)
109 && rule
->fsmagic
!= inode
->i_sb
->s_magic
)
111 if ((rule
->flags
& IMA_UID
) && rule
->uid
!= tsk
->cred
->uid
)
113 for (i
= 0; i
< MAX_LSM_RULES
; i
++) {
117 if (!rule
->lsm
[i
].rule
)
124 security_inode_getsecid(inode
, &osid
);
125 rc
= security_filter_rule_match(osid
,
134 security_task_getsecid(tsk
, &sid
);
135 rc
= security_filter_rule_match(sid
,
150 * ima_match_policy - decision based on LSM and other conditions
151 * @inode: pointer to an inode for which the policy decision is being made
152 * @func: IMA hook identifier
153 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
155 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
158 * (There is no need for locking when walking the policy list,
159 * as elements in the list are never deleted, nor does the list
162 int ima_match_policy(struct inode
*inode
, enum ima_hooks func
, int mask
)
164 struct ima_measure_rule_entry
*entry
;
166 list_for_each_entry(entry
, ima_measure
, list
) {
169 rc
= ima_match_rules(entry
, inode
, func
, mask
);
171 return entry
->action
;
177 * ima_init_policy - initialize the default measure rules.
179 * ima_measure points to either the measure_default_rules or the
180 * the new measure_policy_rules.
182 void __init
ima_init_policy(void)
186 /* if !ima_use_tcb set entries = 0 so we load NO default rules */
188 entries
= ARRAY_SIZE(default_rules
);
192 for (i
= 0; i
< entries
; i
++)
193 list_add_tail(&default_rules
[i
].list
, &measure_default_rules
);
194 ima_measure
= &measure_default_rules
;
198 * ima_update_policy - update default_rules with new measure rules
200 * Called on file .release to update the default rules with a complete new
201 * policy. Once updated, the policy is locked, no additional rules can be
202 * added to the policy.
204 void ima_update_policy(void)
206 const char *op
= "policy_update";
207 const char *cause
= "already exists";
211 if (ima_measure
== &measure_default_rules
) {
212 ima_measure
= &measure_policy_rules
;
216 integrity_audit_msg(AUDIT_INTEGRITY_STATUS
, NULL
,
217 NULL
, op
, cause
, result
, audit_info
);
222 Opt_measure
= 1, Opt_dont_measure
,
223 Opt_obj_user
, Opt_obj_role
, Opt_obj_type
,
224 Opt_subj_user
, Opt_subj_role
, Opt_subj_type
,
225 Opt_func
, Opt_mask
, Opt_fsmagic
, Opt_uid
228 static match_table_t policy_tokens
= {
229 {Opt_measure
, "measure"},
230 {Opt_dont_measure
, "dont_measure"},
231 {Opt_obj_user
, "obj_user=%s"},
232 {Opt_obj_role
, "obj_role=%s"},
233 {Opt_obj_type
, "obj_type=%s"},
234 {Opt_subj_user
, "subj_user=%s"},
235 {Opt_subj_role
, "subj_role=%s"},
236 {Opt_subj_type
, "subj_type=%s"},
237 {Opt_func
, "func=%s"},
238 {Opt_mask
, "mask=%s"},
239 {Opt_fsmagic
, "fsmagic=%s"},
244 static int ima_lsm_rule_init(struct ima_measure_rule_entry
*entry
,
245 char *args
, int lsm_rule
, int audit_type
)
249 entry
->lsm
[lsm_rule
].type
= audit_type
;
250 result
= security_filter_rule_init(entry
->lsm
[lsm_rule
].type
,
252 &entry
->lsm
[lsm_rule
].rule
);
256 static int ima_parse_rule(char *rule
, struct ima_measure_rule_entry
*entry
)
258 struct audit_buffer
*ab
;
262 ab
= audit_log_start(NULL
, GFP_KERNEL
, AUDIT_INTEGRITY_RULE
);
265 while ((p
= strsep(&rule
, " \n")) != NULL
) {
266 substring_t args
[MAX_OPT_ARGS
];
274 token
= match_token(p
, policy_tokens
, args
);
277 audit_log_format(ab
, "%s ", "measure");
278 entry
->action
= MEASURE
;
280 case Opt_dont_measure
:
281 audit_log_format(ab
, "%s ", "dont_measure");
282 entry
->action
= DONT_MEASURE
;
285 audit_log_format(ab
, "func=%s ", args
[0].from
);
286 if (strcmp(args
[0].from
, "FILE_CHECK") == 0)
287 entry
->func
= FILE_CHECK
;
288 /* PATH_CHECK is for backwards compat */
289 else if (strcmp(args
[0].from
, "PATH_CHECK") == 0)
290 entry
->func
= FILE_CHECK
;
291 else if (strcmp(args
[0].from
, "FILE_MMAP") == 0)
292 entry
->func
= FILE_MMAP
;
293 else if (strcmp(args
[0].from
, "BPRM_CHECK") == 0)
294 entry
->func
= BPRM_CHECK
;
298 entry
->flags
|= IMA_FUNC
;
301 audit_log_format(ab
, "mask=%s ", args
[0].from
);
302 if ((strcmp(args
[0].from
, "MAY_EXEC")) == 0)
303 entry
->mask
= MAY_EXEC
;
304 else if (strcmp(args
[0].from
, "MAY_WRITE") == 0)
305 entry
->mask
= MAY_WRITE
;
306 else if (strcmp(args
[0].from
, "MAY_READ") == 0)
307 entry
->mask
= MAY_READ
;
308 else if (strcmp(args
[0].from
, "MAY_APPEND") == 0)
309 entry
->mask
= MAY_APPEND
;
313 entry
->flags
|= IMA_MASK
;
316 audit_log_format(ab
, "fsmagic=%s ", args
[0].from
);
317 result
= strict_strtoul(args
[0].from
, 16,
320 entry
->flags
|= IMA_FSMAGIC
;
323 audit_log_format(ab
, "uid=%s ", args
[0].from
);
324 result
= strict_strtoul(args
[0].from
, 10, &lnum
);
326 entry
->uid
= (uid_t
) lnum
;
327 if (entry
->uid
!= lnum
)
330 entry
->flags
|= IMA_UID
;
334 audit_log_format(ab
, "obj_user=%s ", args
[0].from
);
335 result
= ima_lsm_rule_init(entry
, args
[0].from
,
340 audit_log_format(ab
, "obj_role=%s ", args
[0].from
);
341 result
= ima_lsm_rule_init(entry
, args
[0].from
,
346 audit_log_format(ab
, "obj_type=%s ", args
[0].from
);
347 result
= ima_lsm_rule_init(entry
, args
[0].from
,
352 audit_log_format(ab
, "subj_user=%s ", args
[0].from
);
353 result
= ima_lsm_rule_init(entry
, args
[0].from
,
358 audit_log_format(ab
, "subj_role=%s ", args
[0].from
);
359 result
= ima_lsm_rule_init(entry
, args
[0].from
,
364 audit_log_format(ab
, "subj_type=%s ", args
[0].from
);
365 result
= ima_lsm_rule_init(entry
, args
[0].from
,
370 audit_log_format(ab
, "UNKNOWN=%s ", p
);
374 if (entry
->action
== UNKNOWN
)
377 audit_log_format(ab
, "res=%d", !result
? 0 : 1);
383 * ima_parse_add_rule - add a rule to measure_policy_rules
384 * @rule - ima measurement policy rule
386 * Uses a mutex to protect the policy list from multiple concurrent writers.
387 * Returns 0 on success, an error code on failure.
389 int ima_parse_add_rule(char *rule
)
391 const char *op
= "update_policy";
392 struct ima_measure_rule_entry
*entry
;
396 /* Prevent installed policy from changing */
397 if (ima_measure
!= &measure_default_rules
) {
398 integrity_audit_msg(AUDIT_INTEGRITY_STATUS
, NULL
,
399 NULL
, op
, "already exists",
400 -EACCES
, audit_info
);
404 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
406 integrity_audit_msg(AUDIT_INTEGRITY_STATUS
, NULL
,
407 NULL
, op
, "-ENOMEM", -ENOMEM
, audit_info
);
411 INIT_LIST_HEAD(&entry
->list
);
413 result
= ima_parse_rule(rule
, entry
);
415 mutex_lock(&ima_measure_mutex
);
416 list_add_tail(&entry
->list
, &measure_policy_rules
);
417 mutex_unlock(&ima_measure_mutex
);
420 integrity_audit_msg(AUDIT_INTEGRITY_STATUS
, NULL
,
421 NULL
, op
, "invalid policy", result
,
427 /* ima_delete_rules called to cleanup invalid policy */
428 void ima_delete_rules(void)
430 struct ima_measure_rule_entry
*entry
, *tmp
;
432 mutex_lock(&ima_measure_mutex
);
433 list_for_each_entry_safe(entry
, tmp
, &measure_policy_rules
, list
) {
434 list_del(&entry
->list
);
437 mutex_unlock(&ima_measure_mutex
);