From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751586AbdJRRVJ (ORCPT ); Wed, 18 Oct 2017 13:21:09 -0400 Received: from mga09.intel.com ([134.134.136.24]:22029 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751465AbdJRRVI (ORCPT ); Wed, 18 Oct 2017 13:21:08 -0400 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.43,397,1503385200"; d="scan'208";a="161993855" Subject: [PATCH] [v3] x86, syscalls: use SYSCALL_DEFINE() macros for sys_modify_ldt() To: linux-kernel@vger.kernel.org Cc: Dave Hansen , luto@kernel.org, brgerst@gmail.com, x86@kernel.org From: Dave Hansen Date: Wed, 18 Oct 2017 10:21:07 -0700 Message-Id: <20171018172107.1A79C532@viggo.jf.intel.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Changes from v2: * Fixed UML compile error from not including syscall header Changes from v1: * Added cast to (unsigned int) * Added comments -- From: Dave Hansen 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(). Signed-off-by: Dave Hansen Reviewed-by: Andy Lutomirski Reviewed-by: Brian Gerst Cc: x86@kernel.org --- b/arch/x86/include/asm/syscalls.h | 2 +- b/arch/x86/kernel/ldt.c | 16 +++++++++++++--- b/arch/x86/um/ldt.c | 7 +++++-- 3 files changed, 19 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-18 09:21:54.210460494 -0700 +++ b/arch/x86/include/asm/syscalls.h 2017-10-18 09:21:54.217460834 -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-18 09:21:54.212460591 -0700 +++ b/arch/x86/kernel/ldt.c 2017-10-18 09:21:54.218460882 -0700 @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -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-18 09:21:54.214460688 -0700 +++ b/arch/x86/um/ldt.c 2017-10-18 09:25:55.128131160 -0700 @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -369,7 +370,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); } _