Blame SOURCES/v1.0.0-0035-fix-ipset-normalize-entries-in-CIDR-notation.patch

63f414
From e399840e91c766531923c017ffa00bbc01e7bbe6 Mon Sep 17 00:00:00 2001
63f414
From: Eric Garver <eric@garver.life>
63f414
Date: Fri, 12 Feb 2021 14:23:21 -0500
63f414
Subject: [PATCH 35/36] fix(ipset): normalize entries in CIDR notation
63f414
63f414
This will convert things like 10.0.1.0/22 to 10.0.0.0/22. Fix up test
63f414
cases in which the error code changed due to this.
63f414
63f414
(cherry picked from commit e4dc44fcfd214b27c718eb4d99d3b137495b9626)
63f414
---
63f414
 src/firewall/client.py              |  9 ++++++++-
63f414
 src/firewall/core/fw_ipset.py       | 11 ++++++++++-
63f414
 src/firewall/core/ipset.py          | 13 +++++++++++++
63f414
 src/firewall/server/config_ipset.py | 10 ++++++++--
63f414
 src/tests/regression/rhbz1601610.at | 19 +++++++++++++------
63f414
 5 files changed, 52 insertions(+), 10 deletions(-)
63f414
63f414
diff --git a/src/firewall/client.py b/src/firewall/client.py
63f414
index 51bf09c8fad6..aa6bd7cd282b 100644
63f414
--- a/src/firewall/client.py
63f414
+++ b/src/firewall/client.py
63f414
@@ -34,6 +34,7 @@ from firewall.core.base import DEFAULT_ZONE_TARGET, DEFAULT_POLICY_TARGET, DEFAU
63f414
 from firewall.dbus_utils import dbus_to_python
63f414
 from firewall.functions import b2u
63f414
 from firewall.core.rich import Rich_Rule
63f414
+from firewall.core.ipset import normalize_ipset_entry
63f414
 from firewall import errors
63f414
 from firewall.errors import FirewallError
63f414
 
63f414
@@ -1616,12 +1617,16 @@ class FirewallClientIPSetSettings(object):
63f414
         if "timeout" in self.settings[4] and \
63f414
            self.settings[4]["timeout"] != "0":
63f414
             raise FirewallError(errors.IPSET_WITH_TIMEOUT)
63f414
-        self.settings[5] = entries
63f414
+        _entries = set()
63f414
+        for _entry in dbus_to_python(entries, list):
63f414
+            _entries.add(normalize_ipset_entry(_entry))
63f414
+        self.settings[5] = list(_entries)
63f414
     @handle_exceptions
63f414
     def addEntry(self, entry):
63f414
         if "timeout" in self.settings[4] and \
63f414
            self.settings[4]["timeout"] != "0":
63f414
             raise FirewallError(errors.IPSET_WITH_TIMEOUT)
63f414
+        entry = normalize_ipset_entry(entry)
63f414
         if entry not in self.settings[5]:
63f414
             self.settings[5].append(entry)
63f414
         else:
63f414
@@ -1631,6 +1636,7 @@ class FirewallClientIPSetSettings(object):
63f414
         if "timeout" in self.settings[4] and \
63f414
            self.settings[4]["timeout"] != "0":
63f414
             raise FirewallError(errors.IPSET_WITH_TIMEOUT)
63f414
+        entry = normalize_ipset_entry(entry)
63f414
         if entry in self.settings[5]:
63f414
             self.settings[5].remove(entry)
63f414
         else:
63f414
@@ -1640,6 +1646,7 @@ class FirewallClientIPSetSettings(object):
63f414
         if "timeout" in self.settings[4] and \
63f414
            self.settings[4]["timeout"] != "0":
63f414
             raise FirewallError(errors.IPSET_WITH_TIMEOUT)
63f414
+        entry = normalize_ipset_entry(entry)
63f414
         return entry in self.settings[5]
63f414
 
63f414
 # ipset config
63f414
diff --git a/src/firewall/core/fw_ipset.py b/src/firewall/core/fw_ipset.py
63f414
index 6ebda2d56213..e5348949413c 100644
63f414
--- a/src/firewall/core/fw_ipset.py
63f414
+++ b/src/firewall/core/fw_ipset.py
63f414
@@ -24,7 +24,8 @@
63f414
 __all__ = [ "FirewallIPSet" ]
63f414
 
63f414
 from firewall.core.logger import log
63f414
-from firewall.core.ipset import remove_default_create_options as rm_def_cr_opts
63f414
+from firewall.core.ipset import remove_default_create_options as rm_def_cr_opts, \
63f414
+                                normalize_ipset_entry
63f414
 from firewall.core.io.ipset import IPSet
63f414
 from firewall import errors
63f414
 from firewall.errors import FirewallError
63f414
@@ -189,6 +190,7 @@ class FirewallIPSet(object):
63f414
 
63f414
     def add_entry(self, name, entry):
63f414
         obj = self.get_ipset(name, applied=True)
63f414
+        entry = normalize_ipset_entry(entry)
63f414
 
63f414
         IPSet.check_entry(entry, obj.options, obj.type)
63f414
         if entry in obj.entries:
63f414
@@ -208,6 +210,7 @@ class FirewallIPSet(object):
63f414
 
63f414
     def remove_entry(self, name, entry):
63f414
         obj = self.get_ipset(name, applied=True)
63f414
+        entry = normalize_ipset_entry(entry)
63f414
 
63f414
         # no entry check for removal
63f414
         if entry not in obj.entries:
63f414
@@ -226,6 +229,7 @@ class FirewallIPSet(object):
63f414
 
63f414
     def query_entry(self, name, entry):
63f414
         obj = self.get_ipset(name, applied=True)
63f414
+        entry = normalize_ipset_entry(entry)
63f414
         if "timeout" in obj.options and obj.options["timeout"] != "0":
63f414
             # no entries visible for ipsets with timeout
63f414
             raise FirewallError(errors.IPSET_WITH_TIMEOUT, name)
63f414
@@ -239,6 +243,11 @@ class FirewallIPSet(object):
63f414
     def set_entries(self, name, entries):
63f414
         obj = self.get_ipset(name, applied=True)
63f414
 
63f414
+        _entries = set()
63f414
+        for _entry in entries:
63f414
+            _entries.add(normalize_ipset_entry(_entry))
63f414
+        entries = list(_entries)
63f414
+
63f414
         for entry in entries:
63f414
             IPSet.check_entry(entry, obj.options, obj.type)
63f414
         if "timeout" not in obj.options or obj.options["timeout"] == "0":
63f414
diff --git a/src/firewall/core/ipset.py b/src/firewall/core/ipset.py
63f414
index 0d632143ce13..5bb21856f648 100644
63f414
--- a/src/firewall/core/ipset.py
63f414
+++ b/src/firewall/core/ipset.py
63f414
@@ -24,6 +24,7 @@
63f414
 __all__ = [ "ipset", "check_ipset_name", "remove_default_create_options" ]
63f414
 
63f414
 import os.path
63f414
+import ipaddress
63f414
 
63f414
 from firewall import errors
63f414
 from firewall.errors import FirewallError
63f414
@@ -289,3 +290,15 @@ def remove_default_create_options(options):
63f414
            IPSET_DEFAULT_CREATE_OPTIONS[opt] == _options[opt]:
63f414
             del _options[opt]
63f414
     return _options
63f414
+
63f414
+def normalize_ipset_entry(entry):
63f414
+    """ Normalize IP addresses in entry """
63f414
+    _entry = []
63f414
+    for _part in entry.split(","):
63f414
+        try:
63f414
+            _part.index("/")
63f414
+            _entry.append(str(ipaddress.ip_network(_part, strict=False)))
63f414
+        except ValueError:
63f414
+            _entry.append(_part)
63f414
+
63f414
+    return ",".join(_entry)
63f414
diff --git a/src/firewall/server/config_ipset.py b/src/firewall/server/config_ipset.py
63f414
index 8c647bc29ab9..18ef5783de62 100644
63f414
--- a/src/firewall/server/config_ipset.py
63f414
+++ b/src/firewall/server/config_ipset.py
63f414
@@ -33,7 +33,7 @@ from firewall.dbus_utils import dbus_to_python, \
63f414
     dbus_introspection_prepare_properties, \
63f414
     dbus_introspection_add_properties
63f414
 from firewall.core.io.ipset import IPSet
63f414
-from firewall.core.ipset import IPSET_TYPES
63f414
+from firewall.core.ipset import IPSET_TYPES, normalize_ipset_entry
63f414
 from firewall.core.logger import log
63f414
 from firewall.server.decorators import handle_exceptions, \
63f414
     dbus_handle_exceptions, dbus_service_method
63f414
@@ -406,7 +406,10 @@ class FirewallDConfigIPSet(slip.dbus.service.Object):
63f414
                          in_signature='as')
63f414
     @dbus_handle_exceptions
63f414
     def setEntries(self, entries, sender=None):
63f414
-        entries = dbus_to_python(entries, list)
63f414
+        _entries = set()
63f414
+        for _entry in dbus_to_python(entries, list):
63f414
+            _entries.add(normalize_ipset_entry(_entry))
63f414
+        entries = list(_entries)
63f414
         log.debug1("%s.setEntries('[%s]')", self._log_prefix,
63f414
                    ",".join(entries))
63f414
         self.parent.accessCheck(sender)
63f414
@@ -421,6 +424,7 @@ class FirewallDConfigIPSet(slip.dbus.service.Object):
63f414
     @dbus_handle_exceptions
63f414
     def addEntry(self, entry, sender=None):
63f414
         entry = dbus_to_python(entry, str)
63f414
+        entry = normalize_ipset_entry(entry)
63f414
         log.debug1("%s.addEntry('%s')", self._log_prefix, entry)
63f414
         self.parent.accessCheck(sender)
63f414
         settings = list(self.getSettings())
63f414
@@ -436,6 +440,7 @@ class FirewallDConfigIPSet(slip.dbus.service.Object):
63f414
     @dbus_handle_exceptions
63f414
     def removeEntry(self, entry, sender=None):
63f414
         entry = dbus_to_python(entry, str)
63f414
+        entry = normalize_ipset_entry(entry)
63f414
         log.debug1("%s.removeEntry('%s')", self._log_prefix, entry)
63f414
         self.parent.accessCheck(sender)
63f414
         settings = list(self.getSettings())
63f414
@@ -451,6 +456,7 @@ class FirewallDConfigIPSet(slip.dbus.service.Object):
63f414
     @dbus_handle_exceptions
63f414
     def queryEntry(self, entry, sender=None): # pylint: disable=W0613
63f414
         entry = dbus_to_python(entry, str)
63f414
+        entry = normalize_ipset_entry(entry)
63f414
         log.debug1("%s.queryEntry('%s')", self._log_prefix, entry)
63f414
         settings = list(self.getSettings())
63f414
         if "timeout" in settings[4] and settings[4]["timeout"] != "0":
63f414
diff --git a/src/tests/regression/rhbz1601610.at b/src/tests/regression/rhbz1601610.at
63f414
index ede2c45b88c1..a716539a8acf 100644
63f414
--- a/src/tests/regression/rhbz1601610.at
63f414
+++ b/src/tests/regression/rhbz1601610.at
63f414
@@ -6,11 +6,14 @@ CHECK_IPSET
63f414
 FWD_CHECK([-q --new-ipset=foobar --permanent --type=hash:net])
63f414
 FWD_RELOAD
63f414
 
63f414
-FWD_CHECK([-q --ipset=foobar --add-entry=10.1.1.0/22])
63f414
-FWD_CHECK([-q --ipset=foobar --add-entry=10.1.2.0/22], 13, ignore, ignore)
63f414
-FWD_CHECK([-q --ipset=foobar --add-entry=10.2.0.0/22])
63f414
+FWD_CHECK([--ipset=foobar --add-entry=10.1.1.0/22], 0, [ignore])
63f414
+FWD_CHECK([--ipset=foobar --query-entry 10.1.2.0/22], 0, [ignore])
63f414
+FWD_CHECK([--ipset=foobar --add-entry=10.1.2.0/22], 0, [ignore], [dnl
63f414
+Warning: ALREADY_ENABLED: '10.1.0.0/22' already is in 'foobar'
63f414
+])
63f414
+FWD_CHECK([--ipset=foobar --add-entry=10.2.0.0/22], 0, [ignore])
63f414
 FWD_CHECK([--ipset=foobar --get-entries], 0, [dnl
63f414
-10.1.1.0/22
63f414
+10.1.0.0/22
63f414
 10.2.0.0/22
63f414
 ])
63f414
 NFT_LIST_SET([foobar], 0, [dnl
63f414
@@ -31,6 +34,9 @@ Members:
63f414
 ])
63f414
 
63f414
 FWD_CHECK([-q --ipset=foobar --remove-entry=10.1.1.0/22])
63f414
+FWD_CHECK([--ipset=foobar --query-entry 10.1.1.0/22], 1, [ignore])
63f414
+FWD_CHECK([--ipset=foobar --query-entry 10.1.2.0/22], 1, [ignore])
63f414
+FWD_CHECK([--ipset=foobar --query-entry 10.2.0.0/22], 0, [ignore])
63f414
 FWD_CHECK([--ipset=foobar --get-entries], 0, [dnl
63f414
 10.2.0.0/22
63f414
 ])
63f414
@@ -52,7 +58,7 @@ Members:
63f414
 
63f414
 FWD_CHECK([-q --permanent --ipset=foobar --add-entry=10.1.1.0/22])
63f414
 FWD_CHECK([--permanent --ipset=foobar --get-entries], 0, [dnl
63f414
-10.1.1.0/22
63f414
+10.1.0.0/22
63f414
 ])
63f414
 FWD_CHECK([-q --permanent --ipset=foobar --remove-entry=10.1.1.0/22])
63f414
 FWD_CHECK([--permanent --ipset=foobar --get-entries], 0, [
63f414
@@ -101,4 +107,5 @@ Members:
63f414
 
63f414
 FWD_END_TEST([-e '/ERROR: COMMAND_FAILED:.*already added.*/d'dnl
63f414
               -e '/ERROR: COMMAND_FAILED:.*element.*exists/d'dnl
63f414
-              -e '/Kernel support protocol versions/d'])
63f414
+              -e '/Kernel support protocol versions/d'dnl
63f414
+              -e '/WARNING: ALREADY_ENABLED:/d'])
63f414
-- 
63f414
2.27.0
63f414