[PATCH 0/3] tools/nolibc: fix readdir_r() and the FD_* macros on 64-bit

Danish Khateeb posted 3 patches 11 hours ago
tools/include/nolibc/dirent.h                |  7 +--
tools/include/nolibc/sys/select.h            |  6 +--
tools/testing/selftests/nolibc/nolibc-test.c | 54 ++++++++++++++++++++
3 files changed, 61 insertions(+), 6 deletions(-)
[PATCH 0/3] tools/nolibc: fix readdir_r() and the FD_* macros on 64-bit
Posted by Danish Khateeb 11 hours ago
Two bugs that only show up on 64-bit architectures:

- readdir_r() truncates the directory offset to an int. On ext4 it
  fails within the first few entries of almost every directory (since
  v6.19), or returns the next-to-last entry twice and never the last
  one (v6.15 to v6.18). Patch 1.

- FD_SET(), FD_CLR() and FD_ISSET() build their masks from an int, so
  they use the wrong bits for fds 31-63 of each 64-bit word, and
  select() on fd 40 fails with EBADF. Patch 2, with tests in patch 3.

There is no selftest for patch 1: it needs a directory with large
offsets, and nolibc-test only lists /proc/self, whose offsets are
small. I tested it with a small program that lists a directory with
readdir_r() instead.

The series applies to nolibc/for-next f2212892b6a0 and is independent
of my WIFSIGNALED() series [1]; the two merge cleanly.

Testing, with GCC 16.2, on x86_64 and i386 natively and on arm, arm64
and sparc64 under qemu-user:

- nolibc-test, all tests: no failures before or after the series. The
  only difference is the two new tests, which pass. They also pass
  against glibc (make libc-test).

- On x86_64 without patch 2, fd_set and select_high_fd fail (select()
  returns EBADF). With the default UBSan flags the run stops with
  SIGILL at fd_set instead.

- The readdir_r() program, on ext4, on x86_64, arm64 and sparc64:
  before, it fails on /etc after 4 of 213 entries, /usr/include after
  1 of 1454 and /usr/lib after 2 of 6186. After, it lists as many
  entries as glibc, and on x86_64 the same names. A native i386 build
  was already correct. The arm build failed too, but only because
  under qemu-user the kernel sees a 64-bit process and hands it
  64-bit offsets.

- Built for x86_64 against nolibc from before commit 4ada5679f18d, the
  same program lists a directory holding "..", "code" and "." as "..",
  "code", "code".

[1] https://lore.kernel.org/all/20260923205622.1123270-1-danishkhateeb03@gmail.com/

Danish Khateeb (3):
  tools/nolibc: fix readdir_r() with 64-bit directory offsets
  tools/nolibc: fix FD_SET(), FD_CLR() and FD_ISSET() on 64-bit
  selftests/nolibc: test the FD_* macros and select() on a high fd

 tools/include/nolibc/dirent.h                |  7 +--
 tools/include/nolibc/sys/select.h            |  6 +--
 tools/testing/selftests/nolibc/nolibc-test.c | 54 ++++++++++++++++++++
 3 files changed, 61 insertions(+), 6 deletions(-)


base-commit: f2212892b6a0c2adab438783e9fa8093c90786d7
-- 
2.55.0