From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.44 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796304764; b=Tt+371kiO016R2qA6/nXrQrM3iGz6HX2wLZF1FPfeQS/nsAOOG8oDXpFM8Al3SXXcbvk n/vz3FfZZwTIzsg7oHVM/XK5/ptj8psm/MhAv5Iue4x7JH7uCUl6Y+MiWlUnwt1RZuX9S Zxc6QuJcdWdqHNbzIRLjZfBS+AtfW/+WQXn/3YWOYd3fY+ElymcedH5mENxOgiJEzxBOT VM11/SOpLASxFaC0UrZX9hAZf2ZRZFyZGWkPs5+baMEKs+Hb4ijKByGeKikjyXaaGCNbF 2JXhhqGUPeiYXrhMDpc4gzNcn/HdWwyF9bMdg0drldq5Uw692mNGe3nwXQz/ncSqqTA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796304764; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=Y0PK4LYRgYdU0iXAdOiB3SlWKlyBdmjxAe4hR9QGyP0=; b=nRpWIqFUTY9XRIuGgMdrsnZFYTvruSeatNHrv8Vva6B8ST92rXYQABg7twGA/fCE7Vac 352SfhjC3VeQyZOwY1znTEDX/vsrj2r0rZIHmOXk+Q4vMwlPGJsExRV+96qzWEeS6ruRW CPy1ov8esEmSnsm04LRiu9tlOVqoXipMSAZp2IF4TWFzHCIB0ZbFwYwKRR8jWMtLrslOK UoMALVdTuf6lGMT008rmi49hSmm4xmkEcpPvtmnZAXnrNDy8eRDEH5ODdmL3aOzVeE2z1 1BxNYGn+f/SCyvgUIhMapgzvwUD6AwJlKT/v6CJZOjGqaZXIiPWCsCoA8HvpL8ZTuMw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=lANFKN1a; arc=none smtp.remote-ip=209.85.128.44 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 913FE606A684 for ; Thu, 3 Dec 2026 13:32:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 913FE606A684 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=lANFKN1a Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-4b79502c27aso145305965e9.1 for ; Thu, 03 Dec 2026 05:32:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796304762; x=1796909562; darn=vger.kernel.org; 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:content-type; bh=Y0PK4LYRgYdU0iXAdOiB3SlWKlyBdmjxAe4hR9QGyP0=; b=lANFKN1ay8v2Vj3Un/L1yeDXbIyc7r4JF+2CcPHxfjLmghdaaCmYMSzeae8vNvBtx+ nfgMpra5TTqY8nSWku/vnONSBhA/s/H41UOti/h8qA3eTkSCdWxDWC6WIeU8JSSAavOU jhPvQphBLEFr/Dphijuo3LTMIkJtfPLI/n3z+vhw4mrd/VKP0NNinOob8h4ofZL7TYGb sYDrDbDYrM/dLHsjC0UKcFKF56kcbc7dl7PnkcALN/AmkVOVqrxGolcAZGmZcIwOGSic C1Ki1ytSwxL+UP6aGroZefUiPlRLf28VrHYB57SiBS+yiA0GWK9LWza/kfUW3qIM3Ab6 Ar/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796304762; x=1796909562; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=Y0PK4LYRgYdU0iXAdOiB3SlWKlyBdmjxAe4hR9QGyP0=; b=LQz8EIN9lVpIsYlJvOlynr9NJLHr+KcbGFRSJO0riMIv9wna5NLLamaadBXI46ugDk 1dYpGP6uFXvQGCN+fNZW2zJN7GwSEkTirPnxKRniUmPYEEunNBKS9kV/hvePBGfX1+uZ 0UuJQhQZktjlP4pg6uCBzJyV7wVEYIgDvbOif5jhY82M4fC2ZOfwU7NsoMndSdC7LMza 938iTOUJ2pIL8FSZEBjSA6u0sdCzAeZZsuZ8diEu2A22b2fMeMFuRHAQHozkhxY7YIda iibhAp1xgnzzU8Fk7sL28R2cMhXlsZpDm/YlLhyLJeFNHEy3jXnXpW7hC+AbyLWCNgsU g1UQ== X-Forwarded-Encrypted: i=1; AL+hErHYWhkVZ92BhsAh3PoapxHYvodnO2B1fg8pCQjX4bxoj9H9LI6c669ufoG07eWEsFqiyWMJ8iDMej4uHy4=@vger.kernel.org X-Gm-Message-State: APuU4Z9m6KmXUxMo1616lwldtJ9OJt0MZ5KXfzV2/aeQAH9I8Mvl3vMl 3AujlHk8yAauvNyuipHyOoyojM7GKYZJddNK5nFs12I1wH32VKnMRhuu X-Gm-Gg: AZFdTZfkJABz/HvecWeFqA27CywrYHry4huhGwcZ6JFqTVBaJiV1kWU8b8Wt8kSx1RR xjW+7775T0oTeEoLkR7JIJETe15ZAT/t01kWvuJfgsvxegL1LWhkVJiSiPVBMniBNraivb6AVXo yEs4BTl1LvLR8LLVV/x4qXN6SI9eJ0LS8s1EKRp9HSEhti7kupEjRDBX3r5gw/2x8MkKQxFOHmI 62WvNtqWxStLsirf6Fw9l8BHL1WYcg8ymUSjXprMC2Bea4617sjCdZImxiliYKnmdmwoOjC06w1 yzAVFOGVkeJhKcjtmt0OP2gLjO63iu+rUnBh9VLZyfEWFnLGH52c7t4xoo8VbRZjMKPESAOKHqw S67iVKZsthhSfoO1wfW1HiyRv7DnbHxlnjdyTO3kXCS4W8mEB5HoERH7wtpjA7DRHh8lwCuO9nF WZfxRxDZ5nGAyQlJPZs4pD/qMl7kR7XTpEk5tPrtSQdpg0bl5OLkjeu39oM/I0ewMyL1Fv//iwE iYeIba4OunP6LK5QXZiYvTPfBU+zkqCqwj/3o+IwwHlwWMt7kEyuRfxl/Zuny+vhUogrlCMzsl/ H6tHNRRPUbAdYTVvfVtGoFVC5opiyoAjt3VO0LMliaMflTI= X-Received: by 2002:a05:600c:638f:b0:4bb:d58e:eaa8 with SMTP id 5b1f17b1804b1-4bbd58eefe7mr95404135e9.1.1796304761978; Thu, 03 Dec 2026 05:32:41 -0800 (PST) Received: from localhost.localdomain ([2a04:ee41:4:b2de:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bbe00792besm74326105e9.4.2026.12.03.05.32.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 05:32:41 -0800 (PST) From: Anton Protopopov To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , KP Singh , Matt Bobrowski , Alexander Viro , Christian Brauner , Jan Kara , bpf@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Anton Protopopov Subject: [PATCH v1 bpf-next 3/3] selftests/bpf: Test xattr kfuncs on the new sleepable inode hooks Date: Thu, 3 Dec 2026 13:44:07 +0000 Message-ID: <20261203134411.1092433-4-a.s.protopopov@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261203134411.1092433-1-a.s.protopopov@gmail.com> References: <20261203134411.1092433-1-a.s.protopopov@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add the inode_xattr subtest to fs_kfuncs. Programs on inode_mkdir and inode_link read an xattr of the parent directory with the new bpf_get_inode_xattr(). Programs on inode_set_acl, inode_remove_acl and inode_file_setattr set and remove an xattr of the file. The verifier must replace the setters with the _locked variants there, otherwise these calls deadlock. Add to verifier_vfs_accept a program on inode_copy_up which reads an xattr, and to verifier_vfs_reject two programs which try to set and remove one there. Signed-off-by: Anton Protopopov --- tools/testing/selftests/bpf/bpf_kfuncs.h | 11 ++ .../selftests/bpf/prog_tests/fs_kfuncs.c | 107 ++++++++++++++++++ .../selftests/bpf/progs/test_inode_xattr.c | 89 +++++++++++++++ .../selftests/bpf/progs/verifier_vfs_accept.c | 13 +++ .../selftests/bpf/progs/verifier_vfs_reject.c | 22 ++++ 5 files changed, 242 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/test_inode_xattr.c diff --git a/tools/testing/selftests/bpf/bpf_kfuncs.h b/tools/testing/selftests/bpf/bpf_kfuncs.h index 1b408fe9e0fb..71601eafae6f 100644 --- a/tools/testing/selftests/bpf/bpf_kfuncs.h +++ b/tools/testing/selftests/bpf/bpf_kfuncs.h @@ -91,6 +91,17 @@ extern int bpf_set_dentry_xattr(struct dentry *dentry, const char *name__str, const struct bpf_dynptr *value_p, int flags) __ksym __weak; extern int bpf_remove_dentry_xattr(struct dentry *dentry, const char *name__str) __ksym __weak; +/* + * Description + * Returns xattr of an inode, read through a dentry of the inode + * Returns + * Length of the xattr value, -ENOENT if the inode has no dentry or + * another negative error code + */ +struct inode; +extern int bpf_get_inode_xattr(struct inode *inode, const char *name__str, + struct bpf_dynptr *value_p) __ksym __weak; + /* * Description * Attach a xattr to an inode that is being created, from a program on the diff --git a/tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c b/tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c index 43a26ec69a8e..ad467c766380 100644 --- a/tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +++ b/tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c @@ -1,17 +1,25 @@ // SPDX-License-Identifier: GPL-2.0 /* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */ +#include #include +#include +#include #include #include +#include #include +#include +#include #include #include #include "test_get_xattr.skel.h" #include "test_set_remove_xattr.skel.h" +#include "test_inode_xattr.skel.h" #include "test_fsverity.skel.h" static const char testfile[] = "/tmp/test_progs_fs_kfuncs"; +static const char testdir[] = "/tmp/test_progs_fs_kfuncs_dir"; static void test_get_xattr(const char *name, const char *value, bool allow_access) { @@ -194,6 +202,102 @@ static void test_set_remove_xattr(void) remove(testfile); } +static bool has_bar(struct test_inode_xattr *skel, const char *path) +{ + char value_out[32]; + + return getxattr(path, skel->rodata->xattr_bar, value_out, sizeof(value_out)) == + sizeof(skel->data->value_bar) && + !memcmp(value_out, skel->data->value_bar, sizeof(skel->data->value_bar)); +} + +static void test_inode_xattr(void) +{ + /* An ACL with a named user, so that it is not folded into the mode */ + const struct { + struct posix_acl_xattr_header header; + struct posix_acl_xattr_entry entries[5]; + } acl = { + .header = { htole32(POSIX_ACL_XATTR_VERSION) }, + .entries = { + { htole16(ACL_USER_OBJ), htole16(ACL_READ | ACL_WRITE), + htole32(ACL_UNDEFINED_ID) }, + { htole16(ACL_USER), htole16(ACL_READ), htole32(1) }, + { htole16(ACL_GROUP_OBJ), htole16(ACL_READ), htole32(ACL_UNDEFINED_ID) }, + { htole16(ACL_MASK), htole16(ACL_READ), htole32(ACL_UNDEFINED_ID) }, + { htole16(ACL_OTHER), htole16(ACL_READ), htole32(ACL_UNDEFINED_ID) }, + }, + }; + char child[64], file[64], link_path[64]; + struct test_inode_xattr *skel = NULL; + int fd, flags, err; + + snprintf(child, sizeof(child), "%s/child", testdir); + snprintf(file, sizeof(file), "%s/file", testdir); + snprintf(link_path, sizeof(link_path), "%s/link", testdir); + + if (!ASSERT_OK(mkdir(testdir, 0755), "mkdir testdir")) + return; + fd = open(file, O_CREAT | O_RDONLY, 0644); + if (!ASSERT_GE(fd, 0, "create_file")) + goto out; + + skel = test_inode_xattr__open_and_load(); + if (!ASSERT_OK_PTR(skel, "test_inode_xattr__open_and_load")) + goto out; + + err = setxattr(testdir, skel->rodata->xattr_foo, value_foo, sizeof(value_foo), 0); + if (err && errno == EOPNOTSUPP) { + printf("%s:SKIP:local fs doesn't support xattr (%d)\n" + "To run this test, make sure /tmp filesystem supports xattr.\n", + __func__, errno); + test__skip(); + goto out; + } + if (!ASSERT_OK(err, "setxattr testdir")) + goto out; + + skel->bss->monitored_pid = getpid(); + if (!ASSERT_OK(test_inode_xattr__attach(skel), "test_inode_xattr__attach")) + goto out; + + /* inode_mkdir and inode_link read the label of the directory */ + ASSERT_OK(mkdir(child, 0755), "mkdir child"); + ASSERT_EQ(skel->data->mkdir_ret, sizeof(value_foo), "mkdir_ret"); + ASSERT_STREQ(skel->bss->mkdir_value, value_foo, "mkdir_value"); + + ASSERT_OK(link(file, link_path), "link"); + ASSERT_EQ(skel->data->link_ret, sizeof(value_foo), "link_ret"); + ASSERT_STREQ(skel->bss->link_value, value_foo, "link_value"); + + /* inode_set_acl, inode_remove_acl and inode_file_setattr change xattrs */ + err = setxattr(file, "system.posix_acl_access", &acl, sizeof(acl), 0); + if (ASSERT_OK(err, "set acl")) { + ASSERT_EQ(skel->data->set_acl_ret, 0, "set_acl_ret"); + ASSERT_TRUE(has_bar(skel, file), "bar after set acl"); + + ASSERT_OK(removexattr(file, "system.posix_acl_access"), "remove acl"); + ASSERT_EQ(skel->data->remove_acl_ret, 0, "remove_acl_ret"); + err = getxattr(file, skel->rodata->xattr_bar, NULL, 0); + ASSERT_TRUE(err == -1 && errno == ENODATA, "bar after remove acl"); + } + + if (ASSERT_OK(ioctl(fd, FS_IOC_GETFLAGS, &flags), "FS_IOC_GETFLAGS") && + ASSERT_OK(ioctl(fd, FS_IOC_SETFLAGS, &flags), "FS_IOC_SETFLAGS")) { + ASSERT_EQ(skel->data->file_setattr_ret, 0, "file_setattr_ret"); + ASSERT_TRUE(has_bar(skel, file), "bar after file setattr"); + } + +out: + if (fd >= 0) + close(fd); + test_inode_xattr__destroy(skel); + remove(link_path); + remove(file); + rmdir(child); + rmdir(testdir); +} + #ifndef SHA256_DIGEST_SIZE #define SHA256_DIGEST_SIZE 32 #endif @@ -286,6 +390,9 @@ void test_fs_kfuncs(void) if (test__start_subtest("set_remove_xattr")) test_set_remove_xattr(); + if (test__start_subtest("inode_xattr")) + test_inode_xattr(); + if (test__start_subtest("fsverity")) test_fsverity(); } diff --git a/tools/testing/selftests/bpf/progs/test_inode_xattr.c b/tools/testing/selftests/bpf/progs/test_inode_xattr.c new file mode 100644 index 000000000000..f82b9c0ad067 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_inode_xattr.c @@ -0,0 +1,89 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include +#include +#include "bpf_kfuncs.h" +#include "bpf_misc.h" + +char _license[] SEC("license") = "GPL"; + +__u32 monitored_pid; + +const char xattr_foo[] = "security.bpf.foo"; +const char xattr_bar[] = "security.bpf.bar"; +char value_bar[] = "world"; + +char mkdir_value[32]; +char link_value[32]; +int mkdir_ret = -1; +int link_ret = -1; +int set_acl_ret = -1; +int remove_acl_ret = -1; +int file_setattr_ret = -1; + +static bool monitored(void) +{ + return bpf_get_current_pid_tgid() >> 32 == monitored_pid; +} + +static int read_dir_foo(struct inode *dir, char *value, __u32 size) +{ + struct bpf_dynptr value_ptr; + + bpf_dynptr_from_mem(value, size, 0, &value_ptr); + return bpf_get_inode_xattr(dir, xattr_foo, &value_ptr); +} + +static int set_bar(struct dentry *dentry) +{ + struct bpf_dynptr value_ptr; + + bpf_dynptr_from_mem(value_bar, sizeof(value_bar), 0, &value_ptr); + return bpf_set_dentry_xattr(dentry, xattr_bar, &value_ptr, 0); +} + +SEC("lsm.s/inode_mkdir") +int BPF_PROG(test_inode_mkdir, struct mnt_idmap *idmap, struct inode *dir, + struct dentry *dentry, umode_t mode) +{ + if (monitored()) + mkdir_ret = read_dir_foo(dir, mkdir_value, sizeof(mkdir_value)); + return 0; +} + +SEC("lsm.s/inode_link") +int BPF_PROG(test_inode_link, struct mnt_idmap *idmap, struct dentry *old_dentry, + struct inode *dir, struct dentry *new_dentry) +{ + if (monitored()) + link_ret = read_dir_foo(dir, link_value, sizeof(link_value)); + return 0; +} + +/* The verifier calls the _locked setters on these three hooks. */ +SEC("lsm.s/inode_set_acl") +int BPF_PROG(test_inode_set_acl, struct mnt_idmap *idmap, struct dentry *dentry, + const char *acl_name, struct posix_acl *kacl) +{ + if (monitored()) + set_acl_ret = set_bar(dentry); + return 0; +} + +SEC("lsm.s/inode_remove_acl") +int BPF_PROG(test_inode_remove_acl, struct mnt_idmap *idmap, struct dentry *dentry, + const char *acl_name) +{ + if (monitored()) + remove_acl_ret = bpf_remove_dentry_xattr(dentry, xattr_bar); + return 0; +} + +SEC("lsm.s/inode_file_setattr") +int BPF_PROG(test_inode_file_setattr, struct dentry *dentry, struct file_kattr *fa) +{ + if (monitored()) + file_setattr_ret = set_bar(dentry); + return 0; +} diff --git a/tools/testing/selftests/bpf/progs/verifier_vfs_accept.c b/tools/testing/selftests/bpf/progs/verifier_vfs_accept.c index 25f37a3dd7cf..38edcf1b34e8 100644 --- a/tools/testing/selftests/bpf/progs/verifier_vfs_accept.c +++ b/tools/testing/selftests/bpf/progs/verifier_vfs_accept.c @@ -8,8 +8,10 @@ #include "bpf_misc.h" #include "bpf_experimental.h" +#include "bpf_kfuncs.h" static char buf[64]; +static const char xattr_name[] = "security.bpf.foo"; SEC("lsm.s/file_open") __success @@ -117,4 +119,15 @@ int BPF_PROG(inode_rename, struct inode *old_dir, struct dentry *old_dentry, return 0; } +SEC("lsm.s/inode_copy_up") +__success +int BPF_PROG(inode_copy_up_get_xattr, struct dentry *src, struct cred **new) +{ + struct bpf_dynptr value_ptr; + + bpf_dynptr_from_mem(buf, sizeof(buf), 0, &value_ptr); + bpf_get_dentry_xattr(src, xattr_name, &value_ptr); + return 0; +} + char _license[] SEC("license") = "GPL"; diff --git a/tools/testing/selftests/bpf/progs/verifier_vfs_reject.c b/tools/testing/selftests/bpf/progs/verifier_vfs_reject.c index ed2bd2faebf3..96ad9d895af9 100644 --- a/tools/testing/selftests/bpf/progs/verifier_vfs_reject.c +++ b/tools/testing/selftests/bpf/progs/verifier_vfs_reject.c @@ -9,8 +9,10 @@ #include "bpf_misc.h" #include "bpf_experimental.h" +#include "bpf_kfuncs.h" static char buf[PATH_MAX]; +static const char xattr_name[] = "security.bpf.foo"; SEC("lsm.s/file_open") __failure __msg("invalid mem access 'scalar'") @@ -192,4 +194,24 @@ int BPF_PROG(inode_rename, struct inode *old_dir, struct dentry *old_dentry, return -EACCES; return 0; } + +SEC("lsm.s/inode_copy_up") +__failure __msg("calling kernel function bpf_set_dentry_xattr is not allowed") +int BPF_PROG(inode_copy_up_set_xattr, struct dentry *src, struct cred **new) +{ + struct bpf_dynptr value_ptr; + + bpf_dynptr_from_mem(buf, 8, 0, &value_ptr); + bpf_set_dentry_xattr(src, xattr_name, &value_ptr, 0); + return 0; +} + +SEC("lsm.s/inode_copy_up") +__failure __msg("calling kernel function bpf_remove_dentry_xattr is not allowed") +int BPF_PROG(inode_copy_up_remove_xattr, struct dentry *src, struct cred **new) +{ + bpf_remove_dentry_xattr(src, xattr_name); + return 0; +} + char _license[] SEC("license") = "GPL"; -- 2.43.0