Skip to contentSkip to conversation

bpf: fix error return value in bpf_copy_from_user_dynptr

PATCH Applied Mykyta Yatsenko 2 participants 2 messages May 23, 2025

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

PATCH 1/1 bpf: fix error return value in bpf_copy_from_user_dynptr

From: Mykyta Yatsenko <[email protected]>

On error, copy_from_user returns number of bytes not copied to destination, but current implementation of copy_user_data_sleepable does not handle that correctly and returns it as error value, which may confuse user, expecting meaningful negative error value.

Fixes a498ee7576de ("bpf: Implement dynptr copy kfuncs")
---
 kernel/trace/bpf_trace.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/trace/bpf_trace.c +6 -2
index b0eb721fcfb5..6876985ba527 100644
@@ -3553,8 +3553,12 @@ static __always_inline int copy_user_data_sleepable(void *dst, const void *unsaf
 {
 	int ret;
 
-	if (!tsk) /* Read from the current task */
-		return copy_from_user(dst, (const void __user *)unsafe_src, size);
+	if (!tsk) {/* Read from the current task */
+		ret = copy_from_user(dst, (const void __user *)unsafe_src, size);
+		if (ret)
+			return -EFAULT;
+		return 0;
+	}
 
 	ret = access_process_vm(tsk, (unsigned long)unsafe_src, dst, size, 0);
 	if (ret != size)
Signature · 1 line
2.49.0
#1 lore raw
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 8 lines · On Fri, 23 May 2025 19:17:05 +0100 you wrote:

From: Mykyta Yatsenko <[email protected]>

On error, copy_from_user returns number of bytes not copied to destination, but current implementation of copy_user_data_sleepable does not handle that correctly and returns it as error value, which may confuse user, expecting meaningful negative error value.

[...]

Here is the summary with links:
- [bpf-next] bpf: fix error return value in bpf_copy_from_user_dynptr

    https://git.kernel.org/bpf/bpf-next/c/079e5c56a5c4

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html