ima_policy.c 12 KB

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