ima_policy.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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/security.h>
  16. #include <linux/magic.h>
  17. #include <linux/parser.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 { UNKNOWN = -1, DONT_MEASURE = 0, MEASURE };
  25. #define MAX_LSM_RULES 6
  26. enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
  27. LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
  28. };
  29. struct ima_measure_rule_entry {
  30. struct list_head list;
  31. enum ima_action action;
  32. unsigned int flags;
  33. enum ima_hooks func;
  34. int mask;
  35. unsigned long fsmagic;
  36. uid_t uid;
  37. struct {
  38. void *rule; /* LSM file metadata specific */
  39. int type; /* audit type */
  40. } lsm[MAX_LSM_RULES];
  41. };
  42. /* Without LSM specific knowledge, the default policy can only be
  43. * written in terms of .action, .func, .mask, .fsmagic, and .uid
  44. */
  45. static struct ima_measure_rule_entry default_rules[] = {
  46. {.action = DONT_MEASURE,.fsmagic = PROC_SUPER_MAGIC,
  47. .flags = IMA_FSMAGIC},
  48. {.action = DONT_MEASURE,.fsmagic = SYSFS_MAGIC,.flags = IMA_FSMAGIC},
  49. {.action = DONT_MEASURE,.fsmagic = DEBUGFS_MAGIC,.flags = IMA_FSMAGIC},
  50. {.action = DONT_MEASURE,.fsmagic = TMPFS_MAGIC,.flags = IMA_FSMAGIC},
  51. {.action = DONT_MEASURE,.fsmagic = SECURITYFS_MAGIC,
  52. .flags = IMA_FSMAGIC},
  53. {.action = DONT_MEASURE,.fsmagic = 0xF97CFF8C,.flags = IMA_FSMAGIC},
  54. {.action = MEASURE,.func = FILE_MMAP,.mask = MAY_EXEC,
  55. .flags = IMA_FUNC | IMA_MASK},
  56. {.action = MEASURE,.func = BPRM_CHECK,.mask = MAY_EXEC,
  57. .flags = IMA_FUNC | IMA_MASK},
  58. {.action = MEASURE,.func = PATH_CHECK,.mask = MAY_READ,.uid = 0,
  59. .flags = IMA_FUNC | IMA_MASK | IMA_UID}
  60. };
  61. static LIST_HEAD(measure_default_rules);
  62. static LIST_HEAD(measure_policy_rules);
  63. static struct list_head *ima_measure;
  64. static DEFINE_MUTEX(ima_measure_mutex);
  65. /**
  66. * ima_match_rules - determine whether an inode matches the measure rule.
  67. * @rule: a pointer to a rule
  68. * @inode: a pointer to an inode
  69. * @func: LIM hook identifier
  70. * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
  71. *
  72. * Returns true on rule match, false on failure.
  73. */
  74. static bool ima_match_rules(struct ima_measure_rule_entry *rule,
  75. struct inode *inode, enum ima_hooks func, int mask)
  76. {
  77. struct task_struct *tsk = current;
  78. int i;
  79. if ((rule->flags & IMA_FUNC) && rule->func != func)
  80. return false;
  81. if ((rule->flags & IMA_MASK) && rule->mask != mask)
  82. return false;
  83. if ((rule->flags & IMA_FSMAGIC)
  84. && rule->fsmagic != inode->i_sb->s_magic)
  85. return false;
  86. if ((rule->flags & IMA_UID) && rule->uid != tsk->cred->uid)
  87. return false;
  88. for (i = 0; i < MAX_LSM_RULES; i++) {
  89. int rc;
  90. u32 osid, sid;
  91. if (!rule->lsm[i].rule)
  92. continue;
  93. switch (i) {
  94. case LSM_OBJ_USER:
  95. case LSM_OBJ_ROLE:
  96. case LSM_OBJ_TYPE:
  97. security_inode_getsecid(inode, &osid);
  98. rc = security_filter_rule_match(osid,
  99. rule->lsm[i].type,
  100. AUDIT_EQUAL,
  101. rule->lsm[i].rule,
  102. NULL);
  103. break;
  104. case LSM_SUBJ_USER:
  105. case LSM_SUBJ_ROLE:
  106. case LSM_SUBJ_TYPE:
  107. security_task_getsecid(tsk, &sid);
  108. rc = security_filter_rule_match(sid,
  109. rule->lsm[i].type,
  110. AUDIT_EQUAL,
  111. rule->lsm[i].rule,
  112. NULL);
  113. default:
  114. break;
  115. }
  116. if (!rc)
  117. return false;
  118. }
  119. return true;
  120. }
  121. /**
  122. * ima_match_policy - decision based on LSM and other conditions
  123. * @inode: pointer to an inode for which the policy decision is being made
  124. * @func: IMA hook identifier
  125. * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
  126. *
  127. * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
  128. * conditions.
  129. *
  130. * (There is no need for locking when walking the policy list,
  131. * as elements in the list are never deleted, nor does the list
  132. * change.)
  133. */
  134. int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask)
  135. {
  136. struct ima_measure_rule_entry *entry;
  137. list_for_each_entry(entry, ima_measure, list) {
  138. bool rc;
  139. rc = ima_match_rules(entry, inode, func, mask);
  140. if (rc)
  141. return entry->action;
  142. }
  143. return 0;
  144. }
  145. /**
  146. * ima_init_policy - initialize the default measure rules.
  147. *
  148. * ima_measure points to either the measure_default_rules or the
  149. * the new measure_policy_rules.
  150. */
  151. void ima_init_policy(void)
  152. {
  153. int i;
  154. for (i = 0; i < ARRAY_SIZE(default_rules); i++)
  155. list_add_tail(&default_rules[i].list, &measure_default_rules);
  156. ima_measure = &measure_default_rules;
  157. }
  158. /**
  159. * ima_update_policy - update default_rules with new measure rules
  160. *
  161. * Called on file .release to update the default rules with a complete new
  162. * policy. Once updated, the policy is locked, no additional rules can be
  163. * added to the policy.
  164. */
  165. void ima_update_policy(void)
  166. {
  167. const char *op = "policy_update";
  168. const char *cause = "already exists";
  169. int result = 1;
  170. int audit_info = 0;
  171. if (ima_measure == &measure_default_rules) {
  172. ima_measure = &measure_policy_rules;
  173. cause = "complete";
  174. result = 0;
  175. }
  176. integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
  177. NULL, op, cause, result, audit_info);
  178. }
  179. enum {
  180. Opt_err = -1,
  181. Opt_measure = 1, Opt_dont_measure,
  182. Opt_obj_user, Opt_obj_role, Opt_obj_type,
  183. Opt_subj_user, Opt_subj_role, Opt_subj_type,
  184. Opt_func, Opt_mask, Opt_fsmagic, Opt_uid
  185. };
  186. static match_table_t policy_tokens = {
  187. {Opt_measure, "measure"},
  188. {Opt_dont_measure, "dont_measure"},
  189. {Opt_obj_user, "obj_user=%s"},
  190. {Opt_obj_role, "obj_role=%s"},
  191. {Opt_obj_type, "obj_type=%s"},
  192. {Opt_subj_user, "subj_user=%s"},
  193. {Opt_subj_role, "subj_role=%s"},
  194. {Opt_subj_type, "subj_type=%s"},
  195. {Opt_func, "func=%s"},
  196. {Opt_mask, "mask=%s"},
  197. {Opt_fsmagic, "fsmagic=%s"},
  198. {Opt_uid, "uid=%s"},
  199. {Opt_err, NULL}
  200. };
  201. static int ima_lsm_rule_init(struct ima_measure_rule_entry *entry,
  202. char *args, int lsm_rule, int audit_type)
  203. {
  204. int result;
  205. entry->lsm[lsm_rule].type = audit_type;
  206. result = security_filter_rule_init(entry->lsm[lsm_rule].type,
  207. AUDIT_EQUAL, args,
  208. &entry->lsm[lsm_rule].rule);
  209. return result;
  210. }
  211. static int ima_parse_rule(char *rule, struct ima_measure_rule_entry *entry)
  212. {
  213. struct audit_buffer *ab;
  214. char *p;
  215. int result = 0;
  216. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
  217. entry->action = -1;
  218. while ((p = strsep(&rule, " \n")) != NULL) {
  219. substring_t args[MAX_OPT_ARGS];
  220. int token;
  221. unsigned long lnum;
  222. if (result < 0)
  223. break;
  224. if (!*p)
  225. continue;
  226. token = match_token(p, policy_tokens, args);
  227. switch (token) {
  228. case Opt_measure:
  229. audit_log_format(ab, "%s ", "measure");
  230. entry->action = MEASURE;
  231. break;
  232. case Opt_dont_measure:
  233. audit_log_format(ab, "%s ", "dont_measure");
  234. entry->action = DONT_MEASURE;
  235. break;
  236. case Opt_func:
  237. audit_log_format(ab, "func=%s ", args[0].from);
  238. if (strcmp(args[0].from, "PATH_CHECK") == 0)
  239. entry->func = PATH_CHECK;
  240. else if (strcmp(args[0].from, "FILE_MMAP") == 0)
  241. entry->func = FILE_MMAP;
  242. else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
  243. entry->func = BPRM_CHECK;
  244. else
  245. result = -EINVAL;
  246. if (!result)
  247. entry->flags |= IMA_FUNC;
  248. break;
  249. case Opt_mask:
  250. audit_log_format(ab, "mask=%s ", args[0].from);
  251. if ((strcmp(args[0].from, "MAY_EXEC")) == 0)
  252. entry->mask = MAY_EXEC;
  253. else if (strcmp(args[0].from, "MAY_WRITE") == 0)
  254. entry->mask = MAY_WRITE;
  255. else if (strcmp(args[0].from, "MAY_READ") == 0)
  256. entry->mask = MAY_READ;
  257. else if (strcmp(args[0].from, "MAY_APPEND") == 0)
  258. entry->mask = MAY_APPEND;
  259. else
  260. result = -EINVAL;
  261. if (!result)
  262. entry->flags |= IMA_MASK;
  263. break;
  264. case Opt_fsmagic:
  265. audit_log_format(ab, "fsmagic=%s ", args[0].from);
  266. result = strict_strtoul(args[0].from, 16,
  267. &entry->fsmagic);
  268. if (!result)
  269. entry->flags |= IMA_FSMAGIC;
  270. break;
  271. case Opt_uid:
  272. audit_log_format(ab, "uid=%s ", args[0].from);
  273. result = strict_strtoul(args[0].from, 10, &lnum);
  274. if (!result) {
  275. entry->uid = (uid_t) lnum;
  276. if (entry->uid != lnum)
  277. result = -EINVAL;
  278. else
  279. entry->flags |= IMA_UID;
  280. }
  281. break;
  282. case Opt_obj_user:
  283. audit_log_format(ab, "obj_user=%s ", args[0].from);
  284. result = ima_lsm_rule_init(entry, args[0].from,
  285. LSM_OBJ_USER,
  286. AUDIT_OBJ_USER);
  287. break;
  288. case Opt_obj_role:
  289. audit_log_format(ab, "obj_role=%s ", args[0].from);
  290. result = ima_lsm_rule_init(entry, args[0].from,
  291. LSM_OBJ_ROLE,
  292. AUDIT_OBJ_ROLE);
  293. break;
  294. case Opt_obj_type:
  295. audit_log_format(ab, "obj_type=%s ", args[0].from);
  296. result = ima_lsm_rule_init(entry, args[0].from,
  297. LSM_OBJ_TYPE,
  298. AUDIT_OBJ_TYPE);
  299. break;
  300. case Opt_subj_user:
  301. audit_log_format(ab, "subj_user=%s ", args[0].from);
  302. result = ima_lsm_rule_init(entry, args[0].from,
  303. LSM_SUBJ_USER,
  304. AUDIT_SUBJ_USER);
  305. break;
  306. case Opt_subj_role:
  307. audit_log_format(ab, "subj_role=%s ", args[0].from);
  308. result = ima_lsm_rule_init(entry, args[0].from,
  309. LSM_SUBJ_ROLE,
  310. AUDIT_SUBJ_ROLE);
  311. break;
  312. case Opt_subj_type:
  313. audit_log_format(ab, "subj_type=%s ", args[0].from);
  314. result = ima_lsm_rule_init(entry, args[0].from,
  315. LSM_SUBJ_TYPE,
  316. AUDIT_SUBJ_TYPE);
  317. break;
  318. case Opt_err:
  319. audit_log_format(ab, "UNKNOWN=%s ", p);
  320. break;
  321. }
  322. }
  323. if (entry->action == UNKNOWN)
  324. result = -EINVAL;
  325. audit_log_format(ab, "res=%d", !result ? 0 : 1);
  326. audit_log_end(ab);
  327. return result;
  328. }
  329. /**
  330. * ima_parse_add_rule - add a rule to measure_policy_rules
  331. * @rule - ima measurement policy rule
  332. *
  333. * Uses a mutex to protect the policy list from multiple concurrent writers.
  334. * Returns 0 on success, an error code on failure.
  335. */
  336. int ima_parse_add_rule(char *rule)
  337. {
  338. const char *op = "update_policy";
  339. struct ima_measure_rule_entry *entry;
  340. int result = 0;
  341. int audit_info = 0;
  342. /* Prevent installed policy from changing */
  343. if (ima_measure != &measure_default_rules) {
  344. integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
  345. NULL, op, "already exists",
  346. -EACCES, audit_info);
  347. return -EACCES;
  348. }
  349. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  350. if (!entry) {
  351. integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
  352. NULL, op, "-ENOMEM", -ENOMEM, audit_info);
  353. return -ENOMEM;
  354. }
  355. INIT_LIST_HEAD(&entry->list);
  356. result = ima_parse_rule(rule, entry);
  357. if (!result) {
  358. mutex_lock(&ima_measure_mutex);
  359. list_add_tail(&entry->list, &measure_policy_rules);
  360. mutex_unlock(&ima_measure_mutex);
  361. } else {
  362. kfree(entry);
  363. integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
  364. NULL, op, "invalid policy", result,
  365. audit_info);
  366. }
  367. return result;
  368. }
  369. /* ima_delete_rules called to cleanup invalid policy */
  370. void ima_delete_rules(void)
  371. {
  372. struct ima_measure_rule_entry *entry, *tmp;
  373. mutex_lock(&ima_measure_mutex);
  374. list_for_each_entry_safe(entry, tmp, &measure_policy_rules, list) {
  375. list_del(&entry->list);
  376. kfree(entry);
  377. }
  378. mutex_unlock(&ima_measure_mutex);
  379. }