From: Markus Armbruster <armbru@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PULL 39/58] json: Pass lexical errors and limit violations to callback
Date: Fri, 24 Aug 2018 21:31:47 +0200 [thread overview]
Message-ID: <20180824193206.25475-40-armbru@redhat.com> (raw)
In-Reply-To: <20180824193206.25475-1-armbru@redhat.com>
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>
---
include/qapi/qmp/qerror.h | 3 ---
monitor.c | 5 +----
qga/main.c | 3 ++-
qobject/json-lexer.c | 3 +--
qobject/json-streamer.c | 22 ++++++++++++++++------
tests/check-qjson.c | 15 ++++++++-------
tests/libqtest.c | 7 +++++--
7 files changed, 33 insertions(+), 25 deletions(-)
diff --git a/include/qapi/qmp/qerror.h b/include/qapi/qmp/qerror.h
index c82360f429..145571f618 100644
--- a/include/qapi/qmp/qerror.h
+++ b/include/qapi/qmp/qerror.h
@@ -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"
diff --git a/monitor.c b/monitor.c
index 08f799a7bb..3dbdcb5190 100644
--- a/monitor.c
+++ b/monitor.c
@@ -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) {
diff --git a/qga/main.c b/qga/main.c
index 2fc49d00d8..b74e1241ef 100644
--- a/qga/main.c
+++ b/qga/main.c
@@ -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")) {
diff --git a/qobject/json-lexer.c b/qobject/json-lexer.c
index 96fe13621d..7c31c2c8ff 100644
--- a/qobject/json-lexer.c
+++ b/qobject/json-lexer.c
@@ -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)
diff --git a/qobject/json-streamer.c b/qobject/json-streamer.c
index a373e0114a..e372ecc895 100644
--- a/qobject/json-streamer.c
+++ b/qobject/json-streamer.c
@@ -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"
@@ -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;
@@ -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;
}
diff --git a/tests/check-qjson.c b/tests/check-qjson.c
index 604886a1a2..d6fda0786f 100644
--- a/tests/check-qjson.c
+++ b/tests/check-qjson.c
@@ -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)
@@ -1296,11 +1297,11 @@ 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);
@@ -1308,11 +1309,11 @@ static void junk_input(void)
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);
@@ -1324,7 +1325,7 @@ 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);
}
@@ -1332,7 +1333,7 @@ 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);
}
@@ -1340,7 +1341,7 @@ 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);
}
diff --git a/tests/libqtest.c b/tests/libqtest.c
index 1f3b0cb1b1..5973a67652 100644
--- a/tests/libqtest.c
+++ b/tests/libqtest.c
@@ -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();
}
--
2.17.1
next prev parent reply other threads:[~2018-08-24 19:32 UTC|newest]
Thread overview: 60+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-08-24 19:31 [Qemu-devel] [PULL 00/58] QObject patches for 2018-08-24 Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 01/58] docs/interop/qmp-spec: How to force known good parser state Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 02/58] check-qjson: Cover multiple JSON objects in same string Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 03/58] check-qjson: Cover blank and lexically erroneous input Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 04/58] check-qjson: Cover whitespace more thoroughly Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 05/58] qmp-cmd-test: Split off qmp-test Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 06/58] qmp-test: Cover syntax and lexical errors Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 07/58] test-qga: Clean up how we test QGA synchronization Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 08/58] check-qjson: Cover escaped characters more thoroughly, part 1 Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 09/58] check-qjson: Streamline escaped_string()'s test strings Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 10/58] check-qjson: Cover escaped characters more thoroughly, part 2 Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 11/58] check-qjson: Consolidate partly redundant string tests Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 12/58] check-qjson: Cover UTF-8 in single quoted strings Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 13/58] check-qjson: Simplify utf8_string() Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 14/58] check-qjson: Fix utf8_string() to test all invalid sequences Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 15/58] check-qjson qmp-test: Cover control characters more thoroughly Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 16/58] check-qjson: Cover interpolation " Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 17/58] json: Fix lexer to include the bad character in JSON_ERROR token Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 18/58] json: Reject unescaped control characters Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 19/58] json: Revamp lexer documentation Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 20/58] json: Tighten and simplify qstring_from_escaped_str()'s loop Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 21/58] check-qjson: Document we expect invalid UTF-8 to be rejected Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 22/58] json: Reject invalid UTF-8 sequences Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 23/58] json: Report first rather than last parse error Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 24/58] json: Leave rejecting invalid UTF-8 to parser Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 25/58] json: Accept overlong \xC0\x80 as U+0000 ("modified UTF-8") Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 26/58] json: Leave rejecting invalid escape sequences to parser Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 27/58] json: Simplify parse_string() Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 28/58] json: Reject invalid \uXXXX, fix \u0000 Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 29/58] json: Fix \uXXXX for surrogate pairs Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 30/58] check-qjson: Fix and enable utf8_string()'s disabled part Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 31/58] json: remove useless return value from lexer/parser Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 32/58] json-parser: simplify and avoid JSONParserContext allocation Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 33/58] json: Have lexer call streamer directly Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 34/58] json: Redesign the callback to consume JSON values Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 35/58] json: Don't pass null @tokens to json_parser_parse() Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 36/58] json: Don't create JSON_ERROR tokens that won't be used Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 37/58] json: Rename token JSON_ESCAPE & friends to JSON_INTERP Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 38/58] json: Treat unwanted interpolation as lexical error Markus Armbruster
2018-08-24 19:31 ` Markus Armbruster [this message]
2018-08-24 19:31 ` [Qemu-devel] [PULL 40/58] json: Leave rejecting invalid interpolation to parser Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 41/58] json: Replace %I64d, %I64u by %PRId64, %PRIu64 Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 42/58] json: Improve names of lexer states related to numbers Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 43/58] qjson: Fix qobject_from_json() & friends for multiple values Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 44/58] json: Fix latent parser aborts at end of input Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 45/58] json: Fix streamer not to ignore trailing unterminated structures Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 46/58] json: Assert json_parser_parse() consumes all tokens on success Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 47/58] qjson: Have qobject_from_json() & friends reject empty and blank Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 48/58] json: Enforce token count and size limits more tightly Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 49/58] json: Streamline json_message_process_token() Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 50/58] json: Unbox tokens queue in JSONMessageParser Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 51/58] json: Make JSONToken opaque outside json-parser.c Markus Armbruster
2018-08-24 19:32 ` [Qemu-devel] [PULL 52/58] qobject: Drop superfluous includes of qemu-common.h Markus Armbruster
2018-08-24 19:32 ` [Qemu-devel] [PULL 53/58] json: Clean up headers Markus Armbruster
2018-08-24 19:32 ` [Qemu-devel] [PULL 54/58] tests/drive_del-test: Fix harmless JSON interpolation bug Markus Armbruster
2018-08-24 19:32 ` [Qemu-devel] [PULL 55/58] json: Keep interpolation state in JSONParserContext Markus Armbruster
2018-08-24 19:32 ` [Qemu-devel] [PULL 56/58] json: Improve safety of qobject_from_jsonf_nofail() & friends Markus Armbruster
2018-08-24 19:32 ` [Qemu-devel] [PULL 57/58] json: Support %% in JSON strings when interpolating Markus Armbruster
2018-08-24 19:32 ` [Qemu-devel] [PULL 58/58] json: Update references to RFC 7159 to RFC 8259 Markus Armbruster
2018-08-25 9:58 ` [Qemu-devel] [PULL 00/58] QObject patches for 2018-08-24 Peter Maydell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180824193206.25475-40-armbru@redhat.com \
--to=armbru@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).