Blame SOURCES/0013-xtables-restore-fix-for-noflush-and-empty-lines.patch

576484
From 5ea18ea8c0c99f2c71a5eaf32f4fbf6339ce8cc7 Mon Sep 17 00:00:00 2001
576484
From: Phil Sutter <phil@nwl.cc>
576484
Date: Tue, 11 Feb 2020 16:52:59 +0100
576484
Subject: [PATCH] xtables-restore: fix for --noflush and empty lines
576484
576484
Lookahead buffer used for cache requirements estimate in restore
576484
--noflush separates individual lines with nul-chars. Two consecutive
576484
nul-chars are interpreted as end of buffer and remaining buffer content
576484
is skipped.
576484
576484
Sadly, reading an empty line (i.e., one containing a newline character
576484
only) caused double nul-chars to appear in buffer as well, leading to
576484
premature stop when reading cached lines from buffer.
576484
576484
To fix that, make use of xtables_restore_parse_line() skipping empty
576484
lines without calling strtok() and just leave the newline character in
576484
place. A more intuitive approach, namely skipping empty lines while
576484
buffering, is deliberately not chosen as that would cause wrong values
576484
in 'line' variable.
576484
576484
Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1400
576484
Fixes: 09cb517949e69 ("xtables-restore: Improve performance of --noflush operation")
576484
Signed-off-by: Phil Sutter <phil@nwl.cc>
576484
Acked-by: Arturo Borrero Gonzalez <arturo@netfilter.org>
576484
(cherry picked from commit 8e76391096f12212985c401ee83a67990aa27a29)
576484
Signed-off-by: Phil Sutter <psutter@redhat.com>
576484
---
576484
 .../ipt-restore/0011-noflush-empty-line_0        | 16 ++++++++++++++++
576484
 iptables/xtables-restore.c                       |  8 +++++---
576484
 2 files changed, 21 insertions(+), 3 deletions(-)
576484
 create mode 100755 iptables/tests/shell/testcases/ipt-restore/0011-noflush-empty-line_0
576484
576484
diff --git a/iptables/tests/shell/testcases/ipt-restore/0011-noflush-empty-line_0 b/iptables/tests/shell/testcases/ipt-restore/0011-noflush-empty-line_0
576484
new file mode 100755
576484
index 0000000000000..bea1a690bb624
576484
--- /dev/null
576484
+++ b/iptables/tests/shell/testcases/ipt-restore/0011-noflush-empty-line_0
576484
@@ -0,0 +1,16 @@
576484
+#!/bin/bash -e
576484
+
576484
+# make sure empty lines won't break --noflush
576484
+
576484
+cat <
576484
+# just a comment followed by innocent empty line
576484
+
576484
+*filter
576484
+-A FORWARD -j ACCEPT
576484
+COMMIT
576484
+EOF
576484
+
576484
+EXPECT='Chain FORWARD (policy ACCEPT)
576484
+target     prot opt source               destination         
576484
+ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           '
576484
+diff -u <(echo "$EXPECT") <($XT_MULTI iptables -n -L FORWARD)
576484
diff --git a/iptables/xtables-restore.c b/iptables/xtables-restore.c
576484
index 63cc15cee9621..fb2ac8b5c12a3 100644
576484
--- a/iptables/xtables-restore.c
576484
+++ b/iptables/xtables-restore.c
576484
@@ -293,11 +293,13 @@ void xtables_restore_parse(struct nft_handle *h,
576484
 		while (fgets(buffer, sizeof(buffer), p->in)) {
576484
 			size_t blen = strlen(buffer);
576484
 
576484
-			/* drop trailing newline; xtables_restore_parse_line()
576484
+			/* Drop trailing newline; xtables_restore_parse_line()
576484
 			 * uses strtok() which replaces them by nul-characters,
576484
 			 * causing unpredictable string delimiting in
576484
-			 * preload_buffer */
576484
-			if (buffer[blen - 1] == '\n')
576484
+			 * preload_buffer.
576484
+			 * Unless this is an empty line which would fold into a
576484
+			 * spurious EoB indicator (double nul-char). */
576484
+			if (buffer[blen - 1] == '\n' && blen > 1)
576484
 				buffer[blen - 1] = '\0';
576484
 			else
576484
 				blen++;
576484
-- 
576484
2.24.1
576484