IMA: do not measure everything opened by root by default
[safe/jmp/linux-2.6] / security / integrity / ima / ima_policy.c
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
19 #include "ima.h"
20
21 /* flags definitions */
22 #define IMA_FUNC        0x0001
23 #define IMA_MASK        0x0002
24 #define IMA_FSMAGIC     0x0004
25 #define IMA_UID         0x0008
26
27 enum ima_action { UNKNOWN = -1, DONT_MEASURE = 0, MEASURE };
28
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
32 };
33
34 struct ima_measure_rule_entry {
35         struct list_head list;
36         enum ima_action action;
37         unsigned int flags;
38         enum ima_hooks func;
39         int mask;
40         unsigned long fsmagic;
41         uid_t uid;
42         struct {
43                 void *rule;     /* LSM file metadata specific */
44                 int type;       /* audit type */
45         } lsm[MAX_LSM_RULES];
46 };
47
48 /* Without LSM specific knowledge, the default policy can only be
49  * written in terms of .action, .func, .mask, .fsmagic, and .uid
50  */
51 static struct ima_measure_rule_entry default_rules[] = {
52         {.action = DONT_MEASURE,.fsmagic = PROC_SUPER_MAGIC,
53          .flags = IMA_FSMAGIC},
54         {.action = DONT_MEASURE,.fsmagic = SYSFS_MAGIC,.flags = IMA_FSMAGIC},
55         {.action = DONT_MEASURE,.fsmagic = DEBUGFS_MAGIC,.flags = IMA_FSMAGIC},
56         {.action = DONT_MEASURE,.fsmagic = TMPFS_MAGIC,.flags = IMA_FSMAGIC},
57         {.action = DONT_MEASURE,.fsmagic = SECURITYFS_MAGIC,
58          .flags = IMA_FSMAGIC},
59         {.action = DONT_MEASURE,.fsmagic = 0xF97CFF8C,.flags = IMA_FSMAGIC},
60         {.action = MEASURE,.func = FILE_MMAP,.mask = MAY_EXEC,
61          .flags = IMA_FUNC | IMA_MASK},
62         {.action = MEASURE,.func = BPRM_CHECK,.mask = MAY_EXEC,
63          .flags = IMA_FUNC | IMA_MASK},
64 };
65
66 static LIST_HEAD(measure_default_rules);
67 static LIST_HEAD(measure_policy_rules);
68 static struct list_head *ima_measure;
69
70 static DEFINE_MUTEX(ima_measure_mutex);
71
72 /**
73  * ima_match_rules - determine whether an inode matches the measure rule.
74  * @rule: a pointer to a rule
75  * @inode: a pointer to an inode
76  * @func: LIM hook identifier
77  * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
78  *
79  * Returns true on rule match, false on failure.
80  */
81 static bool ima_match_rules(struct ima_measure_rule_entry *rule,
82                             struct inode *inode, enum ima_hooks func, int mask)
83 {
84         struct task_struct *tsk = current;
85         int i;
86
87         if ((rule->flags & IMA_FUNC) && rule->func != func)
88                 return false;
89         if ((rule->flags & IMA_MASK) && rule->mask != mask)
90                 return false;
91         if ((rule->flags & IMA_FSMAGIC)
92             && rule->fsmagic != inode->i_sb->s_magic)
93                 return false;
94         if ((rule->flags & IMA_UID) && rule->uid != tsk->cred->uid)
95                 return false;
96         for (i = 0; i < MAX_LSM_RULES; i++) {
97                 int rc = 0;
98                 u32 osid, sid;
99
100                 if (!rule->lsm[i].rule)
101                         continue;
102
103                 switch (i) {
104                 case LSM_OBJ_USER:
105                 case LSM_OBJ_ROLE:
106                 case LSM_OBJ_TYPE:
107                         security_inode_getsecid(inode, &osid);
108                         rc = security_filter_rule_match(osid,
109                                                         rule->lsm[i].type,
110                                                         Audit_equal,
111                                                         rule->lsm[i].rule,
112                                                         NULL);
113                         break;
114                 case LSM_SUBJ_USER:
115                 case LSM_SUBJ_ROLE:
116                 case LSM_SUBJ_TYPE:
117                         security_task_getsecid(tsk, &sid);
118                         rc = security_filter_rule_match(sid,
119                                                         rule->lsm[i].type,
120                                                         Audit_equal,
121                                                         rule->lsm[i].rule,
122                                                         NULL);
123                 default:
124                         break;
125                 }
126                 if (!rc)
127                         return false;
128         }
129         return true;
130 }
131
132 /**
133  * ima_match_policy - decision based on LSM and other conditions
134  * @inode: pointer to an inode for which the policy decision is being made
135  * @func: IMA hook identifier
136  * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
137  *
138  * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
139  * conditions.
140  *
141  * (There is no need for locking when walking the policy list,
142  * as elements in the list are never deleted, nor does the list
143  * change.)
144  */
145 int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask)
146 {
147         struct ima_measure_rule_entry *entry;
148
149         list_for_each_entry(entry, ima_measure, list) {
150                 bool rc;
151
152                 rc = ima_match_rules(entry, inode, func, mask);
153                 if (rc)
154                         return entry->action;
155         }
156         return 0;
157 }
158
159 /**
160  * ima_init_policy - initialize the default measure rules.
161  *
162  * ima_measure points to either the measure_default_rules or the
163  * the new measure_policy_rules.
164  */
165 void ima_init_policy(void)
166 {
167         int i;
168
169         for (i = 0; i < ARRAY_SIZE(default_rules); i++)
170                 list_add_tail(&default_rules[i].list, &measure_default_rules);
171         ima_measure = &measure_default_rules;
172 }
173
174 /**
175  * ima_update_policy - update default_rules with new measure rules
176  *
177  * Called on file .release to update the default rules with a complete new
178  * policy.  Once updated, the policy is locked, no additional rules can be
179  * added to the policy.
180  */
181 void ima_update_policy(void)
182 {
183         const char *op = "policy_update";
184         const char *cause = "already exists";
185         int result = 1;
186         int audit_info = 0;
187
188         if (ima_measure == &measure_default_rules) {
189                 ima_measure = &measure_policy_rules;
190                 cause = "complete";
191                 result = 0;
192         }
193         integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
194                             NULL, op, cause, result, audit_info);
195 }
196
197 enum {
198         Opt_err = -1,
199         Opt_measure = 1, Opt_dont_measure,
200         Opt_obj_user, Opt_obj_role, Opt_obj_type,
201         Opt_subj_user, Opt_subj_role, Opt_subj_type,
202         Opt_func, Opt_mask, Opt_fsmagic, Opt_uid
203 };
204
205 static match_table_t policy_tokens = {
206         {Opt_measure, "measure"},
207         {Opt_dont_measure, "dont_measure"},
208         {Opt_obj_user, "obj_user=%s"},
209         {Opt_obj_role, "obj_role=%s"},
210         {Opt_obj_type, "obj_type=%s"},
211         {Opt_subj_user, "subj_user=%s"},
212         {Opt_subj_role, "subj_role=%s"},
213         {Opt_subj_type, "subj_type=%s"},
214         {Opt_func, "func=%s"},
215         {Opt_mask, "mask=%s"},
216         {Opt_fsmagic, "fsmagic=%s"},
217         {Opt_uid, "uid=%s"},
218         {Opt_err, NULL}
219 };
220
221 static int ima_lsm_rule_init(struct ima_measure_rule_entry *entry,
222                              char *args, int lsm_rule, int audit_type)
223 {
224         int result;
225
226         entry->lsm[lsm_rule].type = audit_type;
227         result = security_filter_rule_init(entry->lsm[lsm_rule].type,
228                                            Audit_equal, args,
229                                            &entry->lsm[lsm_rule].rule);
230         return result;
231 }
232
233 static int ima_parse_rule(char *rule, struct ima_measure_rule_entry *entry)
234 {
235         struct audit_buffer *ab;
236         char *p;
237         int result = 0;
238
239         ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
240
241         entry->action = -1;
242         while ((p = strsep(&rule, " \n")) != NULL) {
243                 substring_t args[MAX_OPT_ARGS];
244                 int token;
245                 unsigned long lnum;
246
247                 if (result < 0)
248                         break;
249                 if (!*p)
250                         continue;
251                 token = match_token(p, policy_tokens, args);
252                 switch (token) {
253                 case Opt_measure:
254                         audit_log_format(ab, "%s ", "measure");
255                         entry->action = MEASURE;
256                         break;
257                 case Opt_dont_measure:
258                         audit_log_format(ab, "%s ", "dont_measure");
259                         entry->action = DONT_MEASURE;
260                         break;
261                 case Opt_func:
262                         audit_log_format(ab, "func=%s ", args[0].from);
263                         if (strcmp(args[0].from, "PATH_CHECK") == 0)
264                                 entry->func = PATH_CHECK;
265                         else if (strcmp(args[0].from, "FILE_MMAP") == 0)
266                                 entry->func = FILE_MMAP;
267                         else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
268                                 entry->func = BPRM_CHECK;
269                         else
270                                 result = -EINVAL;
271                         if (!result)
272                                 entry->flags |= IMA_FUNC;
273                         break;
274                 case Opt_mask:
275                         audit_log_format(ab, "mask=%s ", args[0].from);
276                         if ((strcmp(args[0].from, "MAY_EXEC")) == 0)
277                                 entry->mask = MAY_EXEC;
278                         else if (strcmp(args[0].from, "MAY_WRITE") == 0)
279                                 entry->mask = MAY_WRITE;
280                         else if (strcmp(args[0].from, "MAY_READ") == 0)
281                                 entry->mask = MAY_READ;
282                         else if (strcmp(args[0].from, "MAY_APPEND") == 0)
283                                 entry->mask = MAY_APPEND;
284                         else
285                                 result = -EINVAL;
286                         if (!result)
287                                 entry->flags |= IMA_MASK;
288                         break;
289                 case Opt_fsmagic:
290                         audit_log_format(ab, "fsmagic=%s ", args[0].from);
291                         result = strict_strtoul(args[0].from, 16,
292                                                 &entry->fsmagic);
293                         if (!result)
294                                 entry->flags |= IMA_FSMAGIC;
295                         break;
296                 case Opt_uid:
297                         audit_log_format(ab, "uid=%s ", args[0].from);
298                         result = strict_strtoul(args[0].from, 10, &lnum);
299                         if (!result) {
300                                 entry->uid = (uid_t) lnum;
301                                 if (entry->uid != lnum)
302                                         result = -EINVAL;
303                                 else
304                                         entry->flags |= IMA_UID;
305                         }
306                         break;
307                 case Opt_obj_user:
308                         audit_log_format(ab, "obj_user=%s ", args[0].from);
309                         result = ima_lsm_rule_init(entry, args[0].from,
310                                                    LSM_OBJ_USER,
311                                                    AUDIT_OBJ_USER);
312                         break;
313                 case Opt_obj_role:
314                         audit_log_format(ab, "obj_role=%s ", args[0].from);
315                         result = ima_lsm_rule_init(entry, args[0].from,
316                                                    LSM_OBJ_ROLE,
317                                                    AUDIT_OBJ_ROLE);
318                         break;
319                 case Opt_obj_type:
320                         audit_log_format(ab, "obj_type=%s ", args[0].from);
321                         result = ima_lsm_rule_init(entry, args[0].from,
322                                                    LSM_OBJ_TYPE,
323                                                    AUDIT_OBJ_TYPE);
324                         break;
325                 case Opt_subj_user:
326                         audit_log_format(ab, "subj_user=%s ", args[0].from);
327                         result = ima_lsm_rule_init(entry, args[0].from,
328                                                    LSM_SUBJ_USER,
329                                                    AUDIT_SUBJ_USER);
330                         break;
331                 case Opt_subj_role:
332                         audit_log_format(ab, "subj_role=%s ", args[0].from);
333                         result = ima_lsm_rule_init(entry, args[0].from,
334                                                    LSM_SUBJ_ROLE,
335                                                    AUDIT_SUBJ_ROLE);
336                         break;
337                 case Opt_subj_type:
338                         audit_log_format(ab, "subj_type=%s ", args[0].from);
339                         result = ima_lsm_rule_init(entry, args[0].from,
340                                                    LSM_SUBJ_TYPE,
341                                                    AUDIT_SUBJ_TYPE);
342                         break;
343                 case Opt_err:
344                         audit_log_format(ab, "UNKNOWN=%s ", p);
345                         break;
346                 }
347         }
348         if (entry->action == UNKNOWN)
349                 result = -EINVAL;
350
351         audit_log_format(ab, "res=%d", !result ? 0 : 1);
352         audit_log_end(ab);
353         return result;
354 }
355
356 /**
357  * ima_parse_add_rule - add a rule to measure_policy_rules
358  * @rule - ima measurement policy rule
359  *
360  * Uses a mutex to protect the policy list from multiple concurrent writers.
361  * Returns 0 on success, an error code on failure.
362  */
363 int ima_parse_add_rule(char *rule)
364 {
365         const char *op = "update_policy";
366         struct ima_measure_rule_entry *entry;
367         int result = 0;
368         int audit_info = 0;
369
370         /* Prevent installed policy from changing */
371         if (ima_measure != &measure_default_rules) {
372                 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
373                                     NULL, op, "already exists",
374                                     -EACCES, audit_info);
375                 return -EACCES;
376         }
377
378         entry = kzalloc(sizeof(*entry), GFP_KERNEL);
379         if (!entry) {
380                 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
381                                     NULL, op, "-ENOMEM", -ENOMEM, audit_info);
382                 return -ENOMEM;
383         }
384
385         INIT_LIST_HEAD(&entry->list);
386
387         result = ima_parse_rule(rule, entry);
388         if (!result) {
389                 mutex_lock(&ima_measure_mutex);
390                 list_add_tail(&entry->list, &measure_policy_rules);
391                 mutex_unlock(&ima_measure_mutex);
392         } else {
393                 kfree(entry);
394                 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
395                                     NULL, op, "invalid policy", result,
396                                     audit_info);
397         }
398         return result;
399 }
400
401 /* ima_delete_rules called to cleanup invalid policy */
402 void ima_delete_rules(void)
403 {
404         struct ima_measure_rule_entry *entry, *tmp;
405
406         mutex_lock(&ima_measure_mutex);
407         list_for_each_entry_safe(entry, tmp, &measure_policy_rules, list) {
408                 list_del(&entry->list);
409                 kfree(entry);
410         }
411         mutex_unlock(&ima_measure_mutex);
412 }