Repository navigation
Fixed out-of-bounds read in string_replace with nested list elements - #6483
Dhanu-dynamic wants to merge 2 commits into
Conversation
StringReplaceWithLists(), the string_replace(..., "lists") policy
function, passes each element of the match and substitute arrays
directly to JsonPrimitiveGetAsString():
const char *m = JsonPrimitiveGetAsString(JsonAt(match_list, i));
const char *s = JsonPrimitiveGetAsString(JsonAt(substitute_list, i));
That function returns primitive.value, which in the JsonElement union
aliases container.children (a Seq *). For an element that is itself an
array or object it therefore returns the internal container pointer
reinterpreted as a char *, and SearchAndReplace() then reads it as a
NUL-terminated string. FnCallStringReplace() only checks that the two
arguments are arrays of equal length, not that every element is a
primitive, so a nested element reaches the unchecked call. These lists
commonly come from data containers (readjson, parsejson, mergedata,
module output), so JSON with a nested element triggers it.
With NDEBUG the read runs off the Seq object into adjacent heap memory
and those bytes, including internal pointers, end up in the returned
string; otherwise the assertion in JsonPrimitiveGetAsString() aborts,
which cf-promises hits during policy validation.
Reject any non-primitive element before the conversion, reusing the
function's existing warn-and-fail path.
Changelog: Title
Signed-off-by: Dhanraj E <dhanraj@labs.digiscrypt.com>
|
Thank you for submitting a PR! Maybe @craigcomstock can review this? |
|
@cf-bottom jenkins, please :) |
|
Sure, I triggered a build: Jenkins: https://ci.cfengine.com/job/pr-pipeline/15040/ Packages: http://buildcache.cfengine.com/packages/testing-pr/jenkins-pr-pipeline-15040/ |
larsewi
left a comment
There was a problem hiding this comment.
Thanks for the contribution 🚀 Some smaller nits
| if (JsonGetElementType(match_el) != JSON_ELEMENT_TYPE_PRIMITIVE || | ||
| JsonGetElementType(substitute_el) != JSON_ELEMENT_TYPE_PRIMITIVE) | ||
| { | ||
| Log(LOG_LEVEL_WARNING, |
There was a problem hiding this comment.
Can you change this to LOG_LEVEL_ERR?
There was a problem hiding this comment.
Done, it now logs at LOG_LEVEL_ERR.
| "Failed to replace with function '%s', string: '%s': " | ||
| "match and substitute lists must contain only strings", |
There was a problem hiding this comment.
Would be nice if you mentioned which index triggered this error.
There was a problem hiding this comment.
Added, the message now ends with (non-string element at index %zu) so you can see which entry in the lists was rejected. Pushed as a separate commit on top of the one already approved, happy to squash it in if you prefer.
The non-primitive element rejection in StringReplaceWithLists() now logs at LOG_LEVEL_ERR and reports the index of the offending element. Changelog: None Signed-off-by: Dhanraj E <dhanraj@labs.digiscrypt.com>
| const char *s = JsonPrimitiveGetAsString(JsonAt(substitute_list, i)); | ||
| JsonElement *match_el = JsonAt(match_list, i); | ||
| JsonElement *substitute_el = JsonAt(substitute_list, i); | ||
|
|
string_replace(string, match, substitute, "lists")passes every element of the match andsubstitute arrays to
JsonPrimitiveGetAsString()inStringReplaceWithLists(). For a primitivethat returns
primitive.value, but for a nested array or object that field aliasescontainer.children(aSeq *), so the internal pointer comes back as achar *andSearchAndReplace()reads it as a string.FnCallStringReplace()only checks that both argumentsare arrays of equal length, not that the elements are primitives, so a nested element reaches the
call. The lists are usually built from data containers (
readjson,parsejson,mergedata,module output), so JSON carrying a nested element reaches it.
In a release build (NDEBUG) this is an out-of-bounds heap read whose bytes, including internal
pointers, are copied into the returned string, and it can crash depending on the heap layout. In a
debug build the
assertinJsonPrimitiveGetAsString()aborts instead, whichcf-promiseshitswhile validating the policy.
The fix rejects any non-primitive element in
StringReplaceWithLists()before the conversion,using the warn-and-fail path the function already has.
Repro:
string_replace("abcb", '["b"]', '[["Z"]]', "lists"). Withjson.cbuilt-DNDEBUGandASan the result contains heap bytes where
bwas; in the normal debug buildcf-promises -caborts in
JsonPrimitiveGetAsString. The new unit test inevalfunction_test.caborts before thechange and passes after, and checks that a valid flat-list replace still returns
aZcZ.