Skip to content

Commit c7e904c

Browse files
committed
lib: fix heap buffer overflow in dlt_with_filename_and_line_number
When the source filename string is longer than 255 bytes, dlt_with_filename_and_line_number(fina, linr) overflows the heap buffer it allocates for dlt_user.filename: dlt_user.filenamelen = (uint8_t)strlen(fina); // truncates ... dlt_user.filename = malloc(dlt_user.filenamelen + 1); // small strcpy(dlt_user.filename, fina); // full filenamelen is uint8_t (matching the V2 wire-format field width declared in include/dlt/dlt_user.h.in), so the cast truncates strlen(fina) modulo 256. The subsequent malloc allocates the truncated length + 1, but strcpy copies the entire fina including its real terminator, writing strlen(fina) - filenamelen bytes past the end of the heap chunk. Reachable in practice via long compile-time __FILE__ paths (monorepo-rooted absolute paths) and any application that forwards a user-controlled filename through this function. Fix: clamp strlen(fina) to UINT8_MAX before assigning filenamelen, allocate against the clamped length, and copy with memcpy + explicit null terminator instead of strcpy. The on-wire field still gets the truncated length, which matches the maximum the V2 protocol can encode.
1 parent fd1fbf8 commit c7e904c

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

src/lib/dlt_user.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4673,18 +4673,28 @@ DltReturnValue dlt_with_filename_and_line_number(const char *fina, const int lin
46734673

46744674
/* Set filename and line number */
46754675
dlt_user.with_filename_and_line_number = 1;
4676-
dlt_user.filenamelen = (uint8_t)strlen(fina);
4676+
4677+
/* filenamelen is uint8_t (matching the V2 wire-format field width).
4678+
* Clamp the source length to UINT8_MAX before truncation so that
4679+
* the malloc + copy below cannot write past the end of the heap
4680+
* allocation when strlen(fina) > 255. */
4681+
size_t fina_len = strlen(fina);
4682+
if (fina_len > UINT8_MAX)
4683+
fina_len = UINT8_MAX;
4684+
dlt_user.filenamelen = (uint8_t)fina_len;
4685+
46774686
if (dlt_user.filename != NULL) {
46784687
free(dlt_user.filename);
46794688
dlt_user.filename = NULL;
46804689
}
46814690

4682-
dlt_user.filename = (char*)malloc((size_t)dlt_user.filenamelen + 1);
4691+
dlt_user.filename = (char*)malloc(fina_len + 1);
46834692
if (dlt_user.filename == NULL){
46844693
dlt_vlog(LOG_ERR, "%s Could not allocate memory for filename", __func__);
46854694
return DLT_RETURN_ERROR;
46864695
}
4687-
strcpy(dlt_user.filename, fina);
4696+
memcpy(dlt_user.filename, fina, fina_len);
4697+
dlt_user.filename[fina_len] = '\0';
46884698

46894699
dlt_user.linenumber = (uint32_t)linr;
46904700
return DLT_RETURN_OK;

0 commit comments

Comments
 (0)