fix: Guard span baggage inheritance against malformed parent zval#3943
Open
jdani-airalo wants to merge 1 commit into
Open
fix: Guard span baggage inheritance against malformed parent zval#3943jdani-airalo wants to merge 1 commit into
jdani-airalo wants to merge 1 commit into
Conversation
ddtrace_inherit_span_properties copies the parent span's baggage property with ZVAL_COPY_DEREF, which runs GC_ADDREF unconditionally when the source is flagged refcounted. When the parent baggage zval carries the refcounted flag but a NULL counted pointer, GC_ADDREF dereferences NULL and the process takes a SIGSEGV during span creation. Skip the copy and fall back to the property's declared empty-array default when the source zval is in that broken state. Valid baggage is unaffected. Signed-off-by: Dani Jimenez <daniel.jimenez@airalo.com>
Collaborator
|
Hey @jdani-airalo. the big question is - how does that become NULL. This is an invalid state and probably direct access to I was unable to pin down a possible path to get this to crash. Do have any reproducer? Any information your AI could extract from the core dump / source code to reconstruct a path to getting this crashing? This PR is a band-aid, not a proper fix :-( |
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.
Description
ddtrace_inherit_span_properties(ext/serializer.c) copies several parent spanproperties into a new span with
ZVAL_COPY_DEREF. That macro runsGC_ADDREFonthe source whenever its type carries the refcounted flag, without checking that the
counted pointer is valid.
When the parent's
baggageproperty is a zval whose type is flagged refcounted butwhose
Z_COUNTEDpointer isNULL,GC_ADDREFdereferencesNULLand the processcrashes with a SIGSEGV during span creation.
Crash signature observed in production (PHP 8.3, NTS,
fpm-fcgi, tracer 1.20.0):GC_ADDREFincrement (addl $0x1,(%rdx)with%rdx = 0).addr2lineon the faulting offset points intoddtrace_inherit_span_properties.The function issues five back-to-back
zval_ptr_dtor(); ZVAL_COPY_DEREF();sequences; at
-O2these identical tails fold together, so the reported linelands on the first copy regardless of which property actually faults.
(
DD_TRACE_PROPAGATION_STYLE=datadog,tracecontext). The other inheritedproperties (
service,type,env,version) are long-stable strings;baggageis the only inherited field populated from inbound distributed-tracing headers and
managed as an array. That points at the baggage copy as the faulting one.
Change
Guard the baggage copy. If the parent baggage zval is flagged refcounted but holds a
NULLcounted pointer, skip the copy and initialize the new span's baggage to itsdeclared empty-array default instead of dereferencing
NULL. Valid baggage is copiedexactly as before, so propagation behavior is unchanged for well-formed input.
The guard is scoped to the baggage property only, where the evidence points, and adds
no PHP-version or ZTS-specific branches.
Notes
This is a defensive fix at the crash site. The producer path that leaves the baggage
zval in the refcounted-with-
NULLstate is not pinned down here: the visible baggagewrite paths (header merge,
apply_baggage_span_tags, the serializer copy viazend_hash_copy(..., zval_add_ref)) look refcount-balanced. The crash is reproduciblein production under load but not yet as a deterministic unit test, so no regression
test is included rather than adding a flaky one. Happy to add a harness test if you can
point at a reliable trigger, or to move the guard to the producer if you can identify
the exact path.
Reviewer checklist