-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 NEFARIOUSPLAN-CANONICAL-V1 {"body_md":"## The named bug: a four-line readBytesUntil dance and a uint16_t it feeds\n\nThe Tasmota scripter exposes `fetchjp(url, xp, yp, scale)` as a script function. From a Berry script, an operator calls `fetchjp(URL, 0, 0, 1)` to open an MJPEG HTTP stream and `fetchjp(2, 0, 0, 1)` to pull the next frame. Case 0 opens the GET. Case 2 reads one frame per call.\n\nHere is case 2 in v15.3.0, in `tasmota/tasmota_xdrv_driver/xdrv_10_scripter.ino`:\n\n```cpp\ncase 2:\n // get next frame\n /*Wc.client.print(\"--\" BOUNDARY \"\\r\\n\");\n Wc.client.printf(\"Content-Type: image/jpeg\\r\\n\"\n \"Content-Length: %d\\r\\n\"\n \"\\r\\n\", static_cast(_jpg_buf_len));\n */\n {\n if (glob_script_mem.jpg_task.http.connected()) {\n char inbuff[64];\n memset(inbuff, 0, sizeof(inbuff));\n glob_script_mem.jpg_task.stream.readBytesUntil('\\n', inbuff, sizeof(inbuff));\n if (inbuff[0] == '\\r') {\n memset(inbuff, 0, sizeof(inbuff));\n glob_script_mem.jpg_task.stream.readBytesUntil('\\n', inbuff, sizeof(inbuff));\n }\n memset(inbuff, 0, sizeof(inbuff));\n glob_script_mem.jpg_task.stream.readBytesUntil('\\n', inbuff, sizeof(inbuff));\n memset(inbuff, 0, sizeof(inbuff));\n glob_script_mem.jpg_task.stream.readBytesUntil('\\n', inbuff, sizeof(inbuff));\n char *cp = strchr(inbuff, ':');\n uint16_t size = 0;\n if (cp) {\n size = atoi(cp + 1);\n }\n glob_script_mem.jpg_task.stream.readBytesUntil('\\n', inbuff, sizeof(inbuff));\n\n if (size > 0) {\n uint8_t *buff = (uint8_t *)special_malloc(size);\n if (buff) {\n glob_script_mem.jpg_task.stream.readBytes(buff, size);\n }\n if (glob_script_mem.jpg_task.draw) {\n Draw_jpeg(buff, size, glob_script_mem.jpg_task.xp,\n glob_script_mem.jpg_task.yp,\n glob_script_mem.jpg_task.scale);\n }\n if (buff) {\n free(buff);\n }\n }\n return size;\n }\n }\n break;\n```\n\nThe four `readBytesUntil` calls consume the four lines that open a multipart MJPEG part: the boundary line, the `Content-Type` header, the `Content-Length` header, and the blank line that ends the part headers. The fourth one is the length. `strchr(inbuff, ':')` finds the colon. `atoi(cp + 1)` parses everything after it.\n\n`atoi` returns `int`. The result lands in a `uint16_t`. A response that emits `Content-Length: 70000` produces `size = 4464`. `special_malloc(4464)` allocates 4464 bytes. `readBytes(buff, 4464)` reads 4464 bytes off the stream. The 65536 bytes the producer announced but the consumer did not consume stay queued on the WiFi client's receive buffer.\n\nThe next case-2 call's first `readBytesUntil('\\n', ...)` reads from inside the previous frame's JPEG payload. The script expected a boundary line. It got JPEG-payload bytes terminated by whatever byte happened to equal `\\n` (0x0A). Three more `readBytesUntil` calls follow. Each one extracts another slice of JPEG-payload bytes and stores it where the parser expects HTTP framing. The fourth one feeds `strchr(inbuff, ':')` and `atoi(cp + 1)`. The next `size` is whatever JPEG-payload bytes the attacker arranged to appear there.\n\nA header line of `Content-Length: 65537` followed by 65537 actual bytes wraps to `size = 1`. `special_malloc(1)` allocates one byte. `readBytes(buff, 1)` reads one byte. 65536 bytes are still in the stream. `Content-Length: 65536` wraps to `size = 0`; the `if (size > 0)` skips the allocate-and-read entirely, and the entire 65536-byte payload sits there for the next iteration to misparse. The author of the PoC picked 65537 and 131072 for the demonstrations, both of which leave exactly 65536 bytes of attacker-chosen payload queued behind the consumer's framing cursor.\n\nThe fix:\n\n```diff\n- uint16_t size = 0;\n+ uint32_t size = 0;\n```\n\nOne word. The CVE description names this: \"The Content-Length from a JPEG stream is stored in a `uint16_t` variable; values above 65535 wrap around, causing allocation of a smaller buffer than the data actually read.\"\n\n## The bug the CVE does not name\n\nThree hunks above the type change, in the same commit (`5009bea`), in the same function, the patch also changes this:\n\n```diff\n- strcpy(glob_script_mem.jpg_task.boundary,cp + 1);\n+ strlcpy(glob_script_mem.jpg_task.boundary, cp + 1, sizeof(glob_script_mem.jpg_task.boundary));\n```\n\nThe block is in case 0, where the stream is opened. After the initial `GET` returns, the receiver pulls the `Content-Type` response header off the same HTTP exchange, finds the `boundary=` parameter on it, and copies the value into a fixed buffer:\n\n```cpp\ncase 0:\n glob_script_mem.jpg_task.boundary[0] = 0;\n ...\n sprintf(hbuff, \"http://%s\", url);\n glob_script_mem.jpg_task.http.begin(glob_script_mem.jpg_task.stream, hbuff);\n glob_script_mem.jpg_task.http.collectHeaders(headerKeys, numberOfHeaders);\n httpCode = glob_script_mem.jpg_task.http.GET();\n if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {\n String boundary = glob_script_mem.jpg_task.http.header(headerKeys[0]);\n char *cp = strchr(boundary.c_str(), '=');\n if (cp) {\n strcpy(glob_script_mem.jpg_task.boundary, cp + 1);\n }\n }\n```\n\n`headerKeys[0]` is `\"Content-Type\"`. The destination is fixed:\n\n```cpp\nstruct JPG_TASK {\n char boundary[40];\n bool draw;\n uint8_t scale;\n uint16_t xp;\n uint16_t yp;\n WiFiClient stream;\n HTTPClient http;\n} jpg_task;\n```\n\n`boundary[40]` is followed by `draw`, `scale`, the two position fields, then a `WiFiClient` and an `HTTPClient`. The last two are C++ objects with internal pointers, socket descriptors, and callback tables; `HTTPClient` carries the response state for the GET that just delivered the malformed header. A response of `Content-Type: multipart/x-mixed-replace; boundary=AAAA...` with more than 39 bytes after the `=` writes past `boundary` into the live `WiFiClient` and `HTTPClient` instances the receiver is using to read the rest of the stream from.\n\nThe fix is `strcpy` to `strlcpy` with a third argument.\n\nCVE-2026-38427's description: \"An issue in `fetch_jpg()` in `xdrv_10_scripter.ino` in Tasmota through 15.3.0.3 allows a remote attacker to cause heap buffer overflow. The Content-Length from a JPEG stream is stored in a `uint16_t` variable; values above 65535 wrap around, causing allocation of a smaller buffer than the data actually read.\"\n\nThe word `boundary` does not appear. The word `Content-Type` does not appear. The word `strcpy` does not appear.\n\nThe boundary overflow is reachable from the same attacker as the `uint16_t` wrap. The attacker is whoever owns the HTTP server that `fetchjp(URL, ...)` points at. Both bugs are reached through the same `http.GET()`. The commit closes both. The CVE record covers one.\n\n## The producer the receiver was modeled on\n\nTwo lines above the `int32_t fetch_jpg(...)` signature, in v15.3.0:\n\n```cpp\n// \"e8b8c539-047d-4777-a985-fbba6edff11e\"\n\nint32_t fetch_jpg(uint32_t sel, char *url, uint32_t xp, uint32_t yp, uint32_t scale) {\n```\n\nA UUID, in a comment, with no surrounding prose, sitting above the receiver's entry point. Four lines below the case 2 label there is this commented-out block:\n\n```cpp\ncase 2:\n // get next frame\n /*Wc.client.print(\"--\" BOUNDARY \"\\r\\n\");\n Wc.client.printf(\"Content-Type: image/jpeg\\r\\n\"\n \"Content-Length: %d\\r\\n\"\n \"\\r\\n\", static_cast(_jpg_buf_len));\n */\n```\n\nThat block is the matching producer code. It is byte-identical to lines 1004 through 1007 of `tasmota/tasmota_xdrv_driver/xdrv_81_esp32_webcam.ino` in the same repository, the file that turns an ESP32 with an attached camera into an MJPEG server. The author of `xdrv_10_scripter.ino` is Theo Arends. The author of `xdrv_81_esp32_webcam.ino` is also Theo Arends. The fragment was copied from the producer and pasted, commented, into the consumer to document the format the consumer was being written to parse. The UUID two lines above the function signature is the same `BOUNDARY` the producer hardcodes.\n\nLine 166 of the producer:\n\n```cpp\n#define BOUNDARY \"e8b8c539-047d-4777-a985-fbba6edff11e\"\n```\n\nA 36-byte UUID. The consumer's `boundary[40]` is sized for that UUID with three bytes of slack and a NUL. The UUID was important enough to copy as a comment above the receiver because the receiver's buffer width is a function of it.\n\nLine 748 of the producer:\n\n```cpp\nsize_t _jpg_buf_len = 0;\n```\n\nA `size_t`. The producer's `static_cast` casts it down to `int` for the `printf %d`. The consumer's `uint16_t size = atoi(cp + 1)` parses the printed value back. Two narrowings in a row: `size_t` to `int` at emit, `int` to `uint16_t` at parse. The first one is harmless on ESP32. The second one is the CVE.\n\nThe receiver's types were not chosen against the HTTP grammar that an MJPEG response can carry. They were chosen against the producer in the next file over. Tasmota's MJPEG protocol is a Tasmota-to-Tasmota protocol on a LAN, served by `xdrv_81_esp32_webcam.ino` and consumed by `xdrv_10_scripter.ino`. The producer's `_jpg_buf_len` never exceeded 65535 in any configuration the author ran. The producer's `BOUNDARY` was hardcoded to one constant value. The receiver's `uint16_t` and `boundary[40]` are the producer's range constraints, transcribed into types.\n\nThis is closed-network-assumed at the receiver's typedefs. The [OP-TEE PKCS#11 J1939 exhibit](/posts/open-sae-j1939-cve-2026-37537-bound-was-a-comment) named the same shape on a CAN bus, where `uint8_t index = data[0] - 1` is read off the wire and indexed into an array because the J1939 standard says the sequence number stays in the bounded range; the parser trusts the standard, not the wire. Tasmota's `uint16_t size = atoi(cp + 1)` is the same trust at a different protocol layer. The standard the receiver trusts is Tasmota's own producer.\n\nCVE-2026-38427 is what happens the first time a stranger's HTTP server is on the other end. The next file over has stopped being the only producer on the wire.\n\n## One word fixed the CVE. The patched function still has a third sink.\n\n`uint32_t size` accepts values up to roughly four billion. An ESP32 with PSRAM tops out at eight megabytes of heap. `special_malloc(4_000_000_000)` returns `NULL`.\n\nThe post-patch case 2 still does this:\n\n```cpp\nif (size > 0) {\n uint8_t *buff = (uint8_t *)special_malloc(size);\n if (buff) {\n glob_script_mem.jpg_task.stream.readBytes(buff, size);\n }\n if (glob_script_mem.jpg_task.draw) {\n Draw_jpeg(buff, size, glob_script_mem.jpg_task.xp,\n glob_script_mem.jpg_task.yp,\n glob_script_mem.jpg_task.scale);\n }\n if (buff) {\n free(buff);\n }\n}\n```\n\nThe `if (buff)` around `readBytes` is correct. The `if (buff)` around `free` is correct. The `if (glob_script_mem.jpg_task.draw)` around `Draw_jpeg` does not check `buff`. A frame whose `Content-Length` exceeds the device's heap budget reaches `Draw_jpeg(NULL, very_large_size, xp, yp, scale)` whenever `draw` is true. `draw` is true whenever an operator calls `fetchjp(URL, ...)` rather than the no-render variant; it is the configuration the function exists for.\n\nThat sink is not what CVE-2026-38427 describes. It is what survives the patch CVE-2026-38427 names. Whether `Draw_jpeg` reads the NULL pointer for the first byte of its decode or skips through it depends on the JPEG decoder Tasmota was built with; either way, the attacker still controls the call, the bug class is still the one this commit was meant to close, and the patch does not name it.\n\nThe commit message says \"Fix scripter buffer overflow,\" singular. CVE-2026-38427 was assigned to the `uint16_t`. The `strcpy` got fixed in the same commit without a name. The `Draw_jpeg(NULL, ...)` path did not get fixed at all.\n\nA defender reading the CVE description sees one bug. A defender reading the commit sees two. A defender reading the patched function sees three.\n\nPoC: [sermikr0/CVE-2026-38427](https://github.com/sermikr0/CVE-2026-38427)","closing_line":"The CVE description was the bug list. The diff was longer.","hook_md":"Tasmota commit `5009bea` lands on 2026-05-16 with a four-word subject: \"Fix scripter buffer overflow.\" The diff changes four lines of `xdrv_10_scripter.ino`, in two hunks three lines apart, fixing two distinct buffer overflows in `fetch_jpg()`. CVE-2026-38427 names one. The other reaches the same code path from the same attacker, and the commit closes it in the same diff with no second CVE number.","post_id":511,"slug":"tasmota-fetch-jpg-commit-fixes-two-cve-names-one","title":"CVE-2026-38427: Tasmota's fetch_jpg Patch Fixes Two Buffer Overflows. The CVE Names One.","type":"initial","unreadable_sentence":"The CVE description does not mention `boundary`, `Content-Type`, or `strcpy`. The commit's first hunk changes all three."} -----BEGIN PGP SIGNATURE----- iHUEARYIAB0WIQRf0htP5+SjynlxywneZjl4jgkQJgUCaoXxIAAKCRDeZjl4jgkQ JvrGAP9K7v1rH5/ECSdrcIQlpTWgzPWE4Ke4jjGg0NL2uYT5YgD9Eu1nqw2Wn3Xp 9tP6UBHdtu0C4z44mzoYMiZeL4G2DwE= =7PZu -----END PGP SIGNATURE-----