From patchwork Fri Nov 3 18:26:35 2023 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: 13444891 X-Patchwork-Delegate: plautrba@redhat.com 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 B3D46C4167D for ; Fri, 3 Nov 2023 18:27:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234182AbjKCS1H (ORCPT ); Fri, 3 Nov 2023 14:27:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57066 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234129AbjKCS1F (ORCPT ); Fri, 3 Nov 2023 14:27:05 -0400 Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9D972D78 for ; Fri, 3 Nov 2023 11:26:44 -0700 (PDT) Received: by mail-ed1-x52d.google.com with SMTP id 4fb4d7f45d1cf-53d9f001b35so3963275a12.2 for ; Fri, 03 Nov 2023 11:26:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20230601; t=1699036003; x=1699640803; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=GI6urP8avQHI9OnbekJVeVyZjxhxcug7LrgeMj4M0W8=; b=ku8lcThRL99sdz4attBOYgbiQh22ZR5MOmtsKlyfmS0DooJDf20RHdvDOVfiQXxeFu wfmFpMCdA/0xfTNhHEH5DBz0FcXP8N5FsajiEJGFG26nudM4XBejEsSrx14OX53Zothc X0LsxCF2bXGD7lQMENBjL2u/Fw8EP4E+WQcKaweahTAYnGau40w78mfXl6fRnhqaxzxm TzKQINmWs/iLyyFWFD7DjXT/9a3vkQvbCsVQDfD2p9P4claOhLsqQ+14H5271RB14XeF UgTFRWOvOMgMDYP7sFYyb5oJTDsJQq5Wu4bJ3yiwByGqbkuHJ+hDzaUyMMl39MokJck+ fylA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699036003; x=1699640803; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GI6urP8avQHI9OnbekJVeVyZjxhxcug7LrgeMj4M0W8=; b=BqxeXYY3I4iEZACm8ZiVKaH0XzZFzZdtA+OUMdkggaSnh2SlvkaMYU0uAvRGmC9yE5 T9NAGbZ3I0KInqKZwblQIcjWkBUXDUUwnUTZQp0MlK84dMi5s0lU1Xx/CfCEOlB2sW/L sSqL246Tbrmu/ZBTNb/UjbEI+guBnh78mXUD1FPBdbmckJUjGrPHFO2v9IG2Rolwegd4 xcs4mTkno86UVoevtsBMstvQlrdLlggVMz9trqiKfowK+3MGjlweslcP9GuiENO8tb6Q EsppcmGQKR2nbQWGmVC606a7rW7+uHqHaNBdkavOAz5YfqhQWcFzvF1GCwFA4pItqE0b sXPA== X-Gm-Message-State: AOJu0YwAKDkxPUPebOwsbed85tHkZxGVU1RmSL3i9iFqlySqlREGs7pi kuXxB/RKlLrPhGpouQ/R+aBU6U4k/+4= X-Google-Smtp-Source: AGHT+IFNYA/g/IL6gtXnN9/G26R8e1MoFiQ2Y41S5eIENLiOTI1cL0/Z9h6SZgeJexPRijKYWWNGYA== X-Received: by 2002:a50:c351:0:b0:543:7256:6370 with SMTP id q17-20020a50c351000000b0054372566370mr9693208edb.23.1699036002789; Fri, 03 Nov 2023 11:26:42 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-077-003-098-230.77.3.pool.telefonica.de. [77.3.98.230]) by smtp.gmail.com with ESMTPSA id v10-20020aa7dbca000000b0052ff9bae873sm1210275edt.5.2023.11.03.11.26.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 11:26:42 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [PATCH v2 1/3] libsepol: validate default type of transition is not an attribute Date: Fri, 3 Nov 2023 19:26:35 +0100 Message-ID: <20231103182637.85003-1-cgzones@googlemail.com> X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org The default type of a type transition must be a regular type, not an attribute. Signed-off-by: Christian Göttsche Acked-by: James Carter --- v2: fix build error by supplying necessary arguments to validate_simpletype() --- libsepol/src/policydb_validate.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/libsepol/src/policydb_validate.c b/libsepol/src/policydb_validate.c index 892a0ffd..810c3263 100644 --- a/libsepol/src/policydb_validate.c +++ b/libsepol/src/policydb_validate.c @@ -468,7 +468,7 @@ static int validate_role_datum_wrapper(__attribute__((unused)) hashtab_key_t k, return validate_role_datum(margs->handle, d, margs->flavors); } -static int validate_simpletype(uint32_t value, const policydb_t *p, validate_t flavors[]) +static int validate_simpletype(uint32_t value, const policydb_t *p, const validate_t flavors[SYM_NUM]) { const type_datum_t *type; @@ -1094,7 +1094,9 @@ static int validate_filename_trans(hashtab_key_t k, hashtab_datum_t d, void *arg { const filename_trans_key_t *ftk = (filename_trans_key_t *)k; const filename_trans_datum_t *ftd = d; - validate_t *flavors = (validate_t *)args; + const map_arg_t *margs = args; + const validate_t *flavors = margs->flavors; + const policydb_t *p = margs->policy; if (validate_value(ftk->ttype, &flavors[SYM_TYPES])) goto bad; @@ -1105,7 +1107,7 @@ static int validate_filename_trans(hashtab_key_t k, hashtab_datum_t d, void *arg for (; ftd; ftd = ftd->next) { if (validate_ebitmap(&ftd->stypes, &flavors[SYM_TYPES])) goto bad; - if (validate_value(ftd->otype, &flavors[SYM_TYPES])) + if (validate_simpletype(ftd->otype, p, flavors)) goto bad; } @@ -1115,9 +1117,11 @@ bad: return -1; } -static int validate_filename_trans_hashtab(sepol_handle_t *handle, hashtab_t filename_trans, validate_t flavors[]) +static int validate_filename_trans_hashtab(sepol_handle_t *handle, const policydb_t *p, validate_t flavors[]) { - if (hashtab_map(filename_trans, validate_filename_trans, flavors)) { + map_arg_t margs = { flavors, handle, p }; + + if (hashtab_map(p->filename_trans, validate_filename_trans, &margs)) { ERR(handle, "Invalid filename trans"); return -1; } @@ -1555,7 +1559,7 @@ int policydb_validate(sepol_handle_t *handle, const policydb_t *p) if (validate_role_allows(handle, p->role_allow, flavors)) goto bad; if (p->policyvers >= POLICYDB_VERSION_FILENAME_TRANS) - if (validate_filename_trans_hashtab(handle, p->filename_trans, flavors)) + if (validate_filename_trans_hashtab(handle, p, flavors)) goto bad; } else { if (validate_avrule_blocks(handle, p->global, p, flavors)) From patchwork Fri Nov 3 18:26:36 2023 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: 13444890 X-Patchwork-Delegate: plautrba@redhat.com 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 36E0FC4332F for ; Fri, 3 Nov 2023 18:27:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230242AbjKCS1G (ORCPT ); Fri, 3 Nov 2023 14:27:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57074 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234105AbjKCS1F (ORCPT ); Fri, 3 Nov 2023 14:27:05 -0400 Received: from mail-lf1-x135.google.com (mail-lf1-x135.google.com [IPv6:2a00:1450:4864:20::135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C701D7A for ; Fri, 3 Nov 2023 11:26:45 -0700 (PDT) Received: by mail-lf1-x135.google.com with SMTP id 2adb3069b0e04-507b96095abso2936919e87.3 for ; Fri, 03 Nov 2023 11:26:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20230601; t=1699036004; x=1699640804; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=4pURf0alLN08ixOS1VV1i7eCNrZbW+l/zjaCePixsfo=; b=GrL9E820cRjzcdjKOd4x+aORjLMWFzSyxxzY6drHzm0Ux6GuA5dMgxh/ghU46HEeZv m8qR+8VNZ8PjrILos/qqq17mg/jSKffySy4RgmoAGV7N2tgDe4Z9HLfy1qfYgE52hZZn PinfOq1bNFJUE9k6zvO3YvWWug9JazJvVO7KQ8QjWcNp814z14ArzLJEOEasPl7Ez/bd 4FnDa+NPJH/+zt2q2E9C/N1pFb+uXdUAhli/Mx0r9etx7qAnu7vRy+Xoom9NnpW/nNfK ti5BFdrO2hZ2cGUy4hGaf7FyWlaMvgPK4cHQ+l4bNi4Yq6uBOXfKEo60K0UzmAycL2Ad iajA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699036004; x=1699640804; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4pURf0alLN08ixOS1VV1i7eCNrZbW+l/zjaCePixsfo=; b=j6oV6TzH0FVPnzPdtrPYW1Lc+FmLtDSdxUHmIS+JnVUl3mfLeuSyUrfubmjY9VZXP1 MuIHJIESdD+jlggFov5hsQ9Obp2MTCsxxavlrsRF2hKA6mofuLCU977IyAZFtOWW2ASo UFsfHtaE4gT906M7Zaxagm6My+KIHUYup3NNIINj3yi9+ZlmLB5lBKYEE67YF4xXwO1D ItzwsHg+1njEPCUuWW0Qpe5hUDLcrs55s+9K7+nbnM8u/AfVnSql0HYwXq47HWqc5p0z hJvQQIMC3g5BCr64t4u+1cuA2gjMrfWicNtL0neqj58EXWzeLytbvhOrgiRuE01QM7jK 56Xw== X-Gm-Message-State: AOJu0YzmLDehU6/nFQhH6BzsR1+2Rns4bxHr7b7Y1ERto6fCMtCeB0oH DnLgP9t0+sAKAM56qOgCaK3hGXLRodY= X-Google-Smtp-Source: AGHT+IFYtN+RjAJd0cGdfXjDfy0knpTlSgLhzTZCukhYQZuzmUjYWZ03vJ2QKDH8ADTb0QINNZ71QA== X-Received: by 2002:a19:f007:0:b0:504:7ff8:3430 with SMTP id p7-20020a19f007000000b005047ff83430mr15726624lfc.10.1699036003482; Fri, 03 Nov 2023 11:26:43 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-077-003-098-230.77.3.pool.telefonica.de. [77.3.98.230]) by smtp.gmail.com with ESMTPSA id v10-20020aa7dbca000000b0052ff9bae873sm1210275edt.5.2023.11.03.11.26.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 11:26:43 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [PATCH v2 2/3] libsepol: validate constraint depth Date: Fri, 3 Nov 2023 19:26:36 +0100 Message-ID: <20231103182637.85003-2-cgzones@googlemail.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231103182637.85003-1-cgzones@googlemail.com> References: <20231103182637.85003-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Ensure constraint expressions are complete and do not exceed the supported depth limit. Signed-off-by: Christian Göttsche Acked-by: James Carter --- libsepol/src/policydb_validate.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/libsepol/src/policydb_validate.c b/libsepol/src/policydb_validate.c index 810c3263..b20ed579 100644 --- a/libsepol/src/policydb_validate.c +++ b/libsepol/src/policydb_validate.c @@ -228,6 +228,7 @@ bad: static int validate_constraint_nodes(sepol_handle_t *handle, unsigned int nperms, const constraint_node_t *cons, validate_t flavors[]) { const constraint_expr_t *cexp; + int depth; for (; cons; cons = cons->next) { if (nperms == 0 && cons->permissions != 0) @@ -240,8 +241,14 @@ static int validate_constraint_nodes(sepol_handle_t *handle, unsigned int nperms if (!cons->expr) goto bad; + depth = -1; + for (cexp = cons->expr; cexp; cexp = cexp->next) { if (cexp->expr_type == CEXPR_NAMES) { + if (depth >= (CEXPR_MAXDEPTH - 1)) + goto bad; + depth++; + if (cexp->attr & CEXPR_XTARGET && nperms != 0) goto bad; if (!(cexp->attr & CEXPR_TYPE)) { @@ -282,6 +289,10 @@ static int validate_constraint_nodes(sepol_handle_t *handle, unsigned int nperms goto bad; } } else if (cexp->expr_type == CEXPR_ATTR) { + if (depth >= (CEXPR_MAXDEPTH - 1)) + goto bad; + depth++; + if (!ebitmap_is_empty(&cexp->names)) goto bad; if (validate_empty_type_set(cexp->type_names)) @@ -318,8 +329,14 @@ static int validate_constraint_nodes(sepol_handle_t *handle, unsigned int nperms } else { switch (cexp->expr_type) { case CEXPR_NOT: + if (depth < 0) + goto bad; + break; case CEXPR_AND: case CEXPR_OR: + if (depth < 1) + goto bad; + depth--; break; default: goto bad; @@ -335,6 +352,9 @@ static int validate_constraint_nodes(sepol_handle_t *handle, unsigned int nperms goto bad; } } + + if (depth != 0) + goto bad; } return 0; From patchwork Fri Nov 3 18:26:37 2023 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: 13444892 X-Patchwork-Delegate: plautrba@redhat.com 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 0153EC4167B for ; Fri, 3 Nov 2023 18:27:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230265AbjKCS1I (ORCPT ); Fri, 3 Nov 2023 14:27:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57106 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234169AbjKCS1G (ORCPT ); Fri, 3 Nov 2023 14:27:06 -0400 Received: from mail-lj1-x229.google.com (mail-lj1-x229.google.com [IPv6:2a00:1450:4864:20::229]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11D3FD7B for ; Fri, 3 Nov 2023 11:26:46 -0700 (PDT) Received: by mail-lj1-x229.google.com with SMTP id 38308e7fff4ca-2c50d1b9f22so32453311fa.0 for ; Fri, 03 Nov 2023 11:26:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20230601; t=1699036004; x=1699640804; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=XPq51/zWKEaX0Ugs0BO93z31TvLRHL0P4szzXPPrFYA=; b=Lb3pOtPVzJm3Mlx3dIEoQH0AB2HsPk6cTdTWa8QFjXffEbFfPnIhLH6RAIsb/FGZKx Ju9zr89uEGDN6FPyoFEjM1IkNNzj6yxlMUK+9T4ZO8l4Dd3YsMxKTeyfGI+97q62n027 6edkd2OH5HnBP1zrUlyDLRlxfP0mHk/Hq0Z4fThN9F43VX4nfcfDrzzXVEJFeYZBvxEI WVVT+eUQIAAE8+ggiyGAN0maB81FJHQ6GHSxBI/fuzku4wtN2eAOdxls5Ujw9oV59bQI vj0LiQYJf8VdVw6uirqT4UAlPzmOg/dHGRQcxinTOCTcxRc+rRGuoefrbEELtmfYSu/e NnIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699036004; x=1699640804; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XPq51/zWKEaX0Ugs0BO93z31TvLRHL0P4szzXPPrFYA=; b=L8xu0G027OYB3R6D00Ip3SVsVSmMAXZMlszM53RZdECrGLnaKrd+3RB8CsYqDOTsPX mbOIHtyDzM4JS7Yh9RWkB6fmSjIeSN02ot3/+CFZHq5lScQbS4pRkZAXjhQCs2qkjstd vlIbz1iQkB2du0qhzMYFkPN12PJqpSQZb208yo3UhyQO7udCUW4iGS2scaOSFa/8Ocx5 Vn56NHQ+AZTb7VT/udXasM1M2AlzD8OViCAWtWgjs60c2CienqexzmHqqKZUKRPiD6eK qwW6a73fUWnId6vQcV0MvaRSBHsZMn3CizTOltrSPRwWYb25EiSsavPSskwpUFPUyNXc Wrpg== X-Gm-Message-State: AOJu0YyhyFv228baorWHDw+hnjIzwrAKvQ12mz9qlfZRI298CZC07wP2 h6BkjrOCIr6sqm7aJIkr0B+aNRDbU2w= X-Google-Smtp-Source: AGHT+IGCY/bRs9qQYuYzVf8zQjW8UwhVmnLXnJYzIn8xmNA/S3IogfN17bFcHOg/06cRAI07FkyTRg== X-Received: by 2002:a05:6512:34c8:b0:509:497f:c327 with SMTP id w8-20020a05651234c800b00509497fc327mr4031903lfr.0.1699036004104; Fri, 03 Nov 2023 11:26:44 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-077-003-098-230.77.3.pool.telefonica.de. [77.3.98.230]) by smtp.gmail.com with ESMTPSA id v10-20020aa7dbca000000b0052ff9bae873sm1210275edt.5.2023.11.03.11.26.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 11:26:43 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [PATCH v2 3/3] libsepol: more strict validation Date: Fri, 3 Nov 2023 19:26:37 +0100 Message-ID: <20231103182637.85003-3-cgzones@googlemail.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231103182637.85003-1-cgzones@googlemail.com> References: <20231103182637.85003-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Ensure the ibendport port is not 0 (similar to the kernel). More general depth test for boolean expressions. Ensure the boolean id is not set for logic operators. Signed-off-by: Christian Göttsche Acked-by: James Carter --- libsepol/src/policydb_validate.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/libsepol/src/policydb_validate.c b/libsepol/src/policydb_validate.c index b20ed579..d04f6bcd 100644 --- a/libsepol/src/policydb_validate.c +++ b/libsepol/src/policydb_validate.c @@ -1002,13 +1002,15 @@ static int validate_cond_expr(sepol_handle_t *handle, const struct cond_expr *ex case COND_BOOL: if (validate_value(expr->boolean, boolean)) goto bad; - if (depth == (COND_EXPR_MAXDEPTH - 1)) + if (depth >= (COND_EXPR_MAXDEPTH - 1)) goto bad; depth++; break; case COND_NOT: if (depth < 0) goto bad; + if (expr->boolean != 0) + goto bad; break; case COND_OR: case COND_AND: @@ -1017,6 +1019,8 @@ static int validate_cond_expr(sepol_handle_t *handle, const struct cond_expr *ex case COND_NEQ: if (depth < 1) goto bad; + if (expr->boolean != 0) + goto bad; depth--; break; default: @@ -1203,6 +1207,8 @@ static int validate_ocontexts(sepol_handle_t *handle, const policydb_t *p, valid goto bad; break; case OCON_IBENDPORT: + if (octx->u.ibendport.port == 0) + goto bad; if (!octx->u.ibendport.dev_name) goto bad; break;