Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 34 additions & 21 deletions docs/internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -1708,7 +1708,7 @@ not exist is left to the intercepts, which answer `ENOENT`, though calls that
skip the intercepts, such as `chmod`, still reach the backing for it. A `..`
after any other name under `/dev/bus` belongs to the backing, so the path layer
looks the name up there and applies the `..` only to a directory the guest may
search, following the name first when it is a symbolic link, as Linux does. The
search, as Linux does. The
guest's effective user and group IDs and the supplementary groups `getgroups`
reports are judged against the mode bits and the owner and group its `stat`
reports, which follow a `chown` the host refused, and root may search any
Expand All @@ -1721,28 +1721,41 @@ not root. A relative name is folded only through `/dev/bus` and `/dev/bus/usb`,
so `099/../001/001` from a `/dev/bus/usb` cwd and `other/f/../../../null` from a
`/dev/bus` cwd go through the same steps.

A symbolic link the backing holds under `/dev/bus` is followed by the path layer
too, wherever the walk meets it, and the walk goes on from its target, so the
link resolves in the guest namespace, where `/dev/bus/usb` is the layer's: with
a sysroot link from `/dev/bus/link` to `usb`, `/dev/bus/link/001/001` and
`/dev/bus/link/001/../001/001` are the usbfs node. A link that ends the name is
followed when the sysroot resolver would follow it, so `stat` and a trailing
slash reach `/dev/bus/usb` while `lstat` and `readlink` see the link. The links
the path layer follows go on to the sysroot resolver with the name they lead to
once that name leaves `/dev/bus`, so they count toward one `MAXSYMLINKS` with
the links it follows after them, and one with a relative target keeps a host
fallback for that name refused, as after a relative link the resolver follows
itself.

A name that enters `/dev/bus` through a `..` from outside it is not checked this
way: `/etc/../dev/bus/usb/099/001` reaches a `dev/bus/usb/099/001` the sysroot
plants, where Linux answers `ENOENT`. Only the name right before a `..` has its
link followed in the guest namespace: a link earlier in a `/dev/bus` name is
followed in the sysroot alone, which does not hold the layer's `/dev/bus/usb`,
so with a sysroot link from `/dev/bus/link` to `usb`,
`/dev/bus/link/001/../001/001` and `/dev/bus/link/001/001` are `ENOENT` where
Linux serves the node. Nor do the links followed for it count toward the
`MAXSYMLINKS` the sysroot resolver applies once a target leaves `/dev/bus`, so a
chain split between the two can follow more links than Linux allows before
`ELOOP`. Only the search on the name before a `..` uses the guest's IDs; the
host walks every other name with the host user's credentials, as for any sysroot
path. With `mine` at mode `700` and owned by the host user, the default guest
uid 1000 reaches `/dev/bus/mine/f`, where Linux answers `EACCES`, and gets
`EACCES`, as on Linux, for `/dev/bus/mine/../usb/001/001`. Under `--fakeroot`,
with `other` at mode `000` and a host user other than root,
`/dev/bus/other/f/../../usb/001/001` is `EACCES` for a file `f`, where Linux
answers `ENOTDIR`. The search check and the lookup of the rewritten name are two
steps, and the rewritten name drops the name checked, so the walk goes on when
another thread or process renames or replaces that name between them, where
Linux walks the name once and fails at a missing name or a file, or follows the
new link.
plants, where Linux answers `ENOENT`. Nor is a link outside `/dev/bus` whose
target is under it: the sysroot resolver follows it alone, so with a sysroot
link from `/lk/u` to `/dev/bus/usb`, `/lk/u/001/001` is `ENOENT` where Linux
serves the node. On a sysroot without `dev/bus/usb`, a `..` that pops
`/dev/bus/usb` onto a name outside it is left to the backing, which cannot walk
it, so `/dev/bus/usb/../other/f` and `/dev/bus/usb/../link/001/001` are `ENOENT`
where Linux reaches the backing's file and the node. Only the search on the name
before a `..` uses the guest's IDs; the host walks every other name with the
host user's credentials, as for any sysroot path. With `mine` at mode `700` and
owned by the host user, the default guest uid 1000 reaches `/dev/bus/mine/f`,
where Linux answers `EACCES`, and gets `EACCES`, as on Linux, for
`/dev/bus/mine/../usb/001/001`. Under `--fakeroot`, with `other` at mode `000`
and a host user other than root, `/dev/bus/other/f/../../usb/001/001` is
`EACCES` for a file `f`, where Linux answers `ENOTDIR`. The search check and the
lookup of the rewritten name are two steps, and the rewritten name drops the
name checked, so the walk goes on when another thread or process renames or
replaces that name between them, where Linux walks the name once and fails at a
missing name or a file, or follows the new link. Following a link is two steps
as well, the lookup that finds it and the read of its target, so a link replaced
between them by another kind of file fails the walk with `EINVAL`.

### Filesystem Identity Of A Descriptor

Expand Down
9 changes: 8 additions & 1 deletion mk/tests.mk
Original file line number Diff line number Diff line change
Expand Up @@ -1836,7 +1836,14 @@ test-usb-sysfs-sysroot: $(ELFUSE_BIN) $(TEST_DIR)/test-usb-sysfs-sysroot
ELFUSE_USB_FIXTURE=1 $(ELFUSE_BIN) --sysroot "$$sysroot" --user 4242:4242 \
$(TEST_DIR)/test-usb-sysfs-sysroot search; \
ELFUSE_USB_FIXTURE=1 $(ELFUSE_BIN) --sysroot "$$sysroot" --fakeroot \
$(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"; \
Comment on lines +1841 to +1842

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"; \\

ln -s "$$tmpdir/host" "$$sysroot/dev/bus/habs"; \
ln -s "$$tmpdir/host" "$$sysroot/lk/habs"; \
ELFUSE_USB_FIXTURE=1 $(ELFUSE_BIN) --sysroot "$$sysroot" \
$(TEST_DIR)/test-usb-sysfs-sysroot backing

## Every entry point that can name something under /sys or /dev/bus, against
## every class of name those trees can hold. The layer synthesizes one subtree
Expand Down
Loading
Loading