mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 13:05:47 +00:00
Harden high-memory aio, capability and message batch write-backs
Restore patched aio buffers through /proc/self/mem when the control block became read-only and keep the scratch vectors alive if that fails too, fault capability calls whose header write-back fails, stop send batches after a short message, and pin the batch socket with a private dup.
This commit is contained in:
1 parent
0965c46359
commit
a828192cae
2 files changed
+252
-22
No files matched your search
@@ -13,7 +13,7 @@ writes to code in them invalidate translated blocks.
|
||||
Builds on StonkGuy's anti-cheat emulation from https://github.com/StonkGuy/FEX (eac-2611, MIT).
|
||||
---
|
||||
Source/Tools/LinuxEmulation/CMakeLists.txt | 1 +
|
||||
.../LinuxSyscalls/GuestHighMemory.cpp | 1932 +++++++++++++++++
|
||||
.../LinuxSyscalls/GuestHighMemory.cpp | 1994 +++++++++++++++++
|
||||
.../LinuxSyscalls/GuestHighMemory.h | 59 +
|
||||
.../LinuxSyscalls/SignalDelegator.cpp | 5 +
|
||||
.../LinuxEmulation/LinuxSyscalls/Syscalls.cpp | 52 +-
|
||||
@@ -21,7 +21,7 @@ Builds on StonkGuy's anti-cheat emulation from https://github.com/StonkGuy/FEX (
|
||||
.../LinuxSyscalls/SyscallsSMCTracking.cpp | 61 +-
|
||||
.../LinuxSyscalls/ThreadManager.cpp | 2 +
|
||||
.../LinuxSyscalls/ThreadManager.h | 19 +
|
||||
9 files changed, 2131 insertions(+), 30 deletions(-)
|
||||
9 files changed, 2193 insertions(+), 30 deletions(-)
|
||||
create mode 100644 Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp
|
||||
create mode 100644 Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.h
|
||||
|
||||
@@ -39,10 +39,10 @@ index 3f02104..acf4c4c 100644
|
||||
LinuxSyscalls/SyscallsVMATracking.cpp
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp
|
||||
new file mode 100644
|
||||
index 0000000..4323999
|
||||
index 0000000..003db87
|
||||
--- /dev/null
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp
|
||||
@@ -0,0 +1,1932 @@
|
||||
@@ -0,0 +1,1994 @@
|
||||
+#include "LinuxSyscalls/GuestHighMemory.h"
|
||||
+#include "LinuxSyscalls/Syscalls.h"
|
||||
+#include "LinuxSyscalls/SignalDelegator.h"
|
||||
@@ -67,9 +67,11 @@ index 0000000..4323999
|
||||
+#include <fcntl.h>
|
||||
+#include <initializer_list>
|
||||
+#include <iterator>
|
||||
+#include <mutex>
|
||||
+#include <shared_mutex>
|
||||
+#include <sys/mman.h>
|
||||
+#include <unistd.h>
|
||||
+#include <utility>
|
||||
+
|
||||
+namespace FEX::HLE::GuestHighMemory {
|
||||
+namespace {
|
||||
@@ -1326,11 +1328,28 @@ index 0000000..4323999
|
||||
+ *Entries = Received ? Ready : 1;
|
||||
+ Storage->GuestMessages = GuestArray;
|
||||
+ Storage->ReceivedMessages = Received;
|
||||
+ Storage->Socket = static_cast<int>(Args[0]);
|
||||
+ Storage->SendFlags = static_cast<int>(Args[3]);
|
||||
+ if (!Received && Ready > 1) {
|
||||
+ Storage->Socket = fcntl(static_cast<int>(Args[0]), F_DUPFD_CLOEXEC, 0);
|
||||
+ if (Storage->Socket < 0) {
|
||||
+ Storage->Messages.resize(1);
|
||||
+ }
|
||||
+ }
|
||||
+ return 0;
|
||||
+ }
|
||||
+
|
||||
+ uint64_t MessageBytes(const struct msghdr& Message) {
|
||||
+ uint64_t Bytes = 0;
|
||||
+ for (uint64_t i = 0; i < std::min<uint64_t>(Message.msg_iovlen, MaxIoVectors); ++i) {
|
||||
+ struct iovec Vector {};
|
||||
+ if (FaultSafeUserMemAccess::CopyFromUser(&Vector, Message.msg_iov + i, sizeof(Vector)) != 0) {
|
||||
+ return 0;
|
||||
+ }
|
||||
+ Bytes += Vector.iov_len;
|
||||
+ }
|
||||
+ return Bytes;
|
||||
+ }
|
||||
+
|
||||
+ bool ReturnMessageFields(uint64_t Guest, const struct msghdr& Message) {
|
||||
+ return (Message.msg_name == nullptr ||
|
||||
+ CopyToGuest(Guest + offsetof(struct msghdr, msg_namelen), &Message.msg_namelen, sizeof(Message.msg_namelen))) &&
|
||||
@@ -1433,6 +1452,34 @@ index 0000000..4323999
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ std::mutex RetainedMutex;
|
||||
+ fextl::vector<fextl::vector<struct iovec>> RetainedVectors;
|
||||
+
|
||||
+ bool ForceAccess(uint64_t Host, uint64_t* Value, bool Write) {
|
||||
+ const int FD = open("/proc/self/mem", (Write ? O_RDWR : O_RDONLY) | O_CLOEXEC);
|
||||
+ if (FD < 0) {
|
||||
+ return false;
|
||||
+ }
|
||||
+ const auto Offset = static_cast<off_t>(Host);
|
||||
+ const auto Done = Write ? pwrite(FD, Value, sizeof(*Value), Offset) : pread(FD, Value, sizeof(*Value), Offset);
|
||||
+ close(FD);
|
||||
+ return Done == static_cast<ssize_t>(sizeof(*Value));
|
||||
+ }
|
||||
+
|
||||
+ bool ReadWord(uint64_t Host, uint64_t* Value) {
|
||||
+ return FaultSafeUserMemAccess::CopyFromUser(Value, reinterpret_cast<const void*>(Host), sizeof(*Value)) == 0 || ForceAccess(Host, Value, false);
|
||||
+ }
|
||||
+
|
||||
+ bool WriteWord(uint64_t Host, uint64_t Value) {
|
||||
+ return FaultSafeUserMemAccess::CopyToUser(reinterpret_cast<void*>(Host), &Value, sizeof(Value)) == 0 || ForceAccess(Host, &Value, true);
|
||||
+ }
|
||||
+
|
||||
+ void RetainVectors(ArgumentStorage* Storage) {
|
||||
+ auto lk = FEXCore::MaskSignalsAndLockMutex(RetainedMutex);
|
||||
+ RetainedVectors.push_back(std::move(Storage->Heap));
|
||||
+ Storage->Heap = {};
|
||||
+ }
|
||||
+
|
||||
+ bool Patch(uint64_t Guest, uint64_t Field, uint64_t Value, ArgumentStorage* Storage) {
|
||||
+ uint64_t Original {};
|
||||
+ if (FaultSafeUserMemAccess::CopyFromUser(&Original, reinterpret_cast<const void*>(Field), sizeof(Original)) != 0 ||
|
||||
@@ -1793,20 +1840,32 @@ index 0000000..4323999
|
||||
+}
|
||||
+
|
||||
+uint64_t FinishSyscallArguments(uint64_t Result, ArgumentStorage* Storage) {
|
||||
+ struct PinnedSocket {
|
||||
+ int FD;
|
||||
+ ~PinnedSocket() {
|
||||
+ if (FD >= 0) {
|
||||
+ close(FD);
|
||||
+ }
|
||||
+ }
|
||||
+ } Socket {std::exchange(Storage->Socket, -1)};
|
||||
+ if (!Storage->Restores.empty()) {
|
||||
+ auto lk = FEXCore::MaskSignalsAndLockMutex<std::shared_lock>(FEX::HLE::_SyscallHandler->VMATracking.Mutex);
|
||||
+ bool Stranded = false;
|
||||
+ for (auto Saved = Storage->Restores.rbegin(); Saved != Storage->Restores.rend(); ++Saved) {
|
||||
+ uint64_t Host {};
|
||||
+ uint64_t Current {};
|
||||
+ if (TranslateRange(Saved->Address, sizeof(Current), &Host) && Host == Saved->Host &&
|
||||
+ FaultSafeUserMemAccess::CopyFromUser(&Current, reinterpret_cast<const void*>(Host), sizeof(Current)) == 0 &&
|
||||
+ Current == Saved->Patched) {
|
||||
+ static_cast<void>(FaultSafeUserMemAccess::CopyToUser(reinterpret_cast<void*>(Host), &Saved->Value, sizeof(Saved->Value)));
|
||||
+ if (TranslateRange(Saved->Address, sizeof(Current), &Host) && Host == Saved->Host && ReadWord(Host, &Current) &&
|
||||
+ Current == Saved->Patched && !WriteWord(Host, Saved->Value)) {
|
||||
+ Stranded = true;
|
||||
+ }
|
||||
+ }
|
||||
+ if (Stranded) {
|
||||
+ RetainVectors(Storage);
|
||||
+ }
|
||||
+ }
|
||||
+ if (Storage->GuestCapabilityHeader != 0 && Storage->CapabilityHeader[0] != Storage->CapabilityVersion) {
|
||||
+ static_cast<void>(CopyToGuest(Storage->GuestCapabilityHeader, &Storage->CapabilityHeader[0], sizeof(Storage->CapabilityHeader[0])));
|
||||
+ if (Storage->GuestCapabilityHeader != 0 && Storage->CapabilityHeader[0] != Storage->CapabilityVersion &&
|
||||
+ !CopyToGuest(Storage->GuestCapabilityHeader, &Storage->CapabilityHeader[0], sizeof(Storage->CapabilityHeader[0]))) {
|
||||
+ return -EFAULT;
|
||||
+ }
|
||||
+ if (FEX::HLE::HasSyscallError(Result)) {
|
||||
+ return Result;
|
||||
@@ -1828,7 +1887,7 @@ index 0000000..4323999
|
||||
+ uint64_t Done = 0;
|
||||
+ for (; Done < Total; ++Done) {
|
||||
+ auto& Message = Storage->Messages[Done];
|
||||
+ if (Done >= Result && ::sendmmsg(Storage->Socket, &Message, 1, Storage->SendFlags) != 1) {
|
||||
+ if (Done >= Result && ::sendmmsg(Socket.FD, &Message, 1, Storage->SendFlags) != 1) {
|
||||
+ break;
|
||||
+ }
|
||||
+ const auto Guest = Storage->GuestMessages + Done * sizeof(struct mmsghdr);
|
||||
@@ -1836,6 +1895,9 @@ index 0000000..4323999
|
||||
+ !CopyToGuest(Guest + offsetof(struct mmsghdr, msg_len), &Message.msg_len, sizeof(Message.msg_len))) {
|
||||
+ return Done == 0 ? -EFAULT : Done;
|
||||
+ }
|
||||
+ if (!Storage->ReceivedMessages && Message.msg_len < MessageBytes(Message.msg_hdr)) {
|
||||
+ return Done + 1;
|
||||
+ }
|
||||
+ }
|
||||
+ return Done;
|
||||
+ }
|
||||
@@ -1977,7 +2039,7 @@ index 0000000..4323999
|
||||
+} // namespace FEX::HLE::GuestHighMemory
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.h b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.h
|
||||
new file mode 100644
|
||||
index 0000000..0217ee2
|
||||
index 0000000..8f6f010
|
||||
--- /dev/null
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.h
|
||||
@@ -0,0 +1,59 @@
|
||||
@@ -2014,7 +2076,7 @@ index 0000000..0217ee2
|
||||
+ fextl::vector<struct mmsghdr> Messages;
|
||||
+ uint64_t GuestMessages {};
|
||||
+ bool ReceivedMessages {};
|
||||
+ int Socket {};
|
||||
+ int Socket {-1};
|
||||
+ int SendFlags {};
|
||||
+ uint32_t CapabilityHeader[2] {};
|
||||
+ uint32_t CapabilityVersion {};
|
||||
|
||||
@@ -65,8 +65,11 @@ static int TestMunmap(void* Address, size_t Size) {
|
||||
#define munmap TestMunmap
|
||||
static int SendCalls;
|
||||
static int SendFailures;
|
||||
static size_t SendLimit = ~size_t {};
|
||||
static int SentSocket = -1;
|
||||
static size_t SentLengths[8];
|
||||
static int TestSendmmsg(int, struct mmsghdr* Messages, unsigned int Count, int) {
|
||||
static int TestSendmmsg(int Socket, struct mmsghdr* Messages, unsigned int Count, int) {
|
||||
SentSocket = Socket;
|
||||
if (Count != 1 || SendFailures > 0) {
|
||||
SendFailures -= SendFailures > 0;
|
||||
errno = EAGAIN;
|
||||
@@ -76,11 +79,24 @@ static int TestSendmmsg(int, struct mmsghdr* Messages, unsigned int Count, int)
|
||||
for (size_t i = 0; i < Messages[0].msg_hdr.msg_iovlen; ++i) {
|
||||
Length += Messages[0].msg_hdr.msg_iov[i].iov_len;
|
||||
}
|
||||
Length = std::min(Length, SendLimit);
|
||||
SentLengths[SendCalls++ & 7] = Length;
|
||||
Messages[0].msg_len = Length;
|
||||
return 1;
|
||||
}
|
||||
#define sendmmsg TestSendmmsg
|
||||
static int ForcedWrites;
|
||||
static int ForcedWriteFailures;
|
||||
static ssize_t TestPwrite(int FD, const void* Buffer, size_t Size, off_t Offset) {
|
||||
++ForcedWrites;
|
||||
if (ForcedWriteFailures > 0) {
|
||||
--ForcedWriteFailures;
|
||||
errno = EIO;
|
||||
return -1;
|
||||
}
|
||||
return pwrite(FD, Buffer, Size, Offset);
|
||||
}
|
||||
#define pwrite TestPwrite
|
||||
namespace FEXCore {
|
||||
template<template<typename> class LockType = std::unique_lock, typename MutexType>
|
||||
inline auto MaskSignalsAndLockMutex(MutexType& Mutex, uint64_t = ~0ULL) { return LockType<MutexType> {Mutex}; }
|
||||
@@ -152,6 +168,7 @@ int main() {
|
||||
AddEntry(Joined, Page, Base + 5 * Page, false, PROT_READ | PROT_WRITE);
|
||||
AddEntry(Joined + Page, Page, Base + 6 * Page, false, PROT_READ | PROT_WRITE);
|
||||
ArgumentStorage Storage;
|
||||
const int Peer = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
|
||||
|
||||
uint64_t Read[6] = {3, Single + 16, 32, 0, 0, 0};
|
||||
Show("read_result", Call(Sys::SYSCALL_x64_read, Read, &Storage));
|
||||
@@ -430,7 +447,7 @@ int main() {
|
||||
Outgoing[1].msg_hdr.msg_iov = &SendVectors[1];
|
||||
Outgoing[1].msg_hdr.msg_iovlen = 1;
|
||||
ArgumentStorage SendBatch;
|
||||
uint64_t Send[6] = {3, reinterpret_cast<uint64_t>(Outgoing), 2, 0, 0, 0};
|
||||
uint64_t Send[6] = {static_cast<uint64_t>(Peer), reinterpret_cast<uint64_t>(Outgoing), 2, 0, 0, 0};
|
||||
Show("sendmmsg_result", Call(Sys::SYSCALL_x64_sendmmsg, Send, &SendBatch));
|
||||
auto* HostSend = reinterpret_cast<struct mmsghdr*>(Send[1]);
|
||||
Show("sendmmsg_copied", HostSend != Outgoing);
|
||||
@@ -446,7 +463,7 @@ int main() {
|
||||
Outgoing[0].msg_len = 0;
|
||||
Outgoing[1].msg_len = 0;
|
||||
ArgumentStorage DeclinedBatch;
|
||||
uint64_t Declined[6] = {3, reinterpret_cast<uint64_t>(Outgoing), 2, 0, 0, 0};
|
||||
uint64_t Declined[6] = {static_cast<uint64_t>(Peer), reinterpret_cast<uint64_t>(Outgoing), 2, 0, 0, 0};
|
||||
Call(Sys::SYSCALL_x64_sendmmsg, Declined, &DeclinedBatch);
|
||||
reinterpret_cast<struct mmsghdr*>(Declined[1])[0].msg_len = 32;
|
||||
SendFailures = 1;
|
||||
@@ -781,7 +798,7 @@ int main() {
|
||||
ApartSecond->msg_hdr.msg_iov = &ApartVectors[1];
|
||||
ApartSecond->msg_hdr.msg_iovlen = 1;
|
||||
ArgumentStorage ApartBatch;
|
||||
uint64_t HeadersApart[6] = {3, Split + Page - sizeof(struct mmsghdr), 2, 0, 0, 0};
|
||||
uint64_t HeadersApart[6] = {static_cast<uint64_t>(Peer), Split + Page - sizeof(struct mmsghdr), 2, 0, 0, 0};
|
||||
Show("sendmmsg_headers_apart_result", Call(Sys::SYSCALL_x64_sendmmsg, HeadersApart, &ApartBatch));
|
||||
Show("sendmmsg_headers_apart_count", HeadersApart[2]);
|
||||
auto* ApartHost = reinterpret_cast<struct mmsghdr*>(HeadersApart[1]);
|
||||
@@ -828,7 +845,7 @@ int main() {
|
||||
}
|
||||
Protect(Sealed + Page, Page, PROT_READ);
|
||||
ArgumentStorage SealedBatch;
|
||||
uint64_t SealedArgs[6] = {3, Sealed + Page - sizeof(struct mmsghdr), 3, 0, 0, 0};
|
||||
uint64_t SealedArgs[6] = {static_cast<uint64_t>(Peer), Sealed + Page - sizeof(struct mmsghdr), 3, 0, 0, 0};
|
||||
Show("sealed_result", Call(Sys::SYSCALL_x64_sendmmsg, SealedArgs, &SealedBatch));
|
||||
Show("sealed_submitted", SealedArgs[2]);
|
||||
auto* SealedCopy = reinterpret_cast<struct mmsghdr*>(SealedArgs[1]);
|
||||
@@ -936,10 +953,22 @@ int main() {
|
||||
Show("recvmsg_nameless_finish", FinishResult(64, &NamelessReceive));
|
||||
Show("recvmsg_nameless_flags", NamelessHeader->msg_flags);
|
||||
char NameBuffer[16];
|
||||
const uint64_t Named = 0x700000000000ULL;
|
||||
Map(Named, 2 * Page, PROT_READ | PROT_WRITE, Anonymous, -1, 0);
|
||||
uint64_t NamedHost {};
|
||||
TranslateRange(Named, 2 * Page, &NamedHost);
|
||||
auto* NamedHeader = reinterpret_cast<struct msghdr*>(NamedHost + Page - 16);
|
||||
*NamedHeader = {};
|
||||
NamedHeader->msg_name = NameBuffer;
|
||||
NamedHeader->msg_namelen = sizeof(NameBuffer);
|
||||
NamedHeader->msg_iov = &NamelessVector;
|
||||
NamedHeader->msg_iovlen = 1;
|
||||
Protect(Named, Page, PROT_READ);
|
||||
ArgumentStorage NamedReceive;
|
||||
uint64_t NamedArgs[6] = {3, NamelessGuestHeader, 0, 0, 0, 0};
|
||||
Call(Sys::SYSCALL_x64_recvmsg, NamedArgs, &NamedReceive);
|
||||
NamedReceive.Message.msg_name = NameBuffer;
|
||||
uint64_t NamedArgs[6] = {3, Named + Page - 16, 0, 0, 0, 0};
|
||||
Show("recvmsg_named_result", Call(Sys::SYSCALL_x64_recvmsg, NamedArgs, &NamedReceive));
|
||||
Show("recvmsg_named_private", NamedArgs[1] != NamedHost + Page - 16);
|
||||
Show("recvmsg_named_name_kept", reinterpret_cast<const struct msghdr*>(NamedArgs[1])->msg_name == NameBuffer);
|
||||
Show("recvmsg_named_finish", FinishResult(64, &NamedReceive));
|
||||
|
||||
const uint64_t Flagged = 0x730000000000ULL;
|
||||
@@ -988,6 +1017,103 @@ int main() {
|
||||
Show("io_submit_replaced_unrelated_placed", reinterpret_cast<uint64_t>(UnrelatedBlock) == BlocksHost && ReplacementHost != BlocksHost);
|
||||
Show("io_submit_replaced_unrelated", UnrelatedBlock->Buffer);
|
||||
Show("io_submit_replaced_new", ReplacementBlock->Buffer);
|
||||
|
||||
const uint64_t Sealing = 0x710000000000ULL;
|
||||
Map(Sealing, Page, PROT_READ | PROT_WRITE, Anonymous, -1, 0);
|
||||
uint64_t SealingHost {};
|
||||
TranslateRange(Sealing, Page, &SealingHost);
|
||||
auto* SealingBlock = reinterpret_cast<TestIocb*>(SealingHost);
|
||||
*SealingBlock = {};
|
||||
SealingBlock->Opcode = 7;
|
||||
SealingBlock->Buffer = reinterpret_cast<uint64_t>(Scatter);
|
||||
SealingBlock->Bytes = 2;
|
||||
uint64_t SealingList[1] = {Sealing};
|
||||
ArgumentStorage SealingSubmit;
|
||||
uint64_t SealingArgs[6] = {1, 1, reinterpret_cast<uint64_t>(SealingList), 0, 0, 0};
|
||||
Show("io_submit_sealed_result", Call(Sys::SYSCALL_x64_io_submit, SealingArgs, &SealingSubmit));
|
||||
Show("io_submit_sealed_patched", SealingBlock->Buffer == reinterpret_cast<uint64_t>(SealingSubmit.Heap.data()));
|
||||
Protect(Sealing, Page, PROT_READ);
|
||||
ForcedWrites = 0;
|
||||
FinishResult(1, &SealingSubmit);
|
||||
Show("io_submit_sealed_forced", ForcedWrites);
|
||||
Show("io_submit_sealed_restored", SealingBlock->Buffer == reinterpret_cast<uint64_t>(Scatter));
|
||||
Protect(Sealing, Page, PROT_READ | PROT_WRITE);
|
||||
ArgumentStorage StrandedSubmit;
|
||||
uint64_t StrandedArgs[6] = {1, 1, reinterpret_cast<uint64_t>(SealingList), 0, 0, 0};
|
||||
Show("io_submit_stranded_result", Call(Sys::SYSCALL_x64_io_submit, StrandedArgs, &StrandedSubmit));
|
||||
const auto* StrandedVectors = StrandedSubmit.Heap.data();
|
||||
Protect(Sealing, Page, PROT_READ);
|
||||
const auto RetainedBefore = RetainedVectors.size();
|
||||
ForcedWriteFailures = 1;
|
||||
FinishResult(1, &StrandedSubmit);
|
||||
Show("io_submit_stranded_points", SealingBlock->Buffer == reinterpret_cast<uint64_t>(StrandedVectors));
|
||||
Show("io_submit_stranded_retained", RetainedVectors.size() - RetainedBefore);
|
||||
Show("io_submit_stranded_kept", RetainedVectors.back().data() == StrandedVectors && StrandedSubmit.Heap.empty());
|
||||
Show("io_submit_stranded_base", reinterpret_cast<uint64_t>(StrandedVectors[0].iov_base) - Base);
|
||||
|
||||
auto* CapPage = static_cast<uint32_t*>(mmap(nullptr, Page, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
|
||||
CapPage[0] = 0x1234;
|
||||
ArgumentStorage CapSealed;
|
||||
uint64_t CapSealedArgs[6] = {reinterpret_cast<uint64_t>(CapPage), Single + 64, 0, 0, 0, 0};
|
||||
Show("capget_sealed_result", Call(Sys::SYSCALL_x64_capget, CapSealedArgs, &CapSealed));
|
||||
reinterpret_cast<uint32_t*>(CapSealedArgs[0])[0] = 0x20080522;
|
||||
mprotect(CapPage, Page, PROT_READ);
|
||||
Show("capget_sealed_finish", FinishResult(static_cast<uint64_t>(-EINVAL), &CapSealed));
|
||||
Show("capget_sealed_version", CapPage[0]);
|
||||
|
||||
struct iovec ShortVector = {reinterpret_cast<void*>(Single + 256), 32};
|
||||
struct mmsghdr Shorts[3] {};
|
||||
for (auto& Short : Shorts) {
|
||||
Short.msg_hdr.msg_iov = &ShortVector;
|
||||
Short.msg_hdr.msg_iovlen = 1;
|
||||
}
|
||||
ArgumentStorage ShortFirst;
|
||||
uint64_t ShortFirstArgs[6] = {static_cast<uint64_t>(Peer), reinterpret_cast<uint64_t>(Shorts), 3, 0, 0, 0};
|
||||
Show("sendmmsg_short_first_result", Call(Sys::SYSCALL_x64_sendmmsg, ShortFirstArgs, &ShortFirst));
|
||||
reinterpret_cast<struct mmsghdr*>(ShortFirstArgs[1])[0].msg_len = 10;
|
||||
SendCalls = 0;
|
||||
Show("sendmmsg_short_first_finish", FinishResult(1, &ShortFirst));
|
||||
Show("sendmmsg_short_first_sends", SendCalls);
|
||||
Show("sendmmsg_short_first_lengths", Shorts[0].msg_len * 0x10000 + Shorts[1].msg_len * 0x100 + Shorts[2].msg_len);
|
||||
for (auto& Short : Shorts) {
|
||||
Short.msg_len = 0;
|
||||
}
|
||||
ArgumentStorage ShortSecond;
|
||||
uint64_t ShortSecondArgs[6] = {static_cast<uint64_t>(Peer), reinterpret_cast<uint64_t>(Shorts), 3, 0, 0, 0};
|
||||
Show("sendmmsg_short_second_result", Call(Sys::SYSCALL_x64_sendmmsg, ShortSecondArgs, &ShortSecond));
|
||||
Show("sendmmsg_short_second_submitted", ShortSecondArgs[2]);
|
||||
const int Pinned = ShortSecond.Socket;
|
||||
Show("sendmmsg_pinned_distinct", Pinned >= 0 && Pinned != Peer);
|
||||
reinterpret_cast<struct mmsghdr*>(ShortSecondArgs[1])[0].msg_len = 32;
|
||||
SendCalls = 0;
|
||||
SendLimit = 5;
|
||||
SentSocket = -1;
|
||||
Show("sendmmsg_short_second_finish", FinishResult(1, &ShortSecond));
|
||||
SendLimit = ~size_t {};
|
||||
Show("sendmmsg_short_second_sends", SendCalls);
|
||||
Show("sendmmsg_short_second_lengths", Shorts[0].msg_len * 0x10000 + Shorts[1].msg_len * 0x100 + Shorts[2].msg_len);
|
||||
Show("sendmmsg_pinned_used", SentSocket == Pinned);
|
||||
Show("sendmmsg_pinned_closed", fcntl(Pinned, F_GETFD) == -1 && errno == EBADF && ShortSecond.Socket == -1);
|
||||
for (auto& Short : Shorts) {
|
||||
Short.msg_len = 0;
|
||||
}
|
||||
ArgumentStorage ShortFailed;
|
||||
uint64_t ShortFailedArgs[6] = {static_cast<uint64_t>(Peer), reinterpret_cast<uint64_t>(Shorts), 3, 0, 0, 0};
|
||||
Call(Sys::SYSCALL_x64_sendmmsg, ShortFailedArgs, &ShortFailed);
|
||||
const int FailedPin = ShortFailed.Socket;
|
||||
Show("sendmmsg_error_finish", FinishResult(static_cast<uint64_t>(-EAGAIN), &ShortFailed));
|
||||
Show("sendmmsg_error_closed", FailedPin >= 0 && fcntl(FailedPin, F_GETFD) == -1 && errno == EBADF);
|
||||
const int Closed = dup(Peer);
|
||||
close(Closed);
|
||||
ArgumentStorage Unpinned;
|
||||
uint64_t UnpinnedArgs[6] = {static_cast<uint64_t>(Closed), reinterpret_cast<uint64_t>(Shorts), 3, 0, 0, 0};
|
||||
Show("sendmmsg_unpinned_result", Call(Sys::SYSCALL_x64_sendmmsg, UnpinnedArgs, &Unpinned));
|
||||
Show("sendmmsg_unpinned_submitted", UnpinnedArgs[2]);
|
||||
Show("sendmmsg_unpinned_queued", Unpinned.Messages.size());
|
||||
reinterpret_cast<struct mmsghdr*>(UnpinnedArgs[1])[0].msg_len = 32;
|
||||
SendCalls = 0;
|
||||
Show("sendmmsg_unpinned_finish", FinishResult(1, &Unpinned));
|
||||
Show("sendmmsg_unpinned_sends", SendCalls);
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
@@ -1281,6 +1407,17 @@ class GuestHighMemoryTest(unittest.TestCase):
|
||||
self.assertIn(lock, block(lines, "uint64_t TranslateIoSubmit("))
|
||||
self.assertIn(lock, block(lines, "uint64_t FinishSyscallArguments("))
|
||||
|
||||
def test_aio_restores_reach_read_only_control_blocks(self):
|
||||
self.assertEqual(self.value("io_submit_sealed_result"), 0)
|
||||
self.assertEqual(self.value("io_submit_sealed_patched"), 1)
|
||||
self.assertEqual(self.value("io_submit_sealed_forced"), 1)
|
||||
self.assertEqual(self.value("io_submit_sealed_restored"), 1)
|
||||
self.assertEqual(self.value("io_submit_stranded_result"), 0)
|
||||
self.assertEqual(self.value("io_submit_stranded_points"), 1)
|
||||
self.assertEqual(self.value("io_submit_stranded_retained"), 1)
|
||||
self.assertEqual(self.value("io_submit_stranded_kept"), 1)
|
||||
self.assertEqual(self.value("io_submit_stranded_base"), 800)
|
||||
|
||||
def test_aio_completions_report_guest_control_blocks(self):
|
||||
self.assertEqual(self.value("io_getevents_result"), 0)
|
||||
self.assertEqual(self.value("io_getevents_object"), 1536)
|
||||
@@ -1376,10 +1513,41 @@ class GuestHighMemoryTest(unittest.TestCase):
|
||||
self.assertEqual(self.value("capget_invalid_finish"), (1 << 64) - 22)
|
||||
self.assertEqual(self.value("capget_invalid_version"), 0x20080522)
|
||||
|
||||
def test_capability_header_write_back_faults(self):
|
||||
self.assertEqual(self.value("capget_sealed_result"), 0)
|
||||
self.assertEqual(self.value("capget_sealed_finish"), self.efault())
|
||||
self.assertEqual(self.value("capget_sealed_version"), 0x1234)
|
||||
|
||||
def test_short_sends_end_the_message_batch(self):
|
||||
self.assertEqual(self.value("sendmmsg_short_first_result"), 0)
|
||||
self.assertEqual(self.value("sendmmsg_short_first_finish"), 1)
|
||||
self.assertEqual(self.value("sendmmsg_short_first_sends"), 0)
|
||||
self.assertEqual(self.value("sendmmsg_short_first_lengths"), 0x0A0000)
|
||||
self.assertEqual(self.value("sendmmsg_short_second_result"), 0)
|
||||
self.assertEqual(self.value("sendmmsg_short_second_finish"), 2)
|
||||
self.assertEqual(self.value("sendmmsg_short_second_sends"), 1)
|
||||
self.assertEqual(self.value("sendmmsg_short_second_lengths"), 0x200500)
|
||||
|
||||
def test_message_batches_pin_their_socket(self):
|
||||
self.assertEqual(self.value("sendmmsg_short_second_submitted"), 1)
|
||||
self.assertEqual(self.value("sendmmsg_pinned_distinct"), 1)
|
||||
self.assertEqual(self.value("sendmmsg_pinned_used"), 1)
|
||||
self.assertEqual(self.value("sendmmsg_pinned_closed"), 1)
|
||||
self.assertEqual(self.value("sendmmsg_error_finish"), (1 << 64) - 11)
|
||||
self.assertEqual(self.value("sendmmsg_error_closed"), 1)
|
||||
self.assertEqual(self.value("sendmmsg_unpinned_result"), 0)
|
||||
self.assertEqual(self.value("sendmmsg_unpinned_submitted"), 1)
|
||||
self.assertEqual(self.value("sendmmsg_unpinned_queued"), 1)
|
||||
self.assertEqual(self.value("sendmmsg_unpinned_finish"), 1)
|
||||
self.assertEqual(self.value("sendmmsg_unpinned_sends"), 0)
|
||||
|
||||
def test_received_fields_follow_the_kernel_write_order(self):
|
||||
self.assertEqual(self.value("recvmsg_nameless_result"), 0)
|
||||
self.assertEqual(self.value("recvmsg_nameless_finish"), 64)
|
||||
self.assertEqual(self.value("recvmsg_nameless_flags"), 0x20)
|
||||
self.assertEqual(self.value("recvmsg_named_result"), 0)
|
||||
self.assertEqual(self.value("recvmsg_named_private"), 1)
|
||||
self.assertEqual(self.value("recvmsg_named_name_kept"), 1)
|
||||
self.assertEqual(self.value("recvmsg_named_finish"), self.efault())
|
||||
self.assertEqual(self.value("recvmsg_flags_first_result"), 0)
|
||||
self.assertEqual(self.value("recvmsg_flags_first_finish"), self.efault())
|
||||
|
||||
Reference in new issue
Block a user