Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 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 |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 10 | * - initialize default measure policy rules |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 11 | * |
| 12 | */ |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/list.h> |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 15 | #include <linux/security.h> |
| 16 | #include <linux/magic.h> |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 17 | #include <linux/parser.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 19 | #include <linux/genhd.h> |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 20 | |
| 21 | #include "ima.h" |
| 22 | |
| 23 | /* flags definitions */ |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 24 | #define IMA_FUNC 0x0001 |
| 25 | #define IMA_MASK 0x0002 |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 26 | #define IMA_FSMAGIC 0x0004 |
| 27 | #define IMA_UID 0x0008 |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 28 | #define IMA_FOWNER 0x0010 |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 29 | #define IMA_FSUUID 0x0020 |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 30 | |
Dmitry Kasatkin | 45e2472 | 2012-09-12 20:51:32 +0300 | [diff] [blame] | 31 | #define UNKNOWN 0 |
| 32 | #define MEASURE 0x0001 /* same as IMA_MEASURE */ |
| 33 | #define DONT_MEASURE 0x0002 |
| 34 | #define APPRAISE 0x0004 /* same as IMA_APPRAISE */ |
| 35 | #define DONT_APPRAISE 0x0008 |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 36 | #define AUDIT 0x0040 |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 37 | |
| 38 | #define MAX_LSM_RULES 6 |
| 39 | enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE, |
| 40 | LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE |
| 41 | }; |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 42 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 43 | struct ima_rule_entry { |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 44 | struct list_head list; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 45 | int action; |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 46 | unsigned int flags; |
| 47 | enum ima_hooks func; |
| 48 | int mask; |
| 49 | unsigned long fsmagic; |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 50 | u8 fsuuid[16]; |
Eric W. Biederman | 8b94eea | 2012-05-25 18:24:12 -0600 | [diff] [blame] | 51 | kuid_t uid; |
Linus Torvalds | 88265322 | 2012-10-02 21:38:48 -0700 | [diff] [blame] | 52 | kuid_t fowner; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 53 | struct { |
| 54 | void *rule; /* LSM file metadata specific */ |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 55 | void *args_p; /* audit value */ |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 56 | int type; /* audit type */ |
| 57 | } lsm[MAX_LSM_RULES]; |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 58 | }; |
| 59 | |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 60 | /* |
| 61 | * Without LSM specific knowledge, the default policy can only be |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 62 | * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 63 | */ |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * The minimum rule set to allow for full TCB coverage. Measures all files |
| 67 | * opened or mmap for exec and everything read by root. Dangerous because |
| 68 | * normal users can easily run the machine out of memory simply building |
| 69 | * and running executables. |
| 70 | */ |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 71 | static struct ima_rule_entry default_rules[] = { |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 72 | {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
| 73 | {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 74 | {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 75 | {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 76 | {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
| 77 | {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 78 | {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 79 | {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC}, |
| 80 | {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC, |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 81 | .flags = IMA_FUNC | IMA_MASK}, |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 82 | {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC, |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 83 | .flags = IMA_FUNC | IMA_MASK}, |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 84 | {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ, .uid = GLOBAL_ROOT_UID, |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 85 | .flags = IMA_FUNC | IMA_MASK | IMA_UID}, |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 86 | {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC}, |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 87 | }; |
| 88 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 89 | static struct ima_rule_entry default_appraise_rules[] = { |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 90 | {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
| 91 | {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 92 | {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 93 | {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 94 | {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 95 | {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
| 96 | {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 97 | {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 98 | {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC}, |
| 99 | {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
| 100 | {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .flags = IMA_FOWNER}, |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 101 | }; |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 102 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 103 | static LIST_HEAD(ima_default_rules); |
| 104 | static LIST_HEAD(ima_policy_rules); |
| 105 | static struct list_head *ima_rules; |
| 106 | |
| 107 | static DEFINE_MUTEX(ima_rules_mutex); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 108 | |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 109 | static bool ima_use_tcb __initdata; |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 110 | static int __init default_measure_policy_setup(char *str) |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 111 | { |
| 112 | ima_use_tcb = 1; |
| 113 | return 1; |
| 114 | } |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 115 | __setup("ima_tcb", default_measure_policy_setup); |
| 116 | |
| 117 | static bool ima_use_appraise_tcb __initdata; |
| 118 | static int __init default_appraise_policy_setup(char *str) |
| 119 | { |
| 120 | ima_use_appraise_tcb = 1; |
| 121 | return 1; |
| 122 | } |
| 123 | __setup("ima_appraise_tcb", default_appraise_policy_setup); |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 124 | |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 125 | /* |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 126 | * Although the IMA policy does not change, the LSM policy can be |
| 127 | * reloaded, leaving the IMA LSM based rules referring to the old, |
| 128 | * stale LSM policy. |
| 129 | * |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 130 | * Update the IMA LSM based rules to reflect the reloaded LSM policy. |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 131 | * We assume the rules still exist; and BUG_ON() if they don't. |
| 132 | */ |
| 133 | static void ima_lsm_update_rules(void) |
| 134 | { |
| 135 | struct ima_rule_entry *entry, *tmp; |
| 136 | int result; |
| 137 | int i; |
| 138 | |
| 139 | mutex_lock(&ima_rules_mutex); |
| 140 | list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) { |
| 141 | for (i = 0; i < MAX_LSM_RULES; i++) { |
| 142 | if (!entry->lsm[i].rule) |
| 143 | continue; |
| 144 | result = security_filter_rule_init(entry->lsm[i].type, |
| 145 | Audit_equal, |
| 146 | entry->lsm[i].args_p, |
| 147 | &entry->lsm[i].rule); |
| 148 | BUG_ON(!entry->lsm[i].rule); |
| 149 | } |
| 150 | } |
| 151 | mutex_unlock(&ima_rules_mutex); |
| 152 | } |
| 153 | |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 154 | /** |
| 155 | * ima_match_rules - determine whether an inode matches the measure rule. |
| 156 | * @rule: a pointer to a rule |
| 157 | * @inode: a pointer to an inode |
| 158 | * @func: LIM hook identifier |
| 159 | * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC) |
| 160 | * |
| 161 | * Returns true on rule match, false on failure. |
| 162 | */ |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 163 | static bool ima_match_rules(struct ima_rule_entry *rule, |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 164 | struct inode *inode, enum ima_hooks func, int mask) |
| 165 | { |
| 166 | struct task_struct *tsk = current; |
Mimi Zohar | 3db59dd | 2012-01-17 22:11:28 -0500 | [diff] [blame] | 167 | const struct cred *cred = current_cred(); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 168 | int i; |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 169 | |
Dmitry Kasatkin | 09b1148 | 2013-11-13 23:42:39 +0200 | [diff] [blame] | 170 | if ((rule->flags & IMA_FUNC) && |
| 171 | (rule->func != func && func != POST_SETATTR)) |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 172 | return false; |
Dmitry Kasatkin | 09b1148 | 2013-11-13 23:42:39 +0200 | [diff] [blame] | 173 | if ((rule->flags & IMA_MASK) && |
| 174 | (rule->mask != mask && func != POST_SETATTR)) |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 175 | return false; |
| 176 | if ((rule->flags & IMA_FSMAGIC) |
| 177 | && rule->fsmagic != inode->i_sb->s_magic) |
| 178 | return false; |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 179 | if ((rule->flags & IMA_FSUUID) && |
Mimi Zohar | 446d64e | 2013-02-24 23:42:37 -0500 | [diff] [blame] | 180 | memcmp(rule->fsuuid, inode->i_sb->s_uuid, sizeof(rule->fsuuid))) |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 181 | return false; |
Eric W. Biederman | 8b94eea | 2012-05-25 18:24:12 -0600 | [diff] [blame] | 182 | if ((rule->flags & IMA_UID) && !uid_eq(rule->uid, cred->uid)) |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 183 | return false; |
Linus Torvalds | 88265322 | 2012-10-02 21:38:48 -0700 | [diff] [blame] | 184 | if ((rule->flags & IMA_FOWNER) && !uid_eq(rule->fowner, inode->i_uid)) |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 185 | return false; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 186 | for (i = 0; i < MAX_LSM_RULES; i++) { |
Mimi Zohar | 53fc0e2 | 2009-05-05 13:12:48 -0400 | [diff] [blame] | 187 | int rc = 0; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 188 | u32 osid, sid; |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 189 | int retried = 0; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 190 | |
| 191 | if (!rule->lsm[i].rule) |
| 192 | continue; |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 193 | retry: |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 194 | switch (i) { |
| 195 | case LSM_OBJ_USER: |
| 196 | case LSM_OBJ_ROLE: |
| 197 | case LSM_OBJ_TYPE: |
| 198 | security_inode_getsecid(inode, &osid); |
| 199 | rc = security_filter_rule_match(osid, |
| 200 | rule->lsm[i].type, |
Mimi Zohar | 53fc0e2 | 2009-05-05 13:12:48 -0400 | [diff] [blame] | 201 | Audit_equal, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 202 | rule->lsm[i].rule, |
| 203 | NULL); |
| 204 | break; |
| 205 | case LSM_SUBJ_USER: |
| 206 | case LSM_SUBJ_ROLE: |
| 207 | case LSM_SUBJ_TYPE: |
| 208 | security_task_getsecid(tsk, &sid); |
| 209 | rc = security_filter_rule_match(sid, |
| 210 | rule->lsm[i].type, |
Mimi Zohar | 53fc0e2 | 2009-05-05 13:12:48 -0400 | [diff] [blame] | 211 | Audit_equal, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 212 | rule->lsm[i].rule, |
| 213 | NULL); |
| 214 | default: |
| 215 | break; |
| 216 | } |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 217 | if ((rc < 0) && (!retried)) { |
| 218 | retried = 1; |
| 219 | ima_lsm_update_rules(); |
| 220 | goto retry; |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 221 | } |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 222 | if (!rc) |
| 223 | return false; |
| 224 | } |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 225 | return true; |
| 226 | } |
| 227 | |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 228 | /* |
| 229 | * In addition to knowing that we need to appraise the file in general, |
Mimi Zohar | 5a73fcf | 2012-12-05 15:14:38 -0500 | [diff] [blame] | 230 | * we need to differentiate between calling hooks, for hook specific rules. |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 231 | */ |
Mimi Zohar | 5a73fcf | 2012-12-05 15:14:38 -0500 | [diff] [blame] | 232 | static int get_subaction(struct ima_rule_entry *rule, int func) |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 233 | { |
Mimi Zohar | 5a73fcf | 2012-12-05 15:14:38 -0500 | [diff] [blame] | 234 | if (!(rule->flags & IMA_FUNC)) |
| 235 | return IMA_FILE_APPRAISE; |
| 236 | |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 237 | switch (func) { |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 238 | case MMAP_CHECK: |
| 239 | return IMA_MMAP_APPRAISE; |
| 240 | case BPRM_CHECK: |
| 241 | return IMA_BPRM_APPRAISE; |
| 242 | case MODULE_CHECK: |
| 243 | return IMA_MODULE_APPRAISE; |
| 244 | case FILE_CHECK: |
| 245 | default: |
| 246 | return IMA_FILE_APPRAISE; |
| 247 | } |
| 248 | } |
| 249 | |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 250 | /** |
| 251 | * ima_match_policy - decision based on LSM and other conditions |
| 252 | * @inode: pointer to an inode for which the policy decision is being made |
| 253 | * @func: IMA hook identifier |
| 254 | * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC) |
| 255 | * |
| 256 | * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type) |
| 257 | * conditions. |
| 258 | * |
| 259 | * (There is no need for locking when walking the policy list, |
| 260 | * as elements in the list are never deleted, nor does the list |
| 261 | * change.) |
| 262 | */ |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 263 | int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask, |
| 264 | int flags) |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 265 | { |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 266 | struct ima_rule_entry *entry; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 267 | int action = 0, actmask = flags | (flags << 1); |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 268 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 269 | list_for_each_entry(entry, ima_rules, list) { |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 270 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 271 | if (!(entry->action & actmask)) |
| 272 | continue; |
| 273 | |
| 274 | if (!ima_match_rules(entry, inode, func, mask)) |
| 275 | continue; |
| 276 | |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 277 | action |= entry->flags & IMA_ACTION_FLAGS; |
| 278 | |
Dmitry Kasatkin | 45e2472 | 2012-09-12 20:51:32 +0300 | [diff] [blame] | 279 | action |= entry->action & IMA_DO_MASK; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 280 | if (entry->action & IMA_APPRAISE) |
Mimi Zohar | 5a73fcf | 2012-12-05 15:14:38 -0500 | [diff] [blame] | 281 | action |= get_subaction(entry, func); |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 282 | |
Dmitry Kasatkin | 45e2472 | 2012-09-12 20:51:32 +0300 | [diff] [blame] | 283 | if (entry->action & IMA_DO_MASK) |
| 284 | actmask &= ~(entry->action | entry->action << 1); |
| 285 | else |
| 286 | actmask &= ~(entry->action | entry->action >> 1); |
| 287 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 288 | if (!actmask) |
| 289 | break; |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 290 | } |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 291 | |
| 292 | return action; |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /** |
| 296 | * ima_init_policy - initialize the default measure rules. |
| 297 | * |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 298 | * ima_rules points to either the ima_default_rules or the |
| 299 | * the new ima_policy_rules. |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 300 | */ |
Eric Paris | 932995f | 2009-05-21 15:43:32 -0400 | [diff] [blame] | 301 | void __init ima_init_policy(void) |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 302 | { |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 303 | int i, measure_entries, appraise_entries; |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 304 | |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 305 | /* if !ima_use_tcb set entries = 0 so we load NO default rules */ |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 306 | measure_entries = ima_use_tcb ? ARRAY_SIZE(default_rules) : 0; |
| 307 | appraise_entries = ima_use_appraise_tcb ? |
| 308 | ARRAY_SIZE(default_appraise_rules) : 0; |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 309 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 310 | for (i = 0; i < measure_entries + appraise_entries; i++) { |
| 311 | if (i < measure_entries) |
| 312 | list_add_tail(&default_rules[i].list, |
| 313 | &ima_default_rules); |
| 314 | else { |
| 315 | int j = i - measure_entries; |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 316 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 317 | list_add_tail(&default_appraise_rules[j].list, |
| 318 | &ima_default_rules); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | ima_rules = &ima_default_rules; |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 323 | } |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 324 | |
| 325 | /** |
| 326 | * ima_update_policy - update default_rules with new measure rules |
| 327 | * |
| 328 | * Called on file .release to update the default rules with a complete new |
| 329 | * policy. Once updated, the policy is locked, no additional rules can be |
| 330 | * added to the policy. |
| 331 | */ |
| 332 | void ima_update_policy(void) |
| 333 | { |
Mimi Zohar | 52a1328 | 2013-12-11 14:44:04 -0500 | [diff] [blame] | 334 | static const char op[] = "policy_update"; |
Richard Guy Briggs | 7e9001f | 2014-06-16 15:52:07 -0400 | [diff] [blame^] | 335 | const char *cause = "already-exists"; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 336 | int result = 1; |
| 337 | int audit_info = 0; |
| 338 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 339 | if (ima_rules == &ima_default_rules) { |
| 340 | ima_rules = &ima_policy_rules; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 341 | cause = "complete"; |
| 342 | result = 0; |
| 343 | } |
| 344 | integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, |
| 345 | NULL, op, cause, result, audit_info); |
| 346 | } |
| 347 | |
| 348 | enum { |
| 349 | Opt_err = -1, |
| 350 | Opt_measure = 1, Opt_dont_measure, |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 351 | Opt_appraise, Opt_dont_appraise, |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 352 | Opt_audit, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 353 | Opt_obj_user, Opt_obj_role, Opt_obj_type, |
| 354 | Opt_subj_user, Opt_subj_role, Opt_subj_type, |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 355 | Opt_func, Opt_mask, Opt_fsmagic, Opt_uid, Opt_fowner, |
Mimi Zohar | f9b2a73 | 2014-05-12 09:28:11 -0400 | [diff] [blame] | 356 | Opt_appraise_type, Opt_fsuuid, Opt_permit_directio |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 357 | }; |
| 358 | |
| 359 | static match_table_t policy_tokens = { |
| 360 | {Opt_measure, "measure"}, |
| 361 | {Opt_dont_measure, "dont_measure"}, |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 362 | {Opt_appraise, "appraise"}, |
| 363 | {Opt_dont_appraise, "dont_appraise"}, |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 364 | {Opt_audit, "audit"}, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 365 | {Opt_obj_user, "obj_user=%s"}, |
| 366 | {Opt_obj_role, "obj_role=%s"}, |
| 367 | {Opt_obj_type, "obj_type=%s"}, |
| 368 | {Opt_subj_user, "subj_user=%s"}, |
| 369 | {Opt_subj_role, "subj_role=%s"}, |
| 370 | {Opt_subj_type, "subj_type=%s"}, |
| 371 | {Opt_func, "func=%s"}, |
| 372 | {Opt_mask, "mask=%s"}, |
| 373 | {Opt_fsmagic, "fsmagic=%s"}, |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 374 | {Opt_fsuuid, "fsuuid=%s"}, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 375 | {Opt_uid, "uid=%s"}, |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 376 | {Opt_fowner, "fowner=%s"}, |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 377 | {Opt_appraise_type, "appraise_type=%s"}, |
Mimi Zohar | f9b2a73 | 2014-05-12 09:28:11 -0400 | [diff] [blame] | 378 | {Opt_permit_directio, "permit_directio"}, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 379 | {Opt_err, NULL} |
| 380 | }; |
| 381 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 382 | static int ima_lsm_rule_init(struct ima_rule_entry *entry, |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 383 | substring_t *args, int lsm_rule, int audit_type) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 384 | { |
| 385 | int result; |
| 386 | |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 387 | if (entry->lsm[lsm_rule].rule) |
| 388 | return -EINVAL; |
| 389 | |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 390 | entry->lsm[lsm_rule].args_p = match_strdup(args); |
| 391 | if (!entry->lsm[lsm_rule].args_p) |
| 392 | return -ENOMEM; |
| 393 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 394 | entry->lsm[lsm_rule].type = audit_type; |
| 395 | result = security_filter_rule_init(entry->lsm[lsm_rule].type, |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 396 | Audit_equal, |
| 397 | entry->lsm[lsm_rule].args_p, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 398 | &entry->lsm[lsm_rule].rule); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 399 | if (!entry->lsm[lsm_rule].rule) { |
| 400 | kfree(entry->lsm[lsm_rule].args_p); |
Mimi Zohar | 867c202 | 2011-01-03 14:59:10 -0800 | [diff] [blame] | 401 | return -EINVAL; |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 402 | } |
| 403 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 404 | return result; |
| 405 | } |
| 406 | |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 407 | static void ima_log_string(struct audit_buffer *ab, char *key, char *value) |
| 408 | { |
| 409 | audit_log_format(ab, "%s=", key); |
| 410 | audit_log_untrustedstring(ab, value); |
| 411 | audit_log_format(ab, " "); |
| 412 | } |
| 413 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 414 | static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 415 | { |
| 416 | struct audit_buffer *ab; |
| 417 | char *p; |
| 418 | int result = 0; |
| 419 | |
Mimi Zohar | 523979a | 2009-02-11 11:12:28 -0500 | [diff] [blame] | 420 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 421 | |
Eric W. Biederman | 8b94eea | 2012-05-25 18:24:12 -0600 | [diff] [blame] | 422 | entry->uid = INVALID_UID; |
Linus Torvalds | 88265322 | 2012-10-02 21:38:48 -0700 | [diff] [blame] | 423 | entry->fowner = INVALID_UID; |
Eric Paris | b9035b1 | 2010-04-20 10:21:07 -0400 | [diff] [blame] | 424 | entry->action = UNKNOWN; |
Eric Paris | 28ef400 | 2010-04-20 10:21:18 -0400 | [diff] [blame] | 425 | while ((p = strsep(&rule, " \t")) != NULL) { |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 426 | substring_t args[MAX_OPT_ARGS]; |
| 427 | int token; |
| 428 | unsigned long lnum; |
| 429 | |
| 430 | if (result < 0) |
| 431 | break; |
Eric Paris | 28ef400 | 2010-04-20 10:21:18 -0400 | [diff] [blame] | 432 | if ((*p == '\0') || (*p == ' ') || (*p == '\t')) |
| 433 | continue; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 434 | token = match_token(p, policy_tokens, args); |
| 435 | switch (token) { |
| 436 | case Opt_measure: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 437 | ima_log_string(ab, "action", "measure"); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 438 | |
| 439 | if (entry->action != UNKNOWN) |
| 440 | result = -EINVAL; |
| 441 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 442 | entry->action = MEASURE; |
| 443 | break; |
| 444 | case Opt_dont_measure: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 445 | ima_log_string(ab, "action", "dont_measure"); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 446 | |
| 447 | if (entry->action != UNKNOWN) |
| 448 | result = -EINVAL; |
| 449 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 450 | entry->action = DONT_MEASURE; |
| 451 | break; |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 452 | case Opt_appraise: |
| 453 | ima_log_string(ab, "action", "appraise"); |
| 454 | |
| 455 | if (entry->action != UNKNOWN) |
| 456 | result = -EINVAL; |
| 457 | |
| 458 | entry->action = APPRAISE; |
| 459 | break; |
| 460 | case Opt_dont_appraise: |
| 461 | ima_log_string(ab, "action", "dont_appraise"); |
| 462 | |
| 463 | if (entry->action != UNKNOWN) |
| 464 | result = -EINVAL; |
| 465 | |
| 466 | entry->action = DONT_APPRAISE; |
| 467 | break; |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 468 | case Opt_audit: |
| 469 | ima_log_string(ab, "action", "audit"); |
| 470 | |
| 471 | if (entry->action != UNKNOWN) |
| 472 | result = -EINVAL; |
| 473 | |
| 474 | entry->action = AUDIT; |
| 475 | break; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 476 | case Opt_func: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 477 | ima_log_string(ab, "func", args[0].from); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 478 | |
| 479 | if (entry->func) |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 480 | result = -EINVAL; |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 481 | |
Mimi Zohar | 1e93d00 | 2010-01-26 17:02:41 -0500 | [diff] [blame] | 482 | if (strcmp(args[0].from, "FILE_CHECK") == 0) |
| 483 | entry->func = FILE_CHECK; |
| 484 | /* PATH_CHECK is for backwards compat */ |
| 485 | else if (strcmp(args[0].from, "PATH_CHECK") == 0) |
| 486 | entry->func = FILE_CHECK; |
Mimi Zohar | fdf9072 | 2012-10-16 12:40:08 +1030 | [diff] [blame] | 487 | else if (strcmp(args[0].from, "MODULE_CHECK") == 0) |
| 488 | entry->func = MODULE_CHECK; |
Mimi Zohar | 16cac49 | 2012-12-13 11:15:04 -0500 | [diff] [blame] | 489 | else if ((strcmp(args[0].from, "FILE_MMAP") == 0) |
| 490 | || (strcmp(args[0].from, "MMAP_CHECK") == 0)) |
| 491 | entry->func = MMAP_CHECK; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 492 | else if (strcmp(args[0].from, "BPRM_CHECK") == 0) |
| 493 | entry->func = BPRM_CHECK; |
| 494 | else |
| 495 | result = -EINVAL; |
| 496 | if (!result) |
| 497 | entry->flags |= IMA_FUNC; |
| 498 | break; |
| 499 | case Opt_mask: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 500 | ima_log_string(ab, "mask", args[0].from); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 501 | |
| 502 | if (entry->mask) |
| 503 | result = -EINVAL; |
| 504 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 505 | if ((strcmp(args[0].from, "MAY_EXEC")) == 0) |
| 506 | entry->mask = MAY_EXEC; |
| 507 | else if (strcmp(args[0].from, "MAY_WRITE") == 0) |
| 508 | entry->mask = MAY_WRITE; |
| 509 | else if (strcmp(args[0].from, "MAY_READ") == 0) |
| 510 | entry->mask = MAY_READ; |
| 511 | else if (strcmp(args[0].from, "MAY_APPEND") == 0) |
| 512 | entry->mask = MAY_APPEND; |
| 513 | else |
| 514 | result = -EINVAL; |
| 515 | if (!result) |
| 516 | entry->flags |= IMA_MASK; |
| 517 | break; |
| 518 | case Opt_fsmagic: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 519 | ima_log_string(ab, "fsmagic", args[0].from); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 520 | |
| 521 | if (entry->fsmagic) { |
| 522 | result = -EINVAL; |
| 523 | break; |
| 524 | } |
| 525 | |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 526 | result = kstrtoul(args[0].from, 16, &entry->fsmagic); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 527 | if (!result) |
| 528 | entry->flags |= IMA_FSMAGIC; |
| 529 | break; |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 530 | case Opt_fsuuid: |
| 531 | ima_log_string(ab, "fsuuid", args[0].from); |
| 532 | |
| 533 | if (memchr_inv(entry->fsuuid, 0x00, |
Mimi Zohar | 446d64e | 2013-02-24 23:42:37 -0500 | [diff] [blame] | 534 | sizeof(entry->fsuuid))) { |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 535 | result = -EINVAL; |
| 536 | break; |
| 537 | } |
| 538 | |
Mimi Zohar | 446d64e | 2013-02-24 23:42:37 -0500 | [diff] [blame] | 539 | result = blk_part_pack_uuid(args[0].from, |
| 540 | entry->fsuuid); |
| 541 | if (!result) |
| 542 | entry->flags |= IMA_FSUUID; |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 543 | break; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 544 | case Opt_uid: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 545 | ima_log_string(ab, "uid", args[0].from); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 546 | |
Eric W. Biederman | 8b94eea | 2012-05-25 18:24:12 -0600 | [diff] [blame] | 547 | if (uid_valid(entry->uid)) { |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 548 | result = -EINVAL; |
| 549 | break; |
| 550 | } |
| 551 | |
Jingoo Han | 29707b2 | 2014-02-05 15:13:14 +0900 | [diff] [blame] | 552 | result = kstrtoul(args[0].from, 10, &lnum); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 553 | if (!result) { |
Eric W. Biederman | 8b94eea | 2012-05-25 18:24:12 -0600 | [diff] [blame] | 554 | entry->uid = make_kuid(current_user_ns(), (uid_t)lnum); |
| 555 | if (!uid_valid(entry->uid) || (((uid_t)lnum) != lnum)) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 556 | result = -EINVAL; |
| 557 | else |
| 558 | entry->flags |= IMA_UID; |
| 559 | } |
| 560 | break; |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 561 | case Opt_fowner: |
| 562 | ima_log_string(ab, "fowner", args[0].from); |
| 563 | |
Linus Torvalds | 88265322 | 2012-10-02 21:38:48 -0700 | [diff] [blame] | 564 | if (uid_valid(entry->fowner)) { |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 565 | result = -EINVAL; |
| 566 | break; |
| 567 | } |
| 568 | |
Jingoo Han | 29707b2 | 2014-02-05 15:13:14 +0900 | [diff] [blame] | 569 | result = kstrtoul(args[0].from, 10, &lnum); |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 570 | if (!result) { |
Linus Torvalds | 88265322 | 2012-10-02 21:38:48 -0700 | [diff] [blame] | 571 | entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum); |
| 572 | if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum)) |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 573 | result = -EINVAL; |
| 574 | else |
| 575 | entry->flags |= IMA_FOWNER; |
| 576 | } |
| 577 | break; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 578 | case Opt_obj_user: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 579 | ima_log_string(ab, "obj_user", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 580 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 581 | LSM_OBJ_USER, |
| 582 | AUDIT_OBJ_USER); |
| 583 | break; |
| 584 | case Opt_obj_role: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 585 | ima_log_string(ab, "obj_role", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 586 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 587 | LSM_OBJ_ROLE, |
| 588 | AUDIT_OBJ_ROLE); |
| 589 | break; |
| 590 | case Opt_obj_type: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 591 | ima_log_string(ab, "obj_type", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 592 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 593 | LSM_OBJ_TYPE, |
| 594 | AUDIT_OBJ_TYPE); |
| 595 | break; |
| 596 | case Opt_subj_user: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 597 | ima_log_string(ab, "subj_user", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 598 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 599 | LSM_SUBJ_USER, |
| 600 | AUDIT_SUBJ_USER); |
| 601 | break; |
| 602 | case Opt_subj_role: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 603 | ima_log_string(ab, "subj_role", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 604 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 605 | LSM_SUBJ_ROLE, |
| 606 | AUDIT_SUBJ_ROLE); |
| 607 | break; |
| 608 | case Opt_subj_type: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 609 | ima_log_string(ab, "subj_type", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 610 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 611 | LSM_SUBJ_TYPE, |
| 612 | AUDIT_SUBJ_TYPE); |
| 613 | break; |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 614 | case Opt_appraise_type: |
| 615 | if (entry->action != APPRAISE) { |
| 616 | result = -EINVAL; |
| 617 | break; |
| 618 | } |
| 619 | |
| 620 | ima_log_string(ab, "appraise_type", args[0].from); |
| 621 | if ((strcmp(args[0].from, "imasig")) == 0) |
| 622 | entry->flags |= IMA_DIGSIG_REQUIRED; |
| 623 | else |
| 624 | result = -EINVAL; |
| 625 | break; |
Mimi Zohar | f9b2a73 | 2014-05-12 09:28:11 -0400 | [diff] [blame] | 626 | case Opt_permit_directio: |
| 627 | entry->flags |= IMA_PERMIT_DIRECTIO; |
| 628 | break; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 629 | case Opt_err: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 630 | ima_log_string(ab, "UNKNOWN", p); |
Eric Paris | e9d393b | 2010-04-20 10:21:13 -0400 | [diff] [blame] | 631 | result = -EINVAL; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 632 | break; |
| 633 | } |
| 634 | } |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 635 | if (!result && (entry->action == UNKNOWN)) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 636 | result = -EINVAL; |
Mimi Zohar | a7f2a36 | 2012-12-21 08:34:21 -0500 | [diff] [blame] | 637 | else if (entry->func == MODULE_CHECK) |
| 638 | ima_appraise |= IMA_APPRAISE_MODULES; |
Eric Paris | b0d5de4 | 2012-02-14 17:11:07 -0500 | [diff] [blame] | 639 | audit_log_format(ab, "res=%d", !result); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 640 | audit_log_end(ab); |
| 641 | return result; |
| 642 | } |
| 643 | |
| 644 | /** |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 645 | * ima_parse_add_rule - add a rule to ima_policy_rules |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 646 | * @rule - ima measurement policy rule |
| 647 | * |
| 648 | * Uses a mutex to protect the policy list from multiple concurrent writers. |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 649 | * Returns the length of the rule parsed, an error code on failure |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 650 | */ |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 651 | ssize_t ima_parse_add_rule(char *rule) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 652 | { |
Mimi Zohar | 52a1328 | 2013-12-11 14:44:04 -0500 | [diff] [blame] | 653 | static const char op[] = "update_policy"; |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 654 | char *p; |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 655 | struct ima_rule_entry *entry; |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 656 | ssize_t result, len; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 657 | int audit_info = 0; |
| 658 | |
| 659 | /* Prevent installed policy from changing */ |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 660 | if (ima_rules != &ima_default_rules) { |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 661 | integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, |
Richard Guy Briggs | 7e9001f | 2014-06-16 15:52:07 -0400 | [diff] [blame^] | 662 | NULL, op, "already-exists", |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 663 | -EACCES, audit_info); |
| 664 | return -EACCES; |
| 665 | } |
| 666 | |
| 667 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 668 | if (!entry) { |
| 669 | integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, |
| 670 | NULL, op, "-ENOMEM", -ENOMEM, audit_info); |
| 671 | return -ENOMEM; |
| 672 | } |
| 673 | |
| 674 | INIT_LIST_HEAD(&entry->list); |
| 675 | |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 676 | p = strsep(&rule, "\n"); |
| 677 | len = strlen(p) + 1; |
Eric Paris | 7233e3e | 2010-04-20 10:21:24 -0400 | [diff] [blame] | 678 | |
| 679 | if (*p == '#') { |
| 680 | kfree(entry); |
| 681 | return len; |
| 682 | } |
| 683 | |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 684 | result = ima_parse_rule(p, entry); |
Eric Paris | 7233e3e | 2010-04-20 10:21:24 -0400 | [diff] [blame] | 685 | if (result) { |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 686 | kfree(entry); |
Mimi Zohar | 523979a | 2009-02-11 11:12:28 -0500 | [diff] [blame] | 687 | integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, |
Richard Guy Briggs | 7e9001f | 2014-06-16 15:52:07 -0400 | [diff] [blame^] | 688 | NULL, op, "invalid-policy", result, |
Mimi Zohar | 523979a | 2009-02-11 11:12:28 -0500 | [diff] [blame] | 689 | audit_info); |
Eric Paris | 7233e3e | 2010-04-20 10:21:24 -0400 | [diff] [blame] | 690 | return result; |
Mimi Zohar | 523979a | 2009-02-11 11:12:28 -0500 | [diff] [blame] | 691 | } |
Eric Paris | 7233e3e | 2010-04-20 10:21:24 -0400 | [diff] [blame] | 692 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 693 | mutex_lock(&ima_rules_mutex); |
| 694 | list_add_tail(&entry->list, &ima_policy_rules); |
| 695 | mutex_unlock(&ima_rules_mutex); |
Eric Paris | 7233e3e | 2010-04-20 10:21:24 -0400 | [diff] [blame] | 696 | |
| 697 | return len; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | /* ima_delete_rules called to cleanup invalid policy */ |
James Morris | 64c61d8 | 2009-02-05 09:28:26 +1100 | [diff] [blame] | 701 | void ima_delete_rules(void) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 702 | { |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 703 | struct ima_rule_entry *entry, *tmp; |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 704 | int i; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 705 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 706 | mutex_lock(&ima_rules_mutex); |
| 707 | list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) { |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 708 | for (i = 0; i < MAX_LSM_RULES; i++) |
| 709 | kfree(entry->lsm[i].args_p); |
| 710 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 711 | list_del(&entry->list); |
| 712 | kfree(entry); |
| 713 | } |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 714 | mutex_unlock(&ima_rules_mutex); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 715 | } |