QBinaryJson: prevent OOB read in fromBinaryData()


The fromBinaryData() function computes the document bound in
size_t, but then truncates it to uint. As a result, when
provided a malformed binary json with root.size in the range of
[0xFFFFFFF8, 0xFFFFFFFF], the total size overflows to the values
between 0 and 7, and the size check incorrectly passes.
When combined with a malicious tableOffset, this results in an
out-of-bounds read in the range of up to 4 GiB past the end of
the allocated buffer - depending on the memory layout it can
lead to either a crash or a read of some potentially sensitive
data.

Do the calculations in uint and use qAddOverflow() to detect
overflows. Since the underlying data structure uses uint to
store the size of the allocated data, we can simply return
early if we detect an overflow.

The newly-added test creates a larger buffer and copies a correct
binary json blob there. Then it injects the malformed size of the
payload and a new offset of the data table, together with the new
table. Then the test uses this large buffer to demonstrate an
out-of-bounds read on a modified blob. If the data was not backed
by a large buffer, it would have been an out-of-bounds read.

The fromRawData() method is not affected (assuming that the passed
size is correct), because it uses the input size parameter
directly, without analyzing the internal structure of the binary
json blob.

Pick-to: 6.12 6.11 6.8 6.5
Fixes: QTBUG-149036
Change-Id: I1a81deebb86cd7251ebeef9eea789b692f9d94b9
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
commitc0a495894d83a3c48055bd1e544f93147fc6ade1
parenta2d9a68abbc0795439db3612bb7ed23b7eadfb17
authorIvan Solovev <ivan.solovev@qt.io>
committerMarc Mutz <marc.mutz@qt.io>
commit timeTue, 18 Aug 2026 07:36:03 +0000