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