fix(logstorage): replace unbounded strcat with snprintf in storage_dir_info#873
fix(logstorage): replace unbounded strcat with snprintf in storage_dir_info#873SoundMatt wants to merge 1 commit into
Conversation
minminlittleshrimp
left a comment
There was a problem hiding this comment.
Please kindly check and fix the api logic, not remove it completely.
Thank you
| db_offset = db_offset + (int)sizeof(uint8_t); | ||
| memcpy(&(req->apidlen), msg->databuffer + db_offset, sizeof(uint8_t)); | ||
| db_offset = db_offset + (int)sizeof(uint8_t); | ||
| dlt_set_id_v2(req->apid, (const char *)(msg->databuffer + db_offset), req->apidlen); |
There was a problem hiding this comment.
Hello @SoundMatt
please kindly explain why the official api is removed.
Kindly approach with fixing the official API instead of manually handly app id or ctx id.
…r_info Three chained strcat() calls build a path as dir + "/" + d_name into a fixed-size tmpfile[] buffer (DLT_OFFLINE_LOGSTORAGE_MAX_LOG_FILE_LEN+1, i.e. 121 bytes). dir comes from dirname() applied to a config field that is bounded by DLT_OFFLINE_LOGSTORAGE_MAX_FILE_NAME_LEN (100 chars), but d_name is returned by scandir() and can be up to NAME_MAX (255 chars) on Linux. dir(~100) + "/" + d_name(~255) can easily exceed 121 bytes, overflowing tmpfile on the stack. Replace with a single snprintf(..., sizeof(tmpfile), "%s/%s", dir, files[i]->d_name) which hard-limits the write to the buffer size, and use strncpy for the no-dir branch to be consistent. Signed-off-by: Matt Jones <47545907+SoundMatt@users.noreply.github.com>
6ff5773 to
64e2696
Compare
|
Thanks @minminlittleshrimp — I think the confusion was that this branch was accidentally stacked on top of the GET_LOG_INFO V2 commits, so your comment landed on the I've rebased this PR onto The |
Summary
dlt_logstorage_storage_dir_info()indlt_offline_logstorage_behavior.cbuilds a file path from three pieces using three chainedstrcat()calls into a fixed-size stack buffer:diris derived from a config-bounded field (maxDLT_OFFLINE_LOGSTORAGE_MAX_FILE_NAME_LEN= 100 chars), so the directory component is bounded. Howeverfiles[i]->d_namecomes fromscandir()and can be up toNAME_MAX(255 bytes) on Linux.dir(~99) + "/" + d_name(~255) = ~355 bytescan easily overflow the 121-bytetmpfilebuffer, causing a stack buffer overflow. The overflow is triggerable whenever the storage directory contains a file whose name exceeds the buffer capacity, which can happen naturally or via a crafted mount-point with long filenames.Changes
Replace the three
strcat()calls with a singlesnprintf(..., sizeof(tmpfile), ...), which hard-limits the write to the buffer size. Add astrncpyfor the no-directory branch for consistency.Testing
DLT_OFFLINE_LOGSTORAGE_MAX_LOG_FILE_LENare copied unchanged.