Re: [PATCH v2 1/6] uaccess: Add copy_from_user_nul helper

From
Fushuai Wang <hidden>
Date
2026-01-12 10:01:19
Also in
lkml
Reply to
Alice Ryhl, 2026-01-12
quoted
From: Fushuai Wang <redacted>

Many places call copy_from_user() to copy a buffer from user space,
and then manually add a NULL terminator to the destination buffer,
e.g.:

	if (copy_from_user(dest, src, len))
		return -EFAULT;
	dest[len] = '\0';

This is repetitive and error-prone. Add a copy_from_user_nul() helper to
simplify such patterns. It copied n bytes from user space to kernel space,
and NUL-terminates the destination buffer.

Signed-off-by: Fushuai Wang <redacted>
Hmm, this function is very very similar to strncpy_from_user(). Should
they be using that instead?

Alice
The strncpy_from_user() is for NUL-terminated strings and stops at the
first NUL in userspace. But copy_from_user_nul() always copies a fixed length
and adds a NUL at the end in kernel space, even if userspace data doesn’t
contain a NUL.

So I think they are for different cases and can’t replace each other.

---
Regards,
WANG
Thread (21 messages) 21 messages, 5 authors, last reply 2026-01-16
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help