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> 171525b06dSDmitry Kasatkin 183323eec9SMimi Zohar #include "ima.h" 192fe5d6deSMimi Zohar 203323eec9SMimi Zohar /* 21a7ed7c60SRoberto Sassu * ima_free_template_entry - free an existing template entry 22a7ed7c60SRoberto Sassu */ 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 */ 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 */ 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 */ 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. 1654834177eSTyler Hicks * @inode: pointer to the inode associated with the object being validated 166d906c10dSMatthew Garrett * @cred: pointer to credentials structure to validate 167d906c10dSMatthew Garrett * @secid: secid of the task being validated 16820f482abSLans Zhang * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXEC, 16920f482abSLans Zhang * MAY_APPEND) 1704ad87a3dSMimi Zohar * @func: caller identifier 171725de7faSEric Richter * @pcr: pointer filled in if matched measure policy sets pcr= 17219453ce0SMatthew Garrett * @template_desc: pointer filled in if matched measure policy sets template= 1732b4a2474STushar Sugandhi * @func_data: func specific data, may be NULL 1743323eec9SMimi Zohar * 1753323eec9SMimi Zohar * The policy is defined in terms of keypairs: 1763323eec9SMimi Zohar * subj=, obj=, type=, func=, mask=, fsmagic= 1773323eec9SMimi Zohar * subj,obj, and type: are LSM specific. 178d906c10dSMatthew Garrett * func: FILE_CHECK | BPRM_CHECK | CREDS_CHECK | MMAP_CHECK | MODULE_CHECK 179*d6e64501STushar Sugandhi * | KEXEC_CMDLINE | KEY_CHECK | CRITICAL_DATA 1803323eec9SMimi Zohar * mask: contains the permission mask 1813323eec9SMimi Zohar * fsmagic: hex value 1823323eec9SMimi Zohar * 1832fe5d6deSMimi Zohar * Returns IMA_MEASURE, IMA_APPRAISE mask. 1842fe5d6deSMimi Zohar * 1853323eec9SMimi Zohar */ 186d906c10dSMatthew Garrett int ima_get_action(struct inode *inode, const struct cred *cred, u32 secid, 18719453ce0SMatthew Garrett int mask, enum ima_hooks func, int *pcr, 188e9085e0aSLakshmi Ramasubramanian struct ima_template_desc **template_desc, 1892b4a2474STushar Sugandhi const char *func_data) 1902fe5d6deSMimi Zohar { 191da1b0029SMimi Zohar int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE | IMA_HASH; 1922fe5d6deSMimi Zohar 193fd5f4e90SDmitry Kasatkin flags &= ima_policy_flag; 1942fe5d6deSMimi Zohar 19519453ce0SMatthew Garrett return ima_match_policy(inode, cred, secid, func, mask, flags, pcr, 1962b4a2474STushar Sugandhi template_desc, func_data); 1972fe5d6deSMimi Zohar } 1982fe5d6deSMimi Zohar 1993323eec9SMimi Zohar /* 2003323eec9SMimi Zohar * ima_collect_measurement - collect file measurement 2013323eec9SMimi Zohar * 2023323eec9SMimi Zohar * Calculate the file hash, if it doesn't already exist, 2033323eec9SMimi Zohar * storing the measurement and i_version in the iint. 2043323eec9SMimi Zohar * 2053323eec9SMimi Zohar * Must be called with iint->mutex held. 2063323eec9SMimi Zohar * 2073323eec9SMimi Zohar * Return 0 on success, error code otherwise 2083323eec9SMimi Zohar */ 209f381c272SMimi Zohar int ima_collect_measurement(struct integrity_iint_cache *iint, 210cf222217SMimi Zohar struct file *file, void *buf, loff_t size, 21115588227SThiago Jung Bauermann enum hash_algo algo, struct modsig *modsig) 2123323eec9SMimi Zohar { 213f9b2a735SMimi Zohar const char *audit_cause = "failed"; 214496ad9aaSAl Viro struct inode *inode = file_inode(file); 215b583043eSAl Viro const char *filename = file->f_path.dentry->d_name.name; 2162fe5d6deSMimi Zohar int result = 0; 217f3cc6b25SMimi Zohar int length; 218f3cc6b25SMimi Zohar void *tmpbuf; 219f3cc6b25SMimi Zohar u64 i_version; 220a35c3fb6SDmitry Kasatkin struct { 221a35c3fb6SDmitry Kasatkin struct ima_digest_data hdr; 222a35c3fb6SDmitry Kasatkin char digest[IMA_MAX_DIGEST_SIZE]; 223a35c3fb6SDmitry Kasatkin } hash; 2243323eec9SMimi Zohar 225e5092255SThiago Jung Bauermann /* 226e5092255SThiago Jung Bauermann * Always collect the modsig, because IMA might have already collected 227e5092255SThiago Jung Bauermann * the file digest without collecting the modsig in a previous 228e5092255SThiago Jung Bauermann * measurement rule. 229e5092255SThiago Jung Bauermann */ 230e5092255SThiago Jung Bauermann if (modsig) 231e5092255SThiago Jung Bauermann ima_collect_modsig(modsig, buf, size); 232e5092255SThiago Jung Bauermann 233f3cc6b25SMimi Zohar if (iint->flags & IMA_COLLECTED) 234f3cc6b25SMimi Zohar goto out; 2353323eec9SMimi Zohar 236f3cc6b25SMimi Zohar /* 237f3cc6b25SMimi Zohar * Dectecting file change is based on i_version. On filesystems 238f3cc6b25SMimi Zohar * which do not support i_version, support is limited to an initial 239f3cc6b25SMimi Zohar * measurement/appraisal/audit. 240f3cc6b25SMimi Zohar */ 2413b370b21SJeff Layton i_version = inode_query_iversion(inode); 242f3cc6b25SMimi Zohar hash.hdr.algo = algo; 243f3cc6b25SMimi Zohar 244f3cc6b25SMimi Zohar /* Initialize hash digest to 0's in case of failure */ 245f3cc6b25SMimi Zohar memset(&hash.digest, 0, sizeof(hash.digest)); 246f3cc6b25SMimi Zohar 247f3cc6b25SMimi Zohar if (buf) 248f3cc6b25SMimi Zohar result = ima_calc_buffer_hash(buf, size, &hash.hdr); 249f3cc6b25SMimi Zohar else 250f3cc6b25SMimi Zohar result = ima_calc_file_hash(file, &hash.hdr); 251f3cc6b25SMimi Zohar 252f3cc6b25SMimi Zohar if (result && result != -EBADF && result != -EINVAL) 253f3cc6b25SMimi Zohar goto out; 254f3cc6b25SMimi Zohar 255f3cc6b25SMimi Zohar length = sizeof(hash.hdr) + hash.hdr.length; 256f3cc6b25SMimi Zohar tmpbuf = krealloc(iint->ima_hash, length, GFP_NOFS); 257f3cc6b25SMimi Zohar if (!tmpbuf) { 258f3cc6b25SMimi Zohar result = -ENOMEM; 259f9b2a735SMimi Zohar goto out; 260f9b2a735SMimi Zohar } 261f9b2a735SMimi Zohar 262a35c3fb6SDmitry Kasatkin iint->ima_hash = tmpbuf; 263a35c3fb6SDmitry Kasatkin memcpy(iint->ima_hash, &hash, length); 2643323eec9SMimi Zohar iint->version = i_version; 265f3cc6b25SMimi Zohar 266f3cc6b25SMimi Zohar /* Possibly temporary failure due to type of read (eg. O_DIRECT) */ 267f3cc6b25SMimi Zohar if (!result) 2682fe5d6deSMimi Zohar iint->flags |= IMA_COLLECTED; 269f9b2a735SMimi Zohar out: 270f3cc6b25SMimi Zohar if (result) { 271f3cc6b25SMimi Zohar if (file->f_flags & O_DIRECT) 272f3cc6b25SMimi Zohar audit_cause = "failed(directio)"; 273f3cc6b25SMimi Zohar 2742fe5d6deSMimi Zohar integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, 275f9b2a735SMimi Zohar filename, "collect_data", audit_cause, 2762fe5d6deSMimi Zohar result, 0); 277f3cc6b25SMimi Zohar } 2783323eec9SMimi Zohar return result; 2793323eec9SMimi Zohar } 2803323eec9SMimi Zohar 2813323eec9SMimi Zohar /* 2823323eec9SMimi Zohar * ima_store_measurement - store file measurement 2833323eec9SMimi Zohar * 2843323eec9SMimi Zohar * Create an "ima" template and then store the template by calling 2853323eec9SMimi Zohar * ima_store_template. 2863323eec9SMimi Zohar * 2873323eec9SMimi Zohar * We only get here if the inode has not already been measured, 2883323eec9SMimi Zohar * but the measurement could already exist: 2893323eec9SMimi Zohar * - multiple copies of the same file on either the same or 2903323eec9SMimi Zohar * different filesystems. 2913323eec9SMimi Zohar * - the inode was previously flushed as well as the iint info, 2923323eec9SMimi Zohar * containing the hashing info. 2933323eec9SMimi Zohar * 2943323eec9SMimi Zohar * Must be called with iint->mutex held. 2953323eec9SMimi Zohar */ 296f381c272SMimi Zohar void ima_store_measurement(struct integrity_iint_cache *iint, 297bcbc9b0cSMimi Zohar struct file *file, const unsigned char *filename, 298bcbc9b0cSMimi Zohar struct evm_ima_xattr_data *xattr_value, 2993878d505SThiago Jung Bauermann int xattr_len, const struct modsig *modsig, int pcr, 30019453ce0SMatthew Garrett struct ima_template_desc *template_desc) 3013323eec9SMimi Zohar { 30252a13284SMimi Zohar static const char op[] = "add_template_measure"; 30352a13284SMimi Zohar static const char audit_cause[] = "ENOMEM"; 3043323eec9SMimi Zohar int result = -ENOMEM; 305496ad9aaSAl Viro struct inode *inode = file_inode(file); 3063323eec9SMimi Zohar struct ima_template_entry *entry; 307e038f5f6SThiago Jung Bauermann struct ima_event_data event_data = { .iint = iint, 308e038f5f6SThiago Jung Bauermann .file = file, 309e038f5f6SThiago Jung Bauermann .filename = filename, 310e038f5f6SThiago Jung Bauermann .xattr_value = xattr_value, 3113878d505SThiago Jung Bauermann .xattr_len = xattr_len, 3123878d505SThiago Jung Bauermann .modsig = modsig }; 3133323eec9SMimi Zohar int violation = 0; 3143323eec9SMimi Zohar 315e5092255SThiago Jung Bauermann /* 316e5092255SThiago Jung Bauermann * We still need to store the measurement in the case of MODSIG because 317e5092255SThiago Jung Bauermann * we only have its contents to put in the list at the time of 318e5092255SThiago Jung Bauermann * appraisal, but a file measurement from earlier might already exist in 319e5092255SThiago Jung Bauermann * the measurement list. 320e5092255SThiago Jung Bauermann */ 321e5092255SThiago Jung Bauermann if (iint->measured_pcrs & (0x1 << pcr) && !modsig) 3222fe5d6deSMimi Zohar return; 3232fe5d6deSMimi Zohar 32419453ce0SMatthew Garrett result = ima_alloc_init_template(&event_data, &entry, template_desc); 3257bc5f447SRoberto Sassu if (result < 0) { 3263323eec9SMimi Zohar integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename, 3273323eec9SMimi Zohar op, audit_cause, result, 0); 3283323eec9SMimi Zohar return; 3293323eec9SMimi Zohar } 3303323eec9SMimi Zohar 33114b1da85SEric Richter result = ima_store_template(entry, violation, inode, filename, pcr); 332f3cc6b25SMimi Zohar if ((!result || result == -EEXIST) && !(file->f_flags & O_DIRECT)) { 3333323eec9SMimi Zohar iint->flags |= IMA_MEASURED; 334a422638dSEric Richter iint->measured_pcrs |= (0x1 << pcr); 335a422638dSEric Richter } 33645fae749SRoberto Sassu if (result < 0) 337a7ed7c60SRoberto Sassu ima_free_template_entry(entry); 3383323eec9SMimi Zohar } 339e7c568e0SPeter Moody 340e7c568e0SPeter Moody void ima_audit_measurement(struct integrity_iint_cache *iint, 341e7c568e0SPeter Moody const unsigned char *filename) 342e7c568e0SPeter Moody { 343e7c568e0SPeter Moody struct audit_buffer *ab; 344e456ef88STycho Andersen char *hash; 3455278aa52SMimi Zohar const char *algo_name = hash_algo_name[iint->ima_hash->algo]; 346e7c568e0SPeter Moody int i; 347e7c568e0SPeter Moody 348e7c568e0SPeter Moody if (iint->flags & IMA_AUDITED) 349e7c568e0SPeter Moody return; 350e7c568e0SPeter Moody 351e456ef88STycho Andersen hash = kzalloc((iint->ima_hash->length * 2) + 1, GFP_KERNEL); 352e456ef88STycho Andersen if (!hash) 353e456ef88STycho Andersen return; 354e456ef88STycho Andersen 355a35c3fb6SDmitry Kasatkin for (i = 0; i < iint->ima_hash->length; i++) 356a35c3fb6SDmitry Kasatkin hex_byte_pack(hash + (i * 2), iint->ima_hash->digest[i]); 357e7c568e0SPeter Moody hash[i * 2] = '\0'; 358e7c568e0SPeter Moody 359cdfb6b34SRichard Guy Briggs ab = audit_log_start(audit_context(), GFP_KERNEL, 360e7c568e0SPeter Moody AUDIT_INTEGRITY_RULE); 361e7c568e0SPeter Moody if (!ab) 362e456ef88STycho Andersen goto out; 363e7c568e0SPeter Moody 364e7c568e0SPeter Moody audit_log_format(ab, "file="); 365e7c568e0SPeter Moody audit_log_untrustedstring(ab, filename); 366e456ef88STycho Andersen audit_log_format(ab, " hash=\"%s:%s\"", algo_name, hash); 367e7c568e0SPeter Moody 3682a1fe215SPaul Moore audit_log_task_info(ab); 369e7c568e0SPeter Moody audit_log_end(ab); 370e7c568e0SPeter Moody 371e7c568e0SPeter Moody iint->flags |= IMA_AUDITED; 372e456ef88STycho Andersen out: 373e456ef88STycho Andersen kfree(hash); 374e456ef88STycho Andersen return; 375e7c568e0SPeter Moody } 376ea1046d4SDmitry Kasatkin 377bc15ed66SMimi Zohar /* 378bc15ed66SMimi Zohar * ima_d_path - return a pointer to the full pathname 379bc15ed66SMimi Zohar * 380bc15ed66SMimi Zohar * Attempt to return a pointer to the full pathname for use in the 381bc15ed66SMimi Zohar * IMA measurement list, IMA audit records, and auditing logs. 382bc15ed66SMimi Zohar * 383bc15ed66SMimi Zohar * On failure, return a pointer to a copy of the filename, not dname. 384bc15ed66SMimi Zohar * Returning a pointer to dname, could result in using the pointer 385bc15ed66SMimi Zohar * after the memory has been freed. 386bc15ed66SMimi Zohar */ 387bc15ed66SMimi Zohar const char *ima_d_path(const struct path *path, char **pathbuf, char *namebuf) 388ea1046d4SDmitry Kasatkin { 389ea1046d4SDmitry Kasatkin char *pathname = NULL; 390ea1046d4SDmitry Kasatkin 391456f5fd3SDmitry Kasatkin *pathbuf = __getname(); 392ea1046d4SDmitry Kasatkin if (*pathbuf) { 39317f4bad3SDmitry Kasatkin pathname = d_absolute_path(path, *pathbuf, PATH_MAX); 394ea1046d4SDmitry Kasatkin if (IS_ERR(pathname)) { 395456f5fd3SDmitry Kasatkin __putname(*pathbuf); 396ea1046d4SDmitry Kasatkin *pathbuf = NULL; 397ea1046d4SDmitry Kasatkin pathname = NULL; 398ea1046d4SDmitry Kasatkin } 399ea1046d4SDmitry Kasatkin } 400bc15ed66SMimi Zohar 401bc15ed66SMimi Zohar if (!pathname) { 402bc15ed66SMimi Zohar strlcpy(namebuf, path->dentry->d_name.name, NAME_MAX); 403bc15ed66SMimi Zohar pathname = namebuf; 404bc15ed66SMimi Zohar } 405bc15ed66SMimi Zohar 406bc15ed66SMimi Zohar return pathname; 407ea1046d4SDmitry Kasatkin } 408