Page MenuHomeFreeBSD

libkern: Avoid a one-byte OOB access in strndup()
ClosedPublic

Authored by markj on Dec 5 2025, 9:39 PM.
Tags
None
Referenced Files
Unknown Object (File)
Mon, Jan 19, 11:48 AM
Unknown Object (File)
Thu, Jan 15, 10:34 PM
Unknown Object (File)
Thu, Jan 15, 6:38 PM
Unknown Object (File)
Wed, Jan 14, 12:31 PM
Unknown Object (File)
Wed, Jan 14, 7:05 AM
Unknown Object (File)
Wed, Jan 14, 3:15 AM
Unknown Object (File)
Wed, Jan 7, 7:03 PM
Unknown Object (File)
Dec 19 2025, 10:02 AM
Subscribers

Details

Summary

If the length of the string is maxlen, we would end up copying maxlen+1
bytes, which violates the contract of the function. The result is the
same since that extra byte is overwritten.

Reported by: Kevin Day <kevin@your.org>

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

markj requested review of this revision.Dec 5 2025, 9:39 PM
This revision is now accepted and ready to land.Dec 5 2025, 9:41 PM
sys/libkern/strndup.c
43–44

It would be cleaner to stop adding 1 to len. There is only one use of 'strnlen + 1' value, in malloc. There, you can add 1 explicitly.

Then the code becomes similar (if not identical) to the libc version.

markj marked an inline comment as done.

Take kib's suggestion

This revision now requires review to proceed.Dec 5 2025, 10:06 PM
This revision is now accepted and ready to land.Dec 5 2025, 10:29 PM