Skip to content

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
COVESA:masterfrom
SoundMatt:fix/unregister-context-v2-response-builder
Open

daemon: fix multi-bug response builder in dlt_daemon_control_message_unregister_context_v2 (closes #867)#868
SoundMatt wants to merge 1 commit into
COVESA:masterfrom
SoundMatt:fix/unregister-context-v2-response-builder

Conversation

@SoundMatt
Copy link
Copy Markdown

Problem

dlt_daemon_control_message_unregister_context_v2 had four
distinct bugs (full analysis in #867):

  1. Heap underallocation via (uint8_t) truncation — the
    contextSize cast wrapped mod 256, undersizing msg.datasize
    for the subsequent memcpy chain.
  2. Uninitialised-stack disclosure to wireresp.apidlen and
    resp.ctidlen were never assigned, but were memcpy'd into the
    response, leaking two bytes of stack contents per unregister
    notification.
  3. memcpy from NULL with uninitialised lengthresp.apid /
    resp.ctid were set to NULL and the subsequent
    dlt_set_id_v2(resp.apid, ...) calls no-op'd; the variable-length
    memcpys then ran with a NULL source and a random length.
  4. Wrong wire output — the function's parameters were never
    copied into resp, so even when the daemon survived the call the
    notification carried garbage lengths and zero context bytes.

Reachable on every context unregistration (called from
dlt-daemon.c:5001).

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.

Notes

Closes #867.
Related: #866.

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant