2013-03-04 20:30:40 +04:00
/*
* Copyright ( C ) 2013 - 2014 Red Hat , Inc .
*
* This library is free software ; you can redistribute it and / or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation ; either
* version 2.1 of the License , or ( at your option ) any later version .
*
* This library is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* Lesser General Public License for more details .
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library . If not , see
* < http : //www.gnu.org/licenses/>.
*/
# include <config.h>
# include "testutils.h"
2014-05-01 22:43:58 +04:00
# if defined(__linux__)
2013-03-04 20:30:40 +04:00
2020-09-15 15:00:53 +03:00
# include <gio / gio.h>
2014-05-01 22:43:58 +04:00
# include "virbuffer.h"
2018-12-13 17:53:50 +03:00
# define LIBVIRT_VIRCOMMANDPRIV_H_ALLOW
2014-05-01 22:43:58 +04:00
# include "vircommandpriv.h"
2018-12-13 17:53:50 +03:00
# define LIBVIRT_VIRFIREWALLPRIV_H_ALLOW
2014-05-01 22:43:58 +04:00
# include "virfirewallpriv.h"
2019-01-09 22:11:32 +03:00
# define LIBVIRT_VIRFIREWALLDPRIV_H_ALLOW
# include "virfirewalldpriv.h"
2014-05-01 22:43:58 +04:00
# include "virmock.h"
# define VIR_FROM_THIS VIR_FROM_FIREWALL
2013-03-04 20:30:40 +04:00
static bool fwDisabled = true ;
static virBufferPtr fwBuf ;
static bool fwError ;
2017-11-03 15:09:47 +03:00
# define TEST_FILTER_TABLE_LIST \
" Chain INPUT (policy ACCEPT) \n " \
" target prot opt source destination \n " \
" \n " \
" Chain FORWARD (policy ACCEPT) \n " \
" target prot opt source destination \n " \
" \n " \
" Chain OUTPUT (policy ACCEPT) \n " \
2013-03-04 20:30:40 +04:00
" target prot opt source destination \n "
2017-11-03 15:09:47 +03:00
# define TEST_NAT_TABLE_LIST \
" Chain PREROUTING (policy ACCEPT) \n " \
" target prot opt source destination \n " \
" \n " \
" Chain INPUT (policy ACCEPT) \n " \
" target prot opt source destination \n " \
" \n " \
" Chain OUTPUT (policy ACCEPT) \n " \
" target prot opt source destination \n " \
" \n " \
" Chain POSTROUTING (policy ACCEPT) \n " \
2013-03-04 20:30:40 +04:00
" target prot opt source destination \n "
2020-09-15 15:00:53 +03:00
VIR_MOCK_WRAP_RET_ARGS ( g_dbus_connection_call_sync ,
GVariant * ,
GDBusConnection * , connection ,
const gchar * , bus_name ,
const gchar * , object_path ,
const gchar * , interface_name ,
const gchar * , method_name ,
GVariant * , parameters ,
const GVariantType * , reply_type ,
GDBusCallFlags , flags ,
gint , timeout_msec ,
GCancellable * , cancellable ,
GError * * , error )
2013-03-04 20:30:40 +04:00
{
2020-09-15 15:00:53 +03:00
GVariant * reply = NULL ;
g_autoptr ( GVariant ) params = parameters ;
2020-10-02 13:11:45 +03:00
if ( params )
g_variant_ref_sink ( params ) ;
2020-09-15 15:00:53 +03:00
VIR_MOCK_REAL_INIT ( g_dbus_connection_call_sync ) ;
if ( STREQ ( bus_name , " org.freedesktop.DBus " ) & &
STREQ ( method_name , " ListNames " ) ) {
GVariantBuilder builder ;
g_variant_builder_init ( & builder , G_VARIANT_TYPE ( " (as) " ) ) ;
g_variant_builder_open ( & builder , G_VARIANT_TYPE ( " as " ) ) ;
g_variant_builder_add ( & builder , " s " , " org.foo.bar.wizz " ) ;
if ( ! fwDisabled )
g_variant_builder_add ( & builder , " s " , VIR_FIREWALL_FIREWALLD_SERVICE ) ;
g_variant_builder_close ( & builder ) ;
reply = g_variant_builder_end ( & builder ) ;
} else if ( STREQ ( bus_name , VIR_FIREWALL_FIREWALLD_SERVICE ) & &
STREQ ( method_name , " passthrough " ) ) {
g_autoptr ( GVariantIter ) iter = NULL ;
2021-02-05 00:43:40 +03:00
static const size_t maxargs = 5 ;
2020-12-01 11:21:32 +03:00
g_auto ( GStrv ) args = NULL ;
2020-09-15 15:00:53 +03:00
size_t nargs = 0 ;
char * type = NULL ;
char * item = NULL ;
2013-03-04 20:30:40 +04:00
bool isAdd = false ;
bool doError = false ;
2020-09-15 15:00:53 +03:00
g_variant_get ( params , " (&sas) " , & type , & iter ) ;
2013-03-04 20:30:40 +04:00
2021-02-05 00:43:40 +03:00
args = g_new0 ( char * , maxargs ) ;
2020-09-15 15:00:53 +03:00
2021-02-05 17:45:52 +03:00
if ( fwBuf ) {
if ( STREQ ( type , " ipv4 " ) )
virBufferAddLit ( fwBuf , IPTABLES_PATH ) ;
else if ( STREQ ( type , " ipv6 " ) )
virBufferAddLit ( fwBuf , IP6TABLES_PATH ) ;
else
virBufferAddLit ( fwBuf , EBTABLES_PATH ) ;
}
2020-09-15 15:00:53 +03:00
while ( g_variant_iter_loop ( iter , " s " , & item ) ) {
2013-03-04 20:30:40 +04:00
/* Fake failure on the command with this IP addr */
2020-09-15 15:00:53 +03:00
if ( STREQ ( item , " -A " ) ) {
2013-03-04 20:30:40 +04:00
isAdd = true ;
2020-09-15 15:00:53 +03:00
} else if ( isAdd & & STREQ ( item , " 192.168.122.255 " ) ) {
2013-03-04 20:30:40 +04:00
doError = true ;
}
2020-09-15 15:00:53 +03:00
2021-02-05 00:43:40 +03:00
if ( nargs < maxargs )
args [ nargs ] = g_strdup ( item ) ;
nargs + + ;
2020-09-15 15:00:53 +03:00
2013-03-04 20:30:40 +04:00
if ( fwBuf ) {
virBufferAddLit ( fwBuf , " " ) ;
2021-02-05 17:45:52 +03:00
virBufferEscapeShell ( fwBuf , item ) ;
2013-03-04 20:30:40 +04:00
}
}
2020-09-15 15:00:53 +03:00
2013-03-04 20:30:40 +04:00
if ( fwBuf )
virBufferAddLit ( fwBuf , " \n " ) ;
2020-09-15 15:00:53 +03:00
2013-03-04 20:30:40 +04:00
if ( doError ) {
2020-09-15 15:00:53 +03:00
if ( error )
* error = g_dbus_error_new_for_dbus_error ( " org.firewalld.error " ,
" something bad happened " ) ;
2013-03-04 20:30:40 +04:00
} else {
2020-11-17 03:20:53 +03:00
if ( nargs = = 2 & &
2013-03-04 20:30:40 +04:00
STREQ ( type , " ipv4 " ) & &
2020-11-17 03:20:53 +03:00
STREQ ( args [ 0 ] , " -w " ) & &
STREQ ( args [ 1 ] , " -L " ) ) {
2020-09-15 15:00:53 +03:00
reply = g_variant_new ( " (s) " , TEST_FILTER_TABLE_LIST ) ;
2020-11-17 03:20:53 +03:00
} else if ( nargs = = 4 & &
2013-03-04 20:30:40 +04:00
STREQ ( type , " ipv4 " ) & &
2020-11-17 03:20:53 +03:00
STREQ ( args [ 0 ] , " -w " ) & &
STREQ ( args [ 1 ] , " -t " ) & &
STREQ ( args [ 2 ] , " nat " ) & &
STREQ ( args [ 3 ] , " -L " ) ) {
2020-09-15 15:00:53 +03:00
reply = g_variant_new ( " (s) " , TEST_NAT_TABLE_LIST ) ;
2013-03-04 20:30:40 +04:00
} else {
2020-09-15 15:00:53 +03:00
reply = g_variant_new ( " (s) " , " success " ) ;
2013-03-04 20:30:40 +04:00
}
}
} else {
2020-09-15 15:00:53 +03:00
reply = g_variant_new ( " () " ) ;
2013-03-04 20:30:40 +04:00
}
return reply ;
}
struct testFirewallData {
virFirewallBackend tryBackend ;
virFirewallBackend expectBackend ;
bool fwDisabled ;
} ;
static int
testFirewallSingleGroup ( const void * opaque )
{
2020-07-03 02:35:41 +03:00
g_auto ( virBuffer ) cmdbuf = VIR_BUFFER_INITIALIZER ;
2020-07-04 23:35:10 +03:00
g_autoptr ( virFirewall ) fw = virFirewallNew ( ) ;
2013-03-04 20:30:40 +04:00
int ret = - 1 ;
const char * actual = NULL ;
const char * expected =
2020-11-17 21:51:45 +03:00
IPTABLES_PATH " -w -A INPUT --source 192.168.122.1 --jump ACCEPT \n "
IPTABLES_PATH " -w -A INPUT --source '!192.168.122.1' --jump REJECT \n " ;
2013-03-04 20:30:40 +04:00
const struct testFirewallData * data = opaque ;
2021-04-01 18:54:09 +03:00
g_autoptr ( virCommandDryRunToken ) dryRunToken = virCommandDryRunTokenNew ( ) ;
2013-03-04 20:30:40 +04:00
fwDisabled = data - > fwDisabled ;
if ( virFirewallSetBackend ( data - > tryBackend ) < 0 )
goto cleanup ;
util: call iptables directly rather than via firewalld
When libvirt added support for firewalld, we were unable to use
firewalld's higher level rules, because they weren't detailed enough
and could not be applied to the iptables FORWARD or OUTPUT chains
(only to the INPUT chain). Instead we changed our code so that rather
than running the iptables/ip6tables/ebtables binaries ourselves, we
would send these commands to firewalld as "passthrough commands", and
firewalld would run the appropriate program on our behalf.
This was done under the assumption that firewalld was somehow tracking
all these rules, and that this tracking was benefitting proper
operation of firewalld and the system in general.
Several years later this came up in a discussion on IRC, and we
learned from the firewalld developers that, in fact, adding iptables
and ebtables rules with firewalld's passthrough commands actually has
*no* advantage; firewalld doesn't keep track of these rules in any
way, and doesn't use them to tailor the construction of its own rules.
Meanwhile, users have been complaining for some time that whenever
firewalld is restarted on a system with libvirt virtual networks
and/or nwfilter rules active, the system logs would be flooded with
warning messages whining that [lots of different rules] could not be
deleted because they didn't exist. For example:
firewalld[3536040]: WARNING: COMMAND_FAILED:
'/usr/sbin/iptables -w10 -w --table filter --delete LIBVIRT_OUT
--out-interface virbr4 --protocol udp --destination-port 68
--jump ACCEPT' failed: iptables: Bad rule
(does a matching rule exist in that chain?).
(See https://bugzilla.redhat.com/1790837 for many more examples and a
discussion)
Note that these messages are created by iptables, but are logged by
firewalld - when an iptables/ebtables command fails, firewalld grabs
whatever is in stderr of the program, and spits it out to the system
log as a warning. We've requested that firewalld not do this (and
instead leave it up to the calling application to do the appropriate
logging), but this request has been respectfully denied.
But combining the two problems above ( 1) firewalld doesn't do
anything useful when you use it as a proxy to add/remove iptables
rules, 2) firewalld often insists on logging lots of
annoying/misleading/useless "error" messages when you use it as a
proxy to remove iptables rules that don't already exist), leads to a
solution - simply stop using firewalld to add and remove iptables
rules. Instead, exec iptables/ip6tables/ebtables directly in the same
way we do when firewalld isn't active.
We still need to keep track of whether or not firewalld is active, as
there are some things that must be done, e.g. we need to add some
actual firewalld rules in the firewalld "libvirt" zone, and we need to
take notice when firewalld restarts, so that we can reload all our
rules.
This patch doesn't remove the infrastructure that allows having
different firewall backends that perform their functions in different
ways, as that will very possibly come in handy in the future when we
want to have an nftables direct backend, and possibly a "pure"
firewalld backend (now that firewalld supports more complex rules, and
can add those rules to the FORWARD and OUTPUT chains). Instead, it
just changes the action when the selected backend is "firewalld" so
that it adds rules directly rather than through firewalld, while
leaving as much of the existing code intact as possible.
In order for tests to still pass, virfirewalltest also had to be
modified to behave in a different way (i.e. by capturing the generated
commandline as it does for the DIRECT backend, rather than capturing
dbus messages using a mocked dbus API).
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2020-11-17 18:55:12 +03:00
if ( data - > expectBackend = = VIR_FIREWALL_BACKEND_DIRECT | |
data - > expectBackend = = VIR_FIREWALL_BACKEND_FIREWALLD )
2021-04-06 11:56:23 +03:00
virCommandSetDryRun ( dryRunToken , & cmdbuf , false , false , NULL , NULL ) ;
2013-03-04 20:30:40 +04:00
else
fwBuf = & cmdbuf ;
virFirewallStartTransaction ( fw , 0 ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " ACCEPT " , NULL ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " !192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
if ( virFirewallApply ( fw ) < 0 )
goto cleanup ;
actual = virBufferCurrentContent ( & cmdbuf ) ;
if ( STRNEQ_NULLABLE ( expected , actual ) ) {
2018-12-04 20:08:14 +03:00
fprintf ( stderr , " Unexpected command execution \n " ) ;
2016-05-26 18:01:51 +03:00
virTestDifference ( stderr , expected , actual ) ;
2013-03-04 20:30:40 +04:00
goto cleanup ;
}
ret = 0 ;
cleanup :
fwBuf = NULL ;
return ret ;
}
static int
testFirewallRemoveRule ( const void * opaque )
{
2020-07-03 02:35:41 +03:00
g_auto ( virBuffer ) cmdbuf = VIR_BUFFER_INITIALIZER ;
2020-07-04 23:35:10 +03:00
g_autoptr ( virFirewall ) fw = virFirewallNew ( ) ;
2013-03-04 20:30:40 +04:00
int ret = - 1 ;
const char * actual = NULL ;
const char * expected =
2020-11-17 21:51:45 +03:00
IPTABLES_PATH " -w -A INPUT --source 192.168.122.1 --jump ACCEPT \n "
IPTABLES_PATH " -w -A INPUT --source '!192.168.122.1' --jump REJECT \n " ;
2013-03-04 20:30:40 +04:00
const struct testFirewallData * data = opaque ;
virFirewallRulePtr fwrule ;
2021-04-01 18:54:09 +03:00
g_autoptr ( virCommandDryRunToken ) dryRunToken = virCommandDryRunTokenNew ( ) ;
2013-03-04 20:30:40 +04:00
fwDisabled = data - > fwDisabled ;
if ( virFirewallSetBackend ( data - > tryBackend ) < 0 )
goto cleanup ;
util: call iptables directly rather than via firewalld
When libvirt added support for firewalld, we were unable to use
firewalld's higher level rules, because they weren't detailed enough
and could not be applied to the iptables FORWARD or OUTPUT chains
(only to the INPUT chain). Instead we changed our code so that rather
than running the iptables/ip6tables/ebtables binaries ourselves, we
would send these commands to firewalld as "passthrough commands", and
firewalld would run the appropriate program on our behalf.
This was done under the assumption that firewalld was somehow tracking
all these rules, and that this tracking was benefitting proper
operation of firewalld and the system in general.
Several years later this came up in a discussion on IRC, and we
learned from the firewalld developers that, in fact, adding iptables
and ebtables rules with firewalld's passthrough commands actually has
*no* advantage; firewalld doesn't keep track of these rules in any
way, and doesn't use them to tailor the construction of its own rules.
Meanwhile, users have been complaining for some time that whenever
firewalld is restarted on a system with libvirt virtual networks
and/or nwfilter rules active, the system logs would be flooded with
warning messages whining that [lots of different rules] could not be
deleted because they didn't exist. For example:
firewalld[3536040]: WARNING: COMMAND_FAILED:
'/usr/sbin/iptables -w10 -w --table filter --delete LIBVIRT_OUT
--out-interface virbr4 --protocol udp --destination-port 68
--jump ACCEPT' failed: iptables: Bad rule
(does a matching rule exist in that chain?).
(See https://bugzilla.redhat.com/1790837 for many more examples and a
discussion)
Note that these messages are created by iptables, but are logged by
firewalld - when an iptables/ebtables command fails, firewalld grabs
whatever is in stderr of the program, and spits it out to the system
log as a warning. We've requested that firewalld not do this (and
instead leave it up to the calling application to do the appropriate
logging), but this request has been respectfully denied.
But combining the two problems above ( 1) firewalld doesn't do
anything useful when you use it as a proxy to add/remove iptables
rules, 2) firewalld often insists on logging lots of
annoying/misleading/useless "error" messages when you use it as a
proxy to remove iptables rules that don't already exist), leads to a
solution - simply stop using firewalld to add and remove iptables
rules. Instead, exec iptables/ip6tables/ebtables directly in the same
way we do when firewalld isn't active.
We still need to keep track of whether or not firewalld is active, as
there are some things that must be done, e.g. we need to add some
actual firewalld rules in the firewalld "libvirt" zone, and we need to
take notice when firewalld restarts, so that we can reload all our
rules.
This patch doesn't remove the infrastructure that allows having
different firewall backends that perform their functions in different
ways, as that will very possibly come in handy in the future when we
want to have an nftables direct backend, and possibly a "pure"
firewalld backend (now that firewalld supports more complex rules, and
can add those rules to the FORWARD and OUTPUT chains). Instead, it
just changes the action when the selected backend is "firewalld" so
that it adds rules directly rather than through firewalld, while
leaving as much of the existing code intact as possible.
In order for tests to still pass, virfirewalltest also had to be
modified to behave in a different way (i.e. by capturing the generated
commandline as it does for the DIRECT backend, rather than capturing
dbus messages using a mocked dbus API).
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2020-11-17 18:55:12 +03:00
if ( data - > expectBackend = = VIR_FIREWALL_BACKEND_DIRECT | |
data - > expectBackend = = VIR_FIREWALL_BACKEND_FIREWALLD )
2021-04-06 11:56:23 +03:00
virCommandSetDryRun ( dryRunToken , & cmdbuf , false , false , NULL , NULL ) ;
2013-03-04 20:30:40 +04:00
else
fwBuf = & cmdbuf ;
virFirewallStartTransaction ( fw , 0 ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " ACCEPT " , NULL ) ;
fwrule = virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " , NULL ) ;
2020-11-17 21:51:45 +03:00
virFirewallRuleAddArg ( fw , fwrule , " --source " ) ;
2013-03-04 20:30:40 +04:00
virFirewallRemoveRule ( fw , fwrule ) ;
fwrule = virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " , NULL ) ;
2020-11-17 21:51:45 +03:00
virFirewallRuleAddArg ( fw , fwrule , " --source " ) ;
2013-03-04 20:30:40 +04:00
virFirewallRuleAddArgFormat ( fw , fwrule , " %s " , " !192.168.122.1 " ) ;
virFirewallRuleAddArgList ( fw , fwrule , " --jump " , " REJECT " , NULL ) ;
if ( virFirewallApply ( fw ) < 0 )
goto cleanup ;
actual = virBufferCurrentContent ( & cmdbuf ) ;
if ( STRNEQ_NULLABLE ( expected , actual ) ) {
2018-12-04 20:08:14 +03:00
fprintf ( stderr , " Unexpected command execution \n " ) ;
2016-05-26 18:01:51 +03:00
virTestDifference ( stderr , expected , actual ) ;
2013-03-04 20:30:40 +04:00
goto cleanup ;
}
ret = 0 ;
cleanup :
fwBuf = NULL ;
return ret ;
}
static int
2019-10-14 15:45:03 +03:00
testFirewallManyGroups ( const void * opaque G_GNUC_UNUSED )
2013-03-04 20:30:40 +04:00
{
2020-07-03 02:35:41 +03:00
g_auto ( virBuffer ) cmdbuf = VIR_BUFFER_INITIALIZER ;
2020-07-04 23:35:10 +03:00
g_autoptr ( virFirewall ) fw = virFirewallNew ( ) ;
2013-03-04 20:30:40 +04:00
int ret = - 1 ;
const char * actual = NULL ;
const char * expected =
2020-11-17 21:51:45 +03:00
IPTABLES_PATH " -w -A INPUT --source 192.168.122.1 --jump ACCEPT \n "
IPTABLES_PATH " -w -A INPUT --source '!192.168.122.1' --jump REJECT \n "
IPTABLES_PATH " -w -A OUTPUT --source 192.168.122.1 --jump ACCEPT \n "
2020-11-17 03:20:53 +03:00
IPTABLES_PATH " -w -A OUTPUT --jump DROP \n " ;
2013-03-04 20:30:40 +04:00
const struct testFirewallData * data = opaque ;
2021-04-01 18:54:09 +03:00
g_autoptr ( virCommandDryRunToken ) dryRunToken = virCommandDryRunTokenNew ( ) ;
2013-03-04 20:30:40 +04:00
fwDisabled = data - > fwDisabled ;
if ( virFirewallSetBackend ( data - > tryBackend ) < 0 )
goto cleanup ;
util: call iptables directly rather than via firewalld
When libvirt added support for firewalld, we were unable to use
firewalld's higher level rules, because they weren't detailed enough
and could not be applied to the iptables FORWARD or OUTPUT chains
(only to the INPUT chain). Instead we changed our code so that rather
than running the iptables/ip6tables/ebtables binaries ourselves, we
would send these commands to firewalld as "passthrough commands", and
firewalld would run the appropriate program on our behalf.
This was done under the assumption that firewalld was somehow tracking
all these rules, and that this tracking was benefitting proper
operation of firewalld and the system in general.
Several years later this came up in a discussion on IRC, and we
learned from the firewalld developers that, in fact, adding iptables
and ebtables rules with firewalld's passthrough commands actually has
*no* advantage; firewalld doesn't keep track of these rules in any
way, and doesn't use them to tailor the construction of its own rules.
Meanwhile, users have been complaining for some time that whenever
firewalld is restarted on a system with libvirt virtual networks
and/or nwfilter rules active, the system logs would be flooded with
warning messages whining that [lots of different rules] could not be
deleted because they didn't exist. For example:
firewalld[3536040]: WARNING: COMMAND_FAILED:
'/usr/sbin/iptables -w10 -w --table filter --delete LIBVIRT_OUT
--out-interface virbr4 --protocol udp --destination-port 68
--jump ACCEPT' failed: iptables: Bad rule
(does a matching rule exist in that chain?).
(See https://bugzilla.redhat.com/1790837 for many more examples and a
discussion)
Note that these messages are created by iptables, but are logged by
firewalld - when an iptables/ebtables command fails, firewalld grabs
whatever is in stderr of the program, and spits it out to the system
log as a warning. We've requested that firewalld not do this (and
instead leave it up to the calling application to do the appropriate
logging), but this request has been respectfully denied.
But combining the two problems above ( 1) firewalld doesn't do
anything useful when you use it as a proxy to add/remove iptables
rules, 2) firewalld often insists on logging lots of
annoying/misleading/useless "error" messages when you use it as a
proxy to remove iptables rules that don't already exist), leads to a
solution - simply stop using firewalld to add and remove iptables
rules. Instead, exec iptables/ip6tables/ebtables directly in the same
way we do when firewalld isn't active.
We still need to keep track of whether or not firewalld is active, as
there are some things that must be done, e.g. we need to add some
actual firewalld rules in the firewalld "libvirt" zone, and we need to
take notice when firewalld restarts, so that we can reload all our
rules.
This patch doesn't remove the infrastructure that allows having
different firewall backends that perform their functions in different
ways, as that will very possibly come in handy in the future when we
want to have an nftables direct backend, and possibly a "pure"
firewalld backend (now that firewalld supports more complex rules, and
can add those rules to the FORWARD and OUTPUT chains). Instead, it
just changes the action when the selected backend is "firewalld" so
that it adds rules directly rather than through firewalld, while
leaving as much of the existing code intact as possible.
In order for tests to still pass, virfirewalltest also had to be
modified to behave in a different way (i.e. by capturing the generated
commandline as it does for the DIRECT backend, rather than capturing
dbus messages using a mocked dbus API).
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2020-11-17 18:55:12 +03:00
if ( data - > expectBackend = = VIR_FIREWALL_BACKEND_DIRECT | |
data - > expectBackend = = VIR_FIREWALL_BACKEND_FIREWALLD )
2021-04-06 11:56:23 +03:00
virCommandSetDryRun ( dryRunToken , & cmdbuf , false , false , NULL , NULL ) ;
2013-03-04 20:30:40 +04:00
else
fwBuf = & cmdbuf ;
virFirewallStartTransaction ( fw , 0 ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " ACCEPT " , NULL ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " !192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
virFirewallStartTransaction ( fw , 0 ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " OUTPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " ACCEPT " , NULL ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " OUTPUT " ,
" --jump " , " DROP " , NULL ) ;
if ( virFirewallApply ( fw ) < 0 )
goto cleanup ;
actual = virBufferCurrentContent ( & cmdbuf ) ;
if ( STRNEQ_NULLABLE ( expected , actual ) ) {
2018-12-04 20:08:14 +03:00
fprintf ( stderr , " Unexpected command execution \n " ) ;
2016-05-26 18:01:51 +03:00
virTestDifference ( stderr , expected , actual ) ;
2013-03-04 20:30:40 +04:00
goto cleanup ;
}
ret = 0 ;
cleanup :
fwBuf = NULL ;
return ret ;
}
static void
testFirewallRollbackHook ( const char * const * args ,
2019-10-14 15:45:03 +03:00
const char * const * env G_GNUC_UNUSED ,
const char * input G_GNUC_UNUSED ,
char * * output G_GNUC_UNUSED ,
char * * error G_GNUC_UNUSED ,
2013-03-04 20:30:40 +04:00
int * status ,
2019-10-14 15:45:03 +03:00
void * opaque G_GNUC_UNUSED )
2013-03-04 20:30:40 +04:00
{
bool isAdd = false ;
while ( * args ) {
/* Fake failure on the command with this IP addr */
if ( STREQ ( * args , " -A " ) ) {
isAdd = true ;
} else if ( isAdd & & STREQ ( * args , " 192.168.122.255 " ) ) {
* status = 127 ;
break ;
}
args + + ;
}
}
static int
2019-10-14 15:45:03 +03:00
testFirewallIgnoreFailGroup ( const void * opaque G_GNUC_UNUSED )
2013-03-04 20:30:40 +04:00
{
2020-07-03 02:35:41 +03:00
g_auto ( virBuffer ) cmdbuf = VIR_BUFFER_INITIALIZER ;
2020-07-04 23:35:10 +03:00
g_autoptr ( virFirewall ) fw = virFirewallNew ( ) ;
2013-03-04 20:30:40 +04:00
int ret = - 1 ;
const char * actual = NULL ;
const char * expected =
2020-11-17 21:51:45 +03:00
IPTABLES_PATH " -w -A INPUT --source 192.168.122.1 --jump ACCEPT \n "
IPTABLES_PATH " -w -A INPUT --source 192.168.122.255 --jump REJECT \n "
IPTABLES_PATH " -w -A OUTPUT --source 192.168.122.1 --jump ACCEPT \n "
2020-11-17 03:20:53 +03:00
IPTABLES_PATH " -w -A OUTPUT --jump DROP \n " ;
2013-03-04 20:30:40 +04:00
const struct testFirewallData * data = opaque ;
2021-04-01 18:54:09 +03:00
g_autoptr ( virCommandDryRunToken ) dryRunToken = virCommandDryRunTokenNew ( ) ;
2013-03-04 20:30:40 +04:00
fwDisabled = data - > fwDisabled ;
if ( virFirewallSetBackend ( data - > tryBackend ) < 0 )
goto cleanup ;
util: call iptables directly rather than via firewalld
When libvirt added support for firewalld, we were unable to use
firewalld's higher level rules, because they weren't detailed enough
and could not be applied to the iptables FORWARD or OUTPUT chains
(only to the INPUT chain). Instead we changed our code so that rather
than running the iptables/ip6tables/ebtables binaries ourselves, we
would send these commands to firewalld as "passthrough commands", and
firewalld would run the appropriate program on our behalf.
This was done under the assumption that firewalld was somehow tracking
all these rules, and that this tracking was benefitting proper
operation of firewalld and the system in general.
Several years later this came up in a discussion on IRC, and we
learned from the firewalld developers that, in fact, adding iptables
and ebtables rules with firewalld's passthrough commands actually has
*no* advantage; firewalld doesn't keep track of these rules in any
way, and doesn't use them to tailor the construction of its own rules.
Meanwhile, users have been complaining for some time that whenever
firewalld is restarted on a system with libvirt virtual networks
and/or nwfilter rules active, the system logs would be flooded with
warning messages whining that [lots of different rules] could not be
deleted because they didn't exist. For example:
firewalld[3536040]: WARNING: COMMAND_FAILED:
'/usr/sbin/iptables -w10 -w --table filter --delete LIBVIRT_OUT
--out-interface virbr4 --protocol udp --destination-port 68
--jump ACCEPT' failed: iptables: Bad rule
(does a matching rule exist in that chain?).
(See https://bugzilla.redhat.com/1790837 for many more examples and a
discussion)
Note that these messages are created by iptables, but are logged by
firewalld - when an iptables/ebtables command fails, firewalld grabs
whatever is in stderr of the program, and spits it out to the system
log as a warning. We've requested that firewalld not do this (and
instead leave it up to the calling application to do the appropriate
logging), but this request has been respectfully denied.
But combining the two problems above ( 1) firewalld doesn't do
anything useful when you use it as a proxy to add/remove iptables
rules, 2) firewalld often insists on logging lots of
annoying/misleading/useless "error" messages when you use it as a
proxy to remove iptables rules that don't already exist), leads to a
solution - simply stop using firewalld to add and remove iptables
rules. Instead, exec iptables/ip6tables/ebtables directly in the same
way we do when firewalld isn't active.
We still need to keep track of whether or not firewalld is active, as
there are some things that must be done, e.g. we need to add some
actual firewalld rules in the firewalld "libvirt" zone, and we need to
take notice when firewalld restarts, so that we can reload all our
rules.
This patch doesn't remove the infrastructure that allows having
different firewall backends that perform their functions in different
ways, as that will very possibly come in handy in the future when we
want to have an nftables direct backend, and possibly a "pure"
firewalld backend (now that firewalld supports more complex rules, and
can add those rules to the FORWARD and OUTPUT chains). Instead, it
just changes the action when the selected backend is "firewalld" so
that it adds rules directly rather than through firewalld, while
leaving as much of the existing code intact as possible.
In order for tests to still pass, virfirewalltest also had to be
modified to behave in a different way (i.e. by capturing the generated
commandline as it does for the DIRECT backend, rather than capturing
dbus messages using a mocked dbus API).
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2020-11-17 18:55:12 +03:00
if ( data - > expectBackend = = VIR_FIREWALL_BACKEND_DIRECT | |
data - > expectBackend = = VIR_FIREWALL_BACKEND_FIREWALLD ) {
2021-04-06 11:56:23 +03:00
virCommandSetDryRun ( dryRunToken , & cmdbuf , false , false , testFirewallRollbackHook , NULL ) ;
2013-03-04 20:30:40 +04:00
} else {
fwBuf = & cmdbuf ;
fwError = true ;
}
virFirewallStartTransaction ( fw , VIR_FIREWALL_TRANSACTION_IGNORE_ERRORS ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " ACCEPT " , NULL ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.255 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
virFirewallStartTransaction ( fw , 0 ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " OUTPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " ACCEPT " , NULL ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " OUTPUT " ,
" --jump " , " DROP " , NULL ) ;
if ( virFirewallApply ( fw ) < 0 )
goto cleanup ;
actual = virBufferCurrentContent ( & cmdbuf ) ;
if ( STRNEQ_NULLABLE ( expected , actual ) ) {
2018-12-04 20:08:14 +03:00
fprintf ( stderr , " Unexpected command execution \n " ) ;
2016-05-26 18:01:51 +03:00
virTestDifference ( stderr , expected , actual ) ;
2013-03-04 20:30:40 +04:00
goto cleanup ;
}
ret = 0 ;
cleanup :
fwBuf = NULL ;
return ret ;
}
static int
2019-10-14 15:45:03 +03:00
testFirewallIgnoreFailRule ( const void * opaque G_GNUC_UNUSED )
2013-03-04 20:30:40 +04:00
{
2020-07-03 02:35:41 +03:00
g_auto ( virBuffer ) cmdbuf = VIR_BUFFER_INITIALIZER ;
2020-07-04 23:35:10 +03:00
g_autoptr ( virFirewall ) fw = virFirewallNew ( ) ;
2013-03-04 20:30:40 +04:00
int ret = - 1 ;
const char * actual = NULL ;
const char * expected =
2020-11-17 21:51:45 +03:00
IPTABLES_PATH " -w -A INPUT --source 192.168.122.1 --jump ACCEPT \n "
IPTABLES_PATH " -w -A INPUT --source 192.168.122.255 --jump REJECT \n "
IPTABLES_PATH " -w -A OUTPUT --source 192.168.122.1 --jump ACCEPT \n "
2020-11-17 03:20:53 +03:00
IPTABLES_PATH " -w -A OUTPUT --jump DROP \n " ;
2013-03-04 20:30:40 +04:00
const struct testFirewallData * data = opaque ;
2021-04-01 18:54:09 +03:00
g_autoptr ( virCommandDryRunToken ) dryRunToken = virCommandDryRunTokenNew ( ) ;
2013-03-04 20:30:40 +04:00
fwDisabled = data - > fwDisabled ;
if ( virFirewallSetBackend ( data - > tryBackend ) < 0 )
goto cleanup ;
util: call iptables directly rather than via firewalld
When libvirt added support for firewalld, we were unable to use
firewalld's higher level rules, because they weren't detailed enough
and could not be applied to the iptables FORWARD or OUTPUT chains
(only to the INPUT chain). Instead we changed our code so that rather
than running the iptables/ip6tables/ebtables binaries ourselves, we
would send these commands to firewalld as "passthrough commands", and
firewalld would run the appropriate program on our behalf.
This was done under the assumption that firewalld was somehow tracking
all these rules, and that this tracking was benefitting proper
operation of firewalld and the system in general.
Several years later this came up in a discussion on IRC, and we
learned from the firewalld developers that, in fact, adding iptables
and ebtables rules with firewalld's passthrough commands actually has
*no* advantage; firewalld doesn't keep track of these rules in any
way, and doesn't use them to tailor the construction of its own rules.
Meanwhile, users have been complaining for some time that whenever
firewalld is restarted on a system with libvirt virtual networks
and/or nwfilter rules active, the system logs would be flooded with
warning messages whining that [lots of different rules] could not be
deleted because they didn't exist. For example:
firewalld[3536040]: WARNING: COMMAND_FAILED:
'/usr/sbin/iptables -w10 -w --table filter --delete LIBVIRT_OUT
--out-interface virbr4 --protocol udp --destination-port 68
--jump ACCEPT' failed: iptables: Bad rule
(does a matching rule exist in that chain?).
(See https://bugzilla.redhat.com/1790837 for many more examples and a
discussion)
Note that these messages are created by iptables, but are logged by
firewalld - when an iptables/ebtables command fails, firewalld grabs
whatever is in stderr of the program, and spits it out to the system
log as a warning. We've requested that firewalld not do this (and
instead leave it up to the calling application to do the appropriate
logging), but this request has been respectfully denied.
But combining the two problems above ( 1) firewalld doesn't do
anything useful when you use it as a proxy to add/remove iptables
rules, 2) firewalld often insists on logging lots of
annoying/misleading/useless "error" messages when you use it as a
proxy to remove iptables rules that don't already exist), leads to a
solution - simply stop using firewalld to add and remove iptables
rules. Instead, exec iptables/ip6tables/ebtables directly in the same
way we do when firewalld isn't active.
We still need to keep track of whether or not firewalld is active, as
there are some things that must be done, e.g. we need to add some
actual firewalld rules in the firewalld "libvirt" zone, and we need to
take notice when firewalld restarts, so that we can reload all our
rules.
This patch doesn't remove the infrastructure that allows having
different firewall backends that perform their functions in different
ways, as that will very possibly come in handy in the future when we
want to have an nftables direct backend, and possibly a "pure"
firewalld backend (now that firewalld supports more complex rules, and
can add those rules to the FORWARD and OUTPUT chains). Instead, it
just changes the action when the selected backend is "firewalld" so
that it adds rules directly rather than through firewalld, while
leaving as much of the existing code intact as possible.
In order for tests to still pass, virfirewalltest also had to be
modified to behave in a different way (i.e. by capturing the generated
commandline as it does for the DIRECT backend, rather than capturing
dbus messages using a mocked dbus API).
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2020-11-17 18:55:12 +03:00
if ( data - > expectBackend = = VIR_FIREWALL_BACKEND_DIRECT | |
data - > expectBackend = = VIR_FIREWALL_BACKEND_FIREWALLD ) {
2021-04-06 11:56:23 +03:00
virCommandSetDryRun ( dryRunToken , & cmdbuf , false , false , testFirewallRollbackHook , NULL ) ;
2013-03-04 20:30:40 +04:00
} else {
fwBuf = & cmdbuf ;
fwError = true ;
}
virFirewallStartTransaction ( fw , 0 ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " ACCEPT " , NULL ) ;
virFirewallAddRuleFull ( fw , VIR_FIREWALL_LAYER_IPV4 ,
true , NULL , NULL ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.255 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " OUTPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " ACCEPT " , NULL ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " OUTPUT " ,
" --jump " , " DROP " , NULL ) ;
if ( virFirewallApply ( fw ) < 0 )
goto cleanup ;
actual = virBufferCurrentContent ( & cmdbuf ) ;
if ( STRNEQ_NULLABLE ( expected , actual ) ) {
2018-12-04 20:08:14 +03:00
fprintf ( stderr , " Unexpected command execution \n " ) ;
2016-05-26 18:01:51 +03:00
virTestDifference ( stderr , expected , actual ) ;
2013-03-04 20:30:40 +04:00
goto cleanup ;
}
ret = 0 ;
cleanup :
fwBuf = NULL ;
return ret ;
}
static int
2019-10-14 15:45:03 +03:00
testFirewallNoRollback ( const void * opaque G_GNUC_UNUSED )
2013-03-04 20:30:40 +04:00
{
2020-07-03 02:35:41 +03:00
g_auto ( virBuffer ) cmdbuf = VIR_BUFFER_INITIALIZER ;
2020-07-04 23:35:10 +03:00
g_autoptr ( virFirewall ) fw = virFirewallNew ( ) ;
2013-03-04 20:30:40 +04:00
int ret = - 1 ;
const char * actual = NULL ;
const char * expected =
2020-11-17 21:51:45 +03:00
IPTABLES_PATH " -w -A INPUT --source 192.168.122.1 --jump ACCEPT \n "
IPTABLES_PATH " -w -A INPUT --source 192.168.122.255 --jump REJECT \n " ;
2013-03-04 20:30:40 +04:00
const struct testFirewallData * data = opaque ;
2021-04-01 18:54:09 +03:00
g_autoptr ( virCommandDryRunToken ) dryRunToken = virCommandDryRunTokenNew ( ) ;
2013-03-04 20:30:40 +04:00
fwDisabled = data - > fwDisabled ;
if ( virFirewallSetBackend ( data - > tryBackend ) < 0 )
goto cleanup ;
util: call iptables directly rather than via firewalld
When libvirt added support for firewalld, we were unable to use
firewalld's higher level rules, because they weren't detailed enough
and could not be applied to the iptables FORWARD or OUTPUT chains
(only to the INPUT chain). Instead we changed our code so that rather
than running the iptables/ip6tables/ebtables binaries ourselves, we
would send these commands to firewalld as "passthrough commands", and
firewalld would run the appropriate program on our behalf.
This was done under the assumption that firewalld was somehow tracking
all these rules, and that this tracking was benefitting proper
operation of firewalld and the system in general.
Several years later this came up in a discussion on IRC, and we
learned from the firewalld developers that, in fact, adding iptables
and ebtables rules with firewalld's passthrough commands actually has
*no* advantage; firewalld doesn't keep track of these rules in any
way, and doesn't use them to tailor the construction of its own rules.
Meanwhile, users have been complaining for some time that whenever
firewalld is restarted on a system with libvirt virtual networks
and/or nwfilter rules active, the system logs would be flooded with
warning messages whining that [lots of different rules] could not be
deleted because they didn't exist. For example:
firewalld[3536040]: WARNING: COMMAND_FAILED:
'/usr/sbin/iptables -w10 -w --table filter --delete LIBVIRT_OUT
--out-interface virbr4 --protocol udp --destination-port 68
--jump ACCEPT' failed: iptables: Bad rule
(does a matching rule exist in that chain?).
(See https://bugzilla.redhat.com/1790837 for many more examples and a
discussion)
Note that these messages are created by iptables, but are logged by
firewalld - when an iptables/ebtables command fails, firewalld grabs
whatever is in stderr of the program, and spits it out to the system
log as a warning. We've requested that firewalld not do this (and
instead leave it up to the calling application to do the appropriate
logging), but this request has been respectfully denied.
But combining the two problems above ( 1) firewalld doesn't do
anything useful when you use it as a proxy to add/remove iptables
rules, 2) firewalld often insists on logging lots of
annoying/misleading/useless "error" messages when you use it as a
proxy to remove iptables rules that don't already exist), leads to a
solution - simply stop using firewalld to add and remove iptables
rules. Instead, exec iptables/ip6tables/ebtables directly in the same
way we do when firewalld isn't active.
We still need to keep track of whether or not firewalld is active, as
there are some things that must be done, e.g. we need to add some
actual firewalld rules in the firewalld "libvirt" zone, and we need to
take notice when firewalld restarts, so that we can reload all our
rules.
This patch doesn't remove the infrastructure that allows having
different firewall backends that perform their functions in different
ways, as that will very possibly come in handy in the future when we
want to have an nftables direct backend, and possibly a "pure"
firewalld backend (now that firewalld supports more complex rules, and
can add those rules to the FORWARD and OUTPUT chains). Instead, it
just changes the action when the selected backend is "firewalld" so
that it adds rules directly rather than through firewalld, while
leaving as much of the existing code intact as possible.
In order for tests to still pass, virfirewalltest also had to be
modified to behave in a different way (i.e. by capturing the generated
commandline as it does for the DIRECT backend, rather than capturing
dbus messages using a mocked dbus API).
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2020-11-17 18:55:12 +03:00
if ( data - > expectBackend = = VIR_FIREWALL_BACKEND_DIRECT | |
data - > expectBackend = = VIR_FIREWALL_BACKEND_FIREWALLD ) {
2021-04-06 11:56:23 +03:00
virCommandSetDryRun ( dryRunToken , & cmdbuf , false , false , testFirewallRollbackHook , NULL ) ;
2013-03-04 20:30:40 +04:00
} else {
fwBuf = & cmdbuf ;
fwError = true ;
}
virFirewallStartTransaction ( fw , 0 ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " ACCEPT " , NULL ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.255 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " !192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
if ( virFirewallApply ( fw ) = = 0 ) {
fprintf ( stderr , " Firewall apply unexpectedly worked \n " ) ;
goto cleanup ;
}
actual = virBufferCurrentContent ( & cmdbuf ) ;
if ( STRNEQ_NULLABLE ( expected , actual ) ) {
2018-12-04 20:08:14 +03:00
fprintf ( stderr , " Unexpected command execution \n " ) ;
2016-05-26 18:01:51 +03:00
virTestDifference ( stderr , expected , actual ) ;
2013-03-04 20:30:40 +04:00
goto cleanup ;
}
ret = 0 ;
cleanup :
fwBuf = NULL ;
return ret ;
}
static int
2019-10-14 15:45:03 +03:00
testFirewallSingleRollback ( const void * opaque G_GNUC_UNUSED )
2013-03-04 20:30:40 +04:00
{
2020-07-03 02:35:41 +03:00
g_auto ( virBuffer ) cmdbuf = VIR_BUFFER_INITIALIZER ;
2020-07-04 23:35:10 +03:00
g_autoptr ( virFirewall ) fw = virFirewallNew ( ) ;
2013-03-04 20:30:40 +04:00
int ret = - 1 ;
const char * actual = NULL ;
const char * expected =
2020-11-17 21:51:45 +03:00
IPTABLES_PATH " -w -A INPUT --source 192.168.122.1 --jump ACCEPT \n "
IPTABLES_PATH " -w -A INPUT --source 192.168.122.255 --jump REJECT \n "
IPTABLES_PATH " -w -D INPUT --source 192.168.122.1 --jump ACCEPT \n "
IPTABLES_PATH " -w -D INPUT --source 192.168.122.255 --jump REJECT \n "
IPTABLES_PATH " -w -D INPUT --source '!192.168.122.1' --jump REJECT \n " ;
2013-03-04 20:30:40 +04:00
const struct testFirewallData * data = opaque ;
2021-04-01 18:54:09 +03:00
g_autoptr ( virCommandDryRunToken ) dryRunToken = virCommandDryRunTokenNew ( ) ;
2013-03-04 20:30:40 +04:00
fwDisabled = data - > fwDisabled ;
if ( virFirewallSetBackend ( data - > tryBackend ) < 0 )
goto cleanup ;
util: call iptables directly rather than via firewalld
When libvirt added support for firewalld, we were unable to use
firewalld's higher level rules, because they weren't detailed enough
and could not be applied to the iptables FORWARD or OUTPUT chains
(only to the INPUT chain). Instead we changed our code so that rather
than running the iptables/ip6tables/ebtables binaries ourselves, we
would send these commands to firewalld as "passthrough commands", and
firewalld would run the appropriate program on our behalf.
This was done under the assumption that firewalld was somehow tracking
all these rules, and that this tracking was benefitting proper
operation of firewalld and the system in general.
Several years later this came up in a discussion on IRC, and we
learned from the firewalld developers that, in fact, adding iptables
and ebtables rules with firewalld's passthrough commands actually has
*no* advantage; firewalld doesn't keep track of these rules in any
way, and doesn't use them to tailor the construction of its own rules.
Meanwhile, users have been complaining for some time that whenever
firewalld is restarted on a system with libvirt virtual networks
and/or nwfilter rules active, the system logs would be flooded with
warning messages whining that [lots of different rules] could not be
deleted because they didn't exist. For example:
firewalld[3536040]: WARNING: COMMAND_FAILED:
'/usr/sbin/iptables -w10 -w --table filter --delete LIBVIRT_OUT
--out-interface virbr4 --protocol udp --destination-port 68
--jump ACCEPT' failed: iptables: Bad rule
(does a matching rule exist in that chain?).
(See https://bugzilla.redhat.com/1790837 for many more examples and a
discussion)
Note that these messages are created by iptables, but are logged by
firewalld - when an iptables/ebtables command fails, firewalld grabs
whatever is in stderr of the program, and spits it out to the system
log as a warning. We've requested that firewalld not do this (and
instead leave it up to the calling application to do the appropriate
logging), but this request has been respectfully denied.
But combining the two problems above ( 1) firewalld doesn't do
anything useful when you use it as a proxy to add/remove iptables
rules, 2) firewalld often insists on logging lots of
annoying/misleading/useless "error" messages when you use it as a
proxy to remove iptables rules that don't already exist), leads to a
solution - simply stop using firewalld to add and remove iptables
rules. Instead, exec iptables/ip6tables/ebtables directly in the same
way we do when firewalld isn't active.
We still need to keep track of whether or not firewalld is active, as
there are some things that must be done, e.g. we need to add some
actual firewalld rules in the firewalld "libvirt" zone, and we need to
take notice when firewalld restarts, so that we can reload all our
rules.
This patch doesn't remove the infrastructure that allows having
different firewall backends that perform their functions in different
ways, as that will very possibly come in handy in the future when we
want to have an nftables direct backend, and possibly a "pure"
firewalld backend (now that firewalld supports more complex rules, and
can add those rules to the FORWARD and OUTPUT chains). Instead, it
just changes the action when the selected backend is "firewalld" so
that it adds rules directly rather than through firewalld, while
leaving as much of the existing code intact as possible.
In order for tests to still pass, virfirewalltest also had to be
modified to behave in a different way (i.e. by capturing the generated
commandline as it does for the DIRECT backend, rather than capturing
dbus messages using a mocked dbus API).
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2020-11-17 18:55:12 +03:00
if ( data - > expectBackend = = VIR_FIREWALL_BACKEND_DIRECT | |
data - > expectBackend = = VIR_FIREWALL_BACKEND_FIREWALLD ) {
2021-04-06 11:56:23 +03:00
virCommandSetDryRun ( dryRunToken , & cmdbuf , false , false , testFirewallRollbackHook , NULL ) ;
2013-03-04 20:30:40 +04:00
} else {
fwError = true ;
fwBuf = & cmdbuf ;
}
virFirewallStartTransaction ( fw , 0 ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " ACCEPT " , NULL ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.255 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " !192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
virFirewallStartRollback ( fw , 0 ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -D " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " ACCEPT " , NULL ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -D " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.255 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -D " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " !192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
if ( virFirewallApply ( fw ) = = 0 ) {
fprintf ( stderr , " Firewall apply unexpectedly worked \n " ) ;
goto cleanup ;
}
actual = virBufferCurrentContent ( & cmdbuf ) ;
if ( STRNEQ_NULLABLE ( expected , actual ) ) {
2018-12-04 20:08:14 +03:00
fprintf ( stderr , " Unexpected command execution \n " ) ;
2016-05-26 18:01:51 +03:00
virTestDifference ( stderr , expected , actual ) ;
2013-03-04 20:30:40 +04:00
goto cleanup ;
}
ret = 0 ;
cleanup :
fwBuf = NULL ;
return ret ;
}
static int
2019-10-14 15:45:03 +03:00
testFirewallManyRollback ( const void * opaque G_GNUC_UNUSED )
2013-03-04 20:30:40 +04:00
{
2020-07-03 02:35:41 +03:00
g_auto ( virBuffer ) cmdbuf = VIR_BUFFER_INITIALIZER ;
2020-07-04 23:35:10 +03:00
g_autoptr ( virFirewall ) fw = virFirewallNew ( ) ;
2013-03-04 20:30:40 +04:00
int ret = - 1 ;
const char * actual = NULL ;
const char * expected =
2020-11-17 21:51:45 +03:00
IPTABLES_PATH " -w -A INPUT --source 192.168.122.1 --jump ACCEPT \n "
IPTABLES_PATH " -w -A INPUT --source 192.168.122.255 --jump REJECT \n "
IPTABLES_PATH " -w -D INPUT --source 192.168.122.255 --jump REJECT \n "
IPTABLES_PATH " -w -D INPUT --source '!192.168.122.1' --jump REJECT \n " ;
2013-03-04 20:30:40 +04:00
const struct testFirewallData * data = opaque ;
2021-04-01 18:54:09 +03:00
g_autoptr ( virCommandDryRunToken ) dryRunToken = virCommandDryRunTokenNew ( ) ;
2013-03-04 20:30:40 +04:00
fwDisabled = data - > fwDisabled ;
if ( virFirewallSetBackend ( data - > tryBackend ) < 0 )
goto cleanup ;
util: call iptables directly rather than via firewalld
When libvirt added support for firewalld, we were unable to use
firewalld's higher level rules, because they weren't detailed enough
and could not be applied to the iptables FORWARD or OUTPUT chains
(only to the INPUT chain). Instead we changed our code so that rather
than running the iptables/ip6tables/ebtables binaries ourselves, we
would send these commands to firewalld as "passthrough commands", and
firewalld would run the appropriate program on our behalf.
This was done under the assumption that firewalld was somehow tracking
all these rules, and that this tracking was benefitting proper
operation of firewalld and the system in general.
Several years later this came up in a discussion on IRC, and we
learned from the firewalld developers that, in fact, adding iptables
and ebtables rules with firewalld's passthrough commands actually has
*no* advantage; firewalld doesn't keep track of these rules in any
way, and doesn't use them to tailor the construction of its own rules.
Meanwhile, users have been complaining for some time that whenever
firewalld is restarted on a system with libvirt virtual networks
and/or nwfilter rules active, the system logs would be flooded with
warning messages whining that [lots of different rules] could not be
deleted because they didn't exist. For example:
firewalld[3536040]: WARNING: COMMAND_FAILED:
'/usr/sbin/iptables -w10 -w --table filter --delete LIBVIRT_OUT
--out-interface virbr4 --protocol udp --destination-port 68
--jump ACCEPT' failed: iptables: Bad rule
(does a matching rule exist in that chain?).
(See https://bugzilla.redhat.com/1790837 for many more examples and a
discussion)
Note that these messages are created by iptables, but are logged by
firewalld - when an iptables/ebtables command fails, firewalld grabs
whatever is in stderr of the program, and spits it out to the system
log as a warning. We've requested that firewalld not do this (and
instead leave it up to the calling application to do the appropriate
logging), but this request has been respectfully denied.
But combining the two problems above ( 1) firewalld doesn't do
anything useful when you use it as a proxy to add/remove iptables
rules, 2) firewalld often insists on logging lots of
annoying/misleading/useless "error" messages when you use it as a
proxy to remove iptables rules that don't already exist), leads to a
solution - simply stop using firewalld to add and remove iptables
rules. Instead, exec iptables/ip6tables/ebtables directly in the same
way we do when firewalld isn't active.
We still need to keep track of whether or not firewalld is active, as
there are some things that must be done, e.g. we need to add some
actual firewalld rules in the firewalld "libvirt" zone, and we need to
take notice when firewalld restarts, so that we can reload all our
rules.
This patch doesn't remove the infrastructure that allows having
different firewall backends that perform their functions in different
ways, as that will very possibly come in handy in the future when we
want to have an nftables direct backend, and possibly a "pure"
firewalld backend (now that firewalld supports more complex rules, and
can add those rules to the FORWARD and OUTPUT chains). Instead, it
just changes the action when the selected backend is "firewalld" so
that it adds rules directly rather than through firewalld, while
leaving as much of the existing code intact as possible.
In order for tests to still pass, virfirewalltest also had to be
modified to behave in a different way (i.e. by capturing the generated
commandline as it does for the DIRECT backend, rather than capturing
dbus messages using a mocked dbus API).
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2020-11-17 18:55:12 +03:00
if ( data - > expectBackend = = VIR_FIREWALL_BACKEND_DIRECT | |
data - > expectBackend = = VIR_FIREWALL_BACKEND_FIREWALLD ) {
2021-04-06 11:56:23 +03:00
virCommandSetDryRun ( dryRunToken , & cmdbuf , false , false , testFirewallRollbackHook , NULL ) ;
2013-03-04 20:30:40 +04:00
} else {
fwBuf = & cmdbuf ;
fwError = true ;
}
virFirewallStartTransaction ( fw , 0 ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " ACCEPT " , NULL ) ;
virFirewallStartRollback ( fw , 0 ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -D " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " ACCEPT " , NULL ) ;
virFirewallStartTransaction ( fw , 0 ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.255 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " !192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
virFirewallStartRollback ( fw , 0 ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -D " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.255 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -D " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " !192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
if ( virFirewallApply ( fw ) = = 0 ) {
fprintf ( stderr , " Firewall apply unexpectedly worked \n " ) ;
goto cleanup ;
}
actual = virBufferCurrentContent ( & cmdbuf ) ;
if ( STRNEQ_NULLABLE ( expected , actual ) ) {
2018-12-04 20:08:14 +03:00
fprintf ( stderr , " Unexpected command execution \n " ) ;
2016-05-26 18:01:51 +03:00
virTestDifference ( stderr , expected , actual ) ;
2013-03-04 20:30:40 +04:00
goto cleanup ;
}
ret = 0 ;
cleanup :
fwBuf = NULL ;
return ret ;
}
static int
2019-10-14 15:45:03 +03:00
testFirewallChainedRollback ( const void * opaque G_GNUC_UNUSED )
2013-03-04 20:30:40 +04:00
{
2020-07-03 02:35:41 +03:00
g_auto ( virBuffer ) cmdbuf = VIR_BUFFER_INITIALIZER ;
2020-07-04 23:35:10 +03:00
g_autoptr ( virFirewall ) fw = virFirewallNew ( ) ;
2013-03-04 20:30:40 +04:00
int ret = - 1 ;
const char * actual = NULL ;
const char * expected =
2020-11-17 21:51:45 +03:00
IPTABLES_PATH " -w -A INPUT --source 192.168.122.1 --jump ACCEPT \n "
IPTABLES_PATH " -w -A INPUT --source 192.168.122.127 --jump REJECT \n "
IPTABLES_PATH " -w -A INPUT --source '!192.168.122.1' --jump REJECT \n "
IPTABLES_PATH " -w -A INPUT --source 192.168.122.255 --jump REJECT \n "
IPTABLES_PATH " -w -D INPUT --source 192.168.122.127 --jump REJECT \n "
IPTABLES_PATH " -w -D INPUT --source '!192.168.122.1' --jump REJECT \n "
IPTABLES_PATH " -w -D INPUT --source 192.168.122.255 --jump REJECT \n "
IPTABLES_PATH " -w -D INPUT --source '!192.168.122.1' --jump REJECT \n " ;
2013-03-04 20:30:40 +04:00
const struct testFirewallData * data = opaque ;
2021-04-01 18:54:09 +03:00
g_autoptr ( virCommandDryRunToken ) dryRunToken = virCommandDryRunTokenNew ( ) ;
2013-03-04 20:30:40 +04:00
fwDisabled = data - > fwDisabled ;
if ( virFirewallSetBackend ( data - > tryBackend ) < 0 )
goto cleanup ;
util: call iptables directly rather than via firewalld
When libvirt added support for firewalld, we were unable to use
firewalld's higher level rules, because they weren't detailed enough
and could not be applied to the iptables FORWARD or OUTPUT chains
(only to the INPUT chain). Instead we changed our code so that rather
than running the iptables/ip6tables/ebtables binaries ourselves, we
would send these commands to firewalld as "passthrough commands", and
firewalld would run the appropriate program on our behalf.
This was done under the assumption that firewalld was somehow tracking
all these rules, and that this tracking was benefitting proper
operation of firewalld and the system in general.
Several years later this came up in a discussion on IRC, and we
learned from the firewalld developers that, in fact, adding iptables
and ebtables rules with firewalld's passthrough commands actually has
*no* advantage; firewalld doesn't keep track of these rules in any
way, and doesn't use them to tailor the construction of its own rules.
Meanwhile, users have been complaining for some time that whenever
firewalld is restarted on a system with libvirt virtual networks
and/or nwfilter rules active, the system logs would be flooded with
warning messages whining that [lots of different rules] could not be
deleted because they didn't exist. For example:
firewalld[3536040]: WARNING: COMMAND_FAILED:
'/usr/sbin/iptables -w10 -w --table filter --delete LIBVIRT_OUT
--out-interface virbr4 --protocol udp --destination-port 68
--jump ACCEPT' failed: iptables: Bad rule
(does a matching rule exist in that chain?).
(See https://bugzilla.redhat.com/1790837 for many more examples and a
discussion)
Note that these messages are created by iptables, but are logged by
firewalld - when an iptables/ebtables command fails, firewalld grabs
whatever is in stderr of the program, and spits it out to the system
log as a warning. We've requested that firewalld not do this (and
instead leave it up to the calling application to do the appropriate
logging), but this request has been respectfully denied.
But combining the two problems above ( 1) firewalld doesn't do
anything useful when you use it as a proxy to add/remove iptables
rules, 2) firewalld often insists on logging lots of
annoying/misleading/useless "error" messages when you use it as a
proxy to remove iptables rules that don't already exist), leads to a
solution - simply stop using firewalld to add and remove iptables
rules. Instead, exec iptables/ip6tables/ebtables directly in the same
way we do when firewalld isn't active.
We still need to keep track of whether or not firewalld is active, as
there are some things that must be done, e.g. we need to add some
actual firewalld rules in the firewalld "libvirt" zone, and we need to
take notice when firewalld restarts, so that we can reload all our
rules.
This patch doesn't remove the infrastructure that allows having
different firewall backends that perform their functions in different
ways, as that will very possibly come in handy in the future when we
want to have an nftables direct backend, and possibly a "pure"
firewalld backend (now that firewalld supports more complex rules, and
can add those rules to the FORWARD and OUTPUT chains). Instead, it
just changes the action when the selected backend is "firewalld" so
that it adds rules directly rather than through firewalld, while
leaving as much of the existing code intact as possible.
In order for tests to still pass, virfirewalltest also had to be
modified to behave in a different way (i.e. by capturing the generated
commandline as it does for the DIRECT backend, rather than capturing
dbus messages using a mocked dbus API).
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2020-11-17 18:55:12 +03:00
if ( data - > expectBackend = = VIR_FIREWALL_BACKEND_DIRECT | |
data - > expectBackend = = VIR_FIREWALL_BACKEND_FIREWALLD ) {
2021-04-06 11:56:23 +03:00
virCommandSetDryRun ( dryRunToken , & cmdbuf , false , false , testFirewallRollbackHook , NULL ) ;
2013-03-04 20:30:40 +04:00
} else {
fwBuf = & cmdbuf ;
fwError = true ;
}
virFirewallStartTransaction ( fw , 0 ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " ACCEPT " , NULL ) ;
virFirewallStartRollback ( fw , 0 ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -D " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " ACCEPT " , NULL ) ;
virFirewallStartTransaction ( fw , 0 ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.127 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " !192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
virFirewallStartRollback ( fw , 0 ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -D " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.127 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -D " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " !192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
virFirewallStartTransaction ( fw , 0 ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.255 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " !192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
virFirewallStartRollback ( fw , VIR_FIREWALL_ROLLBACK_INHERIT_PREVIOUS ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -D " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.255 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -D " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " !192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
if ( virFirewallApply ( fw ) = = 0 ) {
fprintf ( stderr , " Firewall apply unexpectedly worked \n " ) ;
goto cleanup ;
}
actual = virBufferCurrentContent ( & cmdbuf ) ;
if ( STRNEQ_NULLABLE ( expected , actual ) ) {
2018-12-04 20:08:14 +03:00
fprintf ( stderr , " Unexpected command execution \n " ) ;
2016-05-26 18:01:51 +03:00
virTestDifference ( stderr , expected , actual ) ;
2013-03-04 20:30:40 +04:00
goto cleanup ;
}
ret = 0 ;
cleanup :
fwBuf = NULL ;
return ret ;
}
static const char * expectedLines [ ] = {
" Chain INPUT (policy ACCEPT) " ,
" target prot opt source destination " ,
" " ,
" Chain FORWARD (policy ACCEPT) " ,
" target prot opt source destination " ,
" " ,
" Chain OUTPUT (policy ACCEPT) " ,
" target prot opt source destination " ,
" " ,
" Chain PREROUTING (policy ACCEPT) " ,
" target prot opt source destination " ,
" " ,
" Chain INPUT (policy ACCEPT) " ,
" target prot opt source destination " ,
" " ,
" Chain OUTPUT (policy ACCEPT) " ,
" target prot opt source destination " ,
" " ,
" Chain POSTROUTING (policy ACCEPT) " ,
" target prot opt source destination " ,
" " ,
} ;
static size_t expectedLineNum ;
static bool expectedLineError ;
static void
testFirewallQueryHook ( const char * const * args ,
2019-10-14 15:45:03 +03:00
const char * const * env G_GNUC_UNUSED ,
const char * input G_GNUC_UNUSED ,
2013-03-04 20:30:40 +04:00
char * * output ,
2019-10-14 15:45:03 +03:00
char * * error G_GNUC_UNUSED ,
2019-10-20 14:49:46 +03:00
int * status G_GNUC_UNUSED ,
2019-10-14 15:45:03 +03:00
void * opaque G_GNUC_UNUSED )
2013-03-04 20:30:40 +04:00
{
if ( STREQ ( args [ 0 ] , IPTABLES_PATH ) & &
2020-11-17 03:20:53 +03:00
STREQ ( args [ 1 ] , " -w " ) & &
STREQ ( args [ 2 ] , " -L " ) ) {
2019-10-20 14:49:46 +03:00
* output = g_strdup ( TEST_FILTER_TABLE_LIST ) ;
2013-03-04 20:30:40 +04:00
} else if ( STREQ ( args [ 0 ] , IPTABLES_PATH ) & &
2020-11-17 03:20:53 +03:00
STREQ ( args [ 1 ] , " -w " ) & &
STREQ ( args [ 2 ] , " -t " ) & &
STREQ ( args [ 3 ] , " nat " ) & &
STREQ ( args [ 4 ] , " -L " ) ) {
2019-10-20 14:49:46 +03:00
* output = g_strdup ( TEST_NAT_TABLE_LIST ) ;
2013-03-04 20:30:40 +04:00
}
}
static int
testFirewallQueryCallback ( virFirewallPtr fw ,
2018-12-04 19:33:28 +03:00
virFirewallLayer layer ,
2013-03-04 20:30:40 +04:00
const char * const * lines ,
2019-10-14 15:45:03 +03:00
void * opaque G_GNUC_UNUSED )
2013-03-04 20:30:40 +04:00
{
size_t i ;
2018-12-04 19:33:28 +03:00
virFirewallAddRule ( fw , layer ,
2013-03-04 20:30:40 +04:00
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " !192.168.122.129 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
for ( i = 0 ; lines [ i ] ! = NULL ; i + + ) {
2019-10-15 14:55:26 +03:00
if ( expectedLineNum > = G_N_ELEMENTS ( expectedLines ) ) {
2013-03-04 20:30:40 +04:00
expectedLineError = true ;
break ;
}
if ( STRNEQ ( expectedLines [ expectedLineNum ] , lines [ i ] ) ) {
fprintf ( stderr , " Mismatch '%s' vs '%s' at %zu, %zu \n " ,
expectedLines [ expectedLineNum ] , lines [ i ] ,
expectedLineNum , i ) ;
expectedLineError = true ;
break ;
}
expectedLineNum + + ;
}
return 0 ;
}
static int
2019-10-14 15:45:03 +03:00
testFirewallQuery ( const void * opaque G_GNUC_UNUSED )
2013-03-04 20:30:40 +04:00
{
2020-07-03 02:35:41 +03:00
g_auto ( virBuffer ) cmdbuf = VIR_BUFFER_INITIALIZER ;
2020-07-04 23:35:10 +03:00
g_autoptr ( virFirewall ) fw = virFirewallNew ( ) ;
2013-03-04 20:30:40 +04:00
int ret = - 1 ;
const char * actual = NULL ;
const char * expected =
2020-11-17 21:51:45 +03:00
IPTABLES_PATH " -w -A INPUT --source 192.168.122.1 --jump ACCEPT \n "
IPTABLES_PATH " -w -A INPUT --source 192.168.122.127 --jump REJECT \n "
2020-11-17 03:20:53 +03:00
IPTABLES_PATH " -w -L \n "
IPTABLES_PATH " -w -t nat -L \n "
2020-11-17 21:51:45 +03:00
IPTABLES_PATH " -w -A INPUT --source 192.168.122.130 --jump REJECT \n "
IPTABLES_PATH " -w -A INPUT --source '!192.168.122.129' --jump REJECT \n "
IPTABLES_PATH " -w -A INPUT --source '!192.168.122.129' --jump REJECT \n "
IPTABLES_PATH " -w -A INPUT --source 192.168.122.128 --jump REJECT \n "
IPTABLES_PATH " -w -A INPUT --source '!192.168.122.1' --jump REJECT \n " ;
2013-03-04 20:30:40 +04:00
const struct testFirewallData * data = opaque ;
2021-04-01 18:54:09 +03:00
g_autoptr ( virCommandDryRunToken ) dryRunToken = virCommandDryRunTokenNew ( ) ;
2013-03-04 20:30:40 +04:00
expectedLineNum = 0 ;
expectedLineError = false ;
fwDisabled = data - > fwDisabled ;
if ( virFirewallSetBackend ( data - > tryBackend ) < 0 )
goto cleanup ;
util: call iptables directly rather than via firewalld
When libvirt added support for firewalld, we were unable to use
firewalld's higher level rules, because they weren't detailed enough
and could not be applied to the iptables FORWARD or OUTPUT chains
(only to the INPUT chain). Instead we changed our code so that rather
than running the iptables/ip6tables/ebtables binaries ourselves, we
would send these commands to firewalld as "passthrough commands", and
firewalld would run the appropriate program on our behalf.
This was done under the assumption that firewalld was somehow tracking
all these rules, and that this tracking was benefitting proper
operation of firewalld and the system in general.
Several years later this came up in a discussion on IRC, and we
learned from the firewalld developers that, in fact, adding iptables
and ebtables rules with firewalld's passthrough commands actually has
*no* advantage; firewalld doesn't keep track of these rules in any
way, and doesn't use them to tailor the construction of its own rules.
Meanwhile, users have been complaining for some time that whenever
firewalld is restarted on a system with libvirt virtual networks
and/or nwfilter rules active, the system logs would be flooded with
warning messages whining that [lots of different rules] could not be
deleted because they didn't exist. For example:
firewalld[3536040]: WARNING: COMMAND_FAILED:
'/usr/sbin/iptables -w10 -w --table filter --delete LIBVIRT_OUT
--out-interface virbr4 --protocol udp --destination-port 68
--jump ACCEPT' failed: iptables: Bad rule
(does a matching rule exist in that chain?).
(See https://bugzilla.redhat.com/1790837 for many more examples and a
discussion)
Note that these messages are created by iptables, but are logged by
firewalld - when an iptables/ebtables command fails, firewalld grabs
whatever is in stderr of the program, and spits it out to the system
log as a warning. We've requested that firewalld not do this (and
instead leave it up to the calling application to do the appropriate
logging), but this request has been respectfully denied.
But combining the two problems above ( 1) firewalld doesn't do
anything useful when you use it as a proxy to add/remove iptables
rules, 2) firewalld often insists on logging lots of
annoying/misleading/useless "error" messages when you use it as a
proxy to remove iptables rules that don't already exist), leads to a
solution - simply stop using firewalld to add and remove iptables
rules. Instead, exec iptables/ip6tables/ebtables directly in the same
way we do when firewalld isn't active.
We still need to keep track of whether or not firewalld is active, as
there are some things that must be done, e.g. we need to add some
actual firewalld rules in the firewalld "libvirt" zone, and we need to
take notice when firewalld restarts, so that we can reload all our
rules.
This patch doesn't remove the infrastructure that allows having
different firewall backends that perform their functions in different
ways, as that will very possibly come in handy in the future when we
want to have an nftables direct backend, and possibly a "pure"
firewalld backend (now that firewalld supports more complex rules, and
can add those rules to the FORWARD and OUTPUT chains). Instead, it
just changes the action when the selected backend is "firewalld" so
that it adds rules directly rather than through firewalld, while
leaving as much of the existing code intact as possible.
In order for tests to still pass, virfirewalltest also had to be
modified to behave in a different way (i.e. by capturing the generated
commandline as it does for the DIRECT backend, rather than capturing
dbus messages using a mocked dbus API).
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2020-11-17 18:55:12 +03:00
if ( data - > expectBackend = = VIR_FIREWALL_BACKEND_DIRECT | |
data - > expectBackend = = VIR_FIREWALL_BACKEND_FIREWALLD ) {
2021-04-06 11:56:23 +03:00
virCommandSetDryRun ( dryRunToken , & cmdbuf , false , false , testFirewallQueryHook , NULL ) ;
2013-03-04 20:30:40 +04:00
} else {
fwBuf = & cmdbuf ;
fwError = true ;
}
virFirewallStartTransaction ( fw , 0 ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " ACCEPT " , NULL ) ;
virFirewallStartTransaction ( fw , 0 ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.127 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
virFirewallAddRuleFull ( fw , VIR_FIREWALL_LAYER_IPV4 ,
false ,
testFirewallQueryCallback ,
NULL ,
" -L " , NULL ) ;
virFirewallAddRuleFull ( fw , VIR_FIREWALL_LAYER_IPV4 ,
false ,
testFirewallQueryCallback ,
NULL ,
" -t " , " nat " , " -L " , NULL ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.130 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
virFirewallStartTransaction ( fw , 0 ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " 192.168.122.128 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
virFirewallAddRule ( fw , VIR_FIREWALL_LAYER_IPV4 ,
" -A " , " INPUT " ,
2020-11-17 21:51:45 +03:00
" --source " , " !192.168.122.1 " ,
2013-03-04 20:30:40 +04:00
" --jump " , " REJECT " , NULL ) ;
if ( virFirewallApply ( fw ) < 0 )
goto cleanup ;
actual = virBufferCurrentContent ( & cmdbuf ) ;
if ( expectedLineError ) {
fprintf ( stderr , " Got some unexpected query data \n " ) ;
goto cleanup ;
}
if ( STRNEQ_NULLABLE ( expected , actual ) ) {
2018-12-04 20:08:14 +03:00
fprintf ( stderr , " Unexpected command execution \n " ) ;
2016-05-26 18:01:51 +03:00
virTestDifference ( stderr , expected , actual ) ;
2013-03-04 20:30:40 +04:00
goto cleanup ;
}
ret = 0 ;
cleanup :
fwBuf = NULL ;
return ret ;
}
2017-04-07 17:11:14 +03:00
static bool
hasNetfilterTools ( void )
{
return virFileIsExecutable ( IPTABLES_PATH ) & &
virFileIsExecutable ( IP6TABLES_PATH ) & &
virFileIsExecutable ( EBTABLES_PATH ) ;
}
2013-03-04 20:30:40 +04:00
static int
mymain ( void )
{
int ret = 0 ;
2017-04-07 17:11:14 +03:00
if ( ! hasNetfilterTools ( ) ) {
fprintf ( stderr , " iptables/ip6tables/ebtables tools not present " ) ;
return EXIT_AM_SKIP ;
}
2017-11-03 15:09:47 +03:00
# define RUN_TEST_DIRECT(name, method) \
do { \
struct testFirewallData data ; \
data . tryBackend = VIR_FIREWALL_BACKEND_AUTOMATIC ; \
data . expectBackend = VIR_FIREWALL_BACKEND_DIRECT ; \
data . fwDisabled = true ; \
if ( virTestRun ( name " auto direct " , method , & data ) < 0 ) \
ret = - 1 ; \
data . tryBackend = VIR_FIREWALL_BACKEND_DIRECT ; \
data . expectBackend = VIR_FIREWALL_BACKEND_DIRECT ; \
data . fwDisabled = true ; \
if ( virTestRun ( name " manual direct " , method , & data ) < 0 ) \
ret = - 1 ; \
2013-03-04 20:30:40 +04:00
} while ( 0 )
2020-09-15 15:00:53 +03:00
# define RUN_TEST_FIREWALLD(name, method) \
2017-11-03 15:09:47 +03:00
do { \
struct testFirewallData data ; \
data . tryBackend = VIR_FIREWALL_BACKEND_AUTOMATIC ; \
data . expectBackend = VIR_FIREWALL_BACKEND_FIREWALLD ; \
data . fwDisabled = false ; \
if ( virTestRun ( name " auto firewalld " , method , & data ) < 0 ) \
ret = - 1 ; \
data . tryBackend = VIR_FIREWALL_BACKEND_FIREWALLD ; \
data . expectBackend = VIR_FIREWALL_BACKEND_FIREWALLD ; \
data . fwDisabled = false ; \
if ( virTestRun ( name " manual firewalld " , method , & data ) < 0 ) \
ret = - 1 ; \
2013-03-04 20:30:40 +04:00
} while ( 0 )
2020-09-15 15:00:53 +03:00
# define RUN_TEST(name, method) \
2017-11-03 15:09:47 +03:00
RUN_TEST_DIRECT ( name , method ) ; \
2013-03-04 20:30:40 +04:00
RUN_TEST_FIREWALLD ( name , method )
RUN_TEST ( " single group " , testFirewallSingleGroup ) ;
RUN_TEST ( " remove rule " , testFirewallRemoveRule ) ;
RUN_TEST ( " many groups " , testFirewallManyGroups ) ;
RUN_TEST ( " ignore fail group " , testFirewallIgnoreFailGroup ) ;
RUN_TEST ( " ignore fail rule " , testFirewallIgnoreFailRule ) ;
RUN_TEST ( " no rollback " , testFirewallNoRollback ) ;
RUN_TEST ( " single rollback " , testFirewallSingleRollback ) ;
RUN_TEST ( " many rollback " , testFirewallManyRollback ) ;
RUN_TEST ( " chained rollback " , testFirewallChainedRollback ) ;
RUN_TEST ( " query transaction " , testFirewallQuery ) ;
return ret = = 0 ? EXIT_SUCCESS : EXIT_FAILURE ;
}
2020-09-15 15:00:53 +03:00
VIR_TEST_MAIN_PRELOAD ( mymain , VIR_TEST_MOCK ( " virgdbus " ) )
2014-05-01 22:43:58 +04:00
# else /* ! defined (__linux__) */
int main ( void )
{
return EXIT_AM_SKIP ;
}
# endif /* ! defined(__linux__) */