[LTP] syscalls/xattr: Add runtime probe for socket xattr backports

linuxtestproject.agent@gmail.com linuxtestproject.agent@gmail.com
Tue Jun 2 06:06:01 CEST 2026


Hi Darren,

On Tue, 02 Jun 2026 01:07:54 +0000, Darren Chang wrote:

> 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.

> 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.

The motivation and approach are clearly explained. Good.

> Signed-off-by: Darren Chang <chihsheng@google.com>
>
> diff --git a/testcases/kernel/syscalls/fsetxattr/fsetxattr02.c b/testcases/kernel/syscalls/fsetxattr/fsetxattr02.c
> @@ -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.

> @@ -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.

> +	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);
> +			}
> +		}
> +	}

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.

> diff --git a/testcases/kernel/syscalls/setxattr/setxattr02.c b/testcases/kernel/syscalls/setxattr/setxattr02.c
> @@ -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);
> +		}
> +	}

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


More information about the ltp mailing list