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