Skip to contentSkip to conversation

x86, syscalls: use SYSCALL_DEFINE() macros for sys_modify_ldt()

RFC PATCH In review Dave Hansen 3 participants 7 messages Oct 13, 2017 – Oct 14, 2017

Subsystems (2)
lore
Share & tools
lore

Dave Hansen

author maintainer
#0 lore raw

RFC PATCH 1/1 x86, syscalls: use SYSCALL_DEFINE() macros for sys_modify_ldt()

I noticed that we don't have tracepoints for sys_modify_ldt(). I think that's because we define it directly instead of using the normal SYSCALL_DEFINEx() macros.

Is there a reason for that, or were they just missed when the macros were created?

Cc Andy Lutomirski <[email protected]>

---

 b/arch/x86/include/asm/syscalls.h |    2 +-
 b/arch/x86/kernel/ldt.c           |    5 +++--
 b/arch/x86/um/ldt.c               |    3 ++-
 3 files changed, 6 insertions(+), 4 deletions(-)
arch/x86/kernel/ldt.c renamed from arch/x86/kernel/ldt.c~x86-syscall-macros-modify_ldt +3 -2
diff -puN arch/x86/kernel/ldt.c~x86-syscall-macros-modify_ldt arch/x86/kernel/ldt.c
@@ -12,6 +12,7 @@
 #include <linux/string.h>
 #include <linux/mm.h>
 #include <linux/smp.h>
+#include <linux/syscalls.h>
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
 #include <linux/uaccess.h>
@@ -294,8 +295,8 @@ out:
 	return error;
 }
 
-asmlinkage int sys_modify_ldt(int func, void __user *ptr,
-			      unsigned long bytecount)
+SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
+		unsigned long , bytecount)
 {
 	int ret = -ENOSYS;
 
arch/x86/include/asm/syscalls.h renamed from arch/x86/include/asm/syscalls.h~x86-syscall-macros-modify_ldt +1 -1
diff -puN arch/x86/include/asm/syscalls.h~x86-syscall-macros-modify_ldt arch/x86/include/asm/syscalls.h
@@ -21,7 +21,7 @@ asmlinkage long sys_ioperm(unsigned long
 asmlinkage long sys_iopl(unsigned int);
 
 /* kernel/ldt.c */
-asmlinkage int sys_modify_ldt(int, void __user *, unsigned long);
+asmlinkage long sys_modify_ldt(int, void __user *, unsigned long);
 
 /* kernel/signal.c */
 asmlinkage long sys_rt_sigreturn(void);
arch/x86/um/ldt.c renamed from arch/x86/um/ldt.c~x86-syscall-macros-modify_ldt +2 -1
diff -puN arch/x86/um/ldt.c~x86-syscall-macros-modify_ldt arch/x86/um/ldt.c
@@ -369,7 +369,8 @@ void free_ldt(struct mm_context *mm)
 	mm->arch.ldt.entry_count = 0;
 }
 
-int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
+SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
+		unsigned long , bytecount)
 {
 	return do_modify_ldt_skas(func, ptr, bytecount);
 }

_

#1 lore raw
Quoted 7 lines · Dave Hansen

I noticed that we don't have tracepoints for sys_modify_ldt(). I think that's because we define it directly instead of using the normal SYSCALL_DEFINEx() macros.

Is there a reason for that, or were they just missed when the macros were created?

No, and it's a longstanding fsckup that I think you can't fix like this because...

Quoted 23 lines
Cc Andy Lutomirski <[email protected]>

---

 b/arch/x86/include/asm/syscalls.h |    2 +-
 b/arch/x86/kernel/ldt.c           |    5 +++--
 b/arch/x86/um/ldt.c               |    3 ++-
 3 files changed, 6 insertions(+), 4 deletions(-)

diff -puN arch/x86/kernel/ldt.c~x86-syscall-macros-modify_ldt arch/x86/kernel/ldt.c

arch/x86/kernel/ldt.c 2017-10-13 13:30:12.817553391 -0700 renamed from arch/x86/kernel/ldt.c~x86-syscall-macros-modify_ldt 2017-10-13 13:30:12.802553391 -0700 +1 -0
@@ -12,6 +12,7 @@
 #include <linux/string.h>
 #include <linux/mm.h>
 #include <linux/smp.h>
+#include <linux/syscalls.h>
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
 #include <linux/uaccess.h>

arch/x86/kernel/ldt.c 2017-10-13 13:30:12.817553391 -0700 out: · line 299

Quoted 7 lines
arch/x86/kernel/ldt.c 2017-10-13 13:30:12.817553391 -0700 renamed from arch/x86/kernel/ldt.c~x86-syscall-macros-modify_ldt 2017-10-13 13:30:12.802553391 -0700 +2 -2
@@ -294,8 +295,8 @@ out:
        return error;
 }
 
-asmlinkage int sys_modify_ldt(int func, void __user *ptr,
-                             unsigned long bytecount)
+SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
+               unsigned long , bytecount)

sys_modify_ldt() returns int, which is wrong, and it's visibly wrong to 64-bit user code. So I think you need to make sure that the return value is cast to int in all cases.

--Andy

author maintainer
#2 lore raw
Quoted 8 lines · Andy Lutomirski

arch/x86/kernel/ldt.c out: · line 299

Quoted 5 lines
arch/x86/kernel/ldt.c renamed from arch/x86/kernel/ldt.c~x86-syscall-macros-modify_ldt +2 -2
-asmlinkage int sys_modify_ldt(int func, void __user *ptr,
-                             unsigned long bytecount)
+SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
+               unsigned long , bytecount)

sys_modify_ldt() returns int, which is wrong, and it's visibly wrong to 64-bit user code. So I think you need to make sure that the return value is cast to int in all cases.

I'm not quite following.

Is there any difference between having something return 'int' and having it return 'long' but only use the lower 32 bits? The caller is surely expecting its result in the lower 32 bits, but this should not change that.

Did you just mean that we need to careful to cast the result in sys_modify_ldt() over to an 'int' before returning it?

#3 lore raw
In reply to Andy Lutomirski
Quoted 39 lines · Andy Lutomirski
Quoted 7 lines · Dave Hansen

I noticed that we don't have tracepoints for sys_modify_ldt(). I think that's because we define it directly instead of using the normal SYSCALL_DEFINEx() macros.

Is there a reason for that, or were they just missed when the macros were created?

No, and it's a longstanding fsckup that I think you can't fix like this because...

Quoted 26 lines
Cc Andy Lutomirski <[email protected]>

---

 b/arch/x86/include/asm/syscalls.h |    2 +-
 b/arch/x86/kernel/ldt.c           |    5 +++--
 b/arch/x86/um/ldt.c               |    3 ++-
 3 files changed, 6 insertions(+), 4 deletions(-)

diff -puN arch/x86/kernel/ldt.c~x86-syscall-macros-modify_ldt arch/x86/kernel/ldt.c

arch/x86/kernel/ldt.c 2017-10-13 13:30:12.817553391 -0700 renamed from arch/x86/kernel/ldt.c~x86-syscall-macros-modify_ldt 2017-10-13 13:30:12.802553391 -0700 +1 -0
@@ -12,6 +12,7 @@
 #include <linux/string.h>
 #include <linux/mm.h>
 #include <linux/smp.h>
+#include <linux/syscalls.h>
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
 #include <linux/uaccess.h>
@@ -294,8 +295,8 @@ out:
        return error;
 }
 
Quoted 8 lines

arch/x86/kernel/ldt.c 2017-10-13 13:30:12.817553391 -0700 out: · line 299

Quoted 4 lines
arch/x86/kernel/ldt.c 2017-10-13 13:30:12.817553391 -0700 renamed from arch/x86/kernel/ldt.c~x86-syscall-macros-modify_ldt 2017-10-13 13:30:12.802553391 -0700 +2 -2
-asmlinkage int sys_modify_ldt(int func, void __user *ptr,
-                             unsigned long bytecount)
+SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
+               unsigned long , bytecount)

sys_modify_ldt() returns int, which is wrong, and it's visibly wrong to 64-bit user code. So I think you need to make sure that the return value is cast to int in all cases.

I don't think there will be a problem here. If 64-bit userspace treats it as an int, it will truncate to 32-bit signed and all is well. If it is treating it as a long, then it's currently broken for errors anyways.

--
Brian Gerst

#4 lore raw
Quoted 49 lines · Brian Gerst
Quoted 39 lines · Andy Lutomirski
Quoted 7 lines · Dave Hansen

I noticed that we don't have tracepoints for sys_modify_ldt(). I think that's because we define it directly instead of using the normal SYSCALL_DEFINEx() macros.

Is there a reason for that, or were they just missed when the macros were created?

No, and it's a longstanding fsckup that I think you can't fix like this because...

Quoted 26 lines
Cc Andy Lutomirski <[email protected]>

---

 b/arch/x86/include/asm/syscalls.h |    2 +-
 b/arch/x86/kernel/ldt.c           |    5 +++--
 b/arch/x86/um/ldt.c               |    3 ++-
 3 files changed, 6 insertions(+), 4 deletions(-)

diff -puN arch/x86/kernel/ldt.c~x86-syscall-macros-modify_ldt arch/x86/kernel/ldt.c

arch/x86/kernel/ldt.c 2017-10-13 13:30:12.817553391 -0700 renamed from arch/x86/kernel/ldt.c~x86-syscall-macros-modify_ldt 2017-10-13 13:30:12.802553391 -0700 +1 -0
@@ -12,6 +12,7 @@
 #include <linux/string.h>
 #include <linux/mm.h>
 #include <linux/smp.h>
+#include <linux/syscalls.h>
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
 #include <linux/uaccess.h>
@@ -294,8 +295,8 @@ out:
        return error;
 }
 
Quoted 8 lines

arch/x86/kernel/ldt.c 2017-10-13 13:30:12.817553391 -0700 out: · line 299

Quoted 4 lines
arch/x86/kernel/ldt.c 2017-10-13 13:30:12.817553391 -0700 renamed from arch/x86/kernel/ldt.c~x86-syscall-macros-modify_ldt 2017-10-13 13:30:12.802553391 -0700 +2 -2
-asmlinkage int sys_modify_ldt(int func, void __user *ptr,
-                             unsigned long bytecount)
+SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
+               unsigned long , bytecount)

sys_modify_ldt() returns int, which is wrong, and it's visibly wrong to 64-bit user code. So I think you need to make sure that the return value is cast to int in all cases.

Quoted 5 lines

I don't think there will be a problem here. If 64-bit userspace treats it as an int, it will truncate to 32-bit signed and all is well. If it is treating it as a long, then it's currently broken for errors anyways.

Let me say what I mean more clearly:

The current code is buggy: specifically, a 64-bit modify_ldt() call that *fails* will return something like (int)-EFAULT. This is bogus, but it's the ABI. There's even a selftest in the kernel tree that notices this (although it doesn't check it right now). All that needs to happen for this patch to be okay AFAIK is to make sure that we preserve that bug instead of accidentally fixing it.

#5 lore raw
Quoted 56 lines · Andy Lutomirski
Quoted 49 lines · Brian Gerst
Quoted 39 lines · Andy Lutomirski
Quoted 7 lines · Dave Hansen

I noticed that we don't have tracepoints for sys_modify_ldt(). I think that's because we define it directly instead of using the normal SYSCALL_DEFINEx() macros.

Is there a reason for that, or were they just missed when the macros were created?

No, and it's a longstanding fsckup that I think you can't fix like this because...

Quoted 26 lines
Cc Andy Lutomirski <[email protected]>

---

 b/arch/x86/include/asm/syscalls.h |    2 +-
 b/arch/x86/kernel/ldt.c           |    5 +++--
 b/arch/x86/um/ldt.c               |    3 ++-
 3 files changed, 6 insertions(+), 4 deletions(-)

diff -puN arch/x86/kernel/ldt.c~x86-syscall-macros-modify_ldt arch/x86/kernel/ldt.c

arch/x86/kernel/ldt.c 2017-10-13 13:30:12.817553391 -0700 renamed from arch/x86/kernel/ldt.c~x86-syscall-macros-modify_ldt 2017-10-13 13:30:12.802553391 -0700 +1 -0
@@ -12,6 +12,7 @@
 #include <linux/string.h>
 #include <linux/mm.h>
 #include <linux/smp.h>
+#include <linux/syscalls.h>
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
 #include <linux/uaccess.h>
@@ -294,8 +295,8 @@ out:
        return error;
 }
 
Quoted 8 lines

arch/x86/kernel/ldt.c 2017-10-13 13:30:12.817553391 -0700 out: · line 299

Quoted 4 lines
arch/x86/kernel/ldt.c 2017-10-13 13:30:12.817553391 -0700 renamed from arch/x86/kernel/ldt.c~x86-syscall-macros-modify_ldt 2017-10-13 13:30:12.802553391 -0700 +2 -2
-asmlinkage int sys_modify_ldt(int func, void __user *ptr,
-                             unsigned long bytecount)
+SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
+               unsigned long , bytecount)

sys_modify_ldt() returns int, which is wrong, and it's visibly wrong to 64-bit user code. So I think you need to make sure that the return value is cast to int in all cases.

Quoted 5 lines

I don't think there will be a problem here. If 64-bit userspace treats it as an int, it will truncate to 32-bit signed and all is well. If it is treating it as a long, then it's currently broken for errors anyways.

Quoted 8 lines

Let me say what I mean more clearly:

The current code is buggy: specifically, a 64-bit modify_ldt() call that *fails* will return something like (int)-EFAULT. This is bogus, but it's the ABI. There's even a selftest in the kernel tree that notices this (although it doesn't check it right now). All that needs to happen for this patch to be okay AFAIK is to make sure that we preserve that bug instead of accidentally fixing it.

return (unsigned int)ret;

Problem solved.

--
Brian Gerst

#6 lore raw
Quoted 66 lines · Brian Gerst
Quoted 56 lines · Andy Lutomirski
Quoted 49 lines · Brian Gerst
Quoted 39 lines · Andy Lutomirski
Quoted 7 lines · Dave Hansen

I noticed that we don't have tracepoints for sys_modify_ldt(). I think that's because we define it directly instead of using the normal SYSCALL_DEFINEx() macros.

Is there a reason for that, or were they just missed when the macros were created?

No, and it's a longstanding fsckup that I think you can't fix like this because...

Quoted 26 lines
Cc Andy Lutomirski <[email protected]>

---

 b/arch/x86/include/asm/syscalls.h |    2 +-
 b/arch/x86/kernel/ldt.c           |    5 +++--
 b/arch/x86/um/ldt.c               |    3 ++-
 3 files changed, 6 insertions(+), 4 deletions(-)

diff -puN arch/x86/kernel/ldt.c~x86-syscall-macros-modify_ldt arch/x86/kernel/ldt.c

arch/x86/kernel/ldt.c 2017-10-13 13:30:12.817553391 -0700 renamed from arch/x86/kernel/ldt.c~x86-syscall-macros-modify_ldt 2017-10-13 13:30:12.802553391 -0700 +1 -0
@@ -12,6 +12,7 @@
 #include <linux/string.h>
 #include <linux/mm.h>
 #include <linux/smp.h>
+#include <linux/syscalls.h>
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
 #include <linux/uaccess.h>
@@ -294,8 +295,8 @@ out:
        return error;
 }
 
Quoted 8 lines

arch/x86/kernel/ldt.c 2017-10-13 13:30:12.817553391 -0700 out: · line 299

Quoted 4 lines
arch/x86/kernel/ldt.c 2017-10-13 13:30:12.817553391 -0700 renamed from arch/x86/kernel/ldt.c~x86-syscall-macros-modify_ldt 2017-10-13 13:30:12.802553391 -0700 +2 -2
-asmlinkage int sys_modify_ldt(int func, void __user *ptr,
-                             unsigned long bytecount)
+SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
+               unsigned long , bytecount)

sys_modify_ldt() returns int, which is wrong, and it's visibly wrong to 64-bit user code. So I think you need to make sure that the return value is cast to int in all cases.

Quoted 5 lines

I don't think there will be a problem here. If 64-bit userspace treats it as an int, it will truncate to 32-bit signed and all is well. If it is treating it as a long, then it's currently broken for errors anyways.

Quoted 8 lines

Let me say what I mean more clearly:

The current code is buggy: specifically, a 64-bit modify_ldt() call that *fails* will return something like (int)-EFAULT. This is bogus, but it's the ABI. There's even a selftest in the kernel tree that notices this (although it doesn't check it right now). All that needs to happen for this patch to be okay AFAIK is to make sure that we preserve that bug instead of accidentally fixing it.

Quoted 3 lines

return (unsigned int)ret;

Problem solved.

Agreed. But probably with a comment.

Quoted 3 lines

--
Brian Gerst