ima_policy.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2008 IBM Corporation
  4. * Author: Mimi Zohar <zohar@us.ibm.com>
  5. *
  6. * ima_policy.c
  7. * - initialize default measure policy rules
  8. */
  9. #include <linux/init.h>
  10. #include <linux/list.h>
  11. #include <linux/kernel_read_file.h>
  12. #include <linux/fs.h>
  13. #include <linux/security.h>
  14. #include <linux/magic.h>
  15. #include <linux/parser.h>
  16. #include <linux/slab.h>
  17. #include <linux/rculist.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/ima.h>
  20. #include "ima.h"
  21. /* flags definitions */
  22. #define IMA_FUNC 0x0001
  23. #define IMA_MASK 0x0002
  24. #define IMA_FSMAGIC 0x0004
  25. #define IMA_UID 0x0008
  26. #define IMA_FOWNER 0x0010
  27. #define IMA_FSUUID 0x0020
  28. #define IMA_INMASK 0x0040
  29. #define IMA_EUID 0x0080
  30. #define IMA_PCR 0x0100
  31. #define IMA_FSNAME 0x0200
  32. #define IMA_KEYRINGS 0x0400
  33. #define IMA_LABEL 0x0800
  34. #define IMA_VALIDATE_ALGOS 0x1000
  35. #define IMA_GID 0x2000
  36. #define IMA_EGID 0x4000
  37. #define IMA_FGROUP 0x8000
  38. #define IMA_FS_SUBTYPE 0x10000
  39. #define UNKNOWN 0
  40. #define MEASURE 0x0001 /* same as IMA_MEASURE */
  41. #define DONT_MEASURE 0x0002
  42. #define APPRAISE 0x0004 /* same as IMA_APPRAISE */
  43. #define DONT_APPRAISE 0x0008
  44. #define AUDIT 0x0040
  45. #define DONT_AUDIT 0x0080
  46. #define HASH 0x0100
  47. #define DONT_HASH 0x0200
  48. #define INVALID_PCR(a) (((a) < 0) || \
  49. (a) >= (sizeof_field(struct ima_iint_cache, measured_pcrs) * 8))
  50. int ima_policy_flag;
  51. static int temp_ima_appraise;
  52. static int build_ima_appraise __ro_after_init;
  53. atomic_t ima_setxattr_allowed_hash_algorithms;
  54. #define MAX_LSM_RULES 6
  55. enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
  56. LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
  57. };
  58. enum policy_types { ORIGINAL_TCB = 1, DEFAULT_TCB };
  59. enum policy_rule_list { IMA_DEFAULT_POLICY = 1, IMA_CUSTOM_POLICY };
  60. struct ima_rule_opt_list {
  61. size_t count;
  62. char *items[] __counted_by(count);
  63. };
  64. /*
  65. * These comparators are needed nowhere outside of ima so just define them here.
  66. * This pattern should hopefully never be needed outside of ima.
  67. */
  68. static inline bool vfsuid_gt_kuid(vfsuid_t vfsuid, kuid_t kuid)
  69. {
  70. return __vfsuid_val(vfsuid) > __kuid_val(kuid);
  71. }
  72. static inline bool vfsgid_gt_kgid(vfsgid_t vfsgid, kgid_t kgid)
  73. {
  74. return __vfsgid_val(vfsgid) > __kgid_val(kgid);
  75. }
  76. static inline bool vfsuid_lt_kuid(vfsuid_t vfsuid, kuid_t kuid)
  77. {
  78. return __vfsuid_val(vfsuid) < __kuid_val(kuid);
  79. }
  80. static inline bool vfsgid_lt_kgid(vfsgid_t vfsgid, kgid_t kgid)
  81. {
  82. return __vfsgid_val(vfsgid) < __kgid_val(kgid);
  83. }
  84. struct ima_rule_entry {
  85. struct list_head list;
  86. int action;
  87. unsigned int flags;
  88. enum ima_hooks func;
  89. int mask;
  90. unsigned long fsmagic;
  91. uuid_t fsuuid;
  92. kuid_t uid;
  93. kgid_t gid;
  94. kuid_t fowner;
  95. kgid_t fgroup;
  96. bool (*uid_op)(kuid_t cred_uid, kuid_t rule_uid); /* Handlers for operators */
  97. bool (*gid_op)(kgid_t cred_gid, kgid_t rule_gid);
  98. bool (*fowner_op)(vfsuid_t vfsuid, kuid_t rule_uid); /* vfsuid_eq_kuid(), vfsuid_gt_kuid(), vfsuid_lt_kuid() */
  99. bool (*fgroup_op)(vfsgid_t vfsgid, kgid_t rule_gid); /* vfsgid_eq_kgid(), vfsgid_gt_kgid(), vfsgid_lt_kgid() */
  100. int pcr;
  101. unsigned int allowed_algos; /* bitfield of allowed hash algorithms */
  102. struct {
  103. void *rule; /* LSM file metadata specific */
  104. char *args_p; /* audit value */
  105. int type; /* audit type */
  106. } lsm[MAX_LSM_RULES];
  107. char *fsname;
  108. char *fs_subtype;
  109. struct ima_rule_opt_list *keyrings; /* Measure keys added to these keyrings */
  110. struct ima_rule_opt_list *label; /* Measure data grouped under this label */
  111. struct ima_template_desc *template;
  112. };
  113. /*
  114. * sanity check in case the kernels gains more hash algorithms that can
  115. * fit in an unsigned int
  116. */
  117. static_assert(
  118. 8 * sizeof(unsigned int) >= HASH_ALGO__LAST,
  119. "The bitfield allowed_algos in ima_rule_entry is too small to contain all the supported hash algorithms, consider using a bigger type");
  120. /*
  121. * Without LSM specific knowledge, the default policy can only be
  122. * written in terms of .action, .func, .mask, .fsmagic, .uid, .gid,
  123. * .fowner, and .fgroup
  124. */
  125. /*
  126. * The minimum rule set to allow for full TCB coverage. Measures all files
  127. * opened or mmap for exec and everything read by root. Dangerous because
  128. * normal users can easily run the machine out of memory simply building
  129. * and running executables.
  130. */
  131. static struct ima_rule_entry dont_measure_rules[] __ro_after_init = {
  132. {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
  133. {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
  134. {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
  135. {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .func = FILE_CHECK,
  136. .flags = IMA_FSMAGIC | IMA_FUNC},
  137. {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
  138. {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
  139. {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
  140. {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
  141. {.action = DONT_MEASURE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
  142. {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
  143. .flags = IMA_FSMAGIC},
  144. {.action = DONT_MEASURE, .fsmagic = CGROUP2_SUPER_MAGIC,
  145. .flags = IMA_FSMAGIC},
  146. {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
  147. {.action = DONT_MEASURE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC}
  148. };
  149. static struct ima_rule_entry original_measurement_rules[] __ro_after_init = {
  150. {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
  151. .flags = IMA_FUNC | IMA_MASK},
  152. {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
  153. .flags = IMA_FUNC | IMA_MASK},
  154. {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
  155. .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
  156. .flags = IMA_FUNC | IMA_MASK | IMA_UID},
  157. {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
  158. {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
  159. };
  160. static struct ima_rule_entry default_measurement_rules[] __ro_after_init = {
  161. {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
  162. .flags = IMA_FUNC | IMA_MASK},
  163. {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
  164. .flags = IMA_FUNC | IMA_MASK},
  165. {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
  166. .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
  167. .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
  168. {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
  169. .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
  170. .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
  171. {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
  172. {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
  173. {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC},
  174. };
  175. static struct ima_rule_entry default_appraise_rules[] __ro_after_init = {
  176. {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
  177. {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
  178. {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
  179. {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
  180. {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
  181. {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
  182. {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
  183. {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
  184. {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
  185. {.action = DONT_APPRAISE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
  186. {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
  187. {.action = DONT_APPRAISE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC},
  188. {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
  189. {.action = DONT_APPRAISE, .fsmagic = CGROUP2_SUPER_MAGIC, .flags = IMA_FSMAGIC},
  190. #ifdef CONFIG_IMA_WRITE_POLICY
  191. {.action = APPRAISE, .func = POLICY_CHECK,
  192. .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
  193. #endif
  194. #ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
  195. {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &vfsuid_eq_kuid,
  196. .flags = IMA_FOWNER},
  197. #else
  198. /* force signature */
  199. {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &vfsuid_eq_kuid,
  200. .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
  201. #endif
  202. };
  203. static struct ima_rule_entry build_appraise_rules[] __ro_after_init = {
  204. #ifdef CONFIG_IMA_APPRAISE_REQUIRE_MODULE_SIGS
  205. {.action = APPRAISE, .func = MODULE_CHECK,
  206. .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
  207. #endif
  208. #ifdef CONFIG_IMA_APPRAISE_REQUIRE_FIRMWARE_SIGS
  209. {.action = APPRAISE, .func = FIRMWARE_CHECK,
  210. .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
  211. #endif
  212. #ifdef CONFIG_IMA_APPRAISE_REQUIRE_KEXEC_SIGS
  213. {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
  214. .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
  215. #endif
  216. #ifdef CONFIG_IMA_APPRAISE_REQUIRE_POLICY_SIGS
  217. {.action = APPRAISE, .func = POLICY_CHECK,
  218. .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
  219. #endif
  220. };
  221. static struct ima_rule_entry secure_boot_rules[] __ro_after_init = {
  222. {.action = APPRAISE, .func = MODULE_CHECK,
  223. .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED | IMA_MODSIG_ALLOWED |
  224. IMA_CHECK_BLACKLIST},
  225. {.action = APPRAISE, .func = FIRMWARE_CHECK,
  226. .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
  227. {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
  228. .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
  229. {.action = APPRAISE, .func = POLICY_CHECK,
  230. .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
  231. };
  232. static struct ima_rule_entry critical_data_rules[] __ro_after_init = {
  233. {.action = MEASURE, .func = CRITICAL_DATA, .flags = IMA_FUNC},
  234. };
  235. /* An array of architecture specific rules */
  236. static struct ima_rule_entry *arch_policy_entry __ro_after_init;
  237. static LIST_HEAD(ima_default_rules);
  238. static LIST_HEAD(ima_policy_rules);
  239. static LIST_HEAD(ima_temp_rules);
  240. static struct list_head __rcu *ima_rules = (struct list_head __rcu *)(&ima_default_rules);
  241. static int ima_policy __initdata;
  242. static int __init default_measure_policy_setup(char *str)
  243. {
  244. if (ima_policy)
  245. return 1;
  246. ima_policy = ORIGINAL_TCB;
  247. return 1;
  248. }
  249. __setup("ima_tcb", default_measure_policy_setup);
  250. static bool ima_use_appraise_tcb __initdata;
  251. static bool ima_use_secure_boot __initdata;
  252. static bool ima_use_critical_data __initdata;
  253. static bool ima_fail_unverifiable_sigs __ro_after_init;
  254. static int __init policy_setup(char *str)
  255. {
  256. char *p;
  257. while ((p = strsep(&str, " |\n")) != NULL) {
  258. if (*p == ' ')
  259. continue;
  260. if ((strcmp(p, "tcb") == 0) && !ima_policy)
  261. ima_policy = DEFAULT_TCB;
  262. else if (strcmp(p, "appraise_tcb") == 0)
  263. ima_use_appraise_tcb = true;
  264. else if (strcmp(p, "secure_boot") == 0)
  265. ima_use_secure_boot = true;
  266. else if (strcmp(p, "critical_data") == 0)
  267. ima_use_critical_data = true;
  268. else if (strcmp(p, "fail_securely") == 0)
  269. ima_fail_unverifiable_sigs = true;
  270. else
  271. pr_err("policy \"%s\" not found", p);
  272. }
  273. return 1;
  274. }
  275. __setup("ima_policy=", policy_setup);
  276. static int __init default_appraise_policy_setup(char *str)
  277. {
  278. ima_use_appraise_tcb = true;
  279. return 1;
  280. }
  281. __setup("ima_appraise_tcb", default_appraise_policy_setup);
  282. static struct ima_rule_opt_list *ima_alloc_rule_opt_list(const substring_t *src)
  283. {
  284. struct ima_rule_opt_list *opt_list;
  285. size_t count = 0;
  286. char *src_copy;
  287. char *cur, *next;
  288. size_t i;
  289. src_copy = match_strdup(src);
  290. if (!src_copy)
  291. return ERR_PTR(-ENOMEM);
  292. next = src_copy;
  293. while ((cur = strsep(&next, "|"))) {
  294. /* Don't accept an empty list item */
  295. if (!(*cur)) {
  296. kfree(src_copy);
  297. return ERR_PTR(-EINVAL);
  298. }
  299. count++;
  300. }
  301. /* Don't accept an empty list */
  302. if (!count) {
  303. kfree(src_copy);
  304. return ERR_PTR(-EINVAL);
  305. }
  306. opt_list = kzalloc_flex(*opt_list, items, count);
  307. if (!opt_list) {
  308. kfree(src_copy);
  309. return ERR_PTR(-ENOMEM);
  310. }
  311. opt_list->count = count;
  312. /*
  313. * strsep() has already replaced all instances of '|' with '\0',
  314. * leaving a byte sequence of NUL-terminated strings. Reference each
  315. * string with the array of items.
  316. *
  317. * IMPORTANT: Ownership of the allocated buffer is transferred from
  318. * src_copy to the first element in the items array. To free the
  319. * buffer, kfree() must only be called on the first element of the
  320. * array.
  321. */
  322. for (i = 0, cur = src_copy; i < count; i++) {
  323. opt_list->items[i] = cur;
  324. cur = strchr(cur, '\0') + 1;
  325. }
  326. return opt_list;
  327. }
  328. static void ima_free_rule_opt_list(struct ima_rule_opt_list *opt_list)
  329. {
  330. if (!opt_list)
  331. return;
  332. if (opt_list->count) {
  333. kfree(opt_list->items[0]);
  334. opt_list->count = 0;
  335. }
  336. kfree(opt_list);
  337. }
  338. static void ima_lsm_free_rule(struct ima_rule_entry *entry)
  339. {
  340. int i;
  341. for (i = 0; i < MAX_LSM_RULES; i++) {
  342. ima_filter_rule_free(entry->lsm[i].rule);
  343. kfree(entry->lsm[i].args_p);
  344. }
  345. }
  346. static void ima_free_rule(struct ima_rule_entry *entry)
  347. {
  348. if (!entry)
  349. return;
  350. /*
  351. * entry->template->fields may be allocated in ima_parse_rule() but that
  352. * reference is owned by the corresponding ima_template_desc element in
  353. * the defined_templates list and cannot be freed here
  354. */
  355. kfree(entry->fsname);
  356. kfree(entry->fs_subtype);
  357. ima_free_rule_opt_list(entry->keyrings);
  358. ima_lsm_free_rule(entry);
  359. kfree(entry);
  360. }
  361. static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry,
  362. gfp_t gfp)
  363. {
  364. struct ima_rule_entry *nentry;
  365. int i;
  366. /*
  367. * Immutable elements are copied over as pointers and data; only
  368. * lsm rules can change
  369. */
  370. nentry = kmemdup(entry, sizeof(*nentry), gfp);
  371. if (!nentry)
  372. return NULL;
  373. memset(nentry->lsm, 0, sizeof_field(struct ima_rule_entry, lsm));
  374. for (i = 0; i < MAX_LSM_RULES; i++) {
  375. if (!entry->lsm[i].args_p)
  376. continue;
  377. nentry->lsm[i].type = entry->lsm[i].type;
  378. nentry->lsm[i].args_p = entry->lsm[i].args_p;
  379. ima_filter_rule_init(nentry->lsm[i].type, Audit_equal,
  380. nentry->lsm[i].args_p,
  381. &nentry->lsm[i].rule,
  382. gfp);
  383. if (!nentry->lsm[i].rule)
  384. pr_warn("rule for LSM \'%s\' is undefined\n",
  385. nentry->lsm[i].args_p);
  386. }
  387. return nentry;
  388. }
  389. static int ima_lsm_update_rule(struct ima_rule_entry *entry)
  390. {
  391. int i;
  392. struct ima_rule_entry *nentry;
  393. nentry = ima_lsm_copy_rule(entry, GFP_KERNEL);
  394. if (!nentry)
  395. return -ENOMEM;
  396. list_replace_rcu(&entry->list, &nentry->list);
  397. synchronize_rcu();
  398. /*
  399. * ima_lsm_copy_rule() shallow copied all references, except for the
  400. * LSM references, from entry to nentry so we only want to free the LSM
  401. * references and the entry itself. All other memory references will now
  402. * be owned by nentry.
  403. */
  404. for (i = 0; i < MAX_LSM_RULES; i++)
  405. ima_filter_rule_free(entry->lsm[i].rule);
  406. kfree(entry);
  407. return 0;
  408. }
  409. static bool ima_rule_contains_lsm_cond(struct ima_rule_entry *entry)
  410. {
  411. int i;
  412. for (i = 0; i < MAX_LSM_RULES; i++)
  413. if (entry->lsm[i].args_p)
  414. return true;
  415. return false;
  416. }
  417. /*
  418. * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
  419. * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
  420. * the reloaded LSM policy.
  421. */
  422. static void ima_lsm_update_rules(void)
  423. {
  424. struct ima_rule_entry *entry, *e;
  425. int result;
  426. list_for_each_entry_safe(entry, e, &ima_policy_rules, list) {
  427. if (!ima_rule_contains_lsm_cond(entry))
  428. continue;
  429. result = ima_lsm_update_rule(entry);
  430. if (result) {
  431. pr_err("lsm rule update error %d\n", result);
  432. return;
  433. }
  434. }
  435. }
  436. int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
  437. void *lsm_data)
  438. {
  439. if (event != LSM_POLICY_CHANGE)
  440. return NOTIFY_DONE;
  441. ima_lsm_update_rules();
  442. return NOTIFY_OK;
  443. }
  444. /**
  445. * ima_match_rule_data - determine whether func_data matches the policy rule
  446. * @rule: a pointer to a rule
  447. * @func_data: data to match against the measure rule data
  448. * @cred: a pointer to a credentials structure for user validation
  449. *
  450. * Returns true if func_data matches one in the rule, false otherwise.
  451. */
  452. static bool ima_match_rule_data(struct ima_rule_entry *rule,
  453. const char *func_data,
  454. const struct cred *cred)
  455. {
  456. const struct ima_rule_opt_list *opt_list = NULL;
  457. bool matched = false;
  458. size_t i;
  459. if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
  460. return false;
  461. switch (rule->func) {
  462. case KEY_CHECK:
  463. if (!rule->keyrings)
  464. return true;
  465. opt_list = rule->keyrings;
  466. break;
  467. case CRITICAL_DATA:
  468. if (!rule->label)
  469. return true;
  470. opt_list = rule->label;
  471. break;
  472. default:
  473. return false;
  474. }
  475. if (!func_data)
  476. return false;
  477. for (i = 0; i < opt_list->count; i++) {
  478. if (!strcmp(opt_list->items[i], func_data)) {
  479. matched = true;
  480. break;
  481. }
  482. }
  483. return matched;
  484. }
  485. /**
  486. * ima_match_rules - determine whether an inode matches the policy rule.
  487. * @rule: a pointer to a rule
  488. * @idmap: idmap of the mount the inode was found from
  489. * @inode: a pointer to an inode
  490. * @cred: a pointer to a credentials structure for user validation
  491. * @prop: LSM properties of the task to be validated
  492. * @func: LIM hook identifier
  493. * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
  494. * @func_data: func specific data, may be NULL
  495. *
  496. * Returns true on rule match, false on failure.
  497. */
  498. static bool ima_match_rules(struct ima_rule_entry *rule,
  499. struct mnt_idmap *idmap,
  500. struct inode *inode, const struct cred *cred,
  501. struct lsm_prop *prop, enum ima_hooks func, int mask,
  502. const char *func_data)
  503. {
  504. int i;
  505. bool result = false;
  506. struct ima_rule_entry *lsm_rule = rule;
  507. bool rule_reinitialized = false;
  508. if ((rule->flags & IMA_FUNC) &&
  509. (rule->func != func && func != POST_SETATTR))
  510. return false;
  511. switch (func) {
  512. case KEY_CHECK:
  513. case CRITICAL_DATA:
  514. return ((rule->func == func) &&
  515. ima_match_rule_data(rule, func_data, cred));
  516. default:
  517. break;
  518. }
  519. if ((rule->flags & IMA_MASK) &&
  520. (rule->mask != mask && func != POST_SETATTR))
  521. return false;
  522. if ((rule->flags & IMA_INMASK) &&
  523. (!(rule->mask & mask) && func != POST_SETATTR))
  524. return false;
  525. if ((rule->flags & IMA_FSMAGIC)
  526. && rule->fsmagic != inode->i_sb->s_magic)
  527. return false;
  528. if ((rule->flags & IMA_FSNAME)
  529. && strcmp(rule->fsname, inode->i_sb->s_type->name))
  530. return false;
  531. if (rule->flags & IMA_FS_SUBTYPE) {
  532. if (!inode->i_sb->s_subtype)
  533. return false;
  534. if (strcmp(rule->fs_subtype, inode->i_sb->s_subtype))
  535. return false;
  536. }
  537. if ((rule->flags & IMA_FSUUID) &&
  538. !uuid_equal(&rule->fsuuid, &inode->i_sb->s_uuid))
  539. return false;
  540. if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
  541. return false;
  542. if (rule->flags & IMA_EUID) {
  543. if (has_capability_noaudit(current, CAP_SETUID)) {
  544. if (!rule->uid_op(cred->euid, rule->uid)
  545. && !rule->uid_op(cred->suid, rule->uid)
  546. && !rule->uid_op(cred->uid, rule->uid))
  547. return false;
  548. } else if (!rule->uid_op(cred->euid, rule->uid))
  549. return false;
  550. }
  551. if ((rule->flags & IMA_GID) && !rule->gid_op(cred->gid, rule->gid))
  552. return false;
  553. if (rule->flags & IMA_EGID) {
  554. if (has_capability_noaudit(current, CAP_SETGID)) {
  555. if (!rule->gid_op(cred->egid, rule->gid)
  556. && !rule->gid_op(cred->sgid, rule->gid)
  557. && !rule->gid_op(cred->gid, rule->gid))
  558. return false;
  559. } else if (!rule->gid_op(cred->egid, rule->gid))
  560. return false;
  561. }
  562. if ((rule->flags & IMA_FOWNER) &&
  563. !rule->fowner_op(i_uid_into_vfsuid(idmap, inode),
  564. rule->fowner))
  565. return false;
  566. if ((rule->flags & IMA_FGROUP) &&
  567. !rule->fgroup_op(i_gid_into_vfsgid(idmap, inode),
  568. rule->fgroup))
  569. return false;
  570. for (i = 0; i < MAX_LSM_RULES; i++) {
  571. int rc = 0;
  572. struct lsm_prop inode_prop = { };
  573. if (!lsm_rule->lsm[i].rule) {
  574. if (!lsm_rule->lsm[i].args_p)
  575. continue;
  576. else
  577. return false;
  578. }
  579. retry:
  580. switch (i) {
  581. case LSM_OBJ_USER:
  582. case LSM_OBJ_ROLE:
  583. case LSM_OBJ_TYPE:
  584. security_inode_getlsmprop(inode, &inode_prop);
  585. rc = ima_filter_rule_match(&inode_prop,
  586. lsm_rule->lsm[i].type,
  587. Audit_equal,
  588. lsm_rule->lsm[i].rule);
  589. break;
  590. case LSM_SUBJ_USER:
  591. case LSM_SUBJ_ROLE:
  592. case LSM_SUBJ_TYPE:
  593. rc = ima_filter_rule_match(prop, lsm_rule->lsm[i].type,
  594. Audit_equal,
  595. lsm_rule->lsm[i].rule);
  596. break;
  597. default:
  598. break;
  599. }
  600. if (rc == -ESTALE && !rule_reinitialized) {
  601. lsm_rule = ima_lsm_copy_rule(rule, GFP_ATOMIC);
  602. if (lsm_rule) {
  603. rule_reinitialized = true;
  604. goto retry;
  605. }
  606. }
  607. if (rc <= 0) {
  608. result = false;
  609. goto out;
  610. }
  611. }
  612. result = true;
  613. out:
  614. if (rule_reinitialized) {
  615. for (i = 0; i < MAX_LSM_RULES; i++)
  616. ima_filter_rule_free(lsm_rule->lsm[i].rule);
  617. kfree(lsm_rule);
  618. }
  619. return result;
  620. }
  621. /*
  622. * In addition to knowing that we need to appraise the file in general,
  623. * we need to differentiate between calling hooks, for hook specific rules.
  624. */
  625. static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
  626. {
  627. if (!(rule->flags & IMA_FUNC))
  628. return IMA_FILE_APPRAISE;
  629. switch (func) {
  630. case MMAP_CHECK:
  631. case MMAP_CHECK_REQPROT:
  632. return IMA_MMAP_APPRAISE;
  633. case BPRM_CHECK:
  634. return IMA_BPRM_APPRAISE;
  635. case CREDS_CHECK:
  636. return IMA_CREDS_APPRAISE;
  637. case FILE_CHECK:
  638. case POST_SETATTR:
  639. return IMA_FILE_APPRAISE;
  640. case MODULE_CHECK ... MAX_CHECK - 1:
  641. default:
  642. return IMA_READ_APPRAISE;
  643. }
  644. }
  645. /**
  646. * ima_match_policy - decision based on LSM and other conditions
  647. * @idmap: idmap of the mount the inode was found from
  648. * @inode: pointer to an inode for which the policy decision is being made
  649. * @cred: pointer to a credentials structure for which the policy decision is
  650. * being made
  651. * @prop: LSM properties of the task to be validated
  652. * @func: IMA hook identifier
  653. * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
  654. * @flags: IMA actions to consider (e.g. IMA_MEASURE | IMA_APPRAISE)
  655. * @pcr: set the pcr to extend
  656. * @template_desc: the template that should be used for this rule
  657. * @func_data: func specific data, may be NULL
  658. * @allowed_algos: allowlist of hash algorithms for the IMA xattr
  659. *
  660. * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
  661. * conditions.
  662. *
  663. * Since the IMA policy may be updated multiple times we need to lock the
  664. * list when walking it. Reads are many orders of magnitude more numerous
  665. * than writes so ima_match_policy() is classical RCU candidate.
  666. */
  667. int ima_match_policy(struct mnt_idmap *idmap, struct inode *inode,
  668. const struct cred *cred, struct lsm_prop *prop,
  669. enum ima_hooks func, int mask, int flags, int *pcr,
  670. struct ima_template_desc **template_desc,
  671. const char *func_data, unsigned int *allowed_algos)
  672. {
  673. struct ima_rule_entry *entry;
  674. int action = 0, actmask = flags | (flags << 1);
  675. struct list_head *ima_rules_tmp;
  676. if (template_desc && !*template_desc)
  677. *template_desc = ima_template_desc_current();
  678. rcu_read_lock();
  679. ima_rules_tmp = rcu_dereference(ima_rules);
  680. list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
  681. if (!(entry->action & actmask))
  682. continue;
  683. if (!ima_match_rules(entry, idmap, inode, cred, prop,
  684. func, mask, func_data))
  685. continue;
  686. action |= entry->flags & IMA_NONACTION_FLAGS;
  687. action |= entry->action & IMA_DO_MASK;
  688. if (entry->action & IMA_APPRAISE) {
  689. action |= get_subaction(entry, func);
  690. action &= ~IMA_HASH;
  691. if (ima_fail_unverifiable_sigs)
  692. action |= IMA_FAIL_UNVERIFIABLE_SIGS;
  693. if (allowed_algos &&
  694. entry->flags & IMA_VALIDATE_ALGOS)
  695. *allowed_algos = entry->allowed_algos;
  696. }
  697. if (entry->action & IMA_DO_MASK)
  698. actmask &= ~(entry->action | entry->action << 1);
  699. else
  700. actmask &= ~(entry->action | entry->action >> 1);
  701. if ((pcr) && (entry->flags & IMA_PCR))
  702. *pcr = entry->pcr;
  703. if (template_desc && entry->template)
  704. *template_desc = entry->template;
  705. if (!actmask)
  706. break;
  707. }
  708. rcu_read_unlock();
  709. return action;
  710. }
  711. /**
  712. * ima_update_policy_flags() - Update global IMA variables
  713. *
  714. * Update ima_policy_flag and ima_setxattr_allowed_hash_algorithms
  715. * based on the currently loaded policy.
  716. *
  717. * With ima_policy_flag, the decision to short circuit out of a function
  718. * or not call the function in the first place can be made earlier.
  719. *
  720. * With ima_setxattr_allowed_hash_algorithms, the policy can restrict the
  721. * set of hash algorithms accepted when updating the security.ima xattr of
  722. * a file.
  723. *
  724. * Context: called after a policy update and at system initialization.
  725. */
  726. void ima_update_policy_flags(void)
  727. {
  728. struct ima_rule_entry *entry;
  729. int new_policy_flag = 0;
  730. struct list_head *ima_rules_tmp;
  731. rcu_read_lock();
  732. ima_rules_tmp = rcu_dereference(ima_rules);
  733. list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
  734. /*
  735. * SETXATTR_CHECK rules do not implement a full policy check
  736. * because rule checking would probably have an important
  737. * performance impact on setxattr(). As a consequence, only one
  738. * SETXATTR_CHECK can be active at a given time.
  739. * Because we want to preserve that property, we set out to use
  740. * atomic_cmpxchg. Either:
  741. * - the atomic was non-zero: a setxattr hash policy is
  742. * already enforced, we do nothing
  743. * - the atomic was zero: no setxattr policy was set, enable
  744. * the setxattr hash policy
  745. */
  746. if (entry->func == SETXATTR_CHECK) {
  747. atomic_cmpxchg(&ima_setxattr_allowed_hash_algorithms,
  748. 0, entry->allowed_algos);
  749. /* SETXATTR_CHECK doesn't impact ima_policy_flag */
  750. continue;
  751. }
  752. if (entry->action & IMA_DO_MASK)
  753. new_policy_flag |= entry->action;
  754. }
  755. rcu_read_unlock();
  756. ima_appraise |= (build_ima_appraise | temp_ima_appraise);
  757. if (!ima_appraise)
  758. new_policy_flag &= ~IMA_APPRAISE;
  759. ima_policy_flag = new_policy_flag;
  760. }
  761. static int ima_appraise_flag(enum ima_hooks func)
  762. {
  763. if (func == MODULE_CHECK)
  764. return IMA_APPRAISE_MODULES;
  765. else if (func == FIRMWARE_CHECK)
  766. return IMA_APPRAISE_FIRMWARE;
  767. else if (func == POLICY_CHECK)
  768. return IMA_APPRAISE_POLICY;
  769. else if (func == KEXEC_KERNEL_CHECK)
  770. return IMA_APPRAISE_KEXEC;
  771. return 0;
  772. }
  773. static void add_rules(struct ima_rule_entry *entries, int count,
  774. enum policy_rule_list policy_rule)
  775. {
  776. int i = 0;
  777. for (i = 0; i < count; i++) {
  778. struct ima_rule_entry *entry;
  779. if (policy_rule & IMA_DEFAULT_POLICY)
  780. list_add_tail(&entries[i].list, &ima_default_rules);
  781. if (policy_rule & IMA_CUSTOM_POLICY) {
  782. entry = kmemdup(&entries[i], sizeof(*entry),
  783. GFP_KERNEL);
  784. if (!entry)
  785. continue;
  786. list_add_tail(&entry->list, &ima_policy_rules);
  787. }
  788. if (entries[i].action == APPRAISE) {
  789. if (entries != build_appraise_rules)
  790. temp_ima_appraise |=
  791. ima_appraise_flag(entries[i].func);
  792. else
  793. build_ima_appraise |=
  794. ima_appraise_flag(entries[i].func);
  795. }
  796. }
  797. }
  798. static int ima_parse_rule(char *rule, struct ima_rule_entry *entry);
  799. static int __init ima_init_arch_policy(void)
  800. {
  801. const char * const *arch_rules;
  802. const char * const *rules;
  803. int arch_entries = 0;
  804. int i = 0;
  805. arch_rules = arch_get_ima_policy();
  806. if (!arch_rules)
  807. return arch_entries;
  808. /* Get number of rules */
  809. for (rules = arch_rules; *rules != NULL; rules++)
  810. arch_entries++;
  811. arch_policy_entry = kzalloc_objs(*arch_policy_entry, arch_entries + 1);
  812. if (!arch_policy_entry)
  813. return 0;
  814. /* Convert each policy string rules to struct ima_rule_entry format */
  815. for (rules = arch_rules, i = 0; *rules != NULL; rules++) {
  816. char rule[255];
  817. int result;
  818. result = strscpy(rule, *rules, sizeof(rule));
  819. INIT_LIST_HEAD(&arch_policy_entry[i].list);
  820. result = ima_parse_rule(rule, &arch_policy_entry[i]);
  821. if (result) {
  822. pr_warn("Skipping unknown architecture policy rule: %s\n",
  823. rule);
  824. memset(&arch_policy_entry[i], 0,
  825. sizeof(*arch_policy_entry));
  826. continue;
  827. }
  828. i++;
  829. }
  830. return i;
  831. }
  832. /**
  833. * ima_init_policy - initialize the default measure rules.
  834. *
  835. * ima_rules points to either the ima_default_rules or the new ima_policy_rules.
  836. */
  837. void __init ima_init_policy(void)
  838. {
  839. int build_appraise_entries, arch_entries;
  840. /* if !ima_policy, we load NO default rules */
  841. if (ima_policy)
  842. add_rules(dont_measure_rules, ARRAY_SIZE(dont_measure_rules),
  843. IMA_DEFAULT_POLICY);
  844. switch (ima_policy) {
  845. case ORIGINAL_TCB:
  846. add_rules(original_measurement_rules,
  847. ARRAY_SIZE(original_measurement_rules),
  848. IMA_DEFAULT_POLICY);
  849. break;
  850. case DEFAULT_TCB:
  851. add_rules(default_measurement_rules,
  852. ARRAY_SIZE(default_measurement_rules),
  853. IMA_DEFAULT_POLICY);
  854. break;
  855. default:
  856. break;
  857. }
  858. /*
  859. * Based on runtime secure boot flags, insert arch specific measurement
  860. * and appraise rules requiring file signatures for both the initial
  861. * and custom policies, prior to other appraise rules.
  862. * (Highest priority)
  863. */
  864. arch_entries = ima_init_arch_policy();
  865. if (!arch_entries)
  866. pr_info("No architecture policies found\n");
  867. else
  868. add_rules(arch_policy_entry, arch_entries,
  869. IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
  870. /*
  871. * Insert the builtin "secure_boot" policy rules requiring file
  872. * signatures, prior to other appraise rules.
  873. */
  874. if (ima_use_secure_boot)
  875. add_rules(secure_boot_rules, ARRAY_SIZE(secure_boot_rules),
  876. IMA_DEFAULT_POLICY);
  877. /*
  878. * Insert the build time appraise rules requiring file signatures
  879. * for both the initial and custom policies, prior to other appraise
  880. * rules. As the secure boot rules includes all of the build time
  881. * rules, include either one or the other set of rules, but not both.
  882. */
  883. build_appraise_entries = ARRAY_SIZE(build_appraise_rules);
  884. if (build_appraise_entries) {
  885. if (ima_use_secure_boot)
  886. add_rules(build_appraise_rules, build_appraise_entries,
  887. IMA_CUSTOM_POLICY);
  888. else
  889. add_rules(build_appraise_rules, build_appraise_entries,
  890. IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
  891. }
  892. if (ima_use_appraise_tcb)
  893. add_rules(default_appraise_rules,
  894. ARRAY_SIZE(default_appraise_rules),
  895. IMA_DEFAULT_POLICY);
  896. if (ima_use_critical_data)
  897. add_rules(critical_data_rules,
  898. ARRAY_SIZE(critical_data_rules),
  899. IMA_DEFAULT_POLICY);
  900. atomic_set(&ima_setxattr_allowed_hash_algorithms, 0);
  901. ima_update_policy_flags();
  902. }
  903. /* Make sure we have a valid policy, at least containing some rules. */
  904. int ima_check_policy(void)
  905. {
  906. if (list_empty(&ima_temp_rules))
  907. return -EINVAL;
  908. return 0;
  909. }
  910. /**
  911. * ima_update_policy - update default_rules with new measure rules
  912. *
  913. * Called on file .release to update the default rules with a complete new
  914. * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
  915. * they make a queue. The policy may be updated multiple times and this is the
  916. * RCU updater.
  917. *
  918. * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
  919. * we switch from the default policy to user defined.
  920. */
  921. void ima_update_policy(void)
  922. {
  923. struct list_head *policy = &ima_policy_rules;
  924. list_splice_tail_init_rcu(&ima_temp_rules, policy, synchronize_rcu);
  925. if (ima_rules != (struct list_head __rcu *)policy) {
  926. ima_policy_flag = 0;
  927. rcu_assign_pointer(ima_rules, policy);
  928. /*
  929. * IMA architecture specific policy rules are specified
  930. * as strings and converted to an array of ima_entry_rules
  931. * on boot. After loading a custom policy, free the
  932. * architecture specific rules stored as an array.
  933. */
  934. kfree(arch_policy_entry);
  935. }
  936. ima_update_policy_flags();
  937. /* Custom IMA policy has been loaded */
  938. ima_process_queued_keys();
  939. }
  940. /* Keep the enumeration in sync with the policy_tokens! */
  941. enum policy_opt {
  942. Opt_measure, Opt_dont_measure,
  943. Opt_appraise, Opt_dont_appraise,
  944. Opt_audit, Opt_dont_audit, Opt_hash, Opt_dont_hash,
  945. Opt_obj_user, Opt_obj_role, Opt_obj_type,
  946. Opt_subj_user, Opt_subj_role, Opt_subj_type,
  947. Opt_func, Opt_mask, Opt_fsmagic, Opt_fsname, Opt_fs_subtype, Opt_fsuuid,
  948. Opt_uid_eq, Opt_euid_eq, Opt_gid_eq, Opt_egid_eq,
  949. Opt_fowner_eq, Opt_fgroup_eq,
  950. Opt_uid_gt, Opt_euid_gt, Opt_gid_gt, Opt_egid_gt,
  951. Opt_fowner_gt, Opt_fgroup_gt,
  952. Opt_uid_lt, Opt_euid_lt, Opt_gid_lt, Opt_egid_lt,
  953. Opt_fowner_lt, Opt_fgroup_lt,
  954. Opt_digest_type,
  955. Opt_appraise_type, Opt_appraise_flag, Opt_appraise_algos,
  956. Opt_permit_directio, Opt_pcr, Opt_template, Opt_keyrings,
  957. Opt_label, Opt_err
  958. };
  959. static const match_table_t policy_tokens = {
  960. {Opt_measure, "measure"},
  961. {Opt_dont_measure, "dont_measure"},
  962. {Opt_appraise, "appraise"},
  963. {Opt_dont_appraise, "dont_appraise"},
  964. {Opt_audit, "audit"},
  965. {Opt_dont_audit, "dont_audit"},
  966. {Opt_hash, "hash"},
  967. {Opt_dont_hash, "dont_hash"},
  968. {Opt_obj_user, "obj_user=%s"},
  969. {Opt_obj_role, "obj_role=%s"},
  970. {Opt_obj_type, "obj_type=%s"},
  971. {Opt_subj_user, "subj_user=%s"},
  972. {Opt_subj_role, "subj_role=%s"},
  973. {Opt_subj_type, "subj_type=%s"},
  974. {Opt_func, "func=%s"},
  975. {Opt_mask, "mask=%s"},
  976. {Opt_fsmagic, "fsmagic=%s"},
  977. {Opt_fsname, "fsname=%s"},
  978. {Opt_fs_subtype, "fs_subtype=%s"},
  979. {Opt_fsuuid, "fsuuid=%s"},
  980. {Opt_uid_eq, "uid=%s"},
  981. {Opt_euid_eq, "euid=%s"},
  982. {Opt_gid_eq, "gid=%s"},
  983. {Opt_egid_eq, "egid=%s"},
  984. {Opt_fowner_eq, "fowner=%s"},
  985. {Opt_fgroup_eq, "fgroup=%s"},
  986. {Opt_uid_gt, "uid>%s"},
  987. {Opt_euid_gt, "euid>%s"},
  988. {Opt_gid_gt, "gid>%s"},
  989. {Opt_egid_gt, "egid>%s"},
  990. {Opt_fowner_gt, "fowner>%s"},
  991. {Opt_fgroup_gt, "fgroup>%s"},
  992. {Opt_uid_lt, "uid<%s"},
  993. {Opt_euid_lt, "euid<%s"},
  994. {Opt_gid_lt, "gid<%s"},
  995. {Opt_egid_lt, "egid<%s"},
  996. {Opt_fowner_lt, "fowner<%s"},
  997. {Opt_fgroup_lt, "fgroup<%s"},
  998. {Opt_digest_type, "digest_type=%s"},
  999. {Opt_appraise_type, "appraise_type=%s"},
  1000. {Opt_appraise_flag, "appraise_flag=%s"},
  1001. {Opt_appraise_algos, "appraise_algos=%s"},
  1002. {Opt_permit_directio, "permit_directio"},
  1003. {Opt_pcr, "pcr=%s"},
  1004. {Opt_template, "template=%s"},
  1005. {Opt_keyrings, "keyrings=%s"},
  1006. {Opt_label, "label=%s"},
  1007. {Opt_err, NULL}
  1008. };
  1009. static int ima_lsm_rule_init(struct ima_rule_entry *entry,
  1010. substring_t *args, int lsm_rule, int audit_type)
  1011. {
  1012. int result;
  1013. if (entry->lsm[lsm_rule].rule)
  1014. return -EINVAL;
  1015. entry->lsm[lsm_rule].args_p = match_strdup(args);
  1016. if (!entry->lsm[lsm_rule].args_p)
  1017. return -ENOMEM;
  1018. entry->lsm[lsm_rule].type = audit_type;
  1019. result = ima_filter_rule_init(entry->lsm[lsm_rule].type, Audit_equal,
  1020. entry->lsm[lsm_rule].args_p,
  1021. &entry->lsm[lsm_rule].rule,
  1022. GFP_KERNEL);
  1023. if (!entry->lsm[lsm_rule].rule) {
  1024. pr_warn("rule for LSM \'%s\' is undefined\n",
  1025. entry->lsm[lsm_rule].args_p);
  1026. if (ima_rules == (struct list_head __rcu *)(&ima_default_rules)) {
  1027. kfree(entry->lsm[lsm_rule].args_p);
  1028. entry->lsm[lsm_rule].args_p = NULL;
  1029. result = -EINVAL;
  1030. } else
  1031. result = 0;
  1032. }
  1033. return result;
  1034. }
  1035. static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value,
  1036. enum policy_opt rule_operator)
  1037. {
  1038. if (!ab)
  1039. return;
  1040. switch (rule_operator) {
  1041. case Opt_uid_gt:
  1042. case Opt_euid_gt:
  1043. case Opt_gid_gt:
  1044. case Opt_egid_gt:
  1045. case Opt_fowner_gt:
  1046. case Opt_fgroup_gt:
  1047. audit_log_format(ab, "%s>", key);
  1048. break;
  1049. case Opt_uid_lt:
  1050. case Opt_euid_lt:
  1051. case Opt_gid_lt:
  1052. case Opt_egid_lt:
  1053. case Opt_fowner_lt:
  1054. case Opt_fgroup_lt:
  1055. audit_log_format(ab, "%s<", key);
  1056. break;
  1057. default:
  1058. audit_log_format(ab, "%s=", key);
  1059. }
  1060. audit_log_format(ab, "%s ", value);
  1061. }
  1062. static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
  1063. {
  1064. ima_log_string_op(ab, key, value, Opt_err);
  1065. }
  1066. /*
  1067. * Validating the appended signature included in the measurement list requires
  1068. * the file hash calculated without the appended signature (i.e., the 'd-modsig'
  1069. * field). Therefore, notify the user if they have the 'modsig' field but not
  1070. * the 'd-modsig' field in the template.
  1071. */
  1072. static void check_template_modsig(const struct ima_template_desc *template)
  1073. {
  1074. #define MSG "template with 'modsig' field also needs 'd-modsig' field\n"
  1075. bool has_modsig, has_dmodsig;
  1076. static bool checked;
  1077. int i;
  1078. /* We only need to notify the user once. */
  1079. if (checked)
  1080. return;
  1081. has_modsig = has_dmodsig = false;
  1082. for (i = 0; i < template->num_fields; i++) {
  1083. if (!strcmp(template->fields[i]->field_id, "modsig"))
  1084. has_modsig = true;
  1085. else if (!strcmp(template->fields[i]->field_id, "d-modsig"))
  1086. has_dmodsig = true;
  1087. }
  1088. if (has_modsig && !has_dmodsig)
  1089. pr_notice(MSG);
  1090. checked = true;
  1091. #undef MSG
  1092. }
  1093. /*
  1094. * Warn if the template does not contain the given field.
  1095. */
  1096. static void check_template_field(const struct ima_template_desc *template,
  1097. const char *field, const char *msg)
  1098. {
  1099. int i;
  1100. for (i = 0; i < template->num_fields; i++)
  1101. if (!strcmp(template->fields[i]->field_id, field))
  1102. return;
  1103. pr_notice_once("%s", msg);
  1104. }
  1105. static bool ima_validate_rule(struct ima_rule_entry *entry)
  1106. {
  1107. /* Ensure that the action is set and is compatible with the flags */
  1108. if (entry->action == UNKNOWN)
  1109. return false;
  1110. if (entry->action != MEASURE && entry->flags & IMA_PCR)
  1111. return false;
  1112. if (entry->action != APPRAISE &&
  1113. entry->flags & (IMA_DIGSIG_REQUIRED | IMA_MODSIG_ALLOWED |
  1114. IMA_CHECK_BLACKLIST | IMA_VALIDATE_ALGOS))
  1115. return false;
  1116. /*
  1117. * The IMA_FUNC bit must be set if and only if there's a valid hook
  1118. * function specified, and vice versa. Enforcing this property allows
  1119. * for the NONE case below to validate a rule without an explicit hook
  1120. * function.
  1121. */
  1122. if (((entry->flags & IMA_FUNC) && entry->func == NONE) ||
  1123. (!(entry->flags & IMA_FUNC) && entry->func != NONE))
  1124. return false;
  1125. /*
  1126. * Ensure that the hook function is compatible with the other
  1127. * components of the rule
  1128. */
  1129. switch (entry->func) {
  1130. case NONE:
  1131. case FILE_CHECK:
  1132. case MMAP_CHECK:
  1133. case MMAP_CHECK_REQPROT:
  1134. case BPRM_CHECK:
  1135. case CREDS_CHECK:
  1136. case POST_SETATTR:
  1137. case FIRMWARE_CHECK:
  1138. case POLICY_CHECK:
  1139. if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
  1140. IMA_UID | IMA_FOWNER | IMA_FSUUID |
  1141. IMA_INMASK | IMA_EUID | IMA_PCR |
  1142. IMA_FSNAME | IMA_FS_SUBTYPE |
  1143. IMA_GID | IMA_EGID |
  1144. IMA_FGROUP | IMA_DIGSIG_REQUIRED |
  1145. IMA_PERMIT_DIRECTIO | IMA_VALIDATE_ALGOS |
  1146. IMA_CHECK_BLACKLIST | IMA_VERITY_REQUIRED))
  1147. return false;
  1148. break;
  1149. case MODULE_CHECK:
  1150. case KEXEC_KERNEL_CHECK:
  1151. case KEXEC_INITRAMFS_CHECK:
  1152. if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
  1153. IMA_UID | IMA_FOWNER | IMA_FSUUID |
  1154. IMA_INMASK | IMA_EUID | IMA_PCR |
  1155. IMA_FSNAME | IMA_FS_SUBTYPE |
  1156. IMA_GID | IMA_EGID |
  1157. IMA_FGROUP | IMA_DIGSIG_REQUIRED |
  1158. IMA_PERMIT_DIRECTIO | IMA_MODSIG_ALLOWED |
  1159. IMA_CHECK_BLACKLIST | IMA_VALIDATE_ALGOS))
  1160. return false;
  1161. break;
  1162. case KEXEC_CMDLINE:
  1163. if (entry->action & ~(MEASURE | DONT_MEASURE))
  1164. return false;
  1165. if (entry->flags & ~(IMA_FUNC | IMA_FSMAGIC | IMA_UID |
  1166. IMA_FOWNER | IMA_FSUUID | IMA_EUID |
  1167. IMA_PCR | IMA_FSNAME | IMA_FS_SUBTYPE |
  1168. IMA_GID | IMA_EGID |
  1169. IMA_FGROUP))
  1170. return false;
  1171. break;
  1172. case KEY_CHECK:
  1173. if (entry->action & ~(MEASURE | DONT_MEASURE))
  1174. return false;
  1175. if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_GID | IMA_PCR |
  1176. IMA_KEYRINGS))
  1177. return false;
  1178. if (ima_rule_contains_lsm_cond(entry))
  1179. return false;
  1180. break;
  1181. case CRITICAL_DATA:
  1182. if (entry->action & ~(MEASURE | DONT_MEASURE))
  1183. return false;
  1184. if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_GID | IMA_PCR |
  1185. IMA_LABEL))
  1186. return false;
  1187. if (ima_rule_contains_lsm_cond(entry))
  1188. return false;
  1189. break;
  1190. case SETXATTR_CHECK:
  1191. /* any action other than APPRAISE is unsupported */
  1192. if (entry->action != APPRAISE)
  1193. return false;
  1194. /* SETXATTR_CHECK requires an appraise_algos parameter */
  1195. if (!(entry->flags & IMA_VALIDATE_ALGOS))
  1196. return false;
  1197. /*
  1198. * full policies are not supported, they would have too
  1199. * much of a performance impact
  1200. */
  1201. if (entry->flags & ~(IMA_FUNC | IMA_VALIDATE_ALGOS))
  1202. return false;
  1203. break;
  1204. default:
  1205. return false;
  1206. }
  1207. /* Ensure that combinations of flags are compatible with each other */
  1208. if (entry->flags & IMA_CHECK_BLACKLIST &&
  1209. !(entry->flags & IMA_DIGSIG_REQUIRED))
  1210. return false;
  1211. /*
  1212. * Unlike for regular IMA 'appraise' policy rules where security.ima
  1213. * xattr may contain either a file hash or signature, the security.ima
  1214. * xattr for fsverity must contain a file signature (sigv3). Ensure
  1215. * that 'appraise' rules for fsverity require file signatures by
  1216. * checking the IMA_DIGSIG_REQUIRED flag is set.
  1217. */
  1218. if (entry->action == APPRAISE &&
  1219. (entry->flags & IMA_VERITY_REQUIRED) &&
  1220. !(entry->flags & IMA_DIGSIG_REQUIRED))
  1221. return false;
  1222. return true;
  1223. }
  1224. static unsigned int ima_parse_appraise_algos(char *arg)
  1225. {
  1226. unsigned int res = 0;
  1227. int idx;
  1228. char *token;
  1229. while ((token = strsep(&arg, ",")) != NULL) {
  1230. idx = match_string(hash_algo_name, HASH_ALGO__LAST, token);
  1231. if (idx < 0) {
  1232. pr_err("unknown hash algorithm \"%s\"",
  1233. token);
  1234. return 0;
  1235. }
  1236. if (!crypto_has_alg(hash_algo_name[idx], 0, 0)) {
  1237. pr_err("unavailable hash algorithm \"%s\", check your kernel configuration",
  1238. token);
  1239. return 0;
  1240. }
  1241. /* Add the hash algorithm to the 'allowed' bitfield */
  1242. res |= (1U << idx);
  1243. }
  1244. return res;
  1245. }
  1246. static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
  1247. {
  1248. struct audit_buffer *ab;
  1249. char *from;
  1250. char *p;
  1251. bool eid_token; /* either euid or egid */
  1252. struct ima_template_desc *template_desc;
  1253. int result = 0;
  1254. ab = integrity_audit_log_start(audit_context(), GFP_KERNEL,
  1255. AUDIT_INTEGRITY_POLICY_RULE);
  1256. entry->uid = INVALID_UID;
  1257. entry->gid = INVALID_GID;
  1258. entry->fowner = INVALID_UID;
  1259. entry->fgroup = INVALID_GID;
  1260. entry->uid_op = &uid_eq;
  1261. entry->gid_op = &gid_eq;
  1262. entry->fowner_op = &vfsuid_eq_kuid;
  1263. entry->fgroup_op = &vfsgid_eq_kgid;
  1264. entry->action = UNKNOWN;
  1265. while ((p = strsep(&rule, " \t")) != NULL) {
  1266. substring_t args[MAX_OPT_ARGS];
  1267. int token;
  1268. unsigned long lnum;
  1269. if (result < 0 || *p == '#') /* ignore suffixed comment */
  1270. break;
  1271. if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
  1272. continue;
  1273. token = match_token(p, policy_tokens, args);
  1274. switch (token) {
  1275. case Opt_measure:
  1276. ima_log_string(ab, "action", "measure");
  1277. if (entry->action != UNKNOWN)
  1278. result = -EINVAL;
  1279. entry->action = MEASURE;
  1280. break;
  1281. case Opt_dont_measure:
  1282. ima_log_string(ab, "action", "dont_measure");
  1283. if (entry->action != UNKNOWN)
  1284. result = -EINVAL;
  1285. entry->action = DONT_MEASURE;
  1286. break;
  1287. case Opt_appraise:
  1288. ima_log_string(ab, "action", "appraise");
  1289. if (entry->action != UNKNOWN)
  1290. result = -EINVAL;
  1291. entry->action = APPRAISE;
  1292. break;
  1293. case Opt_dont_appraise:
  1294. ima_log_string(ab, "action", "dont_appraise");
  1295. if (entry->action != UNKNOWN)
  1296. result = -EINVAL;
  1297. entry->action = DONT_APPRAISE;
  1298. break;
  1299. case Opt_audit:
  1300. ima_log_string(ab, "action", "audit");
  1301. if (entry->action != UNKNOWN)
  1302. result = -EINVAL;
  1303. entry->action = AUDIT;
  1304. break;
  1305. case Opt_dont_audit:
  1306. ima_log_string(ab, "action", "dont_audit");
  1307. if (entry->action != UNKNOWN)
  1308. result = -EINVAL;
  1309. entry->action = DONT_AUDIT;
  1310. break;
  1311. case Opt_hash:
  1312. ima_log_string(ab, "action", "hash");
  1313. if (entry->action != UNKNOWN)
  1314. result = -EINVAL;
  1315. entry->action = HASH;
  1316. break;
  1317. case Opt_dont_hash:
  1318. ima_log_string(ab, "action", "dont_hash");
  1319. if (entry->action != UNKNOWN)
  1320. result = -EINVAL;
  1321. entry->action = DONT_HASH;
  1322. break;
  1323. case Opt_func:
  1324. ima_log_string(ab, "func", args[0].from);
  1325. if (entry->func)
  1326. result = -EINVAL;
  1327. if (strcmp(args[0].from, "FILE_CHECK") == 0)
  1328. entry->func = FILE_CHECK;
  1329. /* PATH_CHECK is for backwards compat */
  1330. else if (strcmp(args[0].from, "PATH_CHECK") == 0)
  1331. entry->func = FILE_CHECK;
  1332. else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
  1333. entry->func = MODULE_CHECK;
  1334. else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
  1335. entry->func = FIRMWARE_CHECK;
  1336. else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
  1337. || (strcmp(args[0].from, "MMAP_CHECK") == 0))
  1338. entry->func = MMAP_CHECK;
  1339. else if ((strcmp(args[0].from, "MMAP_CHECK_REQPROT") == 0))
  1340. entry->func = MMAP_CHECK_REQPROT;
  1341. else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
  1342. entry->func = BPRM_CHECK;
  1343. else if (strcmp(args[0].from, "CREDS_CHECK") == 0)
  1344. entry->func = CREDS_CHECK;
  1345. else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
  1346. 0)
  1347. entry->func = KEXEC_KERNEL_CHECK;
  1348. else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
  1349. == 0)
  1350. entry->func = KEXEC_INITRAMFS_CHECK;
  1351. else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
  1352. entry->func = POLICY_CHECK;
  1353. else if (strcmp(args[0].from, "KEXEC_CMDLINE") == 0)
  1354. entry->func = KEXEC_CMDLINE;
  1355. else if (IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) &&
  1356. strcmp(args[0].from, "KEY_CHECK") == 0)
  1357. entry->func = KEY_CHECK;
  1358. else if (strcmp(args[0].from, "CRITICAL_DATA") == 0)
  1359. entry->func = CRITICAL_DATA;
  1360. else if (strcmp(args[0].from, "SETXATTR_CHECK") == 0)
  1361. entry->func = SETXATTR_CHECK;
  1362. else
  1363. result = -EINVAL;
  1364. if (!result)
  1365. entry->flags |= IMA_FUNC;
  1366. break;
  1367. case Opt_mask:
  1368. ima_log_string(ab, "mask", args[0].from);
  1369. if (entry->mask)
  1370. result = -EINVAL;
  1371. from = args[0].from;
  1372. if (*from == '^')
  1373. from++;
  1374. if ((strcmp(from, "MAY_EXEC")) == 0)
  1375. entry->mask = MAY_EXEC;
  1376. else if (strcmp(from, "MAY_WRITE") == 0)
  1377. entry->mask = MAY_WRITE;
  1378. else if (strcmp(from, "MAY_READ") == 0)
  1379. entry->mask = MAY_READ;
  1380. else if (strcmp(from, "MAY_APPEND") == 0)
  1381. entry->mask = MAY_APPEND;
  1382. else
  1383. result = -EINVAL;
  1384. if (!result)
  1385. entry->flags |= (*args[0].from == '^')
  1386. ? IMA_INMASK : IMA_MASK;
  1387. break;
  1388. case Opt_fsmagic:
  1389. ima_log_string(ab, "fsmagic", args[0].from);
  1390. if (entry->fsmagic) {
  1391. result = -EINVAL;
  1392. break;
  1393. }
  1394. result = kstrtoul(args[0].from, 16, &entry->fsmagic);
  1395. if (!result)
  1396. entry->flags |= IMA_FSMAGIC;
  1397. break;
  1398. case Opt_fsname:
  1399. ima_log_string(ab, "fsname", args[0].from);
  1400. entry->fsname = kstrdup(args[0].from, GFP_KERNEL);
  1401. if (!entry->fsname) {
  1402. result = -ENOMEM;
  1403. break;
  1404. }
  1405. result = 0;
  1406. entry->flags |= IMA_FSNAME;
  1407. break;
  1408. case Opt_fs_subtype:
  1409. ima_log_string(ab, "fs_subtype", args[0].from);
  1410. if (entry->fs_subtype) {
  1411. result = -EINVAL;
  1412. break;
  1413. }
  1414. entry->fs_subtype = kstrdup(args[0].from, GFP_KERNEL);
  1415. if (!entry->fs_subtype) {
  1416. result = -ENOMEM;
  1417. break;
  1418. }
  1419. result = 0;
  1420. entry->flags |= IMA_FS_SUBTYPE;
  1421. break;
  1422. case Opt_keyrings:
  1423. ima_log_string(ab, "keyrings", args[0].from);
  1424. if (!IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) ||
  1425. entry->keyrings) {
  1426. result = -EINVAL;
  1427. break;
  1428. }
  1429. entry->keyrings = ima_alloc_rule_opt_list(args);
  1430. if (IS_ERR(entry->keyrings)) {
  1431. result = PTR_ERR(entry->keyrings);
  1432. entry->keyrings = NULL;
  1433. break;
  1434. }
  1435. entry->flags |= IMA_KEYRINGS;
  1436. break;
  1437. case Opt_label:
  1438. ima_log_string(ab, "label", args[0].from);
  1439. if (entry->label) {
  1440. result = -EINVAL;
  1441. break;
  1442. }
  1443. entry->label = ima_alloc_rule_opt_list(args);
  1444. if (IS_ERR(entry->label)) {
  1445. result = PTR_ERR(entry->label);
  1446. entry->label = NULL;
  1447. break;
  1448. }
  1449. entry->flags |= IMA_LABEL;
  1450. break;
  1451. case Opt_fsuuid:
  1452. ima_log_string(ab, "fsuuid", args[0].from);
  1453. if (!uuid_is_null(&entry->fsuuid)) {
  1454. result = -EINVAL;
  1455. break;
  1456. }
  1457. result = uuid_parse(args[0].from, &entry->fsuuid);
  1458. if (!result)
  1459. entry->flags |= IMA_FSUUID;
  1460. break;
  1461. case Opt_uid_gt:
  1462. case Opt_euid_gt:
  1463. entry->uid_op = &uid_gt;
  1464. fallthrough;
  1465. case Opt_uid_lt:
  1466. case Opt_euid_lt:
  1467. if ((token == Opt_uid_lt) || (token == Opt_euid_lt))
  1468. entry->uid_op = &uid_lt;
  1469. fallthrough;
  1470. case Opt_uid_eq:
  1471. case Opt_euid_eq:
  1472. eid_token = (token == Opt_euid_eq) ||
  1473. (token == Opt_euid_gt) ||
  1474. (token == Opt_euid_lt);
  1475. ima_log_string_op(ab, eid_token ? "euid" : "uid",
  1476. args[0].from, token);
  1477. if (uid_valid(entry->uid)) {
  1478. result = -EINVAL;
  1479. break;
  1480. }
  1481. result = kstrtoul(args[0].from, 10, &lnum);
  1482. if (!result) {
  1483. entry->uid = make_kuid(current_user_ns(),
  1484. (uid_t) lnum);
  1485. if (!uid_valid(entry->uid) ||
  1486. (uid_t)lnum != lnum)
  1487. result = -EINVAL;
  1488. else
  1489. entry->flags |= eid_token
  1490. ? IMA_EUID : IMA_UID;
  1491. }
  1492. break;
  1493. case Opt_gid_gt:
  1494. case Opt_egid_gt:
  1495. entry->gid_op = &gid_gt;
  1496. fallthrough;
  1497. case Opt_gid_lt:
  1498. case Opt_egid_lt:
  1499. if ((token == Opt_gid_lt) || (token == Opt_egid_lt))
  1500. entry->gid_op = &gid_lt;
  1501. fallthrough;
  1502. case Opt_gid_eq:
  1503. case Opt_egid_eq:
  1504. eid_token = (token == Opt_egid_eq) ||
  1505. (token == Opt_egid_gt) ||
  1506. (token == Opt_egid_lt);
  1507. ima_log_string_op(ab, eid_token ? "egid" : "gid",
  1508. args[0].from, token);
  1509. if (gid_valid(entry->gid)) {
  1510. result = -EINVAL;
  1511. break;
  1512. }
  1513. result = kstrtoul(args[0].from, 10, &lnum);
  1514. if (!result) {
  1515. entry->gid = make_kgid(current_user_ns(),
  1516. (gid_t)lnum);
  1517. if (!gid_valid(entry->gid) ||
  1518. (((gid_t)lnum) != lnum))
  1519. result = -EINVAL;
  1520. else
  1521. entry->flags |= eid_token
  1522. ? IMA_EGID : IMA_GID;
  1523. }
  1524. break;
  1525. case Opt_fowner_gt:
  1526. entry->fowner_op = &vfsuid_gt_kuid;
  1527. fallthrough;
  1528. case Opt_fowner_lt:
  1529. if (token == Opt_fowner_lt)
  1530. entry->fowner_op = &vfsuid_lt_kuid;
  1531. fallthrough;
  1532. case Opt_fowner_eq:
  1533. ima_log_string_op(ab, "fowner", args[0].from, token);
  1534. if (uid_valid(entry->fowner)) {
  1535. result = -EINVAL;
  1536. break;
  1537. }
  1538. result = kstrtoul(args[0].from, 10, &lnum);
  1539. if (!result) {
  1540. entry->fowner = make_kuid(current_user_ns(),
  1541. (uid_t)lnum);
  1542. if (!uid_valid(entry->fowner) ||
  1543. (((uid_t)lnum) != lnum))
  1544. result = -EINVAL;
  1545. else
  1546. entry->flags |= IMA_FOWNER;
  1547. }
  1548. break;
  1549. case Opt_fgroup_gt:
  1550. entry->fgroup_op = &vfsgid_gt_kgid;
  1551. fallthrough;
  1552. case Opt_fgroup_lt:
  1553. if (token == Opt_fgroup_lt)
  1554. entry->fgroup_op = &vfsgid_lt_kgid;
  1555. fallthrough;
  1556. case Opt_fgroup_eq:
  1557. ima_log_string_op(ab, "fgroup", args[0].from, token);
  1558. if (gid_valid(entry->fgroup)) {
  1559. result = -EINVAL;
  1560. break;
  1561. }
  1562. result = kstrtoul(args[0].from, 10, &lnum);
  1563. if (!result) {
  1564. entry->fgroup = make_kgid(current_user_ns(),
  1565. (gid_t)lnum);
  1566. if (!gid_valid(entry->fgroup) ||
  1567. (((gid_t)lnum) != lnum))
  1568. result = -EINVAL;
  1569. else
  1570. entry->flags |= IMA_FGROUP;
  1571. }
  1572. break;
  1573. case Opt_obj_user:
  1574. ima_log_string(ab, "obj_user", args[0].from);
  1575. result = ima_lsm_rule_init(entry, args,
  1576. LSM_OBJ_USER,
  1577. AUDIT_OBJ_USER);
  1578. break;
  1579. case Opt_obj_role:
  1580. ima_log_string(ab, "obj_role", args[0].from);
  1581. result = ima_lsm_rule_init(entry, args,
  1582. LSM_OBJ_ROLE,
  1583. AUDIT_OBJ_ROLE);
  1584. break;
  1585. case Opt_obj_type:
  1586. ima_log_string(ab, "obj_type", args[0].from);
  1587. result = ima_lsm_rule_init(entry, args,
  1588. LSM_OBJ_TYPE,
  1589. AUDIT_OBJ_TYPE);
  1590. break;
  1591. case Opt_subj_user:
  1592. ima_log_string(ab, "subj_user", args[0].from);
  1593. result = ima_lsm_rule_init(entry, args,
  1594. LSM_SUBJ_USER,
  1595. AUDIT_SUBJ_USER);
  1596. break;
  1597. case Opt_subj_role:
  1598. ima_log_string(ab, "subj_role", args[0].from);
  1599. result = ima_lsm_rule_init(entry, args,
  1600. LSM_SUBJ_ROLE,
  1601. AUDIT_SUBJ_ROLE);
  1602. break;
  1603. case Opt_subj_type:
  1604. ima_log_string(ab, "subj_type", args[0].from);
  1605. result = ima_lsm_rule_init(entry, args,
  1606. LSM_SUBJ_TYPE,
  1607. AUDIT_SUBJ_TYPE);
  1608. break;
  1609. case Opt_digest_type:
  1610. ima_log_string(ab, "digest_type", args[0].from);
  1611. if (entry->flags & IMA_DIGSIG_REQUIRED)
  1612. result = -EINVAL;
  1613. else if ((strcmp(args[0].from, "verity")) == 0)
  1614. entry->flags |= IMA_VERITY_REQUIRED;
  1615. else
  1616. result = -EINVAL;
  1617. break;
  1618. case Opt_appraise_type:
  1619. ima_log_string(ab, "appraise_type", args[0].from);
  1620. if ((strcmp(args[0].from, "imasig")) == 0) {
  1621. if (entry->flags & IMA_VERITY_REQUIRED)
  1622. result = -EINVAL;
  1623. else
  1624. entry->flags |= IMA_DIGSIG_REQUIRED | IMA_CHECK_BLACKLIST;
  1625. } else if (strcmp(args[0].from, "sigv3") == 0) {
  1626. /* Only fsverity supports sigv3 for now */
  1627. if (entry->flags & IMA_VERITY_REQUIRED)
  1628. entry->flags |= IMA_DIGSIG_REQUIRED | IMA_CHECK_BLACKLIST;
  1629. else
  1630. result = -EINVAL;
  1631. } else if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
  1632. strcmp(args[0].from, "imasig|modsig") == 0) {
  1633. if (entry->flags & IMA_VERITY_REQUIRED)
  1634. result = -EINVAL;
  1635. else
  1636. entry->flags |= IMA_DIGSIG_REQUIRED |
  1637. IMA_MODSIG_ALLOWED | IMA_CHECK_BLACKLIST;
  1638. } else {
  1639. result = -EINVAL;
  1640. }
  1641. break;
  1642. case Opt_appraise_flag:
  1643. ima_log_string(ab, "appraise_flag", args[0].from);
  1644. break;
  1645. case Opt_appraise_algos:
  1646. ima_log_string(ab, "appraise_algos", args[0].from);
  1647. if (entry->allowed_algos) {
  1648. result = -EINVAL;
  1649. break;
  1650. }
  1651. entry->allowed_algos =
  1652. ima_parse_appraise_algos(args[0].from);
  1653. /* invalid or empty list of algorithms */
  1654. if (!entry->allowed_algos) {
  1655. result = -EINVAL;
  1656. break;
  1657. }
  1658. entry->flags |= IMA_VALIDATE_ALGOS;
  1659. break;
  1660. case Opt_permit_directio:
  1661. entry->flags |= IMA_PERMIT_DIRECTIO;
  1662. break;
  1663. case Opt_pcr:
  1664. ima_log_string(ab, "pcr", args[0].from);
  1665. result = kstrtoint(args[0].from, 10, &entry->pcr);
  1666. if (result || INVALID_PCR(entry->pcr))
  1667. result = -EINVAL;
  1668. else
  1669. entry->flags |= IMA_PCR;
  1670. break;
  1671. case Opt_template:
  1672. ima_log_string(ab, "template", args[0].from);
  1673. if (entry->action != MEASURE) {
  1674. result = -EINVAL;
  1675. break;
  1676. }
  1677. template_desc = lookup_template_desc(args[0].from);
  1678. if (!template_desc || entry->template) {
  1679. result = -EINVAL;
  1680. break;
  1681. }
  1682. /*
  1683. * template_desc_init_fields() does nothing if
  1684. * the template is already initialised, so
  1685. * it's safe to do this unconditionally
  1686. */
  1687. template_desc_init_fields(template_desc->fmt,
  1688. &(template_desc->fields),
  1689. &(template_desc->num_fields));
  1690. entry->template = template_desc;
  1691. break;
  1692. case Opt_err:
  1693. ima_log_string(ab, "UNKNOWN", p);
  1694. result = -EINVAL;
  1695. break;
  1696. }
  1697. }
  1698. if (!result && !ima_validate_rule(entry))
  1699. result = -EINVAL;
  1700. else if (entry->action == APPRAISE)
  1701. temp_ima_appraise |= ima_appraise_flag(entry->func);
  1702. if (!result && entry->flags & IMA_MODSIG_ALLOWED) {
  1703. template_desc = entry->template ? entry->template :
  1704. ima_template_desc_current();
  1705. check_template_modsig(template_desc);
  1706. }
  1707. /* d-ngv2 template field recommended for unsigned fs-verity digests */
  1708. if (!result && entry->action == MEASURE &&
  1709. entry->flags & IMA_VERITY_REQUIRED) {
  1710. template_desc = entry->template ? entry->template :
  1711. ima_template_desc_current();
  1712. check_template_field(template_desc, "d-ngv2",
  1713. "verity rules should include d-ngv2");
  1714. }
  1715. audit_log_format(ab, "res=%d", !result);
  1716. audit_log_end(ab);
  1717. return result;
  1718. }
  1719. /**
  1720. * ima_parse_add_rule - add a rule to ima_policy_rules
  1721. * @rule: ima measurement policy rule
  1722. *
  1723. * Avoid locking by allowing just one writer at a time in ima_write_policy()
  1724. * Returns the length of the rule parsed, an error code on failure
  1725. */
  1726. ssize_t ima_parse_add_rule(char *rule)
  1727. {
  1728. static const char op[] = "update_policy";
  1729. char *p;
  1730. struct ima_rule_entry *entry;
  1731. ssize_t result, len;
  1732. int audit_info = 0;
  1733. p = strsep(&rule, "\n");
  1734. len = strlen(p) + 1;
  1735. p += strspn(p, " \t");
  1736. if (*p == '#' || *p == '\0')
  1737. return len;
  1738. entry = kzalloc_obj(*entry);
  1739. if (!entry) {
  1740. integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
  1741. NULL, op, "-ENOMEM", -ENOMEM, audit_info);
  1742. return -ENOMEM;
  1743. }
  1744. INIT_LIST_HEAD(&entry->list);
  1745. result = ima_parse_rule(p, entry);
  1746. if (result) {
  1747. ima_free_rule(entry);
  1748. integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
  1749. NULL, op, "invalid-policy", result,
  1750. audit_info);
  1751. return result;
  1752. }
  1753. list_add_tail(&entry->list, &ima_temp_rules);
  1754. return len;
  1755. }
  1756. /**
  1757. * ima_delete_rules() - called to cleanup invalid in-flight policy.
  1758. *
  1759. * We don't need locking as we operate on the temp list, which is
  1760. * different from the active one. There is also only one user of
  1761. * ima_delete_rules() at a time.
  1762. */
  1763. void ima_delete_rules(void)
  1764. {
  1765. struct ima_rule_entry *entry, *tmp;
  1766. temp_ima_appraise = 0;
  1767. list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
  1768. list_del(&entry->list);
  1769. ima_free_rule(entry);
  1770. }
  1771. }
  1772. #define __ima_hook_stringify(func, str) (#func),
  1773. const char *const func_tokens[] = {
  1774. __ima_hooks(__ima_hook_stringify)
  1775. };
  1776. #ifdef CONFIG_IMA_READ_POLICY
  1777. enum {
  1778. mask_exec = 0, mask_write, mask_read, mask_append
  1779. };
  1780. static const char *const mask_tokens[] = {
  1781. "^MAY_EXEC",
  1782. "^MAY_WRITE",
  1783. "^MAY_READ",
  1784. "^MAY_APPEND"
  1785. };
  1786. void *ima_policy_start(struct seq_file *m, loff_t *pos)
  1787. {
  1788. loff_t l = *pos;
  1789. struct ima_rule_entry *entry;
  1790. struct list_head *ima_rules_tmp;
  1791. rcu_read_lock();
  1792. ima_rules_tmp = rcu_dereference(ima_rules);
  1793. list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
  1794. if (!l--) {
  1795. rcu_read_unlock();
  1796. return entry;
  1797. }
  1798. }
  1799. rcu_read_unlock();
  1800. return NULL;
  1801. }
  1802. void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
  1803. {
  1804. struct ima_rule_entry *entry = v;
  1805. rcu_read_lock();
  1806. entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
  1807. rcu_read_unlock();
  1808. (*pos)++;
  1809. return (&entry->list == &ima_default_rules ||
  1810. &entry->list == &ima_policy_rules) ? NULL : entry;
  1811. }
  1812. void ima_policy_stop(struct seq_file *m, void *v)
  1813. {
  1814. }
  1815. #define pt(token) policy_tokens[token].pattern
  1816. #define mt(token) mask_tokens[token]
  1817. /*
  1818. * policy_func_show - display the ima_hooks policy rule
  1819. */
  1820. static void policy_func_show(struct seq_file *m, enum ima_hooks func)
  1821. {
  1822. if (func > 0 && func < MAX_CHECK)
  1823. seq_printf(m, "func=%s ", func_tokens[func]);
  1824. else
  1825. seq_printf(m, "func=%d ", func);
  1826. }
  1827. static void ima_show_rule_opt_list(struct seq_file *m,
  1828. const struct ima_rule_opt_list *opt_list)
  1829. {
  1830. size_t i;
  1831. for (i = 0; i < opt_list->count; i++)
  1832. seq_printf(m, "%s%s", i ? "|" : "", opt_list->items[i]);
  1833. }
  1834. static void ima_policy_show_appraise_algos(struct seq_file *m,
  1835. unsigned int allowed_hashes)
  1836. {
  1837. int idx, list_size = 0;
  1838. for (idx = 0; idx < HASH_ALGO__LAST; idx++) {
  1839. if (!(allowed_hashes & (1U << idx)))
  1840. continue;
  1841. /* only add commas if the list contains multiple entries */
  1842. if (list_size++)
  1843. seq_puts(m, ",");
  1844. seq_puts(m, hash_algo_name[idx]);
  1845. }
  1846. }
  1847. int ima_policy_show(struct seq_file *m, void *v)
  1848. {
  1849. struct ima_rule_entry *entry = v;
  1850. int i;
  1851. char tbuf[64] = {0,};
  1852. int offset = 0;
  1853. rcu_read_lock();
  1854. /* Do not print rules with inactive LSM labels */
  1855. for (i = 0; i < MAX_LSM_RULES; i++) {
  1856. if (entry->lsm[i].args_p && !entry->lsm[i].rule) {
  1857. rcu_read_unlock();
  1858. return 0;
  1859. }
  1860. }
  1861. if (entry->action & MEASURE)
  1862. seq_puts(m, pt(Opt_measure));
  1863. if (entry->action & DONT_MEASURE)
  1864. seq_puts(m, pt(Opt_dont_measure));
  1865. if (entry->action & APPRAISE)
  1866. seq_puts(m, pt(Opt_appraise));
  1867. if (entry->action & DONT_APPRAISE)
  1868. seq_puts(m, pt(Opt_dont_appraise));
  1869. if (entry->action & AUDIT)
  1870. seq_puts(m, pt(Opt_audit));
  1871. if (entry->action & DONT_AUDIT)
  1872. seq_puts(m, pt(Opt_dont_audit));
  1873. if (entry->action & HASH)
  1874. seq_puts(m, pt(Opt_hash));
  1875. if (entry->action & DONT_HASH)
  1876. seq_puts(m, pt(Opt_dont_hash));
  1877. seq_puts(m, " ");
  1878. if (entry->flags & IMA_FUNC)
  1879. policy_func_show(m, entry->func);
  1880. if ((entry->flags & IMA_MASK) || (entry->flags & IMA_INMASK)) {
  1881. if (entry->flags & IMA_MASK)
  1882. offset = 1;
  1883. if (entry->mask & MAY_EXEC)
  1884. seq_printf(m, pt(Opt_mask), mt(mask_exec) + offset);
  1885. if (entry->mask & MAY_WRITE)
  1886. seq_printf(m, pt(Opt_mask), mt(mask_write) + offset);
  1887. if (entry->mask & MAY_READ)
  1888. seq_printf(m, pt(Opt_mask), mt(mask_read) + offset);
  1889. if (entry->mask & MAY_APPEND)
  1890. seq_printf(m, pt(Opt_mask), mt(mask_append) + offset);
  1891. seq_puts(m, " ");
  1892. }
  1893. if (entry->flags & IMA_FSMAGIC) {
  1894. snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
  1895. seq_printf(m, pt(Opt_fsmagic), tbuf);
  1896. seq_puts(m, " ");
  1897. }
  1898. if (entry->flags & IMA_FSNAME) {
  1899. snprintf(tbuf, sizeof(tbuf), "%s", entry->fsname);
  1900. seq_printf(m, pt(Opt_fsname), tbuf);
  1901. seq_puts(m, " ");
  1902. }
  1903. if (entry->flags & IMA_FS_SUBTYPE) {
  1904. snprintf(tbuf, sizeof(tbuf), "%s", entry->fs_subtype);
  1905. seq_printf(m, pt(Opt_fs_subtype), tbuf);
  1906. seq_puts(m, " ");
  1907. }
  1908. if (entry->flags & IMA_KEYRINGS) {
  1909. seq_puts(m, "keyrings=");
  1910. ima_show_rule_opt_list(m, entry->keyrings);
  1911. seq_puts(m, " ");
  1912. }
  1913. if (entry->flags & IMA_LABEL) {
  1914. seq_puts(m, "label=");
  1915. ima_show_rule_opt_list(m, entry->label);
  1916. seq_puts(m, " ");
  1917. }
  1918. if (entry->flags & IMA_PCR) {
  1919. snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
  1920. seq_printf(m, pt(Opt_pcr), tbuf);
  1921. seq_puts(m, " ");
  1922. }
  1923. if (entry->flags & IMA_FSUUID) {
  1924. seq_printf(m, "fsuuid=%pU", &entry->fsuuid);
  1925. seq_puts(m, " ");
  1926. }
  1927. if (entry->flags & IMA_UID) {
  1928. snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
  1929. if (entry->uid_op == &uid_gt)
  1930. seq_printf(m, pt(Opt_uid_gt), tbuf);
  1931. else if (entry->uid_op == &uid_lt)
  1932. seq_printf(m, pt(Opt_uid_lt), tbuf);
  1933. else
  1934. seq_printf(m, pt(Opt_uid_eq), tbuf);
  1935. seq_puts(m, " ");
  1936. }
  1937. if (entry->flags & IMA_EUID) {
  1938. snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
  1939. if (entry->uid_op == &uid_gt)
  1940. seq_printf(m, pt(Opt_euid_gt), tbuf);
  1941. else if (entry->uid_op == &uid_lt)
  1942. seq_printf(m, pt(Opt_euid_lt), tbuf);
  1943. else
  1944. seq_printf(m, pt(Opt_euid_eq), tbuf);
  1945. seq_puts(m, " ");
  1946. }
  1947. if (entry->flags & IMA_GID) {
  1948. snprintf(tbuf, sizeof(tbuf), "%d", __kgid_val(entry->gid));
  1949. if (entry->gid_op == &gid_gt)
  1950. seq_printf(m, pt(Opt_gid_gt), tbuf);
  1951. else if (entry->gid_op == &gid_lt)
  1952. seq_printf(m, pt(Opt_gid_lt), tbuf);
  1953. else
  1954. seq_printf(m, pt(Opt_gid_eq), tbuf);
  1955. seq_puts(m, " ");
  1956. }
  1957. if (entry->flags & IMA_EGID) {
  1958. snprintf(tbuf, sizeof(tbuf), "%d", __kgid_val(entry->gid));
  1959. if (entry->gid_op == &gid_gt)
  1960. seq_printf(m, pt(Opt_egid_gt), tbuf);
  1961. else if (entry->gid_op == &gid_lt)
  1962. seq_printf(m, pt(Opt_egid_lt), tbuf);
  1963. else
  1964. seq_printf(m, pt(Opt_egid_eq), tbuf);
  1965. seq_puts(m, " ");
  1966. }
  1967. if (entry->flags & IMA_FOWNER) {
  1968. snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
  1969. if (entry->fowner_op == &vfsuid_gt_kuid)
  1970. seq_printf(m, pt(Opt_fowner_gt), tbuf);
  1971. else if (entry->fowner_op == &vfsuid_lt_kuid)
  1972. seq_printf(m, pt(Opt_fowner_lt), tbuf);
  1973. else
  1974. seq_printf(m, pt(Opt_fowner_eq), tbuf);
  1975. seq_puts(m, " ");
  1976. }
  1977. if (entry->flags & IMA_FGROUP) {
  1978. snprintf(tbuf, sizeof(tbuf), "%d", __kgid_val(entry->fgroup));
  1979. if (entry->fgroup_op == &vfsgid_gt_kgid)
  1980. seq_printf(m, pt(Opt_fgroup_gt), tbuf);
  1981. else if (entry->fgroup_op == &vfsgid_lt_kgid)
  1982. seq_printf(m, pt(Opt_fgroup_lt), tbuf);
  1983. else
  1984. seq_printf(m, pt(Opt_fgroup_eq), tbuf);
  1985. seq_puts(m, " ");
  1986. }
  1987. if (entry->flags & IMA_VALIDATE_ALGOS) {
  1988. seq_puts(m, "appraise_algos=");
  1989. ima_policy_show_appraise_algos(m, entry->allowed_algos);
  1990. seq_puts(m, " ");
  1991. }
  1992. for (i = 0; i < MAX_LSM_RULES; i++) {
  1993. if (entry->lsm[i].rule) {
  1994. switch (i) {
  1995. case LSM_OBJ_USER:
  1996. seq_printf(m, pt(Opt_obj_user),
  1997. entry->lsm[i].args_p);
  1998. break;
  1999. case LSM_OBJ_ROLE:
  2000. seq_printf(m, pt(Opt_obj_role),
  2001. entry->lsm[i].args_p);
  2002. break;
  2003. case LSM_OBJ_TYPE:
  2004. seq_printf(m, pt(Opt_obj_type),
  2005. entry->lsm[i].args_p);
  2006. break;
  2007. case LSM_SUBJ_USER:
  2008. seq_printf(m, pt(Opt_subj_user),
  2009. entry->lsm[i].args_p);
  2010. break;
  2011. case LSM_SUBJ_ROLE:
  2012. seq_printf(m, pt(Opt_subj_role),
  2013. entry->lsm[i].args_p);
  2014. break;
  2015. case LSM_SUBJ_TYPE:
  2016. seq_printf(m, pt(Opt_subj_type),
  2017. entry->lsm[i].args_p);
  2018. break;
  2019. }
  2020. seq_puts(m, " ");
  2021. }
  2022. }
  2023. if (entry->template)
  2024. seq_printf(m, "template=%s ", entry->template->name);
  2025. if (entry->flags & IMA_DIGSIG_REQUIRED) {
  2026. if (entry->flags & IMA_VERITY_REQUIRED)
  2027. seq_puts(m, "appraise_type=sigv3 ");
  2028. else if (entry->flags & IMA_MODSIG_ALLOWED)
  2029. seq_puts(m, "appraise_type=imasig|modsig ");
  2030. else
  2031. seq_puts(m, "appraise_type=imasig ");
  2032. }
  2033. if (entry->flags & IMA_VERITY_REQUIRED)
  2034. seq_puts(m, "digest_type=verity ");
  2035. if (entry->flags & IMA_PERMIT_DIRECTIO)
  2036. seq_puts(m, "permit_directio ");
  2037. rcu_read_unlock();
  2038. seq_puts(m, "\n");
  2039. return 0;
  2040. }
  2041. #endif /* CONFIG_IMA_READ_POLICY */
  2042. #if defined(CONFIG_IMA_APPRAISE) && defined(CONFIG_INTEGRITY_TRUSTED_KEYRING)
  2043. /*
  2044. * ima_appraise_signature: whether IMA will appraise a given function using
  2045. * an IMA digital signature. This is restricted to cases where the kernel
  2046. * has a set of built-in trusted keys in order to avoid an attacker simply
  2047. * loading additional keys.
  2048. */
  2049. bool ima_appraise_signature(enum kernel_read_file_id id)
  2050. {
  2051. struct ima_rule_entry *entry;
  2052. bool found = false;
  2053. enum ima_hooks func;
  2054. struct list_head *ima_rules_tmp;
  2055. if (id >= READING_MAX_ID)
  2056. return false;
  2057. if (id == READING_KEXEC_IMAGE && !(ima_appraise & IMA_APPRAISE_ENFORCE)
  2058. && security_locked_down(LOCKDOWN_KEXEC))
  2059. return false;
  2060. func = read_idmap[id] ?: FILE_CHECK;
  2061. rcu_read_lock();
  2062. ima_rules_tmp = rcu_dereference(ima_rules);
  2063. list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
  2064. if (entry->action != APPRAISE)
  2065. continue;
  2066. /*
  2067. * A generic entry will match, but otherwise require that it
  2068. * match the func we're looking for
  2069. */
  2070. if (entry->func && entry->func != func)
  2071. continue;
  2072. /*
  2073. * We require this to be a digital signature, not a raw IMA
  2074. * hash.
  2075. */
  2076. if (entry->flags & IMA_DIGSIG_REQUIRED)
  2077. found = true;
  2078. /*
  2079. * We've found a rule that matches, so break now even if it
  2080. * didn't require a digital signature - a later rule that does
  2081. * won't override it, so would be a false positive.
  2082. */
  2083. break;
  2084. }
  2085. rcu_read_unlock();
  2086. return found;
  2087. }
  2088. #endif /* CONFIG_IMA_APPRAISE && CONFIG_INTEGRITY_TRUSTED_KEYRING */