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

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