selftests/bpf: Fix memory leak in msg_alloc_iov error path
Subsystems (3)
PATCH 1/1 selftests/bpf: Fix memory leak in msg_alloc_iov error path
In msg_alloc_iov(), when calloc() fails for an individual iov_base allocation, the error path frees all previously allocated iov_base entries but fails to free the iov array itself that was allocated with calloc() at the beginning of the function. This results in a memory leak of the iov array.
Add free(iov) in the unwind_iov error path to ensure proper cleanup of all allocated memory.
--- tools/testing/selftests/bpf/test_sockmap.c | 1 + 1 file changed, 1 insertion(+)
tools/testing/selftests/bpf/test_sockmap.c +1 -0
@@ -436,6 +436,7 @@ static int msg_alloc_iov(struct msghdr *msg, unwind_iov:for (i--; i >=0 ; i--)free (msg->msg_iov[i].iov_base); +free (iov);return -ENOMEM ; }
Signature · 1 line
2.54.0
[email protected]
AI reviewIs the Fixes: tag pointing at the right commit?
Quoted 8 lines
selftests/bpf: Fix memory leak in
msg_alloc_ioverror pathIn
msg_alloc_iov(), whencalloc()fails for an individualiov_baseallocation, the error path frees all previously allocatediov_baseentries but fails to free the iov array itself that was allocated withcalloc()at the beginning of the function. This results in a memory leak of the iov array.
Quoted 5 lines
Add free(iov) in the
unwind_ioverror path to ensure proper cleanup of all allocated memory.Fixes 16962b2404ac ("bpf: sockmap, add selftests")Signed-off-by Malaya Kumar Rout <[email protected]>
Is the Fixes: tag pointing at the right commit?
In 16962b2404ac ("bpf: sockmap, add selftests") the iov allocation lived inside msg_loop(), and its out_errno error path already freed the iov array with free(iov). There was no msg_alloc_iov() helper and no unwind_iov label at that point.
The leaking unwind_iov path was introduced later, when the allocation was refactored into the new msg_alloc_iov() helper whose unwind_iov label frees each iov_base entry but omits free(iov). Would this be a better match?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/28704662518
PATCH v2 1/1 selftests/bpf: Fix memory leak in msg_alloc_iov error path
In msg_alloc_iov(), when calloc() fails for an individual iov_base allocation, the error path frees all previously allocated iov_base entries but fails to free the iov array itself that was allocated with calloc() at the beginning of the function. This results in a memory leak of the iov array.
Add free(iov) in the unwind_iov error path to ensure proper cleanup of all allocated memory.
--- tools/testing/selftests/bpf/test_sockmap.c | 1 + 1 file changed, 1 insertion(+)
tools/testing/selftests/bpf/test_sockmap.c +1 -0
@@ -436,6 +436,7 @@ static int msg_alloc_iov(struct msghdr *msg, unwind_iov:for (i--; i >=0 ; i--)free (msg->msg_iov[i].iov_base); +free (iov);return -ENOMEM ; }
Signature · 1 line
2.54.0
Quoted 11 lines · Malaya Kumar Rout
In
msg_alloc_iov(), whencalloc()fails for an individualiov_baseallocation, the error path frees all previously allocatediov_baseentries but fails to free the iov array itself that was allocated withcalloc()at the beginning of the function. This results in a memory leak of the iov array.Add free(iov) in the
unwind_ioverror path to ensure proper cleanup of all allocated memory.Fixes 753fb2ee0934 ("bpf: sockmap, add msg_peek tests to test_sockmap")Signed-off-by Malaya Kumar Rout <[email protected]>
Quoted 16 lines
--- tools/testing/selftests/bpf/test_sockmap.c | 1 + 1 file changed, 1 insertion(+)tools/testing/selftests/bpf/test_sockmap.c +1
-0@@ -436,6 +436,7 @@ static int msg_alloc_iov(struct msghdr *msg, unwind_iov:for (i--; i >=0 ; i--)free (msg->msg_iov[i].iov_base); +free (iov);return -ENOMEM ; }
This patch was applied to bpf/bpf-next.git (master) by Kumar Kartikeya Dwivedi <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Kumar Kartikeya Dwivedi <[email protected]>:
Quoted 10 lines · On Sat, 4 Jul 2026 17:59:35 +0530 you wrote:
In
msg_alloc_iov(), whencalloc()fails for an individualiov_baseallocation, the error path frees all previously allocatediov_baseentries but fails to free the iov array itself that was allocated withcalloc()at the beginning of the function. This results in a memory leak of the iov array.Add free(iov) in the
unwind_ioverror path to ensure proper cleanup of all allocated memory.[...]
Here is the summary with links:
- [v2] selftests/bpf: Fix memory leak in msg_alloc_iov error path
https://git.kernel.org/bpf/bpf-next/c/07c1b07d3871
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html