Repository navigation
Harden inline readLength() array decode sites against declared-length OOM (follow-up to #7733) #7776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Harden inline readLength() array decode sites against declared-length OOM (follow-up to #7733) #7776
Changes from 1 commit
d5155ee
e84a198
9cd64ec
c8fbd5b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ | |
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.math.BigInteger; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
@@ -189,4 +191,81 @@ public void testArrayDeclaredLengthNotAtBufferEnd() { | |
| assertThrows(JSONException.class, | ||
| () -> JSONB.parseObject(NESTED_ARRAY_BAD_LEN, int[][].class)); | ||
| } | ||
|
|
||
| // --- R2-1: the same declared-length pre-allocation OOM class, but through the | ||
| // untyped/nested BC_ARRAY decode paths that never call startArray(): | ||
| // readAny() (default branch), readObject() (map-value branch), readArray() | ||
| // (element branch). They read the length via readLength() and pre-allocate | ||
| // new JSONArray(len)/new ArrayList(len) with no remaining-buffer check. | ||
| // This change folds the same checkLength guard into those three inline sites. | ||
|
|
||
| // 0xA4 = BC_ARRAY, 0x48 = BC_INT32, 0x0FFFFFFF = 268,435,455 elements | ||
| static final byte[] UNTYPED_ARRAY_MAX_LEN = { | ||
| (byte) 0xA4, | ||
| (byte) 0x48, | ||
| (byte) 0x0F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF | ||
| }; | ||
|
|
||
| /** | ||
| * Corrupt the first BC_ARRAY (0xA4) + BC_INT32 (0x48) length field found in | ||
| * the payload to a huge value, so the declared length vastly exceeds the | ||
| * remaining buffer. Used to exercise the untyped/nested readLength() sites. | ||
| */ | ||
| private static void corruptFirstArrayLength(byte[] bytes) { | ||
| for (int i = 0; i + 5 < bytes.length; i++) { | ||
| if ((bytes[i] & 0xFF) == 0xA4) { // BC_ARRAY | ||
| // Rewrite the array's leading byte as BC_INT32 (0x48) so the inline | ||
| // readLength() site treats it as a 4-byte length-type, then overwrite | ||
| // those 4 bytes with 0x0FFFFFFF (268,435,455) — far larger than the | ||
| // remaining buffer — to exercise the checkLength guard placed at the | ||
| // inline readLength() decode site (readObject() map-value branch). | ||
| // The corrupt target is the inner array value, not the (BC_OBJECT) | ||
| // root, because the root is never encoded as BC_ARRAY. | ||
| bytes[i + 1] = (byte) 0x48; // BC_INT32 | ||
| bytes[i + 2] = (byte) 0x0F; | ||
| bytes[i + 3] = (byte) 0xFF; | ||
| bytes[i + 4] = (byte) 0xFF; | ||
| bytes[i + 5] = (byte) 0xFF; | ||
| return; | ||
| } | ||
| } | ||
| throw new IllegalStateException("BC_ARRAY not found in payload"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUntypedArrayDeclaredLengthOOM_readAny() { | ||
| // JSONB.parse with no target type dispatches to readAny(); its BC_ARRAY | ||
| // branch reads the length via readLength() and pre-allocates | ||
| // new JSONArray(len) — now guarded by checkLength. | ||
| assertThrows(JSONException.class, () -> JSONB.parse(UNTYPED_ARRAY_MAX_LEN)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] The type-only Fix — pin the guard's own message, which no downstream failure produces (same change for the JSONException ex = assertThrows(JSONException.class, () -> JSONB.parse(UNTYPED_ARRAY_MAX_LEN));
assertTrue(ex.getMessage().contains("array length out of range"));— qwen3.8-max via Qwen Code /review (v0.21.10) |
||
| } | ||
|
|
||
| @Test | ||
| public void testUntypedArrayDeclaredLengthOOM_readObject() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] This test never exercises the Fix — route through assertThrows(JSONException.class, () -> JSONB.parseObject(bytes));(keep the — qwen3.8-max via Qwen Code /review (v0.21.10) |
||
| // A map whose value is an int[] (32 elems -> BC_ARRAY + BC_INT32 length). | ||
| // Corrupt the declared length, then parse as Object.class so readObject() | ||
| // takes the map-value BC_ARRAY branch (readLength). | ||
| Map<String, Object> m = new HashMap<>(); | ||
| m.put("k", new int[32]); | ||
| byte[] bytes = JSONB.toBytes(m); | ||
| corruptFirstArrayLength(bytes); | ||
| assertThrows(JSONException.class, () -> JSONB.parseObject(bytes, Object.class)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUntypedArrayDeclaredLengthOOM_readArray() { | ||
| // Top-level JSONB.parseArray dispatches to readArray(); a nested element | ||
| // that is itself a BC_ARRAY must have its declared length (read via the | ||
| // inline readLength() at the element branch, line ~1099) rejected by | ||
| // checkLength. | ||
| // 0xA4 = BC_ARRAY (outer, 1 element), 0x01 = outer declared length, | ||
| // 0xA4 = BC_ARRAY (inner element), 0x48 = BC_INT32 length-type, | ||
| // 0x0FFFFFFF = declares 268,435,455 inner elements (exceeds buffer). | ||
| byte[] bytes = { | ||
| (byte) 0xA4, 0x01, | ||
| (byte) 0xA4, (byte) 0x48, | ||
| (byte) 0x0F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF | ||
| }; | ||
| assertThrows(JSONException.class, () -> JSONB.parseArray(bytes)); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Suggestion] The array-length guard is spelled two ways in this PR: the
checkArrayLenwrapper (body exactlycheckLength(len, offset, end, "array length")plus the 1-byte-per-element invariant comment) is used only at the twostartArray()sites, while the three inline sites this PR adds (here, ~932 inreadAny(), ~1105 inreadArray()) bypass the wrapper and callcheckLengthdirectly with the identical label.startArray()measures 136 bytes at target 8 either way — the dual spelling buys no bytecode-size benefit. — Failure scenario: a future change to the array-length policy or message (e.g. an absolute cap likereadLength()'s 256M limit) must find two spellings to stay consistent, and the invariant rationale comment is trapped in a wrapper three of the five array sites never route through — the same drift this PR exists to eliminate forcheckBigintLen.(same change at the other two inline sites, ~932 and ~1105 — or delete the wrapper and keep five direct calls)
— qwen3.8-max via Qwen Code /review (v0.21.10)