ima_main.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Integrity Measurement Architecture
  4. *
  5. * Copyright (C) 2005,2006,2007,2008 IBM Corporation
  6. *
  7. * Authors:
  8. * Reiner Sailer <sailer@watson.ibm.com>
  9. * Serge Hallyn <serue@us.ibm.com>
  10. * Kylene Hall <kylene@us.ibm.com>
  11. * Mimi Zohar <zohar@us.ibm.com>
  12. *
  13. * File: ima_main.c
  14. * implements the IMA hooks: ima_bprm_check, ima_file_mmap,
  15. * and ima_file_check.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/file.h>
  19. #include <linux/binfmts.h>
  20. #include <linux/kernel_read_file.h>
  21. #include <linux/mount.h>
  22. #include <linux/mman.h>
  23. #include <linux/slab.h>
  24. #include <linux/xattr.h>
  25. #include <linux/ima.h>
  26. #include <linux/fs.h>
  27. #include <linux/iversion.h>
  28. #include <linux/evm.h>
  29. #include "ima.h"
  30. #ifdef CONFIG_IMA_APPRAISE
  31. int ima_appraise = IMA_APPRAISE_ENFORCE;
  32. #else
  33. int ima_appraise;
  34. #endif
  35. int __ro_after_init ima_hash_algo = HASH_ALGO_SHA1;
  36. static int hash_setup_done;
  37. static struct notifier_block ima_lsm_policy_notifier = {
  38. .notifier_call = ima_lsm_policy_change,
  39. };
  40. static int __init hash_setup(char *str)
  41. {
  42. struct ima_template_desc *template_desc = ima_template_desc_current();
  43. int i;
  44. if (hash_setup_done)
  45. return 1;
  46. if (strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) == 0) {
  47. if (strncmp(str, "sha1", 4) == 0) {
  48. ima_hash_algo = HASH_ALGO_SHA1;
  49. } else if (strncmp(str, "md5", 3) == 0) {
  50. ima_hash_algo = HASH_ALGO_MD5;
  51. } else {
  52. pr_err("invalid hash algorithm \"%s\" for template \"%s\"",
  53. str, IMA_TEMPLATE_IMA_NAME);
  54. return 1;
  55. }
  56. goto out;
  57. }
  58. i = match_string(hash_algo_name, HASH_ALGO__LAST, str);
  59. if (i < 0) {
  60. pr_err("invalid hash algorithm \"%s\"", str);
  61. return 1;
  62. }
  63. ima_hash_algo = i;
  64. out:
  65. hash_setup_done = 1;
  66. return 1;
  67. }
  68. __setup("ima_hash=", hash_setup);
  69. enum hash_algo ima_get_current_hash_algo(void)
  70. {
  71. return ima_hash_algo;
  72. }
  73. /* Prevent mmap'ing a file execute that is already mmap'ed write */
  74. static int mmap_violation_check(enum ima_hooks func, struct file *file,
  75. char **pathbuf, const char **pathname,
  76. char *filename)
  77. {
  78. struct inode *inode;
  79. int rc = 0;
  80. if ((func == MMAP_CHECK || func == MMAP_CHECK_REQPROT) &&
  81. mapping_writably_mapped(file->f_mapping)) {
  82. rc = -ETXTBSY;
  83. inode = file_inode(file);
  84. if (!*pathbuf) /* ima_rdwr_violation possibly pre-fetched */
  85. *pathname = ima_d_path(&file->f_path, pathbuf,
  86. filename);
  87. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, *pathname,
  88. "mmap_file", "mmapped_writers", rc, 0);
  89. }
  90. return rc;
  91. }
  92. /*
  93. * ima_rdwr_violation_check
  94. *
  95. * Only invalidate the PCR for measured files:
  96. * - Opening a file for write when already open for read,
  97. * results in a time of measure, time of use (ToMToU) error.
  98. * - Opening a file for read when already open for write,
  99. * could result in a file measurement error.
  100. *
  101. */
  102. static void ima_rdwr_violation_check(struct file *file,
  103. struct ima_iint_cache *iint,
  104. int must_measure,
  105. char **pathbuf,
  106. const char **pathname,
  107. char *filename)
  108. {
  109. struct inode *inode = file_inode(file);
  110. fmode_t mode = file->f_mode;
  111. bool send_tomtou = false, send_writers = false;
  112. if (mode & FMODE_WRITE) {
  113. if (atomic_read(&inode->i_readcount) && IS_IMA(inode)) {
  114. if (!iint)
  115. iint = ima_iint_find(inode);
  116. /* IMA_MEASURE is set from reader side */
  117. if (iint && test_and_clear_bit(IMA_MAY_EMIT_TOMTOU,
  118. &iint->atomic_flags))
  119. send_tomtou = true;
  120. }
  121. } else {
  122. if (must_measure)
  123. set_bit(IMA_MAY_EMIT_TOMTOU, &iint->atomic_flags);
  124. /* Limit number of open_writers violations */
  125. if (inode_is_open_for_write(inode) && must_measure) {
  126. if (!test_and_set_bit(IMA_EMITTED_OPENWRITERS,
  127. &iint->atomic_flags))
  128. send_writers = true;
  129. }
  130. }
  131. if (!send_tomtou && !send_writers)
  132. return;
  133. *pathname = ima_d_path(&file->f_path, pathbuf, filename);
  134. if (send_tomtou)
  135. ima_add_violation(file, *pathname, iint,
  136. "invalid_pcr", "ToMToU");
  137. if (send_writers)
  138. ima_add_violation(file, *pathname, iint,
  139. "invalid_pcr", "open_writers");
  140. }
  141. static void ima_check_last_writer(struct ima_iint_cache *iint,
  142. struct inode *inode, struct file *file)
  143. {
  144. fmode_t mode = file->f_mode;
  145. bool update;
  146. if (!(mode & FMODE_WRITE))
  147. return;
  148. mutex_lock(&iint->mutex);
  149. if (atomic_read(&inode->i_writecount) == 1) {
  150. struct kstat stat;
  151. clear_bit(IMA_EMITTED_OPENWRITERS, &iint->atomic_flags);
  152. update = test_and_clear_bit(IMA_UPDATE_XATTR,
  153. &iint->atomic_flags);
  154. if ((iint->flags & IMA_NEW_FILE) ||
  155. vfs_getattr_nosec(&file->f_path, &stat,
  156. STATX_CHANGE_COOKIE,
  157. AT_STATX_SYNC_AS_STAT) ||
  158. !(stat.result_mask & STATX_CHANGE_COOKIE) ||
  159. stat.change_cookie != iint->real_inode.version) {
  160. iint->flags &= ~(IMA_DONE_MASK | IMA_NEW_FILE);
  161. iint->measured_pcrs = 0;
  162. if (update)
  163. ima_update_xattr(iint, file);
  164. }
  165. }
  166. mutex_unlock(&iint->mutex);
  167. }
  168. /**
  169. * ima_file_free - called on __fput()
  170. * @file: pointer to file structure being freed
  171. *
  172. * Flag files that changed, based on i_version
  173. */
  174. static void ima_file_free(struct file *file)
  175. {
  176. struct inode *inode = file_inode(file);
  177. struct ima_iint_cache *iint;
  178. if (!ima_policy_flag || !S_ISREG(inode->i_mode))
  179. return;
  180. iint = ima_iint_find(inode);
  181. if (!iint)
  182. return;
  183. ima_check_last_writer(iint, inode, file);
  184. }
  185. static int process_measurement(struct file *file, const struct cred *cred,
  186. u32 secid, char *buf, loff_t size, int mask,
  187. enum ima_hooks func)
  188. {
  189. struct inode *real_inode, *inode = file_inode(file);
  190. struct ima_iint_cache *iint = NULL;
  191. struct ima_template_desc *template_desc = NULL;
  192. struct inode *metadata_inode;
  193. char *pathbuf = NULL;
  194. char filename[NAME_MAX];
  195. const char *pathname = NULL;
  196. int rc = 0, action, must_appraise = 0;
  197. int pcr = CONFIG_IMA_MEASURE_PCR_IDX;
  198. struct evm_ima_xattr_data *xattr_value = NULL;
  199. struct modsig *modsig = NULL;
  200. int xattr_len = 0;
  201. bool violation_check;
  202. enum hash_algo hash_algo;
  203. unsigned int allowed_algos = 0;
  204. if (!ima_policy_flag || !S_ISREG(inode->i_mode))
  205. return 0;
  206. /* Return an IMA_MEASURE, IMA_APPRAISE, IMA_AUDIT action
  207. * bitmask based on the appraise/audit/measurement policy.
  208. * Included is the appraise submask.
  209. */
  210. action = ima_get_action(file_mnt_idmap(file), inode, cred, secid,
  211. mask, func, &pcr, &template_desc, NULL,
  212. &allowed_algos);
  213. violation_check = ((func == FILE_CHECK || func == MMAP_CHECK ||
  214. func == MMAP_CHECK_REQPROT) &&
  215. (ima_policy_flag & IMA_MEASURE) &&
  216. ((action & IMA_MEASURE) ||
  217. (file->f_mode & FMODE_WRITE)));
  218. if (!action && !violation_check)
  219. return 0;
  220. must_appraise = action & IMA_APPRAISE;
  221. /* Is the appraise rule hook specific? */
  222. if (action & IMA_FILE_APPRAISE)
  223. func = FILE_CHECK;
  224. inode_lock(inode);
  225. if (action) {
  226. iint = ima_inode_get(inode);
  227. if (!iint)
  228. rc = -ENOMEM;
  229. }
  230. if (!rc && violation_check)
  231. ima_rdwr_violation_check(file, iint, action & IMA_MEASURE,
  232. &pathbuf, &pathname, filename);
  233. inode_unlock(inode);
  234. if (rc)
  235. goto out;
  236. if (!action)
  237. goto out;
  238. mutex_lock(&iint->mutex);
  239. if (test_and_clear_bit(IMA_CHANGE_ATTR, &iint->atomic_flags))
  240. /*
  241. * Reset appraisal flags (action and non-action rule-specific)
  242. * if ima_inode_post_setattr was called.
  243. */
  244. iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED |
  245. IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK |
  246. IMA_NONACTION_RULE_FLAGS);
  247. /*
  248. * Re-evaulate the file if either the xattr has changed or the
  249. * kernel has no way of detecting file change on the filesystem.
  250. * (Limited to privileged mounted filesystems.)
  251. */
  252. if (test_and_clear_bit(IMA_CHANGE_XATTR, &iint->atomic_flags) ||
  253. ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
  254. !(inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) &&
  255. !(action & IMA_FAIL_UNVERIFIABLE_SIGS))) {
  256. iint->flags &= ~IMA_DONE_MASK;
  257. iint->measured_pcrs = 0;
  258. }
  259. /*
  260. * On stacked filesystems, detect and re-evaluate file data and
  261. * metadata changes.
  262. */
  263. real_inode = d_real_inode(file_dentry(file));
  264. if (real_inode != inode &&
  265. (action & IMA_DO_MASK) && (iint->flags & IMA_DONE_MASK)) {
  266. if (!IS_I_VERSION(real_inode) ||
  267. integrity_inode_attrs_changed(&iint->real_inode,
  268. real_inode)) {
  269. iint->flags &= ~IMA_DONE_MASK;
  270. iint->measured_pcrs = 0;
  271. }
  272. /*
  273. * Reset the EVM status when metadata changed.
  274. */
  275. metadata_inode = d_inode(d_real(file_dentry(file),
  276. D_REAL_METADATA));
  277. if (evm_metadata_changed(inode, metadata_inode))
  278. iint->flags &= ~(IMA_APPRAISED |
  279. IMA_APPRAISED_SUBMASK);
  280. }
  281. /* Determine if already appraised/measured based on bitmask
  282. * (IMA_MEASURE, IMA_MEASURED, IMA_XXXX_APPRAISE, IMA_XXXX_APPRAISED,
  283. * IMA_AUDIT, IMA_AUDITED)
  284. */
  285. iint->flags |= action;
  286. action &= IMA_DO_MASK;
  287. action &= ~((iint->flags & (IMA_DONE_MASK ^ IMA_MEASURED)) >> 1);
  288. /* If target pcr is already measured, unset IMA_MEASURE action */
  289. if ((action & IMA_MEASURE) && (iint->measured_pcrs & (0x1 << pcr)))
  290. action ^= IMA_MEASURE;
  291. /* HASH sets the digital signature and update flags, nothing else */
  292. if ((action & IMA_HASH) &&
  293. !(test_bit(IMA_DIGSIG, &iint->atomic_flags))) {
  294. xattr_len = ima_read_xattr(file_dentry(file),
  295. &xattr_value, xattr_len);
  296. if ((xattr_value && xattr_len > 2) &&
  297. (xattr_value->type == EVM_IMA_XATTR_DIGSIG))
  298. set_bit(IMA_DIGSIG, &iint->atomic_flags);
  299. iint->flags |= IMA_HASHED;
  300. action ^= IMA_HASH;
  301. set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
  302. }
  303. /* Nothing to do, just return existing appraised status */
  304. if (!action) {
  305. if (must_appraise) {
  306. rc = mmap_violation_check(func, file, &pathbuf,
  307. &pathname, filename);
  308. if (!rc)
  309. rc = ima_get_cache_status(iint, func);
  310. }
  311. goto out_locked;
  312. }
  313. if ((action & IMA_APPRAISE_SUBMASK) ||
  314. strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) != 0) {
  315. /* read 'security.ima' */
  316. xattr_len = ima_read_xattr(file_dentry(file),
  317. &xattr_value, xattr_len);
  318. /*
  319. * Read the appended modsig if allowed by the policy, and allow
  320. * an additional measurement list entry, if needed, based on the
  321. * template format and whether the file was already measured.
  322. */
  323. if (iint->flags & IMA_MODSIG_ALLOWED) {
  324. rc = ima_read_modsig(func, buf, size, &modsig);
  325. if (!rc && ima_template_has_modsig(template_desc) &&
  326. iint->flags & IMA_MEASURED)
  327. action |= IMA_MEASURE;
  328. }
  329. }
  330. hash_algo = ima_get_hash_algo(xattr_value, xattr_len);
  331. rc = ima_collect_measurement(iint, file, buf, size, hash_algo, modsig);
  332. if (rc != 0 && rc != -EBADF && rc != -EINVAL)
  333. goto out_locked;
  334. if (!pathbuf) /* ima_rdwr_violation possibly pre-fetched */
  335. pathname = ima_d_path(&file->f_path, &pathbuf, filename);
  336. if (action & IMA_MEASURE)
  337. ima_store_measurement(iint, file, pathname,
  338. xattr_value, xattr_len, modsig, pcr,
  339. template_desc);
  340. if (rc == 0 && (action & IMA_APPRAISE_SUBMASK)) {
  341. rc = ima_check_blacklist(iint, modsig, pcr);
  342. if (rc != -EPERM) {
  343. inode_lock(inode);
  344. rc = ima_appraise_measurement(func, iint, file,
  345. pathname, xattr_value,
  346. xattr_len, modsig);
  347. inode_unlock(inode);
  348. }
  349. if (!rc)
  350. rc = mmap_violation_check(func, file, &pathbuf,
  351. &pathname, filename);
  352. }
  353. if (action & IMA_AUDIT)
  354. ima_audit_measurement(iint, pathname);
  355. if ((file->f_flags & O_DIRECT) && (iint->flags & IMA_PERMIT_DIRECTIO))
  356. rc = 0;
  357. /* Ensure the digest was generated using an allowed algorithm */
  358. if (rc == 0 && must_appraise && allowed_algos != 0 &&
  359. (allowed_algos & (1U << hash_algo)) == 0) {
  360. rc = -EACCES;
  361. integrity_audit_msg(AUDIT_INTEGRITY_DATA, file_inode(file),
  362. pathname, "collect_data",
  363. "denied-hash-algorithm", rc, 0);
  364. }
  365. out_locked:
  366. if ((mask & MAY_WRITE) && test_bit(IMA_DIGSIG, &iint->atomic_flags) &&
  367. !(iint->flags & IMA_NEW_FILE))
  368. rc = -EACCES;
  369. mutex_unlock(&iint->mutex);
  370. kfree(xattr_value);
  371. ima_free_modsig(modsig);
  372. out:
  373. if (pathbuf)
  374. __putname(pathbuf);
  375. if (must_appraise) {
  376. if (rc && (ima_appraise & IMA_APPRAISE_ENFORCE))
  377. return -EACCES;
  378. if (file->f_mode & FMODE_WRITE)
  379. set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
  380. }
  381. return 0;
  382. }
  383. /**
  384. * ima_file_mmap - based on policy, collect/store measurement.
  385. * @file: pointer to the file to be measured (May be NULL)
  386. * @reqprot: protection requested by the application
  387. * @prot: protection that will be applied by the kernel
  388. * @flags: operational flags
  389. *
  390. * Measure files being mmapped executable based on the ima_must_measure()
  391. * policy decision.
  392. *
  393. * On success return 0. On integrity appraisal error, assuming the file
  394. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  395. */
  396. static int ima_file_mmap(struct file *file, unsigned long reqprot,
  397. unsigned long prot, unsigned long flags)
  398. {
  399. u32 secid;
  400. int ret;
  401. if (!file)
  402. return 0;
  403. security_current_getsecid_subj(&secid);
  404. if (reqprot & PROT_EXEC) {
  405. ret = process_measurement(file, current_cred(), secid, NULL,
  406. 0, MAY_EXEC, MMAP_CHECK_REQPROT);
  407. if (ret)
  408. return ret;
  409. }
  410. if (prot & PROT_EXEC)
  411. return process_measurement(file, current_cred(), secid, NULL,
  412. 0, MAY_EXEC, MMAP_CHECK);
  413. return 0;
  414. }
  415. /**
  416. * ima_file_mprotect - based on policy, limit mprotect change
  417. * @vma: vm_area_struct protection is set to
  418. * @reqprot: protection requested by the application
  419. * @prot: protection that will be applied by the kernel
  420. *
  421. * Files can be mmap'ed read/write and later changed to execute to circumvent
  422. * IMA's mmap appraisal policy rules. Due to locking issues (mmap semaphore
  423. * would be taken before i_mutex), files can not be measured or appraised at
  424. * this point. Eliminate this integrity gap by denying the mprotect
  425. * PROT_EXECUTE change, if an mmap appraise policy rule exists.
  426. *
  427. * On mprotect change success, return 0. On failure, return -EACESS.
  428. */
  429. static int ima_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
  430. unsigned long prot)
  431. {
  432. struct ima_template_desc *template = NULL;
  433. struct file *file;
  434. char filename[NAME_MAX];
  435. char *pathbuf = NULL;
  436. const char *pathname = NULL;
  437. struct inode *inode;
  438. int result = 0;
  439. int action;
  440. u32 secid;
  441. int pcr;
  442. /* Is mprotect making an mmap'ed file executable? */
  443. if (!(ima_policy_flag & IMA_APPRAISE) || !vma->vm_file ||
  444. !(prot & PROT_EXEC) || (vma->vm_flags & VM_EXEC))
  445. return 0;
  446. security_current_getsecid_subj(&secid);
  447. inode = file_inode(vma->vm_file);
  448. action = ima_get_action(file_mnt_idmap(vma->vm_file), inode,
  449. current_cred(), secid, MAY_EXEC, MMAP_CHECK,
  450. &pcr, &template, NULL, NULL);
  451. action |= ima_get_action(file_mnt_idmap(vma->vm_file), inode,
  452. current_cred(), secid, MAY_EXEC,
  453. MMAP_CHECK_REQPROT, &pcr, &template, NULL,
  454. NULL);
  455. /* Is the mmap'ed file in policy? */
  456. if (!(action & (IMA_MEASURE | IMA_APPRAISE_SUBMASK)))
  457. return 0;
  458. if (action & IMA_APPRAISE_SUBMASK)
  459. result = -EPERM;
  460. file = vma->vm_file;
  461. pathname = ima_d_path(&file->f_path, &pathbuf, filename);
  462. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, pathname,
  463. "collect_data", "failed-mprotect", result, 0);
  464. if (pathbuf)
  465. __putname(pathbuf);
  466. return result;
  467. }
  468. /**
  469. * ima_bprm_check - based on policy, collect/store measurement.
  470. * @bprm: contains the linux_binprm structure
  471. *
  472. * The OS protects against an executable file, already open for write,
  473. * from being executed in deny_write_access() and an executable file,
  474. * already open for execute, from being modified in get_write_access().
  475. * So we can be certain that what we verify and measure here is actually
  476. * what is being executed.
  477. *
  478. * On success return 0. On integrity appraisal error, assuming the file
  479. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  480. */
  481. static int ima_bprm_check(struct linux_binprm *bprm)
  482. {
  483. int ret;
  484. u32 secid;
  485. security_current_getsecid_subj(&secid);
  486. ret = process_measurement(bprm->file, current_cred(), secid, NULL, 0,
  487. MAY_EXEC, BPRM_CHECK);
  488. if (ret)
  489. return ret;
  490. security_cred_getsecid(bprm->cred, &secid);
  491. return process_measurement(bprm->file, bprm->cred, secid, NULL, 0,
  492. MAY_EXEC, CREDS_CHECK);
  493. }
  494. /**
  495. * ima_file_check - based on policy, collect/store measurement.
  496. * @file: pointer to the file to be measured
  497. * @mask: contains MAY_READ, MAY_WRITE, MAY_EXEC or MAY_APPEND
  498. *
  499. * Measure files based on the ima_must_measure() policy decision.
  500. *
  501. * On success return 0. On integrity appraisal error, assuming the file
  502. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  503. */
  504. static int ima_file_check(struct file *file, int mask)
  505. {
  506. u32 secid;
  507. security_current_getsecid_subj(&secid);
  508. return process_measurement(file, current_cred(), secid, NULL, 0,
  509. mask & (MAY_READ | MAY_WRITE | MAY_EXEC |
  510. MAY_APPEND), FILE_CHECK);
  511. }
  512. static int __ima_inode_hash(struct inode *inode, struct file *file, char *buf,
  513. size_t buf_size)
  514. {
  515. struct ima_iint_cache *iint = NULL, tmp_iint;
  516. int rc, hash_algo;
  517. if (ima_policy_flag) {
  518. iint = ima_iint_find(inode);
  519. if (iint)
  520. mutex_lock(&iint->mutex);
  521. }
  522. if ((!iint || !(iint->flags & IMA_COLLECTED)) && file) {
  523. if (iint)
  524. mutex_unlock(&iint->mutex);
  525. memset(&tmp_iint, 0, sizeof(tmp_iint));
  526. mutex_init(&tmp_iint.mutex);
  527. rc = ima_collect_measurement(&tmp_iint, file, NULL, 0,
  528. ima_hash_algo, NULL);
  529. if (rc < 0) {
  530. /* ima_hash could be allocated in case of failure. */
  531. if (rc != -ENOMEM)
  532. kfree(tmp_iint.ima_hash);
  533. return -EOPNOTSUPP;
  534. }
  535. iint = &tmp_iint;
  536. mutex_lock(&iint->mutex);
  537. }
  538. if (!iint)
  539. return -EOPNOTSUPP;
  540. /*
  541. * ima_file_hash can be called when ima_collect_measurement has still
  542. * not been called, we might not always have a hash.
  543. */
  544. if (!iint->ima_hash || !(iint->flags & IMA_COLLECTED)) {
  545. mutex_unlock(&iint->mutex);
  546. return -EOPNOTSUPP;
  547. }
  548. if (buf) {
  549. size_t copied_size;
  550. copied_size = min_t(size_t, iint->ima_hash->length, buf_size);
  551. memcpy(buf, iint->ima_hash->digest, copied_size);
  552. }
  553. hash_algo = iint->ima_hash->algo;
  554. mutex_unlock(&iint->mutex);
  555. if (iint == &tmp_iint)
  556. kfree(iint->ima_hash);
  557. return hash_algo;
  558. }
  559. /**
  560. * ima_file_hash - return a measurement of the file
  561. * @file: pointer to the file
  562. * @buf: buffer in which to store the hash
  563. * @buf_size: length of the buffer
  564. *
  565. * On success, return the hash algorithm (as defined in the enum hash_algo).
  566. * If buf is not NULL, this function also outputs the hash into buf.
  567. * If the hash is larger than buf_size, then only buf_size bytes will be copied.
  568. * It generally just makes sense to pass a buffer capable of holding the largest
  569. * possible hash: IMA_MAX_DIGEST_SIZE.
  570. * The file hash returned is based on the entire file, including the appended
  571. * signature.
  572. *
  573. * If the measurement cannot be performed, return -EOPNOTSUPP.
  574. * If the parameters are incorrect, return -EINVAL.
  575. */
  576. int ima_file_hash(struct file *file, char *buf, size_t buf_size)
  577. {
  578. if (!file)
  579. return -EINVAL;
  580. return __ima_inode_hash(file_inode(file), file, buf, buf_size);
  581. }
  582. EXPORT_SYMBOL_GPL(ima_file_hash);
  583. /**
  584. * ima_inode_hash - return the stored measurement if the inode has been hashed
  585. * and is in the iint cache.
  586. * @inode: pointer to the inode
  587. * @buf: buffer in which to store the hash
  588. * @buf_size: length of the buffer
  589. *
  590. * On success, return the hash algorithm (as defined in the enum hash_algo).
  591. * If buf is not NULL, this function also outputs the hash into buf.
  592. * If the hash is larger than buf_size, then only buf_size bytes will be copied.
  593. * It generally just makes sense to pass a buffer capable of holding the largest
  594. * possible hash: IMA_MAX_DIGEST_SIZE.
  595. * The hash returned is based on the entire contents, including the appended
  596. * signature.
  597. *
  598. * If IMA is disabled or if no measurement is available, return -EOPNOTSUPP.
  599. * If the parameters are incorrect, return -EINVAL.
  600. */
  601. int ima_inode_hash(struct inode *inode, char *buf, size_t buf_size)
  602. {
  603. if (!inode)
  604. return -EINVAL;
  605. return __ima_inode_hash(inode, NULL, buf, buf_size);
  606. }
  607. EXPORT_SYMBOL_GPL(ima_inode_hash);
  608. /**
  609. * ima_post_create_tmpfile - mark newly created tmpfile as new
  610. * @idmap: idmap of the mount the inode was found from
  611. * @inode: inode of the newly created tmpfile
  612. *
  613. * No measuring, appraising or auditing of newly created tmpfiles is needed.
  614. * Skip calling process_measurement(), but indicate which newly, created
  615. * tmpfiles are in policy.
  616. */
  617. static void ima_post_create_tmpfile(struct mnt_idmap *idmap,
  618. struct inode *inode)
  619. {
  620. struct ima_iint_cache *iint;
  621. int must_appraise;
  622. if (!ima_policy_flag || !S_ISREG(inode->i_mode))
  623. return;
  624. must_appraise = ima_must_appraise(idmap, inode, MAY_ACCESS,
  625. FILE_CHECK);
  626. if (!must_appraise)
  627. return;
  628. /* Nothing to do if we can't allocate memory */
  629. iint = ima_inode_get(inode);
  630. if (!iint)
  631. return;
  632. /* needed for writing the security xattrs */
  633. set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
  634. iint->ima_file_status = INTEGRITY_PASS;
  635. }
  636. /**
  637. * ima_post_path_mknod - mark as a new inode
  638. * @idmap: idmap of the mount the inode was found from
  639. * @dentry: newly created dentry
  640. *
  641. * Mark files created via the mknodat syscall as new, so that the
  642. * file data can be written later.
  643. */
  644. static void ima_post_path_mknod(struct mnt_idmap *idmap, struct dentry *dentry)
  645. {
  646. struct ima_iint_cache *iint;
  647. struct inode *inode = dentry->d_inode;
  648. int must_appraise;
  649. if (!ima_policy_flag || !S_ISREG(inode->i_mode))
  650. return;
  651. must_appraise = ima_must_appraise(idmap, inode, MAY_ACCESS,
  652. FILE_CHECK);
  653. if (!must_appraise)
  654. return;
  655. /* Nothing to do if we can't allocate memory */
  656. iint = ima_inode_get(inode);
  657. if (!iint)
  658. return;
  659. /* needed for re-opening empty files */
  660. iint->flags |= IMA_NEW_FILE;
  661. }
  662. /**
  663. * ima_read_file - pre-measure/appraise hook decision based on policy
  664. * @file: pointer to the file to be measured/appraised/audit
  665. * @read_id: caller identifier
  666. * @contents: whether a subsequent call will be made to ima_post_read_file()
  667. *
  668. * Permit reading a file based on policy. The policy rules are written
  669. * in terms of the policy identifier. Appraising the integrity of
  670. * a file requires a file descriptor.
  671. *
  672. * For permission return 0, otherwise return -EACCES.
  673. */
  674. static int ima_read_file(struct file *file, enum kernel_read_file_id read_id,
  675. bool contents)
  676. {
  677. enum ima_hooks func;
  678. u32 secid;
  679. /*
  680. * Do devices using pre-allocated memory run the risk of the
  681. * firmware being accessible to the device prior to the completion
  682. * of IMA's signature verification any more than when using two
  683. * buffers? It may be desirable to include the buffer address
  684. * in this API and walk all the dma_map_single() mappings to check.
  685. */
  686. /*
  687. * There will be a call made to ima_post_read_file() with
  688. * a filled buffer, so we don't need to perform an extra
  689. * read early here.
  690. */
  691. if (contents)
  692. return 0;
  693. /* Read entire file for all partial reads. */
  694. func = read_idmap[read_id] ?: FILE_CHECK;
  695. security_current_getsecid_subj(&secid);
  696. return process_measurement(file, current_cred(), secid, NULL,
  697. 0, MAY_READ, func);
  698. }
  699. const int read_idmap[READING_MAX_ID] = {
  700. [READING_FIRMWARE] = FIRMWARE_CHECK,
  701. [READING_MODULE] = MODULE_CHECK,
  702. [READING_KEXEC_IMAGE] = KEXEC_KERNEL_CHECK,
  703. [READING_KEXEC_INITRAMFS] = KEXEC_INITRAMFS_CHECK,
  704. [READING_POLICY] = POLICY_CHECK
  705. };
  706. /**
  707. * ima_post_read_file - in memory collect/appraise/audit measurement
  708. * @file: pointer to the file to be measured/appraised/audit
  709. * @buf: pointer to in memory file contents
  710. * @size: size of in memory file contents
  711. * @read_id: caller identifier
  712. *
  713. * Measure/appraise/audit in memory file based on policy. Policy rules
  714. * are written in terms of a policy identifier.
  715. *
  716. * On success return 0. On integrity appraisal error, assuming the file
  717. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  718. */
  719. static int ima_post_read_file(struct file *file, char *buf, loff_t size,
  720. enum kernel_read_file_id read_id)
  721. {
  722. enum ima_hooks func;
  723. u32 secid;
  724. /* permit signed certs */
  725. if (!file && read_id == READING_X509_CERTIFICATE)
  726. return 0;
  727. if (!file || !buf || size == 0) { /* should never happen */
  728. if (ima_appraise & IMA_APPRAISE_ENFORCE)
  729. return -EACCES;
  730. return 0;
  731. }
  732. func = read_idmap[read_id] ?: FILE_CHECK;
  733. security_current_getsecid_subj(&secid);
  734. return process_measurement(file, current_cred(), secid, buf, size,
  735. MAY_READ, func);
  736. }
  737. /**
  738. * ima_load_data - appraise decision based on policy
  739. * @id: kernel load data caller identifier
  740. * @contents: whether the full contents will be available in a later
  741. * call to ima_post_load_data().
  742. *
  743. * Callers of this LSM hook can not measure, appraise, or audit the
  744. * data provided by userspace. Enforce policy rules requiring a file
  745. * signature (eg. kexec'ed kernel image).
  746. *
  747. * For permission return 0, otherwise return -EACCES.
  748. */
  749. static int ima_load_data(enum kernel_load_data_id id, bool contents)
  750. {
  751. bool ima_enforce, sig_enforce;
  752. ima_enforce =
  753. (ima_appraise & IMA_APPRAISE_ENFORCE) == IMA_APPRAISE_ENFORCE;
  754. switch (id) {
  755. case LOADING_KEXEC_IMAGE:
  756. if (IS_ENABLED(CONFIG_KEXEC_SIG)
  757. && arch_ima_get_secureboot()) {
  758. pr_err("impossible to appraise a kernel image without a file descriptor; try using kexec_file_load syscall.\n");
  759. return -EACCES;
  760. }
  761. if (ima_enforce && (ima_appraise & IMA_APPRAISE_KEXEC)) {
  762. pr_err("impossible to appraise a kernel image without a file descriptor; try using kexec_file_load syscall.\n");
  763. return -EACCES; /* INTEGRITY_UNKNOWN */
  764. }
  765. break;
  766. case LOADING_FIRMWARE:
  767. if (ima_enforce && (ima_appraise & IMA_APPRAISE_FIRMWARE) && !contents) {
  768. pr_err("Prevent firmware sysfs fallback loading.\n");
  769. return -EACCES; /* INTEGRITY_UNKNOWN */
  770. }
  771. break;
  772. case LOADING_MODULE:
  773. sig_enforce = is_module_sig_enforced();
  774. if (ima_enforce && (!sig_enforce
  775. && (ima_appraise & IMA_APPRAISE_MODULES))) {
  776. pr_err("impossible to appraise a module without a file descriptor. sig_enforce kernel parameter might help\n");
  777. return -EACCES; /* INTEGRITY_UNKNOWN */
  778. }
  779. break;
  780. default:
  781. break;
  782. }
  783. return 0;
  784. }
  785. /**
  786. * ima_post_load_data - appraise decision based on policy
  787. * @buf: pointer to in memory file contents
  788. * @size: size of in memory file contents
  789. * @load_id: kernel load data caller identifier
  790. * @description: @load_id-specific description of contents
  791. *
  792. * Measure/appraise/audit in memory buffer based on policy. Policy rules
  793. * are written in terms of a policy identifier.
  794. *
  795. * On success return 0. On integrity appraisal error, assuming the file
  796. * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
  797. */
  798. static int ima_post_load_data(char *buf, loff_t size,
  799. enum kernel_load_data_id load_id,
  800. char *description)
  801. {
  802. if (load_id == LOADING_FIRMWARE) {
  803. if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&
  804. (ima_appraise & IMA_APPRAISE_ENFORCE)) {
  805. pr_err("Prevent firmware loading_store.\n");
  806. return -EACCES; /* INTEGRITY_UNKNOWN */
  807. }
  808. return 0;
  809. }
  810. /*
  811. * Measure the init_module syscall buffer containing the ELF image.
  812. */
  813. if (load_id == LOADING_MODULE)
  814. ima_measure_critical_data("modules", "init_module",
  815. buf, size, true, NULL, 0);
  816. return 0;
  817. }
  818. /**
  819. * process_buffer_measurement - Measure the buffer or the buffer data hash
  820. * @idmap: idmap of the mount the inode was found from
  821. * @inode: inode associated with the object being measured (NULL for KEY_CHECK)
  822. * @buf: pointer to the buffer that needs to be added to the log.
  823. * @size: size of buffer(in bytes).
  824. * @eventname: event name to be used for the buffer entry.
  825. * @func: IMA hook
  826. * @pcr: pcr to extend the measurement
  827. * @func_data: func specific data, may be NULL
  828. * @buf_hash: measure buffer data hash
  829. * @digest: buffer digest will be written to
  830. * @digest_len: buffer length
  831. *
  832. * Based on policy, either the buffer data or buffer data hash is measured
  833. *
  834. * Return: 0 if the buffer has been successfully measured, 1 if the digest
  835. * has been written to the passed location but not added to a measurement entry,
  836. * a negative value otherwise.
  837. */
  838. int process_buffer_measurement(struct mnt_idmap *idmap,
  839. struct inode *inode, const void *buf, int size,
  840. const char *eventname, enum ima_hooks func,
  841. int pcr, const char *func_data,
  842. bool buf_hash, u8 *digest, size_t digest_len)
  843. {
  844. int ret = 0;
  845. const char *audit_cause = "ENOMEM";
  846. struct ima_template_entry *entry = NULL;
  847. struct ima_iint_cache iint = {};
  848. struct ima_event_data event_data = {.iint = &iint,
  849. .filename = eventname,
  850. .buf = buf,
  851. .buf_len = size};
  852. struct ima_template_desc *template;
  853. struct ima_max_digest_data hash;
  854. struct ima_digest_data *hash_hdr = container_of(&hash.hdr,
  855. struct ima_digest_data, hdr);
  856. char digest_hash[IMA_MAX_DIGEST_SIZE];
  857. int digest_hash_len = hash_digest_size[ima_hash_algo];
  858. int violation = 0;
  859. int action = 0;
  860. u32 secid;
  861. if (digest && digest_len < digest_hash_len)
  862. return -EINVAL;
  863. if (!ima_policy_flag && !digest)
  864. return -ENOENT;
  865. template = ima_template_desc_buf();
  866. if (!template) {
  867. ret = -EINVAL;
  868. audit_cause = "ima_template_desc_buf";
  869. goto out;
  870. }
  871. /*
  872. * Both LSM hooks and auxilary based buffer measurements are
  873. * based on policy. To avoid code duplication, differentiate
  874. * between the LSM hooks and auxilary buffer measurements,
  875. * retrieving the policy rule information only for the LSM hook
  876. * buffer measurements.
  877. */
  878. if (func) {
  879. security_current_getsecid_subj(&secid);
  880. action = ima_get_action(idmap, inode, current_cred(),
  881. secid, 0, func, &pcr, &template,
  882. func_data, NULL);
  883. if (!(action & IMA_MEASURE) && !digest)
  884. return -ENOENT;
  885. }
  886. if (!pcr)
  887. pcr = CONFIG_IMA_MEASURE_PCR_IDX;
  888. iint.ima_hash = hash_hdr;
  889. iint.ima_hash->algo = ima_hash_algo;
  890. iint.ima_hash->length = hash_digest_size[ima_hash_algo];
  891. ret = ima_calc_buffer_hash(buf, size, iint.ima_hash);
  892. if (ret < 0) {
  893. audit_cause = "hashing_error";
  894. goto out;
  895. }
  896. if (buf_hash) {
  897. memcpy(digest_hash, hash_hdr->digest, digest_hash_len);
  898. ret = ima_calc_buffer_hash(digest_hash, digest_hash_len,
  899. iint.ima_hash);
  900. if (ret < 0) {
  901. audit_cause = "hashing_error";
  902. goto out;
  903. }
  904. event_data.buf = digest_hash;
  905. event_data.buf_len = digest_hash_len;
  906. }
  907. if (digest)
  908. memcpy(digest, iint.ima_hash->digest, digest_hash_len);
  909. if (!ima_policy_flag || (func && !(action & IMA_MEASURE)))
  910. return 1;
  911. ret = ima_alloc_init_template(&event_data, &entry, template);
  912. if (ret < 0) {
  913. audit_cause = "alloc_entry";
  914. goto out;
  915. }
  916. ret = ima_store_template(entry, violation, NULL, event_data.buf, pcr);
  917. if (ret < 0) {
  918. audit_cause = "store_entry";
  919. ima_free_template_entry(entry);
  920. }
  921. out:
  922. if (ret < 0)
  923. integrity_audit_message(AUDIT_INTEGRITY_PCR, NULL, eventname,
  924. func_measure_str(func),
  925. audit_cause, ret, 0, ret);
  926. return ret;
  927. }
  928. /**
  929. * ima_kexec_cmdline - measure kexec cmdline boot args
  930. * @kernel_fd: file descriptor of the kexec kernel being loaded
  931. * @buf: pointer to buffer
  932. * @size: size of buffer
  933. *
  934. * Buffers can only be measured, not appraised.
  935. */
  936. void ima_kexec_cmdline(int kernel_fd, const void *buf, int size)
  937. {
  938. struct fd f;
  939. if (!buf || !size)
  940. return;
  941. f = fdget(kernel_fd);
  942. if (!fd_file(f))
  943. return;
  944. process_buffer_measurement(file_mnt_idmap(fd_file(f)), file_inode(fd_file(f)),
  945. buf, size, "kexec-cmdline", KEXEC_CMDLINE, 0,
  946. NULL, false, NULL, 0);
  947. fdput(f);
  948. }
  949. /**
  950. * ima_measure_critical_data - measure kernel integrity critical data
  951. * @event_label: unique event label for grouping and limiting critical data
  952. * @event_name: event name for the record in the IMA measurement list
  953. * @buf: pointer to buffer data
  954. * @buf_len: length of buffer data (in bytes)
  955. * @hash: measure buffer data hash
  956. * @digest: buffer digest will be written to
  957. * @digest_len: buffer length
  958. *
  959. * Measure data critical to the integrity of the kernel into the IMA log
  960. * and extend the pcr. Examples of critical data could be various data
  961. * structures, policies, and states stored in kernel memory that can
  962. * impact the integrity of the system.
  963. *
  964. * Return: 0 if the buffer has been successfully measured, 1 if the digest
  965. * has been written to the passed location but not added to a measurement entry,
  966. * a negative value otherwise.
  967. */
  968. int ima_measure_critical_data(const char *event_label,
  969. const char *event_name,
  970. const void *buf, size_t buf_len,
  971. bool hash, u8 *digest, size_t digest_len)
  972. {
  973. if (!event_name || !event_label || !buf || !buf_len)
  974. return -ENOPARAM;
  975. return process_buffer_measurement(&nop_mnt_idmap, NULL, buf, buf_len,
  976. event_name, CRITICAL_DATA, 0,
  977. event_label, hash, digest,
  978. digest_len);
  979. }
  980. EXPORT_SYMBOL_GPL(ima_measure_critical_data);
  981. #ifdef CONFIG_INTEGRITY_ASYMMETRIC_KEYS
  982. /**
  983. * ima_kernel_module_request - Prevent crypto-pkcs1pad(rsa,*) requests
  984. * @kmod_name: kernel module name
  985. *
  986. * Avoid a verification loop where verifying the signature of the modprobe
  987. * binary requires executing modprobe itself. Since the modprobe iint->mutex
  988. * is already held when the signature verification is performed, a deadlock
  989. * occurs as soon as modprobe is executed within the critical region, since
  990. * the same lock cannot be taken again.
  991. *
  992. * This happens when public_key_verify_signature(), in case of RSA algorithm,
  993. * use alg_name to store internal information in order to construct an
  994. * algorithm on the fly, but crypto_larval_lookup() will try to use alg_name
  995. * in order to load a kernel module with same name.
  996. *
  997. * Since we don't have any real "crypto-pkcs1pad(rsa,*)" kernel modules,
  998. * we are safe to fail such module request from crypto_larval_lookup(), and
  999. * avoid the verification loop.
  1000. *
  1001. * Return: Zero if it is safe to load the kernel module, -EINVAL otherwise.
  1002. */
  1003. static int ima_kernel_module_request(char *kmod_name)
  1004. {
  1005. if (strncmp(kmod_name, "crypto-pkcs1pad(rsa,", 20) == 0)
  1006. return -EINVAL;
  1007. return 0;
  1008. }
  1009. #endif /* CONFIG_INTEGRITY_ASYMMETRIC_KEYS */
  1010. static int __init init_ima(void)
  1011. {
  1012. int error;
  1013. ima_appraise_parse_cmdline();
  1014. ima_init_template_list();
  1015. hash_setup(CONFIG_IMA_DEFAULT_HASH);
  1016. error = ima_init();
  1017. if (error && strcmp(hash_algo_name[ima_hash_algo],
  1018. CONFIG_IMA_DEFAULT_HASH) != 0) {
  1019. pr_info("Allocating %s failed, going to use default hash algorithm %s\n",
  1020. hash_algo_name[ima_hash_algo], CONFIG_IMA_DEFAULT_HASH);
  1021. hash_setup_done = 0;
  1022. hash_setup(CONFIG_IMA_DEFAULT_HASH);
  1023. error = ima_init();
  1024. }
  1025. if (error)
  1026. return error;
  1027. error = register_blocking_lsm_notifier(&ima_lsm_policy_notifier);
  1028. if (error)
  1029. pr_warn("Couldn't register LSM notifier, error %d\n", error);
  1030. if (!error)
  1031. ima_update_policy_flags();
  1032. return error;
  1033. }
  1034. static struct security_hook_list ima_hooks[] __ro_after_init = {
  1035. LSM_HOOK_INIT(bprm_check_security, ima_bprm_check),
  1036. LSM_HOOK_INIT(file_post_open, ima_file_check),
  1037. LSM_HOOK_INIT(inode_post_create_tmpfile, ima_post_create_tmpfile),
  1038. LSM_HOOK_INIT(file_release, ima_file_free),
  1039. LSM_HOOK_INIT(mmap_file, ima_file_mmap),
  1040. LSM_HOOK_INIT(file_mprotect, ima_file_mprotect),
  1041. LSM_HOOK_INIT(kernel_load_data, ima_load_data),
  1042. LSM_HOOK_INIT(kernel_post_load_data, ima_post_load_data),
  1043. LSM_HOOK_INIT(kernel_read_file, ima_read_file),
  1044. LSM_HOOK_INIT(kernel_post_read_file, ima_post_read_file),
  1045. LSM_HOOK_INIT(path_post_mknod, ima_post_path_mknod),
  1046. #ifdef CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS
  1047. LSM_HOOK_INIT(key_post_create_or_update, ima_post_key_create_or_update),
  1048. #endif
  1049. #ifdef CONFIG_INTEGRITY_ASYMMETRIC_KEYS
  1050. LSM_HOOK_INIT(kernel_module_request, ima_kernel_module_request),
  1051. #endif
  1052. LSM_HOOK_INIT(inode_free_security_rcu, ima_inode_free_rcu),
  1053. };
  1054. static const struct lsm_id ima_lsmid = {
  1055. .name = "ima",
  1056. .id = LSM_ID_IMA,
  1057. };
  1058. static int __init init_ima_lsm(void)
  1059. {
  1060. ima_iintcache_init();
  1061. security_add_hooks(ima_hooks, ARRAY_SIZE(ima_hooks), &ima_lsmid);
  1062. init_ima_appraise_lsm(&ima_lsmid);
  1063. return 0;
  1064. }
  1065. struct lsm_blob_sizes ima_blob_sizes __ro_after_init = {
  1066. .lbs_inode = sizeof(struct ima_iint_cache *),
  1067. };
  1068. DEFINE_LSM(ima) = {
  1069. .name = "ima",
  1070. .init = init_ima_lsm,
  1071. .order = LSM_ORDER_LAST,
  1072. .blobs = &ima_blob_sizes,
  1073. };
  1074. late_initcall(init_ima); /* Start IMA after the TPM is available */