fix(hook): /dev/fd in a forked process names that process's own descriptors (#3105)

* fix(hook): /dev/fd in a forked process names that process's own descriptors

The /dev/fd and /dev/std* binds point at /proc/self/fd. Their sources were
resolved with realpath when the binds were loaded, which turned /proc/self
into the loading process's pid. A child that forks without exec, such as a
bash subshell running cat < <(...), then opened its parent's descriptors
and failed with ENOENT. Sources under /proc keep their own path.

* fix(hook): keep canonical bind sources for /proc paths that name no process

* fix(hook): only /proc bind sources that name a process keep their form
This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-23 21:17:13 -05:00
1 parent 8ee80aa20f
commit 5cb8e7fdf3
2 files changed
+34 -1

No files matched your search

+33
View File
@@ -15,6 +15,7 @@ PROBE = r"""
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char **argv) {
@@ -39,6 +40,26 @@ int main(int argc, char **argv) {
if (fd >= 0) close(fd);
continue;
}
if (strncmp(argv[i], "fork:", 5) == 0) {
fflush(stdout);
pid_t pid = fork();
if (pid == 0) {
int p[2];
char path[64], data[8] = "-";
if (pipe(p) == 0 && write(p[1], "x", 1) == 1 && dup2(p[0], 63) == 63) {
snprintf(path, sizeof(path), "%s/63", argv[i] + 5);
int fd = open(path, O_RDONLY);
ssize_t n = fd >= 0 ? read(fd, data, sizeof(data) - 1) : -1;
if (n > 0) data[n] = 0;
if (fd >= 0) close(fd);
}
printf("%s\t%s\n", argv[i], data);
fflush(stdout);
_exit(0);
}
waitpid(pid, NULL, 0);
continue;
}
if (strncmp(argv[i], "cd:", 3) == 0) {
int ok = chdir(argv[i] + 3) == 0 && getcwd(cwd, sizeof(cwd)) != NULL;
printf("%s\t%s\n", argv[i], ok ? cwd : "-");
@@ -153,6 +174,18 @@ class NativeHookMountTest(unittest.TestCase):
self.assertEqual(out.returncode, 0, out.stderr)
return {line.split("\t")[0]: line.split("\t")[1:] for line in out.stdout.splitlines()}
def test_dev_fd_in_a_forked_child_names_the_child_descriptors(self):
out = self.run_probe("fork:/dev/fd", binds="/proc/self/fd:/dev/fd")
self.assertEqual(["x"], out["fork:/dev/fd"])
def test_a_mount_named_through_proc_self_root_reports_its_guest_path(self):
arg = f"cd:{GAME}/"
self.assertEqual(self.run_probe(arg, binds=f"/proc/self/root{self.library}:/mnt/library")[arg], [GAME])
def test_a_proc_mount_that_names_no_process_reports_its_guest_path(self):
arg = "cd:/mnt/sys/kernel/"
self.assertEqual(self.run_probe(arg, binds="/proc/self/root/proc/sys:/mnt/sys")[arg], ["/mnt/sys/kernel"])
def test_drive_link_stored_as_rootfs_path_reaches_the_mount(self):
path = f"/pfx/native/dosdevices/z:{GAME}/game.txt"
self.assertEqual(self.run_probe(path)[path], ["1", "library"])