The inode_setsecctx hook and its security_inode_setsecctx() caller in fs/nfsd operate on a dentry even though both have a struct path available. Pass a const struct path so that the coming conversion of the xattr helpers can be reflected in the implementations; for now SELinux and Smack keep passing path->dentry and &nop_mnt_idmap to __vfs_setxattr_locked().
Also document in lsm_hook_defs.h what a struct path argument may be used for: it can be passed to VFS helpers taking a struct path, but nothing is guaranteed about path->mnt. Signed-off-by: Cai Xinchen <[email protected]> --- fs/nfsd/vfs.c | 2 +- include/linux/lsm_hook_defs.h | 9 ++++++++- include/linux/security.h | 5 +++-- security/security.c | 6 +++--- security/selinux/hooks.c | 7 ++++--- security/smack/smack_lsm.c | 6 +++--- 6 files changed, 22 insertions(+), 13 deletions(-) diff --git a/fs/nfsd/vfs.c b/fs/nfsd/vfs.c index 359277716fe8..1c686b82e70a 100644 --- a/fs/nfsd/vfs.c +++ b/fs/nfsd/vfs.c @@ -626,7 +626,7 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, break; } if (attr->na_seclabel && attr->na_seclabel->len) - attr->na_labelerr = security_inode_setsecctx(dentry, + attr->na_labelerr = security_inode_setsecctx(&path, attr->na_seclabel->data, attr->na_seclabel->len); if (IS_ENABLED(CONFIG_FS_POSIX_ACL) && attr->na_dpacl) { if (!S_ISDIR(inode->i_mode)) diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h index 87d9d06710cb..7d3cd5395365 100644 --- a/include/linux/lsm_hook_defs.h +++ b/include/linux/lsm_hook_defs.h @@ -25,6 +25,12 @@ * #include <linux/lsm_hook_defs.h> * #undef LSM_HOOK * }; + * + * Hooks that take a struct path argument: the VFS only sanctions passing + * the path to VFS helpers that take a struct path (e.g. notify_change()); + * it makes no guarantees with regard to path->mnt, whose layout and + * implementation details are subject to change. Implementations must + * not rely on its properties. */ LSM_HOOK(int, 0, binder_set_context_mgr, const struct cred *mgr) LSM_HOOK(int, 0, binder_transaction, const struct cred *from, @@ -313,7 +319,8 @@ LSM_HOOK(int, 0, secctx_to_secid, const char *secdata, u32 seclen, u32 *secid) LSM_HOOK(void, LSM_RET_VOID, release_secctx, struct lsm_context *cp) LSM_HOOK(void, LSM_RET_VOID, inode_invalidate_secctx, struct inode *inode) LSM_HOOK(int, 0, inode_notifysecctx, struct inode *inode, void *ctx, u32 ctxlen) -LSM_HOOK(int, 0, inode_setsecctx, struct dentry *dentry, void *ctx, u32 ctxlen) +LSM_HOOK(int, 0, inode_setsecctx, const struct path *path, void *ctx, + u32 ctxlen) LSM_HOOK(int, -EOPNOTSUPP, inode_getsecctx, struct inode *inode, struct lsm_context *cp) diff --git a/include/linux/security.h b/include/linux/security.h index 40831fab5470..b9cae538b6fc 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -588,7 +588,7 @@ int security_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid); void security_release_secctx(struct lsm_context *cp); void security_inode_invalidate_secctx(struct inode *inode); int security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen); -int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen); +int security_inode_setsecctx(const struct path *path, void *ctx, u32 ctxlen); int security_inode_getsecctx(struct inode *inode, struct lsm_context *cp); int security_locked_down(enum lockdown_reason what); int lsm_fill_user_ctx(struct lsm_ctx __user *uctx, u32 *uctx_len, @@ -1630,7 +1630,8 @@ static inline int security_inode_notifysecctx(struct inode *inode, void *ctx, u3 { return -EOPNOTSUPP; } -static inline int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) +static inline int security_inode_setsecctx(const struct path *path, void *ctx, + u32 ctxlen) { return -EOPNOTSUPP; } diff --git a/security/security.c b/security/security.c index 4727b8347fc4..7e6a8a2a2ee5 100644 --- a/security/security.c +++ b/security/security.c @@ -4110,7 +4110,7 @@ EXPORT_SYMBOL(security_inode_notifysecctx); /** * security_inode_setsecctx() - Change the security label of an inode - * @dentry: inode + * @path: inode (the VFS makes no guarantees with regard to @path->mnt) * @ctx: secctx * @ctxlen: length of secctx * @@ -4124,9 +4124,9 @@ EXPORT_SYMBOL(security_inode_notifysecctx); * * Return: Returns 0 on success, error on failure. */ -int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) +int security_inode_setsecctx(const struct path *path, void *ctx, u32 ctxlen) { - return call_int_hook(inode_setsecctx, dentry, ctx, ctxlen); + return call_int_hook(inode_setsecctx, path, ctx, ctxlen); } EXPORT_SYMBOL(security_inode_setsecctx); diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index da83f4111bdf..6ecb5f9657e6 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -7148,10 +7148,11 @@ static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen /* * called with inode->i_mutex locked */ -static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) +static int selinux_inode_setsecctx(const struct path *path, void *ctx, + u32 ctxlen) { - return __vfs_setxattr_locked(&nop_mnt_idmap, dentry, XATTR_NAME_SELINUX, - ctx, ctxlen, 0, NULL); + return __vfs_setxattr_locked(&nop_mnt_idmap, path->dentry, + XATTR_NAME_SELINUX, ctx, ctxlen, 0, NULL); } static int selinux_inode_getsecctx(struct inode *inode, struct lsm_context *cp) diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c index 2192cdcabfe2..f2c34e9ea4f8 100644 --- a/security/smack/smack_lsm.c +++ b/security/smack/smack_lsm.c @@ -4982,10 +4982,10 @@ static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) ctxlen, 0); } -static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) +static int smack_inode_setsecctx(const struct path *path, void *ctx, u32 ctxlen) { - return __vfs_setxattr_locked(&nop_mnt_idmap, dentry, XATTR_NAME_SMACK, - ctx, ctxlen, 0, NULL); + return __vfs_setxattr_locked(&nop_mnt_idmap, path->dentry, + XATTR_NAME_SMACK, ctx, ctxlen, 0, NULL); } static int smack_inode_getsecctx(struct inode *inode, struct lsm_context *cp) -- 2.18.0.huawei.25
