From patchwork Fri Aug 27 14:19:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Carter X-Patchwork-Id: 12462187 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT 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 98EFBC432BE for ; Fri, 27 Aug 2021 14:20:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7D9D760FE6 for ; Fri, 27 Aug 2021 14:20:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233058AbhH0OUv (ORCPT ); Fri, 27 Aug 2021 10:20:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232641AbhH0OUv (ORCPT ); Fri, 27 Aug 2021 10:20:51 -0400 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 654DEC061757 for ; Fri, 27 Aug 2021 07:20:02 -0700 (PDT) Received: by mail-qv1-xf2f.google.com with SMTP id v1so4047028qva.7 for ; Fri, 27 Aug 2021 07:20:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=y26Aws6m3e9NWvqT3/fXTYabi1JAM0bu5hsehcBemjQ=; b=TNXf9FCu4Lf+eGJGhXv+j8d2Coo/BEWkkfPjmtzD25eXEn/sKifhfFzgdTdD1CXSXj czidRMjQu6aeOBAUVTv6agCax1Y4AexZnXNC6qCB/3He922A3N+Eu2IPAZYMt8vuIeMo Z0ajeLsb6YLXh4ViVh+SHYrcM+0YtFUbiVt1FKdMuVWeraXtwVojqPLO4NXpYIrUs2Sd urtIdB24WqdLvdc/eCJhlTOOnAkYw10aLNQLbhK4MtOxTw4/pHPJBKGgVDNwW9Pd5Pyd LydIM9h84HsR3Vr9N1rDo/sixdg+FEEDsa68xCPRn/3p2sbXjUDXVHcTp8qyjqOsieNH eGKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=y26Aws6m3e9NWvqT3/fXTYabi1JAM0bu5hsehcBemjQ=; b=tkhERjO3dO08we/JvC/PYYkkbOv2Be/1lFI8oHq7kvWxQFkB7bsSqFM9bjXd6N0x03 eKOTQmVyJpP6bnvBiNvZCchyo5fPTmgRmnGGaDHEq5QJasVdORtE8HOqunQECnx632Uc yhAAZ5asrXY3F8FKA7FUzGwsv1LOXGgSb4hti6Kl5xpjOzuDIRjQp2lFzPsLVMiLfc+6 Kc9+f1egloCQceQ3IkoF5pJaxWjYCssx5yabyKSOR+5qt79sFkS7aIFL0/Vl5i1txBY1 QRuN/gHoT/m7t5U5MljWFdYVYRjtxwFgiCImkR2USrrQVjquegewG8vgBJnkx2ibPvIT fPig== X-Gm-Message-State: AOAM5320JEOR7IifXeEycoeHNo9D235U8GibmRjZIY20/kw/G4k1seu/ 5sKWbNok5zbhXui0xtXvhpanxI+okZI= X-Google-Smtp-Source: ABdhPJwmPmIwq3N0of35NQOoEYnNonoW+PAvFTyvaQmDaxVq431zQkRNgrJNUvgxWA7CE0FXyFRHnQ== X-Received: by 2002:a0c:f9d2:: with SMTP id j18mr9529515qvo.49.1630074001436; Fri, 27 Aug 2021 07:20:01 -0700 (PDT) Received: from localhost.localdomain (c-73-200-157-122.hsd1.md.comcast.net. [73.200.157.122]) by smtp.gmail.com with ESMTPSA id s10sm4891010qko.134.2021.08.27.07.20.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Aug 2021 07:20:01 -0700 (PDT) From: James Carter To: selinux@vger.kernel.org Cc: nicolas.iooss@m4x.org, James Carter Subject: [PATCH 1/3] libsepol/cil: Properly check parse tree when printing error messages Date: Fri, 27 Aug 2021 10:19:53 -0400 Message-Id: <20210827141955.370777-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 The function cil_tree_get_next_path() does not check whether the parse tree node that stores the high-level language file path of a src_info rule actually exists before trying to read the path. This can result in a NULL dereference. Check that all of the parse tree nodes of a src_info rule exist before reading the data from them. This bug was found by the secilc-fuzzer. Signed-off-by: James Carter Acked-by: Nicolas Iooss --- libsepol/cil/src/cil_tree.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libsepol/cil/src/cil_tree.c b/libsepol/cil/src/cil_tree.c index 75293005..e70722ec 100644 --- a/libsepol/cil/src/cil_tree.c +++ b/libsepol/cil/src/cil_tree.c @@ -62,7 +62,10 @@ struct cil_tree_node *cil_tree_get_next_path(struct cil_tree_node *node, char ** while (node) { if (node->flavor == CIL_NODE && node->data == NULL) { - if (node->cl_head->data == CIL_KEY_SRC_INFO && node->cl_head->next != NULL && node->cl_head->next->next != NULL) { + if (node->cl_head && node->cl_head->data == CIL_KEY_SRC_INFO) { + if (!node->cl_head->next || !node->cl_head->next->next || !node->cl_head->next->next->next) { + goto exit; + } /* Parse Tree */ *info_kind = node->cl_head->next->data; rc = cil_string_to_uint32(node->cl_head->next->next->data, hll_line, 10); From patchwork Fri Aug 27 14:19:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Carter X-Patchwork-Id: 12462189 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT 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 3F913C4320A for ; Fri, 27 Aug 2021 14:20:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 219D860FDA for ; Fri, 27 Aug 2021 14:20:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232641AbhH0OUv (ORCPT ); Fri, 27 Aug 2021 10:20:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46638 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234232AbhH0OUv (ORCPT ); Fri, 27 Aug 2021 10:20:51 -0400 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 C60C5C061757 for ; Fri, 27 Aug 2021 07:20:02 -0700 (PDT) Received: by mail-qk1-x731.google.com with SMTP id 14so7322231qkc.4 for ; Fri, 27 Aug 2021 07:20:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=08dmOMw+jLvaWpCnotK1VjbqmE8K6hSH8bHbH4Eq2lQ=; b=AviyqiIOniFzwcJ76e4GFh14/yA180uejvgYEkm5c4YY6gSXYzKThrOIcz/sGKsGn7 ttn0QPK8xa4fcYwKV7E8k3l7uWZ2ZkSwKHdWX8m3gP/yBlfSq/v7fgj3iTtvC8IQ5whB aKdSBv1z/pyXbxpjv6g4MNa834MFS6OnDOYLSwDkXrOB/ig5dJZe2cTcPeFIqcU5H7Pe FGlRY/w17HA/DGEgoqQM/Kk1mnQ+fLcDmG3FsqqngsMwOHVasE7MnXL7NZebs5taCFut xJVDxoMBjgvRtzKCs+CXhIsF3AtHoafQ+msrZFNFdC0ZYLVEbTaQPCKbCiLhNBHn+hM7 nPmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=08dmOMw+jLvaWpCnotK1VjbqmE8K6hSH8bHbH4Eq2lQ=; b=Ws5JzOZfUHhP+ZR5Ayah7Y+he58/tBNmTX8mtRfCjsCGeYMRqnQB9Hf8KUypCNzYxN 1/SqUId/mN3uLwjzmeLPn9yp/Pdd+BDTEhJRngfknfMeGUilFH8lHRbzfD8FOcJgQYVr F91Q7yHuIzLdLGOGSlj7cP61kUzbUtAPklVL6Md4dfw3mdW+fEoBRC6dGvCKy72AbtTt Rk2nnlQV8C1LxTHqnbVy/6utpobk8YmcjKJ0D4bxWpJ8U8jc+b6gYhFqwHS8Q3XFFC/z xnXXG3B/dWxkCpaRfXfN4ahn4jSBFFjQDGRoMxDmuZc12Qyapbtyu5Sa+GX6tx+QyCLS jOIA== X-Gm-Message-State: AOAM5337xG1GrjjztqD5gg+Pm7e9oZc9GajrD8dL9TNUclnMVN48/O5U c7IWsM6yAUlaLmXdAREC5Jk+srRLXuU= X-Google-Smtp-Source: ABdhPJxOLgborGUU++yTIEwilWouKQ1Lq4ITZ5PDZvHQ5TYm35/OcG93y2a8REHcgnNZpjFKzvZP7Q== X-Received: by 2002:a37:9085:: with SMTP id s127mr8971908qkd.215.1630074001911; Fri, 27 Aug 2021 07:20:01 -0700 (PDT) Received: from localhost.localdomain (c-73-200-157-122.hsd1.md.comcast.net. [73.200.157.122]) by smtp.gmail.com with ESMTPSA id s10sm4891010qko.134.2021.08.27.07.20.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Aug 2021 07:20:01 -0700 (PDT) From: James Carter To: selinux@vger.kernel.org Cc: nicolas.iooss@m4x.org, James Carter Subject: [PATCH 2/3] libsepol/cil: Reset expandtypeattribute rules when resetting AST Date: Fri, 27 Aug 2021 10:19:54 -0400 Message-Id: <20210827141955.370777-2-jwcart2@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210827141955.370777-1-jwcart2@gmail.com> References: <20210827141955.370777-1-jwcart2@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org A list is created to store type attribute datums when resolving an expandtypeattribute rule and that list needs to be destroyed if the AST is reset or a memory leak will occur. Destroy the list storing type attributes datums when resetting expandtypeattribute rules. This bug was found by the secilc-fuzzer. Signed-off-by: James Carter --- libsepol/cil/src/cil_reset_ast.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/libsepol/cil/src/cil_reset_ast.c b/libsepol/cil/src/cil_reset_ast.c index 6d1d2da7..0ba075c8 100644 --- a/libsepol/cil/src/cil_reset_ast.c +++ b/libsepol/cil/src/cil_reset_ast.c @@ -208,6 +208,11 @@ static void cil_reset_typeattributeset(struct cil_typeattributeset *tas) cil_list_destroy(&tas->datum_expr, CIL_FALSE); } +static void cil_reset_expandtypeattribute(struct cil_expandtypeattribute *expandattr) +{ + cil_list_destroy(&expandattr->attr_datums, CIL_FALSE); +} + static void cil_reset_avrule(struct cil_avrule *rule) { cil_reset_classperms_list(rule->perms.classperms); @@ -531,6 +536,9 @@ int __cil_reset_node(struct cil_tree_node *node, __attribute__((unused)) uint32 case CIL_TYPEATTRIBUTESET: cil_reset_typeattributeset(node->data); break; + case CIL_EXPANDTYPEATTRIBUTE: + cil_reset_expandtypeattribute(node->data); + break; case CIL_RANGETRANSITION: cil_reset_rangetransition(node->data); break; @@ -630,7 +638,6 @@ int __cil_reset_node(struct cil_tree_node *node, __attribute__((unused)) uint32 case CIL_CLASSORDER: case CIL_CATORDER: case CIL_SENSITIVITYORDER: - case CIL_EXPANDTYPEATTRIBUTE: break; /* Nothing to reset */ default: break; From patchwork Fri Aug 27 14:19:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Carter X-Patchwork-Id: 12462191 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT 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 4EB7DC43214 for ; Fri, 27 Aug 2021 14:20:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3156060FDA for ; Fri, 27 Aug 2021 14:20:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235864AbhH0OUx (ORCPT ); Fri, 27 Aug 2021 10:20:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46642 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234232AbhH0OUw (ORCPT ); Fri, 27 Aug 2021 10:20:52 -0400 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 8EB03C061757 for ; Fri, 27 Aug 2021 07:20:03 -0700 (PDT) Received: by mail-qk1-x736.google.com with SMTP id y144so7309960qkb.6 for ; Fri, 27 Aug 2021 07:20:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=+VrztBh94lm6Mkk1yWNlZAeYfLYq5UG9Gk8WKSkeYU8=; b=UnRtQRohmWBwEbhtkZ9lXkGrfmaZ1zsLOl8rBux8gXoBdpafFh6RAJlsvRXJO+CaWp fnLX1bWeO3i8MUgBihbkBCUeaY4e1zpogHeTqQfaIcPK/DmtLCC7EiLt9tkU4fRtBWKb anX1lDdJWLV4mRWq/RjVSWXnx9InxWchyLTlXpIO8f7sa7sChS++tukd9mXOWyLcJosI mk9f6X9k2ycwdQWyftitb4cAdaNR/Bg88NQR8/eQmTPU0bChF+WACzsb698DYdjZo5rl TSshKmgmsqwkmNsJypCnOWFg9ItFIM9ZgZtXYCrfPF7yPyv1SrT3yUffE5HyH/oCLoVw jTnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+VrztBh94lm6Mkk1yWNlZAeYfLYq5UG9Gk8WKSkeYU8=; b=QfFMbtlp8KomZsZt2WGfnRrw1iJMUoxSwaKmzL7lP8xp5R5UcyTxcGUocpboDTXNwN 8XpbUvbFlIvuf6Yj1rdEsKvXLCTS/tjOJnxTnF+qmo7yjebXizucDT348VZHA4jPhJJW naiFfXkOoxaseIHr9+/zDNc3uyL/m3Y/P4ZwlsAJG16yeCEvcDnRv4IEabziSJ2S+CFB uML1iv512nzjwsn0OGtnpwg1Ox2M1oUm8Dz++KltaVv6+sZlYWjXZ9sBpnaLoL2K9oDF hVjwnuqtHbhhqNI3ulx6waEs67GDzXUo/Qxw8QWwVKwBq/IRn76KCbUVT+oZnrZycN6s hyMQ== X-Gm-Message-State: AOAM531dwm3MwDYE/FXnMvm/qOcYyheYQ5n4GCTOKHw7ZqX2cRGn2rX6 eLgGZqyy37Ewzm7YckQrJ7+2IcNlu8k= X-Google-Smtp-Source: ABdhPJz6VWm8haX7FW03ko8T67UWBXG1wP2204ANlg5ufdW7W6hcvg0F85BGIsaghsa0MekQdVKLCA== X-Received: by 2002:a05:620a:4cf:: with SMTP id 15mr9359727qks.316.1630074002657; Fri, 27 Aug 2021 07:20:02 -0700 (PDT) Received: from localhost.localdomain (c-73-200-157-122.hsd1.md.comcast.net. [73.200.157.122]) by smtp.gmail.com with ESMTPSA id s10sm4891010qko.134.2021.08.27.07.20.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Aug 2021 07:20:02 -0700 (PDT) From: James Carter To: selinux@vger.kernel.org Cc: nicolas.iooss@m4x.org, James Carter Subject: [PATCH 3/3] libsepol/cil: Properly check for parameter when inserting name Date: Fri, 27 Aug 2021 10:19:55 -0400 Message-Id: <20210827141955.370777-3-jwcart2@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210827141955.370777-1-jwcart2@gmail.com> References: <20210827141955.370777-1-jwcart2@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org File names for typetransition rules are stored in their own datums. This allows them to be passed as a parameter, but there needs to be a check in __cil_insert_name() so that parameter names are not mistaken for file name strings. This check did not verify that a matching parameter name had the flavor of CIL_NAME. Check that the parameter flavor is CIL_NAME and that the paramter name matches the file name to be stored in the datum. This bug was found by the secilc-fuzzer. Signed-off-by: James Carter --- libsepol/cil/src/cil_resolve_ast.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libsepol/cil/src/cil_resolve_ast.c b/libsepol/cil/src/cil_resolve_ast.c index 18007324..a4de1c75 100644 --- a/libsepol/cil/src/cil_resolve_ast.c +++ b/libsepol/cil/src/cil_resolve_ast.c @@ -87,7 +87,8 @@ static struct cil_name * __cil_insert_name(struct cil_db *db, hashtab_key_t key, if (macro != NULL && macro->params != NULL) { struct cil_list_item *item; cil_list_for_each(item, macro->params) { - if (((struct cil_param*)item->data)->str == key) { + struct cil_param *param = item->data; + if (param->flavor == CIL_NAME && param->str == key) { return NULL; } }