Skip to content

Follow /dev/bus links in the guest namespace - #549

Open
jotpalch wants to merge 4 commits into
sysprog21:mainfrom
jotpalch:pr-dev-bus-links
Open

jotpalch wants to merge 4 commits into
sysprog21:mainfrom
jotpalch:pr-dev-bus-links

Conversation

@jotpalch

@jotpalch jotpalch commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

This is the follow-up committed to in #444: the links the path layer follows count toward one MAXSYMLINKS with the sysroot resolver's, and a link anywhere in a /dev/bus name resolves in the guest namespace, with the walk starting again from its target.

With a sysroot link from /dev/bus/link to usb, /dev/bus/link/001/001 and /dev/bus/link/001/../001/001 answer ENOENT on main, where Linux serves the usbfs node: the path layer follows a link only right before a .., and leaves every other one to the sysroot resolver, which does not hold /dev/bus/usb. A chain of 25 links under /dev/bus that goes on as 16 more outside it resolves when a .. follows the chain, where Linux answers ELOOP at the 41st, because the path layer and the sysroot resolver each count from zero. A relative link under /dev/bus followed by a .. reaches a host file outside the sysroot, where the same link elsewhere in the sysroot gets ELOOP, because the resolver takes the name it is handed for one the guest typed.

The first commit lets a create below a dangling link fail with ENOENT, and one below a file with ENOTDIR, as on Linux, rather than ELOOP, since a create through a link the path layer follows meets the same rule, and docs/filenames.md now states the rule a name reached through a link takes. The second lets a sysroot resolver go on from a walk its caller started, with every caller passing NULL, and the third passes it the links the path layer follows. The fourth follows a link wherever it sits in a /dev/bus name, and one that ends the name for an open with O_CREAT but neither O_EXCL nor O_NOFOLLOW, and refuses one it follows to a descriptor's magic link under RESOLVE_NO_MAGICLINKS. docs/internals.md states what still differs from Linux, among it an openat2 with RESOLVE_BENEATH or RESOLVE_IN_ROOT, whose check does not reach /dev/bus/usb through a link, a link in a name relative to a descriptor for a directory the backing holds, which the path layer does not follow, and a link after a .. that pops /dev/bus/usb.

Reproduce with the fixture devices and a static aarch64 busybox, over a sysroot holding the link:

$ mkdir -p sr/dev/bus && ln -s usb sr/dev/bus/link
$ ELFUSE_USB_FIXTURE=1 build/elfuse --sysroot sr busybox stat -c %F /dev/bus/link/001/001
$ ELFUSE_USB_FIXTURE=1 build/elfuse --sysroot sr busybox stat -L -c %F /dev/bus/link

On main both fail with No such file or directory. With these commits they print character special file and directory, as Linux does.

Tested on macOS 15.6.1 (24G90) with the 15.5 SDK on an Apple M1 (MacBookAir10,1). In make check, tests/driver.sh passes 102 of 103 on this branch and on main, the failure in both being test-thread-churn timing out, which stops make check there; the lanes after it, run one by one, pass on both, 89 on this branch and 88 on main. make test-matrix-elfuse-aarch64 gives 337 passed, 1 failed and 10 skipped on both, with the same failure. make check now also runs test-usb-sysfs-sysroot-caseexact, which repeats the backing run of test-usb-sysfs-sysroot over a case-sensitive HFS+ image, where the sysroot resolvers take their byte-exact walk.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 7 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="mk/tests.mk">

<violation number="1" location="mk/tests.mk:1841">
P2: The relative fixtures point inside a nonexistent `sysroot/tmp/...` path rather than at the staged host directory, so the new `backing` lane cannot exercise its relative-link escape checks and fails with `ENOENT` instead of `ELOOP`. Use paths relative to each link's parent (`../../../host` under `/dev/bus` and `../../host` under `/lk`).</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread mk/tests.mk
Comment on lines +1841 to +1842
ln -s "../..$$tmpdir/host" "$$sysroot/dev/bus/hrel"; \
ln -s "..$$tmpdir/host" "$$sysroot/lk/hrel"; \

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The relative fixtures point inside a nonexistent sysroot/tmp/... path rather than at the staged host directory, so the new backing lane cannot exercise its relative-link escape checks and fails with ENOENT instead of ELOOP. Use paths relative to each link's parent (../../../host under /dev/bus and ../../host under /lk).

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At mk/tests.mk, line 1841:

<comment>The relative fixtures point inside a nonexistent `sysroot/tmp/...` path rather than at the staged host directory, so the new `backing` lane cannot exercise its relative-link escape checks and fails with `ENOENT` instead of `ELOOP`. Use paths relative to each link's parent (`../../../host` under `/dev/bus` and `../../host` under `/lk`).</comment>

<file context>
@@ -1836,7 +1836,14 @@ test-usb-sysfs-sysroot: $(ELFUSE_BIN) $(TEST_DIR)/test-usb-sysfs-sysroot
-		$(TEST_DIR)/test-usb-sysfs-sysroot search root
+		$(TEST_DIR)/test-usb-sysfs-sysroot search root; \
+	mkdir "$$tmpdir/host" "$$sysroot/lk"; : > "$$tmpdir/host/f"; \
+	ln -s "../..$$tmpdir/host" "$$sysroot/dev/bus/hrel"; \
+	ln -s "..$$tmpdir/host" "$$sysroot/lk/hrel"; \
+	ln -s "$$tmpdir/host" "$$sysroot/dev/bus/habs"; \
</file context>
Suggested change
ln -s "../..$$tmpdir/host" "$$sysroot/dev/bus/hrel"; \
ln -s "..$$tmpdir/host" "$$sysroot/lk/hrel"; \
ln -s "../../../host" "$$sysroot/dev/bus/hrel"; \\
ln -s "../../host" "$$sysroot/lk/hrel"; \\

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The targets are guest names, not host ones. hrel under /dev/bus holds ../.. followed by the host directory's absolute path, and a .. stops at the guest root, so the link names that path in the guest. The sysroot does not hold it, and a resolver that would fall back to the host for it refuses after a relative link, which is the ELOOP these checks expect; on d3c0785 the check with .. under /dev/bus fails, and on this branch the lane passes. With ../../../host the link would name /host in the guest, and nothing would test the refusal. The lane takes the directory from mktemp -d, under TMPDIR, which is /var/folders on macOS and not a root the sysroot keeps. A comment above the recipe now says how the targets read.

Comment thread src/syscall/path.c Resolved
Comment thread src/syscall/proc-state.c Resolved
Comment thread src/syscall/path.c Outdated
/* As the sysroot resolvers below decide for the last name: a
* trailing slash follows it, and a create does not.
*/
bool follow = !(flags & PATH_TR_CREATE) &&

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PATH_TR_CREATE is also what sys_openat_path passes for every O_CREAT, so this never follows a final link for open(O_CREAT) without O_EXCL. With /dev/bus/link -> usb/001/001, open("/dev/bus/link", O_RDWR | O_CREAT) goes to the backing, where the host follows the link to a dev/bus/usb/001/001 the sysroot does not hold and answers ENOENT. Linux follows an existing final link there and opens the usbfs node.

sys_openat_path drops O_EXCL and O_NOFOLLOW when it picks PATH_TR_CREATE | PATH_TR_CREATE_PARENTS, and mkdirat and symlinkat must keep not following, so the layer cannot tell the cases apart today. It needs a flag of its own that sys_openat_path sets for O_CREAT without O_EXCL or O_NOFOLLOW. If that is out of scope here, add it to the differences listed in docs/internals.md and take it as a follow-up.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In e93cecc: sys_openat_path marks an O_CREAT open without O_EXCL or O_NOFOLLOW with a new PATH_TR_CREATE_FOLLOW, and the path layer follows a link that ends the name when it is set. mkdirat and symlinkat pass no such flag and still stop at the link. Neither does an openat2 with RESOLVE_BENEATH or RESOLVE_IN_ROOT: its check judges an O_CREAT name without following the last link, and following that link in the path layer created /lk/cfb for a link to it from /dev/bus, where Linux refuses, so there the link stays with the host as before, a link to the node included. With /dev/bus/unode linked to usb/001/001, open("/dev/bus/unode", O_RDWR | O_CREAT) opens the node. Through links under /dev/bus to names missing from /lk, O_CREAT creates the target, while O_EXCL gets EEXIST, O_NOFOLLOW gets ELOOP, and mkdir and symlink get EEXIST, each leaving it uncreated. Linux 6.18 under qemu answers these the same way, apart from the ENXIO it gives the node itself, which has no device behind it there. Following the link reached a create rule older than this PR: a create through a followed link whose target directory the sysroot lacks answered ELOOP, where Linux answers ENOENT, or ENOTDIR below a file. 791ad0b refuses it only when the host holds the parent. Outside /dev/bus, a link that ends the name of an O_CREAT open is still left to the host, as before; docs/internals.md lists both cases with the other differences, and I will send the change that follows such a link in the guest namespace separately, with the openat2 check judging such a name as open now does.

Comment thread src/syscall/proc-state.c
*/
bool followed_link = false;
bool followed_relative_link = false;
bool followed_link = walked && walked->links > 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a case-sensitive sysroot, walked->links only sets followed_link here. The byte-exact branch below checks the parent with access, so the host follows the links that are left and counts them from zero against its own limit. mkdir("/dev/bus/c41/l0/n") over the new c41 fixture follows 25 links in the layer and leaves /lk/c41/l0/n, and access on /lk/c41/l0 follows the other 16 and succeeds. Linux answers ELOOP at the 41st link, and so does the case-folding branch above, which starts resolve_through_links at walked->links.

Walk the parent's links from walked->links in this branch too, as resolve_byte_exact_through_links does for a lookup. If not, name this gap in docs/internals.md and pin it with a create case in the backing lane.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a15e514: handed a walk, the create resolver's byte-exact branch walks the links before the leaf from walked->links with resolve_byte_exact_through_links, as a lookup does, and checks the parent that walk ends at, so access finds no link left to follow. The lookup resolver had the same gap for a name under a root the sysroot keeps, such as /tmp or /etc, which its byte-exact branch handed to access without a walk; handed a walk, it now walks that name too. The backing run is on the default volume, where the case-folding branch answers, so 3dd341b adds make test-usb-sysfs-sysroot-caseexact, which repeats it over a case-sensitive HFS+ image, and make check runs it after test-sysroot-path-matrix. There mkdir("/dev/bus/c41/l0/../d/m") is ELOOP, and from e93cecc on so is mkdir("/dev/bus/c41/l0/n"), and so are stat and mkdir through a chain of 25 links under /dev/bus that goes on as 16 under /tmp, as on Linux 6.18. Its 41-link create checks fail with the create resolver's walk taken out, and its 41-link lookup under /tmp with the lookup resolver handing that name to access again, while the run on the default volume passes either way.

@jserv

jserv commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

any progress?

A create through a link whose target directory the sysroot does not hold
answered ELOOP outside the temp roots and the guest system directories,
where Linux answers ENOENT for a missing directory and ENOTDIR for one
below a file. The resolver refused it so that the create would not fall
back to the host literal of the name the guest typed, which names the
link rather than its target, nor land in a host directory the link
reaches.

The create resolver now refuses only when the host holds the parent, by
the test the lookup resolver applies to a name reached through a link,
and otherwise returns the sysroot spelling, on which the create fails as
on Linux. A walk that stopped at a file returns it without asking the
host. docs/filenames.md now gives the rule a name reached through a link
takes as the resolvers apply it: ELOOP after a relative link and for a
create whose parent only the host holds, and the host's file for a
lookup through absolute links alone.

test-sysroot-symlink-target checks that mkdir below a link to
/No.Such.Dir is ENOENT, and below a link to /System/Library with a file
at /System is ENOTDIR, as on Linux 6.18. On d3c0785 both are ELOOP, and
so they are with the new branch taken out; the second is also ELOOP with
the walk's stop at a file ignored.
The path layer follows some links itself before it hands a name to a
sysroot resolver, and the resolver starts as if none had been followed:
its MAXSYMLINKS count begins at zero, and a name reached through a
relative link may fall back to the host, which the resolver refuses
after a relative link of its own.

Each resolver now takes a path_walk_t with the links the caller
followed and whether one had a relative target, and starts its count
and its followed-link state from it. On a case-sensitive volume two
names went to the host, which would count their links from zero: the
lookup resolver handed it a name under a root the sysroot keeps, such
as /tmp, and the create resolver left it the links before the leaf.
Handed a walk, each now follows those links itself. Every caller passes
NULL here, so no answer changes.
The links path_backing_dotdot follows before a '..' are lost once the
name it rewrote reaches the sysroot resolver, so the resolver counts its
own links from zero and judges the name as one the guest typed. A chain
of 25 links under /dev/bus that goes on as 16 more outside it resolved,
where Linux answers ELOOP at the 41st, and a relative link under
/dev/bus reached a host file outside the sysroot, where the same link
elsewhere in the sysroot gets ELOOP. A create through an absolute link
under /dev/bus landed in the host directory the link names, where the
same link elsewhere gets ELOOP.

path_rewrite_dev_climb records in a path_walk_t the links it follows
and whether one had a relative target, and path_translate_at hands it
to the sysroot resolver with the rewritten name.

test-usb-sysfs-sysroot runs the guest again with the argument backing,
over a sysroot where hrel and habs under /dev/bus and /lk link to a host
directory, and the new test-usb-sysfs-sysroot-caseexact target, which
check runs, repeats that run over a case-sensitive HFS+ image, where the
sysroot resolvers take their byte-exact walk. The run resolves 40 links
before a '..' and gets ELOOP for 41, for a lookup and a create, through
a chain that goes on under /lk, and does the same under /tmp but for a
40-link create. It gets ELOOP for a relative link to the host directory
before a '..' under /dev/bus as under /lk, while an absolute one reaches
it under both, and ELOOP for a create through the absolute one. On
d3c0785 the 41-link checks and the /dev/bus relative and create checks
fail. All of them fail with the walk left out of path_translate_at, the
relative one with the relative target left unrecorded or with the
resolver taking the name it is handed for one the guest typed, and the
create one with the create resolver ignoring the walk. In the
case-sensitive run the 41-link creates also fail with the create
resolver leaving the links before the leaf to the host, and the 41-link
lookup under /tmp with the lookup resolver leaving that name to it.
With a sysroot link from /dev/bus/link to usb, /dev/bus/link/001/001 and
/dev/bus/link/001/../001/001 answered ENOENT where Linux serves the
usbfs node, and stat of /dev/bus/link answered ENOENT for a directory,
as did an open with O_CREAT through a link to usb/001/001. The path
layer followed a link only right before a '..', and left every other
link to the sysroot resolver, which follows it within the sysroot, and
the sysroot does not hold the layer's /dev/bus/usb.

path_dev_bus_follow_link follows the first link among the backing's
names in a /dev/bus name, up to its first '..', as Linux follows each
link where the walk meets it, and path_rewrite_dev_climb starts the walk
again from the result. A link that ends the name is followed only when
the backing would follow it, so lstat, readlink and mkdir still see the
link, and an open with O_CREAT follows it unless O_EXCL or O_NOFOLLOW is
set. sys_openat_path drops both flags when it picks PATH_TR_CREATE, so
its callers ask for PATH_TR_CREATE_FOLLOW, which it keeps for such an
open. An openat2 with RESOLVE_BENEATH or RESOLVE_IN_ROOT, whose check
does not follow such a link, leaves it to the host. One with
RESOLVE_NO_MAGICLINKS passes PATH_TR_NO_MAGICLINKS, and the layer checks
the name a link it follows leads to as sc_openat2 checks the name the
guest passed, which cannot see the link, so a link to a descriptor's
magic link is ELOOP, as on Linux. path_backing_dotdot is left with names
that hold no link. docs/internals.md lists what still differs from
Linux.

The backing run of test-usb-sysfs-sysroot reaches the node through such
links, by an absolute name, after a '..', against a /dev/bus dirfd and
from a /dev/bus cwd. It checks the link itself and a trailing slash,
counts 40 and 41 links with no '..' and all under /dev/bus, and 41
across a '..' that pops /dev/bus/usb, and checks open with O_CREAT,
O_EXCL and O_NOFOLLOW, mkdir and symlink at links that end the name, a
create below a link through a file, openat2 through such a link under
RESOLVE_BENEATH and RESOLVE_IN_ROOT, and openat2 under
RESOLVE_NO_MAGICLINKS through a link to /proc/self/fd/1. On d3c0785 18
of its 46 checks fail. Each check this adds fails there, on the
case-sensitive image, or with one of the changes it pins taken out,
except the check that the node is a char device, the 40-link count under
/dev/bus and the checks of habs/f and /lk/hrel/f, which hold either way.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6 issues found across 12 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="docs/filenames.md">

<violation number="1" location="docs/filenames.md:382">
P2: This sentence makes the `O_CREAT` final-link rule sound universal, but ordinary `open(O_CREAT)` follows an existing final `/dev/bus` link when neither `O_EXCL` nor `O_NOFOLLOW` is set. Clarify that only the sysroot create resolver leaves its final link unresolved, while the `/dev/bus` path-layer exception still follows it.</violation>
</file>

<file name="src/syscall/proc-state.c">

<violation number="1" location="src/syscall/proc-state.c:570">
P2: `host_holds_parent` treats any existing host entry as a usable directory parent. A link target whose parent is a regular file therefore takes the `ELOOP` branch instead of returning Linux's `ENOTDIR`; distinguish non-directory parents before applying the host-directory fallback.</violation>
</file>

<file name="src/syscall/syscall.c">

<violation number="1" location="src/syscall/syscall.c:2113">
P1: `RESOLVE_NO_MAGICLINKS` is bypassed for `O_CREAT` when combined with `RESOLVE_BENEATH` or `RESOLVE_IN_ROOT`: this selects `PATH_TR_NONE`, leaving a final `/dev/bus` link to `/dev/fd/N` for the host to follow instead of returning `ELOOP`. Enforce the magic-link check independently of the root flags before handing the final link to the host.</violation>
</file>

<file name="tests/test-usb-sysfs-sysroot.c">

<violation number="1" location="tests/test-usb-sysfs-sysroot.c:409">
P2: This openat2 regression does not verify the errno values it documents, so incorrect resolve-flag behavior can pass. Save `errno` immediately after each syscall and require `EXDEV` for `RESOLVE_BENEATH` and `ENOENT` for `RESOLVE_IN_ROOT` before checking the target.</violation>
</file>

<file name="mk/tests.mk">

<violation number="1" location="mk/tests.mk:1873">
P2: This lane can hide temporary-directory/path creation failures as a successful skip because `set -e` is enabled only after `mktemp` runs. Enable fail-fast before creating `dmg` and `mnt`, while retaining the explicit `if ! hdiutil ...` skip handling.</violation>
</file>

<file name="docs/internals.md">

<violation number="1" location="docs/internals.md:1754">
P2: This documents the wrong `openat2(O_CREAT)` result: the analogous `/dev/bus/clb -> ../../lk/cfb` case fails under both resolve modes and leaves the target uncreated. Say that `/lk/cfr` is not created, while Linux returns `EXDEV` for `RESOLVE_BENEATH` and `ENOENT` for `RESOLVE_IN_ROOT`.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread src/syscall/syscall.c
*/
unsigned int walk_flags =
(resolve & (RESOLVE_BENEATH | RESOLVE_IN_ROOT))
? PATH_TR_NONE

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: RESOLVE_NO_MAGICLINKS is bypassed for O_CREAT when combined with RESOLVE_BENEATH or RESOLVE_IN_ROOT: this selects PATH_TR_NONE, leaving a final /dev/bus link to /dev/fd/N for the host to follow instead of returning ELOOP. Enforce the magic-link check independently of the root flags before handing the final link to the host.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/syscall/syscall.c, line 2113:

<comment>`RESOLVE_NO_MAGICLINKS` is bypassed for `O_CREAT` when combined with `RESOLVE_BENEATH` or `RESOLVE_IN_ROOT`: this selects `PATH_TR_NONE`, leaving a final `/dev/bus` link to `/dev/fd/N` for the host to follow instead of returning `ELOOP`. Enforce the magic-link check independently of the root flags before handing the final link to the host.</comment>

<file context>
@@ -2105,6 +2105,15 @@ static int64_t sc_openat2(guest_t *g,
+         */
+        unsigned int walk_flags =
+            (resolve & (RESOLVE_BENEATH | RESOLVE_IN_ROOT))
+                ? PATH_TR_NONE
+                : PATH_TR_CREATE_FOLLOW;
+        if (resolve & RESOLVE_NO_MAGICLINKS)
</file context>

Comment thread docs/filenames.md
could otherwise hand the guest a file from outside it, and a create through a
link the resolver follows could make a new host file. The resolver does not
follow a link that ends the name of an `open` with `O_CREAT`, which
`docs/internals.md` lists with the other differences from Linux.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This sentence makes the O_CREAT final-link rule sound universal, but ordinary open(O_CREAT) follows an existing final /dev/bus link when neither O_EXCL nor O_NOFOLLOW is set. Clarify that only the sysroot create resolver leaves its final link unresolved, while the /dev/bus path-layer exception still follows it.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At docs/filenames.md, line 382:

<comment>This sentence makes the `O_CREAT` final-link rule sound universal, but ordinary `open(O_CREAT)` follows an existing final `/dev/bus` link when neither `O_EXCL` nor `O_NOFOLLOW` is set. Clarify that only the sysroot create resolver leaves its final link unresolved, while the `/dev/bus` path-layer exception still follows it.</comment>

<file context>
@@ -370,18 +370,28 @@ component is, and only the last one honors a caller's request not to. So
+could otherwise hand the guest a file from outside it, and a create through a
+link the resolver follows could make a new host file. The resolver does not
+follow a link that ends the name of an `open` with `O_CREAT`, which
+`docs/internals.md` lists with the other differences from Linux.
+
+A file in the sysroot that cuts short a walk through a link makes the name
</file context>
Suggested change
`docs/internals.md` lists with the other differences from Linux.
`docs/internals.md` lists with the other sysroot-resolver differences from Linux; the `/dev/bus` path-layer walk may still follow an existing final link for `open(O_CREAT)` without `O_EXCL` or `O_NOFOLLOW`.

Comment thread src/syscall/proc-state.c
if (n > 1)
n--;
snprintf(parent, sizeof(parent), "%.*s", (int) n, path);
return sysroot_path_exists(parent, true);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: host_holds_parent treats any existing host entry as a usable directory parent. A link target whose parent is a regular file therefore takes the ELOOP branch instead of returning Linux's ENOTDIR; distinguish non-directory parents before applying the host-directory fallback.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/syscall/proc-state.c, line 570:

<comment>`host_holds_parent` treats any existing host entry as a usable directory parent. A link target whose parent is a regular file therefore takes the `ELOOP` branch instead of returning Linux's `ENOTDIR`; distinguish non-directory parents before applying the host-directory fallback.</comment>

<file context>
@@ -554,6 +554,22 @@ static bool sysroot_path_exists(const char *resolved_path, bool follow_final)
+    if (n > 1)
+        n--;
+    snprintf(parent, sizeof(parent), "%.*s", (int) n, path);
+    return sysroot_path_exists(parent, true);
+}
+
</file context>

Comment on lines +409 to +413
long ofd =
bfd >= 0 ? syscall(SYS_openat2, bfd, "clb", &how, sizeof(how)) : -1;
EXPECT_TRUE(bfd >= 0 && ofd < 0 && lstat("/lk/cfb", &st) < 0,
r ? "openat2(/dev/bus, \"clb\", O_CREAT, RESOLVE_IN_ROOT)"
: "openat2(/dev/bus, \"clb\", O_CREAT, RESOLVE_BENEATH)");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This openat2 regression does not verify the errno values it documents, so incorrect resolve-flag behavior can pass. Save errno immediately after each syscall and require EXDEV for RESOLVE_BENEATH and ENOENT for RESOLVE_IN_ROOT before checking the target.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At tests/test-usb-sysfs-sysroot.c, line 409:

<comment>This openat2 regression does not verify the errno values it documents, so incorrect resolve-flag behavior can pass. Save `errno` immediately after each syscall and require `EXDEV` for `RESOLVE_BENEATH` and `ENOENT` for `RESOLVE_IN_ROOT` before checking the target.</comment>

<file context>
@@ -281,14 +314,129 @@ static void check_backing_names(void)
+    for (size_t r = 0; r < sizeof(rflags) / sizeof(rflags[0]); r++) {
+        struct open_how how = {
+            .flags = O_WRONLY | O_CREAT, .mode = 0644, .resolve = rflags[r]};
+        long ofd =
+            bfd >= 0 ? syscall(SYS_openat2, bfd, "clb", &how, sizeof(how)) : -1;
+        EXPECT_TRUE(bfd >= 0 && ofd < 0 && lstat("/lk/cfb", &st) < 0,
</file context>
Suggested change
long ofd =
bfd >= 0 ? syscall(SYS_openat2, bfd, "clb", &how, sizeof(how)) : -1;
EXPECT_TRUE(bfd >= 0 && ofd < 0 && lstat("/lk/cfb", &st) < 0,
r ? "openat2(/dev/bus, \"clb\", O_CREAT, RESOLVE_IN_ROOT)"
: "openat2(/dev/bus, \"clb\", O_CREAT, RESOLVE_BENEATH)");
long ofd =
bfd >= 0 ? syscall(SYS_openat2, bfd, "clb", &how, sizeof(how)) : -1;
int saved_errno = errno;
EXPECT_TRUE(bfd >= 0 && ofd < 0 &&
saved_errno == (r ? ENOENT : EXDEV) &&
lstat("/lk/cfb", &st) < 0,
r ? "openat2(/dev/bus, \"clb\", O_CREAT, RESOLVE_IN_ROOT)"
: "openat2(/dev/bus, \"clb\", O_CREAT, RESOLVE_BENEATH)");

Comment thread mk/tests.mk
# query of the file system.
## Re-run the /dev/bus backing run on a case-sensitive volume
test-usb-sysfs-sysroot-caseexact: $(ELFUSE_BIN) $(TEST_DIR)/test-usb-sysfs-sysroot
@dmg=$$(mktemp -u).dmg; mnt=""; \

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This lane can hide temporary-directory/path creation failures as a successful skip because set -e is enabled only after mktemp runs. Enable fail-fast before creating dmg and mnt, while retaining the explicit if ! hdiutil ... skip handling.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At mk/tests.mk, line 1873:

<comment>This lane can hide temporary-directory/path creation failures as a successful skip because `set -e` is enabled only after `mktemp` runs. Enable fail-fast before creating `dmg` and `mnt`, while retaining the explicit `if ! hdiutil ...` skip handling.</comment>

<file context>
@@ -1842,14 +1844,52 @@ test-usb-sysfs-sysroot: $(ELFUSE_BIN) $(TEST_DIR)/test-usb-sysfs-sysroot
+# query of the file system.
+## Re-run the /dev/bus backing run on a case-sensitive volume
+test-usb-sysfs-sysroot-caseexact: $(ELFUSE_BIN) $(TEST_DIR)/test-usb-sysfs-sysroot
+	@dmg=$$(mktemp -u).dmg; mnt=""; \
+	trap 'if [ -n "$$mnt" ] && [ "$$(/usr/bin/stat -f %d "$$mnt" 2>/dev/null)" != "$$(/usr/bin/stat -f %d "$$mnt/.." 2>/dev/null)" ] && \
+		! hdiutil detach "$$mnt" -force -quiet >/dev/null 2>&1; then \
</file context>
Suggested change
@dmg=$$(mktemp -u).dmg; mnt=""; \
@set -e; dmg=$$(mktemp -u).dmg; mnt=""; \\

Comment thread docs/internals.md
`ENOENT` where Linux reaches the node. The check does not follow a link that
ends an `O_CREAT` name, and leaves that link to the host as well, so with
`/dev/bus/clr` linked to `../../lk/cfr`, such an `openat2` of `clr` from
`/dev/bus` creates `/lk/cfr` where Linux answers `EXDEV` under `RESOLVE_BENEATH`

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This documents the wrong openat2(O_CREAT) result: the analogous /dev/bus/clb -> ../../lk/cfb case fails under both resolve modes and leaves the target uncreated. Say that /lk/cfr is not created, while Linux returns EXDEV for RESOLVE_BENEATH and ENOENT for RESOLVE_IN_ROOT.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At docs/internals.md, line 1754:

<comment>This documents the wrong `openat2(O_CREAT)` result: the analogous `/dev/bus/clb -> ../../lk/cfb` case fails under both resolve modes and leaves the target uncreated. Say that `/lk/cfr` is not created, while Linux returns `EXDEV` for `RESOLVE_BENEATH` and `ENOENT` for `RESOLVE_IN_ROOT`.</comment>

<file context>
@@ -1708,54 +1708,89 @@ not exist is left to the intercepts, which answer `ENOENT`, though calls that
+`ENOENT` where Linux reaches the node. The check does not follow a link that
+ends an `O_CREAT` name, and leaves that link to the host as well, so with
+`/dev/bus/clr` linked to `../../lk/cfr`, such an `openat2` of `clr` from
+`/dev/bus` creates `/lk/cfr` where Linux answers `EXDEV` under `RESOLVE_BENEATH`
+and `ENOENT` under `RESOLVE_IN_ROOT`. Under `RESOLVE_NO_MAGICLINKS` the name a
+followed link leads to gets the check `openat2` gives a typed name, which drops
</file context>

@jotpalch

jotpalch commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Pushed e93cecc, with a reply on each open thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants