Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf: Clean code with bpf_copy_to_user()

No logic change, use bpf_copy_to_user() to clean code.

Signed-off-by: Tao Chen <chen.dylane@linux.dev>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Link: https://lore.kernel.org/r/20250703163700.677628-1-chen.dylane@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Tao Chen and committed by
Alexei Starovoitov
3413bc0c 192e3aa1

+3 -14
+3 -14
kernel/bpf/syscall.c
··· 5344 5344 5345 5345 if (put_user(zero, ubuf)) 5346 5346 return -EFAULT; 5347 - } else if (input_len >= len + 1) { 5348 - /* ubuf can hold the string with NULL terminator */ 5349 - if (copy_to_user(ubuf, buf, len + 1)) 5350 - return -EFAULT; 5351 5347 } else { 5352 - /* ubuf cannot hold the string with NULL terminator, 5353 - * do a partial copy with NULL terminator. 5354 - */ 5355 - char zero = '\0'; 5356 - 5357 - err = -ENOSPC; 5358 - if (copy_to_user(ubuf, buf, input_len - 1)) 5359 - return -EFAULT; 5360 - if (put_user(zero, ubuf + input_len - 1)) 5361 - return -EFAULT; 5348 + err = bpf_copy_to_user(ubuf, buf, input_len, len); 5349 + if (err == -EFAULT) 5350 + return err; 5362 5351 } 5363 5352 } 5364 5353