Skip to content

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

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

jotpalch wants to merge 3 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, where Linux answers ELOOP at the 41st, because the path layer and the sysroot resolver each count from zero. For the same reason, 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.

The first commit lets a sysroot resolver go on from a walk its caller started, with every caller passing NULL, and the second passes it the links path_backing_dotdot follows. The third follows a link wherever it sits in a /dev/bus name. docs/internals.md states what still differs from Linux: a link outside /dev/bus whose target is under it, as from /lk/u to /dev/bus/usb, is followed by the sysroot resolver alone; on a sysroot without dev/bus/usb, a .. that pops /dev/bus/usb is left to the backing, which cannot walk it, so /dev/bus/usb/../link/001/001 is ENOENT; and a link replaced between its lookup and the read of its target fails the walk with EINVAL.

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, and make test-matrix-elfuse-aarch64 gives 317 passed, 1 failed and 10 skipped on both. The failure in all four runs is test-thread-churn timing out. The new checks in test-usb-sysfs-sysroot also pass with the sysroot on a case-sensitive volume, where the sysroot resolver takes its byte-exact walk.


Summary by cubic

Fixes how symlinks in /dev/bus names resolve so the path layer follows them in the guest namespace, counting toward the same MAXSYMLINKS limit as the sysroot resolver. Both the sysroot resolvers and the path layer now share one link count and relative-target state, so names reached through links take the same host-fallback rules as links the resolver follows itself. With a sysroot link from /dev/bus/link to usb, /dev/bus/link/001/001 and /dev/bus/link/001/../001/001 now serve the usbfs node, where main answered ENOENT; a chain of 41 links now fails with ELOOP, and a relative link to outside the sysroot no longer reaches a host file.

  • path_resolve_sysroot_* and proc_resolve_sysroot_* take a path_walk_t describing the links the caller already followed; every existing caller passes NULL.
  • path_dev_bus_follow_link follows the first backing link in a /dev/bus name, with the link resolved in the guest namespace and a trailing slash and stat following a final link while lstat and readlink still see it.
  • docs/internals.md documents the remaining differences from Linux: links outside /dev/bus targeting it, a .. that pops /dev/bus/usb on a sysroot without it, and a link replaced between lookup and read.

Written for commit ecc0bd5. Summary will update on new commits.

View guided diff

The path layer follows some links itself before it hands a name to a
sysroot resolver, and the resolver starts its walk as if none had been
followed: its MAXSYMLINKS count begins at zero, and a name reached
through a relative link is free to fall back to the host, which the
resolver refuses after a relative link of its own.

Each resolver 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, so a name the caller reached through links
takes the host fallback rule for a followed link rather than the one
for a name the guest typed. 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.

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. It resolves 40 links before a '..' and gets ELOOP for 41,
and 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. On c57282f the 41-link and relative checks fail. Both fail with
the walk left out of path_translate_at, and the relative one with the
relative target left unrecorded or with the resolver taking the name it
is handed for one the guest typed.
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.
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 sysroot resolver would follow it, so lstat and readlink still see
the link. path_backing_dotdot is left with names that hold no link, and
the walk goes on in the sysroot resolver only once the name has left
/dev/bus: a name left there holds no link to follow, and handed the
walk, the create resolver would refuse mkdir through a link to usb with
ELOOP where mkdir under /dev/bus/usb gets ENOENT.

test-usb-sysfs-sysroot reaches the node through such a link from an
absolute name, after a '..', against a /dev/bus dirfd and from a
/dev/bus cwd, checks stat, lstat and readlink of the link and mkdir
through it, and repeats the link count and host checks with no '..'
after the first link. With links left unfollowed, those six checks fail
and so does each check with a link before a '..'. The check of the link
itself fails with a link that ends the name never or always followed,
and the mkdir check with the walk handed over for a name left in
/dev/bus. With the link before a '..' left unfollowed, the checks that
have one fail, as do the matrix columns fold-in-link and fold-in-loop,
and with the walk or the relative target left out of what the resolver
is handed, the 41-link or relative checks fail.

@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.

3 issues 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>

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

<violation number="1" location="src/syscall/path.c:932">
P2: This drops the shared `MAXSYMLINKS` count for paths that remain lexically under `/dev/bus` but continue into backing components after `..`. A chain split across that boundary can follow more than 40 links instead of returning `ELOOP`; preserve the caller walk for the resolver while keeping synthetic `/dev/bus` names out of backing resolution.</violation>
</file>

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

<violation number="1" location="src/syscall/proc-state.c:1089">
P2: The resolver does not continue a caller-started walk when the target lands under a forced sysroot root such as `/tmp`. Subsequent symlinks are resolved by `sysroot_path_exists()` without the caller’s link count, allowing chains beyond the shared `MAXSYMLINKS` limit; run the link walker from `links` before applying the forced-root fallback rules.</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"; \\

Comment thread src/syscall/path.c
if (path_rewrite_usb_aliases(tx, flags, &walk) < 0)
return -1;
const path_walk_t *walked =
path_prefix_match(tx->guest_path, "/dev/bus", 8) ? NULL : &walk;

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 drops the shared MAXSYMLINKS count for paths that remain lexically under /dev/bus but continue into backing components after ... A chain split across that boundary can follow more than 40 links instead of returning ELOOP; preserve the caller walk for the resolver while keeping synthetic /dev/bus names out of backing resolution.

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/path.c, line 932:

<comment>This drops the shared `MAXSYMLINKS` count for paths that remain lexically under `/dev/bus` but continue into backing components after `..`. A chain split across that boundary can follow more than 40 links instead of returning `ELOOP`; preserve the caller walk for the resolver while keeping synthetic `/dev/bus` names out of backing resolution.</comment>

<file context>
@@ -839,8 +920,16 @@ int path_translate_at(guest_fd_t dirfd,
+    if (path_rewrite_usb_aliases(tx, flags, &walk) < 0)
         return -1;
+    const path_walk_t *walked =
+        path_prefix_match(tx->guest_path, "/dev/bus", 8) ? NULL : &walk;
 
     /* /dev/shm/<leaf> maps into the per-UID host backing dir, through the same
</file context>

Comment thread src/syscall/proc-state.c
bool folded = false;
bool followed_link = false;
bool followed_relative_link = false;
unsigned links = walked ? walked->links : 0;

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 resolver does not continue a caller-started walk when the target lands under a forced sysroot root such as /tmp. Subsequent symlinks are resolved by sysroot_path_exists() without the caller’s link count, allowing chains beyond the shared MAXSYMLINKS limit; run the link walker from links before applying the forced-root fallback rules.

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 1089:

<comment>The resolver does not continue a caller-started walk when the target lands under a forced sysroot root such as `/tmp`. Subsequent symlinks are resolved by `sysroot_path_exists()` without the caller’s link count, allowing chains beyond the shared `MAXSYMLINKS` limit; run the link walker from `links` before applying the forced-root fallback rules.</comment>

<file context>
@@ -1082,8 +1086,9 @@ static const char *proc_resolve_sysroot_path_flags(const char *path,
     bool folded = false;
-    bool followed_link = false;
-    bool followed_relative_link = false;
+    unsigned links = walked ? walked->links : 0;
+    bool followed_link = links > 0;
+    bool followed_relative_link = walked && walked->relative;
</file context>

Comment thread src/syscall/path.c
/* 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.

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.

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