ima_policy.c 13 KB

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