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