2021-12-06 07:19:18

by 赵军奎

[permalink] [raw]
Subject: [PATCH v2] security/selinux: fix potential memleak in error branch

This patch try to fix potential memleak in error branch.

Signed-off-by: Bernard Zhao <[email protected]>
---
security/selinux/hooks.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 62d30c0a30c2..8dc140399a23 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -983,18 +983,22 @@ static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
static int selinux_add_opt(int token, const char *s, void **mnt_opts)
{
struct selinux_mnt_opts *opts = *mnt_opts;
+ bool is_alloc_opts = false;

if (token == Opt_seclabel) /* eaten and completely ignored */
return 0;

+ if (!s)
+ return -ENOMEM;
+
if (!opts) {
opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
if (!opts)
return -ENOMEM;
*mnt_opts = opts;
+ is_alloc_opts = true;
}
- if (!s)
- return -ENOMEM;
+
switch (token) {
case Opt_context:
if (opts->context || opts->defcontext)
@@ -1019,6 +1023,8 @@ static int selinux_add_opt(int token, const char *s, void **mnt_opts)
}
return 0;
Einval:
+ if (is_alloc_opts)
+ kfree(opts);
pr_warn(SEL_MOUNT_FAIL_MSG);
return -EINVAL;
}
--
2.33.1



2021-12-06 09:13:36

by Ondrej Mosnacek

[permalink] [raw]
Subject: Re: [PATCH v2] security/selinux: fix potential memleak in error branch

On Mon, Dec 6, 2021 at 8:19 AM Bernard Zhao <[email protected]> wrote:
> This patch try to fix potential memleak in error branch.
>
> Signed-off-by: Bernard Zhao <[email protected]>
> ---
> security/selinux/hooks.c | 10 ++++++++--
> 1 file changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index 62d30c0a30c2..8dc140399a23 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -983,18 +983,22 @@ static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
> static int selinux_add_opt(int token, const char *s, void **mnt_opts)
> {
> struct selinux_mnt_opts *opts = *mnt_opts;
> + bool is_alloc_opts = false;
>
> if (token == Opt_seclabel) /* eaten and completely ignored */
> return 0;
>
> + if (!s)
> + return -ENOMEM;
> +
> if (!opts) {
> opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
> if (!opts)
> return -ENOMEM;
> *mnt_opts = opts;
> + is_alloc_opts = true;
> }
> - if (!s)
> - return -ENOMEM;
> +
> switch (token) {
> case Opt_context:
> if (opts->context || opts->defcontext)
> @@ -1019,6 +1023,8 @@ static int selinux_add_opt(int token, const char *s, void **mnt_opts)
> }
> return 0;
> Einval:
> + if (is_alloc_opts)
> + kfree(opts);
> pr_warn(SEL_MOUNT_FAIL_MSG);
> return -EINVAL;
> }
> --
> 2.33.1

The problem is a bit more tricky... As is, this patch will lead to
double frees in some cases. Look at security_sb_eat_lsm_opts()
callers, for example - some of them don't do anything when error is
returned, some call security_free_mnt_opts() on the opts regardless,
some will let it store the opts in fc->security, where
put_fs_context() will eventually call security_free_mnt_opts() on
them.

You need to at least *mnt_opts = NULL after kfree(opts), but it would
be also nice to make the LSM hook callers more consistent in what they
do in the error path and document the fact that *mnt_opts will be NULL
on error in lsm_hooks.h (in case of the sb_eat_lsm_opts hook).

--
Ondrej Mosnacek
Software Engineer, Linux Security - SELinux kernel
Red Hat, Inc.