The Linux kernel community has completed a six-year effort to remove the longstanding strncpy() function from the kernel codebase, eliminating a source of memory-related bugs and closing off an entire category of potential security vulnerabilities.
The change was merged into the Linux 7.2 development tree on June 20, marking the end of a cleanup project that required approximately 362 individual code changes across the kernel. The update removes not only the remaining uses of strncpy() but also the final architecture-specific implementations of the function.
While the removal may appear to be a routine code maintenance task, kernel developers view it as a significant security and reliability improvement.
The strncpy() function has been part of the C programming language for decades and was commonly used to copy a limited number of bytes from one memory buffer to another. However, its behavior has long been considered problematic for software development.
Potential for Memory Disclosure
One of the function’s most troublesome characteristics is that it does not always append a terminating null character when the source string reaches the specified length limit. So the software that later treats the destination buffer as a conventional C string can continue reading beyond the intended memory boundary. In kernel code, that behavior can create potential for memory disclosure and other difficult-to-detect bugs.
The function also introduces inefficiencies. When the source string is shorter than the specified length, strncpy() automatically fills the remainder of the destination buffer with zeros. Although originally designed for fixed-width data structures used in early Unix systems, this behavior often performs unnecessary memory writes in workloads.
Because both behaviors are part of the function’s intended design rather than implementation flaws, Linux developers concluded that simply discouraging its use was insufficient. The resulting replacement effort required developers to examine each use case individually. There was no single drop-in substitute because different parts of the kernel relied on strncpy() for different purposes.
Purpose-Specific Alternatives
Linux now uses a collection of purpose-specific alternatives that make memory-copying intentions more explicit. The primary replacement is strscpy(), which copies strings while ensuring proper null termination and returns an error code when truncation occurs. For situations that require both null termination and buffer padding, developers can use strscpy_pad().
Other specialized replacements include strtomem_pad() for fixed-width fields that are not intended to be null-terminated, memcpy_and_pad() for bounded copies requiring explicit padding behavior, and standard memcpy() for known-length memory operations.
The removal also builds on earlier kernel hardening work. In 2024, Linux developers completed a separate multi-year effort to eliminate strlcpy(), another string-copying function that presented its own safety concerns. Compared with strlcpy(), the newer strscpy() implementation avoids reading beyond the portion of memory actually being copied, reducing the risk of source-buffer overreads.
Bottom line, the removal of strncpy() creates a permanent safeguard. Because the function no longer exists within the kernel source tree, future contributors cannot accidentally introduce it into new code. Any attempt to use it will fail during compilation rather than relying on code reviewers to catch the issue.
The update is expected to reach end users when Linux 7.2 is released later this year. While the change affects only kernel development and does not impact user-space applications, it is another example of the Linux community’s focus on security hardening through incremental engineering improvements.

