qemu/qobject
Markus Armbruster 16a4859921 json: Improve safety of qobject_from_jsonf_nofail() & friends
The JSON parser optionally supports interpolation.  This is used to
build QObjects by parsing string templates.  The templates are C
literals, so parse errors (such as invalid interpolation
specifications) are actually programming errors.  Consequently, the
functions providing parsing with interpolation
(qobject_from_jsonf_nofail(), qobject_from_vjsonf_nofail(),
qdict_from_jsonf_nofail(), qdict_from_vjsonf_nofail()) pass
&error_abort to the parser.

However, there's another, more dangerous kind of programming error:
since we use va_arg() to get the value to interpolate, behavior is
undefined when the variable argument isn't consistent with the
interpolation specification.

The same problem exists with printf()-like functions, and the solution
is to have the compiler check consistency.  This is what
GCC_FMT_ATTR() is about.

To enable this type checking for interpolation as well, we carefully
chose our interpolation specifications to match printf conversion
specifications, and decorate functions parsing templates with
GCC_FMT_ATTR().

Note that this only protects against undefined behavior due to type
errors.  It can't protect against use of invalid interpolation
specifications that happen to be valid printf conversion
specifications.

However, there's still a gaping hole in the type checking: GCC
recognizes '%' as start of printf conversion specification anywhere in
the template, but the parser recognizes it only outside JSON strings.
For instance, if someone were to pass a "{ '%s': %d }" template, GCC
would require a char * and an int argument, but the parser would
va_arg() only an int argument, resulting in undefined behavior.

Avoid undefined behavior by catching the programming error at run
time: have the parser recognize and reject '%' in JSON strings.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20180823164025.12553-57-armbru@redhat.com>
2018-08-24 20:26:37 +02:00
..
block-qdict.c qdict: Make qdict_extract_subqdict() accept dst = NULL 2018-08-15 12:50:39 +02:00
json-lexer.c json: Clean up headers 2018-08-24 20:26:37 +02:00
json-parser-int.h json: Clean up headers 2018-08-24 20:26:37 +02:00
json-parser.c json: Improve safety of qobject_from_jsonf_nofail() & friends 2018-08-24 20:26:37 +02:00
json-streamer.c json: Clean up headers 2018-08-24 20:26:37 +02:00
Makefile.objs qobject: Move block-specific qdict code to block-qdict.c 2018-06-15 14:49:44 +02:00
qbool.c qobject: Drop superfluous includes of qemu-common.h 2018-08-24 20:26:37 +02:00
qdict.c qobject: Move block-specific qdict code to block-qdict.c 2018-06-15 14:49:44 +02:00
qjson.c json: Clean up headers 2018-08-24 20:26:37 +02:00
qlist.c qobject: Drop superfluous includes of qemu-common.h 2018-08-24 20:26:37 +02:00
qlit.c qapi: Replace qobject_to_X(o) by qobject_to(X, o) 2018-03-19 14:58:36 -05:00
qnull.c qobject: Drop superfluous includes of qemu-common.h 2018-08-24 20:26:37 +02:00
qnum.c qobject: Drop superfluous includes of qemu-common.h 2018-08-24 20:26:37 +02:00
qobject.c qobject: Drop superfluous includes of qemu-common.h 2018-08-24 20:26:37 +02:00
qstring.c qobject: Drop superfluous includes of qemu-common.h 2018-08-24 20:26:37 +02:00