From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.3 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 756B6C4361B for ; Thu, 17 Dec 2020 21:14:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4097323A53 for ; Thu, 17 Dec 2020 21:14:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731334AbgLQVOi (ORCPT ); Thu, 17 Dec 2020 16:14:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42462 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730768AbgLQVOi (ORCPT ); Thu, 17 Dec 2020 16:14:38 -0500 Received: from mail-qk1-x736.google.com (mail-qk1-x736.google.com [IPv6:2607:f8b0:4864:20::736]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E97D5C061794 for ; Thu, 17 Dec 2020 13:13:57 -0800 (PST) Received: by mail-qk1-x736.google.com with SMTP id 143so96710qke.10 for ; Thu, 17 Dec 2020 13:13:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ieee.org; s=google; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=ympEn3hZZGmui4RMwYFmnqGMWvqyHdpO4KBJEBSOO7o=; b=ZHUYyH+sxCKflK0zA+SPYGU3Lr4zjpFKkrkeSsiPA/cBA/8gMjS/R/L1uGOFhhRt6v 1RUkeTRY/gfa5e7DQdVqHifwINfMchg7LFwcBHQupMG4BmOVAypKJQnM4jGSRFb0tDUO HXvFM6gv+4FTnNUkx/vmvMhQBvsqSnVgvX2NQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=ympEn3hZZGmui4RMwYFmnqGMWvqyHdpO4KBJEBSOO7o=; b=uUCZiGhBSShqe+8fL2X0bjZVP02izbuvHaPyals2Gd5EPPjSxyCEcgQE2IB1SYdXcS Dje4PXNXxYNGf1iyFbL1kGwGi1hryO1ELa4qDUjlShkwwRdP8MG5kTgXoMJI10XQKL5c ACgGpB6mO9VSBbqMLdQ7+Qci9HCy5RwWiBXC/BpELQVD6IQ7a52hPAJU7bPLMmTw3b+K hC3SCgOhNA0NhdXbKskMC+fDTaX3FqSdzKtpuuZJ1rQ6s5hkKXRxGK8EbYaO37OTy4J7 2mhZKzlvVDAJwlGpthc9y+kNDes9i1F8G/cgRwB41QFUPlQkntZZK513FIANmqQoeJuZ YISg== X-Gm-Message-State: AOAM530MVb5Rv3gsJMg8cguKj+d5iMkWeOFSvP/sKYxHF1zvTC+FDtqz g4+CqpEyRZsbmclmKNzLLgBVpK7033rtTw== X-Google-Smtp-Source: ABdhPJxfaxEbgNDC64g0mEcITh98MnOFi0d5/0SLbUq1QvurWglueEKP/KuVQoCUtEPeL3ufe6fHVQ== X-Received: by 2002:a37:470f:: with SMTP id u15mr1291088qka.37.1608239636797; Thu, 17 Dec 2020 13:13:56 -0800 (PST) Received: from fedora.pebenito.net (pool-96-234-173-17.bltmmd.fios.verizon.net. [96.234.173.17]) by smtp.gmail.com with ESMTPSA id h25sm4443146qkh.122.2020.12.17.13.13.55 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 17 Dec 2020 13:13:55 -0800 (PST) Subject: Re: [PATCH] Ensure correct monolithic binary policy is loaded To: Richard Haines , selinux-refpolicy@vger.kernel.org References: <20201217164248.17960-1-richard_c_haines@btinternet.com> From: Chris PeBenito Message-ID: <0adcf519-98aa-c6a1-c442-a2d15f1f4cc0@ieee.org> Date: Thu, 17 Dec 2020 16:13:53 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.5.0 MIME-Version: 1.0 In-Reply-To: <20201217164248.17960-1-richard_c_haines@btinternet.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: selinux-refpolicy@vger.kernel.org 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. 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. is loaded when > 'make DESTDIR=/tmp/custom-rootfs load' is executed (not the > /tmp/custom-rootfs/refpolicy/policy/policy. 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 > --- > 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 //policy/policy. > +# 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 /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