Skip to content

Commit

Permalink
json: Pass lexical errors and limit violations to callback
Browse files Browse the repository at this point in the history
The callback to consume JSON values takes QObject *json, Error *err.
If both are null, the callback is supposed to make up an error by
itself.  This sucks.

qjson.c's consume_json() neglects to do so, which makes
qobject_from_json() null instead of failing.  I consider that a bug.

The culprit is json_message_process_token(): it passes two null
pointers when it runs into a lexical error or a limit violation.  Fix
it to pass a proper Error object then.  Update the callbacks:

* monitor.c's handle_qmp_command(): the code to make up an error is
  now dead, drop it.

* qga/main.c's process_event(): lumps the "both null" case together
  with the "not a JSON object" case.  The former is now gone.  The
  error message "Invalid JSON syntax" is misleading for the latter.
  Improve it to "Input must be a JSON object".

* qobject/qjson.c's consume_json(): no update; check-qjson
  demonstrates qobject_from_json() now sets an error on lexical
  errors, but still doesn't on some other errors.

* tests/libqtest.c's qmp_response(): the Error object is now reliable,
  so use it to improve the error message.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20180823164025.12553-40-armbru@redhat.com>
  • Loading branch information
Markus Armbruster committed Aug 24, 2018
1 parent 2cbd15a commit 84a56f3
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 25 deletions.
3 changes: 0 additions & 3 deletions include/qapi/qmp/qerror.h
Expand Up @@ -61,9 +61,6 @@
#define QERR_IO_ERROR \
"An IO error has occurred"

#define QERR_JSON_PARSING \
"Invalid JSON syntax"

#define QERR_MIGRATION_ACTIVE \
"There's a migration process in progress"

Expand Down
5 changes: 1 addition & 4 deletions monitor.c
Expand Up @@ -4262,10 +4262,7 @@ static void handle_qmp_command(void *opaque, QObject *req, Error *err)
QDict *qdict;
QMPRequest *req_obj;

if (!req && !err) {
/* json_parser_parse() sucks: can fail without setting @err */
error_setg(&err, QERR_JSON_PARSING);
}
assert(!req != !err);

qdict = qobject_to(QDict, req);
if (qdict) {
Expand Down
3 changes: 2 additions & 1 deletion qga/main.c
Expand Up @@ -603,12 +603,13 @@ static void process_event(void *opaque, QObject *obj, Error *err)
int ret;

g_debug("process_event: called");
assert(!obj != !err);
if (err) {
goto err;
}
req = qobject_to(QDict, obj);
if (!req) {
error_setg(&err, QERR_JSON_PARSING);
error_setg(&err, "Input must be a JSON object");
goto err;
}
if (!qdict_haskey(req, "execute")) {
Expand Down
3 changes: 1 addition & 2 deletions qobject/json-lexer.c
Expand Up @@ -334,8 +334,7 @@ static void json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush)
/* XXX: To avoid having previous bad input leaving the parser in an
* unresponsive state where we consume unpredictable amounts of
* subsequent "good" input, percolate this error state up to the
* tokenizer/parser by forcing a NULL object to be emitted, then
* reset state.
* parser by emitting a JSON_ERROR token, then reset lexer state.
*
* Also note that this handling is required for reliable channel
* negotiation between QMP and the guest agent, since chr(0xFF)
Expand Down
22 changes: 16 additions & 6 deletions qobject/json-streamer.c
Expand Up @@ -13,6 +13,7 @@

#include "qemu/osdep.h"
#include "qemu-common.h"
#include "qapi/error.h"
#include "qapi/qmp/json-lexer.h"
#include "qapi/qmp/json-parser.h"
#include "qapi/qmp/json-streamer.h"
Expand Down Expand Up @@ -57,6 +58,7 @@ void json_message_process_token(JSONLexer *lexer, GString *input,
parser->bracket_count--;
break;
case JSON_ERROR:
error_setg(&err, "JSON parse error, stray '%s'", input->str);
goto out_emit;
default:
break;
Expand All @@ -82,12 +84,20 @@ void json_message_process_token(JSONLexer *lexer, GString *input,
goto out_emit;
}

if (parser->token_size > MAX_TOKEN_SIZE ||
g_queue_get_length(parser->tokens) > MAX_TOKEN_COUNT ||
parser->bracket_count + parser->brace_count > MAX_NESTING) {
/* Security consideration, we limit total memory allocated per object
* and the maximum recursion depth that a message can force.
*/
/*
* Security consideration, we limit total memory allocated per object
* and the maximum recursion depth that a message can force.
*/
if (parser->token_size > MAX_TOKEN_SIZE) {
error_setg(&err, "JSON token size limit exceeded");
goto out_emit;
}
if (g_queue_get_length(parser->tokens) > MAX_TOKEN_COUNT) {
error_setg(&err, "JSON token count limit exceeded");
goto out_emit;
}
if (parser->bracket_count + parser->brace_count > MAX_NESTING) {
error_setg(&err, "JSON nesting depth limit exceeded");
goto out_emit;
}

Expand Down
15 changes: 8 additions & 7 deletions tests/check-qjson.c
Expand Up @@ -1021,6 +1021,7 @@ static void interpolation_unknown(void)
}
g_test_trap_subprocess(NULL, 0, 0);
g_test_trap_assert_failed();
g_test_trap_assert_stderr("*Unexpected error*stray '%x'*");
}

static void interpolation_string(void)
Expand Down Expand Up @@ -1296,23 +1297,23 @@ static void junk_input(void)
QObject *obj;

obj = qobject_from_json("@", &err);
g_assert(!err); /* BUG */
error_free_or_abort(&err);
g_assert(obj == NULL);

obj = qobject_from_json("{\x01", &err);
g_assert(!err); /* BUG */
error_free_or_abort(&err);
g_assert(obj == NULL);

obj = qobject_from_json("[0\xFF]", &err);
error_free_or_abort(&err);
g_assert(obj == NULL);

obj = qobject_from_json("00", &err);
g_assert(!err); /* BUG */
error_free_or_abort(&err);
g_assert(obj == NULL);

obj = qobject_from_json("[1e", &err);
g_assert(!err); /* BUG */
error_free_or_abort(&err);
g_assert(obj == NULL);

obj = qobject_from_json("truer", &err);
Expand All @@ -1324,23 +1325,23 @@ static void unterminated_string(void)
{
Error *err = NULL;
QObject *obj = qobject_from_json("\"abc", &err);
g_assert(!err); /* BUG */
error_free_or_abort(&err);
g_assert(obj == NULL);
}

static void unterminated_sq_string(void)
{
Error *err = NULL;
QObject *obj = qobject_from_json("'abc", &err);
g_assert(!err); /* BUG */
error_free_or_abort(&err);
g_assert(obj == NULL);
}

static void unterminated_escape(void)
{
Error *err = NULL;
QObject *obj = qobject_from_json("\"abc\\\"", &err);
g_assert(!err); /* BUG */
error_free_or_abort(&err);
g_assert(obj == NULL);
}

Expand Down
7 changes: 5 additions & 2 deletions tests/libqtest.c
Expand Up @@ -450,8 +450,11 @@ static void qmp_response(void *opaque, QObject *obj, Error *err)
{
QMPResponseParser *qmp = opaque;

if (!obj) {
fprintf(stderr, "QMP JSON response parsing failed\n");
assert(!obj != !err);

if (err) {
error_prepend(&err, "QMP JSON response parsing failed: ");
error_report_err(err);
abort();
}

Expand Down

0 comments on commit 84a56f3

Please sign in to comment.