From patchwork Thu Nov 19 15:29:08 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Armbruster X-Patchwork-Id: 546538 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 7A531141434 for ; Fri, 20 Nov 2015 02:32:19 +1100 (AEDT) Received: from localhost ([::1]:42503 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZzRC5-0002Cb-K0 for incoming@patchwork.ozlabs.org; Thu, 19 Nov 2015 10:32:17 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35593) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZzR98-0005VK-5g for qemu-devel@nongnu.org; Thu, 19 Nov 2015 10:29:20 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZzR94-00035Y-Vh for qemu-devel@nongnu.org; Thu, 19 Nov 2015 10:29:14 -0500 Received: from mx1.redhat.com ([209.132.183.28]:49221) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZzR94-00034v-Nm for qemu-devel@nongnu.org; Thu, 19 Nov 2015 10:29:10 -0500 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id 738FE9090F for ; Thu, 19 Nov 2015 15:29:10 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-43.ams2.redhat.com [10.36.116.43]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id tAJFT8nG023624 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Thu, 19 Nov 2015 10:29:10 -0500 Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 664CA30052EE; Thu, 19 Nov 2015 16:29:08 +0100 (CET) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Thu, 19 Nov 2015 16:29:08 +0100 Message-Id: <1447946948-12489-5-git-send-email-armbru@redhat.com> In-Reply-To: <1447946948-12489-1-git-send-email-armbru@redhat.com> References: <1447946948-12489-1-git-send-email-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: lcapitulino@redhat.com Subject: [Qemu-devel] [PATCH v2 4/4] json-streamer: Limit number of tokens in addition to total size X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@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. Tighten this up: limit the token count to 128Ki. If you think 128Ki is too stingy: check-qjson's large_dict test eats a sweet 500MiB on my machine to parse ~100K tokens. Absurdly wasteful. Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake --- qobject/json-streamer.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/qobject/json-streamer.c b/qobject/json-streamer.c index 2bd22a7..8752834 100644 --- a/qobject/json-streamer.c +++ b/qobject/json-streamer.c @@ -19,6 +19,7 @@ #include "qapi/qmp/json-streamer.h" #define MAX_TOKEN_SIZE (64ULL << 20) +#define MAX_TOKEN_COUNT (128ULL << 10) #define MAX_NESTING (1ULL << 10) static void json_message_process_token(JSONLexer *lexer, QString *token, JSONTokenType type, int x, int y) @@ -64,6 +65,7 @@ static void json_message_process_token(JSONLexer *lexer, QString *token, JSONTok parser->bracket_count == 0)) { goto out_emit; } else if (parser->token_size > MAX_TOKEN_SIZE || + qlist_size(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.