From patchwork Fri Jan 31 17:55:53 2025 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: 13955628 X-Patchwork-Delegate: plautrba@redhat.com Received: from server02.seltendoof.de (server02.seltendoof.de [168.119.48.163]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E1EF61DFF0 for ; Fri, 31 Jan 2025 18:03:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=168.119.48.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738346588; cv=none; b=HYMRYmxRnzvpu2XsHlrztN3xM7YLYooadD+tZtlvIdVLR70d8Oa294oTmZ6u+b//O6F3FYAxeShMu110NWqtXTivo8QqsuicTBrRT4ZI1pafIP3bvmqxFif8XKb44sd4v3PnmdpRGyVb6SAlYt00oWhs3OTch22wO6stPt8Iwjk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738346588; c=relaxed/simple; bh=h8+R/JHHlNjjCxsdntDlVlA5pjrZn6NU51OJMxSsewk=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=f25y1ilJ4RrlVicrbQ2GtZU1oARGlWttLCHEHxwjKltnEquSWDBxm61bHcashMEmzdqjhwGGyz1VB3mvctk+FcFBLAPG7eewzEJQKkiW/DhPFANekUm1eRLwfSTO0rHl2a/ZEJYrKRV3XjNjGj1NjhjScUgqqIUjJ5W0RJEE98M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=seltendoof.de; spf=pass smtp.mailfrom=seltendoof.de; dkim=pass (2048-bit key) header.d=seltendoof.de header.i=@seltendoof.de header.b=hB/k/xq/; arc=none smtp.client-ip=168.119.48.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=seltendoof.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=seltendoof.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=seltendoof.de header.i=@seltendoof.de header.b="hB/k/xq/" From: =?utf-8?q?Christian_G=C3=B6ttsche?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=seltendoof.de; s=2023072701; t=1738346160; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=6XBeX7pPKKCqqC36QZCOplstPy3/W6QHvjEbjcLL0C8=; b=hB/k/xq/89Zmo0q5A2Yev0V57SsgOOGHF+CONOrQ9vNT2IRAoipn3voHLMAaM8rFMZ4Iyf Y4IaGtF4OA7LBIRVECNKBn/GgANlpQ2fSEv+QRZjOGsYvt6Hsnvpm3UPURLZp8yeHq5N6v FV5U0Y1ilWaCCvmNU7Odh4mD5kg02CV5rvvzsoJTBKONdtnGzfZTiDIkvL9L2SPisf7LG9 d9vqfXvcAliohyxGLKv54p7fHevCl7UAoONHnEjqOSa6+OP2Tf9F9gBv4+oY+VHzEsDHCY AFafXzgJQShdrrPheAjETTHG0hzgmXeM1TEgwovPSgx5PA64T4fvdpCi/hn4yQ== To: selinux@vger.kernel.org Cc: =?utf-8?q?Christian_G=C3=B6ttsche?= Subject: [PATCH 1/4] libselinux: constify global strings Date: Fri, 31 Jan 2025 18:55:53 +0100 Message-ID: <20250131175556.21836-1-cgoettsche@seltendoof.de> Reply-To: cgzones@googlemail.com Precedence: bulk X-Mailing-List: selinux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Christian Göttsche Declare the read-only encode table const. Drop the only once used global variable selinux_rootpath. Signed-off-by: Christian Göttsche --- libselinux/src/compute_create.c | 2 +- libselinux/src/selinux_config.c | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/libselinux/src/compute_create.c b/libselinux/src/compute_create.c index d19baa0b..ff8553bc 100644 --- a/libselinux/src/compute_create.c +++ b/libselinux/src/compute_create.c @@ -33,7 +33,7 @@ static int object_name_encode(const char *objname, char *buffer, size_t buflen) return -1; buffer[offset++] = '+'; } else { - static const char *table = "0123456789ABCDEF"; + static const char *const table = "0123456789ABCDEF"; int l = (code & 0x0f); int h = (code & 0xf0) >> 4; diff --git a/libselinux/src/selinux_config.c b/libselinux/src/selinux_config.c index 1d8cfb71..75db14ba 100644 --- a/libselinux/src/selinux_config.c +++ b/libselinux/src/selinux_config.c @@ -153,7 +153,6 @@ static int setpolicytype(const char *type) } static char *selinux_policyroot = NULL; -static const char *selinux_rootpath = SELINUXDIR; static void init_selinux_config(void) { @@ -312,7 +311,7 @@ int selinux_set_policy_root(const char *path) const char *selinux_path(void) { - return selinux_rootpath; + return SELINUXDIR; } From patchwork Fri Jan 31 17:55:54 2025 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: 13955629 X-Patchwork-Delegate: plautrba@redhat.com Received: from server02.seltendoof.de (server02.seltendoof.de [168.119.48.163]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E1FF41CBA18 for ; Fri, 31 Jan 2025 18:03:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=168.119.48.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738346588; cv=none; b=JAJY3sqpuQKzYbSfS5j4SyJxY1EN+KsiWyv08+GnfBPZts1mB+hCCcxggPuEfG7qcWnA7m8WiYOEKtKFMuOkfZ5GQ9L2z856GE1dGbGvB6kAhfRowWCURpKprBlXPKiOewjGkq4TspScbleaODiX34xz/FUIsP+mDEcFF+hDXso= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738346588; c=relaxed/simple; bh=tHEyJqntFJiDyxebK33D8gMUxIMAtEiLgLLX+Yo2mRY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=EK8FpqRP+MDJRldFMJjXvggAEAZJPQwCodW9JaPkQlQgPN/bepWfYijszI2pAQUp8e+glIXVYlX7wtRV33oPBnphb2M7bYVSJoLlIz3qHNpMraDIMVCGshxdQdrecdTWo9szNfNfM4It0xRoSK3TXvwJkQdlJUWajEWBvU+Nw8w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=seltendoof.de; spf=pass smtp.mailfrom=seltendoof.de; dkim=pass (2048-bit key) header.d=seltendoof.de header.i=@seltendoof.de header.b=SCvdurX9; arc=none smtp.client-ip=168.119.48.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=seltendoof.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=seltendoof.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=seltendoof.de header.i=@seltendoof.de header.b="SCvdurX9" From: =?utf-8?q?Christian_G=C3=B6ttsche?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=seltendoof.de; s=2023072701; t=1738346160; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gcerc/1Iawinkz48SJYXnyGFgqWe0wAgmwpUD+Pox6s=; b=SCvdurX9l1UtH1seU2yTM/Jhp+rRw7IbjvlYljUel4xurQLtNKI4co3b+0W14IukuEfY1h Sser/vjI4NEhHi62cRrrLemOP7Ofl8EeWeJHnecyrtz49Ym0XGCrP4MfZbrK8OeXr4W29J XUoV0dZfU/KU+VPRHmKTQ9FW4k4pV1un3x1nJIzMUgamnoLse9TM5R7RduOTgwFFP7RLRJ kT8vgcqJaS0mfYbCMM2+IoAp3dmQ0jiqbjJ8CBF5uSbv0ebwl8ULcy7Sf9aVPNaBAykUXB ijLfPaUu9kKyhX/QAY9SnlG90I4+HWanjL5qQWG4SiPadVZU3qJDMsehK8Ph8Q== To: selinux@vger.kernel.org Cc: =?utf-8?q?Christian_G=C3=B6ttsche?= Subject: [PATCH 2/4] libselinux: use local instead of global error buffer Date: Fri, 31 Jan 2025 18:55:54 +0100 Message-ID: <20250131175556.21836-2-cgoettsche@seltendoof.de> In-Reply-To: <20250131175556.21836-1-cgoettsche@seltendoof.de> References: <20250131175556.21836-1-cgoettsche@seltendoof.de> Reply-To: cgzones@googlemail.com Precedence: bulk X-Mailing-List: selinux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Christian Göttsche Use a function local, and thus thread-safe, buffer for error messages instead of a shared global one. Signed-off-by: Christian Göttsche --- libselinux/src/label_file.c | 4 ++-- libselinux/src/label_file.h | 19 +++++++------------ 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/libselinux/src/label_file.c b/libselinux/src/label_file.c index 56e20949..85d42ff2 100644 --- a/libselinux/src/label_file.c +++ b/libselinux/src/label_file.c @@ -1662,7 +1662,7 @@ static struct lookup_result *lookup_check_node(struct spec_node *node, const cha for (uint32_t i = n->regex_specs_num; i > 0; i--) { /* search in reverse order */ struct regex_spec *rspec = &n->regex_specs[i - 1]; - const char *errbuf = NULL; + char errbuf[256]; int rc; if (child_regex_match && @@ -1673,7 +1673,7 @@ static struct lookup_result *lookup_check_node(struct spec_node *node, const cha if (file_kind != LABEL_FILE_KIND_ALL && rspec->file_kind != LABEL_FILE_KIND_ALL && file_kind != rspec->file_kind) continue; - if (compile_regex(rspec, &errbuf) < 0) { + if (compile_regex(rspec, errbuf, sizeof(errbuf)) < 0) { COMPAT_LOG(SELINUX_ERROR, "Failed to compile regular expression '%s': %s\n", rspec->regex_str, errbuf); goto fail; diff --git a/libselinux/src/label_file.h b/libselinux/src/label_file.h index 60ebbb47..ad7699e6 100644 --- a/libselinux/src/label_file.h +++ b/libselinux/src/label_file.h @@ -388,16 +388,17 @@ static inline void sort_specs(struct saved_data *data) sort_spec_node(data->root, NULL); } -static inline int compile_regex(struct regex_spec *spec, const char **errbuf) +static inline int compile_regex(struct regex_spec *spec, char *errbuf, size_t errbuf_size) { const char *reg_buf; char *anchored_regex, *cp; struct regex_error_data error_data; - static char regex_error_format_buffer[256]; size_t len; int rc; bool regex_compiled; + *errbuf = '\0'; + /* We really want pthread_once() here, but since its * init_routine does not take a parameter, it's not possible * to use, so we generate the same effect with atomics and a @@ -435,9 +436,8 @@ static inline int compile_regex(struct regex_spec *spec, const char **errbuf) len = strlen(reg_buf); cp = anchored_regex = malloc(len + 3); if (!anchored_regex) { - if (errbuf) - *errbuf = "out of memory"; __pthread_mutex_unlock(&spec->regex_lock); + snprintf(errbuf, errbuf_size, "out of memory"); return -1; } @@ -452,12 +452,7 @@ static inline int compile_regex(struct regex_spec *spec, const char **errbuf) rc = regex_prepare_data(&spec->regex, anchored_regex, &error_data); free(anchored_regex); if (rc < 0) { - if (errbuf) { - regex_format_error(&error_data, - regex_error_format_buffer, - sizeof(regex_error_format_buffer)); - *errbuf = ®ex_error_format_buffer[0]; - } + regex_format_error(&error_data, errbuf, errbuf_size); __pthread_mutex_unlock(&spec->regex_lock); errno = EINVAL; return -1; @@ -624,9 +619,9 @@ static int insert_spec(const struct selabel_handle *rec, struct saved_data *data data->num_specs++; if (rec->validating) { - const char *errbuf = NULL; + char errbuf[256]; - if (compile_regex(&node->regex_specs[id], &errbuf)) { + if (compile_regex(&node->regex_specs[id], errbuf, sizeof(errbuf))) { COMPAT_LOG(SELINUX_ERROR, "%s: line %u has invalid regex %s: %s\n", path, lineno, regex, errbuf); From patchwork Fri Jan 31 17:55:55 2025 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: 13955630 X-Patchwork-Delegate: plautrba@redhat.com Received: from server02.seltendoof.de (server02.seltendoof.de [168.119.48.163]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E1FA11C174E for ; Fri, 31 Jan 2025 18:03:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=168.119.48.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738346589; cv=none; b=hGuzBziafbzMK2M7vJbh5E6KXxL+0JVbi+/0weAEXp9CqcrydsGTGcqSH1lWo5eSSJ188owFVyWupE4bWSIMADxAaXaaMlzGKMfA1mV2B30uLWoIfh9EpYs/24gl+mRfl1CnBANTZRKK3iF5QLjtHbZFXp2ZKA+HzYmdKjzT9u4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738346589; c=relaxed/simple; bh=EFloP0hXZ9hWdaEZ2q028NNJdpuCQKEbrGwecCr6/is=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=rdR/6XhyY1R27oYql0Mswi12CBRuTBVnNRsqimYqf6P0vfapgRh56/M4fJQuJiS9DVpTpCilqPcNRA0OXaGFEJzQnrfuEmhr7EeF0g9aoGlgIdgSB77t/6nxD9hilPov68ev4ibo3XJgvhz+9UvuefToyBwMNRsJ79NPGpr7ZQU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=seltendoof.de; spf=pass smtp.mailfrom=seltendoof.de; dkim=pass (2048-bit key) header.d=seltendoof.de header.i=@seltendoof.de header.b=BO/QihhZ; arc=none smtp.client-ip=168.119.48.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=seltendoof.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=seltendoof.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=seltendoof.de header.i=@seltendoof.de header.b="BO/QihhZ" From: =?utf-8?q?Christian_G=C3=B6ttsche?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=seltendoof.de; s=2023072701; t=1738346161; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7FbAa6sbcddsM+zDfU5IZNXedZa+5xN2UzAhbdYND3w=; b=BO/QihhZKKnk7Q/020v4k9YXW0anZ6y3y778nJoRK8zovGpFdJClhVy1c803fYD+vEKWQc cDnlmwQNubK8o3PFsecJWn0498/zQGMwsEzMH/1wUwV9bL/2yvfr9aD4tWm/JGewtgJme3 YfbasqPbuweYvHYH3rIqbNUoVUDWepWti35QiAqpxFmoYL6wcbLEYoYRz/ZYrcp6ewcAL6 e5iQR4TnfZ24KfDCJTUN+V4Qd2S74RgqiQjlKZwDdipDyBgXsRHvdR+i91/wB3ITQlF82o tiNAxyhMMiHOakYZwd+5Mk3F47/K1LZ+xsVEYkmL+beqHfXdVpum4Ya/Tt8GLw== To: selinux@vger.kernel.org Cc: =?utf-8?q?Christian_G=C3=B6ttsche?= Subject: [PATCH 3/4] libselinux: initialize regex arch string in a thread safe way Date: Fri, 31 Jan 2025 18:55:55 +0100 Message-ID: <20250131175556.21836-3-cgoettsche@seltendoof.de> In-Reply-To: <20250131175556.21836-1-cgoettsche@seltendoof.de> References: <20250131175556.21836-1-cgoettsche@seltendoof.de> Reply-To: cgzones@googlemail.com Precedence: bulk X-Mailing-List: selinux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Christian Göttsche Synchronize the initialization of the regex architecture string. Signed-off-by: Christian Göttsche --- libselinux/src/regex.c | 46 ++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/libselinux/src/regex.c b/libselinux/src/regex.c index 182c8c89..976f00d4 100644 --- a/libselinux/src/regex.c +++ b/libselinux/src/regex.c @@ -30,32 +30,38 @@ #endif #ifdef USE_PCRE2 -char const *regex_arch_string(void) +static pthread_once_t once = PTHREAD_ONCE_INIT; +static char arch_string_buffer[32]; + +static void regex_arch_string_init(void) { - static char arch_string_buffer[32]; - static char const *arch_string = ""; - char const *endianness = NULL; + char const *endianness; int rc; - if (arch_string[0] == '\0') { - if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) - endianness = "el"; - else if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) - endianness = "eb"; + if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) + endianness = "el"; + else if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) + endianness = "eb"; + else { + arch_string_buffer[0] = '\0'; + return; + } - if (!endianness) - return NULL; + rc = snprintf(arch_string_buffer, sizeof(arch_string_buffer), + "%zu-%zu-%s", sizeof(void *), + sizeof(REGEX_ARCH_SIZE_T), + endianness); + if (rc < 0 || (size_t)rc >= sizeof(arch_string_buffer)) { + arch_string_buffer[0] = '\0'; + return; + } +} - rc = snprintf(arch_string_buffer, sizeof(arch_string_buffer), - "%zu-%zu-%s", sizeof(void *), - sizeof(REGEX_ARCH_SIZE_T), - endianness); - if (rc < 0) - abort(); +const char *regex_arch_string(void) +{ + __selinux_once(once, regex_arch_string_init); - arch_string = &arch_string_buffer[0]; - } - return arch_string; + return arch_string_buffer[0] != '\0' ? arch_string_buffer : NULL; } struct regex_data { From patchwork Fri Jan 31 17:55:56 2025 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: 13955627 X-Patchwork-Delegate: plautrba@redhat.com Received: from server02.seltendoof.de (server02.seltendoof.de [168.119.48.163]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E1F3B42AA1 for ; Fri, 31 Jan 2025 18:03:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=168.119.48.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738346588; cv=none; b=mCXHnymPPlXPcOQoPLYFF1BwwSC3xsrIygIOpmDeYkDrjeg+CsEfQh8nhUVXto7DEtcKUI8NU4rubUh87SmhC34jNEEljwg3/We7u90zlqUlWQJ5i0s+m5Z6KWNQbImzZkhg3iLiI5d+b1scoI0B93HdyXKJ+S41RYSIccii0R4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738346588; c=relaxed/simple; bh=Gsp+eWv/ooh2BV906VmWbsjfRLcHq8Km69BBI341dmw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=EvbWrfEC6NwT5/H7G5o7SYXjjvzDmidZjQwG7giUQ8VWFwaXX1KSMVkg33vdecsDr11u6dcNoJmOIFJlqpZOsvFWpmoO8NyjY7byftLYt683CTHTqDR1+sqkNSnhNRC0GVxhbSOwX8h+4s1vXA8Z8M94XpDknRymBvjn8EzYK64= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=seltendoof.de; spf=pass smtp.mailfrom=seltendoof.de; dkim=pass (2048-bit key) header.d=seltendoof.de header.i=@seltendoof.de header.b=j79eEqkj; arc=none smtp.client-ip=168.119.48.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=seltendoof.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=seltendoof.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=seltendoof.de header.i=@seltendoof.de header.b="j79eEqkj" From: =?utf-8?q?Christian_G=C3=B6ttsche?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=seltendoof.de; s=2023072701; t=1738346162; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1eitghxm1A7gw8QF8qRkdLkAOEKgmS93NACVjLyz0NA=; b=j79eEqkjCIrbesN3Au7G37kLErYE7R6+G50ZNSdSCDZpA2gC6xNqLZhDV8V6yq7bRJqjb7 xuaqdD9hsKtdhcY77hmtz8cUxHCcr+LedDDvbgG490go5QXgbo/RXTBfv3WWjBuO/BCG7I 3WAor4SKhkuHmFovccqiRu95XPGIOV3YL4tdAqd0eiWgL/XUnxZhbDkM6MbuPISB4t2Ug7 Od7M1em9flEz5+bodl94lMrwNqYgt/ex9b5/4gwNrZ9gvD5D0ob6SGx9YC8gNL6pjo74sM r9TRRuaRTBVIEeT3bhGGb0q0pDzXISqFEeolrr72oHNo6ujxihzVkcwWkyNbiA== To: selinux@vger.kernel.org Cc: =?utf-8?q?Christian_G=C3=B6ttsche?= Subject: [PATCH 4/4] libselinux: limit fcontext regex path length Date: Fri, 31 Jan 2025 18:55:56 +0100 Message-ID: <20250131175556.21836-4-cgoettsche@seltendoof.de> In-Reply-To: <20250131175556.21836-1-cgoettsche@seltendoof.de> References: <20250131175556.21836-1-cgoettsche@seltendoof.de> Reply-To: cgzones@googlemail.com Precedence: bulk X-Mailing-List: selinux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Christian Göttsche Limit the length of regular expression paths in fcontext source definitions to reduce the worst case regex compilation time for abnormal inputs. Reported-by: oss-fuzz (issue 393203212) Signed-off-by: Christian Göttsche --- libselinux/src/label_file.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libselinux/src/label_file.h b/libselinux/src/label_file.h index ad7699e6..7a9834a0 100644 --- a/libselinux/src/label_file.h +++ b/libselinux/src/label_file.h @@ -434,6 +434,12 @@ static inline int compile_regex(struct regex_spec *spec, char *errbuf, size_t er reg_buf = spec->regex_str; /* Anchor the regular expression. */ len = strlen(reg_buf); + if (len >= 4096) { + __pthread_mutex_unlock(&spec->regex_lock); + snprintf(errbuf, errbuf_size, "regex of length %zu too long", len); + errno = EINVAL; + return -1; + } cp = anchored_regex = malloc(len + 3); if (!anchored_regex) { __pthread_mutex_unlock(&spec->regex_lock);