Files
buildroot/package/python-cbor2/0003-CVE-2026-26209.patch
Thomas Perale 5541178b12 package/python-cbor2: patch CVE-2025-6{4076, 8131} CVE-2026-26209
Thanks to OpenEmbedded Community for providing the patches:

https://github.com/openembedded/meta-openembedded/blob/scarthgap/meta-python/recipes-devtools/python/python3-cbor2/

- CVE-2025-64076:
    Multiple vulnerabilities exist in cbor2 through version 5.7.0 in the
    decode_definite_long_string() function of the C extension decoder
    (source/decoder.c): (1) Integer Underflow Leading to Out-of-Bounds
    Read (CWE-191, CWE-125): An incorrect variable reference and missing
    state reset in the chunk processing loop causes buffer_length to not
    be reset to zero after UTF-8 character consumption. This results in
    subsequent chunk_length calculations producing negative values (e.g.,
    chunk_length = 65536 - buffer_length), which are passed as signed
    integers to the read() method, potentially triggering unlimited read
    operations and resource exhaustion. (2) Memory Leak via Missing
    Reference Count Release (CWE-401): The main processing loop fails to
    release Python object references (Py_DECREF) for chunk objects
    allocated in each iteration. For CBOR strings longer than 65536 bytes,
    this causes cumulative memory leaks proportional to the payload size,
    enabling memory exhaustion attacks through repeated processing of
    large CBOR payloads. Both vulnerabilities can be exploited remotely
    without authentication by sending specially-crafted CBOR data
    containing definite-length text strings with multi-byte UTF-8
    characters positioned at 65536-byte chunk boundaries. Successful
    exploitation results in denial of service through process crashes
    (CBORDecodeEOF exceptions) or memory exhaustion. The vulnerabilities
    affect all applications using cbor2's C extension to process untrusted
    CBOR data, including web APIs, IoT data collectors, and message queue
    processors. Fixed in commit 851473490281f82d82560b2368284ef33cf6e8f9
    pushed with released version 5.7.1.

For more information, see:
 - https://www.cve.org/CVERecord?id=CVE-2025-64076

- CVE-2025-68131:
    cbor2 provides encoding and decoding for the Concise Binary Object
    Representation (CBOR) serialization format. Starting in version 3.0.0
    and prior to version 5.8.0, whhen a CBORDecoder instance is reused
    across multiple decode operations, values marked with the shareable
    tag (28) persist in memory and can be accessed by subsequent CBOR
    messages using the sharedref tag (29). This allows an attacker-
    controlled message to read data from previously decoded messages if
    the decoder is reused across trust boundaries. Version 5.8.0 patches
    the issue.

For more information, see:
 - https://www.cve.org/CVERecord?id=CVE-2025-68131

- CVE-2026-26209:
    cbor2 provides encoding and decoding for the Concise Binary Object
    Representation (CBOR) serialization format. Versions prior to 5.9.0
    are vulnerable to a Denial of Service (DoS) attack caused by
    uncontrolled recursion when decoding deeply nested CBOR structures.
    This vulnerability affects both the pure Python implementation and the
    C extension `_cbor2`. The C extension relies on Python's internal
    recursion limits `Py_EnterRecursiveCall` rather than a data-driven
    depth limit, meaning it still raises `RecursionError` and crashes the
    worker process when the limit is hit. While the library handles
    moderate nesting levels, it lacks a hard depth limit. An attacker can
    supply a crafted CBOR payload containing approximately 100,000 nested
    arrays `0x81`. When `cbor2.loads()` attempts to parse this, it hits
    the Python interpreter's maximum recursion depth or exhausts the
    stack, causing the process to crash with a `RecursionError`. Because
    the library does not enforce its own limits, it allows an external
    attacker to exhaust the host application's stack resource. In many web
    application servers (e.g., Gunicorn, Uvicorn) or task queues (Celery),
    an unhandled `RecursionError` terminates the worker process
    immediately. By sending a stream of these small (<100KB) malicious
    packets, an attacker can repeatedly crash worker processes, resulting
    in a complete Denial of Service for the application. Version 5.9.0
    patches the issue.

For more information, see:
 - https://www.cve.org/CVERecord?id=CVE-2026-26209

(cherry picked from commit b676a4f51b)
Signed-off-by: Thomas Perale <thomas.perale@mind.be>
2026-05-13 14:01:15 +02:00

375 lines
13 KiB
Diff

From fb4ee1612a8a1ac0dbd8cf2f2f6f931a4e06d824 Mon Sep 17 00:00:00 2001
From: Andreas Eriksen <andreer@vespa.ai>
Date: Mon, 29 Dec 2025 14:01:52 +0100
Subject: [PATCH] Added a read-ahead buffer to the C decoder (#268)
Upstream: https://github.com/agronholm/cbor2/commit/fb4ee1612a8a1ac0dbd8cf2f2f6f931a4e06d824.patch
Signed-off-by: Hitendra Prajapati <hprajapati@mvista.com>
Upstream: https://github.com/openembedded/meta-openembedded/blob/scarthgap/meta-python/recipes-devtools/python/python3-cbor2/CVE-2026-26209-pre1.patch
CVE: CVE-2026-26209
[thomas: backport, stripped tests and changelog]
Signed-off-by: Thomas Perale <thomas.perale@mind.be>
---
source/decoder.c | 225 ++++++++++++++++++++++++++++++++--------
source/decoder.h | 9 ++
2 files changed, 281 insertions(+), 44 deletions(-)
diff --git a/source/decoder.c b/source/decoder.c
index 4f7ee5d..9cd1596 100644
--- a/source/decoder.c
+++ b/source/decoder.c
@@ -42,6 +42,7 @@ enum DecodeOption {
typedef uint8_t DecodeOptions;
static int _CBORDecoder_set_fp(CBORDecoderObject *, PyObject *, void *);
+static int _CBORDecoder_set_fp_with_read_size(CBORDecoderObject *, PyObject *, Py_ssize_t);
static int _CBORDecoder_set_tag_hook(CBORDecoderObject *, PyObject *, void *);
static int _CBORDecoder_set_object_hook(CBORDecoderObject *, PyObject *, void *);
static int _CBORDecoder_set_str_errors(CBORDecoderObject *, PyObject *, void *);
@@ -101,6 +102,13 @@ CBORDecoder_clear(CBORDecoderObject *self)
Py_CLEAR(self->shareables);
Py_CLEAR(self->stringref_namespace);
Py_CLEAR(self->str_errors);
+ if (self->readahead) {
+ PyMem_Free(self->readahead);
+ self->readahead = NULL;
+ self->readahead_size = 0;
+ }
+ self->read_pos = 0;
+ self->read_len = 0;
return 0;
}
@@ -143,6 +151,10 @@ CBORDecoder_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
self->immutable = false;
self->shared_index = -1;
self->decode_depth = 0;
+ self->readahead = NULL;
+ self->readahead_size = 0;
+ self->read_pos = 0;
+ self->read_len = 0;
}
return (PyObject *) self;
error:
@@ -152,21 +164,27 @@ error:
// CBORDecoder.__init__(self, fp=None, tag_hook=None, object_hook=None,
-// str_errors='strict')
+// str_errors='strict', read_size=4096)
int
CBORDecoder_init(CBORDecoderObject *self, PyObject *args, PyObject *kwargs)
{
static char *keywords[] = {
- "fp", "tag_hook", "object_hook", "str_errors", NULL
+ "fp", "tag_hook", "object_hook", "str_errors", "read_size", NULL
};
PyObject *fp = NULL, *tag_hook = NULL, *object_hook = NULL,
*str_errors = NULL;
+ Py_ssize_t read_size = CBOR2_DEFAULT_READ_SIZE;
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OOO", keywords,
- &fp, &tag_hook, &object_hook, &str_errors))
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OOOn", keywords,
+ &fp, &tag_hook, &object_hook, &str_errors, &read_size))
return -1;
- if (_CBORDecoder_set_fp(self, fp, NULL) == -1)
+ if (read_size < 1) {
+ PyErr_SetString(PyExc_ValueError, "read_size must be at least 1");
+ return -1;
+ }
+
+ if (_CBORDecoder_set_fp_with_read_size(self, fp, read_size) == -1)
return -1;
if (tag_hook && _CBORDecoder_set_tag_hook(self, tag_hook, NULL) == -1)
return -1;
@@ -197,11 +215,12 @@ _CBORDecoder_get_fp(CBORDecoderObject *self, void *closure)
}
-// CBORDecoder._set_fp(self, value)
+// Internal: set fp with configurable read size
static int
-_CBORDecoder_set_fp(CBORDecoderObject *self, PyObject *value, void *closure)
+_CBORDecoder_set_fp_with_read_size(CBORDecoderObject *self, PyObject *value, Py_ssize_t read_size)
{
PyObject *tmp, *read;
+ char *new_buffer = NULL;
if (!value) {
PyErr_SetString(PyExc_AttributeError, "cannot delete fp attribute");
@@ -214,13 +233,43 @@ _CBORDecoder_set_fp(CBORDecoderObject *self, PyObject *value, void *closure)
return -1;
}
+ if (self->readahead == NULL || self->readahead_size != read_size) {
+ new_buffer = (char *)PyMem_Malloc(read_size);
+ if (!new_buffer) {
+ Py_DECREF(read);
+ PyErr_NoMemory();
+ return -1;
+ }
+ }
+
// See notes in encoder.c / _CBOREncoder_set_fp
tmp = self->read;
self->read = read;
Py_DECREF(tmp);
+
+ self->read_pos = 0;
+ self->read_len = 0;
+
+ // Replace buffer (size changed or was NULL)
+ if (new_buffer) {
+ PyMem_Free(self->readahead);
+ self->readahead = new_buffer;
+ self->readahead_size = read_size;
+ }
+
return 0;
}
+// CBORDecoder._set_fp(self, value) - property setter uses default read size
+static int
+_CBORDecoder_set_fp(CBORDecoderObject *self, PyObject *value, void *closure)
+{
+ // Use existing readahead_size if already allocated, otherwise use default
+ Py_ssize_t read_size = (self->readahead_size > 0) ?
+ self->readahead_size : CBOR2_DEFAULT_READ_SIZE;
+ return _CBORDecoder_set_fp_with_read_size(self, value, read_size);
+}
+
// CBORDecoder._get_tag_hook(self)
static PyObject *
@@ -376,45 +425,93 @@ raise_from(PyObject *new_exc_type, const char *message) {
}
}
-static PyObject *
-fp_read_object(CBORDecoderObject *self, const Py_ssize_t size)
+// Read directly into caller's buffer (bypassing readahead buffer)
+static Py_ssize_t
+fp_read_bytes(CBORDecoderObject *self, char *buf, Py_ssize_t size)
{
- PyObject *ret = NULL;
- PyObject *obj, *size_obj;
- size_obj = PyLong_FromSsize_t(size);
- if (size_obj) {
- obj = PyObject_CallFunctionObjArgs(self->read, size_obj, NULL);
- Py_DECREF(size_obj);
- if (obj) {
- assert(PyBytes_CheckExact(obj));
- if (PyBytes_GET_SIZE(obj) == (Py_ssize_t) size) {
- ret = obj;
+ PyObject *size_obj = PyLong_FromSsize_t(size);
+ if (!size_obj)
+ return -1;
+
+ PyObject *obj = PyObject_CallFunctionObjArgs(self->read, size_obj, NULL);
+ Py_DECREF(size_obj);
+ if (!obj)
+ return -1;
+
+ assert(PyBytes_CheckExact(obj));
+ Py_ssize_t bytes_read = PyBytes_GET_SIZE(obj);
+ if (bytes_read > 0)
+ memcpy(buf, PyBytes_AS_STRING(obj), bytes_read);
+
+ Py_DECREF(obj);
+ return bytes_read;
+}
+
+// Read into caller's buffer using the readahead buffer
+static int
+fp_read(CBORDecoderObject *self, char *buf, const Py_ssize_t size)
+{
+ Py_ssize_t available, to_copy, remaining, total_copied;
+
+ remaining = size;
+ total_copied = 0;
+
+ while (remaining > 0) {
+ available = self->read_len - self->read_pos;
+
+ if (available > 0) {
+ // Copy from buffer
+ to_copy = (available < remaining) ? available : remaining;
+ memcpy(buf + total_copied, self->readahead + self->read_pos, to_copy);
+ self->read_pos += to_copy;
+ total_copied += to_copy;
+ remaining -= to_copy;
+ } else {
+ Py_ssize_t bytes_read;
+
+ if (remaining >= self->readahead_size) {
+ // Large remaining: read directly into destination, bypass buffer
+ bytes_read = fp_read_bytes(self, buf + total_copied, remaining);
+ if (bytes_read > 0) {
+ total_copied += bytes_read;
+ remaining -= bytes_read;
+ }
} else {
- PyErr_Format(
- _CBOR2_CBORDecodeEOF,
- "premature end of stream (expected to read %zd bytes, "
- "got %zd instead)", size, PyBytes_GET_SIZE(obj));
- Py_DECREF(obj);
+ // Small remaining: refill buffer
+ self->read_pos = 0;
+ self->read_len = 0;
+ bytes_read = fp_read_bytes(self, self->readahead, self->readahead_size);
+ if (bytes_read > 0)
+ self->read_len = bytes_read;
+ }
+
+ if (bytes_read <= 0) {
+ if (bytes_read == 0)
+ PyErr_Format(
+ _CBOR2_CBORDecodeEOF,
+ "premature end of stream (expected to read %zd bytes, "
+ "got %zd instead)", size, total_copied);
+ return -1;
}
}
}
- return ret;
-}
+ return 0;
+}
-static int
-fp_read(CBORDecoderObject *self, char *buf, const Py_ssize_t size)
+// Read and return as PyBytes object
+static PyObject *
+fp_read_object(CBORDecoderObject *self, const Py_ssize_t size)
{
- int ret = -1;
- PyObject *obj = fp_read_object(self, size);
- if (obj) {
- char *data = PyBytes_AS_STRING(obj);
- if (data) {
- memcpy(buf, data, size);
- ret = 0;
- }
- Py_DECREF(obj);
+ PyObject *ret = PyBytes_FromStringAndSize(NULL, size);
+ if (!ret)
+ return NULL;
+
+ if (fp_read(self, PyBytes_AS_STRING(ret), size) == -1) {
+ Py_DECREF(ret);
+ return NULL;
}
+
return ret;
}
@@ -2091,23 +2188,55 @@ static PyObject *
CBORDecoder_decode_from_bytes(CBORDecoderObject *self, PyObject *data)
{
PyObject *save_read, *buf, *ret = NULL;
+ bool is_nested = (self->decode_depth > 0);
+ Py_ssize_t save_read_pos = 0, save_read_len = 0;
+ char *save_buffer = NULL;
if (!_CBOR2_BytesIO && _CBOR2_init_BytesIO() == -1)
return NULL;
+ buf = PyObject_CallFunctionObjArgs(_CBOR2_BytesIO, data, NULL);
+ if (!buf)
+ return NULL;
+
self->decode_depth++;
save_read = self->read;
- buf = PyObject_CallFunctionObjArgs(_CBOR2_BytesIO, data, NULL);
- if (buf) {
- self->read = PyObject_GetAttr(buf, _CBOR2_str_read);
- if (self->read) {
- ret = decode(self, DECODE_NORMAL);
- Py_DECREF(self->read);
+ Py_INCREF(save_read); // Keep alive while we use a different read method
+ save_read_pos = self->read_pos;
+ save_read_len = self->read_len;
+
+ // Save buffer pointer if nested
+ if (is_nested) {
+ save_buffer = self->readahead;
+ self->readahead = NULL; // Prevent setter from freeing saved buffer
+ }
+
+ // Set up BytesIO decoder - setter handles buffer allocation
+ if (_CBORDecoder_set_fp_with_read_size(self, buf, self->readahead_size) == -1) {
+ if (is_nested) {
+ PyMem_Free(self->readahead);
+ self->readahead = save_buffer;
}
+ Py_DECREF(save_read);
Py_DECREF(buf);
+ self->decode_depth--;
+ return NULL;
}
- self->read = save_read;
+
+ ret = decode(self, DECODE_NORMAL);
+
+ Py_XDECREF(self->read); // Decrement BytesIO read method
+ self->read = save_read; // Restore saved read (already has correct refcount)
+ Py_DECREF(buf);
self->decode_depth--;
+
+ if (is_nested) {
+ PyMem_Free(self->readahead);
+ self->readahead = save_buffer;
+ }
+ self->read_pos = save_read_pos;
+ self->read_len = save_read_len;
+
assert(self->decode_depth >= 0);
if (self->decode_depth == 0) {
clear_shareable_state(self);
@@ -2257,6 +2386,14 @@ PyDoc_STRVAR(CBORDecoder__doc__,
" dictionary. This callback is invoked for each deserialized\n"
" :class:`dict` object. The return value is substituted for the dict\n"
" in the deserialized output.\n"
+":param read_size:\n"
+" the size of the read buffer (default 4096). The decoder reads from\n"
+" the stream in chunks of this size for performance. This means the\n"
+" stream position may advance beyond the bytes actually decoded. For\n"
+" large values (bytestrings, text strings), reads may be larger than\n"
+" ``read_size``. Code that needs to read from the stream after\n"
+" decoding should use :meth:`decode_from_bytes` instead, or set\n"
+" ``read_size=1`` to disable buffering (at a performance cost).\n"
"\n"
".. _CBOR: https://cbor.io/\n"
);
diff --git a/source/decoder.h b/source/decoder.h
index a2f1bcb..a2f4bf1 100644
--- a/source/decoder.h
+++ b/source/decoder.h
@@ -3,6 +3,9 @@
#include <stdbool.h>
#include <stdint.h>
+// Default readahead buffer size for streaming reads
+#define CBOR2_DEFAULT_READ_SIZE 4096
+
typedef struct {
PyObject_HEAD
PyObject *read; // cached read() method of fp
@@ -14,6 +17,12 @@ typedef struct {
bool immutable;
Py_ssize_t shared_index;
Py_ssize_t decode_depth;
+
+ // Readahead buffer for streaming
+ char *readahead; // allocated buffer
+ Py_ssize_t readahead_size; // size of allocated buffer
+ Py_ssize_t read_pos; // current position in buffer
+ Py_ssize_t read_len; // valid bytes in buffer
} CBORDecoderObject;
extern PyTypeObject CBORDecoderType;