From patchwork Tue Feb 16 14:14:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Lautrbach X-Patchwork-Id: 12090051 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DDC4DC433E6 for ; Tue, 16 Feb 2021 14:16:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9851964D5D for ; Tue, 16 Feb 2021 14:16:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229923AbhBPOQZ (ORCPT ); Tue, 16 Feb 2021 09:16:25 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:49113 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229894AbhBPOQY (ORCPT ); Tue, 16 Feb 2021 09:16:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1613484898; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=QVVQpS1h77s1HChoxuZWVqN+q395+dpiUimupvpLqSQ=; b=XZUGvF+HxGWgnalQKnIWvXTHdzbZKsfkWWwuqvP7QXpsg7r1eSFvXyikss8KYyZSoW3uBt /PvppRZiZM4/ntdUQU0d7desDFFpWxpJu9RYcE/fGDEWDCtss3er2eWk+AF+KbUdelRKrn qblZPufDV/2NkG/CLCr5tgWdT/lbt/w= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-356-2fYGkjdyOAOiBA5zxrMfGA-1; Tue, 16 Feb 2021 09:14:55 -0500 X-MC-Unique: 2fYGkjdyOAOiBA5zxrMfGA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 37CD6804023 for ; Tue, 16 Feb 2021 14:14:53 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.40.192.169]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0903D60C62; Tue, 16 Feb 2021 14:14:51 +0000 (UTC) From: Petr Lautrbach To: selinux@vger.kernel.org Cc: Petr Lautrbach Subject: [PATCH 1/2] libselinux: fix segfault in add_xattr_entry() Date: Tue, 16 Feb 2021 15:14:45 +0100 Message-Id: <20210216141446.171306-1-plautrba@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org When selabel_get_digests_all_partial_matches(), resp get_digests_all_partial_matches() doesn't find a match, calculated_digest is not initialized and followup memcmp() could segfault. Given that calculated_digest and xattr_digest are already compared in get_digests_all_partial_matches() and the function returns true or false based on this comparison, it's not neccessary to compare these values again. Fixes: # restorecon_xattr -d -v tmp specfiles SHA1 digest: afc752f47d489f3e82ac1da8fd247a2e1a6af5f8 calculated using the following specfile(s): /etc/selinux/targeted/contexts/files/file_contexts.subs_dist /etc/selinux/targeted/contexts/files/file_contexts.subs /etc/selinux/targeted/contexts/files/file_contexts.bin /etc/selinux/targeted/contexts/files/file_contexts.homedirs.bin /etc/selinux/targeted/contexts/files/file_contexts.local.bin Segmentation fault (core dumped) Signed-off-by: Petr Lautrbach Acked-by: Nicolas Iooss --- libselinux/src/selinux_restorecon.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libselinux/src/selinux_restorecon.c b/libselinux/src/selinux_restorecon.c index 6993be6fda17..4bca29b9de78 100644 --- a/libselinux/src/selinux_restorecon.c +++ b/libselinux/src/selinux_restorecon.c @@ -297,6 +297,7 @@ static int add_xattr_entry(const char *directory, bool delete_nonmatch, char *sha1_buf = NULL; size_t i, digest_len = 0; int rc, digest_result; + bool match; struct dir_xattr *new_entry; uint8_t *xattr_digest = NULL; uint8_t *calculated_digest = NULL; @@ -306,7 +307,7 @@ static int add_xattr_entry(const char *directory, bool delete_nonmatch, return -1; } - selabel_get_digests_all_partial_matches(fc_sehandle, directory, + match = selabel_get_digests_all_partial_matches(fc_sehandle, directory, &calculated_digest, &xattr_digest, &digest_len); @@ -326,11 +327,10 @@ static int add_xattr_entry(const char *directory, bool delete_nonmatch, for (i = 0; i < digest_len; i++) sprintf((&sha1_buf[i * 2]), "%02x", xattr_digest[i]); - rc = memcmp(calculated_digest, xattr_digest, digest_len); - digest_result = rc ? NOMATCH : MATCH; + digest_result = match ? MATCH : NOMATCH; - if ((delete_nonmatch && rc != 0) || delete_all) { - digest_result = rc ? DELETED_NOMATCH : DELETED_MATCH; + if ((delete_nonmatch && ! match) || delete_all) { + digest_result = match ? DELETED_MATCH : DELETED_NOMATCH; rc = removexattr(directory, RESTORECON_PARTIAL_MATCH_DIGEST); if (rc) { selinux_log(SELINUX_ERROR, From patchwork Tue Feb 16 14:14:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Lautrbach X-Patchwork-Id: 12090049 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 944DAC433E0 for ; Tue, 16 Feb 2021 14:16:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4EEF864DEC for ; Tue, 16 Feb 2021 14:16:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229910AbhBPOQY (ORCPT ); Tue, 16 Feb 2021 09:16:24 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:24789 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229713AbhBPOQX (ORCPT ); Tue, 16 Feb 2021 09:16:23 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1613484896; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=aVqFQLgW7RUhN9jM6itaY3qhtu9cMr5shvzdP6THvno=; b=USYML25Oi8/JEdvHf7DxYNi0NH3yj6aKrFCh7Hfm2ADeI8MIzcpvN4Tqk5NLFuQ1hikjhq clNpxnWlS0NdWL/runbi0KZE8eNbKtj1N84zH0o9VBWsoGfXJnRtTb7ubEyHL75wd5O2ke TADBoHQv+bbYE+epnK8NjVURuGzc21w= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-115-lXjFvK0SO_WmE1G-70Z-pg-1; Tue, 16 Feb 2021 09:14:55 -0500 X-MC-Unique: lXjFvK0SO_WmE1G-70Z-pg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 73EF180197A for ; Tue, 16 Feb 2021 14:14:54 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.40.192.169]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9852560C94; Tue, 16 Feb 2021 14:14:53 +0000 (UTC) From: Petr Lautrbach To: selinux@vger.kernel.org Cc: Petr Lautrbach Subject: [PATCH 2/2] policycoreutils: Resolve path in restorecon_xattr Date: Tue, 16 Feb 2021 15:14:46 +0100 Message-Id: <20210216141446.171306-2-plautrba@redhat.com> In-Reply-To: <20210216141446.171306-1-plautrba@redhat.com> References: <20210216141446.171306-1-plautrba@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Resolve pathname before selinux_restorecon_xattr() to prevent problems with 'No Match' when relative path is used. Fixes: # restorecon_xattr -v tmp ... tmp Digest: f9cd2da7141068bd2c08bc02fa471db63ac7d44c No Match # restorecon_xattr -v `pwd`/tmp ... /root/tmp Digest: f9cd2da7141068bd2c08bc02fa471db63ac7d44c Match Signed-off-by: Petr Lautrbach --- policycoreutils/setfiles/restorecon_xattr.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/policycoreutils/setfiles/restorecon_xattr.c b/policycoreutils/setfiles/restorecon_xattr.c index 59b1f748b8c5..56f6f9d0e043 100644 --- a/policycoreutils/setfiles/restorecon_xattr.c +++ b/policycoreutils/setfiles/restorecon_xattr.c @@ -38,7 +38,7 @@ int main(int argc, char **argv) unsigned int xattr_flags = 0, delete_digest = 0, recurse = 0; unsigned int delete_all_digests = 0, ignore_mounts = 0; bool display_digest = false; - char *sha1_buf, **specfiles, *fc_file = NULL; + char *sha1_buf, **specfiles, *fc_file = NULL, *pathname = NULL; unsigned char *fc_digest = NULL; size_t i, fc_digest_len = 0, num_specfiles; @@ -163,7 +163,16 @@ int main(int argc, char **argv) xattr_flags = delete_digest | delete_all_digests | ignore_mounts | recurse; - if (selinux_restorecon_xattr(argv[optind], xattr_flags, &xattr_list)) { + pathname = realpath(argv[optind], NULL); + if (!pathname) { + fprintf(stderr, + "restorecon_xattr: realpath(%s) failed: %s\n", + argv[optind], strerror(errno)); + rc = -1; + goto out; + } + + if (selinux_restorecon_xattr(pathname, xattr_flags, &xattr_list)) { fprintf(stderr, "Error selinux_restorecon_xattr: %s\n", strerror(errno)); @@ -215,6 +224,7 @@ int main(int argc, char **argv) rc = 0; out: + free(pathname); selabel_close(hnd); restore_finish(); return rc;