From: Ana Bobillo <[email protected]>

Since 2026c, OPENAT_TZDIR uses O_RESOLVE_BENEATH only when
privileged, but tzloadbody still skipped its scan for non-terminal
".." components whenever OPENAT_TZDIR and O_RESOLVE_BENEATH were
both nonzero, a leftover from when openat always used
O_RESOLVE_BENEATH.  So on platforms like FreeBSD that have
O_RESOLVE_BENEATH but lack O_REGULAR, an unprivileged program
compiled with -DOPENAT_TZDIR opened a TZ setting like "../file" or
"/usr/share/zoneinfo/../file" without the regular-file check that
other names outside TZDIR get, and that 2025b gave ".." names.
Merely opening a device or FIFO there could have side effects or
hang.  Similarly, if also compiled with -DSUPPRESS_TZDIR, privileged
programs did not reject such names as they do in other builds.
* localtime.c (tzloadbody): Scan for ".." regardless of
O_RESOLVE_BENEATH.  If privileged, fail only if O_RESOLVE_BENEATH
will not be used.
* NEWS: Mention this.
---
A way to see it: with no writer on a FIFO just outside TZDIR, an
unprivileged tzalloc("../fifo") in a FreeBSD-like build
(-DOPENAT_TZDIR, with O_RESOLVE_BENEATH but no O_REGULAR) blocks in
openat, while tzalloc of the FIFO's absolute name fails with EFTYPE.
With this patch both fail with EFTYPE.

 NEWS        |  7 +++++++
 localtime.c | 30 +++++++++++++++---------------
 2 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/NEWS b/NEWS
index 0b0b1ead..8c5bad50 100644
--- a/NEWS
+++ b/NEWS
@@ -24,6 +24,13 @@ Unreleased, experimental changes
     ordering in TZif data as carefully as they do with 64-bit time_t.
     (Problem reported by Ana Bobillo.)
 
+    On platforms like FreeBSD that have O_RESOLVE_BENEATH but lack
+    O_REGULAR, localtime.c compiled with -DOPENAT_TZDIR now checks
+    that a TZ setting with ".." components names a regular file
+    before opening it in an unprivileged program, as it already did
+    for other settings that might name a file outside TZDIR.
+    This fixes a bug introduced in 2026c.
+
   Changes to documentation and commentary
 
     Documentation and commentary now reflect leap seconds’ demise.
diff --git a/localtime.c b/localtime.c
index 710889fe..2a10dcb3 100644
--- a/localtime.c
+++ b/localtime.c
@@ -972,21 +972,21 @@ tzloadbody(char const *name, struct state *sp, char 
tzloadflags,
        }
 
        if (relname[0] != '/') {
-         if (!OPENAT_TZDIR || !O_RESOLVE_BENEATH) {
-           /* Fail if a relative name contains a non-terminal ".." component,
-              as such a name could read a non-directory outside TZDIR
-              when AT_FDCWD and O_RESOLVE_BENEATH are not available.  */
-           char const *component;
-           for (component = relname; component[0]; component++)
-             if (component[0] == '.' && component[1] == '.'
-                 && component[2] == '/'
-                 && (component == relname || component[-1] == '/')) {
-               if (issetugid())
-                 return ENOTCAPABLE;
-               might_escape = true;
-               break;
-             }
-         }
+         /* Set might_escape if a relative name contains a non-terminal
+            ".." component, as such a name could read a non-directory
+            outside TZDIR.  If privileged, fail unless openat with
+            O_RESOLVE_BENEATH will confine the lookup to TZDIR.  */
+         char const *component;
+         for (component = relname; component[0]; component++)
+           if (component[0] == '.' && component[1] == '.'
+               && component[2] == '/'
+               && (component == relname || component[-1] == '/')) {
+             if (! (OPENAT_TZDIR && !SUPPRESS_TZDIR && O_RESOLVE_BENEATH)
+                 && issetugid())
+               return ENOTCAPABLE;
+             might_escape = true;
+             break;
+           }
 
          if (OPENAT_TZDIR && !SUPPRESS_TZDIR) {
            /* Prefer O_SEARCH or O_PATH if available;

Reply via email to