Ensure correct monolithic binary policy is loaded
PATCH 1/1 Ensure correct monolithic binary policy is loaded
When building a monolithic policy with 'make load', the selinux_config(5) file 'SELINUXTYPE' entry determines what policy is loaded as load_policy(8) does not take a path value (it always loads the active system policy as defined by /etc/selinux/config).
Currently it is possible to load the wrong binary policy, for example if the Reference Policy source is located at:/etc/selinux/refpolicy
and the /etc/selinux/config file has the following entry:
SELINUXTYPE=targeted
Then the /etc/selinux/targeted/policy/policy.<ver> is loaded when 'make load' is executed.
Another example is that if the Reference Policy source is located at:/tmp/custom-rootfs/refpolicy
and the /etc/selinux/config file has the following entry:
SELINUXTYPE=refpolicy
Then the /etc/selinux/refpolicy/policy/policy.<ver> is loaded when 'make DESTDIR=/tmp/custom-rootfs load' is executed (not the /tmp/custom-rootfs/refpolicy/policy/policy.<ver> that the developer thought would be loaded).
Resolve these issues by using sestatus(8) to resolve the policy root, then checking the selinux_config(5) file for the appropriate SELINUXTYPE entry.
Remove the '@touch $(tmpdir)/load' line as the file is never referenced.
--- Makefile | 1 + Rules.monolithic | 31 ++++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-)
Makefile +1 -0
@@ -64,6 +64,7 @@ SEMOD_EXP ?= $(tc_usrbindir)/semodule_expand LOADPOLICY ?= $(tc_usrsbindir)/load_policy SEPOLGEN_IFGEN ?= $(tc_usrbindir)/sepolgen-ifgen SETFILES ?= $(tc_sbindir)/setfiles +SESTATUS ?= $(tc_sbindir)/sestatus XMLLINT ?= $(BINDIR)/xmllint SECHECK ?= $(BINDIR)/sechecker
Rules.monolithic +28 -3
@@ -42,6 +42,12 @@ vpath %.te $(all_layers) vpath %.if $(all_layers) vpath %.fc $(all_layers) +# load_policy(8) loads policy from <SELINUXDIR>/<SELINUXTYPE>/policy/policy.<ver> +# Therefore need to determine if policy to load is in the right place, +SELINUXDIR ?= $(strip $(shell $(SESTATUS) | $(AWK) '/^SELinux root directory:/{ print $$4 }')) +# and that <SELINUXDIR>/config contains the correct SELINUXTYPE entry. +SELINUXTYPE ?= $(strip $(shell $(AWK) -F= '/^SELINUXTYPE/{ print $$2 }' $(SELINUXDIR)/config)) + ######################################## # # default action: build policy locally
@@ -91,9 +97,28 @@ endif # Load the binary policy # reload $(tmpdir)/load: $(loadpath) $(fcpath) $(appfiles) - @echo "Loading $(NAME) $(loadpath)" - $(verbose) $(LOADPOLICY) -q $(loadpath) - @touch $(tmpdir)/load +ifneq ($(SELINUXTYPE),$(NAME)) + $(eval NO_LOAD := $(shell echo 1)) + @echo + @echo "Warning: Cannot load policy as $(SELINUXDIR)/config file contains:" + @echo -e "\tSELINUXTYPE=$(SELINUXTYPE)" + @echo "Edit $(SELINUXDIR)/config and set \"SELINUXTYPE=$(NAME)\"." + @echo +endif + +ifneq ($(topdir),$(SELINUXDIR)) + $(eval NO_LOAD := $(shell echo 1)) + @echo + @echo "Warning: Cannot load policy as policy root MUST be $(SELINUXDIR)/$(NAME)" + @echo +endif + + @if test -z $(NO_LOAD); then \ + echo "Loading $(NAME) $(loadpath)" ;\ + $(verbose) $(LOADPOLICY) -q $(loadpath) ;\ + else \ + echo "Resolve binary policy configuration" ;\ + fi ######################################## #
Signature · 1 line
2.29.2
Quoted 50 lines · Richard Haines
When building a monolithic policy with 'make load', the
selinux_config(5)file 'SELINUXTYPE' entry determines what policy is loaded asload_policy(8)does not take a path value (it always loads the active system policy as defined by/etc/selinux/config).Currently it is possible to load the wrong binary policy, for example if the Reference Policy source is located at:
/etc/selinux/refpolicy
and the/etc/selinux/configfile has the following entry:
SELINUXTYPE=targeted
Then the /etc/selinux/targeted/policy/policy.<ver> is loaded when 'make load' is executed.Another example is that if the Reference Policy source is located at:
/tmp/custom-rootfs/refpolicy
and the/etc/selinux/configfile has the following entry:
SELINUXTYPE=refpolicy
Then the /etc/selinux/refpolicy/policy/policy.<ver> is loaded when 'make DESTDIR=/tmp/custom-rootfs load' is executed (not the /tmp/custom-rootfs/refpolicy/policy/policy.<ver> that the developer thought would be loaded).Resolve these issues by using sestatus(8) to resolve the policy root, then checking the
selinux_config(5)file for the appropriate SELINUXTYPE entry.Remove the '@touch $(tmpdir)/load' line as the file is never referenced.
Signed-off-by Richard Haines <[email protected]>--- Makefile | 1 + Rules.monolithic | 31 ++++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-)Makefile +1
-0@@ -64,6 +64,7 @@ SEMOD_EXP ?= $(tc_usrbindir)/semodule_expand LOADPOLICY ?= $(tc_usrsbindir)/load_policy SEPOLGEN_IFGEN ?= $(tc_usrbindir)/sepolgen-ifgen SETFILES ?= $(tc_sbindir)/setfiles +SESTATUS ?= $(tc_sbindir)/sestatus XMLLINT ?= $(BINDIR)/xmllint SECHECK ?= $(BINDIR)/secheckerRules.monolithic +0
-0
Rules.monolithic vpath %.te $(all_layers) · line 49
Quoted 8 lines
Rules.monolithic +5
-0@@ -42,6 +42,12 @@ vpath %.te $(all_layers) vpath %.if $(all_layers) vpath %.fc $(all_layers) +# load_policy(8) loads policy from <SELINUXDIR>/<SELINUXTYPE>/policy/policy.<ver> +# Therefore need to determine if policy to load is in the right place, +SELINUXDIR ?= $(strip $(shell $(SESTATUS) | $(AWK) '/^SELinux root directory:/{ print $$4 }')) +# and that <SELINUXDIR>/config contains the correct SELINUXTYPE entry. +SELINUXTYPE ?= $(strip $(shell $(AWK) -F= '/^SELINUXTYPE/{ print $$2 }' $(SELINUXDIR)/config))
Rather than parsing sestatus output, I'd prefer do a tiny Python script that gets the policy path, e.g.:
python -c 'import selinux; print(selinux.selinux_binary_policy_path())'
See support/policyvers.py too.
Rules.monolithic endif · line 101
Quoted 12 lines
Rules.monolithic +2
-3######################################## # # default action: build policy locally@@ -91,9 +97,28 @@ endif # Load the binary policy # reload $(tmpdir)/load: $(loadpath) $(fcpath) $(appfiles) - @echo "Loading $(NAME) $(loadpath)" - $(verbose) $(LOADPOLICY) -q $(loadpath) - @touch $(tmpdir)/load +ifneq ($(SELINUXTYPE),$(NAME)) + $(eval NO_LOAD := $(shell echo 1))
I think it would be cleaner to use the $(error msg_text) function to terminate make rather than the NO_LOAD logic.
See https://www.gnu.org/software/make/manual/make.html#Make-Control-Functions
Quoted 24 lines
Rules.monolithic +20
-0+ @echo + @echo "Warning: Cannot load policy as $(SELINUXDIR)/config file contains:" + @echo -e "\tSELINUXTYPE=$(SELINUXTYPE)" + @echo "Edit $(SELINUXDIR)/config and set \"SELINUXTYPE=$(NAME)\"." + @echo +endif + +ifneq ($(topdir),$(SELINUXDIR)) + $(eval NO_LOAD := $(shell echo 1)) + @echo + @echo "Warning: Cannot load policy as policy root MUST be $(SELINUXDIR)/$(NAME)" + @echo +endif + + @if test -z $(NO_LOAD); then \ + echo "Loading $(NAME) $(loadpath)" ;\ + $(verbose) $(LOADPOLICY) -q $(loadpath) ;\ + else \ + echo "Resolve binary policy configuration" ;\ + fi ######################################## #
Signature · 1 line
Chris PeBenito