Linux Test Project
 help / color / mirror / Atom feed
From: Wake Liu via ltp <ltp@lists.linux.it>
To: pvorel@suse.cz
Cc: wakel@google.com, ltp@lists.linux.it
Subject: [LTP] [PATCH v2] listmount04: Detect EBADF behavior dynamically at runtime
Date: Mon, 27 Jul 2026 12:11:30 +0000	[thread overview]
Message-ID: <20260727121130.1294997-1-wakel@google.com> (raw)
In-Reply-To: <20260724121114.GB983347@pevik>

Upstream commit 78f0e33cd6c9 ("fs/namespace: correctly handle errors
returned by grab_requested_mnt_ns") changed the errno from EINVAL to
EBADF when an invalid mount namespace file descriptor is passed to
listmount(). This change has been backported to various stable kernels
(e.g., 6.12.59 LTS).

Instead of hardcoding version checks for every stable backport, probe
the behavior dynamically in setup() by calling listmount() with an
invalid fd (-1) and checking the returned errno.

Signed-off-by: Wake Liu <wakel@google.com>
---
 .../kernel/syscalls/listmount/listmount04.c   | 40 ++++++++++++++-----
 1 file changed, 29 insertions(+), 11 deletions(-)

diff --git a/testcases/kernel/syscalls/listmount/listmount04.c b/testcases/kernel/syscalls/listmount/listmount04.c
index 919f4c854..561386cb4 100644
--- a/testcases/kernel/syscalls/listmount/listmount04.c
+++ b/testcases/kernel/syscalls/listmount/listmount04.c
@@ -23,10 +23,11 @@
 #define MNT_SIZE 32
 /*
  * For commit 78f0e33cd6c9 ("fs/namespace: correctly handle errors returned
- * by grab_requested_mnt_ns") from v6.18-rc7 backported to v6.17.9.
+ * by grab_requested_mnt_ns") from v6.18-rc7. Note that this change was
+ * backported to stable kernels (e.g. v6.17.9, v6.12.59).
  */
-#define BEFORE_6_17_9 1
-#define AFTER_6_17_9 2
+#define BEFORE_6_18 1
+#define AFTER_6_18 2
 
 static mnt_id_req *request;
 static uint64_t mnt_ids[MNT_SIZE];
@@ -89,7 +90,7 @@ static struct tcase {
 		.nr_mnt_ids = MNT_SIZE,
 		.exp_errno = EINVAL,
 		.msg = "invalid mnt_id_req.spare",
-		.kver = BEFORE_6_17_9,
+		.kver = BEFORE_6_18,
 	},
 	{
 		.req_usage = 1,
@@ -100,7 +101,7 @@ static struct tcase {
 		.nr_mnt_ids = MNT_SIZE,
 		.exp_errno = EBADF,
 		.msg = "invalid mnt_id_req.mnt_ns_fd",
-		.kver = AFTER_6_17_9,
+		.kver = AFTER_6_18,
 	},
 	{
 		.req_usage = 1,
@@ -153,16 +154,33 @@ static void run(unsigned int n)
 	}
 
 	TST_EXP_FAIL(tst_syscall(__NR_listmount, req, tc->mnt_ids,
-		tc->nr_mnt_ids, tc->flags), tc->exp_errno,
-		"%s", tc->msg);
+				 tc->nr_mnt_ids, tc->flags),
+		     tc->exp_errno,
+		     "%s", tc->msg);
 }
 
 static void setup(void)
 {
-	if (tst_kvercmp(6, 17, 9) >= 0)
-		kver = AFTER_6_17_9;
-	else
-		kver = BEFORE_6_17_9;
+	mnt_id_req req = {
+		.size = MNT_ID_REQ_SIZE_VER0,
+		.mnt_id = LSMT_ROOT,
+		.mnt_ns_fd = -1,
+	};
+	uint64_t ids[MNT_SIZE];
+
+	TEST(tst_syscall(__NR_listmount, &req, ids, MNT_SIZE, 0));
+	if (TST_RET >= 0)
+		tst_brk(TBROK, "listmount() succeeded unexpectedly with invalid fd");
+
+	if (TST_ERR == EBADF) {
+		kver = AFTER_6_18;
+		tst_res(TINFO, "Detected kernel with EBADF behavior for invalid mnt_ns_fd");
+	} else if (TST_ERR == EINVAL) {
+		kver = BEFORE_6_18;
+		tst_res(TINFO, "Detected kernel with EINVAL behavior for invalid mnt_ns_fd");
+	} else {
+		tst_brk(TBROK | TTERRNO, "Unexpected error during probe");
+	}
 }
 
 static struct tst_test test = {
-- 
2.55.0.487.gaf234c4eb3-goog


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

  parent reply	other threads:[~2026-07-27 12:11 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-24  9:57 [LTP] [PATCH] listmount04: Detect EBADF behavior dynamically at runtime Wake Liu via ltp
2026-07-24 12:11 ` Petr Vorel
2026-07-27  8:27   ` Petr Vorel
2026-07-27 12:11   ` Wake Liu via ltp [this message]
2026-07-27 16:22     ` [LTP] " linuxtestproject.agent
2026-07-27 12:21   ` [LTP] [PATCH] " Wake Liu via ltp

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260727121130.1294997-1-wakel@google.com \
    --to=ltp@lists.linux.it \
    --cc=pvorel@suse.cz \
    --cc=wakel@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox