From patchwork Mon Aug 14 13:19:59 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: 13352840 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 2ED63C05052 for ; Mon, 14 Aug 2023 13:21:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230287AbjHNNVQ (ORCPT ); Mon, 14 Aug 2023 09:21:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230398AbjHNNUr (ORCPT ); Mon, 14 Aug 2023 09:20:47 -0400 Received: from mail-lj1-x232.google.com (mail-lj1-x232.google.com [IPv6:2a00:1450:4864:20::232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D00DB10FB for ; Mon, 14 Aug 2023 06:20:41 -0700 (PDT) Received: by mail-lj1-x232.google.com with SMTP id 38308e7fff4ca-2b9cdba1228so67643001fa.2 for ; Mon, 14 Aug 2023 06:20:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019240; x=1692624040; 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=2T+4o5IqM1ckjv1vB6Hj+yuZO8zxIn1UbsF72lQnCa4=; b=oCIX3KnWLl/XTmWwWWCjFzifVZ1RqskFZnv67XMB7Kcc8rgN1NkY2aFP4LTcj8U4w+ nt4mJxGUvUFxMipi4+aHM37e3fKFM22XDl7cm0fxT1VJGQbOfrXpJZtQPeQCmPnQUlsP s5KVUjAzmTDC9HMa704Pv6iX2gvj6SGZEr2HGDKghmmFuKKzn2KouXmMuYn91NMs8kZW fq/T+2o0koQxfoukvuDgcgMPYCaB51IsTJoW8291cGgxnvxYb66cGEk31KtCfDHcaO7j WvqOel3iQhyp4CKzLGKlZEw90n/FytxbJiG3c8DxMG09FNYbkV7xwyi7ad3zN9yc4Z/S dUNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019240; x=1692624040; 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=2T+4o5IqM1ckjv1vB6Hj+yuZO8zxIn1UbsF72lQnCa4=; b=QV71LeY9gjnP3G2zjEl6XACyKY/8Ye/IsEE4PtpRPZTgY+F0UTyxe8AvVRiRBFyZb/ 2Vu0igR5zOOkdzzR1jFiRN3eKE+XW7IV9VhnwhSL9vizddvkeAkW4b3Hqaf+/sn/OL5R PhdzKYekid2sSFlG7wFEfqFrVyEe1XJn3KBBV9fz/HABKusAZFl6fItgisDya9fgAquU TOskhJxR4sGgilZcHn6OWk5tnp6bI2kWepp2sdXgCDYwqL/Daquak7OqQqxbmGW7QATh hSV1WF263LqFyY1VXqbNi+lJ2h4cvy5SoFa5SZZUJP9sNeurn7N2f08cDX2t1cgBmV26 W8TA== X-Gm-Message-State: AOJu0YyyPIk9GkHR3OQexVlVhUbqEuI9S855DnKbH4RRaV8nMqH5EcG4 +QuRXutQt4UfNu5pnxUuL01mS6VrMD3sHfkLpnk= X-Google-Smtp-Source: AGHT+IFV60vtjqBYnaAJl9ZA8RtqIKJ2BbfJOzF713VSENQRpmu8Uqh3KKpNmYRZuluGO2zM5UhdyA== X-Received: by 2002:a2e:8e92:0:b0:2b9:f0b4:eaa1 with SMTP id z18-20020a2e8e92000000b002b9f0b4eaa1mr6368638ljk.16.1692019239878; Mon, 14 Aug 2023 06:20:39 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:39 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 01/27] libselinux/utils: update selabel_partial_match Date: Mon, 14 Aug 2023 15:19:59 +0200 Message-Id: <20230814132025.45364-2-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Print usage information and exit if required path option is not given or superfluous arguments are given. Constify read-only variables assigned command line arguments. Simplify bool evaluation. Signed-off-by: Christian Göttsche Acked-by: James Carter --- libselinux/utils/selabel_partial_match.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/libselinux/utils/selabel_partial_match.c b/libselinux/utils/selabel_partial_match.c index 7bbd5777..0df2627a 100644 --- a/libselinux/utils/selabel_partial_match.c +++ b/libselinux/utils/selabel_partial_match.c @@ -28,7 +28,7 @@ int main(int argc, char **argv) { int opt; bool partial_match; - char *validate = NULL, *path = NULL, *file = NULL; + const char *validate = NULL, *path = NULL, *file = NULL; struct selabel_handle *hnd; struct selinux_opt selabel_option[] = { @@ -55,6 +55,9 @@ int main(int argc, char **argv) } } + if (!path || optind != argc) + usage(argv[0]); + selabel_option[0].value = file; selabel_option[1].value = validate; @@ -69,7 +72,7 @@ int main(int argc, char **argv) partial_match = selabel_partial_match(hnd, path); printf("Match or Partial match: %s\n", - partial_match == 1 ? "TRUE" : "FALSE"); + partial_match ? "TRUE" : "FALSE"); selabel_close(hnd); return partial_match; From patchwork Mon Aug 14 13:20:00 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: 13352845 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 72AF1C07E8C for ; Mon, 14 Aug 2023 13:21:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230363AbjHNNVS (ORCPT ); Mon, 14 Aug 2023 09:21:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230401AbjHNNUr (ORCPT ); Mon, 14 Aug 2023 09:20:47 -0400 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 386451715 for ; Mon, 14 Aug 2023 06:20:42 -0700 (PDT) Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-9936b3d0286so612345266b.0 for ; Mon, 14 Aug 2023 06:20:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019241; x=1692624041; 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=ZBLHW0O+U7ltOxkiaFrPl4r80XrqUswnN0F1DvZlWLA=; b=I0Gs77FwvSzk1nUeKXxtPeBDa82M1WHqIlNIKZLfVfEv9L5OXEn13Dw7j8sTuIIG82 lUexmaSaGq0tdOAI4su6BjztXHJN/7A0gZCcvWLgGm7is0hfM1kv4YLQA4XovDMcliys kgIzABM23uO31TomuQGIWlq/8g95JB4ERMb8MuH4+jT3tlu+bnSCWl9OgJGSExLCWzPA w8qIMK1t01okSOrgvJNTzenhno/xN50hSn2hlpVdkAscAzlMkkAecNH/KEgS2fGS3Sic Eq0oa0N4JxDBPmAi+wHBeOi8wtL6Usrbt4bas/VDW8G5VREi0U23YPRt3zrKxrMG826Y vHhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019241; x=1692624041; 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=ZBLHW0O+U7ltOxkiaFrPl4r80XrqUswnN0F1DvZlWLA=; b=A+kRN7DcBZdf+79TWxdWPz3HBEZpARtvKRylAaH/Ea4Aa5eJmIuzvVxSnKSe5B1HHm lmTw6k4CCsDhDXwihAhwCaTazWMq3xIoWHlOxLH83lwu+gWYyW1Qxm5rpe6tjgsQQ8Tk G4jxe+OLVziMOLb8YoEEz9rKzkwYM3G74QacvkK92Eb9/fX2BnUL5GbL4QNvFKCf/+0a d6/RnSIkpzejnnAVgCzBnj0fjon6a0qUsNGkXljYK0wMSMnMcbEIEc/6VK3spIrp+XwW 4Y6wG2mbLd1YiGYL+4BRSHULLhAo0s47bf5LS8fCdaIj6zsnhipSuYrnUQHzRqWAp4V6 LDNQ== X-Gm-Message-State: AOJu0Ywz0hDJsnRBIaMx049PbGE2DY7+zc7NGjaX/mYr4eCdV0lSh0Vg ry5yC+eehglCTlvMOjurzuKWm+bcVRHeokOijrk= X-Google-Smtp-Source: AGHT+IFvKK3YYbe7bi/hGztikjdedqrj3aIVuiAQ94egHZn8FeCkkhjnLmMT74RMBBD4+tErOeEYlA== X-Received: by 2002:a17:907:7634:b0:992:6064:f32b with SMTP id jy20-20020a170907763400b009926064f32bmr7701430ejc.46.1692019240504; Mon, 14 Aug 2023 06:20:40 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:40 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 02/27] libselinux: misc label cleanup Date: Mon, 14 Aug 2023 15:20:00 +0200 Message-Id: <20230814132025.45364-3-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Drop unused parameter from selabel_is_digest_set(). It is only written to but writes to the function local copy of the pointer are void. Constify read-only handle parameter of selabel_validate() and compat_validate(). Constify read-only from-address parameter of digest_add_specfile(). Constify read-only function pointer array initfuncs. Merge malloc(3) and memset(3) calls into calloc(3). Simplify boolean assignment. Drop duplicate include file. Drop return at end of void function. Signed-off-by: Christian Göttsche Acked-by: James Carter --- libselinux/src/label.c | 17 +++++++---------- libselinux/src/label_internal.h | 6 +++--- libselinux/src/label_support.c | 4 +--- libselinux/src/matchpathcon.c | 2 +- 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/libselinux/src/label.c b/libselinux/src/label.c index 586e5e5e..a2efa99c 100644 --- a/libselinux/src/label.c +++ b/libselinux/src/label.c @@ -45,7 +45,7 @@ typedef int (*selabel_initfunc)(struct selabel_handle *rec, const struct selinux_opt *opts, unsigned nopts); -static selabel_initfunc initfuncs[] = { +static const selabel_initfunc initfuncs[] = { &selabel_file_init, CONFIG_MEDIA_BACKEND(selabel_media_init), CONFIG_X_BACKEND(selabel_x_init), @@ -56,8 +56,7 @@ static selabel_initfunc initfuncs[] = { static inline struct selabel_digest *selabel_is_digest_set (const struct selinux_opt *opts, - unsigned n, - struct selabel_digest *entry) + unsigned n) { struct selabel_digest *digest = NULL; @@ -77,8 +76,7 @@ static inline struct selabel_digest *selabel_is_digest_set if (!digest->specfile_list) goto err; - entry = digest; - return entry; + return digest; } } return NULL; @@ -121,7 +119,7 @@ static inline int selabel_is_validate_set(const struct selinux_opt *opts, return 0; } -int selabel_validate(struct selabel_handle *rec, +int selabel_validate(const struct selabel_handle *rec, struct selabel_lookup_rec *contexts) { int rc = 0; @@ -133,7 +131,7 @@ int selabel_validate(struct selabel_handle *rec, if (rc < 0) goto out; - contexts->validated = 1; + contexts->validated = true; out: return rc; } @@ -215,15 +213,14 @@ struct selabel_handle *selabel_open(unsigned int backend, goto out; } - rec = (struct selabel_handle *)malloc(sizeof(*rec)); + rec = (struct selabel_handle *)calloc(1, sizeof(*rec)); if (!rec) goto out; - memset(rec, 0, sizeof(*rec)); rec->backend = backend; rec->validating = selabel_is_validate_set(opts, nopts); - rec->digest = selabel_is_digest_set(opts, nopts, rec->digest); + rec->digest = selabel_is_digest_set(opts, nopts); if ((*initfuncs[backend])(rec, opts, nopts)) { if (rec->digest) diff --git a/libselinux/src/label_internal.h b/libselinux/src/label_internal.h index 782c6aa8..273a630a 100644 --- a/libselinux/src/label_internal.h +++ b/libselinux/src/label_internal.h @@ -63,7 +63,7 @@ struct selabel_digest { }; extern int digest_add_specfile(struct selabel_digest *digest, FILE *fp, - char *from_addr, + const char *from_addr, size_t buf_len, const char *path); extern void digest_gen_hash(struct selabel_digest *digest); @@ -118,7 +118,7 @@ struct selabel_handle { * Validation function */ extern int -selabel_validate(struct selabel_handle *rec, +selabel_validate(const struct selabel_handle *rec, struct selabel_lookup_rec *contexts) ; /* @@ -136,7 +136,7 @@ extern void __attribute__ ((format(printf, 1, 2))) } while (0) extern int -compat_validate(struct selabel_handle *rec, +compat_validate(const struct selabel_handle *rec, struct selabel_lookup_rec *contexts, const char *path, unsigned lineno) ; diff --git a/libselinux/src/label_support.c b/libselinux/src/label_support.c index 54fd49a5..df474cba 100644 --- a/libselinux/src/label_support.c +++ b/libselinux/src/label_support.c @@ -10,7 +10,6 @@ #include #include #include -#include #include "label_internal.h" /* @@ -138,7 +137,6 @@ void digest_gen_hash(struct selabel_digest *digest) Sha1Finalise(&context, (SHA1_HASH *)digest->digest); free(digest->hashbuf); digest->hashbuf = NULL; - return; } /** @@ -154,7 +152,7 @@ void digest_gen_hash(struct selabel_digest *digest) * Return %0 on success, -%1 with @errno set on failure. */ int digest_add_specfile(struct selabel_digest *digest, FILE *fp, - char *from_addr, size_t buf_len, + const char *from_addr, size_t buf_len, const char *path) { unsigned char *tmp_buf; diff --git a/libselinux/src/matchpathcon.c b/libselinux/src/matchpathcon.c index bf2da083..9a9bafb0 100644 --- a/libselinux/src/matchpathcon.c +++ b/libselinux/src/matchpathcon.c @@ -35,7 +35,7 @@ void set_matchpathcon_printf(void (*f) (const char *fmt, ...)) myprintf_compat = 1; } -int compat_validate(struct selabel_handle *rec, +int compat_validate(const struct selabel_handle *rec, struct selabel_lookup_rec *contexts, const char *path, unsigned lineno) { From patchwork Mon Aug 14 13:20:01 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: 13352846 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 82213C07E8B for ; Mon, 14 Aug 2023 13:21:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230428AbjHNNVT (ORCPT ); Mon, 14 Aug 2023 09:21:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230435AbjHNNUs (ORCPT ); Mon, 14 Aug 2023 09:20:48 -0400 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBDF2171F for ; Mon, 14 Aug 2023 06:20:42 -0700 (PDT) Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-99bdcade7fbso562065666b.1 for ; Mon, 14 Aug 2023 06:20:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019241; x=1692624041; 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=ynGKH2GCuzeDJnxtfumT0lqtEUXUxMF2i07Kle13BUo=; b=sAnhKbE0TwPGnapDhvTlQLOxzYx5o9v9p5bgWxIDNSUbHbC+s4mPnDskkJJ2lsJlvs rA0wHGUa2ArhaCdN7qy2CsPTbeYYSe0Wi7mnPjOKHrxU+EL1JiyKIBPZKqtH/0TGUZME /mKd9sKRH6YiVbtJP1/H0/AgenCNFkNqF7KfVD0bXRzlVOsbomyN9KVW/BYRQSS2E8so OA/uxEkzWTVSvwq6WTaViWKbwrj4SI4xvl9lvnq1vbTUnCiYN/ZJgBdSK9M5vUauYHLr NGn+rsjGns7DayJiek1uTs1U2RX2qWP4meJZe90NZaQeW3Smewd5fmVqB7XIlnP9K7mf WPHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019241; x=1692624041; 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=ynGKH2GCuzeDJnxtfumT0lqtEUXUxMF2i07Kle13BUo=; b=ALHtdnXc1dhUTI9nz7Z/nqhSojpYiYn+tGbIyZ8mId/r2DXVIg/ZLGOP/ZPk1FoOhd oBsuAq5tYyeQDqIr4MvSwaOVWLT9J5SkusK8pSVX0lo81SUMmybtP/zmd+PLMnMlp2IY xBJKIhX0CqUyCWf8nbao5ZSrAFsR+orC4v9f/SmvRFYl2Jt9TqcVD1Cevd+hbiaskP3f PkraVP0se5RwpPuxNYaH+O4RCIXNlqGmPwAleyC34+ScLvCXqiACDNG8xHKzXfEIj+6n 2PL7S2VYn1Zvmx+EfdWVTQhRk4WAbLn1PlYinJNDPuQB+I4cQbUJcx3ZrCLp8d0tu74p rCVQ== X-Gm-Message-State: AOJu0YzOjNbxz4as6A6QgfiF3fNNzPmfP5PscFWCA2QaaKvt7YZznk3q 1b9VH14DqC/SxYMf/EH+aBdNEHufONBvgpCcSvc= X-Google-Smtp-Source: AGHT+IFzQ9EasvYzIUYzeD100GAyfdr4sXxIe+5/v6xNoJAvIFK8O/y8H2mMGvxN5Yb7NAYCTWCEtQ== X-Received: by 2002:a17:907:b10:b0:993:f540:5bb1 with SMTP id h16-20020a1709070b1000b00993f5405bb1mr8048478ejl.41.1692019241310; Mon, 14 Aug 2023 06:20:41 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:40 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 03/27] libselinux: drop obsolete optimization flag Date: Mon, 14 Aug 2023 15:20:01 +0200 Message-Id: <20230814132025.45364-4-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org The flag -fipa-pure-const is enabled by default in GCC at -O0 and above. The flag is not supported by Clang, which might result in issues if a compilation database was created via GCC. Signed-off-by: Christian Göttsche Acked-by: James Carter --- v2: add missing signed-off --- libselinux/src/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libselinux/src/Makefile b/libselinux/src/Makefile index f9a1e5f5..b1620113 100644 --- a/libselinux/src/Makefile +++ b/libselinux/src/Makefile @@ -61,7 +61,7 @@ SRCS= $(filter-out $(GENERATED) audit2why.c, $(sort $(wildcard *.c))) MAX_STACK_SIZE=32768 ifeq ($(COMPILER), gcc) -EXTRA_CFLAGS = -fipa-pure-const -Wlogical-op -Wpacked-bitfield-compat -Wsync-nand \ +EXTRA_CFLAGS = -Wlogical-op -Wpacked-bitfield-compat -Wsync-nand \ -Wcoverage-mismatch -Wcpp -Wformat-contains-nul -Wnormalized=nfc -Wsuggest-attribute=const \ -Wsuggest-attribute=noreturn -Wsuggest-attribute=pure -Wtrampolines -Wjump-misses-init \ -Wno-suggest-attribute=pure -Wno-suggest-attribute=const -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 \ From patchwork Mon Aug 14 13:20:02 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: 13352839 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 0B187C04FE1 for ; Mon, 14 Aug 2023 13:21:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230296AbjHNNVR (ORCPT ); Mon, 14 Aug 2023 09:21:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230450AbjHNNUs (ORCPT ); Mon, 14 Aug 2023 09:20:48 -0400 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C69EB172D for ; Mon, 14 Aug 2023 06:20:43 -0700 (PDT) Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-99bcf2de59cso569769266b.0 for ; Mon, 14 Aug 2023 06:20:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019242; x=1692624042; 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=lRlSHmOv3URj2X0zO+/PjgKRlSp4lVJgKRtEMaqdh2M=; b=FJu6ACM+EQ3OQO8q7mlOtK7SZ45V6af+yioWaGAJ7Hm+snl8i4JxSoyQBU5hcpJOGL YBe5bdZF1MEb7klwwes/OdQDi4vNj49jGq6iYc9OhuFQBuQJ+PT4XvGC/6iDHz9IxG2Y w3G5krdk7fN8AwybvNWONCJ7JAJC/jEsnM+zkgiroY+SLJe/JeQTNglFZ7xNdCjJWUlH Bzbk0gW8u+a/zHFTeIz+fAiaUVtZMG7Aan/UWMjohPkp7ignDdNvLvMPnzFXo1TAUNOO Y5aVBZT7LBm7BuhK13X/8zrIV+odI5LuE0Mdsq8LFaUSwWgzTESiZuORarSJPCEjjLMy Bqrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019242; x=1692624042; 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=lRlSHmOv3URj2X0zO+/PjgKRlSp4lVJgKRtEMaqdh2M=; b=J6G2b48AEgxy/XWHyX3FI1Gr3eo8ma6AjdjjH1JzT/gLEZt0xufyF0JYuHbjRRNL48 sOLZRKqQSocDIO02p++vkNeVusQG4jw/y0IkeN7dm2fK3MGI/7i9+6+RNOyH6GU/qe2k H7qry7v2gIw3rb58ndqb0IAQJSG8+ztj31z3wJwtUAgHDOPCJftAQjDx/eYP9htPfZXy Xj0yesKRMrF1cVRPOxJ8id7+aLj0DEwv3sMZcMxA72mTjnrEyYv77jyMfSSt3sFmKu1I vf6zTNnQWeNb2+/21QdG8II3Ts/TF162P1YOO5OGrQ+E9UHGtrKnFFz7+rjSZ/kJyIhz wgsA== X-Gm-Message-State: AOJu0YyfwkdCynHZVl2M3vSaf7jjzIFdG2vjpfma8oV5t6bDHmeB7een Sbx/0wnk7ef81drngSP4OGCu/z6FVJH+tW45E1o= X-Google-Smtp-Source: AGHT+IEmOB/z0Y/7zdxTnQoOUkOKow4MkwgGFsFoi3lWowoaZboYtXIq0LYu8IDBPeEIwqpHP/S4Bw== X-Received: by 2002:a17:907:7846:b0:99b:f50b:d7d5 with SMTP id lb6-20020a170907784600b0099bf50bd7d5mr6823229ejc.44.1692019241895; Mon, 14 Aug 2023 06:20:41 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:41 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 04/27] libselinux: drop unnecessary warning overrides Date: Mon, 14 Aug 2023 15:20:02 +0200 Message-Id: <20230814132025.45364-5-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Drop overrides of warning flags which are not triggered by any code. Signed-off-by: Christian Göttsche Acked-by: James Carter --- libselinux/src/Makefile | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libselinux/src/Makefile b/libselinux/src/Makefile index b1620113..20d79312 100644 --- a/libselinux/src/Makefile +++ b/libselinux/src/Makefile @@ -83,11 +83,10 @@ CFLAGS ?= -O -Wall -W -Wundef -Wformat-y2k -Wformat-security -Winit-self -Wmissi -Wdeprecated-declarations -Wdiv-by-zero -Wdouble-promotion -Wendif-labels -Wextra \ -Wformat-extra-args -Wformat-zero-length -Wformat=2 -Wmultichar \ -Woverflow -Wpointer-to-int-cast -Wpragmas \ - -Wno-missing-field-initializers -Wno-sign-compare \ - -Wno-format-nonliteral -Wframe-larger-than=$(MAX_STACK_SIZE) \ + -Wframe-larger-than=$(MAX_STACK_SIZE) \ -fstack-protector-all --param=ssp-buffer-size=4 -fexceptions \ -fasynchronous-unwind-tables -fdiagnostics-show-option \ - -Werror -Wno-aggregate-return -Wno-redundant-decls \ + -Werror -Wno-aggregate-return \ $(EXTRA_CFLAGS) LD_SONAME_FLAGS=-soname,$(LIBSO),--version-script=libselinux.map,-z,defs,-z,relro From patchwork Mon Aug 14 13:20:03 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: 13352841 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 19B0AC04FE0 for ; Mon, 14 Aug 2023 13:21:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230290AbjHNNVR (ORCPT ); Mon, 14 Aug 2023 09:21:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230456AbjHNNUs (ORCPT ); Mon, 14 Aug 2023 09:20:48 -0400 Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 558961733 for ; Mon, 14 Aug 2023 06:20:44 -0700 (PDT) Received: by mail-ed1-x534.google.com with SMTP id 4fb4d7f45d1cf-51e28cac164so11107583a12.1 for ; Mon, 14 Aug 2023 06:20:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019243; x=1692624043; 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=yOtpLWp5tJrAK7/EmVMT3zyqfKWlAxfHM6ZvZqzUaig=; b=Tmb+fvt95tg9mljrweV72Uf9k/0Fys5NYf77qhqwCL18dlBMAie2FVhhmOKcE2dlO4 YAR8YgfNTWInpCQomvR2Xb6+iMCZLvPK1ABL79AMhRE5V+4LwivEoMN660UgjaEPI++Q ER4P+luFd2uPKfRarCKi+GURsZ8jqStmKcPKlEycVA6/VCqDK9ojGQiOzDMUk4uaf3Qa gfqYCkSlg7MiJE2/vg6dgeieogSeZs9ZNNCepDZGjeJ8n17/h3g0VHm3bpkSrbd01VCN JoVvUknAgyDJddNm9tatBupcG1ZIc24BPyA5FrOqDSqAYEtkVYhfiuT+qqDwxRSR1KPP /fCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019243; x=1692624043; 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=yOtpLWp5tJrAK7/EmVMT3zyqfKWlAxfHM6ZvZqzUaig=; b=f+53FF2gGzF4n0ar3KJUKww/29UMG20pPNo3XueTJbE/mZzUiAkDnqVYk9l8zlRFe+ bzkvYkxqXLMWXbuJRu7VJof+jUMWC2Ul8b9++Gohce0hNs7xh1vU4vaYX3jz7Z0wk8N0 ph0M75jgFcwGRh1jy2RydBspXABwlGt5XUOZiCWwDYUoXp4ZcBpgXjC7DcR94fi8GIMI rZq3yhujXFKkEmqBQX6uYcvzIsoDY4qEvfBDHq557YZl3yrjrDmJk7/zH0At2dLuvzW8 uh6TWJQU9YlKi2mFGFq0J8fPK2eaW+x6x3MkQPzyXLR2lvJxbLPHK0fXrYhFGj/BEq1h Edww== X-Gm-Message-State: AOJu0Yx1e8qSqGWblnphWnifvo93CTvm7lnTrkM+a8wxOgGMyxXWR1hz HgVtaQnAxcsC6oRhLiHWxDcKxxLnGBs3jbAq148= X-Google-Smtp-Source: AGHT+IHQhyNUOTVnmEa75+C2DndcBwdOFDhpzL14HrHvsIknfekEcwLbY/Vwqc9pz1ktoiL7Fg/geQ== X-Received: by 2002:a17:907:774e:b0:992:ef60:aadd with SMTP id kx14-20020a170907774e00b00992ef60aaddmr10495125ejc.13.1692019242766; Mon, 14 Aug 2023 06:20:42 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:42 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 05/27] setfiles: do not issue AUDIT_FS_RELABEL on dry run Date: Mon, 14 Aug 2023 15:20:03 +0200 Message-Id: <20230814132025.45364-6-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Do not issue an audit event of type AUDIT_FS_RELABEL in case of a passive check. Print the reason for an audit_open(3) failure. Do not prematurely exit in case audit_open(3) fails, since all important work is already done, but proceed with cleanup tasks. Signed-off-by: Christian Göttsche Acked-by: James Carter --- policycoreutils/setfiles/setfiles.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/policycoreutils/setfiles/setfiles.c b/policycoreutils/setfiles/setfiles.c index 4dd0d0dc..88a27241 100644 --- a/policycoreutils/setfiles/setfiles.c +++ b/policycoreutils/setfiles/setfiles.c @@ -86,23 +86,20 @@ static int canoncon(char **contextp) } #ifndef USE_AUDIT -static void maybe_audit_mass_relabel(int mass_relabel __attribute__((unused)), - int mass_relabel_errs __attribute__((unused))) +static void audit_mass_relabel(int mass_relabel_errs __attribute__((unused))) { #else -static void maybe_audit_mass_relabel(int mass_relabel, int mass_relabel_errs) +static void audit_mass_relabel(int mass_relabel_errs) { int audit_fd = -1; int rc = 0; - if (!mass_relabel) /* only audit a forced full relabel */ - return; - audit_fd = audit_open(); if (audit_fd < 0) { - fprintf(stderr, "Error connecting to audit system.\n"); - exit(-1); + fprintf(stderr, "Error connecting to audit system: %s.\n", + strerror(errno)); + return; } rc = audit_log_user_message(audit_fd, AUDIT_FS_RELABEL, @@ -463,7 +460,8 @@ int main(int argc, char **argv) &skipped_errors) < 0; } - maybe_audit_mass_relabel(r_opts.mass_relabel, errors); + if (r_opts.mass_relabel && !r_opts.nochange) + audit_mass_relabel(errors); if (warn_no_match) selabel_stats(r_opts.hnd); From patchwork Mon Aug 14 13:20:04 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: 13352859 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 5CD64C25B73 for ; Mon, 14 Aug 2023 13:21:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230328AbjHNNVY (ORCPT ); Mon, 14 Aug 2023 09:21:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230472AbjHNNUt (ORCPT ); Mon, 14 Aug 2023 09:20:49 -0400 Received: from mail-lf1-x131.google.com (mail-lf1-x131.google.com [IPv6:2a00:1450:4864:20::131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E106173F for ; Mon, 14 Aug 2023 06:20:45 -0700 (PDT) Received: by mail-lf1-x131.google.com with SMTP id 2adb3069b0e04-4fe457ec6e7so6848083e87.3 for ; Mon, 14 Aug 2023 06:20:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019243; x=1692624043; 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=WMRdXXPySKs4DfE+y8O69B0s6zAZugNBKWTPys88D5c=; b=FeXhuEn4ssl5cFBjtlxcYV9o10zgg2RJy8hcAdOtBQp+L6qcM9mFv/fpr08gVEL2IV 4mNCzonZ+Qav9gU6Cz/y+/Y5iSzEEHV/fW/x4vHAzLrF3mvuZweJ+QZMIqX9kAxQjt2q uI8w9h1Y1H4SaNfM9hixVU1iqQ+ATY2EolSyYOBSjUIcErbJ4WauVEkOTd1PpPAevOM4 4M0fUMZzmi7ZCI4e2Y4xry3vjH5inmxVjuSi8Foofn1l74IenzMIZb+cmmY0hd7Xa+Fc SF98hN/0QnOJdWWOHjYUkeVD7t9mlmrwB7CGa7fUexAEhVneS9q7Lz6bAQK7lHTSohjW EdTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019243; x=1692624043; 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=WMRdXXPySKs4DfE+y8O69B0s6zAZugNBKWTPys88D5c=; b=XFw/AK/W788r4v36V4Gu4Dd2BW+XmeX3FZucN2gjsl2ZGlw0bizaJLpry0c96oXCVx Lc/s+HCTeV3Cx2l04cVBDlrJihfZqbQOiue3YMKwr5CuvXMyV3v8kiuwqFZgQXWJoPBc usMnoGGz4Rziw5F6O1Cdym8HytxeyiK7q96SQv6CFk+QKQIq972/D9LCEL+EQGT7b63u V6TcryebpCPZxgTIdDxEvkCiv0Ys9bhEvLmjfXWwoFXOsgx5rfEqtirg7KczraaAIV8u JEeULOSQa0zd4f9O2taERZKKDD66QHxovhc4dN3yrdo5msipPzjYrHfbsfTyrt78Q1AI sPEQ== X-Gm-Message-State: AOJu0YyOmPkezjgg5KiC9ZbzlwOZvWLsZ56FT3WRCJFlxyDMS0tnFaWr iIquiBq1IYQBkAVGJZm0FxIrnqlPMUdAqbjzlZA= X-Google-Smtp-Source: AGHT+IFZwwlTqCwrn7szGmrCth6vFo76YRSorzOdD5UBe7xZsJxMiAWQhQ/73c/Zytrq3Ml4NnZpRw== X-Received: by 2002:a05:6512:3b0f:b0:4fc:4f3e:9cbf with SMTP id f15-20020a0565123b0f00b004fc4f3e9cbfmr7960746lfv.50.1692019243320; Mon, 14 Aug 2023 06:20:43 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:43 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 06/27] libselinux: cast to unsigned char for character handling function Date: Mon, 14 Aug 2023 15:20:04 +0200 Message-Id: <20230814132025.45364-7-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Character handling functions, like isspace(3), expect a value representable as unsigned char or equal to EOF. Otherwise the behavior is undefined. See https://wiki.sei.cmu.edu/confluence/display/c/STR37-C.+Arguments+to+character-handling+functions+must+be+representable+as+an+unsigned+char Signed-off-by: Christian Göttsche --- libselinux/src/booleans.c | 8 ++++---- libselinux/src/compute_create.c | 2 +- libselinux/src/get_context_list.c | 12 ++++++------ libselinux/src/get_default_type.c | 2 +- libselinux/src/label_file.c | 8 ++++---- libselinux/src/label_media.c | 2 +- libselinux/src/label_support.c | 8 ++++---- libselinux/src/label_x.c | 2 +- libselinux/src/load_policy.c | 2 +- libselinux/src/matchmediacon.c | 6 +++--- libselinux/src/selinux_check_securetty_context.c | 4 ++-- libselinux/src/selinux_config.c | 12 ++++++------ libselinux/src/seusers.c | 6 +++--- 13 files changed, 37 insertions(+), 37 deletions(-) diff --git a/libselinux/src/booleans.c b/libselinux/src/booleans.c index e34b39ff..c557df65 100644 --- a/libselinux/src/booleans.c +++ b/libselinux/src/booleans.c @@ -107,7 +107,7 @@ char *selinux_boolean_sub(const char *name) char *ptr; char *src = line_buf; char *dst; - while (*src && isspace(*src)) + while (*src && isspace((unsigned char)*src)) src++; if (!*src) continue; @@ -115,19 +115,19 @@ char *selinux_boolean_sub(const char *name) continue; ptr = src; - while (*ptr && !isspace(*ptr)) + while (*ptr && !isspace((unsigned char)*ptr)) ptr++; *ptr++ = '\0'; if (strcmp(src, name) != 0) continue; dst = ptr; - while (*dst && isspace(*dst)) + while (*dst && isspace((unsigned char)*dst)) dst++; if (!*dst) continue; ptr = dst; - while (*ptr && !isspace(*ptr)) + while (*ptr && !isspace((unsigned char)*ptr)) ptr++; *ptr = '\0'; diff --git a/libselinux/src/compute_create.c b/libselinux/src/compute_create.c index e9f3c96a..92ee2fc3 100644 --- a/libselinux/src/compute_create.c +++ b/libselinux/src/compute_create.c @@ -23,7 +23,7 @@ static int object_name_encode(const char *objname, char *buffer, size_t buflen) do { code = *objname++; - if (isalnum(code) || code == '\0' || code == '-' || + if (isalnum((unsigned char)code) || code == '\0' || code == '-' || code == '.' || code == '_' || code == '~') { if (buflen - offset < 1) return -1; diff --git a/libselinux/src/get_context_list.c b/libselinux/src/get_context_list.c index d774b9cf..0342823c 100644 --- a/libselinux/src/get_context_list.c +++ b/libselinux/src/get_context_list.c @@ -170,27 +170,27 @@ static int get_context_user(FILE * fp, /* Skip leading whitespace. */ start = line; - while (*start && isspace(*start)) + while (*start && isspace((unsigned char)*start)) start++; if (!(*start)) continue; /* Find the end of the (partial) fromcon in the line. */ end = start; - while (*end && !isspace(*end)) + while (*end && !isspace((unsigned char)*end)) end++; if (!(*end)) continue; /* Check for a match. */ linerole = start; - while (*start && !isspace(*start) && *start != ':') + while (*start && !isspace((unsigned char)*start) && *start != ':') start++; if (*start != ':') continue; *start = 0; linetype = ++start; - while (*start && !isspace(*start) && *start != ':') + while (*start && !isspace((unsigned char)*start) && *start != ':') start++; if (!(*start)) continue; @@ -210,14 +210,14 @@ static int get_context_user(FILE * fp, start = ++end; while (*start) { /* Skip leading whitespace */ - while (*start && isspace(*start)) + while (*start && isspace((unsigned char)*start)) start++; if (!(*start)) break; /* Find the end of this partial context. */ end = start; - while (*end && !isspace(*end)) + while (*end && !isspace((unsigned char)*end)) end++; if (*end) *end++ = 0; diff --git a/libselinux/src/get_default_type.c b/libselinux/src/get_default_type.c index 766ea4b7..87e9c72c 100644 --- a/libselinux/src/get_default_type.c +++ b/libselinux/src/get_default_type.c @@ -42,7 +42,7 @@ static int find_default_type(FILE * fp, const char *role, char **type) buf[strlen(buf) - 1] = 0; ptr = buf; - while (*ptr && isspace(*ptr)) + while (*ptr && isspace((unsigned char)*ptr)) ptr++; if (!(*ptr)) continue; diff --git a/libselinux/src/label_file.c b/libselinux/src/label_file.c index 3da28c45..461abc61 100644 --- a/libselinux/src/label_file.c +++ b/libselinux/src/label_file.c @@ -697,20 +697,20 @@ static int selabel_subs_init(const char *path, struct selabel_digest *digest, char *src = buf; char *dst = NULL; - while (*src && isspace(*src)) + while (*src && isspace((unsigned char)*src)) src++; if (src[0] == '#') continue; ptr = src; - while (*ptr && ! isspace(*ptr)) + while (*ptr && ! isspace((unsigned char)*ptr)) ptr++; *ptr++ = '\0'; if (! *src) continue; dst = ptr; - while (*dst && isspace(*dst)) + while (*dst && isspace((unsigned char)*dst)) dst++; ptr = dst; - while (*ptr && ! isspace(*ptr)) + while (*ptr && ! isspace((unsigned char)*ptr)) ptr++; *ptr = '\0'; if (! *dst) diff --git a/libselinux/src/label_media.c b/libselinux/src/label_media.c index 3137c18e..f09461ab 100644 --- a/libselinux/src/label_media.c +++ b/libselinux/src/label_media.c @@ -39,7 +39,7 @@ static int process_line(const char *path, char *line_buf, int pass, char *key, *context; buf_p = line_buf; - while (isspace(*buf_p)) + while (isspace((unsigned char)*buf_p)) buf_p++; /* Skip comment lines and empty lines. */ if (*buf_p == '#' || *buf_p == 0) diff --git a/libselinux/src/label_support.c b/libselinux/src/label_support.c index df474cba..94fb6697 100644 --- a/libselinux/src/label_support.c +++ b/libselinux/src/label_support.c @@ -26,14 +26,14 @@ static inline int read_spec_entry(char **entry, char **ptr, int *len, const char *entry = NULL; char *tmp_buf = NULL; - while (isspace(**ptr) && **ptr != '\0') + while (isspace((unsigned char)**ptr) && **ptr != '\0') (*ptr)++; tmp_buf = *ptr; *len = 0; - while (!isspace(**ptr) && **ptr != '\0') { - if (!isascii(**ptr)) { + while (!isspace((unsigned char)**ptr) && **ptr != '\0') { + if (!isascii((unsigned char)**ptr)) { errno = EINVAL; *errbuf = "Non-ASCII characters found"; return -1; @@ -80,7 +80,7 @@ int read_spec_entries(char *line_buf, const char **errbuf, int num_args, ...) len++; buf_p = line_buf; - while (isspace(*buf_p)) + while (isspace((unsigned char)*buf_p)) buf_p++; /* Skip comment lines and empty lines. */ diff --git a/libselinux/src/label_x.c b/libselinux/src/label_x.c index e6e8d9f6..e80bf107 100644 --- a/libselinux/src/label_x.c +++ b/libselinux/src/label_x.c @@ -41,7 +41,7 @@ static int process_line(const char *path, char *line_buf, int pass, char *type, *key, *context; buf_p = line_buf; - while (isspace(*buf_p)) + while (isspace((unsigned char)*buf_p)) buf_p++; /* Skip comment lines and empty lines. */ if (*buf_p == '#' || *buf_p == 0) diff --git a/libselinux/src/load_policy.c b/libselinux/src/load_policy.c index 17918e8b..57d7aaef 100644 --- a/libselinux/src/load_policy.c +++ b/libselinux/src/load_policy.c @@ -252,7 +252,7 @@ int selinux_init_load_policy(int *enforce) } if (fgets(buf, selinux_page_size, cfg) && (tmp = strstr(buf, "enforcing="))) { - if (tmp == buf || isspace(*(tmp - 1))) { + if (tmp == buf || isspace((unsigned char)*(tmp - 1))) { secmdline = atoi(tmp + sizeof("enforcing=") - 1); } diff --git a/libselinux/src/matchmediacon.c b/libselinux/src/matchmediacon.c index d3d95043..6ba87b99 100644 --- a/libselinux/src/matchmediacon.c +++ b/libselinux/src/matchmediacon.c @@ -29,7 +29,7 @@ int matchmediacon(const char *media, char ** con) current_line[strlen(current_line) - 1] = 0; /* Skip leading whitespace before the partial context. */ ptr = current_line; - while (*ptr && isspace(*ptr)) + while (*ptr && isspace((unsigned char)*ptr)) ptr++; if (!(*ptr)) @@ -37,7 +37,7 @@ int matchmediacon(const char *media, char ** con) /* Find the end of the media context. */ ptr2 = ptr; - while (*ptr2 && !isspace(*ptr2)) + while (*ptr2 && !isspace((unsigned char)*ptr2)) ptr2++; if (!(*ptr2)) continue; @@ -53,7 +53,7 @@ int matchmediacon(const char *media, char ** con) return -1; /* Skip whitespace. */ - while (*ptr2 && isspace(*ptr2)) + while (*ptr2 && isspace((unsigned char)*ptr2)) ptr2++; if (!(*ptr2)) { return -1; diff --git a/libselinux/src/selinux_check_securetty_context.c b/libselinux/src/selinux_check_securetty_context.c index c5c557fd..7609752e 100644 --- a/libselinux/src/selinux_check_securetty_context.c +++ b/libselinux/src/selinux_check_securetty_context.c @@ -26,13 +26,13 @@ int selinux_check_securetty_context(const char * tty_context) /* Skip leading whitespace. */ start = line; - while (*start && isspace(*start)) + while (*start && isspace((unsigned char)*start)) start++; if (!(*start)) continue; end = start; - while (*end && !isspace(*end)) + while (*end && !isspace((unsigned char)*end)) end++; if (*end) *end++ = 0; diff --git a/libselinux/src/selinux_config.c b/libselinux/src/selinux_config.c index d2e49ee1..1d8cfb71 100644 --- a/libselinux/src/selinux_config.c +++ b/libselinux/src/selinux_config.c @@ -103,7 +103,7 @@ int selinux_getenforcemode(int *enforce) if (strncmp(buf, SELINUXTAG, len)) continue; tag = buf+len; - while (isspace(*tag)) + while (isspace((unsigned char)*tag)) tag++; if (!strncasecmp (tag, "enforcing", sizeof("enforcing") - 1)) { @@ -173,7 +173,7 @@ static void init_selinux_config(void) if (line_buf[len - 1] == '\n') line_buf[len - 1] = 0; buf_p = line_buf; - while (isspace(*buf_p)) + while (isspace((unsigned char)*buf_p)) buf_p++; if (*buf_p == '#' || *buf_p == 0) continue; @@ -181,7 +181,7 @@ static void init_selinux_config(void) if (!strncasecmp(buf_p, SELINUXTYPETAG, sizeof(SELINUXTYPETAG) - 1)) { buf_p += sizeof(SELINUXTYPETAG) - 1; - while (isspace(*buf_p)) + while (isspace((unsigned char)*buf_p)) buf_p++; type = strdup(buf_p); if (!type) { @@ -191,7 +191,7 @@ static void init_selinux_config(void) } end = type + strlen(type) - 1; while ((end > type) && - (isspace(*end) || iscntrl(*end))) { + (isspace((unsigned char)*end) || iscntrl((unsigned char)*end))) { *end = 0; end--; } @@ -206,14 +206,14 @@ static void init_selinux_config(void) } else if (!strncmp(buf_p, REQUIRESEUSERS, sizeof(REQUIRESEUSERS) - 1)) { value = buf_p + sizeof(REQUIRESEUSERS) - 1; - while (isspace(*value)) + while (isspace((unsigned char)*value)) value++; intptr = &require_seusers; } else { continue; } - if (isdigit(*value)) + if (isdigit((unsigned char)*value)) *intptr = atoi(value); else if (strncasecmp(value, "true", sizeof("true") - 1)) *intptr = 1; diff --git a/libselinux/src/seusers.c b/libselinux/src/seusers.c index fff80c1a..6da8c318 100644 --- a/libselinux/src/seusers.c +++ b/libselinux/src/seusers.c @@ -25,7 +25,7 @@ static int process_seusers(const char *buffer, goto err; start = newbuf; - while (isspace(*start)) + while (isspace((unsigned char)*start)) start++; if (*start == '#' || *start == 0) { free(newbuf); @@ -46,7 +46,7 @@ static int process_seusers(const char *buffer, mls_found = 0; end = start; - while (*end && !isspace(*end)) + while (*end && !isspace((unsigned char)*end)) end++; } *end = 0; @@ -63,7 +63,7 @@ static int process_seusers(const char *buffer, goto out; start = ++end; - while (*end && !isspace(*end)) + while (*end && !isspace((unsigned char)*end)) end++; *end = 0; From patchwork Mon Aug 14 13:20:05 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: 13352849 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 9E8D5C07E8D for ; Mon, 14 Aug 2023 13:21:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230509AbjHNNVV (ORCPT ); Mon, 14 Aug 2023 09:21:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40274 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230502AbjHNNUv (ORCPT ); Mon, 14 Aug 2023 09:20:51 -0400 Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A2E0B198C for ; Mon, 14 Aug 2023 06:20:45 -0700 (PDT) Received: by mail-ej1-x632.google.com with SMTP id a640c23a62f3a-99bfcf4c814so578915066b.0 for ; Mon, 14 Aug 2023 06:20:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019244; x=1692624044; 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=OZoB2tNmGWecF5SpS2bhjX6gg76W6vta88m4GAWSsu4=; b=BWZCmaSuSNglwXhqsaZF619txghlSSwxEWuC4qhQkpE4iRplX2bHW8OKn/6v9ykG0E s6E8IfNi4HcAKqIxRme2+g93L9g0G1v+1kfP0ptslr0S8Ic4B1KHRkV1CYyDw+qezPOg k9XZPMagF1Rp/k512g5/BRIJyXBMmCAKT/Znw/17Noy6X8uio9aZEBat4PttMo5FoxBr ej3UhO5khhPdRelVHDq9qq1SNTFNoog32yEYivHP1/cPnL4DExZrFUOQLHh6N2BAA+Zt NaYtGPyocsj3EhOJ0P6E4pmZarAHpyOXKbQ6Vqki22bIqfuKEqlO/H+RESunLghlRP9I +EwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019244; x=1692624044; 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=OZoB2tNmGWecF5SpS2bhjX6gg76W6vta88m4GAWSsu4=; b=lUuAU5iB8J9mJNafLXXijrGwXwLcjNrC48p9rrixym0d25ojpHU8Vk+iw618mywvtV HYX15E47EyPAgVoStFg+fGwjMByNMDtlbk+1NgKMlKsZOC2go5XxAp+nVD5++BfpqJaw t9DC45doF19trkD53Vfw1aVYByaS4J+Exa/SzP28F6Fx5EopFG/vA7z/LZ9HGKU3EwTU Fmh0VdWkYo0+hNw0bMmoUqdHJcIRjr5xyk3UqvOkxlXFYNYdNxAjEx22KfXHo0EsUBf7 r6pGepIIKzlUg4lQPByON6ZSVOjOrfK58lWP9Tx6ztcZTUYdnq/IggxGC2fy+7+zGdTq LqSA== X-Gm-Message-State: AOJu0YxNWrev8zhHpwdj1oNk22L7sZ2Y0aEM9AHm5Va+tbsYsUl2mH29 JD8Lrj+y4PcxMmWjeIhwCUp39+dSoiSfkt1Nab0= X-Google-Smtp-Source: AGHT+IH3wbQSWGcOBwv1/nSCwMZ2k1lGUJh+QpJm7+jZ8JobXuSqzYsUwhvo6VK90OufhlnFYG4vMQ== X-Received: by 2002:a17:906:31c6:b0:99c:da24:bdb7 with SMTP id f6-20020a17090631c600b0099cda24bdb7mr8127347ejf.71.1692019244066; Mon, 14 Aug 2023 06:20:44 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:43 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 07/27] libselinux: constify selabel_cmp(3) parameters Date: Mon, 14 Aug 2023 15:20:05 +0200 Message-Id: <20230814132025.45364-8-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Comparing two selabel handles should (and currently does) not modify them. Signed-off-by: Christian Göttsche Acked-by: James Carter --- libselinux/include/selinux/label.h | 4 ++-- libselinux/src/label.c | 4 ++-- libselinux/src/label_file.c | 22 +++++++++++----------- libselinux/src/label_internal.h | 4 ++-- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/libselinux/include/selinux/label.h b/libselinux/include/selinux/label.h index e8983606..ce189a3a 100644 --- a/libselinux/include/selinux/label.h +++ b/libselinux/include/selinux/label.h @@ -153,8 +153,8 @@ enum selabel_cmp_result { * if @h1 is identical to @h2, %SELABEL_SUPERSET if @h1 is a superset * of @h2, and %SELABEL_INCOMPARABLE if @h1 and @h2 are incomparable. */ -extern enum selabel_cmp_result selabel_cmp(struct selabel_handle *h1, - struct selabel_handle *h2); +extern enum selabel_cmp_result selabel_cmp(const struct selabel_handle *h1, + const struct selabel_handle *h2); /** * selabel_stats - log labeling operation statistics. diff --git a/libselinux/src/label.c b/libselinux/src/label.c index a2efa99c..c0f586a2 100644 --- a/libselinux/src/label.c +++ b/libselinux/src/label.c @@ -333,8 +333,8 @@ int selabel_lookup_best_match_raw(struct selabel_handle *rec, char **con, return *con ? 0 : -1; } -enum selabel_cmp_result selabel_cmp(struct selabel_handle *h1, - struct selabel_handle *h2) +enum selabel_cmp_result selabel_cmp(const struct selabel_handle *h1, + const struct selabel_handle *h2) { if (!h1->func_cmp || h1->func_cmp != h2->func_cmp) return SELABEL_INCOMPARABLE; diff --git a/libselinux/src/label_file.c b/libselinux/src/label_file.c index 461abc61..5ac23e1f 100644 --- a/libselinux/src/label_file.c +++ b/libselinux/src/label_file.c @@ -1237,7 +1237,7 @@ out: return lr; } -static enum selabel_cmp_result incomp(struct spec *spec1, struct spec *spec2, const char *reason, int i, int j) +static enum selabel_cmp_result incomp(const struct spec *spec1, const struct spec *spec2, const char *reason, int i, int j) { selinux_log(SELINUX_INFO, "selabel_cmp: mismatched %s on entry %d: (%s, %x, %s) vs entry %d: (%s, %x, %s)\n", @@ -1247,21 +1247,21 @@ static enum selabel_cmp_result incomp(struct spec *spec1, struct spec *spec2, co return SELABEL_INCOMPARABLE; } -static enum selabel_cmp_result cmp(struct selabel_handle *h1, - struct selabel_handle *h2) +static enum selabel_cmp_result cmp(const struct selabel_handle *h1, + const struct selabel_handle *h2) { - struct saved_data *data1 = (struct saved_data *)h1->data; - struct saved_data *data2 = (struct saved_data *)h2->data; + const struct saved_data *data1 = (const struct saved_data *)h1->data; + const struct saved_data *data2 = (const struct saved_data *)h2->data; unsigned int i, nspec1 = data1->nspec, j, nspec2 = data2->nspec; - struct spec *spec_arr1 = data1->spec_arr, *spec_arr2 = data2->spec_arr; - struct stem *stem_arr1 = data1->stem_arr, *stem_arr2 = data2->stem_arr; + const struct spec *spec_arr1 = data1->spec_arr, *spec_arr2 = data2->spec_arr; + const struct stem *stem_arr1 = data1->stem_arr, *stem_arr2 = data2->stem_arr; bool skipped1 = false, skipped2 = false; i = 0; j = 0; while (i < nspec1 && j < nspec2) { - struct spec *spec1 = &spec_arr1[i]; - struct spec *spec2 = &spec_arr2[j]; + const struct spec *spec1 = &spec_arr1[i]; + const struct spec *spec2 = &spec_arr2[j]; /* * Because sort_specs() moves exact pathnames to the @@ -1297,8 +1297,8 @@ static enum selabel_cmp_result cmp(struct selabel_handle *h1, if (spec2->stem_id == -1 && spec1->stem_id != -1) return incomp(spec1, spec2, "stem_id", i, j); if (spec1->stem_id != -1 && spec2->stem_id != -1) { - struct stem *stem1 = &stem_arr1[spec1->stem_id]; - struct stem *stem2 = &stem_arr2[spec2->stem_id]; + const struct stem *stem1 = &stem_arr1[spec1->stem_id]; + const struct stem *stem2 = &stem_arr2[spec2->stem_id]; if (stem1->len != stem2->len || strncmp(stem1->buf, stem2->buf, stem1->len)) return incomp(spec1, spec2, "stem", i, j); diff --git a/libselinux/src/label_internal.h b/libselinux/src/label_internal.h index 273a630a..bc5a6928 100644 --- a/libselinux/src/label_internal.h +++ b/libselinux/src/label_internal.h @@ -98,8 +98,8 @@ struct selabel_handle { const char *key, const char **aliases, int type); - enum selabel_cmp_result (*func_cmp)(struct selabel_handle *h1, - struct selabel_handle *h2); + enum selabel_cmp_result (*func_cmp)(const struct selabel_handle *h1, + const struct selabel_handle *h2); /* supports backend-specific state information */ void *data; From patchwork Mon Aug 14 13:20:06 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: 13352842 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 424D4C04FE2 for ; Mon, 14 Aug 2023 13:21:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230305AbjHNNVS (ORCPT ); Mon, 14 Aug 2023 09:21:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231129AbjHNNUw (ORCPT ); Mon, 14 Aug 2023 09:20:52 -0400 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6ED121997 for ; Mon, 14 Aug 2023 06:20:46 -0700 (PDT) Received: by mail-ej1-x635.google.com with SMTP id a640c23a62f3a-99bfcf4c814so578917066b.0 for ; Mon, 14 Aug 2023 06:20:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019245; x=1692624045; 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=zldRw8Qz99PhYJpeHb62+a0eKAIAXQBkbyHdUHPmJps=; b=jcFBX/EyVx3WnMtyhERuE+vwPe5u96DpEgen4kDx/L+mrGo4wvuCeY0xCQNUYpTiFe IJo2XzWkwmDmsFBvR+4h5eFwCN4X07dnIUmanJ3f9O3TtGSM4Me54zUECJbYLLnHj+WD L3unTp270LvoVXXWK9xJpQyd/wMv0qHZdpwVNtRPZAUanTkirFQc8G6JmgXs7VWPpF0q f61cO6wQp1XXwzIH4E5kk2Lnw9I77A6hDZMQmeGvX5q4Ki6qgECorxh34PNvUIgbg9jk uZnOPJRZ19aw6jpuFZ0gA4L+ya1QeRkBZIHbSYzetPzsozyaLUvFeCHB7Dl7ZKHviTkm ycfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019245; x=1692624045; 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=zldRw8Qz99PhYJpeHb62+a0eKAIAXQBkbyHdUHPmJps=; b=AZvpZlb8J6pA/3kX2hZQ3U4gSfFiq4GvIa2Akf6mBqsdstNYbLJh2u9EMBHSOJ6Awa 7AJqWu/U9UyV7GLdMw35yPD70g66Od0Jp+0mo7T63TGC/yJj0tnhGTtawuGEhu0D69xE 7wImkp+qMzEoOXEtamiAb4zKUBK4uKD+uqhn5z3fCE7VGBJq8aNMEe0euahtkqYKobQB 4dqsX9qVIP+yG3yjwOGex8E8oLMnfv4LVVVLlt4ZgIgvPlp8obF3t1IA4c39w5CBP8t2 LBbfCJxVZOzh0ap9ysF9owsWP0230ZW5A3ZDt9EJs6zY/EnQv/6drLcA1V76C1YAkb08 S57g== X-Gm-Message-State: AOJu0Ywh7a4ay6RA9Y1J2FqoP9xtyr5Nz4B1BHDptXHmmn+/Gcx71EcW xK6m3J1CZujDbnobWSgRx4MKKmyPmHmwJM0WHOE= X-Google-Smtp-Source: AGHT+IH8aD/dARVEei/ay7el7lWmnm9sWEWN1tfjKmfRj+LC4K97ziI1f4zuqjjPs9NNfMdS4c4pzQ== X-Received: by 2002:a17:906:51d8:b0:994:555a:e49f with SMTP id v24-20020a17090651d800b00994555ae49fmr8179748ejk.31.1692019244665; Mon, 14 Aug 2023 06:20:44 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:44 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 08/27] libselinux: introduce reallocarray(3) Date: Mon, 14 Aug 2023 15:20:06 +0200 Message-Id: <20230814132025.45364-9-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Introduce reallocarray(3), a realloc(3) wrapper incorporating a multiplication overflow check. Add private implementation in case the function is not provided by the standard C library. Use in appropriate locations. Signed-off-by: Christian Göttsche --- libselinux/src/Makefile | 6 ++++++ libselinux/src/get_context_list.c | 2 +- libselinux/src/matchpathcon.c | 4 ++-- libselinux/src/selinux_internal.c | 16 ++++++++++++++++ libselinux/src/selinux_internal.h | 4 ++++ libselinux/src/selinux_restorecon.c | 3 +-- 6 files changed, 30 insertions(+), 5 deletions(-) diff --git a/libselinux/src/Makefile b/libselinux/src/Makefile index 20d79312..ac656257 100644 --- a/libselinux/src/Makefile +++ b/libselinux/src/Makefile @@ -108,6 +108,12 @@ ifeq (yes,$(shell printf '${H}include \nint main(void){char*d,*s;strlc override CFLAGS += -DHAVE_STRLCPY endif +# check for reallocarray(3) availability +H := \# +ifeq (yes,$(shell printf '${H}include \nint main(void){reallocarray(NULL, 0, 0);return 0;}' | $(CC) -x c -o /dev/null - >/dev/null 2>&1 && echo yes)) +override CFLAGS += -DHAVE_REALLOCARRAY +endif + SWIG_CFLAGS += -Wno-error -Wno-unused-variable -Wno-unused-but-set-variable -Wno-unused-parameter \ -Wno-shadow -Wno-uninitialized -Wno-missing-prototypes -Wno-missing-declarations \ -Wno-deprecated-declarations diff --git a/libselinux/src/get_context_list.c b/libselinux/src/get_context_list.c index 0342823c..9dafa519 100644 --- a/libselinux/src/get_context_list.c +++ b/libselinux/src/get_context_list.c @@ -272,7 +272,7 @@ static int get_context_user(FILE * fp, continue; } if (security_check_context(usercon_str2) == 0) { - new_reachable = realloc(*reachable, (*nreachable + 2) * sizeof(char *)); + new_reachable = reallocarray(*reachable, *nreachable + 2, sizeof(char *)); if (!new_reachable) { context_free(usercon); rc = -1; diff --git a/libselinux/src/matchpathcon.c b/libselinux/src/matchpathcon.c index 9a9bafb0..a1c5b0cc 100644 --- a/libselinux/src/matchpathcon.c +++ b/libselinux/src/matchpathcon.c @@ -95,8 +95,8 @@ static int add_array_elt(char *con) if (con_array_size) { while (con_array_used >= con_array_size) { con_array_size *= 2; - tmp = (char **)realloc(con_array, sizeof(char*) * - con_array_size); + tmp = (char **)reallocarray(con_array, con_array_size, + sizeof(char*)); if (!tmp) { free_array_elts(); return -1; diff --git a/libselinux/src/selinux_internal.c b/libselinux/src/selinux_internal.c index c2be7c0a..9f1aff40 100644 --- a/libselinux/src/selinux_internal.c +++ b/libselinux/src/selinux_internal.c @@ -1,5 +1,7 @@ #include "selinux_internal.h" +#include +#include #include @@ -16,3 +18,17 @@ size_t strlcpy(char *dest, const char *src, size_t size) return ret; } #endif /* HAVE_STRLCPY */ + +#ifndef HAVE_REALLOCARRAY +void *reallocarray(void *ptr, size_t nmemb, size_t size) +{ + + if (size && nmemb > -1 / size) { + errno = ENOMEM; + return 0; + } + + return realloc(ptr, nmemb * size); + +} +#endif /* HAVE_REALLOCARRAY */ diff --git a/libselinux/src/selinux_internal.h b/libselinux/src/selinux_internal.h index 06f2c038..af69ff04 100644 --- a/libselinux/src/selinux_internal.h +++ b/libselinux/src/selinux_internal.h @@ -98,4 +98,8 @@ extern int has_selinux_config ; size_t strlcpy(char *dest, const char *src, size_t size); #endif +#ifndef HAVE_REALLOCARRAY +void *reallocarray(void *ptr, size_t nmemb, size_t size); +#endif + #endif /* SELINUX_INTERNAL_H_ */ diff --git a/libselinux/src/selinux_restorecon.c b/libselinux/src/selinux_restorecon.c index 7ef2d45d..38f10f1c 100644 --- a/libselinux/src/selinux_restorecon.c +++ b/libselinux/src/selinux_restorecon.c @@ -175,8 +175,7 @@ static int add_exclude(const char *directory, bool who) return -1; } - tmp_list = realloc(exclude_lst, - sizeof(struct edir) * (exclude_count + 1)); + tmp_list = reallocarray(exclude_lst, exclude_count + 1, sizeof(struct edir)); if (!tmp_list) goto oom; From patchwork Mon Aug 14 13:20:07 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: 13352843 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 63E89C0729B for ; Mon, 14 Aug 2023 13:21:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230387AbjHNNVT (ORCPT ); Mon, 14 Aug 2023 09:21:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231133AbjHNNUx (ORCPT ); Mon, 14 Aug 2023 09:20:53 -0400 Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EB61619A0 for ; Mon, 14 Aug 2023 06:20:46 -0700 (PDT) Received: by mail-ej1-x632.google.com with SMTP id a640c23a62f3a-99c1d03e124so530909966b.2 for ; Mon, 14 Aug 2023 06:20:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019245; x=1692624045; 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=Gz2DG6hVVCAcG3/CCzRTc/rhYhJL45RjMUpsbqCWED0=; b=kzLrl3ylMzMNMqCPp6KR95zqm+7uMLP06CqKumPN3HEo0RTTojKreRPAW93giqyWi/ 4QQUncDHxU4yAphxblJ6glxWMIJx9zx85gVxUjJ2izuJm10bm4ADiJCh4abHgNPnCkAd Y9T1VEAtVs6l0NGbnuhhRe6rAsvA7aAo3mmtGVc04eMwrwisD7ax4/kkD3z/kTn8I+Nl FXKtJCVjfSW0N6VC04cAesYOoF2D2vtV3gs8l1BcXMDVVGxHZvO3+YQvncgC1A6qfInj tjPJiv/N/Glt1SyTqdArzmIvemtf/L4ofRC2Bn/wBYUVfCRzEf8ZrIY+mE1aOVu9BZaf p/Mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019245; x=1692624045; 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=Gz2DG6hVVCAcG3/CCzRTc/rhYhJL45RjMUpsbqCWED0=; b=S+3zlsoaLe/lf76aaKFPEr/AFsEbICktWOOipzqOX13O4pvEe0ISnfssw8fUAHLulG WDsNn+2FZuA9ePXUxfQkw+efmCcnZZIu0PLj8lBF4sE4SYuSofU8ylhncKnoObibf3gf krVJ2WXNdiVCwsCelI2inLxlStbq9MhloxMkJaaFlnaSCDWUpEkcda6nKkTUZGUOza7X yrMm94xHktJSYL7WYn+QiCCq1hR8JwalMTeZYQmk9voG+Z4eoVrAaZABDlak9Sh9dEht 9DahBAkeshq/w/osYul02KfdfaPxQ9lLQAPw5o/d8fR5LFOi/mVNRv0rX+N99iIOjSzu jjbQ== X-Gm-Message-State: AOJu0YzPkAoKwpQSg+dwjNEico4PqPckrHh0GPdohowxv6Mb858+eGUW UbxpDdq/8aqp5tgV/eC5jb1TkeGk+oaMLag+1f8= X-Google-Smtp-Source: AGHT+IEIWDVdVayEamvtqEyaYHFLgLDQ8n6auVZqQ1AsyxVsDfCD92JoWO1fN1YbcbnikZu3tGBWFg== X-Received: by 2002:a17:907:7808:b0:99c:5708:496f with SMTP id la8-20020a170907780800b0099c5708496fmr6888803ejc.47.1692019245305; Mon, 14 Aug 2023 06:20:45 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:44 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 09/27] libselinux: simplify zeroing allocation Date: Mon, 14 Aug 2023 15:20:07 +0200 Message-Id: <20230814132025.45364-10-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Merge malloc(3) plus memset(3) call into calloc(3). Signed-off-by: Christian Göttsche Acked-by: James Carter --- libselinux/src/label_file.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libselinux/src/label_file.c b/libselinux/src/label_file.c index 5ac23e1f..b9be1c9d 100644 --- a/libselinux/src/label_file.c +++ b/libselinux/src/label_file.c @@ -716,10 +716,9 @@ static int selabel_subs_init(const char *path, struct selabel_digest *digest, if (! *dst) continue; - sub = malloc(sizeof(*sub)); + sub = calloc(1, sizeof(*sub)); if (! sub) goto err; - memset(sub, 0, sizeof(*sub)); sub->src = strdup(src); if (! sub->src) @@ -1357,10 +1356,9 @@ int selabel_file_init(struct selabel_handle *rec, { struct saved_data *data; - data = (struct saved_data *)malloc(sizeof(*data)); + data = (struct saved_data *)calloc(1, sizeof(*data)); if (!data) return -1; - memset(data, 0, sizeof(*data)); rec->data = data; rec->func_close = &closef; From patchwork Mon Aug 14 13:20:08 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: 13352837 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 F125BC04FDF for ; Mon, 14 Aug 2023 13:21:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230268AbjHNNVO (ORCPT ); Mon, 14 Aug 2023 09:21:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231183AbjHNNUz (ORCPT ); Mon, 14 Aug 2023 09:20:55 -0400 Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [IPv6:2a00:1450:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98AC310DE for ; Mon, 14 Aug 2023 06:20:47 -0700 (PDT) Received: by mail-ej1-x633.google.com with SMTP id a640c23a62f3a-99c4923195dso588535266b.2 for ; Mon, 14 Aug 2023 06:20:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019246; x=1692624046; 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=X8nTYQ0/hIFZJNZrMpjQuMCmggV7Ae4denN2XBUk8J8=; b=VIR78ilkKepLiwf2XVEpXrmPsZxcinUnXi4vmwv+P/QJfBKTrYb+G1VRE4jFpVXdZs q6s5mmVMETMITv6ebVJCqq168AjAAbdOtAIfRbcBPq52wmW6rQnf7TfiH1kfNcq9h4rD nrPEPK7Dvc1okUjrLW8YUJDOHxQ7WJK0GzHHDF/hjDf4T2luP5Bt0Djq6NkT5YCy9PaN 6BuFuUrPWpoenNeKYu8oX0lRvY122tPBUPQzPq9RpY8R/OG3dobjZi89FG8zAClGCUWU ksUH+Y3SfY04wzaDaW8Y/B1v56J0ZUS5TE6VVAzOvmAlheSFd5vb+nBLi1wiU1gz30zU JUYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019246; x=1692624046; 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=X8nTYQ0/hIFZJNZrMpjQuMCmggV7Ae4denN2XBUk8J8=; b=GnnCZd0fUhIZYNhZIwU38aKVUpFzlYKSFY/GYHxtTmbDlhaprmYnX/0Nkgc0Fy+2QK aI3mxhDzXAEaXBEP0qaD/G6Y8twcvMWaKjDwnl/MLm41H2DsbqQdPHP1uub/bnx3OlXs ec+nD1amFqp7Et77GaDPbLZ4GkdXEPD5BH+PzQdLYMSpNnJVgp3UTTzVk4wiadeb1/CM MrQ7WGliTD6jWQjPZpG1t2nuUSl0rRZd3COdggjCvbNG1OeEoEtwCf6xHPBB8slg2wLe TpoYddacO0oL7A7xtFxZnLEkKuzjQnuaiUjxizjXH4ZTcU/PvT3qdbciQCstZcrWbHc3 3fbg== X-Gm-Message-State: AOJu0YwHP7B57B6oi2vb2g42vu2u0+3m1568qFcuNp7E4aSdXNnae+NV L/aiUGtWQ+TxBARNphVmUk7EwXMywhCi6AoJFyM= X-Google-Smtp-Source: AGHT+IH5au9XceKK4sddMm1iZgoa5Pp6WcpSocKALWjJdzu5Y4fRYII3/U2gUPf5bGZVwJowQaW+bA== X-Received: by 2002:a17:906:cc4e:b0:99c:e38d:e484 with SMTP id mm14-20020a170906cc4e00b0099ce38de484mr8090958ejb.6.1692019245830; Mon, 14 Aug 2023 06:20:45 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:45 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 10/27] libselinux: introduce selabel_nuke Date: Mon, 14 Aug 2023 15:20:08 +0200 Message-Id: <20230814132025.45364-11-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Introduce a helper to remove SELinux file contexts. Mainly for testing label operations and only for SELinux disabled systems, since removing file contexts is not supported by SELinux. Signed-off-by: Christian Göttsche --- libselinux/utils/.gitignore | 1 + libselinux/utils/selabel_nuke.c | 134 ++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 libselinux/utils/selabel_nuke.c diff --git a/libselinux/utils/.gitignore b/libselinux/utils/.gitignore index b3311360..a92e1e94 100644 --- a/libselinux/utils/.gitignore +++ b/libselinux/utils/.gitignore @@ -20,6 +20,7 @@ selabel_digest selabel_get_digests_all_partial_matches selabel_lookup selabel_lookup_best_match +selabel_nuke selabel_partial_match selinux_check_securetty_context selinuxenabled diff --git a/libselinux/utils/selabel_nuke.c b/libselinux/utils/selabel_nuke.c new file mode 100644 index 00000000..b6a2df66 --- /dev/null +++ b/libselinux/utils/selabel_nuke.c @@ -0,0 +1,134 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + + +#define XATTR_NAME_SELINUX "security.selinux" + + +static void usage(const char *progname) +{ + fprintf(stderr, "usage: %s [-nrv] \n", progname); +} + +static void nuke(int atfd, const char *path, const char *fullpath, bool dry_run, bool recursive, bool verbose) +{ + ssize_t ret; + int fd, rc; + DIR *dir; + + ret = lgetxattr(fullpath, XATTR_NAME_SELINUX, NULL, 0); + if (ret <= 0) { + if (errno != ENODATA && errno != ENOTSUP) + fprintf(stderr, "Failed to get SELinux label of %s: %m\n", fullpath); + else if (verbose) + printf("Failed to get SELinux label of %s: %m\n", fullpath); + } else { + if (dry_run) { + printf("Would remove SELinux label of %s\n", fullpath); + } else { + if (verbose) + printf("Removing label of %s\n", fullpath); + + rc = lremovexattr(fullpath, XATTR_NAME_SELINUX); + if (rc < 0) + fprintf(stderr, "Failed to remove SELinux label of %s: %m\n", fullpath); + } + } + + if (!recursive) + return; + + fd = openat(atfd, path, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); + if (fd < 0) { + if (errno != ENOTDIR) + fprintf(stderr, "Failed to open %s: %m\n", fullpath); + return; + } + + dir = fdopendir(fd); + if (!dir) { + fprintf(stderr, "Failed to open directory %s: %m\n", fullpath); + close(fd); + return; + } + + while (true) { + const struct dirent *entry; + char *nextfullpath; + + errno = 0; + entry = readdir(dir); + if (!entry) { + if (errno) + fprintf(stderr, "Failed to iterate directory %s: %m\n", fullpath); + break; + } + + if (entry->d_name[0] == '.' && (entry->d_name[1] == '\0' || (entry->d_name[1] == '.' && entry->d_name[2] == '\0'))) + continue; + + rc = asprintf(&nextfullpath, "%s/%s", strcmp(fullpath, "/") == 0 ? "" : fullpath, entry->d_name); + if (rc < 0) { + fprintf(stderr, "Out of memory!\n"); + closedir(dir); + return; + } + + nuke(dirfd(dir), entry->d_name, nextfullpath, dry_run, recursive, verbose); + + free(nextfullpath); + } + + closedir(dir); +} + + +int main(int argc, char *argv[]) +{ + bool dry_run = false, recursive = false, verbose = false; + int c; + + while ((c = getopt(argc, argv, "nrv")) != -1) { + switch (c) { + case 'n': + dry_run = true; + break; + case 'r': + recursive = true; + break; + case 'v': + verbose = true; + break; + default: + usage(argv[0]); + return EXIT_FAILURE; + } + } + + if (optind >= argc) { + usage(argv[0]); + return EXIT_FAILURE; + } + + if (is_selinux_enabled()) { + fprintf(stderr, "Removing SELinux attributes on a SELinux enabled system is not supported!\n"); + return EXIT_FAILURE; + } + + for (int index = optind; index < argc; index++) + nuke(AT_FDCWD, argv[index], argv[index], dry_run, recursive, verbose); + + return EXIT_SUCCESS; +} From patchwork Mon Aug 14 13:20:09 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: 13352836 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 D4F02C04E69 for ; Mon, 14 Aug 2023 13:21:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230269AbjHNNVP (ORCPT ); Mon, 14 Aug 2023 09:21:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231197AbjHNNU4 (ORCPT ); Mon, 14 Aug 2023 09:20:56 -0400 Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E9E5510DD for ; Mon, 14 Aug 2023 06:20:47 -0700 (PDT) Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-99bc512526cso570389966b.1 for ; Mon, 14 Aug 2023 06:20:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019246; x=1692624046; 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=CfrxVDE9VstbWj4fn7sebiRQEdVJJz4CbC0Rg1wJlB8=; b=ddS8uRXQMS/0i4xbF2X28t3MSNGAJvJmn16Yn7+YXKiJRLqllELCZDoaBxwY2VRocS jzy/YnVbqlScZMoBt0j/8++m57LCteVHh/nw0VRFQSF/3QsY7phpBAJ0FzILc1/WX1D/ tE540K18LEhlM7LibknfxDPHrP23L9K0XBqUZAPGl1h+1H3mmzf5SMyGyLrITM7SQDnf /2oyIvhlCs5bZwODUIk6nQphwydtneDwtTGH+sHuq6LKR6knzq2dRmE+TQKihVJMdSn9 /STaRypMk3X/TzqxuTN58KQU94RfxL4lvBGkMlVPHnjacXHaFvKC4+zUNsE1h7MDgbdM 8H9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019246; x=1692624046; 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=CfrxVDE9VstbWj4fn7sebiRQEdVJJz4CbC0Rg1wJlB8=; b=EnvlSmwT6lYeh6iBDqDR7tvyEiJYKKquNjkVEIhQZYM5Qxb/TrenXW9aGySa1gDI8B jsRryUaiwYbt4ULRdbg9Thrb4L7vjOKBCSs/t6NbeMMj3YFEXWHWZVfiX3T/ASBU2ZbZ 2dIxTvLlX0PC2RuEWwTq8vD//9hUE87oKYqbb+XKHfRHVW6FGtu4H9bC1CHgMrMcPs8x t6VY9gCcI+LJaHxd0ug73S4lLrE8oOYccTCh4a53RAsgI/Bh39UYYSmhig8M8xxxwFp8 xeNohyFM9ZvK2jPUvCzAldA6v7fSdxOSLPxe8CPaH978LSQUHMvWPI39PnRjUYdRLz/T 0U0Q== X-Gm-Message-State: AOJu0YzX9AYgwcaj/2T4l0v+TqF8kX+VkVsPLFE4Ne9kj3v0PeZZFBsE J25b9nDyrUM0zk9+28poq6BRjXfQAs0800StQ1U= X-Google-Smtp-Source: AGHT+IFG5zB+wsdXPOf4JOyb4AJT14lsVia6KJpNmG0OjrFmnzni8IOyu5l1j0j33Wh+HQDiQ8Hn/A== X-Received: by 2002:a17:906:73d5:b0:99b:4bab:2838 with SMTP id n21-20020a17090673d500b0099b4bab2838mr7844631ejl.0.1692019246355; Mon, 14 Aug 2023 06:20:46 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:46 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 11/27] libselinux/utils: use type safe union assignment Date: Mon, 14 Aug 2023 15:20:09 +0200 Message-Id: <20230814132025.45364-12-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org selinux_check_access.c:43:10: warning: cast to union type is a GNU extension [-Wgnu-union-cast] 43 | (union selinux_callback)cb_auditinfo); | ^ ~~~~~~~~~~~~ Signed-off-by: Christian Göttsche Acked-by: James Carter --- libselinux/utils/selinux_check_access.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libselinux/utils/selinux_check_access.c b/libselinux/utils/selinux_check_access.c index 6cea40e9..f7b27a85 100644 --- a/libselinux/utils/selinux_check_access.c +++ b/libselinux/utils/selinux_check_access.c @@ -40,7 +40,7 @@ int main(int argc, char **argv) if (audit_msg) selinux_set_callback(SELINUX_CB_AUDIT, - (union selinux_callback)cb_auditinfo); + (union selinux_callback) { .func_audit = cb_auditinfo }); rc = selinux_check_access(argv[optind], argv[optind + 1], argv[optind + 2], argv[optind + 3], From patchwork Mon Aug 14 13:20:10 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: 13352857 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 4EBAAC25B5C for ; Mon, 14 Aug 2023 13:21:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231186AbjHNNVZ (ORCPT ); Mon, 14 Aug 2023 09:21:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60972 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231207AbjHNNU5 (ORCPT ); Mon, 14 Aug 2023 09:20:57 -0400 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6BF5512D for ; Mon, 14 Aug 2023 06:20:48 -0700 (PDT) Received: by mail-ej1-x62f.google.com with SMTP id a640c23a62f3a-99d6d5054bcso796447066b.1 for ; Mon, 14 Aug 2023 06:20:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019247; x=1692624047; 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=qE3ABhVnQhCHOxyYzH8zSMrW/8rvEczqQ6pQab+oN9Q=; b=TI6Ia8qHdzH50ekJRoPl/2//CSmA8C4rh+Zrq5zMHYBuHyI3uFuo8m11YvXs8Tl+XC hSUok8GkEa+uv/75XXOagNRDVL6tgI7RT5LCqqQLbUfJZGRWa340Hd05jfN2FJrEdbrU MgWlXH3heLTCxnSOvCzq1nrJZLj2yrF6oKyCyCyrAEko8HOHZeRg81uWtkSyFqR5Nefg 1tcNW8/d3uQIkN2Ry+VYyKGRfV87ndJIh21ZvvPSnNdNEXoVDt8JjGHRjSnDGFgMpF70 eAuLablkCniaKbCBqUxPpCNVYHzKa7S9SauWwIJj+jNMP0CYsQ1DvnlJp4w9I3A1gHx2 GK0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019247; x=1692624047; 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=qE3ABhVnQhCHOxyYzH8zSMrW/8rvEczqQ6pQab+oN9Q=; b=Gy7wdiy4TClMlOlJ4vxsgAB0Iut8vzgjVhCEB2cUXtGKR04DQzxHs55pODX1h4hdcC IZFILj1oHx663Y2KKzlNZx8l6kFZ0AzQdIPLTkMo7WQMyR8F560CnBKHq6TRRFCPX1s8 NX/kGDDpEMOKS/4haAXtkMjN+95t7QUmowWL+hXi5audrOk/WQLpUJkNyQGDfUfr49G+ pFswgHGOBRi3+pPDXjWW31mj5qyZ6nFq1Bq3Vu5TwxLALVkp5aTsh1LzK1mTC/hBQOnt XBxFy66mKxZVhYCMyHArra7tMMZoHr6fCehRIVW7nyVjfznAVduIAOgIbbX1YmKVvNHQ 9OsA== X-Gm-Message-State: AOJu0YwBWOwbb8ClirivhmyJ+X5WLvvX3LrGs2XzL/M431keCt8aIWfk 09JmSselKEKbrrzdX3z+DKhaQzUuh5xp114VjKo= X-Google-Smtp-Source: AGHT+IGzP3SdIbSVKrtYDIAlCIt4ib4QysDU4hw71srKx7SPxVGrE2gDzwFN3peAAtgx/hHQ75OWMQ== X-Received: by 2002:a17:907:9623:b0:98e:1c4b:10e2 with SMTP id gb35-20020a170907962300b0098e1c4b10e2mr15145124ejc.20.1692019246863; Mon, 14 Aug 2023 06:20:46 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:46 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 12/27] libselinux: avoid regex serialization truncations Date: Mon, 14 Aug 2023 15:20:10 +0200 Message-Id: <20230814132025.45364-13-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Check (for the probably impossible) case the serialized data is longer than the compiled fcontext format supports. Signed-off-by: Christian Göttsche Acked-by: James Carter --- libselinux/src/regex.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libselinux/src/regex.c b/libselinux/src/regex.c index 16df6790..88d82fed 100644 --- a/libselinux/src/regex.c +++ b/libselinux/src/regex.c @@ -176,7 +176,7 @@ int regex_writef(struct regex_data *regex, FILE *fp, int do_write_precompregex) /* encode the pattern for serialization */ rc = pcre2_serialize_encode((const pcre2_code **)®ex->regex, 1, &bytes, &serialized_size, NULL); - if (rc != 1) { + if (rc != 1 || serialized_size >= UINT32_MAX) { rc = -1; goto out; } From patchwork Mon Aug 14 13:20:11 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: 13352855 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 F31B2C16B13 for ; Mon, 14 Aug 2023 13:21:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230516AbjHNNVV (ORCPT ); Mon, 14 Aug 2023 09:21:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231228AbjHNNU6 (ORCPT ); Mon, 14 Aug 2023 09:20:58 -0400 Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2119E10E5 for ; Mon, 14 Aug 2023 06:20:49 -0700 (PDT) Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-99357737980so575538766b.2 for ; Mon, 14 Aug 2023 06:20:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019247; x=1692624047; 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=VNSVLz+2snj6v4+tEhoQzyNUr3LpxDI/LvJXtCV0YHg=; b=LwhoyhUA08IU5KakXBnUubLa050RqKFzdIa5xJYyISY/o20qZS2gFg/iEM0Ymn+i1j 2h2n3NiCnKEEsnf3byhiKhevw0DKU3R6LUtOfWRFwJjnruQ7Os99JRPyHAhgrbWYDSoT KR60FA6b6U7w0hauAyIYEXWyj1fP/xG4gY8tcgY9Y+GXSjXiSG4I51efr1orE4U8yxAs 2efRg6mvfordXwdoD2aZ5bnmdMkmIOvzzqJ7k9Qn+ngA3Ab3XbfLrU9111mO9jUVZCRl wGVrHug5ageL3oLdTL72qqJVa3utST7LfYPq3SIzZdmsWCSbWbkrn/jpl30cbgDGzAig En8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019247; x=1692624047; 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=VNSVLz+2snj6v4+tEhoQzyNUr3LpxDI/LvJXtCV0YHg=; b=BYxqznI9BgwGAGnN0wVfj52yboPE0/0Xr9H2FJ4zboEAx9DofnP97ZUzT46NJarf9G wq6XfavaLtAw55PdOx1M3+B0azSZaZVTU+IRMKc2rS4EnTApgRlG+6QD6IvnQs5gr0Be 0ya85zvD2gfMsezd7qhhwLFqNtp+ayuRm5nZH6waUDciDtjHR5RPoVruTaAmd+z+MTxz BcMl/Dk4BkbmyZlZ1bdPSfbCk5Ash5wNoLXuonPhwKPcGlbxbZVyWTkZipmuJxb/onqN doEXzp9iQsrK8kRVq/hCLMxlKtcMzemtz8NusYhj1ws5OqqAOfpi9aKCPzxAelQXUZnE JaJg== X-Gm-Message-State: AOJu0YwpM/HPfdr9o1l9ocXO5LYrc+K/EGs+TA7/osUXv5HPTKicvJk7 f5zxaf2vUp9fxa9Cw+B8Z/nwTcWYT46/C4TTu/U= X-Google-Smtp-Source: AGHT+IFe2MdPtRGAwmq8mt29apR/xMueO4iDTK1+sfkGJE0GKzjI0qfm7bKj5CUIVgWpmJSfFxlY8g== X-Received: by 2002:a17:906:292a:b0:99b:d1d8:d747 with SMTP id v10-20020a170906292a00b0099bd1d8d747mr8644283ejd.27.1692019247562; Mon, 14 Aug 2023 06:20:47 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:47 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 13/27] libselinux/utils: introduce selabel_compare Date: Mon, 14 Aug 2023 15:20:11 +0200 Message-Id: <20230814132025.45364-14-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Add a utility around selabel_cmp(3). Can be used by users to compare a pre-compiled fcontext file to an original text-based file context definition file. Can be used for development to verify compilation and parsing of the pre-compiled fcontext format works correctly. Signed-off-by: Christian Göttsche --- libselinux/utils/.gitignore | 1 + libselinux/utils/selabel_compare.c | 119 +++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 libselinux/utils/selabel_compare.c diff --git a/libselinux/utils/.gitignore b/libselinux/utils/.gitignore index a92e1e94..4e2dfba8 100644 --- a/libselinux/utils/.gitignore +++ b/libselinux/utils/.gitignore @@ -16,6 +16,7 @@ getseuser matchpathcon policyvers sefcontext_compile +selabel_compare selabel_digest selabel_get_digests_all_partial_matches selabel_lookup diff --git a/libselinux/utils/selabel_compare.c b/libselinux/utils/selabel_compare.c new file mode 100644 index 00000000..f4325f7e --- /dev/null +++ b/libselinux/utils/selabel_compare.c @@ -0,0 +1,119 @@ +#include +#include +#include +#include + +#include + + +static void usage(const char *progname) +{ + fprintf(stderr, + "usage: %s [-b backend] [-v] file1 file2\n\n" + "Where:\n\t" + "-b The backend - \"file\", \"media\", \"x\", \"db\" or \"prop\" (defaults to \"file\")\n\t" + "-v Validate entries against loaded policy.\n\t" + "file1/file2 Files containing the specs.\n", + progname); +} + +int main(int argc, char *argv[]) +{ + unsigned int backend = SELABEL_CTX_FILE; + int opt; + const char *validate = NULL, *file1 = NULL, *file2 = NULL; + + if (argc < 3) { + usage(argv[0]); + return EXIT_FAILURE; + } + + while ((opt = getopt(argc, argv, "b:v")) > 0) { + switch (opt) { + case 'b': + if (!strcasecmp(optarg, "file")) { + backend = SELABEL_CTX_FILE; + } else if (!strcmp(optarg, "media")) { + backend = SELABEL_CTX_MEDIA; + } else if (!strcmp(optarg, "x")) { + backend = SELABEL_CTX_X; + } else if (!strcmp(optarg, "db")) { + backend = SELABEL_CTX_DB; + } else if (!strcmp(optarg, "prop")) { + backend = SELABEL_CTX_ANDROID_PROP; + } else if (!strcmp(optarg, "service")) { + backend = SELABEL_CTX_ANDROID_SERVICE; + } else { + fprintf(stderr, "Unknown backend: %s\n", optarg); + usage(argv[0]); + return EXIT_FAILURE; + } + break; + case 'v': + validate = (char *)1; + break; + default: + usage(argv[0]); + return EXIT_FAILURE; + } + } + + if (argc != optind + 2) { + usage(argv[0]); + return EXIT_FAILURE; + } + + file1 = argv[optind++]; + file2 = argv[optind]; + + { + struct selabel_handle *hnd1, *hnd2; + const struct selinux_opt selabel_option1[] = { + { SELABEL_OPT_PATH, file1 }, + { SELABEL_OPT_VALIDATE, validate } + }; + const struct selinux_opt selabel_option2[] = { + { SELABEL_OPT_PATH, file2 }, + { SELABEL_OPT_VALIDATE, validate } + }; + enum selabel_cmp_result result; + + hnd1 = selabel_open(backend, selabel_option1, 2); + if (!hnd1) { + fprintf(stderr, "ERROR: selabel_open - Could not obtain handle for %s: %m\n", file1); + return EXIT_FAILURE; + } + + hnd2 = selabel_open(backend, selabel_option2, 2); + if (!hnd2) { + fprintf(stderr, "ERROR: selabel_open - Could not obtain handle for %s: %m\n", file2); + selabel_close(hnd1); + return EXIT_FAILURE; + } + + result = selabel_cmp(hnd1, hnd2); + + selabel_close(hnd2); + selabel_close(hnd1); + + switch (result) { + case SELABEL_SUBSET: + printf("spec %s is a subset of spec %s\n", file1, file2); + break; + case SELABEL_EQUAL: + printf("spec %s is equal to spec %s\n", file1, file2); + break; + case SELABEL_SUPERSET: + printf("spec %s is a superset of spec %s\n", file1, file2); + break; + case SELABEL_INCOMPARABLE: + printf("spec %s is uncompareable to spec %s\n", file1, file2); + break; + default: + fprintf(stderr, "ERROR: selabel_cmp - Unexpected result %d\n", result); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; + } +} From patchwork Mon Aug 14 13:20:12 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: 13352844 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 560D7C07E8A for ; Mon, 14 Aug 2023 13:21:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230301AbjHNNVR (ORCPT ); Mon, 14 Aug 2023 09:21:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32886 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231232AbjHNNU6 (ORCPT ); Mon, 14 Aug 2023 09:20:58 -0400 Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [IPv6:2a00:1450:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A317BE7D for ; Mon, 14 Aug 2023 06:20:49 -0700 (PDT) Received: by mail-ej1-x633.google.com with SMTP id a640c23a62f3a-997c4107d62so576360666b.0 for ; Mon, 14 Aug 2023 06:20:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019248; x=1692624048; 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=bHFpfdLtFfPwthV9DSwBs7yA5Vq97jyRJJfLzhZ2AVo=; b=HfjG5eHRUZPNWLIZLORLx8q7JY2QXY0XpDBipLF24CNraXM7DzrdjfxmDWJpXkjYwW KJPKG9kDSSNij1HPs9CoDI8RLF8xp5wA9D9Xmr5yrqOcfqrPHbfDOqK/lBDKEpk7wvTc Qu66cs7ag/AQku1KZ23WvmKa2kv3Xtb9U9C8WGwDtrGJJMus+Eo6tzliv2NgJ9pLvwE2 krtG3NNfVlwxbjHTYjP3FyN9gNWrQXBPNJAi9EObBjN72mkm1M73kUd5kgwNaI9JVqvX 7yjzRo0utVYa8s+jRsE9RczqXz53+BgDP9fVOEj/uEl1h7UOcwcmoMswQVcg7JKk1+0J K8Ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019248; x=1692624048; 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=bHFpfdLtFfPwthV9DSwBs7yA5Vq97jyRJJfLzhZ2AVo=; b=PbUKtpDGmEhI/eKUpDfXrY3HVgLHRdAnmZi2hox5bxKIam7uR2QtlrywX/etl81tqD t6eHIm5Cbyslcq6x1ItQ3GtD89NkGvJS5FBqqjBIKd5b3GzMDb4g+y7ptCnPGNv02mao ekpjOpDGrhf3oHkAbDHa9z8K2J0EOqHD1qOBW91rhRL7PJi7U+C5FErse/1vRjMtXdDB xdAbNFyIG+n6CxHZsOXlQH9V8ybjUnc4Haa5FCKZQGMU+kPXF6JDY4aACCr3xonKD7Zn qJVXxhtk0ow8RAbNAvzu+0SvQgflqm1m8lNiK7E2NV6ooxLYVby8lL5+jymNvBUtwqyi M+/w== X-Gm-Message-State: AOJu0YxTtKPfwbpSargTyftd2oG6NnEpms+3KlGQ/QvxbOwMjjxRjAs5 IX3++UWgIJeK+krqInqCZaIGMGGxCAxP4Ir1aS8= X-Google-Smtp-Source: AGHT+IGO79YrtR+HvcPGbuFJFVqy+yz1/iJFtECMd9kamcYCwycW5IKOtMGbEr4MHiFo3cUuIBEehQ== X-Received: by 2002:a17:906:3013:b0:991:d05c:f065 with SMTP id 19-20020a170906301300b00991d05cf065mr8673456ejz.52.1692019248063; Mon, 14 Aug 2023 06:20:48 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:47 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 14/27] libselinux: parameter simplifications Date: Mon, 14 Aug 2023 15:20:12 +0200 Message-Id: <20230814132025.45364-15-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Use bool where applicable. Signed-off-by: Christian Göttsche Acked-by: James Carter --- libselinux/src/label.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/libselinux/src/label.c b/libselinux/src/label.c index c0f586a2..9a972f79 100644 --- a/libselinux/src/label.c +++ b/libselinux/src/label.c @@ -137,9 +137,9 @@ out: } /* Public API helpers */ -static int selabel_fini(struct selabel_handle *rec, +static int selabel_fini(const struct selabel_handle *rec, struct selabel_lookup_rec *lr, - int translating) + bool translating) { if (compat_validate(rec, lr, rec->spec_file, lr->lineno)) return -1; @@ -152,7 +152,7 @@ static int selabel_fini(struct selabel_handle *rec, } static struct selabel_lookup_rec * -selabel_lookup_common(struct selabel_handle *rec, int translating, +selabel_lookup_common(struct selabel_handle *rec, bool translating, const char *key, int type) { struct selabel_lookup_rec *lr; @@ -173,7 +173,7 @@ selabel_lookup_common(struct selabel_handle *rec, int translating, } static struct selabel_lookup_rec * -selabel_lookup_bm_common(struct selabel_handle *rec, int translating, +selabel_lookup_bm_common(struct selabel_handle *rec, bool translating, const char *key, int type, const char **aliases) { struct selabel_lookup_rec *lr; @@ -239,7 +239,7 @@ int selabel_lookup(struct selabel_handle *rec, char **con, { struct selabel_lookup_rec *lr; - lr = selabel_lookup_common(rec, 1, key, type); + lr = selabel_lookup_common(rec, true, key, type); if (!lr) return -1; @@ -252,7 +252,7 @@ int selabel_lookup_raw(struct selabel_handle *rec, char **con, { struct selabel_lookup_rec *lr; - lr = selabel_lookup_common(rec, 0, key, type); + lr = selabel_lookup_common(rec, false, key, type); if (!lr) return -1; @@ -307,7 +307,7 @@ int selabel_lookup_best_match(struct selabel_handle *rec, char **con, return -1; } - lr = selabel_lookup_bm_common(rec, 1, key, type, aliases); + lr = selabel_lookup_bm_common(rec, true, key, type, aliases); if (!lr) return -1; @@ -325,7 +325,7 @@ int selabel_lookup_best_match_raw(struct selabel_handle *rec, char **con, return -1; } - lr = selabel_lookup_bm_common(rec, 0, key, type, aliases); + lr = selabel_lookup_bm_common(rec, false, key, type, aliases); if (!lr) return -1; From patchwork Mon Aug 14 13:20:13 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: 13352851 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 BAD1CC07E8F for ; Mon, 14 Aug 2023 13:21:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230463AbjHNNVU (ORCPT ); Mon, 14 Aug 2023 09:21:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231233AbjHNNU6 (ORCPT ); Mon, 14 Aug 2023 09:20:58 -0400 Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39CD51715 for ; Mon, 14 Aug 2023 06:20:50 -0700 (PDT) Received: by mail-ej1-x632.google.com with SMTP id a640c23a62f3a-99d937b83efso237416266b.3 for ; Mon, 14 Aug 2023 06:20:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019249; x=1692624049; 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=uFHeKtuzr80FElLaFbiAjddhDdkcDaWlinC+MHI3t3U=; b=gQm/T/R+haoBWmCpMBGCq3kZiBZEM4pT9bkHOpeLjL+kHP8+r2HimV4BjRmMrHAAu4 izdBLUPwYdF1t5W8TOlGTzyWS+J3L2nCiDlXdaGPl3Pz0xa2QIntRyuh/VGAbDmPJCcr OBlbPfcWCH7TROl9PGsX0tz1lQisXF1RJd8Cp/Pbh/6cy1FkLrePjOpHM2AMlehss7D1 smfsBogrCQnTv/QuQZwJZvnTOomNI12Awz4+pKUePhoIFiN6IaBbc8mekUrqqbJ8RTZ5 SXw/BLL+UvxG5GX9g16dmPTpfyKCWKC2n0EEiH0N2LRtqU1t5Hv337pMYEn8ZfJWXPWM UTaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019249; x=1692624049; 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=uFHeKtuzr80FElLaFbiAjddhDdkcDaWlinC+MHI3t3U=; b=JHuN5acYRAAFf+L8fwLJB9zGMM5k/4/CU8FfHTbH/f3kK0ADD0hoPVFXSP7nFT2C/3 p3N4Or/lvu1l+pMjM5C5B6p0HluBYLXBP7JxUIJOFy0iOGerv14Z6OgY6m+sokz3yG69 6XiX5UCQaOgcVg2m5YzCoDEz0D92aJ0nyZrmhUFVFYM0g3zjeq2/hiBDlk4dKyu/ACYB ksAMMSh2XWi3C+0aVFW7LST0lLWmZOGaDlUqOk7Mhvg2ROH7/npKElk1JXffbKMrKWhZ qtassdUwZVpPIQ84Mw5TysQRPpQP5mf7Etpbm5d9sCkYNYNU9cuYxMLEj+yGG3aWLQky 3piQ== X-Gm-Message-State: AOJu0YywUHbANF+updnS71opaX84eOTTMqCvtQmE2+c8pfinMygWS7ob kG3bb3YtTQ2c1Low2/mJJtYPXWhPM+jS+ei6Lu8= X-Google-Smtp-Source: AGHT+IGN3AjW7wWtjcSl1X+zBlHnGgctAJp/Ze9G9zhkkOBwFbGSJFEarScFPXq6uybpRddXw2t2Gg== X-Received: by 2002:a17:906:76d3:b0:99b:4fff:6bb with SMTP id q19-20020a17090676d300b0099b4fff06bbmr8358470ejn.4.1692019248665; Mon, 14 Aug 2023 06:20:48 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:48 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 15/27] libselinux/utils: use correct type for backend argument Date: Mon, 14 Aug 2023 15:20:13 +0200 Message-Id: <20230814132025.45364-16-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org selabel_open(3) expects the backend to be of type unsigned int. Also initialize with a macro flag instead raw 0. Signed-off-by: Christian Göttsche Acked-by: James Carter --- libselinux/utils/selabel_digest.c | 3 ++- libselinux/utils/selabel_lookup.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/libselinux/utils/selabel_digest.c b/libselinux/utils/selabel_digest.c index 6a8313a2..bf22b472 100644 --- a/libselinux/utils/selabel_digest.c +++ b/libselinux/utils/selabel_digest.c @@ -59,7 +59,8 @@ static int run_check_digest(char *cmd, char *selabel_digest) int main(int argc, char **argv) { - int backend = 0, rc, opt, validate = 0; + unsigned int backend = SELABEL_CTX_FILE; + int rc, opt, validate = 0; char *baseonly = NULL, *file = NULL, *digest = (char *)1; char **specfiles = NULL; unsigned char *sha1_digest = NULL; diff --git a/libselinux/utils/selabel_lookup.c b/libselinux/utils/selabel_lookup.c index 112ffda1..5dc32548 100644 --- a/libselinux/utils/selabel_lookup.c +++ b/libselinux/utils/selabel_lookup.c @@ -32,7 +32,8 @@ static __attribute__ ((__noreturn__)) void usage(const char *progname) int main(int argc, char **argv) { - int raw = 0, type = 0, backend = 0, rc, opt; + int raw = 0, type = 0, rc, opt; + unsigned int backend = SELABEL_CTX_FILE; char *validate = NULL, *key = NULL, *context = NULL, *file = NULL; struct selabel_handle *hnd; From patchwork Mon Aug 14 13:20:14 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: 13352848 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 ACAAEC10F00 for ; Mon, 14 Aug 2023 13:21:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230478AbjHNNVU (ORCPT ); Mon, 14 Aug 2023 09:21:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230341AbjHNNVA (ORCPT ); Mon, 14 Aug 2023 09:21:00 -0400 Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03E0A171F for ; Mon, 14 Aug 2023 06:20:51 -0700 (PDT) Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-99bcfe28909so559406566b.3 for ; Mon, 14 Aug 2023 06:20:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019249; x=1692624049; 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=LhEgKOukXoe4fbpEkAYVhQZTs/qWaEAxjlXCbbr7rtY=; b=KhjLHXN3hspGz/IiLbYwuaj2N0uGPCUXKPNcVHopI79tHApYKUEvyjf2fQe0GN+Bvx vIcrt5Mx4z1RJA7BKQ+l7hnhZ4zppRU2aadVHY5CW6fRM3F4mZC9Y+t4fjpKpY6QYJER tO1JfB9x/JA5Ag7fhXBP0iIVvX1eE1WpgDUB7dP/DqaKSfkwmAJrEs0Mtt9tqYKrZqXt Op6FJchyiEL+9iWQs3RsYt+oc6qLa7sRH0ywQ3vp3BO66mM/o3/gfu33uxcBop0O4b44 6OXA0zagdI4QSwmR00jA3k0iaTK/L0JqyskoVkFo5tgNGyoBmxuJKyjIIfW+U4FYVqiW silg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019249; x=1692624049; 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=LhEgKOukXoe4fbpEkAYVhQZTs/qWaEAxjlXCbbr7rtY=; b=jaxnwqFMMuXeXFhU+4w7t8p4SaWizDVzDQ1ZRRuHfuM59RprsNbXD/ZNtt8E9644xH rFjwd3XQcpvAZXo245TLjeZeOjjZ/7Hvug//8u96/9sWEecBuOXxyYoLJybt3lMO8C5I JpHl9hzoZQ1dzHXzTl4KzK3j2gVdQByEn2nqPVaLjm/ETYLXRHuAuHHX8hfq4Pj7gZA6 iZxoFemFGgVongedwT9ldRqIQRqGrEQU697d+3izXZGOj+1gVZfqlqcjAJlfPHEHXaFG c+R7adVgLFG//NGlTexGP2YfkmsxbRlAWR5Z6lZCNoW9N23vdw2wfXIDZYRMPxD54Q6W IgTQ== X-Gm-Message-State: AOJu0YxmPlWEfSBpc+tEetCuH1WzsGj6t2Q8BTnUybQTlUam4Qig3TtY Uexeaw8wkNe1w0aucExniyXOMf7+q2Zlz0i06Zc= X-Google-Smtp-Source: AGHT+IGAWhunf0geHzIIssYzd0TGJBXBbBINn4SW9d1VDZBVzIcOR90C2ggNuaox3+ZFBhogPu7MsQ== X-Received: by 2002:a17:906:51d7:b0:97c:64bd:50a5 with SMTP id v23-20020a17090651d700b0097c64bd50a5mr7625476ejk.53.1692019249325; Mon, 14 Aug 2023 06:20:49 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:48 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 16/27] libselinux: update string_to_mode() Date: Mon, 14 Aug 2023 15:20:14 +0200 Message-Id: <20230814132025.45364-17-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Drop parameter NULL check since the only caller does a NULL check on the argument. Avoid strlen(3) call by comparing by hand. Drop unreachable return statement. Signed-off-by: Christian Göttsche Acked-by: James Carter --- libselinux/src/label_file.h | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/libselinux/src/label_file.h b/libselinux/src/label_file.h index 190bc175..1363c83c 100644 --- a/libselinux/src/label_file.h +++ b/libselinux/src/label_file.h @@ -97,15 +97,10 @@ struct saved_data { struct selabel_sub *subs; }; -static inline mode_t string_to_mode(char *mode) +static inline mode_t string_to_mode(const char *mode) { - size_t len; - - if (!mode) - return 0; - len = strlen(mode); - if (mode[0] != '-' || len != 2) - return -1; + if (mode[0] != '-' || mode[1] == '\0' || mode[2] != '\0') + return (mode_t)-1; switch (mode[1]) { case 'b': return S_IFBLK; @@ -122,10 +117,8 @@ static inline mode_t string_to_mode(char *mode) case '-': return S_IFREG; default: - return -1; + return (mode_t)-1; } - /* impossible to get here */ - return 0; } static inline int grow_specs(struct saved_data *data) From patchwork Mon Aug 14 13:20:15 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: 13352838 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 C6A2DC41513 for ; Mon, 14 Aug 2023 13:21:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230261AbjHNNVN (ORCPT ); Mon, 14 Aug 2023 09:21:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50138 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231235AbjHNNVB (ORCPT ); Mon, 14 Aug 2023 09:21:01 -0400 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A6BF1722 for ; Mon, 14 Aug 2023 06:20:51 -0700 (PDT) Received: by mail-ej1-x631.google.com with SMTP id a640c23a62f3a-99bdeae1d0aso575376266b.1 for ; Mon, 14 Aug 2023 06:20:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019250; x=1692624050; 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=zMXhmSjjU0BUkSzUeTbPfordFh728nv3OcCPvR1zI+0=; b=pxnjmHuuCPYmWL1ubnci8b3PTnAhOVWq5Jm0XQfoKXOEFcUsAN/HGHHY6rjAN/oog6 mSVXaIoXDk7dOWPVNw6oL1cQ1z+9tKk7FJroeUxkuy+zFSK3mSPg31yrCPcwFSelCibT wygD1z5k93NDaFD/3m8OEXL7RjjxhFrmBITBSCpBRT6404mV0USasZTZymPxIIp18XY/ 5jvPFJXqQYm16JzMawWKPx3fFz3ut8ItBsxEbgGHWdhr6bDXIXxuWAbLAy3H+PXVWpl7 C9+KRRUZPJeJ6MQKAstx/dRXGVm/gvo+iM+Ve1qQSB3Qvow6JM21+PUEmqweGFfMubJH SyxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019250; x=1692624050; 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=zMXhmSjjU0BUkSzUeTbPfordFh728nv3OcCPvR1zI+0=; b=jXLeuijk4zimr8yuUDJ2WhTfHTWKFgz93WLKNFd6yHSg2on5lm90xzhtJoWDZ+9Bql R9c3W5FU5EwIy+lqxy+Q2rmIy8pel4O8Wpd0Chk/qqdRUIGX9CEDXAQs2YjfGv0ClozG 7BHgp3xg6QXJxOyH27MAp7F7WW2t7ndeyJZSeCvwAkv1ELwXvkNT2KHEqr1zfQTY9NfZ l+O1YR7qLYQZVt7nSPsJF1MJHD/hAq3sNGjmuLW+sV4+NAlQZaTwzmoiTnA81cUJT0NL hOls3ptXRTxcLy8WaPZmcblr+w76Owe6LDnhxXxqt1QUaYwOOmBjic6ApWV7v+LoAG0K X8DQ== X-Gm-Message-State: AOJu0YzpT8UXwmdqfVOG1RBYb5k3jw0D3KIONqBNAyiBDInh7CKktoTe 83dOSES/JOGSlB4Q5KtYwYssFmjxfkQO8tQmEC4= X-Google-Smtp-Source: AGHT+IHapC3tcZI39DoW+vc12OIOtuPcasqUkcuexSRRDVFzjuLjYMKpd/tqD5ObfeNvTO+ZI7jk1A== X-Received: by 2002:a17:906:73dc:b0:99d:101b:8403 with SMTP id n28-20020a17090673dc00b0099d101b8403mr7679791ejl.36.1692019249870; Mon, 14 Aug 2023 06:20:49 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:49 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 17/27] libselinux: remove SELABEL_OPT_SUBSET support from selabel_file(5) Date: Mon, 14 Aug 2023 15:20:15 +0200 Message-Id: <20230814132025.45364-18-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org The selabel_file(5) option SELABEL_OPT_SUBSET has been deprecated in commit 26e05da0fc2d ("libselinux: matchpathcon/selabel_file: Fix man pages.") for version 2.5. Drop the support to easy refactoring the selabel_file related code. Signed-off-by: Christian Göttsche --- libselinux/include/selinux/label.h | 2 +- libselinux/include/selinux/selinux.h | 6 +++++- libselinux/src/Makefile | 4 ++++ libselinux/src/label_file.c | 19 ++++++++----------- libselinux/src/label_file.h | 13 ++----------- libselinux/src/matchpathcon.c | 4 +--- libselinux/utils/matchpathcon.c | 11 ++--------- libselinux/utils/sefcontext_compile.c | 3 +-- 8 files changed, 24 insertions(+), 38 deletions(-) diff --git a/libselinux/include/selinux/label.h b/libselinux/include/selinux/label.h index ce189a3a..6cb2d782 100644 --- a/libselinux/include/selinux/label.h +++ b/libselinux/include/selinux/label.h @@ -50,7 +50,7 @@ struct selabel_handle; #define SELABEL_OPT_BASEONLY 2 /* specify an alternate path to use when loading backend data */ #define SELABEL_OPT_PATH 3 -/* select a subset of the search space as an optimization (file backend) */ +/* Unsupported since v3.6: select a subset of the search space as an optimization (file backend) */ #define SELABEL_OPT_SUBSET 4 /* require a hash calculation on spec files */ #define SELABEL_OPT_DIGEST 5 diff --git a/libselinux/include/selinux/selinux.h b/libselinux/include/selinux/selinux.h index a0948853..3b23cb50 100644 --- a/libselinux/include/selinux/selinux.h +++ b/libselinux/include/selinux/selinux.h @@ -484,7 +484,11 @@ extern int matchpathcon_init(const char *path) /* Same as matchpathcon_init, but only load entries with regexes that have stems that are prefixes of 'prefix'. */ -extern int matchpathcon_init_prefix(const char *path, const char *prefix); +extern int matchpathcon_init_prefix(const char *path, const char *prefix) +#ifdef __GNUC__ + __attribute__ ((deprecated("Use selabel_open with backend SELABEL_CTX_FILE"))) +#endif +; /* Free the memory allocated by matchpathcon_init. */ extern void matchpathcon_fini(void) diff --git a/libselinux/src/Makefile b/libselinux/src/Makefile index ac656257..15d224e1 100644 --- a/libselinux/src/Makefile +++ b/libselinux/src/Makefile @@ -144,6 +144,10 @@ ifeq ($(DISABLE_X11),y) SRCS:= $(filter-out label_x.c, $(SRCS)) endif +# ignore usage of matchpathcon_init_prefix(3) +matchpathcon.o: CFLAGS += -Wno-deprecated -Wno-deprecated-declarations +matchpathcon.lo: CFLAGS += -Wno-deprecated -Wno-deprecated-declarations + SWIGRUBY = swig -Wall -ruby -o $(SWIGRUBYCOUT) -outdir ./ $(DISABLE_FLAGS) all: $(LIBA) $(LIBSO) $(LIBPC) diff --git a/libselinux/src/label_file.c b/libselinux/src/label_file.c index b9be1c9d..f9f4648a 100644 --- a/libselinux/src/label_file.c +++ b/libselinux/src/label_file.c @@ -166,7 +166,7 @@ static int nodups_specs(struct saved_data *data, const char *path) return rc; } -static int process_text_file(FILE *fp, const char *prefix, +static int process_text_file(FILE *fp, struct selabel_handle *rec, const char *path) { int rc; @@ -175,7 +175,7 @@ static int process_text_file(FILE *fp, const char *prefix, char *line_buf = NULL; while (getline(&line_buf, &line_len, fp) > 0) { - rc = process_line(rec, path, prefix, line_buf, ++lineno); + rc = process_line(rec, path, line_buf, ++lineno); if (rc) goto out; } @@ -603,7 +603,7 @@ static FILE *open_file(const char *path, const char *suffix, static int process_file(const char *path, const char *suffix, struct selabel_handle *rec, - const char *prefix, struct selabel_digest *digest) + struct selabel_digest *digest) { int rc; unsigned int i; @@ -624,7 +624,7 @@ static int process_file(const char *path, const char *suffix, rc = fcontext_is_binary(fp) ? load_mmap(fp, sb.st_size, rec, found_path) : - process_text_file(fp, prefix, rec, found_path); + process_text_file(fp, rec, found_path); if (!rc) rc = digest_add_specfile(digest, fp, NULL, sb.st_size, found_path); @@ -785,7 +785,6 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts, { struct saved_data *data = (struct saved_data *)rec->data; const char *path = NULL; - const char *prefix = NULL; int status = -1, baseonly = 0; /* Process arguments */ @@ -795,7 +794,7 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts, path = opts[n].value; break; case SELABEL_OPT_SUBSET: - prefix = opts[n].value; + selinux_log(SELINUX_WARNING, "selabel_open(3): SELABEL_OPT_SUBSET support has been removed, ignoring option\n"); break; case SELABEL_OPT_BASEONLY: baseonly = !!opts[n].value; @@ -839,7 +838,7 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts, /* * The do detailed validation of the input and fill the spec array */ - status = process_file(path, NULL, rec, prefix, rec->digest); + status = process_file(path, NULL, rec, rec->digest); if (status) goto finish; @@ -850,13 +849,11 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts, } if (!baseonly) { - status = process_file(path, "homedirs", rec, prefix, - rec->digest); + status = process_file(path, "homedirs", rec, rec->digest); if (status && errno != ENOENT) goto finish; - status = process_file(path, "local", rec, prefix, - rec->digest); + status = process_file(path, "local", rec, rec->digest); if (status && errno != ENOENT) goto finish; } diff --git a/libselinux/src/label_file.h b/libselinux/src/label_file.h index 1363c83c..56439e2d 100644 --- a/libselinux/src/label_file.h +++ b/libselinux/src/label_file.h @@ -425,10 +425,10 @@ static inline int compile_regex(struct spec *spec, const char **errbuf) /* This service is used by label_file.c process_file() and * utils/sefcontext_compile.c */ static inline int process_line(struct selabel_handle *rec, - const char *path, const char *prefix, + const char *path, char *line_buf, unsigned lineno) { - int items, len, rc; + int items, rc; char *regex = NULL, *type = NULL, *context = NULL; struct saved_data *data = (struct saved_data *)rec->data; struct spec *spec_arr; @@ -466,15 +466,6 @@ static inline int process_line(struct selabel_handle *rec, type = 0; } - len = get_stem_from_spec(regex); - if (len && prefix && strncmp(prefix, regex, len)) { - /* Stem of regex does not match requested prefix, discard. */ - free(regex); - free(type); - free(context); - return 0; - } - rc = grow_specs(data); if (rc) return rc; diff --git a/libselinux/src/matchpathcon.c b/libselinux/src/matchpathcon.c index a1c5b0cc..971ace62 100644 --- a/libselinux/src/matchpathcon.c +++ b/libselinux/src/matchpathcon.c @@ -347,7 +347,7 @@ static void matchpathcon_init_once(void) destructor_key_initialized = 1; } -int matchpathcon_init_prefix(const char *path, const char *subset) +int matchpathcon_init_prefix(const char *path, const char *prefix __attribute__((unused))) { if (!mycanoncon) mycanoncon = default_canoncon; @@ -355,8 +355,6 @@ int matchpathcon_init_prefix(const char *path, const char *subset) __selinux_once(once, matchpathcon_init_once); __selinux_setspecific(destructor_key, /* some valid address to please GCC */ &selinux_page_size); - options[SELABEL_OPT_SUBSET].type = SELABEL_OPT_SUBSET; - options[SELABEL_OPT_SUBSET].value = subset; options[SELABEL_OPT_PATH].type = SELABEL_OPT_PATH; options[SELABEL_OPT_PATH].value = path; diff --git a/libselinux/utils/matchpathcon.c b/libselinux/utils/matchpathcon.c index 1d713c01..8e1c45c1 100644 --- a/libselinux/utils/matchpathcon.c +++ b/libselinux/utils/matchpathcon.c @@ -13,7 +13,7 @@ static __attribute__ ((__noreturn__)) void usage(const char *progname) { fprintf(stderr, - "usage: %s [-V] [-N] [-n] [-m type] [-f file_contexts_file] [-p prefix] [-P policy_root_path] filepath...\n", + "usage: %s [-V] [-N] [-n] [-m type] [-f file_contexts_file] [-P policy_root_path] filepath...\n", progname); exit(1); } @@ -83,7 +83,7 @@ int main(int argc, char **argv) if (argc < 2) usage(argv[0]); - while ((opt = getopt(argc, argv, "m:Nnf:P:p:Vq")) > 0) { + while ((opt = getopt(argc, argv, "m:Nnf:P:Vq")) > 0) { switch (opt) { case 'n': header = 0; @@ -114,13 +114,6 @@ int main(int argc, char **argv) exit(1); } break; - case 'p': - // This option has been deprecated since libselinux 2.5 (2016): - // https://github.com/SELinuxProject/selinux/commit/26e05da0fc2d0a4bd274320968a88f8acbb3b6a6 - fprintf(stderr, "Warning: using %s -p is deprecated\n", argv[0]); - options[SELABEL_OPT_SUBSET].type = SELABEL_OPT_SUBSET; - options[SELABEL_OPT_SUBSET].value = optarg; - break; case 'q': quiet = 1; break; diff --git a/libselinux/utils/sefcontext_compile.c b/libselinux/utils/sefcontext_compile.c index 6c32172d..eed6e4fd 100644 --- a/libselinux/utils/sefcontext_compile.c +++ b/libselinux/utils/sefcontext_compile.c @@ -36,7 +36,6 @@ static int process_file(struct selabel_handle *rec, const char *filename) char *line_buf = NULL; size_t line_len = 0; FILE *context_file; - const char *prefix = NULL; context_file = fopen(filename, "r"); if (!context_file) { @@ -48,7 +47,7 @@ static int process_file(struct selabel_handle *rec, const char *filename) line_num = 0; rc = 0; while (getline(&line_buf, &line_len, context_file) > 0) { - rc = process_line(rec, filename, prefix, line_buf, ++line_num); + rc = process_line(rec, filename, line_buf, ++line_num); if (rc || ctx_err) { /* With -p option need to check and fail if ctx err as * process_line() context validation on Linux does not From patchwork Mon Aug 14 13:20:16 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: 13352853 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 1F4FFC25B4F for ; Mon, 14 Aug 2023 13:21:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231177AbjHNNVY (ORCPT ); Mon, 14 Aug 2023 09:21:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50138 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230328AbjHNNVC (ORCPT ); Mon, 14 Aug 2023 09:21:02 -0400 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 35399172B for ; Mon, 14 Aug 2023 06:20:52 -0700 (PDT) Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-977e0fbd742so560737466b.2 for ; Mon, 14 Aug 2023 06:20:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019250; x=1692624050; 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=F76+OARcbDG+Dhzl4dXmbQMfBLLsN2l7V+qnY8M1apY=; b=oyE5DKUv2cDh/K8dJ5LEpDpVpXgivYq4+625B1jggy9oiiPRHl1+hcz/YKTWYQ4n75 UT3yjZ4E1GxgNpTFI2ay9DStfDRalys8NIaB7T6Ye3jEU67LuORwJkkpwnSXzSISh28B 1U3j9GySL804ntdxu8aDb4dt7gYUJoaE3sUPl6Y+rUeGjPm83UtfnqGGAnQXp0NLutvQ 5GbjqBiN55ckW7J+mXG2UnhYr4WDukRqRPqyv1P9XZ2T2wFe6Q1YP9D/8rlzujda/O9H fW6NvR8squu0FhwQZVc2wnY497aRGWwH+OuLzUU4ZaOvsWqc4M06I5jjqsIbU6AxjbK7 nfJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019251; x=1692624051; 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=F76+OARcbDG+Dhzl4dXmbQMfBLLsN2l7V+qnY8M1apY=; b=abFBKQSLY+L6dwjwpOfVuI+kW8aZQRRbJFzlIZuULKVC0lvV6RqB1Z5urBePek22PE FCl2A+I1j8jyGCd6PUP5WsLCwyfgeJWiV41kkBLrXzI/TMZrkDG+bTn/XVJ1/J48hCr0 WRxXvBk9yoVOss9Gt4iIEn+U9zcPxP6u4atz4w9wnxEdb78qyxpEO0DIYZvv9/ue/O1q w56tYy4NBilIFxAbmIK+n7nBCRr4n6CAxCCFO0hCqG3DDa/6C+8XycOcmWMp+freWXi6 jDLjxm1DcZ9oHSE2ZOQI3RFE+bXDzfKXnGo0d2Gv5XAKTO+gR1foOYmFYDAEtfEg4UES +JWg== X-Gm-Message-State: AOJu0YzI3+qO/rAsCYpM31iMCscshqWXL78oWyGoFS/QwgWyOSI/U4c7 WpzIQVc6Uu9fHiyVLnVjw9G7L6oFSA3WPlkx2lA= X-Google-Smtp-Source: AGHT+IFScb5PcbLPmbONsiza/n34KVF1mQ1ZbEdYLw1b0afn3pT71u+opCxsUOKjO4k+r6Y1CKbkPQ== X-Received: by 2002:a17:906:31db:b0:993:fe68:569d with SMTP id f27-20020a17090631db00b00993fe68569dmr6651355ejf.17.1692019250484; Mon, 14 Aug 2023 06:20:50 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:50 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 18/27] libselinux: fix logic for building android backend Date: Mon, 14 Aug 2023 15:20:16 +0200 Message-Id: <20230814132025.45364-19-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Fix the typo and adjust the logic accordingly so the android backend is not build by default, but if either ANDROID_HOST or LABEL_BACKEND_ANDROID is set to y. Fixes: c2a58cc52574 ("libselinux: LABEL_BACKEND_ANDROID add option to enable") Signed-off-by: Christian Göttsche Acked-by: James Carter --- libselinux/src/Makefile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libselinux/src/Makefile b/libselinux/src/Makefile index 15d224e1..86a254da 100644 --- a/libselinux/src/Makefile +++ b/libselinux/src/Makefile @@ -131,11 +131,10 @@ DISABLE_FLAGS+= -DNO_MEDIA_BACKEND -DNO_DB_BACKEND -DNO_X_BACKEND \ SRCS= callbacks.c freecon.c label.c label_file.c \ label_backends_android.c regex.c label_support.c \ matchpathcon.c setrans_client.c sha1.c booleans.c -else LABEL_BACKEND_ANDROID=y endif -ifneq ($(LABEL_BACKEND_ANDROIDT),y) +ifneq ($(LABEL_BACKEND_ANDROID),y) SRCS:= $(filter-out label_backends_android.c, $(SRCS)) DISABLE_FLAGS+= -DNO_ANDROID_BACKEND endif From patchwork Mon Aug 14 13:20:17 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: 13352850 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 C8D5BC10F1A for ; Mon, 14 Aug 2023 13:21:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230502AbjHNNVV (ORCPT ); Mon, 14 Aug 2023 09:21:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50214 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230350AbjHNNVD (ORCPT ); Mon, 14 Aug 2023 09:21:03 -0400 Received: from mail-ej1-x62a.google.com (mail-ej1-x62a.google.com [IPv6:2a00:1450:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B01D61736 for ; Mon, 14 Aug 2023 06:20:52 -0700 (PDT) Received: by mail-ej1-x62a.google.com with SMTP id a640c23a62f3a-997c4107d62so576367166b.0 for ; Mon, 14 Aug 2023 06:20:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019251; x=1692624051; 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=PEHJ9A5IiHK7rkzOyAH9B21P48m0kTXcW6LRMfoByDU=; b=mQF1MSm841AI5i5ZkRLT6zGf9i4bNyQ9LFAY31np+tlTK/ToYAdr8oHbVZJybmyA0F Pzh5zOHrzKtF6irgOWesOnvFYdkR/zizQqnWTqH5sbk6QQbpJAUsmFm3mrAoQxpMuGP4 yLLIkeiV5ulqWcH6Q4Dflc9HfqjKsWE20O08S32v1ks3iKqqvrfIE0mGI2+IoXP8vdN3 +iwziZQ+NDC3bWVpUWG5Z+a6Mwpi98RjizbKP8S18K3bw187Z4t+cbClVfcLNdzczSxF w2IENYK+yh0XYG+kfrPUJE/qqQtUdL+G76oTIjxL/EWfocp6W+RUjEGZisFYDbYErpRO 91zQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019251; x=1692624051; 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=PEHJ9A5IiHK7rkzOyAH9B21P48m0kTXcW6LRMfoByDU=; b=DRM4TavWWSr9O/ORdAEW6S0l8TiiEjQCnEPREExzOv8+r9zSmBF9k9FMUfDWBBo/mz ia0ZPjg3OHKkJFTGLIudO+vTTg/FwXFVajlZ3ZZ2Wzux8Wkrzow+8LYyW6JKccN1ZTWZ F8yE9m854iqEIfFJMtSdEUIJYMSxawOUgvIb8tPTng/95aPEygJAKz9qlM3OYs0qzOlF JxfqmaZbLDLhpxIO0OT6sDVp2DEJ5pyH5sk89wM873nMYdlPMO2GbswiwFUS3Eox4TtZ A2j7zdjhroJLbmUpsqFYbbxxxu4gO0qLRV1PMhM+IdQxQm8mK5EhIs4Ud30VoFjVje6x SA+g== X-Gm-Message-State: AOJu0YwY4FZNMRoaJuqJyXjWYkuXDTDjB6PQ7ncqynY9Ie34u25DUBJ4 bP0RpklTH2udRoTLLpe+w+RvcqZX/3y87gHeQvE= X-Google-Smtp-Source: AGHT+IHIfWZ3XlD6t7RgD95O0vw9Xj/Tfgdv+MfM5tqHlYkBpPfhJMqM8mvC9ludgP5S6dfkklunmQ== X-Received: by 2002:a17:907:7886:b0:994:54af:e282 with SMTP id ku6-20020a170907788600b0099454afe282mr8738566ejc.10.1692019251173; Mon, 14 Aug 2023 06:20:51 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:50 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 19/27] libselinux: avoid unused function Date: Mon, 14 Aug 2023 15:20:17 +0200 Message-Id: <20230814132025.45364-20-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Define selabel_subs_init() only if its call-sites are enabled. Signed-off-by: Christian Göttsche Acked-by: James Carter --- libselinux/src/label_file.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libselinux/src/label_file.c b/libselinux/src/label_file.c index f9f4648a..471fd56b 100644 --- a/libselinux/src/label_file.c +++ b/libselinux/src/label_file.c @@ -674,6 +674,7 @@ static char *selabel_sub(struct selabel_sub *ptr, const char *src) return NULL; } +#if !defined(BUILD_HOST) && !defined(ANDROID) static int selabel_subs_init(const char *path, struct selabel_digest *digest, struct selabel_sub **out_subs) { @@ -756,6 +757,7 @@ err: } goto out; } +#endif static char *selabel_sub_key(struct saved_data *data, const char *key) { From patchwork Mon Aug 14 13:20:18 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: 13352854 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 E54DAC18E72 for ; Mon, 14 Aug 2023 13:21:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230521AbjHNNVW (ORCPT ); Mon, 14 Aug 2023 09:21:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231250AbjHNNVF (ORCPT ); Mon, 14 Aug 2023 09:21:05 -0400 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 51DB0173C for ; Mon, 14 Aug 2023 06:20:53 -0700 (PDT) Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-99c1c66876aso573970966b.2 for ; Mon, 14 Aug 2023 06:20:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019252; x=1692624052; 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=N4mOkP4buA/cUMg+O2uX8hOSOHZy6mCesro8TRYnTAY=; b=SdZnu8bpfMA1owOzmJaQJZWMaPIVQV3j5W1t+8Dyn8Lh3y3iDTJfr/crNpBv+8OVXJ PHju7tLh/NNSYvZWe7+4M0M+naQPvJSvyF5ib1bsxs9FykkpoPySPZPqzHD+mhT9xMAO v+hYuEpWhtJ4nsQUOElr2zwrRoUBA+ajucKS1U5/1J23BTcdfwxnXTcoA6q4bauyGPdo Ke6pWpyzg1jM0nBedbSxCziniJfVlkJCy0b2++Q0uqpPzf2tr+lkcdEqzCWfoKX1J6Ks yVzCKeq/hrc5KG8CDBbTGtKYF+T9nB9TyloUaPO3G8Gl30jSMnv0DLGbHkbqp9crdmMd lA/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019252; x=1692624052; 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=N4mOkP4buA/cUMg+O2uX8hOSOHZy6mCesro8TRYnTAY=; b=d9u+PbjJCaVNo67PAZAJ0zSLT9Eb/PN6EHIO6ZUiuVwr31EMD6guAp8c0OwP3Toh0H P2wDRA84awv/4GxwqVx8yOEoy/TmS+RGdypOfKD6r75DCRd9MFPwzF22gnGoV10jZN0A NAMVS7e1YvlGJ/UiIGjxVkNKE5boykEbC+X9uBgvBpRfah/R0c01UYiG6M0bfLSGgDV8 B7Kk1zBdZYPNuyljqhmpvtuX7H4rrhcpBKdA82vd5ewyPSycvQUPN07K0n/rIzkPnMVt yJB/mP5puLnzcahcItaGBlvgT8l/4Aw3dER7R516SGdK4wzqvM2y42CNwPaMtxVV22gC k4gQ== X-Gm-Message-State: AOJu0YyLOFB5/M8bwWh1U80s3DSTlcaL8yxgZVax+jJZwYfGQtNrQwLy b9EKzwez1bCBlXdXrRKDgPDJgXOzWtYV+ywOGzk= X-Google-Smtp-Source: AGHT+IGryLZycBpKYKUk+pJT49kjITtZwAHnNIhvE9sHJkLEqczcbyxBufZCts9+xHHmsLVW3sS0iw== X-Received: by 2002:a17:907:2c61:b0:99b:dea3:8d44 with SMTP id ib1-20020a1709072c6100b0099bdea38d44mr7885719ejc.71.1692019251767; Mon, 14 Aug 2023 06:20:51 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:51 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 20/27] libselinux: check for stream rewind failures Date: Mon, 14 Aug 2023 15:20:18 +0200 Message-Id: <20230814132025.45364-21-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Use fseek(3) instead of rewind(3) to detect failures. Drop the final rewind in digest_add_specfile(), since all callers are going to close the stream without any further action. Signed-off-by: Christian Göttsche Acked-by: James Carter --- libselinux/src/is_customizable_type.c | 7 ++++++- libselinux/src/label_backends_android.c | 5 ++++- libselinux/src/label_file.c | 16 +++++++++++++--- libselinux/src/label_media.c | 5 ++++- libselinux/src/label_support.c | 5 +++-- libselinux/src/label_x.c | 5 ++++- 6 files changed, 34 insertions(+), 9 deletions(-) diff --git a/libselinux/src/is_customizable_type.c b/libselinux/src/is_customizable_type.c index f83e1e83..9be50174 100644 --- a/libselinux/src/is_customizable_type.c +++ b/libselinux/src/is_customizable_type.c @@ -31,7 +31,12 @@ static void customizable_init(void) while (fgets_unlocked(buf, selinux_page_size, fp) && ctr < UINT_MAX) { ctr++; } - rewind(fp); + + if (fseek(fp, 0L, SEEK_SET) == -1) { + fclose(fp); + return; + } + if (ctr) { list = (char **) calloc(sizeof(char *), diff --git a/libselinux/src/label_backends_android.c b/libselinux/src/label_backends_android.c index c2d78360..6494f3cd 100644 --- a/libselinux/src/label_backends_android.c +++ b/libselinux/src/label_backends_android.c @@ -208,7 +208,10 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts, goto finish; maxnspec = data->nspec; - rewind(fp); + + status = fseek(fp, 0L, SEEK_SET); + if (status == -1) + goto finish; } } diff --git a/libselinux/src/label_file.c b/libselinux/src/label_file.c index 471fd56b..a5677411 100644 --- a/libselinux/src/label_file.c +++ b/libselinux/src/label_file.c @@ -519,12 +519,16 @@ static char *rolling_append(char *current, const char *suffix, size_t max) return current; } -static bool fcontext_is_binary(FILE *fp) +static int fcontext_is_binary(FILE *fp) { uint32_t magic; + int rc; size_t len = fread(&magic, sizeof(magic), 1, fp); - rewind(fp); + + rc = fseek(fp, 0L, SEEK_SET); + if (rc == -1) + return -1; return (len && (magic == SELINUX_MAGIC_COMPILED_FCONTEXT)); } @@ -622,7 +626,13 @@ static int process_file(const char *path, const char *suffix, if (fp == NULL) return -1; - rc = fcontext_is_binary(fp) ? + rc = fcontext_is_binary(fp); + if (rc < 0) { + (void) fclose(fp); + return -1; + } + + rc = rc ? load_mmap(fp, sb.st_size, rec, found_path) : process_text_file(fp, rec, found_path); if (!rc) diff --git a/libselinux/src/label_media.c b/libselinux/src/label_media.c index f09461ab..b3443b47 100644 --- a/libselinux/src/label_media.c +++ b/libselinux/src/label_media.c @@ -130,7 +130,10 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts, goto finish; memset(data->spec_arr, 0, sizeof(spec_t)*data->nspec); maxnspec = data->nspec; - rewind(fp); + + status = fseek(fp, 0L, SEEK_SET); + if (status == -1) + goto finish; } } free(line_buf); diff --git a/libselinux/src/label_support.c b/libselinux/src/label_support.c index 94fb6697..f7ab9292 100644 --- a/libselinux/src/label_support.c +++ b/libselinux/src/label_support.c @@ -174,12 +174,13 @@ int digest_add_specfile(struct selabel_digest *digest, FILE *fp, digest->hashbuf = tmp_buf; if (fp) { - rewind(fp); + if (fseek(fp, 0L, SEEK_SET) == -1) + return -1; + if (fread(digest->hashbuf + (digest->hashbuf_size - buf_len), 1, buf_len, fp) != buf_len) return -1; - rewind(fp); } else if (from_addr) { tmp_buf = memcpy(digest->hashbuf + (digest->hashbuf_size - buf_len), diff --git a/libselinux/src/label_x.c b/libselinux/src/label_x.c index e80bf107..e15190ca 100644 --- a/libselinux/src/label_x.c +++ b/libselinux/src/label_x.c @@ -157,7 +157,10 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts, goto finish; memset(data->spec_arr, 0, sizeof(spec_t)*data->nspec); maxnspec = data->nspec; - rewind(fp); + + status = fseek(fp, 0L, SEEK_SET); + if (status == -1) + goto finish; } } free(line_buf); From patchwork Mon Aug 14 13:20:19 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: 13352856 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 2D5E8C19F4F for ; Mon, 14 Aug 2023 13:21:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231146AbjHNNVX (ORCPT ); Mon, 14 Aug 2023 09:21:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50250 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231254AbjHNNVF (ORCPT ); Mon, 14 Aug 2023 09:21:05 -0400 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0AB791984 for ; Mon, 14 Aug 2023 06:20:54 -0700 (PDT) Received: by mail-ej1-x62f.google.com with SMTP id a640c23a62f3a-99cdb0fd093so593597066b.1 for ; Mon, 14 Aug 2023 06:20:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019252; x=1692624052; 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=/mqA1dkTKcJtEnCLqpqWsbIPplUanIX8ARuR1xEPmv0=; b=qLDZpTstKv+o2jZUATf/OhpCb/lDxktg1sUg2dTLP7weFkbPjmR0Fj6tQ49XlkjLYh Mz7/k1D2quY2AQ57BaH8Pv02Nfu3aIYqsLQKmTSewxP5IAtYjK8HrC0D8T8+40uQkAZo h8PPN9vxRKv7NC2vHAl/Jz1UVUA8H9QqOaawUYKoE8UzTqDDEZrtHP2XUQuxqyUGNSZ3 b2AABaxkHkeiN7ljo+uIdfp1NpRbt+weQOSR3xx/9lyKtRNTc7jd+ZCgLXQtvHl1WKBm FqIwP8OvKTiSCbFlkrzYDZEuYem49SuLE3mAuisTRNW5/8AC7gh0FpD5f6bVKWH/5NF+ 5gEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019252; x=1692624052; 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=/mqA1dkTKcJtEnCLqpqWsbIPplUanIX8ARuR1xEPmv0=; b=NkGT/r8f0zbwMGHKsLUDKBPm+WS9MIiB6tWaYpbG2tm8CpIIX8ZLXlzBDsCoRYSSuL a0w0XYVpCp0vebdk22zpakgVhZWCob5Qbp9RIUWakldjfX/QcJWuZCt+1xL2Ks/zc1Wa 9IC0RY31t1RbqUXiOY39uKDqDtuJfULmaHYWdITZP0Fqdv2drxK83/ZMrpfmglEX+ATi YAwXw1FNseKjy/gE0be0H8w2NgHc5Sc2x50zgbx1N05DFbe4NhjV6BeS2tYf+E0c1qMl 9SAaICUrRAIRStPJX2xahlZXH8mhJhc+aDBqPUhnYA4dcT047GENVdm47/Lgf1MgZ1Yq qvoA== X-Gm-Message-State: AOJu0Yw3hzPXo0WKsjWsCAbUSIYN5mwYvmgksC737nBnlWNlcLFMYdVD 9ssBkqUxgzEkilbw2rydTCob9ojGsOM6rYxl8r8= X-Google-Smtp-Source: AGHT+IGHNfAydA5NFTx+nTIIXZKBYmA62hL/PHX7DvKLUBV/X/q7otULy30xZ+feEBQyGHfhk1f/Jg== X-Received: by 2002:a17:907:1de9:b0:992:5135:4112 with SMTP id og41-20020a1709071de900b0099251354112mr7375171ejc.32.1692019252365; Mon, 14 Aug 2023 06:20:52 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:52 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 21/27] libselinux: simplify internal selabel_validate prototype Date: Mon, 14 Aug 2023 15:20:19 +0200 Message-Id: <20230814132025.45364-22-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Move the check whether to validate or not to the caller, to avoid all having to carry the complete selabel_handle around. Signed-off-by: Christian Göttsche Acked-by: James Carter --- libselinux/src/label.c | 5 ++--- libselinux/src/label_backends_android.c | 2 +- libselinux/src/label_file.c | 2 +- libselinux/src/label_internal.h | 3 +-- libselinux/src/matchpathcon.c | 7 ++++--- 5 files changed, 9 insertions(+), 10 deletions(-) diff --git a/libselinux/src/label.c b/libselinux/src/label.c index 9a972f79..1ea9bdcd 100644 --- a/libselinux/src/label.c +++ b/libselinux/src/label.c @@ -119,12 +119,11 @@ static inline int selabel_is_validate_set(const struct selinux_opt *opts, return 0; } -int selabel_validate(const struct selabel_handle *rec, - struct selabel_lookup_rec *contexts) +int selabel_validate(struct selabel_lookup_rec *contexts) { int rc = 0; - if (!rec->validating || contexts->validated) + if (contexts->validated) goto out; rc = selinux_validate(&contexts->ctx_raw); diff --git a/libselinux/src/label_backends_android.c b/libselinux/src/label_backends_android.c index 6494f3cd..cd3875fc 100644 --- a/libselinux/src/label_backends_android.c +++ b/libselinux/src/label_backends_android.c @@ -126,7 +126,7 @@ static int process_line(struct selabel_handle *rec, spec_arr[nspec].lr.ctx_raw = context; if (rec->validating) { - if (selabel_validate(rec, &spec_arr[nspec].lr) < 0) { + if (selabel_validate(&spec_arr[nspec].lr) < 0) { selinux_log(SELINUX_ERROR, "%s: line %u has invalid context %s\n", path, lineno, spec_arr[nspec].lr.ctx_raw); diff --git a/libselinux/src/label_file.c b/libselinux/src/label_file.c index a5677411..64b58d42 100644 --- a/libselinux/src/label_file.c +++ b/libselinux/src/label_file.c @@ -404,7 +404,7 @@ end_arch_check: spec->lr.ctx_raw = str_buf; if (strcmp(spec->lr.ctx_raw, "<>") && rec->validating) { - if (selabel_validate(rec, &spec->lr) < 0) { + if (selabel_validate(&spec->lr) < 0) { selinux_log(SELINUX_ERROR, "%s: context %s is invalid\n", path, spec->lr.ctx_raw); diff --git a/libselinux/src/label_internal.h b/libselinux/src/label_internal.h index bc5a6928..ea60cd9a 100644 --- a/libselinux/src/label_internal.h +++ b/libselinux/src/label_internal.h @@ -118,8 +118,7 @@ struct selabel_handle { * Validation function */ extern int -selabel_validate(const struct selabel_handle *rec, - struct selabel_lookup_rec *contexts) ; +selabel_validate(struct selabel_lookup_rec *contexts); /* * Compatibility support diff --git a/libselinux/src/matchpathcon.c b/libselinux/src/matchpathcon.c index 971ace62..4ed25ce9 100644 --- a/libselinux/src/matchpathcon.c +++ b/libselinux/src/matchpathcon.c @@ -46,8 +46,8 @@ int compat_validate(const struct selabel_handle *rec, rc = myinvalidcon(path, lineno, *ctx); else if (mycanoncon) rc = mycanoncon(path, lineno, ctx); - else { - rc = selabel_validate(rec, contexts); + else if (rec->validating) { + rc = selabel_validate(contexts); if (rc < 0) { if (lineno) { COMPAT_LOG(SELINUX_WARNING, @@ -58,7 +58,8 @@ int compat_validate(const struct selabel_handle *rec, "%s: has invalid context %s\n", path, *ctx); } } - } + } else + rc = 0; return rc ? -1 : 0; } From patchwork Mon Aug 14 13:20:20 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: 13352858 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 3B2D9C25B5E for ; Mon, 14 Aug 2023 13:21:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231187AbjHNNVZ (ORCPT ); Mon, 14 Aug 2023 09:21:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231255AbjHNNVG (ORCPT ); Mon, 14 Aug 2023 09:21:06 -0400 Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 69304198A for ; Mon, 14 Aug 2023 06:20:54 -0700 (PDT) Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-522ab557632so5808027a12.0 for ; Mon, 14 Aug 2023 06:20:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019253; x=1692624053; 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=tdQNlCF8kllSxhHWrl5dxhxYUastPbI3lxdFSaVbvcs=; b=JEg0Jv7RkjXPsXshNn7pNOWHmyKIREGUx06Gzl1WVaiJY5ab9MmX6mde8YX8TUHNqW llOVFosgBU0JEmqYz+fCo6MffDfF5QCgwIRIeUmSeXCwqPZae0YSt0kWwlYf6qnCsrk4 Qtk1GEsVBF5IEZxAxb/YXpixuxDya+5rEbqldCgOu8Lz0N4Bn2hkPxeq/04mfJg5EbCh NNvlJ9nBcGBa8WYBLk30IGg8igfInpokmV63SH0y+yhxznJuGtJG95KfCO/rX0/IMEdL ccgn2bD29oOzZJr1IiQ3FvRRQ3ovKz/cz1NEIJZm6xLvhkIfMso21PITz/yt/NAUAGXH IiRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019253; x=1692624053; 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=tdQNlCF8kllSxhHWrl5dxhxYUastPbI3lxdFSaVbvcs=; b=T0QmOXV7iiGL9HkPTF32fmfHO6Xaa64AGJI85iiB5ZQTJA7OUwXQUxoGoEEYu8TIVz J3NtPIAsrIzp3FxnxwkrszgCdGGxlsk72EPlblzqP9m4hsPMxHufc3iR1n4LAPEa3M12 5rAk1FswOl2HdPVvd+5G5En8nJfVgoY/wR6H8v+KTpULREzHfhqVwv0zAPW5/3Zk95IF NcMB9nfyXxFfovYU07DpTv6MJQOquaUjhRMw9vQYZVu521c8CDHa4hXS46Pqss7aYoso bosnldcLrJpGSGuUGb4u5EymF9w30bbHtLFHGTLeIKnvnDdDzHzO4QVpZPDlwGps996r Pd0g== X-Gm-Message-State: AOJu0YzVB3ULE42BOOpmlQj2AHlPZDXPHnILamXELfUiKAt+9mdBfs+h Y1lY8WgC4pGZoyxAmaJDMpmY4fqiDpxyAI9IZ08= X-Google-Smtp-Source: AGHT+IGU0OY+6NxRgnkYpkrKToGxpM6k/e5uDWmW0qxHj9ukJd9sa4Bv1exUAC+CNYHXKLsDqL5xTA== X-Received: by 2002:a17:907:1dc4:b0:99b:4174:77f6 with SMTP id og4-20020a1709071dc400b0099b417477f6mr7194275ejc.33.1692019252838; Mon, 14 Aug 2023 06:20:52 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:52 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 22/27] libselinux/utils: drop include of internal header file Date: Mon, 14 Aug 2023 15:20:20 +0200 Message-Id: <20230814132025.45364-23-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org selabel_get_digest_all_partial_matches is dynamically linked against libselinux, so it should not depend on any internals and thus do not make use of an internal header file Signed-off-by: Christian Göttsche Acked-by: James Carter --- libselinux/utils/selabel_get_digests_all_partial_matches.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/libselinux/utils/selabel_get_digests_all_partial_matches.c b/libselinux/utils/selabel_get_digests_all_partial_matches.c index c4e0f836..e2733b41 100644 --- a/libselinux/utils/selabel_get_digests_all_partial_matches.c +++ b/libselinux/utils/selabel_get_digests_all_partial_matches.c @@ -8,8 +8,6 @@ #include #include -#include "../src/label_file.h" - static __attribute__ ((__noreturn__)) void usage(const char *progname) { fprintf(stderr, From patchwork Mon Aug 14 13:20:21 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: 13352852 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 D6DF0C10F19 for ; Mon, 14 Aug 2023 13:21:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231132AbjHNNVX (ORCPT ); Mon, 14 Aug 2023 09:21:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231279AbjHNNVI (ORCPT ); Mon, 14 Aug 2023 09:21:08 -0400 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0C3D8198E for ; Mon, 14 Aug 2023 06:20:55 -0700 (PDT) Received: by mail-ej1-x631.google.com with SMTP id a640c23a62f3a-99bcfe28909so559422166b.3 for ; Mon, 14 Aug 2023 06:20:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019253; x=1692624053; 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=dzmzIwMgatNVIeymrQgy5mQmH/Xqnbixd3+E3NBHUAI=; b=o693HdvSriL8ohPOF8fpaVcjzHiHYUzR8SigslKyyDVQwea/Lupfx/aAwKVUXorbeh XrfjIOk/vhb3nLAnJiwyfy+IrYfnwBqccadOtqUG5LeyhAnR5JstdVidxHdC5AWfr8al opxr4F4ZMq6SipfK4cZze90Ea4tfhpX3ZV4qk/sfykBUJB35jn45I317Wb4IPjN0G0c3 Fynl1kO4MkgvnKBDCtr+//aVdpptbcPj01q6MAFHQvxiqX4rj489+B1AoX3MBp70m3XK NDfBI/eAx/n0XbB+1L+cGwi6yxIEZdhH6z2K+DrnrwZjR1xiRTXUhlg08Di/Zb2agr4b 11tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019253; x=1692624053; 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=dzmzIwMgatNVIeymrQgy5mQmH/Xqnbixd3+E3NBHUAI=; b=GZYQQx7eh1sBxg0JT//HFJbDn+hWc9LPEwn9sve+NK5jGPx4K4NpYjLDQWSgYXxiyL 7pnjs/kKDiXRIXPfrtHacQ2HbZy+gFzuPDu0e0eGkdr/RIZg3hr6+wKIzZKkQ0T+g8ca DTsEeHSLvBAIfzgy3Z+vdj5b3K2pqlxJ8Ox8HYG7GAEzU05UbSIruWF4ZF2V5PRCg0ir c+a2tFhK6UZUy2JXqxRhevcmQo4GEUaK2c5vt7T9QIp1wOMNCMVpdYRM6idTDfnjYXN+ Y6ZMrQgjoQxNq6NBFgHZZTbIeroUywQbumbg7Fg/PegutZAFC8EOybJUmH1PDHVIvwZo e1rQ== X-Gm-Message-State: AOJu0YyCKqT2XCJkrPtff/P+mFeR2dSMLNaBcL13mJDvj0DyFCbBqPy5 Q78qzTwc9xjL9Y2oE7eo1847xJrjBu7lkXjUvpk= X-Google-Smtp-Source: AGHT+IH8tQck06xUvsNsn/jjFYEFiMJVochAA2Iw0nYU+NdZLnTKD984wsS5Vnzs3CIUGwLz/xQyNg== X-Received: by 2002:a17:906:3016:b0:99b:eb9c:4fb9 with SMTP id 22-20020a170906301600b0099beb9c4fb9mr6958239ejz.69.1692019253470; Mon, 14 Aug 2023 06:20:53 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:53 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 23/27] libselinux: free elements on read_spec_entries() failure Date: Mon, 14 Aug 2023 15:20:21 +0200 Message-Id: <20230814132025.45364-24-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Some entries might have been already parsed and allocated. Signed-off-by: Christian Göttsche Acked-by: James Carter --- v2: add patch --- libselinux/src/label_file.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libselinux/src/label_file.h b/libselinux/src/label_file.h index 56439e2d..3827b5c0 100644 --- a/libselinux/src/label_file.h +++ b/libselinux/src/label_file.h @@ -446,6 +446,9 @@ static inline int process_line(struct selabel_handle *rec, "%s: line %u error due to: %m\n", path, lineno); } + free(regex); + free(type); + free(context); return -1; } From patchwork Mon Aug 14 13:20:22 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: 13352835 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 B8593C001B0 for ; Mon, 14 Aug 2023 13:21:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230172AbjHNNVL (ORCPT ); Mon, 14 Aug 2023 09:21:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50422 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231295AbjHNNVJ (ORCPT ); Mon, 14 Aug 2023 09:21:09 -0400 Received: from mail-lj1-x22a.google.com (mail-lj1-x22a.google.com [IPv6:2a00:1450:4864:20::22a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F3C5B10DE for ; Mon, 14 Aug 2023 06:20:55 -0700 (PDT) Received: by mail-lj1-x22a.google.com with SMTP id 38308e7fff4ca-2ba1e9b1fa9so66170591fa.3 for ; Mon, 14 Aug 2023 06:20:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019254; x=1692624054; 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=9kDLgGg0yw55jOe6NObH+nTGKk893dES4rl5W0ubmgY=; b=jNJ5A0oBYJDKFM99Z8dWJq0Z7cThznBNZhGSrU7JV0XsVcJ0Ztj2PXkFFZptVB1qEF PijECfPqsqALmLqlJtNbpLAAyTiHLX/X5Q2IVD+sdWLEfpJRRKoGOmVy6U27oE3LIhES e8YaLZOdxD3YDV7vhskaLhfYnc19eTYIuTtpj2/57MgNZ64f+1ulQ+fP/4Pw/7m1c0yJ fVuScCHzTnyNC5pwpWMZVZUbjjvUqWVZA9jQxmqLHaiAtW4EeunbLOR+Em03ZujxcMdz NO/hJgvqPVKvdbp67UwM+ooR6jzKF4i16nMyKmIlU6nyn4iKq12NQB6PD+7aA2VOraTy /XTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019254; x=1692624054; 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=9kDLgGg0yw55jOe6NObH+nTGKk893dES4rl5W0ubmgY=; b=OPnbfGQ1Goxhe+7rVOZlSkUNUfrtGkDGMWodglPEam18lefNzK7J4/tCnprw50Z6ES 7apHcpjdDxjBlpduSDLn/xkVD0M6S91uRcme6Edx9OMjv0X5ianPIzMVAKDKF0cZ+ZGX 785+tk2qISCL7muKLWDo6bG5z9IGizDxRDatSn8q7BG+6pG7tRmlzNBILNpzAtb9ibZ5 +OaL5zRovInnKHtlYiPq+o+GvZSwTRFyPmFjKj2x89MCSqHTAi7su5cvnNEqh6Qkp2Zm hKKF2IcdCuxJGzd5pjcReN33Zazj8oUnHX3LYhTLOXtvBMElpcPJUlwPVu5XHSM/86zy ef7w== X-Gm-Message-State: AOJu0YzmaQVc5nGpXGvjbBg7/lBsRoW5tALhbCdLRfuXPJtN8uLbjUHi 8+hkaX4cuPwK1iqPBrXR/rnUfveSQfNVuSq0HHI= X-Google-Smtp-Source: AGHT+IElE2f9UdJBHDjChQi8OQPrLR4QYIbx+EGoK7ZjBnWVh61sjPhLTngamaNtLPuYRTKAhL5kfA== X-Received: by 2002:a2e:9c1a:0:b0:2ba:38c4:1624 with SMTP id s26-20020a2e9c1a000000b002ba38c41624mr6548556lji.15.1692019254070; Mon, 14 Aug 2023 06:20:54 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:53 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 24/27] libselinux: set errno on label lookup failure Date: Mon, 14 Aug 2023 15:20:22 +0200 Message-Id: <20230814132025.45364-25-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org selabel_lookup(3) is expected to set errno on lookup failure, i.e. when it returns NULL. Set errno if a regular expression failed to compile in compile_regex(). Signed-off-by: Christian Göttsche Acked-by: James Carter --- v2: add patch --- libselinux/src/label_file.h | 1 + 1 file changed, 1 insertion(+) diff --git a/libselinux/src/label_file.h b/libselinux/src/label_file.h index 3827b5c0..5365086b 100644 --- a/libselinux/src/label_file.h +++ b/libselinux/src/label_file.h @@ -407,6 +407,7 @@ static inline int compile_regex(struct spec *spec, const char **errbuf) *errbuf = ®ex_error_format_buffer[0]; } __pthread_mutex_unlock(&spec->regex_lock); + errno = EINVAL; return -1; } From patchwork Mon Aug 14 13:20:24 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: 13352860 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 3067EC001B0 for ; Mon, 14 Aug 2023 13:22:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230466AbjHNNVm (ORCPT ); Mon, 14 Aug 2023 09:21:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230356AbjHNNVK (ORCPT ); Mon, 14 Aug 2023 09:21:10 -0400 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 518F5199C for ; Mon, 14 Aug 2023 06:20:57 -0700 (PDT) Received: by mail-ej1-x62f.google.com with SMTP id a640c23a62f3a-997c4107d62so576376566b.0 for ; Mon, 14 Aug 2023 06:20:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019256; x=1692624056; 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=8KTCosaLfwXLbYBf8UrPTir9MQzO7NqD5UT+xcBIAMg=; b=R1NTZqBYnHrD/IbL7z7kbpNDwBDCmIRB7rzPbPOuCUKUop3Xa7F94nI3Rb57cVl/5s 0dTmAIJI3HQi8Etls5Y0gsbNC6VVMK1MauSXjvLcxpW6FmrQQpzsVQS9etFVsFo99Zye bc13W6a79os0uncN4330Z20lyCQ/4upPh2+4oY4JwGWyxDGKrUZXxG+Lnirnv1BirMnw 7Rw1Hi0k6nwIX3RPGV4kbM9E4z3qUXCW3+Ii/gBOL9PDlTxHn2EDOahrriflax8RzzLd O2gOQY15ANOwoqlhxtQRdgU3hURxBUAPS9ponhULmFQbNi8mufAamxaFyFQCHajQ+o1T Xbbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019256; x=1692624056; 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=8KTCosaLfwXLbYBf8UrPTir9MQzO7NqD5UT+xcBIAMg=; b=QY625jPEB2BQ7J4dQndgLFIo+CKnWY3DKJDAGz5WPzybApR8act21aNKLjns1zFq+A xKxQYvKBtPuOd6gSccXDJAlTmhRd8f93IhKxo+ATcY7d3jWWmmW49IM4DGzfWaaFDHep C8a8QfSjcjIOiSySMhonc5bn7rFLMqzhMgJFfBGksd8tFALtCXHjjajJglCD2VuJl+vY 6NKq7PGnWh4EiqZFtowrsOCO3OHblkEnLq3x6Kg+MiKadRZajnrMOZ9OhHXfwm9+VhM4 GL0s/mxoeT6nDwy6gxa7c97G4f9Sb7LMUzncCTj8YdtYK+Jbk47P9bkpTqVPAm6529Bl Z1kw== X-Gm-Message-State: AOJu0YzH3FGYKaZtmhkNJ5TeqSZBVIRFhusi4FupTRy66Eai4KfNk/zy /HAxq5ttQ3ERFf7JuGky9EkeR7+EVpRtvcEyFE4= X-Google-Smtp-Source: AGHT+IEso7+3S1SfOP0EUUgGJy+zpa31vAWgyaLfETnVcXynwbIHq4Z6D5IaSyqZ+P/tpsRLVP8WOQ== X-Received: by 2002:a17:907:2c42:b0:99b:c689:3f3d with SMTP id hf2-20020a1709072c4200b0099bc6893f3dmr7870738ejc.25.1692019255660; Mon, 14 Aug 2023 06:20:55 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:55 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [RFC PATCH v2 26/27] libselinux: remove unused hashtab code Date: Mon, 14 Aug 2023 15:20:24 +0200 Message-Id: <20230814132025.45364-27-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Due to the selabel_file(5) rework this code is no longer used. Signed-off-by: Christian Göttsche --- libselinux/src/hashtab.c | 234 --------------------------------------- libselinux/src/hashtab.h | 117 -------------------- 2 files changed, 351 deletions(-) delete mode 100644 libselinux/src/hashtab.c delete mode 100644 libselinux/src/hashtab.h diff --git a/libselinux/src/hashtab.c b/libselinux/src/hashtab.c deleted file mode 100644 index 7452613b..00000000 --- a/libselinux/src/hashtab.c +++ /dev/null @@ -1,234 +0,0 @@ - -/* Author : Stephen Smalley, */ - -/* FLASK */ - -/* - * Implementation of the hash table type. - */ - -#include -#include -#include "hashtab.h" - -hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h, - const_hashtab_key_t key), - int (*keycmp) (hashtab_t h, - const_hashtab_key_t key1, - const_hashtab_key_t key2), - unsigned int size) -{ - - hashtab_t p; - unsigned int i; - - p = (hashtab_t) malloc(sizeof(hashtab_val_t)); - if (p == NULL) - return p; - - memset(p, 0, sizeof(hashtab_val_t)); - p->size = size; - p->nel = 0; - p->hash_value = hash_value; - p->keycmp = keycmp; - p->htable = (hashtab_ptr_t *) malloc(sizeof(hashtab_ptr_t) * size); - if (p->htable == NULL) { - free(p); - return NULL; - } - for (i = 0; i < size; i++) - p->htable[i] = (hashtab_ptr_t) NULL; - - return p; -} - -int hashtab_insert(hashtab_t h, hashtab_key_t key, hashtab_datum_t datum) -{ - unsigned int hvalue; - hashtab_ptr_t prev, cur, newnode; - - if (!h) - return HASHTAB_OVERFLOW; - - hvalue = h->hash_value(h, key); - prev = NULL; - cur = h->htable[hvalue]; - while (cur && h->keycmp(h, key, cur->key) > 0) { - prev = cur; - cur = cur->next; - } - - if (cur && (h->keycmp(h, key, cur->key) == 0)) - return HASHTAB_PRESENT; - - newnode = (hashtab_ptr_t) malloc(sizeof(hashtab_node_t)); - if (newnode == NULL) - return HASHTAB_OVERFLOW; - memset(newnode, 0, sizeof(struct hashtab_node)); - newnode->key = key; - newnode->datum = datum; - if (prev) { - newnode->next = prev->next; - prev->next = newnode; - } else { - newnode->next = h->htable[hvalue]; - h->htable[hvalue] = newnode; - } - - h->nel++; - return HASHTAB_SUCCESS; -} - -int hashtab_remove(hashtab_t h, hashtab_key_t key, - void (*destroy) (hashtab_key_t k, - hashtab_datum_t d, void *args), void *args) -{ - unsigned int hvalue; - hashtab_ptr_t cur, last; - - if (!h) - return HASHTAB_MISSING; - - hvalue = h->hash_value(h, key); - last = NULL; - cur = h->htable[hvalue]; - while (cur != NULL && h->keycmp(h, key, cur->key) > 0) { - last = cur; - cur = cur->next; - } - - if (cur == NULL || (h->keycmp(h, key, cur->key) != 0)) - return HASHTAB_MISSING; - - if (last == NULL) - h->htable[hvalue] = cur->next; - else - last->next = cur->next; - - if (destroy) - destroy(cur->key, cur->datum, args); - free(cur); - h->nel--; - return HASHTAB_SUCCESS; -} - -hashtab_datum_t hashtab_search(hashtab_t h, const_hashtab_key_t key) -{ - - unsigned int hvalue; - hashtab_ptr_t cur; - - if (!h) - return NULL; - - hvalue = h->hash_value(h, key); - cur = h->htable[hvalue]; - while (cur != NULL && h->keycmp(h, key, cur->key) > 0) - cur = cur->next; - - if (cur == NULL || (h->keycmp(h, key, cur->key) != 0)) - return NULL; - - return cur->datum; -} - -void hashtab_destroy(hashtab_t h) -{ - unsigned int i; - hashtab_ptr_t cur, temp; - - if (!h) - return; - - for (i = 0; i < h->size; i++) { - cur = h->htable[i]; - while (cur != NULL) { - temp = cur; - cur = cur->next; - free(temp); - } - h->htable[i] = NULL; - } - - free(h->htable); - h->htable = NULL; - - free(h); -} - -void hashtab_destroy_key(hashtab_t h, - int (*destroy_key) (hashtab_key_t k)) -{ - unsigned int i; - hashtab_ptr_t cur, temp; - - if (!h) - return; - - for (i = 0; i < h->size; i++) { - cur = h->htable[i]; - while (cur != NULL) { - temp = cur; - cur = cur->next; - destroy_key(temp->key); - free(temp); - } - h->htable[i] = NULL; - } - - free(h->htable); - h->htable = NULL; - - free(h); -} - -int hashtab_map(hashtab_t h, - int (*apply) (hashtab_key_t k, - hashtab_datum_t d, void *args), void *args) -{ - unsigned int i; - hashtab_ptr_t cur; - int ret; - - if (!h) - return HASHTAB_SUCCESS; - - for (i = 0; i < h->size; i++) { - cur = h->htable[i]; - while (cur != NULL) { - ret = apply(cur->key, cur->datum, args); - if (ret) - return ret; - cur = cur->next; - } - } - return HASHTAB_SUCCESS; -} - -void hashtab_hash_eval(hashtab_t h, char *tag) -{ - unsigned int i; - int chain_len, slots_used, max_chain_len; - hashtab_ptr_t cur; - - slots_used = 0; - max_chain_len = 0; - for (i = 0; i < h->size; i++) { - cur = h->htable[i]; - if (cur) { - slots_used++; - chain_len = 0; - while (cur) { - chain_len++; - cur = cur->next; - } - - if (chain_len > max_chain_len) - max_chain_len = chain_len; - } - } - - printf - ("%s: %d entries and %d/%d buckets used, longest chain length %d\n", - tag, h->nel, slots_used, h->size, max_chain_len); -} diff --git a/libselinux/src/hashtab.h b/libselinux/src/hashtab.h deleted file mode 100644 index f10fc0af..00000000 --- a/libselinux/src/hashtab.h +++ /dev/null @@ -1,117 +0,0 @@ - -/* Author : Stephen Smalley, */ - -/* FLASK */ - -/* - * A hash table (hashtab) maintains associations between - * key values and datum values. The type of the key values - * and the type of the datum values is arbitrary. The - * functions for hash computation and key comparison are - * provided by the creator of the table. - */ - -#ifndef _SELINUX_HASHTAB_H_ -#define _SELINUX_HASHTAB_H_ - -#include -#include -#include - -typedef char *hashtab_key_t; /* generic key type */ -typedef const char *const_hashtab_key_t; /* constant generic key type */ -typedef void *hashtab_datum_t; /* generic datum type */ - -typedef struct hashtab_node *hashtab_ptr_t; - -typedef struct hashtab_node { - hashtab_key_t key; - hashtab_datum_t datum; - hashtab_ptr_t next; -} hashtab_node_t; - -typedef struct hashtab_val { - hashtab_ptr_t *htable; /* hash table */ - unsigned int size; /* number of slots in hash table */ - uint32_t nel; /* number of elements in hash table */ - unsigned int (*hash_value) (struct hashtab_val * h, const_hashtab_key_t key); /* hash function */ - int (*keycmp) (struct hashtab_val * h, const_hashtab_key_t key1, const_hashtab_key_t key2); /* key comparison function */ -} hashtab_val_t; - -typedef hashtab_val_t *hashtab_t; - -/* Define status codes for hash table functions */ -#define HASHTAB_SUCCESS 0 -#define HASHTAB_OVERFLOW -ENOMEM -#define HASHTAB_PRESENT -EEXIST -#define HASHTAB_MISSING -ENOENT - -/* - Creates a new hash table with the specified characteristics. - - Returns NULL if insufficient space is available or - the new hash table otherwise. - */ -extern hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h, - const_hashtab_key_t - key), - int (*keycmp) (hashtab_t h, - const_hashtab_key_t key1, - const_hashtab_key_t key2), - unsigned int size); -/* - Inserts the specified (key, datum) pair into the specified hash table. - - Returns HASHTAB_OVERFLOW if insufficient space is available or - HASHTAB_PRESENT if there is already an entry with the same key or - HASHTAB_SUCCESS otherwise. - */ -extern int hashtab_insert(hashtab_t h, hashtab_key_t k, hashtab_datum_t d); - -/* - Removes the entry with the specified key from the hash table. - Applies the specified destroy function to (key,datum,args) for - the entry. - - Returns HASHTAB_MISSING if no entry has the specified key or - HASHTAB_SUCCESS otherwise. - */ -extern int hashtab_remove(hashtab_t h, hashtab_key_t k, - void (*destroy) (hashtab_key_t k, - hashtab_datum_t d, - void *args), void *args); - -/* - Searches for the entry with the specified key in the hash table. - - Returns NULL if no entry has the specified key or - the datum of the entry otherwise. - */ -extern hashtab_datum_t hashtab_search(hashtab_t h, const_hashtab_key_t k); - -/* - Destroys the specified hash table. - */ -extern void hashtab_destroy(hashtab_t h); -extern void hashtab_destroy_key(hashtab_t h, - int (*destroy_key) (hashtab_key_t k)); - -/* - Applies the specified apply function to (key,datum,args) - for each entry in the specified hash table. - - The order in which the function is applied to the entries - is dependent upon the internal structure of the hash table. - - If apply returns a non-zero status, then hashtab_map will cease - iterating through the hash table and will propagate the error - return to its caller. - */ -extern int hashtab_map(hashtab_t h, - int (*apply) (hashtab_key_t k, - hashtab_datum_t d, - void *args), void *args); - -extern void hashtab_hash_eval(hashtab_t h, char *tag); - -#endif From patchwork Mon Aug 14 13:20:25 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: 13352861 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 4979DC04A6A for ; Mon, 14 Aug 2023 13:22:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230306AbjHNNVo (ORCPT ); Mon, 14 Aug 2023 09:21:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55442 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230378AbjHNNVT (ORCPT ); Mon, 14 Aug 2023 09:21:19 -0400 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EABA110CE for ; Mon, 14 Aug 2023 06:20:57 -0700 (PDT) Received: by mail-ej1-x631.google.com with SMTP id a640c23a62f3a-99bdcade7fbso562109566b.1 for ; Mon, 14 Aug 2023 06:20:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20221208; t=1692019256; x=1692624056; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=i9sB/i+fJ2D8NRrEogkLTLeWWVpbDLi6FEgRZblUVIM=; b=LDgIg5Udn5UUr3Xki2PJQhIBmx2AX4uf2Nd0z/5RFSb7o+uAKVJWkwdoduCZWRhWt6 xBjHIZClXDtnR9AW1jmD7r6h5LoZFQ/qExmmb7dDzTpKpGCCsM/MJUBXYtR3bVBuCNe6 hdLi6D/cocO5uepQjb0X/8mc9TXEkqWG8e+/K7163NFCzSjFOVAKq5VWK3Bxkmg1TLq8 lOJHjq9XPeIH8WFobopDIz6CK3t87lSatUhbd0bVXwHvpF2BE+iVwKAHx9aul04b8GIm qhYC7AczSLarYvRadsPSye5Boz7dHqyfcLnGWQ1oHSXpiuy91Hf0HhLhKAKzQMSDwG3o liJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692019256; x=1692624056; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=i9sB/i+fJ2D8NRrEogkLTLeWWVpbDLi6FEgRZblUVIM=; b=WqG0pQbbuiDfCoUtGsyxJ1fxz44auMVIXE/VlyyLDWp2tFo4eq6qLt39G7xLmGtkfl k2ktmnPwTjVuyHZm2ZxJjZ96HDM4/Y7y3UJod+uJ8vGOpZEHOFimh2p1EcTXF4wsCWIo jkY4RTdPFYKPVyNqHy8fwdIao+8KqAOdODHREsyehD/oo1iP5JLP4VHeXJZE5eGScdsQ yYQ0UGiwV0kPvRet+FWsh+pwcucnkC3AZ16mH1NEEsB93Tnz9RiepRPrcscQEa9UnuZw K3J4WIqr6I0PGL5E68GVOnYtWiDnb6eaVWXlBilUMPjEIL61w0ZvC/kSJCc/4sr6BARz jNNQ== X-Gm-Message-State: AOJu0YxHgotVyePjXFvb+Trr+MOvHgZoibh6RZQPC/DVQtzYIDap1ykB Ey+tbPfGGfGhuv6oIODkUJBlopN+M54lmuliVYY= X-Google-Smtp-Source: AGHT+IHz3ZzqHZJ9ggoSTaYwbUqioWBWuUoojPh9UDGE2PK+/EHUm4QaFEbL6pOHjKPYH9pklRzODA== X-Received: by 2002:a17:906:3f12:b0:99c:5623:a8fb with SMTP id c18-20020a1709063f1200b0099c5623a8fbmr8408181ejj.18.1692019256264; Mon, 14 Aug 2023 06:20:56 -0700 (PDT) Received: from debian_development.DebianHome (dynamic-095-112-167-009.95.112.pool.telefonica.de. [95.112.167.9]) by smtp.gmail.com with ESMTPSA id ck9-20020a170906c44900b00993664a9987sm5683541ejb.103.2023.08.14.06.20.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:20:55 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Cc: Evgeny Vereshchagin Subject: [RFC PATCH v2 27/27] libselinux: add selabel_file(5) fuzzer Date: Mon, 14 Aug 2023 15:20:25 +0200 Message-Id: <20230814132025.45364-28-cgzones@googlemail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230814132025.45364-1-cgzones@googlemail.com> References: <20230814132025.45364-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Add two fuzzer reading and performing lookup on selabel_file(5) databases. One fuzzer takes input in form of a textual fcontext definition, the other one takes compiled fcontexts definitions. The lookup key and whether to lookup any or a specific file type is also part of the generated input. CC: Evgeny Vereshchagin Signed-off-by: Christian Göttsche --- v2: add patch --- libselinux/fuzz/input | 0 .../fuzz/selabel_file_compiled-fuzzer.c | 279 ++++++++++++++++++ libselinux/fuzz/selabel_file_text-fuzzer.c | 223 ++++++++++++++ libselinux/src/label_file.c | 36 ++- libselinux/src/label_file.h | 17 ++ scripts/oss-fuzz.sh | 25 ++ 6 files changed, 561 insertions(+), 19 deletions(-) create mode 100644 libselinux/fuzz/input create mode 100644 libselinux/fuzz/selabel_file_compiled-fuzzer.c create mode 100644 libselinux/fuzz/selabel_file_text-fuzzer.c diff --git a/libselinux/fuzz/input b/libselinux/fuzz/input new file mode 100644 index 00000000..e69de29b diff --git a/libselinux/fuzz/selabel_file_compiled-fuzzer.c b/libselinux/fuzz/selabel_file_compiled-fuzzer.c new file mode 100644 index 00000000..cd0b41d7 --- /dev/null +++ b/libselinux/fuzz/selabel_file_compiled-fuzzer.c @@ -0,0 +1,279 @@ +#include +#include +#include +#include +#include + +#include + +#include "../src/label_file.h" + +extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); + +#define MEMFD_FILE_NAME "file_contexts" +#define CTRL_PARTIAL (1U << 0) +#define CTRL_FIND_ALL (1U << 1) +#define CTRL_MODE (1U << 2) + + +__attribute__ ((format(printf, 2, 3))) +static int null_log(int type __attribute__((unused)), const char *fmt __attribute__((unused)), ...) +{ + return 0; +} + +static int validate_context(char **ctxp) +{ + assert(strcmp(*ctxp, "<>") != 0); + + if (*ctxp[0] == '\0') { + errno = EINVAL; + return -1; + } + + return 0; +} + +static int write_full(int fd, const void *data, size_t size) +{ + ssize_t rc; + const unsigned char *p = data; + + while (size > 0) { + rc = write(fd, p, size); + if (rc == -1) { + if (errno == EINTR) + continue; + + return -1; + } + + p += rc; + size -= rc; + } + + return 0; +} + +static FILE* convert_data(const uint8_t *data, size_t size) +{ + FILE* stream; + int fd, rc; + + fd = memfd_create(MEMFD_FILE_NAME, MFD_CLOEXEC); + if (fd == -1) + return NULL; + + rc = write_full(fd, data, size); + if (rc == -1) { + close(fd); + return NULL; + } + + stream = fdopen(fd, "r"); + if (!stream) { + close(fd); + return NULL; + } + + rc = fseek(stream, 0L, SEEK_SET); + if (rc == -1) { + fclose(stream); + return NULL; + } + + return stream; +} + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + struct selabel_handle rec; + struct saved_data sdata = {}; + struct spec_node *root = NULL; + FILE* fp = NULL; + struct lookup_result *result = NULL; + uint8_t control; + uint8_t *fcontext_data1 = NULL, *fcontext_data2 = NULL, *fcontext_data3 = NULL; + char *key = NULL; + size_t fcontext_data1_len, fcontext_data2_len, fcontext_data3_len, key_len; + bool partial, find_all; + mode_t mode; + int rc; + + /* + * Treat first byte as control byte, whether to use partial mode, find all matches or mode to lookup + */ + if (size == 0) + return 0; + + control = data[0]; + data++; + size--; + + if (control & ~(CTRL_PARTIAL | CTRL_FIND_ALL | CTRL_MODE)) + return 0; + + partial = control & CTRL_PARTIAL; + find_all = control & CTRL_FIND_ALL; + /* S_IFSOCK has the highest integer value */ + mode = (control & CTRL_MODE) ? S_IFSOCK : 0; + + + /* + * Split the fuzzer input into up to four pieces: one to three compiled fcontext + * definitions (to mimic file_contexts, file_contexts.homedirs and file_contexts.local, + * and the lookup key + */ + const unsigned char separator[4] = { 0xde, 0xad, 0xbe, 0xef }; + const uint8_t *sep = memmem(data, size, separator, 4); + if (!sep || sep == data) + return 0; + + fcontext_data1_len = sep - data; + fcontext_data1 = malloc(fcontext_data1_len); + if (!fcontext_data1) + goto cleanup; + + memcpy(fcontext_data1, data, fcontext_data1_len); + data += fcontext_data1_len + 4; + size -= fcontext_data1_len + 4; + + sep = memmem(data, size, separator, 4); + if (sep) { + fcontext_data2_len = sep - data; + fcontext_data2 = malloc(fcontext_data2_len); + if (!fcontext_data2) + goto cleanup; + + memcpy(fcontext_data2, data, fcontext_data2_len); + data += fcontext_data2_len + 4; + size -= fcontext_data2_len + 4; + } + + sep = memmem(data, size, separator, 4); + if (sep) { + fcontext_data3_len = sep - data; + fcontext_data3 = malloc(fcontext_data3_len); + if (!fcontext_data3) + goto cleanup; + + memcpy(fcontext_data3, data, fcontext_data3_len); + data += fcontext_data3_len + 4; + size -= fcontext_data3_len + 4; + } + + key_len = size; + key = malloc(key_len + 1); + if (!key) + goto cleanup; + + memcpy(key, data, key_len); + key[key_len] = '\0'; + + + /* + * Mock selabel handle + */ + rec = (struct selabel_handle) { + .backend = SELABEL_CTX_FILE, + .validating = 1, + .data = &sdata, + }; + + selinux_set_callback(SELINUX_CB_LOG, (union selinux_callback) { .func_log = &null_log }); + /* validate to pre-compile regular expressions */ + selinux_set_callback(SELINUX_CB_VALIDATE, (union selinux_callback) { .func_validate = &validate_context }); + + root = calloc(1, sizeof(*root)); + if (!root) + goto cleanup; + + sdata.root = root; + + fp = convert_data(fcontext_data1, fcontext_data1_len); + if (!fp) + goto cleanup; + + errno = 0; + rc = load_mmap(fp, fcontext_data1_len, &rec, MEMFD_FILE_NAME); + if (rc) { + assert(errno != 0); + goto cleanup; + } + + fclose(fp); + + fp = convert_data(fcontext_data2, fcontext_data2_len); + if (!fp) + goto cleanup; + + errno = 0; + rc = load_mmap(fp, fcontext_data2_len, &rec, MEMFD_FILE_NAME); + if (rc) { + assert(errno != 0); + goto cleanup; + } + + fclose(fp); + + fp = convert_data(fcontext_data3, fcontext_data3_len); + if (!fp) + goto cleanup; + + errno = 0; + rc = load_mmap(fp, fcontext_data3_len, &rec, MEMFD_FILE_NAME); + if (rc) { + assert(errno != 0); + goto cleanup; + } + + sort_specs(&sdata); + + errno = 0; + result = lookup_all(&rec, key, mode, partial, find_all); + + if (!result) + assert(errno != 0); + + for (const struct lookup_result *res = result; res; res = res->next) { + assert(res->regex_str); + assert(res->regex_str[0] != '\0'); + assert(res->lr->ctx_raw); + assert(res->lr->ctx_raw[0] != '\0'); + assert(strcmp(res->lr->ctx_raw, "<>") != 0); + assert(!res->lr->ctx_trans); + assert(res->lr->validated); + assert(res->prefix_len <= strlen(res->regex_str)); + } + + +cleanup: + free_lookup_result(result); + if (fp) + fclose(fp); + if (sdata.root) { + free_spec_node(sdata.root); + free(sdata.root); + } + + { + struct mmap_area *area, *last_area; + + area = sdata.mmap_areas; + while (area) { + rc = munmap(area->addr, area->len); + assert(rc == 0); + last_area = area; + area = area->next; + free(last_area); + } + } + + free(key); + free(fcontext_data3); + free(fcontext_data2); + free(fcontext_data1); + + /* Non-zero return values are reserved for future use. */ + return 0; +} diff --git a/libselinux/fuzz/selabel_file_text-fuzzer.c b/libselinux/fuzz/selabel_file_text-fuzzer.c new file mode 100644 index 00000000..fd25078f --- /dev/null +++ b/libselinux/fuzz/selabel_file_text-fuzzer.c @@ -0,0 +1,223 @@ +#include +#include +#include +#include +#include + +#include + +#include "../src/label_file.h" + +extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); + +#define MEMFD_FILE_NAME "file_contexts" +#define CTRL_PARTIAL (1U << 0) +#define CTRL_FIND_ALL (1U << 1) +#define CTRL_MODE (1U << 2) + + +__attribute__ ((format(printf, 2, 3))) +static int null_log(int type __attribute__((unused)), const char *fmt __attribute__((unused)), ...) +{ + return 0; +} + +static int validate_context(char **ctxp) +{ + assert(strcmp(*ctxp, "<>") != 0); + + if (*ctxp[0] == '\0') { + errno = EINVAL; + return -1; + } + + return 0; +} + +static int write_full(int fd, const void *data, size_t size) +{ + ssize_t rc; + const unsigned char *p = data; + + while (size > 0) { + rc = write(fd, p, size); + if (rc == -1) { + if (errno == EINTR) + continue; + + return -1; + } + + p += rc; + size -= rc; + } + + return 0; +} + +static FILE* convert_data(const uint8_t *data, size_t size) +{ + FILE* stream; + int fd, rc; + + fd = memfd_create(MEMFD_FILE_NAME, MFD_CLOEXEC); + if (fd == -1) + return NULL; + + rc = write_full(fd, data, size); + if (rc == -1) { + close(fd); + return NULL; + } + + stream = fdopen(fd, "r"); + if (!stream) { + close(fd); + return NULL; + } + + rc = fseek(stream, 0L, SEEK_SET); + if (rc == -1) { + fclose(stream); + return NULL; + } + + return stream; +} + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + struct selabel_handle rec; + struct saved_data sdata = {}; + struct spec_node *root = NULL; + FILE* fp = NULL; + struct lookup_result *result = NULL; + uint8_t control; + uint8_t *fcontext_data = NULL; + char *key = NULL; + size_t fcontext_data_len, key_len; + bool partial, find_all; + mode_t mode; + int rc; + + /* + * Treat first byte as control byte, whether to use partial mode, find all matches or mode to lookup + */ + if (size == 0) + return 0; + + control = data[0]; + data++; + size--; + + if (control & ~(CTRL_PARTIAL | CTRL_FIND_ALL | CTRL_MODE)) + return 0; + + partial = control & CTRL_PARTIAL; + find_all = control & CTRL_FIND_ALL; + /* S_IFSOCK has the highest integer value */ + mode = (control & CTRL_MODE) ? S_IFSOCK : 0; + + + /* + * Split the fuzzer input into two pieces: the textual fcontext definition and the lookup key + */ + const unsigned char separator[4] = { 0xde, 0xad, 0xbe, 0xef }; + const uint8_t *sep = memmem(data, size, separator, 4); + if (!sep || sep == data) + return 0; + + fcontext_data_len = sep - data; + fcontext_data = malloc(fcontext_data_len); + if (!fcontext_data) + goto cleanup; + + memcpy(fcontext_data, data, fcontext_data_len); + + key_len = size - fcontext_data_len - 4; + key = malloc(key_len + 1); + if (!key) + goto cleanup; + + memcpy(key, sep + 4, key_len); + key[key_len] = '\0'; + + + /* + * Mock selabel handle + */ + rec = (struct selabel_handle) { + .backend = SELABEL_CTX_FILE, + .validating = 1, + .data = &sdata, + }; + + selinux_set_callback(SELINUX_CB_LOG, (union selinux_callback) { .func_log = &null_log }); + /* validate to pre-compile regular expressions */ + selinux_set_callback(SELINUX_CB_VALIDATE, (union selinux_callback) { .func_validate = &validate_context }); + + root = calloc(1, sizeof(*root)); + if (!root) + goto cleanup; + + sdata.root = root; + + fp = convert_data(fcontext_data, fcontext_data_len); + if (!fp) + goto cleanup; + + errno = 0; + rc = process_text_file(fp, &rec, MEMFD_FILE_NAME); + if (rc) { + assert(errno != 0); + goto cleanup; + } + + sort_specs(&sdata); + + errno = 0; + result = lookup_all(&rec, key, mode, partial, find_all); + + if (!result) + assert(errno != 0); + + for (const struct lookup_result *res = result; res; res = res->next) { + assert(res->regex_str); + assert(res->regex_str[0] != '\0'); + assert(res->lr->ctx_raw); + assert(res->lr->ctx_raw[0] != '\0'); + assert(strcmp(res->lr->ctx_raw, "<>") != 0); + assert(!res->lr->ctx_trans); + assert(res->lr->validated); + assert(res->prefix_len <= strlen(res->regex_str)); + } + + +cleanup: + free_lookup_result(result); + if (fp) + fclose(fp); + if (sdata.root) { + free_spec_node(sdata.root); + free(sdata.root); + } + + { + struct mmap_area *area, *last_area; + + area = sdata.mmap_areas; + while (area) { + rc = munmap(area->addr, area->len); + assert(rc == 0); + last_area = area; + area = area->next; + free(last_area); + } + } + + free(key); + free(fcontext_data); + + /* Non-zero return values are reserved for future use. */ + return 0; +} diff --git a/libselinux/src/label_file.c b/libselinux/src/label_file.c index bb4b7130..60510551 100644 --- a/libselinux/src/label_file.c +++ b/libselinux/src/label_file.c @@ -26,6 +26,13 @@ #include "label_file.h" +#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION +# define FUZZ_EXTERN +#else +# define FUZZ_EXTERN static +#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */ + + /* * Warn about duplicate specifications. */ @@ -113,8 +120,8 @@ static int nodups_spec_node(const struct spec_node *node, const char *path) return rc; } -static int process_text_file(FILE *fp, - struct selabel_handle *rec, const char *path) +FUZZ_EXTERN int process_text_file(FILE *fp, + struct selabel_handle *rec, const char *path) { int rc; size_t line_len; @@ -669,8 +676,8 @@ static int load_mmap_spec_node(struct mmap_area *mmap_area, const char *path, bo return 0; } -static int load_mmap(FILE *fp, const size_t len, struct selabel_handle *rec, - const char *path) +FUZZ_EXTERN int load_mmap(FILE *fp, const size_t len, struct selabel_handle *rec, + const char *path) { struct saved_data *data = (struct saved_data *)rec->data; struct spec_node *root = NULL; @@ -1363,16 +1370,7 @@ static uint32_t search_literal_spec(const struct literal_spec *array, uint32_t s return (uint32_t)-1; } -struct lookup_result { - const char *regex_str; - struct selabel_lookup_rec *lr; - mode_t mode; - bool has_meta_chars; - uint16_t prefix_len; - struct lookup_result *next; -}; - -static void free_lookup_result(struct lookup_result *result) +FUZZ_EXTERN void free_lookup_result(struct lookup_result *result) { struct lookup_result *tmp; @@ -1577,11 +1575,11 @@ static struct spec_node* lookup_find_deepest_node(struct spec_node *node, const // Finds all the matches of |key| in the given context. Returns the result in // the allocated array and updates the match count. If match_count is NULL, // stops early once the 1st match is found. -static struct lookup_result *lookup_all(struct selabel_handle *rec, - const char *key, - int type, - bool partial, - bool find_all) +FUZZ_EXTERN struct lookup_result *lookup_all(struct selabel_handle *rec, + const char *key, + int type, + bool partial, + bool find_all) { struct saved_data *data = (struct saved_data *)rec->data; struct lookup_result *result = NULL; diff --git a/libselinux/src/label_file.h b/libselinux/src/label_file.h index ee01defe..8e4824e8 100644 --- a/libselinux/src/label_file.h +++ b/libselinux/src/label_file.h @@ -40,6 +40,23 @@ /* Required selinux_restorecon and selabel_get_digests_all_partial_matches() */ #define RESTORECON_PARTIAL_MATCH_DIGEST "security.sehash" +/* Only exported for fuzzing*/ +struct lookup_result { + const char *regex_str; + struct selabel_lookup_rec *lr; + mode_t mode; + bool has_meta_chars; + uint16_t prefix_len; + struct lookup_result *next; +}; +#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION +extern int load_mmap(FILE *fp, const size_t len, struct selabel_handle *rec, const char *path); +extern int process_text_file(FILE *fp, struct selabel_handle *rec, const char *path); +extern void free_lookup_result(struct lookup_result *result); +extern struct lookup_result *lookup_all(struct selabel_handle *rec, const char *key, int type, bool partial, bool find_all); +#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */ + + struct selabel_sub { char *src; unsigned int slen; diff --git a/scripts/oss-fuzz.sh b/scripts/oss-fuzz.sh index 72d275e8..e51efe74 100755 --- a/scripts/oss-fuzz.sh +++ b/scripts/oss-fuzz.sh @@ -44,10 +44,13 @@ export LIB_FUZZING_ENGINE=${LIB_FUZZING_ENGINE:--fsanitize=fuzzer} rm -rf "$DESTDIR" make -C libsepol clean +make -C libselinux clean # LIBSO and LIBMAP shouldn't be expanded here because their values are unknown until Makefile # has been read by make # shellcheck disable=SC2016 make -C libsepol V=1 LD_SONAME_FLAGS='-soname,$(LIBSO),--version-script=$(LIBMAP)' -j"$(nproc)" install +# shellcheck disable=SC2016 +make -C libselinux V=1 LD_SONAME_FLAGS='-soname,$(LIBSO),--version-script=libselinux.map' -j"$(nproc)" install ## secilc fuzzer ## @@ -70,3 +73,25 @@ $CC $CFLAGS -c -o binpolicy-fuzzer.o libsepol/fuzz/binpolicy-fuzzer.c $CXX $CXXFLAGS $LIB_FUZZING_ENGINE binpolicy-fuzzer.o "$DESTDIR/usr/lib/libsepol.a" -o "$OUT/binpolicy-fuzzer" zip -j "$OUT/binpolicy-fuzzer_seed_corpus.zip" libsepol/fuzz/policy.bin + +## selabel-file text fcontext based fuzzer ## + +# CFLAGS, CXXFLAGS and LIB_FUZZING_ENGINE have to be split to be accepted by +# the compiler/linker so they shouldn't be quoted +# shellcheck disable=SC2086 +$CC $CFLAGS -DUSE_PCRE2 -DPCRE2_CODE_UNIT_WIDTH=8 -c -o selabel_file_text-fuzzer.o libselinux/fuzz/selabel_file_text-fuzzer.c +# shellcheck disable=SC2086 +$CXX $CXXFLAGS $LIB_FUZZING_ENGINE selabel_file_text-fuzzer.o "$DESTDIR/usr/lib/libselinux.a" -lpcre2-8 -o "$OUT/selabel_file_text-fuzzer" + +zip -j "$OUT/selabel_file_text-fuzzer_seed_corpus.zip" libselinux/fuzz/input + +## selabel-file compiled fcontext based fuzzer ## + +# CFLAGS, CXXFLAGS and LIB_FUZZING_ENGINE have to be split to be accepted by +# the compiler/linker so they shouldn't be quoted +# shellcheck disable=SC2086 +$CC $CFLAGS -DUSE_PCRE2 -DPCRE2_CODE_UNIT_WIDTH=8 -c -o selabel_file_compiled-fuzzer.o libselinux/fuzz/selabel_file_compiled-fuzzer.c +# shellcheck disable=SC2086 +$CXX $CXXFLAGS $LIB_FUZZING_ENGINE selabel_file_compiled-fuzzer.o "$DESTDIR/usr/lib/libselinux.a" -lpcre2-8 -o "$OUT/selabel_file_compiled-fuzzer" + +zip -j "$OUT/selabel_file_compiled-fuzzer_seed_corpus.zip" libselinux/fuzz/input