Skip to contentSkip to conversation

syscalls/xattr: Add runtime probe for socket xattr backports

PATCH Superseded Darren Chang via ltp 3 participants 9 messages Jun 1 – Jun 3

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 syscalls/xattr: Add runtime probe for socket xattr backports

Upstream commit dc0876b9846d ("xattr: support extended attributes on sockets") was merged in v7.1.0. Currently, setxattr02 and fsetxattr02 strictly expect EPERM for sockets on kernels < 7.1.0.

However, downstream kernels (e.g., Android mainline branches) often backport this feature to older kernel versions (such as 7.0.0). On these kernels, the strict version check causes a false positive failure:
"passed unexpectedly".

This patch introduces a runtime probe in setup() for both tests to detect if older kernels have the backported feature. If the xattr syscalls succeed on a socket during setup, we assume the patch is present and adjust the expected result to pass.

---
 .../kernel/syscalls/fsetxattr/fsetxattr02.c   | 28 +++++++++++++++++++
 .../kernel/syscalls/setxattr/setxattr02.c     | 15 ++++++++++
 2 files changed, 43 insertions(+)
testcases/kernel/syscalls/fsetxattr/fsetxattr02.c +28 -0
index 2a48b7da7..ddb1804c8 100644
@@ -245,6 +245,34 @@ static void setup(void)
 	}
 
 	socket_xattr_supported = tst_kvercmp(7, 1, 0) >= 0;
+
+	/*
+	 * Probe for backports on older kernels.
+	 * If the version is < 7.1.0 but fsetxattr() succeeds, we assume the patch
+	 * has been backported.
+	 */
+    int socket_idx = -1;
+	if (!socket_xattr_supported) {
+		for (i = 0; i < ARRAY_SIZE(tc); i++) {
+			if (tc[i].issocket) {
+				socket_idx = i;
+				break;
+			}
+		}
+
+		if (socket_idx != -1) {
+			if (fsetxattr(tc[socket_idx].fd, tc[socket_idx].key,
+				      tc[socket_idx].value, tc[socket_idx].size,
+				      tc[socket_idx].flags) == 0) {
+				
+				tst_res(TINFO, "Socket xattr support detected (backport)");
+				socket_xattr_supported = true;
+				
+				/* Clean up the probed xattr to avoid interfering with the actual test */
+				SAFE_FREMOVEXATTR(tc[socket_idx].fd, tc[socket_idx].key);
+			}
+		}
+	}
 }
 
 static void cleanup(void)
testcases/kernel/syscalls/setxattr/setxattr02.c +15 -0
index 994ab655d..94d1024e6 100644
@@ -196,6 +196,21 @@ static void setup(void)
 	SAFE_MKNOD(SOCK, S_IFSOCK | 0777, 0);
 
 	socket_xattr_supported = tst_kvercmp(7, 1, 0) >= 0;
+
+	/*
+	 * Commit dc0876b9846d "xattr: support extended attributes on sockets"
+	 * merged in 7.1.0 mainline, but might be backported.
+	 * Try to set xattr on socket to detect support.
+	 */
+	if (!socket_xattr_supported) {
+		if (setxattr(SOCK, XATTR_TEST_KEY, XATTR_TEST_VALUE,
+			     XATTR_TEST_VALUE_SIZE, XATTR_CREATE) == 0) {
+			
+			tst_res(TINFO, "Socket xattr support detected (backport)");
+			socket_xattr_supported = true;
+			SAFE_REMOVEXATTR(SOCK, XATTR_TEST_KEY);
+		}
+	}
 }
 
 static struct tst_test test = {
Signature · 5 lines
2.54.0.1013.g208068f2d8-goog


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

Hi Darren,

Thank you for this patch. The approach is sound: a runtime probe for backported socket xattr support is the right way to avoid false failures on downstream kernels. However there are a few issues in fsetxattr02.c that need fixing before this can be applied.

Verdict: Needs revision

---

[1] fsetxattr02.c — variable declared after executable statement

The new int socket_idx = -1; declaration appears after the executable statement `socket_xattr_supported = tst_kvercmp(7, 1, 0)

Quoted 1 line

= 0;`. The existing setup() declares all its local variables at the

top of the function (size_t i = 0; etc.). This new variable must follow the same convention.

Move int socket_idx = -1; to the top of setup(), alongside the existing declarations:

  static void setup(void)
  {
  	size_t i = 0;
  	int socket_idx = -1;         /* add here */
  	struct sockaddr_un sun;
  	...

[2] fsetxattr02.c — indentation uses spaces instead of tabs

The int socket_idx = -1; line in the patch uses 4 spaces for indentation instead of a tab character:

- int socket_idx = -1; /* 4 spaces — wrong */
+ int socket_idx = -1; /* tab — correct */

The rest of the function uses tabs throughout.

[3] fsetxattr02.c — comment doesn't reference the upstream commit

The comment added to fsetxattr02.c is vague compared to the one added to setxattr02.c, which names the upstream commit and its title:

setxattr02.c (new):
/*
* Commit dc0876b9846d "xattr: support extended attributes on sockets"
* merged in 7.1.0 mainline, but might be backported.
* Try to set xattr on socket to detect support.
*/

fsetxattr02.c (new):
/*
* Probe for backports on older kernels.
* If the version is < 7.1.0 but fsetxattr() succeeds, we assume the patch
* has been backported.
*/

These describe the same feature. The fsetxattr02 comment should include the commit reference for consistency. Use the same wording as in setxattr02.c.

[4] fsetxattr02.c — inline comment is unnecessary

/* Clean up the probed xattr to avoid interfering with the actual test */ SAFE_FREMOVEXATTR(tc[socket_idx].fd, tc[socket_idx].key);

The comment states the obvious. SAFE_FREMOVEXATTR after a successful setxattr-probe speaks for itself. Drop it, matching the style of the equivalent block in setxattr02.c which has no such comment.

---

Aside from those four points the logic is correct. In particular:

- setxattr02.c looks clean; no issues there.
- The probe correctly uses the already-opened socket fd in fsetxattr02, mirroring the path-based probe in setxattr02.
- Silently ignoring non-zero probe results is correct: EPERM means no backport, and EOPNOTSUPP is caught later in verify_fsetxattr().
- socket_xattr_supported = true (using bool) is consistent with the field type (static bool socket_xattr_supported).
- The commit message is accurate and the Signed-off-by is present.

---

LTP AI Reviewer

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

PATCH v2 1/1 syscalls/xattr: Add runtime probe for socket xattr backports

In reply to Darren Chang via ltp

Upstream commit dc0876b9846d ("xattr: support extended attributes on sockets") was merged in v7.1.0. Currently, setxattr02 and fsetxattr02 strictly expect EPERM for sockets on kernels < 7.1.0.

However, downstream kernels (e.g., Android mainline branches) often backport this feature to older kernel versions (such as 7.0.0). On these kernels, the strict version check causes a false positive failure:
"passed unexpectedly".

This patch introduces a runtime probe in setup() for both tests to detect if older kernels have the backported feature. If the xattr syscalls succeed on a socket during setup, we assume the patch is present and adjust the expected result to pass.

---
 .../kernel/syscalls/fsetxattr/fsetxattr02.c   | 27 +++++++++++++++++++
 .../kernel/syscalls/setxattr/setxattr02.c     | 15 +++++++++++
 2 files changed, 42 insertions(+)
testcases/kernel/syscalls/fsetxattr/fsetxattr02.c +27 -0
index 2a48b7da7..985204480 100644
@@ -209,6 +209,7 @@ static void verify_fsetxattr(unsigned int i)
 static void setup(void)
 {
 	size_t i = 0;
+	int socket_idx = -1;
 	struct sockaddr_un sun;
 
 	dev_t dev = makedev(1, 3);
@@ -245,6 +246,32 @@ static void setup(void)
 	}
 
 	socket_xattr_supported = tst_kvercmp(7, 1, 0) >= 0;
+
+	/*
+	 * Commit dc0876b9846d "xattr: support extended attributes on sockets"
+	 * merged in 7.1.0 mainline, but might be backported.
+	 * Try to set xattr on socket to detect support.
+	 */
+	if (!socket_xattr_supported) {
+		for (i = 0; i < ARRAY_SIZE(tc); i++) {
+			if (tc[i].issocket) {
+				socket_idx = i;
+				break;
+			}
+		}
+
+		if (socket_idx != -1) {
+			if (fsetxattr(tc[socket_idx].fd, tc[socket_idx].key,
+				      tc[socket_idx].value, tc[socket_idx].size,
+				      tc[socket_idx].flags) == 0) {
+
+				tst_res(TINFO, "Socket xattr support detected (backport)");
+				socket_xattr_supported = true;
+
+				SAFE_FREMOVEXATTR(tc[socket_idx].fd, tc[socket_idx].key);
+			}
+		}
+	}
 }
 
 static void cleanup(void)
testcases/kernel/syscalls/setxattr/setxattr02.c +15 -0
index 994ab655d..d85eb7246 100644
@@ -196,6 +196,21 @@ static void setup(void)
 	SAFE_MKNOD(SOCK, S_IFSOCK | 0777, 0);
 
 	socket_xattr_supported = tst_kvercmp(7, 1, 0) >= 0;
+
+	/*
+	 * Commit dc0876b9846d "xattr: support extended attributes on sockets"
+	 * merged in 7.1.0 mainline, but might be backported.
+	 * Try to set xattr on socket to detect support.
+	 */
+	if (!socket_xattr_supported) {
+		if (setxattr(SOCK, XATTR_TEST_KEY, XATTR_TEST_VALUE,
+			     XATTR_TEST_VALUE_SIZE, XATTR_CREATE) == 0) {
+
+			tst_res(TINFO, "Socket xattr support detected (backport)");
+			socket_xattr_supported = true;
+			SAFE_REMOVEXATTR(SOCK, XATTR_TEST_KEY);
+		}
+	}
 }
 
 static struct tst_test test = {
Signature · 5 lines
2.54.0.1013.g208068f2d8-goog


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

Hi Darren,

Thank you for this patch. The overall approach — using a runtime probe to detect backported socket xattr support rather than relying solely on the version check — is correct and useful for Android downstream kernels.

A couple of issues to fix before merging:

---

[COMMIT MESSAGE]

Quoted 3 lines

was merged in v7.1.0

strictly expect EPERM for sockets on kernels < 7.1.0.

Kernel mainline releases are tagged as "v7.1", not "v7.1.0". The sublevel ".0" is not part of the release tag. Please replace "v7.1.0" → "v7.1" and "7.1.0" → "7.1" throughout the commit message.

---

[testcases/kernel/syscalls/fsetxattr/fsetxattr02.c]

testcases/kernel/syscalls/fsetxattr/fsetxattr02.c static void setup(void) · line 268

Quoted 6 lines
testcases/kernel/syscalls/fsetxattr/fsetxattr02.c +6 -0
+		if (socket_idx != -1) {
+			if (fsetxattr(tc[socket_idx].fd, tc[socket_idx].key,
+				      tc[socket_idx].value, tc[socket_idx].size,
+				      tc[socket_idx].flags) == 0) {
+
+				tst_res(TINFO, "Socket xattr support detected (backport)");

Blank line between the opening brace and the first statement. Linux kernel coding style does not permit blank lines immediately after an opening '{'. Please remove the blank line.

Also, the inline code comment (line just added in setup()) reads:

Review

Quoted 1 line
(quoted diff) +1 -0
+	 * merged in 7.1.0 mainline, but might be backported.

Same version notation issue: "7.1.0" → "7.1".

---

[testcases/kernel/syscalls/setxattr/setxattr02.c]

testcases/kernel/syscalls/setxattr/setxattr02.c static void setup(void) · line 209

Quoted 4 lines
testcases/kernel/syscalls/setxattr/setxattr02.c +4 -0
+		if (setxattr(SOCK, XATTR_TEST_KEY, XATTR_TEST_VALUE,
+			     XATTR_TEST_VALUE_SIZE, XATTR_CREATE) == 0) {
+
+			tst_res(TINFO, "Socket xattr support detected (backport)");

Same blank-line-after-brace issue as in fsetxattr02.c. Remove the blank line.

Also:

Review

Quoted 1 line
(quoted diff) +1 -0
+	 * merged in 7.1.0 mainline, but might be backported.

"7.1.0" → "7.1".

---

Verdict: Needs revision

Regards,
LTP AI Reviewer

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

PATCH v3 1/1 syscalls/xattr: Add runtime probe for socket xattr backports

In reply to Darren Chang via ltp

Upstream commit dc0876b9846d ("xattr: support extended attributes on sockets") was merged in v7.1. Currently, setxattr02 and fsetxattr02 strictly expect EPERM for sockets on kernels < 7.1.

However, downstream kernels (e.g., Android mainline branches) often backport this feature to older kernel versions (such as 7.0). On these kernels, the strict version check causes a false positive failure:
"passed unexpectedly".

This patch introduces a runtime probe in setup() for both tests to detect if older kernels have the backported feature. If the xattr syscalls succeed on a socket during setup, we assume the patch is present and adjust the expected result to pass.

---
 .../kernel/syscalls/fsetxattr/fsetxattr02.c   | 26 +++++++++++++++++++
 .../kernel/syscalls/setxattr/setxattr02.c     | 14 ++++++++++
 2 files changed, 40 insertions(+)
testcases/kernel/syscalls/fsetxattr/fsetxattr02.c +26 -0
index 2a48b7da7..1b4f8e819 100644
@@ -209,6 +209,7 @@ static void verify_fsetxattr(unsigned int i)
 static void setup(void)
 {
 	size_t i = 0;
+	int socket_idx = -1;
 	struct sockaddr_un sun;
 
 	dev_t dev = makedev(1, 3);
@@ -245,6 +246,31 @@ static void setup(void)
 	}
 
 	socket_xattr_supported = tst_kvercmp(7, 1, 0) >= 0;
+
+	/*
+	 * Commit dc0876b9846d "xattr: support extended attributes on sockets"
+	 * merged in 7.1 mainline, but might be backported.
+	 * Try to set xattr on socket to detect support.
+	 */
+	if (!socket_xattr_supported) {
+		for (i = 0; i < ARRAY_SIZE(tc); i++) {
+			if (tc[i].issocket) {
+				socket_idx = i;
+				break;
+			}
+		}
+
+		if (socket_idx != -1) {
+			if (fsetxattr(tc[socket_idx].fd, tc[socket_idx].key,
+				      tc[socket_idx].value, tc[socket_idx].size,
+				      tc[socket_idx].flags) == 0) {
+				tst_res(TINFO, "Socket xattr support detected (backport)");
+				socket_xattr_supported = true;
+
+				SAFE_FREMOVEXATTR(tc[socket_idx].fd, tc[socket_idx].key);
+			}
+		}
+	}
 }
 
 static void cleanup(void)
testcases/kernel/syscalls/setxattr/setxattr02.c +14 -0
index 994ab655d..167e068cf 100644
@@ -196,6 +196,20 @@ static void setup(void)
 	SAFE_MKNOD(SOCK, S_IFSOCK | 0777, 0);
 
 	socket_xattr_supported = tst_kvercmp(7, 1, 0) >= 0;
+
+	/*
+	 * Commit dc0876b9846d "xattr: support extended attributes on sockets"
+	 * merged in 7.1 mainline, but might be backported.
+	 * Try to set xattr on socket to detect support.
+	 */
+	if (!socket_xattr_supported) {
+		if (setxattr(SOCK, XATTR_TEST_KEY, XATTR_TEST_VALUE,
+			     XATTR_TEST_VALUE_SIZE, XATTR_CREATE) == 0) {
+			tst_res(TINFO, "Socket xattr support detected (backport)");
+			socket_xattr_supported = true;
+			SAFE_REMOVEXATTR(SOCK, XATTR_TEST_KEY);
+		}
+	}
 }
 
 static struct tst_test test = {
Signature · 5 lines
2.54.0.1013.g208068f2d8-goog


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

Hi Darren,

Thank you for the patch. The approach is sound: using a runtime probe in setup() to detect a backported feature is the correct LTP pattern when a version check alone is insufficient. One issue needs fixing before merging.

---

syscalls/xattr: Add runtime probe for socket xattr backports

Commit message: OK

testcases/kernel/syscalls/fsetxattr/fsetxattr02.c

Quoted 1 line
testcases/kernel/syscalls/fsetxattr/fsetxattr02.c +0 -0

Lines 8-26 (/*\ description comment, not changed by the patch):

/*\
* ...
* - Set attribute to a UNIX domain socket, fsetxattr(2) should

   *   return -1 and set errno to EPERM on kernels < 7.1.0.
   *   On kernel 7.1.0+ (dc0876b9846d "xattr: support extended
   *   attributes on sockets"), returns 0 (success) as sockets now
   *   support user.* xattrs.

*/

The patch changes the socket test-case behavior for kernels < 7.1.0 with the backport, but does not update this description. After the patch there are three outcomes for the socket case:

- kernel < 7.1.0, no backport → EPERM
- kernel < 7.1.0, backport present → 0 (success, detected by probe)
- kernel >= 7.1.0 → 0 (success)

The comment currently describes only the first and third case. Please update it to mention the runtime probe for backported kernels. For example:

* - Set attribute to a UNIX domain socket, fsetxattr(2) should

  *   return -1 and set errno to EPERM on kernels < 7.1.0, unless the
  *   feature is detected at runtime (e.g. backport). On kernel 7.1.0+
  *   (dc0876b9846d "xattr: support extended attributes on sockets"),
  *   or when the backport is probed in setup(), returns 0 (success).

Note: setxattr02.c uses "EPERM/SUCCEED" in its description which is already open-ended; no change is needed there.

testcases/kernel/syscalls/fsetxattr/fsetxattr02.c static void setup(void) · line 258

Quoted 5 lines
testcases/kernel/syscalls/fsetxattr/fsetxattr02.c +4 -0
+       int socket_idx = -1;
...
+               for (i = 0; i < ARRAY_SIZE(tc); i++) {
+                       if (tc[i].issocket) {
+                               socket_idx = i;

Nit: socket_idx is int but assigned from size_t i. Not a runtime problem here (small array), but declaring it as size_t socket_idx with ARRAY_SIZE(tc) as the "not found" sentinel would avoid the implicit narrowing. If you keep int, that is also acceptable given the tiny array size.

testcases/kernel/syscalls/setxattr/setxattr02.c

Quoted 1 line
testcases/kernel/syscalls/setxattr/setxattr02.c +0 -0

New probe code: OK. Clean and straightforward.

---

Verdict: Needs revision 🔴

The description comment in fsetxattr02.c must be updated to reflect the three possible outcomes for the socket test case. Everything else is correct.

LTP AI Reviewer

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

PATCH v4 1/1 syscalls/xattr: Add runtime probe for socket xattr backports

In reply to Darren Chang via ltp

Upstream commit dc0876b9846d ("xattr: support extended attributes on sockets") was merged in v7.1. Currently, setxattr02 and fsetxattr02 strictly expect EPERM for sockets on kernels < 7.1.

However, downstream kernels (e.g., Android mainline branches) often backport this feature to older kernel versions (such as 7.0). On these kernels, the strict version check causes a false positive failure:
"passed unexpectedly".

This patch introduces a runtime probe in setup() for both tests to detect if older kernels have the backported feature. If the xattr syscalls succeed on a socket during setup, we assume the patch is present and adjust the expected result to pass.

---
 .../kernel/syscalls/fsetxattr/fsetxattr02.c   | 34 ++++++++++++++++---
 .../kernel/syscalls/setxattr/setxattr02.c     | 14 ++++++++
 2 files changed, 44 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/fsetxattr/fsetxattr02.c +30 -4
index 2a48b7da7..4a0997445 100644
@@ -19,10 +19,10 @@
  * - Set attribute to a block special file, fsetxattr(2) should
  *   return -1 and set errno to EPERM.
  * - Set attribute to a UNIX domain socket, fsetxattr(2) should
- *   return -1 and set errno to EPERM on kernels < 7.1.0.
- *   On kernel 7.1.0+ (dc0876b9846d "xattr: support extended
- *   attributes on sockets"), returns 0 (success) as sockets now
- *   support user.* xattrs.
+ *   return -1 and set errno to EPERM on kernels < 7.1, unless the
+ *   feature is detected at runtime (e.g. backport). On kernel 7.1+
+ *   (dc0876b9846d "xattr: support extended attributes on sockets"),
+ *   or when the backport is probed in setup(), returns 0 (success).
  */
 
 /*
@@ -209,6 +209,7 @@ static void verify_fsetxattr(unsigned int i)
 static void setup(void)
 {
 	size_t i = 0;
+	size_t socket_idx = ARRAY_SIZE(tc);
 	struct sockaddr_un sun;
 
 	dev_t dev = makedev(1, 3);
@@ -245,6 +246,31 @@ static void setup(void)
 	}
 
 	socket_xattr_supported = tst_kvercmp(7, 1, 0) >= 0;
+
+	/*
+	 * Commit dc0876b9846d "xattr: support extended attributes on sockets"
+	 * merged in 7.1 mainline, but might be backported.
+	 * Try to set xattr on socket to detect support.
+	 */
+	if (!socket_xattr_supported) {
+		for (i = 0; i < ARRAY_SIZE(tc); i++) {
+			if (tc[i].issocket) {
+				socket_idx = i;
+				break;
+			}
+		}
+
+		if (socket_idx != ARRAY_SIZE(tc)) {
+			if (fsetxattr(tc[socket_idx].fd, tc[socket_idx].key,
+				      tc[socket_idx].value, tc[socket_idx].size,
+				      tc[socket_idx].flags) == 0) {
+				tst_res(TINFO, "Socket xattr support detected (backport)");
+				socket_xattr_supported = true;
+
+				SAFE_FREMOVEXATTR(tc[socket_idx].fd, tc[socket_idx].key);
+			}
+		}
+	}
 }
 
 static void cleanup(void)
testcases/kernel/syscalls/setxattr/setxattr02.c +14 -0
index 994ab655d..167e068cf 100644
@@ -196,6 +196,20 @@ static void setup(void)
 	SAFE_MKNOD(SOCK, S_IFSOCK | 0777, 0);
 
 	socket_xattr_supported = tst_kvercmp(7, 1, 0) >= 0;
+
+	/*
+	 * Commit dc0876b9846d "xattr: support extended attributes on sockets"
+	 * merged in 7.1 mainline, but might be backported.
+	 * Try to set xattr on socket to detect support.
+	 */
+	if (!socket_xattr_supported) {
+		if (setxattr(SOCK, XATTR_TEST_KEY, XATTR_TEST_VALUE,
+			     XATTR_TEST_VALUE_SIZE, XATTR_CREATE) == 0) {
+			tst_res(TINFO, "Socket xattr support detected (backport)");
+			socket_xattr_supported = true;
+			SAFE_REMOVEXATTR(SOCK, XATTR_TEST_KEY);
+		}
+	}
 }
 
 static struct tst_test test = {
Signature · 5 lines
2.54.0.929.g9b7fa37559-goog


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

Hi Darren,

Quoted 4 lines · Darren Chang

syscalls/xattr: Add runtime probe for socket xattr backports

Upstream commit dc0876b9846d ("xattr: support extended attributes on sockets") was merged in v7.1.

Nit: "merged in v7.1" uses the v prefix, but the rest of the commit message writes "7.1" (without v). LTP commit bodies should use a consistent notation. Either drop the v here ("merged in 7.1") or use it throughout.

Quoted 8 lines

Currently, setxattr02 and fsetxattr02 strictly expect EPERM for sockets on kernels < 7.1.

However, downstream kernels (e.g., Android mainline branches) often backport this feature to older kernel versions (such as 7.0). On these kernels, the strict version check causes a false positive failure:
"passed unexpectedly".

Quoted 4 lines

This patch introduces a runtime probe in setup() for both tests to detect if older kernels have the backported feature. If the xattr syscalls succeed on a socket during setup, we assume the patch is present and adjust the expected result to pass.

The motivation and approach are clearly explained. Good.

testcases/kernel/syscalls/fsetxattr/fsetxattr02.c line 23

Quoted 13 lines
testcases/kernel/syscalls/fsetxattr/fsetxattr02.c +4 -4
@@ -19,10 +19,10 @@
  * - Set attribute to a UNIX domain socket, fsetxattr(2) should
- *   return -1 and set errno to EPERM on kernels < 7.1.0.
- *   On kernel 7.1.0+ (dc0876b9846d "xattr: support extended
- *   attributes on sockets"), returns 0 (success) as sockets now
- *   support user.* xattrs.
+ *   return -1 and set errno to EPERM on kernels < 7.1, unless the
+ *   feature is detected at runtime (e.g. backport). On kernel 7.1+
+ *   (dc0876b9846d "xattr: support extended attributes on sockets"),
+ *   or when the backport is probed in setup(), returns 0 (success).

The updated doc comment in fsetxattr02.c accurately reflects the new behaviour, including the backport probe path. Good.

testcases/kernel/syscalls/fsetxattr/fsetxattr02.c static void verify_fsetxattr(unsigned int i) · line 212

Quoted 5 lines
testcases/kernel/syscalls/fsetxattr/fsetxattr02.c +1 -0
@@ -209,6 +209,7 @@ static void verify_fsetxattr(unsigned int i)
 static void setup(void)
 {
 	size_t i = 0;
+	size_t socket_idx = ARRAY_SIZE(tc);

Initialising socket_idx to ARRAY_SIZE(tc) as a sentinel before the search is correct and safe.

Quoted 9 lines
testcases/kernel/syscalls/fsetxattr/fsetxattr02.c +9 -0
+	if (!socket_xattr_supported) {
+		for (i = 0; i < ARRAY_SIZE(tc); i++) {
+			if (tc[i].issocket) {
+				socket_idx = i;
+				break;
+			}
+		}
+
+		if (socket_idx != ARRAY_SIZE(tc)) {

testcases/kernel/syscalls/fsetxattr/fsetxattr02.c static void setup(void) · line 273

Quoted 10 lines
testcases/kernel/syscalls/fsetxattr/fsetxattr02.c +10 -0
+			if (fsetxattr(tc[socket_idx].fd, tc[socket_idx].key,
+				      tc[socket_idx].value, tc[socket_idx].size,
+				      tc[socket_idx].flags) == 0) {
+				tst_res(TINFO, "Socket xattr support detected (backport)");
+				socket_xattr_supported = true;
+
+				SAFE_FREMOVEXATTR(tc[socket_idx].fd, tc[socket_idx].key);
+			}
+		}
+	}

The probe logic is correct:
- It uses a raw fsetxattr() (not SAFE_) so EPERM does not abort.
- On success it logs TINFO, sets the flag and removes the xattr with SAFE_FREMOVEXATTR before the test iterations run, so the subsequent XATTR_CREATE in verify_fsetxattr(6) finds a clean state.
- On failure for any reason the flag stays false and the test conservatively expects EPERM. This is the right default.
- The probe operates on the real, bound UNIX socket fd, which is the same fd used by the test, so there is no mismatch.

Quoted 9 lines
testcases/kernel/syscalls/setxattr/setxattr02.c +4 -0
@@ -196,6 +196,20 @@ static void setup(void)
 	SAFE_MKNOD(SOCK, S_IFSOCK | 0777, 0);
 
 	socket_xattr_supported = tst_kvercmp(7, 1, 0) >= 0;
+
+	/*
+	 * Commit dc0876b9846d "xattr: support extended attributes on sockets"
+	 * merged in 7.1 mainline, but might be backported.

testcases/kernel/syscalls/setxattr/setxattr02.c static void setup(void) · line 212

Quoted 10 lines
testcases/kernel/syscalls/setxattr/setxattr02.c +10 -0
+	 * Try to set xattr on socket to detect support.
+	 */
+	if (!socket_xattr_supported) {
+		if (setxattr(SOCK, XATTR_TEST_KEY, XATTR_TEST_VALUE,
+			     XATTR_TEST_VALUE_SIZE, XATTR_CREATE) == 0) {
+			tst_res(TINFO, "Socket xattr support detected (backport)");
+			socket_xattr_supported = true;
+			SAFE_REMOVEXATTR(SOCK, XATTR_TEST_KEY);
+		}
+	}

The setxattr02 probe is simpler (path-based, no fd to look up) and correct for the same reasons as the fsetxattr02 version. The mknod- created S_IFSOCK inode and a real bound socket are treated identically by the VFS xattr permission check that dc0876b9846d patches, so the probe is a valid detector for both path- and fd-based scenarios.

Nit: the doc comment in setxattr02.c was not updated in parallel with fsetxattr02.c. The existing comment just says:

- EPERM/SUCCEED - set attribute to a UNIX domain socket (dc0876b9846d ...)

While "SUCCEED" technically covers the backport case, a reader will not learn that the SUCCEED outcome can now also be triggered on kernels < 7.1 via the runtime probe. fsetxattr02.c makes this explicit. Consider adding a line to the setxattr02.c comment such as:

- EPERM/SUCCEED - set attribute to a UNIX domain socket (dc0876b9846d "xattr: support extended attributes on sockets"), SUCCEED also when the backport is probed at runtime in setup()

---

Overall the approach is sound: probe only when the kernel version check says the feature is absent, use the real syscall interface, clean up after a successful probe, and fall back conservatively on any failure. Both tests are covered consistently.

The two nits above (commit message v7.1 wording and the missing update to the setxattr02.c doc comment) are minor; the code itself is correct.

Verdict: Reviewed

LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#8 lore raw
In reply to Darren Chang via ltp

Hi!

Quoted 14 lines
testcases/kernel/syscalls/fsetxattr/fsetxattr02.c +13 -0
 	socket_xattr_supported = tst_kvercmp(7, 1, 0) >= 0;
+
+	/*
+	 * Commit dc0876b9846d "xattr: support extended attributes on sockets"
+	 * merged in 7.1 mainline, but might be backported.
+	 * Try to set xattr on socket to detect support.
+	 */
+	if (!socket_xattr_supported) {
+		for (i = 0; i < ARRAY_SIZE(tc); i++) {
+			if (tc[i].issocket) {
+				socket_idx = i;
+				break;
+			}
+		}

testcases/kernel/syscalls/fsetxattr/fsetxattr02.c static void setup(void) · line 271

Quoted 10 lines
testcases/kernel/syscalls/fsetxattr/fsetxattr02.c +10 -0
+
+		if (socket_idx != ARRAY_SIZE(tc)) {
+			if (fsetxattr(tc[socket_idx].fd, tc[socket_idx].key,
+				      tc[socket_idx].value, tc[socket_idx].size,
+				      tc[socket_idx].flags) == 0) {
+				tst_res(TINFO, "Socket xattr support detected (backport)");
+				socket_xattr_supported = true;
+
+				SAFE_FREMOVEXATTR(tc[socket_idx].fd, tc[socket_idx].key);
+			}

Instead of looking up the attribute in the array we can just create a socket, try ato add an attribute and remove the socked instead. With that we can up the code into a common header so that it's not duplicated int two testcases. I guess that the best place would be the lapi/socket.h header and we should add a static inline function socket_xattrs_supported() there.

Quoted 33 lines
testcases/kernel/syscalls/fsetxattr/fsetxattr02.c +2 -0
+		}
+	}
 }
 
 static void cleanup(void)
testcases/kernel/syscalls/setxattr/setxattr02.c +14 -0
index 994ab655d..167e068cf 100644
@@ -196,6 +196,20 @@ static void setup(void)
 	SAFE_MKNOD(SOCK, S_IFSOCK | 0777, 0);
 
 	socket_xattr_supported = tst_kvercmp(7, 1, 0) >= 0;
+
+	/*
+	 * Commit dc0876b9846d "xattr: support extended attributes on sockets"
+	 * merged in 7.1 mainline, but might be backported.
+	 * Try to set xattr on socket to detect support.
+	 */
+	if (!socket_xattr_supported) {
+		if (setxattr(SOCK, XATTR_TEST_KEY, XATTR_TEST_VALUE,
+			     XATTR_TEST_VALUE_SIZE, XATTR_CREATE) == 0) {
+			tst_res(TINFO, "Socket xattr support detected (backport)");
+			socket_xattr_supported = true;
+			SAFE_REMOVEXATTR(SOCK, XATTR_TEST_KEY);
+		}
+	}
 }
 
 static struct tst_test test = {
Signature · 1 line
2.54.0.929.g9b7fa37559-goog
Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp