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>
21 /* flags definitions */
22 #define IMA_FUNC 0x0001
23 #define IMA_MASK 0x0002
24 #define IMA_FSMAGIC 0x0004
25 #define IMA_UID 0x0008
27 enum ima_action
{ UNKNOWN
= -1, DONT_MEASURE
= 0, MEASURE
};
29 #define MAX_LSM_RULES 6
30 enum lsm_rule_types
{ LSM_OBJ_USER
, LSM_OBJ_ROLE
, LSM_OBJ_TYPE
,
31 LSM_SUBJ_USER
, LSM_SUBJ_ROLE
, LSM_SUBJ_TYPE
34 struct ima_measure_rule_entry
{
35 struct list_head list
;
36 enum ima_action action
;
40 unsigned long fsmagic
;
43 void *rule
; /* LSM file metadata specific */
44 int type
; /* audit type */
49 * Without LSM specific knowledge, the default policy can only be
50 * written in terms of .action, .func, .mask, .fsmagic, and .uid
54 * The minimum rule set to allow for full TCB coverage. Measures all files
55 * opened or mmap for exec and everything read by root. Dangerous because
56 * normal users can easily run the machine out of memory simply building
57 * and running executables.
59 static struct ima_measure_rule_entry default_rules
[] = {
60 {.action
= DONT_MEASURE
,.fsmagic
= PROC_SUPER_MAGIC
,.flags
= IMA_FSMAGIC
},
61 {.action
= DONT_MEASURE
,.fsmagic
= SYSFS_MAGIC
,.flags
= IMA_FSMAGIC
},
62 {.action
= DONT_MEASURE
,.fsmagic
= DEBUGFS_MAGIC
,.flags
= IMA_FSMAGIC
},
63 {.action
= DONT_MEASURE
,.fsmagic
= TMPFS_MAGIC
,.flags
= IMA_FSMAGIC
},
64 {.action
= DONT_MEASURE
,.fsmagic
= SECURITYFS_MAGIC
,.flags
= IMA_FSMAGIC
},
65 {.action
= DONT_MEASURE
,.fsmagic
= SELINUX_MAGIC
,.flags
= IMA_FSMAGIC
},
66 {.action
= MEASURE
,.func
= FILE_MMAP
,.mask
= MAY_EXEC
,
67 .flags
= IMA_FUNC
| IMA_MASK
},
68 {.action
= MEASURE
,.func
= BPRM_CHECK
,.mask
= MAY_EXEC
,
69 .flags
= IMA_FUNC
| IMA_MASK
},
70 {.action
= MEASURE
,.func
= PATH_CHECK
,.mask
= MAY_READ
,.uid
= 0,
71 .flags
= IMA_FUNC
| IMA_MASK
| IMA_UID
},
74 static LIST_HEAD(measure_default_rules
);
75 static LIST_HEAD(measure_policy_rules
);
76 static struct list_head
*ima_measure
;
78 static DEFINE_MUTEX(ima_measure_mutex
);
80 static bool ima_use_tcb __initdata
;
81 static int __init
default_policy_setup(char *str
)
86 __setup("ima_tcb", default_policy_setup
);
89 * ima_match_rules - determine whether an inode matches the measure rule.
90 * @rule: a pointer to a rule
91 * @inode: a pointer to an inode
92 * @func: LIM hook identifier
93 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
95 * Returns true on rule match, false on failure.
97 static bool ima_match_rules(struct ima_measure_rule_entry
*rule
,
98 struct inode
*inode
, enum ima_hooks func
, int mask
)
100 struct task_struct
*tsk
= current
;
103 if ((rule
->flags
& IMA_FUNC
) && rule
->func
!= func
)
105 if ((rule
->flags
& IMA_MASK
) && rule
->mask
!= mask
)
107 if ((rule
->flags
& IMA_FSMAGIC
)
108 && rule
->fsmagic
!= inode
->i_sb
->s_magic
)
110 if ((rule
->flags
& IMA_UID
) && rule
->uid
!= tsk
->cred
->uid
)
112 for (i
= 0; i
< MAX_LSM_RULES
; i
++) {
116 if (!rule
->lsm
[i
].rule
)
123 security_inode_getsecid(inode
, &osid
);
124 rc
= security_filter_rule_match(osid
,
133 security_task_getsecid(tsk
, &sid
);
134 rc
= security_filter_rule_match(sid
,
149 * ima_match_policy - decision based on LSM and other conditions
150 * @inode: pointer to an inode for which the policy decision is being made
151 * @func: IMA hook identifier
152 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
154 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
157 * (There is no need for locking when walking the policy list,
158 * as elements in the list are never deleted, nor does the list
161 int ima_match_policy(struct inode
*inode
, enum ima_hooks func
, int mask
)
163 struct ima_measure_rule_entry
*entry
;
165 list_for_each_entry(entry
, ima_measure
, list
) {
168 rc
= ima_match_rules(entry
, inode
, func
, mask
);
170 return entry
->action
;
176 * ima_init_policy - initialize the default measure rules.
178 * ima_measure points to either the measure_default_rules or the
179 * the new measure_policy_rules.
181 void __init
ima_init_policy(void)
185 /* if !ima_use_tcb set entries = 0 so we load NO default rules */
187 entries
= ARRAY_SIZE(default_rules
);
191 for (i
= 0; i
< entries
; i
++)
192 list_add_tail(&default_rules
[i
].list
, &measure_default_rules
);
193 ima_measure
= &measure_default_rules
;
197 * ima_update_policy - update default_rules with new measure rules
199 * Called on file .release to update the default rules with a complete new
200 * policy. Once updated, the policy is locked, no additional rules can be
201 * added to the policy.
203 void ima_update_policy(void)
205 const char *op
= "policy_update";
206 const char *cause
= "already exists";
210 if (ima_measure
== &measure_default_rules
) {
211 ima_measure
= &measure_policy_rules
;
215 integrity_audit_msg(AUDIT_INTEGRITY_STATUS
, NULL
,
216 NULL
, op
, cause
, result
, audit_info
);
221 Opt_measure
= 1, Opt_dont_measure
,
222 Opt_obj_user
, Opt_obj_role
, Opt_obj_type
,
223 Opt_subj_user
, Opt_subj_role
, Opt_subj_type
,
224 Opt_func
, Opt_mask
, Opt_fsmagic
, Opt_uid
227 static match_table_t policy_tokens
= {
228 {Opt_measure
, "measure"},
229 {Opt_dont_measure
, "dont_measure"},
230 {Opt_obj_user
, "obj_user=%s"},
231 {Opt_obj_role
, "obj_role=%s"},
232 {Opt_obj_type
, "obj_type=%s"},
233 {Opt_subj_user
, "subj_user=%s"},
234 {Opt_subj_role
, "subj_role=%s"},
235 {Opt_subj_type
, "subj_type=%s"},
236 {Opt_func
, "func=%s"},
237 {Opt_mask
, "mask=%s"},
238 {Opt_fsmagic
, "fsmagic=%s"},
243 static int ima_lsm_rule_init(struct ima_measure_rule_entry
*entry
,
244 char *args
, int lsm_rule
, int audit_type
)
248 entry
->lsm
[lsm_rule
].type
= audit_type
;
249 result
= security_filter_rule_init(entry
->lsm
[lsm_rule
].type
,
251 &entry
->lsm
[lsm_rule
].rule
);
255 static int ima_parse_rule(char *rule
, struct ima_measure_rule_entry
*entry
)
257 struct audit_buffer
*ab
;
261 ab
= audit_log_start(NULL
, GFP_KERNEL
, AUDIT_INTEGRITY_RULE
);
264 while ((p
= strsep(&rule
, " \n")) != NULL
) {
265 substring_t args
[MAX_OPT_ARGS
];
273 token
= match_token(p
, policy_tokens
, args
);
276 audit_log_format(ab
, "%s ", "measure");
277 entry
->action
= MEASURE
;
279 case Opt_dont_measure
:
280 audit_log_format(ab
, "%s ", "dont_measure");
281 entry
->action
= DONT_MEASURE
;
284 audit_log_format(ab
, "func=%s ", args
[0].from
);
285 if (strcmp(args
[0].from
, "PATH_CHECK") == 0)
286 entry
->func
= PATH_CHECK
;
287 else if (strcmp(args
[0].from
, "FILE_MMAP") == 0)
288 entry
->func
= FILE_MMAP
;
289 else if (strcmp(args
[0].from
, "BPRM_CHECK") == 0)
290 entry
->func
= BPRM_CHECK
;
294 entry
->flags
|= IMA_FUNC
;
297 audit_log_format(ab
, "mask=%s ", args
[0].from
);
298 if ((strcmp(args
[0].from
, "MAY_EXEC")) == 0)
299 entry
->mask
= MAY_EXEC
;
300 else if (strcmp(args
[0].from
, "MAY_WRITE") == 0)
301 entry
->mask
= MAY_WRITE
;
302 else if (strcmp(args
[0].from
, "MAY_READ") == 0)
303 entry
->mask
= MAY_READ
;
304 else if (strcmp(args
[0].from
, "MAY_APPEND") == 0)
305 entry
->mask
= MAY_APPEND
;
309 entry
->flags
|= IMA_MASK
;
312 audit_log_format(ab
, "fsmagic=%s ", args
[0].from
);
313 result
= strict_strtoul(args
[0].from
, 16,
316 entry
->flags
|= IMA_FSMAGIC
;
319 audit_log_format(ab
, "uid=%s ", args
[0].from
);
320 result
= strict_strtoul(args
[0].from
, 10, &lnum
);
322 entry
->uid
= (uid_t
) lnum
;
323 if (entry
->uid
!= lnum
)
326 entry
->flags
|= IMA_UID
;
330 audit_log_format(ab
, "obj_user=%s ", args
[0].from
);
331 result
= ima_lsm_rule_init(entry
, args
[0].from
,
336 audit_log_format(ab
, "obj_role=%s ", args
[0].from
);
337 result
= ima_lsm_rule_init(entry
, args
[0].from
,
342 audit_log_format(ab
, "obj_type=%s ", args
[0].from
);
343 result
= ima_lsm_rule_init(entry
, args
[0].from
,
348 audit_log_format(ab
, "subj_user=%s ", args
[0].from
);
349 result
= ima_lsm_rule_init(entry
, args
[0].from
,
354 audit_log_format(ab
, "subj_role=%s ", args
[0].from
);
355 result
= ima_lsm_rule_init(entry
, args
[0].from
,
360 audit_log_format(ab
, "subj_type=%s ", args
[0].from
);
361 result
= ima_lsm_rule_init(entry
, args
[0].from
,
366 audit_log_format(ab
, "UNKNOWN=%s ", p
);
370 if (entry
->action
== UNKNOWN
)
373 audit_log_format(ab
, "res=%d", !result
? 0 : 1);
379 * ima_parse_add_rule - add a rule to measure_policy_rules
380 * @rule - ima measurement policy rule
382 * Uses a mutex to protect the policy list from multiple concurrent writers.
383 * Returns 0 on success, an error code on failure.
385 int ima_parse_add_rule(char *rule
)
387 const char *op
= "update_policy";
388 struct ima_measure_rule_entry
*entry
;
392 /* Prevent installed policy from changing */
393 if (ima_measure
!= &measure_default_rules
) {
394 integrity_audit_msg(AUDIT_INTEGRITY_STATUS
, NULL
,
395 NULL
, op
, "already exists",
396 -EACCES
, audit_info
);
400 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
402 integrity_audit_msg(AUDIT_INTEGRITY_STATUS
, NULL
,
403 NULL
, op
, "-ENOMEM", -ENOMEM
, audit_info
);
407 INIT_LIST_HEAD(&entry
->list
);
409 result
= ima_parse_rule(rule
, entry
);
411 mutex_lock(&ima_measure_mutex
);
412 list_add_tail(&entry
->list
, &measure_policy_rules
);
413 mutex_unlock(&ima_measure_mutex
);
416 integrity_audit_msg(AUDIT_INTEGRITY_STATUS
, NULL
,
417 NULL
, op
, "invalid policy", result
,
423 /* ima_delete_rules called to cleanup invalid policy */
424 void ima_delete_rules(void)
426 struct ima_measure_rule_entry
*entry
, *tmp
;
428 mutex_lock(&ima_measure_mutex
);
429 list_for_each_entry_safe(entry
, tmp
, &measure_policy_rules
, list
) {
430 list_del(&entry
->list
);
433 mutex_unlock(&ima_measure_mutex
);