Skip to content

Fix buffer overrun while parsing malformed JSON hex escape sequence #2201

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions jerry-core/ecma/builtin-objects/ecma-builtin-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
#define BUILTIN_UNDERSCORED_ID json
#include "ecma-builtin-internal-routines-template.inc.h"

/**
* The number of expected hexidecimal characters in a hex escape sequence
*/
#define ECMA_JSON_HEX_ESCAPE_SEQUENCE_LENGTH (4)

/** \addtogroup ecma ECMA
* @{
*
Expand Down Expand Up @@ -182,13 +187,18 @@ ecma_builtin_json_parse_string (ecma_json_token_t *token_p) /**< token argument
}
case LIT_CHAR_LOWERCASE_U:
{
if ((end_p - current_p <= ECMA_JSON_HEX_ESCAPE_SEQUENCE_LENGTH))
{
return;
}

ecma_char_t code_unit;
if ((end_p - current_p >= 2) && !(lit_read_code_unit_from_hex (current_p + 1, 4, &code_unit)))
if (!(lit_read_code_unit_from_hex (current_p + 1, ECMA_JSON_HEX_ESCAPE_SEQUENCE_LENGTH, &code_unit)))
{
return;
}

current_p += 5;
current_p += ECMA_JSON_HEX_ESCAPE_SEQUENCE_LENGTH + 1;

lit_utf8_byte_t char_buffer[LIT_UTF8_MAX_BYTES_IN_CODE_UNIT];
buffer_size += lit_code_unit_to_utf8 (code_unit, char_buffer);
Expand Down Expand Up @@ -258,9 +268,9 @@ ecma_builtin_json_parse_string (ecma_json_token_t *token_p) /**< token argument
{
ecma_char_t code_unit;

lit_read_code_unit_from_hex (current_p + 1, 4, &code_unit);
lit_read_code_unit_from_hex (current_p + 1, ECMA_JSON_HEX_ESCAPE_SEQUENCE_LENGTH, &code_unit);

current_p += 5;
current_p += ECMA_JSON_HEX_ESCAPE_SEQUENCE_LENGTH + 1;
write_p += lit_code_unit_to_utf8 (code_unit, write_p);
continue;
}
Expand Down
27 changes: 27 additions & 0 deletions tests/jerry/regression-test-issue-2200.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright JS Foundation and other contributors, http://js.foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

[
// This input caused a buffer overrun,
// see https://github.com/jerryscript-project/jerryscript/issues/2200
'"\\ubad',
// Test similar malformations as well:
'"\\ubad"',
'"\\u',
].forEach(function(badJson) {
try {
JSON.parse(badJson);
} catch (e) {
}
});