Skip to content

Commit 07ad45e

Browse files
Yuuoniyhcahca
authored andcommitted
s390/mm: Fix memory leak in add_marker() when kvrealloc() fails
The function has a memory leak when kvrealloc() fails. The function directly assigns NULL to the markers pointer, losing the reference to the previously allocated memory. This causes kvfree() in pt_dump_init() to free NULL instead of the leaked memory. Fix by: 1. Using kvrealloc() uniformly for all allocations 2. Using a temporary variable to preserve the original pointer until allocation succeeds 3. Removing the error path that sets markers_cnt=0 to keep consistency between markers and markers_cnt Found via static analysis and this is similar to commit 42378a9 ("bpf, verifier: Fix memory leak in array reallocation for stack state") Fixes: d0e7915 ("s390/mm/ptdump: Generate address marker array dynamically") Cc: [email protected] Signed-off-by: Miaoqian Lin <[email protected]> Signed-off-by: Heiko Carstens <[email protected]>
1 parent b45873c commit 07ad45e

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

arch/s390/mm/dump_pagetables.c

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -291,16 +291,14 @@ static int ptdump_cmp(const void *a, const void *b)
291291

292292
static int add_marker(unsigned long start, unsigned long end, const char *name)
293293
{
294-
size_t oldsize, newsize;
295-
296-
oldsize = markers_cnt * sizeof(*markers);
297-
newsize = oldsize + 2 * sizeof(*markers);
298-
if (!oldsize)
299-
markers = kvmalloc(newsize, GFP_KERNEL);
300-
else
301-
markers = kvrealloc(markers, newsize, GFP_KERNEL);
302-
if (!markers)
303-
goto error;
294+
struct addr_marker *new;
295+
size_t newsize;
296+
297+
newsize = (markers_cnt + 2) * sizeof(*markers);
298+
new = kvrealloc(markers, newsize, GFP_KERNEL);
299+
if (!new)
300+
return -ENOMEM;
301+
markers = new;
304302
markers[markers_cnt].is_start = 1;
305303
markers[markers_cnt].start_address = start;
306304
markers[markers_cnt].size = end - start;
@@ -312,9 +310,6 @@ static int add_marker(unsigned long start, unsigned long end, const char *name)
312310
markers[markers_cnt].name = name;
313311
markers_cnt++;
314312
return 0;
315-
error:
316-
markers_cnt = 0;
317-
return -ENOMEM;
318313
}
319314

320315
static int pt_dump_init(void)

0 commit comments

Comments
 (0)