Skip to contentSkip to conversation

libselinux: compile Python bytecode when installing Python files

PATCH In review Nicolas Iooss 2 participants 7 messages Nov 2, 2019 – Nov 9, 2019 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 libselinux: compile Python bytecode when installing Python files

When selinux module is imported from a Python script, the content of __init__.py is compiled into bytecode and the result is saved into a file if it is allowed. For example, when root runs with Python 3.7 a script that uses "import selinux" on a system where SELinux is in permissive mode, this file may be created:

    /usr/lib/python3.7/site-packages/selinux/__pycache__/__init__.cpython-37.pyc

Prevent this file from being dynamically created by creating it when libselinux is installed, using "python -m compileall".

---
 libselinux/src/Makefile | 1 +
 1 file changed, 1 insertion(+)
libselinux/src/Makefile +1 -0
index 3b8bad810de0..349f957355c1 100644
@@ -176,6 +176,7 @@ install-pywrap: pywrap
 	$(PYTHON) setup.py install --prefix=$(PREFIX) `test -n "$(DESTDIR)" && echo --root $(DESTDIR)`
 	install -m 644 $(SWIGPYOUT) $(DESTDIR)$(PYTHONLIBDIR)/selinux/__init__.py
 	ln -sf --relative $(DESTDIR)$(PYTHONLIBDIR)/selinux/_selinux$(PYCEXT) $(DESTDIR)$(PYTHONLIBDIR)/_selinux$(PYCEXT)
+	$(PYTHON) -m compileall $(DESTDIR)$(PYTHONLIBDIR)/selinux
 
 install-rubywrap: rubywrap
 	test -d $(DESTDIR)$(RUBYINSTALL) || install -m 755 -d $(DESTDIR)$(RUBYINSTALL) 
Signature · 1 line
2.23.0
#1 lore raw

PATCH 2/4 libsemanage: compile Python bytecode when installing Python files

Create a cache file such as /usr/lib/python3.7/site-packages/__pycache__/semanage.cpython-37.pyc in order to prevent it from being dynamically created when semanage module is imported.

---
 libsemanage/src/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsemanage/src/Makefile +1 -1
index e029f0988dd8..a9adfa47bdc8 100644
@@ -145,7 +145,7 @@ install-pywrap: pywrap
 	test -d $(DESTDIR)$(PYTHONLIBDIR) || install -m 755 -d $(DESTDIR)$(PYTHONLIBDIR)
 	install -m 755 $(SWIGSO) $(DESTDIR)$(PYTHONLIBDIR)/_semanage$(PYCEXT)
 	install -m 644 semanage.py $(DESTDIR)$(PYTHONLIBDIR)
-
+	$(PYTHON) -m compileall $(DESTDIR)$(PYTHONLIBDIR)/semanage.py
 
 install-rubywrap: rubywrap
 	test -d $(DESTDIR)$(RUBYINSTALL) || install -m 755 -d $(DESTDIR)$(RUBYINSTALL) 
Signature · 1 line
2.23.0
#2 lore raw

PATCH 3/4 python: compile Python bytecode when installing Python files

In reply to Nicolas Iooss

Create a cache directory such as /usr/lib/python3.7/site-packages/sepolgen/__pycache__/ in order to prevent it from being dynamically created when sepolgen module is imported.

---
 python/semanage/Makefile              | 1 +
 python/sepolgen/src/sepolgen/Makefile | 1 +
 2 files changed, 2 insertions(+)
python/semanage/Makefile +1 -0
index 024e9640fed0..c6a16918360d 100644
@@ -28,6 +28,7 @@ install: all
 	done
 	test -d $(DESTDIR)/$(PACKAGEDIR) || install -m 755 -d $(DESTDIR)/$(PACKAGEDIR)
 	install -m 644 seobject.py $(DESTDIR)/$(PACKAGEDIR)
+	$(PYTHON) -m compileall $(DESTDIR)/$(PACKAGEDIR)/seobject.py
 	-mkdir -p $(DESTDIR)$(BASHCOMPLETIONDIR)
 	install -m 644 $(BASHCOMPLETIONS) $(DESTDIR)$(BASHCOMPLETIONDIR)/semanage
 
python/sepolgen/src/sepolgen/Makefile +1 -0
index cac8def7204e..95a9d41571e7 100644
@@ -8,6 +8,7 @@ all:
 install: all
 	-mkdir -p $(DESTDIR)$(PACKAGEDIR)
 	install -m 644 *.py $(DESTDIR)$(PACKAGEDIR)
+	$(PYTHON) -m compileall $(DESTDIR)$(PACKAGEDIR)
 
 clean:
 	rm -f parser.out parsetab.py
Signature · 1 line
2.23.0
#3 lore raw

PATCH 4/4 gui: compile Python bytecode when installing Python files

In reply to Nicolas Iooss

Create a cache directory such as /usr/share/system-config-selinux/__pycache__/ in order to prevent it from being dynamically created when the gui is used.

---
 gui/Makefile | 4 ++++
 1 file changed, 4 insertions(+)
gui/Makefile +4 -0
index ca965c942912..c3fd196fcd2c 100644
@@ -1,3 +1,5 @@
+PYTHON ?= python3
+
 # Installation directories.
 LINGUAS ?= ru
 PREFIX ?= /usr
@@ -54,6 +56,8 @@ install: all
 		install -m 644 sepolicy_$${i}.png $(DESTDIR)$(DATADIR)/icons/hicolor/$${i}x$${i}/apps/sepolicy.png; \
 	done
 	install -m 644 org.selinux.config.policy $(DESTDIR)$(DATADIR)/polkit-1/actions/
+	$(PYTHON) -m compileall $(DESTDIR)$(SHAREDIR)
+
 clean:
 
 indent:
Signature · 1 line
2.23.0
#4 lore raw
In reply to Nicolas Iooss

Hello Nicolas,

Quoted 6 lines · On Sat, 2 Nov 2019 18:28:09 +0100 Nicolas Iooss <[email protected]> wrote:

When selinux module is imported from a Python script, the content of __init__.py is compiled into bytecode and the result is saved into a file if it is allowed. For example, when root runs with Python 3.7 a script that uses "import selinux" on a system where SELinux is in permissive mode, this file may be created:

Quoted 6 lines
    /usr/lib/python3.7/site-packages/selinux/__pycache__/__init__.cpython-37.pyc

Prevent this file from being dynamically created by creating it when libselinux is installed, using "python -m compileall".

As far as I know, this not typically done by "setup.py install", and this is generally left to distributions.

In the context of Buildroot [1], we do the byte-compilation all at once at the very end of the build of all packages. Having individual packages do their own byte-compilation would be annoying.

If you would like to have this byte-compilation done by the SELinux Makefile, could you make it optional (even if you decide to enable it by default) ?

Thanks!

Thomas

Signature · 3 lines
Thomas Petazzoni, CTO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#5 lore raw
Quoted 14 lines · Thomas Petazzoni

Hello Nicolas,

Quoted 8 lines · On Sat, 2 Nov 2019 18:28:09 +0100 Nicolas Iooss <[email protected]> wrote:

When selinux module is imported from a Python script, the content of __init__.py is compiled into bytecode and the result is saved into a file if it is allowed. For example, when root runs with Python 3.7 a script that uses "import selinux" on a system where SELinux is in permissive mode, this file may be created:

    /usr/lib/python3.7/site-packages/selinux/__pycache__/__init__.cpython-37.pyc
Quoted 7 lines
Quoted 4 lines

Prevent this file from being dynamically created by creating it when libselinux is installed, using "python -m compileall".

As far as I know, this not typically done by "setup.py install", and this is generally left to distributions.

Hello, this is done when a parameter such as "--optimize=1" is given to "python setup.py install". Such a parameter is described in Arch Linux packaging guidelines
(https://wiki.archlinux.org/index.php/Python_package_guidelines#distutils).
Nevertheless I agree it seems to be left to distributions whether to bytecode-compile Python modules.

Quoted 3 lines

In the context of Buildroot [1], we do the byte-compilation all at once at the very end of the build of all packages. Having individual packages do their own byte-compilation would be annoying.

What is the reference you are using for [1]? Why would this be annoying? For example, do you use a non-standard way of generating the bytecode which is not compatible with the one that would be generated by the package itself?

Quoted 3 lines

If you would like to have this byte-compilation done by the SELinux Makefile, could you make it optional (even if you decide to enable it by default) ?

My main motivation behind this patch is to remove lines such as this one from Arch Linux's PKGBUILD:

/usr/bin/python3 -m compileall "${pkgdir}/$(/usr/bin/python3 -c 'import site; print(site.getsitepackages()[0])')"

(Example from https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=selinux-python&id=dd87f7ae1d636a02bf404ed527a27022ee5d2ab3)
As every Makefile installing Python scripts has everything that is
needed to build this command line, it is much simpler for "make
install" to perform the bytecode-compilation directly instead.
Nevertheless, as there is an interest to keep bytecode-compilation out
of the "make && make install" process, I will rework my patches to
make it optional (so that Arch Linux and maybe other distributions can
invoke it, but not Buildroot).

Thanks,
Nicolas

#6 lore raw
Quoted 25 lines · Nicolas Iooss
Quoted 14 lines · Thomas Petazzoni

Hello Nicolas,

Quoted 8 lines · On Sat, 2 Nov 2019 18:28:09 +0100 Nicolas Iooss <[email protected]> wrote:

When selinux module is imported from a Python script, the content of __init__.py is compiled into bytecode and the result is saved into a file if it is allowed. For example, when root runs with Python 3.7 a script that uses "import selinux" on a system where SELinux is in permissive mode, this file may be created:

    /usr/lib/python3.7/site-packages/selinux/__pycache__/__init__.cpython-37.pyc
Quoted 7 lines
Quoted 4 lines

Prevent this file from being dynamically created by creating it when libselinux is installed, using "python -m compileall".

As far as I know, this not typically done by "setup.py install", and this is generally left to distributions.

Quoted 6 lines

Hello, this is done when a parameter such as "--optimize=1" is given to "python setup.py install". Such a parameter is described in Arch Linux packaging guidelines
(https://wiki.archlinux.org/index.php/Python_package_guidelines#distutils).
Nevertheless I agree it seems to be left to distributions whether to bytecode-compile Python modules.

I have spent some time working on these patches, and it appears that the base assumption of "this not typically done by setup.py install" is not true on my system (Arch Linux with Python 3.7): setup.py *DOES* compile bytecode into .pyc files. What the distribution I'm using does when packaging is giving a parameter such as --optimize=1 in order to create .opt-1.pyc files too. Currently, python/sepolicy/Makefile already uses setup.py (cf.
https://github.com/SELinuxProject/selinux/blob/selinux-python-3.0-rc1/python/sepolicy/Makefile#L30),
so .pyc files are already generated in some Makefiles of the project.

Could you please confirm that "setup.py install" does not compile Python scripts into bytecode on your system? This test can be achieved for example by cloning https://github.com/SELinuxProject/selinux,
running "make -C python/sepolicy DESTDIR=/tmp/selinux install" and searching for .pyc files in /tmp/selinux.

If "setup.py install" produces .pyc files on every system, from my point of view there are several alternatives that can be considered:

1. Keep the current behavior by default and introduce a COMPILE_PY boolean variable in Makefiles that produces both .pyc and .opt-1.pyc files in Makefiles that do not use setup.py.
2. Generate .pyc files everywhere (what my patches do), and introduce a OPTIMIZE_PY boolean variable in Makefiles that is used to trigger the compilation to .opt-1.pyc files.
3. Mix 2 and 3 by introducing both COMPILE_PY and OPTIMIZE_PY.
4. Find a way for "setup.py install" not to produce such files in python/sepolicy/Makefile and do as 1 or 3.

(A 5th option would be to compile both .pyc and .opt-1.pyc files without introducing new variables, but this would go against what appears to be a legitimate user request)

My personal preference here would be either 1. or 2. Would one of these options suits Buildroot maintainers better?

Thanks,
Nicolas