Re: [PATCH v4 3/20] lsm: Refactor return value of LSM hook inode_getsecurity

Paul Moore
2024-07-19T02:08:03Z

On Jul 11, 2024 Xu Kuohai <xukuohai@huaweicloud.com> wrote:
>
> To be consistent with most LSM hooks, convert the return value of
> hook inode_getsecurity to 0 or a negative error code.
>
> Before:
> - Hook inode_getsecurity returns size of buffer on success or a
> negative error code on failure.
>
> After:
> - Hook inode_getsecurity returns 0 on success or a negative error
> code on failure. An output parameter @len is introduced to hold
> the buffer size on success.
>
> Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
> ---
> fs/xattr.c | 19 ++++++++++---------
> include/linux/lsm_hook_defs.h | 3 ++-
> include/linux/security.h | 12 ++++++------
> security/commoncap.c | 9 ++++++---
> security/security.c | 11 ++++++-----
> security/selinux/hooks.c | 16 ++++++----------
> security/smack/smack_lsm.c | 14 +++++++-------
> 7 files changed, 43 insertions(+), 41 deletions(-)

Aside from Simon's concern over variable types, I saw a few other issues
when looking at this patch (below).

> diff --git a/security/commoncap.c b/security/commoncap.c
> index 17d6188d22cf..ff82e2ab6f8f 100644
> --- a/security/commoncap.c
> +++ b/security/commoncap.c
> @@ -485,7 +485,10 @@ int cap_inode_getsecurity(struct mnt_idmap *idmap,
> }
> out_free:
> kfree(tmpbuf);
> - return size;
> + if (size < 0)
> + return size;
> + *len = size;
> + return 0;
> }

We should do a better job converting cap_inode_getsecurity(), create a
new local variable, e.g. 'int error', and use it to store and return the
error code instead of reusing @size. I understand that what you've done
is easier, but I'd prefer to see it done properly.

> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index 9cd5a8f1f6a3..70792bba24d9 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -3407,7 +3407,7 @@ static int selinux_path_notify(const struct path *path, u64 mask,
> */
> static int selinux_inode_getsecurity(struct mnt_idmap *idmap,
> struct inode *inode, const char *name,
> - void **buffer, bool alloc)
> + bool alloc, void **buffer, u32 *len)
> {
> u32 size;
> int error;
> @@ -3440,14 +3440,14 @@ static int selinux_inode_getsecurity(struct mnt_idmap *idmap,
> &context, &size);
> if (error)
> return error;
> - error = size;
> + *len = size;

Depending on how you choose to resolve the variable type issue, you may
be able to pass @len directly to security_sid_to_context().

> if (alloc) {
> *buffer = context;
> goto out_nofree;
> }
> kfree(context);
> out_nofree:
> - return error;
> + return 0;
> }

--
paul-moore.com