From 95c677e4862c64c1c3b2d008d34c806fbd31967d Mon Sep 17 00:00:00 2001 From: The412Banner <205237651+The412Banner@users.noreply.github.com> Date: Sat, 5 Dec 2026 20:46:34 +0000 Subject: [PATCH] fix(native): answer a refused memfd reopen when the copy's mode cannot be set The copy that answers a refused read-only reopen of an own memfd was discarded whenever fchmod on it failed. Every other step of the copy passes on Samsung Android 17 phones, which still refuse the reopen with EACCES, so the mode change is the step their policy denies. The mode is now set only when the new memfd differs from the original, and a refused mode change keeps the copy. Easy Anti-Cheat's module load from a memfd goes through this path in native and FEX processes. --- tools/nativeguest/hook/files.c | 8 ++- tools/tests/test_native_hook_memfd_reopen.py | 59 +++++++++++++++++++- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/tools/nativeguest/hook/files.c b/tools/nativeguest/hook/files.c index 7ea658d006..9dd60ee88f 100644 --- a/tools/nativeguest/hook/files.c +++ b/tools/nativeguest/hook/files.c @@ -273,10 +273,14 @@ static int copy_of_memfd(int src, int flags) { if (st.st_mode & 0111) copy = call_orig_syscall(SYS_memfd_create, (unsigned long)name, mfd_flags | MFD_EXEC, 0, 0, 0, 0); if (copy < 0) copy = call_orig_syscall(SYS_memfd_create, (unsigned long)name, mfd_flags, 0, 0, 0, 0); if (copy < 0) return -1; + struct stat made; int ok = copy_memfd_contents(src, (int)copy, st.st_size) == 0 && call_orig_syscall(SYS_lseek, (unsigned long)copy, 0, SEEK_SET, 0, 0, 0) == 0 && - call_orig_syscall(SYS_fchmod, (unsigned long)copy, st.st_mode & 07777, 0, 0, 0, 0) == 0 && - call_orig_syscall(SYS_fcntl, (unsigned long)copy, F_ADD_SEALS, (unsigned long)(seals | F_SEAL_WRITE | F_SEAL_GROW | F_SEAL_SHRINK), 0, 0, 0) == 0; + call_orig_syscall(SYS_fstat, (unsigned long)copy, (unsigned long)&made, 0, 0, 0, 0) == 0; + if (ok && (made.st_mode & 07777) != (st.st_mode & 07777) && + call_orig_syscall(SYS_fchmod, (unsigned long)copy, st.st_mode & 07777, 0, 0, 0, 0) != 0) + ok = errno == EACCES || errno == EPERM; + ok = ok && call_orig_syscall(SYS_fcntl, (unsigned long)copy, F_ADD_SEALS, (unsigned long)(seals | F_SEAL_WRITE | F_SEAL_GROW | F_SEAL_SHRINK), 0, 0, 0) == 0; if (!ok) { call_orig_syscall(SYS_close, (unsigned long)copy, 0, 0, 0, 0, 0); return -1; diff --git a/tools/tests/test_native_hook_memfd_reopen.py b/tools/tests/test_native_hook_memfd_reopen.py index 44cac88ca1..493d78bc49 100644 --- a/tools/tests/test_native_hook_memfd_reopen.py +++ b/tools/tests/test_native_hook_memfd_reopen.py @@ -93,6 +93,7 @@ int main(int argc, char **argv) { } snprintf(path, sizeof(path), "/proc/self/fd/%d", src); report("keep-cloexec-off", open(path, O_RDONLY), src); + printf("syscall-fchmod\t%d\n", syscall(SYS_fchmod, src, 0) == 0 ? 0 : errno); report("write", open(path, O_RDWR), src); int foreign = memfd_create("probe-foreign", 0); fchmod(foreign, 0); @@ -138,6 +139,37 @@ int main(int argc, char **argv) { """ +CHMOD_REFUSED = r""" +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +long syscall(long number, ...) { + va_list ap; + va_start(ap, number); + long a[6]; + for (int i = 0; i < 6; i++) a[i] = va_arg(ap, long); + va_end(ap); + long (*next)(long, ...) = (long (*)(long, ...))dlsym(RTLD_NEXT, "syscall"); + if (number == SYS_fchmod) { + char link[64], target[64]; + snprintf(link, sizeof(link), "/proc/self/fd/%ld", a[0]); + long n = next(SYS_readlinkat, AT_FDCWD, link, target, sizeof(target) - 1); + if (n > 7 && strncmp(target, "/memfd:", 7) == 0) { + errno = EACCES; + return -1; + } + } + return next(number, a[0], a[1], a[2], a[3], a[4], a[5]); +} +""" + + class NativeHookMemfdReopenTest(unittest.TestCase): @classmethod def setUpClass(cls): @@ -153,12 +185,16 @@ class NativeHookMemfdReopenTest(unittest.TestCase): (tmp / "probe.c").write_text(PROBE) cls.probe = tmp / "probe" subprocess.run([cls.cc, "-O1", "-o", str(cls.probe), str(tmp / "probe.c")], check=True) + (tmp / "chmod_refused.c").write_text(CHMOD_REFUSED) + cls.chmod_refused_lib = tmp / "chmod_refused.so" + subprocess.run([cls.cc, "-shared", "-fPIC", "-O1", "-o", str(cls.chmod_refused_lib), str(tmp / "chmod_refused.c"), "-ldl"], check=True) cls.root = tmp / "root" cls.root.mkdir() cls.plain = cls.root / "plain" cls.bare = cls.run_probe(False) cls.kernel_openat2 = cls.bare["kernel-openat2"] == ["1"] cls.hooked = cls.run_probe(True, *(["how"] if cls.kernel_openat2 else [])) + cls.chmod_refused = cls.run_probe(True, preload_after=cls.chmod_refused_lib) @classmethod def tearDownClass(cls): @@ -166,10 +202,11 @@ class NativeHookMemfdReopenTest(unittest.TestCase): cls.tmp.cleanup() @classmethod - def run_probe(cls, hook, *extra): + def run_probe(cls, hook, *extra, preload_after=None): if cls.plain.exists(): os.chmod(cls.plain, 0o600) - hook_env = {"DDNATV_ROOT": str(cls.root), "LD_PRELOAD": str(cls.hook)} if hook else {} + preload = str(cls.hook) + (f":{preload_after}" if preload_after else "") + hook_env = {"DDNATV_ROOT": str(cls.root), "LD_PRELOAD": preload} if hook else {} env = {k: v for k, v in os.environ.items() if k not in ("LD_PRELOAD", "DDNATV_ROOT")} if cls.prefix: cmd = list(cls.prefix) + [arg for key, value in hook_env.items() for arg in ("-E", f"{key}={value}")] @@ -201,6 +238,24 @@ class NativeHookMemfdReopenTest(unittest.TestCase): self.assertEqual("100003", offset) self.assertEqual("0", mode) + def test_a_copy_whose_mode_cannot_be_set_still_answers_the_reopen(self): + self.assertEqual(["0"], self.hooked["syscall-fchmod"]) + self.assertEqual([str(errno.EACCES)], self.chmod_refused["syscall-fchmod"]) + want = self.chmod_refused["want"][0] + for form in ("self", "thread", "pid", "dev"): + for how in ("open", "openat", "syscall"): + with self.subTest(form=form, how=how): + status, total, seals, name, cloexec, offset, mode, unwritable, fixed = self.chmod_refused[f"{form}-{how}"] + self.assertEqual("ok", status) + self.assertEqual(want, total) + self.assertEqual(str(0x2 | 0x4 | 0x8), seals) + self.assertEqual("1", unwritable) + self.assertEqual("1", fixed) + self.assertEqual("/memfd:probe-module", name) + self.assertEqual("1", cloexec) + self.assertEqual(["err", str(errno.EACCES)], self.chmod_refused["write"]) + self.assertEqual(["err", str(errno.EACCES)], self.chmod_refused["plain-file"]) + def test_close_on_exec_follows_the_open_flags(self): self.assertEqual("0", self.hooked["keep-cloexec-off"][4])