//nefariousplan

CVE-2026-38427: Tasmota's fetch_jpg Patch Fixes Two Buffer Overflows. The CVE Names One.

pattern

cve

proof of concept

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.

The named bug: a four-line readBytesUntil dance and a uint16_t it feeds

The 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.

Here is case 2 in v15.3.0, in tasmota/tasmota_xdrv_driver/xdrv_10_scripter.ino:

case 2:
  // get next frame
  /*Wc.client.print("--" BOUNDARY "\r\n");
  Wc.client.printf("Content-Type: image/jpeg\r\n"
    "Content-Length: %d\r\n"
    "\r\n", static_cast<int>(_jpg_buf_len));
    */
  {
    if (glob_script_mem.jpg_task.http.connected()) {
      char inbuff[64];
      memset(inbuff, 0, sizeof(inbuff));
      glob_script_mem.jpg_task.stream.readBytesUntil('\n', inbuff, sizeof(inbuff));
      if (inbuff[0] == '\r') {
        memset(inbuff, 0, sizeof(inbuff));
        glob_script_mem.jpg_task.stream.readBytesUntil('\n', inbuff, sizeof(inbuff));
      }
      memset(inbuff, 0, sizeof(inbuff));
      glob_script_mem.jpg_task.stream.readBytesUntil('\n', inbuff, sizeof(inbuff));
      memset(inbuff, 0, sizeof(inbuff));
      glob_script_mem.jpg_task.stream.readBytesUntil('\n', inbuff, sizeof(inbuff));
      char *cp = strchr(inbuff, ':');
      uint16_t size = 0;
      if (cp) {
        size = atoi(cp + 1);
      }
      glob_script_mem.jpg_task.stream.readBytesUntil('\n', inbuff, sizeof(inbuff));

      if (size > 0) {
        uint8_t *buff = (uint8_t *)special_malloc(size);
        if (buff) {
          glob_script_mem.jpg_task.stream.readBytes(buff, size);
        }
        if (glob_script_mem.jpg_task.draw) {
          Draw_jpeg(buff, size, glob_script_mem.jpg_task.xp,
                                glob_script_mem.jpg_task.yp,
                                glob_script_mem.jpg_task.scale);
        }
        if (buff) {
          free(buff);
        }
      }
      return size;
    }
  }
  break;

The 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.

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.

The 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.

A 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.

The fix:

-          uint16_t size = 0;
+          uint32_t size = 0;

One 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."

The bug the CVE does not name

Three hunks above the type change, in the same commit (5009bea), in the same function, the patch also changes this:

-          strcpy(glob_script_mem.jpg_task.boundary,cp + 1);
+          strlcpy(glob_script_mem.jpg_task.boundary, cp + 1, sizeof(glob_script_mem.jpg_task.boundary));

The 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:

case 0:
  glob_script_mem.jpg_task.boundary[0] = 0;
  ...
  sprintf(hbuff, "http://%s", url);
  glob_script_mem.jpg_task.http.begin(glob_script_mem.jpg_task.stream, hbuff);
  glob_script_mem.jpg_task.http.collectHeaders(headerKeys, numberOfHeaders);
  httpCode = glob_script_mem.jpg_task.http.GET();
  if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
    String boundary = glob_script_mem.jpg_task.http.header(headerKeys[0]);
    char *cp = strchr(boundary.c_str(), '=');
    if (cp) {
      strcpy(glob_script_mem.jpg_task.boundary, cp + 1);
    }
  }

headerKeys[0] is "Content-Type". The destination is fixed:

struct JPG_TASK {
  char boundary[40];
  bool draw;
  uint8_t scale;
  uint16_t xp;
  uint16_t yp;
  WiFiClient stream;
  HTTPClient http;
} jpg_task;

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.

The fix is strcpy to strlcpy with a third argument.

CVE-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."

The word boundary does not appear. The word Content-Type does not appear. The word strcpy does not appear.

The 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.

The producer the receiver was modeled on

Two lines above the int32_t fetch_jpg(...) signature, in v15.3.0:

// "e8b8c539-047d-4777-a985-fbba6edff11e"

int32_t fetch_jpg(uint32_t sel, char *url, uint32_t xp, uint32_t yp, uint32_t scale) {

A 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:

case 2:
  // get next frame
  /*Wc.client.print("--" BOUNDARY "\r\n");
  Wc.client.printf("Content-Type: image/jpeg\r\n"
    "Content-Length: %d\r\n"
    "\r\n", static_cast<int>(_jpg_buf_len));
    */

That 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.

Line 166 of the producer:

#define BOUNDARY "e8b8c539-047d-4777-a985-fbba6edff11e"

A 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.

Line 748 of the producer:

size_t _jpg_buf_len = 0;

A size_t. The producer's static_cast<int> 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.

The 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.

This is closed-network-assumed at the receiver's typedefs. The OP-TEE PKCS#11 J1939 exhibit 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.

CVE-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.

One word fixed the CVE. The patched function still has a third sink.

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.

The post-patch case 2 still does this:

if (size > 0) {
  uint8_t *buff = (uint8_t *)special_malloc(size);
  if (buff) {
    glob_script_mem.jpg_task.stream.readBytes(buff, size);
  }
  if (glob_script_mem.jpg_task.draw) {
    Draw_jpeg(buff, size, glob_script_mem.jpg_task.xp,
                          glob_script_mem.jpg_task.yp,
                          glob_script_mem.jpg_task.scale);
  }
  if (buff) {
    free(buff);
  }
}

The 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.

That 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.

The 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.

A defender reading the CVE description sees one bug. A defender reading the commit sees two. A defender reading the patched function sees three.

PoC: sermikr0/CVE-2026-38427

The CVE description was the bug list. The diff was longer.