The Linux kernel has officially removed the strncpy() API in version 7.2, ending a six-year effort to eliminate the function from the codebase. Developers replaced the deprecated string-copying utility with safer, more predictable alternatives to mitigate long-standing security vulnerabilities and performance overheads, according to recent commits in the official Linux kernel source tree.
Why was strncpy removed from the Linux kernel?
Kernel maintainers removed strncpy() because it was a persistent source of memory-related bugs. According to documentation from the Linux kernel maintainers, the function suffered from counter-intuitive semantics regarding NUL-termination. Additionally, the function often caused performance bottlenecks by performing redundant zero-filling on the destination memory. After approximately 362 commits, the kernel no longer relies on this legacy API.
The effort to scrub
strncpy from the kernel spanned six years, highlighting the massive scale of refactoring required to modernize the world’s most widely used operating system kernel.
What are the approved replacements for strncpy?
Developers are now required to use specific alternatives that offer better memory safety and explicit behavior. The Phoronix report notes that the kernel now mandates the following functions depending on the use case:

- strscpy(): Used for NUL-terminated destinations where safety is a priority.
- strscpy_pad(): Used when NUL-termination is required alongside zero-padding.
- strtomem_pad(): Reserved for fixed-width fields that do not require NUL-termination.
- memcpy_and_pad(): Used for bounded copies that need explicit padding.
- memcpy(): Reserved strictly for operations where the length of the data is already known.
How does this impact kernel security?
Removing strncpy reduces the “attack surface” of the kernel by eliminating a common point of failure for buffer overflows and incomplete string handling. By forcing developers to use explicit, bounds-checked functions, the kernel team prevents errors where developers might forget to terminate a string. This shift follows a broader industry trend toward memory-safe programming practices, even within low-level C-based environments.
strncpy will lead to compilation errors in Linux 7.2 and beyond.
Frequently Asked Questions
Why did it take six years to remove one function?
The Linux kernel is a massive codebase. Replacing a fundamental string function requires careful auditing of thousands of call sites across various CPU architectures to ensure the changes do not introduce regressions or performance issues.
Is memcpy() safer than strncpy()?
memcpy() is not inherently “safer” in terms of logic, but it is more predictable. Unlike strncpy, which has complex and often misunderstood rules about padding and termination, memcpy() performs a straightforward byte copy, making it easier for developers to manage bounds manually.
Will this change affect user-space applications?
No. This change is specific to the Linux kernel API. User-space applications that use the standard C library (glibc) will continue to have access to strncpy, though developers are generally encouraged to use safer alternatives there as well.
Are you a kernel developer or a system administrator managing custom modules? Share your experience with the migration process in the comments below, or subscribe to our newsletter for more deep dives into Linux kernel internals.
