All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ondrej Mosnacek <omosnace@redhat.com>
To: xiujianfeng <xiujianfeng@huawei.com>
Cc: "Paul Moore" <paul@paul-moore.com>,
	"Stephen Smalley" <stephen.smalley.work@gmail.com>,
	"Eric Paris" <eparis@parisplace.org>,
	"Christian Göttsche" <cgzones@googlemail.com>,
	michalorzel.eng@gmail.com, "Austin Kim" <austin.kim@lge.com>,
	"SElinux list" <selinux@vger.kernel.org>,
	"Linux kernel mailing list" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH -next] selinux: Fix memleak in security_read_state_kernel
Date: Tue, 14 Jun 2022 16:57:32 +0200	[thread overview]
Message-ID: <CAFqZXNvb2AD6T6NcubAbbzdbNoU1ThZ_P+5ioG844mnEs_9=xA@mail.gmail.com> (raw)
In-Reply-To: <f7151722-6450-7efd-1e3d-e31245dc3da2@huawei.com>

On Tue, Jun 14, 2022 at 3:35 PM xiujianfeng <xiujianfeng@huawei.com> wrote:
>
>
> 在 2022/6/14 20:57, Ondrej Mosnacek 写道:
> > On Mon, Jun 13, 2022 at 4:02 PM Xiu Jianfeng <xiujianfeng@huawei.com> wrote:
> >> In this function, it directly returns the result of __security_read_policy
> >> without freeing the allocated memory in *data, cause memory leak issue,
> >> so free the memory if __security_read_policy failed.
> >>
> >> Signed-off-by: Xiu Jianfeng <xiujianfeng@huawei.com>
> >> ---
> >>   security/selinux/ss/services.c | 9 ++++++++-
> >>   1 file changed, 8 insertions(+), 1 deletion(-)
> >>
> >> diff --git a/security/selinux/ss/services.c b/security/selinux/ss/services.c
> >> index 69b2734311a6..fe5fcf571c56 100644
> >> --- a/security/selinux/ss/services.c
> >> +++ b/security/selinux/ss/services.c
> >> @@ -4048,6 +4048,7 @@ int security_read_policy(struct selinux_state *state,
> >>   int security_read_state_kernel(struct selinux_state *state,
> >>                                 void **data, size_t *len)
> >>   {
> >> +       int err;
> >>          struct selinux_policy *policy;
> >>
> >>          policy = rcu_dereference_protected(
> >> @@ -4060,5 +4061,11 @@ int security_read_state_kernel(struct selinux_state *state,
> >>          if (!*data)
> >>                  return -ENOMEM;
> >>
> >> -       return __security_read_policy(policy, *data, len);
> >> +       err = __security_read_policy(policy, *data, len);
> >> +       if (err) {
> >> +               vfree(*data);
> >> +               *data = NULL;
> >> +               *len = 0;
> >> +       }
> >> +       return err;
> >>   }
> >> --
> >> 2.17.1
> >>
> > security_read_policy() defined a few lines above has the same pattern
> > (just with vmalloc_user() in place of vmalloc()). Would you like to
> > send another patch to fix that function as well?
> No problem, patch already sent.

Wow, you're fast :) Thanks!

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


      reply	other threads:[~2022-06-14 14:57 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-13 13:59 [PATCH -next] selinux: Fix memleak in security_read_state_kernel Xiu Jianfeng
2022-06-13 20:34 ` Paul Moore
2022-06-14 12:57 ` Ondrej Mosnacek
2022-06-14 13:34   ` xiujianfeng
2022-06-14 14:57     ` Ondrej Mosnacek [this message]

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='CAFqZXNvb2AD6T6NcubAbbzdbNoU1ThZ_P+5ioG844mnEs_9=xA@mail.gmail.com' \
    --to=omosnace@redhat.com \
    --cc=austin.kim@lge.com \
    --cc=cgzones@googlemail.com \
    --cc=eparis@parisplace.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michalorzel.eng@gmail.com \
    --cc=paul@paul-moore.com \
    --cc=selinux@vger.kernel.org \
    --cc=stephen.smalley.work@gmail.com \
    --cc=xiujianfeng@huawei.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.