Skip to content

Commit 3ec2cf1

Browse files
pitrouraulcd
authored andcommitted
GH-54936: [C++] Ensure reading an invalid map array from IPC doesn't crash (#54939)
### Rationale for this change When loading a MapArray from IPC, we call [`MapArray::ValidateChildData`](https://github.com/apache/arrow/blob/b9479757dd5c1914c2b007f11f6679c4fc080c05/cpp/src/arrow/ipc/reader.cc#L456-L459) to avoid a later crash in `MapArray::SetData`. Unfortunately `MapArray::ValidateChildData` calls `GetNullCount()` which is unsafe if the advertised length is too large for the actual validity buffer size. Found by OSS-Fuzz in https://issues.oss-fuzz.com/issues/556994537 ### What changes are included in this PR? Check that the `null_count` of the relevant fields is equal to zero, so that `GetNullCount` does not actually try to read buffer memory. ### Are these changes tested? Yes, by additional fuzz regression test. ### Are there any user-facing changes? Just a bugfix. **This PR contains a "Critical Fix".** An invalid IPC file would lead to reading memory out of bounds for the purpose of counting validity bits. It is not obvious how this could be exploited by an attacker except for causing a denial of service. ### Was AI used for this PR? **PR code and description written by:** - [x] Human - [ ] AI **Reviewed before submission by:** - [x] Human - [ ] AI - [ ] Not reviewed * GitHub Issue: #54936 Authored-by: Antoine Pitrou <[email protected]> Signed-off-by: Antoine Pitrou <[email protected]>
1 parent f942ba3 commit 3ec2cf1

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

‎cpp/src/arrow/ipc/reader.cc‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <flatbuffers/flatbuffers.h> // IWYU pragma: export
3535

3636
#include "arrow/array.h"
37+
#include "arrow/array/validate.h"
3738
#include "arrow/buffer.h"
3839
#include "arrow/extension_type.h"
3940
#include "arrow/io/caching.h"
@@ -455,6 +456,20 @@ class ArrayLoader {
455456

456457
Status Visit(const MapType& type) {
457458
RETURN_NOT_OK(LoadList(type));
459+
DCHECK_EQ(out_->child_data.size(), 1); // ensured by LoadList()
460+
// Ensure that GetNullCount() calls in MapArray::ValidateChildData
461+
// don't access invalid memory.
462+
// We cannot call array validation as it will fail on non-native endianness
463+
// (due to checking the last offset on variable-width data) or missing
464+
// dictionaries (they are not yet populated at this point).
465+
const ArrayData& child_data = *out_->child_data[0];
466+
if (child_data.null_count != 0) {
467+
return Status::Invalid("Map array child array should have no nulls");
468+
}
469+
if (!child_data.child_data.empty() && child_data.child_data[0]->null_count != 0) {
470+
return Status::Invalid("Map array keys array should have no nulls");
471+
}
472+
// If we don't do this, MapArray::SetData will abort on invalid data.
458473
return MapArray::ValidateChildData(out_->child_data);
459474
}
460475

0 commit comments

Comments
 (0)