Public research

libucl

Embedded-NUL string-copy out-of-bounds read

Additional upstream fix · no assigned CVE/GHSA · Public source snapshot: November 12, 2026

Original portfolio report · Source reference

Shared credits and the scope of my investigation are documented below.

An application can expose heap bytes or crash if it accepts a string containing an embedded NUL, creates a length-delimited libucl string, copies that object and emits the copy. Mallory must influence the string bytes reaching that application-specific sequence. This is a library memory-safety failure, not a demonstrated network endpoint, arbitrary memory read or code-execution exploit.

The copied object retains the original string length, but its new allocation stops at the first NUL. JSON emission trusts the retained length and reads beyond that allocation. Source inspection confirms this sequence in releases 0.9.0, 0.9.1, 0.9.2, 0.9.3 and 0.9.4. The upstream fix was merged on October 3, 2026 in PR #401. No fixed numbered release was established in this review.

I reported the finding as the-vibe-dev in issue #385. This write-up checks the public reproducer, its published AddressSanitizer trace, released source and merged fix; it does not claim a new local execution of that reproducer. The fix was authored by afonsojanu, not by me. No CVE or GHSA assignment is claimed here.

libucl is a C library for working with configuration objects and serialized data. Its strings carry an explicit length, so an embedded NUL can be valid data rather than the end of the value. A deep copy must preserve both the bytes and their length.

The relevant topology is one process embedding libucl. The host application passes Mallory-influenced bytes to ucl_object_fromlstring(), calls ucl_object_copy() and later calls ucl_object_emit() on the copy. The reproducer uses the ordinary JSON emitter and default library allocation behavior; it needs no account, server configuration or concurrency. Those API calls do not establish that any particular deployed application exposes this path remotely.

The expected boundary is straightforward: serialization of a copied string should read only the string's own allocation. Memory belonging to other allocations must not become part of the serialized value.

We can follow the four-byte input {'A', '\0', 'B', 'C'} through the same object operations used in the public report.

In 0.9.4, src/ucl_util.c, ucl_object_fromlstring() delegates to ucl_object_fromstring_common() with UCL_STRING_RAW. The raw-string path copies the explicit byte range and records obj->len = end - start. Its copy helper, ucl_strlcpy_unsafe(), uses memcpy, not a NUL-terminated string loop. The original allocation therefore contains all four input bytes and an additional terminating NUL.

ucl_object_copy_internal() first copies the object structure, including its length:

memcpy(new, other, sz);

It then duplicates the owned string storage using a NUL-terminated operation:

if (other->trash_stack[UCL_TRASH_VALUE] != NULL) {
    new->trash_stack[UCL_TRASH_VALUE] =
        UCL_STRDUP(other->trash_stack[UCL_TRASH_VALUE]);
    if (new->type == UCL_STRING) {
        new->value.sv = new->trash_stack[UCL_TRASH_VALUE];
    }
}

UCL_STRDUP stops at the embedded NUL. For this input, the copy points to an allocation containing A and its terminator, while the object still describes a four-byte value. Allocation success is not a guard against this mismatch; it creates the undersized allocation used by the subsequent operation.

In 0.9.4, src/ucl_emitter.c, ucl_emitter_common_elt() passes the string pointer and retained length to the JSON writer:

ucl_elt_string_write_json(obj->value.sv, obj->len, ctx);

The writer in src/ucl_emitter_utils.c advances through the supplied byte count. It is correctly treating the value as length-delimited; the earlier copy operation broke the pointer/length invariant. Reading the third and fourth bytes therefore exceeds the copied allocation. No scheduling or use-after-free condition is needed.

The history separates the old copying mistake from the specific reported entry point. The deep-copy implementation introduced NUL-terminated value duplication in July 2014, and the subsequent object-copy correction redirected string values to that duplicated storage. In March 2022, the string-construction change switched ucl_object_fromlstring() from escaped to raw string storage. That exposed the reported embedded-NUL sequence through this API.

Release 0.8.2 still uses escaped construction for that entry point; 0.9.0 is the earliest published tag verified here with the raw constructor and vulnerable copy/emission sequence. Every listed 0.9.x release snapshot was checked. This is not a claim that older versions are safe through every alternative object-construction API. The immediate parent of the merged fix also retains the vulnerable value-copy path; distribution-specific patches and separate downstream forks were not assessed.

The established primitive is an out-of-bounds heap read during serialization. The public issue contains an AddressSanitizer trace identifying ucl_elt_string_write_json() as the read site and the string duplication inside ucl_object_copy_internal() as the allocation site. It also reports a non-sanitized synthetic-marker experiment in which emitted output included separately allocated marker bytes. These are published report observations, not fresh runs performed for this write-up.

Availability impact depends on allocator behavior and whether the invalid read faults. Disclosure requires the emitted output to reach someone who should not see the surrounding process memory; the API failure alone does not establish that exposure in a real application. The marker experiment supports a possible disclosure route, but does not establish attacker-selected addresses, reliable recovery of secrets or code execution.

Meaningful controls include emitting the original object, copying a string without an embedded NUL and repeating the exact sequence after the fix. The PR author reports reproducing the sanitizer failure, restoring it by reverting the fix, and passing the regression and full sanitizer-enabled test suite with the fix restored. I reviewed that published account and its source changes, but did not independently run those controls.

The original public issue #385 contains the complete C driver and sanitizer build command. It identifies release 0.9.2 and a specific development revision as tested. Its decisive sequence is:

const char payload[] = {'A', '\0', 'B', 'C'};
ucl_object_t *obj = ucl_object_fromlstring(payload, sizeof(payload));
ucl_object_t *copy = ucl_object_copy(obj);
char *out = ucl_object_emit(copy, UCL_EMIT_JSON);

This excerpt explains the trigger; it is not a complete standalone program. The linked driver includes allocation checks and releases the emitted buffer and both objects. Its inspected sanitizer trace reports heap-buffer-overflow and READ of size 1. No new executable, crash log or fixed-target result is supplied with this write-up.

Reproduction should use a disposable local build. The trigger deliberately causes an invalid memory read and may terminate the process. It does not require contacting or testing a public service.

Merged fix 49d08fd changes string-value duplication to allocate new->len + 1, copy exactly new->len bytes and append a terminating NUL. Other trash-stack uses retain their separate duplication path. This preserves the explicit extent rather than shortening the value at an embedded NUL.

The accompanying tests/test_generate.c regression creates the same four-byte value, copies it, checks the copied length and compares every byte with memcmp. Direct JSON-emission coverage under AddressSanitizer would additionally exercise the original failing sink. Nearby cases worth covering include leading and trailing embedded NULs and multiple NULs.

Use a build containing the merged correction or a verified downstream backport. Release 0.9.4 still contains the vulnerable code; no first fixed numbered release is asserted. If an upgrade is temporarily unavailable, applications can avoid the affected copy sequence or reject embedded NULs where their own input format forbids them. Rejecting NULs changes accepted input and is not a substitute for correct binary-safe copying in the library.

The finding connects a valid length-delimited string to an invalid copied allocation: construction preserves the bytes, copying truncates the backing storage, and emission trusts the unchanged length. The reported sequence is source-confirmed in 0.9.0–0.9.4 and corrected by the merged upstream fix. My contribution was the public report; the accepted patch belongs to its upstream author. Practical disclosure exposure remains dependent on the embedding application's input and output paths.