Skip to contentSkip to conversation

Rewrite restorecon python method

PATCH Applied Petr Lautrbach 2 participants 4 messages Dec 22, 2016 – Jan 9, 2017 SELinux security module

lore
Share & tools
lore
#0 lore raw

Hi.

selinux.restorecon(path, recursive=True) uses matchpathcon() to get a label for a file and when the label is defined as <<None>>,it throws a backtrace with error:

"OSError: [Errno 2] No such file or directory"

It creates a problem for scripts which tries to relabel whole directory tree when there a subdirectory with a specification like this:

/var/lib/nfs/rpc_pipefs(/.*)? all files <<None>>

Quoted 1 line
Quoted 1 line
Quoted 1 line

selinux.restorecon('/var/lib', recursive=True)

Traceback (most recent call last):
File "/usr/lib64/python3.5/site-packages/selinux/__init__.py", line 114, in restorecon
status, context = matchpathcon(path, mode)
FileNotFoundError: [Errno 2] No such file or directory

At the same time, there's a rfe to rewrite restorecon() to use selinux_restorecon() which uses selabel_lookup() instead of deprecated matchpathcon() - [1]

The following 2 patches tries to address the described problem using the RFE.

First patch exports selinux_restorecon() to SWIG bindings. Second one rewites python implementation of restorecon() to use it.

[1] https://github.com/SELinuxProject/selinux/issues/29

Petr

#1 lore raw

PATCH 1/2 libselinux: Generate SWIG wrappers for selinux_restorecon()

---
 libselinux/src/selinuxswig.i | 2 ++
 1 file changed, 2 insertions(+)
libselinux/src/selinuxswig.i +2 -0
index c1e4ef7..687c43b 100644
@@ -9,6 +9,7 @@
 	#include "../include/selinux/get_context_list.h"
 	#include "../include/selinux/get_default_type.h"
 	#include "../include/selinux/label.h"
+	#include "../include/selinux/restorecon.h"
 	#include "../include/selinux/selinux.h"
 %}
 %apply int *OUTPUT { int *enforce };
@@ -61,4 +62,5 @@
 %include "../include/selinux/get_context_list.h"
 %include "../include/selinux/get_default_type.h"
 %include "../include/selinux/label.h"
+%include "../include/selinux/restorecon.h"
 %include "../include/selinux/selinux.h"
Signature · 1 line
2.9.3
#2 lore raw

PATCH 2/2 libselinux: Rewrite restorecon() python method

In reply to Petr Lautrbach

When the restorecon method was added to the libselinux swig python bindings, there was no libselinux restorecon implementation and it he had to call matchpathcon() which is deprecated in favor of selabel_lookup().

The new restorecon method uses selinux_restorecon method from libselinux and which is exported by the previous commit.

https://github.com/SELinuxProject/selinux/issues/29

Fixes:

Quoted 1 line
Quoted 1 line
Quoted 1 line

selinux.restorecon('/var/lib', recursive=True)

Traceback (most recent call last):
File "/usr/lib64/python3.5/site-packages/selinux/__init__.py", line 114, in restorecon
status, context = matchpathcon(path, mode)
FileNotFoundError: [Errno 2] No such file or directory

---
 libselinux/src/selinuxswig_python.i | 42 +++++++++++++++----------------------
 1 file changed, 17 insertions(+), 25 deletions(-)
libselinux/src/selinuxswig_python.i +17 -25
index a239f30..be17cef 100644
@@ -19,31 +19,23 @@ DISABLED = -1
 PERMISSIVE = 0
 ENFORCING = 1
 
-def restorecon(path, recursive=False):
-    """ Restore SELinux context on a given path """
-
-    try:
-        mode = os.lstat(path)[stat.ST_MODE]
-        status, context = matchpathcon(path, mode)
-    except OSError:
-        path = os.path.realpath(os.path.expanduser(path))
-        mode = os.lstat(path)[stat.ST_MODE]
-        status, context = matchpathcon(path, mode)
-
-    if status == 0:
-        try:
-            status, oldcontext = lgetfilecon(path)
-        except OSError as e:
-            if e.errno != errno.ENODATA:
-                raise
-            oldcontext = None
-        if context != oldcontext:
-            lsetfilecon(path, context)
-
-        if recursive:
-            for root, dirs, files in os.walk(path):
-                for name in files + dirs:
-                   restorecon(os.path.join(root, name))
+def restorecon(path, recursive=False, verbose=False):
+    """ Restore SELinux context on a given path
+
+    Arguments:
+    path -- The pathname for the file or directory to be relabeled.
+
+    Keyword arguments:
+    recursive -- Change files and directories file labels recursively (default False)
+    verbose -- Show changes in file labels (default False)
+    """
+
+    restorecon_flags = SELINUX_RESTORECON_IGNORE_DIGEST | SELINUX_RESTORECON_REALPATH
+    if recursive:
+        restorecon_flags |= SELINUX_RESTORECON_RECURSE
+    if verbose:
+        restorecon_flags |= SELINUX_RESTORECON_VERBOSE
+    selinux_restorecon(os.path.expanduser(path), restorecon_flags)
 
 def chcon(path, context, recursive=False):
     """ Set the SELinux context on a given path """
Signature · 1 line
2.9.3
#3 lore raw

Re: [PATCH 2/2] libselinux: Rewrite restorecon() python method

Quoted 15 lines · Petr Lautrbach

When the restorecon method was added to the libselinux swig python bindings, there was no libselinux restorecon implementation and it he had to call matchpathcon() which is deprecated in favor of selabel_lookup().

The new restorecon method uses selinux_restorecon method from libselinux
and which is exported by the previous commit.

https://github.com/SELinuxProject/selinux/issues/29

Fixes:

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 8 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

selinux.restorecon('/var/lib', recursive=True)

Traceback (most recent call last):
  File "/usr/lib64/python3.5/site-packages/selinux/__init__.py", line 114, in restorecon
    status, context = matchpathcon(path, mode)
FileNotFoundError: [Errno 2] No such file or directory

Thanks, applied both patches.

Quoted 61 lines

---
 libselinux/src/selinuxswig_python.i | 42 +++++++++++++++----------
------------

 1 file changed, 17 insertions(+), 25 deletions(-)
libselinux/src/selinuxswig_python.i +0 -0
index a239f30..be17cef 100644
@@ -19,31 +19,23 @@ DISABLED = -1

 PERMISSIVE = 0
 ENFORCING = 1

libselinux/src/selinuxswig_python.i +8 -25
-def restorecon(path, recursive=False):
-    """ Restore SELinux context on a given path """
-
-    try:
-        mode = os.lstat(path)[stat.ST_MODE]
-        status, context = matchpathcon(path, mode)
-    except OSError:
-        path = os.path.realpath(os.path.expanduser(path))
-        mode = os.lstat(path)[stat.ST_MODE]
-        status, context = matchpathcon(path, mode)
-
-    if status == 0:
-        try:
-            status, oldcontext = lgetfilecon(path)
-        except OSError as e:
-            if e.errno != errno.ENODATA:
-                raise
-            oldcontext = None
-        if context != oldcontext:
-            lsetfilecon(path, context)
-
-        if recursive:
-            for root, dirs, files in os.walk(path):
-                for name in files + dirs:
-                   restorecon(os.path.join(root, name))
+def restorecon(path, recursive=False, verbose=False):
+    """ Restore SELinux context on a given path
+
+    Arguments:
+    path -- The pathname for the file or directory to be relabeled.
+
+    Keyword arguments:
+    recursive -- Change files and directories file labels

recursively (default False)
+    verbose -- Show changes in file labels (default False) +    """
+
+    restorecon_flags = SELINUX_RESTORECON_IGNORE_DIGEST | SELINUX_RESTORECON_REALPATH
+    if recursive:
+        restorecon_flags |= SELINUX_RESTORECON_RECURSE +    if verbose:
+        restorecon_flags |= SELINUX_RESTORECON_VERBOSE +    selinux_restorecon(os.path.expanduser(path), restorecon_flags)
 def chcon(path, context, recursive=False):
     """ Set the SELinux context on a given path """