34b321
From 60ad1bad25ea99c538b745ff95e6e0a877d37d1f Mon Sep 17 00:00:00 2001
34b321
From: Markus Armbruster <armbru@redhat.com>
34b321
Date: Wed, 27 Jul 2016 07:35:11 +0200
34b321
Subject: [PATCH 13/16] qjson: Limit number of tokens in addition to total size
34b321
34b321
RH-Author: Markus Armbruster <armbru@redhat.com>
34b321
Message-id: <1469604913-12442-15-git-send-email-armbru@redhat.com>
34b321
Patchwork-id: 71476
34b321
O-Subject: [RHEL-7.3 qemu-kvm PATCH v2 13/15] qjson: Limit number of tokens in addition to total size
34b321
Bugzilla: 1276036
34b321
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
34b321
RH-Acked-by: John Snow <jsnow@redhat.com>
34b321
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
34b321
34b321
Commit 29c75dd "json-streamer: limit the maximum recursion depth and
34b321
maximum token count" attempts to guard against excessive heap usage by
34b321
limiting total token size (it says "token count", but that's a lie).
34b321
34b321
Total token size is a rather imprecise predictor of heap usage: many
34b321
small tokens use more space than few large tokens with the same input
34b321
size, because there's a constant per-token overhead: 37 bytes on my
34b321
system.
34b321
34b321
Tighten this up: limit the token count to 2Mi.  Chosen to roughly
34b321
match the 64MiB total token size limit.
34b321
34b321
Signed-off-by: Markus Armbruster <armbru@redhat.com>
34b321
Reviewed-by: Eric Blake <eblake@redhat.com>
34b321
Message-Id: <1448486613-17634-13-git-send-email-armbru@redhat.com>
34b321
(cherry picked from commit df649835fe48f635a93316fdefe96ced7189316e)
34b321
Signed-off-by: Markus Armbruster <armbru@redhat.com>
34b321
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
34b321
---
34b321
 qobject/json-streamer.c | 2 ++
34b321
 1 file changed, 2 insertions(+)
34b321
34b321
diff --git a/qobject/json-streamer.c b/qobject/json-streamer.c
34b321
index e87230d..a4db4b8 100644
34b321
--- a/qobject/json-streamer.c
34b321
+++ b/qobject/json-streamer.c
34b321
@@ -16,6 +16,7 @@
34b321
 #include "qapi/qmp/json-streamer.h"
34b321
 
34b321
 #define MAX_TOKEN_SIZE (64ULL << 20)
34b321
+#define MAX_TOKEN_COUNT (2ULL << 20)
34b321
 #define MAX_NESTING (1ULL << 10)
34b321
 
34b321
 static void json_message_free_tokens(JSONMessageParser *parser)
34b321
@@ -68,6 +69,7 @@ static void json_message_process_token(JSONLexer *lexer, GString *input,
34b321
          parser->bracket_count == 0)) {
34b321
         goto out_emit;
34b321
     } else if (parser->token_size > MAX_TOKEN_SIZE ||
34b321
+               g_queue_get_length(parser->tokens) > MAX_TOKEN_COUNT ||
34b321
                parser->bracket_count + parser->brace_count > MAX_NESTING) {
34b321
         /* Security consideration, we limit total memory allocated per object
34b321
          * and the maximum recursion depth that a message can force.
34b321
-- 
34b321
1.8.3.1
34b321