Fix integer overflow and off-by-one in binfmt itemref parsing

This commit is contained in:
Yorhel 2024-08-23 07:53:05 +02:00
parent ca46c7241f
commit cc26ead5f8

View file

@ -242,7 +242,7 @@ const CborVal = struct {
fn itemref(v: *const CborVal, cur: u64) u64 { fn itemref(v: *const CborVal, cur: u64) u64 {
if (v.major == .pos) return v.arg; if (v.major == .pos) return v.arg;
if (v.major == .neg) { if (v.major == .neg) {
if (v.arg > (1<<24)) die(); if (v.arg >= (cur & 0xffffff)) die();
return cur - v.arg - 1; return cur - v.arg - 1;
} }
return die(); return die();
@ -357,7 +357,7 @@ fn readItem(ref: u64) ItemParser {
global.lastitem = ref; global.lastitem = ref;
if (ref >= (1 << (24 + 32))) die(); if (ref >= (1 << (24 + 32))) die();
const block = readBlock(@intCast(ref >> 24)); const block = readBlock(@intCast(ref >> 24));
if ((ref & 0xffffff) > block.len) die(); if ((ref & 0xffffff) >= block.len) die();
return ItemParser.init(block[@intCast(ref & 0xffffff)..]); return ItemParser.init(block[@intCast(ref & 0xffffff)..]);
} }