Check against overflow due to corrupted length field

This commit is contained in:
Marcus Holland-Moritz 2021-03-28 23:06:27 +02:00
parent dff559bc3f
commit 3cf157421d

View File

@ -98,11 +98,18 @@ class filesystem_parser {
auto sh = mm.as<section_header_v2>(pos); auto sh = mm.as<section_header_v2>(pos);
if (sh->number == 0) { if (sh->number == 0) {
if (pos + 2 * sizeof(section_header_v2) + sh->length >= mm.size()) { auto endpos = pos + sh->length + 2 * sizeof(section_header_v2);
if (endpos < sh->length) {
// overflow
break; break;
} }
ps = mm.as<void>(pos + sizeof(section_header_v2) + sh->length); if (endpos >= mm.size()) {
break;
}
ps = mm.as<void>(pos + sh->length + sizeof(section_header_v2));
if (::memcmp(ps, magic.data(), magic.size()) == 0 and if (::memcmp(ps, magic.data(), magic.size()) == 0 and
reinterpret_cast<section_header_v2 const*>(ps)->number == 1) { reinterpret_cast<section_header_v2 const*>(ps)->number == 1) {