daemon: fix multi-bug response builder in dlt_daemon_control_message_unregister_context_v2 (closes #867)#868
Open
SoundMatt wants to merge 1 commit into
Conversation
…unregister_context_v2 This function had four distinct bugs (full analysis in COVESA#867): 1. uint8_t truncation of the size calculation: contextSize = (uint8_t)(... + apidlen + ... + ctidlen + DLT_ID_SIZE); sums to up to 521 but is cast to uint8_t, wrapping mod 256. msg.datasize then underallocates and the memcpy chain below overflows the heap when apidlen + ctidlen approach their uint8_t maxima. 2. resp.apidlen / resp.ctidlen never assigned. The struct lives on the stack without zero-initialisation. memcpy(buf, &resp.apidlen, 1) and memcpy(buf, &resp.ctidlen, 1) therefore write two bytes of uninitialised stack memory to the wire response — leaking stack contents to every receiving client every time an application unregisters a context. 3. resp.apid / resp.ctid explicitly NULL, never reassigned. The subsequent dlt_set_id_v2(resp.apid, ...) calls are no-ops because the destination is NULL. memcpy(buf, resp.apid, resp.apidlen) then copies from NULL with the uninitialised length from COVESA#2 — undefined behaviour, typically a segfault if the random length is non-zero. 4. The function's apidlen / apid / ctidlen / ctid parameters are never copied into resp, so even when the function happens not to crash the wire response carries wrong lengths and zero context bytes. Fix: - Zero-initialise resp with `= {0}`. - Explicitly assign resp.apidlen / resp.apid / resp.ctidlen / resp.ctid from the function parameters before serialisation. - Replace the `(uint8_t) contextSize` cast with a uint32_t accumulator that matches the int32_t width of msg.datasize. - Guard the variable-length memcpys with len > 0 && ptr != NULL so a future caller passing NULL for an empty field can't UB. Closes COVESA#867. Related: COVESA#866.
SoundMatt
added a commit
to SoundMatt/dlt-daemon
that referenced
this pull request
May 5, 2026
Companion fix to the OOB-read fix in this same commit series. The function never assigned req->apid or req->ctid: req is calloc'd, so the char * pointer fields start NULL, and the dlt_set_id_v2(req->apid, ...) call to populate them was a no-op (dlt_set_id_v2 early-returns when its destination is NULL). req->apid / req->ctid stayed NULL and were then passed to dlt_daemon_application_find_v2 and dlt_daemon_context_find_v2 despite req->apidlen / req->ctidlen being non-zero — every non-empty lookup was silently turned into a zero-length one. Replace the no-op dlt_set_id_v2 calls with conditional pointer assignments into msg->databuffer, mirroring the surgical approach taken for set_log_level_v2 in PR COVESA#864 and unregister_context_v2 in PR COVESA#868. The bounds checks added in the previous commit ensure the pointer-into-databuffer assignments are safe. Closes COVESA#870. Related: COVESA#866.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
dlt_daemon_control_message_unregister_context_v2had fourdistinct bugs (full analysis in #867):
(uint8_t)truncation — thecontextSizecast wrapped mod 256, undersizingmsg.datasizefor the subsequent
memcpychain.resp.apidlenandresp.ctidlenwere never assigned, but werememcpy'd into theresponse, leaking two bytes of stack contents per unregister
notification.
memcpyfrom NULL with uninitialised length —resp.apid/resp.ctidwere set to NULL and the subsequentdlt_set_id_v2(resp.apid, ...)calls no-op'd; the variable-lengthmemcpys then ran with a NULL source and a random length.copied into
resp, so even when the daemon survived the call thenotification carried garbage lengths and zero context bytes.
Reachable on every context unregistration (called from
dlt-daemon.c:5001).Fix
respwith= {0}.resp.apidlen/resp.apid/resp.ctidlen/resp.ctidfrom the function parameters before serialisation.(uint8_t) contextSizecast with auint32_taccumulator that matches the
int32_twidth ofmsg.datasize.memcpys withlen > 0 && ptr != NULLso a future caller passing NULL for an empty field can't UB.
Notes
meta-Issue Multiple V2 control-message handlers are broken by char* fields in protocol structs (set_log_level_v2 [#863], set_trace_status_v2, unregister_context_v2, get_log_info_v2) #866. Follow-up PRs for the others
(
set_trace_status_v2,get_log_info_v2parsing, possibly more)will reference Multiple V2 control-message handlers are broken by char* fields in protocol structs (set_log_level_v2 [#863], set_trace_status_v2, unregister_context_v2, get_log_info_v2) #866 too.
not added a regression test because this code path is exercised
only end-to-end with a live daemon and a disconnecting application.
Happy to add one if maintainers can point me at the right harness.
Closes #867.
Related: #866.