ima_policy.c 21 KB

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