Skip to contentSkip to conversation

Add listxattr04 test reproducer

PATCH Superseded Andrea Cervesato 3 participants 8 messages Jul 9, 2025 – Jul 16, 2025 Library code

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover Add listxattr04 test reproducer

Reproducer for https://lore.kernel.org/linux-fsdevel/[email protected]/T/

---
Changes in v3:
- add tst_selinux_enabled() utility
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- only check if SELinux is up and running
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Andrea Cervesato (2):
core: add tst_selinux_enabled() utility
Add listxattr04 reproducer

 include/tst_security.h                            |   1 +
 lib/tst_security.c                                |  15 ++-
 testcases/kernel/syscalls/listxattr/.gitignore    |   1 +
 testcases/kernel/syscalls/listxattr/Makefile      |   2 +
 testcases/kernel/syscalls/listxattr/listxattr04.c | 108 ++++++++++++++++++++++
 5 files changed, 126 insertions(+), 1 deletion(-)

---
base-commit: d071de02e800d8d2cf823af9bdf263aff5945321
change-id: 20250702-xattr_bug_repr-5873b84792fb

Best regards,

Signature · 5 lines
Andrea Cervesato <[email protected]>


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH v3 1/2 core: add tst_selinux_enabled() utility

From: Andrea Cervesato <[email protected]>

Add tst_selinux_enabled() utility in tst_security.h in order to verify if SELinux is currently up and running in the system.

---
 include/tst_security.h |  1 +
 lib/tst_security.c     | 15 ++++++++++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)
include/tst_security.h +1 -0
index 5d91f8a98f104b0cafaaf2046bc0ceec06870606..cb5490a896f027245064abebb9d7c36270fd2e8a 100644
@@ -14,5 +14,6 @@ int tst_fips_enabled(void);
 int tst_lockdown_enabled(void);
 int tst_secureboot_enabled(void);
 int tst_selinux_enforcing(void);
+int tst_selinux_enabled(void);
 
 #endif /* TST_SECURITY_H__ */
lib/tst_security.c +14 -1
index 7d929fafe729058f55b921bf5cf7806b253496e0..c07f783178fa81da84113d4582230a391ab35b43 100644
@@ -7,7 +7,8 @@
 
 #define PATH_FIPS	"/proc/sys/crypto/fips_enabled"
 #define PATH_LOCKDOWN	"/sys/kernel/security/lockdown"
-#define SELINUX_STATUS_PATH "/sys/fs/selinux/enforce"
+#define SELINUX_PATH "/sys/fs/selinux"
+#define SELINUX_STATUS_PATH (SELINUX_PATH "/enforce")
 
 #if defined(__powerpc64__) || defined(__ppc64__)
 # define SECUREBOOT_VAR "/proc/device-tree/ibm,secure-boot"
@@ -102,6 +103,18 @@ int tst_secureboot_enabled(void)
 	return data[VAR_DATA_SIZE - 1];
 }
 
+int tst_selinux_enabled(void)
+{
+	int res = 0;
+
+	if (access(SELINUX_PATH, F_OK) == 0 && !tst_dir_is_empty(SELINUX_PATH, 0))
+		res = 1;
+
+	tst_res(TINFO, "SELinux enabled: %s", res ? "yes" : "no");
+
+	return res;
+}
+
 int tst_selinux_enforcing(void)
 {
 	int res = 0;
Signature · 5 lines
2.50.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Re: [LTP] [PATCH v3 1/2] core: add tst_selinux_enabled() utility

Hi!

lib/tst_security.c int tst_secureboot_enabled(void) · line 111

Quoted 6 lines
lib/tst_security.c +6 -0
+int tst_selinux_enabled(void)
+{
+	int res = 0;
+
+	if (access(SELINUX_PATH, F_OK) == 0 && !tst_dir_is_empty(SELINUX_PATH, 0))
+		res = 1;

Maybe we we can do tst_is_mounted(SELINUX_PATH) here instead. At least that seems to be what is_selinux_enabled() seems to be doing.

Quoted 15 lines
lib/tst_security.c +5 -0
+	tst_res(TINFO, "SELinux enabled: %s", res ? "yes" : "no");
+
+	return res;
+}
+
 int tst_selinux_enforcing(void)
 {
 	int res = 0;
Signature · 6 lines
-- 
2.50.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

PATCH v3 2/2 Add listxattr04 reproducer

In reply to Andrea Cervesato

From: Andrea Cervesato <[email protected]>

Test reproducer for a bug introduced in 8b0ba61df5a1 ("fs/xattr.c: fix simple_xattr_list to always include security.* xattrs").

Bug can be reproduced when SELinux and ACL are activated on inodes as following:

    $ touch testfile
    $ setfacl -m u:myuser:rwx testfile
    $ getfattr -dm. /tmp/testfile
    Segmentation fault (core dumped)

The reason why this happens is that simple_xattr_list() always includes security.* xattrs without resetting error flag after security_inode_listsecurity(). This results into an incorrect length of the returned xattr name if POSIX ACL is also applied on the inode.

---
 testcases/kernel/syscalls/listxattr/.gitignore    |   1 +
 testcases/kernel/syscalls/listxattr/Makefile      |   2 +
 testcases/kernel/syscalls/listxattr/listxattr04.c | 108 ++++++++++++++++++++++
 3 files changed, 111 insertions(+)
testcases/kernel/syscalls/listxattr/.gitignore +1 -0
index be0675a6df0080d176d53d70194442bbc9ed376c..0d672b6ea5eec03aab37ee89316c56e24356c1d9 100644
@@ -1,3 +1,4 @@
 /listxattr01
 /listxattr02
 /listxattr03
+/listxattr04
testcases/kernel/syscalls/listxattr/Makefile +2 -0
index c2f84b1590fc24a7a98f890ea7706771d944aa79..e96bb3fa4c2c6b14b8d2bc8fd4c475e4789d72fe 100644
@@ -6,4 +6,6 @@ top_srcdir		?= ../../../..
 
 include $(top_srcdir)/include/mk/testcases.mk
 
+listxattr04: LDLIBS	+= $(ACL_LIBS)
+
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/listxattr/listxattr04.c new file +108 -0

108 lines not on this page · raw message

Signature · 5 lines
2.50.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

Re: [LTP] [PATCH v3 2/2] Add listxattr04 reproducer

Quoted 112 lines · Andrea Cervesato

From: Andrea Cervesato <[email protected]>

Test reproducer for a bug introduced in 8b0ba61df5a1 ("fs/xattr.c: fix simple_xattr_list to always include security.* xattrs").

Bug can be reproduced when SELinux and ACL are activated on inodes as following:

    $ touch testfile
    $ setfacl -m u:myuser:rwx testfile
    $ getfattr -dm. /tmp/testfile
    Segmentation fault (core dumped)

The reason why this happens is that simple_xattr_list() always includes security.* xattrs without resetting error flag after security_inode_listsecurity(). This results into an incorrect length of the returned xattr name if POSIX ACL is also applied on the inode.

---
 testcases/kernel/syscalls/listxattr/.gitignore    |   1 +
 testcases/kernel/syscalls/listxattr/Makefile      |   2 +
 testcases/kernel/syscalls/listxattr/listxattr04.c | 108 ++++++++++++++++++++++
 3 files changed, 111 insertions(+)
testcases/kernel/syscalls/listxattr/.gitignore +1 -0
index be0675a6df0080d176d53d70194442bbc9ed376c..0d672b6ea5eec03aab37ee89316c56e24356c1d9 100644
@@ -1,3 +1,4 @@
 /listxattr01
 /listxattr02
 /listxattr03
+/listxattr04
testcases/kernel/syscalls/listxattr/Makefile +2 -0
index c2f84b1590fc24a7a98f890ea7706771d944aa79..e96bb3fa4c2c6b14b8d2bc8fd4c475e4789d72fe 100644
@@ -6,4 +6,6 @@ top_srcdir		?= ../../../..
 
 include $(top_srcdir)/include/mk/testcases.mk
 
+listxattr04: LDLIBS	+= $(ACL_LIBS)
+
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/listxattr/listxattr04.c new file +61 -0
new file mode 100644
index 0000000000000000000000000000000000000000..b7357d6c2a8dda2ace72081bee9f45d7f84442d9
@@ -0,0 +1,108 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2025 Andrea Cervesato <[email protected]>
+ */
+
+/*\
+ * Test reproducer for a bug introduced in 8b0ba61df5a1 ("fs/xattr.c: fix
+ * simple_xattr_list to always include security.* xattrs").
+ *
+ * Bug can be reproduced when SELinux and ACL are activated on inodes as
+ * following:
+ *
+ *     $ touch testfile
+ *     $ setfacl -m u:myuser:rwx testfile
+ *     $ getfattr -dm. /tmp/testfile
+ *     Segmentation fault (core dumped)
+ *
+ * The reason why this happens is that simple_xattr_list() always includes
+ * security.* xattrs without resetting error flag after
+ * security_inode_listsecurity(). This results into an incorrect length of the
+ * returned xattr name if POSIX ACL is also applied on the inode.
+ */
+
+#include "config.h"
+#include "tst_test.h"
+
+#if defined(HAVE_SYS_XATTR_H) && defined(HAVE_LIBACL)
+
+#include <pwd.h>
+#include <sys/acl.h>
+#include <sys/xattr.h>
+
+#define ACL_PERM        "u::rw-,u:root:rwx,g::r--,o::r--,m::rwx"
+#define TEST_FILE       "test.bin"
+
+static acl_t acl;
+
+static void verify_xattr(const int size)
+{
+	char buf[size];
+
+	memset(buf, 0, sizeof(buf));
+
+	if (listxattr(TEST_FILE, buf, size) == -1) {
+		if (errno != ERANGE)
+			tst_brk(TBROK | TERRNO, "listxattr() error");
+
+		tst_res(TFAIL, "listxattr() failed to read attributes length: ERANGE");
+		return;
+	}
+
+	tst_res(TPASS, "listxattr() correctly read attributes length");
+}
+
+static void run(void)
+{
+	int size;
+
+	size = listxattr(TEST_FILE, NULL, 0);
+	if (size == -1)
+		tst_brk(TBROK | TERRNO, "listxattr() error");

testcases/kernel/syscalls/listxattr/listxattr04.c line 71

Quoted 10 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +10 -0
+
+	verify_xattr(size);
+}
+
+static void setup(void)
+{
+	int res;
+
+	if (!tst_selinux_enabled())
+		tst_brk(TCONF, "SELinux is not running");

Current tst_selinux_enabled within your patchset not check running state of selinux. so i guess you need check /sys/fs/selinux/enforce value here.
Or you can use tst_selinux_enforcing. Correct me if any misunderstanding.

Quoted 44 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +37 -0
+
+	SAFE_TOUCH(TEST_FILE, 0644, NULL);
+
+	acl = acl_from_text(ACL_PERM);
+	if (!acl)
+		tst_brk(TBROK | TERRNO, "acl_from_text() failed");
+
+	res = acl_set_file(TEST_FILE, ACL_TYPE_ACCESS, acl);
+	if (res == -1) {
+		if (errno == EOPNOTSUPP)
+			tst_brk(TCONF | TERRNO, "acl_set_file()");
+
+		tst_brk(TBROK | TERRNO, "acl_set_file(%s) failed", TEST_FILE);
+	}
+}
+
+static void cleanup(void)
+{
+	if (acl)
+		acl_free(acl);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.tags = (const struct tst_tag[]) {
+		{"linux-git", "800d0b9b6a8b"},
+		{}
+	}
+};
+
+#else /* HAVE_SYS_XATTR_H && HAVE_LIBACL */
+	TST_TEST_TCONF("<sys/xattr.h> or <sys/acl.h> does not exist.");
+#endif
Signature · 6 lines
-- 
2.50.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

Re: [LTP] [PATCH v3 2/2] Add listxattr04 reproducer

Quoted 5 lines · Wei Gao

Current tst_selinux_enabled within your patchset not check running state of selinux. so i guess you need check /sys/fs/selinux/enforce value here.
Or you can use tst_selinux_enforcing. Correct me if any misunderstanding.

If SELinux is enabled, we will have /sys/fs/selinux folder filled with files that can be used to verify SELinux status (sudo sestatus -v). tst_selinux_enabled() is using:

    access(SELINUX_PATH, F_OK) == 0 && !tst_dir_is_empty(SELINUX_PATH, 0)

That is enough to know if we have SELinux or not. According to Cyril we probably just need:

    tst_is_mounted(SELINUX_PATH)

Like it is in is_selinux_enabled().

- Andrea

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

Re: [LTP] [PATCH v3 2/2] Add listxattr04 reproducer

Quoted 12 lines · Andrea Cervesato
Quoted 5 lines · Wei Gao

Current tst_selinux_enabled within your patchset not check running state of selinux. so i guess you need check /sys/fs/selinux/enforce value here.
Or you can use tst_selinux_enforcing. Correct me if any misunderstanding.

If SELinux is enabled, we will have /sys/fs/selinux folder filled with files that can be used to verify SELinux status (sudo sestatus -v). tst_selinux_enabled() is using:

    access(SELINUX_PATH, F_OK) == 0 && !tst_dir_is_empty(SELINUX_PATH, 0)

Quoted 9 lines

That is enough to know if we have SELinux or not. According to Cyril we probably just need:

    tst_is_mounted(SELINUX_PATH)

Like it is in is_selinux_enabled().

- Andrea

If only check enable status then i guess the tst_brk message should use tst_brk(TCONF, "SELinux is not enabled") instead of tst_brk(TCONF, "SELinux is not running") ?

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw

Re: [LTP] [PATCH v3 2/2] Add listxattr04 reproducer

Quoted 18 lines · Wei Gao
Quoted 17 lines · Andrea Cervesato
Quoted 5 lines · Wei Gao

Current tst_selinux_enabled within your patchset not check running state of selinux. so i guess you need check /sys/fs/selinux/enforce value here.
Or you can use tst_selinux_enforcing. Correct me if any misunderstanding.

If SELinux is enabled, we will have /sys/fs/selinux folder filled with files that can be used to verify SELinux status (sudo sestatus -v). tst_selinux_enabled() is using:

    access(SELINUX_PATH, F_OK) == 0 && !tst_dir_is_empty(SELINUX_PATH, 0)

That is enough to know if we have SELinux or not. According to Cyril we probably just need:

    tst_is_mounted(SELINUX_PATH)

Quoted 7 lines
Quoted 4 lines

Like it is in is_selinux_enabled().

- Andrea

If only check enable status then i guess the tst_brk message should use tst_brk(TCONF, "SELinux is not enabled") instead of tst_brk(TCONF, "SELinux is not running") ?

Right, "enabled" is more correct.

- Andrea

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp