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