mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-11-26 01:00:36 +08:00
fix(tinybson): reject a negative document length (#28607)
The BSON document length is the first four bytes of the document and is signed. bson_decoder_init_buf() checked it against the buffer size, but the check only ran when the value was positive, so a negative length skipped it and decoding proceeded with no bound. bson_decoder_init_file() did not check the length at all. Reject a negative length in both, and add a regression test, since the existing BSON test only covers a round trip of data it encoded itself. Reported by @lihnucs. Assisted-by: Claude:claude-opus-5[1m] Signed-off-by: Julian Oes <julian@oes.ch>
This commit is contained in:
1 parent
4547119094
commit
c8a6cc3656
2 files changed
+36
No files matched your search
@@ -257,6 +257,27 @@ decode(bson_decoder_t decoder)
|
||||
} while (result > 0);
|
||||
}
|
||||
|
||||
static int
|
||||
test_negative_document_size()
|
||||
{
|
||||
// The document length is the first four bytes and is signed. A negative value must be
|
||||
// rejected: it would otherwise skip the "larger than buffer" check, which only rejects
|
||||
// lengths above the buffer size.
|
||||
uint8_t buf[16] {};
|
||||
const int32_t negative_size = -2;
|
||||
memcpy(buf, &negative_size, sizeof(negative_size));
|
||||
|
||||
bson_decoder_s decoder{};
|
||||
|
||||
if (bson_decoder_init_buf(&decoder, buf, sizeof(buf), decode_callback) == 0) {
|
||||
PX4_ERR("FAIL: decoder: accepted a negative document length");
|
||||
return 1;
|
||||
}
|
||||
|
||||
PX4_INFO("PASS: decoder: rejected a negative document length");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
test_bson(int argc, char *argv[])
|
||||
{
|
||||
@@ -295,5 +316,9 @@ test_bson(int argc, char *argv[])
|
||||
decode(&decoder);
|
||||
free(buf);
|
||||
|
||||
if (test_negative_document_size() != 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return PX4_OK;
|
||||
}
|
||||
Reference in new issue
Block a user