From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47067) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a1vy1-0004R8-6i for qemu-devel@nongnu.org; Thu, 26 Nov 2015 07:48:06 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a1vxz-0003XY-Sf for qemu-devel@nongnu.org; Thu, 26 Nov 2015 07:48:05 -0500 Received: from mx1.redhat.com ([209.132.183.28]:51069) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a1vxz-0003XL-Nh for qemu-devel@nongnu.org; Thu, 26 Nov 2015 07:48:03 -0500 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (Postfix) with ESMTPS id 69D201B282B for ; Thu, 26 Nov 2015 12:48:03 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-37.ams2.redhat.com [10.36.116.37]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id tAQCm1eZ008095 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO) for ; Thu, 26 Nov 2015 07:48:02 -0500 From: Markus Armbruster Date: Thu, 26 Nov 2015 13:47:58 +0100 Message-Id: <1448542078-11690-14-git-send-email-armbru@redhat.com> In-Reply-To: <1448542078-11690-1-git-send-email-armbru@redhat.com> References: <1448542078-11690-1-git-send-email-armbru@redhat.com> Subject: [Qemu-devel] [PULL for-2.5 13/13] qjson: Limit number of tokens in addition to total size List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Commit 29c75dd "json-streamer: limit the maximum recursion depth and maximum token count" attempts to guard against excessive heap usage by limiting total token size (it says "token count", but that's a lie). Total token size is a rather imprecise predictor of heap usage: many small tokens use more space than few large tokens with the same input size, because there's a constant per-token overhead: 37 bytes on my system. Tighten this up: limit the token count to 2Mi. Chosen to roughly match the 64MiB total token size limit. Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Message-Id: <1448486613-17634-13-git-send-email-armbru@redhat.com> --- qobject/json-streamer.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/qobject/json-streamer.c b/qobject/json-streamer.c index e87230d..a4db4b8 100644 --- a/qobject/json-streamer.c +++ b/qobject/json-streamer.c @@ -16,6 +16,7 @@ #include "qapi/qmp/json-streamer.h" #define MAX_TOKEN_SIZE (64ULL << 20) +#define MAX_TOKEN_COUNT (2ULL << 20) #define MAX_NESTING (1ULL << 10) static void json_message_free_tokens(JSONMessageParser *parser) @@ -68,6 +69,7 @@ static void json_message_process_token(JSONLexer *lexer, GString *input, parser->bracket_count == 0)) { goto out_emit; } else 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. -- 2.4.3