render / rpms / libvirt

Forked from rpms/libvirt 10 months ago
Clone
7548c0
From 4d8a10886f4dffd08fcf6a93694e12f76a2afd66 Mon Sep 17 00:00:00 2001
7548c0
Message-Id: <4d8a10886f4dffd08fcf6a93694e12f76a2afd66@dist-git>
7548c0
From: Laine Stump <laine@redhat.com>
7548c0
Date: Fri, 15 Jan 2021 22:51:51 -0500
7548c0
Subject: [PATCH] util: call iptables directly rather than via firewalld
7548c0
7548c0
When libvirt added support for firewalld, we were unable to use
7548c0
firewalld's higher level rules, because they weren't detailed enough
7548c0
and could not be applied to the iptables FORWARD or OUTPUT chains
7548c0
(only to the INPUT chain). Instead we changed our code so that rather
7548c0
than running the iptables/ip6tables/ebtables binaries ourselves, we
7548c0
would send these commands to firewalld as "passthrough commands", and
7548c0
firewalld would run the appropriate program on our behalf.
7548c0
7548c0
This was done under the assumption that firewalld was somehow tracking
7548c0
all these rules, and that this tracking was benefitting proper
7548c0
operation of firewalld and the system in general.
7548c0
7548c0
Several years later this came up in a discussion on IRC, and we
7548c0
learned from the firewalld developers that, in fact, adding iptables
7548c0
and ebtables rules with firewalld's passthrough commands actually has
7548c0
*no* advantage; firewalld doesn't keep track of these rules in any
7548c0
way, and doesn't use them to tailor the construction of its own rules.
7548c0
7548c0
Meanwhile, users have been complaining for some time that whenever
7548c0
firewalld is restarted on a system with libvirt virtual networks
7548c0
and/or nwfilter rules active, the system logs would be flooded with
7548c0
warning messages whining that [lots of different rules] could not be
7548c0
deleted because they didn't exist. For example:
7548c0
7548c0
firewalld[3536040]: WARNING: COMMAND_FAILED:
7548c0
  '/usr/sbin/iptables -w10 -w --table filter --delete LIBVIRT_OUT
7548c0
  --out-interface virbr4 --protocol udp --destination-port 68
7548c0
  --jump ACCEPT' failed: iptables: Bad rule
7548c0
  (does a matching rule exist in that chain?).
7548c0
7548c0
See:
7548c0
7548c0
  https://bugzilla.redhat.com/1607929 (RHEL8)
7548c0
  https://bugzilla.redhat.com/1790837 (RHEL8-AV)
7548c0
7548c0
for many more examples and a discussion)
7548c0
7548c0
Note that these messages are created by iptables, but are logged by
7548c0
firewalld - when an iptables/ebtables command fails, firewalld grabs
7548c0
whatever is in stderr of the program, and spits it out to the system
7548c0
log as a warning. We've requested that firewalld not do this (and
7548c0
instead leave it up to the calling application to do the appropriate
7548c0
logging), but this request has been respectfully denied.
7548c0
7548c0
But combining the two problems above ( 1) firewalld doesn't do
7548c0
anything useful when you use it as a proxy to add/remove iptables
7548c0
rules, 2) firewalld often insists on logging lots of
7548c0
annoying/misleading/useless "error" messages when you use it as a
7548c0
proxy to remove iptables rules that don't already exist), leads to a
7548c0
solution - simply stop using firewalld to add and remove iptables
7548c0
rules. Instead, exec iptables/ip6tables/ebtables directly in the same
7548c0
way we do when firewalld isn't active.
7548c0
7548c0
We still need to keep track of whether or not firewalld is active, as
7548c0
there are some things that must be done, e.g. we need to add some
7548c0
actual firewalld rules in the firewalld "libvirt" zone, and we need to
7548c0
take notice when firewalld restarts, so that we can reload all our
7548c0
rules.
7548c0
7548c0
This patch doesn't remove the infrastructure that allows having
7548c0
different firewall backends that perform their functions in different
7548c0
ways, as that will very possibly come in handy in the future when we
7548c0
want to have an nftables direct backend, and possibly a "pure"
7548c0
firewalld backend (now that firewalld supports more complex rules, and
7548c0
can add those rules to the FORWARD and OUTPUT chains). Instead, it
7548c0
just changes the action when the selected backend is "firewalld" so
7548c0
that it adds rules directly rather than through firewalld, while
7548c0
leaving as much of the existing code intact as possible.
7548c0
7548c0
In order for tests to still pass, virfirewalltest also had to be
7548c0
modified to behave in a different way (i.e. by capturing the generated
7548c0
commandline as it does for the DIRECT backend, rather than capturing
7548c0
dbus messages using a mocked dbus API).
7548c0
7548c0
Signed-off-by: Laine Stump <laine@redhat.com>
7548c0
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
7548c0
(cherry picked from commit b19863640d10b47b7c4a7cbadb21f196d61d96a2)
7548c0
Message-Id: <20210116035151.1066734-9-laine@redhat.com>
7548c0
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
7548c0
---
7548c0
 src/util/virfirewall.c  | 13 +++++++++++--
7548c0
 tests/virfirewalltest.c | 30 ++++++++++++++++++++----------
7548c0
 2 files changed, 31 insertions(+), 12 deletions(-)
7548c0
7548c0
diff --git a/src/util/virfirewall.c b/src/util/virfirewall.c
7548c0
index 66d20d3f17..2ea821ec17 100644
7548c0
--- a/src/util/virfirewall.c
7548c0
+++ b/src/util/virfirewall.c
7548c0
@@ -644,7 +644,7 @@ virFirewallApplyRuleDirect(virFirewallRulePtr rule,
7548c0
 }
7548c0
 
7548c0
 
7548c0
-static int
7548c0
+static int G_GNUC_UNUSED
7548c0
 virFirewallApplyRuleFirewallD(virFirewallRulePtr rule,
7548c0
                               bool ignoreErrors,
7548c0
                               char **output)
7548c0
@@ -702,7 +702,16 @@ virFirewallApplyRule(virFirewallPtr firewall,
7548c0
             return -1;
7548c0
         break;
7548c0
     case VIR_FIREWALL_BACKEND_FIREWALLD:
7548c0
-        if (virFirewallApplyRuleFirewallD(rule, ignoreErrors, &output) < 0)
7548c0
+        /* Since we are using raw iptables rules, there is no
7548c0
+         * advantage to going through firewalld, so instead just add
7548c0
+         * them directly rather that via dbus calls to firewalld. This
7548c0
+         * has the useful side effect of eliminating extra unwanted
7548c0
+         * warning messages in the system logs when trying to delete
7548c0
+         * rules that don't exist (which is something that happens
7548c0
+         * often when libvirtd is started, and *always* when firewalld
7548c0
+         * is restarted)
7548c0
+         */
7548c0
+        if (virFirewallApplyRuleDirect(rule, ignoreErrors, &output) < 0)
7548c0
             return -1;
7548c0
         break;
7548c0
 
7548c0
diff --git a/tests/virfirewalltest.c b/tests/virfirewalltest.c
7548c0
index 40e7f4f00b..1036353579 100644
7548c0
--- a/tests/virfirewalltest.c
7548c0
+++ b/tests/virfirewalltest.c
7548c0
@@ -214,7 +214,8 @@ testFirewallSingleGroup(const void *opaque)
7548c0
     if (virFirewallSetBackend(data->tryBackend) < 0)
7548c0
         goto cleanup;
7548c0
 
7548c0
-    if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT)
7548c0
+    if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT ||
7548c0
+        data->expectBackend == VIR_FIREWALL_BACKEND_FIREWALLD)
7548c0
         virCommandSetDryRun(&cmdbuf, NULL, NULL);
7548c0
     else
7548c0
         fwBuf = &cmdbuf;
7548c0
@@ -271,7 +272,8 @@ testFirewallRemoveRule(const void *opaque)
7548c0
     if (virFirewallSetBackend(data->tryBackend) < 0)
7548c0
         goto cleanup;
7548c0
 
7548c0
-    if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT)
7548c0
+    if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT ||
7548c0
+        data->expectBackend == VIR_FIREWALL_BACKEND_FIREWALLD)
7548c0
         virCommandSetDryRun(&cmdbuf, NULL, NULL);
7548c0
     else
7548c0
         fwBuf = &cmdbuf;
7548c0
@@ -335,7 +337,8 @@ testFirewallManyGroups(const void *opaque G_GNUC_UNUSED)
7548c0
     if (virFirewallSetBackend(data->tryBackend) < 0)
7548c0
         goto cleanup;
7548c0
 
7548c0
-    if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT)
7548c0
+    if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT ||
7548c0
+        data->expectBackend == VIR_FIREWALL_BACKEND_FIREWALLD)
7548c0
         virCommandSetDryRun(&cmdbuf, NULL, NULL);
7548c0
     else
7548c0
         fwBuf = &cmdbuf;
7548c0
@@ -426,7 +429,8 @@ testFirewallIgnoreFailGroup(const void *opaque G_GNUC_UNUSED)
7548c0
     if (virFirewallSetBackend(data->tryBackend) < 0)
7548c0
         goto cleanup;
7548c0
 
7548c0
-    if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT) {
7548c0
+    if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT ||
7548c0
+        data->expectBackend == VIR_FIREWALL_BACKEND_FIREWALLD) {
7548c0
         virCommandSetDryRun(&cmdbuf, testFirewallRollbackHook, NULL);
7548c0
     } else {
7548c0
         fwBuf = &cmdbuf;
7548c0
@@ -498,7 +502,8 @@ testFirewallIgnoreFailRule(const void *opaque G_GNUC_UNUSED)
7548c0
     if (virFirewallSetBackend(data->tryBackend) < 0)
7548c0
         goto cleanup;
7548c0
 
7548c0
-    if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT) {
7548c0
+    if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT ||
7548c0
+        data->expectBackend == VIR_FIREWALL_BACKEND_FIREWALLD) {
7548c0
         virCommandSetDryRun(&cmdbuf, testFirewallRollbackHook, NULL);
7548c0
     } else {
7548c0
         fwBuf = &cmdbuf;
7548c0
@@ -567,7 +572,8 @@ testFirewallNoRollback(const void *opaque G_GNUC_UNUSED)
7548c0
     if (virFirewallSetBackend(data->tryBackend) < 0)
7548c0
         goto cleanup;
7548c0
 
7548c0
-    if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT) {
7548c0
+    if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT ||
7548c0
+        data->expectBackend == VIR_FIREWALL_BACKEND_FIREWALLD) {
7548c0
         virCommandSetDryRun(&cmdbuf, testFirewallRollbackHook, NULL);
7548c0
     } else {
7548c0
         fwBuf = &cmdbuf;
7548c0
@@ -634,7 +640,8 @@ testFirewallSingleRollback(const void *opaque G_GNUC_UNUSED)
7548c0
     if (virFirewallSetBackend(data->tryBackend) < 0)
7548c0
         goto cleanup;
7548c0
 
7548c0
-    if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT) {
7548c0
+    if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT ||
7548c0
+        data->expectBackend == VIR_FIREWALL_BACKEND_FIREWALLD) {
7548c0
         virCommandSetDryRun(&cmdbuf, testFirewallRollbackHook, NULL);
7548c0
     } else {
7548c0
         fwError = true;
7548c0
@@ -717,7 +724,8 @@ testFirewallManyRollback(const void *opaque G_GNUC_UNUSED)
7548c0
     if (virFirewallSetBackend(data->tryBackend) < 0)
7548c0
         goto cleanup;
7548c0
 
7548c0
-    if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT) {
7548c0
+    if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT ||
7548c0
+        data->expectBackend == VIR_FIREWALL_BACKEND_FIREWALLD) {
7548c0
         virCommandSetDryRun(&cmdbuf, testFirewallRollbackHook, NULL);
7548c0
     } else {
7548c0
         fwBuf = &cmdbuf;
7548c0
@@ -808,7 +816,8 @@ testFirewallChainedRollback(const void *opaque G_GNUC_UNUSED)
7548c0
     if (virFirewallSetBackend(data->tryBackend) < 0)
7548c0
         goto cleanup;
7548c0
 
7548c0
-    if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT) {
7548c0
+    if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT ||
7548c0
+        data->expectBackend == VIR_FIREWALL_BACKEND_FIREWALLD) {
7548c0
         virCommandSetDryRun(&cmdbuf, testFirewallRollbackHook, NULL);
7548c0
     } else {
7548c0
         fwBuf = &cmdbuf;
7548c0
@@ -1007,7 +1016,8 @@ testFirewallQuery(const void *opaque G_GNUC_UNUSED)
7548c0
     if (virFirewallSetBackend(data->tryBackend) < 0)
7548c0
         goto cleanup;
7548c0
 
7548c0
-    if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT) {
7548c0
+    if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT ||
7548c0
+        data->expectBackend == VIR_FIREWALL_BACKEND_FIREWALLD) {
7548c0
         virCommandSetDryRun(&cmdbuf, testFirewallQueryHook, NULL);
7548c0
     } else {
7548c0
         fwBuf = &cmdbuf;
7548c0
-- 
7548c0
2.30.0
7548c0