ima_policy.c 15 KB

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