genetlink: Prevent memory leak when krealloc fail
Subsystems (2)
PATCH 1/1 genetlink: Prevent memory leak when krealloc fail
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
--- net/netlink/genetlink.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
net/netlink/genetlink.c +3 -1
@@ -437,8 +437,10 @@ static int genl_allocate_reserve_groups(int n_groups, int *first_id) }else { new_groups =krealloc (mc_groups, nlen,GFP_KERNEL ); -if (!new_groups) +if (!new_groups) { +kfree (mc_groups);return -ENOMEM ; + } mc_groups = new_groups;for (i =0 ; i <BITS_TO_LONGS (n_groups); i++) mc_groups[mc_groups_longs + i] =0 ;
Signature · 1 line
2.42.0.windows.2
Quoted 4 lines · Kamil Duljas
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.
net/netlink/genetlink.c static int genl_allocate_reserve_groups(int n_groups, int *first_id) · line 443
Quoted 7 lines
net/netlink/genetlink.c +3
-1new_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.
Yes, you're right. I did not think about it. So if we have a static pointer that may be resued, should not restore the pointer as at the beginning?
static unsigned long *mc_groups = &mc_group_start;
At this moment we don't know how much memory is allocated. What do you think about this?
net/netlink/genetlink.c static int genl_allocate_reserve_groups(int n_groups, int *first_id) · line 443
Quoted 8 lines
new_groups= krealloc(mc_groups, nlen,
GFP_KERNEL);net/netlink/genetlink.c +4
-1-if (!new_groups) +if (!new_groups) { +kfree (mc_groups); + mc_groups = &mc_group_start;return -ENOMEM ; + }
sob., 18 lis 2023 o 13:02 Florian Westphal <[email protected]> napisał(a):
Quoted 22 lines
Quoted 4 lines · Kamil Duljas
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-ENOMEMWhy should it be free'd?
mc_groupsis not a local variable.net/netlink/genetlink.c static int genl_allocate_reserve_groups(int n_groups, int *first_id) · line 443
Quoted 7 lines
new_groups= krealloc(mc_groups, nlen,
GFP_KERNEL);net/netlink/genetlink.c +3
-1-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_groupsafter this error path is taken.Existing code looks correct, we can't grow
mc_groupsand return an error.
Signature · 2 lines
Pozdrawiam, Kamil Duljas
Quoted 7 lines · Kamil Duljas
Yes, you're right. I did not think about it. So if we have a static pointer that may be resued, should not restore the pointer as at the beginning?
static unsigned long*mc_groups=&mc_group_start;At this moment we don't know how much memory is allocated. What do you think about this?
We do: mc_groups_longs.
Quoted 8 lines
net/netlink/genetlink.c static int genl_allocate_reserve_groups(int n_groups, int *first_id) · line 443
Quoted 8 lines
new_groups= krealloc(mc_groups, nlen,
GFP_KERNEL);net/netlink/genetlink.c +4
-1-if (!new_groups) +if (!new_groups) { +kfree (mc_groups); + mc_groups = &mc_group_start;return -ENOMEM ; + }
Seems wrong to shrink when we can't grow. Whats the point?