mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-26 16:52:40 +00:00
Added alignment checking to FlexBuffers verifier
This commit is contained in:
@@ -1645,6 +1645,7 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
|
|||||||
// comes at the cost of using additional memory the same size of
|
// comes at the cost of using additional memory the same size of
|
||||||
// the buffer being verified, so it is by default off.
|
// the buffer being verified, so it is by default off.
|
||||||
std::vector<uint8_t> *reuse_tracker = nullptr,
|
std::vector<uint8_t> *reuse_tracker = nullptr,
|
||||||
|
bool _check_alignment = true,
|
||||||
size_t max_depth = 64)
|
size_t max_depth = 64)
|
||||||
: buf_(buf),
|
: buf_(buf),
|
||||||
size_(buf_len),
|
size_(buf_len),
|
||||||
@@ -1652,6 +1653,7 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
|
|||||||
max_depth_(max_depth),
|
max_depth_(max_depth),
|
||||||
num_vectors_(0),
|
num_vectors_(0),
|
||||||
max_vectors_(buf_len),
|
max_vectors_(buf_len),
|
||||||
|
check_alignment_(_check_alignment),
|
||||||
reuse_tracker_(reuse_tracker) {
|
reuse_tracker_(reuse_tracker) {
|
||||||
FLATBUFFERS_ASSERT(size_ < FLATBUFFERS_MAX_BUFFER_SIZE);
|
FLATBUFFERS_ASSERT(size_ < FLATBUFFERS_MAX_BUFFER_SIZE);
|
||||||
if (reuse_tracker_) {
|
if (reuse_tracker_) {
|
||||||
@@ -1701,6 +1703,11 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
|
|||||||
off <= static_cast<uint64_t>(p - buf_);
|
off <= static_cast<uint64_t>(p - buf_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool VerifyAlignment(const uint8_t *p, size_t size) const {
|
||||||
|
auto o = static_cast<size_t>(p - buf_);
|
||||||
|
return Check((o & (size - 1)) == 0 || !check_alignment_);
|
||||||
|
}
|
||||||
|
|
||||||
// Macro, since we want to escape from parent function & use lazy args.
|
// Macro, since we want to escape from parent function & use lazy args.
|
||||||
#define FLEX_CHECK_VERIFIED(P, PACKED_TYPE) \
|
#define FLEX_CHECK_VERIFIED(P, PACKED_TYPE) \
|
||||||
if (reuse_tracker_) { \
|
if (reuse_tracker_) { \
|
||||||
@@ -1795,6 +1802,8 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
|
|||||||
if (!VerifyOffset(off, r.data_))
|
if (!VerifyOffset(off, r.data_))
|
||||||
return false;
|
return false;
|
||||||
auto p = r.Indirect();
|
auto p = r.Indirect();
|
||||||
|
if (!VerifyAlignment(p, r.byte_width_))
|
||||||
|
return false;
|
||||||
switch (r.type_) {
|
switch (r.type_) {
|
||||||
case FBT_INDIRECT_INT:
|
case FBT_INDIRECT_INT:
|
||||||
case FBT_INDIRECT_UINT:
|
case FBT_INDIRECT_UINT:
|
||||||
@@ -1862,6 +1871,7 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
|
|||||||
const size_t max_depth_;
|
const size_t max_depth_;
|
||||||
size_t num_vectors_;
|
size_t num_vectors_;
|
||||||
const size_t max_vectors_;
|
const size_t max_vectors_;
|
||||||
|
bool check_alignment_;
|
||||||
std::vector<uint8_t> *reuse_tracker_;
|
std::vector<uint8_t> *reuse_tracker_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user