Repository navigation
Apply a /dev/bus '..' only to a directory - #444
Conversation
db51cc5 to
7088d35
Compare
7088d35 to
855ba9c
Compare
| errno = ENOTDIR; | ||
| return -1; | ||
| } else { | ||
| /* A final '..' leaves '.', as the climb rewrite does. */ |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
855ba9c to
9528be9
Compare
9528be9 to
f5c6ad7
Compare
|
Pushed f5c6ad7 and answered both comments inline. |
| const char *hp = | ||
| path_resolve_sysroot_nofollow_path(name, host, sizeof(host)); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
jserv
left a comment
There was a problem hiding this comment.
Rebase onto latest main branch and resolve conflicts.
f5c6ad7 to
424447a
Compare
| if [ "$$(id -u)" != 0 ]; then \ | ||
| ELFUSE_USB_FIXTURE=1 $(ELFUSE_BIN) --sysroot "$$sysroot" \ | ||
| $(TEST_DIR)/test-usb-sysfs-sysroot search; \ | ||
| fi |
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
Applied in f1da5a6 as suggested. With an id on PATH that prints 0, the lane prints that line and passes.
424447a to
f1da5a6
Compare
Benchmark changes outside ±20%Compared Improvements
Workflow run · commit |
| struct stat st; | ||
| const char *hp = | ||
| path_resolve_sysroot_nofollow_path(name, host, sizeof(host)); | ||
| if (!hp || lstat(hp, &st) < 0) |
There was a problem hiding this comment.
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.
| } else if (!S_ISDIR(st.st_mode)) { | ||
| errno = ENOTDIR; | ||
| return -1; | ||
| } else if (faccessat(AT_FDCWD, hp, X_OK, AT_EACCESS) < 0) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| } else if (!S_ISDIR(st.st_mode)) { | ||
| errno = ENOTDIR; | ||
| return -1; | ||
| } else if (faccessat(AT_FDCWD, hp, X_OK, AT_EACCESS) < 0) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
f1da5a6 to
25d8716
Compare
|
Pushed 25d8716 and answered the three comments inline. |
|
Thank @jotpalch for contributing! |
/dev/bus/usb/099/../001/001answers as the usbfs node and099/../../../nullfrom a/dev/bus/usbcwd reaches/dev/null, where Linux answersENOENTfor both, because the layer applies a..without checking that the name before it exists. On a sysroot with adev/bus/usb/099directory,/dev/bus/usb/099/../../other/freaches the backing's file although/dev/bus/usb/099itself isENOENT, because the layer leaves that walk to the backing, which has a099the layer does not. With this change, every name under/dev/bus/usbis the layer's, so a..after a bus no device has, or after a malformed bus or device number, fails the walk withENOENT. #439 left the..after an absent bus for a separate change.A name outside
/dev/bus/usbis folded as if the backing had a directory there, so/dev/bus/nosuch/../usb/001/001and, on a sysroot where/dev/bus/other/fis a file,/dev/bus/other/f/../../usb/099/../001/001answer as the node, where Linux answersENOENTandENOTDIR. On a sysroot where/dev/bus/otheris at mode000,other/../usb/001/001from a/dev/buscwd answers as the node as well, where Linux answersEACCESto 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.mdstates where this differs from Linux.Reproduce with the fixture devices and a static aarch64 busybox:
On
mainthese printcharacter special file,reachedandcharacter special file. With this change the first and third fail withNo such file or directoryand the second printsrefused, 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.shpasses 102 of 103 and the failure stopsmake checkthere; every lane after it passes when run separately.make test-matrix-elfuse-aarch64gives 317 passed, 1 failed and 10 skipped. The failure in both istest-thread-churntiming out, which it also does onmainin both. On both trees it passes when run alone, and all 103 tests pass whentests/driver.shruns outsidemake check.