Skip to contentSkip to conversation

Ensure correct monolithic binary policy is loaded

PATCH Superseded Richard Haines 2 participants 7 messages Dec 18, 2020 – Dec 20, 2020 Kbuild

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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/etc/selinux/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/etc/selinux/refpolicy/policy/policy.<ver> that the developer thought would be loaded).

Resolve these issues by using selinux_path(3) 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.

---
V2 Changes: Use $(error .. instead of NO_LOAD logic. Use python script to find selinux path not sestatus. Reword error messages.

 Makefile                |  1 +
 Rules.monolithic        | 15 ++++++++++++++-
 support/selinux_path.py | 13 +++++++++++++
 3 files changed, 28 insertions(+), 1 deletion(-)
 create mode 100644 support/selinux_path.py
Makefile +1 -0
index 6ba215f1..e49d43d0 100644
@@ -97,6 +97,7 @@ genxml := $(PYTHON) $(support)/segenxml.py
 gendoc := $(PYTHON) $(support)/sedoctool.py
 genperm := $(PYTHON) $(support)/genclassperms.py
 policyvers := $(PYTHON) $(support)/policyvers.py
+selinux_path := $(PYTHON) $(support)/selinux_path.py
 fcsort := $(PYTHON) $(support)/fc_sort.py
 setbools := $(AWK) -f $(support)/set_bools_tuns.awk
 get_type_attr_decl := $(SED) -r -f $(support)/get_type_attr_decl.sed
Rules.monolithic +14 -1
index a8ae98d1..cd065362 100644
@@ -42,6 +42,12 @@ vpath %.te $(all_layers)
 vpath %.if $(all_layers)
 vpath %.fc $(all_layers)
 
+# load_policy(8) loads policy from <SELINUX_PATH>/<SELINUXTYPE>/policy/policy.<ver>
+# It does this by reading the <SELINUX_PATH>/config file and using the
+# SELINUX_PATH/SELINUXTYPE entries to form the initial path.
+SELINUX_PATH := $(shell $(selinux_path))
+SELINUXTYPE := $(strip $(shell $(AWK) -F= '/^SELINUXTYPE/{ print $$2 }' $(SELINUX_PATH)/config))
+
 ########################################
 #
 # default action: build policy locally
@@ -91,9 +97,16 @@ endif
 # Load the binary policy
 #
 reload $(tmpdir)/load: $(loadpath) $(fcpath) $(appfiles)
+ifneq ($(SELINUXTYPE),$(NAME))
+	$(error Cannot load policy as $(SELINUX_PATH)/config file contains SELINUXTYPE=$(SELINUXTYPE) - \
+		Edit $(SELINUX_PATH)/config and set "SELINUXTYPE=$(NAME)")
+endif
+ifneq ($(topdir),$(SELINUX_PATH))
+	$(error Cannot load policy as policy root MUST be $(SELINUX_PATH)/$(NAME) - \
+		Current policy root is: $(topdir)/$(NAME))
+endif
 	@echo "Loading $(NAME) $(loadpath)"
 	$(verbose) $(LOADPOLICY) -q $(loadpath)
-	@touch $(tmpdir)/load
 
 ########################################
 #
support/selinux_path.py new file +13 -0
new file mode 100644
index 00000000..b663ff09
@@ -0,0 +1,13 @@
+#!/usr/bin/env python3
+
+try:
+    import warnings
+    with warnings.catch_warnings():
+        warnings.filterwarnings("ignore", category=PendingDeprecationWarning)
+        import selinux
+
+    if selinux.is_selinux_enabled():
+        # Strip the trailing '/'
+        print(selinux.selinux_path()[:-1])
+except ImportError:
+    exit(0)
Signature · 1 line
2.29.2
#1 lore raw
Quoted 93 lines · Richard Haines

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/etc/selinux/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/etc/selinux/refpolicy/policy/policy.<ver> that the developer thought would be loaded).

Resolve these issues by using selinux_path(3) 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.

---
V2 Changes: Use $(error .. instead of NO_LOAD logic. Use python script to find selinux path not sestatus. Reword error messages.

  Makefile                |  1 +
  Rules.monolithic        | 15 ++++++++++++++-
  support/selinux_path.py | 13 +++++++++++++
  3 files changed, 28 insertions(+), 1 deletion(-)

create mode 100644 support/selinux_path.py

Makefile +1 -0
index 6ba215f1..e49d43d0 100644
@@ -97,6 +97,7 @@ genxml := $(PYTHON) $(support)/segenxml.py
  gendoc := $(PYTHON) $(support)/sedoctool.py
  genperm := $(PYTHON) $(support)/genclassperms.py
  policyvers := $(PYTHON) $(support)/policyvers.py
+selinux_path := $(PYTHON) $(support)/selinux_path.py
  fcsort := $(PYTHON) $(support)/fc_sort.py
  setbools := $(AWK) -f $(support)/set_bools_tuns.awk
  get_type_attr_decl := $(SED) -r -f $(support)/get_type_attr_decl.sed
Rules.monolithic +14 -1
index a8ae98d1..cd065362 100644
@@ -42,6 +42,12 @@ vpath %.te $(all_layers)
  vpath %.if $(all_layers)
  vpath %.fc $(all_layers)
 
+# load_policy(8) loads policy from <SELINUX_PATH>/<SELINUXTYPE>/policy/policy.<ver>
+# It does this by reading the <SELINUX_PATH>/config file and using the
+# SELINUX_PATH/SELINUXTYPE entries to form the initial path.
+SELINUX_PATH := $(shell $(selinux_path))
+SELINUXTYPE := $(strip $(shell $(AWK) -F= '/^SELINUXTYPE/{ print $$2 }' $(SELINUX_PATH)/config))
+
  ########################################
  #
  # default action: build policy locally
@@ -91,9 +97,16 @@ endif
  # Load the binary policy
  #
  reload $(tmpdir)/load: $(loadpath) $(fcpath) $(appfiles)
+ifneq ($(SELINUXTYPE),$(NAME))
+	$(error Cannot load policy as $(SELINUX_PATH)/config file contains SELINUXTYPE=$(SELINUXTYPE) - \
+		Edit $(SELINUX_PATH)/config and set "SELINUXTYPE=$(NAME)")
+endif
+ifneq ($(topdir),$(SELINUX_PATH))
+	$(error Cannot load policy as policy root MUST be $(SELINUX_PATH)/$(NAME) - \
+		Current policy root is: $(topdir)/$(NAME))
+endif
  	@echo "Loading $(NAME) $(loadpath)"
  	$(verbose) $(LOADPOLICY) -q $(loadpath)
-	@touch $(tmpdir)/load
 
  ########################################
  #
support/selinux_path.py new file +1 -0
new file mode 100644
index 00000000..b663ff09
@@ -0,0 +1,13 @@
+#!/usr/bin/env python3

support/selinux_path.py line 11

Quoted 10 lines
support/selinux_path.py new file +10 -0
+
+try:
+    import warnings
+    with warnings.catch_warnings():
+        warnings.filterwarnings("ignore", category=PendingDeprecationWarning)
+        import selinux
+
+    if selinux.is_selinux_enabled():
+        # Strip the trailing '/'
+        print(selinux.selinux_path()[:-1])

Why not use selinux.selinux_binary_policy_path()? Then you don't need to parse for SELINUXTYPE above.

Signature · 1 line
Chris PeBenito
#2 lore raw
Quoted 116 lines · Chris PeBenito
Quoted 115 lines · Richard Haines

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/etc/selinux/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/etc/selinux/refpolicy/policy/policy.<ver> that the
developer thought would be loaded).

Resolve these issues by using selinux_path(3) 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.

---
V2 Changes: Use $(error .. instead of NO_LOAD logic. Use python script to
find selinux path not sestatus. Reword error messages.

  Makefile                |  1 +
  Rules.monolithic        | 15 ++++++++++++++-
  support/selinux_path.py | 13 +++++++++++++

  3 files changed, 28 insertions(+), 1 deletion(-)

  create mode 100644 support/selinux_path.py

Makefile +0 -0
index 6ba215f1..e49d43d0 100644
@@ -97,6 +97,7 @@ genxml := $(PYTHON) $(support)/segenxml.py

  gendoc := $(PYTHON) $(support)/sedoctool.py genperm := $(PYTHON) $(support)/genclassperms.py policyvers := $(PYTHON) $(support)/policyvers.py +selinux_path := $(PYTHON) $(support)/selinux_path.py fcsort := $(PYTHON) $(support)/fc_sort.py setbools := $(AWK) -f $(support)/set_bools_tuns.awk get_type_attr_decl := $(SED) -r -f $(support)/get_type_attr_decl.sed

Rules.monolithic +0 -0
index a8ae98d1..cd065362 100644
@@ -42,6 +42,12 @@ vpath %.te $(all_layers)
   vpath %.if $(all_layers)
   vpath %.fc $(all_layers)


+# load_policy(8) loads policy from <SELINUX_PATH>/<SELINUXTYPE>/policy/policy.<ver>
+# It does this by reading the <SELINUX_PATH>/config file and using the

Rules.monolithic +3 -0
+# SELINUX_PATH/SELINUXTYPE entries to form the initial path.
+SELINUX_PATH := $(shell $(selinux_path))
+SELINUXTYPE := $(strip $(shell $(AWK) -F= '/^SELINUXTYPE/{ print

$$2 }' $(SELINUX_PATH)/config))
+
  ########################################
  #

  # default action: build policy locally
Rules.monolithic +8 -0
@@ -91,9 +97,16 @@ endif
   # Load the binary policy
   #
   reload $(tmpdir)/load: $(loadpath) $(fcpath) $(appfiles)
+ifneq ($(SELINUXTYPE),$(NAME))
+       $(error Cannot load policy as $(SELINUX_PATH)/config file
 contains SELINUXTYPE=$(SELINUXTYPE) - \
+               Edit $(SELINUX_PATH)/config and set
 "SELINUXTYPE=$(NAME)")
+endif
+ifneq ($(topdir),$(SELINUX_PATH))
+       $(error Cannot load policy as policy root MUST be
 $(SELINUX_PATH)/$(NAME) - \
+               Current policy root is: $(topdir)/$(NAME))
+endif

        @echo "Loading $(NAME) $(loadpath)"
        $(verbose) $(LOADPOLICY) -q $(loadpath)
-       @touch $(tmpdir)/load

  ########################################
  #

support/selinux_path.py new file +6 -0
new file mode 100644
index 00000000..b663ff09
@@ -0,0 +1,13 @@
+#!/usr/bin/env python3
+
+try:
+    import warnings
+    with warnings.catch_warnings():
+        warnings.filterwarnings("ignore",

category=PendingDeprecationWarning)
+        import selinux

Quoted 8 lines
Quoted 4 lines

+
+    if selinux.is_selinux_enabled():
+        # Strip the trailing '/'
+        print(selinux.selinux_path()[:-1])

Why not use selinux.selinux_binary_policy_path()? Then you don't need to parse
for SELINUXTYPE above.

Because it has more information than needed. How about using selinux.selinux_policy_root() to give:

# load_policy(8) loads policy from <POLICY_ROOT>/policy/policy.<ver> # It does this by reading the <SELINUX_PATH>/config file and using the # SELINUX_PATH/SELINUXTYPE entry to form the <POLICY_ROOT>. POLICY_ROOT := $(shell $(selinux_policy_root))
SELINUXTYPE := $(shell basename $(POLICY_ROOT)) SELINUX_PATH := $(shell dirname $(POLICY_ROOT))

Quoted 2 lines
#3 lore raw
Quoted 129 lines · Richard Haines
Quoted 116 lines · Chris PeBenito
Quoted 115 lines · Richard Haines

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/etc/selinux/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/etc/selinux/refpolicy/policy/policy.<ver> that the
developer thought would be loaded).

Resolve these issues by using selinux_path(3) 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.

---
V2 Changes: Use $(error .. instead of NO_LOAD logic. Use python script to
find selinux path not sestatus. Reword error messages.

   Makefile                |  1 +
   Rules.monolithic        | 15 ++++++++++++++-
   support/selinux_path.py | 13 +++++++++++++
   3 files changed, 28 insertions(+), 1 deletion(-)

  create mode 100644 support/selinux_path.py

Makefile +1 -0
index 6ba215f1..e49d43d0 100644
@@ -97,6 +97,7 @@ genxml := $(PYTHON) $(support)/segenxml.py
   gendoc := $(PYTHON) $(support)/sedoctool.py
   genperm := $(PYTHON) $(support)/genclassperms.py
   policyvers := $(PYTHON) $(support)/policyvers.py
+selinux_path := $(PYTHON) $(support)/selinux_path.py
   fcsort := $(PYTHON) $(support)/fc_sort.py
   setbools := $(AWK) -f $(support)/set_bools_tuns.awk
   get_type_attr_decl := $(SED) -r -f

$(support)/get_type_attr_decl.sed

Rules.monolithic +1 -0
index a8ae98d1..cd065362 100644
@@ -42,6 +42,12 @@ vpath %.te $(all_layers)
   vpath %.if $(all_layers)
   vpath %.fc $(all_layers)
 
+# load_policy(8) loads policy from

<SELINUX_PATH>/<SELINUXTYPE>/policy/policy.<ver>
+# It does this by reading the <SELINUX_PATH>/config file and using the

Rules.monolithic +3 -0
+# SELINUX_PATH/SELINUXTYPE entries to form the initial path.
+SELINUX_PATH := $(shell $(selinux_path))
+SELINUXTYPE := $(strip $(shell $(AWK) -F= '/^SELINUXTYPE/{ print

$$2 }' $(SELINUX_PATH)/config))

Rules.monolithic +9 -1
+
   ########################################
   #
   # default action: build policy locally
@@ -91,9 +97,16 @@ endif
   # Load the binary policy
   #
   reload $(tmpdir)/load: $(loadpath) $(fcpath) $(appfiles)
+ifneq ($(SELINUXTYPE),$(NAME))
+       $(error Cannot load policy as $(SELINUX_PATH)/config file
 contains SELINUXTYPE=$(SELINUXTYPE) - \
+               Edit $(SELINUX_PATH)/config and set
 "SELINUXTYPE=$(NAME)")
+endif
+ifneq ($(topdir),$(SELINUX_PATH))
+       $(error Cannot load policy as policy root MUST be
 $(SELINUX_PATH)/$(NAME) - \
+               Current policy root is: $(topdir)/$(NAME))
+endif
         @echo "Loading $(NAME) $(loadpath)"
         $(verbose) $(LOADPOLICY) -q $(loadpath)
-       @touch $(tmpdir)/load

  ########################################
  #

support/selinux_path.py new file +6 -0
new file mode 100644
index 00000000..b663ff09
@@ -0,0 +1,13 @@
+#!/usr/bin/env python3
+
+try:
+    import warnings
+    with warnings.catch_warnings():
+        warnings.filterwarnings("ignore",

category=PendingDeprecationWarning)
+        import selinux

Quoted 8 lines
Quoted 4 lines

+
+    if selinux.is_selinux_enabled():
+        # Strip the trailing '/'
+        print(selinux.selinux_path()[:-1])

Why not use selinux.selinux_binary_policy_path()? Then you don't need to parse
for SELINUXTYPE above.

Because it has more information than needed. How about using selinux.selinux_policy_root() to give:

Quoted 6 lines

# load_policy(8) loads policy from <POLICY_ROOT>/policy/policy.<ver> # It does this by reading the <SELINUX_PATH>/config file and using the # SELINUX_PATH/SELINUXTYPE entry to form the <POLICY_ROOT>. POLICY_ROOT := $(shell $(selinux_policy_root))
SELINUXTYPE := $(shell basename $(POLICY_ROOT)) SELINUX_PATH := $(shell dirname $(POLICY_ROOT))

Ok, that works, though make has builtin versions of basename and dirname: https://www.gnu.org/software/make/manual/html_node/File-Name-Functions.html

Signature · 1 line
Chris PeBenito
#4 lore raw
In reply to Richard Haines
Quoted 129 lines · Richard Haines
Quoted 116 lines · Chris PeBenito
Quoted 115 lines · Richard Haines

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/etc/selinux/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/etc/selinux/refpolicy/policy/policy.<ver> that the
developer thought would be loaded).

Resolve these issues by using selinux_path(3) 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.

---
V2 Changes: Use $(error .. instead of NO_LOAD logic. Use python script to
find selinux path not sestatus. Reword error messages.

   Makefile                |  1 +
   Rules.monolithic        | 15 ++++++++++++++-
   support/selinux_path.py | 13 +++++++++++++
   3 files changed, 28 insertions(+), 1 deletion(-)

  create mode 100644 support/selinux_path.py

Makefile +1 -0
index 6ba215f1..e49d43d0 100644
@@ -97,6 +97,7 @@ genxml := $(PYTHON) $(support)/segenxml.py
   gendoc := $(PYTHON) $(support)/sedoctool.py
   genperm := $(PYTHON) $(support)/genclassperms.py
   policyvers := $(PYTHON) $(support)/policyvers.py
+selinux_path := $(PYTHON) $(support)/selinux_path.py
   fcsort := $(PYTHON) $(support)/fc_sort.py
   setbools := $(AWK) -f $(support)/set_bools_tuns.awk
   get_type_attr_decl := $(SED) -r -f

$(support)/get_type_attr_decl.sed

Rules.monolithic +1 -0
index a8ae98d1..cd065362 100644
@@ -42,6 +42,12 @@ vpath %.te $(all_layers)
   vpath %.if $(all_layers)
   vpath %.fc $(all_layers)
 
+# load_policy(8) loads policy from

<SELINUX_PATH>/<SELINUXTYPE>/policy/policy.<ver>
+# It does this by reading the <SELINUX_PATH>/config file and using the

Rules.monolithic +3 -0
+# SELINUX_PATH/SELINUXTYPE entries to form the initial path.
+SELINUX_PATH := $(shell $(selinux_path))
+SELINUXTYPE := $(strip $(shell $(AWK) -F= '/^SELINUXTYPE/{ print

$$2 }' $(SELINUX_PATH)/config))

Rules.monolithic +9 -1
+
   ########################################
   #
   # default action: build policy locally
@@ -91,9 +97,16 @@ endif
   # Load the binary policy
   #
   reload $(tmpdir)/load: $(loadpath) $(fcpath) $(appfiles)
+ifneq ($(SELINUXTYPE),$(NAME))
+       $(error Cannot load policy as $(SELINUX_PATH)/config file
 contains SELINUXTYPE=$(SELINUXTYPE) - \
+               Edit $(SELINUX_PATH)/config and set
 "SELINUXTYPE=$(NAME)")
+endif
+ifneq ($(topdir),$(SELINUX_PATH))
+       $(error Cannot load policy as policy root MUST be
 $(SELINUX_PATH)/$(NAME) - \
+               Current policy root is: $(topdir)/$(NAME))
+endif
         @echo "Loading $(NAME) $(loadpath)"
         $(verbose) $(LOADPOLICY) -q $(loadpath)
-       @touch $(tmpdir)/load

  ########################################
  #

support/selinux_path.py new file +6 -0
new file mode 100644
index 00000000..b663ff09
@@ -0,0 +1,13 @@
+#!/usr/bin/env python3
+
+try:
+    import warnings
+    with warnings.catch_warnings():
+        warnings.filterwarnings("ignore",

category=PendingDeprecationWarning)
+        import selinux

Quoted 8 lines
Quoted 4 lines

+
+    if selinux.is_selinux_enabled():
+        # Strip the trailing '/'
+        print(selinux.selinux_path()[:-1])

Why not use selinux.selinux_binary_policy_path()? Then you don't need to parse
for SELINUXTYPE above.

Because it has more information than needed. How about using selinux.selinux_policy_root() to give:

Quoted 6 lines

# load_policy(8) loads policy from <POLICY_ROOT>/policy/policy.<ver> # It does this by reading the <SELINUX_PATH>/config file and using the # SELINUX_PATH/SELINUXTYPE entry to form the <POLICY_ROOT>. POLICY_ROOT := $(shell $(selinux_policy_root))
SELINUXTYPE := $(shell basename $(POLICY_ROOT)) SELINUX_PATH := $(shell dirname $(POLICY_ROOT))

On second thought, isn't another way of doing the check:

selinux.selinux_binary_policy_path() + "." + POLICYVER == $(loadpath)?

Signature · 1 line
Chris PeBenito
#5 lore raw
Quoted 150 lines · Chris PeBenito
Quoted 140 lines · Richard Haines
Quoted 127 lines · Chris PeBenito
Quoted 126 lines · Richard Haines

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/etc/selinux/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/etc/selinux/refpolicy/policy/policy.<ver> that
the
developer thought would be loaded).

Resolve these issues by using selinux_path(3) 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.

---
V2 Changes: Use $(error .. instead of NO_LOAD logic. Use python script to
find selinux path not sestatus. Reword error messages.

   Makefile                |  1 +
   Rules.monolithic        | 15 ++++++++++++++-
   support/selinux_path.py | 13 +++++++++++++

   3 files changed, 28 insertions(+), 1 deletion(-)

   create mode 100644 support/selinux_path.py

Makefile +0 -0
index 6ba215f1..e49d43d0 100644
@@ -97,6 +97,7 @@ genxml := $(PYTHON) $(support)/segenxml.py

   gendoc := $(PYTHON) $(support)/sedoctool.py genperm := $(PYTHON) $(support)/genclassperms.py policyvers := $(PYTHON) $(support)/policyvers.py +selinux_path := $(PYTHON) $(support)/selinux_path.py fcsort := $(PYTHON) $(support)/fc_sort.py setbools := $(AWK) -f $(support)/set_bools_tuns.awk get_type_attr_decl := $(SED) -r -f $(support)/get_type_attr_decl.sed

Rules.monolithic +0 -0
index a8ae98d1..cd065362 100644
@@ -42,6 +42,12 @@ vpath %.te $(all_layers)
    vpath %.if $(all_layers)
    vpath %.fc $(all_layers)


+# load_policy(8) loads policy from <SELINUX_PATH>/<SELINUXTYPE>/policy/policy.<ver>
+# It does this by reading the <SELINUX_PATH>/config file and using
the

Rules.monolithic +3 -0
+# SELINUX_PATH/SELINUXTYPE entries to form the initial path.
+SELINUX_PATH := $(shell $(selinux_path))
+SELINUXTYPE := $(strip $(shell $(AWK) -F= '/^SELINUXTYPE/{

print
$$2 }' $(SELINUX_PATH)/config))
+
   ########################################
   #

   # default action: build policy locally
Rules.monolithic +2 -0
@@ -91,9 +97,16 @@ endif
    # Load the binary policy
    #
    reload $(tmpdir)/load: $(loadpath) $(fcpath) $(appfiles)
+ifneq ($(SELINUXTYPE),$(NAME))
+       $(error Cannot load policy as $(SELINUX_PATH)/config

file
contains SELINUXTYPE=$(SELINUXTYPE) - \
+               Edit $(SELINUX_PATH)/config and set "SELINUXTYPE=$(NAME)")

Rules.monolithic +3 -0
+endif
+ifneq ($(topdir),$(SELINUX_PATH))
+       $(error Cannot load policy as policy root MUST be

$(SELINUX_PATH)/$(NAME) - \
+               Current policy root is: $(topdir)/$(NAME)) +endif
         @echo "Loading $(NAME) $(loadpath)" $(verbose) $(LOADPOLICY) -q $(loadpath) -       @touch $(tmpdir)/load

   ########################################
   #

support/selinux_path.py new file +6 -0
new file mode 100644
index 00000000..b663ff09
@@ -0,0 +1,13 @@
+#!/usr/bin/env python3
+
+try:
+    import warnings
+    with warnings.catch_warnings():
+        warnings.filterwarnings("ignore",

category=PendingDeprecationWarning)
+        import selinux
+

Quoted 8 lines
Quoted 3 lines

+    if selinux.is_selinux_enabled():
+        # Strip the trailing '/'
+        print(selinux.selinux_path()[:-1])

Why not use selinux.selinux_binary_policy_path()? Then you don't need
to parse
for SELINUXTYPE above.

Because it has more information than needed. How about using selinux.selinux_policy_root() to give:

Quoted 8 lines

# load_policy(8) loads policy from <POLICY_ROOT>/policy/policy.<ver>
# It does this by reading the <SELINUX_PATH>/config file and using the
# SELINUX_PATH/SELINUXTYPE entry to form the <POLICY_ROOT>. POLICY_ROOT := $(shell $(selinux_policy_root)) SELINUXTYPE := $(shell basename $(POLICY_ROOT)) SELINUX_PATH := $(shell dirname $(POLICY_ROOT))

Quoted 4 lines

On second thought, isn't another way of doing the check:

selinux.selinux_binary_policy_path() + "." + POLICYVER == $(loadpath)?

Yes I could, however that accounts for only one of the checks:
Cannot load policy as policy root MUST be ...

The other check uses SELINUXTYPE against $NAME to see if the config file has the correct entry:
Cannot load policy as $(SELINUX_PATH)/config file ...

I could just use one test and say that the policy must be based at /etc/selinux/$(NAME) plus the /etc/selinux/config file SELINUXTYPE entry must be $(NAME) ???

Quoted 3 lines
#6 lore raw
Quoted 159 lines · Richard Haines
Quoted 150 lines · Chris PeBenito
Quoted 140 lines · Richard Haines
Quoted 127 lines · Chris PeBenito
Quoted 126 lines · Richard Haines

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/etc/selinux/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/etc/selinux/refpolicy/policy/policy.<ver> that
the
developer thought would be loaded).

Resolve these issues by using selinux_path(3) 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.

---
V2 Changes: Use $(error .. instead of NO_LOAD logic. Use python script to
find selinux path not sestatus. Reword error messages.

    Makefile                |  1 +
    Rules.monolithic        | 15 ++++++++++++++-
    support/selinux_path.py | 13 +++++++++++++
    3 files changed, 28 insertions(+), 1 deletion(-)

   create mode 100644 support/selinux_path.py

Makefile +1 -0
index 6ba215f1..e49d43d0 100644
@@ -97,6 +97,7 @@ genxml := $(PYTHON) $(support)/segenxml.py
    gendoc := $(PYTHON) $(support)/sedoctool.py
    genperm := $(PYTHON) $(support)/genclassperms.py
    policyvers := $(PYTHON) $(support)/policyvers.py
+selinux_path := $(PYTHON) $(support)/selinux_path.py
    fcsort := $(PYTHON) $(support)/fc_sort.py
    setbools := $(AWK) -f $(support)/set_bools_tuns.awk
    get_type_attr_decl := $(SED) -r -f

$(support)/get_type_attr_decl.sed

Rules.monolithic +1 -0
index a8ae98d1..cd065362 100644
@@ -42,6 +42,12 @@ vpath %.te $(all_layers)
    vpath %.if $(all_layers)
    vpath %.fc $(all_layers)
 
+# load_policy(8) loads policy from

<SELINUX_PATH>/<SELINUXTYPE>/policy/policy.<ver>
+# It does this by reading the <SELINUX_PATH>/config file and using
the

Rules.monolithic +3 -0
+# SELINUX_PATH/SELINUXTYPE entries to form the initial path.
+SELINUX_PATH := $(shell $(selinux_path))
+SELINUXTYPE := $(strip $(shell $(AWK) -F= '/^SELINUXTYPE/{

print
$$2 }' $(SELINUX_PATH)/config))

Rules.monolithic +3 -0
+
    ########################################
    #
    # default action: build policy locally
@@ -91,9 +97,16 @@ endif
    # Load the binary policy
    #
    reload $(tmpdir)/load: $(loadpath) $(fcpath) $(appfiles)
+ifneq ($(SELINUXTYPE),$(NAME))
+       $(error Cannot load policy as $(SELINUX_PATH)/config

file
contains SELINUXTYPE=$(SELINUXTYPE) - \
+               Edit $(SELINUX_PATH)/config and set "SELINUXTYPE=$(NAME)")

Rules.monolithic +3 -0
+endif
+ifneq ($(topdir),$(SELINUX_PATH))
+       $(error Cannot load policy as policy root MUST be

$(SELINUX_PATH)/$(NAME) - \
+               Current policy root is: $(topdir)/$(NAME))

Rules.monolithic +1 -1
+endif
          @echo "Loading $(NAME) $(loadpath)"
          $(verbose) $(LOADPOLICY) -q $(loadpath)
-       @touch $(tmpdir)/load
 
    ########################################
    #
support/selinux_path.py new file +6 -0
new file mode 100644
index 00000000..b663ff09
@@ -0,0 +1,13 @@
+#!/usr/bin/env python3
+
+try:
+    import warnings
+    with warnings.catch_warnings():
+        warnings.filterwarnings("ignore",

category=PendingDeprecationWarning)
+        import selinux
+

Quoted 8 lines
Quoted 3 lines

+    if selinux.is_selinux_enabled():
+        # Strip the trailing '/'
+        print(selinux.selinux_path()[:-1])

Why not use selinux.selinux_binary_policy_path()? Then you don't need
to parse
for SELINUXTYPE above.

Because it has more information than needed. How about using selinux.selinux_policy_root() to give:

Quoted 8 lines

# load_policy(8) loads policy from <POLICY_ROOT>/policy/policy.<ver>
# It does this by reading the <SELINUX_PATH>/config file and using the
# SELINUX_PATH/SELINUXTYPE entry to form the <POLICY_ROOT>. POLICY_ROOT := $(shell $(selinux_policy_root)) SELINUXTYPE := $(shell basename $(POLICY_ROOT)) SELINUX_PATH := $(shell dirname $(POLICY_ROOT))

Quoted 4 lines

On second thought, isn't another way of doing the check:

selinux.selinux_binary_policy_path() + "." + POLICYVER == $(loadpath)?

Yes I could, however that accounts for only one of the checks:
Cannot load policy as policy root MUST be ...

Quoted 7 lines

The other check uses SELINUXTYPE against $NAME to see if the config file has the correct entry:

    Cannot load policy as $(SELINUX_PATH)/config file ...

I could just use one test and say that the policy must be based at /etc/selinux/$(NAME) plus the /etc/selinux/config file SELINUXTYPE entry must be $(NAME) ???

Since the Makefile creates $(loadpath) to be $(topdir)/$NAME/policy/policy.$ver, and selinux.selinux_binary_policy_path() takes into account SELINUXTYPE, the only reason the $topdir ($DESTDIR/etc/selinux), would be a problem is if DESTDIR is set. We should instead fail the load if DESTDIR is set. If the user alters the $(topdir) of the Makefile to change /etc/selinux into something else, that is undefined behavior since lowercase variables aren't meant to be altered by users. I see no reason to check that case.

Signature · 1 line
Chris PeBenito