mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 12:56:06 +00:00
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:
1 parent
3d0c38c1d3
commit
f65e7a3d43
4 files changed
+276
-10
No files matched your search
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user