Bug 291359 - x86_64 strncpy/stpncpy reads extra bytes from the source string.
Summary: x86_64 strncpy/stpncpy reads extra bytes from the source string.
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 15.0-RELEASE
Hardware: amd64 Any
: --- Affects Some People
Assignee: Robert Clausecker
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2025-12-03 02:47 UTC by Collin Funk
Modified: 2026-01-04 13:27 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Collin Funk 2025-12-03 02:47:38 UTC
Note that this bug should be filed under the amd64 component. However, the bugzilla seems bugged and does not show the option when filing a bug.

Using this test program to map two pages and then make the second inaccessible, we can see the strncpy and stpncpy reads bytes that it shouldn't:

```
$ cat main.c 
#include <unistd.h>
#include <string.h>
#include <sys/mman.h>
#ifndef STRNCPY
# define STRNCPY strncpy
#endif
int
main (void)
{
  char *fence = NULL;
  long int pagesize = sysconf (_SC_PAGESIZE);
  /* Map two pages.  */
  char *two_pages =
    (char *) mmap (NULL, 2 * pagesize, PROT_READ | PROT_WRITE,
                   MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  /* Make the second page inaccessible.  */
  if (two_pages != (char *)(-1)
      && mprotect (two_pages + pagesize, pagesize, PROT_NONE) == 0)
    fence = two_pages + pagesize;
  if (fence)
    {
      char dest[8];
      dest[0] = 'a';
      dest[1] = 'b';
      dest[2] = 'c';
      dest[3] = 'd';
      dest[4] = 'e';
      dest[5] = 'f';
      dest[6] = 'g';

      *(fence - 3) = '7';
      *(fence - 2) = '2';
      *(fence - 1) = '9';

      if (STRNCPY (dest + 1, fence - 3, 3) != dest + 1)
        return 1;
      if (dest[0] != 'a')
        return 2;
      if (dest[1] != '7' || dest[2] != '2' || dest[3] != '9')
        return 3;
      if (dest[4] != 'e')
        return 4;
    }
  return 0;
}
$ cc main.c && ./a.out 
Segmentation fault         (core dumped) ./a.out
$ cc -DSTRNCPY=stpncpy main.c && ./a.out 
Segmentation fault         (core dumped) ./a.out
$ gdb ./a.out ./a.out.core
[...]
Reading symbols from ./a.out...
[New LWP 100560]
Core was generated by `./a.out'.
Program terminated with signal SIGSEGV, Segmentation fault.
Invalid permissions for mapped object.
#0  ?? () at /usr/src/lib/libc/amd64/string/stpncpy.S:230 from /lib/libc.so.7
230		movdqa		16(%rsi), %xmm0		# load second chunk of input
```

Originally found in Gnulib and reported by Bruno Haible here: https://lists.gnu.org/archive/html/bug-gnulib/2025-12/msg00023.html
Comment 1 Robert Clausecker freebsd_committer freebsd_triage 2025-12-03 11:23:43 UTC
Thank you for the report.  This is my code and I'll go investigate.
Comment 2 Robert Clausecker freebsd_committer freebsd_triage 2025-12-03 12:26:12 UTC
As far as I can see, this is an off-by-one error in processing the runt (1--32 byte input) case.  There may be additional failure conditions, such as when a longer buffer ends right at a page boundary.  Conditions necessary to trigger this:

 - input of 1--16 bytes without a NUL terminator
 - input ends right at a page boundary

The code first assembles in r8d a mask of NUL bytes encountered in the head of the source (that is, the 16 bytes read from src&~15) and in r9d a bit mask of those bits in r8d that refer to bytes inside the string (that is, -1<<(src&15)).  In r10d we have the number of characters in the string, including those added by rounding down the pointer (that is, len + (src&15)).

We then check if the string is 32 bytes or less and go into the runt case.  Then the code does this check to see if we can read another 16 bytes:

        /* 1--32 bytes to copy, bounce through the stack */
.Lrunt: movdqa  %xmm1, bounce+16(%rsp)  # clear out rest of on-stack copy
        bts     %r10d, %r8d             # treat end of buffer as end of string
        and     %r9w, %r8w              # end of string within first buffer?
        jnz     0f                      # if yes, do not inspect second buffer

The code first sets the bit corresponding to the end of the buffer in r8d, pretending the buffer is after all NUL terminated.
Then it checks if a NUL terminator has been found in the head of the string (that is r8w&r9w, note the word-sized and!). If yes, we skip the logic to load and check the second 16 byte chunk.

The problem with this code is that r10d points not to the last character in the buffer, but rather to one character after.  So if the string ends at a 16 byte boundary, the nul byte is injected at 0x10000 and the 16-bit AND fails to find it.  We then read another 16 bytes of input, causing a crash.

Note that contrary to what Jeffrey Walton said in the gnulib bug report, this is not a matter of movdqa vs. movdqu.  The read is guaranteed to be aligned, it just goes too far out of bounds.

We do have a unit test for this function (lib/libc/tests/string/stpncpy_test.c), which I had extended in base 6fa9e7d8737548ef93c573387ce62402c368d486 to cover exactly this sort of issue, but it seems like my extension was incomplete, as it was for memccpy() (see base 61ed5748e4e9c7397fcb2638b442f46ac5c9e7c5).

Getz didn't get around to port this one to AArch64 yet, so that is unaffected.

Will start to work on a fix immediately.
Comment 3 Collin Funk 2025-12-03 16:20:35 UTC
Thanks for the quick investigation!
Comment 4 Getz Mikalsen 2025-12-03 16:33:05 UTC
You can try working around it until the patch is ready by setting the ARCHLEVEL flag to scalar documented in simd(7).
Comment 5 Robert Clausecker freebsd_committer freebsd_triage 2025-12-10 21:19:24 UTC
Unit test updated to diagnose this issue: https://reviews.freebsd.org/D54169
Comment 6 Robert Clausecker freebsd_committer freebsd_triage 2025-12-10 21:21:56 UTC
Bug fix: https://reviews.freebsd.org/D54170

Please test and let me know if the updated code passes your test suite.
Comment 7 Collin Funk 2025-12-11 05:57:40 UTC
I did `make installkernel` and `make installworld` using the GENERIC configuration with the patch applied to my source tree.

After running the tests I see it is using the generic implementation at `/usr/src/lib/libc/string/strncpy.c`.

Can you help me out with the correct config so I can test it? I do not use FreeBSD much, sorry.
Comment 8 Collin Funk 2025-12-11 06:11:02 UTC
Nevermind, I set a break point at `stpncpy` and can see the fixed code is executed.

The failing tests past now. So your changes look good. Thanks!
Comment 9 Robert Clausecker freebsd_committer freebsd_triage 2025-12-11 11:30:12 UTC
(In reply to Collin Funk from comment #8)

Great to hear!  Yeah, strncpy() is just a thin wrapper around stpncpy(), which is the function with the bug.
Comment 10 commit-hook freebsd_committer freebsd_triage 2025-12-14 16:08:46 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=123c086200491819595abc271d360e605288fd18

commit 123c086200491819595abc271d360e605288fd18
Author:     Robert Clausecker <fuz@FreeBSD.org>
AuthorDate: 2025-12-04 16:34:59 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
CommitDate: 2025-12-14 16:05:30 +0000

    libc/tests/string: improve stpncpy() "bounds" unit test

    The test is extended the same way I previously extended the memccpy()
    test to fix what is probably the same kind of bug.

    PR:             291359
    Reported by:    Collin Funk <collin.funk1@gmail.com>
    Reviewed by:    ngie
    Approved by:    markj (mentor)
    Fixes:          6fa9e7d8737548ef93c573387ce62402c368d486 (D42519)
    See also:       61ed5748e4e9c7397fcb2638b442f46ac5c9e7c5 (D46051)
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D54169

    lib/libc/tests/string/stpncpy_test.c: apply ngie's fixes

 lib/libc/tests/string/stpncpy_test.c | 55 +++++++++++++++++++++++++-----------
 1 file changed, 39 insertions(+), 16 deletions(-)
Comment 11 commit-hook freebsd_committer freebsd_triage 2025-12-14 16:08:48 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=66eb78377bf109af1d9e25626bf254b4369436ec

commit 66eb78377bf109af1d9e25626bf254b4369436ec
Author:     Robert Clausecker <fuz@FreeBSD.org>
AuthorDate: 2025-12-10 20:45:18 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
CommitDate: 2025-12-14 16:06:05 +0000

    libc/amd64: fix overread conditions in stpncpy()

    Due to incorrect unit test design, two overread conditions went
    undetected in the amd64 baseline stpncpy() implementation.
    For buffers of 1--16 and 32 bytes that do not contain nul bytes
    and end exactly at a page boundary, the code would incorrectly
    read 16 bytes from the next page, possibly crossing into an
    unmapped page and crashing the program.  If the next page was
    mapped, the code would then proceed with the expected behaviour
    of the stpncpy() function.

    Three changes were made to fix the bug:

     - an off-by-one error is fixed in the code deciding whether to
       enter the runt case or not, entering it for 0<n<=32 bytes
       instead of 0<n<32 bytes as it was before.
     - in the runt case, the logic to skip reading a second 16-byte
       chunk if the buffer ends in the first chunk was fixed to
       account for buffers that end at a 16-byte boundary but do not
       hold a nul byte.
     - in the runt case, the logic to transform the location of the
       end of the input buffer into a bit mask was fixed to allow
       the case of n==32, which was previously impossible due to the
       incorrect logic for entering said case.

    The performance impact should be minimal.

    PR:             291359
    See also:       D54169
    Reported by:    Collin Funk <collin.funk1@gmail.com>
    Reviewed by:    getz
    Approved by:    markj (mentor)
    MFC after:      1 week
    Fixes:          90253d49db09a9b1490c448d05314f3e4bbfa468 (D42519)
    Differential Revision:  https://reviews.freebsd.org/D54170

 lib/libc/amd64/string/stpncpy.S | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
Comment 12 Robert Clausecker freebsd_committer freebsd_triage 2025-12-14 16:09:35 UTC
Thank you for your report.
Comment 13 Collin Funk 2025-12-14 20:53:47 UTC
Thanks for the quick fix. :)
Comment 14 commit-hook freebsd_committer freebsd_triage 2026-01-01 22:22:17 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=54849107681dadaf0aa7bc88d55b7defe3f7826d

commit 54849107681dadaf0aa7bc88d55b7defe3f7826d
Author:     Robert Clausecker <fuz@FreeBSD.org>
AuthorDate: 2025-12-04 16:34:59 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
CommitDate: 2026-01-01 20:57:33 +0000

    libc/tests/string: improve stpncpy() "bounds" unit test

    The test is extended the same way I previously extended the memccpy()
    test to fix what is probably the same kind of bug.

    PR:             291359
    Reported by:    Collin Funk <collin.funk1@gmail.com>
    Reviewed by:    ngie
    Approved by:    markj (mentor)
    Fixes:          6fa9e7d8737548ef93c573387ce62402c368d486 (D42519)
    See also:       61ed5748e4e9c7397fcb2638b442f46ac5c9e7c5 (D46051)
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D54169

    (cherry picked from commit 123c086200491819595abc271d360e605288fd18)
    (cherry picked from commit 8be8642826099368208efb2c1222f2a102cc5b08)

 lib/libc/tests/string/stpncpy_test.c | 55 +++++++++++++++++++++++++-----------
 1 file changed, 39 insertions(+), 16 deletions(-)
Comment 15 commit-hook freebsd_committer freebsd_triage 2026-01-04 13:24:37 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=b49401c0bd4c2af16cee0561f6a9b90e6349193b

commit b49401c0bd4c2af16cee0561f6a9b90e6349193b
Author:     Robert Clausecker <fuz@FreeBSD.org>
AuthorDate: 2025-12-10 20:45:18 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
CommitDate: 2026-01-04 13:22:50 +0000

    libc/amd64: fix overread conditions in stpncpy()

    Due to incorrect unit test design, two overread conditions went
    undetected in the amd64 baseline stpncpy() implementation.
    For buffers of 1--16 and 32 bytes that do not contain nul bytes
    and end exactly at a page boundary, the code would incorrectly
    read 16 bytes from the next page, possibly crossing into an
    unmapped page and crashing the program.  If the next page was
    mapped, the code would then proceed with the expected behaviour
    of the stpncpy() function.

    Three changes were made to fix the bug:

     - an off-by-one error is fixed in the code deciding whether to
       enter the runt case or not, entering it for 0<n<=32 bytes
       instead of 0<n<32 bytes as it was before.
     - in the runt case, the logic to skip reading a second 16-byte
       chunk if the buffer ends in the first chunk was fixed to
       account for buffers that end at a 16-byte boundary but do not
       hold a nul byte.
     - in the runt case, the logic to transform the location of the
       end of the input buffer into a bit mask was fixed to allow
       the case of n==32, which was previously impossible due to the
       incorrect logic for entering said case.

    The performance impact should be minimal.

    PR:             291359
    See also:       D54169
    Reported by:    Collin Funk <collin.funk1@gmail.com>
    Reviewed by:    getz
    Approved by:    markj (mentor)
    MFC after:      1 week
    Fixes:          90253d49db09a9b1490c448d05314f3e4bbfa468 (D42519)
    Differential Revision:  https://reviews.freebsd.org/D54170

    (cherry picked from commit 66eb78377bf109af1d9e25626bf254b4369436ec)

 lib/libc/amd64/string/stpncpy.S | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
Comment 16 commit-hook freebsd_committer freebsd_triage 2026-01-04 13:27:42 UTC
A commit in branch stable/14 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=e2f095779323204e90843b405863c81dc02423d2

commit e2f095779323204e90843b405863c81dc02423d2
Author:     Robert Clausecker <fuz@FreeBSD.org>
AuthorDate: 2025-12-04 16:34:59 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
CommitDate: 2026-01-04 13:25:21 +0000

    libc/tests/string: improve stpncpy() "bounds" unit test

    The test is extended the same way I previously extended the memccpy()
    test to fix what is probably the same kind of bug.

    PR:             291359
    Reported by:    Collin Funk <collin.funk1@gmail.com>
    Reviewed by:    ngie
    Approved by:    markj (mentor)
    Fixes:          6fa9e7d8737548ef93c573387ce62402c368d486 (D42519)
    See also:       61ed5748e4e9c7397fcb2638b442f46ac5c9e7c5 (D46051)
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D54169

    lib/libc/tests/string/stpncpy_test.c: apply ngie's fixes

    (cherry picked from commit 123c086200491819595abc271d360e605288fd18)
    (cherry picked from commit 8be8642826099368208efb2c1222f2a102cc5b08)

 lib/libc/tests/string/stpncpy_test.c | 55 +++++++++++++++++++++++++-----------
 1 file changed, 39 insertions(+), 16 deletions(-)
Comment 17 commit-hook freebsd_committer freebsd_triage 2026-01-04 13:27:44 UTC
A commit in branch stable/14 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=b0dc25c6d378fbab41f86e494d4e7fcbadeb1b74

commit b0dc25c6d378fbab41f86e494d4e7fcbadeb1b74
Author:     Robert Clausecker <fuz@FreeBSD.org>
AuthorDate: 2025-12-10 20:45:18 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
CommitDate: 2026-01-04 13:25:33 +0000

    libc/amd64: fix overread conditions in stpncpy()

    Due to incorrect unit test design, two overread conditions went
    undetected in the amd64 baseline stpncpy() implementation.
    For buffers of 1--16 and 32 bytes that do not contain nul bytes
    and end exactly at a page boundary, the code would incorrectly
    read 16 bytes from the next page, possibly crossing into an
    unmapped page and crashing the program.  If the next page was
    mapped, the code would then proceed with the expected behaviour
    of the stpncpy() function.

    Three changes were made to fix the bug:

     - an off-by-one error is fixed in the code deciding whether to
       enter the runt case or not, entering it for 0<n<=32 bytes
       instead of 0<n<32 bytes as it was before.
     - in the runt case, the logic to skip reading a second 16-byte
       chunk if the buffer ends in the first chunk was fixed to
       account for buffers that end at a 16-byte boundary but do not
       hold a nul byte.
     - in the runt case, the logic to transform the location of the
       end of the input buffer into a bit mask was fixed to allow
       the case of n==32, which was previously impossible due to the
       incorrect logic for entering said case.

    The performance impact should be minimal.

    PR:             291359
    See also:       D54169
    Reported by:    Collin Funk <collin.funk1@gmail.com>
    Reviewed by:    getz
    Approved by:    markj (mentor)
    MFC after:      1 week
    Fixes:          90253d49db09a9b1490c448d05314f3e4bbfa468 (D42519)
    Differential Revision:  https://reviews.freebsd.org/D54170

    (cherry picked from commit 66eb78377bf109af1d9e25626bf254b4369436ec)

 lib/libc/amd64/string/stpncpy.S | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)