Skip to contentSkip to conversation

python/sepolgen: close /etc/selinux/sepolgen.conf after parsing it

PATCH In review Nicolas Iooss 2 participants 5 messages Dec 21, 2018 – Jan 7, 2019 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 python/sepolgen: close /etc/selinux/sepolgen.conf after parsing it

sepolgen testsuite reports the following warning on a system with /etc/selinux/sepolgen.conf:

.../src/./sepolgen/defaults.py:35: ResourceWarning: unclosed file <_io.TextIOWrapper name='/etc/selinux/sepolgen.conf' mode='r' encoding='UTF-8'>

Fix this by properly closing the file in PathChooser.__init__().

---
 python/sepolgen/src/sepolgen/defaults.py | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
python/sepolgen/src/sepolgen/defaults.py +7 -6
index 199acfafe4cf..533a90412475 100644
@@ -32,12 +32,13 @@ class PathChooser(object):
         self.config_pathname = pathname
         ignore = re.compile(r"^\s*(?:#.+)?$")
         consider = re.compile(r"^\s*(\w+)\s*=\s*(.+?)\s*$")
-        for lineno, line in enumerate(open(pathname)):
-            if ignore.match(line): continue
-            mo = consider.match(line)
-            if not mo:
-                raise ValueError("%s:%d: line is not in key = value format" % (pathname, lineno+1))
-            self.config[mo.group(1)] = mo.group(2)
+        with open(pathname, "r") as fd:
+            for lineno, line in enumerate(fd):
+                if ignore.match(line): continue
+                mo = consider.match(line)
+                if not mo:
+                    raise ValueError("%s:%d: line is not in key = value format" % (pathname, lineno+1))
+                self.config[mo.group(1)] = mo.group(2)
 
     # We're only exporting one useful function, so why not be a function
     def __call__(self, testfilename, pathset="SELINUX_DEVEL_PATH"):
Signature · 1 line
2.19.1
#1 lore raw

PATCH 2/4 python/audit2allow/sepolgen-ifgen: add missing \n to error message

---
 python/audit2allow/sepolgen-ifgen | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
python/audit2allow/sepolgen-ifgen +1 -1
index acf96384ee16..aa1ae8dc21d2 100644
@@ -96,7 +96,7 @@ def get_attrs(policy_path):
     ret = subprocess.Popen([ATTR_HELPER, policy_path, outfile.name], stdout=fd).wait()
     fd.close()
     if ret != 0:
-        sys.stderr.write("could not run attribute helper")
+        sys.stderr.write("could not run attribute helper\n")
         return None
 
     attrs = interfaces.AttributeSet()
Signature · 1 line
2.19.1
#2 lore raw

PATCH 3/4 python/audit2allow/sepolgen-ifgen: show errors on stderr

In reply to Nicolas Iooss

This allows test_audit2allow.py to display the errors correctly.

---
 python/audit2allow/sepolgen-ifgen | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
python/audit2allow/sepolgen-ifgen +1 -2
index aa1ae8dc21d2..e3f67d430647 100644
@@ -135,8 +135,7 @@ def main():
     try:
         headers = refparser.parse_headers(options.headers, output=log, debug=options.debug)
     except ValueError as e:
-        print("error parsing headers")
-        print(str(e))
+        sys.stderr.write("error parsing headers: %s\n" % e)
         return 1
 
     if_set = interfaces.InterfaceSet(output=log)
Signature · 1 line
2.19.1
#3 lore raw

PATCH 4/4 python/audit2allow: allow using audit2why as non-root user

In reply to Nicolas Iooss

Importing sepolicy as non-root on a system with SELinux causes the following exception to be raised:

ValueError: No SELinux Policy installed

Ignore this when using audit2why, which allows using it with option --policy as a non-root user.

---
 python/audit2allow/audit2allow | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
python/audit2allow/audit2allow +8 -3
index 195f151c6ca1..18fe0a531d02 100644
@@ -242,7 +242,10 @@ class AuditToPolicy:
 
     def __output_audit2why(self):
         import selinux
-        import sepolicy
+        try:
+            import sepolicy
+        except (ImportError, ValueError):
+            sepolicy = None
         for i in self.__parser.avc_msgs:
             rc = i.type
             data = i.data
@@ -262,11 +265,13 @@ class AuditToPolicy:
                 if len(data) > 1:
                     print("\tOne of the following booleans was set incorrectly.")
                     for b in data:
-                        print("\tDescription:\n\t%s\n" % sepolicy.boolean_desc(b[0]))
+                        if sepolicy is not None:
+                            print("\tDescription:\n\t%s\n" % sepolicy.boolean_desc(b[0]))
                         print("\tAllow access by executing:\n\t# setsebool -P %s %d" % (b[0], b[1]))
                 else:
                     print("\tThe boolean %s was set incorrectly. " % (data[0][0]))
-                    print("\tDescription:\n\t%s\n" % sepolicy.boolean_desc(data[0][0]))
+                    if sepolicy is not None:
+                        print("\tDescription:\n\t%s\n" % sepolicy.boolean_desc(data[0][0]))
                     print("\tAllow access by executing:\n\t# setsebool -P %s %d" % (data[0][0], data[0][1]))
                 continue
 
Signature · 1 line
2.19.1
#4 lore raw

Re: [PATCH 4/4] python/audit2allow: allow using audit2why as non-root user

Quoted 9 lines · Nicolas Iooss

Importing sepolicy as non-root on a system with SELinux causes the following exception to be raised:

ValueError: No SELinux Policy installed

Ignore this when using audit2why, which allows using it with option --policy as a non-root user.

All 4 merged. Thanks!

Quoted 35 lines
---
 python/audit2allow/audit2allow | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
python/audit2allow/audit2allow +8 -3
index 195f151c6ca1..18fe0a531d02 100644
@@ -242,7 +242,10 @@ class AuditToPolicy:
 
     def __output_audit2why(self):
         import selinux
-        import sepolicy
+        try:
+            import sepolicy
+        except (ImportError, ValueError):
+            sepolicy = None
         for i in self.__parser.avc_msgs:
             rc = i.type
             data = i.data
@@ -262,11 +265,13 @@ class AuditToPolicy:
                 if len(data) > 1:
                     print("\tOne of the following booleans was set incorrectly.")
                     for b in data:
-                        print("\tDescription:\n\t%s\n" % sepolicy.boolean_desc(b[0]))
+                        if sepolicy is not None:
+                            print("\tDescription:\n\t%s\n" % sepolicy.boolean_desc(b[0]))
                         print("\tAllow access by executing:\n\t# setsebool -P %s %d" % (b[0], b[1]))
                 else:
                     print("\tThe boolean %s was set incorrectly. " % (data[0][0]))
-                    print("\tDescription:\n\t%s\n" % sepolicy.boolean_desc(data[0][0]))
+                    if sepolicy is not None:
+                        print("\tDescription:\n\t%s\n" % sepolicy.boolean_desc(data[0][0]))
                     print("\tAllow access by executing:\n\t# setsebool -P %s %d" % (data[0][0], data[0][1]))
                 continue