096-mips-math-emu-Write-protect-delay-slot-emulation-pages.patch 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. From adcc81f148d733b7e8e641300c5590a2cdc13bf3 Mon Sep 17 00:00:00 2001
  2. From: Paul Burton <[email protected]>
  3. Date: Thu, 20 Dec 2018 17:45:43 +0000
  4. Subject: MIPS: math-emu: Write-protect delay slot emulation pages
  5. Mapping the delay slot emulation page as both writeable & executable
  6. presents a security risk, in that if an exploit can write to & jump into
  7. the page then it can be used as an easy way to execute arbitrary code.
  8. Prevent this by mapping the page read-only for userland, and using
  9. access_process_vm() with the FOLL_FORCE flag to write to it from
  10. mips_dsemul().
  11. This will likely be less efficient due to copy_to_user_page() performing
  12. cache maintenance on a whole page, rather than a single line as in the
  13. previous use of flush_cache_sigtramp(). However this delay slot
  14. emulation code ought not to be running in any performance critical paths
  15. anyway so this isn't really a problem, and we can probably do better in
  16. copy_to_user_page() anyway in future.
  17. A major advantage of this approach is that the fix is small & simple to
  18. backport to stable kernels.
  19. Reported-by: Andy Lutomirski <[email protected]>
  20. Signed-off-by: Paul Burton <[email protected]>
  21. Fixes: 432c6bacbd0c ("MIPS: Use per-mm page to execute branch delay slot instructions")
  22. Cc: [email protected] # v4.8+
  23. Cc: [email protected]
  24. Cc: [email protected]
  25. Cc: Rich Felker <[email protected]>
  26. Cc: David Daney <[email protected]>
  27. ---
  28. arch/mips/kernel/vdso.c | 4 ++--
  29. arch/mips/math-emu/dsemul.c | 38 ++++++++++++++++++++------------------
  30. 2 files changed, 22 insertions(+), 20 deletions(-)
  31. --- a/arch/mips/kernel/vdso.c
  32. +++ b/arch/mips/kernel/vdso.c
  33. @@ -126,8 +126,8 @@ int arch_setup_additional_pages(struct l
  34. /* Map delay slot emulation page */
  35. base = mmap_region(NULL, STACK_TOP, PAGE_SIZE,
  36. - VM_READ|VM_WRITE|VM_EXEC|
  37. - VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
  38. + VM_READ | VM_EXEC |
  39. + VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
  40. 0, NULL);
  41. if (IS_ERR_VALUE(base)) {
  42. ret = base;
  43. --- a/arch/mips/math-emu/dsemul.c
  44. +++ b/arch/mips/math-emu/dsemul.c
  45. @@ -214,8 +214,9 @@ int mips_dsemul(struct pt_regs *regs, mi
  46. {
  47. int isa16 = get_isa16_mode(regs->cp0_epc);
  48. mips_instruction break_math;
  49. - struct emuframe __user *fr;
  50. - int err, fr_idx;
  51. + unsigned long fr_uaddr;
  52. + struct emuframe fr;
  53. + int fr_idx, ret;
  54. /* NOP is easy */
  55. if (ir == 0)
  56. @@ -250,27 +251,31 @@ int mips_dsemul(struct pt_regs *regs, mi
  57. fr_idx = alloc_emuframe();
  58. if (fr_idx == BD_EMUFRAME_NONE)
  59. return SIGBUS;
  60. - fr = &dsemul_page()[fr_idx];
  61. /* Retrieve the appropriately encoded break instruction */
  62. break_math = BREAK_MATH(isa16);
  63. /* Write the instructions to the frame */
  64. if (isa16) {
  65. - err = __put_user(ir >> 16,
  66. - (u16 __user *)(&fr->emul));
  67. - err |= __put_user(ir & 0xffff,
  68. - (u16 __user *)((long)(&fr->emul) + 2));
  69. - err |= __put_user(break_math >> 16,
  70. - (u16 __user *)(&fr->badinst));
  71. - err |= __put_user(break_math & 0xffff,
  72. - (u16 __user *)((long)(&fr->badinst) + 2));
  73. + union mips_instruction _emul = {
  74. + .halfword = { ir >> 16, ir }
  75. + };
  76. + union mips_instruction _badinst = {
  77. + .halfword = { break_math >> 16, break_math }
  78. + };
  79. +
  80. + fr.emul = _emul.word;
  81. + fr.badinst = _badinst.word;
  82. } else {
  83. - err = __put_user(ir, &fr->emul);
  84. - err |= __put_user(break_math, &fr->badinst);
  85. + fr.emul = ir;
  86. + fr.badinst = break_math;
  87. }
  88. - if (unlikely(err)) {
  89. + /* Write the frame to user memory */
  90. + fr_uaddr = (unsigned long)&dsemul_page()[fr_idx];
  91. + ret = access_process_vm(current, fr_uaddr, &fr, sizeof(fr),
  92. + FOLL_FORCE | FOLL_WRITE);
  93. + if (unlikely(ret != sizeof(fr))) {
  94. MIPS_FPU_EMU_INC_STATS(errors);
  95. free_emuframe(fr_idx, current->mm);
  96. return SIGBUS;
  97. @@ -282,10 +287,7 @@ int mips_dsemul(struct pt_regs *regs, mi
  98. atomic_set(&current->thread.bd_emu_frame, fr_idx);
  99. /* Change user register context to execute the frame */
  100. - regs->cp0_epc = (unsigned long)&fr->emul | isa16;
  101. -
  102. - /* Ensure the icache observes our newly written frame */
  103. - flush_cache_sigtramp((unsigned long)&fr->emul);
  104. + regs->cp0_epc = fr_uaddr | isa16;
  105. return 0;
  106. }