linux-security-module.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Serge E. Hallyn" <serge@hallyn.com>
To: Gaosheng Cui <cuigaosheng1@huawei.com>
Cc: serge@hallyn.com, paul@paul-moore.com, jmorris@namei.org,
	ebiederm@xmission.com, brauner@kernel.org,
	linux-security-module@vger.kernel.org
Subject: Re: [PATCH v2] security: commoncap: fix potential memleak on error path from vfs_getxattr_alloc
Date: Tue, 25 Oct 2022 19:11:10 -0500	[thread overview]
Message-ID: <20221026001110.GA12167@mail.hallyn.com> (raw)
In-Reply-To: <20221025133357.2404086-1-cuigaosheng1@huawei.com>

On Tue, Oct 25, 2022 at 09:33:57PM +0800, Gaosheng Cui wrote:
> In cap_inode_getsecurity(), we will use vfs_getxattr_alloc() to
> complete the memory allocation of tmpbuf, if we have completed
> the memory allocation of tmpbuf, but failed to call handler->get(...),
> there will be a memleak in below logic:
> 
>   |-- ret = (int)vfs_getxattr_alloc(mnt_userns, ...)  <-- alloc for tmpbuf
>     |-- value = krealloc(*xattr_value, error + 1, flags)  <-- alloc memory
>     |-- error = handler->get(handler, ...)  <-- if error
>     |-- *xattr_value = value <-- xattr_value is &tmpbuf  <-- memory leak
> 
> So we will try to free(tmpbuf) after vfs_getxattr_alloc() fails to fix it.
> 
> Fixes: 8db6c34f1dbc ("Introduce v3 namespaced file capabilities")

Acked-by: Serge Hallyn <serge@hallyn.com>

> Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com>
> ---
> v2:
> - Update the Fixes tag, from 71bc356f93a1 to 8db6c34f1dbc. Thanks!
>  security/commoncap.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/security/commoncap.c b/security/commoncap.c
> index 5fc8986c3c77..bc751fa5adad 100644
> --- a/security/commoncap.c
> +++ b/security/commoncap.c
> @@ -401,8 +401,10 @@ int cap_inode_getsecurity(struct user_namespace *mnt_userns,
>  				      &tmpbuf, size, GFP_NOFS);
>  	dput(dentry);
>  
> -	if (ret < 0 || !tmpbuf)
> -		return ret;
> +	if (ret < 0 || !tmpbuf) {
> +		size = ret;
> +		goto out_free;
> +	}
>  
>  	fs_ns = inode->i_sb->s_user_ns;
>  	cap = (struct vfs_cap_data *) tmpbuf;
> -- 
> 2.25.1

  reply	other threads:[~2022-10-26  0:11 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-25 13:33 [PATCH v2] security: commoncap: fix potential memleak on error path from vfs_getxattr_alloc Gaosheng Cui
2022-10-26  0:11 ` Serge E. Hallyn [this message]
2022-10-28 10:54 ` Paul Moore

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221026001110.GA12167@mail.hallyn.com \
    --to=serge@hallyn.com \
    --cc=brauner@kernel.org \
    --cc=cuigaosheng1@huawei.com \
    --cc=ebiederm@xmission.com \
    --cc=jmorris@namei.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=paul@paul-moore.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).