ima_api.c 13 KB

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