Skip to content

Commit 6bdaa4e

Browse files
committed
prevent variable headers <=4 from reading OOB
1 parent 8225d0b commit 6bdaa4e

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

tinyfseq.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ typedef enum tf_err_t {
1818
TF_EINVALID_MAGIC,
1919
TF_EINVALID_COMPRESSION_TYPE,
2020
TF_EINVALID_BUFFER_SIZE,
21+
TF_EINVALID_VAR_SIZE,
2122
} TFError;
2223

2324
/**
@@ -145,6 +146,8 @@ const char *TFError_string(const TFError err) {
145146
return "TF_EINVALID_COMPRESSION_TYPE (unknown compression identifier)";
146147
case TF_EINVALID_BUFFER_SIZE:
147148
return "TF_EINVALID_BUFFER_SIZE (undersized data decoding buffer argument)";
149+
case TF_EINVALID_VAR_SIZE:
150+
return "TF_EINVALID_VAR_SIZE (invalid variable size in header)";
148151
default:
149152
return "unknown TFError value";
150153
}
@@ -228,13 +231,18 @@ TFError TFVarHeader_read(const uint8_t *const bd,
228231

229232
varHeader->size = ((uint16_t *) &bd[0])[0];
230233

234+
if (varHeader->size <= VAR_HEADER_SIZE) return TF_EINVALID_VAR_SIZE;
235+
231236
__builtin_memcpy(varHeader->id, &bd[2], sizeof(varHeader->id));
232237

233238
// only attempt to read variable value if a decoding buffer (`vd`) is provided
234239
// `.size` already includes the 4 bytes the header consumes
235240
if (vd) {
236-
const int valueSize = varHeader->size - VAR_HEADER_SIZE;
241+
// ensure the source buffer has enough data to read the variable value
242+
if (bs < varHeader->size) return TF_EINVALID_VAR_SIZE;
237243

244+
// ensure the destination buffer is large enough to store the variable value
245+
const int valueSize = varHeader->size - VAR_HEADER_SIZE;
238246
if (vs < valueSize) return TF_EINVALID_BUFFER_SIZE;
239247

240248
__builtin_memcpy(vd, &bd[VAR_HEADER_SIZE], valueSize);

0 commit comments

Comments
 (0)