ima_policy.c 19 KB

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