Skip to contentSkip to conversation

syscalls/modify_ldt: Refactor into new API

PATCH Superseded Ricardo B. Marlière via ltp 2 participants 8 messages Apr 2, 2025 – Apr 7, 2025

v6 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v6 cover syscalls/modify_ldt: Refactor into new API

---
Changes in v6:
- Removed unnecessary includes
- Made modify_ldt wrappers static inline
- Used const struct user_desc *ptr instead of void *ptr in modify_ldt()
- Fixed modify_ldt01 test description
- Added explanation on removal of "asm/ldt.h" check in commit message
- Removed run_child() in modify_ldt02 - instead, removed unused "int val"
- Renamed "entry" to "invalid_entry" in modify_ldt01
- Link to v5: https://lore.kernel.org/r/[email protected]

Changes in v5:
- Removed unnecessary #error
- Removed const specifier in cve-2015-3290.c
- Improved modify_ldt02 test description
- Added run_child() in modify_ldt02
- Removed unnecessary #include "lapi/ldt.h" in files that include common.h
- Extended coverage in modify_ldt01
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Re-added the asm/ldt.h include directive
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Dropped the set_thread_area01 patch for a later series
- Squashed modify_ldt03 commit into modify_ldt01
- Renamed struct tst_case to tcase
- Removed fallback definition of struct user_desc
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Also refactored set_thread_area01
- Merged simple tests (including the existing modify_ldt03) into
modify_ldt01 instead of splitting it
- Added SAFE_MODIFY_LDT() macro
- Removed check to asm/ldt.h in configure.ac
- Used tst_syscall() instead of syscall() in modify_ldt() @ lapi/ldt.h
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Ricardo B. Marlière (3):
syscalls/modify_ldt: Add lapi/ldt.h syscalls/modify_ldt02: Refactor into new API syscalls/modify_ldt01: Refactor into new API

 configure.ac                                       |   1 -
 include/lapi/ldt.h                                 |  36 +++
 testcases/cve/cve-2015-3290.c                      |  37 +--
 testcases/cve/cve-2017-17053.c                     |  12 +-
 testcases/kernel/syscalls/fork/fork05.c            |   5 +-
 testcases/kernel/syscalls/modify_ldt/.gitignore    |   5 +-
 testcases/kernel/syscalls/modify_ldt/common.h      |  30 +++
 .../kernel/syscalls/modify_ldt/modify_ldt01.c      | 264 +++++----------------
 .../kernel/syscalls/modify_ldt/modify_ldt02.c      | 221 ++++-------------
 .../kernel/syscalls/modify_ldt/modify_ldt03.c      | 105 --------
 10 files changed, 183 insertions(+), 533 deletions(-)

---
base-commit: 0a0a77c444b56483bdde5046feeb518c96f5e4a2
change-id: 20250324-conversions-modify_ldt-816329223f6e

Best regards,

Signature · 5 lines
Ricardo B. Marlière <[email protected]>


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

PATCH v6 1/3 syscalls/modify_ldt: Add lapi/ldt.h

From: Ricardo B. Marlière <[email protected]>

Add a wrapper to modify_ldt which is used in a few tests and should be reused. Currently, LTP supports Linux v4.4+, which always have "asm/ldt.h", therefore remove the check for it in configure.ac.

---
 configure.ac                            |  1 -
 include/lapi/ldt.h                      | 36 ++++++++++++++++++++++++++++++++
 testcases/cve/cve-2015-3290.c           | 37 ++++++---------------------------
 testcases/cve/cve-2017-17053.c          | 12 +++++------
 testcases/kernel/syscalls/fork/fork05.c |  5 ++---
 5 files changed, 49 insertions(+), 42 deletions(-)
configure.ac +0 -1
index 671079f3aa48c72866f69e7c545d3428ba87f931..b22f3eacdb1ccb764eae443ab1fb70afd971a14c 100644
@@ -46,7 +46,6 @@ AC_CHECK_DECLS([PR_CAPBSET_DROP, PR_CAPBSET_READ],,,[#include <sys/prctl.h>])
 AC_CHECK_DECLS([SEM_STAT_ANY],,,[#include <sys/sem.h>])
 
 AC_CHECK_HEADERS_ONCE([ \
-    asm/ldt.h \
     asm/prctl.h \
     cpuid.h \
     emmintrin.h \
include/lapi/ldt.h new file +36 -0
new file mode 100644
index 0000000000000000000000000000000000000000..068a577cf113ba47a45d29920c6adb6851f4fc52
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2025 SUSE LLC Ricardo B. Marlière <[email protected]>
+ */
+
+#ifndef LAPI_LDT_H__
+#define LAPI_LDT_H__
+
+#include "config.h"
+#include "lapi/syscalls.h"
+#include <asm/ldt.h>
+
+static inline int modify_ldt(int func, const struct user_desc *ptr,
+			     unsigned long bytecount)
+{
+	return tst_syscall(__NR_modify_ldt, func, ptr, bytecount);
+}
+
+static inline int safe_modify_ldt(const char *file, const int lineno, int func,
+				  const struct user_desc *ptr,
+				  unsigned long bytecount)
+{
+	int rval;
+
+	rval = modify_ldt(func, ptr, bytecount);
+	if (rval == -1)
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			 "modify_ldt(%d, %p, %lu)", func, ptr, bytecount);
+
+	return rval;
+}
+
+#define SAFE_MODIFY_LDT(func, ptr, bytecount) \
+	safe_modify_ldt(__FILE__, __LINE__, (func), (ptr), (bytecount))
+
+#endif /* LAPI_LDT_H__ */
testcases/cve/cve-2015-3290.c +6 -31
index 63e5d92c91b830cd8066a6a6c329461b72731f32..603a4ba19f713dc7b7d4134324114ba8ffe245cb 100644
@@ -118,21 +118,18 @@ perhaps unsurprisingly.)
 #include "tst_test.h"
 #include "tst_timer.h"
 
-#if defined(__x86_64__) || defined(__i386__)
-
+#if defined(__i386__) || defined(__x86_64__)
 #include <stdlib.h>
 #include <stdio.h>
 #include <inttypes.h>
-#include <asm/ldt.h>
 #include <unistd.h>
-#include <sys/syscall.h>
 #include <setjmp.h>
 #include <signal.h>
 #include <string.h>
 #include <sys/wait.h>
 #include <linux/perf_event.h>
 
-#include "lapi/syscalls.h"
+#include "lapi/ldt.h"
 #include "tst_safe_pthread.h"
 
 /* Abstractions for some 32-bit vs 64-bit differences. */
@@ -199,27 +196,7 @@ static void set_ldt(void)
 		.useable	 = 0
 	};
 
-	TEST(tst_syscall(__NR_modify_ldt, 1, &data_desc, sizeof(data_desc)));
-
-	/*
-	 * The kernel intentionally casts modify_ldt() return value
-	 * to unsigned int to prevent sign extension to 64 bits. This may
-	 * result in syscall() returning the value as is instead of setting
-	 * errno and returning -1.
-	 */
-	if (TST_RET > 0 && ((int)TST_RET) < 0) {
-		tst_res(TINFO,
-			"WARNING: Libc mishandled modify_ldt() return value");
-		TST_ERR = -(int)TST_RET;
-		TST_RET = -1;
-	}
-
-	if (TST_RET == -1 && TST_ERR == EINVAL) {
-		tst_brk(TCONF | TTERRNO,
-			"modify_ldt: 16-bit data segments are probably disabled");
-	} else if (TST_RET != 0) {
-		tst_brk(TBROK | TTERRNO, "modify_ldt");
-	}
+	SAFE_MODIFY_LDT(1, &data_desc, sizeof(data_desc));
 }
 
 static void try_corrupt_stack(unsigned short *orig_ss)
@@ -528,8 +505,6 @@ static struct tst_test test = {
 	}
 };
 
-#else /* defined(__x86_64__) || defined(__i386__) */
-
-TST_TEST_TCONF("not (i386 or x86_64)");
-
-#endif
+#else
+TST_TEST_TCONF("Test supported only on x86");
+#endif /* defined(__i386__) || defined(__x86_64__) */
testcases/cve/cve-2017-17053.c +5 -7
index fe7b6d694d6ffbbce863abc1672e03ae5f419df1..8ebf97380b7d7f71ec3f9f6a79e627f9c2a4b09a 100644
@@ -16,17 +16,15 @@
 #include "config.h"
 #include "tst_test.h"
 
-#ifdef HAVE_ASM_LDT_H
-#include <asm/ldt.h>
+#if defined(__i386__) || defined(__x86_64__)
 #include <pthread.h>
 #include <signal.h>
 #include <stdlib.h>
-#include <sys/syscall.h>
 #include <sys/wait.h>
 #include <unistd.h>
 #include <stdio.h>
 
-#include "lapi/syscalls.h"
+#include "lapi/ldt.h"
 
 #define EXEC_USEC   5000000
 
@@ -109,7 +107,7 @@ void run_test(void)
 	struct user_desc desc = { .entry_number = 8191 };
 
 	install_sighandler();
-	syscall(__NR_modify_ldt, 1, &desc, sizeof(desc));
+	SAFE_MODIFY_LDT(1, &desc, sizeof(desc));
 
 	for (;;) {
 		if (shm->do_exit)
@@ -164,5 +162,5 @@ static struct tst_test test = {
 };
 
 #else
-TST_TEST_TCONF("no asm/ldt.h header (only for i386 or x86_64)");
-#endif
+TST_TEST_TCONF("Test supported only on x86");
+#endif /* defined(__i386__) || defined(__x86_64__) */
testcases/kernel/syscalls/fork/fork05.c +2 -3
index 22edefc3686978fbb9453dffabfcbccb7ea6bb12..9aa12e16201dec8f3d2a4c99df83c4e5e25ef857 100644
@@ -55,8 +55,7 @@
 
 #if defined(__i386__)
 
-#include "lapi/syscalls.h"
-#include <asm/ldt.h>
+#include "lapi/ldt.h"
 
 static void run(void)
 {
@@ -76,7 +75,7 @@ static void run(void)
 	ldt0.seg_not_present = 0;
 	ldt0.useable = 1;
 
-	tst_syscall(__NR_modify_ldt, 1, &ldt0, sizeof(ldt0));
+	SAFE_MODIFY_LDT(1, &ldt0, sizeof(ldt0));
 
 	asm volatile ("movw %w0, %%fs"::"q" (7));
 	asm volatile ("movl %%fs:0, %0":"=r" (lo));
Signature · 5 lines
2.49.0


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

Re: [LTP] [PATCH v6 1/3] syscalls/modify_ldt: Add lapi/ldt.h

Hi!

Quoted 6 lines
include/lapi/ldt.h new file +6 -0
+static inline int modify_ldt(int func, const struct user_desc *ptr,
+			     unsigned long bytecount)
+{
+	return tst_syscall(__NR_modify_ldt, func, ptr, bytecount);
+}
+

include/lapi/ldt.h line 28

Quoted 10 lines
include/lapi/ldt.h new file +10 -0
+static inline int safe_modify_ldt(const char *file, const int lineno, int func,
+				  const struct user_desc *ptr,
+				  unsigned long bytecount)
+{
+	int rval;
+
+	rval = modify_ldt(func, ptr, bytecount);
+	if (rval == -1)
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			 "modify_ldt(%d, %p, %lu)", func, ptr, bytecount);

We should make sure that we only accept the success value here (as we do in the rest of the safe macros) so that we catch the cases when kernel returns an invalid value.

This should be:

	if (rval == -1) {
		tst_brk_(...);
	} else if (rval) {
		tst_brk_("modify_ltd(...) invalid retval %i", ...);
	}

With that fixed:

Signature · 5 lines
Cyril Hrubis
[email protected]

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

Re: [LTP] [PATCH v6 1/3] syscalls/modify_ldt: Add lapi/ldt.h

Quoted 22 lines · Cyril Hrubis

Hi!

Quoted 6 lines
include/lapi/ldt.h new file +6 -0
+static inline int modify_ldt(int func, const struct user_desc *ptr,
+			     unsigned long bytecount)
+{
+	return tst_syscall(__NR_modify_ldt, func, ptr, bytecount);
+}
+

include/lapi/ldt.h line 28

Quoted 10 lines
include/lapi/ldt.h new file +10 -0
+static inline int safe_modify_ldt(const char *file, const int lineno, int func,
+				  const struct user_desc *ptr,
+				  unsigned long bytecount)
+{
+	int rval;
+
+	rval = modify_ldt(func, ptr, bytecount);
+	if (rval == -1)
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			 "modify_ldt(%d, %p, %lu)", func, ptr, bytecount);

We should make sure that we only accept the success value here (as we do in the rest of the safe macros) so that we catch the cases when kernel returns an invalid value.

Quoted 7 lines

This should be:

	if (rval == -1) {
		tst_brk_(...);
	} else if (rval) {
		tst_brk_("modify_ltd(...) invalid retval %i", ...);
	}

Makes sense! Unfortunately I just sent a v7, will wait some more before sending the next one :)

Quoted 5 lines

With that fixed:

Thanks for reviewing,
- Ricardo.

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

PATCH v6 2/3 syscalls/modify_ldt02: Refactor into new API

In reply to Ricardo B. Marlière via ltp

From: Ricardo B. Marlière <[email protected]>

---
 testcases/kernel/syscalls/modify_ldt/common.h      |  30 +++
 .../kernel/syscalls/modify_ldt/modify_ldt02.c      | 221 +++++----------------
 2 files changed, 76 insertions(+), 175 deletions(-)
testcases/kernel/syscalls/modify_ldt/common.h new file +30 -0
new file mode 100644
index 0000000000000000000000000000000000000000..40e33251528dc3352c39c221f26bb91733424162
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) International Business Machines  Corp., 2001
+ *	07/2001 Ported by Wayne Boyer
+ * Copyright (c) 2025 SUSE LLC Ricardo B. Marlière <[email protected]>
+ */
+
+#ifndef COMMON_H
+#define COMMON_H
+
+#include "tst_test.h"
+#include "lapi/ldt.h"
+
+int create_segment(void *seg, size_t size)
+{
+	struct user_desc entry = {
+		.entry_number = 0,
+		.base_addr = (unsigned long)seg,
+		.limit = size,
+		.seg_32bit = 1,
+		.contents = 0,
+		.read_exec_only = 0,
+		.limit_in_pages = 0,
+		.seg_not_present = 0,
+	};
+
+	return SAFE_MODIFY_LDT(1, &entry, sizeof(entry));
+}
+
+#endif
testcases/kernel/syscalls/modify_ldt/modify_ldt02.c +46 -175

245 lines not on this page · raw message

Signature · 5 lines
2.49.0


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

Re: [LTP] [PATCH v6 2/3] syscalls/modify_ldt02: Refactor into new API

Hi!

Quoted 7 lines
testcases/kernel/syscalls/modify_ldt/common.h new file +4 -0
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) International Business Machines  Corp., 2001
+ *	07/2001 Ported by Wayne Boyer

testcases/kernel/syscalls/modify_ldt/common.h line 14

Quoted 10 lines
testcases/kernel/syscalls/modify_ldt/common.h new file +10 -0
+ * Copyright (c) 2025 SUSE LLC Ricardo B. Marlière <[email protected]>
+ */
+
+#ifndef COMMON_H
+#define COMMON_H
+
+#include "tst_test.h"
+#include "lapi/ldt.h"
+
+int create_segment(void *seg, size_t size)

This should be void now, since we are not checking the return value anyways.

Quoted 14 lines
testcases/kernel/syscalls/modify_ldt/common.h new file +14 -0
+{
+	struct user_desc entry = {
+		.entry_number = 0,
+		.base_addr = (unsigned long)seg,
+		.limit = size,
+		.seg_32bit = 1,
+		.contents = 0,
+		.read_exec_only = 0,
+		.limit_in_pages = 0,
+		.seg_not_present = 0,
+	};
+
+	return SAFE_MODIFY_LDT(1, &entry, sizeof(entry));
+}

...

testcases/kernel/syscalls/modify_ldt/common.h

Quoted 2 lines
testcases/kernel/syscalls/modify_ldt/common.h new file +2 -0
+	if (!pid) {
+		signal(SIGSEGV, SIG_DFL);

We do not mess with signal handlers in the new test library, so this shouldn't be needed.

Quoted 4 lines
testcases/kernel/syscalls/modify_ldt/common.h new file +4 -0
+		read_segment(0);
+		exit(1);
+	}
+

testcases/kernel/syscalls/modify_ldt/common.h

Quoted 10 lines
testcases/kernel/syscalls/modify_ldt/common.h new file +10 -0
+	SAFE_WAITPID(pid, &status, 0);
+	if (WEXITSTATUS(status) != 0) {
+		tst_res(TFAIL, "Did not generate SEGV, child returned "
+			       "unexpected status");
+	} else {
+		if (WIFSIGNALED(status) && (WTERMSIG(status) == SIGSEGV))
+			tst_res(TPASS, "generate SEGV as expected");
+		else
+			tst_res(TFAIL, "Did not generate SEGV");
+	}

This should be just:

	if (WIFSIGNALED(status) && (WTERMSIG(status) == SIGSEGV))
		tst_res(TPASS, "generate SEGV as expected");
	else
		tst_res(TFAIL, "child %s", tst_strstatus(status));
Quoted 18 lines

}

testcases/kernel/syscalls/modify_ldt/common.h new file +8 -1
-#endif /* if defined(__i386__) */
+static struct tst_test test = {
+	.test_all = run,
+	.forks_child = 1,
+};
+
+#else
+TST_TEST_TCONF("Test supported only on i386");
+#endif /* __i386__ */
Signature · 6 lines
-- 
2.49.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis
[email protected]

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

PATCH v6 3/3 syscalls/modify_ldt01: Refactor into new API

In reply to Ricardo B. Marlière via ltp

From: Ricardo B. Marlière <[email protected]>

Now that we're using the wrapped modify_ldt() from the lapi/ldt.h which uses tst_syscall(), it does not make sense to keep the first block of the original test which simply checked for ENOSYS. Also, merge modify_ldt03 into modify_ldt01 and extend the test to cover all the errors from the manual page.

---
 testcases/kernel/syscalls/modify_ldt/.gitignore    |   5 +-
 .../kernel/syscalls/modify_ldt/modify_ldt01.c      | 264 +++++----------------
 .../kernel/syscalls/modify_ldt/modify_ldt03.c      | 105 --------
 3 files changed, 58 insertions(+), 316 deletions(-)
testcases/kernel/syscalls/modify_ldt/.gitignore +2 -3
index c0b8bbf5875af453b4880ef4b717fdb40d109ee7..c8817b2c3a811afdf40841dc1b81e4b2c034e7d8 100644
@@ -1,3 +1,2 @@
-/modify_ldt01
-/modify_ldt02
-/modify_ldt03
+modify_ldt01
+modify_ldt02
testcases/kernel/syscalls/modify_ldt/modify_ldt01.c +56 -208

286 lines not on this page · raw message

testcases/kernel/syscalls/modify_ldt/modify_ldt03.c deleted +0 -105

105 lines not on this page · raw message

Signature · 5 lines
2.49.0


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

Re: [LTP] [PATCH v6 3/3] syscalls/modify_ldt01: Refactor into new API

Hi!

Quoted 11 lines

Now that we're using the wrapped modify_ldt() from the lapi/ldt.h which uses tst_syscall(), it does not make sense to keep the first block of the original test which simply checked for ENOSYS. Also, merge modify_ldt03 into modify_ldt01 and extend the test to cover all the errors from the manual page.

---
 testcases/kernel/syscalls/modify_ldt/.gitignore    |   5 +-
 .../kernel/syscalls/modify_ldt/modify_ldt01.c      | 264 +++++----------------
 .../kernel/syscalls/modify_ldt/modify_ldt03.c      | 105 --------

You should remove modify_ldt03 from the syscalls runtest file as well.

Signature · 5 lines
Cyril Hrubis
[email protected]

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