Skip to contentSkip to conversation

semanage, sepolicy: list also ports not attributed with port_type

PATCH Superseded Topi Miettinen 2 participants 4 messages Jun 13, 2023 – Jun 16, 2023 SELinux security module

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 semanage, sepolicy: list also ports not attributed with port_type

For semanage port -l and sepolicy network -t type, show also ports which are not attributed with port_type. Such ports may exist in custom policies and even the attribute port_type may not be defined.

This fixes the following error with semanage port -l (and similar error with sepolicy network -t type):

Traceback (most recent call last):
File "/usr/sbin/semanage", line 975, in <module>

    do_parser()

File "/usr/sbin/semanage", line 947, in do_parser
args.func(args)
File "/usr/sbin/semanage", line 441, in handlePort
OBJECT = object_dict['port'](args)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/seobject.py", line 1057, in __init__
self.valid_types = list(list(sepolicy.info(sepolicy.ATTRIBUTE, "port_type"))[0]["types"])
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range

---

v3: use even better version, thanks to Petr Lautrbach
v2: fix other cases and use better version courtesy of Petr Lautrbach

---
 python/semanage/semanage-bash-completion.sh | 2 +-
 python/semanage/seobject.py                 | 2 +-
 python/sepolicy/sepolicy-bash-completion.sh | 2 +-
 python/sepolicy/sepolicy/__init__.py        | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)
python/semanage/semanage-bash-completion.sh +1 -1
index d0dd139f..1e3f6f9d 100644
@@ -37,7 +37,7 @@ __get_all_types () {
     seinfo -t 2> /dev/null | tail -n +3 
 }
 __get_all_port_types () { 
-    seinfo -aport_type -x 2>/dev/null | tail -n +2 
+    sepolicy network -l
 }
 __get_all_domains () { 
     seinfo -adomain -x 2>/dev/null | tail -n +2 
python/semanage/seobject.py +1 -1
index d82da494..323aae3f 100644
@@ -1055,7 +1055,7 @@ class portRecords(semanageRecords):
     def __init__(self, args = None):
         semanageRecords.__init__(self, args)
         try:
-            self.valid_types = list(list(sepolicy.info(sepolicy.ATTRIBUTE, "port_type"))[0]["types"])
+            self.valid_types = [x["type"] for x in sepolicy.info(sepolicy.PORT)]
         except RuntimeError:
             pass
 
python/sepolicy/sepolicy-bash-completion.sh +1 -1
index 13638e4d..467333b8 100644
@@ -52,7 +52,7 @@ __get_all_classes () {
     seinfo -c 2> /dev/null | tail -n +2
 }
 __get_all_port_types () {
-    seinfo -aport_type -x 2> /dev/null | tail -n +2
+    sepolicy network -l
 }
 __get_all_domain_types () {
     seinfo -adomain -x 2> /dev/null | tail -n +2
python/sepolicy/sepolicy/__init__.py +1 -1
index c177cdfc..9d3b640b 100644
@@ -989,7 +989,7 @@ def get_all_port_types():
     global port_types
     if port_types:
         return port_types
-    port_types = list(sorted(info(ATTRIBUTE, "port_type"))[0]["types"])
+    port_types = [x["type"] for x in info(PORT)]
     return port_types
 
 
Signature · 1 line
2.39.2
#1 lore raw
Quoted 51 lines · Topi Miettinen

For semanage port -l and sepolicy network -t type, show also ports which are not attributed with port_type. Such ports may exist in custom policies and even the attribute port_type may not be defined.

This fixes the following error with semanage port -l (and similar error with sepolicy network -t type):

Traceback (most recent call last):
File "/usr/sbin/semanage", line 975, in <module>

    do_parser()

File "/usr/sbin/semanage", line 947, in do_parser
args.func(args)
File "/usr/sbin/semanage", line 441, in handlePort
OBJECT = object_dict['port'](args)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/seobject.py", line 1057, in __init__
self.valid_types = list(list(sepolicy.info(sepolicy.ATTRIBUTE, "port_type"))[0]["types"])
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range

---

v3: use even better version, thanks to Petr Lautrbach
v2: fix other cases and use better version courtesy of Petr Lautrbach

---
 python/semanage/semanage-bash-completion.sh | 2 +-
 python/semanage/seobject.py                 | 2 +-
 python/sepolicy/sepolicy-bash-completion.sh | 2 +-
 python/sepolicy/sepolicy/__init__.py        | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)
python/semanage/semanage-bash-completion.sh +1 -1
index d0dd139f..1e3f6f9d 100644
@@ -37,7 +37,7 @@ __get_all_types () {
     seinfo -t 2> /dev/null | tail -n +3 
 }
 __get_all_port_types () { 
-    seinfo -aport_type -x 2>/dev/null | tail -n +2 
+    sepolicy network -l
 }
 __get_all_domains () { 
     seinfo -adomain -x 2>/dev/null | tail -n +2 
python/semanage/seobject.py +0 -0
index d82da494..323aae3f 100644

python/semanage/seobject.py class portRecords(semanageRecords): · line 1060

Quoted 7 lines
python/semanage/seobject.py +1 -1
@@ -1055,7 +1055,7 @@ class portRecords(semanageRecords):
     def __init__(self, args = None):
         semanageRecords.__init__(self, args)
         try:
-            self.valid_types = list(list(sepolicy.info(sepolicy.ATTRIBUTE, "port_type"))[0]["types"])
+            self.valid_types = [x["type"] for x in sepolicy.info(sepolicy.PORT)]
         except RuntimeError:
             pass

It looked good to me but I was getting different number of types when I searched for port_type attribute method and for portcon (seinfo --portcon).

So I compared lists and checked differences and it turned out that this would break using semanage port -a ... for all policies based on refpolicy.

In refpolicy there are types with port_type attribute but without any portcon statement, see this commit
https://github.com/SELinuxProject/refpolicy/commit/a108d9db60747a887f626b99cce37738462dd3cd

SELinux notebook uses port_type attribute too -
https://github.com/SELinuxProject/selinux-notebook/blob/main/src/network_support.md#socket-controls-access-control-for-network-ports

I.e. using this change it would not be possible to add a new port mapping for types like stunnel_port_t:

    # seinfo -aport_type -x | grep stunnel
        stunnel_port_t
    # semanage port -a -t stunnel_port_t -p tcp 12345
    ValueError: Type stunnel_port_t is invalid, must be a port type

Thanks,

Petr

Quoted 28 lines
python/sepolicy/sepolicy-bash-completion.sh +1 -1
index 13638e4d..467333b8 100644
@@ -52,7 +52,7 @@ __get_all_classes () {
     seinfo -c 2> /dev/null | tail -n +2
 }
 __get_all_port_types () {
-    seinfo -aport_type -x 2> /dev/null | tail -n +2
+    sepolicy network -l
 }
 __get_all_domain_types () {
     seinfo -adomain -x 2> /dev/null | tail -n +2
python/sepolicy/sepolicy/__init__.py +1 -1
index c177cdfc..9d3b640b 100644
@@ -989,7 +989,7 @@ def get_all_port_types():
     global port_types
     if port_types:
         return port_types
-    port_types = list(sorted(info(ATTRIBUTE, "port_type"))[0]["types"])
+    port_types = [x["type"] for x in info(PORT)]
     return port_types
 
 
Signature · 1 line
2.39.2
#2 lore raw
Quoted 76 lines · Petr Lautrbach
Quoted 51 lines · Topi Miettinen

For semanage port -l and sepolicy network -t type, show also ports which are not attributed with port_type. Such ports may exist in custom policies and even the attribute port_type may not be defined.

This fixes the following error with semanage port -l (and similar error with sepolicy network -t type):

Traceback (most recent call last):
File "/usr/sbin/semanage", line 975, in <module>

     do_parser()

File "/usr/sbin/semanage", line 947, in do_parser
args.func(args)
File "/usr/sbin/semanage", line 441, in handlePort
OBJECT = object_dict['port'](args)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/seobject.py", line 1057, in __init__
self.valid_types = list(list(sepolicy.info(sepolicy.ATTRIBUTE, "port_type"))[0]["types"])
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range

---

v3: use even better version, thanks to Petr Lautrbach
v2: fix other cases and use better version courtesy of Petr Lautrbach

---
  python/semanage/semanage-bash-completion.sh | 2 +-
  python/semanage/seobject.py                 | 2 +-
  python/sepolicy/sepolicy-bash-completion.sh | 2 +-
  python/sepolicy/sepolicy/__init__.py        | 2 +-
  4 files changed, 4 insertions(+), 4 deletions(-)
python/semanage/semanage-bash-completion.sh +1 -1
index d0dd139f..1e3f6f9d 100644
@@ -37,7 +37,7 @@ __get_all_types () {
      seinfo -t 2> /dev/null | tail -n +3
  }
  __get_all_port_types () {
-    seinfo -aport_type -x 2>/dev/null | tail -n +2
+    sepolicy network -l
  }
  __get_all_domains () {
      seinfo -adomain -x 2>/dev/null | tail -n +2
python/semanage/seobject.py +0 -0
index d82da494..323aae3f 100644

python/semanage/seobject.py class portRecords(semanageRecords): · line 1060

Quoted 7 lines
python/semanage/seobject.py +1 -1
@@ -1055,7 +1055,7 @@ class portRecords(semanageRecords):
      def __init__(self, args = None):
          semanageRecords.__init__(self, args)
          try:
-            self.valid_types = list(list(sepolicy.info(sepolicy.ATTRIBUTE, "port_type"))[0]["types"])
+            self.valid_types = [x["type"] for x in sepolicy.info(sepolicy.PORT)]
          except RuntimeError:
              pass

It looked good to me but I was getting different number of types when I searched for port_type attribute method and for portcon (seinfo --portcon).

So I compared lists and checked differences and it turned out that this would break using semanage port -a ... for all policies based on refpolicy.

In refpolicy there are types with port_type attribute but without any portcon statement, see this commit
https://github.com/SELinuxProject/refpolicy/commit/a108d9db60747a887f626b99cce37738462dd3cd

SELinux notebook uses port_type attribute too -
https://github.com/SELinuxProject/selinux-notebook/blob/main/src/network_support.md#socket-controls-access-control-for-network-ports

Quoted 8 lines

I.e. using this change it would not be possible to add a new port mapping for types like stunnel_port_t:

     # seinfo -aport_type -x | grep stunnel
         stunnel_port_t
     # semanage port -a -t stunnel_port_t -p tcp 12345
     ValueError: Type stunnel_port_t is invalid, must be a port type

Perhaps both plain types attributed with port_type and actual portcon types could be considered? Then the tools should work with refpolicy and custom policies.

-Topi

Quoted 36 lines

Thanks,

Petr

Quoted 28 lines
python/sepolicy/sepolicy-bash-completion.sh +1 -1
index 13638e4d..467333b8 100644
@@ -52,7 +52,7 @@ __get_all_classes () {
      seinfo -c 2> /dev/null | tail -n +2
  }
  __get_all_port_types () {
-    seinfo -aport_type -x 2> /dev/null | tail -n +2
+    sepolicy network -l
  }
  __get_all_domain_types () {
      seinfo -adomain -x 2> /dev/null | tail -n +2
python/sepolicy/sepolicy/__init__.py +1 -1
index c177cdfc..9d3b640b 100644
@@ -989,7 +989,7 @@ def get_all_port_types():
      global port_types
      if port_types:
          return port_types
-    port_types = list(sorted(info(ATTRIBUTE, "port_type"))[0]["types"])
+    port_types = [x["type"] for x in info(PORT)]
      return port_types
 
 
Signature · 1 line
2.39.2
#3 lore raw
Quoted 83 lines · Topi Miettinen
Quoted 82 lines · Petr Lautrbach
Quoted 51 lines · Topi Miettinen

For semanage port -l and sepolicy network -t type, show also ports which are not attributed with port_type. Such ports may exist in custom policies and even the attribute port_type may not be defined.

This fixes the following error with semanage port -l (and similar error with sepolicy network -t type):

Traceback (most recent call last):
File "/usr/sbin/semanage", line 975, in <module>

     do_parser()

File "/usr/sbin/semanage", line 947, in do_parser
args.func(args)
File "/usr/sbin/semanage", line 441, in handlePort
OBJECT = object_dict['port'](args)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/seobject.py", line 1057, in __init__
self.valid_types = list(list(sepolicy.info(sepolicy.ATTRIBUTE, "port_type"))[0]["types"])
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range

---

v3: use even better version, thanks to Petr Lautrbach
v2: fix other cases and use better version courtesy of Petr Lautrbach

---
  python/semanage/semanage-bash-completion.sh | 2 +-
  python/semanage/seobject.py                 | 2 +-
  python/sepolicy/sepolicy-bash-completion.sh | 2 +-
  python/sepolicy/sepolicy/__init__.py        | 2 +-
  4 files changed, 4 insertions(+), 4 deletions(-)
python/semanage/semanage-bash-completion.sh +1 -1
index d0dd139f..1e3f6f9d 100644
@@ -37,7 +37,7 @@ __get_all_types () {
      seinfo -t 2> /dev/null | tail -n +3
  }
  __get_all_port_types () {
-    seinfo -aport_type -x 2>/dev/null | tail -n +2
+    sepolicy network -l
  }
  __get_all_domains () {
      seinfo -adomain -x 2>/dev/null | tail -n +2
python/semanage/seobject.py +0 -0
index d82da494..323aae3f 100644

python/semanage/seobject.py class portRecords(semanageRecords): · line 1060

Quoted 7 lines
python/semanage/seobject.py +1 -1
@@ -1055,7 +1055,7 @@ class portRecords(semanageRecords):
      def __init__(self, args = None):
          semanageRecords.__init__(self, args)
          try:
-            self.valid_types = list(list(sepolicy.info(sepolicy.ATTRIBUTE, "port_type"))[0]["types"])
+            self.valid_types = [x["type"] for x in sepolicy.info(sepolicy.PORT)]
          except RuntimeError:
              pass

It looked good to me but I was getting different number of types when I searched for port_type attribute method and for portcon (seinfo --portcon).

So I compared lists and checked differences and it turned out that this would break using semanage port -a ... for all policies based on refpolicy.

In refpolicy there are types with port_type attribute but without any portcon statement, see this commit
https://github.com/SELinuxProject/refpolicy/commit/a108d9db60747a887f626b99cce37738462dd3cd

SELinux notebook uses port_type attribute too -
https://github.com/SELinuxProject/selinux-notebook/blob/main/src/network_support.md#socket-controls-access-control-for-network-ports

I.e. using this change it would not be possible to add a new port mapping for types like stunnel_port_t:

     # seinfo -aport_type -x | grep stunnel
         stunnel_port_t
Quoted 7 lines
Quoted 2 lines
     # semanage port -a -t stunnel_port_t -p tcp 12345
     ValueError: Type stunnel_port_t is invalid, must be a port type

Perhaps both plain types attributed with port_type and actual portcon types could be considered? Then the tools should work with refpolicy and custom policies.

I guess it could work.

Quoted 37 lines
Quoted 37 lines

Thanks,

Petr

Quoted 28 lines
python/sepolicy/sepolicy-bash-completion.sh +1 -1
index 13638e4d..467333b8 100644
@@ -52,7 +52,7 @@ __get_all_classes () {
      seinfo -c 2> /dev/null | tail -n +2
  }
  __get_all_port_types () {
-    seinfo -aport_type -x 2> /dev/null | tail -n +2
+    sepolicy network -l
  }
  __get_all_domain_types () {
      seinfo -adomain -x 2> /dev/null | tail -n +2
python/sepolicy/sepolicy/__init__.py +1 -1
index c177cdfc..9d3b640b 100644
@@ -989,7 +989,7 @@ def get_all_port_types():
      global port_types
      if port_types:
          return port_types
-    port_types = list(sorted(info(ATTRIBUTE, "port_type"))[0]["types"])
+    port_types = [x["type"] for x in info(PORT)]
      return port_types
 
 
Signature · 1 line
2.39.2