0073-x86-entry-Use-SYSCALL_DEFINE-macros-for-sys_modify_l.patch 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. From 1e3688f9e76b3d8b218ed1afa292585a91b0b0c6 Mon Sep 17 00:00:00 2001
  2. From: Dave Hansen <[email protected]>
  3. Date: Wed, 18 Oct 2017 10:21:07 -0700
  4. Subject: [PATCH 073/232] x86/entry: Use SYSCALL_DEFINE() macros for
  5. sys_modify_ldt()
  6. MIME-Version: 1.0
  7. Content-Type: text/plain; charset=UTF-8
  8. Content-Transfer-Encoding: 8bit
  9. CVE-2017-5754
  10. We do not have tracepoints for sys_modify_ldt() because we define
  11. it directly instead of using the normal SYSCALL_DEFINEx() macros.
  12. However, there is a reason sys_modify_ldt() does not use the macros:
  13. it has an 'int' return type instead of 'unsigned long'. This is
  14. a bug, but it's a bug cemented in the ABI.
  15. What does this mean? If we return -EINVAL from a function that
  16. returns 'int', we have 0x00000000ffffffea in %rax. But, if we
  17. return -EINVAL from a function returning 'unsigned long', we end
  18. up with 0xffffffffffffffea in %rax, which is wrong.
  19. To work around this and maintain the 'int' behavior while using
  20. the SYSCALL_DEFINEx() macros, so we add a cast to 'unsigned int'
  21. in both implementations of sys_modify_ldt().
  22. Signed-off-by: Dave Hansen <[email protected]>
  23. Reviewed-by: Andy Lutomirski <[email protected]>
  24. Reviewed-by: Brian Gerst <[email protected]>
  25. Cc: Linus Torvalds <[email protected]>
  26. Cc: Peter Zijlstra <[email protected]>
  27. Cc: Thomas Gleixner <[email protected]>
  28. Link: http://lkml.kernel.org/r/[email protected]
  29. Signed-off-by: Ingo Molnar <[email protected]>
  30. (cherry picked from commit da20ab35180780e4a6eadc804544f1fa967f3567)
  31. Signed-off-by: Andy Whitcroft <[email protected]>
  32. Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
  33. (cherry picked from commit d865f635f4b2c3307e79de9be5c49ea8bd4c43a6)
  34. Signed-off-by: Fabian Grünbichler <[email protected]>
  35. ---
  36. arch/x86/include/asm/syscalls.h | 2 +-
  37. arch/x86/kernel/ldt.c | 16 +++++++++++++---
  38. arch/x86/um/ldt.c | 7 +++++--
  39. 3 files changed, 19 insertions(+), 6 deletions(-)
  40. diff --git a/arch/x86/include/asm/syscalls.h b/arch/x86/include/asm/syscalls.h
  41. index 91dfcafe27a6..bad25bb80679 100644
  42. --- a/arch/x86/include/asm/syscalls.h
  43. +++ b/arch/x86/include/asm/syscalls.h
  44. @@ -21,7 +21,7 @@ asmlinkage long sys_ioperm(unsigned long, unsigned long, int);
  45. asmlinkage long sys_iopl(unsigned int);
  46. /* kernel/ldt.c */
  47. -asmlinkage int sys_modify_ldt(int, void __user *, unsigned long);
  48. +asmlinkage long sys_modify_ldt(int, void __user *, unsigned long);
  49. /* kernel/signal.c */
  50. asmlinkage long sys_rt_sigreturn(void);
  51. diff --git a/arch/x86/kernel/ldt.c b/arch/x86/kernel/ldt.c
  52. index f0e64db18ac8..0402d44deb4d 100644
  53. --- a/arch/x86/kernel/ldt.c
  54. +++ b/arch/x86/kernel/ldt.c
  55. @@ -12,6 +12,7 @@
  56. #include <linux/string.h>
  57. #include <linux/mm.h>
  58. #include <linux/smp.h>
  59. +#include <linux/syscalls.h>
  60. #include <linux/slab.h>
  61. #include <linux/vmalloc.h>
  62. #include <linux/uaccess.h>
  63. @@ -294,8 +295,8 @@ static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
  64. return error;
  65. }
  66. -asmlinkage int sys_modify_ldt(int func, void __user *ptr,
  67. - unsigned long bytecount)
  68. +SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
  69. + unsigned long , bytecount)
  70. {
  71. int ret = -ENOSYS;
  72. @@ -313,5 +314,14 @@ asmlinkage int sys_modify_ldt(int func, void __user *ptr,
  73. ret = write_ldt(ptr, bytecount, 0);
  74. break;
  75. }
  76. - return ret;
  77. + /*
  78. + * The SYSCALL_DEFINE() macros give us an 'unsigned long'
  79. + * return type, but tht ABI for sys_modify_ldt() expects
  80. + * 'int'. This cast gives us an int-sized value in %rax
  81. + * for the return code. The 'unsigned' is necessary so
  82. + * the compiler does not try to sign-extend the negative
  83. + * return codes into the high half of the register when
  84. + * taking the value from int->long.
  85. + */
  86. + return (unsigned int)ret;
  87. }
  88. diff --git a/arch/x86/um/ldt.c b/arch/x86/um/ldt.c
  89. index 836a1eb5df43..3ee234b6234d 100644
  90. --- a/arch/x86/um/ldt.c
  91. +++ b/arch/x86/um/ldt.c
  92. @@ -6,6 +6,7 @@
  93. #include <linux/mm.h>
  94. #include <linux/sched.h>
  95. #include <linux/slab.h>
  96. +#include <linux/syscalls.h>
  97. #include <linux/uaccess.h>
  98. #include <asm/unistd.h>
  99. #include <os.h>
  100. @@ -369,7 +370,9 @@ void free_ldt(struct mm_context *mm)
  101. mm->arch.ldt.entry_count = 0;
  102. }
  103. -int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
  104. +SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
  105. + unsigned long , bytecount)
  106. {
  107. - return do_modify_ldt_skas(func, ptr, bytecount);
  108. + /* See non-um modify_ldt() for why we do this cast */
  109. + return (unsigned int)do_modify_ldt_skas(func, ptr, bytecount);
  110. }
  111. --
  112. 2.14.2