Skip to contentSkip to conversation

listmount04: Detect EBADF behavior dynamically at runtime

PATCH Superseded Wake Liu via ltp 3 participants 8 messages Jul 24 – Aug 17 Arm STM32

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 listmount04: Detect EBADF behavior dynamically at runtime

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.

---
 .../kernel/syscalls/listmount/listmount04.c   | 27 ++++++++++++++++---
 1 file changed, 23 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/listmount/listmount04.c +23 -4
index 919f4c854..f7343608f 100644
@@ -153,16 +153,35 @@ 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)
+	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_17_9;
-	else
+		tst_res(TINFO, "Detected kernel with EBADF behavior for invalid mnt_ns_fd");
+	} else if (TST_ERR == EINVAL) {
 		kver = BEFORE_6_17_9;
+		tst_res(TINFO, "Detected kernel with EINVAL behavior for invalid mnt_ns_fd");
+	} else if (TST_ERR == ENOSYS) {
+		tst_brk(TCONF, "listmount() syscall not supported");
+	} else {
+		tst_brk(TBROK | TTERRNO, "Unexpected error during probe");
+	}
 }
 
 static struct tst_test test = {
Signature · 5 lines
2.55.0.487.gaf234c4eb3-goog


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 1/1
#1 lore raw
Quoted 31 lines

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.

---
 .../kernel/syscalls/listmount/listmount04.c   | 27 ++++++++++++++++---
 1 file changed, 23 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/listmount/listmount04.c +3 -2
index 919f4c854..f7343608f 100644
@@ -153,16 +153,35 @@ 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)

testcases/kernel/syscalls/listmount/listmount04.c static void run(unsigned int n) · line 170

Quoted 10 lines
testcases/kernel/syscalls/listmount/listmount04.c +8 -1
 {
-	if (tst_kvercmp(6, 17, 9) >= 0)
+	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));

I wonder if we should use listmount() from listmount.h.

testcases/kernel/syscalls/listmount/listmount04.c static void run(unsigned int n) · line 172

Quoted 2 lines
testcases/kernel/syscalls/listmount/listmount04.c +2 -0
+	if (TST_RET >= 0)
+		tst_brk(TBROK, "listmount() succeeded unexpectedly with invalid fd");

Maybe we could have TST_EXP_FAIL_ARR_SILENT_BRK() in the future :)

testcases/kernel/syscalls/listmount/listmount04.c static void run(unsigned int n) · line 175

Quoted 3 lines
testcases/kernel/syscalls/listmount/listmount04.c +2 -0
+
+	if (TST_ERR == EBADF) {
 		kver = AFTER_6_17_9;

At this point we may rename macros back to AFTER_6_18 and BEFORE_6_18, with keeping note that it got backported into stable.

testcases/kernel/syscalls/listmount/listmount04.c static void run(unsigned int n) · line 181

Quoted 7 lines
testcases/kernel/syscalls/listmount/listmount04.c +5 -1
-	else
+		tst_res(TINFO, "Detected kernel with EBADF behavior for invalid mnt_ns_fd");
+	} else if (TST_ERR == EINVAL) {
 		kver = BEFORE_6_17_9;
+		tst_res(TINFO, "Detected kernel with EINVAL behavior for invalid mnt_ns_fd");
+	} else if (TST_ERR == ENOSYS) {
+		tst_brk(TCONF, "listmount() syscall not supported");

This is IMHO not supported, because it's part of tst_syscall(). Other than this LGTM.

Kind regards,
Petr

Quoted 3 lines
testcases/kernel/syscalls/listmount/listmount04.c +3 -0
+	} else {
+		tst_brk(TBROK | TTERRNO, "Unexpected error during probe");
+	}
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Hi Wake Liu,

...

Quoted 8 lines

testcases/kernel/syscalls/listmount/listmount04.c static void run(unsigned int n) · line 181

Quoted 7 lines
testcases/kernel/syscalls/listmount/listmount04.c +5 -1
-	else
+		tst_res(TINFO, "Detected kernel with EBADF behavior for invalid mnt_ns_fd");
+	} else if (TST_ERR == EINVAL) {
 		kver = BEFORE_6_17_9;
+		tst_res(TINFO, "Detected kernel with EINVAL behavior for invalid mnt_ns_fd");
+	} else if (TST_ERR == ENOSYS) {
+		tst_brk(TCONF, "listmount() syscall not supported");

This is IMHO not supported, because it's part of tst_syscall().

I'm sorry, I meant "not needed".

Kind regards,
Petr

Quoted 9 lines

Other than this LGTM.

Kind regards,
Petr

Quoted 3 lines
testcases/kernel/syscalls/listmount/listmount04.c +3 -0
+	} else {
+		tst_brk(TBROK | TTERRNO, "Unexpected error during probe");
+	}
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

PATCH v2 1/1 listmount04: Detect EBADF behavior dynamically at runtime

In reply to Petr Vorel

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.

---
 .../kernel/syscalls/listmount/listmount04.c   | 40 ++++++++++++++-----
 1 file changed, 29 insertions(+), 11 deletions(-)
testcases/kernel/syscalls/listmount/listmount04.c +29 -11
index 919f4c854..561386cb4 100644
@@ -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 = {
Signature · 5 lines
2.55.0.487.gaf234c4eb3-goog


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

Hi Wake,

Quoted 1 line · Wake Liu

listmount04: Detect EBADF behavior dynamically at runtime

Verdict - Reviewed

---
Note:

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw
In reply to Wake Liu via ltp

Hi Petr,

Gentle ping on this v2 patch.

All comments from v1 have been addressed (renamed macros back to BEFORE_6_18/AFTER_6_18 and removed the unnecessary ENOSYS check).

Could you please take a look when you have time?

Thanks,
Wake

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

Hi Wake,

Quoted 3 lines

Hi Petr,

Gentle ping on this v2 patch.

FYI it was merged in the end of July by Andrea.
https://github.com/linux-test-project/ltp/commit/b4b82b28dcc63827e59f4cbc45f718f5913940ce

Kind regards,
Petr

Quoted 7 lines

All comments from v1 have been addressed (renamed macros back to BEFORE_6_18/AFTER_6_18 and removed the unnecessary ENOSYS check).

Could you please take a look when you have time?

Thanks,
Wake

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw
In reply to Petr Vorel

Hi Petr,

Thanks for the review. I've just sent out v2.

To address your comments:
- listmount() helper: We can't use it here because it constructs the request struct internally with mnt_ns_fd = 0. We need to set mnt_ns_fd = -1 to trigger the error behavior we are probing.
- Macro rename: Renamed BEFORE/AFTER_6_17_9 to BEFORE/AFTER_6_18 and added a note about stable backports.
- ENOSYS check: Removed since .min_kver = "6.11" guarantees listmount support.

Best regards,
Wake

Quoted 71 lines · Petr Vorel
Quoted 32 lines

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.

---
 .../kernel/syscalls/listmount/listmount04.c   | 27 ++++++++++++++++---
 1 file changed, 23 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/listmount/listmount04.c +3 -2
index 919f4c854..f7343608f 100644
@@ -153,16 +153,35 @@ 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)

testcases/kernel/syscalls/listmount/listmount04.c static void run(unsigned int n) · line 170

Quoted 10 lines
testcases/kernel/syscalls/listmount/listmount04.c +8 -1
 {
-     if (tst_kvercmp(6, 17, 9) >= 0)
+     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));

I wonder if we should use listmount() from listmount.h.

testcases/kernel/syscalls/listmount/listmount04.c static void run(unsigned int n) · line 172

Quoted 2 lines
testcases/kernel/syscalls/listmount/listmount04.c +2 -0
+     if (TST_RET >= 0)
+             tst_brk(TBROK, "listmount() succeeded unexpectedly with invalid fd");

Maybe we could have TST_EXP_FAIL_ARR_SILENT_BRK() in the future :)

testcases/kernel/syscalls/listmount/listmount04.c static void run(unsigned int n) · line 175

Quoted 3 lines
testcases/kernel/syscalls/listmount/listmount04.c +2 -0
+
+     if (TST_ERR == EBADF) {
              kver = AFTER_6_17_9;

At this point we may rename macros back to AFTER_6_18 and BEFORE_6_18, with keeping note that it got backported into stable.

testcases/kernel/syscalls/listmount/listmount04.c static void run(unsigned int n) · line 181

Quoted 7 lines
testcases/kernel/syscalls/listmount/listmount04.c +5 -1
-     else
+             tst_res(TINFO, "Detected kernel with EBADF behavior for invalid mnt_ns_fd");
+     } else if (TST_ERR == EINVAL) {
              kver = BEFORE_6_17_9;
+             tst_res(TINFO, "Detected kernel with EINVAL behavior for invalid mnt_ns_fd");
+     } else if (TST_ERR == ENOSYS) {
+             tst_brk(TCONF, "listmount() syscall not supported");

This is IMHO not supported, because it's part of tst_syscall(). Other than this LGTM.

Kind regards,
Petr

Quoted 3 lines
testcases/kernel/syscalls/listmount/listmount04.c +3 -0
+     } else {
+             tst_brk(TBROK | TTERRNO, "Unexpected error during probe");
+     }
Signature · 5 lines
Best Regards,
Wake Liu

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