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=-20.6 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,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 AF8AAC4361B for ; Sun, 20 Dec 2020 17:48:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 71C6C22B2A for ; Sun, 20 Dec 2020 17:48:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727713AbgLTRrs (ORCPT ); Sun, 20 Dec 2020 12:47:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49852 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727621AbgLTRrr (ORCPT ); Sun, 20 Dec 2020 12:47:47 -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 0181AC061282 for ; Sun, 20 Dec 2020 09:46:09 -0800 (PST) Received: by mail-qk1-x736.google.com with SMTP id 19so6980256qkm.8 for ; Sun, 20 Dec 2020 09:46:09 -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=F7ygh+C2HpAX4PsxmrjYFw7RsVsFgnNx7yRI+UdY4Nk=; b=a+n2WGqRdq/dDXwtl8286XM5dLeBDLTsAisQ+6fm7kOw6nFehmmFt+NEcAVahuTlIJ pj1oPokGc8XmdJ7c0Qr/zj5LAejyZvXZjflze59mB0TGXy18rgjOO+Ml/9QTFKLiqAWU x++5K1+8xlDjUJ7vshwkReh+S+SXYQ4lLclUU= 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=F7ygh+C2HpAX4PsxmrjYFw7RsVsFgnNx7yRI+UdY4Nk=; b=dcBitsKj9ocbVSQIBrR71Lqn5I1OvcAsZJjkUdMues2PAW21A1JURJd5cz1m/NOIlA dRT/umvLtw/BlfP/1sa9C53fTBqhIMCpzUIeeuh+C63IDI6bkrV7OzZ6gthmikorVa1V d+wSGUUmRDFHb+rHZQa9mTq3LHiL9xMNSaALJp4Q/sH/PW6hSOfRCkZ/AbanetRyt8O8 g0ECIf1GMTxX3wo6LIk1z+AXzGgCJ5M8MOggGsN2WwPPb7n2N9/4RY0JB+iJEcTJIgBJ pkLYEFy2mKenjVmriq4BEHoJXSu9I9D5uydaDC2rX4hlL6zB1+ZnJupNrYk//ltw5BNx /2Ow== X-Gm-Message-State: AOAM531ZMBK4Bdeq8roistdsM6dYu/+HN9XwJ7lbP4EiB+rD24NvgK9+ 8yv0Ng0GxvC3R8h/8RcS6kBYt2VZnIkxgw== X-Google-Smtp-Source: ABdhPJxmQqCiu5un5dTFR67ZRSKIIK7js9Q7aO5AtcV9WA26U1vEvmDl0JmlZymEDFQs4lk8uaFNGQ== X-Received: by 2002:a37:7444:: with SMTP id p65mr2250595qkc.476.1608486368148; Sun, 20 Dec 2020 09:46:08 -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 a35sm9645387qtk.82.2020.12.20.09.46.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 20 Dec 2020 09:46:07 -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> <5125f892-4f58-27f5-cc0d-bc6741120672@ieee.org> <531e1530e858cbf3e062f9fd14b294a3177c016d.camel@btinternet.com> From: Chris PeBenito Message-ID: <0e50cc85-36f3-5015-5582-f14093299458@ieee.org> Date: Sun, 20 Dec 2020 12:46:07 -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: <531e1530e858cbf3e062f9fd14b294a3177c016d.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 11:40 AM, Richard Haines wrote: > On Sun, 2020-12-20 at 10:01 -0500, Chris PeBenito wrote: >> 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)) >> >> On second thought, isn't another way of doing the check: >> >> selinux.selinux_binary_policy_path() + "." + POLICYVER == >> $(loadpath)? > > Yes I could, however that accounts for only one of the checks: > Cannot load policy as policy root MUST be ... > > The other check uses SELINUXTYPE against $NAME to see if the config > file has the correct entry: > Cannot load policy as $(SELINUX_PATH)/config file ... > > I could just use one test and say that the policy must be based at > /etc/selinux/$(NAME) plus the /etc/selinux/config file SELINUXTYPE > entry must be $(NAME) ??? Since the Makefile creates $(loadpath) to be $(topdir)/$NAME/policy/policy.$ver, and selinux.selinux_binary_policy_path() takes into account SELINUXTYPE, the only reason the $topdir ($DESTDIR/etc/selinux), would be a problem is if DESTDIR is set. We should instead fail the load if DESTDIR is set. If the user alters the $(topdir) of the Makefile to change /etc/selinux into something else, that is undefined behavior since lowercase variables aren't meant to be altered by users. I see no reason to check that case. -- Chris PeBenito