fix(preload): Windows games run from noexec shared storage under every Proton (#2594)

* fix(preload): Windows games run from noexec shared storage under every Proton

When mprotect(PROT_EXEC) on a private mapping is refused, libblsession
moves the pages into anonymous memory and retries, so Wine can make an
image's code sections executable on noexec mounts. FEX keeps its
existing handling.

* fix(preload): copy noexec image pages without faulting or dropping access

* fix(preload): install copied image pages with final permissions
This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-15 23:17:44 -05:00
1 parent 3d0c38c1d3
commit f65e7a3d43
4 files changed
+276 -10

No files matched your search

+1
View File
@@ -74,6 +74,7 @@ DroidDeck is [GPL-3.0](LICENSE). Steam and Proton belong to Valve Corporation. V
- x86 AppImages, and arm64 ones whose runtime can't unpack on Android, are unpacked with [uruntime](https://github.com/VHSgunzo/uruntime) by VHSgunzo (MIT), shipped unmodified with its licence.
- x86 programs linking OpenSSL 3 use the copy in the [Steam Linux Runtime 4.0](https://gitlab.steamos.cloud/steamrt/steamrt). x86 Linux games get the `/etc/asound.conf` that [pressure-vessel](https://gitlab.steamos.cloud/steamrt/steam-runtime-tools) writes.
- `WriteProcessMemory` hooks fall back to `/proc/<pid>/mem` the way [Wine](https://gitlab.winehq.org/wine/wine)'s `server/ptrace.c` did before Wine 11.
- Windows programs on noexec storage get executable copies of their image pages, following [@yujikaido](https://github.com/yujikaido)'s finding in [droiddeck-rp4pro](https://github.com/yujikaido/droiddeck-rp4pro) that Wine's own processes need the noexec shim.
- Wine write watches use userfaultfd support in [Proton](https://github.com/ValveSoftware/wine)'s `dlls/ntdll/unix/virtual.c` (LGPL-2.1-or-later). libblsession supplies the arm64 syscall number and answers `PAGEMAP_SCAN` from `/proc/self/pagemap`.
- libblsession runs Wine's arm64 `SIGUSR2` context handler (`dlls/ntdll/unix/signal_arm64.c`, LGPL-2.1-or-later) in place on a frame laid out like [Linux](https://github.com/torvalds/linux)'s `arch/arm64/kernel/signal.c`, and serves `fill_vm_counters` in `dlls/ntdll/unix/process.c` the kernel's own `/proc/<pid>/status`.
- droiddeck-ntsync follows the [Linux](https://github.com/torvalds/linux) ntsync semantics in `Documentation/userspace-api/ntsync.rst` (GPL-2.0).
+1 -1
View File
@@ -16,7 +16,7 @@ if grep -qxE '__gxx_personality_v0|__cxa_throw|_Unwind_Resume' <<<"$FAKE"; then
echo "::error::libfakeinput.so exports the C++ runtime"; exit 1
fi
SESSION=$(syms "$D/libblsession.so")
for sym in socket bind getsockname setsockopt statfs statvfs syscall shm_open shm_unlink mmap mmap64 munmap drmPrimeFDToHandle drmCloseBufferHandle; do
for sym in socket bind getsockname setsockopt statfs statvfs syscall shm_open shm_unlink mmap mmap64 munmap mprotect drmPrimeFDToHandle drmCloseBufferHandle; do
grep -qx "$sym" <<<"$SESSION" || { echo "::error::libblsession.so does not export $sym"; exit 1; }
done
if grep -qx drmIoctl <<<"$SESSION"; then
+92 -2
View File
@@ -1,9 +1,13 @@
#define _GNU_SOURCE
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/statvfs.h>
#include <sys/uio.h>
#include <unistd.h>
#if defined(__aarch64__) || defined(NOEXEC_TEST)
@@ -32,6 +36,92 @@ __attribute__((visibility("hidden"))) int bl_noexec_retry(int prot, int flags, i
static int (*real_mprotect)(void *, size_t, int);
struct span {
uintptr_t lo, hi;
int prot;
};
static size_t private_range(uintptr_t start, uintptr_t end, struct span *spans, size_t cap) {
char buf[4096];
size_t used = 0, count = 0;
uintptr_t covered = start;
int fd = open("/proc/self/maps", O_RDONLY | O_CLOEXEC);
if (fd < 0) return 0;
for (;;) {
if (used >= sizeof buf - 1) used = 0;
ssize_t n = read(fd, buf + used, sizeof buf - 1 - used);
if (n <= 0) break;
used += (size_t)n;
buf[used] = 0;
char *line = buf, *nl;
while ((nl = strchr(line, '\n'))) {
*nl = 0;
char *p = line;
uintptr_t lo = strtoull(p, &p, 16), hi = strtoull(p + 1, &p, 16);
char perms[5] = {0};
if (*p == ' ') memcpy(perms, p + 1, 4);
line = nl + 1;
if (hi <= covered || lo >= end) continue;
if (lo > covered || perms[3] != 'p' || count == cap) {
close(fd);
return 0;
}
spans[count].lo = covered;
spans[count].hi = hi < end ? hi : end;
spans[count].prot = (perms[0] == 'r' ? PROT_READ : 0) | (perms[1] == 'w' ? PROT_WRITE : 0) | (perms[2] == 'x' ? PROT_EXEC : 0);
covered = spans[count++].hi;
if (covered >= end) {
close(fd);
return count;
}
}
used = strlen(line);
memmove(buf, line, used);
}
close(fd);
return 0;
}
static void restore(const struct span *spans, size_t count) {
for (size_t i = 0; i < count; i++)
if (!(spans[i].prot & PROT_READ)) real_mprotect((void *)spans[i].lo, spans[i].hi - spans[i].lo, spans[i].prot);
}
static int copy_to_anonymous(void *addr, size_t len, int prot) {
uintptr_t page = (uintptr_t)sysconf(_SC_PAGESIZE);
uintptr_t start = (uintptr_t)addr, end = (start + len + page - 1) & ~(page - 1);
struct span spans[64];
size_t count = 0, opened = 0;
if (start & (page - 1) || end <= start || !(count = private_range(start, end, spans, sizeof spans / sizeof *spans))) return -1;
size_t size = end - start;
int fd = memfd_create("noexec-image", MFD_CLOEXEC);
if (fd < 0) return -1;
if (ftruncate(fd, (off_t)size)) {
close(fd);
return -1;
}
void *copy = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (copy == MAP_FAILED) {
close(fd);
return -1;
}
while (opened < count && ((spans[opened].prot & PROT_READ) ||
!real_mprotect((void *)spans[opened].lo, spans[opened].hi - spans[opened].lo, spans[opened].prot | PROT_READ)))
opened++;
struct iovec local = {copy, size}, remote = {addr, size};
if (opened < count || process_vm_readv(getpid(), &local, 1, &remote, 1, 0) != (ssize_t)size || real_mprotect(copy, size, prot)) {
restore(spans, opened);
munmap(copy, size);
close(fd);
return -1;
}
int result = mmap(addr, size, prot, MAP_PRIVATE | MAP_FIXED, fd, 0) == addr ? 0 : -1;
if (result) restore(spans, count);
munmap(copy, size);
close(fd);
return result;
}
__attribute__((constructor)) static void noexec_init(void) {
real_mprotect = (int (*)(void *, size_t, int))dlsym(RTLD_NEXT, "mprotect");
}
@@ -39,8 +129,8 @@ __attribute__((constructor)) static void noexec_init(void) {
int mprotect(void *addr, size_t len, int prot) {
if (!real_mprotect) noexec_init();
int result = real_mprotect(addr, len, prot);
if (result && errno == EACCES && (prot & PROT_EXEC) && in_fex()) {
result = real_mprotect(addr, len, prot & ~PROT_EXEC);
if (result && errno == EACCES && (prot & PROT_EXEC)) {
result = in_fex() ? real_mprotect(addr, len, prot & ~PROT_EXEC) : copy_to_anonymous(addr, len, prot);
if (result) errno = EACCES;
}
return result;
+182 -7
View File
@@ -1,5 +1,6 @@
import os
from pathlib import Path
import platform
import shutil
import subprocess
import tempfile
@@ -25,6 +26,134 @@ int main(int argc, char **argv) {
}
"""
IMAGE = r"""
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
static void run(const char *name, void *code, size_t len, int prot) {
int error = mprotect(code, len, prot) ? errno : 0;
printf("%s %d %d\n", name, error, error ? -1 : ((int (*)(void))code)());
}
int main(int argc, char **argv) {
if (argc < 2) return 2;
size_t page = (size_t)sysconf(_SC_PAGESIZE);
int fd = open(argv[1], O_RDONLY);
run("private", mmap(NULL, page, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0), page, PROT_READ | PROT_EXEC);
run("hidden", mmap(NULL, page, PROT_NONE, MAP_PRIVATE, fd, 0), page, PROT_READ | PROT_EXEC);
char *mixed = mmap(NULL, 2 * page, PROT_READ, MAP_PRIVATE, fd, 0);
if (mixed == MAP_FAILED || mmap(mixed + page, page, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0) != mixed + page)
return 3;
run("mixed", mixed, page + 1, PROT_READ | PROT_EXEC);
char *past = mmap(NULL, 3 * page, PROT_NONE, MAP_PRIVATE, fd, 0);
if (past == MAP_FAILED || mmap(past + page, page, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0) != past + page ||
mmap(past + 2 * page, page, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, page) != past + 2 * page)
return 3;
int error = mprotect(past, 3 * page, PROT_READ | PROT_EXEC) ? errno : 0;
past[page] = 7;
printf("past %d %d\n", error, past[page]);
error = mprotect(mmap(NULL, page, PROT_READ, MAP_SHARED, fd, 0), page, PROT_READ | PROT_EXEC) ? errno : 0;
printf("shared %d\n", error);
return 0;
}
"""
FAILURES = r"""
#define _GNU_SOURCE
#include <dlfcn.h>
#include <errno.h>
#include <sys/mman.h>
#include <stdlib.h>
static void *destination;
static int fixed_calls, remap_calls, protect_calls;
void arm(void *addr) { destination = addr; }
int counts(int which) { return which == 0 ? fixed_calls : which == 1 ? remap_calls : protect_calls; }
int mprotect(void *addr, size_t len, int prot) {
int (*next)(void *, size_t, int) = dlsym(RTLD_NEXT, "mprotect");
if (addr == destination && (prot & PROT_EXEC)) {
protect_calls++;
errno = EACCES;
return -1;
}
if (destination && (prot & PROT_EXEC) && getenv("FAIL_COPY")) {
errno = EACCES;
return -1;
}
return next(addr, len, prot);
}
void *mremap(void *addr, size_t old, size_t size, int flags, ...) {
remap_calls++;
errno = ENOMEM;
return MAP_FAILED;
}
void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset) {
void *(*next)(void *, size_t, int, int, int, off_t) = dlsym(RTLD_NEXT, "mmap");
if (addr == destination && (flags & MAP_FIXED)) {
fixed_calls++;
if (fd >= 0 && !getenv("ALLOW_FIXED")) {
errno = ENOMEM;
return MAP_FAILED;
}
}
return next(addr, len, prot, flags, fd, offset);
}
"""
FAILURE_IMAGE = r"""
#define _GNU_SOURCE
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
static void mapping(void *addr, char *out) {
FILE *maps = fopen("/proc/self/maps", "r");
char line[1024];
unsigned long lo, hi;
out[0] = 0;
while (fgets(line, sizeof line, maps))
if (sscanf(line, "%lx-%lx", &lo, &hi) == 2 && (uintptr_t)addr >= lo && (uintptr_t)addr < hi) {
strcpy(out, line);
break;
}
fclose(maps);
}
int main(int argc, char **argv) {
if (argc != 2) return 2;
size_t page = sysconf(_SC_PAGESIZE);
int fd = open(argv[1], O_RDONLY);
char *addr = mmap(NULL, page * 2, PROT_NONE, MAP_PRIVATE, fd, 0);
if (addr == MAP_FAILED || mmap(addr + page, page, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0) != addr + page) return 3;
addr[page] = 73;
char before[1024], after[1024], tail_before[1024], tail_after[1024];
mapping(addr, before);
mapping(addr + page, tail_before);
void (*arm)(void *) = dlsym(RTLD_DEFAULT, "arm");
int (*counts)(int) = dlsym(RTLD_DEFAULT, "counts");
if (!arm || !counts) return 4;
arm(addr);
int result = mprotect(addr, 2 * page, PROT_READ | PROT_EXEC);
int error = errno;
mapping(addr, after);
mapping(addr + page, tail_after);
printf("result %d %d preserved %d %d tail %d calls %d %d %d\n", result, error,
!strcmp(before, after), !strcmp(tail_before, tail_after), addr[page], counts(0), counts(1), counts(2));
if (!result) printf("execute %d\n", ((int (*)(void))addr)());
return 0;
}
"""
RETURN_42 = {"x86_64": bytes.fromhex("b82a000000c3"), "aarch64": bytes.fromhex("40058052c0035fd6")}
def noexec_folder():
for folder in ("/run/lock", "/dev/shm", "/run/user/%d" % os.getuid()):
@@ -36,7 +165,7 @@ def noexec_folder():
return None
@unittest.skipUnless(shutil.which("cc") and noexec_folder(), "needs cc and a writable noexec mount")
@unittest.skipUnless(shutil.which("cc"), "needs cc")
class NoexecPreloadTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
@@ -48,26 +177,72 @@ class NoexecPreloadTest(unittest.TestCase):
source.write_text(PROGRAM)
for name in ("FEX", "game"):
subprocess.run(["cc", "-O2", "-o", str(cls.tmp / name), str(source)], check=True)
image = cls.tmp / "image.c"
image.write_text(IMAGE)
subprocess.run(["cc", "-O2", "-o", str(cls.tmp / "wine"), str(image)], check=True)
failures = cls.tmp / "failures.c"
failures.write_text(FAILURES)
cls.failures = cls.tmp / "failures.so"
subprocess.run(["cc", "-Wall", "-Werror", "-shared", "-fPIC", "-o", str(cls.failures), str(failures), "-ldl"], check=True)
failure_image = cls.tmp / "failure_image.c"
failure_image.write_text(FAILURE_IMAGE)
subprocess.run(["cc", "-Wall", "-Werror", "-o", str(cls.tmp / "failure_image"), str(failure_image), "-ldl"], check=True)
cls.target = Path(tempfile.mkdtemp(dir=noexec_folder())) / "Game.x86_64"
cls.target.write_bytes(b"\x7fELF" + bytes(4092))
cls.code = cls.target.parent / "game.exe"
cls.code.write_bytes(RETURN_42.get(platform.machine(), b"").ljust(os.sysconf("SC_PAGE_SIZE"), b"\0"))
@classmethod
def tearDownClass(cls):
shutil.rmtree(cls.tmp, ignore_errors=True)
shutil.rmtree(cls.target.parent, ignore_errors=True)
def run_as(self, name, preload=True):
def run_as(self, name, preload=True, target=None, lines=3):
env = dict(os.environ, LD_PRELOAD=str(self.lib)) if preload else {k: v for k, v in os.environ.items() if k != "LD_PRELOAD"}
result = subprocess.run([str(self.tmp / name), str(self.target)], env=env, capture_output=True, text=True, check=True)
return result.stdout.split("\n")[:3]
result = subprocess.run([str(self.tmp / name), str(target or self.target)], env=env, capture_output=True, text=True, check=True)
return result.stdout.split("\n")[:lines]
def test_failed_replacement_preserves_original_mappings_and_permissions(self):
env = dict(os.environ, LD_PRELOAD=f"{self.lib}:{self.failures}")
result = subprocess.run([str(self.tmp / "failure_image"), str(self.code)], env=env,
capture_output=True, text=True, check=True)
self.assertEqual(result.stdout.strip(), "result -1 13 preserved 1 1 tail 73 calls 1 0 1")
def test_failed_copy_protection_preserves_original_mappings(self):
env = dict(os.environ, LD_PRELOAD=f"{self.lib}:{self.failures}", FAIL_COPY="1")
result = subprocess.run([str(self.tmp / "failure_image"), str(self.code)], env=env,
capture_output=True, text=True, check=True)
self.assertEqual(result.stdout.strip(), "result -1 13 preserved 1 1 tail 73 calls 0 0 1")
@unittest.skipUnless(platform.machine() in RETURN_42, "needs a known host instruction set")
def test_replacement_installs_executable_pages_without_final_mprotect(self):
env = dict(os.environ, LD_PRELOAD=f"{self.lib}:{self.failures}", ALLOW_FIXED="1")
result = subprocess.run([str(self.tmp / "failure_image"), str(self.code)], env=env,
capture_output=True, text=True, check=True)
self.assertIn("preserved 0 0 tail 73 calls 1 0 1", result.stdout)
self.assertEqual(result.stdout.splitlines()[-1], "execute 42")
@unittest.skipUnless(noexec_folder(), "needs a writable noexec mount")
def test_fex_maps_a_program_on_noexec_storage(self):
self.assertEqual(self.run_as("FEX"), ["map 0", "protect 0", "read \x7f"])
@unittest.skipUnless(noexec_folder(), "needs a writable noexec mount")
def test_the_kernel_still_refuses_everyone_else(self):
for refused in (self.run_as("game"), self.run_as("FEX", preload=False)):
self.assertIn(refused[0], ("map 1", "map 13"))
self.assertEqual(refused[1:], ["protect 13", "read -"])
refused = self.run_as("FEX", preload=False)
self.assertIn(refused[0], ("map 1", "map 13"))
self.assertEqual(refused[1:], ["protect 13", "read -"])
self.assertIn(self.run_as("game")[0], ("map 1", "map 13"))
@unittest.skipUnless(platform.machine() in RETURN_42, "needs a known host instruction set")
@unittest.skipUnless(noexec_folder(), "needs a writable noexec mount")
def test_private_image_pages_become_executable_copies(self):
self.assertEqual(self.run_as("wine", target=self.code, lines=5), ["private 0 42", "hidden 0 42", "mixed 0 42", "past 13 7", "shared 13"])
@unittest.skipUnless(platform.machine() in RETURN_42, "needs a known host instruction set")
@unittest.skipUnless(noexec_folder(), "needs a writable noexec mount")
def test_image_pages_stay_refused_without_the_preload(self):
self.assertEqual(self.run_as("wine", preload=False, target=self.code, lines=5),
["private 13 -1", "hidden 13 -1", "mixed 13 -1", "past 13 7", "shared 13"])
if __name__ == "__main__":