From patchwork Wed Nov 9 19:56:38 2022 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: 13038010 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 58690C4332F for ; Wed, 9 Nov 2022 19:56:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231433AbiKIT4z (ORCPT ); Wed, 9 Nov 2022 14:56:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49976 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230128AbiKIT4w (ORCPT ); Wed, 9 Nov 2022 14:56:52 -0500 Received: from mail-ej1-x629.google.com (mail-ej1-x629.google.com [IPv6:2a00:1450:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D1A02F00A for ; Wed, 9 Nov 2022 11:56:51 -0800 (PST) Received: by mail-ej1-x629.google.com with SMTP id f27so49824691eje.1 for ; Wed, 09 Nov 2022 11:56:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=p3evPF9n6/pYBL1A2bZLvFrT14TggGNeYka+kkhwyBk=; b=Kyi4xfycQBr1DMYW+um/cJpvjrfDIA5LvgH32PtE2z4xJRd2H77Yq7UN7GV1mu0tQq jr5xXqq6NiEOovM0ExjZ1gQ1ceN5Ongsn/0lJPAiQLPmL7cyBz0mi73kgZvF+H3xmVhT 2E7WFeQwE0P3jTE5EvNwqMTYqFA+IsmPCqs73nf1qkS3vpkD86DELgp/cjYrWr2+0Km4 7F6WF0ZO6zDDGVtpa6NnN0s/PDbDjsYPwlmvSQOxNLoQ3g72oFFaudqIfFCmMS+Y0nwA s/HIUgkQXOhC6aGsa2rzHVGOYBf5kJ7vpOSmPoQlmm5Mo2TDLFCGKaq9eiE5z27VnpFM PGlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=p3evPF9n6/pYBL1A2bZLvFrT14TggGNeYka+kkhwyBk=; b=6eqgssrg/wQwYegRYLxdvNLn7Oo4N96Y6VwiuV00rsAA9PX7kCXTTOLYwL9C+ObMPD nAfOFbzBqWzgiujA94xl8qJ+eYWwFma8mO4MDVFljAywVWkpsnnjBdUjCi3vXy7OGd7W UVXRgZoncm9uXupICdpF5othY6XfC57q1XVyF4mmsuhsYCO4SSTPS8fP5FjaAY25rYXV 2xMbPaTictEvAuZdM7vTyBb1ahbCIKJqSHqPVuj0HTHYM7u0mZVxqTHkujDhT2Zas54f sOSpJ2BVPIUHticrpJ3rdqgEWQebF23EI5eNsjWi658D97QSHvfVWd6RnkLbjIzjHNi+ tYXw== X-Gm-Message-State: ACrzQf3UDOAtakW84Ww0xb3/ebpv94Wy7nc+y4TczG36FYNcZd3A3rYC 1YPwfhAWfia1I576XZoIGhiWvNY890U= X-Google-Smtp-Source: AMsMyM4U0r4ewolUkDMWKQNiGDQ3rIec+Mm1abwaJMr47EWCg/ttJNQaGCBk5GLEdlnD8y/TmJ3B8A== X-Received: by 2002:a17:907:a05c:b0:7ad:f563:815d with SMTP id gz28-20020a170907a05c00b007adf563815dmr40053796ejc.371.1668023809724; Wed, 09 Nov 2022 11:56:49 -0800 (PST) Received: from debianHome.localdomain (dynamic-077-010-185-019.77.10.pool.telefonica.de. [77.10.185.19]) by smtp.gmail.com with ESMTPSA id bh1-20020a170906a0c100b00787a6adab7csm6379089ejb.147.2022.11.09.11.56.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Nov 2022 11:56:49 -0800 (PST) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [PATCH 1/3] libselinux: make use of strndup Date: Wed, 9 Nov 2022 20:56:38 +0100 Message-Id: <20221109195640.60484-1-cgzones@googlemail.com> X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Using strndup(3) instead of malloc(3) followed by strncpy(3) simplifies the code and pleases GCC: In file included from /usr/include/string.h:535, from context.c:2: In function ‘strncpy’, inlined from ‘context_new’ at context.c:74:3: /usr/include/x86_64-linux-gnu/bits/string_fortified.h:95:10: error: ‘__builtin_strncpy’ destination unchanged after copying no bytes [-Werror=stringop-truncation] 95 | return __builtin___strncpy_chk (__dest, __src, __len, | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 96 | __glibc_objsize (__dest)); | ~~~~~~~~~~~~~~~~~~~~~~~~~ Signed-off-by: Christian Göttsche --- libselinux/src/context.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/libselinux/src/context.c b/libselinux/src/context.c index 9dddbc5a..a6779a03 100644 --- a/libselinux/src/context.c +++ b/libselinux/src/context.c @@ -68,11 +68,9 @@ context_t context_new(const char *str) for (p = tok; *p; p++) { /* empty */ } } - n->component[i] = (char *)malloc(p - tok + 1); + n->component[i] = strndup(tok, p - tok); if (n->component[i] == 0) goto err; - strncpy(n->component[i], tok, p - tok); - n->component[i][p - tok] = '\0'; tok = *p ? p + 1 : p; } return result; From patchwork Wed Nov 9 19:56:39 2022 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: 13038011 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 EC001C433FE for ; Wed, 9 Nov 2022 19:56:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231703AbiKIT4z (ORCPT ); Wed, 9 Nov 2022 14:56:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231717AbiKIT4x (ORCPT ); Wed, 9 Nov 2022 14:56:53 -0500 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 BBE5D2F00C for ; Wed, 9 Nov 2022 11:56:51 -0800 (PST) Received: by mail-ej1-x633.google.com with SMTP id n12so49703091eja.11 for ; Wed, 09 Nov 2022 11:56:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20210112; 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=fseVezmR5p/YxJ3up3swvrMLFD852DcFyKhRxvReSIU=; b=FuedeF23U+lGECXXERm5gIbkAAZS++YooTcELXfiyu6mfih8V6nlTMatL4tbEL9n2l Hdv/94q1Ytieq34Z7XYzLZ3IRpixhp2RxiBBiKJLmNOnfbGOdxxR2/e07BjRWR00d5Lw hykOZMptcIqkvC0yolnEqphH8LnXz/15zV9WO2LL7yyI3J4/tiX3KTS8zWq3f8h89hxE w5Erk8QxrbAtyMNyY6X86pMZicKKp2+9p2Si1clvWWToUFV/Piy0goS5W/2Mptd+93tX lB8jWFjrpGhgBe5IbxfjGcL64AJbphq91OXt5ufq2uPcretUNAgUVtnARqbbqS5IUH7d LbXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=fseVezmR5p/YxJ3up3swvrMLFD852DcFyKhRxvReSIU=; b=MaKRt47PM+/UJvu/f7393Y4gIS01cvdO2Iv03GIvfoO5v6Ubdu8vwv3CGziVfv/noG mJYFJ0rC2gpgGG7jz7WrfBgfVQcp/PJQ7lwFPPAoLoupGKTAFHvBVx5aw4f/48HfDAb7 BHyEM/m1bs36jGDaYkS9JeqkjsSvF974wmLHKJZf7VMz5iLobobxq2StIPB2nSXsNU/J LP40dfw2s29m4f5I4qJ2L6RBwY8PJTtIkpE603MzN9LJaYWoLmEhOhrJQ5DTM2RsqHFQ F5sGMLaStiCX0rIw/nx26R28feQzB64KVK+TKnGBkqHfZfkbx3qiJWBymQlPA3upt3mR /KHw== X-Gm-Message-State: ACrzQf0RsqJdle83QvDadrfrpcfEoT8tT5EuQ1rwvuH5iM+PantoLJf3 M7YxfbNvmyZPGuZUW9k4BKyzaESoXw8= X-Google-Smtp-Source: AMsMyM5BzZ1GcAbi5JgvizwGxhklyrYcgu2eBmmylt1RMV95B6tMx4T0c+MNj2Ea9pZFSoUNYuMKVQ== X-Received: by 2002:a17:906:55ca:b0:7ad:b822:d2e4 with SMTP id z10-20020a17090655ca00b007adb822d2e4mr1620633ejp.35.1668023810268; Wed, 09 Nov 2022 11:56:50 -0800 (PST) Received: from debianHome.localdomain (dynamic-077-010-185-019.77.10.pool.telefonica.de. [77.10.185.19]) by smtp.gmail.com with ESMTPSA id bh1-20020a170906a0c100b00787a6adab7csm6379089ejb.147.2022.11.09.11.56.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Nov 2022 11:56:49 -0800 (PST) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [PATCH 2/3] libselinux: bail out on path truncations Date: Wed, 9 Nov 2022 20:56:39 +0100 Message-Id: <20221109195640.60484-2-cgzones@googlemail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221109195640.60484-1-cgzones@googlemail.com> References: <20221109195640.60484-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Bail out if computed paths based on user input are being truncated, to avoid wrong files to be opened. Signed-off-by: Christian Göttsche --- libselinux/src/booleans.c | 4 ++-- libselinux/src/get_initial_context.c | 8 ++++++-- libselinux/src/stringrep.c | 15 ++++++++++++--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/libselinux/src/booleans.c b/libselinux/src/booleans.c index ef1f64a0..66c946f9 100644 --- a/libselinux/src/booleans.c +++ b/libselinux/src/booleans.c @@ -164,7 +164,7 @@ static int bool_open(const char *name, int flag) { return -1; ret = snprintf(fname, len, "%s%s%s", selinux_mnt, SELINUX_BOOL_DIR, name); - if (ret < 0) + if (ret < 0 || (size_t)ret >= len) goto out; assert(ret < len); @@ -184,7 +184,7 @@ static int bool_open(const char *name, int flag) { fname = ptr; ret = snprintf(fname, len, "%s%s%s", selinux_mnt, SELINUX_BOOL_DIR, alt_name); - if (ret < 0) + if (ret < 0 || (size_t)ret >= len) goto out; assert(ret < len); diff --git a/libselinux/src/get_initial_context.c b/libselinux/src/get_initial_context.c index 97ae3dcf..87c8adfa 100644 --- a/libselinux/src/get_initial_context.c +++ b/libselinux/src/get_initial_context.c @@ -23,8 +23,12 @@ int security_get_initial_context_raw(const char * name, char ** con) return -1; } - snprintf(path, sizeof path, "%s%s%s", - selinux_mnt, SELINUX_INITCON_DIR, name); + ret = snprintf(path, sizeof path, "%s%s%s", selinux_mnt, SELINUX_INITCON_DIR, name); + if (ret < 0 || (size_t)ret >= sizeof path) { + errno = EOVERFLOW; + return -1; + } + fd = open(path, O_RDONLY | O_CLOEXEC); if (fd < 0) return -1; diff --git a/libselinux/src/stringrep.c b/libselinux/src/stringrep.c index 592410e5..d2237d1c 100644 --- a/libselinux/src/stringrep.c +++ b/libselinux/src/stringrep.c @@ -82,7 +82,10 @@ static struct discover_class_node * discover_class(const char *s) goto err2; /* load up class index */ - snprintf(path, sizeof path, "%s/class/%s/index", selinux_mnt,s); + ret = snprintf(path, sizeof path, "%s/class/%s/index", selinux_mnt,s); + if (ret < 0 || (size_t)ret >= sizeof path) + goto err3; + fd = open(path, O_RDONLY | O_CLOEXEC); if (fd < 0) goto err3; @@ -97,7 +100,10 @@ static struct discover_class_node * discover_class(const char *s) goto err3; /* load up permission indices */ - snprintf(path, sizeof path, "%s/class/%s/perms",selinux_mnt,s); + ret = snprintf(path, sizeof path, "%s/class/%s/perms",selinux_mnt,s); + if (ret < 0 || (size_t)ret >= sizeof path) + goto err3; + dir = opendir(path); if (dir == NULL) goto err3; @@ -107,7 +113,10 @@ static struct discover_class_node * discover_class(const char *s) unsigned int value; struct stat m; - snprintf(path, sizeof path, "%s/class/%s/perms/%s", selinux_mnt,s,dentry->d_name); + ret = snprintf(path, sizeof path, "%s/class/%s/perms/%s", selinux_mnt,s,dentry->d_name); + if (ret < 0 || (size_t)ret >= sizeof path) + goto err4; + fd = open(path, O_RDONLY | O_CLOEXEC); if (fd < 0) goto err4; From patchwork Wed Nov 9 19:56:40 2022 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: 13038012 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 D0CBEC43219 for ; Wed, 9 Nov 2022 19:56:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230128AbiKIT44 (ORCPT ); Wed, 9 Nov 2022 14:56:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231927AbiKIT4x (ORCPT ); Wed, 9 Nov 2022 14:56:53 -0500 Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B8912F010 for ; Wed, 9 Nov 2022 11:56:52 -0800 (PST) Received: by mail-ed1-x52d.google.com with SMTP id u24so28595436edd.13 for ; Wed, 09 Nov 2022 11:56:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20210112; 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=H2/9uHZatBPqiWBVGD6EcLxst6Tri9L6fGhTfHOAu80=; b=A9Ooa8nKx78Y8Tzahh3wy9OrmWp2R7k5aP8q2LsRmOGt12sg5nE9UKBdpIRdKouzb6 OyeZ0aBxFIoO4baL6tWW5YMmnuZ2h7wu6VJfnVUtTL+WYhRtIehbVpF6K3J8OvY8o5Rb jIuEi9m+KEflgom+B/IoeY1IeK9Zwt2KObT5YmvzU9u1I6jABj+j5WbQcI3BBxNelqOh aF0ei+sEvM1ZVzeZhfVbxjTrJCgPCtBfZLlPgfOnqgfiXheZ9zO/0ENmO8cLCgd0lYCd FXIV9nKkaNwf4RL+U/ISMgGB7isp/oOGRKVEi0mCuyRrw3xTBaJA9K8euw/8RqZMMFJ9 aPnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=H2/9uHZatBPqiWBVGD6EcLxst6Tri9L6fGhTfHOAu80=; b=6m9/Nno5vaEb9ORKIiVgEN2v7ajv3CAejnd3vYApoa4ga6Sa1XwW7o2aygLmv/3LlJ M+nhbxgnYKmjZe9F2fVvVD4pi9o5tTdHvnLHc3xQolQlJ4zMhlFGOG0Jd2e7F+QZPaIV r2QBJ+MWzXrqjgVLbsrSArEe2ARrld7mJI9mHlabcHuVCgNQCoXMEft1Hca+kBxHX09l GpOWrVLXUg/5bS81/xkLRsL/b723mtAAiuMqtcbV/ZnF0vCjQ4Gb9nSap2A2mXKWFJuq eraEkYGNpsgKQ+M90Dzw53cOYdgHVRHNYvyPiQrRSdVL0enoiVjXHFCtJIPMJDWh9Kcd hdGg== X-Gm-Message-State: ACrzQf3gEXY/3wvAfUQMw2Rgue5wPvTEunk2WqHXwHO4sZMaPKoEvnbJ JotB3Qz3EH8ZIYikZ8qIHo0V8x5IEGI= X-Google-Smtp-Source: AMsMyM5dxY1LKEkmypHr70/9mUJQuHPnbPYhj09ljcAaZn2iQwNBBdHWAIP/6GwFrtTLBcJr4lBkxA== X-Received: by 2002:aa7:ca54:0:b0:461:ec03:3c04 with SMTP id j20-20020aa7ca54000000b00461ec033c04mr1204419edt.24.1668023810804; Wed, 09 Nov 2022 11:56:50 -0800 (PST) Received: from debianHome.localdomain (dynamic-077-010-185-019.77.10.pool.telefonica.de. [77.10.185.19]) by smtp.gmail.com with ESMTPSA id bh1-20020a170906a0c100b00787a6adab7csm6379089ejb.147.2022.11.09.11.56.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Nov 2022 11:56:50 -0800 (PST) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Subject: [PATCH 3/3] libselinux: filter arguments with path separators Date: Wed, 9 Nov 2022 20:56:40 +0100 Message-Id: <20221109195640.60484-3-cgzones@googlemail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221109195640.60484-1-cgzones@googlemail.com> References: <20221109195640.60484-1-cgzones@googlemail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Boolean names, taken by security_get_boolean_pending(3), security_get_boolean_active(3) and security_set_boolean(3), as well as user names, taken by security_get_initial_context(3), are used in path constructions. Ensure they do not contain path separators to avoid unwanted path traversal. Signed-off-by: Christian Göttsche --- libselinux/src/booleans.c | 7 ++++++- libselinux/src/get_initial_context.c | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/libselinux/src/booleans.c b/libselinux/src/booleans.c index 66c946f9..64248191 100644 --- a/libselinux/src/booleans.c +++ b/libselinux/src/booleans.c @@ -152,7 +152,7 @@ static int bool_open(const char *name, int flag) { int ret; char *ptr; - if (!name) { + if (!name || strchr(name, '/')) { errno = EINVAL; return -1; } @@ -176,6 +176,11 @@ static int bool_open(const char *name, int flag) { if (!alt_name) goto out; + if (strchr(alt_name, '/')) { + errno = EINVAL; + goto out; + } + /* note the 'sizeof' gets us enough room for the '\0' */ len = strlen(alt_name) + strlen(selinux_mnt) + sizeof(SELINUX_BOOL_DIR); ptr = realloc(fname, len); diff --git a/libselinux/src/get_initial_context.c b/libselinux/src/get_initial_context.c index 87c8adfa..0f25ba3f 100644 --- a/libselinux/src/get_initial_context.c +++ b/libselinux/src/get_initial_context.c @@ -23,6 +23,11 @@ int security_get_initial_context_raw(const char * name, char ** con) return -1; } + if (strchr(name, '/')) { + errno = EINVAL; + return -1; + } + ret = snprintf(path, sizeof path, "%s%s%s", selinux_mnt, SELINUX_INITCON_DIR, name); if (ret < 0 || (size_t)ret >= sizeof path) { errno = EOVERFLOW;