public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] x86, syscalls: use SYSCALL_DEFINE() macros for sys_modify_ldt()
@ 2017-10-17 19:22 Dave Hansen
  2017-10-17 22:01 ` Andy Lutomirski
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Dave Hansen @ 2017-10-17 19:22 UTC (permalink / raw)
  To: linux-kernel; +Cc: Dave Hansen, x86, luto, brgerst


We do not have tracepoints for sys_modify_ldt() because we define
it directly instead of using the normal SYSCALL_DEFINEx() macros.

However, there is a reason sys_modify_ldt() does not use the macros:
it has an 'int' return type instead of 'unsigned long'.  This is
a bug, but it's a bug cemented in the ABI.

What does this mean?  If we return -EINVAL from a function that
returns 'int', we have 0x00000000ffffffea in %rax.  But, if we
return -EINVAL from a function returning 'unsigned long', we end
up with 0xffffffffffffffea in %rax, which is wrong.

To work around this and maintain the 'int' behavior while using
the SYSCALL_DEFINEx() macros, so we add a cast to 'unsigned int'
in both implementations of sys_modify_ldt().

Cc: x86@kernel.org
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Brian Gerst <brgerst@gmail.com>

---

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

diff -puN arch/x86/include/asm/syscalls.h~x86-syscall-macros-modify_ldt arch/x86/include/asm/syscalls.h
--- a/arch/x86/include/asm/syscalls.h~x86-syscall-macros-modify_ldt	2017-10-17 10:30:53.032718307 -0700
+++ b/arch/x86/include/asm/syscalls.h	2017-10-17 10:30:53.039718307 -0700
@@ -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);
diff -puN arch/x86/kernel/ldt.c~x86-syscall-macros-modify_ldt arch/x86/kernel/ldt.c
--- a/arch/x86/kernel/ldt.c~x86-syscall-macros-modify_ldt	2017-10-17 10:30:53.034718307 -0700
+++ b/arch/x86/kernel/ldt.c	2017-10-17 12:02:51.137704545 -0700
@@ -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;
 
@@ -313,5 +314,14 @@ asmlinkage int sys_modify_ldt(int func,
 		ret = write_ldt(ptr, bytecount, 0);
 		break;
 	}
-	return ret;
+	/*
+	 * The SYSCALL_DEFINE() macros give us an 'unsigned long'
+	 * return type, but tht ABI for sys_modify_ldt() expects
+	 * 'int'.  This cast gives us an int-sized value in %rax
+	 * for the return code.  The 'unsigned' is necessary so
+	 * the compiler does not try to sign-extend the negative
+	 * return codes into the high half of the register when
+	 * taking the value from int->long.
+	 */
+	return (unsigned int)ret;
 }
diff -puN arch/x86/um/ldt.c~x86-syscall-macros-modify_ldt arch/x86/um/ldt.c
--- a/arch/x86/um/ldt.c~x86-syscall-macros-modify_ldt	2017-10-17 10:30:53.036718307 -0700
+++ b/arch/x86/um/ldt.c	2017-10-17 10:58:58.711714103 -0700
@@ -369,7 +369,9 @@ 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);
+	/* See non-um modify_ldt() for why we do this cast */
+	return (unsigned int)do_modify_ldt_skas(func, ptr, bytecount);
 }
_

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] x86, syscalls: use SYSCALL_DEFINE() macros for sys_modify_ldt()
  2017-10-17 19:22 [PATCH] x86, syscalls: use SYSCALL_DEFINE() macros for sys_modify_ldt() Dave Hansen
@ 2017-10-17 22:01 ` Andy Lutomirski
  2017-10-18 13:17 ` Ingo Molnar
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Andy Lutomirski @ 2017-10-17 22:01 UTC (permalink / raw)
  To: Dave Hansen
  Cc: linux-kernel@vger.kernel.org, X86 ML, Andrew Lutomirski,
	Brian Gerst

On Tue, Oct 17, 2017 at 12:22 PM, Dave Hansen
<dave.hansen@linux.intel.com> wrote:
>
> We do not have tracepoints for sys_modify_ldt() because we define
> it directly instead of using the normal SYSCALL_DEFINEx() macros.
>
> However, there is a reason sys_modify_ldt() does not use the macros:
> it has an 'int' return type instead of 'unsigned long'.  This is
> a bug, but it's a bug cemented in the ABI.
>
> What does this mean?  If we return -EINVAL from a function that
> returns 'int', we have 0x00000000ffffffea in %rax.  But, if we
> return -EINVAL from a function returning 'unsigned long', we end
> up with 0xffffffffffffffea in %rax, which is wrong.
>
> To work around this and maintain the 'int' behavior while using
> the SYSCALL_DEFINEx() macros, so we add a cast to 'unsigned int'
> in both implementations of sys_modify_ldt().

Reviewed-by: Andy Lutomirski <luto@kernel.org>

>
> Cc: x86@kernel.org
> Cc: Andy Lutomirski <luto@kernel.org>
> Cc: Brian Gerst <brgerst@gmail.com>
>
> ---
>
>  b/arch/x86/include/asm/syscalls.h |    2 +-
>  b/arch/x86/kernel/ldt.c           |   16 +++++++++++++---
>  b/arch/x86/um/ldt.c               |    6 ++++--
>  3 files changed, 18 insertions(+), 6 deletions(-)
>
> diff -puN arch/x86/include/asm/syscalls.h~x86-syscall-macros-modify_ldt arch/x86/include/asm/syscalls.h
> --- a/arch/x86/include/asm/syscalls.h~x86-syscall-macros-modify_ldt     2017-10-17 10:30:53.032718307 -0700
> +++ b/arch/x86/include/asm/syscalls.h   2017-10-17 10:30:53.039718307 -0700
> @@ -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);
> diff -puN arch/x86/kernel/ldt.c~x86-syscall-macros-modify_ldt arch/x86/kernel/ldt.c
> --- a/arch/x86/kernel/ldt.c~x86-syscall-macros-modify_ldt       2017-10-17 10:30:53.034718307 -0700
> +++ b/arch/x86/kernel/ldt.c     2017-10-17 12:02:51.137704545 -0700
> @@ -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;
>
> @@ -313,5 +314,14 @@ asmlinkage int sys_modify_ldt(int func,
>                 ret = write_ldt(ptr, bytecount, 0);
>                 break;
>         }
> -       return ret;
> +       /*
> +        * The SYSCALL_DEFINE() macros give us an 'unsigned long'
> +        * return type, but tht ABI for sys_modify_ldt() expects
> +        * 'int'.  This cast gives us an int-sized value in %rax
> +        * for the return code.  The 'unsigned' is necessary so
> +        * the compiler does not try to sign-extend the negative
> +        * return codes into the high half of the register when
> +        * taking the value from int->long.
> +        */
> +       return (unsigned int)ret;
>  }
> diff -puN arch/x86/um/ldt.c~x86-syscall-macros-modify_ldt arch/x86/um/ldt.c
> --- a/arch/x86/um/ldt.c~x86-syscall-macros-modify_ldt   2017-10-17 10:30:53.036718307 -0700
> +++ b/arch/x86/um/ldt.c 2017-10-17 10:58:58.711714103 -0700
> @@ -369,7 +369,9 @@ 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);
> +       /* See non-um modify_ldt() for why we do this cast */
> +       return (unsigned int)do_modify_ldt_skas(func, ptr, bytecount);
>  }
> _

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] x86, syscalls: use SYSCALL_DEFINE() macros for sys_modify_ldt()
  2017-10-17 19:22 [PATCH] x86, syscalls: use SYSCALL_DEFINE() macros for sys_modify_ldt() Dave Hansen
  2017-10-17 22:01 ` Andy Lutomirski
@ 2017-10-18 13:17 ` Ingo Molnar
  2017-10-18 14:12   ` Dave Hansen
  2017-10-18 14:30 ` Brian Gerst
  2017-10-18 16:18 ` Ingo Molnar
  3 siblings, 1 reply; 6+ messages in thread
From: Ingo Molnar @ 2017-10-18 13:17 UTC (permalink / raw)
  To: Dave Hansen; +Cc: linux-kernel, x86, luto, brgerst


* Dave Hansen <dave.hansen@linux.intel.com> wrote:

> 
> We do not have tracepoints for sys_modify_ldt() because we define
> it directly instead of using the normal SYSCALL_DEFINEx() macros.
> 
> However, there is a reason sys_modify_ldt() does not use the macros:
> it has an 'int' return type instead of 'unsigned long'.  This is
> a bug, but it's a bug cemented in the ABI.
> 
> What does this mean?  If we return -EINVAL from a function that
> returns 'int', we have 0x00000000ffffffea in %rax.  But, if we
> return -EINVAL from a function returning 'unsigned long', we end
> up with 0xffffffffffffffea in %rax, which is wrong.
> 
> To work around this and maintain the 'int' behavior while using
> the SYSCALL_DEFINEx() macros, so we add a cast to 'unsigned int'
> in both implementations of sys_modify_ldt().
> 
> Cc: x86@kernel.org
> Cc: Andy Lutomirski <luto@kernel.org>
> Cc: Brian Gerst <brgerst@gmail.com>

I have added your:

  Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>

let me know if that's OK.

Thanks,

	Ingo

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] x86, syscalls: use SYSCALL_DEFINE() macros for sys_modify_ldt()
  2017-10-18 13:17 ` Ingo Molnar
@ 2017-10-18 14:12   ` Dave Hansen
  0 siblings, 0 replies; 6+ messages in thread
From: Dave Hansen @ 2017-10-18 14:12 UTC (permalink / raw)
  To: Ingo Molnar; +Cc: linux-kernel, x86, luto, brgerst

On 10/18/2017 06:17 AM, Ingo Molnar wrote:
> I have added your:
> 
>   Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
> 
> let me know if that's OK.

Yes, that's OK.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] x86, syscalls: use SYSCALL_DEFINE() macros for sys_modify_ldt()
  2017-10-17 19:22 [PATCH] x86, syscalls: use SYSCALL_DEFINE() macros for sys_modify_ldt() Dave Hansen
  2017-10-17 22:01 ` Andy Lutomirski
  2017-10-18 13:17 ` Ingo Molnar
@ 2017-10-18 14:30 ` Brian Gerst
  2017-10-18 16:18 ` Ingo Molnar
  3 siblings, 0 replies; 6+ messages in thread
From: Brian Gerst @ 2017-10-18 14:30 UTC (permalink / raw)
  To: Dave Hansen
  Cc: Linux Kernel Mailing List, the arch/x86 maintainers,
	Andy Lutomirski

On Tue, Oct 17, 2017 at 12:22 PM, Dave Hansen
<dave.hansen@linux.intel.com> wrote:
>
> We do not have tracepoints for sys_modify_ldt() because we define
> it directly instead of using the normal SYSCALL_DEFINEx() macros.
>
> However, there is a reason sys_modify_ldt() does not use the macros:
> it has an 'int' return type instead of 'unsigned long'.  This is
> a bug, but it's a bug cemented in the ABI.
>
> What does this mean?  If we return -EINVAL from a function that
> returns 'int', we have 0x00000000ffffffea in %rax.  But, if we
> return -EINVAL from a function returning 'unsigned long', we end
> up with 0xffffffffffffffea in %rax, which is wrong.
>
> To work around this and maintain the 'int' behavior while using
> the SYSCALL_DEFINEx() macros, so we add a cast to 'unsigned int'
> in both implementations of sys_modify_ldt().

Reviewed-by: Brian Gerst <brgerst@gmail.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] x86, syscalls: use SYSCALL_DEFINE() macros for sys_modify_ldt()
  2017-10-17 19:22 [PATCH] x86, syscalls: use SYSCALL_DEFINE() macros for sys_modify_ldt() Dave Hansen
                   ` (2 preceding siblings ...)
  2017-10-18 14:30 ` Brian Gerst
@ 2017-10-18 16:18 ` Ingo Molnar
  3 siblings, 0 replies; 6+ messages in thread
From: Ingo Molnar @ 2017-10-18 16:18 UTC (permalink / raw)
  To: Dave Hansen; +Cc: linux-kernel, x86, luto, brgerst


* Dave Hansen <dave.hansen@linux.intel.com> wrote:

> 
> We do not have tracepoints for sys_modify_ldt() because we define
> it directly instead of using the normal SYSCALL_DEFINEx() macros.
> 
> However, there is a reason sys_modify_ldt() does not use the macros:
> it has an 'int' return type instead of 'unsigned long'.  This is
> a bug, but it's a bug cemented in the ABI.
> 
> What does this mean?  If we return -EINVAL from a function that
> returns 'int', we have 0x00000000ffffffea in %rax.  But, if we
> return -EINVAL from a function returning 'unsigned long', we end
> up with 0xffffffffffffffea in %rax, which is wrong.
> 
> To work around this and maintain the 'int' behavior while using
> the SYSCALL_DEFINEx() macros, so we add a cast to 'unsigned int'
> in both implementations of sys_modify_ldt().
> 
> Cc: x86@kernel.org
> Cc: Andy Lutomirski <luto@kernel.org>
> Cc: Brian Gerst <brgerst@gmail.com>
> 
> ---
> 
>  b/arch/x86/include/asm/syscalls.h |    2 +-
>  b/arch/x86/kernel/ldt.c           |   16 +++++++++++++---
>  b/arch/x86/um/ldt.c               |    6 ++++--
>  3 files changed, 18 insertions(+), 6 deletions(-)

Fails to build on UML:

/home/mingo/tip/arch/x86/um/ldt.c:372:29: error: expected ‘)’ before ‘int’
 SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
                             ^
/home/mingo/tip/arch/x86/um/ldt.c:206:13: warning: ‘do_modify_ldt_skas’ defined 
but not used [-Wunused-function]
 static long do_modify_ldt_skas(int func, void __user *ptr,

etc.

Thanks,

	Ingo

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2017-10-18 16:18 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-10-17 19:22 [PATCH] x86, syscalls: use SYSCALL_DEFINE() macros for sys_modify_ldt() Dave Hansen
2017-10-17 22:01 ` Andy Lutomirski
2017-10-18 13:17 ` Ingo Molnar
2017-10-18 14:12   ` Dave Hansen
2017-10-18 14:30 ` Brian Gerst
2017-10-18 16:18 ` Ingo Molnar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox