On 2026-11-15 07:29, Paul Eggert wrote:
Do you have a bug report against seccomp and/or systemd that we can cite? I 
suppose that at configure time, we could see if it's possible that the bug is 
present, and work around it.

I found a systemd bug report[1] and it seems plausible that the bug will not be 
fixed, which means RestrictSUIDSGID/DynamicUser is busted as it can make 
applications less secure.

Suggested fix: keep the emulation fallback on ENOSYS even when libc
provides openat2, for example by always wrapping it on Linux.
I reluctantly did that in Gnulib by installing the attached patch, as in this 
case it's better to have an insecure openat2 than to have one that never works 
- though the real security problem here is in systemd's 
RestrictSUIDSGID/DynamicUser, not in Gnulib.

[1]: https://github.com/systemd/systemd/issues/44034
From 574314bcd1bbc8b0fdeb7c61f5cc911a2e4a97a3 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Sun, 15 Nov 2026 15:41:05 -0800
Subject: [PATCH] openat2: port to systemd RestrictSUIDSGID=yes

Problem reported by Hakob Chobanyan in:
https://lists.gnu.org/r/bug-gnulib/2026-11/msg00072.html
* lib/fcntl.in.h (openat2): Support REPLACE_OPENAT2.
* m4/fcntl_h.m4 (gl_FCNTL_H_DEFAULTS): Initialize REPLACE_OPENAT2.
* m4/openat2.m4 (gl_FUNC_OPENAT2):
Define REPLACE_OPENAT2=1 on Linux platforms that have openat2.
* modules/fcntl-h (fcntl.h): Replace REPLACE_OPENAT2.
* modules/openat2 (Depends-on, configure.ac):
Also test REPLACE_OPENAT2.
---
 ChangeLog                        | 13 +++++++++++++
 doc/glibc-functions/openat2.texi |  5 +++++
 lib/fcntl.in.h                   | 18 ++++++++++++++++--
 lib/openat2.c                    | 18 +++++++++++++-----
 m4/fcntl_h.m4                    |  3 ++-
 m4/openat2.m4                    | 18 ++++++++++++++++--
 modules/fcntl-h                  |  1 +
 modules/openat2                  | 30 +++++++++++++++---------------
 8 files changed, 81 insertions(+), 25 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 00c662cd59..4fc85b5729 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2026-11-15  Paul Eggert  <[email protected]>
+
+	openat2: port to systemd RestrictSUIDSGID=yes
+	Problem reported by Hakob Chobanyan in:
+	https://lists.gnu.org/r/bug-gnulib/2026-11/msg00072.html
+	* lib/fcntl.in.h (openat2): Support REPLACE_OPENAT2.
+	* m4/fcntl_h.m4 (gl_FCNTL_H_DEFAULTS): Initialize REPLACE_OPENAT2.
+	* m4/openat2.m4 (gl_FUNC_OPENAT2):
+	Define REPLACE_OPENAT2=1 on Linux platforms that have openat2.
+	* modules/fcntl-h (fcntl.h): Replace REPLACE_OPENAT2.
+	* modules/openat2 (Depends-on, configure.ac):
+	Also test REPLACE_OPENAT2.
+
 2026-11-15  Bruno Haible  <[email protected]>
 
 	thread-optim: Improvement for statically linked programs on musl libc.
diff --git a/doc/glibc-functions/openat2.texi b/doc/glibc-functions/openat2.texi
index ec6712f0eb..94a70a0c27 100644
--- a/doc/glibc-functions/openat2.texi
+++ b/doc/glibc-functions/openat2.texi
@@ -12,6 +12,11 @@ openat2
 @item
 This function is missing on most platforms:
 glibc 2.42 on Linux, glibc on non-Linux, all non-glibc platforms.
+@item
+This function fails with @code{ENOSYS} on some platforms when it
+exists and its execution would be benign:
+Linux 7.3 and @command{systemd} 262.2 configured with
+@code{RestrictSUIDSGID=yes}.
 @end itemize
 
 Portability problems not fixed by Gnulib:
diff --git a/lib/fcntl.in.h b/lib/fcntl.in.h
index 5272707492..75e2115a55 100644
--- a/lib/fcntl.in.h
+++ b/lib/fcntl.in.h
@@ -268,15 +268,29 @@ struct open_how
 #  define RESOLVE_CACHED	0x20
 # endif
 
-# if !@HAVE_OPENAT2@
+# if @REPLACE_OPENAT2@
+#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#   undef openat2
+#   define openat2 rpl_openat2
+#  endif
+_GL_FUNCDECL_RPL (openat2, int,
+                  (int fd, char const *file, struct open_how const *how,
+                   size_t size),
+                  _GL_ARG_NONNULL ((2, 3)));
+_GL_CXXALIAS_RPL (openat2, int,
+                  (int fd, char const *file, struct open_how const *how,
+                   size_t size));
+# else
+#  if !@HAVE_OPENAT2@
 _GL_FUNCDECL_SYS (openat2, int,
                   (int fd, char const *file, struct open_how const *how,
                    size_t size),
                   _GL_ARG_NONNULL ((2, 3)));
-# endif
+#  endif
 _GL_CXXALIAS_SYS (openat2, int,
                   (int fd, char const *file, struct open_how const *how,
                    size_t size));
+# endif
 _GL_CXXALIASWARN (openat2);
 #elif defined GNULIB_POSIXCHECK
 # if HAVE_RAW_DECL_OPENAT2
diff --git a/lib/openat2.c b/lib/openat2.c
index e860252654..12c102c481 100644
--- a/lib/openat2.c
+++ b/lib/openat2.c
@@ -525,11 +525,19 @@ openat2 (int dfd, char const *filename,
     if (! (r < 0 && errno == ENOSYS))
       return r;
   }
-
-  /* Keep going, to support the dubious practice of compiling for an
-     older kernel.  The openat2 syscall was introduced in Linux 5.6.
-     Linux 5.4 LTS is EOLed at the end of 2025, so perhaps after that
-     we can simply return the syscall result instead of continuing.  */
+  /* Keep going, because:
+
+     - openat2 fails with ENOSYS when running with the dubious systemd
+       configuration setting RestrictSUIDSGID=yes, a setting that,
+       whatever its other virtues, unfortunately introduces races into
+       openat2-using programs and therefore makes them less secure.
+
+     - Some builds incorrectly target Linux 5.5 and earlier by
+       compiling with kernel headers from Linux 5.6 or later.
+       The openat2 syscall was introduced in Linux 5.6 (2020);
+       for what it's worth Linux 5.4 LTS, the last upstream Linux
+       without openat2, was EOLed in 2025, so this justification
+       for continuing will eventually go away.  */
 #endif
 
   int r;
diff --git a/m4/fcntl_h.m4 b/m4/fcntl_h.m4
index 3a5ddb2219..a208d22d9e 100644
--- a/m4/fcntl_h.m4
+++ b/m4/fcntl_h.m4
@@ -1,5 +1,5 @@
 # fcntl_h.m4
-# serial 22
+# serial 23
 dnl Copyright (C) 2006-2007, 2009-2026 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -72,4 +72,5 @@ AC_DEFUN([gl_FCNTL_H_DEFAULTS]
   REPLACE_FCNTL=0;       AC_SUBST([REPLACE_FCNTL])
   REPLACE_OPEN=0;        AC_SUBST([REPLACE_OPEN])
   REPLACE_OPENAT=0;      AC_SUBST([REPLACE_OPENAT])
+  REPLACE_OPENAT2=0;     AC_SUBST([REPLACE_OPENAT2])
 ])
diff --git a/m4/openat2.m4 b/m4/openat2.m4
index c58276f4c3..e27f55390a 100644
--- a/m4/openat2.m4
+++ b/m4/openat2.m4
@@ -1,5 +1,5 @@
 # openat2.m4
-# serial 2
+# serial 3
 
 dnl Copyright 2025-2026 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
@@ -11,12 +11,26 @@
 
 AC_DEFUN([gl_FUNC_OPENAT2],
 [
+  AC_REQUIRE([AC_CANONICAL_HOST])
   AC_REQUIRE([gl_FCNTL_H_DEFAULTS])
   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
   AC_REQUIRE([gl_FCNTL_O_FLAGS])
   AC_CHECK_FUNCS_ONCE([openat2])
   AS_CASE([$ac_cv_func_openat2],
-    [no], [HAVE_OPENAT2=0])
+    [no], [HAVE_OPENAT2=0],
+    [AC_CACHE_CHECK([whether openat2 works with seccomp-bpf],
+       [gl_cv_openat2_seccomp_compatible],
+       [gl_cv_openat2_seccomp_compatible=yes
+        AS_CASE([$host_os],
+          [linux*], [gl_cv_openat2_seccomp_compatible=no])])
+     AS_CASE([$gl_cv_openat2_seccomp_compatible],
+       [no],
+         [# Replace openat2 in case seccomp-bpf can break openat2, e.g.:
+          # https://github.com/systemd/systemd/issues/44034
+          # If targets never use a seccomp-bpf filter that breaks openat2,
+          # run './configure gl_cv_openat2_seccomp_compatible=yes'
+          # to lessen bloat in apps.
+          REPLACE_OPENAT2=1])])
 ])
 
 # Prerequisites of lib/openat2.c.
diff --git a/modules/fcntl-h b/modules/fcntl-h
index 2e8de83245..bd198ff75f 100644
--- a/modules/fcntl-h
+++ b/modules/fcntl-h
@@ -50,6 +50,7 @@ fcntl.h: fcntl.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H)
 	      -e 's|@''REPLACE_FCNTL''@|$(REPLACE_FCNTL)|g' \
 	      -e 's|@''REPLACE_OPEN''@|$(REPLACE_OPEN)|g' \
 	      -e 's|@''REPLACE_OPENAT''@|$(REPLACE_OPENAT)|g' \
+	      -e 's|@''REPLACE_OPENAT2''@|$(REPLACE_OPENAT2)|g' \
 	      -e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \
 	      -e '/definition of _GL_ARG_NONNULL/r $(ARG_NONNULL_H)' \
 	      -e '/definition of _GL_WARN_ON_USE/r $(WARN_ON_USE_H)' \
diff --git a/modules/openat2 b/modules/openat2
index 6fb5397051..9804799b71 100644
--- a/modules/openat2
+++ b/modules/openat2
@@ -8,25 +8,25 @@ m4/openat2.m4
 Depends-on:
 fcntl-h
 extensions
-bool            [test $HAVE_OPENAT2 = 0]
-close           [test $HAVE_OPENAT2 = 0]
-eloop-threshold [test $HAVE_OPENAT2 = 0]
-errno-h         [test $HAVE_OPENAT2 = 0]
-filename        [test $HAVE_OPENAT2 = 0]
-ialloc          [test $HAVE_OPENAT2 = 0]
-idx             [test $HAVE_OPENAT2 = 0]
-largefile       [test $HAVE_OPENAT2 = 0]
-openat          [test $HAVE_OPENAT2 = 0]
-readlinkat      [test $HAVE_OPENAT2 = 0]
-stdckdint-h     [test $HAVE_OPENAT2 = 0]
-sys_stat-h      [test $HAVE_OPENAT2 = 0]
-unistd-h        [test $HAVE_OPENAT2 = 0]
-verify          [test $HAVE_OPENAT2 = 0]
+bool            [test $HAVE_OPENAT2 = 0 || test $REPLACE_OPENAT2 = 1]
+close           [test $HAVE_OPENAT2 = 0 || test $REPLACE_OPENAT2 = 1]
+eloop-threshold [test $HAVE_OPENAT2 = 0 || test $REPLACE_OPENAT2 = 1]
+errno-h         [test $HAVE_OPENAT2 = 0 || test $REPLACE_OPENAT2 = 1]
+filename        [test $HAVE_OPENAT2 = 0 || test $REPLACE_OPENAT2 = 1]
+ialloc          [test $HAVE_OPENAT2 = 0 || test $REPLACE_OPENAT2 = 1]
+idx             [test $HAVE_OPENAT2 = 0 || test $REPLACE_OPENAT2 = 1]
+largefile       [test $HAVE_OPENAT2 = 0 || test $REPLACE_OPENAT2 = 1]
+openat          [test $HAVE_OPENAT2 = 0 || test $REPLACE_OPENAT2 = 1]
+readlinkat      [test $HAVE_OPENAT2 = 0 || test $REPLACE_OPENAT2 = 1]
+stdckdint-h     [test $HAVE_OPENAT2 = 0 || test $REPLACE_OPENAT2 = 1]
+sys_stat-h      [test $HAVE_OPENAT2 = 0 || test $REPLACE_OPENAT2 = 1]
+unistd-h        [test $HAVE_OPENAT2 = 0 || test $REPLACE_OPENAT2 = 1]
+verify          [test $HAVE_OPENAT2 = 0 || test $REPLACE_OPENAT2 = 1]
 
 configure.ac:
 gl_FUNC_OPENAT2
 gl_CONDITIONAL([GL_COND_OBJ_OPENAT2],
-               [test $HAVE_OPENAT2 = 0])
+               [test $HAVE_OPENAT2 = 0 || test $REPLACE_OPENAT2 = 1])
 AM_COND_IF([GL_COND_OBJ_OPENAT2], [
   gl_PREREQ_OPENAT2
 ])
-- 
2.56.1

Reply via email to