ima_policy.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  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/fs.h>
  16. #include <linux/security.h>
  17. #include <linux/magic.h>
  18. #include <linux/parser.h>
  19. #include <linux/slab.h>
  20. #include <linux/rculist.h>
  21. #include <linux/genhd.h>
  22. #include <linux/seq_file.h>
  23. #include "ima.h"
  24. /* flags definitions */
  25. #define IMA_FUNC 0x0001
  26. #define IMA_MASK 0x0002
  27. #define IMA_FSMAGIC 0x0004
  28. #define IMA_UID 0x0008
  29. #define IMA_FOWNER 0x0010
  30. #define IMA_FSUUID 0x0020
  31. #define IMA_INMASK 0x0040
  32. #define IMA_EUID 0x0080
  33. #define IMA_PCR 0x0100
  34. #define UNKNOWN 0
  35. #define MEASURE 0x0001 /* same as IMA_MEASURE */
  36. #define DONT_MEASURE 0x0002
  37. #define APPRAISE 0x0004 /* same as IMA_APPRAISE */
  38. #define DONT_APPRAISE 0x0008
  39. #define AUDIT 0x0040
  40. #define INVALID_PCR(a) (((a) < 0) || \
  41. (a) >= (FIELD_SIZEOF(struct integrity_iint_cache, measured_pcrs) * 8))
  42. int ima_policy_flag;
  43. static int temp_ima_appraise;
  44. #define MAX_LSM_RULES 6
  45. enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
  46. LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
  47. };
  48. enum policy_types { ORIGINAL_TCB = 1, DEFAULT_TCB };
  49. struct ima_rule_entry {
  50. struct list_head list;
  51. int action;
  52. unsigned int flags;
  53. enum ima_hooks func;
  54. int mask;
  55. unsigned long fsmagic;
  56. u8 fsuuid[16];
  57. kuid_t uid;
  58. kuid_t fowner;
  59. int pcr;
  60. struct {
  61. void *rule; /* LSM file metadata specific */
  62. void *args_p; /* audit value */
  63. int type; /* audit type */
  64. } lsm[MAX_LSM_RULES];
  65. };
  66. /*
  67. * Without LSM specific knowledge, the default policy can only be
  68. * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
  69. */
  70. /*
  71. * The minimum rule set to allow for full TCB coverage. Measures all files
  72. * opened or mmap for exec and everything read by root. Dangerous because
  73. * normal users can easily run the machine out of memory simply building
  74. * and running executables.
  75. */
  76. static struct ima_rule_entry dont_measure_rules[] = {
  77. {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
  78. {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
  79. {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
  80. {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
  81. {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
  82. {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
  83. {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
  84. {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
  85. {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
  86. .flags = IMA_FSMAGIC},
  87. {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC}
  88. };
  89. static struct ima_rule_entry original_measurement_rules[] = {
  90. {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
  91. .flags = IMA_FUNC | IMA_MASK},
  92. {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
  93. .flags = IMA_FUNC | IMA_MASK},
  94. {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
  95. .uid = GLOBAL_ROOT_UID, .flags = IMA_FUNC | IMA_MASK | IMA_UID},
  96. {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
  97. {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
  98. };
  99. static struct ima_rule_entry default_measurement_rules[] = {
  100. {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
  101. .flags = IMA_FUNC | IMA_MASK},
  102. {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
  103. .flags = IMA_FUNC | IMA_MASK},
  104. {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
  105. .uid = GLOBAL_ROOT_UID, .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
  106. {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
  107. .uid = GLOBAL_ROOT_UID, .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
  108. {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
  109. {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
  110. {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC},
  111. };
  112. static struct ima_rule_entry default_appraise_rules[] = {
  113. {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
  114. {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
  115. {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
  116. {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
  117. {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
  118. {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
  119. {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
  120. {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
  121. {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
  122. {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
  123. {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
  124. #ifdef CONFIG_IMA_WRITE_POLICY
  125. {.action = APPRAISE, .func = POLICY_CHECK,
  126. .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
  127. #endif
  128. #ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
  129. {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .flags = IMA_FOWNER},
  130. #else
  131. /* force signature */
  132. {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID,
  133. .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
  134. #endif
  135. };
  136. static LIST_HEAD(ima_default_rules);
  137. static LIST_HEAD(ima_policy_rules);
  138. static LIST_HEAD(ima_temp_rules);
  139. static struct list_head *ima_rules;
  140. static int ima_policy __initdata;
  141. static int __init default_measure_policy_setup(char *str)
  142. {
  143. if (ima_policy)
  144. return 1;
  145. ima_policy = ORIGINAL_TCB;
  146. return 1;
  147. }
  148. __setup("ima_tcb", default_measure_policy_setup);
  149. static int __init policy_setup(char *str)
  150. {
  151. if (ima_policy)
  152. return 1;
  153. if (strcmp(str, "tcb") == 0)
  154. ima_policy = DEFAULT_TCB;
  155. return 1;
  156. }
  157. __setup("ima_policy=", policy_setup);
  158. static bool ima_use_appraise_tcb __initdata;
  159. static int __init default_appraise_policy_setup(char *str)
  160. {
  161. ima_use_appraise_tcb = 1;
  162. return 1;
  163. }
  164. __setup("ima_appraise_tcb", default_appraise_policy_setup);
  165. /*
  166. * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
  167. * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
  168. * the reloaded LSM policy. We assume the rules still exist; and BUG_ON() if
  169. * they don't.
  170. */
  171. static void ima_lsm_update_rules(void)
  172. {
  173. struct ima_rule_entry *entry;
  174. int result;
  175. int i;
  176. list_for_each_entry(entry, &ima_policy_rules, list) {
  177. for (i = 0; i < MAX_LSM_RULES; i++) {
  178. if (!entry->lsm[i].rule)
  179. continue;
  180. result = security_filter_rule_init(entry->lsm[i].type,
  181. Audit_equal,
  182. entry->lsm[i].args_p,
  183. &entry->lsm[i].rule);
  184. BUG_ON(!entry->lsm[i].rule);
  185. }
  186. }
  187. }
  188. /**
  189. * ima_match_rules - determine whether an inode matches the measure rule.
  190. * @rule: a pointer to a rule
  191. * @inode: a pointer to an inode
  192. * @func: LIM hook identifier
  193. * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
  194. *
  195. * Returns true on rule match, false on failure.
  196. */
  197. static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,
  198. enum ima_hooks func, int mask)
  199. {
  200. struct task_struct *tsk = current;
  201. const struct cred *cred = current_cred();
  202. int i;
  203. if ((rule->flags & IMA_FUNC) &&
  204. (rule->func != func && func != POST_SETATTR))
  205. return false;
  206. if ((rule->flags & IMA_MASK) &&
  207. (rule->mask != mask && func != POST_SETATTR))
  208. return false;
  209. if ((rule->flags & IMA_INMASK) &&
  210. (!(rule->mask & mask) && func != POST_SETATTR))
  211. return false;
  212. if ((rule->flags & IMA_FSMAGIC)
  213. && rule->fsmagic != inode->i_sb->s_magic)
  214. return false;
  215. if ((rule->flags & IMA_FSUUID) &&
  216. memcmp(rule->fsuuid, inode->i_sb->s_uuid, sizeof(rule->fsuuid)))
  217. return false;
  218. if ((rule->flags & IMA_UID) && !uid_eq(rule->uid, cred->uid))
  219. return false;
  220. if (rule->flags & IMA_EUID) {
  221. if (has_capability_noaudit(current, CAP_SETUID)) {
  222. if (!uid_eq(rule->uid, cred->euid)
  223. && !uid_eq(rule->uid, cred->suid)
  224. && !uid_eq(rule->uid, cred->uid))
  225. return false;
  226. } else if (!uid_eq(rule->uid, cred->euid))
  227. return false;
  228. }
  229. if ((rule->flags & IMA_FOWNER) && !uid_eq(rule->fowner, inode->i_uid))
  230. return false;
  231. for (i = 0; i < MAX_LSM_RULES; i++) {
  232. int rc = 0;
  233. u32 osid, sid;
  234. int retried = 0;
  235. if (!rule->lsm[i].rule)
  236. continue;
  237. retry:
  238. switch (i) {
  239. case LSM_OBJ_USER:
  240. case LSM_OBJ_ROLE:
  241. case LSM_OBJ_TYPE:
  242. security_inode_getsecid(inode, &osid);
  243. rc = security_filter_rule_match(osid,
  244. rule->lsm[i].type,
  245. Audit_equal,
  246. rule->lsm[i].rule,
  247. NULL);
  248. break;
  249. case LSM_SUBJ_USER:
  250. case LSM_SUBJ_ROLE:
  251. case LSM_SUBJ_TYPE:
  252. security_task_getsecid(tsk, &sid);
  253. rc = security_filter_rule_match(sid,
  254. rule->lsm[i].type,
  255. Audit_equal,
  256. rule->lsm[i].rule,
  257. NULL);
  258. default:
  259. break;
  260. }
  261. if ((rc < 0) && (!retried)) {
  262. retried = 1;
  263. ima_lsm_update_rules();
  264. goto retry;
  265. }
  266. if (!rc)
  267. return false;
  268. }
  269. return true;
  270. }
  271. /*
  272. * In addition to knowing that we need to appraise the file in general,
  273. * we need to differentiate between calling hooks, for hook specific rules.
  274. */
  275. static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
  276. {
  277. if (!(rule->flags & IMA_FUNC))
  278. return IMA_FILE_APPRAISE;
  279. switch (func) {
  280. case MMAP_CHECK:
  281. return IMA_MMAP_APPRAISE;
  282. case BPRM_CHECK:
  283. return IMA_BPRM_APPRAISE;
  284. case FILE_CHECK:
  285. case POST_SETATTR:
  286. return IMA_FILE_APPRAISE;
  287. case MODULE_CHECK ... MAX_CHECK - 1:
  288. default:
  289. return IMA_READ_APPRAISE;
  290. }
  291. }
  292. /**
  293. * ima_match_policy - decision based on LSM and other conditions
  294. * @inode: pointer to an inode for which the policy decision is being made
  295. * @func: IMA hook identifier
  296. * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
  297. * @pcr: set the pcr to extend
  298. *
  299. * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
  300. * conditions.
  301. *
  302. * Since the IMA policy may be updated multiple times we need to lock the
  303. * list when walking it. Reads are many orders of magnitude more numerous
  304. * than writes so ima_match_policy() is classical RCU candidate.
  305. */
  306. int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
  307. int flags, int *pcr)
  308. {
  309. struct ima_rule_entry *entry;
  310. int action = 0, actmask = flags | (flags << 1);
  311. rcu_read_lock();
  312. list_for_each_entry_rcu(entry, ima_rules, list) {
  313. if (!(entry->action & actmask))
  314. continue;
  315. if (!ima_match_rules(entry, inode, func, mask))
  316. continue;
  317. action |= entry->flags & IMA_ACTION_FLAGS;
  318. action |= entry->action & IMA_DO_MASK;
  319. if (entry->action & IMA_APPRAISE)
  320. action |= get_subaction(entry, func);
  321. if (entry->action & IMA_DO_MASK)
  322. actmask &= ~(entry->action | entry->action << 1);
  323. else
  324. actmask &= ~(entry->action | entry->action >> 1);
  325. if ((pcr) && (entry->flags & IMA_PCR))
  326. *pcr = entry->pcr;
  327. if (!actmask)
  328. break;
  329. }
  330. rcu_read_unlock();
  331. return action;
  332. }
  333. /*
  334. * Initialize the ima_policy_flag variable based on the currently
  335. * loaded policy. Based on this flag, the decision to short circuit
  336. * out of a function or not call the function in the first place
  337. * can be made earlier.
  338. */
  339. void ima_update_policy_flag(void)
  340. {
  341. struct ima_rule_entry *entry;
  342. list_for_each_entry(entry, ima_rules, list) {
  343. if (entry->action & IMA_DO_MASK)
  344. ima_policy_flag |= entry->action;
  345. }
  346. ima_appraise |= temp_ima_appraise;
  347. if (!ima_appraise)
  348. ima_policy_flag &= ~IMA_APPRAISE;
  349. }
  350. /**
  351. * ima_init_policy - initialize the default measure rules.
  352. *
  353. * ima_rules points to either the ima_default_rules or the
  354. * the new ima_policy_rules.
  355. */
  356. void __init ima_init_policy(void)
  357. {
  358. int i, measure_entries, appraise_entries;
  359. /* if !ima_policy set entries = 0 so we load NO default rules */
  360. measure_entries = ima_policy ? ARRAY_SIZE(dont_measure_rules) : 0;
  361. appraise_entries = ima_use_appraise_tcb ?
  362. ARRAY_SIZE(default_appraise_rules) : 0;
  363. for (i = 0; i < measure_entries; i++)
  364. list_add_tail(&dont_measure_rules[i].list, &ima_default_rules);
  365. switch (ima_policy) {
  366. case ORIGINAL_TCB:
  367. for (i = 0; i < ARRAY_SIZE(original_measurement_rules); i++)
  368. list_add_tail(&original_measurement_rules[i].list,
  369. &ima_default_rules);
  370. break;
  371. case DEFAULT_TCB:
  372. for (i = 0; i < ARRAY_SIZE(default_measurement_rules); i++)
  373. list_add_tail(&default_measurement_rules[i].list,
  374. &ima_default_rules);
  375. default:
  376. break;
  377. }
  378. for (i = 0; i < appraise_entries; i++) {
  379. list_add_tail(&default_appraise_rules[i].list,
  380. &ima_default_rules);
  381. if (default_appraise_rules[i].func == POLICY_CHECK)
  382. temp_ima_appraise |= IMA_APPRAISE_POLICY;
  383. }
  384. ima_rules = &ima_default_rules;
  385. ima_update_policy_flag();
  386. }
  387. /* Make sure we have a valid policy, at least containing some rules. */
  388. int ima_check_policy(void)
  389. {
  390. if (list_empty(&ima_temp_rules))
  391. return -EINVAL;
  392. return 0;
  393. }
  394. /**
  395. * ima_update_policy - update default_rules with new measure rules
  396. *
  397. * Called on file .release to update the default rules with a complete new
  398. * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
  399. * they make a queue. The policy may be updated multiple times and this is the
  400. * RCU updater.
  401. *
  402. * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
  403. * we switch from the default policy to user defined.
  404. */
  405. void ima_update_policy(void)
  406. {
  407. struct list_head *first, *last, *policy;
  408. /* append current policy with the new rules */
  409. first = (&ima_temp_rules)->next;
  410. last = (&ima_temp_rules)->prev;
  411. policy = &ima_policy_rules;
  412. synchronize_rcu();
  413. last->next = policy;
  414. rcu_assign_pointer(list_next_rcu(policy->prev), first);
  415. first->prev = policy->prev;
  416. policy->prev = last;
  417. /* prepare for the next policy rules addition */
  418. INIT_LIST_HEAD(&ima_temp_rules);
  419. if (ima_rules != policy) {
  420. ima_policy_flag = 0;
  421. ima_rules = policy;
  422. }
  423. ima_update_policy_flag();
  424. }
  425. enum {
  426. Opt_err = -1,
  427. Opt_measure = 1, Opt_dont_measure,
  428. Opt_appraise, Opt_dont_appraise,
  429. Opt_audit,
  430. Opt_obj_user, Opt_obj_role, Opt_obj_type,
  431. Opt_subj_user, Opt_subj_role, Opt_subj_type,
  432. Opt_func, Opt_mask, Opt_fsmagic,
  433. Opt_fsuuid, Opt_uid, Opt_euid, Opt_fowner,
  434. Opt_appraise_type, Opt_permit_directio,
  435. Opt_pcr
  436. };
  437. static match_table_t policy_tokens = {
  438. {Opt_measure, "measure"},
  439. {Opt_dont_measure, "dont_measure"},
  440. {Opt_appraise, "appraise"},
  441. {Opt_dont_appraise, "dont_appraise"},
  442. {Opt_audit, "audit"},
  443. {Opt_obj_user, "obj_user=%s"},
  444. {Opt_obj_role, "obj_role=%s"},
  445. {Opt_obj_type, "obj_type=%s"},
  446. {Opt_subj_user, "subj_user=%s"},
  447. {Opt_subj_role, "subj_role=%s"},
  448. {Opt_subj_type, "subj_type=%s"},
  449. {Opt_func, "func=%s"},
  450. {Opt_mask, "mask=%s"},
  451. {Opt_fsmagic, "fsmagic=%s"},
  452. {Opt_fsuuid, "fsuuid=%s"},
  453. {Opt_uid, "uid=%s"},
  454. {Opt_euid, "euid=%s"},
  455. {Opt_fowner, "fowner=%s"},
  456. {Opt_appraise_type, "appraise_type=%s"},
  457. {Opt_permit_directio, "permit_directio"},
  458. {Opt_pcr, "pcr=%s"},
  459. {Opt_err, NULL}
  460. };
  461. static int ima_lsm_rule_init(struct ima_rule_entry *entry,
  462. substring_t *args, int lsm_rule, int audit_type)
  463. {
  464. int result;
  465. if (entry->lsm[lsm_rule].rule)
  466. return -EINVAL;
  467. entry->lsm[lsm_rule].args_p = match_strdup(args);
  468. if (!entry->lsm[lsm_rule].args_p)
  469. return -ENOMEM;
  470. entry->lsm[lsm_rule].type = audit_type;
  471. result = security_filter_rule_init(entry->lsm[lsm_rule].type,
  472. Audit_equal,
  473. entry->lsm[lsm_rule].args_p,
  474. &entry->lsm[lsm_rule].rule);
  475. if (!entry->lsm[lsm_rule].rule) {
  476. kfree(entry->lsm[lsm_rule].args_p);
  477. return -EINVAL;
  478. }
  479. return result;
  480. }
  481. static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
  482. {
  483. audit_log_format(ab, "%s=", key);
  484. audit_log_untrustedstring(ab, value);
  485. audit_log_format(ab, " ");
  486. }
  487. static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
  488. {
  489. struct audit_buffer *ab;
  490. char *from;
  491. char *p;
  492. int result = 0;
  493. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
  494. entry->uid = INVALID_UID;
  495. entry->fowner = INVALID_UID;
  496. entry->action = UNKNOWN;
  497. while ((p = strsep(&rule, " \t")) != NULL) {
  498. substring_t args[MAX_OPT_ARGS];
  499. int token;
  500. unsigned long lnum;
  501. if (result < 0)
  502. break;
  503. if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
  504. continue;
  505. token = match_token(p, policy_tokens, args);
  506. switch (token) {
  507. case Opt_measure:
  508. ima_log_string(ab, "action", "measure");
  509. if (entry->action != UNKNOWN)
  510. result = -EINVAL;
  511. entry->action = MEASURE;
  512. break;
  513. case Opt_dont_measure:
  514. ima_log_string(ab, "action", "dont_measure");
  515. if (entry->action != UNKNOWN)
  516. result = -EINVAL;
  517. entry->action = DONT_MEASURE;
  518. break;
  519. case Opt_appraise:
  520. ima_log_string(ab, "action", "appraise");
  521. if (entry->action != UNKNOWN)
  522. result = -EINVAL;
  523. entry->action = APPRAISE;
  524. break;
  525. case Opt_dont_appraise:
  526. ima_log_string(ab, "action", "dont_appraise");
  527. if (entry->action != UNKNOWN)
  528. result = -EINVAL;
  529. entry->action = DONT_APPRAISE;
  530. break;
  531. case Opt_audit:
  532. ima_log_string(ab, "action", "audit");
  533. if (entry->action != UNKNOWN)
  534. result = -EINVAL;
  535. entry->action = AUDIT;
  536. break;
  537. case Opt_func:
  538. ima_log_string(ab, "func", args[0].from);
  539. if (entry->func)
  540. result = -EINVAL;
  541. if (strcmp(args[0].from, "FILE_CHECK") == 0)
  542. entry->func = FILE_CHECK;
  543. /* PATH_CHECK is for backwards compat */
  544. else if (strcmp(args[0].from, "PATH_CHECK") == 0)
  545. entry->func = FILE_CHECK;
  546. else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
  547. entry->func = MODULE_CHECK;
  548. else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
  549. entry->func = FIRMWARE_CHECK;
  550. else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
  551. || (strcmp(args[0].from, "MMAP_CHECK") == 0))
  552. entry->func = MMAP_CHECK;
  553. else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
  554. entry->func = BPRM_CHECK;
  555. else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
  556. 0)
  557. entry->func = KEXEC_KERNEL_CHECK;
  558. else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
  559. == 0)
  560. entry->func = KEXEC_INITRAMFS_CHECK;
  561. else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
  562. entry->func = POLICY_CHECK;
  563. else
  564. result = -EINVAL;
  565. if (!result)
  566. entry->flags |= IMA_FUNC;
  567. break;
  568. case Opt_mask:
  569. ima_log_string(ab, "mask", args[0].from);
  570. if (entry->mask)
  571. result = -EINVAL;
  572. from = args[0].from;
  573. if (*from == '^')
  574. from++;
  575. if ((strcmp(from, "MAY_EXEC")) == 0)
  576. entry->mask = MAY_EXEC;
  577. else if (strcmp(from, "MAY_WRITE") == 0)
  578. entry->mask = MAY_WRITE;
  579. else if (strcmp(from, "MAY_READ") == 0)
  580. entry->mask = MAY_READ;
  581. else if (strcmp(from, "MAY_APPEND") == 0)
  582. entry->mask = MAY_APPEND;
  583. else
  584. result = -EINVAL;
  585. if (!result)
  586. entry->flags |= (*args[0].from == '^')
  587. ? IMA_INMASK : IMA_MASK;
  588. break;
  589. case Opt_fsmagic:
  590. ima_log_string(ab, "fsmagic", args[0].from);
  591. if (entry->fsmagic) {
  592. result = -EINVAL;
  593. break;
  594. }
  595. result = kstrtoul(args[0].from, 16, &entry->fsmagic);
  596. if (!result)
  597. entry->flags |= IMA_FSMAGIC;
  598. break;
  599. case Opt_fsuuid:
  600. ima_log_string(ab, "fsuuid", args[0].from);
  601. if (memchr_inv(entry->fsuuid, 0x00,
  602. sizeof(entry->fsuuid))) {
  603. result = -EINVAL;
  604. break;
  605. }
  606. result = blk_part_pack_uuid(args[0].from,
  607. entry->fsuuid);
  608. if (!result)
  609. entry->flags |= IMA_FSUUID;
  610. break;
  611. case Opt_uid:
  612. ima_log_string(ab, "uid", args[0].from);
  613. case Opt_euid:
  614. if (token == Opt_euid)
  615. ima_log_string(ab, "euid", args[0].from);
  616. if (uid_valid(entry->uid)) {
  617. result = -EINVAL;
  618. break;
  619. }
  620. result = kstrtoul(args[0].from, 10, &lnum);
  621. if (!result) {
  622. entry->uid = make_kuid(current_user_ns(),
  623. (uid_t) lnum);
  624. if (!uid_valid(entry->uid) ||
  625. (uid_t)lnum != lnum)
  626. result = -EINVAL;
  627. else
  628. entry->flags |= (token == Opt_uid)
  629. ? IMA_UID : IMA_EUID;
  630. }
  631. break;
  632. case Opt_fowner:
  633. ima_log_string(ab, "fowner", args[0].from);
  634. if (uid_valid(entry->fowner)) {
  635. result = -EINVAL;
  636. break;
  637. }
  638. result = kstrtoul(args[0].from, 10, &lnum);
  639. if (!result) {
  640. entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
  641. if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
  642. result = -EINVAL;
  643. else
  644. entry->flags |= IMA_FOWNER;
  645. }
  646. break;
  647. case Opt_obj_user:
  648. ima_log_string(ab, "obj_user", args[0].from);
  649. result = ima_lsm_rule_init(entry, args,
  650. LSM_OBJ_USER,
  651. AUDIT_OBJ_USER);
  652. break;
  653. case Opt_obj_role:
  654. ima_log_string(ab, "obj_role", args[0].from);
  655. result = ima_lsm_rule_init(entry, args,
  656. LSM_OBJ_ROLE,
  657. AUDIT_OBJ_ROLE);
  658. break;
  659. case Opt_obj_type:
  660. ima_log_string(ab, "obj_type", args[0].from);
  661. result = ima_lsm_rule_init(entry, args,
  662. LSM_OBJ_TYPE,
  663. AUDIT_OBJ_TYPE);
  664. break;
  665. case Opt_subj_user:
  666. ima_log_string(ab, "subj_user", args[0].from);
  667. result = ima_lsm_rule_init(entry, args,
  668. LSM_SUBJ_USER,
  669. AUDIT_SUBJ_USER);
  670. break;
  671. case Opt_subj_role:
  672. ima_log_string(ab, "subj_role", args[0].from);
  673. result = ima_lsm_rule_init(entry, args,
  674. LSM_SUBJ_ROLE,
  675. AUDIT_SUBJ_ROLE);
  676. break;
  677. case Opt_subj_type:
  678. ima_log_string(ab, "subj_type", args[0].from);
  679. result = ima_lsm_rule_init(entry, args,
  680. LSM_SUBJ_TYPE,
  681. AUDIT_SUBJ_TYPE);
  682. break;
  683. case Opt_appraise_type:
  684. if (entry->action != APPRAISE) {
  685. result = -EINVAL;
  686. break;
  687. }
  688. ima_log_string(ab, "appraise_type", args[0].from);
  689. if ((strcmp(args[0].from, "imasig")) == 0)
  690. entry->flags |= IMA_DIGSIG_REQUIRED;
  691. else
  692. result = -EINVAL;
  693. break;
  694. case Opt_permit_directio:
  695. entry->flags |= IMA_PERMIT_DIRECTIO;
  696. break;
  697. case Opt_pcr:
  698. if (entry->action != MEASURE) {
  699. result = -EINVAL;
  700. break;
  701. }
  702. ima_log_string(ab, "pcr", args[0].from);
  703. result = kstrtoint(args[0].from, 10, &entry->pcr);
  704. if (result || INVALID_PCR(entry->pcr))
  705. result = -EINVAL;
  706. else
  707. entry->flags |= IMA_PCR;
  708. break;
  709. case Opt_err:
  710. ima_log_string(ab, "UNKNOWN", p);
  711. result = -EINVAL;
  712. break;
  713. }
  714. }
  715. if (!result && (entry->action == UNKNOWN))
  716. result = -EINVAL;
  717. else if (entry->func == MODULE_CHECK)
  718. temp_ima_appraise |= IMA_APPRAISE_MODULES;
  719. else if (entry->func == FIRMWARE_CHECK)
  720. temp_ima_appraise |= IMA_APPRAISE_FIRMWARE;
  721. else if (entry->func == POLICY_CHECK)
  722. temp_ima_appraise |= IMA_APPRAISE_POLICY;
  723. audit_log_format(ab, "res=%d", !result);
  724. audit_log_end(ab);
  725. return result;
  726. }
  727. /**
  728. * ima_parse_add_rule - add a rule to ima_policy_rules
  729. * @rule - ima measurement policy rule
  730. *
  731. * Avoid locking by allowing just one writer at a time in ima_write_policy()
  732. * Returns the length of the rule parsed, an error code on failure
  733. */
  734. ssize_t ima_parse_add_rule(char *rule)
  735. {
  736. static const char op[] = "update_policy";
  737. char *p;
  738. struct ima_rule_entry *entry;
  739. ssize_t result, len;
  740. int audit_info = 0;
  741. p = strsep(&rule, "\n");
  742. len = strlen(p) + 1;
  743. p += strspn(p, " \t");
  744. if (*p == '#' || *p == '\0')
  745. return len;
  746. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  747. if (!entry) {
  748. integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
  749. NULL, op, "-ENOMEM", -ENOMEM, audit_info);
  750. return -ENOMEM;
  751. }
  752. INIT_LIST_HEAD(&entry->list);
  753. result = ima_parse_rule(p, entry);
  754. if (result) {
  755. kfree(entry);
  756. integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
  757. NULL, op, "invalid-policy", result,
  758. audit_info);
  759. return result;
  760. }
  761. list_add_tail(&entry->list, &ima_temp_rules);
  762. return len;
  763. }
  764. /**
  765. * ima_delete_rules() called to cleanup invalid in-flight policy.
  766. * We don't need locking as we operate on the temp list, which is
  767. * different from the active one. There is also only one user of
  768. * ima_delete_rules() at a time.
  769. */
  770. void ima_delete_rules(void)
  771. {
  772. struct ima_rule_entry *entry, *tmp;
  773. int i;
  774. temp_ima_appraise = 0;
  775. list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
  776. for (i = 0; i < MAX_LSM_RULES; i++)
  777. kfree(entry->lsm[i].args_p);
  778. list_del(&entry->list);
  779. kfree(entry);
  780. }
  781. }
  782. #ifdef CONFIG_IMA_READ_POLICY
  783. enum {
  784. mask_exec = 0, mask_write, mask_read, mask_append
  785. };
  786. static char *mask_tokens[] = {
  787. "MAY_EXEC",
  788. "MAY_WRITE",
  789. "MAY_READ",
  790. "MAY_APPEND"
  791. };
  792. enum {
  793. func_file = 0, func_mmap, func_bprm,
  794. func_module, func_firmware, func_post,
  795. func_kexec_kernel, func_kexec_initramfs,
  796. func_policy
  797. };
  798. static char *func_tokens[] = {
  799. "FILE_CHECK",
  800. "MMAP_CHECK",
  801. "BPRM_CHECK",
  802. "MODULE_CHECK",
  803. "FIRMWARE_CHECK",
  804. "POST_SETATTR",
  805. "KEXEC_KERNEL_CHECK",
  806. "KEXEC_INITRAMFS_CHECK",
  807. "POLICY_CHECK"
  808. };
  809. void *ima_policy_start(struct seq_file *m, loff_t *pos)
  810. {
  811. loff_t l = *pos;
  812. struct ima_rule_entry *entry;
  813. rcu_read_lock();
  814. list_for_each_entry_rcu(entry, ima_rules, list) {
  815. if (!l--) {
  816. rcu_read_unlock();
  817. return entry;
  818. }
  819. }
  820. rcu_read_unlock();
  821. return NULL;
  822. }
  823. void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
  824. {
  825. struct ima_rule_entry *entry = v;
  826. rcu_read_lock();
  827. entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
  828. rcu_read_unlock();
  829. (*pos)++;
  830. return (&entry->list == ima_rules) ? NULL : entry;
  831. }
  832. void ima_policy_stop(struct seq_file *m, void *v)
  833. {
  834. }
  835. #define pt(token) policy_tokens[token + Opt_err].pattern
  836. #define mt(token) mask_tokens[token]
  837. #define ft(token) func_tokens[token]
  838. /*
  839. * policy_func_show - display the ima_hooks policy rule
  840. */
  841. static void policy_func_show(struct seq_file *m, enum ima_hooks func)
  842. {
  843. char tbuf[64] = {0,};
  844. switch (func) {
  845. case FILE_CHECK:
  846. seq_printf(m, pt(Opt_func), ft(func_file));
  847. break;
  848. case MMAP_CHECK:
  849. seq_printf(m, pt(Opt_func), ft(func_mmap));
  850. break;
  851. case BPRM_CHECK:
  852. seq_printf(m, pt(Opt_func), ft(func_bprm));
  853. break;
  854. case MODULE_CHECK:
  855. seq_printf(m, pt(Opt_func), ft(func_module));
  856. break;
  857. case FIRMWARE_CHECK:
  858. seq_printf(m, pt(Opt_func), ft(func_firmware));
  859. break;
  860. case POST_SETATTR:
  861. seq_printf(m, pt(Opt_func), ft(func_post));
  862. break;
  863. case KEXEC_KERNEL_CHECK:
  864. seq_printf(m, pt(Opt_func), ft(func_kexec_kernel));
  865. break;
  866. case KEXEC_INITRAMFS_CHECK:
  867. seq_printf(m, pt(Opt_func), ft(func_kexec_initramfs));
  868. break;
  869. case POLICY_CHECK:
  870. seq_printf(m, pt(Opt_func), ft(func_policy));
  871. break;
  872. default:
  873. snprintf(tbuf, sizeof(tbuf), "%d", func);
  874. seq_printf(m, pt(Opt_func), tbuf);
  875. break;
  876. }
  877. seq_puts(m, " ");
  878. }
  879. int ima_policy_show(struct seq_file *m, void *v)
  880. {
  881. struct ima_rule_entry *entry = v;
  882. int i;
  883. char tbuf[64] = {0,};
  884. rcu_read_lock();
  885. if (entry->action & MEASURE)
  886. seq_puts(m, pt(Opt_measure));
  887. if (entry->action & DONT_MEASURE)
  888. seq_puts(m, pt(Opt_dont_measure));
  889. if (entry->action & APPRAISE)
  890. seq_puts(m, pt(Opt_appraise));
  891. if (entry->action & DONT_APPRAISE)
  892. seq_puts(m, pt(Opt_dont_appraise));
  893. if (entry->action & AUDIT)
  894. seq_puts(m, pt(Opt_audit));
  895. seq_puts(m, " ");
  896. if (entry->flags & IMA_FUNC)
  897. policy_func_show(m, entry->func);
  898. if (entry->flags & IMA_MASK) {
  899. if (entry->mask & MAY_EXEC)
  900. seq_printf(m, pt(Opt_mask), mt(mask_exec));
  901. if (entry->mask & MAY_WRITE)
  902. seq_printf(m, pt(Opt_mask), mt(mask_write));
  903. if (entry->mask & MAY_READ)
  904. seq_printf(m, pt(Opt_mask), mt(mask_read));
  905. if (entry->mask & MAY_APPEND)
  906. seq_printf(m, pt(Opt_mask), mt(mask_append));
  907. seq_puts(m, " ");
  908. }
  909. if (entry->flags & IMA_FSMAGIC) {
  910. snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
  911. seq_printf(m, pt(Opt_fsmagic), tbuf);
  912. seq_puts(m, " ");
  913. }
  914. if (entry->flags & IMA_PCR) {
  915. snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
  916. seq_printf(m, pt(Opt_pcr), tbuf);
  917. seq_puts(m, " ");
  918. }
  919. if (entry->flags & IMA_FSUUID) {
  920. seq_printf(m, "fsuuid=%pU", entry->fsuuid);
  921. seq_puts(m, " ");
  922. }
  923. if (entry->flags & IMA_UID) {
  924. snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
  925. seq_printf(m, pt(Opt_uid), tbuf);
  926. seq_puts(m, " ");
  927. }
  928. if (entry->flags & IMA_EUID) {
  929. snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
  930. seq_printf(m, pt(Opt_euid), tbuf);
  931. seq_puts(m, " ");
  932. }
  933. if (entry->flags & IMA_FOWNER) {
  934. snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
  935. seq_printf(m, pt(Opt_fowner), tbuf);
  936. seq_puts(m, " ");
  937. }
  938. for (i = 0; i < MAX_LSM_RULES; i++) {
  939. if (entry->lsm[i].rule) {
  940. switch (i) {
  941. case LSM_OBJ_USER:
  942. seq_printf(m, pt(Opt_obj_user),
  943. (char *)entry->lsm[i].args_p);
  944. break;
  945. case LSM_OBJ_ROLE:
  946. seq_printf(m, pt(Opt_obj_role),
  947. (char *)entry->lsm[i].args_p);
  948. break;
  949. case LSM_OBJ_TYPE:
  950. seq_printf(m, pt(Opt_obj_type),
  951. (char *)entry->lsm[i].args_p);
  952. break;
  953. case LSM_SUBJ_USER:
  954. seq_printf(m, pt(Opt_subj_user),
  955. (char *)entry->lsm[i].args_p);
  956. break;
  957. case LSM_SUBJ_ROLE:
  958. seq_printf(m, pt(Opt_subj_role),
  959. (char *)entry->lsm[i].args_p);
  960. break;
  961. case LSM_SUBJ_TYPE:
  962. seq_printf(m, pt(Opt_subj_type),
  963. (char *)entry->lsm[i].args_p);
  964. break;
  965. }
  966. }
  967. }
  968. if (entry->flags & IMA_DIGSIG_REQUIRED)
  969. seq_puts(m, "appraise_type=imasig ");
  970. if (entry->flags & IMA_PERMIT_DIRECTIO)
  971. seq_puts(m, "permit_directio ");
  972. rcu_read_unlock();
  973. seq_puts(m, "\n");
  974. return 0;
  975. }
  976. #endif /* CONFIG_IMA_READ_POLICY */