From patchwork Wed Oct 10 22:15:52 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yifeng Sun X-Patchwork-Id: 982122 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="jYJ0Cp24"; 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 42VpMp054jz9s2P for ; Thu, 11 Oct 2018 09:16:05 +1100 (AEDT) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id E343CD0D; Wed, 10 Oct 2018 22:16:01 +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 28A50C7C for ; Wed, 10 Oct 2018 22:16:01 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.7.6 Received: from mail-pg1-f196.google.com (mail-pg1-f196.google.com [209.85.215.196]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id A5B47D0 for ; Wed, 10 Oct 2018 22:16:00 +0000 (UTC) Received: by mail-pg1-f196.google.com with SMTP id g2-v6so3141397pgu.11 for ; Wed, 10 Oct 2018 15:16:00 -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=BqTUv+ycz3FFQ1G8c0AE1pAoxGru16nQHQDaEwmzALo=; b=jYJ0Cp247zhokmvG15kklSIa0ZjG2KIhWjA2dEop5217KEaapk93Xo4Gk8I/wLKLcj HIOTzJYxC8/XJojsaasb4L8wWDINjZFrePm1RDDzxXC4RpCx3HNxSvRZVW6Gs/ga/jK7 kpjf9KfyySQ3NM4RGGgKMrFgj6ek88m+5hC8fbQ34JIZQCsf6/Bh1Nx0oLDoVyj7/76/ DmsLKjKo8clGuiVNKlCxfmLbPyjBi5Wnya3uhX0Ur5nrvdhv4jLgdSojDQXRNrlGbzO8 gBIkGHZiKPceZYSiu+dVekidikpUkaj2OVOVB8k5D1gQOQ1EBAqpq9QXOwGCKvL+zBmk luMg== 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=BqTUv+ycz3FFQ1G8c0AE1pAoxGru16nQHQDaEwmzALo=; b=sWMMI7uWu+G/i+jkr7/cXz+VZ6kmsnEzDKzCyCq12Cp9Ev91m4dSdg1yfaMC1bzcNp /BJbB0kKutZg+W/3Qw5kYuOfbI+oCG6CL1mXutcbfpixf+Ee0nmxdQoU9NCHz7Ni1ahc GfeKon4NFL4Fy8DJEcy6YCgWTM/8+kxK4aplhqUJy8FBTPJSiARTOoIjWaMnXIpzI7kZ fCyGdI4QKDpal9FgwddS1NjL1EXsS5tLMxM/IO31dJmDy8EU2/aO6uRWU6OA5QQQDU8A qsdeRiJwnyJdwJp19RnTmlcrqaHJQqilnOpuk2QY7fx1mGHMyhA3Lu4EEgQMKytJ7ohq scTQ== X-Gm-Message-State: ABuFfoge+KhrSqUh75c25J8wmFLQSqW17Vn0lG2ETjasbabtDVwRojYk yduXQ73jygpMqg8cWDFGqhrzWOpO X-Google-Smtp-Source: ACcGV63CYX6QENrmz0uxHnFCJV3bKWvxn1viGJm+7IQzk1RumJFwWLLbJQDhX+rsRvYqUmE2Uj5eRg== X-Received: by 2002:a63:e05:: with SMTP id d5-v6mr31185558pgl.272.1539209759792; Wed, 10 Oct 2018 15:15:59 -0700 (PDT) Received: from kern417.eng.vmware.com ([66.170.99.1]) by smtp.gmail.com with ESMTPSA id e64-v6sm34136502pfe.55.2018.10.10.15.15.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 10 Oct 2018 15:15:59 -0700 (PDT) From: Yifeng Sun To: dev@openvswitch.org Date: Wed, 10 Oct 2018 15:15:52 -0700 Message-Id: <1539209752-9882-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 v3] 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. Since is_chassis_resident doesn't allow nested parentheses, its following parentheses are not taken into acount in the parentheses-depth context. 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! v2->v3: Ignore parentheses from chassis resident. ovn/lib/expr.c | 10 ++++++++++ tests/ovn.at | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/ovn/lib/expr.c b/ovn/lib/expr.c index 148ac869e861..5880fd2e7289 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 *); @@ -1077,11 +1080,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; } diff --git a/tests/ovn.at b/tests/ovn.at index 44475175d20a..39122b08bf22 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" +(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((0))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) => 0 + 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,8 @@ 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. ]]) sed 's/ =>.*//' test-cases.txt > input.txt sed 's/.* => //' test-cases.txt > expout