ima_policy.c 34 KB

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