Skip to contentSkip to conversation

Add restorecon and install methods for libselinux python bindings.

PATCH Daniel J Walsh 2 participants 9 messages Nov 17, 2008 – Jan 12, 2009 SELinux security module

lore
Share & tools
lore
#0 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Luke Macken wrote restorecon and install functions used in Fedora Infrastructure which can be used to install files with the proper context and to fix the labels of files/directories without having to exec restorecon.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAkkhjhwACgkQrlYvE4MpobPyDgCfZ3vdiX/irkv3A9ka89LvUV1s RjQAniK+8rHaotyzEVoCM/yIg8nvAk8x
=bePO
-----END PGP SIGNATURE-----

src/selinux.py +24 -1
diff --exclude-from=exclude -N -u -r nsalibselinux/src/selinux.py libselinux-2.0.75/src/selinux.py
@@ -1,5 +1,5 @@
 # This file was automatically generated by SWIG (http://www.swig.org).
-# Version 1.3.33
+# Version 1.3.35
 #
 # Don't modify this file, modify the SWIG interface instead.
 # This file is compatible with both classic and new-style classes.
@@ -48,6 +48,29 @@
 del types
 
 
+import shutil, os
+
+def restorecon(path, recursive=False):
+    """ Restore SELinux context on a given path """
+    mode = os.stat(path)[stat.ST_MODE]
+    status, context = matchpathcon(path, mode)
+    if status == 0:
+        lsetfilecon(path, context)
+        if recursive:
+            os.path.walk(path, lambda arg, dirname, fnames:
+                             map(restorecon, [os.path.join(dirname, fname)
+                              s                for fname in fnames]), None)
+
+def copytree(src, dest):
+    """ An SELinux-friendly shutil.copytree method """
+    shutil.copytree(src, dest)
+    restorecon(dest, recursive=True)
+
+def install(src, dest):
+    """ An SELinux-friendly shutil.move method """
+    shutil.move(src, dest)
+    restorecon(dest, recursive=True)
+
 is_selinux_enabled = _selinux.is_selinux_enabled
 is_selinux_mls_enabled = _selinux.is_selinux_mls_enabled
 getcon = _selinux.getcon
src/selinuxswig_python.i +26 -0
diff --exclude-from=exclude -N -u -r nsalibselinux/src/selinuxswig_python.i libselinux-2.0.75/src/selinuxswig_python.i
@@ -6,6 +6,32 @@
 	#include "selinux/selinux.h"
 %}
 
+%pythoncode %{
+
+import shutil, os
+
+def restorecon(path, recursive=False):
+    """ Restore SELinux context on a given path """
+    mode = os.stat(path)[stat.ST_MODE]
+    status, context = matchpathcon(path, mode)
+    if status == 0:
+        lsetfilecon(path, context)
+        if recursive:
+            os.path.walk(path, lambda arg, dirname, fnames:
+                             map(restorecon, [os.path.join(dirname, fname)
+                              s                for fname in fnames]), None)
+
+def copytree(src, dest):
+    """ An SELinux-friendly shutil.copytree method """
+    shutil.copytree(src, dest)
+    restorecon(dest, recursive=True)
+
+def install(src, dest):
+    """ An SELinux-friendly shutil.move method """
+    shutil.move(src, dest)
+    restorecon(dest, recursive=True)
+%}
+
 /* security_get_boolean_names() typemap */
 %typemap(argout) (char ***names, int *len) {
 	PyObject* list = PyList_New(*$2);
src/selinuxswig_wrap.c +70 -43

362 lines not on this page · raw message

#1 lore raw
Quoted 11 lines · Daniel J Walsh

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Luke Macken wrote restorecon and install functions used in Fedora Infrastructure which can be used to install files with the proper context and to fix the labels of files/directories without having to exec restorecon.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

Quoted 5 lines

iEYEARECAAYFAkkhjhwACgkQrlYvE4MpobPyDgCfZ3vdiX/irkv3A9ka89LvUV1s RjQAniK+8rHaotyzEVoCM/yIg8nvAk8x
=bePO
-----END PGP SIGNATURE-----

src/selinuxswig_python.i +0 -0
diff --exclude-from=exclude -N -u -r nsalibselinux/src/selinuxswig_python.i libselinux-2.0.75/src/selinuxswig_python.i
@@ -6,6 +6,32 @@
	#include "selinux/selinux.h"

%}

+%pythoncode %{
+
+import shutil, os
+
+def restorecon(path, recursive=False):
+ """ Restore SELinux context on a given path """
+ mode = os.stat(path)[stat.ST_MODE]

stat doesn't exist here, perhaps he meant mode?

+ status, context = matchpathcon(path, mode)
+ if status == 0:
+ lsetfilecon(path, context)
+ if recursive:
+ os.path.walk(path, lambda arg, dirname, fnames:
+ map(restorecon, [os.path.join(dirname, fname)
+ s for fname in fnames]), None)

typo, the s causes a syntax error

+
+def copytree(src, dest):

+    """ An SELinux-friendly shutil.copytree method """
+    shutil.copytree(src, dest)
+    restorecon(dest, recursive=True)
+
+def install(src, dest):
+    """ An SELinux-friendly shutil.move method """
+    shutil.move(src, dest)
+    restorecon(dest, recursive=True)
+%}
+
/* security_get_boolean_names() typemap */
%typemap(argout) (char ***names, int *len) {
	PyObject* list = PyList_New(*$2);

This patch doesn't appear correct, I'll fix the things above, have you been testing this at all?

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#2 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 51 lines · Joshua Brindle

Daniel J Walsh wrote:
Luke Macken wrote restorecon and install functions used in Fedora Infrastructure which can be used to install files with the proper context and to fix the labels of files/directories without having to exec restorecon.

diff --exclude-from=exclude -N -u -r
nsalibselinux/src/selinuxswig_python.i
libselinux-2.0.75/src/selinuxswig_python.i
--- nsalibselinux/src/selinuxswig_python.i 2008-08-28
09:34:24.000000000 -0400
+++ libselinux-2.0.75/src/selinuxswig_python.i 2008-11-14
17:09:50.000000000 -0500

(quoted diff) +7 -0
@@ -6,6 +6,32 @@
    #include "selinux/selinux.h"
 %}
 
+%pythoncode %{
+
+import shutil, os
+
+def restorecon(path, recursive=False):
+    """ Restore SELinux context on a given path """
+    mode = os.stat(path)[stat.ST_MODE]
 

stat doesn't exist here, perhaps he meant mode?

(quoted diff) +7 -0
+    status, context = matchpathcon(path, mode)
+    if status == 0:
+        lsetfilecon(path, context)
+        if recursive:
+            os.path.walk(path, lambda arg, dirname, fnames:
+                             map(restorecon, [os.path.join(dirname, fname)
+                              s                for fname in fnames]),

None)

typo, the s causes a syntax error

(quoted diff) +12 -0
+
+def copytree(src, dest):
+    """ An SELinux-friendly shutil.copytree method """
+    shutil.copytree(src, dest)
+    restorecon(dest, recursive=True)
+
+def install(src, dest):
+    """ An SELinux-friendly shutil.move method """
+    shutil.move(src, dest)
+    restorecon(dest, recursive=True)
+%}
+

/* security_get_boolean_names() typemap */

Quoted 5 lines
%typemap(argout) (char ***names, int *len) {
    PyObject* list = PyList_New(*$2);

This patch doesn't appear correct, I'll fix the things above, have you been testing this at all?

Must have sent you a bad patch.

This is what the current patch looks like.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAkljdt8ACgkQrlYvE4MpobMzpQCg5USqSeTJzdPipICo1MVwKn/c sz0An2fm7yc8T1my5y3Zi4kzIQP2OStQ
=oQ4y
-----END PGP SIGNATURE-----

src/selinuxswig_python.i +26 -0
diff --exclude-from=exclude -N -u -r nsalibselinux/src/selinuxswig_python.i libselinux-2.0.76/src/selinuxswig_python.i
@@ -6,6 +6,32 @@
 	#include "selinux/selinux.h"
 %}
 
+%pythoncode %{
+
+import shutil, os, stat
+
+def restorecon(path, recursive=False):
+    """ Restore SELinux context on a given path """
+    mode = os.stat(path)[stat.ST_MODE]
+    status, context = matchpathcon(path, mode)
+    if status == 0:
+        lsetfilecon(path, context)
+        if recursive:
+            os.path.walk(path, lambda arg, dirname, fnames:
+                             map(restorecon, [os.path.join(dirname, fname)
+                                              for fname in fnames]), None)
+
+def copytree(src, dest):
+    """ An SELinux-friendly shutil.copytree method """
+    shutil.copytree(src, dest)
+    restorecon(dest, recursive=True)
+
+def install(src, dest):
+    """ An SELinux-friendly shutil.move method """
+    shutil.move(src, dest)
+    restorecon(dest, recursive=True)
+%}
+
 /* security_get_boolean_names() typemap */
 %typemap(argout) (char ***names, int *len) {
 	PyObject* list = PyList_New(*$2);
#3 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 55 lines · Daniel J Walsh
Quoted 53 lines · Joshua Brindle

Daniel J Walsh wrote:
Luke Macken wrote restorecon and install functions used in Fedora Infrastructure which can be used to install files with the proper context and to fix the labels of files/directories without having to exec restorecon.

diff --exclude-from=exclude -N -u -r
nsalibselinux/src/selinuxswig_python.i
libselinux-2.0.75/src/selinuxswig_python.i
--- nsalibselinux/src/selinuxswig_python.i 2008-08-28
09:34:24.000000000 -0400
+++ libselinux-2.0.75/src/selinuxswig_python.i 2008-11-14
17:09:50.000000000 -0500

(quoted diff) +7 -0
@@ -6,6 +6,32 @@
    #include "selinux/selinux.h"
 %}
 
+%pythoncode %{
+
+import shutil, os
+
+def restorecon(path, recursive=False):
+    """ Restore SELinux context on a given path """
+    mode = os.stat(path)[stat.ST_MODE]
 

stat doesn't exist here, perhaps he meant mode?

(quoted diff) +7 -0
+    status, context = matchpathcon(path, mode)
+    if status == 0:
+        lsetfilecon(path, context)
+        if recursive:
+            os.path.walk(path, lambda arg, dirname, fnames:
+                             map(restorecon, [os.path.join(dirname, fname)
+                              s                for fname in fnames]),

None)

typo, the s causes a syntax error

(quoted diff) +12 -0
+
+def copytree(src, dest):
+    """ An SELinux-friendly shutil.copytree method """
+    shutil.copytree(src, dest)
+    restorecon(dest, recursive=True)
+
+def install(src, dest):
+    """ An SELinux-friendly shutil.move method """
+    shutil.move(src, dest)
+    restorecon(dest, recursive=True)
+%}
+

/* security_get_boolean_names() typemap */

%typemap(argout) (char ***names, int *len) {
    PyObject* list = PyList_New(*$2);
Quoted 8 lines
Quoted 2 lines

This patch doesn't appear correct, I'll fix the things above, have you been testing this at all?

Must have sent you a bad patch.

This is what the current patch looks like.

And this is still broken.

One more fix.

+ mode = os.stat(path)[stat.ST_MODE]
should be
+ mode = os.lstat(path)[stat.ST_MODE]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAkljeTgACgkQrlYvE4MpobP3sACgnyo4HIvhaQ3TWmfph94ElSvF 84gAoLi4v7/UaWmpvTO5oIdvHj/tsb0H
=D91y
-----END PGP SIGNATURE-----

src/selinuxswig_python.i +26 -0
diff --exclude-from=exclude -N -u -r nsalibselinux/src/selinuxswig_python.i libselinux-2.0.76/src/selinuxswig_python.i
@@ -6,6 +6,32 @@
 	#include "selinux/selinux.h"
 %}
 
+%pythoncode %{
+
+import shutil, os, stat
+
+def restorecon(path, recursive=False):
+    """ Restore SELinux context on a given path """
+    mode = os.lstat(path)[stat.ST_MODE]
+    status, context = matchpathcon(path, mode)
+    if status == 0:
+        lsetfilecon(path, context)
+        if recursive:
+            os.path.walk(path, lambda arg, dirname, fnames:
+                             map(restorecon, [os.path.join(dirname, fname)
+                                              for fname in fnames]), None)
+
+def copytree(src, dest):
+    """ An SELinux-friendly shutil.copytree method """
+    shutil.copytree(src, dest)
+    restorecon(dest, recursive=True)
+
+def install(src, dest):
+    """ An SELinux-friendly shutil.move method """
+    shutil.move(src, dest)
+    restorecon(dest, recursive=True)
+%}
+
 /* security_get_boolean_names() typemap */
 %typemap(argout) (char ***names, int *len) {
 	PyObject* list = PyList_New(*$2);
#4 lore raw
Quoted 70 lines · Daniel J Walsh

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 56 lines · Daniel J Walsh
Quoted 54 lines · Joshua Brindle

Daniel J Walsh wrote:
Luke Macken wrote restorecon and install functions used in Fedora Infrastructure which can be used to install files with the proper context and to fix the labels of files/directories without having to exec restorecon.

diff --exclude-from=exclude -N -u -r
nsalibselinux/src/selinuxswig_python.i
libselinux-2.0.75/src/selinuxswig_python.i
--- nsalibselinux/src/selinuxswig_python.i 2008-08-28
09:34:24.000000000 -0400
+++ libselinux-2.0.75/src/selinuxswig_python.i 2008-11-14
17:09:50.000000000 -0500

src/selinuxswig_python.i +7 -0
@@ -6,6 +6,32 @@
    #include "selinux/selinux.h"
 %}
 
+%pythoncode %{
+
+import shutil, os
+
+def restorecon(path, recursive=False):
+    """ Restore SELinux context on a given path """
+    mode = os.stat(path)[stat.ST_MODE]
 

stat doesn't exist here, perhaps he meant mode?

src/selinuxswig_python.i +7 -0
+    status, context = matchpathcon(path, mode)
+    if status == 0:
+        lsetfilecon(path, context)
+        if recursive:
+            os.path.walk(path, lambda arg, dirname, fnames:
+                             map(restorecon, [os.path.join(dirname, fname)
+                              s                for fname in fnames]),

None)

typo, the s causes a syntax error

src/selinuxswig_python.i +12 -0
+
+def copytree(src, dest):
+    """ An SELinux-friendly shutil.copytree method """
+    shutil.copytree(src, dest)
+    restorecon(dest, recursive=True)
+
+def install(src, dest):
+    """ An SELinux-friendly shutil.move method """
+    shutil.move(src, dest)
+    restorecon(dest, recursive=True)
+%}
+

/* security_get_boolean_names() typemap */

%typemap(argout) (char ***names, int *len) {
    PyObject* list = PyList_New(*$2);
Quoted 9 lines
Quoted 3 lines

This patch doesn't appear correct, I'll fix the things above, have you been testing this at all?

Must have sent you a bad patch.

This is what the current patch looks like.

Quoted 7 lines

And this is still broken.

One more fix.

+ mode = os.stat(path)[stat.ST_MODE]
should be
+ mode = os.lstat(path)[stat.ST_MODE]

Ok, this works but isn't ideal, for example if I try selinux.restorecon("/") as a non-root user there is no error, no exception throw, no indication that it failed. There is an exception thrown if the path doesn't exist which is good.

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#5 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 70 lines · Joshua Brindle
Quoted 69 lines · Daniel J Walsh

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 57 lines · Daniel J Walsh
Quoted 45 lines · Joshua Brindle

Daniel J Walsh wrote:
Luke Macken wrote restorecon and install functions used in Fedora Infrastructure which can be used to install files with the proper context and to fix the labels of files/directories without having to exec restorecon.

      diff --exclude-from=exclude -N -u -r

nsalibselinux/src/selinuxswig_python.i
libselinux-2.0.75/src/selinuxswig_python.i
--- nsalibselinux/src/selinuxswig_python.i 2008-08-28
09:34:24.000000000 -0400
+++ libselinux-2.0.75/src/selinuxswig_python.i 2008-11-14
17:09:50.000000000 -0500

src/selinuxswig_python.i +12 -0
@@ -6,6 +6,32 @@
    #include "selinux/selinux.h"
 %}
      +%pythoncode %{
+
+import shutil, os
+
+def restorecon(path, recursive=False):
+    """ Restore SELinux context on a given path """
+    mode = os.stat(path)[stat.ST_MODE]
      stat doesn't exist here, perhaps he meant mode?
      +    status, context = matchpathcon(path, mode)
+    if status == 0:
+        lsetfilecon(path, context)
+        if recursive:
+            os.path.walk(path, lambda arg, dirname, fnames:
+                             map(restorecon, [os.path.join(dirname,
 fname)
+                              s                for fname in fnames]),

None)
typo, the s causes a syntax error
+

src/selinuxswig_python.i +11 -0
+def copytree(src, dest):
+    """ An SELinux-friendly shutil.copytree method """
+    shutil.copytree(src, dest)
+    restorecon(dest, recursive=True)
+
+def install(src, dest):
+    """ An SELinux-friendly shutil.move method """
+    shutil.move(src, dest)
+    restorecon(dest, recursive=True)
+%}
+
Quoted 7 lines

/* security_get_boolean_names() typemap */ %typemap(argout) (char ***names, int *len) {
PyObject* list = PyList_New(*$2);
This patch doesn't appear correct, I'll fix the things above,
have you
been testing this at all?

Must have sent you a bad patch.

Quoted 3 lines

This is what the current patch looks like.

And this is still broken.

One more fix.

Quoted 9 lines
Quoted 3 lines

+ mode = os.stat(path)[stat.ST_MODE]
should be
+ mode = os.lstat(path)[stat.ST_MODE]

Ok, this works but isn't ideal, for example if I try selinux.restorecon("/") as a non-root user there is no error, no exception throw, no indication that it failed. There is an exception thrown if the path doesn't exist which is good.

That is a more fundamental problem in that selinux.lsetfilecon should be raising the exception.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAkljsZsACgkQrlYvE4MpobNcagCeI61AtrtseaQMlg+286l+mCgx N70AnjaLY5gwav7uJeoIvoW0W2XYbAjz
=OWP9
-----END PGP SIGNATURE-----

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#6 lore raw
Quoted 77 lines · Daniel J Walsh

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 73 lines · Joshua Brindle
Quoted 72 lines · Daniel J Walsh

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 57 lines · Daniel J Walsh
Quoted 45 lines · Joshua Brindle

Daniel J Walsh wrote:
Luke Macken wrote restorecon and install functions used in Fedora Infrastructure which can be used to install files with the proper context and to fix the labels of files/directories without having to exec restorecon.

      diff --exclude-from=exclude -N -u -r

nsalibselinux/src/selinuxswig_python.i
libselinux-2.0.75/src/selinuxswig_python.i
--- nsalibselinux/src/selinuxswig_python.i 2008-08-28
09:34:24.000000000 -0400
+++ libselinux-2.0.75/src/selinuxswig_python.i 2008-11-14
17:09:50.000000000 -0500

src/selinuxswig_python.i +12 -0
@@ -6,6 +6,32 @@
    #include "selinux/selinux.h"
 %}
      +%pythoncode %{
+
+import shutil, os
+
+def restorecon(path, recursive=False):
+    """ Restore SELinux context on a given path """
+    mode = os.stat(path)[stat.ST_MODE]
      stat doesn't exist here, perhaps he meant mode?
      +    status, context = matchpathcon(path, mode)
+    if status == 0:
+        lsetfilecon(path, context)
+        if recursive:
+            os.path.walk(path, lambda arg, dirname, fnames:
+                             map(restorecon, [os.path.join(dirname,
 fname)
+                              s                for fname in fnames]),

None)
typo, the s causes a syntax error
+

src/selinuxswig_python.i +11 -0
+def copytree(src, dest):
+    """ An SELinux-friendly shutil.copytree method """
+    shutil.copytree(src, dest)
+    restorecon(dest, recursive=True)
+
+def install(src, dest):
+    """ An SELinux-friendly shutil.move method """
+    shutil.move(src, dest)
+    restorecon(dest, recursive=True)
+%}
+
Quoted 7 lines

/* security_get_boolean_names() typemap */ %typemap(argout) (char ***names, int *len) {
PyObject* list = PyList_New(*$2);
This patch doesn't appear correct, I'll fix the things above,
have you
been testing this at all?

Must have sent you a bad patch.

Quoted 3 lines

This is what the current patch looks like.

And this is still broken.

One more fix.

+ mode = os.stat(path)[stat.ST_MODE]
should be
+ mode = os.lstat(path)[stat.ST_MODE]

Quoted 7 lines
Quoted 5 lines

Ok, this works but isn't ideal, for example if I try selinux.restorecon("/") as a non-root user there is no error, no exception throw, no indication that it failed. There is an exception thrown if the path doesn't exist which is good.

That is a more fundamental problem in that selinux.lsetfilecon should be raising the exception.

Ok, hopefully we can get someone to look at the wrappers and make sure they throw exceptions properly.

Back to this patch, it does restorecon after copying or moving files, which isn't ideal. I think it should be rewritten to set contexts and copy/move atomically.

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#7 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 87 lines · Joshua Brindle
Quoted 78 lines · Daniel J Walsh

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 74 lines · Joshua Brindle
Quoted 73 lines · Daniel J Walsh

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 58 lines · Daniel J Walsh
Quoted 46 lines · Joshua Brindle

Daniel J Walsh wrote:
Luke Macken wrote restorecon and install functions used in Fedora Infrastructure which can be used to install files with the proper context and to fix the labels of files/directories without having to exec restorecon.

      diff --exclude-from=exclude -N -u -r

nsalibselinux/src/selinuxswig_python.i
libselinux-2.0.75/src/selinuxswig_python.i
--- nsalibselinux/src/selinuxswig_python.i 2008-08-28
09:34:24.000000000 -0400
+++ libselinux-2.0.75/src/selinuxswig_python.i 2008-11-14
17:09:50.000000000 -0500

src/selinuxswig_python.i +12 -0
@@ -6,6 +6,32 @@
    #include "selinux/selinux.h"
 %}
      +%pythoncode %{
+
+import shutil, os
+
+def restorecon(path, recursive=False):
+    """ Restore SELinux context on a given path """
+    mode = os.stat(path)[stat.ST_MODE]
      stat doesn't exist here, perhaps he meant mode?
      +    status, context = matchpathcon(path, mode)
+    if status == 0:
+        lsetfilecon(path, context)
+        if recursive:
+            os.path.walk(path, lambda arg, dirname, fnames:
+                             map(restorecon, [os.path.join(dirname,
 fname)
+                              s                for fname in

fnames]),
None)
typo, the s causes a syntax error
+

src/selinuxswig_python.i +11 -0
+def copytree(src, dest):
+    """ An SELinux-friendly shutil.copytree method """
+    shutil.copytree(src, dest)
+    restorecon(dest, recursive=True)
+
+def install(src, dest):
+    """ An SELinux-friendly shutil.move method """
+    shutil.move(src, dest)
+    restorecon(dest, recursive=True)
+%}
+
Quoted 7 lines

/* security_get_boolean_names() typemap */ %typemap(argout) (char ***names, int *len) {
PyObject* list = PyList_New(*$2);
This patch doesn't appear correct, I'll fix the things above,
have you
been testing this at all?

Must have sent you a bad patch.

Quoted 3 lines

This is what the current patch looks like.

And this is still broken.

One more fix.

+ mode = os.stat(path)[stat.ST_MODE]
should be
+ mode = os.lstat(path)[stat.ST_MODE]

Quoted 7 lines
Quoted 5 lines

Ok, this works but isn't ideal, for example if I try selinux.restorecon("/") as a non-root user there is no error, no exception throw, no indication that it failed. There is an exception thrown if the path doesn't exist which is good.

That is a more fundamental problem in that selinux.lsetfilecon should be raising the exception.

Quoted 6 lines

Ok, hopefully we can get someone to look at the wrappers and make sure they throw exceptions properly.

Back to this patch, it does restorecon after copying or moving files, which isn't ideal. I think it should be rewritten to set contexts and copy/move atomically.

Well since this is using shutil.copy and shutil.move it is not likely that this is going to be upstreamable to them. Especially when what happens on a move and copy is not well understood. I order to do the write thing, we would need to reimplent this code within the selinux python module or make selinux a requirement of shutil. Either way we end up with a recursive import which would be a problem.

So eliminate everything but restorecon.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAklrl+YACgkQrlYvE4MpobNPTgCg109mB4oXBoASzhJQhGf3qwf4 9DsAoJ1MEWWACLyci7iK9VeVn2m9xDae
=2t4M
-----END PGP SIGNATURE-----

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#8 lore raw
In reply to Daniel J Walsh
Quoted 59 lines · Daniel J Walsh

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 53 lines · Daniel J Walsh
Quoted 45 lines · Joshua Brindle

Daniel J Walsh wrote:
Luke Macken wrote restorecon and install functions used in Fedora Infrastructure which can be used to install files with the proper context and to fix the labels of files/directories without having to exec restorecon.
diff --exclude-from=exclude -N -u -r nsalibselinux/src/selinuxswig_python.i libselinux-2.0.75/src/selinuxswig_python.i
--- nsalibselinux/src/selinuxswig_python.i 2008-08-28
09:34:24.000000000 -0400
+++ libselinux-2.0.75/src/selinuxswig_python.i 2008-11-14
17:09:50.000000000 -0500

src/selinuxswig_python.i +7 -0
@@ -6,6 +6,32 @@
    #include "selinux/selinux.h"
 %}
+%pythoncode %{
+
+import shutil, os
+
+def restorecon(path, recursive=False):
+    """ Restore SELinux context on a given path """
+    mode = os.stat(path)[stat.ST_MODE]

stat doesn't exist here, perhaps he meant mode?

src/selinuxswig_python.i +7 -0
+    status, context = matchpathcon(path, mode)
+    if status == 0:
+        lsetfilecon(path, context)
+        if recursive:
+            os.path.walk(path, lambda arg, dirname, fnames:
+                             map(restorecon, [os.path.join(dirname, fname)
+                              s                for fname in fnames]),

None)
typo, the s causes a syntax error

src/selinuxswig_python.i +12 -0
+
+def copytree(src, dest):
+    """ An SELinux-friendly shutil.copytree method """
+    shutil.copytree(src, dest)
+    restorecon(dest, recursive=True)
+
+def install(src, dest):
+    """ An SELinux-friendly shutil.move method """
+    shutil.move(src, dest)
+    restorecon(dest, recursive=True)
+%}
+

/* security_get_boolean_names() typemap */

Quoted 4 lines
%typemap(argout) (char ***names, int *len) {
    PyObject* list = PyList_New(*$2);

This patch doesn't appear correct, I'll fix the things above, have you been testing this at all?

Must have sent you a bad patch.

Quoted 2 lines

This is what the current patch looks like.

Quoted 7 lines

And this is still broken.

One more fix.

+ mode = os.stat(path)[stat.ST_MODE]
should be
+ mode = os.lstat(path)[stat.ST_MODE]

merged in libselinux 2.0.77 without copytree and install.

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.