syscalls/modify_ldt: Refactor into new API
v2 cover syscalls/modify_ldt: Refactor into new API
---
Changes in v2:
- Also refactored set_thread_area01
- Merged simple tests (including the existing modify_ldt03) intomodify_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 (6):
syscalls/modify_ldt: Add lapi/ldt.h syscalls/modify_ldt03: Refactor into new API syscalls/modify_ldt02: Refactor into new API syscalls/modify_ldt01: Refactor into new API syscalls/modify_ldt03: Merge into modify_ldt01 syscalls/set_thread_area01: Refactor into new API
configure.ac | 1 - include/lapi/ldt.h | 61 +++++ testcases/cve/cve-2015-3290.c | 35 +-- testcases/cve/cve-2017-17053.c | 10 +- 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 | 258 ++++----------------- .../kernel/syscalls/modify_ldt/modify_ldt02.c | 222 ++++-------------- .../kernel/syscalls/modify_ldt/modify_ldt03.c | 105 --------- .../syscalls/set_thread_area/set_thread_area.h | 31 --- .../syscalls/set_thread_area/set_thread_area01.c | 120 +++------- 12 files changed, 236 insertions(+), 647 deletions(-)
---
base-commit: e3aef569f0d0e7079bdb646965afaf419035eaa5
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
PATCH v2 1/6 syscalls/modify_ldt: Add lapi/ldt.h
From: Ricardo B. Marlière <[email protected]>
Add a wrapper to modify_ldt and a fallback to the user_desc structure which is used in a few tests and should be reused.
--- configure.ac | 1 - include/lapi/ldt.h | 61 +++++++++++++++++++++++++++++++++ testcases/cve/cve-2015-3290.c | 35 ++++--------------- testcases/cve/cve-2017-17053.c | 10 +++--- testcases/kernel/syscalls/fork/fork05.c | 5 ++- 5 files changed, 74 insertions(+), 38 deletions(-)
configure.ac +0 -1
@@ -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 +61 -0
61 lines not on this page · raw message
testcases/cve/cve-2015-3290.c +6 -29
@@ -118,12 +118,10 @@ 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>
@@ -132,6 +130,7 @@ perhaps unsurprisingly.)#include <sys/wait.h> #include <linux/perf_event.h> +#include "lapi/ldt.h" #include "lapi/syscalls.h" #include "tst_safe_pthread.h"
@@ -199,27 +198,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 +507,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 -5
@@ -16,8 +16,7 @@#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>
@@ -26,6 +25,7 @@#include <unistd.h> #include <stdio.h> +#include "lapi/ldt.h" #include "lapi/syscalls.h" #define EXEC_USEC 5000000
@@ -109,7 +109,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 +164,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
@@ -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)); asmvolatile ("movw %w0, %%fs" ::"q" (7 )); asmvolatile ("movl %%fs:0, %0" :"=r" (lo));
Signature · 5 lines
2.49.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v2 1/6] syscalls/modify_ldt: Add lapi/ldt.h
Hi Ricardo,
Quoted 60 lines · Ricardo B. Marliere via ltp
From: Ricardo B. Marlière <[email protected]>
Add a wrapper to
modify_ldtand a fallback to theuser_descstructure which is used in a few tests and should be reused.Signed-off-by Ricardo B. Marlière <[email protected]>--- configure.ac | 1 - include/lapi/ldt.h | 61 +++++++++++++++++++++++++++++++++ testcases/cve/cve-2015-3290.c | 35 ++++--------------- testcases/cve/cve-2017-17053.c | 10 +++--- testcases/kernel/syscalls/fork/fork05.c | 5 ++- 5 files changed, 74 insertions(+), 38 deletions(-)configure.ac +0
-1@@ -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 +28
-0@@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2025 SUSE LLC Ricardo B. Marlière <[email protected]> +*/ + +#if !(defined(__i386__) || defined(__x86_64__)) +#error "ldt.h should only be included on x86" +#endif + +#ifndef LAPI_LDT_H__ +#define LAPI_LDT_H__ + +#include "config.h" +#include "lapi/syscalls.h" + +#ifdef HAVE_STRUCT_USER_DESC +#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;
include/lapi/ldt.h line 38
Quoted 10 lines
include/lapi/ldt.h new file +10
-0+#ifdef __x86_64__ + unsigned int lm : 1; +#endif /* __x86_64__ */ +}; +#endif /* HAVE_STRUCT_USER_DESC */ + +#ifdef HAVE_STRUCT_MODIFY_LDT_LDT_S +/* In Linux 2.4 and earlier, this structure was named modify_ldt_ldt_s. */ +typedef struct modify_ldt_ldt_s user_desc ; +#endif /* HAVE_STRUCT_MODIFY_LDT_LDT_S */
All the above is not needed, because we always support asm/ldt.h functionalities. The reason is that our minimum supported kernel is 4.4, while feature was still present in 2.x. You can safely remove HAVE_STRUCT_USER_DESC and the fallback definitions of user_desc.
Quoted 57 lines
include/lapi/ldt.h new file +23
-0+ +static int modify_ldt (int func,void *ptr,unsigned long bytecount) +{ +return tst_syscall (__NR_modify_ldt, func, ptr, bytecount); +} + +static int safe_modify_ldt (const char *file,const int lineno,int func, +void *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
-7@@ -118,12 +118,10 @@ 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> @@ -132,6 +130,7 @@ perhaps unsurprisingly.)#include <sys/wait.h> #include <linux/perf_event.h> +#include "lapi/ldt.h" #include "lapi/syscalls.h" #include "tst_safe_pthread.h" @@ -199,27 +198,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
testcases/cve/cve-2015-3290.c static void set_ldt(void) · line 215
Quoted 10 lines
testcases/cve/cve-2015-3290.c +0
-10-* 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 ; - }
Sorry but I missed this. It seems to be right...kernel code in arch/x86/kernel/ldt.c:687
Even kselftests are not taking care of this and we should follow documentation, which says -1 return value is the default value for error.
I have the feeling this code was wrong. @Cyril what do you think?
Quoted 78 lines
testcases/cve/cve-2015-3290.c +4
-12- -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 +507,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
-5@@ -16,8 +16,7 @@#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> @@ -26,6 +25,7 @@#include <unistd.h> #include <stdio.h> +#include "lapi/ldt.h" #include "lapi/syscalls.h" #define EXEC_USEC 5000000@@ -109,7 +109,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 +164,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 +1
-2@@ -55,8 +55,7 @@#if defined(__i386__) -#include "lapi/syscalls.h" -#include <asm/ldt.h> +#include "lapi/ldt.h" static void run (void ) {
testcases/kernel/syscalls/fork/fork05.c static void run(void) · line 81
Quoted 8 lines
testcases/kernel/syscalls/fork/fork05.c +1
-1@@ -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)); asmvolatile ("movw %w0, %%fs" ::"q" (7 )); asmvolatile ("movl %%fs:0, %0" :"=r" (lo));
The rest looks ok,
Andrea
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v2 2/6 syscalls/modify_ldt03: Refactor into new API
In reply to Ricardo B. Marliere via ltpFrom: Ricardo B. Marlière <[email protected]>
--- .../kernel/syscalls/modify_ldt/modify_ldt03.c | 118 +++++---------------- 1 file changed, 24 insertions(+), 94 deletions(-)
testcases/kernel/syscalls/modify_ldt/modify_ldt03.c +24 -94
@@ -1,105 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* -* Copyright (c) 2014 Fujitsu Ltd. -* Author: Zeng Linggang <[email protected]> -* -* This program is free software; you can redistribute it and/or modify it -* under the terms of version 2 of the GNU General Public License as -* published by the Free Software Foundation. -* -* This program is distributed in the hope that it would be useful, but -* WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -* -* You should have received a copy of the GNU General Public License along -* with this program. -*/ -/* -* DESCRIPTION -* Basic test for modify_ldt(2) using func=0 argument. +* Copyright (c) 2014 Fujitsu Ltd. Zeng Linggang <[email protected]> +* Copyright (c) 2025 SUSE LLC Ricardo B. Marlière <[email protected]> */ -#include "config.h" -#include "test.h" - -char *TCID ="modify_ldt03" ; -int TST_TOTAL =1 ; - -#if defined(__i386__) && defined(HAVE_MODIFY_LDT ) - -#ifdef HAVE_ASM_LDT_H -# include <asm/ldt.h> -#endif -extern int modify_ldt (int ,void *,unsigned long ); - -#include <asm/unistd.h> -#include <string.h> -#include <sys/wait.h> -#include <errno.h> -#include "safe_macros.h" - -#ifdef HAVE_STRUCT_USER_DESC -# define SIZE sizeof(struct user_desc) -#else -# define SIZE 16 -#endif - -static char buf[SIZE ]; -static void cleanup (void ); -static void setup (void ); - -int main (int ac,char **av) -{ -int lc; - -tst_parse_opts (ac, av,NULL ,NULL ); - -setup (); - -for (lc =0 ;TEST_LOOPING (lc); lc++) { - - tst_count =0 ; - -TEST (modify_ldt (0 , buf,SIZE )); - -if (TEST_RETURN <0 ) { -tst_resm (TFAIL |TTERRNO , -"modify_ldt() failed with errno: %s" , -strerror (TEST_ERRNO )); - }else { -tst_resm (TPASS ,"modify_ldt() tested success" ); - } - } +/*\ +* Verify that a simple read (func=0) works when calling modify_ldt. +*/ -cleanup (); -tst_exit (); -} +#include "tst_test.h" -static void setup (void ) -{ -tst_sig (NOFORK ,DEF_HANDLER , cleanup); +#ifdef __i386__ +#include "lapi/ldt.h" -TEST_PAUSE ; -} +static char *buf; -static void cleanup (void ) +void run (void ) { +TST_EXP_POSITIVE (modify_ldt (0 , buf,sizeof (buf))); } -#elif HAVE_MODIFY_LDT +static struct tst_test test = { + .test_all = run, + .bufs = + (struct tst_buffers []){ + { &buf, .size =sizeof (struct user_desc ) }, + {}, + }, +}; -int main (void ) -{ -tst_brkm (TCONF , -NULL ,"modify_ldt is available but not tested on the platform than " -"__i386__" ); -} - -#else /* if defined(__i386__) */ - -int main (void ) -{ -tst_resm (TINFO ,"modify_ldt03 test only for ix86" ); -tst_exit (); -} - -#endif /* if defined(__i386__) */ +#else +TST_TEST_TCONF ("Test supported only on i386" ); +#endif /* __i386__ */
Signature · 5 lines
2.49.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v2 3/6 syscalls/modify_ldt02: Refactor into new API
In reply to Ricardo B. Marliere via ltpFrom: Ricardo B. Marlière <[email protected]>
--- testcases/kernel/syscalls/modify_ldt/common.h | 30 +++ .../kernel/syscalls/modify_ldt/modify_ldt02.c | 222 +++++---------------- 2 files changed, 77 insertions(+), 175 deletions(-)
testcases/kernel/syscalls/modify_ldt/common.h new file +30 -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 +* 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 +47 -175
246 lines not on this page · raw message
Signature · 5 lines
2.49.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v2 4/6 syscalls/modify_ldt01: Refactor into new API
In reply to Ricardo B. Marliere via ltpFrom: Ricardo B. Marlière <[email protected]>
Now that we're using the wrapper 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.
--- .../kernel/syscalls/modify_ldt/modify_ldt01.c | 254 ++++----------------- 1 file changed, 44 insertions(+), 210 deletions(-)
testcases/kernel/syscalls/modify_ldt/modify_ldt01.c +44 -210
@@ -1,230 +1,64 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* -* -* Copyright (c) International Business Machines Corp., 2001 -* -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 2 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See -* the GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +* Copyright (c) International Business Machines Corp., 2001 +* 07/2001 Ported by Wayne Boyer +* Copyright (c) 2025 SUSE LLC Ricardo B. Marlière <[email protected]> */ -/* -* NAME -* modify_ldt01.c -* -* DESCRIPTION -* Testcase to check the error conditions for modify_ldt(2) +/*\ +* Verify that modify_ldt() call fails with errno: * -* CALLS -* modify_ldt() -* -* ALGORITHM -* block1: -* Invoke modify_ldt() with a func value which is neither -* 0 or 1. Verify that ENOSYS is set. -* block2: -* Invoke mprotect() with ptr == NULL. Verify that EINVAL -* is set. -* block3: -* Create an LDT segment. -* Try to read from an invalid pointer. -* Verify that EFAULT is set. -* -* USAGE -* modify_ldt01 -* -* HISTORY -* 07/2001 Ported by Wayne Boyer -* -* RESTRICTIONS -* None +* - EINVAL, when writing (func=1) to an invalid pointer +* - EFAULT, when reading (func=0) from an invalid pointer */ -#include "config.h" -#include "test.h" - -TCID_DEFINE (modify_ldt01); -int TST_TOTAL =1 ; +#include "tst_test.h" -#if defined(__i386__) && defined(HAVE_MODIFY_LDT ) - -#ifdef HAVE_ASM_LDT_H -#include <asm/ldt.h> -#endif -extern int modify_ldt (int ,void *,unsigned long ); - -#include <asm/unistd.h> -#include <errno.h> - -/* Newer ldt.h files use user_desc, instead of modify_ldt_ldt_s */ -#ifdef HAVE_STRUCT_USER_DESC -typedef struct user_desc modify_ldt_s ; -#elif HAVE_STRUCT_MODIFY_LDT_LDT_S -typedef struct modify_ldt_ldt_s modify_ldt_s ; -#else -typedef struct modify_ldt_ldt_t { -unsigned int entry_number; -unsigned long 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 ; -unsigned int empty:25 ; -}modify_ldt_s ; -#endif - -int create_segment (void *,size_t ); -void cleanup (void ); -void setup (void ); - -int main (int ac,char **av) -{ -int lc; +#ifdef __i386__ +#include "lapi/ldt.h" +#include "common.h" +static void *ptr; +static char *buf; +static struct tst_case { +int tfunc;void *ptr; -int retval, func; - -int seg[4 ]; - -tst_parse_opts (ac, av,NULL ,NULL ); - -setup (); - -for (lc =0 ;TEST_LOOPING (lc); lc++) { - -/* reset tst_count in case we are looping */ - tst_count =0 ; - -/* -* Check for ENOSYS. -*/ - ptr =malloc (10 ); - func =100 ; - retval =modify_ldt (func, ptr,sizeof (ptr)); -if (retval <0 ) { -if (errno !=ENOSYS ) { -tst_resm (TFAIL ,"modify_ldt() set invalid " -"errno, expected ENOSYS, got: %d" , - errno); - }else { -tst_resm (TPASS , -"modify_ldt() set expected errno" ); - } - }else { -tst_resm (TFAIL ,"modify_ldt error: " -"unexpected return value %d" , retval); - } - -free (ptr); - -/* -* Check for EINVAL -*/ - ptr =0 ; - - retval =modify_ldt (1 , ptr,sizeof (ptr)); -if (retval <0 ) { -if (errno !=EINVAL ) { -tst_resm (TFAIL ,"modify_ldt() set invalid " -"errno, expected EINVAL, got: %d" , - errno); - }else { -tst_resm (TPASS , -"modify_ldt() set expected errno" ); - } - }else { -tst_resm (TFAIL ,"modify_ldt error: " -"unexpected return value %d" , retval); - } - -/* -* Create a new LDT segment. -*/ -if (create_segment (seg,sizeof (seg)) ==-1 ) { -tst_brkm (TBROK , cleanup,"Creation of segment failed" ); - } - -/* -* Check for EFAULT -*/ - ptr =sbrk (0 ); - - retval =modify_ldt (0 , ptr +0xFFF ,sizeof (ptr)); -if (retval <0 ) { -if (errno !=EFAULT ) { -tst_resm (TFAIL ,"modify_ldt() set invalid " -"errno, expected EFAULT, got: %d" , - errno); - }else { -tst_resm (TPASS , -"modify_ldt() set expected errno" ); - } - }else { -tst_resm (TFAIL ,"modify_ldt error: " -"unexpected return value %d" , retval); - } - } -cleanup (); -tst_exit (); -} - -/* -* create_segment() - -*/ -int create_segment (void *seg,size_t size) +unsigned long bytecount; +int exp_errno; +} tst_cases[] = { +/* { 100, &buf, sizeof(buf), ENOSYS }, // 01 block1 */ + {1 , (void *)0 ,0 ,EINVAL },// 01 block2 + {0 , &ptr,sizeof (ptr),EFAULT },// 01 block3 +}; + +void run (unsigned int i) { -modify_ldt_s entry; +struct tst_case *tc = &tst_cases[i]; - entry.entry_number =0 ; - entry.base_addr = (unsigned long )seg; - entry.limit = size; - entry.seg_32bit =1 ; - entry.contents =0 ; - entry.read_exec_only =0 ; - entry.limit_in_pages =0 ; - entry.seg_not_present =0 ; - -return modify_ldt (1 , &entry,sizeof (entry)); +TST_EXP_FAIL (modify_ldt (tc->tfunc, tc->ptr, tc->bytecount), + tc->exp_errno); }void setup (void ) { +int seg[4 ]; -tst_sig (FORK ,DEF_HANDLER , cleanup); - -TEST_PAUSE ; -} - -void cleanup (void ) -{ - + ptr =sbrk (0 ) +0xFFF ; +create_segment (seg,sizeof (seg)); + tst_cases[1 ].ptr = ptr; } -#elif HAVE_MODIFY_LDT -int main (void ) -{ -tst_brkm (TCONF , -NULL , -"modify_ldt is available but not tested on the platform than __i386__" ); -} +static struct tst_test test = { + .test = run, + .tcnt =ARRAY_SIZE (tst_cases), + .setup = setup, + .bufs = + (struct tst_buffers []){ + { &buf, .size =sizeof (struct user_desc ) }, + {}, + }, +};#else -int main (void ) -{ -tst_resm (TINFO ,"modify_ldt01 test only for ix86" ); -tst_exit (); -} - -#endif /* defined(__i386__) */ +TST_TEST_TCONF ("Test supported only on i386" ); +#endif /* __i386__ */
Signature · 5 lines
2.49.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v2 4/6] syscalls/modify_ldt01: Refactor into new API
Hi!
This follow-up patch should be merged with modify_ldt01 refactoring patch. There's no need to create too many patches. To make it easier, we should have the following ones:
- create fallback file with SAFE_* macro + SAFE_* macro usage inside other tests
- refactor modify_ldt01 (including modify_ldt03 in it)
- refactor modify_ldt02
- refactor set_thread_area01
Some comments below.
Quoted 116 lines · Ricardo B. Marliere via ltp
From: Ricardo B. Marlière <[email protected]>
Now that we're using the wrapper
modify_ldt()from thelapi/ldt.hwhich usestst_syscall(), it does not make sense to keep the first block of the original test.Signed-off-by Ricardo B. Marlière <[email protected]>--- .../kernel/syscalls/modify_ldt/modify_ldt01.c | 254 ++++----------------- 1 file changed, 44 insertions(+), 210 deletions(-)testcases/kernel/syscalls/modify_ldt/modify_ldt01.c +9
-84@@ -1,230 +1,64 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* -* -* Copyright (c) International Business Machines Corp., 2001 -* -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 2 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See -* the GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +* Copyright (c) International Business Machines Corp., 2001 +* 07/2001 Ported by Wayne Boyer +* Copyright (c) 2025 SUSE LLC Ricardo B. Marlière <[email protected]> */ -/* -* NAME -* modify_ldt01.c -* -* DESCRIPTION -* Testcase to check the error conditions for modify_ldt(2) +/*\ +* Verify that modify_ldt() call fails with errno: * -* CALLS -* modify_ldt() -* -* ALGORITHM -* block1: -* Invoke modify_ldt() with a func value which is neither -* 0 or 1. Verify that ENOSYS is set. -* block2: -* Invoke mprotect() with ptr == NULL. Verify that EINVAL -* is set. -* block3: -* Create an LDT segment. -* Try to read from an invalid pointer. -* Verify that EFAULT is set. -* -* USAGE -* modify_ldt01 -* -* HISTORY -* 07/2001 Ported by Wayne Boyer -* -* RESTRICTIONS -* None +* - EINVAL, when writing (func=1) to an invalid pointer +* - EFAULT, when reading (func=0) from an invalid pointer */ -#include "config.h" -#include "test.h" - -TCID_DEFINE (modify_ldt01); -int TST_TOTAL =1 ; +#include "tst_test.h" -#if defined(__i386__) && defined(HAVE_MODIFY_LDT ) - -#ifdef HAVE_ASM_LDT_H -#include <asm/ldt.h> -#endif -extern int modify_ldt (int ,void *,unsigned long ); - -#include <asm/unistd.h> -#include <errno.h> - -/* Newer ldt.h files use user_desc, instead of modify_ldt_ldt_s */ -#ifdef HAVE_STRUCT_USER_DESC -typedef struct user_desc modify_ldt_s ; -#elif HAVE_STRUCT_MODIFY_LDT_LDT_S -typedef struct modify_ldt_ldt_s modify_ldt_s ; -#else -typedef struct modify_ldt_ldt_t { -unsigned int entry_number; -unsigned long 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 ; -unsigned int empty:25 ; -}modify_ldt_s ; -#endif - -int create_segment (void *,size_t ); -void cleanup (void ); -void setup (void ); -
testcases/kernel/syscalls/modify_ldt/modify_ldt01.c line 23
Quoted 10 lines
testcases/kernel/syscalls/modify_ldt/modify_ldt01.c +6
-3-int main (int ac,char **av) -{ -int lc; +#ifdef __i386__ +#include "lapi/ldt.h" +#include "common.h" +static void *ptr; +static char *buf; +static struct tst_case {
tst_* is a reserved prefix for LTP library, so it's important to use something different. We usually name it "tcase"
Quoted 85 lines
testcases/kernel/syscalls/modify_ldt/modify_ldt01.c +1
-83+int tfunc;void *ptr; -int retval, func; - -int seg[4 ]; - -tst_parse_opts (ac, av,NULL ,NULL ); - -setup (); - -for (lc =0 ;TEST_LOOPING (lc); lc++) { - -/* reset tst_count in case we are looping */ - tst_count =0 ; - -/* -* Check for ENOSYS. -*/ - ptr =malloc (10 ); - func =100 ; - retval =modify_ldt (func, ptr,sizeof (ptr)); -if (retval <0 ) { -if (errno !=ENOSYS ) { -tst_resm (TFAIL ,"modify_ldt() set invalid " -"errno, expected ENOSYS, got: %d" , - errno); - }else { -tst_resm (TPASS , -"modify_ldt() set expected errno" ); - } - }else { -tst_resm (TFAIL ,"modify_ldt error: " -"unexpected return value %d" , retval); - } - -free (ptr); - -/* -* Check for EINVAL -*/ - ptr =0 ; - - retval =modify_ldt (1 , ptr,sizeof (ptr)); -if (retval <0 ) { -if (errno !=EINVAL ) { -tst_resm (TFAIL ,"modify_ldt() set invalid " -"errno, expected EINVAL, got: %d" , - errno); - }else { -tst_resm (TPASS , -"modify_ldt() set expected errno" ); - } - }else { -tst_resm (TFAIL ,"modify_ldt error: " -"unexpected return value %d" , retval); - } - -/* -* Create a new LDT segment. -*/ -if (create_segment (seg,sizeof (seg)) ==-1 ) { -tst_brkm (TBROK , cleanup,"Creation of segment failed" ); - } - -/* -* Check for EFAULT -*/ - ptr =sbrk (0 ); - - retval =modify_ldt (0 , ptr +0xFFF ,sizeof (ptr)); -if (retval <0 ) { -if (errno !=EFAULT ) { -tst_resm (TFAIL ,"modify_ldt() set invalid " -"errno, expected EFAULT, got: %d" , - errno); - }else { -tst_resm (TPASS , -"modify_ldt() set expected errno" ); - } - }else { -tst_resm (TFAIL ,"modify_ldt error: " -"unexpected return value %d" , retval); - } - } -cleanup ();
testcases/kernel/syscalls/modify_ldt/modify_ldt01.c line 28
Quoted 10 lines
testcases/kernel/syscalls/modify_ldt/modify_ldt01.c +3
-7-tst_exit (); -} - -/* -* create_segment() - -*/ -int create_segment (void *seg,size_t size) +unsigned long bytecount; +int exp_errno; +} tst_cases[] = {
Here tcases[]
Quoted 60 lines
testcases/kernel/syscalls/modify_ldt/modify_ldt01.c +23
-26+/* { 100, &buf, sizeof(buf), ENOSYS }, // 01 block1 */ + { 1, (void *)0, 0, EINVAL }, // 01 block2 + { 0, &ptr, sizeof(ptr), EFAULT }, // 01 block3 +}; + +void run (unsigned int i) { -modify_ldt_s entry; +struct tst_case *tc = &tst_cases[i]; - entry.entry_number =0 ; - entry.base_addr = (unsigned long )seg; - entry.limit = size; - entry.seg_32bit =1 ; - entry.contents =0 ; - entry.read_exec_only =0 ; - entry.limit_in_pages =0 ; - entry.seg_not_present =0 ; - -return modify_ldt (1 , &entry,sizeof (entry)); +TST_EXP_FAIL (modify_ldt (tc->tfunc, tc->ptr, tc->bytecount), + tc->exp_errno); }void setup (void ) { +int seg[4 ]; -tst_sig (FORK ,DEF_HANDLER , cleanup); - -TEST_PAUSE ; -} - -void cleanup (void ) -{ - + ptr =sbrk (0 ) +0xFFF ; +create_segment (seg,sizeof (seg)); + tst_cases[1 ].ptr = ptr; } -#elif HAVE_MODIFY_LDT -int main (void ) -{ -tst_brkm (TCONF , -NULL , -"modify_ldt is available but not tested on the platform than __i386__" ); -} +static struct tst_test test = { + .test = run, + .tcnt =ARRAY_SIZE (tst_cases), + .setup = setup, + .bufs = + (struct tst_buffers []){ + { &buf, .size =sizeof (struct user_desc ) }, + {}, + }, +};
testcases/kernel/syscalls/modify_ldt/modify_ldt01.c line 64
Quoted 10 lines
testcases/kernel/syscalls/modify_ldt/modify_ldt01.c +2
-7#else -int main (void ) -{ -tst_resm (TINFO ,"modify_ldt01 test only for ix86" ); -tst_exit (); -} - -#endif /* defined(__i386__) */ +TST_TEST_TCONF ("Test supported only on i386" ); +#endif /* __i386__ */
Andrea
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v2 4/6] syscalls/modify_ldt01: Refactor into new API
Quoted 11 lines · Andrea Cervesato
Hi!
This follow-up patch should be merged with
modify_ldt01refactoring patch. There's no need to create too many patches. To make it easier, we should have the following ones:- create fallback file with SAFE_* macro + SAFE_* macro usage inside other tests
- refactormodify_ldt01(includingmodify_ldt03in it)
- refactormodify_ldt02
- refactorset_thread_area01
Works for me, will squash in v3.
Quoted 127 lines
Some comments below.
Quoted 123 lines · Ricardo B. Marliere via ltp
From: Ricardo B. Marlière <[email protected]>
Now that we're using the wrapper
modify_ldt()from thelapi/ldt.hwhich usestst_syscall(), it does not make sense to keep the first block of the original test.Signed-off-by Ricardo B. Marlière <[email protected]>--- .../kernel/syscalls/modify_ldt/modify_ldt01.c | 254 ++++----------------- 1 file changed, 44 insertions(+), 210 deletions(-)testcases/kernel/syscalls/modify_ldt/modify_ldt01.c +12
-87@@ -1,230 +1,64 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* -* -* Copyright (c) International Business Machines Corp., 2001 -* -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 2 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See -* the GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +* Copyright (c) International Business Machines Corp., 2001 +* 07/2001 Ported by Wayne Boyer +* Copyright (c) 2025 SUSE LLC Ricardo B. Marlière <[email protected]> */ -/* -* NAME -* modify_ldt01.c -* -* DESCRIPTION -* Testcase to check the error conditions for modify_ldt(2) +/*\ +* Verify that modify_ldt() call fails with errno: * -* CALLS -* modify_ldt() -* -* ALGORITHM -* block1: -* Invoke modify_ldt() with a func value which is neither -* 0 or 1. Verify that ENOSYS is set. -* block2: -* Invoke mprotect() with ptr == NULL. Verify that EINVAL -* is set. -* block3: -* Create an LDT segment. -* Try to read from an invalid pointer. -* Verify that EFAULT is set. -* -* USAGE -* modify_ldt01 -* -* HISTORY -* 07/2001 Ported by Wayne Boyer -* -* RESTRICTIONS -* None +* - EINVAL, when writing (func=1) to an invalid pointer +* - EFAULT, when reading (func=0) from an invalid pointer */ -#include "config.h" -#include "test.h" - -TCID_DEFINE (modify_ldt01); -int TST_TOTAL =1 ; +#include "tst_test.h" -#if defined(__i386__) && defined(HAVE_MODIFY_LDT ) - -#ifdef HAVE_ASM_LDT_H -#include <asm/ldt.h> -#endif -extern int modify_ldt (int ,void *,unsigned long ); - -#include <asm/unistd.h> -#include <errno.h> - -/* Newer ldt.h files use user_desc, instead of modify_ldt_ldt_s */ -#ifdef HAVE_STRUCT_USER_DESC -typedef struct user_desc modify_ldt_s ; -#elif HAVE_STRUCT_MODIFY_LDT_LDT_S -typedef struct modify_ldt_ldt_s modify_ldt_s ; -#else -typedef struct modify_ldt_ldt_t { -unsigned int entry_number; -unsigned long 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 ; -unsigned int empty:25 ; -}modify_ldt_s ; -#endif - -int create_segment (void *,size_t ); -void cleanup (void ); -void setup (void ); - -int main (int ac,char **av) -{ -int lc; +#ifdef __i386__ +#include "lapi/ldt.h" +#include "common.h"
Quoted 5 lines
testcases/kernel/syscalls/modify_ldt/modify_ldt01.c line 23
Quoted 3 lines
testcases/kernel/syscalls/modify_ldt/modify_ldt01.c +3
-0+static void *ptr; +static char *buf; +static struct tst_case {tst_* is a reserved prefix for LTP library, so it's important to use something different. We usually name it "tcase"
Ah, makes sense, I didn't know that!
Thanks for reviewing,
- Ricardo.
Quoted 167 lines
Quoted 85 lines
testcases/kernel/syscalls/modify_ldt/modify_ldt01.c +1
-83+int tfunc;void *ptr; -int retval, func; - -int seg[4 ]; - -tst_parse_opts (ac, av,NULL ,NULL ); - -setup (); - -for (lc =0 ;TEST_LOOPING (lc); lc++) { - -/* reset tst_count in case we are looping */ - tst_count =0 ; - -/* -* Check for ENOSYS. -*/ - ptr =malloc (10 ); - func =100 ; - retval =modify_ldt (func, ptr,sizeof (ptr)); -if (retval <0 ) { -if (errno !=ENOSYS ) { -tst_resm (TFAIL ,"modify_ldt() set invalid " -"errno, expected ENOSYS, got: %d" , - errno); - }else { -tst_resm (TPASS , -"modify_ldt() set expected errno" ); - } - }else { -tst_resm (TFAIL ,"modify_ldt error: " -"unexpected return value %d" , retval); - } - -free (ptr); - -/* -* Check for EINVAL -*/ - ptr =0 ; - - retval =modify_ldt (1 , ptr,sizeof (ptr)); -if (retval <0 ) { -if (errno !=EINVAL ) { -tst_resm (TFAIL ,"modify_ldt() set invalid " -"errno, expected EINVAL, got: %d" , - errno); - }else { -tst_resm (TPASS , -"modify_ldt() set expected errno" ); - } - }else { -tst_resm (TFAIL ,"modify_ldt error: " -"unexpected return value %d" , retval); - } - -/* -* Create a new LDT segment. -*/ -if (create_segment (seg,sizeof (seg)) ==-1 ) { -tst_brkm (TBROK , cleanup,"Creation of segment failed" ); - } - -/* -* Check for EFAULT -*/ - ptr =sbrk (0 ); - - retval =modify_ldt (0 , ptr +0xFFF ,sizeof (ptr)); -if (retval <0 ) { -if (errno !=EFAULT ) { -tst_resm (TFAIL ,"modify_ldt() set invalid " -"errno, expected EFAULT, got: %d" , - errno); - }else { -tst_resm (TPASS , -"modify_ldt() set expected errno" ); - } - }else { -tst_resm (TFAIL ,"modify_ldt error: " -"unexpected return value %d" , retval); - } - } -cleanup ();testcases/kernel/syscalls/modify_ldt/modify_ldt01.c line 28
Quoted 10 lines
testcases/kernel/syscalls/modify_ldt/modify_ldt01.c +3
-7-tst_exit (); -} - -/* -* create_segment() - -*/ -int create_segment (void *seg,size_t size) +unsigned long bytecount; +int exp_errno; +} tst_cases[] = {Here tcases[]
Quoted 60 lines
testcases/kernel/syscalls/modify_ldt/modify_ldt01.c +23
-26+/* { 100, &buf, sizeof(buf), ENOSYS }, // 01 block1 */ + { 1, (void *)0, 0, EINVAL }, // 01 block2 + { 0, &ptr, sizeof(ptr), EFAULT }, // 01 block3 +}; + +void run (unsigned int i) { -modify_ldt_s entry; +struct tst_case *tc = &tst_cases[i]; - entry.entry_number =0 ; - entry.base_addr = (unsigned long )seg; - entry.limit = size; - entry.seg_32bit =1 ; - entry.contents =0 ; - entry.read_exec_only =0 ; - entry.limit_in_pages =0 ; - entry.seg_not_present =0 ; - -return modify_ldt (1 , &entry,sizeof (entry)); +TST_EXP_FAIL (modify_ldt (tc->tfunc, tc->ptr, tc->bytecount), + tc->exp_errno); }void setup (void ) { +int seg[4 ]; -tst_sig (FORK ,DEF_HANDLER , cleanup); - -TEST_PAUSE ; -} - -void cleanup (void ) -{ - + ptr =sbrk (0 ) +0xFFF ; +create_segment (seg,sizeof (seg)); + tst_cases[1 ].ptr = ptr; } -#elif HAVE_MODIFY_LDT -int main (void ) -{ -tst_brkm (TCONF , -NULL , -"modify_ldt is available but not tested on the platform than __i386__" ); -} +static struct tst_test test = { + .test = run, + .tcnt =ARRAY_SIZE (tst_cases), + .setup = setup, + .bufs = + (struct tst_buffers []){ + { &buf, .size =sizeof (struct user_desc ) }, + {}, + }, +};testcases/kernel/syscalls/modify_ldt/modify_ldt01.c line 64
Quoted 10 lines
testcases/kernel/syscalls/modify_ldt/modify_ldt01.c +2
-7#else -int main (void ) -{ -tst_resm (TINFO ,"modify_ldt01 test only for ix86" ); -tst_exit (); -} - -#endif /* defined(__i386__) */ +TST_TEST_TCONF ("Test supported only on i386" ); +#endif /* __i386__ */ Andrea
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v2 5/6 syscalls/modify_ldt03: Merge into modify_ldt01
In reply to Ricardo B. Marliere via ltpFrom: Ricardo B. Marlière <[email protected]>
--- testcases/kernel/syscalls/modify_ldt/.gitignore | 5 ++-- .../kernel/syscalls/modify_ldt/modify_ldt01.c | 20 ++++++++----- .../kernel/syscalls/modify_ldt/modify_ldt03.c | 35 ---------------------- 3 files changed, 14 insertions(+), 46 deletions(-)
testcases/kernel/syscalls/modify_ldt/.gitignore +2 -3
@@ -1,3 +1,2 @@ -/modify_ldt01 -/modify_ldt02 -/modify_ldt03 +modify_ldt01 +modify_ldt02
testcases/kernel/syscalls/modify_ldt/modify_ldt01.c +12 -8
@@ -6,10 +6,11 @@*/ /*\ -* Verify that modify_ldt() call fails with errno: +* Verify that modify_ldt() calls: * -* - EINVAL, when writing (func=1) to an invalid pointer -* - EFAULT, when reading (func=0) from an invalid pointer +* - Fails with EINVAL, when writing (func=1) to an invalid pointer +* - Fails with EFAULT, when reading (func=0) from an invalid pointer +* - Passes when reading (func=0) from a valid pointer */ #include "tst_test.h"
@@ -26,17 +27,20 @@ static struct tst_case {unsigned long bytecount;int exp_errno; } tst_cases[] = { - /* { 100, &buf, sizeof(buf), ENOSYS }, // 01 block1 */ - { 1, (void *)0, 0, EINVAL }, // 01 block2 - { 0, &ptr, sizeof(ptr), EFAULT }, // 01 block3 + { 1, (void *)0, 0, EINVAL }, + { 0, &ptr, sizeof(ptr), EFAULT }, + { 0, &buf, sizeof(buf), 0 }, }; void run(unsigned int i) { struct tst_case *tc = &tst_cases[i]; - TST_EXP_FAIL(modify_ldt(tc->tfunc, tc->ptr, tc->bytecount), - tc->exp_errno); + if (tc->exp_errno) + TST_EXP_FAIL(modify_ldt(tc->tfunc, tc->ptr, tc->bytecount), + tc->exp_errno); + else + TST_EXP_POSITIVE(modify_ldt(tc->tfunc, tc->ptr, tc->bytecount)); } void setup(void)
testcases/kernel/syscalls/modify_ldt/modify_ldt03.c deleted +0 -35
@@ -1,35 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-or-later - -/* -* Copyright (c) 2014 Fujitsu Ltd. Zeng Linggang <[email protected]> -* Copyright (c) 2025 SUSE LLC Ricardo B. Marlière <[email protected]> -*/ - -/*\ -* Verify that a simple read (func=0) works when calling modify_ldt. -*/ - -#include "tst_test.h" - -#ifdef __i386__ -#include "lapi/ldt.h" - -static char *buf; - -void run (void ) -{ -TST_EXP_POSITIVE (modify_ldt (0 , buf,sizeof (buf))); -} - -static struct tst_test test = { - .test_all = run, - .bufs = - (struct tst_buffers []){ - { &buf, .size =sizeof (struct user_desc ) }, - {}, - }, -}; - -#else -TST_TEST_TCONF ("Test supported only on i386" ); -#endif /* __i386__ */
Signature · 5 lines
2.49.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v2 6/6 syscalls/set_thread_area01: Refactor into new API
In reply to Ricardo B. Marliere via ltpFrom: Ricardo B. Marlière <[email protected]>
--- .../syscalls/set_thread_area/set_thread_area.h | 31 ------ .../syscalls/set_thread_area/set_thread_area01.c | 120 ++++++--------------- 2 files changed, 35 insertions(+), 116 deletions(-)
testcases/kernel/syscalls/set_thread_area/set_thread_area.h deleted +0 -31
@@ -1,31 +0,0 @@ -#include <stdio.h> -#include <errno.h> - -/* Harness Specific Include Files. */ -#include "test.h" -#include "lapi/syscalls.h" -#include "config.h" - -#if definedHAVE_ASM_LDT_H -#include <linux/unistd.h> -#include <asm/ldt.h> - -#if definedHAVE_STRUCT_USER_DESC -typedef struct user_desc thread_area_s ; -#elif definedHAVE_STRUCT_MODIFY_LDT_LDT_S -typedef struct modify_ldt_ldt_s thread_area_s ; -#else -typedef struct user_desc { -unsigned int entry_number; -unsigned long 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 ; -unsigned int empty:25 ; -}thread_area_s ; -#endif /* HAVE_STRUCT_USER_DESC */ -#endif /* HAVE_ASM_LDT_H */
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c +35 -85
144 lines not on this page · raw message
Signature · 5 lines
2.49.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v2 6/6] syscalls/set_thread_area01: Refactor into new API
Hi!
This test is weird. It stays inside set_thread_area folder, but it's testing get_thread_area(2) as well. We should probably create a new testing folder for get_thread_area(2), splitting test in two, doing the exact same thing: verifying that syscalls can be executed correctly and raises the correct errors.
More comments below.
Quoted 73 lines · Ricardo B. Marliere via ltp
From: Ricardo B. Marlière <[email protected]>
Signed-off-by Ricardo B. Marlière <[email protected]>--- .../syscalls/set_thread_area/set_thread_area.h | 31 ------ .../syscalls/set_thread_area/set_thread_area01.c | 120 ++++++--------------- 2 files changed, 35 insertions(+), 116 deletions(-)testcases/kernel/syscalls/set_thread_area/set_thread_area.h deleted +0
-31@@ -1,31 +0,0 @@ -#include <stdio.h> -#include <errno.h> - -/* Harness Specific Include Files. */ -#include "test.h" -#include "lapi/syscalls.h" -#include "config.h" - -#if definedHAVE_ASM_LDT_H -#include <linux/unistd.h> -#include <asm/ldt.h> - -#if definedHAVE_STRUCT_USER_DESC -typedef struct user_desc thread_area_s ; -#elif definedHAVE_STRUCT_MODIFY_LDT_LDT_S -typedef struct modify_ldt_ldt_s thread_area_s ; -#else -typedef struct user_desc { -unsigned int entry_number; -unsigned long 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 ; -unsigned int empty:25 ; -}thread_area_s ; -#endif /* HAVE_STRUCT_USER_DESC */ -#endif /* HAVE_ASM_LDT_H */ testcases/kernel/syscalls/set_thread_area/set_thread_area01.c +3
-17@@ -1,60 +1,37 @@ -/************************************************************************* +// SPDX-License-Identifier: GPL-2.0-or-later +/* * Copyright (c) Crackerjack Project., 2007 * Copyright (c) Manas Kumar Nayak <[email protected]> * Copyright (c) Cyril Hrubis <[email protected]> 2011 -* -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 2 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See -* the GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -* -************************************************************************/ +* Copyright (c) 2025 SUSE LLC Ricardo B. Marlière <[email protected]>
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c line 15
Quoted 10 lines
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c +8
-1+*/ -#include "set_thread_area.h" +/*\ +* The test will first call set_thread_area to a struct user_desc pointer with +* entry_number = -1, which will be set to a free entry_number upon exiting. +* Therefore, a subsequent call to get_thread_area using the same pointer will +* be valid. The same process is done but using -2 instead, which returns EINVAL +* to both calls. Finally, it's done one more time but to an invalid pointer and +* therefore an EFAULT is returned.
This is a really specific technical explanation which could be easily generated by AI or a person who's reading the test code :-)
What we need in the tests description is something that describe the logic behind the test, abstracting from the technical implementations, which could stay in [Algorithm] part. In short, we should be able to read the description at first glance and to understand the reason why a test has been made for.
Quoted 4 lines
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c +1
-2+*/ -char *TCID ="set_thread_area_01" ; -int TST_TOTAL =6 ;
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c line 23
Quoted 10 lines
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c +4
-4+#include "tst_test.h" -#if defined(HAVE_ASM_LDT_H ) && defined(HAVE_STRUCT_USER_DESC ) +#ifdef __i386__ +#include "lapi/ldt.h" -static void cleanup (void ) -{ -} +#define VALUE_AND_STRING (val) val, #val
We never use syscall_name in the whole test, so we can remove this macro which is complicating the code.
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c line 29
Quoted 11 lines
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c +3
-5-static void setup (void ) -{ -TEST_PAUSE ; -} +static struct user_desc u_info1 = {.entry_number =-1 }; +static struct user_desc u_info2 = {.entry_number =-2 }; -struct test { +static struct tst_case { int syscall;
This is not needed if we split the test.
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c line 30
Quoted 1 line
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c +0
-0const char *const syscall_name;
This is never used.
Quoted 23 lines
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c +2
-15-thread_area_s *u_info; +struct user_desc *u_info;int exp_ret;int exp_errno; -}; - -/* -* The set_thread_area uses a free entry_number if entry number is set to -1 -* and upon the syscall exit the entry number is set to entry which was used. -* So when we call get_thread_area on u_info1, the entry number is initalized -* corectly by the previous set_thread_area. -*/ -static struct user_desc u_info1 = {.entry_number =-1 }; -static struct user_desc u_info2 = {.entry_number =-2 }; - -#define VALUE_AND_STRING (val) val, #val - -static struct test tests[] = { +} tst_cases[] = { {VALUE_AND_STRING(__NR_set_thread_area), &u_info1, 0, 0}, {VALUE_AND_STRING(__NR_get_thread_area), &u_info1, 0, 0}, {VALUE_AND_STRING(__NR_set_thread_area), &u_info2, -1, EINVAL},
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c static struct test tests[] = { · line 40
Quoted 1 line
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c +0
-0@@ -63,49 +40,22 @@ static struct test tests[] = { {VALUE_AND_STRING(__NR_get_thread_area), (void *)-9, -1, EFAULT},
We have 2 more errno to test according to man pages. We should probably add them.
Quoted 53 lines
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c +12
-34}; -int main (int argc,char *argv[]) -{ -int lc; -unsigned i; - -tst_parse_opts (argc, argv,NULL ,NULL ); - -setup (); -for (lc =0 ;TEST_LOOPING (lc); lc++) { -for (i =0 ; i <sizeof (tests) /sizeof (struct test ); i++) { -TEST (tst_syscall (tests[i].syscall, tests[i].u_info)); - -if (TEST_RETURN != tests[i].exp_ret) { -tst_resm (TFAIL ,"%s returned %li expected %i" , - tests[i].syscall_name, -TEST_RETURN , tests[i].exp_ret); -continue ; - } +void run (unsigned int i) +{ +struct tst_case *tc = &tst_cases[i]; -if (TEST_ERRNO != tests[i].exp_errno) { -tst_resm (TFAIL , -"%s failed with %i (%s) expected %i (%s)" , - tests[i].syscall_name,TEST_ERRNO , -strerror (TEST_ERRNO ), - tests[i].exp_errno, -strerror (tests[i].exp_errno)); -continue ; - } +if (tc->exp_errno) +TST_EXP_FAIL (tst_syscall (tc->syscall, tc->u_info), tc->exp_errno); +else +TST_EXP_PASS (tst_syscall (tc->syscall, tc->u_info)); +} -tst_resm (TPASS ,"%s returned %li errno %i (%s)" , - tests[i].syscall_name,TEST_RETURN , -TEST_ERRNO ,strerror (TEST_ERRNO )); - } - } +static struct tst_test test = { + .test = run, + .tcnt =ARRAY_SIZE (tst_cases), +}; -cleanup (); -tst_exit ();
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c static struct test tests[] = { · line 61
Quoted 10 lines
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c +2
-7-}#else -int main (void ) -{ -tst_brkm (TCONF ,NULL , -"set_thread_area isn't available for this architecture" ); -} -#endif +TST_TEST_TCONF ("Test supported only on i386" ); +#endif /* __i386__ */
I guess we can skip this test from the modify_ldt testing suite and create a new patch-set only for these 2 syscalls: get_thread_area(2) and set_thread_area(2).
Kind Regards,
Andrea Cervesato
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v2 6/6] syscalls/set_thread_area01: Refactor into new API
Hello Andrea,
Quoted 7 lines · Andrea Cervesato
Hi!
This test is weird. It stays inside
set_thread_areafolder, but it's testingget_thread_area(2)as well. We should probably create a new testing folder forget_thread_area(2), splitting test in two, doing the exact same thing: verifying that syscalls can be executed correctly and raises the correct errors.
That is not the original intent of the test, which depends on what happens when calling set_thread_area with the correct value, which then changed what happens with the get_thread_area call.
Quoted 79 lines
More comments below.
Quoted 75 lines · Ricardo B. Marliere via ltp
From: Ricardo B. Marlière <[email protected]>
Signed-off-by Ricardo B. Marlière <[email protected]>--- .../syscalls/set_thread_area/set_thread_area.h | 31 ------ .../syscalls/set_thread_area/set_thread_area01.c | 120 ++++++--------------- 2 files changed, 35 insertions(+), 116 deletions(-)testcases/kernel/syscalls/set_thread_area/set_thread_area.h deleted +0
-31@@ -1,31 +0,0 @@ -#include <stdio.h> -#include <errno.h> - -/* Harness Specific Include Files. */ -#include "test.h" -#include "lapi/syscalls.h" -#include "config.h" - -#if definedHAVE_ASM_LDT_H -#include <linux/unistd.h> -#include <asm/ldt.h> - -#if definedHAVE_STRUCT_USER_DESC -typedef struct user_desc thread_area_s ; -#elif definedHAVE_STRUCT_MODIFY_LDT_LDT_S -typedef struct modify_ldt_ldt_s thread_area_s ; -#else -typedef struct user_desc { -unsigned int entry_number; -unsigned long 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 ; -unsigned int empty:25 ; -}thread_area_s ; -#endif /* HAVE_STRUCT_USER_DESC */ -#endif /* HAVE_ASM_LDT_H */ testcases/kernel/syscalls/set_thread_area/set_thread_area01.c +4
-17@@ -1,60 +1,37 @@ -/************************************************************************* +// SPDX-License-Identifier: GPL-2.0-or-later +/* * Copyright (c) Crackerjack Project., 2007 * Copyright (c) Manas Kumar Nayak <[email protected]> * Copyright (c) Cyril Hrubis <[email protected]> 2011 -* -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 2 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See -* the GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -* -************************************************************************/ +* Copyright (c) 2025 SUSE LLC Ricardo B. Marlière <[email protected]> +*/
Quoted 15 lines
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c line 15
Quoted 8 lines
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c +7
-1-#include "set_thread_area.h" +/*\ +* The test will first call set_thread_area to a struct user_desc pointer with +* entry_number = -1, which will be set to a free entry_number upon exiting. +* Therefore, a subsequent call to get_thread_area using the same pointer will +* be valid. The same process is done but using -2 instead, which returns EINVAL +* to both calls. Finally, it's done one more time but to an invalid pointer and +* therefore an EFAULT is returned. This is a really specific technical explanation which could be easily generated by AI or a person who's reading the test code :-) What we need in the tests description is something that describe the logic behind the test, abstracting from the technical implementations, which could stay in [Algorithm] part.
In short, we should be able to read the description at first glance and to understand the reason why a test has been made for.
See the comment below,
Quoted 30 lines
Quoted 4 lines
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c +1
-2+*/ -char *TCID ="set_thread_area_01" ; -int TST_TOTAL =6 ;testcases/kernel/syscalls/set_thread_area/set_thread_area01.c line 23
Quoted 10 lines
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c +4
-4+#include "tst_test.h" -#if defined(HAVE_ASM_LDT_H ) && defined(HAVE_STRUCT_USER_DESC ) +#ifdef __i386__ +#include "lapi/ldt.h" -static void cleanup (void ) -{ -} +#define VALUE_AND_STRING (val) val, #valWe never use
syscall_namein the whole test, so we can remove this macro which is complicating the code.testcases/kernel/syscalls/set_thread_area/set_thread_area01.c line 29
Quoted 11 lines
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c +3
-5-static void setup (void ) -{ -TEST_PAUSE ; -} +static struct user_desc u_info1 = {.entry_number =-1 }; +static struct user_desc u_info2 = {.entry_number =-2 }; -struct test { +static struct tst_case { int syscall;This is not needed if we split the test.
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c line 30
Quoted 1 line
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c +0
-0const char *const syscall_name;This is never used.
Quoted 11 lines
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c line 50
Quoted 11 lines
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c +1
-8-thread_area_s *u_info; +struct user_desc *u_info;int exp_ret;int exp_errno; -}; - -/* -* The set_thread_area uses a free entry_number if entry number is set to -1 -* and upon the syscall exit the entry number is set to entry which was used. -* So when we call get_thread_area on u_info1, the entry number is initalized -* corectly by the previous set_thread_area.
The test description was an elaboration of this comment. This is the reason why it calls set_ then get_. But if its too detailed I'll try simplifying it.
Quoted 65 lines
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c static struct test tests[] = { · line 40
Quoted 13 lines
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c +1
-7-*/ -static struct user_desc u_info1 = {.entry_number =-1 }; -static struct user_desc u_info2 = {.entry_number =-2 }; - -#define VALUE_AND_STRING (val) val, #val - -static struct test tests[] = { +} tst_cases[] = { {VALUE_AND_STRING(__NR_set_thread_area), &u_info1, 0, 0}, {VALUE_AND_STRING(__NR_get_thread_area), &u_info1, 0, 0}, {VALUE_AND_STRING(__NR_set_thread_area), &u_info2, -1, EINVAL},@@ -63,49 +40,22 @@ static struct test tests[] = { {VALUE_AND_STRING(__NR_get_thread_area), (void *)-9, -1, EFAULT},We have 2 more errno to test according to man pages. We should probably add them.
Quoted 50 lines
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c +12
-32}; -int main (int argc,char *argv[]) -{ -int lc; -unsigned i; - -tst_parse_opts (argc, argv,NULL ,NULL ); - -setup (); -for (lc =0 ;TEST_LOOPING (lc); lc++) { -for (i =0 ; i <sizeof (tests) /sizeof (struct test ); i++) { -TEST (tst_syscall (tests[i].syscall, tests[i].u_info)); - -if (TEST_RETURN != tests[i].exp_ret) { -tst_resm (TFAIL ,"%s returned %li expected %i" , - tests[i].syscall_name, -TEST_RETURN , tests[i].exp_ret); -continue ; - } +void run (unsigned int i) +{ +struct tst_case *tc = &tst_cases[i]; -if (TEST_ERRNO != tests[i].exp_errno) { -tst_resm (TFAIL , -"%s failed with %i (%s) expected %i (%s)" , - tests[i].syscall_name,TEST_ERRNO , -strerror (TEST_ERRNO ), - tests[i].exp_errno, -strerror (tests[i].exp_errno)); -continue ; - } +if (tc->exp_errno) +TST_EXP_FAIL (tst_syscall (tc->syscall, tc->u_info), tc->exp_errno); +else +TST_EXP_PASS (tst_syscall (tc->syscall, tc->u_info)); +} -tst_resm (TPASS ,"%s returned %li errno %i (%s)" , - tests[i].syscall_name,TEST_RETURN , -TEST_ERRNO ,strerror (TEST_ERRNO )); - } - } +static struct tst_test test = { + .test = run, + .tcnt =ARRAY_SIZE (tst_cases), +};
Quoted 16 lines
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c static struct test tests[] = { · line 61
Quoted 13 lines
testcases/kernel/syscalls/set_thread_area/set_thread_area01.c +2
-9-cleanup (); - tst_exit(); -}#else -int main (void ) -{ -tst_brkm (TCONF ,NULL , -"set_thread_area isn't available for this architecture" ); -} -#endif +TST_TEST_TCONF ("Test supported only on i386" ); +#endif /* __i386__ */ I guess we can skip this test from the
modify_ldttesting suite and create a new patch-set only for these 2 syscalls:get_thread_area(2)andset_thread_area(2).
Sure.
Thanks for reviewing,
- Ricardo.
Quoted 3 lines
Kind Regards,
Andrea Cervesato
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v2 6/6] syscalls/set_thread_area01: Refactor into new API
Hi,
Quoted 11 lines · Ricardo B. Marlière
Quoted 7 lines · Andrea Cervesato
Hi!
This test is weird. It stays inside
set_thread_areafolder, but it's testingget_thread_area(2)as well. We should probably create a new testing folder forget_thread_area(2), splitting test in two, doing the exact same thing: verifying that syscalls can be executed correctly and raises the correct errors.That is not the original intent of the test, which depends on what happens when calling
set_thread_areawith the correct value, which then changed what happens with theget_thread_areacall.
Thanks for clarifying. Then the way it's implemented needs some attention, because the struct test is misleading. Test cases are independent from each others by definition. We should put the right sequence of syscalls inside the run test function and to obtain the right output.
I still believe these two tests could be easily created and they would include the current testing scenario for free.
Andrea
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp