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