d3d12_xs: verify DXBC total_size and incoming checksum before re-signing RTS0 blobs

This commit is contained in:
Massy Collom
2026-11-23 20:51:51 +01:00
parent 867d922f09
commit b95925cbfc
3 changed files with 99 additions and 5 deletions

View File

@ -151,12 +151,28 @@ bool copy_rts0_blob_clear_xbox_flags(const void* data, size_t size, std::vector<
static_cast<D3D12_ROOT_SIGNATURE_FLAGS>(flags)));
if (cleared == flags) return false; // no Xbox high byte
uint32_t total_size = 0;
std::memcpy(&total_size, bytes + 24, sizeof(total_size));
if (total_size != size) return false;
// Do not forge a valid hash for a corrupted container: host d3d12.dll accepts a matching
// DXBC checksum or an all-zero checksum, and rejects any other checksum with E_INVALIDARG.
constexpr uint8_t kZeroHash[16] = {};
const bool zero_hash = std::memcmp(bytes + 4, kZeroHash, sizeof(kZeroHash)) == 0;
if (!zero_hash) {
uint32_t expected[4];
dxbc_checksum(bytes, size, expected);
if (std::memcmp(bytes + 4, expected, sizeof(expected)) != 0) return false;
}
out.assign(bytes, bytes + size);
std::memcpy(out.data() + off + 8 + 20, &cleared, sizeof(cleared));
// The host deserializer checks the container hash (bytes 4..19), so a stale one is E_INVALIDARG.
uint32_t hash[4];
dxbc_checksum(out.data(), out.size(), hash);
std::memcpy(out.data() + 4, hash, sizeof(hash));
if (!zero_hash) {
uint32_t hash[4];
dxbc_checksum(out.data(), out.size(), hash);
std::memcpy(out.data() + 4, hash, sizeof(hash));
}
return true;
}
return false;

View File

@ -36,8 +36,10 @@ void copy_root_signature_desc_clear_xbox_flags(const D3D12_ROOT_SIGNATURE_DESC&
// D3D12CreateRootSignatureDeserializer path: the host deserializer accepts a correctly hashed blob whose RTS0
// flags carry the Xbox high byte and hands the byte back, and CreateRootSignature then refuses it (E_FAIL on WARP
// and an RTX 5070). So copy the blob, clear bits 24-31 of the RTS0 flags, recompute the DXBC hash the host
// deserializer checks (a stale one is E_INVALIDARG), and return true with the copy in `out`. Returns false (and
// leaves `out` empty) when no rewrite is needed: no RTS0 chunk, no Xbox high byte, or a malformed/truncated header.
// deserializer checks (a stale one is E_INVALIDARG; an all-zero hash is accepted as-is and kept zeroed), and return
// true with the copy in `out`. Returns false (and leaves `out` empty) when no rewrite is needed or the container is
// not valid to re-sign: no RTS0 chunk, no Xbox high byte, a malformed/truncated header, total_size != size, or a
// non-zero incoming DXBC hash that does not match the input bytes.
bool copy_rts0_blob_clear_xbox_flags(const void* data, size_t size, std::vector<uint8_t>& out);
// The DXBC container hash of `blob` (bytes 20..size), as stored at bytes 4..19. `size` must be at least 20.

View File

@ -489,6 +489,82 @@ void test_deserializer() {
check(d->Flags == 0, "shim deserializer returns flags with the high byte cleared");
shim_de->Release();
}
// Corrupted non-zero DXBC checksum: the helper must not forge a valid hash over a corrupted container.
{
std::vector<uint8_t> bad_hash = tampered;
bad_hash[4] ^= 0x01;
std::vector<uint8_t> out(1, 0xCC);
const bool rewrote = rt::d3d12_xs::copy_rts0_blob_clear_xbox_flags(bad_hash.data(), bad_hash.size(), out);
check(!rewrote && out.empty(), "blob helper refuses to rewrite and re-sign a corrupted DXBC checksum");
ID3D12RootSignatureDeserializer* host_bad = nullptr;
const HRESULT hr_host =
D3D12CreateRootSignatureDeserializer(bad_hash.data(), bad_hash.size(), IID_PPV_ARGS(&host_bad));
if (host_bad) host_bad->Release();
if (FAILED(hr_host)) {
ID3D12RootSignatureDeserializer* shim_bad = nullptr;
const HRESULT hr_shim = deserialize(bad_hash.data(), bad_hash.size(), IID_PPV_ARGS(&shim_bad));
check(FAILED(hr_shim) && !shim_bad,
"shim deserializer rejects a corrupted DXBC checksum just as the host does");
if (shim_bad) shim_bad->Release();
} else {
std::printf("SKIP host-vs-shim corrupted checksum rejection (this host accepts bad checksums)\n");
}
}
// Trailing bytes past DXBC total_size (bytes 24..27): host rejects and the helper must not rewrite/rehash it.
{
std::vector<uint8_t> trailing = tampered;
trailing.resize(trailing.size() + 16, 0xAB);
std::vector<uint8_t> out(1, 0xCC);
const bool rewrote = rt::d3d12_xs::copy_rts0_blob_clear_xbox_flags(trailing.data(), trailing.size(), out);
check(!rewrote && out.empty(), "blob helper refuses a DXBC container with trailing bytes past total_size");
ID3D12RootSignatureDeserializer* host_trail = nullptr;
const HRESULT hr_host =
D3D12CreateRootSignatureDeserializer(trailing.data(), trailing.size(), IID_PPV_ARGS(&host_trail));
if (host_trail) host_trail->Release();
if (FAILED(hr_host)) {
ID3D12RootSignatureDeserializer* shim_trail = nullptr;
const HRESULT hr_shim = deserialize(trailing.data(), trailing.size(), IID_PPV_ARGS(&shim_trail));
check(FAILED(hr_shim) && !shim_trail,
"shim deserializer rejects trailing bytes past total_size just as the host does");
if (shim_trail) shim_trail->Release();
} else {
std::printf("SKIP host-vs-shim trailing-bytes rejection (this host accepts trailing bytes)\n");
}
}
// Mismatched total_size header field (even if bytes 4..19 hold dxbc_checksum of the buffer length).
{
std::vector<uint8_t> bad_total = tampered;
uint32_t bogus_total = static_cast<uint32_t>(bad_total.size() + 4);
std::memcpy(bad_total.data() + 24, &bogus_total, sizeof(bogus_total));
uint32_t bad_total_hash[4];
rt::d3d12_xs::dxbc_checksum(bad_total.data(), bad_total.size(), bad_total_hash);
std::memcpy(bad_total.data() + 4, bad_total_hash, sizeof(bad_total_hash));
std::vector<uint8_t> out(1, 0xCC);
const bool rewrote = rt::d3d12_xs::copy_rts0_blob_clear_xbox_flags(bad_total.data(), bad_total.size(), out);
check(!rewrote && out.empty(), "blob helper refuses a DXBC container whose header total_size != size");
}
// All-zero DXBC checksum: accepted by host d3d12.dll, so the helper clears the Xbox high byte and keeps it zeroed.
{
std::vector<uint8_t> zero_hash = tampered;
std::memset(zero_hash.data() + 4, 0, 16);
std::vector<uint8_t> out;
const bool rewrote = rt::d3d12_xs::copy_rts0_blob_clear_xbox_flags(zero_hash.data(), zero_hash.size(), out);
constexpr uint8_t kZero[16] = {};
check(rewrote && out.size() == zero_hash.size() && std::memcmp(out.data() + 4, kZero, 16) == 0,
"blob helper rewrites an all-zero-checksum Xbox blob and keeps its zero checksum");
ID3D12RootSignatureDeserializer* shim_zero = nullptr;
const HRESULT hr_zero = deserialize(zero_hash.data(), zero_hash.size(), IID_PPV_ARGS(&shim_zero));
check(SUCCEEDED(hr_zero) && shim_zero && shim_zero->GetRootSignatureDesc()->Flags == 0,
"shim deserializer accepts an all-zero-checksum Xbox blob with the high byte cleared");
if (shim_zero) shim_zero->Release();
}
}
} // namespace