Skip to content

Commit 830ec1e

Browse files
committed
Fix JPEG XR metadata count overflow in ReadBinaryData (GHSA-gwj3-px9g-x6gf)
ReadBinaryData() sized its buffer as uCount + 2 in 32-bit arithmetic, so a file-supplied count of 0xFFFFFFFE or 0xFFFFFFFF wrapped to a 0 or 1 byte allocation while the stream was still asked for the full count. The memory read callback then copied the remaining input into that buffer (heap buffer overflow through FreeImage_LoadFromMemory). Widen the count to size_t before adding the terminator room and reject counts that would overflow size_t on 32-bit targets. Add JxrMetadataCountTest with the advisory's input and count variants. Under AddressSanitizer it reports a 39-byte heap write without the fix and passes with it.
1 parent b92f945 commit 830ec1e

3 files changed

Lines changed: 85 additions & 1 deletion

File tree

‎Source/LibJXR/jxrgluelib/JXRMeta.c‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,8 +673,17 @@ ERR ReadBinaryData(__in_ecount(1) struct WMPStream* pWS,
673673
{
674674
ERR err = WMP_errSuccess;
675675
U8 *pbData = NULL;
676+
size_t cbData = 0;
676677

677-
Call(PKAlloc((void **) &pbData, uCount + 2)); // Allocate buffer to store data with space for an added ascii or unicode null
678+
// uCount comes straight from the file. Widen it before adding the room for
679+
// the terminating null: in U32 arithmetic uCount + 2 wrapped for counts of
680+
// 0xFFFFFFFE and above, so a tiny buffer was allocated while the Read below
681+
// still used the full uCount (heap overflow). The check also covers targets
682+
// where size_t is 32 bits wide.
683+
FailIf((size_t) uCount > ((size_t) -1) - 2, WMP_errBufferOverflow);
684+
cbData = (size_t) uCount + 2;
685+
686+
Call(PKAlloc((void **) &pbData, cbData)); // Allocate buffer to store data with space for an added ascii or unicode null
678687
if (uCount <= 4)
679688
{
680689
unsigned int i;

‎TestAPI/CMakeLists.txt‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,19 @@ if(BUILD_TESTS AND BUILD_LIBOPENJPEG)
192192
set_tests_properties(J2KCallbacksTest PROPERTIES TIMEOUT 30)
193193
endif()
194194

195+
if(BUILD_TESTS AND BUILD_JXR)
196+
add_executable(JxrMetadataCountTest testJxrMetadataCount.cpp)
197+
target_link_libraries(JxrMetadataCountTest PRIVATE FreeImage)
198+
if(WIN32 AND NOT FREEIMAGE_STATIC)
199+
add_custom_command(TARGET JxrMetadataCountTest POST_BUILD
200+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
201+
$<TARGET_FILE:FreeImage> $<TARGET_FILE_DIR:JxrMetadataCountTest>
202+
VERBATIM)
203+
endif()
204+
add_test(NAME JxrMetadataCountTest COMMAND JxrMetadataCountTest)
205+
set_tests_properties(JxrMetadataCountTest PROPERTIES TIMEOUT 30)
206+
endif()
207+
195208
if(BUILD_TESTS AND FREEIMAGE_STATIC AND FreeImage_ZLIB_SRCS)
196209
add_executable(ZLibTest testZLib.cpp)
197210
target_link_libraries(ZLibTest PRIVATE FreeImage)

‎TestAPI/testJxrMetadataCount.cpp‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// JPEG XR metadata count regression (GHSA-gwj3-px9g-x6gf).
2+
//
3+
// ReadBinaryData() computed the buffer size as (U32)(count + 2), so a
4+
// CameraMake entry whose file-supplied count was 0xFFFFFFFE or 0xFFFFFFFF
5+
// wrapped to a 0 or 1 byte allocation while the stream was still asked for
6+
// the full count. The memory read callback then copied the remaining input
7+
// into that buffer. Run under AddressSanitizer to see the overflow; the
8+
// checks below also verify that loading fails or succeeds cleanly.
9+
#include "FreeImage.h"
10+
11+
#include <cstdio>
12+
#include <cstdlib>
13+
#include <cstring>
14+
15+
static const BYTE kPoc[119] = {
16+
0x49, 0x49, 0xbc, 0x01, 0x1e, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x50, 0x00, 0x0c, 0xff,
17+
0x8d, 0x9c, 0x8d, 0x79, 0x03, 0x8d, 0x8d, 0x01, 0x00, 0x53, 0x00, 0x00, 0x80, 0x7f, 0x04, 0x00,
18+
0x81, 0xbc, 0x01, 0x1e, 0x8d, 0x8d, 0x01, 0x00, 0x53, 0x00, 0x00, 0x00, 0x01, 0x8d, 0x01, 0x00,
19+
0x02, 0xf0, 0xfe, 0x02, 0x12, 0x00, 0x00, 0x00, 0x0f, 0x01, 0x02, 0x00, 0xff, 0xff, 0xff, 0xff,
20+
0x50, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x11, 0x03, 0x8d, 0x8d, 0x57, 0x50, 0x20, 0x22, 0x00, 0x10,
21+
0x7f, 0x80, 0x00, 0x00, 0xf5, 0x00, 0x0a, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
22+
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
23+
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41
24+
};
25+
26+
// Offset of the CameraMake (tag 0x010F, type ASCII) entry's 32-bit count.
27+
static const size_t kCountOffset = 60;
28+
29+
static void check(bool ok, const char *message) {
30+
if (!ok) {
31+
std::fprintf(stderr, "JXR metadata count test failed: %s\n", message);
32+
std::exit(1);
33+
}
34+
}
35+
36+
static void loadWithCount(unsigned count) {
37+
BYTE data[sizeof(kPoc)];
38+
std::memcpy(data, kPoc, sizeof(data));
39+
for (unsigned i = 0; i < 4; ++i)
40+
data[kCountOffset + i] = static_cast<BYTE>(count >> (8 * i));
41+
42+
FIMEMORY *memory = FreeImage_OpenMemory(data, sizeof(data));
43+
check(memory != NULL, "could not open memory stream");
44+
check(FreeImage_GetFileTypeFromMemory(memory, 0) == FIF_JXR, "input is not detected as JPEG XR");
45+
FreeImage_SeekMemory(memory, 0, SEEK_SET);
46+
FIBITMAP *bitmap = FreeImage_LoadFromMemory(FIF_JXR, memory, 0);
47+
FreeImage_Unload(bitmap);
48+
FreeImage_CloseMemory(memory);
49+
}
50+
51+
int main() {
52+
FreeImage_Initialise(FALSE);
53+
check(std::memcmp(kPoc + kCountOffset - 4, "\x0f\x01\x02\x00", 4) == 0, "count offset is stale");
54+
check(std::memcmp(kPoc + kCountOffset, "\xff\xff\xff\xff", 4) == 0, "count is not 0xFFFFFFFF");
55+
loadWithCount(0xFFFFFFFFu);
56+
loadWithCount(0xFFFFFFFEu);
57+
loadWithCount(0xFFFFFFFDu);
58+
loadWithCount(64u);
59+
FreeImage_DeInitialise();
60+
std::puts("JXR metadata count tests passed");
61+
return 0;
62+
}

0 commit comments

Comments
 (0)