Source: ujson Version: 5.10.0-1 Severity: important Tags: patch security X-Debbugs-Cc: [email protected], [email protected]
Hi, I prepared one trixie update for CVE-2026-32874 and CVE-2026-32875. Commit 4baeb950df780092bd3c89fc702a868e99a3a1d2 frees the integer parse buffer. Commit 486bd4553dc471a1de11613bc7347a6b318e37ea bounds indentation arithmetic. The tested trixie debdiff is attached. The earlier BTS reports #1131486 and #1131485 are archived. Both builds passed package tests: 291 pytest tests on the base and 295 on the combined patched source. Lintian output was empty on both. Hopefully it helps for the next trixie point release! Happy to adjust anything Dickson Prepared with AI assistance (OpenAI Codex) and reviewed by me before sending.
diff -Nru '--exclude=.pc' inspection/debian/changelog packaged/debian/changelog --- inspection/debian/changelog 2024-05-19 02:27:04.000000000 +0000 +++ packaged/debian/changelog 2026-11-26 10:43:29.698676605 +0000 @@ -1,3 +1,10 @@ +ujson (5.10.0-1+deb13u1) trixie; urgency=medium + + * Backport upstream fix for CVE-2026-32874 (free the parsing buffer). + * Backport upstream fix for CVE-2026-32875 (bound indentation values). + + -- Dickson Wu <[email protected]> Thu, 26 Nov 2026 01:30:00 +0000 + ujson (5.10.0-1) unstable; urgency=medium [ Alexandre Detiste ] diff -Nru '--exclude=.pc' inspection/debian/patches/CVE-2026-32874.patch packaged/debian/patches/CVE-2026-32874.patch --- inspection/debian/patches/CVE-2026-32874.patch 1970-01-01 00:00:00.000000000 +0000 +++ packaged/debian/patches/CVE-2026-32874.patch 2026-11-26 10:43:29.680676398 +0000 @@ -0,0 +1,46 @@ +Description: Free the temporary buffer after parsing large integers +Origin: upstream, https://github.com/ultrajson/ultrajson/commit/4baeb950df780092bd3c89fc702a868e99a3a1d2 +Bug-Debian: https://bugs.debian.org/1131486 +Applied-Upstream: 4baeb950df780092bd3c89fc702a868e99a3a1d2 +Last-Update: 2026-11-26 +--- +diff --git a/python/JSONtoObj.c b/python/JSONtoObj.c +index 92545ce5..c72faff1 100644 +--- a/python/JSONtoObj.c ++++ b/python/JSONtoObj.c +@@ -145,7 +145,9 @@ static JSOBJ Object_newIntegerFromString(void *prv, char *value, size_t length) + char *buf = PyObject_Malloc(length + 1); + memcpy(buf, value, length); + buf[length] = '\0'; +- return PyLong_FromString(buf, NULL, 10); ++ PyObject *ret = PyLong_FromString(buf, NULL, 10); ++ PyObject_Free(buf); ++ return ret; + } + + static JSOBJ Object_newDouble(void *prv, double value) +diff --git a/tests/test_ujson.py b/tests/test_ujson.py +index 17cc0d75..6aa101e3 100644 +--- a/tests/test_ujson.py ++++ b/tests/test_ujson.py +@@ -656,6 +656,20 @@ def test_encode_decode_big_int(i, mode): + assert ujson.decode(json_string) == python_object + + [email protected]( ++ sys.implementation.name == "pypy", ++ reason="PyPy's PyNumber_ToBase ignores sys.get_int_max_str_digits()", ++) ++def test_encode_too_big_int_error(): ++ with pytest.raises(ValueError, match="integer string conversion"): ++ ujson.dumps(pow(10, 10_000)) ++ ++ ++def test_decode_too_big_int_error(): ++ with pytest.raises(ValueError, match="integer string conversion"): ++ ujson.loads("9" * 10_000) ++ ++ + @pytest.mark.parametrize( + "test_input, expected", + [ diff -Nru '--exclude=.pc' inspection/debian/patches/CVE-2026-32875.patch packaged/debian/patches/CVE-2026-32875.patch --- inspection/debian/patches/CVE-2026-32875.patch 1970-01-01 00:00:00.000000000 +0000 +++ packaged/debian/patches/CVE-2026-32875.patch 2026-11-26 10:43:29.693676548 +0000 @@ -0,0 +1,192 @@ +Description: Bound indentation arithmetic and preserve negative-indent behavior +Origin: upstream, https://github.com/ultrajson/ultrajson/commit/486bd4553dc471a1de11613bc7347a6b318e37ea +Bug-Debian: https://bugs.debian.org/1131485 +Applied-Upstream: 486bd4553dc471a1de11613bc7347a6b318e37ea +Last-Update: 2026-11-26 +--- +From 486bd4553dc471a1de11613bc7347a6b318e37ea Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Br=C3=A9nainn=20Woodsend?= <[email protected]> +Date: Wed, 4 Mar 2026 22:28:11 +0000 +Subject: [PATCH] Fix buffer overflow/infinite loop from indent handling + +If indent * nest depth is large enough to overflow an int, it causes the +required output buffer size to be underestimated leading to a buffer +overflow. + +The offending arithmetic is upgraded to a ptrdiff_t and indent is +artificially capped. An overflow is still technically possible given a +high enough recursion depth but not without first consuming petabytes of +RAM. + +If indent is negative, it causes a size_t to underflow to some number a +little bellow size_t max. If that underflow isn't accidentally rectified +by a subsequent overflow (if -indent * (nest_depth + 1) > +current_buffer_size) then the buffer up-sizer gets stuck in an infinite +loop trying to find a power of two that fits in a size_t but is greater +that size_t max / 2. + +It's hard to tell if `ujson.dumps(..., indent=-1)` was ever an +intentional feature but I don't feel comfortable breaking it in a +security fix. For now, the dubious *any negative indent -> pad colons +but add no indentation or newlines* behaviour is preserved but +internally the indent is clipped to -1 and all subsequent indentation +code paths are skipped over. +--- + lib/ultrajson.h | 3 ++- + lib/ultrajsonenc.c | 16 +++++++++------- + python/objToJSON.c | 16 +++++++++++++++- + tests/test_ujson.py | 20 ++++++++++++++++++-- + 4 files changed, 44 insertions(+), 11 deletions(-) + +diff --git a/lib/ultrajson.h b/lib/ultrajson.h +index 143cd9e6..4560f4dc 100644 +--- a/lib/ultrajson.h ++++ b/lib/ultrajson.h +@@ -54,6 +54,7 @@ tree doesn't have cyclic references. + #define __ULTRAJSON_H__ + + #include <stdio.h> ++#include <stddef.h> + + // Max decimals to encode double floating point numbers with + #ifndef JSON_DOUBLE_MAX_DECIMALS +@@ -257,7 +258,7 @@ typedef struct __JSONObjectEncoder + + /* + Configuration for spaces of indent */ +- int indent; ++ ptrdiff_t indent; + + /* + If true, NaN will be encoded as a string matching the Python standard library's JSON behavior. +diff --git a/lib/ultrajsonenc.c b/lib/ultrajsonenc.c +index 9ec2fafe..0f9fde3f 100644 +--- a/lib/ultrajsonenc.c ++++ b/lib/ultrajsonenc.c +@@ -575,7 +575,7 @@ static void Buffer_AppendIndentNewlineUnchecked(JSONObjectEncoder *enc) + + static void Buffer_AppendIndentUnchecked(JSONObjectEncoder *enc, JSINT32 value) + { +- int i; ++ ptrdiff_t i; + if (enc->indent > 0) + while (value-- > 0) + for (i = 0; i < enc->indent; i++) +@@ -741,10 +741,11 @@ static void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t c + + Buffer_AppendCharUnchecked (enc, '['); + ++ // The extra 1 byte covers the optional newline. ++ size_t per_item_reserve = (enc->indent > 0 ? enc->indent : 0) * (enc->level + 1) + enc->itemSeparatorLength + 1; + while (enc->iterNext(obj, &tc)) + { +- // The extra 1 byte covers the optional newline. +- Buffer_Reserve (enc, enc->indent * (enc->level + 1) + enc->itemSeparatorLength + 1); ++ Buffer_Reserve (enc, per_item_reserve); + + if (count > 0) + { +@@ -769,7 +770,7 @@ static void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t c + + enc->iterEnd(obj, &tc); + +- if (count > 0) { ++ if (count > 0 && enc->indent > 0) { + // Reserve space for the indentation plus the newline. + Buffer_Reserve (enc, enc->indent * enc->level + 1); + Buffer_AppendIndentNewlineUnchecked (enc); +@@ -786,10 +787,11 @@ static void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t c + + Buffer_AppendCharUnchecked (enc, '{'); + ++ // The extra 1 byte covers the optional newline. ++ size_t reserve_size = (enc->indent > 0 ? enc->indent : 0) * (enc->level + 1) + enc->itemSeparatorLength + 1; + while ((res = enc->iterNext(obj, &tc))) + { +- // The extra 1 byte covers the optional newline. +- Buffer_Reserve (enc, enc->indent * (enc->level + 1) + enc->itemSeparatorLength + 1); ++ Buffer_Reserve (enc, reserve_size); + + if(res < 0) + { +@@ -823,7 +825,7 @@ static void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t c + + enc->iterEnd(obj, &tc); + +- if (count > 0) { ++ if (count > 0 && enc->indent > 0) { + Buffer_Reserve (enc, enc->indent * enc->level + 1); + Buffer_AppendIndentNewlineUnchecked (enc); + Buffer_AppendIndentUnchecked (enc, enc->level); +diff --git a/python/objToJSON.c b/python/objToJSON.c +index b754819e..90132059 100644 +--- a/python/objToJSON.c ++++ b/python/objToJSON.c +@@ -678,6 +678,7 @@ PyObject* objToJSON(PyObject* self, PyObject *args, PyObject *kwargs) + PyObject *separatorsKeyBytes = NULL; + int allowNan = -1; + int orejectBytes = -1; ++ int indent = 0; + size_t retLen; + + JSONObjectEncoder encoder = +@@ -714,7 +715,7 @@ PyObject* objToJSON(PyObject* self, PyObject *args, PyObject *kwargs) + + PRINTMARK(); + +- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OOOOiiiOO", kwlist, &oinput, &oensureAscii, &oencodeHTMLChars, &oescapeForwardSlashes, &osortKeys, &encoder.indent, &allowNan, &orejectBytes, &odefaultFn, &oseparators)) ++ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OOOOiiiOO", kwlist, &oinput, &oensureAscii, &oencodeHTMLChars, &oescapeForwardSlashes, &osortKeys, &indent, &allowNan, &orejectBytes, &odefaultFn, &oseparators)) + { + return NULL; + } +@@ -761,6 +762,19 @@ PyObject* objToJSON(PyObject* self, PyObject *args, PyObject *kwargs) + encoder.rejectBytes = orejectBytes; + } + ++ if (indent < -1) ++ { ++ encoder.indent = -1; ++ } ++ else if (indent > 1000) ++ { ++ PyErr_SetString(PyExc_ValueError, "Maximum allowed indentation is 1000"); ++ return NULL; ++ } ++ else { ++ encoder.indent = indent; ++ } ++ + if (oseparators != NULL && oseparators != Py_None) + { + if (!PyTuple_Check(oseparators)) +diff --git a/tests/test_ujson.py b/tests/test_ujson.py +--- a/tests/test_ujson.py ++++ b/tests/test_ujson.py +@@ -1064,9 +1064,25 @@ + ujson.dumps(unjsonable_obj, default=default) + + [email protected]("indent", list(range(65537, 65542))) [email protected]("indent", [999, 1000, 1001, 1 << 30, 1 << 63, 1 << 128]) + def test_dump_huge_indent(indent): +- ujson.encode({"a": True}, indent=indent) ++ obj = {"list": [1, [2, 3], 4], "nested": {"key": "value", "a": True}} ++ if indent <= 1000: ++ assert ujson.loads(ujson.encode(obj, indent=indent)) == obj ++ else: ++ with pytest.raises((ValueError, OverflowError)): ++ ujson.encode(obj, indent=indent) ++ ++ ++def test_negative_indent(): ++ obj = {"a": [1, 2], "b": "c"} ++ assert ujson.dumps(obj) == '{"a":[1,2],"b":"c"}' ++ assert ujson.dumps(obj, 0) == '{"a":[1,2],"b":"c"}' ++ assert ujson.dumps(obj, indent=-1) == '{"a": [1,2],"b": "c"}' ++ assert ujson.dumps(obj, indent=-1000000) == '{"a": [1,2],"b": "c"}' ++ assert ( ++ ujson.dumps(obj, indent=2) == '{\n "a": [\n 1,\n 2\n ],\n "b": "c"\n}' ++ ) + + + @pytest.mark.parametrize("first_length", list(range(2, 7))) diff -Nru '--exclude=.pc' inspection/debian/patches/series packaged/debian/patches/series --- inspection/debian/patches/series 2023-06-17 22:48:08.000000000 +0000 +++ packaged/debian/patches/series 2026-11-26 10:43:29.693676548 +0000 @@ -1,2 +1,4 @@ #0001-dont-remove-build-dir.patch #0002-run-test-more-verbosely.patch +CVE-2026-32874.patch +CVE-2026-32875.patch diff -Nru '--exclude=.pc' inspection/lib/ultrajson.h packaged/lib/ultrajson.h --- inspection/lib/ultrajson.h 2024-05-14 02:00:06.000000000 +0000 +++ packaged/lib/ultrajson.h 2026-11-26 10:43:29.685676455 +0000 @@ -54,6 +54,7 @@ #define __ULTRAJSON_H__ #include <stdio.h> +#include <stddef.h> // Max decimals to encode double floating point numbers with #ifndef JSON_DOUBLE_MAX_DECIMALS @@ -257,7 +258,7 @@ /* Configuration for spaces of indent */ - int indent; + ptrdiff_t indent; /* If true, NaN will be encoded as a string matching the Python standard library's JSON behavior. diff -Nru '--exclude=.pc' inspection/lib/ultrajsonenc.c packaged/lib/ultrajsonenc.c --- inspection/lib/ultrajsonenc.c 2024-05-14 02:00:06.000000000 +0000 +++ packaged/lib/ultrajsonenc.c 2026-11-26 10:43:29.685676455 +0000 @@ -575,7 +575,7 @@ static void Buffer_AppendIndentUnchecked(JSONObjectEncoder *enc, JSINT32 value) { - int i; + ptrdiff_t i; if (enc->indent > 0) while (value-- > 0) for (i = 0; i < enc->indent; i++) @@ -741,10 +741,11 @@ Buffer_AppendCharUnchecked (enc, '['); + // The extra 1 byte covers the optional newline. + size_t per_item_reserve = (enc->indent > 0 ? enc->indent : 0) * (enc->level + 1) + enc->itemSeparatorLength + 1; while (enc->iterNext(obj, &tc)) { - // The extra 1 byte covers the optional newline. - Buffer_Reserve (enc, enc->indent * (enc->level + 1) + enc->itemSeparatorLength + 1); + Buffer_Reserve (enc, per_item_reserve); if (count > 0) { @@ -769,7 +770,7 @@ enc->iterEnd(obj, &tc); - if (count > 0) { + if (count > 0 && enc->indent > 0) { // Reserve space for the indentation plus the newline. Buffer_Reserve (enc, enc->indent * enc->level + 1); Buffer_AppendIndentNewlineUnchecked (enc); @@ -786,10 +787,11 @@ Buffer_AppendCharUnchecked (enc, '{'); + // The extra 1 byte covers the optional newline. + size_t reserve_size = (enc->indent > 0 ? enc->indent : 0) * (enc->level + 1) + enc->itemSeparatorLength + 1; while ((res = enc->iterNext(obj, &tc))) { - // The extra 1 byte covers the optional newline. - Buffer_Reserve (enc, enc->indent * (enc->level + 1) + enc->itemSeparatorLength + 1); + Buffer_Reserve (enc, reserve_size); if(res < 0) { @@ -823,7 +825,7 @@ enc->iterEnd(obj, &tc); - if (count > 0) { + if (count > 0 && enc->indent > 0) { Buffer_Reserve (enc, enc->indent * enc->level + 1); Buffer_AppendIndentNewlineUnchecked (enc); Buffer_AppendIndentUnchecked (enc, enc->level); diff -Nru '--exclude=.pc' inspection/python/JSONtoObj.c packaged/python/JSONtoObj.c --- inspection/python/JSONtoObj.c 2024-05-14 02:00:06.000000000 +0000 +++ packaged/python/JSONtoObj.c 2026-11-26 10:43:29.678676374 +0000 @@ -136,7 +136,9 @@ char *buf = PyObject_Malloc(length + 1); memcpy(buf, value, length); buf[length] = '\0'; - return PyLong_FromString(buf, NULL, 10); + PyObject *ret = PyLong_FromString(buf, NULL, 10); + PyObject_Free(buf); + return ret; } static JSOBJ Object_newDouble(void *prv, double value) diff -Nru '--exclude=.pc' inspection/python/objToJSON.c packaged/python/objToJSON.c --- inspection/python/objToJSON.c 2024-05-14 02:00:06.000000000 +0000 +++ packaged/python/objToJSON.c 2026-11-26 10:43:29.686676467 +0000 @@ -678,6 +678,7 @@ PyObject *separatorsKeyBytes = NULL; int allowNan = -1; int orejectBytes = -1; + int indent = 0; size_t retLen; JSONObjectEncoder encoder = @@ -714,7 +715,7 @@ PRINTMARK(); - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OOOOiiiOO", kwlist, &oinput, &oensureAscii, &oencodeHTMLChars, &oescapeForwardSlashes, &osortKeys, &encoder.indent, &allowNan, &orejectBytes, &odefaultFn, &oseparators)) + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OOOOiiiOO", kwlist, &oinput, &oensureAscii, &oencodeHTMLChars, &oescapeForwardSlashes, &osortKeys, &indent, &allowNan, &orejectBytes, &odefaultFn, &oseparators)) { return NULL; } @@ -761,6 +762,19 @@ encoder.rejectBytes = orejectBytes; } + if (indent < -1) + { + encoder.indent = -1; + } + else if (indent > 1000) + { + PyErr_SetString(PyExc_ValueError, "Maximum allowed indentation is 1000"); + return NULL; + } + else { + encoder.indent = indent; + } + if (oseparators != NULL && oseparators != Py_None) { if (!PyTuple_Check(oseparators)) diff -Nru '--exclude=.pc' inspection/tests/test_ujson.py packaged/tests/test_ujson.py --- inspection/tests/test_ujson.py 2024-05-14 02:00:06.000000000 +0000 +++ packaged/tests/test_ujson.py 2026-11-26 10:43:29.686676467 +0000 @@ -653,6 +653,20 @@ assert ujson.decode(json_string) == python_object [email protected]( + sys.implementation.name == "pypy", + reason="PyPy's PyNumber_ToBase ignores sys.get_int_max_str_digits()", +) +def test_encode_too_big_int_error(): + with pytest.raises(ValueError, match="integer string conversion"): + ujson.dumps(pow(10, 10_000)) + + +def test_decode_too_big_int_error(): + with pytest.raises(ValueError, match="integer string conversion"): + ujson.loads("9" * 10_000) + + @pytest.mark.parametrize( "test_input, expected", [ @@ -1050,9 +1064,25 @@ ujson.dumps(unjsonable_obj, default=default) [email protected]("indent", list(range(65537, 65542))) [email protected]("indent", [999, 1000, 1001, 1 << 30, 1 << 63, 1 << 128]) def test_dump_huge_indent(indent): - ujson.encode({"a": True}, indent=indent) + obj = {"list": [1, [2, 3], 4], "nested": {"key": "value", "a": True}} + if indent <= 1000: + assert ujson.loads(ujson.encode(obj, indent=indent)) == obj + else: + with pytest.raises((ValueError, OverflowError)): + ujson.encode(obj, indent=indent) + + +def test_negative_indent(): + obj = {"a": [1, 2], "b": "c"} + assert ujson.dumps(obj) == '{"a":[1,2],"b":"c"}' + assert ujson.dumps(obj, 0) == '{"a":[1,2],"b":"c"}' + assert ujson.dumps(obj, indent=-1) == '{"a": [1,2],"b": "c"}' + assert ujson.dumps(obj, indent=-1000000) == '{"a": [1,2],"b": "c"}' + assert ( + ujson.dumps(obj, indent=2) == '{\n "a": [\n 1,\n 2\n ],\n "b": "c"\n}' + ) @pytest.mark.parametrize("first_length", list(range(2, 7)))

