Skip to contentSkip to conversation

Add openat2 support, update to version 1.9.4

PATCH New Mark Hatle 1 participant 6 messages Apr 20 Kbuild

lore
Share & tools
lore
author
#1 lore raw

PATCH 1/5 openat2: Implement openat2 wrapper

In reply to Mark Hatle

From: Mark Hatle <[email protected]>

This wrapper is based on ports/linux/guts/openat.c wrapper.

The flag and mode semantics have been replaced with 'open_how'. The new resolve items should work, but are not processed by pseudo. The input value is simply passed to the openat2 syscall.

---
 ports/linux/openat2/guts/openat2.c | 184 +++++++++++++++++++++++++++++++++++--
 ports/linux/pseudo_wrappers.c      |   3 +
 test/test-syscall.c                |   9 +-
 3 files changed, 182 insertions(+), 14 deletions(-)
ports/linux/openat2/guts/openat2.c +174 -10
index 366fb95..946a4e4 100644
@@ -1,22 +1,186 @@
 /*
- * Copyright (c) 2026 Mark Hatle <[email protected]>; see
- * guts/COPYRIGHT for information.
+ * Copyright (c) 2008-2010, 2013 Wind River Systems
+ * Copyright (c) 2026 Yocto Project
+ * see guts/COPYRIGHT for information.
  *
  * SPDX-License-Identifier: LGPL-2.1-only
  *
  * int openat2(int dirfd, const char *path, const struct open_how *how, size_t size)
  *	int rc = -1;
  */
+	struct stat64 buf;
+	int overly_magic_nonblocking = 0;
+	int existed = 1;
+	int save_errno;
+	sigset_t local_saved_sigmask;
+	struct open_how my_how;
 
-	(void) dirfd;
-	(void) path;
-	(void) how;
-	(void) size;
-	/* for now, let's try just failing out hard, and hope things retry with a
-	 * different syscall.
+	/* Validate parameters */
+	if (!how || size < sizeof(struct open_how)) {
+		errno = EINVAL;
+		return -1;
+	}
+	if (size > sizeof(struct open_how)) {
+		errno = E2BIG;
+		return -1;
+	}
+
+	memcpy(&my_how, how, size);
+
+	/* mask out mode bits appropriately */
+	my_how.mode = my_how.mode & ~pseudo_umask;
+
+#if defined(PSEUDO_NO_REAL_AT_FUNCTIONS) || ! defined(SYS_openat2)
+	if (dirfd != AT_FDCWD) {
+		errno = ENOSYS;
+		return -1;
+	}
+#endif
+
+#ifdef PSEUDO_FORCE_ASYNC
+	/* Yes, I'm aware that every Linux system I've seen has
+	 * DSYNC and RSYNC being the same value as SYNC.
+	 */
+
+	my_how.flags &= ~(O_SYNC
+#ifdef O_DIRECT
+		| O_DIRECT
+#endif
+#ifdef O_DSYNC
+		| O_DSYNC
+#endif
+#ifdef O_RSYNC
+		| O_RSYNC
+#endif
+	);
+#endif
+
+#ifdef O_TMPFILE
+	/* don't handle O_CREAT the same way if O_TMPFILE exists
+	 * and is set.
 	 */
-	errno = ENOSYS;
-	rc = -1;
+	if ((my_how.flags & O_TMPFILE) == O_TMPFILE) {
+		existed = 0;
+	} else
+#endif
+	/* if a creation has been requested, check whether file exists */
+	/* note "else" in #ifdef O_TMPFILE above */
+	if (my_how.flags & O_CREAT) {
+		save_errno = errno;
+#ifdef PSEUDO_NO_REAL_AT_FUNCTIONS
+		if (my_how.flags & O_NOFOLLOW) {
+			rc = real___lxstat64(_STAT_VER, path, &buf);
+		} else {
+			rc = real___xstat64(_STAT_VER, path, &buf);
+		}
+#else
+		rc = real___fxstatat64(_STAT_VER, dirfd, path, &buf, (my_how.flags & O_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0);
+#endif
+		existed = (rc != -1);
+		if (!existed)
+			pseudo_debug(PDBGF_FILE, "openat2_creat: %s -> 0%lld\n", path, my_how.mode);
+		errno = save_errno;
+	}
+
+	/* if a pipe is opened without O_NONBLOCK, for only reading or
+	 * only writing, it can block forever. We need to do extra magic
+	 * in that case...
+	 */
+	if (!(my_how.flags & O_NONBLOCK) && ((my_how.flags & (O_WRONLY | O_RDONLY | O_RDWR)) != O_RDWR)) {
+		save_errno = errno;
+#ifdef PSEUDO_NO_REAL_AT_FUNCTIONS
+		if (my_how.flags & O_NOFOLLOW) {
+			rc = real___lxstat64(_STAT_VER, path, &buf);
+		} else {
+			rc = real___xstat64(_STAT_VER, path, &buf);
+		}
+#else
+		rc = real___fxstatat64(_STAT_VER, dirfd, path, &buf, (my_how.flags & O_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0);
+#endif
+		if (rc != -1 && S_ISFIFO(buf.st_mode)) {
+			overly_magic_nonblocking = 1;
+		}
+	}
+
+	/* this is a horrible special case and i do not know whether it will work */
+	if (overly_magic_nonblocking) {
+		pseudo_droplock();
+		sigprocmask(SIG_SETMASK, &pseudo_saved_sigmask, &local_saved_sigmask);
+	}
+	/* because we are not actually root, secretly mask in 0600 to the
+	 * underlying mode.  The ", 0" is because the only time mode matters
+	 * is if a file is going to be created, in which case it's
+	 * not a directory.
+	 */
+#if defined(PSEUDO_NO_REAL_AT_FUNCTIONS) || ! defined(SYS_openat2)
+	pseudo_debug(PDBGF_SYSCALL, "openat2, calling open.\n");
+	rc = real_open(path, my_how.flags, PSEUDO_FS_MODE(my_how.mode, 0));
+#else
+	/* openat2 in glibc is still rare, so directly call the syscall if required */
+	if (real_openat2) {
+		pseudo_debug(PDBGF_SYSCALL, "openat2, calling openat2.\n");
+		rc = real_openat2(dirfd, path, how, size);
+	} else {
+		pseudo_debug(PDBGF_SYSCALL, "openat2, calling syscall.\n");
+		rc = real_syscall(SYS_openat2, dirfd, path, how, size);
+	}
+#endif
+	if (overly_magic_nonblocking) {
+		save_errno = errno;
+		sigprocmask(SIG_SETMASK, &local_saved_sigmask, NULL);
+		/* well this is a problem. we can't NOT proceed; we may have
+		 * already opened the file! we can't even return up the call
+		 * stack to stuff that's going to try to drop the lock.
+		 */
+		if (pseudo_getlock()) {
+			pseudo_diag("PANIC: after opening a readonly/writeonly FIFO (path '%s', fd %d, errno %d, saved errno %d), could not regain lock. unrecoverable. sorry. bye.\n",
+				path, rc, errno, save_errno);
+			abort();
+		}
+		errno = save_errno;
+	}
+
+	if (rc != -1) {
+		save_errno = errno;
+		int stat_rc;
+#ifdef O_TMPFILE
+		/* in O_TMPFILE case, nothing gets put in the
+		 * database, because there's no directory entries for
+		 * the file yet.
+		 */
+		if ((my_how.flags & O_TMPFILE) == O_TMPFILE) {
+			real_fchmod(rc, PSEUDO_FS_MODE(my_how.mode, 0));
+			errno = save_errno;
+			return rc;
+		}
+#endif
+#ifdef PSEUDO_NO_REAL_AT_FUNCTIONS
+		if (my_how.flags & O_NOFOLLOW) {
+			stat_rc = real___lxstat64(_STAT_VER, path, &buf);
+		} else {
+			stat_rc = real___xstat64(_STAT_VER, path, &buf);
+		}
+#else
+		stat_rc = real___fxstatat64(_STAT_VER, dirfd, path, &buf, (my_how.flags & O_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0);
+#endif
+
+		pseudo_debug(PDBGF_FILE, "openat2(path %s), flags %lld, stat rc %d, stat mode %o\n",
+			path, my_how.flags, stat_rc, buf.st_mode);
+		if (stat_rc != -1) {
+			buf.st_mode = PSEUDO_DB_MODE(buf.st_mode, my_how.mode);
+			if (!existed) {
+				real_fchmod(rc, PSEUDO_FS_MODE(my_how.mode, 0));
+				// file has no path, but has been created
+				pseudo_client_op(OP_CREAT, 0, -1, dirfd, path, &buf);
+			}
+				pseudo_client_op(OP_OPEN, PSEUDO_ACCESS(my_how.flags), rc, dirfd, path, &buf);
+		} else {
+			pseudo_debug(PDBGF_FILE, "openat2 (fd %d, path %d/%s, flags %lld) succeeded, but stat failed (%s).\n",
+				rc, dirfd, path, my_how.flags, strerror(errno));
+			pseudo_client_op(OP_OPEN, PSEUDO_ACCESS(my_how.flags), rc, dirfd, path, 0);
+		}
+		errno = save_errno;
+	}
 
 /*	return rc;
  * }
ports/linux/pseudo_wrappers.c +3 -0
index 300b5a5..b920cb2 100644
@@ -69,6 +69,7 @@ syscall(long number, ...) {
 	/* pseudo and seccomp are incompatible as pseudo uses different syscalls
 	 * so pretend to enable seccomp but really do nothing */
 	if (number == SYS_seccomp) {
+		pseudo_debug(PDBGF_SYSCALL, "syscall, faking seccomp.\n");
 		unsigned long cmd;
 		va_start(ap, number);
 		cmd = va_arg(ap, unsigned long);
@@ -88,6 +89,7 @@ syscall(long number, ...) {
 	 * uses syscall to access openat2() and breaks builds if we don't redirect.
 	 */
 	if (number == SYS_openat2) {
+		pseudo_debug(PDBGF_SYSCALL, "syscall, faking openat2.\n");
 		va_start(ap, number);
 		int dirfd = va_arg(ap, int);
 		const char * path = va_arg(ap, const char *);
@@ -103,6 +105,7 @@ syscall(long number, ...) {
 #ifdef SYS_renameat2
         /* Call out wrapper, expanding the variable arguments first */
 	if (number == SYS_renameat2) {
+		pseudo_debug(PDBGF_SYSCALL, "syscall, faking renameat2.\n");
 		va_start(ap, number);
 		int olddirfd = va_arg(ap, int);
 		const char * oldpath = va_arg(ap, const char *);
test/test-syscall.c +5 -4
index 9031766..170fd31 100644
@@ -72,12 +72,13 @@ int main() {
             printf("openat2: fail: function implemented: %s\n", strerror(errno));
             rc++;
         }
-        else
-            printf("openat2: pass\n");
+        else {
+            printf("openat2: fail: %s", strerror(errno));
+            rc++;
+	}
     }
     else {
-        printf("openat2: fail: function implemented\n");
-        rc++;
+        printf("openat2: pass\n");
     }
 
     close(fd);
Signature · 1 line
1.8.3.1
author
#3 lore raw

PATCH 3/5 test: Add openat2 test cases

In reply to Mark Hatle

From: Mark Hatle <[email protected]>

Add two primary test cases, openat2 via syscall and openat2 via function call. Add two secondary tests calling each of these from a chroot environment.

Since the function may or may not exist in different versions of glibc, this may return skipped if the glibc used for testing does not contain the function.

Similarly if the running kernel does not define the openat2 syscall, the test may also be skipped.

Otherwise the two test cases are virtually identical in nature, but were broken into two different .c files to make it easier to capture the difference between the syscall and function in the future.

Note, we're not checking for the openat2 escaping the chroot. We expect it is able to escape the chroot via relative paths.

AI-Generated: Test cases written using GitHub Copilot (Claude Sonnet 4.6)

---
 test/test-openat2-func-chroot.sh    |  18 +++
 test/test-openat2-func.c            | 274 +++++++++++++++++++++++++++++++++
 test/test-openat2-func.sh           |  18 +++
 test/test-openat2-syscall-chroot.sh |  18 +++
 test/test-openat2-syscall.c         | 299 ++++++++++++++++++++++++++++++++++++
 test/test-openat2-syscall.sh        |  18 +++
 6 files changed, 645 insertions(+)
 create mode 100755 test/test-openat2-func-chroot.sh
 create mode 100644 test/test-openat2-func.c
 create mode 100755 test/test-openat2-func.sh
 create mode 100755 test/test-openat2-syscall-chroot.sh
 create mode 100644 test/test-openat2-syscall.c
 create mode 100755 test/test-openat2-syscall.sh
test/test-openat2-func-chroot.sh new file +18 -0
new file mode 100755
index 0000000..b20b4ff
@@ -0,0 +1,18 @@
+#! /bin/sh
+
+# rc 77 indicates an ENOSYS, so return back we're skipped
+# This usually indicates that glibc does not have the openat2 function
+run_test() {
+	$@
+	rc=$?
+	if [ "$rc" -eq 77 ]; then
+		exit 255
+	fi
+	return "$rc"
+}
+
+# Run with and without ignored paths, matching test-openat coverage.
+run_test chroot ./test ./test-openat2-func || exit $?
+run_test env PSEUDO_IGNORE_PATHS=/ chroot ./test ./test-openat2-func || exit $?
+
+exit 0
test/test-openat2-func.c new file +274 -0

274 lines not on this page · raw message

test/test-openat2-func.sh new file +18 -0
new file mode 100755
index 0000000..727b377
@@ -0,0 +1,18 @@
+#! /bin/sh
+
+# rc 77 indicates an ENOSYS, so return back we're skipped
+# This usually indicates that glibc does not have the openat2 function
+run_test() {
+	$@
+	rc=$?
+	if [ "$rc" -eq 77 ]; then
+		exit 255
+	fi
+	return "$rc"
+}
+
+# Run with and without ignored paths, matching test-openat coverage.
+run_test ./test/test-openat2-func || exit $?
+run_test env PSEUDO_IGNORE_PATHS=/ ./test/test-openat2-func || exit $?
+
+exit 0
test/test-openat2-syscall-chroot.sh new file +18 -0
new file mode 100755
index 0000000..c3aef7a
@@ -0,0 +1,18 @@
+#! /bin/sh
+
+# rc 77 indicates an ENOSYS, so return back we're skipped
+# This shouldn't happen, but the user could disable openat2 syscall emulation
+run_test() {
+	$@
+	rc=$?
+	if [ "$rc" -eq 77 ]; then
+		exit 255
+	fi
+	return "$rc"
+}
+
+# Run with and without ignored paths, matching test-openat coverage.
+run_test chroot ./test ./test-openat2-syscall || exit $?
+run_test env PSEUDO_IGNORE_PATHS=/ chroot ./test ./test-openat2-syscall || exit $?
+
+exit 0
test/test-openat2-syscall.c new file +299 -0

299 lines not on this page · raw message

test/test-openat2-syscall.sh new file +18 -0
new file mode 100755
index 0000000..0a26c0c
@@ -0,0 +1,18 @@
+#! /bin/sh
+
+# rc 77 indicates an ENOSYS, so return back we're skipped
+# This shouldn't happen, but the user could disable openat2 syscall emulation
+run_test() {
+	$@
+	rc=$?
+	if [ "$rc" -eq 77 ]; then
+		exit 255
+	fi
+	return "$rc"
+}
+
+# Run with and without ignored paths, matching test-openat coverage.
+run_test ./test/test-openat2-syscall || exit $?
+run_test env PSEUDO_IGNORE_PATHS=/ ./test/test-openat2-syscall || exit $?
+
+exit 0
Signature · 1 line
1.8.3.1
author
#4 lore raw

PATCH 4/5 test: Add renameat2 test cases

In reply to Mark Hatle

From: Mark Hatle <[email protected]>

Add two test cases for renameat2 semantics. A primary test and a chroot test case.

AI-Generated: Test cases written using GitHub Copilot (Claude Sonnet 4.6)

---
 test/test-renameat2-chroot.sh |  11 ++
 test/test-renameat2.c         | 263 ++++++++++++++++++++++++++++++++++++++++++
 test/test-renameat2.sh        |  11 ++
 3 files changed, 285 insertions(+)
 create mode 100755 test/test-renameat2-chroot.sh
 create mode 100644 test/test-renameat2.c
 create mode 100755 test/test-renameat2.sh
test/test-renameat2-chroot.sh new file +11 -0
new file mode 100755
index 0000000..3e44033
@@ -0,0 +1,11 @@
+#! /bin/sh
+
+# Run with and without ignored paths, matching test-openat coverage.
+
+chroot ./test ./test-renameat2
+rc=$?
+if [ "$rc" -ne 0 ]; then
+	exit "$rc"
+fi
+
+PSEUDO_IGNORE_PATHS=/ chroot ./test ./test-renameat2
test/test-renameat2.c new file +263 -0

263 lines not on this page · raw message

test/test-renameat2.sh new file +11 -0
new file mode 100755
index 0000000..6aa7fbc
@@ -0,0 +1,11 @@
+#! /bin/sh
+
+# Run with and without ignored paths, matching test-openat coverage.
+
+./test/test-renameat2
+rc=$?
+if [ "$rc" -ne 0 ]; then
+	exit "$rc"
+fi
+
+PSEUDO_IGNORE_PATHS=/ ./test/test-renameat2
Signature · 1 line
1.8.3.1

Mark Hatle

author
#0 lore raw

cover Add openat2 support, update to version 1.9.4

Thanks to Richard, it looks like we have a working openat2 implementation.

There is a know issue where it appears to be able to escape the chroot, I don't know if this is expected behavior for the call or not. But for our Yocto Project workflow this should be ok. (Since we're only emulating chroot semantics, escaping from the chroot is not a security issue! There a multiple ways to escape or by-pass the emulation and pseudo should not be used for this type of security.)

I've also attempted to add test cases for the functionality, both the function call and syscall versions of openat2.

Finally we're bumping the release version to 1.9.4 (assuming no issues in the review of these changes.)

Mark Hatle (4):
openat2: Implement openat2 wrapper
test: Add openat2 test cases
test: Add renameat2 test cases
Makefile.in: Bump version to 1.9.4

Richard Purdie (1):
makewrappers/openat2: Add preserve_path option

 Makefile.in                         |   2 +-
 makewrappers                        |  18 ++-
 ports/linux/openat2/guts/openat2.c  | 198 ++++++++++++++++++++++--
 ports/linux/openat2/wrapfuncs.in    |   2 +-
 ports/linux/pseudo_wrappers.c       |   3 +
 templates/wrapfuncs.c               |   1 +
 test/test-openat2-func-chroot.sh    |  18 +++
 test/test-openat2-func.c            | 274 +++++++++++++++++++++++++++++++++
 test/test-openat2-func.sh           |  18 +++
 test/test-openat2-syscall-chroot.sh |  18 +++
 test/test-openat2-syscall.c         | 299 ++++++++++++++++++++++++++++++++++++
 test/test-openat2-syscall.sh        |  18 +++
 test/test-renameat2-chroot.sh       |  11 ++
 test/test-renameat2.c               | 263 +++++++++++++++++++++++++++++++
 test/test-renameat2.sh              |  11 ++
 test/test-syscall.c                 |   9 +-
 16 files changed, 1146 insertions(+), 17 deletions(-)
 create mode 100755 test/test-openat2-func-chroot.sh
 create mode 100644 test/test-openat2-func.c
 create mode 100755 test/test-openat2-func.sh
 create mode 100755 test/test-openat2-syscall-chroot.sh
 create mode 100644 test/test-openat2-syscall.c
 create mode 100755 test/test-openat2-syscall.sh
 create mode 100755 test/test-renameat2-chroot.sh
 create mode 100644 test/test-renameat2.c
 create mode 100755 test/test-renameat2.sh
Signature · 1 line
1.8.3.1
author
#2 lore raw

PATCH 2/5 makewrappers/openat2: Add preserve_path option

From: Richard Purdie <[email protected]>

openat2 needs the relative path preserved as a call argument, we can't pass in the absolute path pseudo converts to. The difference in behaviour is clear in this example:

openat2(AT_FDCWD, "/tmp/testdir/dir1/dir2/", {flags=O_RDONLY|O_NOFOLLOW|O_CLOEXEC|O_PATH|O_DIRECTORY, resolve=RESOLVE_BENEATH}, 24) = -1 EXDEV (Invalid cross-device link)

vs.

openat2(AT_FDCWD, "dir1/dir2/", {flags=O_RDONLY|O_NOFOLLOW|O_CLOEXEC|O_PATH|O_DIRECTORY, resolve=RESOLVE_BENEATH}, 24) = 4

Add a makewrappers option to preserve paths to the underlying function call and use it for openat2.

This code attempts to handle a chroot for absolute paths but if the path is relative, chroot behaviour would be tricky.

---
 makewrappers                       | 18 ++++++++++++++-
 ports/linux/openat2/guts/openat2.c | 46 +++++++++++++++++++++++++-------------
 ports/linux/openat2/wrapfuncs.in   |  2 +-
 templates/wrapfuncs.c              |  1 +
 4 files changed, 49 insertions(+), 18 deletions(-)
makewrappers +17 -1
index 326f70e..df405fc 100755
@@ -248,6 +248,11 @@ class Function:
         self.date = datetime.date.today().year
         # Used to define pointers that should EFAULT if null
         self.efault = None
+
+        # Used for functions which need to called with the original unconverted paths, e.g. openat2
+        # In those cases, use a suffix ('_int') for the path variable names instead of
+        # overwriting them. This means the wrapper needs to handle paths itself.
+        self.preserve_paths = False
     
         function, comments = line.split(';')
         comment = re.search(r'/\* *(.*) *\*/', comments)
@@ -383,6 +388,11 @@ class Function:
         """present argument list for a function call"""
         return self.args.call()
 
+    def paths_decl(self):
+        if self.preserve_paths:
+            return "\n\t".join("char *" + path + "_int;" for path in self.paths_to_munge)
+        return ""
+
     def fix_paths(self):
         """create/allocate canonical paths"""
         fix_paths = []
@@ -390,6 +400,9 @@ class Function:
             prefix = path[:-4]
             if prefix not in self.specific_dirfds:
                 prefix = ''
+            pathvar = path
+            if self.preserve_paths:
+                pathvar = path + "_int"
             if self.dirfd != "AT_FDCWD" and "flags" in self.flags \
                     and "AT_SYMLINK_NOFOLLOW" in self.flags:
                 fix_paths.append(
@@ -409,7 +422,7 @@ class Function:
                     "\t\t}\n" % (path, self.rc_assign()))
             fix_paths.append(
                 "%s = pseudo_root_path(__func__, __LINE__, %s%s, %s, %s);" %
-                (path, prefix, self.dirfd, path, self.flags))
+                (pathvar, prefix, self.dirfd, path, self.flags))
         return "\n\t\t".join(fix_paths)
 
     def ignore_paths(self):
@@ -424,6 +437,9 @@ class Function:
         elif "path" in self.paths_to_munge:
             mainpath = "path"
 
+        if mainpath and self.preserve_paths:
+            mainpath += "_int"
+
         if mainpath:
             return "pseudo_client_ignore_path(%s)" % mainpath
         if self.fd_arg:
ports/linux/openat2/guts/openat2.c +30 -16

105 lines not on this page · raw message

ports/linux/openat2/wrapfuncs.in +1 -1
index 2f1e716..2995646 100644
@@ -1 +1 @@
-int openat2(int dirfd, const char *path, const struct open_how *how, size_t size);
+int openat2(int dirfd, const char *path, const struct open_how *how, size_t size); /* preserve_paths=1 */
templates/wrapfuncs.c +1 -0
index e925b6e..9385a40 100644
@@ -22,6 +22,7 @@ ${name}(${decl_args}) {
 	sigset_t saved;
 	${variadic_decl}
 	${rc_decl}
+	${paths_decl}
 	PROFILE_START;
 
 ${maybe_async_skip}
Signature · 1 line
1.8.3.1
author
#5 lore raw

PATCH 5/5 Makefile.in: Bump version to 1.9.4

In reply to Mark Hatle
---
 Makefile.in | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Makefile.in +1 -1
index 8bf3f48..a5aba38 100644
@@ -34,7 +34,7 @@ BITS=@BITS@
 ARCH_FLAGS=@ARCH_FLAGS@
 MARK64=@MARK64@
 RPATH=@RPATH@
-VERSION=1.9.3
+VERSION=1.9.4
 
 LIB=@LIB@
 BIN=bin
Signature · 1 line
1.8.3.1