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> 15 #include <linux/fs.h> 16 #include <linux/security.h> 17 #include <linux/magic.h> 18 #include <linux/parser.h> 19 #include <linux/slab.h> 20 #include <linux/rculist.h> 21 #include <linux/genhd.h> 22 #include <linux/seq_file.h> 23 24 #include "ima.h" 25 26 /* flags definitions */ 27 #define IMA_FUNC 0x0001 28 #define IMA_MASK 0x0002 29 #define IMA_FSMAGIC 0x0004 30 #define IMA_UID 0x0008 31 #define IMA_FOWNER 0x0010 32 #define IMA_FSUUID 0x0020 33 #define IMA_INMASK 0x0040 34 #define IMA_EUID 0x0080 35 #define IMA_PCR 0x0100 36 37 #define UNKNOWN 0 38 #define MEASURE 0x0001 /* same as IMA_MEASURE */ 39 #define DONT_MEASURE 0x0002 40 #define APPRAISE 0x0004 /* same as IMA_APPRAISE */ 41 #define DONT_APPRAISE 0x0008 42 #define AUDIT 0x0040 43 44 #define INVALID_PCR(a) (((a) < 0) || \ 45 (a) >= (FIELD_SIZEOF(struct integrity_iint_cache, measured_pcrs) * 8)) 46 47 int ima_policy_flag; 48 static int temp_ima_appraise; 49 50 #define MAX_LSM_RULES 6 51 enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE, 52 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE 53 }; 54 55 enum policy_types { ORIGINAL_TCB = 1, DEFAULT_TCB }; 56 57 struct ima_rule_entry { 58 struct list_head list; 59 int action; 60 unsigned int flags; 61 enum ima_hooks func; 62 int mask; 63 unsigned long fsmagic; 64 u8 fsuuid[16]; 65 kuid_t uid; 66 kuid_t fowner; 67 bool (*uid_op)(kuid_t, kuid_t); /* Handlers for operators */ 68 bool (*fowner_op)(kuid_t, kuid_t); /* uid_eq(), uid_gt(), uid_lt() */ 69 int pcr; 70 struct { 71 void *rule; /* LSM file metadata specific */ 72 void *args_p; /* audit value */ 73 int type; /* audit type */ 74 } lsm[MAX_LSM_RULES]; 75 }; 76 77 /* 78 * Without LSM specific knowledge, the default policy can only be 79 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner 80 */ 81 82 /* 83 * The minimum rule set to allow for full TCB coverage. Measures all files 84 * opened or mmap for exec and everything read by root. Dangerous because 85 * normal users can easily run the machine out of memory simply building 86 * and running executables. 87 */ 88 static struct ima_rule_entry dont_measure_rules[] __ro_after_init = { 89 {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC}, 90 {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC}, 91 {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC}, 92 {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC}, 93 {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC}, 94 {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC}, 95 {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC}, 96 {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC}, 97 {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC, 98 .flags = IMA_FSMAGIC}, 99 {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC} 100 }; 101 102 static struct ima_rule_entry original_measurement_rules[] __ro_after_init = { 103 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC, 104 .flags = IMA_FUNC | IMA_MASK}, 105 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC, 106 .flags = IMA_FUNC | IMA_MASK}, 107 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ, 108 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq, 109 .flags = IMA_FUNC | IMA_MASK | IMA_UID}, 110 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC}, 111 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC}, 112 }; 113 114 static struct ima_rule_entry default_measurement_rules[] __ro_after_init = { 115 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC, 116 .flags = IMA_FUNC | IMA_MASK}, 117 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC, 118 .flags = IMA_FUNC | IMA_MASK}, 119 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ, 120 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq, 121 .flags = IMA_FUNC | IMA_INMASK | IMA_EUID}, 122 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ, 123 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq, 124 .flags = IMA_FUNC | IMA_INMASK | IMA_UID}, 125 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC}, 126 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC}, 127 {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC}, 128 }; 129 130 static struct ima_rule_entry default_appraise_rules[] __ro_after_init = { 131 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC}, 132 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC}, 133 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC}, 134 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC}, 135 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC}, 136 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC}, 137 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC}, 138 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC}, 139 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC}, 140 {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC}, 141 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC}, 142 #ifdef CONFIG_IMA_WRITE_POLICY 143 {.action = APPRAISE, .func = POLICY_CHECK, 144 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, 145 #endif 146 #ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT 147 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq, 148 .flags = IMA_FOWNER}, 149 #else 150 /* force signature */ 151 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq, 152 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED}, 153 #endif 154 }; 155 156 static LIST_HEAD(ima_default_rules); 157 static LIST_HEAD(ima_policy_rules); 158 static LIST_HEAD(ima_temp_rules); 159 static struct list_head *ima_rules; 160 161 static int ima_policy __initdata; 162 163 static int __init default_measure_policy_setup(char *str) 164 { 165 if (ima_policy) 166 return 1; 167 168 ima_policy = ORIGINAL_TCB; 169 return 1; 170 } 171 __setup("ima_tcb", default_measure_policy_setup); 172 173 static int __init policy_setup(char *str) 174 { 175 if (ima_policy) 176 return 1; 177 178 if (strcmp(str, "tcb") == 0) 179 ima_policy = DEFAULT_TCB; 180 181 return 1; 182 } 183 __setup("ima_policy=", policy_setup); 184 185 static bool ima_use_appraise_tcb __initdata; 186 static int __init default_appraise_policy_setup(char *str) 187 { 188 ima_use_appraise_tcb = 1; 189 return 1; 190 } 191 __setup("ima_appraise_tcb", default_appraise_policy_setup); 192 193 /* 194 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring 195 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect 196 * the reloaded LSM policy. We assume the rules still exist; and BUG_ON() if 197 * they don't. 198 */ 199 static void ima_lsm_update_rules(void) 200 { 201 struct ima_rule_entry *entry; 202 int result; 203 int i; 204 205 list_for_each_entry(entry, &ima_policy_rules, list) { 206 for (i = 0; i < MAX_LSM_RULES; i++) { 207 if (!entry->lsm[i].rule) 208 continue; 209 result = security_filter_rule_init(entry->lsm[i].type, 210 Audit_equal, 211 entry->lsm[i].args_p, 212 &entry->lsm[i].rule); 213 BUG_ON(!entry->lsm[i].rule); 214 } 215 } 216 } 217 218 /** 219 * ima_match_rules - determine whether an inode matches the measure rule. 220 * @rule: a pointer to a rule 221 * @inode: a pointer to an inode 222 * @func: LIM hook identifier 223 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC) 224 * 225 * Returns true on rule match, false on failure. 226 */ 227 static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode, 228 enum ima_hooks func, int mask) 229 { 230 struct task_struct *tsk = current; 231 const struct cred *cred = current_cred(); 232 int i; 233 234 if ((rule->flags & IMA_FUNC) && 235 (rule->func != func && func != POST_SETATTR)) 236 return false; 237 if ((rule->flags & IMA_MASK) && 238 (rule->mask != mask && func != POST_SETATTR)) 239 return false; 240 if ((rule->flags & IMA_INMASK) && 241 (!(rule->mask & mask) && func != POST_SETATTR)) 242 return false; 243 if ((rule->flags & IMA_FSMAGIC) 244 && rule->fsmagic != inode->i_sb->s_magic) 245 return false; 246 if ((rule->flags & IMA_FSUUID) && 247 memcmp(rule->fsuuid, inode->i_sb->s_uuid, sizeof(rule->fsuuid))) 248 return false; 249 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid)) 250 return false; 251 if (rule->flags & IMA_EUID) { 252 if (has_capability_noaudit(current, CAP_SETUID)) { 253 if (!rule->uid_op(cred->euid, rule->uid) 254 && !rule->uid_op(cred->suid, rule->uid) 255 && !rule->uid_op(cred->uid, rule->uid)) 256 return false; 257 } else if (!rule->uid_op(cred->euid, rule->uid)) 258 return false; 259 } 260 261 if ((rule->flags & IMA_FOWNER) && 262 !rule->fowner_op(inode->i_uid, rule->fowner)) 263 return false; 264 for (i = 0; i < MAX_LSM_RULES; i++) { 265 int rc = 0; 266 u32 osid, sid; 267 int retried = 0; 268 269 if (!rule->lsm[i].rule) 270 continue; 271 retry: 272 switch (i) { 273 case LSM_OBJ_USER: 274 case LSM_OBJ_ROLE: 275 case LSM_OBJ_TYPE: 276 security_inode_getsecid(inode, &osid); 277 rc = security_filter_rule_match(osid, 278 rule->lsm[i].type, 279 Audit_equal, 280 rule->lsm[i].rule, 281 NULL); 282 break; 283 case LSM_SUBJ_USER: 284 case LSM_SUBJ_ROLE: 285 case LSM_SUBJ_TYPE: 286 security_task_getsecid(tsk, &sid); 287 rc = security_filter_rule_match(sid, 288 rule->lsm[i].type, 289 Audit_equal, 290 rule->lsm[i].rule, 291 NULL); 292 default: 293 break; 294 } 295 if ((rc < 0) && (!retried)) { 296 retried = 1; 297 ima_lsm_update_rules(); 298 goto retry; 299 } 300 if (!rc) 301 return false; 302 } 303 return true; 304 } 305 306 /* 307 * In addition to knowing that we need to appraise the file in general, 308 * we need to differentiate between calling hooks, for hook specific rules. 309 */ 310 static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func) 311 { 312 if (!(rule->flags & IMA_FUNC)) 313 return IMA_FILE_APPRAISE; 314 315 switch (func) { 316 case MMAP_CHECK: 317 return IMA_MMAP_APPRAISE; 318 case BPRM_CHECK: 319 return IMA_BPRM_APPRAISE; 320 case FILE_CHECK: 321 case POST_SETATTR: 322 return IMA_FILE_APPRAISE; 323 case MODULE_CHECK ... MAX_CHECK - 1: 324 default: 325 return IMA_READ_APPRAISE; 326 } 327 } 328 329 /** 330 * ima_match_policy - decision based on LSM and other conditions 331 * @inode: pointer to an inode for which the policy decision is being made 332 * @func: IMA hook identifier 333 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC) 334 * @pcr: set the pcr to extend 335 * 336 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type) 337 * conditions. 338 * 339 * Since the IMA policy may be updated multiple times we need to lock the 340 * list when walking it. Reads are many orders of magnitude more numerous 341 * than writes so ima_match_policy() is classical RCU candidate. 342 */ 343 int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask, 344 int flags, int *pcr) 345 { 346 struct ima_rule_entry *entry; 347 int action = 0, actmask = flags | (flags << 1); 348 349 rcu_read_lock(); 350 list_for_each_entry_rcu(entry, ima_rules, list) { 351 352 if (!(entry->action & actmask)) 353 continue; 354 355 if (!ima_match_rules(entry, inode, func, mask)) 356 continue; 357 358 action |= entry->flags & IMA_ACTION_FLAGS; 359 360 action |= entry->action & IMA_DO_MASK; 361 if (entry->action & IMA_APPRAISE) 362 action |= get_subaction(entry, func); 363 364 if (entry->action & IMA_DO_MASK) 365 actmask &= ~(entry->action | entry->action << 1); 366 else 367 actmask &= ~(entry->action | entry->action >> 1); 368 369 if ((pcr) && (entry->flags & IMA_PCR)) 370 *pcr = entry->pcr; 371 372 if (!actmask) 373 break; 374 } 375 rcu_read_unlock(); 376 377 return action; 378 } 379 380 /* 381 * Initialize the ima_policy_flag variable based on the currently 382 * loaded policy. Based on this flag, the decision to short circuit 383 * out of a function or not call the function in the first place 384 * can be made earlier. 385 */ 386 void ima_update_policy_flag(void) 387 { 388 struct ima_rule_entry *entry; 389 390 list_for_each_entry(entry, ima_rules, list) { 391 if (entry->action & IMA_DO_MASK) 392 ima_policy_flag |= entry->action; 393 } 394 395 ima_appraise |= temp_ima_appraise; 396 if (!ima_appraise) 397 ima_policy_flag &= ~IMA_APPRAISE; 398 } 399 400 /** 401 * ima_init_policy - initialize the default measure rules. 402 * 403 * ima_rules points to either the ima_default_rules or the 404 * the new ima_policy_rules. 405 */ 406 void __init ima_init_policy(void) 407 { 408 int i, measure_entries, appraise_entries; 409 410 /* if !ima_policy set entries = 0 so we load NO default rules */ 411 measure_entries = ima_policy ? ARRAY_SIZE(dont_measure_rules) : 0; 412 appraise_entries = ima_use_appraise_tcb ? 413 ARRAY_SIZE(default_appraise_rules) : 0; 414 415 for (i = 0; i < measure_entries; i++) 416 list_add_tail(&dont_measure_rules[i].list, &ima_default_rules); 417 418 switch (ima_policy) { 419 case ORIGINAL_TCB: 420 for (i = 0; i < ARRAY_SIZE(original_measurement_rules); i++) 421 list_add_tail(&original_measurement_rules[i].list, 422 &ima_default_rules); 423 break; 424 case DEFAULT_TCB: 425 for (i = 0; i < ARRAY_SIZE(default_measurement_rules); i++) 426 list_add_tail(&default_measurement_rules[i].list, 427 &ima_default_rules); 428 default: 429 break; 430 } 431 432 for (i = 0; i < appraise_entries; i++) { 433 list_add_tail(&default_appraise_rules[i].list, 434 &ima_default_rules); 435 if (default_appraise_rules[i].func == POLICY_CHECK) 436 temp_ima_appraise |= IMA_APPRAISE_POLICY; 437 } 438 439 ima_rules = &ima_default_rules; 440 ima_update_policy_flag(); 441 } 442 443 /* Make sure we have a valid policy, at least containing some rules. */ 444 int ima_check_policy(void) 445 { 446 if (list_empty(&ima_temp_rules)) 447 return -EINVAL; 448 return 0; 449 } 450 451 /** 452 * ima_update_policy - update default_rules with new measure rules 453 * 454 * Called on file .release to update the default rules with a complete new 455 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so 456 * they make a queue. The policy may be updated multiple times and this is the 457 * RCU updater. 458 * 459 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when 460 * we switch from the default policy to user defined. 461 */ 462 void ima_update_policy(void) 463 { 464 struct list_head *first, *last, *policy; 465 466 /* append current policy with the new rules */ 467 first = (&ima_temp_rules)->next; 468 last = (&ima_temp_rules)->prev; 469 policy = &ima_policy_rules; 470 471 synchronize_rcu(); 472 473 last->next = policy; 474 rcu_assign_pointer(list_next_rcu(policy->prev), first); 475 first->prev = policy->prev; 476 policy->prev = last; 477 478 /* prepare for the next policy rules addition */ 479 INIT_LIST_HEAD(&ima_temp_rules); 480 481 if (ima_rules != policy) { 482 ima_policy_flag = 0; 483 ima_rules = policy; 484 } 485 ima_update_policy_flag(); 486 } 487 488 enum { 489 Opt_err = -1, 490 Opt_measure = 1, Opt_dont_measure, 491 Opt_appraise, Opt_dont_appraise, 492 Opt_audit, 493 Opt_obj_user, Opt_obj_role, Opt_obj_type, 494 Opt_subj_user, Opt_subj_role, Opt_subj_type, 495 Opt_func, Opt_mask, Opt_fsmagic, 496 Opt_fsuuid, Opt_uid_eq, Opt_euid_eq, Opt_fowner_eq, 497 Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt, 498 Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt, 499 Opt_appraise_type, Opt_permit_directio, 500 Opt_pcr 501 }; 502 503 static match_table_t policy_tokens = { 504 {Opt_measure, "measure"}, 505 {Opt_dont_measure, "dont_measure"}, 506 {Opt_appraise, "appraise"}, 507 {Opt_dont_appraise, "dont_appraise"}, 508 {Opt_audit, "audit"}, 509 {Opt_obj_user, "obj_user=%s"}, 510 {Opt_obj_role, "obj_role=%s"}, 511 {Opt_obj_type, "obj_type=%s"}, 512 {Opt_subj_user, "subj_user=%s"}, 513 {Opt_subj_role, "subj_role=%s"}, 514 {Opt_subj_type, "subj_type=%s"}, 515 {Opt_func, "func=%s"}, 516 {Opt_mask, "mask=%s"}, 517 {Opt_fsmagic, "fsmagic=%s"}, 518 {Opt_fsuuid, "fsuuid=%s"}, 519 {Opt_uid_eq, "uid=%s"}, 520 {Opt_euid_eq, "euid=%s"}, 521 {Opt_fowner_eq, "fowner=%s"}, 522 {Opt_uid_gt, "uid>%s"}, 523 {Opt_euid_gt, "euid>%s"}, 524 {Opt_fowner_gt, "fowner>%s"}, 525 {Opt_uid_lt, "uid<%s"}, 526 {Opt_euid_lt, "euid<%s"}, 527 {Opt_fowner_lt, "fowner<%s"}, 528 {Opt_appraise_type, "appraise_type=%s"}, 529 {Opt_permit_directio, "permit_directio"}, 530 {Opt_pcr, "pcr=%s"}, 531 {Opt_err, NULL} 532 }; 533 534 static int ima_lsm_rule_init(struct ima_rule_entry *entry, 535 substring_t *args, int lsm_rule, int audit_type) 536 { 537 int result; 538 539 if (entry->lsm[lsm_rule].rule) 540 return -EINVAL; 541 542 entry->lsm[lsm_rule].args_p = match_strdup(args); 543 if (!entry->lsm[lsm_rule].args_p) 544 return -ENOMEM; 545 546 entry->lsm[lsm_rule].type = audit_type; 547 result = security_filter_rule_init(entry->lsm[lsm_rule].type, 548 Audit_equal, 549 entry->lsm[lsm_rule].args_p, 550 &entry->lsm[lsm_rule].rule); 551 if (!entry->lsm[lsm_rule].rule) { 552 kfree(entry->lsm[lsm_rule].args_p); 553 return -EINVAL; 554 } 555 556 return result; 557 } 558 559 static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value, 560 bool (*rule_operator)(kuid_t, kuid_t)) 561 { 562 if (rule_operator == &uid_gt) 563 audit_log_format(ab, "%s>", key); 564 else if (rule_operator == &uid_lt) 565 audit_log_format(ab, "%s<", key); 566 else 567 audit_log_format(ab, "%s=", key); 568 audit_log_untrustedstring(ab, value); 569 audit_log_format(ab, " "); 570 } 571 static void ima_log_string(struct audit_buffer *ab, char *key, char *value) 572 { 573 ima_log_string_op(ab, key, value, NULL); 574 } 575 576 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) 577 { 578 struct audit_buffer *ab; 579 char *from; 580 char *p; 581 bool uid_token; 582 int result = 0; 583 584 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE); 585 586 entry->uid = INVALID_UID; 587 entry->fowner = INVALID_UID; 588 entry->uid_op = &uid_eq; 589 entry->fowner_op = &uid_eq; 590 entry->action = UNKNOWN; 591 while ((p = strsep(&rule, " \t")) != NULL) { 592 substring_t args[MAX_OPT_ARGS]; 593 int token; 594 unsigned long lnum; 595 596 if (result < 0) 597 break; 598 if ((*p == '\0') || (*p == ' ') || (*p == '\t')) 599 continue; 600 token = match_token(p, policy_tokens, args); 601 switch (token) { 602 case Opt_measure: 603 ima_log_string(ab, "action", "measure"); 604 605 if (entry->action != UNKNOWN) 606 result = -EINVAL; 607 608 entry->action = MEASURE; 609 break; 610 case Opt_dont_measure: 611 ima_log_string(ab, "action", "dont_measure"); 612 613 if (entry->action != UNKNOWN) 614 result = -EINVAL; 615 616 entry->action = DONT_MEASURE; 617 break; 618 case Opt_appraise: 619 ima_log_string(ab, "action", "appraise"); 620 621 if (entry->action != UNKNOWN) 622 result = -EINVAL; 623 624 entry->action = APPRAISE; 625 break; 626 case Opt_dont_appraise: 627 ima_log_string(ab, "action", "dont_appraise"); 628 629 if (entry->action != UNKNOWN) 630 result = -EINVAL; 631 632 entry->action = DONT_APPRAISE; 633 break; 634 case Opt_audit: 635 ima_log_string(ab, "action", "audit"); 636 637 if (entry->action != UNKNOWN) 638 result = -EINVAL; 639 640 entry->action = AUDIT; 641 break; 642 case Opt_func: 643 ima_log_string(ab, "func", args[0].from); 644 645 if (entry->func) 646 result = -EINVAL; 647 648 if (strcmp(args[0].from, "FILE_CHECK") == 0) 649 entry->func = FILE_CHECK; 650 /* PATH_CHECK is for backwards compat */ 651 else if (strcmp(args[0].from, "PATH_CHECK") == 0) 652 entry->func = FILE_CHECK; 653 else if (strcmp(args[0].from, "MODULE_CHECK") == 0) 654 entry->func = MODULE_CHECK; 655 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0) 656 entry->func = FIRMWARE_CHECK; 657 else if ((strcmp(args[0].from, "FILE_MMAP") == 0) 658 || (strcmp(args[0].from, "MMAP_CHECK") == 0)) 659 entry->func = MMAP_CHECK; 660 else if (strcmp(args[0].from, "BPRM_CHECK") == 0) 661 entry->func = BPRM_CHECK; 662 else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") == 663 0) 664 entry->func = KEXEC_KERNEL_CHECK; 665 else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK") 666 == 0) 667 entry->func = KEXEC_INITRAMFS_CHECK; 668 else if (strcmp(args[0].from, "POLICY_CHECK") == 0) 669 entry->func = POLICY_CHECK; 670 else 671 result = -EINVAL; 672 if (!result) 673 entry->flags |= IMA_FUNC; 674 break; 675 case Opt_mask: 676 ima_log_string(ab, "mask", args[0].from); 677 678 if (entry->mask) 679 result = -EINVAL; 680 681 from = args[0].from; 682 if (*from == '^') 683 from++; 684 685 if ((strcmp(from, "MAY_EXEC")) == 0) 686 entry->mask = MAY_EXEC; 687 else if (strcmp(from, "MAY_WRITE") == 0) 688 entry->mask = MAY_WRITE; 689 else if (strcmp(from, "MAY_READ") == 0) 690 entry->mask = MAY_READ; 691 else if (strcmp(from, "MAY_APPEND") == 0) 692 entry->mask = MAY_APPEND; 693 else 694 result = -EINVAL; 695 if (!result) 696 entry->flags |= (*args[0].from == '^') 697 ? IMA_INMASK : IMA_MASK; 698 break; 699 case Opt_fsmagic: 700 ima_log_string(ab, "fsmagic", args[0].from); 701 702 if (entry->fsmagic) { 703 result = -EINVAL; 704 break; 705 } 706 707 result = kstrtoul(args[0].from, 16, &entry->fsmagic); 708 if (!result) 709 entry->flags |= IMA_FSMAGIC; 710 break; 711 case Opt_fsuuid: 712 ima_log_string(ab, "fsuuid", args[0].from); 713 714 if (memchr_inv(entry->fsuuid, 0x00, 715 sizeof(entry->fsuuid))) { 716 result = -EINVAL; 717 break; 718 } 719 720 result = blk_part_pack_uuid(args[0].from, 721 entry->fsuuid); 722 if (!result) 723 entry->flags |= IMA_FSUUID; 724 break; 725 case Opt_uid_gt: 726 case Opt_euid_gt: 727 entry->uid_op = &uid_gt; 728 case Opt_uid_lt: 729 case Opt_euid_lt: 730 if ((token == Opt_uid_lt) || (token == Opt_euid_lt)) 731 entry->uid_op = &uid_lt; 732 case Opt_uid_eq: 733 case Opt_euid_eq: 734 uid_token = (token == Opt_uid_eq) || 735 (token == Opt_uid_gt) || 736 (token == Opt_uid_lt); 737 738 ima_log_string_op(ab, uid_token ? "uid" : "euid", 739 args[0].from, entry->uid_op); 740 741 if (uid_valid(entry->uid)) { 742 result = -EINVAL; 743 break; 744 } 745 746 result = kstrtoul(args[0].from, 10, &lnum); 747 if (!result) { 748 entry->uid = make_kuid(current_user_ns(), 749 (uid_t) lnum); 750 if (!uid_valid(entry->uid) || 751 (uid_t)lnum != lnum) 752 result = -EINVAL; 753 else 754 entry->flags |= uid_token 755 ? IMA_UID : IMA_EUID; 756 } 757 break; 758 case Opt_fowner_gt: 759 entry->fowner_op = &uid_gt; 760 case Opt_fowner_lt: 761 if (token == Opt_fowner_lt) 762 entry->fowner_op = &uid_lt; 763 case Opt_fowner_eq: 764 ima_log_string_op(ab, "fowner", args[0].from, 765 entry->fowner_op); 766 767 if (uid_valid(entry->fowner)) { 768 result = -EINVAL; 769 break; 770 } 771 772 result = kstrtoul(args[0].from, 10, &lnum); 773 if (!result) { 774 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum); 775 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum)) 776 result = -EINVAL; 777 else 778 entry->flags |= IMA_FOWNER; 779 } 780 break; 781 case Opt_obj_user: 782 ima_log_string(ab, "obj_user", args[0].from); 783 result = ima_lsm_rule_init(entry, args, 784 LSM_OBJ_USER, 785 AUDIT_OBJ_USER); 786 break; 787 case Opt_obj_role: 788 ima_log_string(ab, "obj_role", args[0].from); 789 result = ima_lsm_rule_init(entry, args, 790 LSM_OBJ_ROLE, 791 AUDIT_OBJ_ROLE); 792 break; 793 case Opt_obj_type: 794 ima_log_string(ab, "obj_type", args[0].from); 795 result = ima_lsm_rule_init(entry, args, 796 LSM_OBJ_TYPE, 797 AUDIT_OBJ_TYPE); 798 break; 799 case Opt_subj_user: 800 ima_log_string(ab, "subj_user", args[0].from); 801 result = ima_lsm_rule_init(entry, args, 802 LSM_SUBJ_USER, 803 AUDIT_SUBJ_USER); 804 break; 805 case Opt_subj_role: 806 ima_log_string(ab, "subj_role", args[0].from); 807 result = ima_lsm_rule_init(entry, args, 808 LSM_SUBJ_ROLE, 809 AUDIT_SUBJ_ROLE); 810 break; 811 case Opt_subj_type: 812 ima_log_string(ab, "subj_type", args[0].from); 813 result = ima_lsm_rule_init(entry, args, 814 LSM_SUBJ_TYPE, 815 AUDIT_SUBJ_TYPE); 816 break; 817 case Opt_appraise_type: 818 if (entry->action != APPRAISE) { 819 result = -EINVAL; 820 break; 821 } 822 823 ima_log_string(ab, "appraise_type", args[0].from); 824 if ((strcmp(args[0].from, "imasig")) == 0) 825 entry->flags |= IMA_DIGSIG_REQUIRED; 826 else 827 result = -EINVAL; 828 break; 829 case Opt_permit_directio: 830 entry->flags |= IMA_PERMIT_DIRECTIO; 831 break; 832 case Opt_pcr: 833 if (entry->action != MEASURE) { 834 result = -EINVAL; 835 break; 836 } 837 ima_log_string(ab, "pcr", args[0].from); 838 839 result = kstrtoint(args[0].from, 10, &entry->pcr); 840 if (result || INVALID_PCR(entry->pcr)) 841 result = -EINVAL; 842 else 843 entry->flags |= IMA_PCR; 844 845 break; 846 case Opt_err: 847 ima_log_string(ab, "UNKNOWN", p); 848 result = -EINVAL; 849 break; 850 } 851 } 852 if (!result && (entry->action == UNKNOWN)) 853 result = -EINVAL; 854 else if (entry->func == MODULE_CHECK) 855 temp_ima_appraise |= IMA_APPRAISE_MODULES; 856 else if (entry->func == FIRMWARE_CHECK) 857 temp_ima_appraise |= IMA_APPRAISE_FIRMWARE; 858 else if (entry->func == POLICY_CHECK) 859 temp_ima_appraise |= IMA_APPRAISE_POLICY; 860 audit_log_format(ab, "res=%d", !result); 861 audit_log_end(ab); 862 return result; 863 } 864 865 /** 866 * ima_parse_add_rule - add a rule to ima_policy_rules 867 * @rule - ima measurement policy rule 868 * 869 * Avoid locking by allowing just one writer at a time in ima_write_policy() 870 * Returns the length of the rule parsed, an error code on failure 871 */ 872 ssize_t ima_parse_add_rule(char *rule) 873 { 874 static const char op[] = "update_policy"; 875 char *p; 876 struct ima_rule_entry *entry; 877 ssize_t result, len; 878 int audit_info = 0; 879 880 p = strsep(&rule, "\n"); 881 len = strlen(p) + 1; 882 p += strspn(p, " \t"); 883 884 if (*p == '#' || *p == '\0') 885 return len; 886 887 entry = kzalloc(sizeof(*entry), GFP_KERNEL); 888 if (!entry) { 889 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, 890 NULL, op, "-ENOMEM", -ENOMEM, audit_info); 891 return -ENOMEM; 892 } 893 894 INIT_LIST_HEAD(&entry->list); 895 896 result = ima_parse_rule(p, entry); 897 if (result) { 898 kfree(entry); 899 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, 900 NULL, op, "invalid-policy", result, 901 audit_info); 902 return result; 903 } 904 905 list_add_tail(&entry->list, &ima_temp_rules); 906 907 return len; 908 } 909 910 /** 911 * ima_delete_rules() called to cleanup invalid in-flight policy. 912 * We don't need locking as we operate on the temp list, which is 913 * different from the active one. There is also only one user of 914 * ima_delete_rules() at a time. 915 */ 916 void ima_delete_rules(void) 917 { 918 struct ima_rule_entry *entry, *tmp; 919 int i; 920 921 temp_ima_appraise = 0; 922 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) { 923 for (i = 0; i < MAX_LSM_RULES; i++) 924 kfree(entry->lsm[i].args_p); 925 926 list_del(&entry->list); 927 kfree(entry); 928 } 929 } 930 931 #ifdef CONFIG_IMA_READ_POLICY 932 enum { 933 mask_exec = 0, mask_write, mask_read, mask_append 934 }; 935 936 static char *mask_tokens[] = { 937 "MAY_EXEC", 938 "MAY_WRITE", 939 "MAY_READ", 940 "MAY_APPEND" 941 }; 942 943 enum { 944 func_file = 0, func_mmap, func_bprm, 945 func_module, func_firmware, func_post, 946 func_kexec_kernel, func_kexec_initramfs, 947 func_policy 948 }; 949 950 static char *func_tokens[] = { 951 "FILE_CHECK", 952 "MMAP_CHECK", 953 "BPRM_CHECK", 954 "MODULE_CHECK", 955 "FIRMWARE_CHECK", 956 "POST_SETATTR", 957 "KEXEC_KERNEL_CHECK", 958 "KEXEC_INITRAMFS_CHECK", 959 "POLICY_CHECK" 960 }; 961 962 void *ima_policy_start(struct seq_file *m, loff_t *pos) 963 { 964 loff_t l = *pos; 965 struct ima_rule_entry *entry; 966 967 rcu_read_lock(); 968 list_for_each_entry_rcu(entry, ima_rules, list) { 969 if (!l--) { 970 rcu_read_unlock(); 971 return entry; 972 } 973 } 974 rcu_read_unlock(); 975 return NULL; 976 } 977 978 void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos) 979 { 980 struct ima_rule_entry *entry = v; 981 982 rcu_read_lock(); 983 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list); 984 rcu_read_unlock(); 985 (*pos)++; 986 987 return (&entry->list == ima_rules) ? NULL : entry; 988 } 989 990 void ima_policy_stop(struct seq_file *m, void *v) 991 { 992 } 993 994 #define pt(token) policy_tokens[token + Opt_err].pattern 995 #define mt(token) mask_tokens[token] 996 #define ft(token) func_tokens[token] 997 998 /* 999 * policy_func_show - display the ima_hooks policy rule 1000 */ 1001 static void policy_func_show(struct seq_file *m, enum ima_hooks func) 1002 { 1003 char tbuf[64] = {0,}; 1004 1005 switch (func) { 1006 case FILE_CHECK: 1007 seq_printf(m, pt(Opt_func), ft(func_file)); 1008 break; 1009 case MMAP_CHECK: 1010 seq_printf(m, pt(Opt_func), ft(func_mmap)); 1011 break; 1012 case BPRM_CHECK: 1013 seq_printf(m, pt(Opt_func), ft(func_bprm)); 1014 break; 1015 case MODULE_CHECK: 1016 seq_printf(m, pt(Opt_func), ft(func_module)); 1017 break; 1018 case FIRMWARE_CHECK: 1019 seq_printf(m, pt(Opt_func), ft(func_firmware)); 1020 break; 1021 case POST_SETATTR: 1022 seq_printf(m, pt(Opt_func), ft(func_post)); 1023 break; 1024 case KEXEC_KERNEL_CHECK: 1025 seq_printf(m, pt(Opt_func), ft(func_kexec_kernel)); 1026 break; 1027 case KEXEC_INITRAMFS_CHECK: 1028 seq_printf(m, pt(Opt_func), ft(func_kexec_initramfs)); 1029 break; 1030 case POLICY_CHECK: 1031 seq_printf(m, pt(Opt_func), ft(func_policy)); 1032 break; 1033 default: 1034 snprintf(tbuf, sizeof(tbuf), "%d", func); 1035 seq_printf(m, pt(Opt_func), tbuf); 1036 break; 1037 } 1038 seq_puts(m, " "); 1039 } 1040 1041 int ima_policy_show(struct seq_file *m, void *v) 1042 { 1043 struct ima_rule_entry *entry = v; 1044 int i; 1045 char tbuf[64] = {0,}; 1046 1047 rcu_read_lock(); 1048 1049 if (entry->action & MEASURE) 1050 seq_puts(m, pt(Opt_measure)); 1051 if (entry->action & DONT_MEASURE) 1052 seq_puts(m, pt(Opt_dont_measure)); 1053 if (entry->action & APPRAISE) 1054 seq_puts(m, pt(Opt_appraise)); 1055 if (entry->action & DONT_APPRAISE) 1056 seq_puts(m, pt(Opt_dont_appraise)); 1057 if (entry->action & AUDIT) 1058 seq_puts(m, pt(Opt_audit)); 1059 1060 seq_puts(m, " "); 1061 1062 if (entry->flags & IMA_FUNC) 1063 policy_func_show(m, entry->func); 1064 1065 if (entry->flags & IMA_MASK) { 1066 if (entry->mask & MAY_EXEC) 1067 seq_printf(m, pt(Opt_mask), mt(mask_exec)); 1068 if (entry->mask & MAY_WRITE) 1069 seq_printf(m, pt(Opt_mask), mt(mask_write)); 1070 if (entry->mask & MAY_READ) 1071 seq_printf(m, pt(Opt_mask), mt(mask_read)); 1072 if (entry->mask & MAY_APPEND) 1073 seq_printf(m, pt(Opt_mask), mt(mask_append)); 1074 seq_puts(m, " "); 1075 } 1076 1077 if (entry->flags & IMA_FSMAGIC) { 1078 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic); 1079 seq_printf(m, pt(Opt_fsmagic), tbuf); 1080 seq_puts(m, " "); 1081 } 1082 1083 if (entry->flags & IMA_PCR) { 1084 snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr); 1085 seq_printf(m, pt(Opt_pcr), tbuf); 1086 seq_puts(m, " "); 1087 } 1088 1089 if (entry->flags & IMA_FSUUID) { 1090 seq_printf(m, "fsuuid=%pU", entry->fsuuid); 1091 seq_puts(m, " "); 1092 } 1093 1094 if (entry->flags & IMA_UID) { 1095 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid)); 1096 if (entry->uid_op == &uid_gt) 1097 seq_printf(m, pt(Opt_uid_gt), tbuf); 1098 else if (entry->uid_op == &uid_lt) 1099 seq_printf(m, pt(Opt_uid_lt), tbuf); 1100 else 1101 seq_printf(m, pt(Opt_uid_eq), tbuf); 1102 seq_puts(m, " "); 1103 } 1104 1105 if (entry->flags & IMA_EUID) { 1106 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid)); 1107 if (entry->uid_op == &uid_gt) 1108 seq_printf(m, pt(Opt_euid_gt), tbuf); 1109 else if (entry->uid_op == &uid_lt) 1110 seq_printf(m, pt(Opt_euid_lt), tbuf); 1111 else 1112 seq_printf(m, pt(Opt_euid_eq), tbuf); 1113 seq_puts(m, " "); 1114 } 1115 1116 if (entry->flags & IMA_FOWNER) { 1117 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner)); 1118 if (entry->fowner_op == &uid_gt) 1119 seq_printf(m, pt(Opt_fowner_gt), tbuf); 1120 else if (entry->fowner_op == &uid_lt) 1121 seq_printf(m, pt(Opt_fowner_lt), tbuf); 1122 else 1123 seq_printf(m, pt(Opt_fowner_eq), tbuf); 1124 seq_puts(m, " "); 1125 } 1126 1127 for (i = 0; i < MAX_LSM_RULES; i++) { 1128 if (entry->lsm[i].rule) { 1129 switch (i) { 1130 case LSM_OBJ_USER: 1131 seq_printf(m, pt(Opt_obj_user), 1132 (char *)entry->lsm[i].args_p); 1133 break; 1134 case LSM_OBJ_ROLE: 1135 seq_printf(m, pt(Opt_obj_role), 1136 (char *)entry->lsm[i].args_p); 1137 break; 1138 case LSM_OBJ_TYPE: 1139 seq_printf(m, pt(Opt_obj_type), 1140 (char *)entry->lsm[i].args_p); 1141 break; 1142 case LSM_SUBJ_USER: 1143 seq_printf(m, pt(Opt_subj_user), 1144 (char *)entry->lsm[i].args_p); 1145 break; 1146 case LSM_SUBJ_ROLE: 1147 seq_printf(m, pt(Opt_subj_role), 1148 (char *)entry->lsm[i].args_p); 1149 break; 1150 case LSM_SUBJ_TYPE: 1151 seq_printf(m, pt(Opt_subj_type), 1152 (char *)entry->lsm[i].args_p); 1153 break; 1154 } 1155 } 1156 } 1157 if (entry->flags & IMA_DIGSIG_REQUIRED) 1158 seq_puts(m, "appraise_type=imasig "); 1159 if (entry->flags & IMA_PERMIT_DIRECTIO) 1160 seq_puts(m, "permit_directio "); 1161 rcu_read_unlock(); 1162 seq_puts(m, "\n"); 1163 return 0; 1164 } 1165 #endif /* CONFIG_IMA_READ_POLICY */ 1166