ima_policy.c 19 KB

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