mirror of
https://github.com/FEX-Emu/FEX.git
synced 2026-10-09 19:00:20 +02:00
Seccomp is a relatively complex feature that was added to Linux back in
2005, and was further extended in 2013 to support BPF based protections.
Once seccomp is enabled, you can no longer disable seccomp but
additional protections can be placed on top of existing seccomp filters.
Additionally seccomp filters are inherited in child processes, which
ensures the process tree can't escape from the secure computing
environment through child processes.
The basis of this feature is a shim that lives between userspace and the
kernel at the syscall entrypoint.
In "strict" mode, seccomp only allows read, write, exit, exit_group, and {rt_,}sigreturn to function.
When in "filter" mode, a BPF filter is run on syscall entrypoint and
returns state about if the syscall should be allowed or not. Multiple
filters can be installed in this mode, all of which get executed. The
result that is the most restricted is the action that occurs at the end.
There are some significant limitations in filter mode that must be
adhered to which makes executing this code inside of kernel space a
non-issue and effectively limits how much cpu time is spent in the filters.
Although these filters are free to do basically anything with the
provided data, just can't do any loops.
FEX needs to implement seccomp because there are multiple applications
using the feature, the primary one being Chromium which some games embed
without disabling the sandbox. WINE also uses seccomp for capturing
games that do raw Windows system calls. Apparently Red Dead Redemption
is one of the games that requires this.
While FEX implements seccomp, it is not yet all encompassing, which is
one of the reasons why it isn't enabled by default and requires a config
option.
**seccomp_unotify is not implemented**
This is a relatively new feature for seccomp which lets the seccomp
filter signal an FD for multiple things. Luckily Chromium and WINE don't
use this. This will be tricky to implement under FEX since it
requires ioctl trapping and some other behaviour
**ptrace isn't supported**
One feature of seccomp is that it can raise ptrace events. Since FEX
doesn't support ptrace at all, this isn't handled. Again Chromium and
WINE don't use this.
**kill-thread not quite correct**
This isn't directly related to seccomp but more about how we do thread
shutdown in FEX. This will require some more changes around thread state
tracking before fully supporting this. Chromium and WINE don't use this.
kill-process also falls under this
Features that are supported:
- Strict mode and seccomp-bpf mode supported
- All BFP instructions that seccomp-bpf understands
- Inheriting seccomp through execve
- This means we serialize and deserialize the calling thread's
seccomp filters
- An execve that escapes FEX will also escape seccomp. Not much we
can do about it
- TSync - Allowing post-mortem seccomp insertion which allows threads to
synchronize seccomp filters after the fact
Features that are not supported:
- Different arch qualifiers depending on syscall entrypoint
- Just like our syscall handler, we are hardcoded to the arch that the
application starts with
- user_notif
- ptrace
- Runtime code cache invalidation when seccomp is installed
- Currently we must ensure all syscalls go through the frontend
syscall handler
- Runtime invalidation of code cache with inline syscalls will get
fixed in the future.
This currently isn't enabled by default because of the minor feature
problems that haven't been resolved. Currently the Linux Kernel's test
application works for the features that FEX supports, and WINE's usage
can be handled by FEX. Chromium's sandbox doesn't yet work with this PR,
but it only fails due to features unrelated to seccomp.
Having this open for merging now so we can work to resolve the remaining
issues without this bitrotting.
113 lines
3.4 KiB
C++
113 lines
3.4 KiB
C++
// SPDX-License-Identifier: MIT
|
|
/*
|
|
$info$
|
|
tags: LinuxSyscalls|syscalls-shared
|
|
$end_info$
|
|
*/
|
|
#pragma once
|
|
|
|
#include <FEXCore/Config/Config.h>
|
|
#include <FEXCore/fextl/vector.h>
|
|
#include <FEXCore/Utils/SignalScopeGuards.h>
|
|
|
|
#include <signal.h>
|
|
|
|
struct sock_fprog;
|
|
struct seccomp_data;
|
|
struct seccomp_notif_sizes;
|
|
|
|
namespace FEXCore {
|
|
|
|
namespace Core {
|
|
struct CpuStateFrame;
|
|
}
|
|
|
|
namespace HLE {
|
|
struct SyscallArguments;
|
|
}
|
|
|
|
} // namespace FEXCore
|
|
|
|
namespace FEX::HLE {
|
|
|
|
class SyscallHandler;
|
|
class SignalDelegator;
|
|
struct ThreadStateObject;
|
|
|
|
class SeccompEmulator final {
|
|
public:
|
|
SeccompEmulator(FEX::HLE::SyscallHandler* SyscallHandler, FEX::HLE::SignalDelegator* SignalDelegation)
|
|
: SyscallHandler {SyscallHandler}
|
|
, SignalDelegation {SignalDelegation} {}
|
|
|
|
uint64_t Handle(FEXCore::Core::CpuStateFrame* Frame, uint32_t Op, uint32_t flags, void* arg);
|
|
|
|
// Equivalent to prctl(PR_GET_SECCOMP)
|
|
uint64_t GetSeccomp(FEXCore::Core::CpuStateFrame* Frame);
|
|
|
|
void InheritSeccompFilters(FEX::HLE::ThreadStateObject* Parent, FEX::HLE::ThreadStateObject* Child);
|
|
void FreeSeccompFilters(FEX::HLE::ThreadStateObject* Thread);
|
|
|
|
struct ExecuteFilterResult {
|
|
bool EarlyReturn {};
|
|
uint64_t Result;
|
|
};
|
|
ExecuteFilterResult ExecuteFilter(FEXCore::Core::CpuStateFrame* Frame, uint64_t JITPC, FEXCore::HLE::SyscallArguments* Args);
|
|
int GetKillSignal() const {
|
|
return CurrentKillSignal;
|
|
}
|
|
|
|
std::optional<int> SerializeFilters(FEXCore::Core::CpuStateFrame* Frame);
|
|
void DeserializeFilters(FEXCore::Core::CpuStateFrame* Frame, int FD);
|
|
|
|
using FilterFunc = uint64_t (*)(uint32_t Acc, uint32_t Index, uint32_t Tmp, uint32_t Tmp2, void* Data);
|
|
struct FilterInformation final {
|
|
FilterFunc Func;
|
|
uint64_t RefCount;
|
|
size_t MappedSize;
|
|
uint32_t FilterInstructions;
|
|
bool ShouldLog;
|
|
};
|
|
|
|
private:
|
|
FEX_CONFIG_OPT(Is64BitMode, IS64BIT_MODE);
|
|
FEX_CONFIG_OPT(NeedsSeccomp, NEEDSSECCOMP);
|
|
FEX_CONFIG_OPT(Filename, APP_FILENAME);
|
|
FEX::HLE::SyscallHandler* SyscallHandler;
|
|
FEX::HLE::SignalDelegator* SignalDelegation;
|
|
|
|
int CurrentKillSignal {SIGSYS};
|
|
|
|
// Equivalent to seccomp(SECCOMP_SET_MODE_STRICT, ...);
|
|
uint64_t SetModeStrict(FEXCore::Core::CpuStateFrame* Frame, uint32_t flags, const void* arg);
|
|
// Equivalent to seccomp(SECCOMP_SET_MODE_FILTER, ...);
|
|
uint64_t SetModeFilter(FEXCore::Core::CpuStateFrame* Frame, uint32_t flags, const sock_fprog* prog);
|
|
// Equivalent to seccomp(SECCOMP_GET_ACTION_AVAIL, ...);
|
|
uint64_t GetActionAvail(uint32_t flags, const uint32_t* action);
|
|
// Equivalent to seccomp(SECCOMP_GET_NOTIF_SIZES, ...);
|
|
uint64_t GetNotifSizes(uint32_t flags, struct seccomp_notif_sizes* sizes);
|
|
|
|
// 0 on TSync possible
|
|
/// TID for the first thread that breaks tsync.
|
|
uint64_t CanDoTSync(FEXCore::Core::CpuStateFrame* Frame);
|
|
void TSyncFilters(FEXCore::Core::CpuStateFrame* Frame);
|
|
|
|
static void DumpProgram(const sock_fprog* prog);
|
|
|
|
// Multiple filter instruction count penalty.
|
|
// When multiple filters are installed there is a penalty per filter counted towards the maximum number of instructions.
|
|
constexpr static size_t BPF_MULTIFILTERPENALTY = 4;
|
|
// Maximum number of BPF instructions.
|
|
constexpr static size_t BPF_MAX_INSNS_PER_PATH = 32768;
|
|
uint64_t TotalFilterInstructions {};
|
|
|
|
FEXCore::ForkableUniqueMutex FilterMutex;
|
|
fextl::list<FilterInformation> Filters {};
|
|
|
|
uint64_t AuditSerialIncrement() {
|
|
return AuditSerial.fetch_add(1);
|
|
}
|
|
std::atomic<uint64_t> AuditSerial {};
|
|
};
|
|
} // namespace FEX::HLE
|