Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileManagement: Hide the FEX RootFS fd from /proc/self/fd #4138

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Source/Tools/LinuxEmulation/LinuxSyscalls/FileManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -994,4 +994,38 @@ uint64_t FileManager::LRemovexattr(const char* path, const char* name) {
return ::lremovexattr(SelfPath, name);
}

void FileManager::UpdatePID(uint32_t PID) {
CurrentPID = PID;

// Track the inode of /proc/self/fd/<RootFSFD>, to be able to hide it
auto FDpath = fextl::fmt::format("/proc/self/fd/{}", RootFSFD);
struct stat Buffer {};
int Result = fstatat(AT_FDCWD, FDpath.c_str(), &Buffer, AT_SYMLINK_NOFOLLOW);
if (Result >= 0) {
RootFSFDInode = Buffer.st_ino;
}

// Track the st_dev of /proc, to check for inode equality
Result = stat("/proc/self", &Buffer);
if (Result >= 0) {
ProcFSDev = Buffer.st_dev;
}
}

bool FileManager::IsRootFSFD(int dirfd, uint64_t inode) {

// Check if we have to hide this entry
if (inode == RootFSFDInode) {
struct stat Buffer;
if (fstat(dirfd, &Buffer) >= 0) {
if (Buffer.st_dev == ProcFSDev) {
LogMan::Msg::DFmt("Hiding directory entry for RootFSFD");
return true;
} else {
}
}
}
return false;
}

} // namespace FEX::HLE
7 changes: 4 additions & 3 deletions Source/Tools/LinuxEmulation/LinuxSyscalls/FileManagement.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ class FileManager final {
std::optional<std::string_view> GetSelf(const char* Pathname);
bool IsSelfNoFollow(const char* Pathname, int flags) const;

void UpdatePID(uint32_t PID) {
CurrentPID = PID;
}
void UpdatePID(uint32_t PID);
bool IsRootFSFD(int dirfd, uint64_t inode);

fextl::string GetEmulatedPath(const char* pathname, bool FollowSymlink = false);
using FDPathTmpData = std::array<char[PATH_MAX], 2>;
Expand Down Expand Up @@ -162,5 +161,7 @@ class FileManager final {
FEX_CONFIG_OPT(Is64BitMode, IS64BIT_MODE);
uint32_t CurrentPID {};
int RootFSFD {AT_FDCWD};
int64_t RootFSFDInode = 0;
dev_t ProcFSDev;
};
} // namespace FEX::HLE
5 changes: 5 additions & 0 deletions Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ uint64_t GetDentsEmulation(int fd, T* dirp, uint32_t count) {
Outgoing->d_name[Outgoing->d_reclen - offsetof(T, d_name) - 1] = Tmp->d_type;

TmpOffset += Tmp->d_reclen;

if (FEX::HLE::_SyscallHandler->FM.IsRootFSFD(fd, Outgoing->d_ino)) {
continue;
}

// Outgoing is 5 bytes smaller
Offset += NewRecLen;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -694,8 +694,6 @@ namespace x64 {
SyscallPassthrough6<SYSCALL_DEF(futex)>);
REGISTER_SYSCALL_IMPL_X64_PASS_FLAGS(io_getevents, SyscallFlags::OPTIMIZETHROUGH | SyscallFlags::NOSYNCSTATEONENTRY,
SyscallPassthrough5<SYSCALL_DEF(io_getevents)>);
REGISTER_SYSCALL_IMPL_X64_PASS_FLAGS(getdents64, SyscallFlags::OPTIMIZETHROUGH | SyscallFlags::NOSYNCSTATEONENTRY,
SyscallPassthrough3<SYSCALL_DEF(getdents64)>);
REGISTER_SYSCALL_IMPL_X64_PASS_FLAGS(semtimedop, SyscallFlags::OPTIMIZETHROUGH | SyscallFlags::NOSYNCSTATEONENTRY,
SyscallPassthrough4<SYSCALL_DEF(semtimedop)>);
REGISTER_SYSCALL_IMPL_X64_PASS_FLAGS(timer_create, SyscallFlags::OPTIMIZETHROUGH | SyscallFlags::NOSYNCSTATEONENTRY,
Expand Down
5 changes: 5 additions & 0 deletions Source/Tools/LinuxEmulation/LinuxSyscalls/x32/FD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,11 @@ void RegisterFD(FEX::HLE::SyscallHandler* Handler) {
for (size_t i = 0, num = 0; i < Result; ++num) {
linux_dirent_64* Incoming = (linux_dirent_64*)(reinterpret_cast<uint64_t>(dirp) + i);
Incoming->d_off = num;
if (FEX::HLE::_SyscallHandler->FM.IsRootFSFD(fd, Incoming->d_ino)) {
Result -= Incoming->d_reclen;
memmove(Incoming, (linux_dirent_64*)(reinterpret_cast<uint64_t>(Incoming) + Incoming->d_reclen), Result - i);
continue;
}
i += Incoming->d_reclen;
}
}
Expand Down
17 changes: 17 additions & 0 deletions Source/Tools/LinuxEmulation/LinuxSyscalls/x64/FD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ void RegisterFD(FEX::HLE::SyscallHandler* Handler) {
return GetDentsEmulation<false>(fd, reinterpret_cast<FEX::HLE::x64::linux_dirent*>(dirp), count);
});

REGISTER_SYSCALL_IMPL_X64(getdents64, [](FEXCore::Core::CpuStateFrame* Frame, int fd, void* dirp, uint32_t count) -> uint64_t {
uint64_t Result = ::syscall(SYSCALL_DEF(getdents64), static_cast<uint64_t>(fd), dirp, static_cast<uint64_t>(count));
if (Result != -1) {
// Check for and hide the RootFS FD
for (size_t i = 0; i < Result;) {
linux_dirent_64* Incoming = (linux_dirent_64*)(reinterpret_cast<uint64_t>(dirp) + i);
if (FEX::HLE::_SyscallHandler->FM.IsRootFSFD(fd, Incoming->d_ino)) {
Result -= Incoming->d_reclen;
memmove(Incoming, (linux_dirent_64*)(reinterpret_cast<uint64_t>(Incoming) + Incoming->d_reclen), Result - i);
continue;
}
i += Incoming->d_reclen;
}
}
SYSCALL_ERRNO();
});

REGISTER_SYSCALL_IMPL_X64(dup2, [](FEXCore::Core::CpuStateFrame* Frame, int oldfd, int newfd) -> uint64_t {
uint64_t Result = ::dup2(oldfd, newfd);
SYSCALL_ERRNO();
Expand Down
Loading