ima_policy.c 17 KB

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