From patchwork Tue Nov 23 19:07:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Christian_G=C3=B6ttsche?= X-Patchwork-Id: 12635185 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 65B96C433EF for ; Tue, 23 Nov 2021 19:07:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232333AbhKWTKZ (ORCPT ); Tue, 23 Nov 2021 14:10:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232058AbhKWTKY (ORCPT ); Tue, 23 Nov 2021 14:10:24 -0500 Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 270DCC061574 for ; Tue, 23 Nov 2021 11:07:16 -0800 (PST) Received: by mail-ed1-x529.google.com with SMTP id l25so79495662eda.11 for ; Tue, 23 Nov 2021 11:07:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20210112; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=lperPD0EgmDe1UueVG3FdQoaVjhZvf7afOR3TfMdptc=; b=i3Kxh+ytSq6Kjai7WLeAsVinV9o1I7S288mG4hpb5l72HtWa+weBdTL8G4+fNh/vwt pYAd/neylcc0whWrd7RqL/815C1tigEkC4ntl5UkoJkm9A3KusgyqYr+rtdeuvFecgxt mdY5EFsw182mijgh7Gf1Ifb7D7zOHNSpttyEonC1wMSvEcCsiZeHkSmyxdRym9hDLaaA m8nYqYMY2IoNjb+JPSljWnriM7HaQuSE764facgH9XfquY06ZQqj/qcysFYAX8dJSwBe eJ4CKcoOtr50qfZ+88GtSG6R+WzbUFv7OjryfYFSDHwallvb9An6niawI89b41fgCcC3 BPBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=lperPD0EgmDe1UueVG3FdQoaVjhZvf7afOR3TfMdptc=; b=pOJKOPoaGduKodQzQrWg1B/STsbHxcyCljX9p1nOw8PC+7/iJEqXLo/Wd2mdWd9LL9 faMFFEFVf7vMUAmPu13qro3qsH7N2YXWsCZbYyWX788ATiGlDkzjtqD60UifweeXaRF+ i0FsNbZT1Z9adegbtUC801ldQsTaFZNeQyTEiXAE4im7MjDfWEJgk7BEhZHRXKV9wbFW ol+1kvoPeA10ltoG6vhmBt8Q2dAZ+yg8/wN5Sl+FmSx/krSnxCOQazW6fC11LsRFGk3D R1TkEymaCcuDQ3gQkeRjkPyS+Dd41Pfp5O2NQbiCXSSCcJD4abZ5T5CcjA6WvNuPV+Lr fVBg== X-Gm-Message-State: AOAM533wD7uNe1sYRdPXDLpf5RUbnaZbT79mhmAbgIUyXZovzWIpe2UI L7+1IAjpcyDOp0s7iX5ne0j86/i/3vI= X-Google-Smtp-Source: ABdhPJwPbjvsmcjHj59ZYE0vZNnrA10de911/jdmCeSEVci/AIcC5M+kgcvONh07djWi0vVQHKDiEw== X-Received: by 2002:a17:907:1614:: with SMTP id hb20mr10742090ejc.299.1637694434574; Tue, 23 Nov 2021 11:07:14 -0800 (PST) Received: from debianHome.localdomain (dynamic-095-112-244-006.95.112.pool.telefonica.de. [95.112.244.6]) by smtp.gmail.com with ESMTPSA id e16sm7093326edz.18.2021.11.23.11.07.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Nov 2021 11:07:14 -0800 (PST) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH 1/3] libsepol: introduce ebitmap_subtract() Date: Tue, 23 Nov 2021 20:07:02 +0100 Message-Id: <20211123190704.14341-1-cgzones@googlemail.com> X-Mailer: git-send-email 2.34.0 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Add a subtract method for ebitmaps. Signed-off-by: Christian Göttsche --- libsepol/include/sepol/policydb/ebitmap.h | 1 + libsepol/src/ebitmap.c | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/libsepol/include/sepol/policydb/ebitmap.h b/libsepol/include/sepol/policydb/ebitmap.h index 81d0c7a6..daca67a2 100644 --- a/libsepol/include/sepol/policydb/ebitmap.h +++ b/libsepol/include/sepol/policydb/ebitmap.h @@ -83,6 +83,7 @@ static inline int ebitmap_node_get_bit(const ebitmap_node_t * n, unsigned int bi extern int ebitmap_cmp(const ebitmap_t * e1, const ebitmap_t * e2); extern int ebitmap_or(ebitmap_t * dst, const ebitmap_t * e1, const ebitmap_t * e2); extern int ebitmap_union(ebitmap_t * dst, const ebitmap_t * e1); +extern int ebitmap_subtract(ebitmap_t *dst, const ebitmap_t *e1); extern int ebitmap_and(ebitmap_t *dst, const ebitmap_t *e1, const ebitmap_t *e2); extern int ebitmap_xor(ebitmap_t *dst, const ebitmap_t *e1, const ebitmap_t *e2); extern int ebitmap_not(ebitmap_t *dst, const ebitmap_t *e1, unsigned int maxbit); diff --git a/libsepol/src/ebitmap.c b/libsepol/src/ebitmap.c index 1de3816a..faa9e42e 100644 --- a/libsepol/src/ebitmap.c +++ b/libsepol/src/ebitmap.c @@ -72,6 +72,19 @@ int ebitmap_union(ebitmap_t * dst, const ebitmap_t * e1) return 0; } +int ebitmap_subtract(ebitmap_t *dst, const ebitmap_t *e1) +{ + unsigned int i, length = min(ebitmap_length(dst), ebitmap_length(e1)); + for (i=0; i < length; i++) { + if (ebitmap_get_bit(e1, i)) { + int rc = ebitmap_set_bit(dst, i, 0); + if (rc < 0) + return rc; + } + } + return 0; +} + int ebitmap_and(ebitmap_t *dst, const ebitmap_t *e1, const ebitmap_t *e2) { unsigned int i, length = min(ebitmap_length(e1), ebitmap_length(e2)); From patchwork Tue Nov 23 19:07:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Christian_G=C3=B6ttsche?= X-Patchwork-Id: 12635187 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BB656C433F5 for ; Tue, 23 Nov 2021 19:07:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235762AbhKWTKZ (ORCPT ); Tue, 23 Nov 2021 14:10:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44290 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232058AbhKWTKZ (ORCPT ); Tue, 23 Nov 2021 14:10:25 -0500 Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [IPv6:2a00:1450:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A309CC061714 for ; Tue, 23 Nov 2021 11:07:16 -0800 (PST) Received: by mail-ed1-x52a.google.com with SMTP id r25so59483727edq.7 for ; Tue, 23 Nov 2021 11:07:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20210112; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=nt0jIK7SPFKBMVlvHFcofgHig0quJrfmFbQ9J8vJA7w=; b=h43rrrWuH9hgAHBarDjV6PYPauVY0UbVAYX7BQPL6S2OBDi0U4UIsdpGasA2yET14g hNQbWlncmb+dT6AcGvRIF2+kqvhEz5/viocCyq9OXfGaGRJx37aLeBU91/IxhktR2RGl C0P25XSbKctsUsbzgvEzP/1adFoyEf2GvNQT4cn3Gov1zIa4AFvSh0Zz+urrN1boq4Ft YX4mz0WLItoQzKfwJJZk0lSmUT42nbd/FKh6nlAFY+y43SReGGSg1hcKGDmzLDYllbJw O0Hq82lrz/JdsGbWux4zotv4tMbe+Q9ztosCSprq7Y8IbQMJPHwVUMLS3iA2FJaCD2t5 5KhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=nt0jIK7SPFKBMVlvHFcofgHig0quJrfmFbQ9J8vJA7w=; b=piVfguOAJAuUDIF+tOhMtbjZEfYT9nWM/F83IBIJ2RgisgeZo8O9uBtcIEbdcq46fZ 5Agm8TUqjGs/k3KQZXuyY3NbGw/k5OVr65jejgKRsanuj2tiL/K7zId56b51ceaUnMrA pEvjEWURNMyBoNMa4U2gqoyklNPgxy75Wz5OKCfmReg4PDqrMS5nDjXOBa/07v0g2sZW GqniF/p8j+ATH/TCTA7JIau+qKjAW4469CCn0rc7/vjuYged+ImywJSfE4e0OlRrA0S0 exEp2NeNz2RDQS96XNgNszWTtfhCLJnNbdn8+uXKVwvsFogob4Hr1eHJMoItQAIUg601 Z4gw== X-Gm-Message-State: AOAM531aC+P8gcAn0jtufCiVhvplEEPNHIMD1sQjxBu1o//y6/iMHHU8 WMmnm/4ObbpJlAwLaDrHnXcoxxugRQw= X-Google-Smtp-Source: ABdhPJwq58hcNiKEfOUCj8OtWRZQ2RqbYUHxGT3xhNVWde9DYOS1nrCul1C4raVgFQF11MRXSoWkCg== X-Received: by 2002:a05:6402:170a:: with SMTP id y10mr13091305edu.324.1637694435097; Tue, 23 Nov 2021 11:07:15 -0800 (PST) Received: from debianHome.localdomain (dynamic-095-112-244-006.95.112.pool.telefonica.de. [95.112.244.6]) by smtp.gmail.com with ESMTPSA id e16sm7093326edz.18.2021.11.23.11.07.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Nov 2021 11:07:14 -0800 (PST) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH 2/3] libsepol: add not-self neverallow support Date: Tue, 23 Nov 2021 20:07:03 +0100 Message-Id: <20211123190704.14341-2-cgzones@googlemail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211123190704.14341-1-cgzones@googlemail.com> References: <20211123190704.14341-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Add support for not-self neverallow rules. These do not trigger on allow rules where the source type is exactly equal to the target type. Signed-off-by: Christian Göttsche --- libsepol/include/sepol/policydb/policydb.h | 3 +- libsepol/src/assertion.c | 39 ++++++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/libsepol/include/sepol/policydb/policydb.h b/libsepol/include/sepol/policydb/policydb.h index 4bf9f05d..da5256e8 100644 --- a/libsepol/include/sepol/policydb/policydb.h +++ b/libsepol/include/sepol/policydb/policydb.h @@ -285,7 +285,8 @@ typedef struct avrule { #define AVRULE_XPERMS (AVRULE_XPERMS_ALLOWED | AVRULE_XPERMS_AUDITALLOW | \ AVRULE_XPERMS_DONTAUDIT | AVRULE_XPERMS_NEVERALLOW) uint32_t specified; -#define RULE_SELF 1 +#define RULE_SELF (1U << 1) +#define RULE_NOTSELF (1U << 2) uint32_t flags; type_set_t stypes; type_set_t ttypes; diff --git a/libsepol/src/assertion.c b/libsepol/src/assertion.c index dd2749a0..9bbeddb0 100644 --- a/libsepol/src/assertion.c +++ b/libsepol/src/assertion.c @@ -241,7 +241,7 @@ static int report_assertion_avtab_matches(avtab_key_t *k, avtab_datum_t *d, void if (rc) goto oom; - if (avrule->flags == RULE_SELF) { + if (avrule->flags & RULE_SELF) { rc = ebitmap_and(&matches, &p->attr_type_map[k->source_type - 1], &p->attr_type_map[k->target_type - 1]); if (rc) goto oom; @@ -268,6 +268,8 @@ static int report_assertion_avtab_matches(avtab_key_t *k, avtab_datum_t *d, void ebitmap_for_each_positive_bit(&src_matches, snode, i) { ebitmap_for_each_positive_bit(&tgt_matches, tnode, j) { + if ((avrule->flags & RULE_NOTSELF) && i == j) + continue; if (avrule->specified == AVRULE_XPERMS_NEVERALLOW) { a->errors += report_assertion_extended_permissions(handle,p, avrule, i, j, cp, perms, k, avtab); @@ -402,7 +404,7 @@ static int check_assertion_extended_permissions(avrule_t *avrule, avtab_t *avtab if (rc) goto oom; - if (avrule->flags == RULE_SELF) { + if (avrule->flags & RULE_SELF) { rc = ebitmap_and(&matches, &p->attr_type_map[k->source_type - 1], &p->attr_type_map[k->target_type - 1]); if (rc) @@ -418,6 +420,21 @@ static int check_assertion_extended_permissions(avrule_t *avrule, avtab_t *avtab } } + if (avrule->flags & RULE_NOTSELF) { + rc = ebitmap_and(&matches, &p->attr_type_map[k->source_type - 1], &p->attr_type_map[k->target_type - 1]); + if (rc) + goto oom; + rc = ebitmap_and(&self_matches, &avrule->ttypes.types, &matches); + if (rc) + goto oom; + + if (!ebitmap_is_empty(&self_matches)) { + rc = ebitmap_subtract(&tgt_matches, &self_matches); + if (rc) + goto oom; + } + } + if (ebitmap_is_empty(&tgt_matches)) goto exit; @@ -463,7 +480,7 @@ static int check_assertion_avtab_match(avtab_key_t *k, avtab_datum_t *d, void *a if (rc == 0) goto exit; - if (avrule->flags == RULE_SELF) { + if (avrule->flags & RULE_SELF) { /* If the neverallow uses SELF, then it is not enough that the * neverallow's source matches the src and tgt of the rule being checked. * It must match the same thing in the src and tgt, so AND the source @@ -479,6 +496,22 @@ static int check_assertion_avtab_match(avtab_key_t *k, avtab_datum_t *d, void *a ebitmap_destroy(&match); } + if (avrule->flags & RULE_NOTSELF) { + ebitmap_t match; + rc = ebitmap_cpy(&match, &p->attr_type_map[k->source_type - 1]); + if (rc) { + ebitmap_destroy(&match); + goto oom; + } + rc = ebitmap_subtract(&match, &p->attr_type_map[k->target_type - 1]); + if (rc) { + ebitmap_destroy(&match); + goto oom; + } + rc2 = ebitmap_match_any(&avrule->ttypes.types, &match); + ebitmap_destroy(&match); + } + /* neverallow may have tgts even if it uses SELF */ rc = ebitmap_match_any(&avrule->ttypes.types, &p->attr_type_map[k->target_type -1]); if (rc == 0 && rc2 == 0) From patchwork Tue Nov 23 19:07:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Christian_G=C3=B6ttsche?= X-Patchwork-Id: 12635189 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 12D06C4332F for ; Tue, 23 Nov 2021 19:07:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232058AbhKWTKZ (ORCPT ); Tue, 23 Nov 2021 14:10:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44292 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235934AbhKWTKZ (ORCPT ); Tue, 23 Nov 2021 14:10:25 -0500 Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [IPv6:2a00:1450:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1BD06C061574 for ; Tue, 23 Nov 2021 11:07:17 -0800 (PST) Received: by mail-ed1-x52f.google.com with SMTP id l25so79495845eda.11 for ; Tue, 23 Nov 2021 11:07:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20210112; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=cKw3WPbPPuIF/Kj5aGah+9PPq5SwBakWIoDCIzkUXHU=; b=Y5OkN+/mVAaPmeTxy+gFhZV6VMjg1kqubhJn7+1KXaem8wllCFtwqfhWlYluT24TAo UUmVh9FP748jP+J/Y3+P+A8mGjGmWYMpLle5m5ujuZb1DyNRFt6vDHwv5rPv5EKDf3LA 0WHXsWRj5n/oA5U01U/Xm9Mw+otHHAGlsxYHwJOJmV2HhCc+M8lU0jK2S30bncqbsBFt uA6cnLdpR5r4zbd27PNvb5fj4Se2aSD7qKJfO4HCVGLIf/dCg1Uyp3HMPuC4trHSxlJr 0cWSF4CjHeclPJdUE6ncYQ6lRtQe/qkgNCAZ06IDnKf311TIGVJWEyIOG4tFGleZY9so o0Qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=cKw3WPbPPuIF/Kj5aGah+9PPq5SwBakWIoDCIzkUXHU=; b=NZ07ED2W2+rj2Ts6vsEPZocDPfuMIvcN2EX6Bd7TAMv4URw3KuEoiPnj1oBZ0B0piG 8IEHFnHPSL/x1gGP8QShMGV6w53/q6VI600bUz2ZkxyIbnsgVTR1m45aXjcnUG3LoPRr Ntpt+LwmZzGZIqkB395pkREGgdBAHaeBshBqcDUJx9Nu3YqrkO83q++6zhdw0P3Iexi3 kLyzOpxK6kskzNztc05QS0eUCoHANMNLMR32fgWsUWdLvepLsd3NoC5tdYyVNLmP2UjR qz2oNb/PWTxNp6lns72RMEXRAKS3CTHrpMdv5fbGOdBgXC+jxg7JEfm6QG7YBJhO+Tza tfoQ== X-Gm-Message-State: AOAM533FWvlcgwnM6YWBqmWRra07KpHDgGUaKrMedXGQp+85Bu/VvPNG E54aqlbUvke+GayqBy34WdaOsXbAKgc= X-Google-Smtp-Source: ABdhPJx0ZnJXOcW+kOPvZ8CDIXpl0pSyPUFUIEr/0jGtfkI15FTHAKALtB5sUx0pss2EJGe8YBZM2w== X-Received: by 2002:a05:6402:40c7:: with SMTP id z7mr13471696edb.255.1637694435660; Tue, 23 Nov 2021 11:07:15 -0800 (PST) Received: from debianHome.localdomain (dynamic-095-112-244-006.95.112.pool.telefonica.de. [95.112.244.6]) by smtp.gmail.com with ESMTPSA id e16sm7093326edz.18.2021.11.23.11.07.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Nov 2021 11:07:15 -0800 (PST) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH 3/3] checkpolicy: add not-self neverallow support Date: Tue, 23 Nov 2021 20:07:04 +0100 Message-Id: <20211123190704.14341-3-cgzones@googlemail.com> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211123190704.14341-1-cgzones@googlemail.com> References: <20211123190704.14341-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Add support for using negated or complemented self in the target type of neverallow rules. Some refpolicy examples: neverallow * ~self:{ capability cap_userns capability2 cap2_userns } *; # no violations neverallow domain domain:file ~{ append read_file_perms write }; libsepol.report_failure: neverallow on line 565 of policy/modules/kernel/kernel.te (or line 30300 of policy.conf) violated by allow sysadm_t httpd_bugzilla_script_t:file { create setattr relabelfrom relabelto unlink link rename }; libsepol.report_failure: neverallow on line 565 of policy/modules/kernel/kernel.te (or line 30300 of policy.conf) violated by allow chromium_t chromium_t:file { create }; libsepol.report_failure: neverallow on line 564 of policy/modules/kernel/kernel.te (or line 30299 of policy.conf) violated by allow sysadm_t httpd_bugzilla_script_t:dir { create }; neverallow domain { domain -self }:file ~{ append read_file_perms write }; libsepol.report_failure: neverallow on line 565 of policy/modules/kernel/kernel.te (or line 30300 of policy.conf) violated by allow sysadm_t httpd_bugzilla_script_t:file { create setattr relabelfrom relabelto unlink link rename }; libsepol.report_failure: neverallow on line 564 of policy/modules/kernel/kernel.te (or line 30299 of policy.conf) violated by allow sysadm_t httpd_bugzilla_script_t:dir { create }; Using negated self in a complement `~{ domain -self }` is not supported. Signed-off-by: Christian Göttsche --- checkpolicy/policy_define.c | 46 ++++++++++++++++++++++++++++++++----- checkpolicy/test/dismod.c | 6 ++++- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/checkpolicy/policy_define.c b/checkpolicy/policy_define.c index d3eb6111..662b35e7 100644 --- a/checkpolicy/policy_define.c +++ b/checkpolicy/policy_define.c @@ -2067,12 +2067,17 @@ static int define_te_avtab_xperms_helper(int which, avrule_t ** rule) while ((id = queue_remove(id_queue))) { if (strcmp(id, "self") == 0) { free(id); - if (add == 0) { - yyerror("-self is not supported"); + if (add == 0 && which != AVRULE_NEVERALLOW) { + yyerror("-self is only supported in neverallowx rules"); + ret = -1; + goto out; + } + avrule->flags |= (add ? RULE_SELF : RULE_NOTSELF); + if ((avrule->flags & RULE_SELF) && (avrule->flags & RULE_NOTSELF)) { + yyerror("self and -self is not supported"); ret = -1; goto out; } - avrule->flags |= RULE_SELF; continue; } if (set_types @@ -2083,6 +2088,18 @@ static int define_te_avtab_xperms_helper(int which, avrule_t ** rule) } } + if ((avrule->ttypes.flags & TYPE_COMP)) { + if (avrule->flags & RULE_NOTSELF) { + yyerror("-self is not supported in complements"); + ret = -1; + goto out; + } + if (avrule->flags & RULE_SELF) { + avrule->flags &= ~RULE_SELF; + avrule->flags |= RULE_NOTSELF; + } + } + ebitmap_init(&tclasses); ret = read_classes(&tclasses); if (ret) @@ -2528,12 +2545,17 @@ static int define_te_avtab_helper(int which, avrule_t ** rule) while ((id = queue_remove(id_queue))) { if (strcmp(id, "self") == 0) { free(id); - if (add == 0) { - yyerror("-self is not supported"); + if (add == 0 && which != AVRULE_NEVERALLOW) { + yyerror("-self is only supported in neverallow rules"); + ret = -1; + goto out; + } + avrule->flags |= (add ? RULE_SELF : RULE_NOTSELF); + if ((avrule->flags & RULE_SELF) && (avrule->flags & RULE_NOTSELF)) { + yyerror("self and -self is not supported"); ret = -1; goto out; } - avrule->flags |= RULE_SELF; continue; } if (set_types @@ -2544,6 +2566,18 @@ static int define_te_avtab_helper(int which, avrule_t ** rule) } } + if ((avrule->ttypes.flags & TYPE_COMP)) { + if (avrule->flags & RULE_NOTSELF) { + yyerror("-self is not supported in complements"); + ret = -1; + goto out; + } + if (avrule->flags & RULE_SELF) { + avrule->flags &= ~RULE_SELF; + avrule->flags |= RULE_NOTSELF; + } + } + ebitmap_init(&tclasses); ret = read_classes(&tclasses); if (ret) diff --git a/checkpolicy/test/dismod.c b/checkpolicy/test/dismod.c index ec2a3e9a..a2d74d42 100644 --- a/checkpolicy/test/dismod.c +++ b/checkpolicy/test/dismod.c @@ -124,7 +124,7 @@ static int display_type_set(type_set_t * set, uint32_t flags, policydb_t * polic } num_types = 0; - if (flags & RULE_SELF) { + if (flags & (RULE_SELF | RULE_NOTSELF)) { num_types++; } @@ -169,6 +169,10 @@ static int display_type_set(type_set_t * set, uint32_t flags, policydb_t * polic fprintf(fp, " self"); } + if (flags & RULE_NOTSELF) { + fprintf(fp, " -self"); + } + if (num_types > 1) fprintf(fp, " }");