FEX: open the host status file with a direct system call so preloads cannot rewrite it

This commit is contained in:
maxjivi05 committed 2026-12-03 13:01:38 -05:00
1 parent 7f561f6f92
commit faf7c7b003
1 file changed
+75 -12
@@ -1,37 +1,100 @@
From e865603a39eeeb42b8573abf35845c2d0d008c4a Mon Sep 17 00:00:00 2001
From ab6a01db9e93e7f143b9a3da5b40dcb160b960f3 Mon Sep 17 00:00:00 2001
From: maxjivi05 <maxjividen@gmail.com>
Date: Wed, 2 Dec 2026 18:25:20 -0500
Subject: [PATCH] Read FEX's own /proc/self/status through the raw openat
syscall
Subject: [PATCH] Read FEX's own /proc/self/status with a direct openat system
call
---
.../LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp | 2 +-
Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.cpp | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
FEXHeaderUtils/FEXHeaderUtils/Syscalls.h | 30 +++++++++++++++++++
.../EmulatedFiles/EmulatedFiles.cpp | 3 +-
.../LinuxSyscalls/PtraceEmu.cpp | 3 +-
3 files changed, 34 insertions(+), 2 deletions(-)
diff --git a/FEXHeaderUtils/FEXHeaderUtils/Syscalls.h b/FEXHeaderUtils/FEXHeaderUtils/Syscalls.h
index dae3d3bcc..c0f3eb3ed 100644
--- a/FEXHeaderUtils/FEXHeaderUtils/Syscalls.h
+++ b/FEXHeaderUtils/FEXHeaderUtils/Syscalls.h
@@ -4,6 +4,7 @@
#include <FEXCore/Utils/LogManager.h>
#include <cstdint>
+#include <errno.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
@@ -123,4 +124,33 @@ inline int32_t gettid() {
#endif
+#ifndef _WIN32
+inline int32_t openat_kernel(int dirfd, const char* pathname, int flags) {
+#if defined(__aarch64__)
+ register long x0 asm("x0") = dirfd;
+ register long x1 asm("x1") = reinterpret_cast<long>(pathname);
+ register long x2 asm("x2") = flags;
+ register long x3 asm("x3") = 0;
+ register long x8 asm("x8") = SYS_openat;
+ asm volatile("svc #0" : "+r"(x0) : "r"(x1), "r"(x2), "r"(x3), "r"(x8) : "memory");
+ if (x0 < 0) {
+ errno = static_cast<int>(-x0);
+ return -1;
+ }
+ return static_cast<int32_t>(x0);
+#elif defined(__x86_64__)
+ long ret;
+ register long r10 asm("r10") = 0;
+ asm volatile("syscall" : "=a"(ret) : "a"(SYS_openat), "D"(dirfd), "S"(pathname), "d"(flags), "r"(r10) : "rcx", "r11", "memory");
+ if (ret < 0) {
+ errno = static_cast<int>(-ret);
+ return -1;
+ }
+ return static_cast<int32_t>(ret);
+#else
+ return static_cast<int32_t>(::syscall(SYS_openat, dirfd, pathname, flags));
+#endif
+}
+#endif
+
} // namespace FHU::Syscalls
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp
index c1a91d122..5394b1603 100644
index c1a91d122..49adaaa66 100644
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp
@@ -784,7 +784,7 @@ int32_t EmulatedFDManager::ProcStatus(FEXCore::Context::Context* ctx, int32_t fd
@@ -14,6 +14,7 @@ $end_info$
#include "LinuxSyscalls/Syscalls.h"
#include "LinuxSyscalls/EmulatedFiles/EmulatedFiles.h"
+#include <FEXHeaderUtils/Syscalls.h>
#include <FEXCore/Config/Config.h>
#include <FEXCore/Core/Context.h>
#include <FEXCore/Core/CPUID.h>
@@ -784,7 +785,7 @@ int32_t EmulatedFDManager::ProcStatus(FEXCore::Context::Context* ctx, int32_t fd
char Buf[8192];
ssize_t Len = 0;
{
- int HostFD = ::open("/proc/self/status", O_RDONLY | O_CLOEXEC);
+ int HostFD = static_cast<int>(::syscall(SYS_openat, AT_FDCWD, "/proc/self/status", O_RDONLY | O_CLOEXEC));
+ int HostFD = FHU::Syscalls::openat_kernel(AT_FDCWD, "/proc/self/status", O_RDONLY | O_CLOEXEC);
if (HostFD >= 0) {
Len = ::read(HostFD, Buf, sizeof(Buf) - 1);
::close(HostFD);
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.cpp
index ca4c86063..6680a93ac 100644
index ca4c86063..49ece24a3 100644
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.cpp
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.cpp
@@ -133,7 +133,7 @@ namespace {
@@ -3,6 +3,7 @@
#include "LinuxSyscalls/PtraceEmu.h"
#include "LinuxSyscalls/Syscalls.h"
+#include <FEXHeaderUtils/Syscalls.h>
#include <FEXCore/Core/CoreState.h>
#include <FEXCore/Core/X86Enums.h>
@@ -133,7 +134,7 @@ namespace {
}
bool HasHostTracer() {
- const int fd = ::open("/proc/self/status", O_RDONLY | O_CLOEXEC);
+ const int fd = static_cast<int>(::syscall(SYS_openat, AT_FDCWD, "/proc/self/status", O_RDONLY | O_CLOEXEC));
+ const int fd = FHU::Syscalls::openat_kernel(AT_FDCWD, "/proc/self/status", O_RDONLY | O_CLOEXEC);
if (fd < 0) {
return true;
}