mirror of
https://github.com/par274/sharpemu.git
synced 2026-07-22 19:06:15 +08:00
[CPU] Reject context transfers to unmapped guest addresses (#273)
This commit is contained in:
@@ -1768,14 +1768,16 @@ public sealed unsafe partial class DirectExecutionBackend : INativeCpuBackend, I
|
||||
frameAddress = 0;
|
||||
transferStub = 0;
|
||||
error = null;
|
||||
if (target.Rip < 65536 || target.Rsp == 0)
|
||||
if (ActiveCpuContext is not { } activeContext)
|
||||
{
|
||||
error = $"invalid guest context transfer target rip=0x{target.Rip:X16} rsp=0x{target.Rsp:X16}";
|
||||
error = "guest context transfer without an active CPU context";
|
||||
return false;
|
||||
}
|
||||
if (target.Rsp < sizeof(ulong) ||
|
||||
ActiveCpuContext is not { } activeContext ||
|
||||
!activeContext.TryWriteUInt64(target.Rsp - sizeof(ulong), target.Rip))
|
||||
if (!TryValidateGuestContextTransferTarget(activeContext.Memory, target, out error))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!activeContext.TryWriteUInt64(target.Rsp - sizeof(ulong), target.Rip))
|
||||
{
|
||||
error = $"guest context transfer slot is not writable at 0x{target.Rsp - sizeof(ulong):X16}";
|
||||
return false;
|
||||
@@ -1819,6 +1821,30 @@ public sealed unsafe partial class DirectExecutionBackend : INativeCpuBackend, I
|
||||
return true;
|
||||
}
|
||||
|
||||
internal static bool TryValidateGuestContextTransferTarget(
|
||||
ICpuMemory memory,
|
||||
in GuestCpuContinuation target,
|
||||
out string? error)
|
||||
{
|
||||
if (target.Rip < 65536 || target.Rsp < sizeof(ulong))
|
||||
{
|
||||
error = $"invalid guest context transfer target rip=0x{target.Rip:X16} rsp=0x{target.Rsp:X16}";
|
||||
return false;
|
||||
}
|
||||
|
||||
Span<byte> ripProbe = stackalloc byte[1];
|
||||
if (!memory.TryRead(target.Rip, ripProbe))
|
||||
{
|
||||
error =
|
||||
$"guest context transfer target rip=0x{target.Rip:X16} is not mapped guest memory " +
|
||||
$"(rsp=0x{target.Rsp:X16})";
|
||||
return false;
|
||||
}
|
||||
|
||||
error = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
private unsafe nint GetOrCreateGuestContextTransferStub()
|
||||
{
|
||||
if (Volatile.Read(ref _guestContextTransferStub) != 0)
|
||||
|
||||
@@ -14,6 +14,10 @@ SPDX-License-Identifier: GPL-2.0-or-later
|
||||
<PackageReference Include="Iced" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="SharpEmu.Libs.Tests" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<NoWarn>$(NoWarn);1591</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
Reference in New Issue
Block a user