Skip to contentSkip to conversation

Add open_tree() wrapper

PATCH In review [email protected] 2 participants 9 messages Oct 5 – Oct 21

lore
Share & tools
lore
#0 lore raw

v4 cover Add open_tree() wrapper

From: Dmitry Sakhonchik <[email protected]>

v2:
- Handle relative paths correctly in the open_tree wrapper
- Add fallback to SYS_open_tree when libc function is absent
- Add tests coverage with relative paths (dirfd != AT_FDCWD)
- Add support and tests for syscall(SYS_open_tree, ...)

v3:
- Add open_tree_attr wrapper
- Add open_tree_attr syscall test

v4:
- Account for review

Dmitry Sakhonchik (6):
ports/linux/guts: Add open_tree wrapper
test: Add open_tree test
ports/linux/pseudo_wrappers.c: Add syscall support for open_tree()
wrapper
test: Add open_tree syscall test
test: Add open_tree_attr syscall test
ports/linux/guts: Add open_tree_attr wrapper

 ports/linux/guts/open_tree.c        |  59 ++++++++++
 ports/linux/guts/open_tree_attr.c   |  57 ++++++++++
 ports/linux/portdefs.h              |  24 ++++
 ports/linux/pseudo_wrappers.c       |  30 +++++
 ports/linux/wrapfuncs.in            |   2 +
 pseudo_wrappers.c                   |   1 +
 test/test-open_tree-syscall.c       | 154 +++++++++++++++++++++++++
 test/test-open_tree-syscall.sh      |   9 ++
 test/test-open_tree.c               | 141 +++++++++++++++++++++++
 test/test-open_tree.sh              |   9 ++
 test/test-open_tree_attr-syscall.c  | 167 ++++++++++++++++++++++++++++
 test/test-open_tree_attr-syscall.sh |   9 ++
 12 files changed, 662 insertions(+)
 create mode 100644 ports/linux/guts/open_tree.c
 create mode 100644 ports/linux/guts/open_tree_attr.c
 create mode 100644 test/test-open_tree-syscall.c
 create mode 100755 test/test-open_tree-syscall.sh
 create mode 100644 test/test-open_tree.c
 create mode 100755 test/test-open_tree.sh
 create mode 100644 test/test-open_tree_attr-syscall.c
 create mode 100755 test/test-open_tree_attr-syscall.sh
#1 lore raw

PATCH v4 1/6 ports/linux/guts: Add open_tree wrapper

From: Dmitry Sakhonchik <[email protected]>

[YOCTO #16379]

OPEN_TREE_CLONE is not handled since it requires CAP_SYS_ADMIN and is outside the common pseudo use case.

---
 ports/linux/guts/open_tree.c | 59 ++++++++++++++++++++++++++++++++++++
 ports/linux/portdefs.h       | 16 ++++++++++
 ports/linux/wrapfuncs.in     |  1 +
 3 files changed, 76 insertions(+)
 create mode 100644 ports/linux/guts/open_tree.c
ports/linux/guts/open_tree.c new file +59 -0
new file mode 100644
index 0000000..ca299a1
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2026 Yocto Project
+ * guts/COPYRIGHT for information.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * int open_tree(int dirfd, const char *path, unsigned int flags)
+ *	int rc = -1;
+ */
+
+	struct stat64 buf;
+	int save_errno;
+	char *pseudo_path;
+
+	if (flags & OPEN_TREE_CLONE) {
+		pseudo_diag("ERROR: OPEN_TREE_CLONE is not supported\n");
+	}
+
+	if (path && path[0] == '/') {
+		/* If the path is absolute, we should account for a potential chroot */
+		path = pseudo_path;
+	}
+
+	/*
+	* Older libc versions do not provide open_tree(), so call the syscall
+	* directly when the libc wrapper is not available.
+	*/
+	if (real_open_tree) {
+		pseudo_debug(PDBGF_SYSCALL, "open_tree, calling open_tree.\n");
+		rc = real_open_tree(dirfd, path, flags);
+	} else {
+		pseudo_debug(PDBGF_SYSCALL, "open_tree, calling syscall.\n");
+		rc = real_syscall(SYS_open_tree, dirfd, path, flags);
+	}
+
+	if (rc != -1) {
+		save_errno = errno;
+		int stat_rc;
+
+		pseudo_path = pseudo_root_path(__func__, __LINE__, dirfd, path, 0);
+
+		stat_rc = real___fxstatat64(_STAT_VER, dirfd, pseudo_path, &buf, (flags & AT_SYMLINK_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0);
+
+		pseudo_debug(PDBGF_FILE, "open_tree(path %s), flags %o, stat rc %d, stat mode %o\n",
+			pseudo_path, flags, stat_rc, buf.st_mode);
+
+		if (stat_rc != -1) {
+			pseudo_client_op(OP_OPEN, 0, rc, dirfd, pseudo_path, &buf);
+		} else {
+			pseudo_debug(PDBGF_FILE, "open_tree(fd %d, path %d/%s, flags %d) succeeded, but stat failed (%s).\n",
+				rc, dirfd, pseudo_path, flags, strerror(errno));
+			pseudo_client_op(OP_OPEN, 0, rc, dirfd, pseudo_path, 0);
+		}
+		errno = save_errno;
+	}
+
+/*	return rc;
+ * }
+ */
ports/linux/portdefs.h +16 -0
index 1f1a41a..579dde7 100644
@@ -82,3 +82,19 @@ extern int unshare(int flags);
 
 #define SYS_openat2 __NR_openat2
 #endif
+
+#ifndef SYS_open_tree
+
+#ifndef __NR_open_tree
+#define __NR_open_tree 428
+#endif
+
+#define SYS_open_tree __NR_open_tree
+#endif
+
+/* OPEN_TREE_CLONE may be missing from older host headers.
+ */
+#ifndef OPEN_TREE_CLONE
+#define OPEN_TREE_CLONE (1U << 0)
+#endif
+
ports/linux/wrapfuncs.in +1 -0
index 3cea20a..bc6ca23 100644
@@ -10,6 +10,7 @@ int lchown(const char *path, uid_t owner, gid_t group); /* flags=AT_SYMLINK_NOFO
 int __fxstatat(int ver, int dirfd, const char *path, struct stat *buf, int flags);
 int openat(int dirfd, const char *path, int flags, ...{mode_t mode}); /* flags=((flags&O_NOFOLLOW)||((flags&(O_CREAT|O_EXCL))==(O_CREAT|O_EXCL))), noignore_path=1 */
 int __openat_2(int dirfd, const char *path, int flags); /* flags=((flags&O_NOFOLLOW)||((flags&(O_CREAT|O_EXCL))==(O_CREAT|O_EXCL))), noignore_path=1 */
+int open_tree(int dirfd, const char *path, unsigned int flags); /* preserve_paths=1, noignore_path=1 */
 int mknod(const char *path, mode_t mode, dev_t dev); /* real_func=pseudo_mknod */
 int mknodat(int dirfd, const char *path, mode_t mode, dev_t dev); /* real_func=pseudo_mknodat */
 int __xmknod(int ver, const char *path, mode_t mode, dev_t *dev); /* flags=AT_SYMLINK_NOFOLLOW */
#2 lore raw

Re: [pseudo] [PATCH v4 1/6] ports/linux/guts: Add open_tree wrapper

Quoted 7 lines · On Mon, 2026-10-05 at 11:11 +0300, [email protected] wrote:

From: Dmitry Sakhonchik <[email protected]>

[YOCTO #16379]

OPEN_TREE_CLONE is not handled since it requires CAP_SYS_ADMIN and is outside the common pseudo use case.

Hi Dmitry,

This patch has now merged to master but I've spotted a problem... Please could you send a follow-up patch to address this? A similar change is also needed in open_tree_attr().

Quoted 24 lines
---
 ports/linux/guts/open_tree.c | 59 ++++++++++++++++++++++++++++++++++++
 ports/linux/portdefs.h       | 16 ++++++++++
 ports/linux/wrapfuncs.in     |  1 +
 3 files changed, 76 insertions(+)
 create mode 100644 ports/linux/guts/open_tree.c
ports/linux/guts/open_tree.c new file +11 -0
new file mode 100644
index 0000000..ca299a1
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2026 Yocto Project
+ * guts/COPYRIGHT for information.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * int open_tree(int dirfd, const char *path, unsigned int flags)
+ *	int rc = -1;
+ */
+
+	struct stat64 buf;

ports/linux/guts/open_tree.c line 21

Quoted 10 lines
ports/linux/guts/open_tree.c new file +10 -0
+	int save_errno;
+	char *pseudo_path;
+
+	if (flags & OPEN_TREE_CLONE) {
+		pseudo_diag("ERROR: OPEN_TREE_CLONE is not supported\n");
+	}
+
+	if (path && path[0] == '/') {
+		/* If the path is absolute, we should account for a potential chroot */
+		path = pseudo_path;

pseudo_path is uninitialised here.

I think we're missing the comment and call that's present in the openat2() wrapper:

    /* This wrapper is not called with modified paths, therefore we have to handle this ourselves.
       We can't use the standard mappings since the function needs relative paths to be preserved,
       they can't be our usual absolute paths. An example difference:
       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
       FIXME - we don't handle chroot for relative paths
    */
    pseudo_path = pseudo_root_path(__func__, __LINE__, dirfd, path, 0);

The openat2() examples there need replacing with open_tree() examples.

Quoted 9 lines
ports/linux/guts/open_tree.c new file +9 -0
+	}
+
+	/*
+	* Older libc versions do not provide open_tree(), so call the syscall
+	* directly when the libc wrapper is not available.
+	*/
+	if (real_open_tree) {
+		pseudo_debug(PDBGF_SYSCALL, "open_tree, calling open_tree.\n");
+		rc = real_open_tree(dirfd, path, flags);

ports/linux/guts/open_tree.c line 40

Quoted 10 lines
ports/linux/guts/open_tree.c new file +10 -0
+	} else {
+		pseudo_debug(PDBGF_SYSCALL, "open_tree, calling syscall.\n");
+		rc = real_syscall(SYS_open_tree, dirfd, path, flags);
+	}
+
+	if (rc != -1) {
+		save_errno = errno;
+		int stat_rc;
+
+		pseudo_path = pseudo_root_path(__func__, __LINE__, dirfd, path, 0);

This won't be needed here once pseudo_path is initialised at the top of the function.

Best regards,

Signature · 1 line
Paul Barker
#3 lore raw

Re: [pseudo] [PATCH v4 1/6] ports/linux/guts: Add open_tree wrapper

Hi.

Sure, I've sent the patch.

ср, 21 окт. 2026 г. в 12:03, Paul Barker <[email protected]>:

Quoted 108 lines · On Mon, 2026-10-05 at 11:11 +0300, [email protected] wrote:
Quoted 5 lines

From: Dmitry Sakhonchik <[email protected]>

[YOCTO #16379]

OPEN_TREE_CLONE is not handled since it requires CAP_SYS_ADMIN and is

outside the common pseudo use case.

Quoted 2 lines

Hi Dmitry,

This patch has now merged to master but I've spotted a problem... Please could you send a follow-up patch to address this? A similar change is also needed in open_tree_attr().

Quoted 23 lines
---
 ports/linux/guts/open_tree.c | 59 ++++++++++++++++++++++++++++++++++++
 ports/linux/portdefs.h       | 16 ++++++++++
 ports/linux/wrapfuncs.in     |  1 +
 3 files changed, 76 insertions(+)
 create mode 100644 ports/linux/guts/open_tree.c
ports/linux/guts/open_tree.c new file +10 -0
new file mode 100644
index 0000000..ca299a1
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2026 Yocto Project
+ * guts/COPYRIGHT for information.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * int open_tree(int dirfd, const char *path, unsigned int flags)
+ *   int rc = -1;
+ */
+

ports/linux/guts/open_tree.c line 20

Quoted 10 lines
ports/linux/guts/open_tree.c new file +10 -0
+     struct stat64 buf;
+     int save_errno;
+     char *pseudo_path;
+
+     if (flags & OPEN_TREE_CLONE) {
+             pseudo_diag("ERROR: OPEN_TREE_CLONE is not supported\n");
+     }
+
+     if (path && path[0] == '/') {
+             /* If the path is absolute, we should account for a

potential chroot */

ports/linux/guts/open_tree.c line 21

Quoted 1 line
ports/linux/guts/open_tree.c new file +1 -0
+             path = pseudo_path;

pseudo_path is uninitialised here.

I think we're missing the comment and call that's present in the openat2() wrapper:

    /* This wrapper is not called with modified paths, therefore we have

to handle this ourselves.
We can't use the standard mappings since the function needs
relative paths to be preserved,
they can't be our usual absolute paths. An example difference:
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
       FIXME - we don't handle chroot for relative paths
    */
    pseudo_path = pseudo_root_path(__func__, __LINE__, dirfd, path, 0);

The openat2() examples there need replacing with open_tree() examples.

ports/linux/guts/open_tree.c

Quoted 4 lines
ports/linux/guts/open_tree.c new file +4 -0
+     }
+
+     /*
+     * Older libc versions do not provide open_tree(), so call the

syscall

ports/linux/guts/open_tree.c

Quoted 4 lines
ports/linux/guts/open_tree.c new file +4 -0
+     * directly when the libc wrapper is not available.
+     */
+     if (real_open_tree) {
+             pseudo_debug(PDBGF_SYSCALL, "open_tree, calling

open_tree.\n");

ports/linux/guts/open_tree.c

Quoted 3 lines
ports/linux/guts/open_tree.c new file +3 -0
+             rc = real_open_tree(dirfd, path, flags);
+     } else {
+             pseudo_debug(PDBGF_SYSCALL, "open_tree, calling

syscall.\n");

ports/linux/guts/open_tree.c

Quoted 8 lines
ports/linux/guts/open_tree.c new file +8 -0
+             rc = real_syscall(SYS_open_tree, dirfd, path, flags);
+     }
+
+     if (rc != -1) {
+             save_errno = errno;
+             int stat_rc;
+
+             pseudo_path = pseudo_root_path(__func__, __LINE__, dirfd,

path, 0);

This won't be needed here once pseudo_path is initialised at the top of the function.

Best regards,

--
Paul Barker

#4 lore raw

PATCH v4 2/6 test: Add open_tree test

In reply to [email protected]

From: Dmitry Sakhonchik <[email protected]>

[YOCTO #16379]

Covers basic open_tree wrapper functionality and the AT_EMPTY_PATH case.

---
 test/test-open_tree.c  | 141 +++++++++++++++++++++++++++++++++++++++++
 test/test-open_tree.sh |   9 +++
 2 files changed, 150 insertions(+)
 create mode 100644 test/test-open_tree.c
 create mode 100755 test/test-open_tree.sh
test/test-open_tree.c new file +141 -0
new file mode 100644
index 0000000..9570f11
@@ -0,0 +1,141 @@
+#define _GNU_SOURCE
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/mount.h>
+
+#define TEST_DIR "test-open-tree-dir"
+#define TEST_FILE "test-open-tree-file"
+
+static int test_no_flags(void) {
+    int treefd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    treefd = open_tree(AT_FDCWD, TEST_DIR, 0);
+    if (treefd == -1) {
+        perror("open_tree");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+out:
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static int test_relative_dirfd(void) {
+    int basefd = -1;
+    int treefd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    basefd = open(".", O_PATH | O_DIRECTORY);
+    if (basefd < 0) {
+        perror("open");
+        goto out;
+    }
+
+    treefd = open_tree(basefd, TEST_DIR, 0);
+    if (treefd < 0) {
+        perror("open_tree");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+out:
+    if (basefd != -1) {
+        close(basefd);
+    }
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static int test_at_empty_path(void) {
+    int treefd = -1;
+    int dirfd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    dirfd = open(TEST_DIR, O_PATH | O_DIRECTORY);
+    if (dirfd < 0) {
+        perror("open");
+        goto out;
+    }
+
+    treefd = open_tree(dirfd, "", AT_EMPTY_PATH);
+    if (treefd == -1) {
+        perror("open_tree");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+out:
+    if (dirfd != -1) {
+        close(dirfd);
+    }
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static void cleanup(void) {
+    if (unlink(TEST_DIR "/" TEST_FILE) == -1 && errno != ENOENT) {
+        perror("unlink");
+    }
+    if (rmdir(TEST_DIR) == -1 && errno != ENOENT) {
+        perror("rmdir");
+    }
+}
+
+int main(void) {
+    cleanup();
+
+    if (mkdir(TEST_DIR, 0777) == -1) {
+        perror("mkdir");
+        return 1;
+    }
+
+    int rc = test_no_flags();
+    rc |= test_at_empty_path();
+    rc |= test_relative_dirfd();
+
+    cleanup();
+
+    return rc;
+}
test/test-open_tree.sh new file +9 -0
new file mode 100755
index 0000000..427d550
@@ -0,0 +1,9 @@
+#!/bin/sh
+
+./test/test-open_tree
+rc1=$?
+
+PSEUDO_IGNORE_PATHS=/ ./test/test-open_tree
+rc2=$?
+
+exit $((rc1 | rc2))
#5 lore raw

PATCH v4 3/6 ports/linux/pseudo_wrappers.c: Add syscall support for open_tree() wrapper

In reply to [email protected]

From: Dmitry Sakhonchik <[email protected]>

[YOCTO #16379]

open_tree() was added in Linux 5.2, while the glibc wrapper was only introduced in glibc 2.36. Before that, applications using glibc had to invoke it through syscall.

As a result, applications supporting older glibc versions may still invoke open_tree() via syscall()

---
 ports/linux/pseudo_wrappers.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
ports/linux/pseudo_wrappers.c +14 -0
index b920cb2..33cba2d 100644
@@ -102,6 +102,20 @@ syscall(long number, ...) {
 #error SYS_openat2 not defined
 #endif
 
+#ifdef SYS_open_tree
+	if (number == SYS_open_tree) {
+		pseudo_debug(PDBGF_SYSCALL, "syscall, faking open_tree.\n");
+		va_start(ap, number);
+		int dirfd = va_arg(ap, int);
+		const char *path = va_arg(ap, const char*);
+		unsigned int flags = va_arg(ap, unsigned int);
+
+		return wrap_open_tree(dirfd, path, flags);
+	}
+#else
+#error SYS_open_tree not defined
+#endif
+
 #ifdef SYS_renameat2
         /* Call out wrapper, expanding the variable arguments first */
 	if (number == SYS_renameat2) {
#6 lore raw

PATCH v4 4/6 test: Add open_tree syscall test

In reply to [email protected]

From: Dmitry Sakhonchik <[email protected]>

[YOCTO #16379]

Covers basic wrapper functionality when using syscall(SYS_open_tree, ...), also tests AT_EMPTY_PATH flag and related paths using dirfd != AT_FDCWD.

---
 test/test-open_tree-syscall.c  | 154 +++++++++++++++++++++++++++++++++
 test/test-open_tree-syscall.sh |   9 ++
 2 files changed, 163 insertions(+)
 create mode 100644 test/test-open_tree-syscall.c
 create mode 100755 test/test-open_tree-syscall.sh
test/test-open_tree-syscall.c new file +154 -0
new file mode 100644
index 0000000..42eeb37
@@ -0,0 +1,154 @@
+#define _GNU_SOURCE
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/syscall.h>
+#include <sys/stat.h>
+
+#define TEST_DIR "test-open-tree-dir"
+#define TEST_FILE "test-open-tree-file"
+
+static int do_open_tree_syscall(int dirfd, const char *path,
+                unsigned int flags)
+{
+#ifdef SYS_open_tree
+    return syscall(SYS_open_tree, dirfd, path, flags);
+#elif defined(__NR_open_tree)
+    return syscall(__NR_open_tree, dirfd, path, flags);
+#else
+    errno = ENOSYS;
+    return -1;
+#endif
+}
+
+static int test_no_flags(void) {
+    int treefd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    treefd = do_open_tree_syscall(AT_FDCWD, TEST_DIR, 0);
+    if (treefd == -1) {
+        perror("open_tree");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+out:
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static int test_relative_dirfd(void) {
+    int basefd = -1;
+    int treefd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    basefd = open(".", O_PATH | O_DIRECTORY);
+    if (basefd < 0) {
+        perror("open");
+        goto out;
+    }
+
+    treefd = do_open_tree_syscall(basefd, TEST_DIR, 0);
+    if (treefd < 0) {
+        perror("open_tree");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+    out:
+        if (basefd != -1) {
+            close(basefd);
+        }
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static int test_at_empty_path(void) {
+    int treefd = -1;
+    int dirfd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    dirfd = open(TEST_DIR, O_PATH | O_DIRECTORY);
+    if (dirfd < 0) {
+        perror("open");
+        goto out;
+    }
+
+    treefd = do_open_tree_syscall(dirfd, "", AT_EMPTY_PATH);
+    if (treefd == -1) {
+        perror("open_tree");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+out:
+    if (dirfd != -1) {
+        close(dirfd);
+    }
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static void cleanup(void) {
+    if (unlink(TEST_DIR "/" TEST_FILE) == -1 && errno != ENOENT) {
+        perror("unlink");
+    }
+    if (rmdir(TEST_DIR) == -1 && errno != ENOENT) {
+        perror("rmdir");
+    }
+}
+
+int main(void) {
+    cleanup();
+
+    if (mkdir(TEST_DIR, 0777) == -1) {
+        perror("mkdir");
+        return 1;
+    }
+
+    int rc = test_no_flags();
+    rc |= test_at_empty_path();
+    rc |= test_relative_dirfd();
+
+    cleanup();
+
+    return rc;
+}
test/test-open_tree-syscall.sh new file +9 -0
new file mode 100755
index 0000000..b3cd550
@@ -0,0 +1,9 @@
+#!/bin/sh
+
+./test/test-open_tree-syscall
+rc1=$?
+
+PSEUDO_IGNORE_PATHS=/ ./test/test-open_tree-syscall
+rc2=$?
+
+exit $((rc1 | rc2))
#7 lore raw

PATCH v4 5/6 test: Add open_tree_attr syscall test

In reply to [email protected]

From: Dmitry Sakhonchik <[email protected]>

[YOCTO #16379]

Covers basic wrapper functionality when using syscall(SYS_open_tree_attr, ...), also tests AT_EMPTY_PATH flag and related paths using dirfd != AT_FDCWD. The test uses attr == NULL, because using mount_attr in this call starts mount-specific privilege checks which end up with EPERM.

---
 test/test-open_tree_attr-syscall.c  | 167 ++++++++++++++++++++++++++++
 test/test-open_tree_attr-syscall.sh |   9 ++
 2 files changed, 176 insertions(+)
 create mode 100644 test/test-open_tree_attr-syscall.c
 create mode 100755 test/test-open_tree_attr-syscall.sh
test/test-open_tree_attr-syscall.c new file +167 -0
new file mode 100644
index 0000000..cc38c5f
@@ -0,0 +1,167 @@
+#define _GNU_SOURCE
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/syscall.h>
+#include <linux/mount.h>
+#include <sys/stat.h>
+
+#ifndef SYS_open_tree_attr
+
+#ifndef __NR_open_tree_attr
+#define __NR_open_tree_attr 467
+#endif
+
+#define SYS_open_tree_attr __NR_open_tree_attr
+#endif
+
+#define TEST_DIR "test-open-tree-dir"
+#define TEST_FILE "test-open-tree-file"
+
+static int do_open_tree_attr_syscall(int dirfd, const char *path,
+                unsigned int flags)
+{
+    /* Using mount_attr in this call starts mount-specific privilege checks
+     * which end up with EPERM.
+     * The functionality when using attr == NULL is equivalent to open_tree() */
+#ifdef SYS_open_tree_attr
+    return syscall(SYS_open_tree_attr, dirfd, path, flags, NULL, 0);
+#elif defined(__NR_open_tree_attr)
+    return syscall(__NR_open_tree_attr, dirfd, path, flags, NULL, 0);
+#else
+    errno = ENOSYS;
+    return -1;
+#endif
+}
+
+static int test_no_flags(void) {
+    int treefd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    treefd = do_open_tree_attr_syscall(AT_FDCWD, TEST_DIR, 0);
+    if (treefd == -1) {
+        perror("open_tree_attr");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+out:
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static int test_relative_dirfd(void) {
+    int basefd = -1;
+    int treefd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    basefd = open(".", O_PATH | O_DIRECTORY);
+    if (basefd < 0) {
+        perror("open");
+        goto out;
+    }
+
+    treefd = do_open_tree_attr_syscall(basefd, TEST_DIR, 0);
+    if (treefd < 0) {
+        perror("open_tree_attr");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+    out:
+        if (basefd != -1) {
+            close(basefd);
+        }
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static int test_at_empty_path(void) {
+    int treefd = -1;
+    int dirfd = -1;
+    int fd = -1;
+    int rc = 1;
+
+    dirfd = open(TEST_DIR, O_PATH | O_DIRECTORY);
+    if (dirfd < 0) {
+        perror("open");
+        goto out;
+    }
+
+    treefd = do_open_tree_attr_syscall(dirfd, "", AT_EMPTY_PATH);
+    if (treefd == -1) {
+        perror("open_tree_attr");
+        goto out;
+    }
+
+    fd = openat(treefd, TEST_FILE, O_CREAT | O_RDWR, 0666);
+    if (fd < 0) {
+        perror("openat");
+        goto out;
+    }
+
+    rc = 0;
+out:
+    if (dirfd != -1) {
+        close(dirfd);
+    }
+    if (treefd != -1) {
+        close(treefd);
+    }
+    if (fd != -1) {
+        close(fd);
+    }
+    return rc;
+}
+
+static void cleanup(void) {
+    if (unlink(TEST_DIR "/" TEST_FILE) == -1 && errno != ENOENT) {
+        perror("unlink");
+    }
+    if (rmdir(TEST_DIR) == -1 && errno != ENOENT) {
+        perror("rmdir");
+    }
+}
+
+int main(void) {
+    cleanup();
+
+    if (mkdir(TEST_DIR, 0777) == -1) {
+        perror("mkdir");
+        return 1;
+    }
+
+    int rc = test_no_flags();
+    rc |= test_at_empty_path();
+    rc |= test_relative_dirfd();
+
+    cleanup();
+
+    return rc;
+}
test/test-open_tree_attr-syscall.sh new file +9 -0
new file mode 100755
index 0000000..f6e51f4
@@ -0,0 +1,9 @@
+#!/bin/sh
+
+./test/test-open_tree_attr-syscall
+rc1=$?
+
+PSEUDO_IGNORE_PATHS=/ ./test/test-open_tree_attr-syscall
+rc2=$?
+
+exit $((rc1 | rc2))
#8 lore raw

PATCH v4 6/6 ports/linux/guts: Add open_tree_attr wrapper

In reply to [email protected]

From: Dmitry Sakhonchik <[email protected]>

[YOCTO #16379]

It mostly mirrors open_tree, with the additional mount attribute arguments passed to the underlying syscall.

---
 ports/linux/guts/open_tree_attr.c | 57 +++++++++++++++++++++++++++++++
 ports/linux/portdefs.h            |  8 +++++
 ports/linux/pseudo_wrappers.c     | 16 +++++++++
 ports/linux/wrapfuncs.in          |  1 +
 pseudo_wrappers.c                 |  1 +
 5 files changed, 83 insertions(+)
 create mode 100644 ports/linux/guts/open_tree_attr.c
ports/linux/guts/open_tree_attr.c new file +57 -0
new file mode 100644
index 0000000..3cf8fc7
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2026 Yocto Project
+ * guts/COPYRIGHT for information.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * int open_tree_attr(int dirfd, const char *path, unsigned int flags, struct mount_attr *attr, size_t size)
+ *	int rc = -1;
+ */
+
+	/* Mostly based on open_tree wrapper */
+
+	struct stat64 buf;
+	int save_errno;
+	char *pseudo_path;
+
+	if (flags & OPEN_TREE_CLONE) {
+		pseudo_diag("ERROR: OPEN_TREE_CLONE is not supported\n");
+	}
+
+	if (path && path[0] == '/') {
+		/* If the path is absolute, we should account for a potential chroot */
+		path = pseudo_path;
+	}
+
+	if (real_open_tree_attr) {
+		pseudo_debug(PDBGF_SYSCALL, "open_tree_attr, calling open_tree_attr.\n");
+		rc = real_open_tree_attr(dirfd, path, flags, attr, size);
+	} else {
+		pseudo_debug(PDBGF_SYSCALL, "open_tree_attr, calling syscall.\n");
+		rc = real_syscall(SYS_open_tree_attr, dirfd, path, flags, attr, size);
+	}
+
+	if (rc != -1) {
+		save_errno = errno;
+		int stat_rc;
+
+		pseudo_path = pseudo_root_path(__func__, __LINE__, dirfd, path, 0);
+
+		stat_rc = real___fxstatat64(_STAT_VER, dirfd, pseudo_path, &buf, (flags & AT_SYMLINK_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0);
+
+		pseudo_debug(PDBGF_FILE, "open_tree_attr(path %s), flags %o, stat rc %d, stat mode %o\n",
+			pseudo_path, flags, stat_rc, buf.st_mode);
+
+		if (stat_rc != -1) {
+			pseudo_client_op(OP_OPEN, 0, rc, dirfd, pseudo_path, &buf);
+		} else {
+			pseudo_debug(PDBGF_FILE, "open_tree_attr(fd %d, path %d/%s, flags %d) succeeded, but stat failed (%s).\n",
+				rc, dirfd, pseudo_path, flags, strerror(errno));
+			pseudo_client_op(OP_OPEN, 0, rc, dirfd, pseudo_path, 0);
+		}
+		errno = save_errno;
+	}
+
+/*	return rc;
+ * }
+ */
ports/linux/portdefs.h +8 -0
index 579dde7..6cece4e 100644
@@ -98,3 +98,11 @@ extern int unshare(int flags);
 #define OPEN_TREE_CLONE (1U << 0)
 #endif
 
+#ifndef SYS_open_tree_attr
+
+#ifndef __NR_open_tree_attr
+#define __NR_open_tree_attr 467
+#endif
+
+#define SYS_open_tree_attr __NR_open_tree_attr
+#endif
ports/linux/pseudo_wrappers.c +16 -0
index 33cba2d..4141556 100644
@@ -116,6 +116,22 @@ syscall(long number, ...) {
 #error SYS_open_tree not defined
 #endif
 
+#ifdef SYS_open_tree_attr
+	if (number == SYS_open_tree_attr) {
+		pseudo_debug(PDBGF_SYSCALL, "syscall, faking open_tree_attr.\n");
+		va_start(ap, number);
+		int dirfd = va_arg(ap, int);
+		const char *path = va_arg(ap, const char*);
+		unsigned int flags = va_arg(ap, unsigned int);
+		struct mount_attr *attr = va_arg(ap, struct mount_attr*);
+		size_t size = va_arg(ap, size_t);
+
+		return wrap_open_tree_attr(dirfd, path, flags, attr, size);
+	}
+#else
+#error SYS_open_tree_attr not defined
+#endif
+
 #ifdef SYS_renameat2
         /* Call out wrapper, expanding the variable arguments first */
 	if (number == SYS_renameat2) {
ports/linux/wrapfuncs.in +1 -0
index bc6ca23..d417ed3 100644
@@ -11,6 +11,7 @@ int __fxstatat(int ver, int dirfd, const char *path, struct stat *buf, int flags
 int openat(int dirfd, const char *path, int flags, ...{mode_t mode}); /* flags=((flags&O_NOFOLLOW)||((flags&(O_CREAT|O_EXCL))==(O_CREAT|O_EXCL))), noignore_path=1 */
 int __openat_2(int dirfd, const char *path, int flags); /* flags=((flags&O_NOFOLLOW)||((flags&(O_CREAT|O_EXCL))==(O_CREAT|O_EXCL))), noignore_path=1 */
 int open_tree(int dirfd, const char *path, unsigned int flags); /* preserve_paths=1, noignore_path=1 */
+int open_tree_attr(int dirfd, const char *path, unsigned int flags, struct mount_attr *attr, size_t size); /* preserve_paths=1, noignore_path=1*/
 int mknod(const char *path, mode_t mode, dev_t dev); /* real_func=pseudo_mknod */
 int mknodat(int dirfd, const char *path, mode_t mode, dev_t dev); /* real_func=pseudo_mknodat */
 int __xmknod(int ver, const char *path, mode_t mode, dev_t *dev); /* flags=AT_SYMLINK_NOFOLLOW */
pseudo_wrappers.c +1 -0
index 112239b..78cdecf 100644
@@ -37,6 +37,7 @@
 #include <grp.h>
 #include <pwd.h>
 #include <utime.h>
+#include <sys/mount.h>
 #ifdef PSEUDO_PORT_LINUX_STATVFS
 #include <sys/statvfs.h>
 #endif