From: Chris PeBenito <pebenito@ieee.org>
To: Richard Haines <richard_c_haines@btinternet.com>,
selinux-refpolicy@vger.kernel.org
Subject: Re: [PATCH] Ensure correct monolithic binary policy is loaded
Date: Thu, 17 Dec 2020 16:13:53 -0500 [thread overview]
Message-ID: <0adcf519-98aa-c6a1-c442-a2d15f1f4cc0@ieee.org> (raw)
In-Reply-To: <20201217164248.17960-1-richard_c_haines@btinternet.com>
On 12/17/20 11:42 AM, Richard Haines wrote:
> 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))
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.
> ########################################
> #
> # 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
> + @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
>
> ########################################
> #
>
--
Chris PeBenito
prev parent reply other threads:[~2020-12-17 21:14 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-17 16:42 [PATCH] Ensure correct monolithic binary policy is loaded Richard Haines
2020-12-17 21:13 ` Chris PeBenito [this message]
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=0adcf519-98aa-c6a1-c442-a2d15f1f4cc0@ieee.org \
--to=pebenito@ieee.org \
--cc=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).