TL;DR

Linux kernel version 7.2 has officially removed the strncpy API after six years of development and hundreds of patches. The change aims to improve code safety and performance by replacing it with more reliable functions.

Linux 7.2 has officially eliminated the strncpy API from the kernel after six years of work and 360 patches, concluding a long-standing effort to improve code safety and performance.

The removal of the strncpy API marks the end of a multi-year effort within the Linux kernel community. The function, used for copying up to a specified number of bytes, has been known as a source of bugs due to its counter-intuitive semantics around null termination and redundant zero-filling, which can cause performance issues.

Over the past six years, developers have systematically replaced all uses of strncpy within the kernel, culminating in the patch merge for Linux 7.2. The last remaining per-CPU architecture implementations of strncpy were also eliminated at this time.

In place of strncpy, the Linux kernel now recommends using functions such as strscpy() for null-terminated strings, strscpy_pad() for padded null-terminated strings, strtomem_pad() for fixed-width fields, memcpy_and_pad() for bounded copies with explicit padding, or memcpy() for known-length memory copies, depending on the use case.

Impact of Removing the strncpy API in Linux Kernel

The elimination of the strncpy API from the Linux kernel reduces the risk of bugs and security vulnerabilities associated with its previous usage. It also enhances performance by avoiding unnecessary zero-filling operations. This change reflects a broader effort to modernize and secure Linux kernel code, setting a precedent for replacing legacy functions with safer alternatives.

Amazon

safe string copy functions

As an affiliate, we earn on qualifying purchases.

As an affiliate, we earn on qualifying purchases.

Background on strncpy and Kernel Development

The strncpy function has been part of C standard libraries for decades but has long been criticized for its confusing behavior, especially around null termination. Within the Linux kernel, its use has persisted despite known issues, prompting a multi-year effort to phase it out. The process involved hundreds of patches and was driven by community consensus on improving code safety and efficiency.

The move to remove strncpy aligns with industry best practices for secure coding and has been discussed within kernel development circles for years. The final patch set was accepted into Linux 7.2, marking a significant milestone in kernel hygiene and modernization.

“The removal of strncpy from the Linux kernel is a major step toward safer and more efficient code.”

— an anonymous researcher

Amazon

memory copy and padding tools

As an affiliate, we earn on qualifying purchases.

As an affiliate, we earn on qualifying purchases.

Remaining Questions About the Transition

It is not yet clear how widespread the use of the new recommended functions will be across all kernel modules or if future issues could arise from their implementation. Additionally, the impact on legacy code and compatibility with older systems remains to be fully evaluated.

MATLAB: A Practical Introduction to Programming and Problem Solving

MATLAB: A Practical Introduction to Programming and Problem Solving

As an affiliate, we earn on qualifying purchases.

As an affiliate, we earn on qualifying purchases.

Next Steps for Linux Kernel Developers

Developers will focus on monitoring the adoption of the new functions and ensuring stability across kernel updates. Future efforts may include further modernization of legacy code and addressing any unforeseen issues resulting from the removal of strncpy.

Linux Kernel Development: The Complete Hands-On Guide to Building, Debugging, and Mastering the Linux Operating System from Scratch

Linux Kernel Development: The Complete Hands-On Guide to Building, Debugging, and Mastering the Linux Operating System from Scratch

As an affiliate, we earn on qualifying purchases.

As an affiliate, we earn on qualifying purchases.

Key Questions

Why was the strncpy API removed from Linux?

The strncpy API was removed because it was a persistent source of bugs, had confusing semantics, and caused performance issues due to unnecessary zero-filling. Replacing it with safer, more predictable functions improves code safety and efficiency.

Functions such as strscpy(), strscpy_pad(), strtomem_pad(), memcpy_and_pad(), and memcpy() are recommended depending on the specific use case, to ensure safer string and memory handling.

Will this change affect user-space applications?

No, the change impacts only the Linux kernel code itself. User-space applications are unaffected directly, as they typically use standard C library functions.

When was the last patch set for this removal merged?

The last patches were merged into Linux 7.2, which was recently released, completing a six-year effort.

Are there any known issues resulting from this change?

It is too early to determine if any issues will arise. Developers will monitor the transition and address any problems as they are identified.

Source: Hacker News


You May Also Like

Project Glasswing: An Initial Update

Initial results from Project Glasswing show AI models discovering over 10,000 vulnerabilities in critical software, highlighting a new cybersecurity frontier.

The queue. Why the grid, not the chip, is the binding constraint on AI.

A Thorsten Meyer AI item argues power-grid queues, not chips alone, are limiting AI data center growth.

Meta’s CTO says morale is almost ‘the worst it’s ever been’

Meta’s CTO Andrew Bosworth warns internal staff morale is near historic lows due to layoffs and internal AI initiatives, raising concerns about company culture.

Why people might ditch their smartwatches for something simpler

Growing interest in minimal wearable tech reflects a shift away from feature-heavy smartwatches towards simpler, less distracting alternatives.