From 7630173b2431c8b5e1051ff836af3913cb712f9d Mon Sep 17 00:00:00 2001 From: MaxsTechReview Date: Tue, 24 Nov 2026 17:19:24 -0500 Subject: [PATCH] fix(ddsession): keep path and status buffers off the wrapper fast path (#3071) dd_descriptor_unlink, dd_descriptor_rmdir and dd_descriptor_rename put one or two PATH_MAX buffers on their own frame (12 KB, 24 KB), and dd_status_without_tracer put its 16 KB status buffer there, even on the common path that only calls through to libc. Threads with small stacks, such as .NET or Wine threads near their guard page, could overflow in an otherwise plain unlink, rename or open. The wrappers now do only the cheap checks and call the real function; the buffers live in noinline helpers that run only when the descriptor store or the status rewrite is actually needed. A test compiles both sources with -fstack-usage and keeps each wrapper frame under 1 KB. Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> --- tools/linuxfs/preload/descriptor.c | 42 +++++++++++++++-------- tools/linuxfs/preload/tracer.c | 12 ++++--- tools/tests/test_preload_wrapper_stack.py | 39 +++++++++++++++++++++ 3 files changed, 74 insertions(+), 19 deletions(-) create mode 100644 tools/tests/test_preload_wrapper_stack.py diff --git a/tools/linuxfs/preload/descriptor.c b/tools/linuxfs/preload/descriptor.c index 686030988c..5f2c19ed22 100644 --- a/tools/linuxfs/preload/descriptor.c +++ b/tools/linuxfs/preload/descriptor.c @@ -31,7 +31,7 @@ static int store_root(char *out, size_t size) { return n > 0 && (size_t)n < size ? n : -1; } -static int store_exists(void) { +static __attribute__((noinline)) int store_exists(void) { char root[PATH_MAX]; if (store_seen) return 1; @@ -223,10 +223,18 @@ int fremovexattr(int fd, const char *name) { return 0; } +static int watched(void) { + int saved = errno, found; + + found = dd_is_wineserver() && store_exists(); + errno = saved; + return found; +} + static int tracked(const char *path, char *out, size_t size) { int saved = errno, found; - found = dd_is_wineserver() && store_exists() && resolved(path, out, size) == 0; + found = resolved(path, out, size) == 0; errno = saved; return found; } @@ -258,22 +266,30 @@ static void follow(const char *from, const char *to) { errno = saved; } -__attribute__((visibility("hidden"))) -int dd_descriptor_unlink(const char *path, int (*real)(const char *)) { +static __attribute__((noinline)) int removed(const char *path, int (*real)(const char *), int folder) { char target[PATH_MAX]; int known = tracked(path, target, sizeof(target)), result = real(path); - if (result == 0 && known) forget(target, 0); + if (result == 0 && known) forget(target, folder); + return result; +} + +static __attribute__((noinline)) int moved(const char *from, const char *to, int (*real)(const char *, const char *)) { + char old[PATH_MAX], new[PATH_MAX]; + int known = tracked(from, old, sizeof(old)), result = real(from, to); + + if (result == 0 && known && tracked(to, new, sizeof(new))) follow(old, new); return result; } __attribute__((visibility("hidden"))) -int dd_descriptor_rmdir(const char *path, int (*real)(const char *)) { - char target[PATH_MAX]; - int known = tracked(path, target, sizeof(target)), result = real(path); +int dd_descriptor_unlink(const char *path, int (*real)(const char *)) { + return watched() ? removed(path, real, 0) : real(path); +} - if (result == 0 && known) forget(target, 1); - return result; +__attribute__((visibility("hidden"))) +int dd_descriptor_rmdir(const char *path, int (*real)(const char *)) { + return watched() ? removed(path, real, 1) : real(path); } __attribute__((visibility("hidden"))) @@ -283,9 +299,5 @@ int dd_descriptor_remove(const char *path, int (*real)(const char *)) { __attribute__((visibility("hidden"))) int dd_descriptor_rename(const char *from, const char *to, int (*real)(const char *, const char *)) { - char old[PATH_MAX], new[PATH_MAX]; - int known = tracked(from, old, sizeof(old)), result = real(from, to); - - if (result == 0 && known && tracked(to, new, sizeof(new))) follow(old, new); - return result; + return watched() ? moved(from, to, real) : real(from, to); } diff --git a/tools/linuxfs/preload/tracer.c b/tools/linuxfs/preload/tracer.c index ec8dd003f9..4b12a1d6a7 100644 --- a/tools/linuxfs/preload/tracer.c +++ b/tools/linuxfs/preload/tracer.c @@ -72,15 +72,12 @@ static int read_only_copy(const char *data, size_t len, int flags) { return ro; } -__attribute__((visibility("hidden"))) int dd_status_without_tracer(const char *path, int flags, const void *caller) { +static __attribute__((noinline)) int status_copy(const char *path, int flags, long pid) { char text[STATUS_MAX]; size_t len = 0; int saved = errno; int out = -1; - long pid; - if ((flags & O_ACCMODE) != O_RDONLY || (pid = status_pid(path)) < 0 || is_web_helper(caller) || dd_from_ntdll(caller)) - return -1; // through the next open(): the native userland's hook gives the app's ids as root's there static int (*next_open)(const char *, int, ...) = NULL; if (!next_open) next_open = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open"); @@ -116,3 +113,10 @@ done: errno = saved; return out; } + +__attribute__((visibility("hidden"))) int dd_status_without_tracer(const char *path, int flags, const void *caller) { + long pid; + if ((flags & O_ACCMODE) != O_RDONLY || (pid = status_pid(path)) < 0 || is_web_helper(caller) || dd_from_ntdll(caller)) + return -1; + return status_copy(path, flags, pid); +} diff --git a/tools/tests/test_preload_wrapper_stack.py b/tools/tests/test_preload_wrapper_stack.py new file mode 100644 index 0000000000..409799921e --- /dev/null +++ b/tools/tests/test_preload_wrapper_stack.py @@ -0,0 +1,39 @@ +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + +PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload" +LIMIT = 1024 +WRAPPERS = { + "descriptor.c": ("dd_descriptor_unlink", "dd_descriptor_rmdir", "dd_descriptor_remove", "dd_descriptor_rename"), + "tracer.c": ("dd_status_without_tracer",), +} + + +def frames(source, build): + obj = build / (source + ".o") + subprocess.run(["cc", "-c", "-fPIC", "-O2", "-Wall", "-fstack-usage", "-o", str(obj), str(PRELOAD / source)], + check=True) + sizes = {} + for line in obj.with_suffix(".su").read_text().splitlines(): + place, size, _ = line.split("\t") + sizes[place.rsplit(":", 1)[-1]] = int(size) + return sizes + + +@unittest.skipUnless(shutil.which("cc"), "needs a C compiler") +class PreloadWrapperStackTest(unittest.TestCase): + def test_wrappers_keep_path_buffers_off_their_frames(self): + with tempfile.TemporaryDirectory() as build: + for source, names in WRAPPERS.items(): + sizes = frames(source, Path(build)) + for name in names: + with self.subTest(function=name): + self.assertIn(name, sizes) + self.assertLess(sizes[name], LIMIT) + + +if __name__ == "__main__": + unittest.main()