From patchwork Thu Jan 3 12:03:36 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Lautrbach X-Patchwork-Id: 10747243 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5CBD814DE for ; Thu, 3 Jan 2019 12:03:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4DE7B26D08 for ; Thu, 3 Jan 2019 12:03:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2F30D280FC; Thu, 3 Jan 2019 12:03:53 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 87DE826C9B for ; Thu, 3 Jan 2019 12:03:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728474AbfACMDr (ORCPT ); Thu, 3 Jan 2019 07:03:47 -0500 Received: from mx1.redhat.com ([209.132.183.28]:47836 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726735AbfACMDr (ORCPT ); Thu, 3 Jan 2019 07:03:47 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A60EC7F7B1 for ; Thu, 3 Jan 2019 12:03:46 +0000 (UTC) Received: from workstation.brq.redhat.com (unknown [10.43.12.130]) by smtp.corp.redhat.com (Postfix) with ESMTP id F0FAB6013C; Thu, 3 Jan 2019 12:03:45 +0000 (UTC) From: Petr Lautrbach To: selinux@vger.kernel.org Cc: Petr Lautrbach Subject: [PATCH v2 1/5] python/semanage: move valid_types initialisations to class constructors Date: Thu, 3 Jan 2019 13:03:36 +0100 Message-Id: <20190103120340.2695-1-plautrba@redhat.com> In-Reply-To: <20181220151420.30878-1-plautrba@redhat.com> References: <20181220151420.30878-1-plautrba@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Thu, 03 Jan 2019 12:03:46 +0000 (UTC) Sender: selinux-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Based on idea from Nicolas Iooss Fixes: $ sudo semanage Traceback (most recent call last): File "/usr/sbin/semanage", line 28, in import seobject File "/usr/lib/python3.7/site-packages/seobject.py", line 1045, in class portRecords(semanageRecords): File "/usr/lib/python3.7/site-packages/seobject.py", line 1047, in portRecords valid_types = list(list(sepolicy.info(sepolicy.ATTRIBUTE, "port_type"))[0]["types"]) File "/usr/lib/python3.7/site-packages/sepolicy/__init__.py", line 203, in return ({ File "/usr/lib64/python3.7/site-packages/setools/typeattrquery.py", line 65, in results for attr in self.policy.typeattributes(): AttributeError: 'NoneType' object has no attribute 'typeattributes' https://github.com/SELinuxProject/selinux/issues/81 Signed-off-by: Petr Lautrbach --- python/semanage/seobject.py | 57 ++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/python/semanage/seobject.py b/python/semanage/seobject.py index efec0a55..4490e03f 100644 --- a/python/semanage/seobject.py +++ b/python/semanage/seobject.py @@ -1043,13 +1043,15 @@ class seluserRecords(semanageRecords): class portRecords(semanageRecords): - try: - valid_types = list(list(sepolicy.info(sepolicy.ATTRIBUTE, "port_type"))[0]["types"]) - except RuntimeError: - valid_types = [] + + valid_types = [] def __init__(self, args = None): semanageRecords.__init__(self, args) + try: + self.valid_types = list(list(sepolicy.info(sepolicy.ATTRIBUTE, "port_type"))[0]["types"]) + except RuntimeError: + pass def __genkey(self, port, proto): if proto == "tcp": @@ -1321,14 +1323,16 @@ class portRecords(semanageRecords): print(rec) class ibpkeyRecords(semanageRecords): - try: - q = setools.TypeQuery(setools.SELinuxPolicy(sepolicy.get_installed_policy()), attrs=["ibpkey_type"]) - valid_types = sorted(str(t) for t in q.results()) - except: - valid_types = [] + + valid_types = [] def __init__(self, args = None): semanageRecords.__init__(self, args) + try: + q = setools.TypeQuery(setools.SELinuxPolicy(sepolicy.get_installed_policy()), attrs=["ibpkey_type"]) + self.valid_types = sorted(str(t) for t in q.results()) + except: + pass def __genkey(self, pkey, subnet_prefix): if subnet_prefix == "": @@ -1579,14 +1583,16 @@ class ibpkeyRecords(semanageRecords): print(rec) class ibendportRecords(semanageRecords): - try: - q = setools.TypeQuery(setools.SELinuxPolicy(sepolicy.get_installed_policy()), attrs=["ibendport_type"]) - valid_types = set(str(t) for t in q.results()) - except: - valid_types = [] + + valid_types = [] def __init__(self, args = None): semanageRecords.__init__(self, args) + try: + q = setools.TypeQuery(setools.SELinuxPolicy(sepolicy.get_installed_policy()), attrs=["ibendport_type"]) + self.valid_types = set(str(t) for t in q.results()) + except: + pass def __genkey(self, ibendport, ibdev_name): if ibdev_name == "": @@ -1823,14 +1829,16 @@ class ibendportRecords(semanageRecords): print(rec) class nodeRecords(semanageRecords): - try: - valid_types = list(list(sepolicy.info(sepolicy.ATTRIBUTE, "node_type"))[0]["types"]) - except RuntimeError: - valid_types = [] + + valid_types = [] def __init__(self, args = None): semanageRecords.__init__(self, args) self.protocol = ["ipv4", "ipv6"] + try: + self.valid_types = list(list(sepolicy.info(sepolicy.ATTRIBUTE, "node_type"))[0]["types"]) + except RuntimeError: + pass def validate(self, addr, mask, protocol): newaddr = addr @@ -2264,14 +2272,17 @@ class interfaceRecords(semanageRecords): class fcontextRecords(semanageRecords): - try: - valid_types = list(list(sepolicy.info(sepolicy.ATTRIBUTE, "file_type"))[0]["types"]) - valid_types += list(list(sepolicy.info(sepolicy.ATTRIBUTE, "device_node"))[0]["types"]) - except RuntimeError: - valid_types = [] + + valid_types = [] def __init__(self, args = None): semanageRecords.__init__(self, args) + try: + self.valid_types = list(list(sepolicy.info(sepolicy.ATTRIBUTE, "file_type"))[0]["types"]) + self.valid_types += list(list(sepolicy.info(sepolicy.ATTRIBUTE, "device_node"))[0]["types"]) + except RuntimeError: + pass + self.equiv = {} self.equiv_dist = {} self.equal_ind = False From patchwork Thu Jan 3 12:03:37 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Lautrbach X-Patchwork-Id: 10747241 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 456336C2 for ; Thu, 3 Jan 2019 12:03:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2B18826E74 for ; Thu, 3 Jan 2019 12:03:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1971D28617; Thu, 3 Jan 2019 12:03:53 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B38F326D08 for ; Thu, 3 Jan 2019 12:03:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728553AbfACMDs (ORCPT ); Thu, 3 Jan 2019 07:03:48 -0500 Received: from mx1.redhat.com ([209.132.183.28]:47858 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726735AbfACMDr (ORCPT ); Thu, 3 Jan 2019 07:03:47 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BD69D88304 for ; Thu, 3 Jan 2019 12:03:47 +0000 (UTC) Received: from workstation.brq.redhat.com (unknown [10.43.12.130]) by smtp.corp.redhat.com (Postfix) with ESMTP id EBAA56013C; Thu, 3 Jan 2019 12:03:46 +0000 (UTC) From: Petr Lautrbach To: selinux@vger.kernel.org Cc: Petr Lautrbach Subject: [PATCH v2 2/5] python/semanage: import sepolicy only when it's needed Date: Thu, 3 Jan 2019 13:03:37 +0100 Message-Id: <20190103120340.2695-2-plautrba@redhat.com> In-Reply-To: <20190103120340.2695-1-plautrba@redhat.com> References: <20181220151420.30878-1-plautrba@redhat.com> <20190103120340.2695-1-plautrba@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Thu, 03 Jan 2019 12:03:47 +0000 (UTC) Sender: selinux-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Related: https://github.com/SELinuxProject/selinux/issues/81 Signed-off-by: Petr Lautrbach --- python/semanage/semanage | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/python/semanage/semanage b/python/semanage/semanage index a192fac8..1cb13683 100644 --- a/python/semanage/semanage +++ b/python/semanage/semanage @@ -73,7 +73,7 @@ usage_interface_dict = {' --add': ('-t TYPE', '-r RANGE', 'interface'), ' --modi usage_boolean = "semanage boolean [-h] [-n] [-N] [-S STORE] [" usage_boolean_dict = {' --modify': ('(', '--on', '|', '--off', ')', 'boolean'), ' --list': ('-C',), ' --extract': ('',), ' --deleteall': ('',)} -import sepolicy + class CheckRole(argparse.Action): @@ -82,7 +82,11 @@ class CheckRole(argparse.Action): newval = getattr(namespace, self.dest) if not newval: newval = [] - roles = sepolicy.get_all_roles() + try: + import sepolicy + roles = sepolicy.get_all_roles() + except ValueError: + roles = [] for v in value.split(): if v not in roles: raise ValueError("%s must be an SELinux role:\nValid roles: %s" % (v, ", ".join(roles))) From patchwork Thu Jan 3 12:03:38 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Lautrbach X-Patchwork-Id: 10747245 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 67E3A1880 for ; Thu, 3 Jan 2019 12:03:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 599A026C9B for ; Thu, 3 Jan 2019 12:03:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4E3572861D; Thu, 3 Jan 2019 12:03:53 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DDCCA26E69 for ; Thu, 3 Jan 2019 12:03:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729055AbfACMDs (ORCPT ); Thu, 3 Jan 2019 07:03:48 -0500 Received: from mx1.redhat.com ([209.132.183.28]:51798 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726735AbfACMDs (ORCPT ); Thu, 3 Jan 2019 07:03:48 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 99D01806B3 for ; Thu, 3 Jan 2019 12:03:48 +0000 (UTC) Received: from workstation.brq.redhat.com (unknown [10.43.12.130]) by smtp.corp.redhat.com (Postfix) with ESMTP id E5E796013C; Thu, 3 Jan 2019 12:03:47 +0000 (UTC) From: Petr Lautrbach To: selinux@vger.kernel.org Cc: Petr Lautrbach Subject: [PATCH v2 3/5] python/sepolicy: Add sepolicy.load_store_policy(store) Date: Thu, 3 Jan 2019 13:03:38 +0100 Message-Id: <20190103120340.2695-3-plautrba@redhat.com> In-Reply-To: <20190103120340.2695-1-plautrba@redhat.com> References: <20181220151420.30878-1-plautrba@redhat.com> <20190103120340.2695-1-plautrba@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Thu, 03 Jan 2019 12:03:48 +0000 (UTC) Sender: selinux-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP load_store_policy() allows to (re)load SELinux policy based on a store name. It is useful when SELinux is disabled and default policy is not installed; or when a user wants to query or manipulate another policy. Related: https://bugzilla.redhat.com/show_bug.cgi?id=1558861 Signed-off-by: Petr Lautrbach --- python/sepolicy/sepolicy/__init__.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/python/sepolicy/sepolicy/__init__.py b/python/sepolicy/sepolicy/__init__.py index fbeb731d..b69a6b94 100644 --- a/python/sepolicy/sepolicy/__init__.py +++ b/python/sepolicy/sepolicy/__init__.py @@ -129,6 +129,13 @@ def get_installed_policy(root="/"): pass raise ValueError(_("No SELinux Policy installed")) +def get_store_policy(store, root="/"): + try: + policies = glob.glob("%s%s/policy/policy.*" % (selinux.selinux_path(), store)) + policies.sort() + return policies[-1] + except: + return None def policy(policy_file): global all_domains @@ -156,6 +163,11 @@ def policy(policy_file): except: raise ValueError(_("Failed to read %s policy file") % policy_file) +def load_store_policy(store): + policy_file = get_store_policy(store) + if not policy_file: + return None + policy(policy_file) try: policy_file = get_installed_policy() From patchwork Thu Jan 3 12:03:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Lautrbach X-Patchwork-Id: 10747247 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 89AC113B5 for ; Thu, 3 Jan 2019 12:03:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7AD0526C9B for ; Thu, 3 Jan 2019 12:03:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6F75026D08; Thu, 3 Jan 2019 12:03:53 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 153BB27F94 for ; Thu, 3 Jan 2019 12:03:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729042AbfACMDw (ORCPT ); Thu, 3 Jan 2019 07:03:52 -0500 Received: from mx1.redhat.com ([209.132.183.28]:48170 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726735AbfACMDw (ORCPT ); Thu, 3 Jan 2019 07:03:52 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 24E1AC0528CF for ; Thu, 3 Jan 2019 12:03:52 +0000 (UTC) Received: from workstation.brq.redhat.com (unknown [10.43.12.130]) by smtp.corp.redhat.com (Postfix) with ESMTP id DE0EA6013C; Thu, 3 Jan 2019 12:03:48 +0000 (UTC) From: Petr Lautrbach To: selinux@vger.kernel.org Cc: Petr Lautrbach Subject: [PATCH v2 4/5] python/semanage: Load a store policy and set the store SELinux policy root Date: Thu, 3 Jan 2019 13:03:39 +0100 Message-Id: <20190103120340.2695-4-plautrba@redhat.com> In-Reply-To: <20190103120340.2695-1-plautrba@redhat.com> References: <20181220151420.30878-1-plautrba@redhat.com> <20190103120340.2695-1-plautrba@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Thu, 03 Jan 2019 12:03:52 +0000 (UTC) Sender: selinux-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When "store" is set, sepolicy needs to load a new policy file and selinux module needs to set the new store root path. With this patch, semanage is able to work correctly with non-default -S even when the default policy is not installed yet. Fixes: $ sudo semanage login -S minimum -m -s unconfined_u -r s0-s0:c0.c1023 __default__ libsemanage.dbase_llist_query: could not query record value OSError: [Errno 0] Error Related: https://bugzilla.redhat.com/show_bug.cgi?id=1558861 Signed-off-by: Petr Lautrbach --- python/semanage/seobject.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/python/semanage/seobject.py b/python/semanage/seobject.py index 4490e03f..556d3ba5 100644 --- a/python/semanage/seobject.py +++ b/python/semanage/seobject.py @@ -260,6 +260,8 @@ class semanageRecords: if self.store == "" or self.store == localstore: self.mylog = logger() else: + sepolicy.load_store_policy(self.store) + selinux.selinux_set_policy_root("%s%s" % (selinux.selinux_path(), self.store)) self.mylog = nulllogger() def set_reload(self, load): @@ -1329,7 +1331,7 @@ class ibpkeyRecords(semanageRecords): def __init__(self, args = None): semanageRecords.__init__(self, args) try: - q = setools.TypeQuery(setools.SELinuxPolicy(sepolicy.get_installed_policy()), attrs=["ibpkey_type"]) + q = setools.TypeQuery(setools.SELinuxPolicy(sepolicy.get_store_policy(self.store)), attrs=["ibpkey_type"]) self.valid_types = sorted(str(t) for t in q.results()) except: pass @@ -1589,7 +1591,7 @@ class ibendportRecords(semanageRecords): def __init__(self, args = None): semanageRecords.__init__(self, args) try: - q = setools.TypeQuery(setools.SELinuxPolicy(sepolicy.get_installed_policy()), attrs=["ibendport_type"]) + q = setools.TypeQuery(setools.SELinuxPolicy(sepolicy.get_store_policy(self.store)), attrs=["ibendport_type"]) self.valid_types = set(str(t) for t in q.results()) except: pass From patchwork Thu Jan 3 12:03:40 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Lautrbach X-Patchwork-Id: 10747249 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 38CC614DE for ; Thu, 3 Jan 2019 12:03:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2A2B526C9B for ; Thu, 3 Jan 2019 12:03:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1EB4326E74; Thu, 3 Jan 2019 12:03:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B13A826C9B for ; Thu, 3 Jan 2019 12:03:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729077AbfACMDx (ORCPT ); Thu, 3 Jan 2019 07:03:53 -0500 Received: from mx1.redhat.com ([209.132.183.28]:33310 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726735AbfACMDx (ORCPT ); Thu, 3 Jan 2019 07:03:53 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1CF2E368DD for ; Thu, 3 Jan 2019 12:03:53 +0000 (UTC) Received: from workstation.brq.redhat.com (unknown [10.43.12.130]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6BB136013C; Thu, 3 Jan 2019 12:03:52 +0000 (UTC) From: Petr Lautrbach To: selinux@vger.kernel.org Cc: Petr Lautrbach Subject: [PATCH v2 5/5] python/sepolicy: Make policy files sorting more robust Date: Thu, 3 Jan 2019 13:03:40 +0100 Message-Id: <20190103120340.2695-5-plautrba@redhat.com> In-Reply-To: <20190103120340.2695-1-plautrba@redhat.com> References: <20181220151420.30878-1-plautrba@redhat.com> <20190103120340.2695-1-plautrba@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Thu, 03 Jan 2019 12:03:53 +0000 (UTC) Sender: selinux-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The sorting order seems to be fragile because '100' < '99', so the policy filename needs to be parsed in order to extract the version as an integer and sort according to it. Based on idea from Nicolas Iooss Signed-off-by: Petr Lautrbach --- python/sepolicy/sepolicy/__init__.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/python/sepolicy/sepolicy/__init__.py b/python/sepolicy/sepolicy/__init__.py index b69a6b94..6f729472 100644 --- a/python/sepolicy/sepolicy/__init__.py +++ b/python/sepolicy/sepolicy/__init__.py @@ -119,23 +119,34 @@ all_allow_rules = None all_transitions = None +def policy_sortkey(policy_path): + # Parse the extension of a policy path which looks like .../policy/policy.31 + extension = policy_path.rsplit('/policy.', 1)[1] + try: + return int(extension), policy_path + except ValueError: + # Fallback with sorting on the full path + return 0, policy_path + def get_installed_policy(root="/"): try: path = root + selinux.selinux_binary_policy_path() policies = glob.glob("%s.*" % path) - policies.sort() + policies.sort(key=policy_sortkey) return policies[-1] except: pass raise ValueError(_("No SELinux Policy installed")) -def get_store_policy(store, root="/"): - try: - policies = glob.glob("%s%s/policy/policy.*" % (selinux.selinux_path(), store)) - policies.sort() - return policies[-1] - except: +def get_store_policy(store): + """Get the path to the policy file located in the given store name""" + policies = glob.glob("%s%s/policy/policy.*" % + (selinux.selinux_path(), store)) + if not policies: return None + # Return the policy with the higher version number + policies.sort(key=policy_sortkey) + return policies[-1] def policy(policy_file): global all_domains