Skip to contentSkip to conversation

syscalls/modify_ldt: Refactor into new API

PATCH Superseded Ricardo B. Marlière via ltp 1 participant 4 messages Apr 7, 2025

v7 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v7 cover syscalls/modify_ldt: Refactor into new API

This series is available at:

https://github.com/rbmarliere/ltp/tree/conversions/modify_ldt

CI is green:

https://github.com/rbmarliere/ltp/actions/runs/14308437153

---
Changes in v7:
- Added back the check for asm/ldt.h in configure.ac
- Added back the fallback definition of struct user_desc
- Link to v6: https://lore.kernel.org/r/[email protected]

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

 include/lapi/ldt.h                                 |  54 +++++
 testcases/cve/cve-2015-3290.c                      |  26 +-
 testcases/cve/cve-2017-17053.c                     |   6 +-
 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 --------
 9 files changed, 194 insertions(+), 522 deletions(-)

---
base-commit: 40576d40f7ed93fafae99ad78431cd8fc81490e2
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 v7 1/3 syscalls/modify_ldt: Add lapi/ldt.h

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

Add a wrapper to modify_ldt and a fallback definition to struct user_desc, which are needed in a few tests and should be reused.

---
 include/lapi/ldt.h                      | 54 +++++++++++++++++++++++++++++++++
 testcases/cve/cve-2015-3290.c           | 26 ++--------------
 testcases/cve/cve-2017-17053.c          |  6 ++--
 testcases/kernel/syscalls/fork/fork05.c |  5 ++-
 4 files changed, 60 insertions(+), 31 deletions(-)
include/lapi/ldt.h new file +54 -0
new file mode 100644
index 0000000000000000000000000000000000000000..e233f7925c0cce65298ff9e489e03c50d0aeabb3
@@ -0,0 +1,54 @@
+// 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"
+
+#ifdef HAVE_ASM_LDT_H
+#include <asm/ldt.h>
+#else
+struct user_desc {
+	unsigned int entry_number;
+	unsigned int base_addr;
+	unsigned int limit;
+	unsigned int seg_32bit : 1;
+	unsigned int contents : 2;
+	unsigned int read_exec_only : 1;
+	unsigned int limit_in_pages : 1;
+	unsigned int seg_not_present : 1;
+	unsigned int useable : 1;
+#ifdef __x86_64__
+	unsigned int lm : 1;
+#endif /* __x86_64__ */
+};
+#endif /* HAVE_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 +2 -24
index 63e5d92c91b830cd8066a6a6c329461b72731f32..8ec1d53bbb5a9f3e7761d39855d34f593e118a28 100644
@@ -123,16 +123,14 @@ perhaps unsurprisingly.)
 #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 +197,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)
testcases/cve/cve-2017-17053.c +2 -4
index fe7b6d694d6ffbbce863abc1672e03ae5f419df1..ec7a534a6c0104e6688f204d763c31ed7a048201 100644
@@ -17,16 +17,14 @@
 #include "tst_test.h"
 
 #ifdef HAVE_ASM_LDT_H
-#include <asm/ldt.h>
 #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)
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
#2 lore raw

PATCH v7 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
#3 lore raw

PATCH v7 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