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

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