mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-24 23:36:04 +00:00
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>
This commit is contained in:
1 parent
7d942367fd
commit
7630173b24
3 files changed
+74
-19
No files matched your search
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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()
|
||||
Reference in new issue
Block a user