ima_policy.c 18 KB

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