ima_policy.c 12 KB

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