From patchwork Wed Oct 10 18:03:54 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yifeng Sun X-Patchwork-Id: 982020 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Nk16z7qr"; dkim-atps=neutral Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42Vhn33F3Yz9s9J for ; Thu, 11 Oct 2018 05:04:06 +1100 (AEDT) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 591EAC4D; Wed, 10 Oct 2018 18:04:03 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id D7A4BC2D for ; Wed, 10 Oct 2018 18:04:01 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.7.6 Received: from mail-pl1-f193.google.com (mail-pl1-f193.google.com [209.85.214.193]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id 6A8E64DA for ; Wed, 10 Oct 2018 18:04:01 +0000 (UTC) Received: by mail-pl1-f193.google.com with SMTP id s4-v6so2911926plp.0 for ; Wed, 10 Oct 2018 11:04:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=/weGozHMBSYx56kcEhUo76IXQmsFYjdMg3s8j11yyYY=; b=Nk16z7qrDlGaZarf8mjPtt9PjHeFJCQk9gwQGD/t8Myan7aAWiEfaRuRVjk0uVpJJD 5M7DTZLUNzBoXLbFi1QV1AJb8rHcHLq5uDFz0nWDcrOy7ZW1sOBDH4iAXX8KB4xtm/jP RVuJlX1PWYAs7F+ryW00jnJVwKeZ/NohllpqUZ439kaKFl2GDGQt8Oc5HC+7FKsYnYzM JGoOqFQIYcJx8dHps1xjw8vEYEV6RNSgeLqSsAyNukMHTKZmzsAWz+UZurMPj/arlB4Y HmR3GuujjjFCB/bEcDp3XtEcLM+QNZfU9EoEDZyojH5/co5ghQnJiE9ngGG9mOnYlAR2 XtEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=/weGozHMBSYx56kcEhUo76IXQmsFYjdMg3s8j11yyYY=; b=lpXixq6fFGCVkd+iyM+46E7DabIGiti9WTvH/wAW4bdYy9Lm280TEs8T+oP3M9VaDM M25dQOPe9FBuOcSoLzzCEBdPDyotH6XL08oFG6iqwIySWJcglIMn4KUAJy5LRKXmvWPt CKg72aNd/Faxz4rhRsrbrJj/XG5rk+Xwxpst5irCrPB55fvPIPYEtyFq3ht+GiTPOVc9 DadHxQ+UNx95Z4kq+KWif6axe88cx8Qo0dlojtD3BrlLxC9cLEc9/BXdl/GnN0tv9Avi 0PWOMu3FN/8eFolmY6ApjDSWPsKDVj4BfC+II+6Nm2SMyrJgupI/m2XzH1qOekn73Ude mpJw== X-Gm-Message-State: ABuFfoiDcDyICxlvghjzsfD+KJ4xufCSfk2r3mkB4nUfXCKnkECQzGqc eDRVn6IK9ZTPJRcxs5qd8UNnTPtP X-Google-Smtp-Source: ACcGV62UpZ/BxbRJLWZQvpD64dbkRtwb8TyrzpNeQYlOdtWY1AQl7gVWgQfBym4wj6AVH2SuJ4Ksvg== X-Received: by 2002:a17:902:708a:: with SMTP id z10-v6mr23273118plk.330.1539194640642; Wed, 10 Oct 2018 11:04:00 -0700 (PDT) Received: from kern417.eng.vmware.com ([66.170.99.1]) by smtp.gmail.com with ESMTPSA id k13-v6sm30589678pff.30.2018.10.10.11.03.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 10 Oct 2018 11:03:59 -0700 (PDT) From: Yifeng Sun To: dev@openvswitch.org Date: Wed, 10 Oct 2018 11:03:54 -0700 Message-Id: <1539194634-348-1-git-send-email-pkusunyifeng@gmail.com> X-Mailer: git-send-email 2.7.4 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Subject: [ovs-dev] [PATCH v2] expr: Set a limit on the depth of nested parentheses X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org This patch checks the depth of nested parentheses to prevent stack overflow. Reported-at: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=10714 Signed-off-by: Yifeng Sun Suggested-by: Ben Pfaff --- v1->v2: Handle parse_chassis_resident and add new test, thanks Ben! ovn/lib/expr.c | 17 +++++++++++++++++ tests/ovn.at | 5 +++++ 2 files changed, 22 insertions(+) diff --git a/ovn/lib/expr.c b/ovn/lib/expr.c index 148ac869e861..96e61606e288 100644 --- a/ovn/lib/expr.c +++ b/ovn/lib/expr.c @@ -459,6 +459,8 @@ expr_print(const struct expr *e) /* Parsing. */ +#define MAX_PAREN_DEPTH 100 + /* Context maintained during expr_parse(). */ struct expr_context { struct lexer *lexer; /* Lexer for pulling more tokens. */ @@ -466,6 +468,7 @@ struct expr_context { const struct shash *addr_sets; /* Address set table. */ const struct shash *port_groups; /* Port group table. */ bool not; /* True inside odd number of NOT operators. */ + unsigned int paren_depth; /* Depth of nested parentheses. */ }; struct expr *expr_parse__(struct expr_context *); @@ -1068,6 +1071,7 @@ parse_chassis_resident(struct expr_context *ctx) expr_destroy(e); return NULL; } + --ctx->paren_depth; return e; } @@ -1077,11 +1081,18 @@ expr_parse_primary(struct expr_context *ctx, bool *atomic) { *atomic = false; if (lexer_match(ctx->lexer, LEX_T_LPAREN)) { + if (++ctx->paren_depth > MAX_PAREN_DEPTH) { + lexer_syntax_error(ctx->lexer, + "parenthesis nested too deeply"); + return NULL; + } + struct expr *e = expr_parse__(ctx); if (!lexer_force_match(ctx->lexer, LEX_T_RPAREN)) { expr_destroy(e); return NULL; } + --ctx->paren_depth; *atomic = true; return e; } @@ -1093,6 +1104,12 @@ expr_parse_primary(struct expr_context *ctx, bool *atomic) if (lexer_lookahead(ctx->lexer) == LEX_T_LPAREN) { if (lexer_match_id(ctx->lexer, "is_chassis_resident")) { + if (++ctx->paren_depth > MAX_PAREN_DEPTH) { + lexer_syntax_error(ctx->lexer, + "parenthesis nested too deeply"); + return NULL; + } + lexer_get(ctx->lexer); /* Skip "(". */ *atomic = true; return parse_chassis_resident(ctx); diff --git a/tests/ovn.at b/tests/ovn.at index 44475175d20a..584fb5c2a727 100644 --- a/tests/ovn.at +++ b/tests/ovn.at @@ -285,6 +285,8 @@ ip6.src == ::1 => ip6.src == 0x1 inport == "eth0" !(inport != "eth0") => inport == "eth0" +(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((is_chassis_resident("eth0")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) => is_chassis_resident("eth0") + ip4.src == "eth0" => Integer field ip4.src is not compatible with string constant. inport == 1 => String field inport is not compatible with integer constant. ip4.src = 1.2.3.4 => Syntax error at `=' expecting relational operator. @@ -351,6 +353,9 @@ eth.dst[40] x => Syntax error at `x' expecting end of input. ip4.src == {1.2.3.4, $set1, $unknownset} => Syntax error at `$unknownset' expecting address set name. eth.src == {$set3, badmac, 00:00:00:00:00:01} => Syntax error at `badmac' expecting constant. + +((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((( => Syntax error at end of input parenthesis nested too deeply. +((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((is_chassis_resident("eth1") => Syntax error at `(' parenthesis nested too deeply. ]]) sed 's/ =>.*//' test-cases.txt > input.txt sed 's/.* => //' test-cases.txt > expout