Follow /dev/bus links in the guest namespace

JO
5h ago
+423-123

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.