#!/bin/bash # test case to attempt to fool ruleset validation. # Initial ruleset added here is fine, then we try to make the # ruleset exceed the jump chain depth via jumps, gotos, verdict # map entries etc, either by having the map loop back to itself, # jumping back to an earlier chain and so on. # # Also check that can't hook up a user-defined chain with a # restricted expression (here: tproxy, only valid from prerouting # hook) to the input hook, even if reachable indirectly via vmap. bad_ruleset() { ret=$1 shift if [ $ret -eq 0 ];then echo "Accepted bad ruleset with $@" $NFT list ruleset exit 1 fi } good_ruleset() { ret=$1 shift if [ $ret -ne 0 ];then echo "Rejected good ruleset with $@" exit 1 fi } # add a loop with a vmap statement, either goto or jump, # both with single rule and delta-transaction that also # contains valid information. check_loop() { what=$1 $NFT "add element t m { 1.2.3.9 : $what c1 }" bad_ruleset $? "bound map with $what to backjump should exceed jump stack" $NFT "add element t m { 1.2.3.9 : $what c7 }" bad_ruleset $? "bound map with $what to backjump should exceed jump stack" $NFT "add element t m { 1.2.3.9 : $what c8 }" bad_ruleset $? "bound map with $what to self should exceed jump stack" # rule bound to c8, this should not work -- jump stack should be exceeded. $NFT "add element t m { 1.2.3.9 : jump c9 }" bad_ruleset $? "bound map with $what should exceed jump stack" # rule bound to c8, this should be within jump stack limit $NFT "add element t m { 1.2.3.9 : jump c10 }" good_ruleset $? "bound map with $what should not have exceeded jump stack" $NFT -f - < vmap -> c8 -> c9" } # 16 jump levels are permitted. # First ruleset is fine, there is no jump # from c8 to c9. $NFT -f - <c9 via vmap expression" # delete the mapping again. $NFT "delete element t m { 1.2.3.9 }" $NFT "add rule t c8 ip saddr vmap @m" good_ruleset $? "bind empty map to c8" check_loop "jump" check_loop "goto" $NFT "flush chain t c8" good_ruleset $? "flush chain t c8" # should work, c9 not connected to c0 aka filter input. $NFT "add rule t c9 tcp dport 80 tproxy to :20000 meta mark set 1 accept" good_ruleset $? "add tproxy expression to c9" check_bad_expr exit $?