From 8a9f48515fb8f9aed0af04e05f4528aa0e32116f Mon Sep 17 00:00:00 2001 From: Eric Jallot Date: Fri, 29 Nov 2019 15:30:39 +0100 Subject: scanner: fix out-of-bound memory write in include_file() Before patch: # echo 'include "/tmp/rules.nft"' > /tmp/rules.nft # nft -f /tmp/rules.nft In file included from /tmp/rules.nft:1:1-25: from /tmp/rules.nft:1:1-25: [snip] from /tmp/rules.nft:1:1-25: /tmp/rules.nft:1:1-25: Error: Include nested too deeply, max 16 levels include "/tmp/rules.nft" ^^^^^^^^^^^^^^^^^^^^^^^^^ double free or corruption (out) Aborted (core dumped) valgrind reports: ==8856== Invalid write of size 8 ==8856== at 0x4E8FCAF: include_file (scanner.l:718) ==8856== by 0x4E8FEF6: include_glob (scanner.l:793) ==8856== by 0x4E9985D: scanner_include_file (scanner.l:875) ==8856== by 0x4E89D7A: nft_parse (parser_bison.y:828) ==8856== by 0x4E765E1: nft_parse_bison_filename (libnftables.c:394) ==8856== by 0x4E765E1: nft_run_cmd_from_filename (libnftables.c:497) ==8856== by 0x40172D: main (main.c:340) So perform bounds checking on MAX_INCLUDE_DEPTH before writing. After patch: # nft -f /tmp/rules.nft In file included from /tmp/rules.nft:1:1-25: from /tmp/rules.nft:1:1-25: [snip] from /tmp/rules.nft:1:1-25: /tmp/rules.nft:1:1-25: Error: Include nested too deeply, max 16 levels include "/tmp/rules.nft" ^^^^^^^^^^^^^^^^^^^^^^^^^ # echo $? 1 Also: Update scanner_push_file() function definition accordingly. Fixes: 32325e3c3fab4 ("libnftables: Store top_scope in struct nft_ctx") Signed-off-by: Eric Jallot Signed-off-by: Pablo Neira Ayuso --- tests/shell/testcases/include/0016maxdepth_0 | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100755 tests/shell/testcases/include/0016maxdepth_0 (limited to 'tests') diff --git a/tests/shell/testcases/include/0016maxdepth_0 b/tests/shell/testcases/include/0016maxdepth_0 new file mode 100755 index 00000000..89eb13c4 --- /dev/null +++ b/tests/shell/testcases/include/0016maxdepth_0 @@ -0,0 +1,8 @@ +#!/bin/bash + +set -e + +tmpfile=$(mktemp) + +echo 'include "/tmp/rules.nft"' > $tmpfile +$NFT -f $tmpfile || exit 0 -- cgit v1.2.3