security/selinux: fix potential memleak
PATCH 1/1 security/selinux: fix potential memleak
This patch try to fix potential memleak in function selinux_fs_context_dup`s error branch.
--- security/selinux/hooks.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-)
security/selinux/hooks.c +18 -4
@@ -2856,24 +2856,38 @@ static int selinux_fs_context_dup(struct fs_context *fc,if (src->fscontext) { opts->fscontext =kstrdup (src->fscontext,GFP_KERNEL );if (!opts->fscontext) -return -ENOMEM ; + goto err_fscontext; }if (src->context) { opts->context =kstrdup (src->context,GFP_KERNEL );if (!opts->context) -return -ENOMEM ; + goto err_context; }if (src->rootcontext) { opts->rootcontext =kstrdup (src->rootcontext,GFP_KERNEL );if (!opts->rootcontext) -return -ENOMEM ; + goto err_rootcontext; }if (src->defcontext) { opts->defcontext =kstrdup (src->defcontext,GFP_KERNEL );if (!opts->defcontext) -return -ENOMEM ; + goto err_defcontext; }return 0 ; + +err_defcontext: +if (src->rootcontext) +kfree (opts->rootcontext); +err_rootcontext: +if (src->context) +kfree (opts->context); +err_context: +if (src->fscontext) +kfree (opts->fscontext); +err_fscontext: +kfree (fc->security); + +return -ENOMEM ; }static const struct fs_parameter_spec selinux_fs_parameters[] = {
Signature · 1 line
2.33.1
Paul Moore
maintainerQuoted 44 lines · Bernard Zhao
This patch try to fix potential memleak in function selinux_fs_context_dup`s error branch.
Signed-off-by Bernard Zhao <[email protected]>--- security/selinux/hooks.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-)security/selinux/hooks.c +9
-4@@ -2856,24 +2856,38 @@ static int selinux_fs_context_dup(struct fs_context *fc,if (src->fscontext) { opts->fscontext =kstrdup (src->fscontext,GFP_KERNEL );if (!opts->fscontext) -return -ENOMEM ; + goto err_fscontext; }if (src->context) { opts->context =kstrdup (src->context,GFP_KERNEL );if (!opts->context) -return -ENOMEM ; + goto err_context; }if (src->rootcontext) { opts->rootcontext =kstrdup (src->rootcontext,GFP_KERNEL );if (!opts->rootcontext) -return -ENOMEM ; + goto err_rootcontext; }if (src->defcontext) { opts->defcontext =kstrdup (src->defcontext,GFP_KERNEL );if (!opts->defcontext) -return -ENOMEM ; + goto err_defcontext; }return 0 ; + +err_defcontext: +if (src->rootcontext) +kfree (opts->rootcontext); +err_rootcontext:
security/selinux/hooks.c static int selinux_fs_context_dup(struct fs_context *fc, · line 2891
Quoted 10 lines
security/selinux/hooks.c +9
-0+if (src->context) +kfree (opts->context); +err_context: +if (src->fscontext) +kfree (opts->fscontext); +err_fscontext: +kfree (fc->security); + +return -ENOMEM ; }
Thanks for catching this a providing a patch, however I think the memory cleanup can be made simpler, see the pseudo code below:
static int selinux_fs_context_dup(...)
{
fc->security = kzalloc(...);
if (!fc->security)
return -ENOMEM;
opts = fc->security;
if (src->fscontext) {
opts->fscontext = kstrdup(...);
if (!opts->fscontext)
goto err;
}/* ... */
return 0;
err:
kfree(opts->fscontext);
kfree(opts->context);
kfree(opts->rootcontext);
kfree(opts->defcontext);
kfree(opts);
fc->security = NULL;
return -ENOMEM;
}--
paul moore
www.paul-moore.com
Quoted 44 lines · Paul Moore
Quoted 43 lines · Bernard Zhao
This patch try to fix potential memleak in function selinux_fs_context_dup`s error branch.
Signed-off-by Bernard Zhao <[email protected]>--- security/selinux/hooks.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-)security/selinux/hooks.c +8
-4@@ -2856,24 +2856,38 @@ static int selinux_fs_context_dup(struct fs_context *fc,if (src->fscontext) { opts->fscontext =kstrdup (src->fscontext,GFP_KERNEL );if (!opts->fscontext) -return -ENOMEM ; + goto err_fscontext; }if (src->context) { opts->context =kstrdup (src->context,GFP_KERNEL );if (!opts->context) -return -ENOMEM ; + goto err_context; }if (src->rootcontext) { opts->rootcontext =kstrdup (src->rootcontext,GFP_KERNEL );if (!opts->rootcontext) -return -ENOMEM ; + goto err_rootcontext; }if (src->defcontext) { opts->defcontext =kstrdup (src->defcontext,GFP_KERNEL );if (!opts->defcontext) -return -ENOMEM ; + goto err_defcontext; }return 0 ; + +err_defcontext: +if (src->rootcontext) +kfree (opts->rootcontext);
Quoted 8 lines
security/selinux/hooks.c static int selinux_fs_context_dup(struct fs_context *fc, · line 2888
Quoted 8 lines
security/selinux/hooks.c +8
-0+err_rootcontext: +if (src->context) +kfree (opts->context); +err_context: +if (src->fscontext) +kfree (opts->fscontext); +err_fscontext: +kfree (fc->security);
Also here you need to be careful to not double-free fc->security. (Paul's pseudocode below already correctly resets it to NULL after freeing.)
Quoted 36 lines
security/selinux/hooks.c static int selinux_fs_context_dup(struct fs_context *fc, · line 2891
Quoted 3 lines
security/selinux/hooks.c +2
-0+ +return -ENOMEM ; }Thanks for catching this a providing a patch, however I think the memory cleanup can be made simpler, see the pseudo code below:
static int
selinux_fs_context_dup(...)
{fc->security = kzalloc(...); if (!fc->security) return -ENOMEM; opts = fc->security; if (src->fscontext) { opts->fscontext = kstrdup(...); if (!opts->fscontext) goto err; }/* ... */
return 0;
err:
kfree(opts->fscontext); kfree(opts->context); kfree(opts->rootcontext); kfree(opts->defcontext); kfree(opts); fc->security = NULL; return -ENOMEM; }
Quoted 4 lines
--
paul moore
www.paul-moore.com
--
Ondrej Mosnacek
Software Engineer, Linux Security - SELinux kernel Red Hat, Inc.
A message that is not in the archive (sent off-list or not yet received): [email protected].@PENBRnFaWE52U2NWVmNvbmZmZHFBdWUtbTVVYjExOVB3K3JkU1oyVFhIdC1CRmVWYS1HUUBtYWlsLmdtYWlsLmNvbT4=
-----邮件原件-----
发件人: [email protected] <[email protected]> 代表 Ondrej Mosnacek
发送时间: 2021年12月6日 17:16
收件人: Paul Moore <[email protected]>
抄送: 赵军奎 <[email protected]>; Stephen Smalley <[email protected]>; Eric Paris <[email protected]>; SElinux list <[email protected]>; Linux kernel mailing list <[email protected]> 主题: Re: [PATCH] security/selinux: fix potential memleak
Quoted 48 lines · Paul Moore
Quoted 47 lines · Bernard Zhao
This patch try to fix potential memleak in function selinux_fs_context_dup`s error branch.
Signed-off-by Bernard Zhao <[email protected]>--- security/selinux/hooks.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-)security/selinux/hooks.c +12
-4@@ -2856,24 +2856,38 @@ static int selinux_fs_context_dup(struct fs_context *fc,if (src->fscontext) { opts->fscontext =kstrdup (src->fscontext,GFP_KERNEL );if (!opts->fscontext) -return -ENOMEM ; + goto err_fscontext; }if (src->context) { opts->context =kstrdup (src->context,GFP_KERNEL );if (!opts->context) -return -ENOMEM ; + goto err_context; }if (src->rootcontext) { opts->rootcontext =kstrdup (src->rootcontext,GFP_KERNEL );if (!opts->rootcontext) -return -ENOMEM ; + goto err_rootcontext; }if (src->defcontext) { opts->defcontext =kstrdup (src->defcontext,GFP_KERNEL );if (!opts->defcontext) -return -ENOMEM ; + goto err_defcontext; }return 0 ; + +err_defcontext: +if (src->rootcontext) +kfree (opts->rootcontext); +err_rootcontext: +if (src->context) +kfree (opts->context); +err_context:
Quoted 8 lines
security/selinux/hooks.c static int selinux_fs_context_dup(struct fs_context *fc, · line 2888
Quoted 4 lines
security/selinux/hooks.c +4
-0+if (src->fscontext) +kfree (opts->fscontext); +err_fscontext: +kfree (fc->security);Also here you need to be careful to not double-free
fc->security. (Paul's pseudocode below already correctly resets it to NULL after freeing.)
Hi Ondrej Mosnacek:
Thanks for your comments!
I will modify it and resubmit one patch, thanks!
BR//Bernard
Quoted 35 lines
security/selinux/hooks.c static int selinux_fs_context_dup(struct fs_context *fc, · line 2891
Quoted 3 lines
security/selinux/hooks.c +2
-0+ +return -ENOMEM ; }Thanks for catching this a providing a patch, however I think the memory cleanup can be made simpler, see the pseudo code below:
static int
selinux_fs_context_dup(...){fc->security = kzalloc(...); if (!fc->security) return -ENOMEM; opts = fc->security; if (src->fscontext) { opts->fscontext = kstrdup(...); if (!opts->fscontext) goto err; }/* ... */
return 0;
err:
kfree(opts->fscontext); kfree(opts->context); kfree(opts->rootcontext); kfree(opts->defcontext); kfree(opts); fc->security = NULL; return -ENOMEM; }
Quoted 4 lines
--
paul moore
www.paul-moore.com
--
Ondrej Mosnacek
Software Engineer, Linux Security - SELinux kernel Red Hat, Inc.