linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] security/selinux: fix potential memleak in error branch
@ 2021-12-10 12:03 Bernard Zhao
  2021-12-21 20:00 ` Paul Moore
  0 siblings, 1 reply; 2+ messages in thread
From: Bernard Zhao @ 2021-12-10 12:03 UTC (permalink / raw)
  To: Paul Moore, Stephen Smalley, Eric Paris, selinux, linux-kernel
  Cc: Bernard Zhao

This patch try to fix potential memleak in error branch.

Signed-off-by: Bernard Zhao <bernard@vivo.com>

Changes since V1:
*make it to be simpler to do the "(!s)" check before the "(!opts)" check.

Changes since v2:
*add *mnt_opts = NULL after kfree(opt) to avoid double free risk.
---
 security/selinux/hooks.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 62d30c0a30c2..0d018f054dfb 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,10 @@ static int selinux_add_opt(int token, const char *s, void **mnt_opts)
 	}
 	return 0;
 Einval:
+	if (is_alloc_opts) {
+		kfree(opts);
+		*mnt_opts = NULL;
+	}
 	pr_warn(SEL_MOUNT_FAIL_MSG);
 	return -EINVAL;
 }
-- 
2.33.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v3] security/selinux: fix potential memleak in error branch
  2021-12-10 12:03 [PATCH v3] security/selinux: fix potential memleak in error branch Bernard Zhao
@ 2021-12-21 20:00 ` Paul Moore
  0 siblings, 0 replies; 2+ messages in thread
From: Paul Moore @ 2021-12-21 20:00 UTC (permalink / raw)
  To: Bernard Zhao; +Cc: Stephen Smalley, Eric Paris, selinux, linux-kernel

On Fri, Dec 10, 2021 at 7:04 AM Bernard Zhao <bernard@vivo.com> wrote:
>
> This patch try to fix potential memleak in error branch.
>
> Signed-off-by: Bernard Zhao <bernard@vivo.com>
>
> Changes since V1:
> *make it to be simpler to do the "(!s)" check before the "(!opts)" check.
>
> Changes since v2:
> *add *mnt_opts = NULL after kfree(opt) to avoid double free risk.
> ---
>  security/selinux/hooks.c | 12 ++++++++++--
>  1 file changed, 10 insertions(+), 2 deletions(-)

Hi Bernard,

I apologize for the late response, this was lost in my inbox for some
reason.  Regardless, this looks fine to me so I'm merging it into
selinux/next; thanks for your help.

However, Ondrej made a few good suggestions about further improvements
that could be made up at the LSM layer, I think it would be nice if
you could look into that too.

Thanks again for your help.

-- 
paul moore
www.paul-moore.com

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-12-21 20:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-10 12:03 [PATCH v3] security/selinux: fix potential memleak in error branch Bernard Zhao
2021-12-21 20:00 ` Paul Moore

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).