0221-x86-ldt-Plug-memory-leak-in-error-path.patch 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. From f7b3a0038fd5bdc21d05f09002e16db3ea8e6e3b Mon Sep 17 00:00:00 2001
  2. From: Thomas Gleixner <[email protected]>
  3. Date: Sun, 31 Dec 2017 11:24:34 +0100
  4. Subject: [PATCH 221/242] x86/ldt: Plug memory leak in error path
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. CVE-2017-5754
  9. The error path in write_ldt() tries to free 'old_ldt' instead of the newly
  10. allocated 'new_ldt', resulting in a memory leak. It also misses to clean up a
  11. half populated LDT pagetable, which is not a leak as it gets cleaned up
  12. when the process exits.
  13. Free both the potentially half populated LDT pagetable and the newly
  14. allocated LDT struct. This can be done unconditionally because once an LDT
  15. is mapped subsequent maps will succeed, because the PTE page is already
  16. populated and the two LDTs fit into that single page.
  17. Reported-by: Mathieu Desnoyers <[email protected]>
  18. Signed-off-by: Thomas Gleixner <[email protected]>
  19. Cc: Andy Lutomirski <[email protected]>
  20. Cc: Borislav Petkov <[email protected]>
  21. Cc: Dave Hansen <[email protected]>
  22. Cc: Dominik Brodowski <[email protected]>
  23. Cc: Linus Torvalds <[email protected]>
  24. Cc: Linus Torvalds <[email protected]>
  25. Cc: Peter Zijlstra <[email protected]>
  26. Fixes: f55f0501cbf6 ("x86/pti: Put the LDT in its own PGD if PTI is on")
  27. Link: http://lkml.kernel.org/r/alpine.DEB.2.20.1712311121340.1899@nanos
  28. Signed-off-by: Ingo Molnar <[email protected]>
  29. (cherry picked from commit a62d69857aab4caa43049e72fe0ed5c4a60518dd)
  30. Signed-off-by: Andy Whitcroft <[email protected]>
  31. Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
  32. (cherry picked from commit 03d02494f6253d0bdca7254d85e50786448c14f9)
  33. Signed-off-by: Fabian Grünbichler <[email protected]>
  34. ---
  35. arch/x86/kernel/ldt.c | 8 +++++++-
  36. 1 file changed, 7 insertions(+), 1 deletion(-)
  37. diff --git a/arch/x86/kernel/ldt.c b/arch/x86/kernel/ldt.c
  38. index 2260eb6e2de7..9a35b7e541bc 100644
  39. --- a/arch/x86/kernel/ldt.c
  40. +++ b/arch/x86/kernel/ldt.c
  41. @@ -420,7 +420,13 @@ static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
  42. */
  43. error = map_ldt_struct(mm, new_ldt, old_ldt ? !old_ldt->slot : 0);
  44. if (error) {
  45. - free_ldt_struct(old_ldt);
  46. + /*
  47. + * This only can fail for the first LDT setup. If an LDT is
  48. + * already installed then the PTE page is already
  49. + * populated. Mop up a half populated page table.
  50. + */
  51. + free_ldt_pgtables(mm);
  52. + free_ldt_struct(new_ldt);
  53. goto out_unlock;
  54. }
  55. --
  56. 2.14.2