From patchwork Mon Aug 16 19:57:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Carter X-Patchwork-Id: 12439179 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 DDBA0C4320A for ; Mon, 16 Aug 2021 19:58:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C875260F38 for ; Mon, 16 Aug 2021 19:58:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231243AbhHPT6d (ORCPT ); Mon, 16 Aug 2021 15:58:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56746 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229802AbhHPT6b (ORCPT ); Mon, 16 Aug 2021 15:58:31 -0400 Received: from mail-qv1-xf2a.google.com (mail-qv1-xf2a.google.com [IPv6:2607:f8b0:4864:20::f2a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B670C0613C1 for ; Mon, 16 Aug 2021 12:57:59 -0700 (PDT) Received: by mail-qv1-xf2a.google.com with SMTP id g11so1185066qvd.2 for ; Mon, 16 Aug 2021 12:57:59 -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=xpxPrrTiM5SRC2n12ZPGSGD0+g4rIv6oPky8lykdC70=; b=um9hgn/4nnAcXov+PAuHQUb7rvIICe/CFnsMFz9Nrt1Z7y3voBRUu6y2V1icnvFB49 xUV3Hm41r0MNLMPrDuzRiEJISr4YQVy7qL/EwZTPVs07iOaDnXJ7RZlb69pT9+6rnjML wykkvaGCUkrgWnTc3ixtmTkSXSRVayTtb6zOoKFBvvO4BoqLW7yrQDbecwXVTuCT9YXE 25OJWFt4BgQNqOmuOzKoIo2T+sojWqONePkReKIXyMm/enf1nfW3WzvYe/N75DqtDL9W PlKM0m6vJOE9nWdErxvhrO4Z1MZGeEWklrUWLRFjpzbrvfePMXSutN3Q5ZxqZwnhCOCC T3tA== 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=xpxPrrTiM5SRC2n12ZPGSGD0+g4rIv6oPky8lykdC70=; b=accgTQz+nSqTsPevwLa2WldbkmlyvHJYBIdAK62rgbRFP9qkRsZ0lLQdjCyJx8W85O ABEIgIuyHyJmvxgEuisV/xSIml3yF8YBHIopA3AJvXG70IckdTO8JPurgrvWaxmwpAdj shao28FrPaSUuyou4aUAYuf2l6DFSqYNbyU04J+StOGW/rQzM6CQCZglgo+OfWEdqA9M bZuh9LVymxvi9jv4q6Zfpl8Dct3A+B7tlUul0036Iz4r0ufvjkVHGbQjYHD8ag51eh36 5hE3oWQn0HtDOjBBuDBOE/mYijAh0Pao4Wt9GLZIk1pJpeFy5QljYqz+cYjaU0/FVqBS X8Tg== X-Gm-Message-State: AOAM533aXKbqVniDjTixDqL2D40pRm2dBpwVMbHFxZdnBA0v9hf5JeuD S8Vb5wU1U3BFWmud03ogRSLIYV52UnlIIA== X-Google-Smtp-Source: ABdhPJyTZhi9dvdVK2lP/kGKklw4Eh3Q+9pZI+zJ75NDLyxFFBL00xjpYGAD0F2xVVqaRt1fw2RQoQ== X-Received: by 2002:a0c:fe6a:: with SMTP id b10mr480956qvv.6.1629143878102; Mon, 16 Aug 2021 12:57:58 -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 f11sm135147qtf.45.2021.08.16.12.57.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Aug 2021 12:57:57 -0700 (PDT) From: James Carter To: selinux@vger.kernel.org Cc: nicolas.iooss@m4x.org, James Carter Subject: [PATCH 1/7 v2] libsepol/cil: Check syntax of src_info statement Date: Mon, 16 Aug 2021 15:57:46 -0400 Message-Id: <20210816195752.923028-2-jwcart2@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210816195752.923028-1-jwcart2@gmail.com> References: <20210816195752.923028-1-jwcart2@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Every rule other than src_info has their syntax checked when building the AST. It wasn't considered necessary for src_info rules because they were expected to always be generated by the parser and aren't part of the CIL language. But there is no check preventing them from occurring in a policy and the secilc fuzzer found some bugs by using src_info rules in a policy. This caused some syntax checking to be added. Since the parse AST from secil2tree will contain src_info rules and since the goal is to be able to compile the output of secil2tree, it makes sense to check the syntax of src_info rules in the same way that all of the other rules are checked. Check the syntax of src_info statements in the same way every other rule is checked. Signed-off-by: James Carter --- libsepol/cil/src/cil_build_ast.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/libsepol/cil/src/cil_build_ast.c b/libsepol/cil/src/cil_build_ast.c index 9da90883..5e65a266 100644 --- a/libsepol/cil/src/cil_build_ast.c +++ b/libsepol/cil/src/cil_build_ast.c @@ -6075,12 +6075,24 @@ void cil_destroy_mls(struct cil_mls *mls) int cil_gen_src_info(struct cil_tree_node *parse_current, struct cil_tree_node *ast_node) { - /* No need to check syntax, because this is auto generated */ + int rc = SEPOL_ERR; + enum cil_syntax syntax[] = { + CIL_SYN_STRING, + CIL_SYN_STRING, + CIL_SYN_STRING, + CIL_SYN_N_LISTS | CIL_SYN_END, + CIL_SYN_END + }; + int syntax_len = sizeof(syntax)/sizeof(*syntax); struct cil_src_info *info = NULL; - if (parse_current->next == NULL || parse_current->next->next == NULL) { - cil_tree_log(parse_current, CIL_ERR, "Bad "); - return SEPOL_ERR; + if (parse_current == NULL || ast_node == NULL) { + goto exit; + } + + rc = __cil_verify_syntax(parse_current, syntax, syntax_len); + if (rc != SEPOL_OK) { + goto exit; } cil_src_info_init(&info); @@ -6092,6 +6104,10 @@ int cil_gen_src_info(struct cil_tree_node *parse_current, struct cil_tree_node * ast_node->flavor = CIL_SRC_INFO; return SEPOL_OK; + +exit: + cil_tree_log(parse_current, CIL_ERR, "Bad src info"); + return rc; } void cil_destroy_src_info(struct cil_src_info *info)