d_real() is about to be able to fail for D_REAL_DATA. So switch IMA from d_real_inode() to d_real_data_inode() and deal with the error.
Currently, if overlayfs fails to resolve the data of a file it hands back the overlayfs inode. process_measurement() then sees real_inode == inode, skips the detection of changes in the backing file and returns the cached verdict. That's the wrong default. With metacopy and verity this isn't limited to files nobody opened yet either. A metadata-only copy-up of an open file makes overlayfs validate the verity digest of the data again on the next D_REAL_DATA lookup and that can fail. So if the inode hosting the data can't be resolved in process_measurement() we can't tell whether the data changed. Treat it like a change and clear IMA_DONE_MASK and measured_pcrs so the file is evaluated again. The metadata check for EVM stays independent of that. This is a no-op until d_real() can actually fail. Signed-off-by: Christian Brauner (Amutable) <[email protected]> --- security/integrity/ima/ima_api.c | 17 +++++++++++++---- security/integrity/ima/ima_main.c | 8 +++++--- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/security/integrity/ima/ima_api.c b/security/integrity/ima/ima_api.c index 45701f04ba62f..f8c3437559943 100644 --- a/security/integrity/ima/ima_api.c +++ b/security/integrity/ima/ima_api.c @@ -246,7 +246,7 @@ int ima_collect_measurement(struct ima_iint_cache *iint, struct file *file, { const char *audit_cause = "failed"; struct inode *inode = file_inode(file); - struct inode *real_inode = d_real_inode(file_dentry(file)); + struct inode *real_inode; struct ima_max_digest_data hash; struct ima_digest_data *hash_hdr = container_of(&hash.hdr, struct ima_digest_data, hdr); @@ -268,6 +268,13 @@ int ima_collect_measurement(struct ima_iint_cache *iint, struct file *file, if (iint->flags & IMA_COLLECTED) goto out; + /* + * On stacked filesystems the inode hosting the data may have to be + * looked up and that can fail. Treat it like failing to calculate + * the hash. + */ + real_inode = d_real_data_inode(file_dentry(file)); + /* * Detect file change based on STATX_CHANGE_COOKIE, when supported, * and fallback to detecting file change based on i_version. @@ -280,7 +287,7 @@ int ima_collect_measurement(struct ima_iint_cache *iint, struct file *file, AT_STATX_SYNC_AS_STAT); if (!result && (stat.result_mask & STATX_CHANGE_COOKIE)) i_version = stat.change_cookie; - else if (IS_I_VERSION(real_inode)) + else if (!IS_ERR(real_inode) && IS_I_VERSION(real_inode)) i_version = inode_peek_iversion(real_inode); hash.hdr.algo = algo; @@ -289,7 +296,9 @@ int ima_collect_measurement(struct ima_iint_cache *iint, struct file *file, /* Initialize hash digest to 0's in case of failure */ memset(&hash.digest, 0, sizeof(hash.digest)); - if (iint->flags & IMA_VERITY_REQUIRED) { + if (IS_ERR(real_inode)) { + result = PTR_ERR(real_inode); + } else if (iint->flags & IMA_VERITY_REQUIRED) { if (!ima_get_verity_digest(iint, inode, &hash)) { audit_cause = "no-verity-digest"; result = -ENODATA; @@ -314,7 +323,7 @@ int ima_collect_measurement(struct ima_iint_cache *iint, struct file *file, memcpy(iint->ima_hash, &hash, length); if (real_inode == inode) iint->real_inode.version = i_version; - else + else if (!IS_ERR(real_inode)) integrity_inode_attrs_store(&iint->real_inode, i_version, real_inode); diff --git a/security/integrity/ima/ima_main.c b/security/integrity/ima/ima_main.c index fed80da3ee096..e463cfc1e3f43 100644 --- a/security/integrity/ima/ima_main.c +++ b/security/integrity/ima/ima_main.c @@ -342,12 +342,14 @@ static int process_measurement(struct file *file, const struct cred *cred, /* * On stacked filesystems, detect and re-evaluate file data and - * metadata changes. + * metadata changes. If the inode hosting the data can't be resolved + * we can't tell whether the data changed. Re-evaluate the file then + * and let ima_collect_measurement() fail. */ - real_inode = d_real_inode(file_dentry(file)); + real_inode = d_real_data_inode(file_dentry(file)); if (real_inode != inode && (action & IMA_DO_MASK) && (iint->flags & IMA_DONE_MASK)) { - if (!IS_I_VERSION(real_inode) || + if (IS_ERR(real_inode) || !IS_I_VERSION(real_inode) || integrity_inode_attrs_changed(&iint->real_inode, real_inode)) { iint->flags &= ~IMA_DONE_MASK; -- 2.53.0
