Skip to contentSkip to conversation

Add listxattr04 test reproducer

PATCH In review Andrea Cervesato 3 participants 6 messages Jul 23, 2025 – Jul 24, 2025 Library code

lore
Share & tools
lore
#0 lore raw

v5 cover Add listxattr04 test reproducer

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

---
Changes in v5:
- add tst_lsm_enabled()
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- check if SELinux is enabled by looking at the mount point
- Link to v3: https://lore.kernel.org/r/[email protected]

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                            |   4 +
 lib/tst_security.c                                |  30 +++++-
 testcases/kernel/syscalls/listxattr/.gitignore    |   1 +
 testcases/kernel/syscalls/listxattr/Makefile      |   2 +
 testcases/kernel/syscalls/listxattr/listxattr04.c | 114 ++++++++++++++++++++++
 testcases/kernel/syscalls/lsm/lsm_common.h        |  34 +------
 6 files changed, 153 insertions(+), 32 deletions(-)

---
base-commit: 87cde3ff47d0728b7e7f937c6b20d7a95ad4e4aa
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 v5 1/2 core: add tst_selinux_enabled() utility

From: Andrea Cervesato <[email protected]>

Add tst_lsm_enabled() utility in tst_security.h in order to verify if a certain LSM is up and running.

---
 include/tst_security.h                     |  4 ++++
 lib/tst_security.c                         | 30 +++++++++++++++++++++++++-
 testcases/kernel/syscalls/lsm/lsm_common.h | 34 +++---------------------------
 3 files changed, 36 insertions(+), 32 deletions(-)
include/tst_security.h +4 -0
index 5d91f8a98f104b0cafaaf2046bc0ceec06870606..e2d7270daedcd5a303aa6a6184965cacf25c8fad 100644
@@ -5,6 +5,10 @@
 #ifndef TST_SECURITY_H__
 #define TST_SECURITY_H__
 
+#define LSM_SYS_FILE "/sys/kernel/security/lsm"
+
+int tst_lsm_enabled(const char *name);
+
 /*
  * Detect whether FIPS enabled
  * @return 0: FIPS not enabled, 1: FIPS enabled
lib/tst_security.c +29 -1
index 7d929fafe729058f55b921bf5cf7806b253496e0..c515271351bbc3126e58feef28dc2a88f2d40509 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"
@@ -19,6 +20,7 @@
 
 #include <fcntl.h>
 #include <stdio.h>
+#include <string.h>
 #include <stdlib.h>
 #include <sys/mount.h>
 
@@ -28,6 +30,32 @@
 #include "tst_security.h"
 #include "tst_private.h"
 
+int tst_lsm_enabled(const char *name)
+{
+	int fd;
+	char *ptr;
+	char data[BUFSIZ];
+
+	if (access(LSM_SYS_FILE, F_OK))
+		tst_brk(TCONF, "%s file is not present", LSM_SYS_FILE);
+
+	fd = SAFE_OPEN(LSM_SYS_FILE, O_RDONLY);
+	SAFE_READ(0, fd, data, BUFSIZ);
+	SAFE_CLOSE(fd);
+
+	ptr = strtok(data, ",");
+	while (ptr != NULL) {
+		if (!strcmp(ptr, name)) {
+			tst_res(TINFO, "%s is enabled", name);
+			return 1;
+		}
+
+		ptr = strtok(NULL, ",");
+	}
+
+	return 0;
+}
+
 int tst_fips_enabled(void)
 {
 	int fips = 0;
testcases/kernel/syscalls/lsm/lsm_common.h +3 -31
index 549f2d49b0b9290c4d75c87025911a81f4fa3c19..cc063eeab25545e8fc9ee40314c591b68c07dda7 100644
@@ -9,8 +9,6 @@
 #include "tst_test.h"
 #include "lapi/lsm.h"
 
-#define LSM_SYS_FILE "/sys/kernel/security/lsm"
-
 static inline struct lsm_ctx *next_ctx(struct lsm_ctx *tctx)
 {
 	return (struct lsm_ctx *)((char *)tctx + sizeof(*tctx) + tctx->ctx_len);
@@ -40,43 +38,17 @@ static inline void read_proc_attr(const char *attr, char *val, const size_t size
 	SAFE_CLOSE(fd);
 }
 
-static inline int verify_enabled_lsm(const char *name)
-{
-	int fd;
-	char *ptr;
-	char data[BUFSIZ];
-
-	if (access(LSM_SYS_FILE, F_OK))
-		tst_brk(TCONF, "%s file is not present", LSM_SYS_FILE);
-
-	fd = SAFE_OPEN(LSM_SYS_FILE, O_RDONLY);
-	SAFE_READ(0, fd, data, BUFSIZ);
-	SAFE_CLOSE(fd);
-
-	ptr = strtok(data, ",");
-	while (ptr != NULL) {
-		if (!strcmp(ptr, name)) {
-			tst_res(TINFO, "%s is enabled", name);
-			return 1;
-		}
-
-		ptr = strtok(NULL, ",");
-	}
-
-	return 0;
-}
-
 static inline uint32_t count_supported_attr_current(void)
 {
 	uint32_t lsm_count = 0;
 
-	if (verify_enabled_lsm("selinux"))
+	if (tst_lsm_enabled("selinux"))
 		lsm_count++;
 
-	if (verify_enabled_lsm("apparmor"))
+	if (tst_lsm_enabled("apparmor"))
 		lsm_count++;
 
-	if (verify_enabled_lsm("smack"))
+	if (tst_lsm_enabled("smack"))
 		lsm_count++;
 
 	return lsm_count;
Signature · 5 lines
2.50.1


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

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

Hi Andrea,

Quoted 6 lines

From: Andrea Cervesato <[email protected]>

Add tst_lsm_enabled() utility in tst_security.h in order to verify if a certain LSM is up and running.

LGTM, thank you!

Quoted 2 lines

Kind regards,
Petr

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

PATCH v5 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 | 114 ++++++++++++++++++++++
 3 files changed, 117 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 +114 -0

114 lines not on this page · raw message

Signature · 5 lines
2.50.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v5 2/2
#4 lore raw

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

Hi Andrea,

First, can you please before merge add
listxattr04 to runtest/syscalls?

With that you may add my RBT

Thanks for the test.

3 very minor notes, up to you if you change it before merge.

Quoted 1 line

+#include <pwd.h>

I guess this is now not needed, right?

Quoted 36 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +36 -0
+#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");
+
+	verify_xattr(size);
+}
+

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

Quoted 10 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +10 -0
+static void setup(void)
+{
+	int res;
+	int selinux;
+	int smack;
+
+	selinux = tst_lsm_enabled("selinux");
+	smack = tst_lsm_enabled("smack");
+
+	if (!selinux && !smack)

very nit: up to you, but I would avoid variables and just

	if (!tst_lsm_enabled("selinux") && !tst_lsm_enabled("smack"))
		tst_brk(TCONF, "There are no LSM(s) implementing xattr");
Quoted 28 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +27 -0
+		tst_brk(TCONF, "There are no LSM(s) implementing xattr");
 
+
+	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,

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

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

very very nit: please no dot in LTP messages.

Kind regards,
Petr

Quoted 1 line

+#endif

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

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

Merged with the suggestions below.

- Andrea

Quoted 109 lines · Petr Vorel

Hi Andrea,

First, can you please before merge add
listxattr04 to runtest/syscalls?

With that you may add my RBT

Thanks for the test.

3 very minor notes, up to you if you change it before merge.

Quoted 1 line

+#include <pwd.h>

I guess this is now not needed, right?

Quoted 36 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +36 -0
+#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");
+
+	verify_xattr(size);
+}
+

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

Quoted 10 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +10 -0
+static void setup(void)
+{
+	int res;
+	int selinux;
+	int smack;
+
+	selinux = tst_lsm_enabled("selinux");
+	smack = tst_lsm_enabled("smack");
+
+	if (!selinux && !smack)

very nit: up to you, but I would avoid variables and just

	if (!tst_lsm_enabled("selinux") && !tst_lsm_enabled("smack"))
		tst_brk(TCONF, "There are no LSM(s) implementing xattr");
Quoted 27 lines
testcases/kernel/syscalls/listxattr/listxattr04.c new file +27 -0
+		tst_brk(TCONF, "There are no LSM(s) implementing xattr");
+
+	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,

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

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

very very nit: please no dot in LTP messages.

Kind regards,
Petr

Quoted 1 line

+#endif

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