ima_policy.c 19 KB

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