Skip to content

gh-156939: Detect buffer overflow in PyBytesWriter in debug mode - #156943

Merged
vstinner merged 12 commits into
python:mainfrom
vstinner:writer_canary
Sep 13, 2026
Merged

gh-156939: Detect buffer overflow in PyBytesWriter in debug mode#156943
vstinner merged 12 commits into
python:mainfrom
vstinner:writer_canary

Conversation

@vstinner

@vstinner vstinner commented Sep 4, 2026

Copy link
Copy Markdown
Member

Reserve one byte in PyBytesWriter used as a canary byte: set it to a special value. PyBytesWriter_Finish() checks if the canary byte has been overriden. Add a test on the feature.

Update buffer overflow check in fcntl: allocate extra guard bytes in the writer and then truncate these bytes.

@vstinner

vstinner commented Sep 7, 2026

Copy link
Copy Markdown
Member Author

Oh, the test_xml_etree_c failure is a legit failure: unicode_encode_ucs1() calls with errors="xmlcharrefreplace" writes a NUL byte after the writer buffer. xmlcharrefreplace() calls sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)) which writes a final NUL byte.

@vstinner

vstinner commented Sep 7, 2026

Copy link
Copy Markdown
Member Author

The test_struct error is also a legit bug: a Pascal string of 0 characters writes a NUL byte into an empty buffer, and so triggers a buffer overflow.

@vstinner

vstinner commented Sep 7, 2026

Copy link
Copy Markdown
Member Author

On Ubuntu, 8 tests are failing:

8 tests failed:
    test_codeccallbacks test_fcntl test_minidom test_sax test_struct
    test_urllib test_xml_etree test_xml_etree_c

Reserve one byte in PyBytesWriter used as a canary byte: set it to a
special value. PyBytesWriter_Finish() checks if the canary byte has
been overriden. Add a test on the feature.

Update buffer overflow check in fcntl: allocate extra guard bytes in
the writer and then truncate these bytes.
@vstinner

vstinner commented Sep 9, 2026

Copy link
Copy Markdown
Member Author

I fixed struct.pack() in PR gh-157071 and xmlcharrefreplace in PR gh-157109.

Avoid calling PyBytesWriter_Grow().
@vstinner
vstinner marked this pull request as ready for review September 9, 2026 20:42
@vstinner

vstinner commented Sep 9, 2026

Copy link
Copy Markdown
Member Author

cc @serhiy-storchaka

@vstinner
vstinner enabled auto-merge (squash) September 12, 2026 17:46
unicode_encode_ucs1() now calls PyBytesWriter_Grow() to update the
PyBytesWriter size.
PyBytesWriter_Grow() no longer calls byteswriter_resize() if grow is
smaller than 0.
@vstinner
vstinner merged commit 12a1de1 into python:main Sep 13, 2026
53 of 54 checks passed
@vstinner
vstinner deleted the writer_canary branch September 13, 2026 00:28
@vstinner

Copy link
Copy Markdown
Member Author

Android x86_64 fails with:

  ERROR: test_str (test.test_android.TestAndroidOutput.test_str) [stdout]
  ----------------------------------------------------------------------
  Traceback (most recent call last):
    File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 171, in test_str
      write("\ud800\udc00", [r"\ud800\udc00"])
      ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 148, in write
      self.assertEqual(write_len, stream.write(s))
                                  ~~~~~~~~~~~~^^^
    File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 76, in write
      self._write_chunk(chunk)
      ~~~~~~~~~~~~~~~~~^^^^^^^
    File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 83, in _write_chunk
      b = s.encode(self.encoding, self.errors)
  ValueError: size larger than allocated size

This error comes from PyBytesWriter_FinishWithSize(): the requested size is too big. I can reproduce it with just:

>>> "\ud800\udc00".encode('utf8', 'backslashreplace')
Traceback (most recent call last):
  File "<python-input-3>", line 1, in <module>
    "\ud800\udc00".encode('utf8', 'backslashreplace')
    ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: size larger than allocated size

Ah! I forgot to update the UTF-8 encoder.

@vstinner

Copy link
Copy Markdown
Member Author

Ah! I forgot to update the UTF-8 encoder.

I wrote PR gh-157383 for that.

@bedevere-bot

Copy link
Copy Markdown

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot aarch64 Android 3.x (tier-3) has failed when building commit 12a1de1.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/1594/builds/5913) and take a look at the build logs.
  4. Check if the failure is related to this commit (12a1de1) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#/builders/1594/builds/5913

Failed tests:

  • test_android

Failed subtests:

  • test_str - test.test_android.TestAndroidOutput.test_str

Summary of the results of the build (if available):

==

Click to see traceback logs
Traceback (most recent call last):
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 171, in test_str
    write("\ud800\udc00", [r"\ud800\udc00"])
    ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 148, in write
    self.assertEqual(write_len, stream.write(s))
                                ~~~~~~~~~~~~^^^
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 76, in write
    self._write_chunk(chunk)
    ~~~~~~~~~~~~~~~~~^^^^^^^
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 83, in _write_chunk
    b = s.encode(self.encoding, self.errors)
ValueError: size larger than allocated size
 
======================================================================
ERROR: test_str (test.test_android.TestAndroidOutput.test_str) [stderr]
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 171, in test_str
    write("\ud800\udc00", [r"\ud800\udc00"])
    ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 148, in write
    self.assertEqual(write_len, stream.write(s))
                                ~~~~~~~~~~~~^^^
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 76, in write
    self._write_chunk(chunk)
    ~~~~~~~~~~~~~~~~~^^^^^^^
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 83, in _write_chunk
    b = s.encode(self.encoding, self.errors)
ValueError: size larger than allocated size
 
----------------------------------------------------------------------
Ran 3 tests in 0.081s
 
FAILED (errors=2)


Traceback (most recent call last):
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 171, in test_str
    write("\ud800\udc00", [r"\ud800\udc00"])
    ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 148, in write
    self.assertEqual(write_len, stream.write(s))
                                ~~~~~~~~~~~~^^^
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 76, in write
    self._write_chunk(chunk)
    ~~~~~~~~~~~~~~~~~^^^^^^^
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 83, in _write_chunk
    b = s.encode(self.encoding, self.errors)
ValueError: size larger than allocated size
 
======================================================================
ERROR: test_str (test.test_android.TestAndroidOutput.test_str) [stderr]
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 171, in test_str
    write("\ud800\udc00", [r"\ud800\udc00"])
    ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 148, in write
    self.assertEqual(write_len, stream.write(s))
                                ~~~~~~~~~~~~^^^
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 76, in write
    self._write_chunk(chunk)
    ~~~~~~~~~~~~~~~~~^^^^^^^
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 83, in _write_chunk
    b = s.encode(self.encoding, self.errors)
ValueError: size larger than allocated size
 
----------------------------------------------------------------------
Ran 3 tests in 0.134s
 
FAILED (errors=2)
bytearraymemoryviewmmrve\xc0\x80\xc0\x80�\xc0\x80\xfe\xff\xff\xffaHelloHello world   olé中文😀test test_android failed

@bedevere-bot

Copy link
Copy Markdown

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot AMD64 Android 3.x (tier-3) has failed when building commit 12a1de1.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/1591/builds/5239) and take a look at the build logs.
  4. Check if the failure is related to this commit (12a1de1) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#/builders/1591/builds/5239

Failed tests:

  • test_android

Failed subtests:

  • test_str - test.test_android.TestAndroidOutput.test_str

Summary of the results of the build (if available):

==

Click to see traceback logs
Traceback (most recent call last):
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 171, in test_str
    write("\ud800\udc00", [r"\ud800\udc00"])
    ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 148, in write
    self.assertEqual(write_len, stream.write(s))
                                ~~~~~~~~~~~~^^^
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 76, in write
    self._write_chunk(chunk)
    ~~~~~~~~~~~~~~~~~^^^^^^^
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 83, in _write_chunk
    b = s.encode(self.encoding, self.errors)
ValueError: size larger than allocated size
 
======================================================================
ERROR: test_str (test.test_android.TestAndroidOutput.test_str) [stderr]
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 171, in test_str
    write("\ud800\udc00", [r"\ud800\udc00"])
    ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 148, in write
    self.assertEqual(write_len, stream.write(s))
                                ~~~~~~~~~~~~^^^
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 76, in write
    self._write_chunk(chunk)
    ~~~~~~~~~~~~~~~~~^^^^^^^
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 83, in _write_chunk
    b = s.encode(self.encoding, self.errors)
ValueError: size larger than allocated size
 
----------------------------------------------------------------------
Ran 3 tests in 0.595s
 
FAILED (errors=2)
bytearraymemoryviewmmrve\xc0\x80\xc0\x80�\xc0\x80\xfe\xff\xff\xffaHelloHello world   olé中文😀test test_android failed


Traceback (most recent call last):
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 171, in test_str
    write("\ud800\udc00", [r"\ud800\udc00"])
    ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 148, in write
    self.assertEqual(write_len, stream.write(s))
                                ~~~~~~~~~~~~^^^
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 76, in write
    self._write_chunk(chunk)
    ~~~~~~~~~~~~~~~~~^^^^^^^
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 83, in _write_chunk
    b = s.encode(self.encoding, self.errors)
ValueError: size larger than allocated size
 
======================================================================
ERROR: test_str (test.test_android.TestAndroidOutput.test_str) [stderr]
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 171, in test_str
    write("\ud800\udc00", [r"\ud800\udc00"])
    ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 148, in write
    self.assertEqual(write_len, stream.write(s))
                                ~~~~~~~~~~~~^^^
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 76, in write
    self._write_chunk(chunk)
    ~~~~~~~~~~~~~~~~~^^^^^^^
  File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 83, in _write_chunk
    b = s.encode(self.encoding, self.errors)
ValueError: size larger than allocated size
 
----------------------------------------------------------------------
Ran 3 tests in 0.228s
 
FAILED (errors=2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants