ima_policy.c 16 KB

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