Skip to contentSkip to conversation

python/sepolgen: search fallback location for sepolgen.conf

PATCH In review Stephen Smalley 1 participant 3 messages Sep 28 – Sep 29 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/2 python/sepolgen: search fallback location for sepolgen.conf

sepolgen.conf is a top-level configuration file (not under the policy root), so libselinux's selinux_policy_open() search does not apply. Give PathChooser a first-existing search over a small tuple of candidate paths and add /usr/lib/selinux/sepolgen.conf as the fallback, matching the per-tool pattern used for other top-level configuration files (restorecond, newrole). A single string is still accepted so external PathChooser callers are unaffected.

Absent both files behavior is unchanged: PathChooser falls back to its built-in SELINUX_DEVEL_PATH list, so audit2allow -R / sepolgen-ifgen still find the refpolicy headers on a stock install.

---
 python/sepolgen/src/sepolgen/defaults.py | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
python/sepolgen/src/sepolgen/defaults.py +15 -3
index 6e800695..d72f5cd3 100644
@@ -25,7 +25,14 @@ import re
 class PathChooser(object):
     def __init__(self, pathname):
         self.config = dict()
-        if not os.path.exists(pathname):
+        if isinstance(pathname, str):
+            candidates = (pathname,)
+        else:
+            candidates = tuple(pathname)
+        for pathname in candidates:
+            if os.path.exists(pathname):
+                break
+        else:
             self.config_pathname = "(defaults)"
             self.config["SELINUX_DEVEL_PATH"] = "/usr/share/selinux/default:/usr/share/selinux/mls:/usr/share/selinux/devel"
             return
@@ -68,14 +75,19 @@ def interface_info():
 def attribute_info():
     return data_dir() + "/attribute_info"
 
+SEPOLGEN_CONF_PATHS = (
+    "/etc/selinux/sepolgen.conf",
+    "/usr/lib/selinux/sepolgen.conf",
+)
+
 def refpolicy_makefile():
-    chooser = PathChooser("/etc/selinux/sepolgen.conf")
+    chooser = PathChooser(SEPOLGEN_CONF_PATHS)
     result = chooser("Makefile")
     if not os.path.exists(result):
         result = chooser("include/Makefile")
     return result
 
 def headers():
-    chooser = PathChooser("/etc/selinux/sepolgen.conf")
+    chooser = PathChooser(SEPOLGEN_CONF_PATHS)
     return chooser("include")
 
Signature · 1 line
2.55.0
author maintainer
#1 lore raw

PATCH 2/2 gui/statusPage.py: also enumerate policy types under /usr/lib/selinux

read_selinux_config() populates the "SELinux Type" dropdown by listing /etc/selinux/*/policy/. On a stateless install where the shipped policies live under /usr/lib/selinux/ this yields an empty list even though the running policy was found there.

Walk each configuration root in turn and de-duplicate by name so an /etc override of a vendor policy is listed once. Sort the entries so the menu order is stable, and skip a root that does not exist rather than raising. write_selinux_config() continues to write via selinux_path() (i.e. to /etc/selinux/config in the writable configuration root).

---
 gui/statusPage.py | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
gui/statusPage.py +18 -7
index 832849e6..82b3490c 100644
@@ -30,6 +30,10 @@ DISABLED = -1
 modearray = ("disabled", "permissive", "enforcing")
 
 SELINUXDIR = "/etc/selinux/"
+# Fallback configuration roots searched (in order) for installed policy
+# types when populating the type menu. Writes always go to SELINUXDIR
+# via selinux_path().
+SELINUX_CONFDIRS = (SELINUXDIR, "/usr/lib/selinux/")
 RELABELFILE = "/.autorelabel"
 
 ##
@@ -195,13 +199,20 @@ class statusPage:
         n = 0
         current = n
 
-        for i in os.listdir(SELINUXDIR):
-            if os.path.isdir(SELINUXDIR + i) and os.path.isdir(SELINUXDIR + i + "/policy"):
-                self.types.append(i)
-                self.selinuxTypeOptionMenu.append_text(i)
-                if i == self.initialtype:
-                    current = n
-                n = n + 1
+        for confdir in SELINUX_CONFDIRS:
+            try:
+                entries = sorted(os.listdir(confdir))
+            except OSError:
+                continue
+            for i in entries:
+                if i in self.types:
+                    continue
+                if os.path.isdir(confdir + i + "/policy"):
+                    self.types.append(i)
+                    self.selinuxTypeOptionMenu.append_text(i)
+                    if i == self.initialtype:
+                        current = n
+                    n = n + 1
         self.selinuxTypeOptionMenu.set_active(current)
         self.typeHistory = current
 
Signature · 1 line
2.55.0
author maintainer
#2 lore raw
In reply to Stephen Smalley
Quoted 9 lines · Stephen Smalley

sepolgen.conf is a top-level configuration file (not under the policy root), so libselinux's selinux_policy_open() search does not apply. Give PathChooser a first-existing search over a small tuple of candidate paths and add /usr/lib/selinux/sepolgen.conf as the fallback, matching the per-tool pattern used for other top-level configuration files (restorecond, newrole). A single string is still accepted so external PathChooser callers are unaffected.

Quoted 5 lines

Absent both files behavior is unchanged: PathChooser falls back to its built-in SELINUX_DEVEL_PATH list, so audit2allow -R / sepolgen-ifgen still find the refpolicy headers on a stock install.

This series has been merged.