ima_policy.c 12 KB

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