linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] audit: fix a memory leak bug
@ 2019-04-18 17:39 Wenwen Wang
  2019-04-18 21:52 ` Paul Moore
  0 siblings, 1 reply; 3+ messages in thread
From: Wenwen Wang @ 2019-04-18 17:39 UTC (permalink / raw)
  To: Wenwen Wang
  Cc: Paul Moore, Eric Paris, moderated list:AUDIT SUBSYSTEM, open list

In audit_rule_change(), audit_data_to_entry() is firstly invoked to
translate the payload data to the kernel's rule representation. In
audit_data_to_entry(), depending on the audit field type, an audit tree may
be created in audit_make_tree(), which eventually invokes kmalloc() to
allocate the tree.  Since this tree is a temporary tree, it will be then
freed in the following execution, e.g., audit_add_rule() if the message
type is AUDIT_ADD_RULE or audit_del_rule() if the message type is
AUDIT_DEL_RULE. However, if the message type is neither AUDIT_ADD_RULE nor
AUDIT_DEL_RULE, i.e., the default case of the switch statement, this
temporary tree is not freed.

To fix this issue, free the allocated tree in the default case.

Signed-off-by: Wenwen Wang <wang6495@umn.edu>
---
 kernel/auditfilter.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/kernel/auditfilter.c b/kernel/auditfilter.c
index 63f8b3f..70a34db 100644
--- a/kernel/auditfilter.c
+++ b/kernel/auditfilter.c
@@ -1128,6 +1128,8 @@ int audit_rule_change(int type, int seq, void *data, size_t datasz)
 		audit_log_rule_change("remove_rule", &entry->rule, !err);
 		break;
 	default:
+		if (entry->rule.tree)
+			audit_put_tree(entry->rule.tree);
 		err = -EINVAL;
 		WARN_ON(1);
 	}
-- 
2.7.4


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

* Re: [PATCH] audit: fix a memory leak bug
  2019-04-18 17:39 [PATCH] audit: fix a memory leak bug Wenwen Wang
@ 2019-04-18 21:52 ` Paul Moore
  2019-04-19 14:53   ` Wenwen Wang
  0 siblings, 1 reply; 3+ messages in thread
From: Paul Moore @ 2019-04-18 21:52 UTC (permalink / raw)
  To: Wenwen Wang; +Cc: Eric Paris, moderated list:AUDIT SUBSYSTEM, open list

On Thu, Apr 18, 2019 at 1:39 PM Wenwen Wang <wang6495@umn.edu> wrote:
> In audit_rule_change(), audit_data_to_entry() is firstly invoked to
> translate the payload data to the kernel's rule representation. In
> audit_data_to_entry(), depending on the audit field type, an audit tree may
> be created in audit_make_tree(), which eventually invokes kmalloc() to
> allocate the tree.  Since this tree is a temporary tree, it will be then
> freed in the following execution, e.g., audit_add_rule() if the message
> type is AUDIT_ADD_RULE or audit_del_rule() if the message type is
> AUDIT_DEL_RULE. However, if the message type is neither AUDIT_ADD_RULE nor
> AUDIT_DEL_RULE, i.e., the default case of the switch statement, this
> temporary tree is not freed.
>
> To fix this issue, free the allocated tree in the default case.
>
> Signed-off-by: Wenwen Wang <wang6495@umn.edu>
> ---
>  kernel/auditfilter.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/kernel/auditfilter.c b/kernel/auditfilter.c
> index 63f8b3f..70a34db 100644
> --- a/kernel/auditfilter.c
> +++ b/kernel/auditfilter.c
> @@ -1128,6 +1128,8 @@ int audit_rule_change(int type, int seq, void *data, size_t datasz)
>                 audit_log_rule_change("remove_rule", &entry->rule, !err);
>                 break;
>         default:
> +               if (entry->rule.tree)
> +                       audit_put_tree(entry->rule.tree);
>                 err = -EINVAL;
>                 WARN_ON(1);
>         }

Since there are only two "types" (_ADD_RULE and _DEL_RULE) and the
allocation is only three lines (audit_data_to_entry() + two lines for
error handling), maybe it makes more sense to duplicate the
audit_data_to_entry() call into the individual case statements so we
are only doing the allocations when we have a valid "type"?

-- 
paul moore
www.paul-moore.com

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

* Re: [PATCH] audit: fix a memory leak bug
  2019-04-18 21:52 ` Paul Moore
@ 2019-04-19 14:53   ` Wenwen Wang
  0 siblings, 0 replies; 3+ messages in thread
From: Wenwen Wang @ 2019-04-19 14:53 UTC (permalink / raw)
  To: Paul Moore
  Cc: Eric Paris, moderated list:AUDIT SUBSYSTEM, open list, Wenwen Wang

On Thu, Apr 18, 2019 at 4:52 PM Paul Moore <paul@paul-moore.com> wrote:
>
> On Thu, Apr 18, 2019 at 1:39 PM Wenwen Wang <wang6495@umn.edu> wrote:
> > In audit_rule_change(), audit_data_to_entry() is firstly invoked to
> > translate the payload data to the kernel's rule representation. In
> > audit_data_to_entry(), depending on the audit field type, an audit tree may
> > be created in audit_make_tree(), which eventually invokes kmalloc() to
> > allocate the tree.  Since this tree is a temporary tree, it will be then
> > freed in the following execution, e.g., audit_add_rule() if the message
> > type is AUDIT_ADD_RULE or audit_del_rule() if the message type is
> > AUDIT_DEL_RULE. However, if the message type is neither AUDIT_ADD_RULE nor
> > AUDIT_DEL_RULE, i.e., the default case of the switch statement, this
> > temporary tree is not freed.
> >
> > To fix this issue, free the allocated tree in the default case.
> >
> > Signed-off-by: Wenwen Wang <wang6495@umn.edu>
> > ---
> >  kernel/auditfilter.c | 2 ++
> >  1 file changed, 2 insertions(+)
> >
> > diff --git a/kernel/auditfilter.c b/kernel/auditfilter.c
> > index 63f8b3f..70a34db 100644
> > --- a/kernel/auditfilter.c
> > +++ b/kernel/auditfilter.c
> > @@ -1128,6 +1128,8 @@ int audit_rule_change(int type, int seq, void *data, size_t datasz)
> >                 audit_log_rule_change("remove_rule", &entry->rule, !err);
> >                 break;
> >         default:
> > +               if (entry->rule.tree)
> > +                       audit_put_tree(entry->rule.tree);
> >                 err = -EINVAL;
> >                 WARN_ON(1);
> >         }
>
> Since there are only two "types" (_ADD_RULE and _DEL_RULE) and the
> allocation is only three lines (audit_data_to_entry() + two lines for
> error handling), maybe it makes more sense to duplicate the
> audit_data_to_entry() call into the individual case statements so we
> are only doing the allocations when we have a valid "type"?
>
This sounds reasonable to me. I will rework the patch. Thanks!

Wenwen

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

end of thread, other threads:[~2019-04-19 18:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-18 17:39 [PATCH] audit: fix a memory leak bug Wenwen Wang
2019-04-18 21:52 ` Paul Moore
2019-04-19 14:53   ` Wenwen Wang

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