[PATCH][v2] audit: fix a memleak caused by auditing load module

Paul Moore paul at paul-moore.com
Wed Mar 6 21:50:58 UTC 2019


On Wed, Mar 6, 2019 at 2:33 AM Li RongQing <lirongqing at baidu.com> wrote:
> module.name will be allocated unconditionally when auditing load
> module, and audit_log_start() can fail with other reasons, or
> audit_log_exit maybe not called, caused module.name is released
>
> so free module.name in audit_free_context and audit exit syscall
>
> unreferenced object 0xffff88af90837d20 (size 8):
>   comm "modprobe", pid 1036, jiffies 4294704867 (age 3069.138s)
>   hex dump (first 8 bytes):
>     69 78 67 62 65 00 ff ff                          ixgbe...
>   backtrace:
>     [<0000000008da28fe>] __audit_log_kern_module+0x33/0x80
>     [<00000000c1491e61>] load_module+0x64f/0x3850
>     [<000000007fc9ae3f>] __do_sys_init_module+0x218/0x250
>     [<0000000000d4a478>] do_syscall_64+0x117/0x400
>     [<000000004924ded8>] entry_SYSCALL_64_after_hwframe+0x49/0xbe
>     [<000000007dc331dd>] 0xffffffffffffffff
>
> Fixes: ca86cad7380e3 ("audit: log module name on init_module")
> Signed-off-by: Zhang Yu <zhangyu31 at baidu.com>
> Signed-off-by: Li RongQing <lirongqing at baidu.com>
> ---
>  kernel/auditsc.c | 10 ++++++++++
>  1 file changed, 10 insertions(+)
>
> diff --git a/kernel/auditsc.c b/kernel/auditsc.c
> index b2d1f043f..07728b07a 100644
> --- a/kernel/auditsc.c
> +++ b/kernel/auditsc.c
> @@ -964,6 +964,9 @@ int audit_alloc(struct task_struct *tsk)
>
>  static inline void audit_free_context(struct audit_context *context)
>  {
> +       if (context->type == AUDIT_KERN_MODULE)
> +               kfree(context->module.name);
> +
>         audit_free_names(context);
>         unroll_tree_refs(context, NULL, 0);
>         free_tree_refs(context);
> @@ -1282,6 +1285,8 @@ static void show_special(struct audit_context *context, int *call_panic)
>                 if (context->module.name) {
>                         audit_log_untrustedstring(ab, context->module.name);
>                         kfree(context->module.name);
> +                       context->module.name = NULL;
> +                       context->type = 0;

I think we can get rid of the kfree() and other cleanup here, yes?
The show_special() function is only going to end up being called from
__audit_free() or __audit_syscall_exit() and both functions appear to
cleanup the audit_context struct correctly (assuming we fixup the
__audit_syscall_exit() below).

>                 } else
>                         audit_log_format(ab, "(null)");
>
> @@ -1583,6 +1588,11 @@ void __audit_syscall_exit(int success, long return_code)
>         if (!list_empty(&context->killed_trees))
>                 audit_kill_trees(&context->killed_trees);
>
> +       if (context->type == AUDIT_KERN_MODULE) {
> +               kfree(context->module.name);
> +               context->module.name = NULL;
> +       }

Since we have to worry about free'ing the memory in places other than
audit_free_context(), let's create a helper function similar to
audit_free_aux() and use that when we need to free module.name.  For
example:

static inline void audit_free_module(struct audit_context *context)
{
  if (context-type == AUDIT_KERN_MODULE) {
    kfree(context->module.name);
    context->module.name = NULL;
  }
}

>         audit_free_names(context);
>         unroll_tree_refs(context, NULL, 0);
>         audit_free_aux(context);
> --
> 2.16.2

-- 
paul moore
www.paul-moore.com




More information about the Linux-audit mailing list