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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS 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 CD953C4361B for ; Sun, 20 Dec 2020 12:32:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 871CF22B2A for ; Sun, 20 Dec 2020 12:32:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727251AbgLTMcY (ORCPT ); Sun, 20 Dec 2020 07:32:24 -0500 Received: from mailomta5-sa.btinternet.com ([213.120.69.11]:64408 "EHLO sa-prd-fep-048.btinternet.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727131AbgLTMcX (ORCPT ); Sun, 20 Dec 2020 07:32:23 -0500 Received: from sa-prd-rgout-002.btmx-prd.synchronoss.net ([10.2.38.5]) by sa-prd-fep-048.btinternet.com with ESMTP id <20201220123138.TDPP7754.sa-prd-fep-048.btinternet.com@sa-prd-rgout-002.btmx-prd.synchronoss.net>; Sun, 20 Dec 2020 12:31:38 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=btinternet.com; s=btmx201904; t=1608467498; bh=7KMv7BU2g+ibn28haiX/8ZTNPGWxIb3qEa+xHfRt7LI=; h=Message-ID:Subject:From:To:Date:In-Reply-To:References:MIME-Version; b=rci5SZyApsrbUxr9xAxkBItNh5pBdXxU35a1/2OpKKX6v7Lo4MUV8Ym6PtM2/8jJ6+K5pOAgrSuRD/RTIFS1QTag1KM5WVQmSMSTCEg0alhjdMkgaPpV7YLk0FluFHFrwrpTBqhDAvAWM4AJzDQ6c43Q4CDJNlY8oekGmM9+/Vp3xdOHZ3sn1uVG0G5l5nK0b7CDqdOvNoNuefjvZ6XGIxsMdm31q88DHV3TRvPhqSxIUGnJNCvHLpvRaYpBQTkm5wZji1e4kfPd0yldkQM9k+lJi49eNriw1PJ1AwCWYwd+VPKwtYNWA3O4EjA2fv0d/kbLUsZGMeKW7zmuH/J2Kg== Authentication-Results: btinternet.com; auth=pass (LOGIN) smtp.auth=richard_c_haines@btinternet.com X-SNCR-Rigid: 5ED9AA6E1F791703 X-Originating-IP: [86.183.114.93] X-OWM-Source-IP: 86.183.114.93 (GB) X-OWM-Env-Sender: richard_c_haines@btinternet.com X-VadeSecure-score: verdict=clean score=0/300, class=clean X-RazorGate-Vade: gggruggvucftvghtrhhoucdtuddrgedujedrvddttddggeegucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuueftkffvkffujffvgffngfevqffopdfqfgfvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefkuffhvfffjghftggfggfgsehtkeertddtreejnecuhfhrohhmpeftihgthhgrrhguucfjrghinhgvshcuoehrihgthhgrrhgupggtpghhrghinhgvshessghtihhnthgvrhhnvghtrdgtohhmqeenucggtffrrghtthgvrhhnpefhkeegheduudeggfffkeehheettefgjedugefhhfevuedvveduhedtleejkeduveenucfkphepkeeirddukeefrdduudegrdelfeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhephhgvlhhopegludelvddrudeikedruddrudelkegnpdhinhgvthepkeeirddukeefrdduudegrdelfedpmhgrihhlfhhrohhmpeeorhhitghhrghruggptggphhgrihhnvghssegsthhinhhtvghrnhgvthdrtghomhequceuqfffjgepkeeukffvoffkoffgpdhrtghpthhtohepoehpvggsvghnihhtohesihgvvggvrdhorhhgqedprhgtphhtthhopeeoshgvlhhinhhugidqrhgvfhhpohhlihgthiesvhhgvghrrdhkvghrnhgvlhdrohhrgheq X-RazorGate-Vade-Verdict: clean 0 X-RazorGate-Vade-Classification: clean X-SNCR-hdrdom: btinternet.com Received: from [192.168.1.198] (86.183.114.93) by sa-prd-rgout-002.btmx-prd.synchronoss.net (5.8.340) (authenticated as richard_c_haines@btinternet.com) id 5ED9AA6E1F791703; Sun, 20 Dec 2020 12:31:38 +0000 Message-ID: <5877289e96fc4fc8c4c4560ae1ea77a1ee91112c.camel@btinternet.com> Subject: Re: [PATCH V2] Ensure correct monolithic binary policy is loaded From: Richard Haines To: Chris PeBenito , selinux-refpolicy@vger.kernel.org Date: Sun, 20 Dec 2020 12:31:30 +0000 In-Reply-To: <76cca0f9-5506-6590-cf05-874ae1b8fde1@ieee.org> References: <20201218150307.8826-1-richard_c_haines@btinternet.com> <76cca0f9-5506-6590-cf05-874ae1b8fde1@ieee.org> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.38.2 (3.38.2-1.fc33) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: selinux-refpolicy@vger.kernel.org On Sat, 2020-12-19 at 15:49 -0500, Chris PeBenito wrote: > On 12/18/20 10:03 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/etc/selinux/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/etc/selinux/refpolicy/policy/policy. 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. > > > > Signed-off-by: Richard Haines > > --- > > 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 > > > > diff --git a/Makefile b/Makefile > > index 6ba215f1..e49d43d0 100644 > > --- a/Makefile > > +++ b/Makefile > > @@ -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 > > diff --git a/Rules.monolithic b/Rules.monolithic > > index a8ae98d1..cd065362 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. > > +# It does this by reading the /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 > >   > >   ######################################## > >   # > > diff --git a/support/selinux_path.py b/support/selinux_path.py > > new file mode 100644 > > index 00000000..b663ff09 > > --- /dev/null > > +++ b/support/selinux_path.py > > @@ -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]) > > 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/policy. # It does this by reading the /config file and using the # SELINUX_PATH/SELINUXTYPE entry to form the . POLICY_ROOT := $(shell $(selinux_policy_root)) SELINUXTYPE := $(shell basename $(POLICY_ROOT)) SELINUX_PATH := $(shell dirname $(POLICY_ROOT)) > >