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