RC RANDOM CHAOS

Linux kernel deleted strncpy across 360 patches

Linux 7.2 removed strncpy() from the kernel after six years and 362 commits. What the replacements are and how to port out-of-tree code.

· 3 min read
Linux kernel deleted strncpy across 360 patches

Linux 7.2 ships without strncpy(). A merge on Friday removed the API and the last per-architecture implementations, closing out around 362 commits spread across six years of work. The function that C programmers have reached for since forever is now gone from the kernel source entirely.

The case against strncpy() is about its semantics, not any single bug. The kernel maintainers have called it a “persistent source of bugs” because of how it handles NUL termination. strncpy(dest, src, n) copies up to n bytes from src. If src is shorter than n, the remainder of dest is zero-filled. If src is n bytes or longer, dest is left without a trailing NUL. That second case is the trap: you hand the result to anything that expects a C string and it reads past the end of the buffer. The zero-filling in the first case is its own problem, redundant work that zeroes a destination you may be about to overwrite, which matters when the buffer is large and the string is short.

So you get an API that is unsafe when the source is too long and slow when it is too short, with a name that reads like “bounded strcpy” and behaves like neither. Six years and 362 commits is the cost of unwinding that from a codebase the size of the kernel, one caller at a time, because each removal needs someone to work out what the original author actually meant.

The replacement is not a single function, because strncpy() was doing several different jobs badly. The kernel splits them out:

  • strscpy() for NUL-terminated destinations. This is the common case and the default answer.
  • strscpy_pad() when you want a NUL-terminated string and the rest of the buffer zeroed.
  • strtomem_pad() for non-NUL-terminated fixed-width fields, the thing people were actually using strncpy() for when they relied on the padding behaviour.
  • memcpy_and_pad() for bounded copies that need explicit padding.
  • memcpy() when the length is known and you are copying memory, not a string.

The point of the split is that each function has one behaviour and a name that describes it, so the next reader does not have to remember which quadrant of strncpy()’s behaviour a given call site depended on.

If you maintain out-of-tree modules or drivers, this is a build break. Code that calls strncpy() will not compile against 7.2, and there is no shim coming. Porting is mechanical but requires judgement at each site: decide whether you need termination, padding, both, or neither, then pick from the list above. The one substitution to avoid is reaching for strscpy() everywhere out of habit. If the original code fed a fixed-width on-wire or on-disk field that is not NUL-terminated by design, strtomem_pad() is the correct target and strscpy() will change the layout.

For anyone doing kernel security review, the upside is that a whole class of string-handling defect is now impossible to introduce by mistake in mainline. The audit question shifts from “is this strncpy() call terminated correctly” to “did whoever ported this pick the right replacement,” which is a smaller and more tractable question to ask of 362 changes.

Share

Keep Reading

Latest on the Wire

Full wire →

New signal daily · RSS

Stay in the loop

New writing delivered when it's ready. No schedule, no spam.