SELinux-Refpolicy Archive mirror
 help / color / mirror / Atom feed
From: Richard Haines <richard_c_haines@btinternet.com>
To: selinux-refpolicy@vger.kernel.org
Cc: Richard Haines <richard_c_haines@btinternet.com>
Subject: [PATCH] Ensure correct monolithic binary policy is loaded
Date: Thu, 17 Dec 2020 16:42:48 +0000	[thread overview]
Message-ID: <20201217164248.17960-1-richard_c_haines@btinternet.com> (raw)

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.

Signed-off-by: Richard Haines <richard_c_haines@btinternet.com>
---
 Makefile         |  1 +
 Rules.monolithic | 31 ++++++++++++++++++++++++++++---
 2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/Makefile b/Makefile
index 6ba215f1..88a5e78f 100644
--- a/Makefile
+++ b/Makefile
@@ -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
 
diff --git a/Rules.monolithic b/Rules.monolithic
index a8ae98d1..01e445ca 100644
--- a/Rules.monolithic
+++ b/Rules.monolithic
@@ -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
 
 ########################################
 #
-- 
2.29.2


             reply	other threads:[~2020-12-17 16:43 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-17 16:42 Richard Haines [this message]
2020-12-17 21:13 ` [PATCH] Ensure correct monolithic binary policy is loaded Chris PeBenito

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201217164248.17960-1-richard_c_haines@btinternet.com \
    --to=richard_c_haines@btinternet.com \
    --cc=selinux-refpolicy@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).