Skip to contentSkip to conversation

python/semanage: Use ipaddress module instead of IPy

PATCH Superseded Petr Lautrbach 3 participants 6 messages Apr 24, 2020 – May 1, 2020 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 python/semanage: Use ipaddress module instead of IPy

ipaddress python module was added to standard library in Python 3.3 -
https://docs.python.org/3/library/ipaddress.html

seobject.py was the only consumer of IPy module so this dependency is not needed anymore.

---
 python/semanage/seobject.py | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
python/semanage/seobject.py +6 -6
index f2a139c970bd..c89c67e491b6 100644
@@ -32,7 +32,7 @@ from semanage import *
 PROGNAME = "policycoreutils"
 import sepolicy
 import setools
-from IPy import IP
+import ipaddress
 
 try:
     import gettext
@@ -1860,13 +1860,13 @@ class nodeRecords(semanageRecords):
 
         # verify valid combination
         if len(mask) == 0 or mask[0] == "/":
-            i = IP(addr + mask)
-            newaddr = i.strNormal(0)
-            newmask = str(i.netmask())
-            if newmask == "0.0.0.0" and i.version() == 6:
+            i = ipaddress.ip_network(addr + mask)
+            newaddr = str(i.network_address)
+            newmask = str(i.netmask)
+            if newmask == "0.0.0.0" and i.version == 6:
                 newmask = "::"
 
-            protocol = "ipv%d" % i.version()
+            protocol = "ipv%d" % i.version
 
         try:
             newprotocol = self.protocol.index(protocol)
Signature · 1 line
2.26.0
#1 lore raw
Quoted 51 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Petr Lautrbach
Sent: Friday, April 24, 2020 10:00 AM
To: [email protected]
Cc: Petr Lautrbach <[email protected]>
Subject: [PATCH] python/semanage: Use ipaddress module instead of IPy

ipaddress python module was added to standard library in Python 3.3 -
https://docs.python.org/3/library/ipaddress.html

seobject.py was the only consumer of IPy module so this dependency is not needed anymore.

---
 python/semanage/seobject.py | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
python/semanage/seobject.py +6 -7
index f2a139c970bd..c89c67e491b6 100644
@@ -32,7 +32,7 @@ from semanage import *  PROGNAME = "policycoreutils"
 import sepolicy
 import setools
-from IPy import IP
+import ipaddress
 
 try:
     import gettext
@@ -1860,13 +1860,13 @@ class nodeRecords(semanageRecords):
 
         # verify valid combination
         if len(mask) == 0 or mask[0] == "/":
-            i = IP(addr + mask)
-            newaddr = i.strNormal(0)
-            newmask = str(i.netmask())
-            if newmask == "0.0.0.0" and i.version() == 6:
+            i = ipaddress.ip_network(addr + mask)
+            newaddr = str(i.network_address)
+            newmask = str(i.netmask)
+            if newmask == "0.0.0.0" and i.version == 6:
                 newmask = "::"
 
-            protocol = "ipv%d" % i.version()
+            protocol = "ipv%d" % i.version
 
         try:
             newprotocol = self.protocol.index(protocol)
--
Quoted 1 line

2.26.0

LGTM
<Reviewed-by: [email protected]>

I can give it an acked by if you give me testing steps.

#2 lore raw
Quoted 34 lines · William C
Quoted 34 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Petr Lautrbach
Sent: Friday, April 24, 2020 10:00 AM
To: [email protected]
Cc: Petr Lautrbach <[email protected]>
Subject: [PATCH] python/semanage: Use ipaddress module instead of IPy

ipaddress python module was added to standard library in Python 3.3 -
https://docs.python.org/3/library/ipaddress.html

seobject.py was the only consumer of IPy module so this dependency is not needed anymore.

---
 python/semanage/seobject.py | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
python/semanage/seobject.py +1 -1
index f2a139c970bd..c89c67e491b6 100644
@@ -32,7 +32,7 @@ from semanage import *  PROGNAME = "policycoreutils"
 import sepolicy
 import setools
-from IPy import IP
+import ipaddress
 
 try:
     import gettext
@@ -1860,13 +1860,13 @@ class nodeRecords(semanageRecords):
 
Quoted 9 lines

python/semanage/seobject.py class nodeRecords(semanageRecords): · line 1865

Quoted 9 lines
python/semanage/seobject.py +3 -4
         # verify valid combination
         if len(mask) == 0 or mask[0] == "/":
-            i = IP(addr + mask)
-            newaddr = i.strNormal(0)
-            newmask = str(i.netmask())
-            if newmask == "0.0.0.0" and i.version() == 6:
+            i = ipaddress.ip_network(addr + mask)
+            newaddr = str(i.network_address)
+            newmask = str(i.netmask)

I was wondering whether there was a change of behavior with addresses that are not network masks, such as 10.0.0.1/24 (and why ipaddress.ip_network is used instead of ipaddress.ip_interface). But it seems that "semanage node" already only accepts valid network masks:

Before:

Quoted 1 line

python/semanage/seobject.py class nodeRecords(semanageRecords): · line 1866

Quoted 1 line
python/semanage/seobject.py +0 -0
 > IP('10.0.0.1/24').netmask()

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.8/site-packages/IPy.py", line 260, in __init__
raise ValueError("%s has invalid prefix length (%s)" %
(repr(self), self._prefixlen))

    ValueError: IP('10.0.0.1/24') has invalid prefix length (24)

After:

Quoted 1 line

python/semanage/seobject.py class nodeRecords(semanageRecords): · line 1867

Quoted 1 line
python/semanage/seobject.py +0 -0
 > ipaddress.ip_network('10.0.0.1/24')

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.8/ipaddress.py", line 74, in ip_network
return IPv4Network(address, strict)
File "/usr/lib/python3.8/ipaddress.py", line 1454, in __init__
raise ValueError('%s has host bits set' % self)
ValueError: 10.0.0.1/24 has host bits set

So this change looks good to me :) Nevertheless the comment can be improved, from "# verify valid combination" to "verify that (addr, mask) is either a IP address (without a mask) or a valid network mask", for example.

Quoted 2 lines

python/semanage/seobject.py class nodeRecords(semanageRecords): · line 1867

Quoted 2 lines
python/semanage/seobject.py +1 -0
+            if newmask == "0.0.0.0" and i.version == 6:
                 newmask = "::"

What does this check do? It seems to be a workaround for some bug in IPy related to IPv6, where an IPv6 address could return a 0.0.0.0 netmask. Does this bug also exist in ipaddress? If yes, it would be useful to add a comment to document how to reproduce this bug.

Quoted 9 lines
Quoted 8 lines
python/semanage/seobject.py +1 -1
-            protocol = "ipv%d" % i.version()
+            protocol = "ipv%d" % i.version
 
         try:

newprotocol = self.protocol.index(protocol)
--
2.26.0

Quoted 4 lines

LGTM
<Reviewed-by: [email protected]>

I can give it an acked by if you give me testing steps.

For testing such things, I usually use a Vagrant virtual machine: I have one to test Arch Linux packages
(https://github.com/archlinuxhardened/selinux/tree/master/_vagrant),
and I also contributed to a Debian VM to test refpolicy
(https://github.com/SELinuxProject/refpolicy/blob/2b2b5bad06f0eb2f45217ae337542e5e15bacda8/Vagrantfile#L121).
I usually patch the Arch Linux packages when testing small patches that can be applied on top of previous releases. When the previous release is quite old, I copy the whole project in the VM, run "make clean distclean && make DESTDIR=/tmp/dest install install-pywrap", relabel binaries in /tmp/dest and run them.

Anyway, +1 for migrating to ipaddress :) Thanks!
Nicolas

#3 lore raw

PATCH v2 1/1 python/semanage: Use ipaddress module instead of IPy

ipaddress python module was added to standard library in Python 3.3 -
https://docs.python.org/3/library/ipaddress.html

seobject.py was the only consumer of IPy module so this dependency is not needed anymore.

---

Based on Nicolas input:

- improved the check comment
- dropped the unnecessary check

 python/semanage/seobject.py | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
python/semanage/seobject.py +6 -9
index f2a139c970bd..6e0b87f2fa3c 100644
@@ -32,7 +32,7 @@ from semanage import *
 PROGNAME = "policycoreutils"
 import sepolicy
 import setools
-from IPy import IP
+import ipaddress
 
 try:
     import gettext
@@ -1858,15 +1858,12 @@ class nodeRecords(semanageRecords):
         if addr == "":
             raise ValueError(_("Node Address is required"))
 
-        # verify valid combination
+        # verify that (addr, mask) is either a IP address (without a mask) or a valid network mask
         if len(mask) == 0 or mask[0] == "/":
-            i = IP(addr + mask)
-            newaddr = i.strNormal(0)
-            newmask = str(i.netmask())
-            if newmask == "0.0.0.0" and i.version() == 6:
-                newmask = "::"
-
-            protocol = "ipv%d" % i.version()
+            i = ipaddress.ip_network(addr + mask)
+            newaddr = str(i.network_address)
+            newmask = str(i.netmask)
+            protocol = "ipv%d" % i.version
 
         try:
             newprotocol = self.protocol.index(protocol)
Signature · 1 line
2.26.2
Acked-by on v2 1/1
#4 lore raw
Quoted 7 lines · Petr Lautrbach

ipaddress python module was added to standard library in Python 3.3 -
https://docs.python.org/3/library/ipaddress.html

seobject.py was the only consumer of IPy module so this dependency is not needed anymore.

Quoted 7 lines

---

Based on Nicolas input:

- improved the check comment
- dropped the unnecessary check

I will apply this patch tomorrow if nobody else has any comment. Thanks,
Nicolas

Quoted 40 lines
 python/semanage/seobject.py | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
python/semanage/seobject.py +6 -10
index f2a139c970bd..6e0b87f2fa3c 100644
@@ -32,7 +32,7 @@ from semanage import *
 PROGNAME = "policycoreutils"
 import sepolicy
 import setools
-from IPy import IP
+import ipaddress
 
 try:
     import gettext
@@ -1858,15 +1858,12 @@ class nodeRecords(semanageRecords):
         if addr == "":
             raise ValueError(_("Node Address is required"))
 
-        # verify valid combination
+        # verify that (addr, mask) is either a IP address (without a mask) or a valid network mask
         if len(mask) == 0 or mask[0] == "/":
-            i = IP(addr + mask)
-            newaddr = i.strNormal(0)
-            newmask = str(i.netmask())
-            if newmask == "0.0.0.0" and i.version() == 6:
-                newmask = "::"
-
-            protocol = "ipv%d" % i.version()
+            i = ipaddress.ip_network(addr + mask)
+            newaddr = str(i.network_address)
+            newmask = str(i.netmask)
+            protocol = "ipv%d" % i.version
 
         try:
             newprotocol = self.protocol.index(protocol)
--

2.26.2

#5 lore raw
Quoted 12 lines · Nicolas Iooss
Quoted 10 lines · Petr Lautrbach

ipaddress python module was added to standard library in Python 3.3 -
https://docs.python.org/3/library/ipaddress.html

seobject.py was the only consumer of IPy module so this dependency is not needed anymore.

---

Quoted 7 lines
Quoted 4 lines

Based on Nicolas input:

- improved the check comment
- dropped the unnecessary check

Applied.
Thanks,
Nicolas

Quoted 40 lines
Quoted 40 lines
 python/semanage/seobject.py | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
python/semanage/seobject.py +6 -10
index f2a139c970bd..6e0b87f2fa3c 100644
@@ -32,7 +32,7 @@ from semanage import *
 PROGNAME = "policycoreutils"
 import sepolicy
 import setools
-from IPy import IP
+import ipaddress
 
 try:
     import gettext
@@ -1858,15 +1858,12 @@ class nodeRecords(semanageRecords):
         if addr == "":
             raise ValueError(_("Node Address is required"))
 
-        # verify valid combination
+        # verify that (addr, mask) is either a IP address (without a mask) or a valid network mask
         if len(mask) == 0 or mask[0] == "/":
-            i = IP(addr + mask)
-            newaddr = i.strNormal(0)
-            newmask = str(i.netmask())
-            if newmask == "0.0.0.0" and i.version() == 6:
-                newmask = "::"
-
-            protocol = "ipv%d" % i.version()
+            i = ipaddress.ip_network(addr + mask)
+            newaddr = str(i.network_address)
+            newmask = str(i.netmask)
+            protocol = "ipv%d" % i.version
 
         try:
             newprotocol = self.protocol.index(protocol)
--

2.26.2