From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 C351921E0B7 for ; Wed, 18 Jun 2025 20:54:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750280062; cv=none; b=j1CD+n+BN6m1y0ZP5kudAMahEbFwJsCinYhGgEY0tX4Xevmmx6jJqv5lQIqatbDPibU4Q3JyKWyMUhDMTgBQiI4/fdBdERwAD9NegFkjECnZiNyI07331pAxt8zaqCprpLYOkB8Emj7JDBsDJ03wwa0Ddhu4JsonYpdYX+2PjvQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750280062; c=relaxed/simple; bh=KMvpAWSodoSsrtpouN04b6OOcS21wvgJb7lhmUpwii8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WLKpRgASpye2RIg3f1MkFALYZ9Lrkmi1BP7Og3ly4rwrL1OhGSvjnzczkKwRHMdl4UCzvxktF3UnbkRGnntJOHZT/UgsnArfQ9zRCirZmZs6cmeIzZ33P7zF/8obIQlMrIbvOPZpO27yoqB8J8LdwcWruHvYQujK2Bp8GtC003U= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Maq3YvLd; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Maq3YvLd" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 134A4C4CEED; Wed, 18 Jun 2025 20:54:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750280062; bh=KMvpAWSodoSsrtpouN04b6OOcS21wvgJb7lhmUpwii8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Maq3YvLdKvb+ndYq6XyhQnuEuiKo5l3r/2KOXRWIflNMqum0VeYz4bKr/205zVDEf iL9qjgPvJLaXlD/wwDoYpj62AtOtsR+zG7gxGFHOViGJABgemBraTkNpptwmgCiT/s 2otccj+FY1m4BEIinwbHNdPg3j+rCeK887F7CVnM9FNAgqcN058rnu96wuHybrZSeL j9294Whky8t55o6nTLlo0uPNSGBrRLPxJ0i7QqJLvsrP7O0NDkjquACfRebXbvf2xI eAFQVixlKWNnS5tkodFTb/InhqXU6ANyEsnLdexrdWeUdOagDgJyAzizsI5Oq8yOhb Vj9GMuFfo7YMg== From: Christian Brauner Date: Wed, 18 Jun 2025 22:53:47 +0200 Subject: [PATCH v2 13/16] selftests/pidfd: test extended attribute support Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20250618-work-pidfs-persistent-v2-13-98f3456fd552@kernel.org> References: <20250618-work-pidfs-persistent-v2-0-98f3456fd552@kernel.org> In-Reply-To: <20250618-work-pidfs-persistent-v2-0-98f3456fd552@kernel.org> To: linux-fsdevel@vger.kernel.org Cc: Jann Horn , Josef Bacik , Jeff Layton , Daan De Meyer , Lennart Poettering , Mike Yuan , =?utf-8?q?Zbigniew_J=C4=99drzejewski-Szmek?= , Christian Brauner , Alexander Mikhalitsyn X-Mailer: b4 0.15-dev-262a7 X-Developer-Signature: v=1; a=openpgp-sha256; l=4174; i=brauner@kernel.org; h=from:subject:message-id; bh=KMvpAWSodoSsrtpouN04b6OOcS21wvgJb7lhmUpwii8=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWQEq0f6nTlo7HnxQqH6Vs4HjSsjlPN7NXqefzvrvbs0U PDgjmOPOkpZGMS4GGTFFFkc2k3C5ZbzVGw2ytSAmcPKBDKEgYtTACYi/Yvhn9qRZdFPohlWm5xv s/J//pLXS1ilvy9GUGOVvKHUppbiJQz/rE60xEVtL6/XrPsZ1a8jF1mmOnHKi9qPKRyWXMbKs2O 5AQ== X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Add tests for extended attribute support on pidfds. Signed-off-by: Christian Brauner --- tools/testing/selftests/pidfd/.gitignore | 1 + tools/testing/selftests/pidfd/Makefile | 3 +- tools/testing/selftests/pidfd/pidfd_xattr_test.c | 97 ++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/pidfd/.gitignore b/tools/testing/selftests/pidfd/.gitignore index 0406a065deb4..bc4130506eda 100644 --- a/tools/testing/selftests/pidfd/.gitignore +++ b/tools/testing/selftests/pidfd/.gitignore @@ -10,3 +10,4 @@ pidfd_file_handle_test pidfd_bind_mount pidfd_info_test pidfd_exec_helper +pidfd_xattr_test diff --git a/tools/testing/selftests/pidfd/Makefile b/tools/testing/selftests/pidfd/Makefile index fcbefc0d77f6..c9fd5023ef15 100644 --- a/tools/testing/selftests/pidfd/Makefile +++ b/tools/testing/selftests/pidfd/Makefile @@ -3,7 +3,8 @@ CFLAGS += -g $(KHDR_INCLUDES) -pthread -Wall TEST_GEN_PROGS := pidfd_test pidfd_fdinfo_test pidfd_open_test \ pidfd_poll_test pidfd_wait pidfd_getfd_test pidfd_setns_test \ - pidfd_file_handle_test pidfd_bind_mount pidfd_info_test + pidfd_file_handle_test pidfd_bind_mount pidfd_info_test \ + pidfd_xattr_test TEST_GEN_PROGS_EXTENDED := pidfd_exec_helper diff --git a/tools/testing/selftests/pidfd/pidfd_xattr_test.c b/tools/testing/selftests/pidfd/pidfd_xattr_test.c new file mode 100644 index 000000000000..00d400ac515b --- /dev/null +++ b/tools/testing/selftests/pidfd/pidfd_xattr_test.c @@ -0,0 +1,97 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "pidfd.h" +#include "../kselftest_harness.h" + +FIXTURE(pidfs_xattr) +{ + pid_t child_pid; + int child_pidfd; +}; + +FIXTURE_SETUP(pidfs_xattr) +{ + self->child_pid = create_child(&self->child_pidfd, CLONE_NEWUSER | CLONE_NEWPID); + EXPECT_GE(self->child_pid, 0); + + if (self->child_pid == 0) + _exit(EXIT_SUCCESS); +} + +FIXTURE_TEARDOWN(pidfs_xattr) +{ + sys_waitid(P_PID, self->child_pid, NULL, WEXITED); +} + +TEST_F(pidfs_xattr, set_get_list_xattr_multiple) +{ + int ret, i; + char xattr_name[32]; + char xattr_value[32]; + char buf[32]; + const int num_xattrs = 10; + char list[PATH_MAX] = {}; + + for (i = 0; i < num_xattrs; i++) { + snprintf(xattr_name, sizeof(xattr_name), "trusted.testattr%d", i); + snprintf(xattr_value, sizeof(xattr_value), "testvalue%d", i); + ret = fsetxattr(self->child_pidfd, xattr_name, xattr_value, strlen(xattr_value), 0); + ASSERT_EQ(ret, 0); + } + + for (i = 0; i < num_xattrs; i++) { + snprintf(xattr_name, sizeof(xattr_name), "trusted.testattr%d", i); + snprintf(xattr_value, sizeof(xattr_value), "testvalue%d", i); + memset(buf, 0, sizeof(buf)); + ret = fgetxattr(self->child_pidfd, xattr_name, buf, sizeof(buf)); + ASSERT_EQ(ret, strlen(xattr_value)); + ASSERT_EQ(strcmp(buf, xattr_value), 0); + } + + ret = flistxattr(self->child_pidfd, list, sizeof(list)); + ASSERT_GT(ret, 0); + for (i = 0; i < num_xattrs; i++) { + snprintf(xattr_name, sizeof(xattr_name), "trusted.testattr%d", i); + bool found = false; + for (char *it = list; it < list + ret; it += strlen(it) + 1) { + if (strcmp(it, xattr_name)) + continue; + found = true; + break; + } + ASSERT_TRUE(found); + } + + for (i = 0; i < num_xattrs; i++) { + snprintf(xattr_name, sizeof(xattr_name), "trusted.testattr%d", i); + ret = fremovexattr(self->child_pidfd, xattr_name); + ASSERT_EQ(ret, 0); + + ret = fgetxattr(self->child_pidfd, xattr_name, buf, sizeof(buf)); + ASSERT_EQ(ret, -1); + ASSERT_EQ(errno, ENODATA); + } +} + +TEST_HARNESS_MAIN -- 2.47.2