Repository navigation
fix: bound jsonb string length to remaining input #7867
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
Changes from 2 commits
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 |
|---|---|---|
|
|
@@ -159,6 +159,7 @@ public String getString() { | |
| if (strlen < 0) { | ||
| return symbolTable.getName(-strlen); | ||
| } | ||
| checkStrLen(strlen, strBegin, end); | ||
|
|
||
| Charset charset; | ||
| if (strtype == BC_STR_ASCII) { | ||
|
|
@@ -604,6 +605,7 @@ public Object readAny() { | |
| } | ||
| case BC_STR_UTF8: { | ||
| int strlen = readLength(); | ||
| checkStrLenCopy(strlen, offset, end); | ||
|
|
||
| if (STRING_CREATOR_JDK11 != null && !JDKUtils.BIG_ENDIAN) { | ||
| if (valueBytes == null) { | ||
|
|
@@ -634,12 +636,14 @@ public Object readAny() { | |
| } | ||
| case BC_STR_UTF16: { | ||
| int strlen = readLength(); | ||
| checkStrLenCopy(strlen, offset, end); | ||
| String str = new String(bytes, offset, strlen, StandardCharsets.UTF_16); | ||
| offset += strlen; | ||
| return str; | ||
| } | ||
| case BC_STR_UTF16LE: { | ||
| int strlen = readLength(); | ||
| checkStrLenCopy(strlen, offset, end); | ||
|
|
||
| String str; | ||
| if (STRING_CREATOR_JDK11 != null && !JDKUtils.BIG_ENDIAN) { | ||
|
|
@@ -655,6 +659,7 @@ public Object readAny() { | |
| } | ||
| case BC_STR_UTF16BE: { | ||
| int strlen = readLength(); | ||
| checkStrLenCopy(strlen, offset, end); | ||
|
|
||
| String str; | ||
| if (STRING_CREATOR_JDK11 != null && JDKUtils.BIG_ENDIAN) { | ||
|
|
@@ -673,6 +678,7 @@ public Object readAny() { | |
| GB18030 = Charset.forName("GB18030"); | ||
| } | ||
| int strlen = readLength(); | ||
| checkStrLenCopy(strlen, offset, end); | ||
| String str = new String(bytes, offset, strlen, GB18030); | ||
| offset += strlen; | ||
| return str; | ||
|
|
@@ -959,6 +965,7 @@ public Object readAny() { | |
| if (strlen < 0) { | ||
| return symbolTable.getName(-strlen); | ||
| } | ||
| checkStrLen(strlen, offset, end); | ||
|
|
||
| if (STRING_CREATOR_JDK8 != null) { | ||
| String str = latin1StringJDK8(bytes, offset, strlen); | ||
|
|
@@ -2622,10 +2629,12 @@ public String readFieldName() { | |
| Charset charset = null; | ||
| String str = null; | ||
| if (strtype == BC_STR_ASCII_FIX_MIN + 1) { | ||
| checkStrLen(1, offset, end); | ||
| str = TypeUtils.toString((char) (bytes[offset] & 0xff)); | ||
| strlen = 1; | ||
| offset++; | ||
| } else if (strtype == BC_STR_ASCII_FIX_MIN + 2) { | ||
| checkStrLen(2, offset, end); | ||
| str = TypeUtils.toString( | ||
| (char) (bytes[offset] & 0xff), | ||
| (char) (bytes[offset + 1] & 0xff) | ||
|
|
@@ -2638,10 +2647,13 @@ public String readFieldName() { | |
| if (strtype == BC_STR_ASCII) { | ||
| strlen = readLength(); | ||
| strBegin = offset; | ||
| checkStrLen(strlen, offset, end); | ||
| } else { | ||
| final int offset = this.offset; | ||
| strlen = strtype - BC_STR_ASCII_FIX_MIN; | ||
| checkStrLen(strlen, offset, end); | ||
|
|
||
| // end may itself exceed bytes.length, so the array bound has to stay as well | ||
| if (offset + strlen > bytes.length) { | ||
| throw new JSONException("illegal jsonb data"); | ||
| } | ||
|
|
@@ -2785,6 +2797,7 @@ public String readFieldName() { | |
| } else if (strtype == BC_STR_UTF8) { | ||
| strlen = readLength(); | ||
| strBegin = offset; | ||
| checkStrLen(strlen, offset, end); | ||
|
|
||
| if (STRING_CREATOR_JDK11 != null && !JDKUtils.BIG_ENDIAN) { | ||
| if (valueBytes == null) { | ||
|
|
@@ -2812,10 +2825,12 @@ public String readFieldName() { | |
| } else if (strtype == BC_STR_UTF16) { | ||
| strlen = readLength(); | ||
| strBegin = offset; | ||
| checkStrLen(strlen, offset, end); | ||
| charset = StandardCharsets.UTF_16; | ||
| } else if (strtype == BC_STR_UTF16LE) { | ||
| strlen = readLength(); | ||
| strBegin = offset; | ||
| checkStrLen(strlen, offset, end); | ||
|
|
||
| if (STRING_CREATOR_JDK11 != null && !JDKUtils.BIG_ENDIAN) { | ||
| byte[] chars = new byte[strlen]; | ||
|
|
@@ -2828,6 +2843,7 @@ public String readFieldName() { | |
| } else if (strtype == BC_STR_UTF16BE) { | ||
| strlen = readLength(); | ||
| strBegin = offset; | ||
| checkStrLen(strlen, offset, end); | ||
|
|
||
| if (STRING_CREATOR_JDK11 != null && JDKUtils.BIG_ENDIAN) { | ||
| byte[] chars = new byte[strlen]; | ||
|
|
@@ -2839,6 +2855,7 @@ public String readFieldName() { | |
| charset = StandardCharsets.UTF_16BE; | ||
| } else if (strtype == BC_STR_GB18030) { | ||
| strlen = readLength(); | ||
| checkStrLen(strlen, offset, end); | ||
|
|
||
| if (GB18030 == null) { | ||
| GB18030 = Charset.forName("GB18030"); | ||
|
|
@@ -2911,6 +2928,7 @@ public String readString() { | |
|
|
||
| String str = null; | ||
| if (strlen >= 0) { | ||
| checkStrLen(strlen, offset, end); | ||
| if (STRING_CREATOR_JDK11 != null) { | ||
| byte[] chars = new byte[strlen]; | ||
| System.arraycopy(bytes, offset, chars, 0, strlen); | ||
|
|
@@ -2977,6 +2995,7 @@ private String readString(Charset charset) { | |
| if (strlen < 0) { | ||
| return symbolTable.getName(-strlen); | ||
| } | ||
| checkStrLen(strlen, offset, end); | ||
|
|
||
| char[] chars = null; | ||
| if (JVM_VERSION == 8 && strtype == BC_STR_UTF8 && strlen < 8192) { | ||
|
|
@@ -3012,6 +3031,7 @@ private String readString(Charset charset) { | |
| private void readGB18030() { | ||
| strlen = readLength(); | ||
| strBegin = offset; | ||
| checkStrLenCopy(strlen, offset, end); | ||
|
|
||
| if (GB18030 == null) { | ||
| GB18030 = Charset.forName("GB18030"); | ||
|
|
@@ -3021,6 +3041,7 @@ private void readGB18030() { | |
| private String readUTF16BE() { | ||
| strlen = readLength(); | ||
| strBegin = offset; | ||
| checkStrLenCopy(strlen, offset, end); | ||
|
|
||
| if (STRING_CREATOR_JDK11 != null && JDKUtils.BIG_ENDIAN) { | ||
| byte[] chars = new byte[strlen]; | ||
|
|
@@ -3054,6 +3075,7 @@ private String readUTF16LE() { | |
| strlen = readLength(); | ||
| } | ||
| strBegin = offset; | ||
| checkStrLenCopy(strlen, offset, end); | ||
|
|
||
| if (strlen == 0) { | ||
| return ""; | ||
|
|
@@ -3089,6 +3111,7 @@ private String readStringUTF8() { | |
| strlen = readLength(); | ||
| } | ||
| strBegin = offset; | ||
| checkStrLenCopy(strlen, offset, end); | ||
|
|
||
| if (STRING_CREATOR_JDK11 != null && !JDKUtils.BIG_ENDIAN) { | ||
| if (valueBytes == null) { | ||
|
|
@@ -6256,6 +6279,35 @@ static void checkBigintLen(int len, int offset, int end) { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Bound a string byte length against the bytes left in the frame. A negative length is a symbol | ||
| * reference only at the sites that resolve one before calling - {@link #getString()}, | ||
| * {@link #readAny()}'s BC_STR_ASCII branch, {@link #readString()}, {@link #readString(Charset)} | ||
| * and {@link #readFieldName()}, which resolves it after its guards - so those pass it through. | ||
|
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. [Critical] R2-1: [fails-closed] The Witness (probe on this PR's build; base = parent blob, arms discriminated): Suggested fix — fail the negative closed while keeping the ordinal path where a table exists: // ASCII arm's shared cache gate (:2741-2742)
if (strlen >= 0 && bytes[offset + strlen - 1] > 0 && nameValue0 != -1) {
// UTF16LE (:2835) / UTF16BE (:2848) / UTF8 (:2802) fast paths
if (strlen >= 0 && STRING_CREATOR_JDK11 != null && !JDKUtils.BIG_ENDIAN) {
// then fail the negative closed at these arms (throw strLenOutOfRange(strlen, end - offset))
// or null-guard the tail resolution — the gates alone still NPE via parseObjectThe fix must not remove the ordinal semantics: a negative ASCII/UTF8 name length is a legitimate symbol reference by wire design ( — glm-5.3-flash via Qwen Code /review (v0.25.1)
Contributor
Author
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. Confirmed all three shapes on c71a11f and fixed them in 2b470c3, keeping the ordinal path wherever a table exists:
The over-length bound and the fixed arm's array bound stay as they were. The Tests: The UTF16BE gate only runs on a big-endian JVM, so it can't be made to fail here. 中文在 c71a11f 上复现了这三种情况,已在 2b470c3 中修复,存在 symbol table 时序号解析路径保持不变:
超长上界检查和定长分支的数组边界保持不变。 测试: 逐项删除验证结果见上方代码块。UTF16BE 的条件只在大端 JVM 上执行,本机无法让其失败。 |
||
| * A site that copies the length straight out of the backing array must use | ||
| * {@link #checkStrLenCopy} instead, which rejects the negative too. | ||
| */ | ||
| static void checkStrLen(int strlen, int offset, int end) { | ||
|
wenshao marked this conversation as resolved.
Resolved
|
||
| if (strlen > end - offset) { | ||
| throw strLenOutOfRange(strlen, end - offset); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Bound a string byte length that is copied directly, with no symbol reference resolved ahead of | ||
| * it, so a negative wire length is rejected here rather than reaching the copy as a negative | ||
| * array size or count. | ||
| */ | ||
| static void checkStrLenCopy(int strlen, int offset, int end) { | ||
| if (strlen < 0 || strlen > end - offset) { | ||
| throw strLenOutOfRange(strlen, end - offset); | ||
| } | ||
| } | ||
|
|
||
| static JSONException strLenOutOfRange(int strlen, int available) { | ||
| return new JSONException("string length out of range: " + strlen + ", available: " + available); | ||
| } | ||
|
|
||
| static JSONException outOfBoundsCheckFromToIndex(int offset, int end) { | ||
| return new JSONException("offset overflow, offset " + offset + ", end " + end); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.