ima_policy.c 34 KB

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