ima_policy.c 62 KB

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