Blame SOURCES/0072-xshared-Fix-response-to-unprivileged-users.patch

d8275f
From 6d1b02218a591ff95053b22c1ed802355e44878d Mon Sep 17 00:00:00 2001
d8275f
From: Phil Sutter <phil@nwl.cc>
d8275f
Date: Tue, 18 Jan 2022 22:39:08 +0100
d8275f
Subject: [PATCH] xshared: Fix response to unprivileged users
d8275f
d8275f
Expected behaviour in both variants is:
d8275f
d8275f
* Print help without error, append extension help if -m and/or -j
d8275f
  options are present
d8275f
* Indicate lack of permissions in an error message for anything else
d8275f
d8275f
With iptables-nft, this was broken basically from day 1. Shared use of
d8275f
do_parse() then somewhat broke legacy: it started complaining about
d8275f
inability to create a lock file.
d8275f
d8275f
Fix this by making iptables-nft assume extension revision 0 is present
d8275f
if permissions don't allow to verify. This is consistent with legacy.
d8275f
d8275f
Second part is to exit directly after printing help - this avoids having
d8275f
to make the following code "nop-aware" to prevent privileged actions.
d8275f
d8275f
Signed-off-by: Phil Sutter <phil@nwl.cc>
d8275f
Reviewed-by: Florian Westphal <fw@strlen.de>
d8275f
(cherry picked from commit 26ecdf53960658771c0fc582f72a4025e2887f75)
d8275f
d8275f
Conflicts:
d8275f
	iptables/xshared.c
d8275f
-> Some chunks not applied as not necessary in RHEL8.
d8275f
---
d8275f
 iptables/nft.c                                |  5 ++
d8275f
 .../testcases/iptables/0008-unprivileged_0    | 60 +++++++++++++++++++
d8275f
 2 files changed, 65 insertions(+)
d8275f
 create mode 100755 iptables/tests/shell/testcases/iptables/0008-unprivileged_0
d8275f
d8275f
diff --git a/iptables/nft.c b/iptables/nft.c
d8275f
index dc5490c085364..c5cc6f83bf573 100644
d8275f
--- a/iptables/nft.c
d8275f
+++ b/iptables/nft.c
d8275f
@@ -3110,6 +3110,11 @@ int nft_compatible_revision(const char *name, uint8_t rev, int opt)
d8275f
 err:
d8275f
 	mnl_socket_close(nl);
d8275f
 
d8275f
+	/* pretend revision 0 is valid if not permitted to check -
d8275f
+	 * this is required for printing extension help texts as user */
d8275f
+	if (ret < 0 && errno == EPERM && rev == 0)
d8275f
+		return 1;
d8275f
+
d8275f
 	return ret < 0 ? 0 : 1;
d8275f
 }
d8275f
 
d8275f
diff --git a/iptables/tests/shell/testcases/iptables/0008-unprivileged_0 b/iptables/tests/shell/testcases/iptables/0008-unprivileged_0
d8275f
new file mode 100755
d8275f
index 0000000000000..43e3bc8721dbd
d8275f
--- /dev/null
d8275f
+++ b/iptables/tests/shell/testcases/iptables/0008-unprivileged_0
d8275f
@@ -0,0 +1,60 @@
d8275f
+#!/bin/bash
d8275f
+
d8275f
+# iptables may print match/target specific help texts
d8275f
+# help output should work for unprivileged users
d8275f
+
d8275f
+run() {
d8275f
+	echo "running: $*" >&2
d8275f
+	runuser -u nobody -- "$@"
d8275f
+}
d8275f
+
d8275f
+grep_or_rc() {
d8275f
+	declare -g rc
d8275f
+	grep -q "$*" && return 0
d8275f
+	echo "missing in output: $*" >&2
d8275f
+	return 1
d8275f
+}
d8275f
+
d8275f
+out=$(run $XT_MULTI iptables --help)
d8275f
+let "rc+=$?"
d8275f
+grep_or_rc "iptables -h (print this help information)" <<< "$out"
d8275f
+let "rc+=$?"
d8275f
+
d8275f
+out=$(run $XT_MULTI iptables -m limit --help)
d8275f
+let "rc+=$?"
d8275f
+grep_or_rc "limit match options:" <<< "$out"
d8275f
+let "rc+=$?"
d8275f
+
d8275f
+out=$(run $XT_MULTI iptables -p tcp --help)
d8275f
+let "rc+=$?"
d8275f
+grep_or_rc "tcp match options:" <<< "$out"
d8275f
+let "rc+=$?"
d8275f
+
d8275f
+out=$(run $XT_MULTI iptables -j DNAT --help)
d8275f
+let "rc+=$?"
d8275f
+grep_or_rc "DNAT target options:" <<< "$out"
d8275f
+let "rc+=$?"
d8275f
+
d8275f
+out=$(run $XT_MULTI iptables -p tcp -j DNAT --help)
d8275f
+let "rc+=$?"
d8275f
+grep_or_rc "tcp match options:" <<< "$out"
d8275f
+let "rc+=$?"
d8275f
+out=$(run $XT_MULTI iptables -p tcp -j DNAT --help)
d8275f
+let "rc+=$?"
d8275f
+grep_or_rc "DNAT target options:" <<< "$out"
d8275f
+let "rc+=$?"
d8275f
+
d8275f
+
d8275f
+run $XT_MULTI iptables -L 2>&1 | \
d8275f
+	grep_or_rc "Permission denied"
d8275f
+let "rc+=$?"
d8275f
+
d8275f
+run $XT_MULTI iptables -A FORWARD -p tcp --dport 123 2>&1 | \
d8275f
+	grep_or_rc "Permission denied"
d8275f
+let "rc+=$?"
d8275f
+
d8275f
+run $XT_MULTI iptables -A FORWARD -j DNAT --to-destination 1.2.3.4 2>&1 | \
d8275f
+	grep_or_rc "Permission denied"
d8275f
+let "rc+=$?"
d8275f
+
d8275f
+exit $rc
d8275f
-- 
d8275f
2.34.1
d8275f