ima_policy.c 18 KB

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