ima_policy.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/audit.h>
  16. #include <linux/security.h>
  17. #include <linux/magic.h>
  18. #include "ima.h"
  19. /* flags definitions */
  20. #define IMA_FUNC 0x0001
  21. #define IMA_MASK 0x0002
  22. #define IMA_FSMAGIC 0x0004
  23. #define IMA_UID 0x0008
  24. enum ima_action { DONT_MEASURE, MEASURE };
  25. struct ima_measure_rule_entry {
  26. struct list_head list;
  27. enum ima_action action;
  28. unsigned int flags;
  29. enum ima_hooks func;
  30. int mask;
  31. unsigned long fsmagic;
  32. uid_t uid;
  33. };
  34. static struct ima_measure_rule_entry default_rules[] = {
  35. {.action = DONT_MEASURE,.fsmagic = PROC_SUPER_MAGIC,
  36. .flags = IMA_FSMAGIC},
  37. {.action = DONT_MEASURE,.fsmagic = SYSFS_MAGIC,.flags = IMA_FSMAGIC},
  38. {.action = DONT_MEASURE,.fsmagic = DEBUGFS_MAGIC,.flags = IMA_FSMAGIC},
  39. {.action = DONT_MEASURE,.fsmagic = TMPFS_MAGIC,.flags = IMA_FSMAGIC},
  40. {.action = DONT_MEASURE,.fsmagic = SECURITYFS_MAGIC,
  41. .flags = IMA_FSMAGIC},
  42. {.action = DONT_MEASURE,.fsmagic = 0xF97CFF8C,.flags = IMA_FSMAGIC},
  43. {.action = MEASURE,.func = FILE_MMAP,.mask = MAY_EXEC,
  44. .flags = IMA_FUNC | IMA_MASK},
  45. {.action = MEASURE,.func = BPRM_CHECK,.mask = MAY_EXEC,
  46. .flags = IMA_FUNC | IMA_MASK},
  47. {.action = MEASURE,.func = PATH_CHECK,.mask = MAY_READ,.uid = 0,
  48. .flags = IMA_FUNC | IMA_MASK | IMA_UID}
  49. };
  50. static LIST_HEAD(measure_default_rules);
  51. static struct list_head *ima_measure;
  52. /**
  53. * ima_match_rules - determine whether an inode matches the measure rule.
  54. * @rule: a pointer to a rule
  55. * @inode: a pointer to an inode
  56. * @func: LIM hook identifier
  57. * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
  58. *
  59. * Returns true on rule match, false on failure.
  60. */
  61. static bool ima_match_rules(struct ima_measure_rule_entry *rule,
  62. struct inode *inode, enum ima_hooks func, int mask)
  63. {
  64. struct task_struct *tsk = current;
  65. if ((rule->flags & IMA_FUNC) && rule->func != func)
  66. return false;
  67. if ((rule->flags & IMA_MASK) && rule->mask != mask)
  68. return false;
  69. if ((rule->flags & IMA_FSMAGIC)
  70. && rule->fsmagic != inode->i_sb->s_magic)
  71. return false;
  72. if ((rule->flags & IMA_UID) && rule->uid != tsk->cred->uid)
  73. return false;
  74. return true;
  75. }
  76. /**
  77. * ima_match_policy - decision based on LSM and other conditions
  78. * @inode: pointer to an inode for which the policy decision is being made
  79. * @func: IMA hook identifier
  80. * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
  81. *
  82. * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
  83. * conditions.
  84. *
  85. * (There is no need for locking when walking the policy list,
  86. * as elements in the list are never deleted, nor does the list
  87. * change.)
  88. */
  89. int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask)
  90. {
  91. struct ima_measure_rule_entry *entry;
  92. list_for_each_entry(entry, ima_measure, list) {
  93. bool rc;
  94. rc = ima_match_rules(entry, inode, func, mask);
  95. if (rc)
  96. return entry->action;
  97. }
  98. return 0;
  99. }
  100. /**
  101. * ima_init_policy - initialize the default measure rules.
  102. *
  103. * (Could use the default_rules directly, but in policy patch
  104. * ima_measure points to either the measure_default_rules or the
  105. * the new measure_policy_rules.)
  106. */
  107. void ima_init_policy(void)
  108. {
  109. int i;
  110. for (i = 0; i < ARRAY_SIZE(default_rules); i++)
  111. list_add_tail(&default_rules[i].list, &measure_default_rules);
  112. ima_measure = &measure_default_rules;
  113. }