Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp3551836pxu; Sun, 20 Dec 2020 06:56:19 -0800 (PST) X-Google-Smtp-Source: ABdhPJxX/ieEi64aQ6GbDnGqlX1nctglzUroA2RmUO6j/VWpSW0O0MV0MLRuOtIxoEAMZ6ATNMum X-Received: by 2002:a17:906:b79a:: with SMTP id dt26mr11534403ejb.337.1608476179620; Sun, 20 Dec 2020 06:56:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608476179; cv=none; d=google.com; s=arc-20160816; b=EVlnibvl+UqHPSybcyqLsO9Ke7wTyLaW3yBByftPv6P3Y3+6TNedh4KGA8es9V0DNz r3gkdYEY/m4aiOrLRi52tLG/xJ1c33ycSgAVWmP7iv6luj22/ogKKtOhrTAqPinH0ST8 laoNcIDMsCyY05VJygK5CtNwUNE9a+ozfqOEV9RLogLBSyRSiWwGd6o3fk0pTjs2KFcQ 04om2L2TM3WZ+bRq0sqJbMeC/yR19HzaNKPw6n417nfiuc+Q+1AN6KfsH1u1Pkl2AdCd McOEXWbCS9bLyuiRy0NeIWJ+a9jPhFOvu62JGgAaM8HPngYEgyReSWhF70Bk6OyuKgLY 9ybw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :to:subject:dkim-signature; bh=uAFSc4uv4+8zvnWm+ahTlOA14l8x7DfcV/ZAgIWB/24=; b=AcPjgtlgFDUYa1F3jDR2FdKCN+tHEDzsbbCDLSJ61FsxOBbm8LcBbJfc08PyrUawvm KPE062TcDUK2IpCp5SQKVELfiKwsdsiOYxvnJajuCHgPzZ64EbtyjhbcVdnyi2A8//im Na3RwDRAamxdh7B4JEyeWxFK/aj0E/oMhtuuO3HoTfZGfRsltUkFKvEpwFSJJvxO/3mz XDP+6opXZcN1r0rC7PvejrH+M/+bGgVTSadWs/wNZqBFhzDEzy92CvtNNHURl4CjCDlr /ecC9ZwFow0yLrnfwT/tUNYSpvDPVfprngF6TzFZ9P9rKqE2HtJV5mVYzE8F5nAUKPJs 4s5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ieee.org header.s=google header.b=OaTP2sPw; spf=pass (google.com: domain of selinux-refpolicy-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=selinux-refpolicy-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ieee.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id fy24si7513709ejb.348.2020.12.20.06.56.12; Sun, 20 Dec 2020 06:56:19 -0800 (PST) Received-SPF: pass (google.com: domain of selinux-refpolicy-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@ieee.org header.s=google header.b=OaTP2sPw; spf=pass (google.com: domain of selinux-refpolicy-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=selinux-refpolicy-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ieee.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727589AbgLTOzk (ORCPT + 18 others); Sun, 20 Dec 2020 09:55:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726943AbgLTOzk (ORCPT ); Sun, 20 Dec 2020 09:55:40 -0500 Received: from mail-qv1-xf2f.google.com (mail-qv1-xf2f.google.com [IPv6:2607:f8b0:4864:20::f2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A944C0613CF for ; Sun, 20 Dec 2020 06:54:57 -0800 (PST) Received: by mail-qv1-xf2f.google.com with SMTP id j18so3341344qvu.3 for ; Sun, 20 Dec 2020 06:54: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=uAFSc4uv4+8zvnWm+ahTlOA14l8x7DfcV/ZAgIWB/24=; b=OaTP2sPw/CaslWQ/78Qm1S6+X6ym+80qJ3I+ckVGmHpAusKrbx8Hmcfp7YFLLCVgt/ LguZKq7TkvJKcBSyV/Yu1KPXOMAC3afUSCVgNA1RcssCpgjskdDpBa3X84CLyjWoKNvw I+yyd/Tt6WoTIMp3a6R52Jv73pZfZ7WX3dTxs= 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=uAFSc4uv4+8zvnWm+ahTlOA14l8x7DfcV/ZAgIWB/24=; b=YBW2auwhAwBnSCWI84oBhWOxpIOSyREs5qkQYEWjn0FKcZx5F7XU6JoFkHcfeTYFZQ obAjsn0Iffk+PF09fpp3OaVWqjrVu7eRtOWcT2Cx1s/HzGyotuA78yMviOIeg0l+uRT8 rVWl9ly6GvUGmPbTfuVfel0T89CfzK6yzVXJ7xZX8q+arL2K06p/ZyMX0UgZfxac6Std If62zX5XtKLYe4SAfdpE/drd0g++aeuiKJdQ0fGNr0ZsUjQUKbkr1PNTedJjjBvZC+sg 6Yhn0XrnTJsuM+nK7nQKt45B0IS9Eu/PJsomBHGbzlxGAGfBI4ubUxrDpfg2coh3z7MZ b2fQ== X-Gm-Message-State: AOAM532i1cteAm8POkpRt6TcbzlTBSMv+bQ5iRIOEnRNSMng2xaOK4F9 ZVdzc8XSbaMP551QIaXwc4RnvPIR29CP3g== X-Received: by 2002:a05:6214:20a2:: with SMTP id 2mr13870032qvd.27.1608476096516; Sun, 20 Dec 2020 06:54: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 l11sm8979973qtn.83.2020.12.20.06.54.55 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 20 Dec 2020 06:54:55 -0800 (PST) Subject: Re: [PATCH V2] Ensure correct monolithic binary policy is loaded To: Richard Haines , selinux-refpolicy@vger.kernel.org References: <20201218150307.8826-1-richard_c_haines@btinternet.com> <76cca0f9-5506-6590-cf05-874ae1b8fde1@ieee.org> <5877289e96fc4fc8c4c4560ae1ea77a1ee91112c.camel@btinternet.com> From: Chris PeBenito Message-ID: <5e0309bd-9681-cd82-bfec-c64ebce33895@ieee.org> Date: Sun, 20 Dec 2020 09:54:55 -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: <5877289e96fc4fc8c4c4560ae1ea77a1ee91112c.camel@btinternet.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: selinux-refpolicy@vger.kernel.org On 12/20/20 7:31 AM, Richard Haines wrote: > 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)) 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 -- Chris PeBenito