xref: /openbmc/linux/security/integrity/ima/ima_api.c (revision dd431c3a)
1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
23323eec9SMimi Zohar /*
33323eec9SMimi Zohar  * Copyright (C) 2008 IBM Corporation
43323eec9SMimi Zohar  *
53323eec9SMimi Zohar  * Author: Mimi Zohar <zohar@us.ibm.com>
63323eec9SMimi Zohar  *
73323eec9SMimi Zohar  * File: ima_api.c
82fe5d6deSMimi Zohar  *	Implements must_appraise_or_measure, collect_measurement,
92fe5d6deSMimi Zohar  *	appraise_measurement, store_measurement and store_template.
103323eec9SMimi Zohar  */
115a0e3ad6STejun Heo #include <linux/slab.h>
122fe5d6deSMimi Zohar #include <linux/file.h>
132fe5d6deSMimi Zohar #include <linux/fs.h>
142fe5d6deSMimi Zohar #include <linux/xattr.h>
152fe5d6deSMimi Zohar #include <linux/evm.h>
1654f03916SMimi Zohar #include <linux/fsverity.h>
171525b06dSDmitry Kasatkin 
183323eec9SMimi Zohar #include "ima.h"
192fe5d6deSMimi Zohar 
203323eec9SMimi Zohar /*
21a7ed7c60SRoberto Sassu  * ima_free_template_entry - free an existing template entry
22a7ed7c60SRoberto Sassu  */
ima_free_template_entry(struct ima_template_entry * entry)23a7ed7c60SRoberto Sassu void ima_free_template_entry(struct ima_template_entry *entry)
24a7ed7c60SRoberto Sassu {
25a7ed7c60SRoberto Sassu 	int i;
26a7ed7c60SRoberto Sassu 
27a7ed7c60SRoberto Sassu 	for (i = 0; i < entry->template_desc->num_fields; i++)
28a7ed7c60SRoberto Sassu 		kfree(entry->template_data[i].data);
29a7ed7c60SRoberto Sassu 
30aa724fe1SRoberto Sassu 	kfree(entry->digests);
31a7ed7c60SRoberto Sassu 	kfree(entry);
32a7ed7c60SRoberto Sassu }
33a7ed7c60SRoberto Sassu 
34a7ed7c60SRoberto Sassu /*
357bc5f447SRoberto Sassu  * ima_alloc_init_template - create and initialize a new template entry
367bc5f447SRoberto Sassu  */
ima_alloc_init_template(struct ima_event_data * event_data,struct ima_template_entry ** entry,struct ima_template_desc * desc)3723b57419SRoberto Sassu int ima_alloc_init_template(struct ima_event_data *event_data,
3819453ce0SMatthew Garrett 			    struct ima_template_entry **entry,
3919453ce0SMatthew Garrett 			    struct ima_template_desc *desc)
407bc5f447SRoberto Sassu {
4119453ce0SMatthew Garrett 	struct ima_template_desc *template_desc;
42aa724fe1SRoberto Sassu 	struct tpm_digest *digests;
43a71dc65dSRoberto Sassu 	int i, result = 0;
447bc5f447SRoberto Sassu 
4519453ce0SMatthew Garrett 	if (desc)
4619453ce0SMatthew Garrett 		template_desc = desc;
4719453ce0SMatthew Garrett 	else
4819453ce0SMatthew Garrett 		template_desc = ima_template_desc_current();
4919453ce0SMatthew Garrett 
502a7f0e53SGustavo A. R. Silva 	*entry = kzalloc(struct_size(*entry, template_data,
512a7f0e53SGustavo A. R. Silva 				     template_desc->num_fields), GFP_NOFS);
52a71dc65dSRoberto Sassu 	if (!*entry)
537bc5f447SRoberto Sassu 		return -ENOMEM;
547bc5f447SRoberto Sassu 
55aa724fe1SRoberto Sassu 	digests = kcalloc(NR_BANKS(ima_tpm_chip) + ima_extra_slots,
56aa724fe1SRoberto Sassu 			  sizeof(*digests), GFP_NOFS);
57aa724fe1SRoberto Sassu 	if (!digests) {
5842413b49SRoberto Sassu 		kfree(*entry);
5942413b49SRoberto Sassu 		*entry = NULL;
6042413b49SRoberto Sassu 		return -ENOMEM;
61aa724fe1SRoberto Sassu 	}
62aa724fe1SRoberto Sassu 
63aa724fe1SRoberto Sassu 	(*entry)->digests = digests;
64a7ed7c60SRoberto Sassu 	(*entry)->template_desc = template_desc;
65a71dc65dSRoberto Sassu 	for (i = 0; i < template_desc->num_fields; i++) {
66b2724d58SEric Biggers 		const struct ima_template_field *field =
67b2724d58SEric Biggers 			template_desc->fields[i];
68a71dc65dSRoberto Sassu 		u32 len;
69a71dc65dSRoberto Sassu 
7023b57419SRoberto Sassu 		result = field->field_init(event_data,
71a71dc65dSRoberto Sassu 					   &((*entry)->template_data[i]));
72a71dc65dSRoberto Sassu 		if (result != 0)
737bc5f447SRoberto Sassu 			goto out;
747bc5f447SRoberto Sassu 
75a71dc65dSRoberto Sassu 		len = (*entry)->template_data[i].len;
76a71dc65dSRoberto Sassu 		(*entry)->template_data_len += sizeof(len);
77a71dc65dSRoberto Sassu 		(*entry)->template_data_len += len;
787bc5f447SRoberto Sassu 	}
797bc5f447SRoberto Sassu 	return 0;
80a71dc65dSRoberto Sassu out:
81a7ed7c60SRoberto Sassu 	ima_free_template_entry(*entry);
82a71dc65dSRoberto Sassu 	*entry = NULL;
837bc5f447SRoberto Sassu 	return result;
847bc5f447SRoberto Sassu }
857bc5f447SRoberto Sassu 
867bc5f447SRoberto Sassu /*
873323eec9SMimi Zohar  * ima_store_template - store ima template measurements
883323eec9SMimi Zohar  *
893323eec9SMimi Zohar  * Calculate the hash of a template entry, add the template entry
903323eec9SMimi Zohar  * to an ordered list of measurement entries maintained inside the kernel,
913323eec9SMimi Zohar  * and also update the aggregate integrity value (maintained inside the
923323eec9SMimi Zohar  * configured TPM PCR) over the hashes of the current list of measurement
933323eec9SMimi Zohar  * entries.
943323eec9SMimi Zohar  *
953323eec9SMimi Zohar  * Applications retrieve the current kernel-held measurement list through
963323eec9SMimi Zohar  * the securityfs entries in /sys/kernel/security/ima. The signed aggregate
973323eec9SMimi Zohar  * TPM PCR (called quote) can be retrieved using a TPM user space library
983323eec9SMimi Zohar  * and is used to validate the measurement list.
993323eec9SMimi Zohar  *
1003323eec9SMimi Zohar  * Returns 0 on success, error code otherwise
1013323eec9SMimi Zohar  */
ima_store_template(struct ima_template_entry * entry,int violation,struct inode * inode,const unsigned char * filename,int pcr)1023323eec9SMimi Zohar int ima_store_template(struct ima_template_entry *entry,
1039803d413SRoberto Sassu 		       int violation, struct inode *inode,
10414b1da85SEric Richter 		       const unsigned char *filename, int pcr)
1053323eec9SMimi Zohar {
10652a13284SMimi Zohar 	static const char op[] = "add_template_measure";
10752a13284SMimi Zohar 	static const char audit_cause[] = "hashing_error";
108a71dc65dSRoberto Sassu 	char *template_name = entry->template_desc->name;
1093323eec9SMimi Zohar 	int result;
1103323eec9SMimi Zohar 
1113323eec9SMimi Zohar 	if (!violation) {
112a71dc65dSRoberto Sassu 		result = ima_calc_field_array_hash(&entry->template_data[0],
1137ca79645SRoberto Sassu 						   entry);
1143323eec9SMimi Zohar 		if (result < 0) {
1153323eec9SMimi Zohar 			integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode,
116a71dc65dSRoberto Sassu 					    template_name, op,
1173323eec9SMimi Zohar 					    audit_cause, result, 0);
1183323eec9SMimi Zohar 			return result;
1193323eec9SMimi Zohar 		}
1203323eec9SMimi Zohar 	}
12114b1da85SEric Richter 	entry->pcr = pcr;
1229803d413SRoberto Sassu 	result = ima_add_template_entry(entry, violation, op, inode, filename);
1233323eec9SMimi Zohar 	return result;
1243323eec9SMimi Zohar }
1253323eec9SMimi Zohar 
1263323eec9SMimi Zohar /*
1273323eec9SMimi Zohar  * ima_add_violation - add violation to measurement list.
1283323eec9SMimi Zohar  *
1293323eec9SMimi Zohar  * Violations are flagged in the measurement list with zero hash values.
1303323eec9SMimi Zohar  * By extending the PCR with 0xFF's instead of with zeroes, the PCR
1313323eec9SMimi Zohar  * value is invalidated.
1323323eec9SMimi Zohar  */
ima_add_violation(struct file * file,const unsigned char * filename,struct integrity_iint_cache * iint,const char * op,const char * cause)1337d802a22SRoberto Sassu void ima_add_violation(struct file *file, const unsigned char *filename,
1348d94eb9bSRoberto Sassu 		       struct integrity_iint_cache *iint,
1353323eec9SMimi Zohar 		       const char *op, const char *cause)
1363323eec9SMimi Zohar {
1373323eec9SMimi Zohar 	struct ima_template_entry *entry;
13831d4b761SLibo Chen 	struct inode *inode = file_inode(file);
139e038f5f6SThiago Jung Bauermann 	struct ima_event_data event_data = { .iint = iint,
140e038f5f6SThiago Jung Bauermann 					     .file = file,
141e038f5f6SThiago Jung Bauermann 					     .filename = filename,
142e038f5f6SThiago Jung Bauermann 					     .violation = cause };
1433323eec9SMimi Zohar 	int violation = 1;
1443323eec9SMimi Zohar 	int result;
1453323eec9SMimi Zohar 
1463323eec9SMimi Zohar 	/* can overflow, only indicator */
1473323eec9SMimi Zohar 	atomic_long_inc(&ima_htable.violations);
1483323eec9SMimi Zohar 
14919453ce0SMatthew Garrett 	result = ima_alloc_init_template(&event_data, &entry, NULL);
1507bc5f447SRoberto Sassu 	if (result < 0) {
1513323eec9SMimi Zohar 		result = -ENOMEM;
1523323eec9SMimi Zohar 		goto err_out;
1533323eec9SMimi Zohar 	}
15414b1da85SEric Richter 	result = ima_store_template(entry, violation, inode,
15514b1da85SEric Richter 				    filename, CONFIG_IMA_MEASURE_PCR_IDX);
1563323eec9SMimi Zohar 	if (result < 0)
157a7ed7c60SRoberto Sassu 		ima_free_template_entry(entry);
1583323eec9SMimi Zohar err_out:
1593323eec9SMimi Zohar 	integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
1603323eec9SMimi Zohar 			    op, cause, result, 0);
1613323eec9SMimi Zohar }
1623323eec9SMimi Zohar 
1633323eec9SMimi Zohar /**
164d9d300cdSDmitry Kasatkin  * ima_get_action - appraise & measure decision based on policy.
16539f60c1cSChristian Brauner  * @idmap: idmap of the mount the inode was found from
1664834177eSTyler Hicks  * @inode: pointer to the inode associated with the object being validated
167d906c10dSMatthew Garrett  * @cred: pointer to credentials structure to validate
168d906c10dSMatthew Garrett  * @secid: secid of the task being validated
16920f482abSLans Zhang  * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXEC,
17020f482abSLans Zhang  *        MAY_APPEND)
1714ad87a3dSMimi Zohar  * @func: caller identifier
172725de7faSEric Richter  * @pcr: pointer filled in if matched measure policy sets pcr=
17319453ce0SMatthew Garrett  * @template_desc: pointer filled in if matched measure policy sets template=
1742b4a2474STushar Sugandhi  * @func_data: func specific data, may be NULL
1751624dc00STHOBY Simon  * @allowed_algos: allowlist of hash algorithms for the IMA xattr
1763323eec9SMimi Zohar  *
1773323eec9SMimi Zohar  * The policy is defined in terms of keypairs:
1783323eec9SMimi Zohar  *		subj=, obj=, type=, func=, mask=, fsmagic=
1793323eec9SMimi Zohar  *	subj,obj, and type: are LSM specific.
180d906c10dSMatthew Garrett  *	func: FILE_CHECK | BPRM_CHECK | CREDS_CHECK | MMAP_CHECK | MODULE_CHECK
1814958db32SRoberto Sassu  *	| KEXEC_CMDLINE | KEY_CHECK | CRITICAL_DATA | SETXATTR_CHECK
1824958db32SRoberto Sassu  *	| MMAP_CHECK_REQPROT
1833323eec9SMimi Zohar  *	mask: contains the permission mask
1843323eec9SMimi Zohar  *	fsmagic: hex value
1853323eec9SMimi Zohar  *
1862fe5d6deSMimi Zohar  * Returns IMA_MEASURE, IMA_APPRAISE mask.
1872fe5d6deSMimi Zohar  *
1883323eec9SMimi Zohar  */
ima_get_action(struct mnt_idmap * idmap,struct inode * inode,const struct cred * cred,u32 secid,int mask,enum ima_hooks func,int * pcr,struct ima_template_desc ** template_desc,const char * func_data,unsigned int * allowed_algos)18939f60c1cSChristian Brauner int ima_get_action(struct mnt_idmap *idmap, struct inode *inode,
190a2d2329eSChristian Brauner 		   const struct cred *cred, u32 secid, int mask,
191a2d2329eSChristian Brauner 		   enum ima_hooks func, int *pcr,
192e9085e0aSLakshmi Ramasubramanian 		   struct ima_template_desc **template_desc,
1931624dc00STHOBY Simon 		   const char *func_data, unsigned int *allowed_algos)
1942fe5d6deSMimi Zohar {
195da1b0029SMimi Zohar 	int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE | IMA_HASH;
1962fe5d6deSMimi Zohar 
197fd5f4e90SDmitry Kasatkin 	flags &= ima_policy_flag;
1982fe5d6deSMimi Zohar 
19939f60c1cSChristian Brauner 	return ima_match_policy(idmap, inode, cred, secid, func, mask,
2001624dc00STHOBY Simon 				flags, pcr, template_desc, func_data,
2011624dc00STHOBY Simon 				allowed_algos);
2022fe5d6deSMimi Zohar }
2032fe5d6deSMimi Zohar 
ima_get_verity_digest(struct integrity_iint_cache * iint,struct ima_max_digest_data * hash)20474836ecbSEric Biggers static bool ima_get_verity_digest(struct integrity_iint_cache *iint,
20554f03916SMimi Zohar 				  struct ima_max_digest_data *hash)
20654f03916SMimi Zohar {
20774836ecbSEric Biggers 	enum hash_algo alg;
20874836ecbSEric Biggers 	int digest_len;
20954f03916SMimi Zohar 
21054f03916SMimi Zohar 	/*
21154f03916SMimi Zohar 	 * On failure, 'measure' policy rules will result in a file data
21254f03916SMimi Zohar 	 * hash containing 0's.
21354f03916SMimi Zohar 	 */
21474836ecbSEric Biggers 	digest_len = fsverity_get_digest(iint->inode, hash->digest, NULL, &alg);
21574836ecbSEric Biggers 	if (digest_len == 0)
21674836ecbSEric Biggers 		return false;
21754f03916SMimi Zohar 
21854f03916SMimi Zohar 	/*
21954f03916SMimi Zohar 	 * Unlike in the case of actually calculating the file hash, in
22054f03916SMimi Zohar 	 * the fsverity case regardless of the hash algorithm, return
22154f03916SMimi Zohar 	 * the verity digest to be included in the measurement list. A
22254f03916SMimi Zohar 	 * mismatch between the verity algorithm and the xattr signature
22354f03916SMimi Zohar 	 * algorithm, if one exists, will be detected later.
22454f03916SMimi Zohar 	 */
22574836ecbSEric Biggers 	hash->hdr.algo = alg;
22674836ecbSEric Biggers 	hash->hdr.length = digest_len;
22774836ecbSEric Biggers 	return true;
22854f03916SMimi Zohar }
22954f03916SMimi Zohar 
2303323eec9SMimi Zohar /*
2313323eec9SMimi Zohar  * ima_collect_measurement - collect file measurement
2323323eec9SMimi Zohar  *
2333323eec9SMimi Zohar  * Calculate the file hash, if it doesn't already exist,
2343323eec9SMimi Zohar  * storing the measurement and i_version in the iint.
2353323eec9SMimi Zohar  *
2363323eec9SMimi Zohar  * Must be called with iint->mutex held.
2373323eec9SMimi Zohar  *
2383323eec9SMimi Zohar  * Return 0 on success, error code otherwise
2393323eec9SMimi Zohar  */
ima_collect_measurement(struct integrity_iint_cache * iint,struct file * file,void * buf,loff_t size,enum hash_algo algo,struct modsig * modsig)240f381c272SMimi Zohar int ima_collect_measurement(struct integrity_iint_cache *iint,
241cf222217SMimi Zohar 			    struct file *file, void *buf, loff_t size,
24215588227SThiago Jung Bauermann 			    enum hash_algo algo, struct modsig *modsig)
2433323eec9SMimi Zohar {
244f9b2a735SMimi Zohar 	const char *audit_cause = "failed";
245496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
246a3116387SMimi Zohar 	struct inode *real_inode = d_real_inode(file_dentry(file));
2478c54135eSMimi Zohar 	struct ima_max_digest_data hash;
248dd431c3aSStefan Berger 	struct name_snapshot filename;
249db1d1e8bSJeff Layton 	struct kstat stat;
2502fe5d6deSMimi Zohar 	int result = 0;
251f3cc6b25SMimi Zohar 	int length;
252f3cc6b25SMimi Zohar 	void *tmpbuf;
253db1d1e8bSJeff Layton 	u64 i_version = 0;
2543323eec9SMimi Zohar 
255e5092255SThiago Jung Bauermann 	/*
256e5092255SThiago Jung Bauermann 	 * Always collect the modsig, because IMA might have already collected
257e5092255SThiago Jung Bauermann 	 * the file digest without collecting the modsig in a previous
258e5092255SThiago Jung Bauermann 	 * measurement rule.
259e5092255SThiago Jung Bauermann 	 */
260e5092255SThiago Jung Bauermann 	if (modsig)
261e5092255SThiago Jung Bauermann 		ima_collect_modsig(modsig, buf, size);
262e5092255SThiago Jung Bauermann 
263f3cc6b25SMimi Zohar 	if (iint->flags & IMA_COLLECTED)
264f3cc6b25SMimi Zohar 		goto out;
2653323eec9SMimi Zohar 
266f3cc6b25SMimi Zohar 	/*
26765603435SAustin Kim 	 * Detecting file change is based on i_version. On filesystems
2688c54135eSMimi Zohar 	 * which do not support i_version, support was originally limited
2698c54135eSMimi Zohar 	 * to an initial measurement/appraisal/audit, but was modified to
2708c54135eSMimi Zohar 	 * assume the file changed.
271f3cc6b25SMimi Zohar 	 */
272db1d1e8bSJeff Layton 	result = vfs_getattr_nosec(&file->f_path, &stat, STATX_CHANGE_COOKIE,
273db1d1e8bSJeff Layton 				   AT_STATX_SYNC_AS_STAT);
274db1d1e8bSJeff Layton 	if (!result && (stat.result_mask & STATX_CHANGE_COOKIE))
275db1d1e8bSJeff Layton 		i_version = stat.change_cookie;
276f3cc6b25SMimi Zohar 	hash.hdr.algo = algo;
27754f03916SMimi Zohar 	hash.hdr.length = hash_digest_size[algo];
278f3cc6b25SMimi Zohar 
279f3cc6b25SMimi Zohar 	/* Initialize hash digest to 0's in case of failure */
280f3cc6b25SMimi Zohar 	memset(&hash.digest, 0, sizeof(hash.digest));
281f3cc6b25SMimi Zohar 
28254f03916SMimi Zohar 	if (iint->flags & IMA_VERITY_REQUIRED) {
28374836ecbSEric Biggers 		if (!ima_get_verity_digest(iint, &hash)) {
28454f03916SMimi Zohar 			audit_cause = "no-verity-digest";
28574836ecbSEric Biggers 			result = -ENODATA;
28654f03916SMimi Zohar 		}
28754f03916SMimi Zohar 	} else if (buf) {
288f3cc6b25SMimi Zohar 		result = ima_calc_buffer_hash(buf, size, &hash.hdr);
28954f03916SMimi Zohar 	} else {
290f3cc6b25SMimi Zohar 		result = ima_calc_file_hash(file, &hash.hdr);
29154f03916SMimi Zohar 	}
292f3cc6b25SMimi Zohar 
2936dc387d5SMatt Bobrowski 	if (result && result != -EBADF && result != -EINVAL)
294f3cc6b25SMimi Zohar 		goto out;
295f3cc6b25SMimi Zohar 
296f3cc6b25SMimi Zohar 	length = sizeof(hash.hdr) + hash.hdr.length;
297f3cc6b25SMimi Zohar 	tmpbuf = krealloc(iint->ima_hash, length, GFP_NOFS);
298f3cc6b25SMimi Zohar 	if (!tmpbuf) {
299f3cc6b25SMimi Zohar 		result = -ENOMEM;
300f9b2a735SMimi Zohar 		goto out;
301f9b2a735SMimi Zohar 	}
302f9b2a735SMimi Zohar 
303a35c3fb6SDmitry Kasatkin 	iint->ima_hash = tmpbuf;
304a35c3fb6SDmitry Kasatkin 	memcpy(iint->ima_hash, &hash, length);
3053323eec9SMimi Zohar 	iint->version = i_version;
306a3116387SMimi Zohar 	if (real_inode != inode) {
307a3116387SMimi Zohar 		iint->real_ino = real_inode->i_ino;
308a3116387SMimi Zohar 		iint->real_dev = real_inode->i_sb->s_dev;
309a3116387SMimi Zohar 	}
310f3cc6b25SMimi Zohar 
311f3cc6b25SMimi Zohar 	/* Possibly temporary failure due to type of read (eg. O_DIRECT) */
312f3cc6b25SMimi Zohar 	if (!result)
3132fe5d6deSMimi Zohar 		iint->flags |= IMA_COLLECTED;
314f9b2a735SMimi Zohar out:
315f3cc6b25SMimi Zohar 	if (result) {
316f3cc6b25SMimi Zohar 		if (file->f_flags & O_DIRECT)
317f3cc6b25SMimi Zohar 			audit_cause = "failed(directio)";
318f3cc6b25SMimi Zohar 
319dd431c3aSStefan Berger 		take_dentry_name_snapshot(&filename, file->f_path.dentry);
320dd431c3aSStefan Berger 
3212fe5d6deSMimi Zohar 		integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
322dd431c3aSStefan Berger 				    filename.name.name, "collect_data",
323dd431c3aSStefan Berger 				    audit_cause, result, 0);
324dd431c3aSStefan Berger 
325dd431c3aSStefan Berger 		release_dentry_name_snapshot(&filename);
326f3cc6b25SMimi Zohar 	}
3273323eec9SMimi Zohar 	return result;
3283323eec9SMimi Zohar }
3293323eec9SMimi Zohar 
3303323eec9SMimi Zohar /*
3313323eec9SMimi Zohar  * ima_store_measurement - store file measurement
3323323eec9SMimi Zohar  *
3333323eec9SMimi Zohar  * Create an "ima" template and then store the template by calling
3343323eec9SMimi Zohar  * ima_store_template.
3353323eec9SMimi Zohar  *
3363323eec9SMimi Zohar  * We only get here if the inode has not already been measured,
3373323eec9SMimi Zohar  * but the measurement could already exist:
3383323eec9SMimi Zohar  *	- multiple copies of the same file on either the same or
3393323eec9SMimi Zohar  *	  different filesystems.
3403323eec9SMimi Zohar  *	- the inode was previously flushed as well as the iint info,
3413323eec9SMimi Zohar  *	  containing the hashing info.
3423323eec9SMimi Zohar  *
3433323eec9SMimi Zohar  * Must be called with iint->mutex held.
3443323eec9SMimi Zohar  */
ima_store_measurement(struct integrity_iint_cache * iint,struct file * file,const unsigned char * filename,struct evm_ima_xattr_data * xattr_value,int xattr_len,const struct modsig * modsig,int pcr,struct ima_template_desc * template_desc)345f381c272SMimi Zohar void ima_store_measurement(struct integrity_iint_cache *iint,
346bcbc9b0cSMimi Zohar 			   struct file *file, const unsigned char *filename,
347bcbc9b0cSMimi Zohar 			   struct evm_ima_xattr_data *xattr_value,
3483878d505SThiago Jung Bauermann 			   int xattr_len, const struct modsig *modsig, int pcr,
34919453ce0SMatthew Garrett 			   struct ima_template_desc *template_desc)
3503323eec9SMimi Zohar {
35152a13284SMimi Zohar 	static const char op[] = "add_template_measure";
35252a13284SMimi Zohar 	static const char audit_cause[] = "ENOMEM";
3533323eec9SMimi Zohar 	int result = -ENOMEM;
354496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
3553323eec9SMimi Zohar 	struct ima_template_entry *entry;
356e038f5f6SThiago Jung Bauermann 	struct ima_event_data event_data = { .iint = iint,
357e038f5f6SThiago Jung Bauermann 					     .file = file,
358e038f5f6SThiago Jung Bauermann 					     .filename = filename,
359e038f5f6SThiago Jung Bauermann 					     .xattr_value = xattr_value,
3603878d505SThiago Jung Bauermann 					     .xattr_len = xattr_len,
3613878d505SThiago Jung Bauermann 					     .modsig = modsig };
3623323eec9SMimi Zohar 	int violation = 0;
3633323eec9SMimi Zohar 
364e5092255SThiago Jung Bauermann 	/*
365e5092255SThiago Jung Bauermann 	 * We still need to store the measurement in the case of MODSIG because
366e5092255SThiago Jung Bauermann 	 * we only have its contents to put in the list at the time of
367e5092255SThiago Jung Bauermann 	 * appraisal, but a file measurement from earlier might already exist in
368e5092255SThiago Jung Bauermann 	 * the measurement list.
369e5092255SThiago Jung Bauermann 	 */
370e5092255SThiago Jung Bauermann 	if (iint->measured_pcrs & (0x1 << pcr) && !modsig)
3712fe5d6deSMimi Zohar 		return;
3722fe5d6deSMimi Zohar 
37319453ce0SMatthew Garrett 	result = ima_alloc_init_template(&event_data, &entry, template_desc);
3747bc5f447SRoberto Sassu 	if (result < 0) {
3753323eec9SMimi Zohar 		integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
3763323eec9SMimi Zohar 				    op, audit_cause, result, 0);
3773323eec9SMimi Zohar 		return;
3783323eec9SMimi Zohar 	}
3793323eec9SMimi Zohar 
38014b1da85SEric Richter 	result = ima_store_template(entry, violation, inode, filename, pcr);
381f3cc6b25SMimi Zohar 	if ((!result || result == -EEXIST) && !(file->f_flags & O_DIRECT)) {
3823323eec9SMimi Zohar 		iint->flags |= IMA_MEASURED;
383a422638dSEric Richter 		iint->measured_pcrs |= (0x1 << pcr);
384a422638dSEric Richter 	}
38545fae749SRoberto Sassu 	if (result < 0)
386a7ed7c60SRoberto Sassu 		ima_free_template_entry(entry);
3873323eec9SMimi Zohar }
388e7c568e0SPeter Moody 
ima_audit_measurement(struct integrity_iint_cache * iint,const unsigned char * filename)389e7c568e0SPeter Moody void ima_audit_measurement(struct integrity_iint_cache *iint,
390e7c568e0SPeter Moody 			   const unsigned char *filename)
391e7c568e0SPeter Moody {
392e7c568e0SPeter Moody 	struct audit_buffer *ab;
393e456ef88STycho Andersen 	char *hash;
3945278aa52SMimi Zohar 	const char *algo_name = hash_algo_name[iint->ima_hash->algo];
395e7c568e0SPeter Moody 	int i;
396e7c568e0SPeter Moody 
397e7c568e0SPeter Moody 	if (iint->flags & IMA_AUDITED)
398e7c568e0SPeter Moody 		return;
399e7c568e0SPeter Moody 
400e456ef88STycho Andersen 	hash = kzalloc((iint->ima_hash->length * 2) + 1, GFP_KERNEL);
401e456ef88STycho Andersen 	if (!hash)
402e456ef88STycho Andersen 		return;
403e456ef88STycho Andersen 
404a35c3fb6SDmitry Kasatkin 	for (i = 0; i < iint->ima_hash->length; i++)
405a35c3fb6SDmitry Kasatkin 		hex_byte_pack(hash + (i * 2), iint->ima_hash->digest[i]);
406e7c568e0SPeter Moody 	hash[i * 2] = '\0';
407e7c568e0SPeter Moody 
408cdfb6b34SRichard Guy Briggs 	ab = audit_log_start(audit_context(), GFP_KERNEL,
409e7c568e0SPeter Moody 			     AUDIT_INTEGRITY_RULE);
410e7c568e0SPeter Moody 	if (!ab)
411e456ef88STycho Andersen 		goto out;
412e7c568e0SPeter Moody 
413e7c568e0SPeter Moody 	audit_log_format(ab, "file=");
414e7c568e0SPeter Moody 	audit_log_untrustedstring(ab, filename);
415e456ef88STycho Andersen 	audit_log_format(ab, " hash=\"%s:%s\"", algo_name, hash);
416e7c568e0SPeter Moody 
4172a1fe215SPaul Moore 	audit_log_task_info(ab);
418e7c568e0SPeter Moody 	audit_log_end(ab);
419e7c568e0SPeter Moody 
420e7c568e0SPeter Moody 	iint->flags |= IMA_AUDITED;
421e456ef88STycho Andersen out:
422e456ef88STycho Andersen 	kfree(hash);
423e456ef88STycho Andersen 	return;
424e7c568e0SPeter Moody }
425ea1046d4SDmitry Kasatkin 
426bc15ed66SMimi Zohar /*
427bc15ed66SMimi Zohar  * ima_d_path - return a pointer to the full pathname
428bc15ed66SMimi Zohar  *
429bc15ed66SMimi Zohar  * Attempt to return a pointer to the full pathname for use in the
430bc15ed66SMimi Zohar  * IMA measurement list, IMA audit records, and auditing logs.
431bc15ed66SMimi Zohar  *
432bc15ed66SMimi Zohar  * On failure, return a pointer to a copy of the filename, not dname.
433bc15ed66SMimi Zohar  * Returning a pointer to dname, could result in using the pointer
434bc15ed66SMimi Zohar  * after the memory has been freed.
435bc15ed66SMimi Zohar  */
ima_d_path(const struct path * path,char ** pathbuf,char * namebuf)436bc15ed66SMimi Zohar const char *ima_d_path(const struct path *path, char **pathbuf, char *namebuf)
437ea1046d4SDmitry Kasatkin {
438dd431c3aSStefan Berger 	struct name_snapshot filename;
439ea1046d4SDmitry Kasatkin 	char *pathname = NULL;
440ea1046d4SDmitry Kasatkin 
441456f5fd3SDmitry Kasatkin 	*pathbuf = __getname();
442ea1046d4SDmitry Kasatkin 	if (*pathbuf) {
44317f4bad3SDmitry Kasatkin 		pathname = d_absolute_path(path, *pathbuf, PATH_MAX);
444ea1046d4SDmitry Kasatkin 		if (IS_ERR(pathname)) {
445456f5fd3SDmitry Kasatkin 			__putname(*pathbuf);
446ea1046d4SDmitry Kasatkin 			*pathbuf = NULL;
447ea1046d4SDmitry Kasatkin 			pathname = NULL;
448ea1046d4SDmitry Kasatkin 		}
449ea1046d4SDmitry Kasatkin 	}
450bc15ed66SMimi Zohar 
451bc15ed66SMimi Zohar 	if (!pathname) {
452dd431c3aSStefan Berger 		take_dentry_name_snapshot(&filename, path->dentry);
453dd431c3aSStefan Berger 		strscpy(namebuf, filename.name.name, NAME_MAX);
454dd431c3aSStefan Berger 		release_dentry_name_snapshot(&filename);
455dd431c3aSStefan Berger 
456bc15ed66SMimi Zohar 		pathname = namebuf;
457bc15ed66SMimi Zohar 	}
458bc15ed66SMimi Zohar 
459bc15ed66SMimi Zohar 	return pathname;
460ea1046d4SDmitry Kasatkin }
461