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> 163b370b21SJeff Layton #include <linux/iversion.h> 17*54f03916SMimi Zohar #include <linux/fsverity.h> 181525b06dSDmitry Kasatkin 193323eec9SMimi Zohar #include "ima.h" 202fe5d6deSMimi Zohar 213323eec9SMimi Zohar /* 22a7ed7c60SRoberto Sassu * ima_free_template_entry - free an existing template entry 23a7ed7c60SRoberto Sassu */ 24a7ed7c60SRoberto Sassu void ima_free_template_entry(struct ima_template_entry *entry) 25a7ed7c60SRoberto Sassu { 26a7ed7c60SRoberto Sassu int i; 27a7ed7c60SRoberto Sassu 28a7ed7c60SRoberto Sassu for (i = 0; i < entry->template_desc->num_fields; i++) 29a7ed7c60SRoberto Sassu kfree(entry->template_data[i].data); 30a7ed7c60SRoberto Sassu 31aa724fe1SRoberto Sassu kfree(entry->digests); 32a7ed7c60SRoberto Sassu kfree(entry); 33a7ed7c60SRoberto Sassu } 34a7ed7c60SRoberto Sassu 35a7ed7c60SRoberto Sassu /* 367bc5f447SRoberto Sassu * ima_alloc_init_template - create and initialize a new template entry 377bc5f447SRoberto Sassu */ 3823b57419SRoberto Sassu int ima_alloc_init_template(struct ima_event_data *event_data, 3919453ce0SMatthew Garrett struct ima_template_entry **entry, 4019453ce0SMatthew Garrett struct ima_template_desc *desc) 417bc5f447SRoberto Sassu { 4219453ce0SMatthew Garrett struct ima_template_desc *template_desc; 43aa724fe1SRoberto Sassu struct tpm_digest *digests; 44a71dc65dSRoberto Sassu int i, result = 0; 457bc5f447SRoberto Sassu 4619453ce0SMatthew Garrett if (desc) 4719453ce0SMatthew Garrett template_desc = desc; 4819453ce0SMatthew Garrett else 4919453ce0SMatthew Garrett template_desc = ima_template_desc_current(); 5019453ce0SMatthew Garrett 512a7f0e53SGustavo A. R. Silva *entry = kzalloc(struct_size(*entry, template_data, 522a7f0e53SGustavo A. R. Silva template_desc->num_fields), GFP_NOFS); 53a71dc65dSRoberto Sassu if (!*entry) 547bc5f447SRoberto Sassu return -ENOMEM; 557bc5f447SRoberto Sassu 56aa724fe1SRoberto Sassu digests = kcalloc(NR_BANKS(ima_tpm_chip) + ima_extra_slots, 57aa724fe1SRoberto Sassu sizeof(*digests), GFP_NOFS); 58aa724fe1SRoberto Sassu if (!digests) { 5942413b49SRoberto Sassu kfree(*entry); 6042413b49SRoberto Sassu *entry = NULL; 6142413b49SRoberto Sassu return -ENOMEM; 62aa724fe1SRoberto Sassu } 63aa724fe1SRoberto Sassu 64aa724fe1SRoberto Sassu (*entry)->digests = digests; 65a7ed7c60SRoberto Sassu (*entry)->template_desc = template_desc; 66a71dc65dSRoberto Sassu for (i = 0; i < template_desc->num_fields; i++) { 67b2724d58SEric Biggers const struct ima_template_field *field = 68b2724d58SEric Biggers template_desc->fields[i]; 69a71dc65dSRoberto Sassu u32 len; 70a71dc65dSRoberto Sassu 7123b57419SRoberto Sassu result = field->field_init(event_data, 72a71dc65dSRoberto Sassu &((*entry)->template_data[i])); 73a71dc65dSRoberto Sassu if (result != 0) 747bc5f447SRoberto Sassu goto out; 757bc5f447SRoberto Sassu 76a71dc65dSRoberto Sassu len = (*entry)->template_data[i].len; 77a71dc65dSRoberto Sassu (*entry)->template_data_len += sizeof(len); 78a71dc65dSRoberto Sassu (*entry)->template_data_len += len; 797bc5f447SRoberto Sassu } 807bc5f447SRoberto Sassu return 0; 81a71dc65dSRoberto Sassu out: 82a7ed7c60SRoberto Sassu ima_free_template_entry(*entry); 83a71dc65dSRoberto Sassu *entry = NULL; 847bc5f447SRoberto Sassu return result; 857bc5f447SRoberto Sassu } 867bc5f447SRoberto Sassu 877bc5f447SRoberto Sassu /* 883323eec9SMimi Zohar * ima_store_template - store ima template measurements 893323eec9SMimi Zohar * 903323eec9SMimi Zohar * Calculate the hash of a template entry, add the template entry 913323eec9SMimi Zohar * to an ordered list of measurement entries maintained inside the kernel, 923323eec9SMimi Zohar * and also update the aggregate integrity value (maintained inside the 933323eec9SMimi Zohar * configured TPM PCR) over the hashes of the current list of measurement 943323eec9SMimi Zohar * entries. 953323eec9SMimi Zohar * 963323eec9SMimi Zohar * Applications retrieve the current kernel-held measurement list through 973323eec9SMimi Zohar * the securityfs entries in /sys/kernel/security/ima. The signed aggregate 983323eec9SMimi Zohar * TPM PCR (called quote) can be retrieved using a TPM user space library 993323eec9SMimi Zohar * and is used to validate the measurement list. 1003323eec9SMimi Zohar * 1013323eec9SMimi Zohar * Returns 0 on success, error code otherwise 1023323eec9SMimi Zohar */ 1033323eec9SMimi Zohar int ima_store_template(struct ima_template_entry *entry, 1049803d413SRoberto Sassu int violation, struct inode *inode, 10514b1da85SEric Richter const unsigned char *filename, int pcr) 1063323eec9SMimi Zohar { 10752a13284SMimi Zohar static const char op[] = "add_template_measure"; 10852a13284SMimi Zohar static const char audit_cause[] = "hashing_error"; 109a71dc65dSRoberto Sassu char *template_name = entry->template_desc->name; 1103323eec9SMimi Zohar int result; 1113323eec9SMimi Zohar 1123323eec9SMimi Zohar if (!violation) { 113a71dc65dSRoberto Sassu result = ima_calc_field_array_hash(&entry->template_data[0], 1147ca79645SRoberto Sassu entry); 1153323eec9SMimi Zohar if (result < 0) { 1163323eec9SMimi Zohar integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, 117a71dc65dSRoberto Sassu template_name, op, 1183323eec9SMimi Zohar audit_cause, result, 0); 1193323eec9SMimi Zohar return result; 1203323eec9SMimi Zohar } 1213323eec9SMimi Zohar } 12214b1da85SEric Richter entry->pcr = pcr; 1239803d413SRoberto Sassu result = ima_add_template_entry(entry, violation, op, inode, filename); 1243323eec9SMimi Zohar return result; 1253323eec9SMimi Zohar } 1263323eec9SMimi Zohar 1273323eec9SMimi Zohar /* 1283323eec9SMimi Zohar * ima_add_violation - add violation to measurement list. 1293323eec9SMimi Zohar * 1303323eec9SMimi Zohar * Violations are flagged in the measurement list with zero hash values. 1313323eec9SMimi Zohar * By extending the PCR with 0xFF's instead of with zeroes, the PCR 1323323eec9SMimi Zohar * value is invalidated. 1333323eec9SMimi Zohar */ 1347d802a22SRoberto Sassu void ima_add_violation(struct file *file, const unsigned char *filename, 1358d94eb9bSRoberto Sassu struct integrity_iint_cache *iint, 1363323eec9SMimi Zohar const char *op, const char *cause) 1373323eec9SMimi Zohar { 1383323eec9SMimi Zohar struct ima_template_entry *entry; 13931d4b761SLibo Chen struct inode *inode = file_inode(file); 140e038f5f6SThiago Jung Bauermann struct ima_event_data event_data = { .iint = iint, 141e038f5f6SThiago Jung Bauermann .file = file, 142e038f5f6SThiago Jung Bauermann .filename = filename, 143e038f5f6SThiago Jung Bauermann .violation = cause }; 1443323eec9SMimi Zohar int violation = 1; 1453323eec9SMimi Zohar int result; 1463323eec9SMimi Zohar 1473323eec9SMimi Zohar /* can overflow, only indicator */ 1483323eec9SMimi Zohar atomic_long_inc(&ima_htable.violations); 1493323eec9SMimi Zohar 15019453ce0SMatthew Garrett result = ima_alloc_init_template(&event_data, &entry, NULL); 1517bc5f447SRoberto Sassu if (result < 0) { 1523323eec9SMimi Zohar result = -ENOMEM; 1533323eec9SMimi Zohar goto err_out; 1543323eec9SMimi Zohar } 15514b1da85SEric Richter result = ima_store_template(entry, violation, inode, 15614b1da85SEric Richter filename, CONFIG_IMA_MEASURE_PCR_IDX); 1573323eec9SMimi Zohar if (result < 0) 158a7ed7c60SRoberto Sassu ima_free_template_entry(entry); 1593323eec9SMimi Zohar err_out: 1603323eec9SMimi Zohar integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename, 1613323eec9SMimi Zohar op, cause, result, 0); 1623323eec9SMimi Zohar } 1633323eec9SMimi Zohar 1643323eec9SMimi Zohar /** 165d9d300cdSDmitry Kasatkin * ima_get_action - appraise & measure decision based on policy. 166a2d2329eSChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 1674834177eSTyler Hicks * @inode: pointer to the inode associated with the object being validated 168d906c10dSMatthew Garrett * @cred: pointer to credentials structure to validate 169d906c10dSMatthew Garrett * @secid: secid of the task being validated 17020f482abSLans Zhang * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXEC, 17120f482abSLans Zhang * MAY_APPEND) 1724ad87a3dSMimi Zohar * @func: caller identifier 173725de7faSEric Richter * @pcr: pointer filled in if matched measure policy sets pcr= 17419453ce0SMatthew Garrett * @template_desc: pointer filled in if matched measure policy sets template= 1752b4a2474STushar Sugandhi * @func_data: func specific data, may be NULL 1761624dc00STHOBY Simon * @allowed_algos: allowlist of hash algorithms for the IMA xattr 1773323eec9SMimi Zohar * 1783323eec9SMimi Zohar * The policy is defined in terms of keypairs: 1793323eec9SMimi Zohar * subj=, obj=, type=, func=, mask=, fsmagic= 1803323eec9SMimi Zohar * subj,obj, and type: are LSM specific. 181d906c10dSMatthew Garrett * func: FILE_CHECK | BPRM_CHECK | CREDS_CHECK | MMAP_CHECK | MODULE_CHECK 182d6e64501STushar Sugandhi * | KEXEC_CMDLINE | KEY_CHECK | CRITICAL_DATA 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 */ 189a2d2329eSChristian Brauner int ima_get_action(struct user_namespace *mnt_userns, 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 199a2d2329eSChristian Brauner return ima_match_policy(mnt_userns, inode, cred, secid, func, mask, 2001624dc00STHOBY Simon flags, pcr, template_desc, func_data, 2011624dc00STHOBY Simon allowed_algos); 2022fe5d6deSMimi Zohar } 2032fe5d6deSMimi Zohar 204*54f03916SMimi Zohar static int ima_get_verity_digest(struct integrity_iint_cache *iint, 205*54f03916SMimi Zohar struct ima_max_digest_data *hash) 206*54f03916SMimi Zohar { 207*54f03916SMimi Zohar enum hash_algo verity_alg; 208*54f03916SMimi Zohar int ret; 209*54f03916SMimi Zohar 210*54f03916SMimi Zohar /* 211*54f03916SMimi Zohar * On failure, 'measure' policy rules will result in a file data 212*54f03916SMimi Zohar * hash containing 0's. 213*54f03916SMimi Zohar */ 214*54f03916SMimi Zohar ret = fsverity_get_digest(iint->inode, hash->digest, &verity_alg); 215*54f03916SMimi Zohar if (ret) 216*54f03916SMimi Zohar return ret; 217*54f03916SMimi Zohar 218*54f03916SMimi Zohar /* 219*54f03916SMimi Zohar * Unlike in the case of actually calculating the file hash, in 220*54f03916SMimi Zohar * the fsverity case regardless of the hash algorithm, return 221*54f03916SMimi Zohar * the verity digest to be included in the measurement list. A 222*54f03916SMimi Zohar * mismatch between the verity algorithm and the xattr signature 223*54f03916SMimi Zohar * algorithm, if one exists, will be detected later. 224*54f03916SMimi Zohar */ 225*54f03916SMimi Zohar hash->hdr.algo = verity_alg; 226*54f03916SMimi Zohar hash->hdr.length = hash_digest_size[verity_alg]; 227*54f03916SMimi Zohar return 0; 228*54f03916SMimi Zohar } 229*54f03916SMimi 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 */ 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); 246b583043eSAl Viro const char *filename = file->f_path.dentry->d_name.name; 2478c54135eSMimi Zohar struct ima_max_digest_data hash; 2482fe5d6deSMimi Zohar int result = 0; 249f3cc6b25SMimi Zohar int length; 250f3cc6b25SMimi Zohar void *tmpbuf; 251f3cc6b25SMimi Zohar u64 i_version; 2523323eec9SMimi Zohar 253e5092255SThiago Jung Bauermann /* 254e5092255SThiago Jung Bauermann * Always collect the modsig, because IMA might have already collected 255e5092255SThiago Jung Bauermann * the file digest without collecting the modsig in a previous 256e5092255SThiago Jung Bauermann * measurement rule. 257e5092255SThiago Jung Bauermann */ 258e5092255SThiago Jung Bauermann if (modsig) 259e5092255SThiago Jung Bauermann ima_collect_modsig(modsig, buf, size); 260e5092255SThiago Jung Bauermann 261f3cc6b25SMimi Zohar if (iint->flags & IMA_COLLECTED) 262f3cc6b25SMimi Zohar goto out; 2633323eec9SMimi Zohar 264f3cc6b25SMimi Zohar /* 26565603435SAustin Kim * Detecting file change is based on i_version. On filesystems 2668c54135eSMimi Zohar * which do not support i_version, support was originally limited 2678c54135eSMimi Zohar * to an initial measurement/appraisal/audit, but was modified to 2688c54135eSMimi Zohar * assume the file changed. 269f3cc6b25SMimi Zohar */ 2703b370b21SJeff Layton i_version = inode_query_iversion(inode); 271f3cc6b25SMimi Zohar hash.hdr.algo = algo; 272*54f03916SMimi Zohar hash.hdr.length = hash_digest_size[algo]; 273f3cc6b25SMimi Zohar 274f3cc6b25SMimi Zohar /* Initialize hash digest to 0's in case of failure */ 275f3cc6b25SMimi Zohar memset(&hash.digest, 0, sizeof(hash.digest)); 276f3cc6b25SMimi Zohar 277*54f03916SMimi Zohar if (iint->flags & IMA_VERITY_REQUIRED) { 278*54f03916SMimi Zohar result = ima_get_verity_digest(iint, &hash); 279*54f03916SMimi Zohar switch (result) { 280*54f03916SMimi Zohar case 0: 281*54f03916SMimi Zohar break; 282*54f03916SMimi Zohar case -ENODATA: 283*54f03916SMimi Zohar audit_cause = "no-verity-digest"; 284*54f03916SMimi Zohar break; 285*54f03916SMimi Zohar default: 286*54f03916SMimi Zohar audit_cause = "invalid-verity-digest"; 287*54f03916SMimi Zohar break; 288*54f03916SMimi Zohar } 289*54f03916SMimi Zohar } else if (buf) { 290f3cc6b25SMimi Zohar result = ima_calc_buffer_hash(buf, size, &hash.hdr); 291*54f03916SMimi Zohar } else { 292f3cc6b25SMimi Zohar result = ima_calc_file_hash(file, &hash.hdr); 293*54f03916SMimi Zohar } 294f3cc6b25SMimi Zohar 295*54f03916SMimi Zohar if (result == -ENOMEM) 296f3cc6b25SMimi Zohar goto out; 297f3cc6b25SMimi Zohar 298f3cc6b25SMimi Zohar length = sizeof(hash.hdr) + hash.hdr.length; 299f3cc6b25SMimi Zohar tmpbuf = krealloc(iint->ima_hash, length, GFP_NOFS); 300f3cc6b25SMimi Zohar if (!tmpbuf) { 301f3cc6b25SMimi Zohar result = -ENOMEM; 302f9b2a735SMimi Zohar goto out; 303f9b2a735SMimi Zohar } 304f9b2a735SMimi Zohar 305a35c3fb6SDmitry Kasatkin iint->ima_hash = tmpbuf; 306a35c3fb6SDmitry Kasatkin memcpy(iint->ima_hash, &hash, length); 3073323eec9SMimi Zohar iint->version = i_version; 308f3cc6b25SMimi Zohar 309f3cc6b25SMimi Zohar /* Possibly temporary failure due to type of read (eg. O_DIRECT) */ 310f3cc6b25SMimi Zohar if (!result) 3112fe5d6deSMimi Zohar iint->flags |= IMA_COLLECTED; 312f9b2a735SMimi Zohar out: 313f3cc6b25SMimi Zohar if (result) { 314f3cc6b25SMimi Zohar if (file->f_flags & O_DIRECT) 315f3cc6b25SMimi Zohar audit_cause = "failed(directio)"; 316f3cc6b25SMimi Zohar 3172fe5d6deSMimi Zohar integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, 318f9b2a735SMimi Zohar filename, "collect_data", audit_cause, 3192fe5d6deSMimi Zohar result, 0); 320f3cc6b25SMimi Zohar } 3213323eec9SMimi Zohar return result; 3223323eec9SMimi Zohar } 3233323eec9SMimi Zohar 3243323eec9SMimi Zohar /* 3253323eec9SMimi Zohar * ima_store_measurement - store file measurement 3263323eec9SMimi Zohar * 3273323eec9SMimi Zohar * Create an "ima" template and then store the template by calling 3283323eec9SMimi Zohar * ima_store_template. 3293323eec9SMimi Zohar * 3303323eec9SMimi Zohar * We only get here if the inode has not already been measured, 3313323eec9SMimi Zohar * but the measurement could already exist: 3323323eec9SMimi Zohar * - multiple copies of the same file on either the same or 3333323eec9SMimi Zohar * different filesystems. 3343323eec9SMimi Zohar * - the inode was previously flushed as well as the iint info, 3353323eec9SMimi Zohar * containing the hashing info. 3363323eec9SMimi Zohar * 3373323eec9SMimi Zohar * Must be called with iint->mutex held. 3383323eec9SMimi Zohar */ 339f381c272SMimi Zohar void ima_store_measurement(struct integrity_iint_cache *iint, 340bcbc9b0cSMimi Zohar struct file *file, const unsigned char *filename, 341bcbc9b0cSMimi Zohar struct evm_ima_xattr_data *xattr_value, 3423878d505SThiago Jung Bauermann int xattr_len, const struct modsig *modsig, int pcr, 34319453ce0SMatthew Garrett struct ima_template_desc *template_desc) 3443323eec9SMimi Zohar { 34552a13284SMimi Zohar static const char op[] = "add_template_measure"; 34652a13284SMimi Zohar static const char audit_cause[] = "ENOMEM"; 3473323eec9SMimi Zohar int result = -ENOMEM; 348496ad9aaSAl Viro struct inode *inode = file_inode(file); 3493323eec9SMimi Zohar struct ima_template_entry *entry; 350e038f5f6SThiago Jung Bauermann struct ima_event_data event_data = { .iint = iint, 351e038f5f6SThiago Jung Bauermann .file = file, 352e038f5f6SThiago Jung Bauermann .filename = filename, 353e038f5f6SThiago Jung Bauermann .xattr_value = xattr_value, 3543878d505SThiago Jung Bauermann .xattr_len = xattr_len, 3553878d505SThiago Jung Bauermann .modsig = modsig }; 3563323eec9SMimi Zohar int violation = 0; 3573323eec9SMimi Zohar 358e5092255SThiago Jung Bauermann /* 359e5092255SThiago Jung Bauermann * We still need to store the measurement in the case of MODSIG because 360e5092255SThiago Jung Bauermann * we only have its contents to put in the list at the time of 361e5092255SThiago Jung Bauermann * appraisal, but a file measurement from earlier might already exist in 362e5092255SThiago Jung Bauermann * the measurement list. 363e5092255SThiago Jung Bauermann */ 364e5092255SThiago Jung Bauermann if (iint->measured_pcrs & (0x1 << pcr) && !modsig) 3652fe5d6deSMimi Zohar return; 3662fe5d6deSMimi Zohar 36719453ce0SMatthew Garrett result = ima_alloc_init_template(&event_data, &entry, template_desc); 3687bc5f447SRoberto Sassu if (result < 0) { 3693323eec9SMimi Zohar integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename, 3703323eec9SMimi Zohar op, audit_cause, result, 0); 3713323eec9SMimi Zohar return; 3723323eec9SMimi Zohar } 3733323eec9SMimi Zohar 37414b1da85SEric Richter result = ima_store_template(entry, violation, inode, filename, pcr); 375f3cc6b25SMimi Zohar if ((!result || result == -EEXIST) && !(file->f_flags & O_DIRECT)) { 3763323eec9SMimi Zohar iint->flags |= IMA_MEASURED; 377a422638dSEric Richter iint->measured_pcrs |= (0x1 << pcr); 378a422638dSEric Richter } 37945fae749SRoberto Sassu if (result < 0) 380a7ed7c60SRoberto Sassu ima_free_template_entry(entry); 3813323eec9SMimi Zohar } 382e7c568e0SPeter Moody 383e7c568e0SPeter Moody void ima_audit_measurement(struct integrity_iint_cache *iint, 384e7c568e0SPeter Moody const unsigned char *filename) 385e7c568e0SPeter Moody { 386e7c568e0SPeter Moody struct audit_buffer *ab; 387e456ef88STycho Andersen char *hash; 3885278aa52SMimi Zohar const char *algo_name = hash_algo_name[iint->ima_hash->algo]; 389e7c568e0SPeter Moody int i; 390e7c568e0SPeter Moody 391e7c568e0SPeter Moody if (iint->flags & IMA_AUDITED) 392e7c568e0SPeter Moody return; 393e7c568e0SPeter Moody 394e456ef88STycho Andersen hash = kzalloc((iint->ima_hash->length * 2) + 1, GFP_KERNEL); 395e456ef88STycho Andersen if (!hash) 396e456ef88STycho Andersen return; 397e456ef88STycho Andersen 398a35c3fb6SDmitry Kasatkin for (i = 0; i < iint->ima_hash->length; i++) 399a35c3fb6SDmitry Kasatkin hex_byte_pack(hash + (i * 2), iint->ima_hash->digest[i]); 400e7c568e0SPeter Moody hash[i * 2] = '\0'; 401e7c568e0SPeter Moody 402cdfb6b34SRichard Guy Briggs ab = audit_log_start(audit_context(), GFP_KERNEL, 403e7c568e0SPeter Moody AUDIT_INTEGRITY_RULE); 404e7c568e0SPeter Moody if (!ab) 405e456ef88STycho Andersen goto out; 406e7c568e0SPeter Moody 407e7c568e0SPeter Moody audit_log_format(ab, "file="); 408e7c568e0SPeter Moody audit_log_untrustedstring(ab, filename); 409e456ef88STycho Andersen audit_log_format(ab, " hash=\"%s:%s\"", algo_name, hash); 410e7c568e0SPeter Moody 4112a1fe215SPaul Moore audit_log_task_info(ab); 412e7c568e0SPeter Moody audit_log_end(ab); 413e7c568e0SPeter Moody 414e7c568e0SPeter Moody iint->flags |= IMA_AUDITED; 415e456ef88STycho Andersen out: 416e456ef88STycho Andersen kfree(hash); 417e456ef88STycho Andersen return; 418e7c568e0SPeter Moody } 419ea1046d4SDmitry Kasatkin 420bc15ed66SMimi Zohar /* 421bc15ed66SMimi Zohar * ima_d_path - return a pointer to the full pathname 422bc15ed66SMimi Zohar * 423bc15ed66SMimi Zohar * Attempt to return a pointer to the full pathname for use in the 424bc15ed66SMimi Zohar * IMA measurement list, IMA audit records, and auditing logs. 425bc15ed66SMimi Zohar * 426bc15ed66SMimi Zohar * On failure, return a pointer to a copy of the filename, not dname. 427bc15ed66SMimi Zohar * Returning a pointer to dname, could result in using the pointer 428bc15ed66SMimi Zohar * after the memory has been freed. 429bc15ed66SMimi Zohar */ 430bc15ed66SMimi Zohar const char *ima_d_path(const struct path *path, char **pathbuf, char *namebuf) 431ea1046d4SDmitry Kasatkin { 432ea1046d4SDmitry Kasatkin char *pathname = NULL; 433ea1046d4SDmitry Kasatkin 434456f5fd3SDmitry Kasatkin *pathbuf = __getname(); 435ea1046d4SDmitry Kasatkin if (*pathbuf) { 43617f4bad3SDmitry Kasatkin pathname = d_absolute_path(path, *pathbuf, PATH_MAX); 437ea1046d4SDmitry Kasatkin if (IS_ERR(pathname)) { 438456f5fd3SDmitry Kasatkin __putname(*pathbuf); 439ea1046d4SDmitry Kasatkin *pathbuf = NULL; 440ea1046d4SDmitry Kasatkin pathname = NULL; 441ea1046d4SDmitry Kasatkin } 442ea1046d4SDmitry Kasatkin } 443bc15ed66SMimi Zohar 444bc15ed66SMimi Zohar if (!pathname) { 445cc4299eaSPetr Vorel strscpy(namebuf, path->dentry->d_name.name, NAME_MAX); 446bc15ed66SMimi Zohar pathname = namebuf; 447bc15ed66SMimi Zohar } 448bc15ed66SMimi Zohar 449bc15ed66SMimi Zohar return pathname; 450ea1046d4SDmitry Kasatkin } 451