[PATCH v2] nss: Fix NUL terminator handling in __nss_readline whitespace strip [BZ #34765]
Avinal Kumar
avinal.xlvii@gmail.com
Tue Nov 3 15:53:05 GMT 2026
__nss_readline strips leading whitespace (via isspace, which includes space,
tab, newline, and other whitespace) by advancing a pointer past the leading
characters, then calling memmove(buf, p, strlen(p)) to shift the content to
the start of the buffer. strlen(p) does not include the NUL terminator, so
the original terminator stays at its old position and bytes between the
shifted content and the old terminator i.e. the un-shifted tail from the
original line, remain visible as part of the returned string.
For example, " nobody:x:...:nologin\n" becomes
"nobody:x:...:nologin\nin\n" after the buggy shift, leaking "in\n" from
the un-shifted tail.
However, in-tree callers pass the buffer to a LINE_PARSER that truncates at
the first newline, masking this bug for typical newline-terminated lines.
It becomes visible when lines lack trailing newlines or through direct
__nss_readline callers.
Copy strlen(p) + 1 bytes to include the NUL terminator. Add a test that
calls __nss_readline directly and verifies the result, including a case
without trailing newline.
This is a 2.32 regression from commit bdee910e880 ("nss: Add __nss_fgetent_r").
Checked on x86_64-linux-gnu.
Co-Authored-By: Ekalabya Ghosh <ekalabya2010@gmail.com>
Signed-off-by: Avinal Kumar <avinal.xlvii@gmail.com>
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
---
Changes from v1:
- fixed author attribution
- updated test to include a public interface
nss/Makefile | 1 +
nss/nss_readline.c | 3 +-
nss/tst-nss-readline-whitespace.c | 80 +++++++++++++++++++++++++++++++
3 files changed, 83 insertions(+), 1 deletion(-)
create mode 100644 nss/tst-nss-readline-whitespace.c
diff --git a/nss/Makefile b/nss/Makefile
index 5a4d5794b3..8574c1f798 100644
--- a/nss/Makefile
+++ b/nss/Makefile
@@ -306,6 +306,7 @@ tests-static = tst-field
tests-internal := \
tst-field \
+ tst-nss-readline-whitespace \
tst-rfc3484 \
tst-rfc3484-2 \
tst-rfc3484-3 \
diff --git a/nss/nss_readline.c b/nss/nss_readline.c
index 08583e9d1b..5482402c2b 100644
--- a/nss/nss_readline.c
+++ b/nss/nss_readline.c
@@ -71,7 +71,8 @@ __nss_readline (FILE *fp, char *buf, size_t len, off64_t *poffset)
/* Skip empty lines and comments. */
continue;
if (p != buf)
- memmove (buf, p, strlen (p));
+ /* Include the NUL terminator while moving the buffer. */
+ memmove (buf, p, strlen (p) + 1);
/* Return line to the caller. */
return 0;
diff --git a/nss/tst-nss-readline-whitespace.c b/nss/tst-nss-readline-whitespace.c
new file mode 100644
index 0000000000..ee07d0ec94
--- /dev/null
+++ b/nss/tst-nss-readline-whitespace.c
@@ -0,0 +1,80 @@
+/* Test __nss_readline leading whitespace stripping (bug 34765).
+ Copyright (C) 2026 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <array_length.h>
+#include <nss_files.h>
+#include <pwd.h>
+#include <stdio.h>
+#include <string.h>
+#include <support/check.h>
+
+struct test_case
+{
+ const char *input;
+ const char *expected;
+ const char *expected_shell;
+};
+
+static const struct test_case tests[] =
+ {
+ { "root:x:0:0:root:/root:/bin/sh\n",
+ "root:x:0:0:root:/root:/bin/sh\n", "/bin/sh" },
+ { " daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin\n",
+ "daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin\n", "/usr/sbin/nologin" },
+ { "\tbin:x:2:2:bin:/bin:/usr/sbin/nologin\n",
+ "bin:x:2:2:bin:/bin:/usr/sbin/nologin\n", "/usr/sbin/nologin" },
+ { " nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin\n",
+ "nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin\n", "/usr/sbin/nologin" },
+ { "\t sync:x:4:65534:sync:/bin:/bin/sync\n",
+ "sync:x:4:65534:sync:/bin:/bin/sync\n", "/bin/sync" },
+ { " root:x:0:0:root:/root:/bin/sh",
+ "root:x:0:0:root:/root:/bin/sh", "/bin/sh" },
+ };
+
+static int
+do_test (void)
+{
+ for (size_t i = 0; i < array_length (tests); ++i)
+ {
+ FILE *fp = fmemopen ((void *) tests[i].input,
+ strlen (tests[i].input), "r");
+ TEST_VERIFY_EXIT (fp != NULL);
+
+ char buf[256];
+ off64_t offset;
+ int ret = __nss_readline (fp, buf, sizeof (buf), &offset);
+ TEST_COMPARE (ret, 0);
+ TEST_COMPARE_STRING (buf, tests[i].expected);
+
+ /* Verify the same line through a public interface. */
+ rewind (fp);
+ struct passwd result;
+ struct passwd *rp;
+ char buffer[1024];
+ ret = fgetpwent_r (fp, &result, buffer, sizeof (buffer), &rp);
+ TEST_COMPARE (ret, 0);
+ TEST_VERIFY (rp != NULL);
+ TEST_COMPARE_STRING (result.pw_shell, tests[i].expected_shell);
+
+ fclose (fp);
+ }
+
+ return 0;
+}
+
+#include <support/test-driver.c>
--
2.56.0
More information about the Libc-alpha
mailing list