ima_api.c 13 KB

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