Currently, d_real() can't fail. When overlayfs fails to look up the data of a metacopy file in a data-only layer it hands back the overlayfs dentry and callers silently work with the overlayfs inode. That's more problematic than current callers realize.
For trace_uprobe and bpf's uprobe_multi it means uprobe_register() fails with an -EIO that has nothing to do with the actual problem. For IMA it means the stacked change detection is skipped and for IPE the file is evaluated against an inode that has none of the properties of the real file. Teach d_real() to fail and teach all callers to handle errors. This adds two helpers for the callers to switch over to: - d_real_data_inode() returns the inode hosting the data or an ERR_PTR() if that can't be resolved. - d_real_metadata_inode() returns the inode hosting the metadata. It never has to look anything up and so never fails (but no promises for the future). d_real() can't fail yet so this is a pure preparation. d_real_inode() stays around until all callers are converted. No functional changes. Suggested-by: Amir Goldstein <[email protected]> Link: https://lore.kernel.org/all/caoq4uxhgxpbtmz5kfuzr1tqaewiecox7ujxqro91qjjhh9t...@mail.gmail.com Signed-off-by: Christian Brauner (Amutable) <[email protected]> --- include/linux/dcache.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/include/linux/dcache.h b/include/linux/dcache.h index 84b0cfe4ed0e7..674bc50b81827 100644 --- a/include/linux/dcache.h +++ b/include/linux/dcache.h @@ -3,6 +3,7 @@ #define __LINUX_DCACHE_H #include <linux/atomic.h> +#include <linux/err.h> #include <linux/list.h> #include <linux/math.h> #include <linux/rculist.h> @@ -660,6 +661,49 @@ static inline struct inode *d_real_inode(const struct dentry *dentry) return d_inode(d_real((struct dentry *) dentry, D_REAL_DATA)); } +/** + * d_real_data_inode - Return the real inode hosting the data + * @dentry: The dentry to query + * + * If @dentry is a regular file on a union/overlay, then return the + * underlying, real inode that hosts its data. Otherwise return d_inode(). + * + * The union/overlay may have to look up the data first. This can sleep and it + * can fail, e.g., because the data is missing or because its verity digest + * doesn't match. + * + * Return: The real inode hosting the data of @dentry, or an ERR_PTR() if it + * can't be resolved. + */ +static inline struct inode *d_real_data_inode(const struct dentry *dentry) +{ + struct dentry *real; + + /* This usage of d_real() results in const dentry */ + real = d_real((struct dentry *)dentry, D_REAL_DATA); + if (IS_ERR(real)) + return ERR_CAST(real); + return d_inode(real); +} + +/** + * d_real_metadata_inode - Return the real inode hosting the metadata + * @dentry: The dentry to query + * + * If @dentry is a regular file on a union/overlay, then return the + * underlying, real inode that hosts its metadata. Otherwise return d_inode(). + * + * Unlike d_real_data_inode() this never has to look anything up and so it + * never fails. + * + * Return: The real inode hosting the metadata of @dentry. + */ +static inline struct inode *d_real_metadata_inode(const struct dentry *dentry) +{ + /* This usage of d_real() results in const dentry */ + return d_inode(d_real((struct dentry *)dentry, D_REAL_METADATA)); +} + struct name_snapshot { struct qstr name; union shortname_store inline_name; -- 2.53.0
