ldt.h: Add workaround for x86_64
PATCH v2 1/1 ldt.h: Add workaround for x86_64
From: Ricardo B. Marlière <[email protected]>
The commit be0aaca2f742 ("syscalls/modify_ldt: Add lapi/ldt.h") left behind an important factor of modify_ldt(): the kernel intentionally casts the return value to unsigned int. This was handled in testcases/cve/cve-2015-3290.c but was removed. Add it back to the relevant file.
---
Changes in v2:
- Added TBROK for any ret != 0 in modify_ldt call in cve-2015-3290.c
- Link to v1: https://lore.kernel.org/r/[email protected]
--- include/lapi/ldt.h | 22 +++++++++++++++++++++- testcases/cve/cve-2015-3290.c | 8 +++++++- 2 files changed, 28 insertions(+), 2 deletions(-)
include/lapi/ldt.h +21 -1
@@ -31,7 +31,27 @@ struct user_desc {static inline int modify_ldt (int func,const struct user_desc *ptr,unsigned long bytecount) { -return tst_syscall (__NR_modify_ldt, func, ptr, bytecount); +long rval; + + errno =0 ; + rval =tst_syscall (__NR_modify_ldt, func, ptr, bytecount); + +#ifdef __x86_64__ +/* +* 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 (rval >0 && (int )rval <0 ) { +tst_res (TINFO , +"WARNING: Libc mishandled modify_ldt() return value" ); + errno = -(int )errno; + rval =-1 ; + } +#endif /* __x86_64__ */ + +return rval; } static inline int safe_modify_ldt(const char *file, const int lineno, int func,
testcases/cve/cve-2015-3290.c +7 -1
@@ -197,7 +197,13 @@ static void set_ldt(void) .useable = 0 }; -SAFE_MODIFY_LDT (1 , &data_desc,sizeof (data_desc)); +TEST (modify_ldt (1 , &data_desc,sizeof (data_desc))); +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" ); + } }static void try_corrupt_stack (unsigned short *orig_ss)
---
base-commit: b070a5692e035ec12c3d3c7a7e9e97c270fd4d7d
change-id: 20250507-fixes-modify_ldt-ebcfdd2a7d30
Best regards,
Signature · 5 lines
Ricardo B. Marlière <[email protected]> -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi,
Quoted 77 lines · Ricardo B. Marlière
From: Ricardo B. Marlière <[email protected]>
The commit
be0aaca2f742("syscalls/modify_ldt: Addlapi/ldt.h") left behind an important factor ofmodify_ldt(): the kernel intentionally casts the return value to unsigned int. This was handled intestcases/cve/cve-2015-3290.cbut was removed. Add it back to the relevant file.Reported-by Martin Doucha <[email protected]>Signed-off-by Ricardo B. Marlière <[email protected]>---
Changes in v2:
- Added TBROK for any ret != 0 inmodify_ldtcall incve-2015-3290.c
- Link to v1: https://lore.kernel.org/r/[email protected]--- include/lapi/ldt.h | 22 +++++++++++++++++++++- testcases/cve/cve-2015-3290.c | 8 +++++++- 2 files changed, 28 insertions(+), 2 deletions(-)include/lapi/ldt.h +21
-1@@ -31,7 +31,27 @@ struct user_desc {static inline int modify_ldt (int func,const struct user_desc *ptr,unsigned long bytecount) { -return tst_syscall (__NR_modify_ldt, func, ptr, bytecount); +long rval; + + errno =0 ; + rval =tst_syscall (__NR_modify_ldt, func, ptr, bytecount); + +#ifdef __x86_64__ +/* +* 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 (rval >0 && (int )rval <0 ) { +tst_res (TINFO , +"WARNING: Libc mishandled modify_ldt() return value" ); + errno = -(int )errno; + rval =-1 ; + } +#endif /* __x86_64__ */ + +return rval; } static inline int safe_modify_ldt(const char *file, const int lineno, int func,testcases/cve/cve-2015-3290.c +7
-2@@ -197,7 +197,13 @@ static void set_ldt(void) .useable = 0 }; -SAFE_MODIFY_LDT (1 , &data_desc,sizeof (data_desc)); +TEST (modify_ldt (1 , &data_desc,sizeof (data_desc))); +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" ); + } }static void try_corrupt_stack (unsigned short *orig_ss) ---base-commit:
b070a5692e035ec12c3d3c7a7e9e97c270fd4d7d
change-id: 20250507-fixes-modify_ldt-ebcfdd2a7d30Best regards,
Signature · 10 lines
Martin Doucha [email protected] SW Quality Engineer SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 2 lines · Martin Doucha
Hi,
Reviewed-by Martin Doucha <[email protected]>
Thanks for reviewing and all the help!
- Ricardo.
Quoted 79 lines · Ricardo B. Marlière
Quoted 77 lines
From: Ricardo B. Marlière <[email protected]>
The commit
be0aaca2f742("syscalls/modify_ldt: Addlapi/ldt.h") left behind an important factor ofmodify_ldt(): the kernel intentionally casts the return value to unsigned int. This was handled intestcases/cve/cve-2015-3290.cbut was removed. Add it back to the relevant file.Reported-by Martin Doucha <[email protected]>Signed-off-by Ricardo B. Marlière <[email protected]>---
Changes in v2:
- Added TBROK for any ret != 0 inmodify_ldtcall incve-2015-3290.c
- Link to v1: https://lore.kernel.org/r/[email protected]--- include/lapi/ldt.h | 22 +++++++++++++++++++++- testcases/cve/cve-2015-3290.c | 8 +++++++- 2 files changed, 28 insertions(+), 2 deletions(-)include/lapi/ldt.h +21
-1@@ -31,7 +31,27 @@ struct user_desc {static inline int modify_ldt (int func,const struct user_desc *ptr,unsigned long bytecount) { -return tst_syscall (__NR_modify_ldt, func, ptr, bytecount); +long rval; + + errno =0 ; + rval =tst_syscall (__NR_modify_ldt, func, ptr, bytecount); + +#ifdef __x86_64__ +/* +* 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 (rval >0 && (int )rval <0 ) { +tst_res (TINFO , +"WARNING: Libc mishandled modify_ldt() return value" ); + errno = -(int )errno; + rval =-1 ; + } +#endif /* __x86_64__ */ + +return rval; } static inline int safe_modify_ldt(const char *file, const int lineno, int func,testcases/cve/cve-2015-3290.c +7
-2@@ -197,7 +197,13 @@ static void set_ldt(void) .useable = 0 }; -SAFE_MODIFY_LDT (1 , &data_desc,sizeof (data_desc)); +TEST (modify_ldt (1 , &data_desc,sizeof (data_desc))); +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" ); + } }static void try_corrupt_stack (unsigned short *orig_ss) ---base-commit:
b070a5692e035ec12c3d3c7a7e9e97c270fd4d7d
change-id: 20250507-fixes-modify_ldt-ebcfdd2a7d30Best regards,
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 37 lines · Ricardo B. Marlière via ltp
From: Ricardo B. Marlière <[email protected]>
The commit
be0aaca2f742("syscalls/modify_ldt: Addlapi/ldt.h") left behind an important factor ofmodify_ldt(): the kernel intentionally casts the return value to unsigned int. This was handled intestcases/cve/cve-2015-3290.cbut was removed. Add it back to the relevant file.Reported-by Martin Doucha <[email protected]>Signed-off-by Ricardo B. Marlière <[email protected]>---
Changes in v2:
- Added TBROK for any ret != 0 inmodify_ldtcall incve-2015-3290.c
- Link to v1: https://lore.kernel.org/r/[email protected]--- include/lapi/ldt.h | 22 +++++++++++++++++++++- testcases/cve/cve-2015-3290.c | 8 +++++++- 2 files changed, 28 insertions(+), 2 deletions(-)include/lapi/ldt.h +9
-1@@ -31,7 +31,27 @@ struct user_desc {static inline int modify_ldt (int func,const struct user_desc *ptr,unsigned long bytecount) { -return tst_syscall (__NR_modify_ldt, func, ptr, bytecount); +long rval; + + errno =0 ; + rval =tst_syscall (__NR_modify_ldt, func, ptr, bytecount); + +#ifdef __x86_64__ +/* +* The kernel intentionally casts modify_ldt() return value +* to unsigned int to prevent sign extension to 64 bits. This may
include/lapi/ldt.h struct user_desc { · line 52
Quoted 10 lines
include/lapi/ldt.h +10
-0+* result in syscall() returning the value as is instead of setting +* errno and returning -1. +*/ +if (rval >0 && (int )rval <0 ) { +tst_res (TINFO , +"WARNING: Libc mishandled modify_ldt() return value" ); + errno = -(int )errno; + rval =-1 ; + } +#endif /* __x86_64__ */
This code is correct, but check comment below..
Quoted 11 lines
include/lapi/ldt.h +2
-0+ +return rval; } static inline int safe_modify_ldt(const char *file, const int lineno, int func,testcases/cve/cve-2015-3290.c +0
-0@@ -197,7 +197,13 @@ static void set_ldt(void) .useable = 0
testcases/cve/cve-2015-3290.c static void set_ldt(void) · line 206
Quoted 10 lines
testcases/cve/cve-2015-3290.c +7
-1}; -SAFE_MODIFY_LDT (1 , &data_desc,sizeof (data_desc)); +TEST (modify_ldt (1 , &data_desc,sizeof (data_desc))); +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" ); + }
What about handling this in safe_modify_ldt, so all tests using SAFE_MODIFY_LDT() will use it?
Quoted 9 lines
testcases/cve/cve-2015-3290.c +0
-1}static void try_corrupt_stack (unsigned short *orig_ss) ---base-commit:
b070a5692e035ec12c3d3c7a7e9e97c270fd4d7d
change-id: 20250507-fixes-modify_ldt-ebcfdd2a7d30Best regards,
- Andrea
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi,
it turns out that there's a mistake in this patch. See below.
Quoted 34 lines · Ricardo B. Marlière
From: Ricardo B. Marlière <[email protected]>
The commit
be0aaca2f742("syscalls/modify_ldt: Addlapi/ldt.h") left behind an important factor ofmodify_ldt(): the kernel intentionally casts the return value to unsigned int. This was handled intestcases/cve/cve-2015-3290.cbut was removed. Add it back to the relevant file.Reported-by Martin Doucha <[email protected]>Signed-off-by Ricardo B. Marlière <[email protected]>---
Changes in v2:
- Added TBROK for any ret != 0 inmodify_ldtcall incve-2015-3290.c
- Link to v1: https://lore.kernel.org/r/[email protected]--- include/lapi/ldt.h | 22 +++++++++++++++++++++- testcases/cve/cve-2015-3290.c | 8 +++++++- 2 files changed, 28 insertions(+), 2 deletions(-)include/lapi/ldt.h +6
-1@@ -31,7 +31,27 @@ struct user_desc {static inline int modify_ldt (int func,const struct user_desc *ptr,unsigned long bytecount) { -return tst_syscall (__NR_modify_ldt, func, ptr, bytecount); +long rval; + + errno =0 ; + rval =tst_syscall (__NR_modify_ldt, func, ptr, bytecount); + +#ifdef __x86_64__
include/lapi/ldt.h struct user_desc { · line 49
Quoted 10 lines
include/lapi/ldt.h +10
-0+/* +* 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 (rval >0 && (int )rval <0 ) { +tst_res (TINFO , +"WARNING: Libc mishandled modify_ldt() return value" ); + errno = -(int )errno;
I've completely missed that this line is supposed to be:
errno = -(int)rval;
Quoted 33 lines
include/lapi/ldt.h +5
-0+ rval =-1 ; + } +#endif /* __x86_64__ */ + +return rval; } static inline int safe_modify_ldt(const char *file, const int lineno, int func,testcases/cve/cve-2015-3290.c +7
-2@@ -197,7 +197,13 @@ static void set_ldt(void) .useable = 0 }; -SAFE_MODIFY_LDT (1 , &data_desc,sizeof (data_desc)); +TEST (modify_ldt (1 , &data_desc,sizeof (data_desc))); +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" ); + } }static void try_corrupt_stack (unsigned short *orig_ss) ---base-commit:
b070a5692e035ec12c3d3c7a7e9e97c270fd4d7d
change-id: 20250507-fixes-modify_ldt-ebcfdd2a7d30Best regards,
Signature · 10 lines
Martin Doucha [email protected] SW Quality Engineer SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 43 lines · Martin Doucha
Hi,
it turns out that there's a mistake in this patch. See below.Quoted 39 lines · Ricardo B. Marlière
From: Ricardo B. Marlière <[email protected]>
The commit
be0aaca2f742("syscalls/modify_ldt: Addlapi/ldt.h") left behind an important factor ofmodify_ldt(): the kernel intentionally casts the return value to unsigned int. This was handled intestcases/cve/cve-2015-3290.cbut was removed. Add it back to the relevant file.Reported-by Martin Doucha <[email protected]>Signed-off-by Ricardo B. Marlière <[email protected]>---
Changes in v2:
- Added TBROK for any ret != 0 inmodify_ldtcall incve-2015-3290.c
- Link to v1: https://lore.kernel.org/r/[email protected]--- include/lapi/ldt.h | 22 +++++++++++++++++++++- testcases/cve/cve-2015-3290.c | 8 +++++++- 2 files changed, 28 insertions(+), 2 deletions(-)include/lapi/ldt.h +11
-1@@ -31,7 +31,27 @@ struct user_desc {static inline int modify_ldt (int func,const struct user_desc *ptr,unsigned long bytecount) { -return tst_syscall (__NR_modify_ldt, func, ptr, bytecount); +long rval; + + errno =0 ; + rval =tst_syscall (__NR_modify_ldt, func, ptr, bytecount); + +#ifdef __x86_64__ +/* +* 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.
Quoted 8 lines
include/lapi/ldt.h struct user_desc { · line 49
Quoted 5 lines
include/lapi/ldt.h +5
-0+*/ +if (rval >0 && (int )rval <0 ) { +tst_res (TINFO , +"WARNING: Libc mishandled modify_ldt() return value" ); + errno = -(int )errno;I've completely missed that this line is supposed to be:
errno = -(int)rval;
oops. I'm very sorry about that. Will send a fix right now.
Quoted 34 lines
Quoted 34 lines
include/lapi/ldt.h +5
-0+ rval =-1 ; + } +#endif /* __x86_64__ */ + +return rval; } static inline int safe_modify_ldt(const char *file, const int lineno, int func,testcases/cve/cve-2015-3290.c +7
-2@@ -197,7 +197,13 @@ static void set_ldt(void) .useable = 0 }; -SAFE_MODIFY_LDT (1 , &data_desc,sizeof (data_desc)); +TEST (modify_ldt (1 , &data_desc,sizeof (data_desc))); +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" ); + } }static void try_corrupt_stack (unsigned short *orig_ss) ---base-commit:
b070a5692e035ec12c3d3c7a7e9e97c270fd4d7d
change-id: 20250507-fixes-modify_ldt-ebcfdd2a7d30Best regards,
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp