Skip to contentSkip to conversation

selftests/bpf: Fix memory leak in msg_alloc_iov error path

PATCH Superseded Malaya Kumar Rout 4 participants 5 messages Jul 4 – Jul 6

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

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.

Fixes 16962b2404ac ("bpf: sockmap, add selftests")
---
 tools/testing/selftests/bpf/test_sockmap.c | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/bpf/test_sockmap.c +1 -0
index ac814eb63edb..3e6be455d158 100644
@@ -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
#1 lore raw
Is the Fixes: tag pointing at the right commit?
Quoted 8 lines

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.

Quoted 5 lines

Add free(iov) in the unwind_iov error path to ensure proper cleanup of all allocated memory.

Fixes 16962b2404ac ("bpf: sockmap, add selftests")

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?

Fixes 753fb2ee0934 ("bpf: sockmap, add msg_peek tests to test_sockmap")

---
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

#2 lore raw

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.

Fixes 753fb2ee0934 ("bpf: sockmap, add msg_peek tests to test_sockmap")
---
 tools/testing/selftests/bpf/test_sockmap.c | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/bpf/test_sockmap.c +1 -0
index ac814eb63edb..3e6be455d158 100644
@@ -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
reviewer Reviewed-by on v2 1/1
#3 lore raw
Quoted 11 lines · Malaya Kumar Rout

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.

Fixes 753fb2ee0934 ("bpf: sockmap, add msg_peek tests to test_sockmap")
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
index ac814eb63edb..3e6be455d158 100644
@@ -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;
 }
 
#4 lore raw
In reply to Malaya Kumar Rout
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(), 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.

[...]

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