ima_policy.c 61 KB

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