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