Skip to content

Apply a /dev/bus '..' only to a directory - #444

Merged
jserv merged 1 commit into
sysprog21:mainfrom
jotpalch:pr-dotdot-absent-bus
Nov 21, 2026
Merged

jserv merged 1 commit into
sysprog21:mainfrom
jotpalch:pr-dotdot-absent-bus

Conversation

@jotpalch

@jotpalch jotpalch commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

/dev/bus/usb/099/../001/001 answers as the usbfs node and 099/../../../null from a /dev/bus/usb cwd reaches /dev/null, where Linux answers ENOENT for both, because the layer applies a .. without checking that the name before it exists. On a sysroot with a dev/bus/usb/099 directory, /dev/bus/usb/099/../../other/f reaches the backing's file although /dev/bus/usb/099 itself is ENOENT, because the layer leaves that walk to the backing, which has a 099 the layer does not. With this change, every name under /dev/bus/usb is the layer's, so a .. after a bus no device has, or after a malformed bus or device number, fails the walk with ENOENT. #439 left the .. after an absent bus for a separate change.

A name outside /dev/bus/usb is folded as if the backing had a directory there, so /dev/bus/nosuch/../usb/001/001 and, on a sysroot where /dev/bus/other/f is a file, /dev/bus/other/f/../../usb/099/../001/001 answer as the node, where Linux answers ENOENT and ENOTDIR. On a sysroot where /dev/bus/other is at mode 000, other/../usb/001/001 from a /dev/bus cwd answers as the node as well, where Linux answers EACCES to a caller that may not search /dev/bus/other. With this change, a .. after such a name is checked against the backing: the path layer looks the name up there, follows it first when it is a symbolic link, and applies the .. only to a directory the guest's IDs and groups may search, or any directory for root. docs/internals.md states where this differs from Linux.

Reproduce with the fixture devices and a static aarch64 busybox:

$ ELFUSE_USB_FIXTURE=1 build/elfuse busybox stat -c %F /dev/bus/usb/099/../001/001
$ ELFUSE_USB_FIXTURE=1 build/elfuse busybox sh -c 'cd /dev/bus/usb && test -c 099/../../../null && echo reached || echo refused'
$ ELFUSE_USB_FIXTURE=1 build/elfuse busybox stat -c %F /dev/bus/nosuch/../usb/001/001

On main these print character special file, reached and character special file. With this change the first and third fail with No such file or directory and the second prints refused, 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 and the failure stops make check there; every lane after it passes when run separately. make test-matrix-elfuse-aarch64 gives 317 passed, 1 failed and 10 skipped. The failure in both is test-thread-churn timing out, which it also does on main in both. On both trees it passes when run alone, and all 103 tests pass when tests/driver.sh runs outside make check.

cubic-dev-ai[bot]

This comment was marked as resolved.

@jotpalch
jotpalch force-pushed the pr-dotdot-absent-bus branch from db51cc5 to 7088d35 Compare October 19, 2026 07:25
cubic-dev-ai[bot]

This comment was marked as resolved.

@jotpalch
jotpalch force-pushed the pr-dotdot-absent-bus branch from 7088d35 to 855ba9c Compare October 19, 2026 17:28
@jotpalch jotpalch changed the title Fail a '..' after a missing /dev/bus/usb name Apply a /dev/bus '..' only to a directory Oct 19, 2026
Comment thread src/syscall/path.c
errno = ENOTDIR;
return -1;
} else {
/* A final '..' leaves '.', as the climb rewrite does. */

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.

The directory arm drops name/.. after an lstat. That lstat only needs search permission on the parents, so walking through name itself is never checked. With a sysroot /dev/bus/other at mode 000, stat("/dev/bus/other/../usb/001/001") serves the usbfs node, where Linux fails the walk at other with EACCES. Check access(hp, X_OK) before popping the component, and add a lane with a non-searchable backing directory.

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 9528be9: the directory arm calls access(hp, X_OK) before popping the component, so with a sysroot /dev/bus/other at mode 000, stat("/dev/bus/other/../usb/001/001") is EACCES, and so are /dev/bus/other/.. and other/../usb/001/001 from a /dev/bus cwd or against a /dev/bus descriptor. A link to such a directory reaches the same check once it is followed. The test-usb-sysfs-sysroot lane runs the guest a second time with /dev/bus/other at mode 000 and checks those four spellings, which fail with the access call taken out. It is a second run because the first one needs the sysroot's /dev empty for its rmdir("/dev/bus/..") check.

@jotpalch
jotpalch force-pushed the pr-dotdot-absent-bus branch from 855ba9c to 9528be9 Compare October 20, 2026 06:33
cubic-dev-ai[bot]

This comment was marked as resolved.

Comment thread src/syscall/path.c Resolved
Comment thread src/syscall/path.c Resolved
@jotpalch
jotpalch force-pushed the pr-dotdot-absent-bus branch from 9528be9 to f5c6ad7 Compare November 5, 2026 17:21
@jotpalch

jotpalch commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

Pushed f5c6ad7 and answered both comments inline.

Comment thread mk/tests.mk Resolved
Comment thread src/runtime/usb-sysfs.h Resolved
Comment thread src/syscall/path.c
Comment on lines +618 to +619
const char *hp =
path_resolve_sysroot_nofollow_path(name, host, sizeof(host));

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.

Only the name right before the .. gets link handling here. Links earlier in name are followed by path_resolve_sysroot_nofollow_path, which sees only the sysroot. With a sysroot link /dev/bus/link -> usb, /dev/bus/link/001/../001/001 returns ENOENT because the sysroot has no dev/bus/usb/001, while Linux serves the node. main fails the same way (as does /dev/bus/link/001/001 with no ..), so this is not a regression, but the docs text that links are followed "as Linux does" claims more than this covers. Follow-up: resolve intermediate links in the guest namespace and restart the walk from the result, or state the limit in docs/internals.md.

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.

Reproduced: with a sysroot link from /dev/bus/link to usb, /dev/bus/link/001/../001/001 and /dev/bus/link/001/001 are ENOENT on main and on 424447a, and Linux 7.2 serves the node for both. 424447a states this limit in docs/internals.md, together with the MAXSYMLINKS gap from the other thread, and narrows the sentence about following a link to the name before the ... The follow-up I committed to there will resolve the earlier links in the guest namespace, restart the walk from the result, and carry one link count through both resolvers.

Comment thread src/syscall/path.c Resolved

@jserv jserv left a comment

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.

Rebase onto latest main branch and resolve conflicts.

@jotpalch
jotpalch force-pushed the pr-dotdot-absent-bus branch from f5c6ad7 to 424447a Compare November 6, 2026 04:11
@jotpalch

jotpalch commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto 170b5df in 424447a. The conflicts were in src/syscall/path.c, where main moved the climb loop into path_rewrite_usb_aliases; the four comments are answered inline.

Comment thread src/runtime/usb-sysfs.c Resolved
Comment thread mk/tests.mk Outdated
Comment on lines +1783 to +1786
if [ "$$(id -u)" != 0 ]; then \
ELFUSE_USB_FIXTURE=1 $(ELFUSE_BIN) --sysroot "$$sysroot" \
$(TEST_DIR)/test-usb-sysfs-sysroot search; \
fi

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.

As root the search run is skipped silently, so a CI job running as root reports this lane green while F4 never ran. Print a skip line so the missing coverage shows up in the log.

Suggested change
if [ "$$(id -u)" != 0 ]; then \
ELFUSE_USB_FIXTURE=1 $(ELFUSE_BIN) --sysroot "$$sysroot" \
$(TEST_DIR)/test-usb-sysfs-sysroot search; \
fi
if [ "$$(id -u)" != 0 ]; then \
ELFUSE_USB_FIXTURE=1 $(ELFUSE_BIN) --sysroot "$$sysroot" \
$(TEST_DIR)/test-usb-sysfs-sysroot search; \
else \
echo "test-usb-sysfs-sysroot search: SKIP (root may search mode 000)"; \
fi

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.

Applied in f1da5a6 as suggested. With an id on PATH that prints 0, the lane prints that line and passes.

@jotpalch
jotpalch force-pushed the pr-dotdot-absent-bus branch from 424447a to f1da5a6 Compare November 10, 2026 13:16
@github-actions

Copy link
Copy Markdown

Benchmark changes outside ±20%

Compared elfuse-aarch64 with the default branch's baseline. Lower is better.

Improvements

Metric Baseline Current Change
applications.git_status_ms 113.327 27.872 -75.4%
applications.make_ms 5850.39 1525.02 -73.9%
applications.python_startup_ms 117.826 24.581 -79.1%
applications.zstd_ms 112.975 32.845 -70.9%
lmbench.lat_proc.exec 112392 24668 -78.1%
lmbench.lat_proc.fork 116097 4774.9 -95.9%
lmbench.lat_syscall.read 1.72295 0.9271 -46.2%
lmbench.lat_syscall.write 1.7114 1.0126 -40.8%

Workflow run · commit f1da5a6f3dd2

Comment thread src/syscall/path.c Outdated
struct stat st;
const char *hp =
path_resolve_sysroot_nofollow_path(name, host, sizeof(host));
if (!hp || lstat(hp, &st) < 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.

path_resolve_sysroot_nofollow_path can return NULL without setting errno (the sysroot seed and containment failures), so this return -1 can hand path_translate_at a stale or zero errno. Set one explicitly on the !hp branch, e.g. if (!hp) { if (!errno) errno = ENOENT; return -1; }, or apply the same normalization the tx->host_path == NULL handling in path_translate_at does.

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.

Applied in 25d8716, which is also rebased onto 3876e08: errno is cleared before the lookup, so a NULL that leaves it unset returns ENOENT instead of a stale value.

Comment thread src/syscall/path.c Outdated
} else if (!S_ISDIR(st.st_mode)) {
errno = ENOTDIR;
return -1;
} else if (faccessat(AT_FDCWD, hp, X_OK, AT_EACCESS) < 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.

The search check asks the host kernel with the host user's credentials, while the rest of the guest permission model uses proc_get_euid()/proc_get_egid() (see path_check_intercept_access). Under --fakeroot the guest is euid 0 and Linux lets root search a mode 000 directory, but here /dev/bus/other/../usb/001/001 fails with EACCES because the host user cannot. Decide the verdict from the guest credentials (root passes, otherwise check the owner/group/other x bits from the lstat already in hand), and add a --fakeroot case to the search run.

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.

Applied in 25d8716: root passes, and otherwise exec_check_permission(&st) decides from proc_get_euid(), proc_get_egid() and the supplementary groups getgroups reports, against the owner and group the guest's stat reports, as exec_stat_image does for an exec. It takes the place of path_check_intercept_access here because that compares the effective group alone, where Linux's acl_permission_check counts the supplementary groups too. The search run goes again under --fakeroot, where /dev/bus/other/../usb/001/001 is the node, and the id -u skip goes, since the check judges the guest, uid 4242 or root, for any host user.

The rest of the walk runs as the host user. 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 the .. after mine is EACCES as on Linux. 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. docs/internals.md lists both with the other differences.

Comment thread src/syscall/path.c Outdated
} else if (!S_ISDIR(st.st_mode)) {
errno = ENOTDIR;
return -1;
} else if (faccessat(AT_FDCWD, hp, X_OK, AT_EACCESS) < 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.

Follow-up, not for this PR: the X_OK check and the rewrite are two steps, and the rewrite then drops the name it checked. If another guest thread renames or replaces other between faccessat and the rewritten lookup, the node is served for a walk that now crosses a missing name, a file, or a link elsewhere. The effect stays inside the layer's own node, so it is a correctness gap against Linux's single-walk semantics, not an escape. Worth a line in docs/internals.md next to the other stated differences, and a pinned-descriptor walk if it ever matters.

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.

Added in 25d8716, next to the other differences in docs/internals.md. The rewritten name can leave /dev/bus: /dev/bus/other/../../null reaches /dev/null. It is a name the guest could pass directly, so it is not an escape.

/dev/bus/usb/099/../001/001 answered as the usbfs node where Linux
answers ENOENT: the walk fails at 099, which does not exist, before the
'..' is applied. The layer folded each '..' without checking the name
before it, so stat and open served the node and 099/../../../null from a
/dev/bus/usb cwd reached /dev/null. On a sysroot with a dev/bus/usb/099
directory, /dev/bus/usb/099/../../other/f reached the backing's file,
since the layer left that walk to the backing, which has a 099 the layer
does not. A name outside /dev/bus/usb was folded as if the backing had a
directory there, so /dev/bus/nosuch/../usb/001/001 and, for a file f,
/dev/bus/other/f/../../usb/099/../001/001 served the node, where Linux
answers ENOENT and ENOTDIR. With /dev/bus/other at mode 000,
other/../usb/001/001 from a /dev/bus cwd served the node and
/dev/bus/other/.. answered as a directory, where Linux answers EACCES.

Every name under /dev/bus/usb is the layer's, so
usb_sysfs_resolve_dev_climb, which already checks each of the layer's
directories it pops, fails such a walk with ENOENT in path_translate_at.
A '..' after a /dev/bus name outside /dev/bus/usb is the backing's:
path_backing_dotdot looks the name up through the sysroot resolver,
follows a symbolic link, and applies the '..' only to a directory the
guest may search, since Linux checks search permission on a directory
before each component it looks up there, '..' included. The guest's
effective IDs and supplementary groups are judged against the mode bits
and the owner and group its stat reports, as exec_check_permission
judges an exec, and root may search any directory, as generic_permission
allows; the host user's credentials would refuse root under --fakeroot
and judge the host user rather than the guest. Only the name before a
'..' is judged by the guest's IDs; the host walks every other name with
the host user's credentials, as for any sysroot path. The links
path_backing_dotdot follows stop at MAXSYMLINKS but do not count toward
the sysroot resolver's limit once a target leaves /dev/bus, so a split
chain can follow more links than Linux allows. path_rebuild_under folds
a name relative to a /dev/bus cwd or descriptor only through /dev/bus
and /dev/bus/usb, so each '..' after that reaches the same checks.

The six new matrix columns were recorded on Linux 7.0 over a /dev/bus
that also holds a link to /etc and a link to itself; recording the table
again left every other cell unchanged. With the ENOENT return taken out,
shadow-dotdot and fold-in-absent fail 19 of their 20 cells; with the
backing lookup taken out, dev-climb-f, fold-in-file, fold-in-link and
fold-in-loop fail, as do the /dev/bus/nosuch checks in test-usb-sysfs,
and with the link left unfollowed, fold-in-link and fold-in-loop fail.
test-usb-sysfs-sysroot runs the guest again as uid 4242 and under
--fakeroot over /dev/bus directories at modes 000, 010 and 100; its
checks fail with the search check or root's exemption taken out, with
the host user's credentials, the real uid or the effective group alone
deciding, and, unless the host user is root, without the chown overlay.
@jotpalch
jotpalch force-pushed the pr-dotdot-absent-bus branch from f1da5a6 to 25d8716 Compare November 21, 2026 07:31
@jotpalch

Copy link
Copy Markdown
Contributor Author

Pushed 25d8716 and answered the three comments inline.

@jserv
jserv merged commit c57282f into sysprog21:main Nov 21, 2026
15 checks passed
@jserv

jserv commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Thank @jotpalch for contributing!

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