0114-selftests-x86-ldt_gdt-Add-infrastructure-to-test-set.patch 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. From 5f1c500617cd1a6f629237471344ee200debaa60 Mon Sep 17 00:00:00 2001
  2. From: Andy Lutomirski <[email protected]>
  3. Date: Sat, 4 Nov 2017 04:19:50 -0700
  4. Subject: [PATCH 114/242] selftests/x86/ldt_gdt: Add infrastructure to test
  5. set_thread_area()
  6. MIME-Version: 1.0
  7. Content-Type: text/plain; charset=UTF-8
  8. Content-Transfer-Encoding: 8bit
  9. CVE-2017-5754
  10. Much of the test design could apply to set_thread_area() (i.e. GDT),
  11. not just modify_ldt(). Add set_thread_area() to the
  12. install_valid_mode() helper.
  13. Signed-off-by: Andy Lutomirski <[email protected]>
  14. Cc: Borislav Petkov <[email protected]>
  15. Cc: Linus Torvalds <[email protected]>
  16. Cc: Peter Zijlstra <[email protected]>
  17. Cc: Thomas Gleixner <[email protected]>
  18. Link: http://lkml.kernel.org/r/02c23f8fba5547007f741dc24c3926e5284ede02.1509794321.git.luto@kernel.org
  19. Signed-off-by: Ingo Molnar <[email protected]>
  20. (cherry picked from commit d744dcad39094c9187075e274d1cdef79c57c8b5)
  21. Signed-off-by: Andy Whitcroft <[email protected]>
  22. Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
  23. (cherry picked from commit d6ae7ac5849304e520538a6ce3111f372f809596)
  24. Signed-off-by: Fabian Grünbichler <[email protected]>
  25. ---
  26. tools/testing/selftests/x86/ldt_gdt.c | 53 ++++++++++++++++++++++++-----------
  27. 1 file changed, 37 insertions(+), 16 deletions(-)
  28. diff --git a/tools/testing/selftests/x86/ldt_gdt.c b/tools/testing/selftests/x86/ldt_gdt.c
  29. index b2c54f4673f2..337f217d0ae9 100644
  30. --- a/tools/testing/selftests/x86/ldt_gdt.c
  31. +++ b/tools/testing/selftests/x86/ldt_gdt.c
  32. @@ -136,30 +136,51 @@ static void check_valid_segment(uint16_t index, int ldt,
  33. }
  34. }
  35. -static bool install_valid_mode(const struct user_desc *desc, uint32_t ar,
  36. - bool oldmode)
  37. +static bool install_valid_mode(const struct user_desc *d, uint32_t ar,
  38. + bool oldmode, bool ldt)
  39. {
  40. - int ret = syscall(SYS_modify_ldt, oldmode ? 1 : 0x11,
  41. - desc, sizeof(*desc));
  42. - if (ret < -1)
  43. - errno = -ret;
  44. + struct user_desc desc = *d;
  45. + int ret;
  46. +
  47. + if (!ldt) {
  48. +#ifndef __i386__
  49. + /* No point testing set_thread_area in a 64-bit build */
  50. + return false;
  51. +#endif
  52. + if (!gdt_entry_num)
  53. + return false;
  54. + desc.entry_number = gdt_entry_num;
  55. +
  56. + ret = syscall(SYS_set_thread_area, &desc);
  57. + } else {
  58. + ret = syscall(SYS_modify_ldt, oldmode ? 1 : 0x11,
  59. + &desc, sizeof(desc));
  60. +
  61. + if (ret < -1)
  62. + errno = -ret;
  63. +
  64. + if (ret != 0 && errno == ENOSYS) {
  65. + printf("[OK]\tmodify_ldt returned -ENOSYS\n");
  66. + return false;
  67. + }
  68. + }
  69. +
  70. if (ret == 0) {
  71. - uint32_t limit = desc->limit;
  72. - if (desc->limit_in_pages)
  73. + uint32_t limit = desc.limit;
  74. + if (desc.limit_in_pages)
  75. limit = (limit << 12) + 4095;
  76. - check_valid_segment(desc->entry_number, 1, ar, limit, true);
  77. + check_valid_segment(desc.entry_number, ldt, ar, limit, true);
  78. return true;
  79. - } else if (errno == ENOSYS) {
  80. - printf("[OK]\tmodify_ldt returned -ENOSYS\n");
  81. - return false;
  82. } else {
  83. - if (desc->seg_32bit) {
  84. - printf("[FAIL]\tUnexpected modify_ldt failure %d\n",
  85. + if (desc.seg_32bit) {
  86. + printf("[FAIL]\tUnexpected %s failure %d\n",
  87. + ldt ? "modify_ldt" : "set_thread_area",
  88. errno);
  89. nerrs++;
  90. return false;
  91. } else {
  92. - printf("[OK]\tmodify_ldt rejected 16 bit segment\n");
  93. + printf("[OK]\t%s rejected 16 bit segment\n",
  94. + ldt ? "modify_ldt" : "set_thread_area");
  95. return false;
  96. }
  97. }
  98. @@ -167,7 +188,7 @@ static bool install_valid_mode(const struct user_desc *desc, uint32_t ar,
  99. static bool install_valid(const struct user_desc *desc, uint32_t ar)
  100. {
  101. - return install_valid_mode(desc, ar, false);
  102. + return install_valid_mode(desc, ar, false, true);
  103. }
  104. static void install_invalid(const struct user_desc *desc, bool oldmode)
  105. --
  106. 2.14.2