Skip to contentSkip to conversation

Add listxattr04 reproducer

PATCH Superseded Andrea Cervesato 3 participants 5 messages Jul 3, 2025 – Jul 9, 2025

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Add listxattr04 reproducer

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.

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

---
 testcases/kernel/syscalls/listxattr/.gitignore    |   1 +
 testcases/kernel/syscalls/listxattr/Makefile      |   2 +
 testcases/kernel/syscalls/listxattr/listxattr04.c | 133 ++++++++++++++++++++++
 3 files changed, 136 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 +133 -0

133 lines not on this page · raw message

---
base-commit: a908cff70f9389c2dd2bf535976cb179bfa8f340
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
Quoted 98 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.

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

---
 testcases/kernel/syscalls/listxattr/.gitignore    |   1 +
 testcases/kernel/syscalls/listxattr/Makefile      |   2 +
 testcases/kernel/syscalls/listxattr/listxattr04.c | 133 ++++++++++++++++++++++
 3 files changed, 136 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 +41 -0
new file mode 100644
index 0000000000000000000000000000000000000000..af80e0c609d5931132b3bf16b28805577b2a853d
@@ -0,0 +1,133 @@
+// 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 "lapi/lsm.h"
+
+#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)
+{

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

Quoted 10 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +10 -0
+	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;

Why ERANGE is Checked Separately?
Such as tst_res(TFAIL | TERRNO, "listxattr() error") will also report errno.

Quoted 34 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +34 -0
+	}
+
+	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");
+
+	verify_xattr(size);
+}
+
+static int selinux_enabled(void)
+{
+	uint32_t lsm_num;
+	uint64_t ids[32];
+	uint32_t page_size;
+	int available = 0;
+
+	page_size = SAFE_SYSCONF(_SC_PAGESIZE);
+
+	lsm_num = lsm_list_modules(ids, &page_size, 0);
+	if (!lsm_num)
+		return 0;
+
+	for (uint32_t i = 0; i < lsm_num; i++) {
+		if (ids[i] == LSM_ID_SELINUX) {
+			available = 1;
+			break;
+		}

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

Quoted 10 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +10 -0
+	}
+
+	return available;
+}
+
+static void setup(void)
+{
+	int res;
+
+	if (!selinux_enabled())

Mabye we can use is_selinux_enabled instead of self build function?

Quoted 11 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +11 -0
+		tst_brk(TCONF, "SELinux is not running");
+
+	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)

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

Quoted 10 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +10 -0
+			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);

unlink(TEST_FILE);

Quoted 22 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +17 -1
+}
+
+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
 
---

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

Quoted 4 lines

Best regards,

Signature · 1 line
Andrea Cervesato <[email protected]>

Thanks for your patch, some minor comments see above.

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

Hi Wei,

Quoted 104 lines · Wei Gao
Quoted 103 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.

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

---
  testcases/kernel/syscalls/listxattr/.gitignore    |   1 +
  testcases/kernel/syscalls/listxattr/Makefile      |   2 +
  testcases/kernel/syscalls/listxattr/listxattr04.c | 133 ++++++++++++++++++++++
  3 files changed, 136 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 +46 -0
new file mode 100644
index 0000000000000000000000000000000000000000..af80e0c609d5931132b3bf16b28805577b2a853d
@@ -0,0 +1,133 @@
+// 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 "lapi/lsm.h"
+
+#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) {
Quoted 8 lines

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

Quoted 5 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +5 -0
+		if (errno != ERANGE)
+			tst_brk(TBROK | TERRNO, "listxattr() error");
+
+		tst_res(TFAIL, "listxattr() failed to read attributes length: ERANGE");
+		return;

Why ERANGE is Checked Separately?
Such as tst_res(TFAIL | TERRNO, "listxattr() error") will also report errno.

ERANGE is the error we get if kernel is affected by this bug.

Quoted 37 lines
Quoted 37 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +37 -0
+	}
+
+	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");
+
+	verify_xattr(size);
+}
+
+static int selinux_enabled(void)
+{
+	uint32_t lsm_num;
+	uint64_t ids[32];
+	uint32_t page_size;
+	int available = 0;
+
+	page_size = SAFE_SYSCONF(_SC_PAGESIZE);
+
+	lsm_num = lsm_list_modules(ids, &page_size, 0);
+	if (!lsm_num)
+		return 0;
+
+	for (uint32_t i = 0; i < lsm_num; i++) {
+		if (ids[i] == LSM_ID_SELINUX) {
+			available = 1;
+			break;
+		}
+	}
+
+	return available;
Quoted 8 lines

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

Quoted 7 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +7 -0
+}
+
+static void setup(void)
+{
+	int res;
+
+	if (!selinux_enabled())

Mabye we can use is_selinux_enabled instead of self build function?

I seen it, but it's related to the libselinux header files used to communicate with the LSM. We might not have libselinux headers, but SELinux enabled anyway.

Quoted 44 lines
Quoted 11 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +11 -0
+		tst_brk(TCONF, "SELinux is not running");
+
+	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)

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

Quoted 10 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +10 -0
+			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);

unlink(TEST_FILE);

Quoted 22 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +17 -1
+}
+
+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
 
---

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

Quoted 7 lines
Quoted 4 lines

Best regards,

Signature · 1 line
Andrea Cervesato <[email protected]>

Thanks for your patch, some minor comments see above.

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

- Andrea

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw
In reply to Wei Gao via ltp

Hi!

Quoted 5 lines

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

Quoted 2 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +2 -0
+		tst_res(TFAIL, "listxattr() failed to read attributes length: ERANGE");
+		return;

Why ERANGE is Checked Separately?
Such as tst_res(TFAIL | TERRNO, "listxattr() error") will also report errno.

Because with ERANGE we reproduced the bug and return TFAIL while with any other errno we report TBROK.

Quoted 37 lines
Quoted 37 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +37 -0
+	}
+
+	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");
+
+	verify_xattr(size);
+}
+
+static int selinux_enabled(void)
+{
+	uint32_t lsm_num;
+	uint64_t ids[32];
+	uint32_t page_size;
+	int available = 0;
+
+	page_size = SAFE_SYSCONF(_SC_PAGESIZE);
+
+	lsm_num = lsm_list_modules(ids, &page_size, 0);
+	if (!lsm_num)
+		return 0;
+
+	for (uint32_t i = 0; i < lsm_num; i++) {
+		if (ids[i] == LSM_ID_SELINUX) {
+			available = 1;
+			break;
+		}
+	}
+
+	return available;
Quoted 8 lines

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

Quoted 7 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +7 -0
+}
+
+static void setup(void)
+{
+	int res;
+
+	if (!selinux_enabled())

Mabye we can use is_selinux_enabled instead of self build function?

That mean that we have to compile the test again one more library. I would rather add tst_selinux_enabled() into the test library. We already have tst_selinux_enforcing() there anyways. That should be as easy as reading another file in /sys/fs/selinux/.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw
Quoted 56 lines · Cyril Hrubis

Hi!

Quoted 5 lines

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

Quoted 2 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +2 -0
+		tst_res(TFAIL, "listxattr() failed to read attributes length: ERANGE");
+		return;

Why ERANGE is Checked Separately?
Such as tst_res(TFAIL | TERRNO, "listxattr() error") will also report errno.

Because with ERANGE we reproduced the bug and return TFAIL while with any other errno we report TBROK.

Quoted 37 lines
Quoted 37 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +37 -0
+	}
+
+	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");
+
+	verify_xattr(size);
+}
+
+static int selinux_enabled(void)
+{
+	uint32_t lsm_num;
+	uint64_t ids[32];
+	uint32_t page_size;
+	int available = 0;
+
+	page_size = SAFE_SYSCONF(_SC_PAGESIZE);
+
+	lsm_num = lsm_list_modules(ids, &page_size, 0);
+	if (!lsm_num)
+		return 0;
+
+	for (uint32_t i = 0; i < lsm_num; i++) {
+		if (ids[i] == LSM_ID_SELINUX) {
+			available = 1;
+			break;
+		}
+	}
+
+	return available;
Quoted 8 lines

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

Quoted 7 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +7 -0
+}
+
+static void setup(void)
+{
+	int res;
+
+	if (!selinux_enabled())

Mabye we can use is_selinux_enabled instead of self build function?

Quoted 4 lines

That mean that we have to compile the test again one more library. I would rather add tst_selinux_enabled() into the test library. We already have tst_selinux_enforcing() there anyways. That should be as easy as reading another file in /sys/fs/selinux/.

Got it. Also agree with using an easy way to check selinux status.

Quoted 4 lines
Signature · 3 lines
-- 
Cyril Hrubis
[email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp