Validate section patch sizes in case object files are invalid (#2242)

This commit is contained in:
Rangi authored and GitHub committed 2026-11-15 01:50:34 +01:00
1 parent a2086efa50
commit bec11bcaa7
3 files changed
+22 -10

No files matched your search

+12 -1
View File
@@ -8,7 +8,7 @@
#include <string>
#include <vector>
#include "helpers.hpp" // QualifiedEquivalent
#include "helpers.hpp" // assume, QualifiedEquivalent
#include "linkdefs.hpp"
struct FileStackNode;
@@ -28,6 +28,17 @@ struct Patch {
Expression rpn;
uint32_t offset;
PatchType type;
uint8_t typeSize() const {
assume(type < PATCHTYPE_INVALID);
uint8_t typeSizes[PATCHTYPE_INVALID] = {
1, // PATCHTYPE_BYTE
2, // PATCHTYPE_WORD
4, // PATCHTYPE_LONG
1, // PATCHTYPE_JR
};
return typeSizes[type];
}
};
struct Section {
+9
View File
@@ -383,6 +383,15 @@ static void readSection(
patch.type = PatchType(type);
}
if (patch.offset > section.size || patch.offset + patch.typeSize() > section.size) {
fatal(
"%s: \"%s\"'s patch #%" PRIu32 " overflows the section",
fileName,
section.name.c_str(),
patchID
);
}
uint32_t rpnSize;
tryReadLong(
rpnSize,
+1 -9
View File
@@ -560,15 +560,7 @@ static void applyFilePatches(Section &section, Section &dataSection) {
for (Patch &patch : section.patches) {
int32_t value = computeRPNExpr(patch.rpn, *section.fileSymbols);
uint32_t offset = patch.offset + section.offset;
uint8_t typeSizes[PATCHTYPE_INVALID] = {
1, // PATCHTYPE_BYTE
2, // PATCHTYPE_WORD
4, // PATCHTYPE_LONG
1, // PATCHTYPE_JR
};
uint8_t typeSize = typeSizes[patch.type];
uint8_t typeSize = patch.typeSize();
if (dataSection.data.size() < offset + typeSize) {
rpnErrorAt(
patch.rpn,