Skip to content

Use-after-free in sentry__value_merge_objects on allocation failure #1538

@jpnurmi

Description

@jpnurmi

Title: Use-after-free in sentry__value_merge_objects on allocation failure

sentry__value_merge_objects() can corrupt the source object when sentry_value_set_by_key() fails due to OOM. The source object is left with a dangling pointer to freed memory.

The incref happens after sentry_value_set_by_key:

} else if (sentry_value_is_null(dst_val)) {
if (sentry_value_set_by_key(dst, key, src_val) != 0) {
return 1;
}
sentry_value_incref(src_val);

But set_by_key decrefs the value on failure:

fail:
sentry_value_decref(v);
return 1;

If the source value has refcount 1, the decref frees it and the source object is left with a dangling pointer.

sentry__value_clone does it correctly — incref before set_by_key:

sentry_value_incref(obj->pairs[i].v);
sentry_value_set_by_key(rv, obj->pairs[i].k, obj->pairs[i].v);

The affected function is called from sentry__scope_apply_to_event to merge the global scope's tags, extra, and propagation context into events. On OOM, this corrupts the global scope, affecting all subsequent event captures.

Possible fix: swap the incref to before set_by_key, matching the pattern in sentry__value_clone.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions