hc
2024-01-03 2f7c68cb55ecb7331f2381deb497c27155f32faf
kernel/security/integrity/ima/ima_policy.c
....@@ -370,12 +370,6 @@
370370
371371 nentry->lsm[i].type = entry->lsm[i].type;
372372 nentry->lsm[i].args_p = entry->lsm[i].args_p;
373
- /*
374
- * Remove the reference from entry so that the associated
375
- * memory will not be freed during a later call to
376
- * ima_lsm_free_rule(entry).
377
- */
378
- entry->lsm[i].args_p = NULL;
379373
380374 ima_filter_rule_init(nentry->lsm[i].type, Audit_equal,
381375 nentry->lsm[i].args_p,
....@@ -389,6 +383,7 @@
389383
390384 static int ima_lsm_update_rule(struct ima_rule_entry *entry)
391385 {
386
+ int i;
392387 struct ima_rule_entry *nentry;
393388
394389 nentry = ima_lsm_copy_rule(entry);
....@@ -403,7 +398,8 @@
403398 * references and the entry itself. All other memory refrences will now
404399 * be owned by nentry.
405400 */
406
- ima_lsm_free_rule(entry);
401
+ for (i = 0; i < MAX_LSM_RULES; i++)
402
+ ima_filter_rule_free(entry->lsm[i].rule);
407403 kfree(entry);
408404
409405 return 0;
....@@ -503,6 +499,9 @@
503499 const char *keyring)
504500 {
505501 int i;
502
+ bool result = false;
503
+ struct ima_rule_entry *lsm_rule = rule;
504
+ bool rule_reinitialized = false;
506505
507506 if (func == KEY_CHECK) {
508507 return (rule->flags & IMA_FUNC) && (rule->func == func) &&
....@@ -545,34 +544,55 @@
545544 int rc = 0;
546545 u32 osid;
547546
548
- if (!rule->lsm[i].rule) {
549
- if (!rule->lsm[i].args_p)
547
+ if (!lsm_rule->lsm[i].rule) {
548
+ if (!lsm_rule->lsm[i].args_p)
550549 continue;
551550 else
552551 return false;
553552 }
553
+
554
+retry:
554555 switch (i) {
555556 case LSM_OBJ_USER:
556557 case LSM_OBJ_ROLE:
557558 case LSM_OBJ_TYPE:
558559 security_inode_getsecid(inode, &osid);
559
- rc = ima_filter_rule_match(osid, rule->lsm[i].type,
560
+ rc = ima_filter_rule_match(osid, lsm_rule->lsm[i].type,
560561 Audit_equal,
561
- rule->lsm[i].rule);
562
+ lsm_rule->lsm[i].rule);
562563 break;
563564 case LSM_SUBJ_USER:
564565 case LSM_SUBJ_ROLE:
565566 case LSM_SUBJ_TYPE:
566
- rc = ima_filter_rule_match(secid, rule->lsm[i].type,
567
+ rc = ima_filter_rule_match(secid, lsm_rule->lsm[i].type,
567568 Audit_equal,
568
- rule->lsm[i].rule);
569
+ lsm_rule->lsm[i].rule);
570
+ break;
569571 default:
570572 break;
571573 }
572
- if (!rc)
573
- return false;
574
+
575
+ if (rc == -ESTALE && !rule_reinitialized) {
576
+ lsm_rule = ima_lsm_copy_rule(rule);
577
+ if (lsm_rule) {
578
+ rule_reinitialized = true;
579
+ goto retry;
580
+ }
581
+ }
582
+ if (!rc) {
583
+ result = false;
584
+ goto out;
585
+ }
574586 }
575
- return true;
587
+ result = true;
588
+
589
+out:
590
+ if (rule_reinitialized) {
591
+ for (i = 0; i < MAX_LSM_RULES; i++)
592
+ ima_filter_rule_free(lsm_rule->lsm[i].rule);
593
+ kfree(lsm_rule);
594
+ }
595
+ return result;
576596 }
577597
578598 /*
....@@ -608,6 +628,7 @@
608628 * @secid: LSM secid of the task to be validated
609629 * @func: IMA hook identifier
610630 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
631
+ * @flags: IMA actions to consider (e.g. IMA_MEASURE | IMA_APPRAISE)
611632 * @pcr: set the pcr to extend
612633 * @template_desc: the template that should be used for this rule
613634 * @keyring: the keyring name, if given, to be used to check in the policy.
....@@ -802,6 +823,7 @@
802823 add_rules(default_measurement_rules,
803824 ARRAY_SIZE(default_measurement_rules),
804825 IMA_DEFAULT_POLICY);
826
+ break;
805827 default:
806828 break;
807829 }
....@@ -1494,7 +1516,7 @@
14941516
14951517 /**
14961518 * ima_parse_add_rule - add a rule to ima_policy_rules
1497
- * @rule - ima measurement policy rule
1519
+ * @rule: ima measurement policy rule
14981520 *
14991521 * Avoid locking by allowing just one writer at a time in ima_write_policy()
15001522 * Returns the length of the rule parsed, an error code on failure