ima_policy.c 19 KB

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