Make XDR decoding more robust.

This commit is contained in:
wolfbeast 2018-01-26 21:26:07 +01:00 • committed by Roy Tam
commit 537415eeb7
5 changed files with 57 additions and 23 deletions

View file

@ -143,13 +143,17 @@ class XDRState {
template <typename T>
bool codeEnum32(T* val, typename mozilla::EnableIf<mozilla::IsEnum<T>::value, T>::Type * = NULL)
{
// Mix the enumeration value with a random magic number, such that a
// corruption with a low-ranged value (like 0) is less likely to cause a
// miss-interpretation of the XDR content and instead cause a failure.
const uint32_t MAGIC = 0xAF647BCE;
uint32_t tmp;
if (mode == XDR_ENCODE)
tmp = uint32_t(*val);
tmp = uint32_t(*val) ^ MAGIC;
if (!codeUint32(&tmp))
return false;
if (mode == XDR_DECODE)
*val = T(tmp);
*val = T(tmp ^ MAGIC);
return true;
}
@ -167,6 +171,18 @@ class XDRState {
return true;
}
bool codeMarker(uint32_t magic) {
uint32_t actual = magic;
if (!codeUint32(&actual))
return false;
if (actual != magic) {
// Fail in debug, but only soft-fail in release
MOZ_ASSERT(false, "Bad XDR marker");
return fail(JS::TranscodeResult_Failure_BadDecode);
}
return true;
}
bool codeBytes(void* bytes, size_t len) {
if (len == 0)
return true;