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

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