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