ima_policy.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  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, measure_entries, appraise_entries, secure_boot_entries;
  439. /* if !ima_policy set entries = 0 so we load NO default rules */
  440. measure_entries = ima_policy ? ARRAY_SIZE(dont_measure_rules) : 0;
  441. appraise_entries = ima_use_appraise_tcb ?
  442. ARRAY_SIZE(default_appraise_rules) : 0;
  443. secure_boot_entries = ima_use_secure_boot ?
  444. ARRAY_SIZE(secure_boot_rules) : 0;
  445. for (i = 0; i < measure_entries; i++)
  446. list_add_tail(&dont_measure_rules[i].list, &ima_default_rules);
  447. switch (ima_policy) {
  448. case ORIGINAL_TCB:
  449. for (i = 0; i < ARRAY_SIZE(original_measurement_rules); i++)
  450. list_add_tail(&original_measurement_rules[i].list,
  451. &ima_default_rules);
  452. break;
  453. case DEFAULT_TCB:
  454. for (i = 0; i < ARRAY_SIZE(default_measurement_rules); i++)
  455. list_add_tail(&default_measurement_rules[i].list,
  456. &ima_default_rules);
  457. default:
  458. break;
  459. }
  460. /*
  461. * Insert the builtin "secure_boot" policy rules requiring file
  462. * signatures, prior to any other appraise rules.
  463. */
  464. for (i = 0; i < secure_boot_entries; i++) {
  465. list_add_tail(&secure_boot_rules[i].list, &ima_default_rules);
  466. temp_ima_appraise |=
  467. ima_appraise_flag(secure_boot_rules[i].func);
  468. }
  469. /*
  470. * Insert the build time appraise rules requiring file signatures
  471. * for both the initial and custom policies, prior to other appraise
  472. * rules.
  473. */
  474. for (i = 0; i < ARRAY_SIZE(build_appraise_rules); i++) {
  475. struct ima_rule_entry *entry;
  476. if (!secure_boot_entries)
  477. list_add_tail(&build_appraise_rules[i].list,
  478. &ima_default_rules);
  479. entry = kmemdup(&build_appraise_rules[i], sizeof(*entry),
  480. GFP_KERNEL);
  481. if (entry)
  482. list_add_tail(&entry->list, &ima_policy_rules);
  483. build_ima_appraise |=
  484. ima_appraise_flag(build_appraise_rules[i].func);
  485. }
  486. for (i = 0; i < appraise_entries; i++) {
  487. list_add_tail(&default_appraise_rules[i].list,
  488. &ima_default_rules);
  489. if (default_appraise_rules[i].func == POLICY_CHECK)
  490. temp_ima_appraise |= IMA_APPRAISE_POLICY;
  491. }
  492. ima_rules = &ima_default_rules;
  493. ima_update_policy_flag();
  494. }
  495. /* Make sure we have a valid policy, at least containing some rules. */
  496. int ima_check_policy(void)
  497. {
  498. if (list_empty(&ima_temp_rules))
  499. return -EINVAL;
  500. return 0;
  501. }
  502. /**
  503. * ima_update_policy - update default_rules with new measure rules
  504. *
  505. * Called on file .release to update the default rules with a complete new
  506. * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
  507. * they make a queue. The policy may be updated multiple times and this is the
  508. * RCU updater.
  509. *
  510. * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
  511. * we switch from the default policy to user defined.
  512. */
  513. void ima_update_policy(void)
  514. {
  515. struct list_head *policy = &ima_policy_rules;
  516. list_splice_tail_init_rcu(&ima_temp_rules, policy, synchronize_rcu);
  517. if (ima_rules != policy) {
  518. ima_policy_flag = 0;
  519. ima_rules = policy;
  520. }
  521. ima_update_policy_flag();
  522. }
  523. /* Keep the enumeration in sync with the policy_tokens! */
  524. enum {
  525. Opt_measure, Opt_dont_measure,
  526. Opt_appraise, Opt_dont_appraise,
  527. Opt_audit, Opt_hash, Opt_dont_hash,
  528. Opt_obj_user, Opt_obj_role, Opt_obj_type,
  529. Opt_subj_user, Opt_subj_role, Opt_subj_type,
  530. Opt_func, Opt_mask, Opt_fsmagic, Opt_fsname,
  531. Opt_fsuuid, Opt_uid_eq, Opt_euid_eq, Opt_fowner_eq,
  532. Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt,
  533. Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt,
  534. Opt_appraise_type, Opt_permit_directio,
  535. Opt_pcr, Opt_err
  536. };
  537. static const match_table_t policy_tokens = {
  538. {Opt_measure, "measure"},
  539. {Opt_dont_measure, "dont_measure"},
  540. {Opt_appraise, "appraise"},
  541. {Opt_dont_appraise, "dont_appraise"},
  542. {Opt_audit, "audit"},
  543. {Opt_hash, "hash"},
  544. {Opt_dont_hash, "dont_hash"},
  545. {Opt_obj_user, "obj_user=%s"},
  546. {Opt_obj_role, "obj_role=%s"},
  547. {Opt_obj_type, "obj_type=%s"},
  548. {Opt_subj_user, "subj_user=%s"},
  549. {Opt_subj_role, "subj_role=%s"},
  550. {Opt_subj_type, "subj_type=%s"},
  551. {Opt_func, "func=%s"},
  552. {Opt_mask, "mask=%s"},
  553. {Opt_fsmagic, "fsmagic=%s"},
  554. {Opt_fsname, "fsname=%s"},
  555. {Opt_fsuuid, "fsuuid=%s"},
  556. {Opt_uid_eq, "uid=%s"},
  557. {Opt_euid_eq, "euid=%s"},
  558. {Opt_fowner_eq, "fowner=%s"},
  559. {Opt_uid_gt, "uid>%s"},
  560. {Opt_euid_gt, "euid>%s"},
  561. {Opt_fowner_gt, "fowner>%s"},
  562. {Opt_uid_lt, "uid<%s"},
  563. {Opt_euid_lt, "euid<%s"},
  564. {Opt_fowner_lt, "fowner<%s"},
  565. {Opt_appraise_type, "appraise_type=%s"},
  566. {Opt_permit_directio, "permit_directio"},
  567. {Opt_pcr, "pcr=%s"},
  568. {Opt_err, NULL}
  569. };
  570. static int ima_lsm_rule_init(struct ima_rule_entry *entry,
  571. substring_t *args, int lsm_rule, int audit_type)
  572. {
  573. int result;
  574. if (entry->lsm[lsm_rule].rule)
  575. return -EINVAL;
  576. entry->lsm[lsm_rule].args_p = match_strdup(args);
  577. if (!entry->lsm[lsm_rule].args_p)
  578. return -ENOMEM;
  579. entry->lsm[lsm_rule].type = audit_type;
  580. result = security_filter_rule_init(entry->lsm[lsm_rule].type,
  581. Audit_equal,
  582. entry->lsm[lsm_rule].args_p,
  583. &entry->lsm[lsm_rule].rule);
  584. if (!entry->lsm[lsm_rule].rule) {
  585. kfree(entry->lsm[lsm_rule].args_p);
  586. return -EINVAL;
  587. }
  588. return result;
  589. }
  590. static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value,
  591. bool (*rule_operator)(kuid_t, kuid_t))
  592. {
  593. if (!ab)
  594. return;
  595. if (rule_operator == &uid_gt)
  596. audit_log_format(ab, "%s>", key);
  597. else if (rule_operator == &uid_lt)
  598. audit_log_format(ab, "%s<", key);
  599. else
  600. audit_log_format(ab, "%s=", key);
  601. audit_log_format(ab, "%s ", value);
  602. }
  603. static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
  604. {
  605. ima_log_string_op(ab, key, value, NULL);
  606. }
  607. static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
  608. {
  609. struct audit_buffer *ab;
  610. char *from;
  611. char *p;
  612. bool uid_token;
  613. int result = 0;
  614. ab = integrity_audit_log_start(audit_context(), GFP_KERNEL,
  615. AUDIT_INTEGRITY_POLICY_RULE);
  616. entry->uid = INVALID_UID;
  617. entry->fowner = INVALID_UID;
  618. entry->uid_op = &uid_eq;
  619. entry->fowner_op = &uid_eq;
  620. entry->action = UNKNOWN;
  621. while ((p = strsep(&rule, " \t")) != NULL) {
  622. substring_t args[MAX_OPT_ARGS];
  623. int token;
  624. unsigned long lnum;
  625. if (result < 0)
  626. break;
  627. if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
  628. continue;
  629. token = match_token(p, policy_tokens, args);
  630. switch (token) {
  631. case Opt_measure:
  632. ima_log_string(ab, "action", "measure");
  633. if (entry->action != UNKNOWN)
  634. result = -EINVAL;
  635. entry->action = MEASURE;
  636. break;
  637. case Opt_dont_measure:
  638. ima_log_string(ab, "action", "dont_measure");
  639. if (entry->action != UNKNOWN)
  640. result = -EINVAL;
  641. entry->action = DONT_MEASURE;
  642. break;
  643. case Opt_appraise:
  644. ima_log_string(ab, "action", "appraise");
  645. if (entry->action != UNKNOWN)
  646. result = -EINVAL;
  647. entry->action = APPRAISE;
  648. break;
  649. case Opt_dont_appraise:
  650. ima_log_string(ab, "action", "dont_appraise");
  651. if (entry->action != UNKNOWN)
  652. result = -EINVAL;
  653. entry->action = DONT_APPRAISE;
  654. break;
  655. case Opt_audit:
  656. ima_log_string(ab, "action", "audit");
  657. if (entry->action != UNKNOWN)
  658. result = -EINVAL;
  659. entry->action = AUDIT;
  660. break;
  661. case Opt_hash:
  662. ima_log_string(ab, "action", "hash");
  663. if (entry->action != UNKNOWN)
  664. result = -EINVAL;
  665. entry->action = HASH;
  666. break;
  667. case Opt_dont_hash:
  668. ima_log_string(ab, "action", "dont_hash");
  669. if (entry->action != UNKNOWN)
  670. result = -EINVAL;
  671. entry->action = DONT_HASH;
  672. break;
  673. case Opt_func:
  674. ima_log_string(ab, "func", args[0].from);
  675. if (entry->func)
  676. result = -EINVAL;
  677. if (strcmp(args[0].from, "FILE_CHECK") == 0)
  678. entry->func = FILE_CHECK;
  679. /* PATH_CHECK is for backwards compat */
  680. else if (strcmp(args[0].from, "PATH_CHECK") == 0)
  681. entry->func = FILE_CHECK;
  682. else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
  683. entry->func = MODULE_CHECK;
  684. else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
  685. entry->func = FIRMWARE_CHECK;
  686. else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
  687. || (strcmp(args[0].from, "MMAP_CHECK") == 0))
  688. entry->func = MMAP_CHECK;
  689. else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
  690. entry->func = BPRM_CHECK;
  691. else if (strcmp(args[0].from, "CREDS_CHECK") == 0)
  692. entry->func = CREDS_CHECK;
  693. else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
  694. 0)
  695. entry->func = KEXEC_KERNEL_CHECK;
  696. else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
  697. == 0)
  698. entry->func = KEXEC_INITRAMFS_CHECK;
  699. else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
  700. entry->func = POLICY_CHECK;
  701. else
  702. result = -EINVAL;
  703. if (!result)
  704. entry->flags |= IMA_FUNC;
  705. break;
  706. case Opt_mask:
  707. ima_log_string(ab, "mask", args[0].from);
  708. if (entry->mask)
  709. result = -EINVAL;
  710. from = args[0].from;
  711. if (*from == '^')
  712. from++;
  713. if ((strcmp(from, "MAY_EXEC")) == 0)
  714. entry->mask = MAY_EXEC;
  715. else if (strcmp(from, "MAY_WRITE") == 0)
  716. entry->mask = MAY_WRITE;
  717. else if (strcmp(from, "MAY_READ") == 0)
  718. entry->mask = MAY_READ;
  719. else if (strcmp(from, "MAY_APPEND") == 0)
  720. entry->mask = MAY_APPEND;
  721. else
  722. result = -EINVAL;
  723. if (!result)
  724. entry->flags |= (*args[0].from == '^')
  725. ? IMA_INMASK : IMA_MASK;
  726. break;
  727. case Opt_fsmagic:
  728. ima_log_string(ab, "fsmagic", args[0].from);
  729. if (entry->fsmagic) {
  730. result = -EINVAL;
  731. break;
  732. }
  733. result = kstrtoul(args[0].from, 16, &entry->fsmagic);
  734. if (!result)
  735. entry->flags |= IMA_FSMAGIC;
  736. break;
  737. case Opt_fsname:
  738. ima_log_string(ab, "fsname", args[0].from);
  739. entry->fsname = kstrdup(args[0].from, GFP_KERNEL);
  740. if (!entry->fsname) {
  741. result = -ENOMEM;
  742. break;
  743. }
  744. result = 0;
  745. entry->flags |= IMA_FSNAME;
  746. break;
  747. case Opt_fsuuid:
  748. ima_log_string(ab, "fsuuid", args[0].from);
  749. if (!uuid_is_null(&entry->fsuuid)) {
  750. result = -EINVAL;
  751. break;
  752. }
  753. result = uuid_parse(args[0].from, &entry->fsuuid);
  754. if (!result)
  755. entry->flags |= IMA_FSUUID;
  756. break;
  757. case Opt_uid_gt:
  758. case Opt_euid_gt:
  759. entry->uid_op = &uid_gt;
  760. case Opt_uid_lt:
  761. case Opt_euid_lt:
  762. if ((token == Opt_uid_lt) || (token == Opt_euid_lt))
  763. entry->uid_op = &uid_lt;
  764. case Opt_uid_eq:
  765. case Opt_euid_eq:
  766. uid_token = (token == Opt_uid_eq) ||
  767. (token == Opt_uid_gt) ||
  768. (token == Opt_uid_lt);
  769. ima_log_string_op(ab, uid_token ? "uid" : "euid",
  770. args[0].from, entry->uid_op);
  771. if (uid_valid(entry->uid)) {
  772. result = -EINVAL;
  773. break;
  774. }
  775. result = kstrtoul(args[0].from, 10, &lnum);
  776. if (!result) {
  777. entry->uid = make_kuid(current_user_ns(),
  778. (uid_t) lnum);
  779. if (!uid_valid(entry->uid) ||
  780. (uid_t)lnum != lnum)
  781. result = -EINVAL;
  782. else
  783. entry->flags |= uid_token
  784. ? IMA_UID : IMA_EUID;
  785. }
  786. break;
  787. case Opt_fowner_gt:
  788. entry->fowner_op = &uid_gt;
  789. case Opt_fowner_lt:
  790. if (token == Opt_fowner_lt)
  791. entry->fowner_op = &uid_lt;
  792. case Opt_fowner_eq:
  793. ima_log_string_op(ab, "fowner", args[0].from,
  794. entry->fowner_op);
  795. if (uid_valid(entry->fowner)) {
  796. result = -EINVAL;
  797. break;
  798. }
  799. result = kstrtoul(args[0].from, 10, &lnum);
  800. if (!result) {
  801. entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
  802. if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
  803. result = -EINVAL;
  804. else
  805. entry->flags |= IMA_FOWNER;
  806. }
  807. break;
  808. case Opt_obj_user:
  809. ima_log_string(ab, "obj_user", args[0].from);
  810. result = ima_lsm_rule_init(entry, args,
  811. LSM_OBJ_USER,
  812. AUDIT_OBJ_USER);
  813. break;
  814. case Opt_obj_role:
  815. ima_log_string(ab, "obj_role", args[0].from);
  816. result = ima_lsm_rule_init(entry, args,
  817. LSM_OBJ_ROLE,
  818. AUDIT_OBJ_ROLE);
  819. break;
  820. case Opt_obj_type:
  821. ima_log_string(ab, "obj_type", args[0].from);
  822. result = ima_lsm_rule_init(entry, args,
  823. LSM_OBJ_TYPE,
  824. AUDIT_OBJ_TYPE);
  825. break;
  826. case Opt_subj_user:
  827. ima_log_string(ab, "subj_user", args[0].from);
  828. result = ima_lsm_rule_init(entry, args,
  829. LSM_SUBJ_USER,
  830. AUDIT_SUBJ_USER);
  831. break;
  832. case Opt_subj_role:
  833. ima_log_string(ab, "subj_role", args[0].from);
  834. result = ima_lsm_rule_init(entry, args,
  835. LSM_SUBJ_ROLE,
  836. AUDIT_SUBJ_ROLE);
  837. break;
  838. case Opt_subj_type:
  839. ima_log_string(ab, "subj_type", args[0].from);
  840. result = ima_lsm_rule_init(entry, args,
  841. LSM_SUBJ_TYPE,
  842. AUDIT_SUBJ_TYPE);
  843. break;
  844. case Opt_appraise_type:
  845. if (entry->action != APPRAISE) {
  846. result = -EINVAL;
  847. break;
  848. }
  849. ima_log_string(ab, "appraise_type", args[0].from);
  850. if ((strcmp(args[0].from, "imasig")) == 0)
  851. entry->flags |= IMA_DIGSIG_REQUIRED;
  852. else
  853. result = -EINVAL;
  854. break;
  855. case Opt_permit_directio:
  856. entry->flags |= IMA_PERMIT_DIRECTIO;
  857. break;
  858. case Opt_pcr:
  859. if (entry->action != MEASURE) {
  860. result = -EINVAL;
  861. break;
  862. }
  863. ima_log_string(ab, "pcr", args[0].from);
  864. result = kstrtoint(args[0].from, 10, &entry->pcr);
  865. if (result || INVALID_PCR(entry->pcr))
  866. result = -EINVAL;
  867. else
  868. entry->flags |= IMA_PCR;
  869. break;
  870. case Opt_err:
  871. ima_log_string(ab, "UNKNOWN", p);
  872. result = -EINVAL;
  873. break;
  874. }
  875. }
  876. if (!result && (entry->action == UNKNOWN))
  877. result = -EINVAL;
  878. else if (entry->action == APPRAISE)
  879. temp_ima_appraise |= ima_appraise_flag(entry->func);
  880. audit_log_format(ab, "res=%d", !result);
  881. audit_log_end(ab);
  882. return result;
  883. }
  884. /**
  885. * ima_parse_add_rule - add a rule to ima_policy_rules
  886. * @rule - ima measurement policy rule
  887. *
  888. * Avoid locking by allowing just one writer at a time in ima_write_policy()
  889. * Returns the length of the rule parsed, an error code on failure
  890. */
  891. ssize_t ima_parse_add_rule(char *rule)
  892. {
  893. static const char op[] = "update_policy";
  894. char *p;
  895. struct ima_rule_entry *entry;
  896. ssize_t result, len;
  897. int audit_info = 0;
  898. p = strsep(&rule, "\n");
  899. len = strlen(p) + 1;
  900. p += strspn(p, " \t");
  901. if (*p == '#' || *p == '\0')
  902. return len;
  903. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  904. if (!entry) {
  905. integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
  906. NULL, op, "-ENOMEM", -ENOMEM, audit_info);
  907. return -ENOMEM;
  908. }
  909. INIT_LIST_HEAD(&entry->list);
  910. result = ima_parse_rule(p, entry);
  911. if (result) {
  912. kfree(entry);
  913. integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
  914. NULL, op, "invalid-policy", result,
  915. audit_info);
  916. return result;
  917. }
  918. list_add_tail(&entry->list, &ima_temp_rules);
  919. return len;
  920. }
  921. /**
  922. * ima_delete_rules() called to cleanup invalid in-flight policy.
  923. * We don't need locking as we operate on the temp list, which is
  924. * different from the active one. There is also only one user of
  925. * ima_delete_rules() at a time.
  926. */
  927. void ima_delete_rules(void)
  928. {
  929. struct ima_rule_entry *entry, *tmp;
  930. int i;
  931. temp_ima_appraise = 0;
  932. list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
  933. for (i = 0; i < MAX_LSM_RULES; i++)
  934. kfree(entry->lsm[i].args_p);
  935. list_del(&entry->list);
  936. kfree(entry);
  937. }
  938. }
  939. #ifdef CONFIG_IMA_READ_POLICY
  940. enum {
  941. mask_exec = 0, mask_write, mask_read, mask_append
  942. };
  943. static const char *const mask_tokens[] = {
  944. "MAY_EXEC",
  945. "MAY_WRITE",
  946. "MAY_READ",
  947. "MAY_APPEND"
  948. };
  949. #define __ima_hook_stringify(str) (#str),
  950. static const char *const func_tokens[] = {
  951. __ima_hooks(__ima_hook_stringify)
  952. };
  953. void *ima_policy_start(struct seq_file *m, loff_t *pos)
  954. {
  955. loff_t l = *pos;
  956. struct ima_rule_entry *entry;
  957. rcu_read_lock();
  958. list_for_each_entry_rcu(entry, ima_rules, list) {
  959. if (!l--) {
  960. rcu_read_unlock();
  961. return entry;
  962. }
  963. }
  964. rcu_read_unlock();
  965. return NULL;
  966. }
  967. void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
  968. {
  969. struct ima_rule_entry *entry = v;
  970. rcu_read_lock();
  971. entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
  972. rcu_read_unlock();
  973. (*pos)++;
  974. return (&entry->list == ima_rules) ? NULL : entry;
  975. }
  976. void ima_policy_stop(struct seq_file *m, void *v)
  977. {
  978. }
  979. #define pt(token) policy_tokens[token].pattern
  980. #define mt(token) mask_tokens[token]
  981. /*
  982. * policy_func_show - display the ima_hooks policy rule
  983. */
  984. static void policy_func_show(struct seq_file *m, enum ima_hooks func)
  985. {
  986. if (func > 0 && func < MAX_CHECK)
  987. seq_printf(m, "func=%s ", func_tokens[func]);
  988. else
  989. seq_printf(m, "func=%d ", func);
  990. }
  991. int ima_policy_show(struct seq_file *m, void *v)
  992. {
  993. struct ima_rule_entry *entry = v;
  994. int i;
  995. char tbuf[64] = {0,};
  996. rcu_read_lock();
  997. if (entry->action & MEASURE)
  998. seq_puts(m, pt(Opt_measure));
  999. if (entry->action & DONT_MEASURE)
  1000. seq_puts(m, pt(Opt_dont_measure));
  1001. if (entry->action & APPRAISE)
  1002. seq_puts(m, pt(Opt_appraise));
  1003. if (entry->action & DONT_APPRAISE)
  1004. seq_puts(m, pt(Opt_dont_appraise));
  1005. if (entry->action & AUDIT)
  1006. seq_puts(m, pt(Opt_audit));
  1007. if (entry->action & HASH)
  1008. seq_puts(m, pt(Opt_hash));
  1009. if (entry->action & DONT_HASH)
  1010. seq_puts(m, pt(Opt_dont_hash));
  1011. seq_puts(m, " ");
  1012. if (entry->flags & IMA_FUNC)
  1013. policy_func_show(m, entry->func);
  1014. if (entry->flags & IMA_MASK) {
  1015. if (entry->mask & MAY_EXEC)
  1016. seq_printf(m, pt(Opt_mask), mt(mask_exec));
  1017. if (entry->mask & MAY_WRITE)
  1018. seq_printf(m, pt(Opt_mask), mt(mask_write));
  1019. if (entry->mask & MAY_READ)
  1020. seq_printf(m, pt(Opt_mask), mt(mask_read));
  1021. if (entry->mask & MAY_APPEND)
  1022. seq_printf(m, pt(Opt_mask), mt(mask_append));
  1023. seq_puts(m, " ");
  1024. }
  1025. if (entry->flags & IMA_FSMAGIC) {
  1026. snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
  1027. seq_printf(m, pt(Opt_fsmagic), tbuf);
  1028. seq_puts(m, " ");
  1029. }
  1030. if (entry->flags & IMA_FSNAME) {
  1031. snprintf(tbuf, sizeof(tbuf), "%s", entry->fsname);
  1032. seq_printf(m, pt(Opt_fsname), tbuf);
  1033. seq_puts(m, " ");
  1034. }
  1035. if (entry->flags & IMA_PCR) {
  1036. snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
  1037. seq_printf(m, pt(Opt_pcr), tbuf);
  1038. seq_puts(m, " ");
  1039. }
  1040. if (entry->flags & IMA_FSUUID) {
  1041. seq_printf(m, "fsuuid=%pU", &entry->fsuuid);
  1042. seq_puts(m, " ");
  1043. }
  1044. if (entry->flags & IMA_UID) {
  1045. snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
  1046. if (entry->uid_op == &uid_gt)
  1047. seq_printf(m, pt(Opt_uid_gt), tbuf);
  1048. else if (entry->uid_op == &uid_lt)
  1049. seq_printf(m, pt(Opt_uid_lt), tbuf);
  1050. else
  1051. seq_printf(m, pt(Opt_uid_eq), tbuf);
  1052. seq_puts(m, " ");
  1053. }
  1054. if (entry->flags & IMA_EUID) {
  1055. snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
  1056. if (entry->uid_op == &uid_gt)
  1057. seq_printf(m, pt(Opt_euid_gt), tbuf);
  1058. else if (entry->uid_op == &uid_lt)
  1059. seq_printf(m, pt(Opt_euid_lt), tbuf);
  1060. else
  1061. seq_printf(m, pt(Opt_euid_eq), tbuf);
  1062. seq_puts(m, " ");
  1063. }
  1064. if (entry->flags & IMA_FOWNER) {
  1065. snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
  1066. if (entry->fowner_op == &uid_gt)
  1067. seq_printf(m, pt(Opt_fowner_gt), tbuf);
  1068. else if (entry->fowner_op == &uid_lt)
  1069. seq_printf(m, pt(Opt_fowner_lt), tbuf);
  1070. else
  1071. seq_printf(m, pt(Opt_fowner_eq), tbuf);
  1072. seq_puts(m, " ");
  1073. }
  1074. for (i = 0; i < MAX_LSM_RULES; i++) {
  1075. if (entry->lsm[i].rule) {
  1076. switch (i) {
  1077. case LSM_OBJ_USER:
  1078. seq_printf(m, pt(Opt_obj_user),
  1079. (char *)entry->lsm[i].args_p);
  1080. break;
  1081. case LSM_OBJ_ROLE:
  1082. seq_printf(m, pt(Opt_obj_role),
  1083. (char *)entry->lsm[i].args_p);
  1084. break;
  1085. case LSM_OBJ_TYPE:
  1086. seq_printf(m, pt(Opt_obj_type),
  1087. (char *)entry->lsm[i].args_p);
  1088. break;
  1089. case LSM_SUBJ_USER:
  1090. seq_printf(m, pt(Opt_subj_user),
  1091. (char *)entry->lsm[i].args_p);
  1092. break;
  1093. case LSM_SUBJ_ROLE:
  1094. seq_printf(m, pt(Opt_subj_role),
  1095. (char *)entry->lsm[i].args_p);
  1096. break;
  1097. case LSM_SUBJ_TYPE:
  1098. seq_printf(m, pt(Opt_subj_type),
  1099. (char *)entry->lsm[i].args_p);
  1100. break;
  1101. }
  1102. }
  1103. }
  1104. if (entry->flags & IMA_DIGSIG_REQUIRED)
  1105. seq_puts(m, "appraise_type=imasig ");
  1106. if (entry->flags & IMA_PERMIT_DIRECTIO)
  1107. seq_puts(m, "permit_directio ");
  1108. rcu_read_unlock();
  1109. seq_puts(m, "\n");
  1110. return 0;
  1111. }
  1112. #endif /* CONFIG_IMA_READ_POLICY */