diff --git a/README.md b/README.md index f07237abe..2ab977bb6 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ Compatibility and performance vary by device; hardware validation is limited. De ## Credits and licence -GPL-3.0. Runtime, shim, input, and controller work build on WinNative and Bannerlator (maxjivi05). Epic Games and GOG support build on [Phobos’s](https://github.com/phobos665) adaptation of [Legendary](https://github.com/legendary-gl/legendary) and [gogdl](https://github.com/Heroic-Games-Launcher/heroic-gogdl), respectively. LSFG frame generation is from the work of Camille LaVey and the [Eden](https://eden-emu.dev) emulator project, following [lsfg-vk](https://github.com/PancakeTAS/lsfg-vk), ported to WinNative and DroidDeck by [@maxjivi05](https://github.com/maxjivi05); it needs your own copy of [Lossless Scaling](https://store.steampowered.com/app/993090/) and ships none of its shaders. x86 AppImages, and any whose own runtime cannot unpack them, are unpacked with [uruntime](https://github.com/VHSgunzo/uruntime) by VHSgunzo (MIT), shipped unmodified with its licence. The Steam client's audio goes through [@The412Banner](https://github.com/The412Banner)'s [DirectAudio](https://github.com/The412Banner/directaudio) PulseAudio sink (LGPL-2.1-or-later), built from `tools/directaudio/sink` with its request size kept to its ring so clients are not asked to queue 100 ms ahead. mmap ring-buffer controller input was developed by ribbit384 ([WinNative #345](https://github.com/WinNative-Emu/WinNative/pull/345)). Why the x86-64 Steam client refused the arm64 client's sign-in was checked by reading Steam's saved token with the `ConnectCache` format from [deadlock-api-ingest](https://github.com/deadlock-api/deadlock-api-ingest). x86 programs that link OpenSSL 3 under FEX use the copy in the [Steam Linux Runtime 4.0](https://gitlab.steamos.cloud/steamrt/steamrt) platform. See [LICENSE](LICENSE). Steam and Proton belong to Valve Corporation; this project is not affiliated with Valve. +GPL-3.0. Runtime, shim, input, and controller work build on WinNative and Bannerlator (maxjivi05). Epic Games and GOG support build on [Phobos’s](https://github.com/phobos665) adaptation of [Legendary](https://github.com/legendary-gl/legendary) and [gogdl](https://github.com/Heroic-Games-Launcher/heroic-gogdl), respectively. LSFG frame generation is from the work of Camille LaVey and the [Eden](https://eden-emu.dev) emulator project, following [lsfg-vk](https://github.com/PancakeTAS/lsfg-vk), ported to WinNative and DroidDeck by [@maxjivi05](https://github.com/maxjivi05); it needs your own copy of [Lossless Scaling](https://store.steampowered.com/app/993090/) and ships none of its shaders. x86 AppImages, and any whose own runtime cannot unpack them, are unpacked with [uruntime](https://github.com/VHSgunzo/uruntime) by VHSgunzo (MIT), shipped unmodified with its licence. The Steam client's audio goes through [@The412Banner](https://github.com/The412Banner)'s [DirectAudio](https://github.com/The412Banner/directaudio) PulseAudio sink (LGPL-2.1-or-later), built from `tools/directaudio/sink` with its request size kept to its ring so clients are not asked to queue 100 ms ahead. mmap ring-buffer controller input was developed by ribbit384 ([WinNative #345](https://github.com/WinNative-Emu/WinNative/pull/345)). Why the x86-64 Steam client refused the arm64 client's sign-in was checked by reading Steam's saved token with the `ConnectCache` format from [deadlock-api-ingest](https://github.com/deadlock-api/deadlock-api-ingest). x86 programs that link OpenSSL 3 under FEX use the copy in the [Steam Linux Runtime 4.0](https://gitlab.steamos.cloud/steamrt/steamrt) platform. Hooks written with `WriteProcessMemory` into code [FEX](https://github.com/FEX-Emu/FEX) has translated go through `/proc//mem` again when the kernel refuses `process_vm_writev`, as [Wine](https://gitlab.winehq.org/wine/wine)'s `server/ptrace.c` wrote them before Wine 11. See [LICENSE](LICENSE). Steam and Proton belong to Valve Corporation; this project is not affiliated with Valve. XR glasses support builds on WinNative's VITURE glasses work. VITURE protocol details were checked against [XRLinuxDriver](https://github.com/wheaney/XRLinuxDriver) by [@wheaney](https://github.com/wheaney), including the VITURE protocol header it ships, [ar-drivers-rs](https://github.com/badicsalex/ar-drivers-rs) by [@badicsalex](https://github.com/badicsalex) and the [go-macos/iokit VITURE package](https://pkg.go.dev/github.com/go-macos/iokit/viture); the optional VITURE SDK belongs to VITURE and is not included. RayNeo support adapts [@informalTechCode](https://github.com/informalTechCode)'s [RayNeo-Air-Series-3DoF-Scaffold](https://github.com/informalTechCode/RayNeo-Air-Series-3DoF-Scaffold) (Apache-2.0) and references [@verncat](https://github.com/verncat)'s [RayNeo-Air-3S-Pro-OpenVR](https://github.com/verncat/RayNeo-Air-3S-Pro-OpenVR) (MIT) and [Monado](https://gitlab.freedesktop.org/monado/monado) (BSL-1.0); see [docs/third-party/rayneo](docs/third-party/rayneo/README.md). XREAL 1S support references [XRLinuxDriver](https://github.com/wheaney/XRLinuxDriver) and [xrealOneDeviceKit](https://github.com/wheaney/xrealOneDeviceKit) by @wheaney, [xreal_one_driver](https://github.com/rohitsangwan01/xreal_one_driver) by [@rohitsangwan01](https://github.com/rohitsangwan01) (MIT) and [xr-tools](https://github.com/0xcaff/xr-tools) by [@0xcaff](https://github.com/0xcaff). The REDMAGIC glasses-display findings draw on [@mekhontsev](https://github.com/mekhontsev)'s [MagicDesk investigation](https://github.com/mekhontsev/magicdesk/blob/main/docs/xr-resolution-fix.md) and [@Coding-BR](https://github.com/Coding-BR)'s [decompiled Nubia kernel](https://github.com/Coding-BR/android_kernel_nubia_sm8850_qwjujube). VITURE, RayNeo, XREAL and REDMAGIC are trademarks of their owners; this project is not affiliated with them. diff --git a/tools/linuxfs/preload/vmwrite.c b/tools/linuxfs/preload/vmwrite.c new file mode 100644 index 000000000..7737632c0 --- /dev/null +++ b/tools/linuxfs/preload/vmwrite.c @@ -0,0 +1,55 @@ +#define _GNU_SOURCE +#define _FILE_OFFSET_BITS 64 +#include +#include +#include +#include +#include +#include +#include +#include + +typedef ssize_t (*vm_writev_fn)(pid_t, const struct iovec *, unsigned long, const struct iovec *, unsigned long, + unsigned long); + +static int in_wineserver(void) { + static int known = -1; + if (known < 0) { + char exe[512]; + ssize_t n = readlink("/proc/self/exe", exe, sizeof exe - 1); + exe[n > 0 ? n : 0] = 0; + const char *name = strrchr(exe, '/'); + known = !strcmp(name ? name + 1 : exe, "wineserver"); + } + return known; +} + +ssize_t process_vm_writev(pid_t pid, const struct iovec *local, unsigned long liovcnt, const struct iovec *remote, + unsigned long riovcnt, unsigned long flags) { + static vm_writev_fn real; + if (!real) real = (vm_writev_fn)dlsym(RTLD_NEXT, "process_vm_writev"); + if (!real) { + errno = ENOSYS; + return -1; + } + ssize_t result = real(pid, local, liovcnt, remote, riovcnt, flags); + if (result >= 0 || errno != EFAULT || liovcnt != 1 || riovcnt != 1 || flags || !in_wineserver()) return result; + struct iovec ends[2]; + struct iovec into = {ends, sizeof ends}; + struct iovec from[2] = {{(void *)local, sizeof *local}, {(void *)remote, sizeof *remote}}; + if (process_vm_readv(getpid(), &into, 1, from, 2, 0) != (ssize_t)sizeof ends || ends[0].iov_len != ends[1].iov_len) { + errno = EFAULT; + return -1; + } + char path[32]; + snprintf(path, sizeof path, "/proc/%d/mem", (int)pid); + int fd = open(path, O_WRONLY | O_CLOEXEC); + if (fd < 0) { + errno = EFAULT; + return -1; + } + ssize_t written = pwrite(fd, ends[0].iov_base, ends[0].iov_len, (off_t)(uintptr_t)ends[1].iov_base); + close(fd); + if (written < 0) errno = EFAULT; + return written; +} diff --git a/tools/tests/test_vmwrite_preload.py b/tools/tests/test_vmwrite_preload.py new file mode 100644 index 000000000..0c6dd27f6 --- /dev/null +++ b/tools/tests/test_vmwrite_preload.py @@ -0,0 +1,108 @@ +import os +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + +PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload" + +PROGRAM = r""" +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(void) { + unsigned char *code = mmap((void *)0xc0000000UL, 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (sizeof(void *) == 4 && (uintptr_t)code <= 0x7fffffffU) return 3; + unsigned char *data = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + memset(code, 0x90, 4096); + mprotect(code, 4096, PROT_READ | PROT_EXEC); + int ready[2]; + if (pipe(ready)) return 2; + pid_t child = fork(); + if (child == 0) { + prctl(PR_SET_PDEATHSIG, SIGKILL); + close(ready[0]); + if (write(ready[1], "", 1) != 1) _exit(1); + pause(); + _exit(0); + } + char c; + close(ready[1]); + if (read(ready[0], &c, 1) != 1) return 2; + unsigned char patch[4] = {0xB8, 0x78, 0x56, 0x34}; + struct iovec mine = {patch, sizeof patch}; + struct iovec theirs[2] = {{code, sizeof patch}, {data, sizeof patch}}; + for (int i = 0; i < 2; i++) { + ssize_t n = process_vm_writev(child, &mine, 1, &theirs[i], 1, 0); + printf("%s %zd %d", i ? "data" : "code", n, n < 0 ? errno : 0); + unsigned char back[4] = {0}; + struct iovec got = {back, sizeof back}; + process_vm_readv(child, &got, 1, &theirs[i], 1, 0); + printf(" %s\n", memcmp(back, patch, sizeof patch) ? "old" : "new"); + } + struct iovec split[2] = {{patch, 2}, {patch + 2, 2}}; + ssize_t n = process_vm_writev(child, split, 2, &theirs[0], 1, 0); + printf("split %zd %d\n", n, n < 0 ? errno : 0); + n = process_vm_writev(child, (const struct iovec *)8, 1, &theirs[0], 1, 0); + printf("invalid %zd %d\n", n, n < 0 ? errno : 0); + kill(child, SIGKILL); + waitpid(child, NULL, 0); + return 0; +} +""" + + +@unittest.skipUnless(shutil.which("cc") and os.path.exists("/proc/self/mem"), "needs cc and /proc") +class VmWritePreloadTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.tmp = Path(tempfile.mkdtemp()) + cls.addClassCleanup(shutil.rmtree, cls.tmp, ignore_errors=True) + source = cls.tmp / "program.c" + source.write_text(PROGRAM) + cls.bits32 = True + for flags, sub in (([], "64"), (["-m32"], "32")): + (cls.tmp / sub).mkdir() + try: + subprocess.run(["cc", *flags, "-O2", "-Wall", "-Werror", "-fPIC", "-shared", "-o", + str(cls.tmp / sub / "libvmwrite.so"), str(PRELOAD / "vmwrite.c"), "-ldl"], + check=True, capture_output=True) + for name in ("wineserver", "game"): + subprocess.run(["cc", *flags, "-O2", "-o", str(cls.tmp / sub / name), str(source)], + check=True, capture_output=True) + except subprocess.CalledProcessError: + if not flags: + raise + cls.bits32 = False + + def run_as(self, name, preload=True, bits="64"): + lib = self.tmp / bits / "libvmwrite.so" + env = dict(os.environ, LD_PRELOAD=str(lib)) if preload else {k: v for k, v in os.environ.items() if k != "LD_PRELOAD"} + result = subprocess.run([str(self.tmp / bits / name)], env=env, capture_output=True, text=True, check=True) + return result.stdout.splitlines() + + def test_wineserver_writes_a_page_the_kernel_refused(self): + self.assertEqual(self.run_as("wineserver"), ["code 4 0 new", "data 4 0 new", "split -1 14", "invalid -1 14"]) + + def test_a_32_bit_wineserver_writes_above_2_gib(self): + if not self.bits32: + self.skipTest("needs cc -m32") + self.assertEqual(self.run_as("wineserver", bits="32"), ["code 4 0 new", "data 4 0 new", "split -1 14", "invalid -1 14"]) + + def test_everyone_else_keeps_the_kernel_answer(self): + for refused in (self.run_as("game"), self.run_as("wineserver", preload=False)): + self.assertEqual(refused, ["code -1 14 old", "data 4 0 new", "split -1 14", "invalid -1 14"]) + + +if __name__ == "__main__": + unittest.main()