Repository navigation
generate: reject a NUL character inside a JSON string - #181
Merged
Merged
Conversation
JSON allows \u0000 inside a string, and json-c keeps it, but the parsed
value is copied with strdup(), so everything after the first NUL is
silently dropped:
"options": ["nosuid\u0000,noexec"]
parses without any error and yields a single "nosuid" option, with
"noexec" gone. The same happens to every string in the document:
paths, process arguments and environment, hook paths, annotations.
A runtime has no way to notice, as by the time it sees the value it is
a perfectly valid, if shorter, C string.
Add dup_json_string(), which copies a JSON string, rejecting it if its
JSON length does not match its C string length, and use it for every
string the generated code and the string maps read.
Note that json-c truncates object keys at the first NUL by itself, so
those cannot be checked here.
Add tests/test-17.c.
Reported-in: containers/crun#2105
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
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.
JSON allows
\u0000inside a string, and json-c keeps it, but the parsed value is copied withstrdup(), so everything after the first NUL is silently dropped:parses without any error and yields a single
nosuidoption, withnoexecgone. The same happens to every string in the document: paths, process arguments and environment, hook paths, annotations. A runtime has no way to notice, as by the time it sees the value it is a perfectly valid, if shorter, C string. This was reported against crun in containers/crun#2105 (runc and youki reject such a config).This adds
dup_json_string(), which copies a JSON string, rejecting it if its JSON length does not match its C string length, and uses it for every string the generated code reads (165 call sites in the runtime and image specs), plus the string values ofjson_map_string_stringandjson_map_int_string.Object keys are not covered: json-c truncates them at the first NUL by itself, so there is nothing left to check by the time libocispec sees them.
tests/test-17.ccovers a plain string, a string inside an array of objects, arrays of strings, and a string map; without the change all six cases fail.Verified:
make check17/17,make distcheck.