ima_api.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2008 IBM Corporation
  4. *
  5. * Author: Mimi Zohar <[email protected]>
  6. *
  7. * File: ima_api.c
  8. * Implements must_appraise_or_measure, collect_measurement,
  9. * appraise_measurement, store_measurement and store_template.
  10. */
  11. #include <linux/slab.h>
  12. #include <linux/file.h>
  13. #include <linux/fs.h>
  14. #include <linux/xattr.h>
  15. #include <linux/evm.h>
  16. #include <linux/iversion.h>
  17. #include <linux/fsverity.h>
  18. #include "ima.h"
  19. /*
  20. * ima_free_template_entry - free an existing template entry
  21. */
  22. void ima_free_template_entry(struct ima_template_entry *entry)
  23. {
  24. int i;
  25. for (i = 0; i < entry->template_desc->num_fields; i++)
  26. kfree(entry->template_data[i].data);
  27. kfree(entry->digests);
  28. kfree(entry);
  29. }
  30. /*
  31. * ima_alloc_init_template - create and initialize a new template entry
  32. */
  33. int ima_alloc_init_template(struct ima_event_data *event_data,
  34. struct ima_template_entry **entry,
  35. struct ima_template_desc *desc)
  36. {
  37. struct ima_template_desc *template_desc;
  38. struct tpm_digest *digests;
  39. int i, result = 0;
  40. if (desc)
  41. template_desc = desc;
  42. else
  43. template_desc = ima_template_desc_current();
  44. *entry = kzalloc(struct_size(*entry, template_data,
  45. template_desc->num_fields), GFP_NOFS);
  46. if (!*entry)
  47. return -ENOMEM;
  48. digests = kcalloc(NR_BANKS(ima_tpm_chip) + ima_extra_slots,
  49. sizeof(*digests), GFP_NOFS);
  50. if (!digests) {
  51. kfree(*entry);
  52. *entry = NULL;
  53. return -ENOMEM;
  54. }
  55. (*entry)->digests = digests;
  56. (*entry)->template_desc = template_desc;
  57. for (i = 0; i < template_desc->num_fields; i++) {
  58. const struct ima_template_field *field =
  59. template_desc->fields[i];
  60. u32 len;
  61. result = field->field_init(event_data,
  62. &((*entry)->template_data[i]));
  63. if (result != 0)
  64. goto out;
  65. len = (*entry)->template_data[i].len;
  66. (*entry)->template_data_len += sizeof(len);
  67. (*entry)->template_data_len += len;
  68. }
  69. return 0;
  70. out:
  71. ima_free_template_entry(*entry);
  72. *entry = NULL;
  73. return result;
  74. }
  75. /*
  76. * ima_store_template - store ima template measurements
  77. *
  78. * Calculate the hash of a template entry, add the template entry
  79. * to an ordered list of measurement entries maintained inside the kernel,
  80. * and also update the aggregate integrity value (maintained inside the
  81. * configured TPM PCR) over the hashes of the current list of measurement
  82. * entries.
  83. *
  84. * Applications retrieve the current kernel-held measurement list through
  85. * the securityfs entries in /sys/kernel/security/ima. The signed aggregate
  86. * TPM PCR (called quote) can be retrieved using a TPM user space library
  87. * and is used to validate the measurement list.
  88. *
  89. * Returns 0 on success, error code otherwise
  90. */
  91. int ima_store_template(struct ima_template_entry *entry,
  92. int violation, struct inode *inode,
  93. const unsigned char *filename, int pcr)
  94. {
  95. static const char op[] = "add_template_measure";
  96. static const char audit_cause[] = "hashing_error";
  97. char *template_name = entry->template_desc->name;
  98. int result;
  99. if (!violation) {
  100. result = ima_calc_field_array_hash(&entry->template_data[0],
  101. entry);
  102. if (result < 0) {
  103. integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode,
  104. template_name, op,
  105. audit_cause, result, 0);
  106. return result;
  107. }
  108. }
  109. entry->pcr = pcr;
  110. result = ima_add_template_entry(entry, violation, op, inode, filename);
  111. return result;
  112. }
  113. /*
  114. * ima_add_violation - add violation to measurement list.
  115. *
  116. * Violations are flagged in the measurement list with zero hash values.
  117. * By extending the PCR with 0xFF's instead of with zeroes, the PCR
  118. * value is invalidated.
  119. */
  120. void ima_add_violation(struct file *file, const unsigned char *filename,
  121. struct integrity_iint_cache *iint,
  122. const char *op, const char *cause)
  123. {
  124. struct ima_template_entry *entry;
  125. struct inode *inode = file_inode(file);
  126. struct ima_event_data event_data = { .iint = iint,
  127. .file = file,
  128. .filename = filename,
  129. .violation = cause };
  130. int violation = 1;
  131. int result;
  132. /* can overflow, only indicator */
  133. atomic_long_inc(&ima_htable.violations);
  134. result = ima_alloc_init_template(&event_data, &entry, NULL);
  135. if (result < 0) {
  136. result = -ENOMEM;
  137. goto err_out;
  138. }
  139. result = ima_store_template(entry, violation, inode,
  140. filename, CONFIG_IMA_MEASURE_PCR_IDX);
  141. if (result < 0)
  142. ima_free_template_entry(entry);
  143. err_out:
  144. integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
  145. op, cause, result, 0);
  146. }
  147. /**
  148. * ima_get_action - appraise & measure decision based on policy.
  149. * @mnt_userns: user namespace of the mount the inode was found from
  150. * @inode: pointer to the inode associated with the object being validated
  151. * @cred: pointer to credentials structure to validate
  152. * @secid: secid of the task being validated
  153. * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXEC,
  154. * MAY_APPEND)
  155. * @func: caller identifier
  156. * @pcr: pointer filled in if matched measure policy sets pcr=
  157. * @template_desc: pointer filled in if matched measure policy sets template=
  158. * @func_data: func specific data, may be NULL
  159. * @allowed_algos: allowlist of hash algorithms for the IMA xattr
  160. *
  161. * The policy is defined in terms of keypairs:
  162. * subj=, obj=, type=, func=, mask=, fsmagic=
  163. * subj,obj, and type: are LSM specific.
  164. * func: FILE_CHECK | BPRM_CHECK | CREDS_CHECK | MMAP_CHECK | MODULE_CHECK
  165. * | KEXEC_CMDLINE | KEY_CHECK | CRITICAL_DATA
  166. * mask: contains the permission mask
  167. * fsmagic: hex value
  168. *
  169. * Returns IMA_MEASURE, IMA_APPRAISE mask.
  170. *
  171. */
  172. int ima_get_action(struct user_namespace *mnt_userns, struct inode *inode,
  173. const struct cred *cred, u32 secid, int mask,
  174. enum ima_hooks func, int *pcr,
  175. struct ima_template_desc **template_desc,
  176. const char *func_data, unsigned int *allowed_algos)
  177. {
  178. int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE | IMA_HASH;
  179. flags &= ima_policy_flag;
  180. return ima_match_policy(mnt_userns, inode, cred, secid, func, mask,
  181. flags, pcr, template_desc, func_data,
  182. allowed_algos);
  183. }
  184. static int ima_get_verity_digest(struct integrity_iint_cache *iint,
  185. struct ima_max_digest_data *hash)
  186. {
  187. enum hash_algo verity_alg;
  188. int ret;
  189. /*
  190. * On failure, 'measure' policy rules will result in a file data
  191. * hash containing 0's.
  192. */
  193. ret = fsverity_get_digest(iint->inode, hash->digest, &verity_alg);
  194. if (ret)
  195. return ret;
  196. /*
  197. * Unlike in the case of actually calculating the file hash, in
  198. * the fsverity case regardless of the hash algorithm, return
  199. * the verity digest to be included in the measurement list. A
  200. * mismatch between the verity algorithm and the xattr signature
  201. * algorithm, if one exists, will be detected later.
  202. */
  203. hash->hdr.algo = verity_alg;
  204. hash->hdr.length = hash_digest_size[verity_alg];
  205. return 0;
  206. }
  207. /*
  208. * ima_collect_measurement - collect file measurement
  209. *
  210. * Calculate the file hash, if it doesn't already exist,
  211. * storing the measurement and i_version in the iint.
  212. *
  213. * Must be called with iint->mutex held.
  214. *
  215. * Return 0 on success, error code otherwise
  216. */
  217. int ima_collect_measurement(struct integrity_iint_cache *iint,
  218. struct file *file, void *buf, loff_t size,
  219. enum hash_algo algo, struct modsig *modsig)
  220. {
  221. const char *audit_cause = "failed";
  222. struct inode *inode = file_inode(file);
  223. struct inode *real_inode = d_real_inode(file_dentry(file));
  224. const char *filename = file->f_path.dentry->d_name.name;
  225. struct ima_max_digest_data hash;
  226. int result = 0;
  227. int length;
  228. void *tmpbuf;
  229. u64 i_version;
  230. /*
  231. * Always collect the modsig, because IMA might have already collected
  232. * the file digest without collecting the modsig in a previous
  233. * measurement rule.
  234. */
  235. if (modsig)
  236. ima_collect_modsig(modsig, buf, size);
  237. if (iint->flags & IMA_COLLECTED)
  238. goto out;
  239. /*
  240. * Detecting file change is based on i_version. On filesystems
  241. * which do not support i_version, support was originally limited
  242. * to an initial measurement/appraisal/audit, but was modified to
  243. * assume the file changed.
  244. */
  245. i_version = inode_query_iversion(inode);
  246. hash.hdr.algo = algo;
  247. hash.hdr.length = hash_digest_size[algo];
  248. /* Initialize hash digest to 0's in case of failure */
  249. memset(&hash.digest, 0, sizeof(hash.digest));
  250. if (iint->flags & IMA_VERITY_REQUIRED) {
  251. result = ima_get_verity_digest(iint, &hash);
  252. switch (result) {
  253. case 0:
  254. break;
  255. case -ENODATA:
  256. audit_cause = "no-verity-digest";
  257. break;
  258. default:
  259. audit_cause = "invalid-verity-digest";
  260. break;
  261. }
  262. } else if (buf) {
  263. result = ima_calc_buffer_hash(buf, size, &hash.hdr);
  264. } else {
  265. result = ima_calc_file_hash(file, &hash.hdr);
  266. }
  267. if (result && result != -EBADF && result != -EINVAL)
  268. goto out;
  269. length = sizeof(hash.hdr) + hash.hdr.length;
  270. tmpbuf = krealloc(iint->ima_hash, length, GFP_NOFS);
  271. if (!tmpbuf) {
  272. result = -ENOMEM;
  273. goto out;
  274. }
  275. iint->ima_hash = tmpbuf;
  276. memcpy(iint->ima_hash, &hash, length);
  277. iint->version = i_version;
  278. if (real_inode != inode) {
  279. iint->real_ino = real_inode->i_ino;
  280. iint->real_dev = real_inode->i_sb->s_dev;
  281. }
  282. /* Possibly temporary failure due to type of read (eg. O_DIRECT) */
  283. if (!result)
  284. iint->flags |= IMA_COLLECTED;
  285. out:
  286. if (result) {
  287. if (file->f_flags & O_DIRECT)
  288. audit_cause = "failed(directio)";
  289. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
  290. filename, "collect_data", audit_cause,
  291. result, 0);
  292. }
  293. return result;
  294. }
  295. /*
  296. * ima_store_measurement - store file measurement
  297. *
  298. * Create an "ima" template and then store the template by calling
  299. * ima_store_template.
  300. *
  301. * We only get here if the inode has not already been measured,
  302. * but the measurement could already exist:
  303. * - multiple copies of the same file on either the same or
  304. * different filesystems.
  305. * - the inode was previously flushed as well as the iint info,
  306. * containing the hashing info.
  307. *
  308. * Must be called with iint->mutex held.
  309. */
  310. void ima_store_measurement(struct integrity_iint_cache *iint,
  311. struct file *file, const unsigned char *filename,
  312. struct evm_ima_xattr_data *xattr_value,
  313. int xattr_len, const struct modsig *modsig, int pcr,
  314. struct ima_template_desc *template_desc)
  315. {
  316. static const char op[] = "add_template_measure";
  317. static const char audit_cause[] = "ENOMEM";
  318. int result = -ENOMEM;
  319. struct inode *inode = file_inode(file);
  320. struct ima_template_entry *entry;
  321. struct ima_event_data event_data = { .iint = iint,
  322. .file = file,
  323. .filename = filename,
  324. .xattr_value = xattr_value,
  325. .xattr_len = xattr_len,
  326. .modsig = modsig };
  327. int violation = 0;
  328. /*
  329. * We still need to store the measurement in the case of MODSIG because
  330. * we only have its contents to put in the list at the time of
  331. * appraisal, but a file measurement from earlier might already exist in
  332. * the measurement list.
  333. */
  334. if (iint->measured_pcrs & (0x1 << pcr) && !modsig)
  335. return;
  336. result = ima_alloc_init_template(&event_data, &entry, template_desc);
  337. if (result < 0) {
  338. integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
  339. op, audit_cause, result, 0);
  340. return;
  341. }
  342. result = ima_store_template(entry, violation, inode, filename, pcr);
  343. if ((!result || result == -EEXIST) && !(file->f_flags & O_DIRECT)) {
  344. iint->flags |= IMA_MEASURED;
  345. iint->measured_pcrs |= (0x1 << pcr);
  346. }
  347. if (result < 0)
  348. ima_free_template_entry(entry);
  349. }
  350. void ima_audit_measurement(struct integrity_iint_cache *iint,
  351. const unsigned char *filename)
  352. {
  353. struct audit_buffer *ab;
  354. char *hash;
  355. const char *algo_name = hash_algo_name[iint->ima_hash->algo];
  356. int i;
  357. if (iint->flags & IMA_AUDITED)
  358. return;
  359. hash = kzalloc((iint->ima_hash->length * 2) + 1, GFP_KERNEL);
  360. if (!hash)
  361. return;
  362. for (i = 0; i < iint->ima_hash->length; i++)
  363. hex_byte_pack(hash + (i * 2), iint->ima_hash->digest[i]);
  364. hash[i * 2] = '\0';
  365. ab = audit_log_start(audit_context(), GFP_KERNEL,
  366. AUDIT_INTEGRITY_RULE);
  367. if (!ab)
  368. goto out;
  369. audit_log_format(ab, "file=");
  370. audit_log_untrustedstring(ab, filename);
  371. audit_log_format(ab, " hash=\"%s:%s\"", algo_name, hash);
  372. audit_log_task_info(ab);
  373. audit_log_end(ab);
  374. iint->flags |= IMA_AUDITED;
  375. out:
  376. kfree(hash);
  377. return;
  378. }
  379. /*
  380. * ima_d_path - return a pointer to the full pathname
  381. *
  382. * Attempt to return a pointer to the full pathname for use in the
  383. * IMA measurement list, IMA audit records, and auditing logs.
  384. *
  385. * On failure, return a pointer to a copy of the filename, not dname.
  386. * Returning a pointer to dname, could result in using the pointer
  387. * after the memory has been freed.
  388. */
  389. const char *ima_d_path(const struct path *path, char **pathbuf, char *namebuf)
  390. {
  391. char *pathname = NULL;
  392. *pathbuf = __getname();
  393. if (*pathbuf) {
  394. pathname = d_absolute_path(path, *pathbuf, PATH_MAX);
  395. if (IS_ERR(pathname)) {
  396. __putname(*pathbuf);
  397. *pathbuf = NULL;
  398. pathname = NULL;
  399. }
  400. }
  401. if (!pathname) {
  402. strscpy(namebuf, path->dentry->d_name.name, NAME_MAX);
  403. pathname = namebuf;
  404. }
  405. return pathname;
  406. }