Received: from mail.netfilter.org (mail.netfilter.org [217.70.190.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2FBA8EEB1 for ; Thu, 10 Apr 2025 22:17:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.190.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744323444; cv=none; b=SxtvsaDfI6qT1m0lGcwlkeXCMca2batdecKPXLpcKQb0FdaDelfwdI5JNDtNZuMrNyhaY2HT1sy2wqc7wt/kOzIYLagxCbwE7bKnVmXHeLL3AXIki/+Icsgt/485ch4N4Xxar3r8xBX3VFIi3YiVgRhdsvdIM4pOn209CKghc1Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744323444; c=relaxed/simple; bh=Dp03vyArHnDO3ieTGsrG0QwQANRcoAVqgdcC3Y0h3Lg=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=oHSUdhta72S20nMatchsk3tqpU5AMeojVQ1SB1Y56hyINhmP5XVL1f3VCNS266NNbgmREHxpXLm5nOM/j0udHepi6b9AzNWH2iAZ8bon6pffLNqRQbToz5jJRdq9EoQR/gF7p6A78n+RFCZ6pYE3BOY3yf4H6XVF7W1c6ZDDHeA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=netfilter.org; spf=pass smtp.mailfrom=netfilter.org; dkim=pass (2048-bit key) header.d=netfilter.org header.i=@netfilter.org header.b=AJqHMTZU; dkim=pass (2048-bit key) header.d=netfilter.org header.i=@netfilter.org header.b=g2pTUPXK; arc=none smtp.client-ip=217.70.190.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=netfilter.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=netfilter.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=netfilter.org header.i=@netfilter.org header.b="AJqHMTZU"; dkim=pass (2048-bit key) header.d=netfilter.org header.i=@netfilter.org header.b="g2pTUPXK" Received: by mail.netfilter.org (Postfix, from userid 109) id B8758603B1; Fri, 11 Apr 2025 00:17:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netfilter.org; s=2025; t=1744323436; bh=5b1B/yUky/NZ3TGgo5oZ0C56/qWRp4IYeFyGFRIHnIU=; h=From:To:Cc:Subject:Date:From; b=AJqHMTZUoU8ntLA8hEx0nuAGSD5Kaw6mrnS6lJmBmqiJcNkyqA2Gn9Il6HPF/D4hE XZggU80FHIpHSW4pszrawCu+Jb6U0JRHcl0Ru2jwfuS2Eg+ulc4x7jd7xrSqxfTLGL qO/wZXcihj6vHVM+KMcLKh/FIVkfkZY6dGkfIblfi4+wvjrIh5dfqjYwoEzVFRunDq EBGugTpc6n88ce24RWc609Q+IHxo72djVZkVZp3rw8tqd6ygeylHX9yLRokon3qo5m trELP51Xyv9Hxap2Pf/Qtioj4G79xy2+fWYrzGE46eyRCOwcHxeZrjgXo05pvgYeCE z03Czdn73elsw== X-Spam-Level: Received: from localhost.localdomain (mail-agni [217.70.190.124]) by mail.netfilter.org (Postfix) with ESMTPSA id BE5D2603B1; Fri, 11 Apr 2025 00:17:15 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netfilter.org; s=2025; t=1744323435; bh=5b1B/yUky/NZ3TGgo5oZ0C56/qWRp4IYeFyGFRIHnIU=; h=From:To:Cc:Subject:Date:From; b=g2pTUPXKXvzMOVFMIr9sGDQz1uIY1bJ/jz9QhebaNdrigX6dBZP8zMNXH0IFadAo/ 1kEtT3JmmjA4RxxOt9F42pv4XdOWvqdz7FHgYTnxV7L7beGj1RHpunjLZ51R2Hmdxc ESyy8KJNMwpwbJZMh5VClKnIdD9uXgFhUqMoc2nuXrY3CmaqZf3h+vSRPkwi6AgAnt 6Jab+x3r2DgP08Or3+qc07NfyhR44YzwPw5xuleZYahd7B2kewWGeREArehoyMtgJC nToJ025obaRc6H5MYX8rXnyLHbw0WBEtb8ltWX4HeHoyjLKtK+8ZhJ80FcBcWj3GAG 4/DpUbZdYkghw== From: Pablo Neira Ayuso To: netfilter-devel@vger.kernel.org Cc: fw@strlen.de Subject: [PATCH nft] parser_bison: add selector_expr rule to restrict typeof_expr Date: Fri, 11 Apr 2025 00:17:10 +0200 Message-Id: <20250410221710.1350528-1-pablo@netfilter.org> X-Mailer: git-send-email 2.30.2 Precedence: bulk X-Mailing-List: netfilter-devel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit typeof_expr allows for symbol, constant and bitwise expressions, restrict it to selector expressions. After this patch, input generated by fuzzer is rejected upfront: # nft -f test.nft test.nft:3:53-53: Error: syntax error, unexpected number typeof numgen inc mod 2 : ip daddr . 0 ^ test.nft:2:12-13: Error: set definition does not specify key map t2 { ^^ test.nft:8:65-67: Error: No such file or directory meta l4proto tcp dnat ip to numgen inc mod 2 map @t2 ^^^ test.nft:8:65-67: Error: No such file or directory meta l4proto tcp dnat ip to numgen inc mod 2 map @t2 ^^^ Revisit 4ab1e5e60779 ("src: allow use of 'verdict' in typeof definitions") to handle to handle verdict as string, later a token can be added to the scanner and enable it via flex start conditions. Fixes: 14357cff40ed ("parser: add typeof keyword for declarations") Reported-by: Florian Westphal Signed-off-by: Pablo Neira Ayuso --- src/parser_bison.y | 43 +++++++++++-------- ...valid_set_key_stmt_evaluate_nat_map_assert | 10 +++++ 2 files changed, 36 insertions(+), 17 deletions(-) create mode 100644 tests/shell/testcases/bogons/nft-f/invalid_set_key_stmt_evaluate_nat_map_assert diff --git a/src/parser_bison.y b/src/parser_bison.y index 4b2b51d4275c..ed6a24a15377 100644 --- a/src/parser_bison.y +++ b/src/parser_bison.y @@ -818,8 +818,8 @@ int nft_lex(void *, void *, void *); %type symbol_expr verdict_expr integer_expr variable_expr chain_expr policy_expr %destructor { expr_free($$); } symbol_expr verdict_expr integer_expr variable_expr chain_expr policy_expr -%type primary_expr shift_expr and_expr primary_typeof_expr typeof_expr typeof_data_expr typeof_key_expr typeof_verdict_expr -%destructor { expr_free($$); } primary_expr shift_expr and_expr primary_typeof_expr typeof_expr typeof_data_expr typeof_key_expr typeof_verdict_expr +%type primary_expr shift_expr and_expr primary_typeof_expr typeof_expr typeof_data_expr typeof_key_expr typeof_verdict_expr selector_expr +%destructor { expr_free($$); } primary_expr shift_expr and_expr primary_typeof_expr typeof_expr typeof_data_expr typeof_key_expr typeof_verdict_expr selector_expr %type exclusive_or_expr inclusive_or_expr %destructor { expr_free($$); } exclusive_or_expr inclusive_or_expr %type basic_expr @@ -2097,19 +2097,10 @@ subchain_block : /* empty */ { $$ = $-1; } } ; -typeof_verdict_expr : primary_expr +typeof_verdict_expr : selector_expr { struct expr *e = $1; - if (e->etype == EXPR_SYMBOL && - strcmp("verdict", e->identifier) == 0) { - struct expr *v = verdict_expr_alloc(&@1, NF_ACCEPT, NULL); - - expr_free(e); - v->flags &= ~EXPR_F_CONSTANT; - e = v; - } - if (expr_ops(e)->build_udata == NULL) { erec_queue(error(&@1, "map data type '%s' lacks typeof serialization", expr_ops(e)->name), state->msgs); @@ -2118,7 +2109,7 @@ typeof_verdict_expr : primary_expr } $$ = e; } - | typeof_expr DOT primary_expr + | typeof_expr DOT selector_expr { struct location rhs[] = { [1] = @2, @@ -2142,9 +2133,24 @@ typeof_data_expr : INTERVAL typeof_expr { $$ = constant_expr_alloc(&@$, &queue_type, BYTEORDER_HOST_ENDIAN, 16, NULL); } + | STRING + { + struct expr *verdict; + + if (strcmp("verdict", $1) != 0) { + erec_queue(error(&@1, "map data type '%s' lacks typeof serialization", $1), + state->msgs); + free_const($1); + YYERROR; + } + verdict = verdict_expr_alloc(&@1, NF_ACCEPT, NULL); + verdict->flags &= ~EXPR_F_CONSTANT; + $$ = verdict; + free_const($1); + } ; -primary_typeof_expr : primary_expr +primary_typeof_expr : selector_expr { if (expr_ops($1)->build_udata == NULL) { erec_queue(error(&@1, "primary expression type '%s' lacks typeof serialization", expr_ops($1)->name), @@ -4332,9 +4338,7 @@ integer_expr : NUM } ; -primary_expr : symbol_expr { $$ = $1; } - | integer_expr { $$ = $1; } - | payload_expr { $$ = $1; } +selector_expr : payload_expr { $$ = $1; } | exthdr_expr { $$ = $1; } | exthdr_exists_expr { $$ = $1; } | meta_expr { $$ = $1; } @@ -4346,6 +4350,11 @@ primary_expr : symbol_expr { $$ = $1; } | fib_expr { $$ = $1; } | osf_expr { $$ = $1; } | xfrm_expr { $$ = $1; } + ; + +primary_expr : symbol_expr { $$ = $1; } + | integer_expr { $$ = $1; } + | selector_expr { $$ = $1; } | '(' basic_expr ')' { $$ = $2; } ; diff --git a/tests/shell/testcases/bogons/nft-f/invalid_set_key_stmt_evaluate_nat_map_assert b/tests/shell/testcases/bogons/nft-f/invalid_set_key_stmt_evaluate_nat_map_assert new file mode 100644 index 000000000000..d73dce8e5ce1 --- /dev/null +++ b/tests/shell/testcases/bogons/nft-f/invalid_set_key_stmt_evaluate_nat_map_assert @@ -0,0 +1,10 @@ +table ip t { + map t2 { + typeof numgen inc mod 2 : ip daddr . 0 + } + + chain c { + type nat hook prerouting priority dstnat; policy accept; + meta l4proto tcp dnat ip to numgen inc mod 2 map @t2 + } +} -- 2.30.2