From patchwork Wed Jan 5 21:16:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Carter X-Patchwork-Id: 12704722 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 F009EC4332F for ; Wed, 5 Jan 2022 21:16:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244378AbiAEVQR (ORCPT ); Wed, 5 Jan 2022 16:16:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50692 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244377AbiAEVQP (ORCPT ); Wed, 5 Jan 2022 16:16:15 -0500 Received: from mail-qk1-x72c.google.com (mail-qk1-x72c.google.com [IPv6:2607:f8b0:4864:20::72c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61BC7C061201 for ; Wed, 5 Jan 2022 13:16:15 -0800 (PST) Received: by mail-qk1-x72c.google.com with SMTP id 131so744367qkk.2 for ; Wed, 05 Jan 2022 13:16:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=76MauF8kdtXPkR+xoMbR8/fIK4qjtBHmkxy7ooSHQ50=; b=elplV5j+EBX+EKWQhLTALO/efZEAif7icdR+t9VXcpxGUJ9M46BR5p0OKKJ0td2pTJ RDSruo4hMn1bRHj+zhUsqeYwLl9jQvQNhMOmR7IHuosOCO9r+2oz8/KXS+b3r9VrnbUq 0yLeAiKWiYGczzU92SearoHHO8C/LNR1sOZefRxhmX1JeMlLAoq32URKiUNQ5l/eb1t4 wfwntA6ndOVCENKz0S5DUA0Z/aje8rXS1KjJoYy6YYpQiGhk4x1qPpyhbrRZPWKUNamF vZZfj3HijpU4ZY0wr2vENMezabZoRer8ZvZ88vU6LeZGKzNMaD2BsnK5QGnsDqPEXdqe yQRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=76MauF8kdtXPkR+xoMbR8/fIK4qjtBHmkxy7ooSHQ50=; b=isH0mO8KjP1QvUibk6u5frV+dpB8z3U7NuKoSyAuHR3sTzIs6Rlb+3QXhAYHfCD7ZD RS5SLMjzyfbq3i6sOvuBz3N8UWq9e2BsflBncY0zU8hIcz75k0pyBKuVy58h6yE0X4TY L5moAkLOF8TfhVS2smpQvvGBziUvzUX91Q0QelIL43szh+zzkayz6WhP2pn2QVLc0Zvs oYwbJ1u06PF6zpK1EEyJ15tYhzpY/1IE9eqkMeZYIkNs9K7ec4Xa8Q4wNdNNzJ2I71y4 L0RjflZInd5ZFaceVml9DIOKJ8DwxMWYngCI1MsSOIBMKO4IHxhoJYMzyOghvT77UEez 28Fg== X-Gm-Message-State: AOAM530OKmuzW0pWUT4ZjaDu4hS58/A8cLJsCEwRrZgWAvunXh6V7opQ JLKJbgpP+Q9gyD2OcQVBlW464aHMy6U= X-Google-Smtp-Source: ABdhPJy7LYFva8eEX0MxPOcYRR6rSpuzJCJRFeq0mSD7yGgHZDBb7zX+vUt6xM55Tw2SiqLhQFhleQ== X-Received: by 2002:a05:620a:4111:: with SMTP id j17mr39556996qko.593.1641417373198; Wed, 05 Jan 2022 13:16:13 -0800 (PST) Received: from localhost.localdomain (c-69-250-217-147.hsd1.md.comcast.net. [69.250.217.147]) by smtp.gmail.com with ESMTPSA id m1sm36148502qkn.115.2022.01.05.13.16.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jan 2022 13:16:12 -0800 (PST) From: James Carter To: selinux@vger.kernel.org Cc: James Carter Subject: [PATCH 1/3] libsepol/cil: Do not copy blockabstracts when inheriting a block Date: Wed, 5 Jan 2022 16:16:00 -0500 Message-Id: <20220105211602.359300-1-jwcart2@gmail.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Do not copy any blockabstract statements when copying a block to resolve a blockinherit statement. Inheriting a block from what was just inherited does not work, so there is no reason to create an abstract block. Signed-off-by: James Carter --- libsepol/cil/src/cil_copy_ast.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libsepol/cil/src/cil_copy_ast.c b/libsepol/cil/src/cil_copy_ast.c index 2fad972c..a4ead9db 100644 --- a/libsepol/cil/src/cil_copy_ast.c +++ b/libsepol/cil/src/cil_copy_ast.c @@ -1725,6 +1725,12 @@ int __cil_copy_node_helper(struct cil_tree_node *orig, uint32_t *finished, void copy_func = &cil_copy_block; break; case CIL_BLOCKABSTRACT: + if (args->orig_dest->flavor == CIL_BLOCKINHERIT) { + /* When inheriting a block, don't copy any blockabstract + * statements. Inheriting a block from a block that was + * just inherited never worked. */ + return SEPOL_OK; + } copy_func = &cil_copy_blockabstract; break; case CIL_BLOCKINHERIT: From patchwork Wed Jan 5 21:16:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Carter X-Patchwork-Id: 12704720 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 EE085C433EF for ; Wed, 5 Jan 2022 21:16:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244380AbiAEVQQ (ORCPT ); Wed, 5 Jan 2022 16:16:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50696 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244379AbiAEVQP (ORCPT ); Wed, 5 Jan 2022 16:16:15 -0500 Received: from mail-qk1-x72c.google.com (mail-qk1-x72c.google.com [IPv6:2607:f8b0:4864:20::72c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61F32C061212 for ; Wed, 5 Jan 2022 13:16:15 -0800 (PST) Received: by mail-qk1-x72c.google.com with SMTP id w27so716311qkj.7 for ; Wed, 05 Jan 2022 13:16:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=hnaVrkmlYse3rWDrMGsc1OvrBAsnnFBvXQxoQ0lC+fw=; b=ltHJIKPGN7fKSnDyCpJ15WmpOcy+B0XWBVAwwzDIA9REkbA1+TAUBu4SGWbDNX0ssb yXXuGdPkLoPJi2bCYsOOoEDpPcMS6ffxcRrpWIu+AenswyJYE+xdmBzwVWM59CCVFV9a L9O1n6bI2jnHn4dZSP+f6ZPFNexfBbGIpMmWzhm8qNY1RIYqDYMh+eCFCPyrPaugJRbM S4VoAKew7GW40bwr8hnmu6gJcwkJ/KYZ7Xm+eaR3Tw8510SmIbVUC9tpLo9Ow7eFUzSt dOCo8R28LnrfilIn0nJmQl57JEbO8+1fHxzgtsEneDdpHDqNTqmVSJALTsD/7McvvoOg zizg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=hnaVrkmlYse3rWDrMGsc1OvrBAsnnFBvXQxoQ0lC+fw=; b=A3q01URwJTe+shlybr6BBbamOCG8JaALSJu0LS4VcomxukCRhBjFv+czw0qT+3Cyzt JGeOiAD4khouNXfFRlcNwj7MGQ4xpnhxkNX0SdU8wmNLrvCc9dnY3j4dvcMcaJi24/YD +NodoCo7wXXyBy1BKOk12XfvxN8JSOqm2XeNgtmTPxLHllqg/dK2UFkOzQZTjLwtGLDT gUV0rsdQ2+Ixc6QLz797ros111xW3IINiVqUALOorVK5mwvE9HJF786/NSdauyD2V2iG Z9NWiC3SlUK6LH6lxxv7xzaR2P1tBJzxKC9rMWdV1XdUEDm1X2qRZxyCcXNyI5R0nuQz YUpw== X-Gm-Message-State: AOAM5307KAOiAyY3FvHm6OcM3sxRD/3/NAHBrlwbSnbK5mSvNCQxIdWq za6uzdyYNc8I4KJRYcWhkkhccX/xD58= X-Google-Smtp-Source: ABdhPJxG7v6fLL/uPYbW8gj3+dM8sysmqClYD4d4uCIDg2lpvS2kXBWN/GMX+Sg5Oi+2ZODIIUFBcg== X-Received: by 2002:a05:620a:4107:: with SMTP id j7mr40117042qko.645.1641417373725; Wed, 05 Jan 2022 13:16:13 -0800 (PST) Received: from localhost.localdomain (c-69-250-217-147.hsd1.md.comcast.net. [69.250.217.147]) by smtp.gmail.com with ESMTPSA id m1sm36148502qkn.115.2022.01.05.13.16.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jan 2022 13:16:13 -0800 (PST) From: James Carter To: selinux@vger.kernel.org Cc: James Carter Subject: [PATCH 2/3] libsepol/cil: Mark as abstract all sub-blocks of an abstract block Date: Wed, 5 Jan 2022 16:16:01 -0500 Message-Id: <20220105211602.359300-2-jwcart2@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220105211602.359300-1-jwcart2@gmail.com> References: <20220105211602.359300-1-jwcart2@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org If a block is marked as abstract, then it will be skipped during every pass after blockabstracts are resolved (only tunables, in-befores, and blockinherits are before blockabstracts), so mark all of its sub-blocks as abstract to reflect their actual status. Signed-off-by: James Carter --- libsepol/cil/src/cil_resolve_ast.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/libsepol/cil/src/cil_resolve_ast.c b/libsepol/cil/src/cil_resolve_ast.c index e97a9f46..0288b7dc 100644 --- a/libsepol/cil/src/cil_resolve_ast.c +++ b/libsepol/cil/src/cil_resolve_ast.c @@ -2379,6 +2379,19 @@ exit: return rc; } +static void cil_mark_subtree_abstract(struct cil_tree_node *node) +{ + struct cil_block *block = node->data; + + block->is_abstract = CIL_TRUE; + + for (node = node->cl_head; node; node = node->next) { + if (node->flavor == CIL_BLOCK) { + cil_mark_subtree_abstract(node); + } + } +} + int cil_resolve_blockabstract(struct cil_tree_node *current, void *extra_args) { struct cil_blockabstract *abstract = current->data; @@ -2398,7 +2411,7 @@ int cil_resolve_blockabstract(struct cil_tree_node *current, void *extra_args) goto exit; } - ((struct cil_block*)block_datum)->is_abstract = CIL_TRUE; + cil_mark_subtree_abstract(block_node); return SEPOL_OK; From patchwork Wed Jan 5 21:16:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Carter X-Patchwork-Id: 12704721 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 BA038C433F5 for ; Wed, 5 Jan 2022 21:16:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244379AbiAEVQQ (ORCPT ); Wed, 5 Jan 2022 16:16:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244378AbiAEVQP (ORCPT ); Wed, 5 Jan 2022 16:16:15 -0500 Received: from mail-qk1-x731.google.com (mail-qk1-x731.google.com [IPv6:2607:f8b0:4864:20::731]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 35AC4C061245 for ; Wed, 5 Jan 2022 13:16:15 -0800 (PST) Received: by mail-qk1-x731.google.com with SMTP id m2so711369qkd.8 for ; Wed, 05 Jan 2022 13:16:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=0lMY/kihy1HYhLdDNz3DdawjvP54+9gnZ5vo0C1c+R0=; b=VGj++Ny7aU5NCmp/IrDd4rUFMenFlYBhzpgb8o0Ympy20a44x+fcLMbDlvRytHOKql M/XNclFDI7ptGVqBlUJWoG8SA/LphkWVrUExMK788VYp7Y5kDMDksFmBbB2Q5T5lcNgZ QKxYMrniLZMoMPJ6YtgAAt6BPmun88KgK+saiX9FDYAn+QAmeZq1aZ6HDMF+arDvK8AB ufjhyMwE8dDeKazdQhWqN3ndKPbEyCnoO4WqhjqUQl5FeT0kvWHKktDGke+r48gUzSTL 1hUMLvCzDrg26jG+UJAtt9nqx0tuvIedqsBuHrLu+3svJFyW5Cb1nt90u/8FTbz94Wc8 olow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0lMY/kihy1HYhLdDNz3DdawjvP54+9gnZ5vo0C1c+R0=; b=4cYg0LDLNy6aqM69Cf4H7r+C5SYuaeSOH/jnurLsSilStTEel5XLelk7Gmj+YIeB2c BqZOTGnAiZ7WSPkQogw453o6y3fZTQYCm9ufPo/G4uIvaNT8GWeXQokrESuAEJdpvHoL RIRpK6ZPbsDn0Lj9huVMmcd4wBeKzsRpwBQvysIDi6QvYMZY7auxGt2o14GMQlUs+fM2 mPXAiHS5NraSfspJZKyUuRuHtDX6JBHrKfOYJIkUgKmdiPlaj9H4992BQRQTYSZtUTxO NdQcJvUmbfkF1VBeMtdxUIYSKv79kyTZZe6lNaW7wwHdJSFiXZ313WuIZHcgZgwqK8ji m7LQ== X-Gm-Message-State: AOAM532O3oCCem5jfKfvqDnhC7buTxYpZoXpO1nm3iAmy9tIF7kFg7Sp 0c2lAdV2wfeQBMkY6CCystXey/3xdD8= X-Google-Smtp-Source: ABdhPJy5hzzb7JE3qoWaW1Ol1bbZhD9fWduv4Nlnu0yL4XOwIOTmTR8Og9nr+0gymMkpN/3ebpuPQg== X-Received: by 2002:a05:620a:2905:: with SMTP id m5mr40262929qkp.598.1641417374313; Wed, 05 Jan 2022 13:16:14 -0800 (PST) Received: from localhost.localdomain (c-69-250-217-147.hsd1.md.comcast.net. [69.250.217.147]) by smtp.gmail.com with ESMTPSA id m1sm36148502qkn.115.2022.01.05.13.16.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jan 2022 13:16:13 -0800 (PST) From: James Carter To: selinux@vger.kernel.org Cc: James Carter Subject: [PATCH 3/3] libsepol/cil: Do not resolve names to declarations in abstract blocks Date: Wed, 5 Jan 2022 16:16:02 -0500 Message-Id: <20220105211602.359300-3-jwcart2@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220105211602.359300-1-jwcart2@gmail.com> References: <20220105211602.359300-1-jwcart2@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Since abstract blocks will not appear in the final policy, do not resolve names to a declaration inside one. When resolving blockabstract rules, they must be collected in a list and processed at the end of the pass because if a parent block is marked as abstract, then a blockabstract rule for a sub-block will fail to resolve. Found by oss-fuzz (#42981) Signed-off-by: James Carter --- libsepol/cil/src/cil_resolve_ast.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/libsepol/cil/src/cil_resolve_ast.c b/libsepol/cil/src/cil_resolve_ast.c index 0288b7dc..73115c55 100644 --- a/libsepol/cil/src/cil_resolve_ast.c +++ b/libsepol/cil/src/cil_resolve_ast.c @@ -65,6 +65,7 @@ struct cil_args_resolve { struct cil_list *sensitivityorder_lists; struct cil_list *in_list_before; struct cil_list *in_list_after; + struct cil_list *abstract_blocks; }; static struct cil_name * __cil_insert_name(struct cil_db *db, hashtab_key_t key, struct cil_tree_node *ast_node) @@ -2397,6 +2398,7 @@ int cil_resolve_blockabstract(struct cil_tree_node *current, void *extra_args) struct cil_blockabstract *abstract = current->data; struct cil_symtab_datum *block_datum = NULL; struct cil_tree_node *block_node = NULL; + struct cil_args_resolve *args = extra_args; int rc = SEPOL_ERR; rc = cil_resolve_name(current, abstract->block_str, CIL_SYM_BLOCKS, extra_args, &block_datum); @@ -2411,7 +2413,7 @@ int cil_resolve_blockabstract(struct cil_tree_node *current, void *extra_args) goto exit; } - cil_mark_subtree_abstract(block_node); + cil_list_append(args->abstract_blocks, CIL_NODE, block_node); return SEPOL_OK; @@ -4097,6 +4099,7 @@ int cil_resolve_ast(struct cil_db *db, struct cil_tree_node *current) extra_args.sensitivityorder_lists = NULL; extra_args.in_list_before = NULL; extra_args.in_list_after = NULL; + extra_args.abstract_blocks = NULL; cil_list_init(&extra_args.to_destroy, CIL_NODE); cil_list_init(&extra_args.sidorder_lists, CIL_LIST_ITEM); @@ -4106,6 +4109,7 @@ int cil_resolve_ast(struct cil_db *db, struct cil_tree_node *current) cil_list_init(&extra_args.sensitivityorder_lists, CIL_LIST_ITEM); cil_list_init(&extra_args.in_list_before, CIL_IN); cil_list_init(&extra_args.in_list_after, CIL_IN); + cil_list_init(&extra_args.abstract_blocks, CIL_NODE); for (pass = CIL_PASS_TIF; pass < CIL_PASS_NUM; pass++) { extra_args.pass = pass; @@ -4129,6 +4133,13 @@ int cil_resolve_ast(struct cil_db *db, struct cil_tree_node *current) cil_list_destroy(&extra_args.in_list_after, CIL_FALSE); } + if (pass == CIL_PASS_BLKABS) { + struct cil_list_item *item; + cil_list_for_each(item, extra_args.abstract_blocks) { + cil_mark_subtree_abstract(item->data); + } + } + if (pass == CIL_PASS_BLKIN_LINK) { rc = cil_check_for_bad_inheritance(current); if (rc != SEPOL_OK) { @@ -4247,6 +4258,7 @@ exit: cil_list_destroy(&extra_args.to_destroy, CIL_FALSE); cil_list_destroy(&extra_args.in_list_before, CIL_FALSE); cil_list_destroy(&extra_args.in_list_after, CIL_FALSE); + cil_list_destroy(&extra_args.abstract_blocks, CIL_FALSE); return rc; } @@ -4268,9 +4280,13 @@ static int __cil_resolve_name_with_parents(struct cil_tree_node *node, char *nam case CIL_ROOT: goto exit; break; - case CIL_BLOCK: - symtab = &((struct cil_block*)node->data)->symtab[sym_index]; - rc = cil_symtab_get_datum(symtab, name, datum); + case CIL_BLOCK: { + struct cil_block *block = node->data; + if (!block->is_abstract) { + symtab = &block->symtab[sym_index]; + rc = cil_symtab_get_datum(symtab, name, datum); + } + } break; case CIL_BLOCKINHERIT: { struct cil_blockinherit *inherit = node->data;