lkml.org 
[lkml]   [2023]   [Nov]   [18]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [PATCH] genetlink: Prevent memory leak when krealloc fail
Kamil Duljas <kamil.duljas@gmail.com> wrote:
> genl_allocate_reserve_groups() allocs new memory in while loop
> but if krealloc fail, the memory allocated by kzalloc is not freed.
> It seems allocated memory is unnecessary when the function
> returns -ENOMEM

Why should it be free'd? mc_groups is not a local variable.

> new_groups = krealloc(mc_groups, nlen,
> GFP_KERNEL);
> - if (!new_groups)
> + if (!new_groups) {
> + kfree(mc_groups);
> return -ENOMEM;
> + }

How did you test this? AFAICS this results in use-after-free for every
access to mc_groups after this error path is taken.

Existing code looks correct, we can't grow mc_groups and return an
error.

\
 
 \ /
  Last update: 2023-11-20 14:04    [W:0.179 / U:0.704 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site