[PATCH 0/6] Consolidate /proc/self/maps parsing and fix pthread_getattr_np stack bounds

Adhemerval Zanella adhemerval.zanella@linaro.org
Tue Sep 15 18:34:44 GMT 2026


The pthread_getattr_np computes the initial thread stack bounds by parsing
/proc/self/maps with stdio, which allocates memory and issues an extra
fstat to size the stream buffer (as Josh Triplett noted in his patch to
use a BUFSIZ buffer [1]).  The fortify %n check has a second, independent
stdio based parser of the same file.

This series moves both users on top of the existing
__libc_procutils_read_file line reader, through a new internal
__libc_procmaps_iterate that hands each mapping range and permissions
to a closure.

On top of that, two fixes for the initial thread computation:

  - When /proc is not available (for instance in a mount namespace
    without procfs), fall back to AT_EXECFN, whose page-aligned end is
    the top of the stack vma.  It is deliberately not the default:
    /proc/self/maps also provides the end of the mapping below the
    stack, which bounds how far the stack can grow when RLIMIT_STACK
    was raised after exec, and AT_EXECFN cannot provide that.  The
    fallback therefore refuses an unlimited RLIMIT_STACK.

  - BZ 34637, reported by Pavel Labath: the kernel does not grow the
    stack into the stack_guard_gap (256 pages by default) above the
    mapping below it, so clamping to the end of that mapping
    over-reports the usable stack by up to the gap, and touching the
    reported bottom faults.  The fix assumes the default gap; a smaller
    configured value only makes the result conservative, a larger one
    is no worse than today.

Checked on x86_64-linux-gnu.

[1] https://sourceware.org/pipermail/libc-alpha/2026-July/179123.html

Adhemerval Zanella (6):
  linux: Add buffer argument and line truncation to
    __libc_procutils_read_file
  linux: Add __libc_procmaps_iterate
  linux: Use __libc_procmaps_iterate on __readonly_area_fallback
  nptl: Use __libc_procmaps_iterate on pthread_getattr_np
  nptl: Fall back to AT_EXECFN on __pthread_main_stack without /proc
  nptl: Account for the stack guard gap on pthread_getattr_np (BZ 34637)

 nptl/Makefile                                 |   1 +
 nptl/pthread_getattr_np.c                     | 236 +++++++++++-------
 nptl/tst-pthread-getattr-guard-gap.c          |  97 +++++++
 sysdeps/unix/sysv/linux/Makefile              |   8 +
 sysdeps/unix/sysv/linux/pidfd_getpid.c        |  23 +-
 sysdeps/unix/sysv/linux/procmaps.c            |  70 ++++++
 sysdeps/unix/sysv/linux/procmaps.h            |  37 +++
 sysdeps/unix/sysv/linux/procutils.c           | 159 +++++++-----
 sysdeps/unix/sysv/linux/procutils.h           |  38 ++-
 .../unix/sysv/linux/readonly-area-fallback.c  | 100 ++++----
 sysdeps/unix/sysv/linux/tst-procutils.c       | 130 ++++++++++
 11 files changed, 665 insertions(+), 234 deletions(-)
 create mode 100644 nptl/tst-pthread-getattr-guard-gap.c
 create mode 100644 sysdeps/unix/sysv/linux/procmaps.c
 create mode 100644 sysdeps/unix/sysv/linux/procmaps.h
 create mode 100644 sysdeps/unix/sysv/linux/tst-procutils.c

-- 
2.53.0



More information about the Libc-alpha mailing list