ima_policy.c 17 KB

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