ldt_gdt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ldt_gdt.c - Test cases for LDT and GDT access
  4. * Copyright (c) 2015 Andrew Lutomirski
  5. */
  6. #define _GNU_SOURCE
  7. #include <err.h>
  8. #include <stdio.h>
  9. #include <stdint.h>
  10. #include <signal.h>
  11. #include <setjmp.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <errno.h>
  15. #include <unistd.h>
  16. #include <sys/syscall.h>
  17. #include <asm/ldt.h>
  18. #include <sys/types.h>
  19. #include <sys/wait.h>
  20. #include <stdbool.h>
  21. #include <pthread.h>
  22. #include <sched.h>
  23. #include <linux/futex.h>
  24. #include <sys/mman.h>
  25. #include <asm/prctl.h>
  26. #include <sys/prctl.h>
  27. #include "helpers.h"
  28. #define AR_ACCESSED (1<<8)
  29. #define AR_TYPE_RODATA (0 * (1<<9))
  30. #define AR_TYPE_RWDATA (1 * (1<<9))
  31. #define AR_TYPE_RODATA_EXPDOWN (2 * (1<<9))
  32. #define AR_TYPE_RWDATA_EXPDOWN (3 * (1<<9))
  33. #define AR_TYPE_XOCODE (4 * (1<<9))
  34. #define AR_TYPE_XRCODE (5 * (1<<9))
  35. #define AR_TYPE_XOCODE_CONF (6 * (1<<9))
  36. #define AR_TYPE_XRCODE_CONF (7 * (1<<9))
  37. #define AR_DPL3 (3 * (1<<13))
  38. #define AR_S (1 << 12)
  39. #define AR_P (1 << 15)
  40. #define AR_AVL (1 << 20)
  41. #define AR_L (1 << 21)
  42. #define AR_DB (1 << 22)
  43. #define AR_G (1 << 23)
  44. #ifdef __x86_64__
  45. # define INT80_CLOBBERS "r8", "r9", "r10", "r11"
  46. #else
  47. # define INT80_CLOBBERS
  48. #endif
  49. static int nerrs;
  50. /* Points to an array of 1024 ints, each holding its own index. */
  51. static const unsigned int *counter_page;
  52. static struct user_desc *low_user_desc;
  53. static struct user_desc *low_user_desc_clear; /* Use to delete GDT entry */
  54. static int gdt_entry_num;
  55. static void check_invalid_segment(uint16_t index, int ldt)
  56. {
  57. uint32_t has_limit = 0, has_ar = 0, limit, ar;
  58. uint32_t selector = (index << 3) | (ldt << 2) | 3;
  59. asm ("lsl %[selector], %[limit]\n\t"
  60. "jnz 1f\n\t"
  61. "movl $1, %[has_limit]\n\t"
  62. "1:"
  63. : [limit] "=r" (limit), [has_limit] "+rm" (has_limit)
  64. : [selector] "r" (selector));
  65. asm ("larl %[selector], %[ar]\n\t"
  66. "jnz 1f\n\t"
  67. "movl $1, %[has_ar]\n\t"
  68. "1:"
  69. : [ar] "=r" (ar), [has_ar] "+rm" (has_ar)
  70. : [selector] "r" (selector));
  71. if (has_limit || has_ar) {
  72. printf("[FAIL]\t%s entry %hu is valid but should be invalid\n",
  73. (ldt ? "LDT" : "GDT"), index);
  74. nerrs++;
  75. } else {
  76. printf("[OK]\t%s entry %hu is invalid\n",
  77. (ldt ? "LDT" : "GDT"), index);
  78. }
  79. }
  80. static void check_valid_segment(uint16_t index, int ldt,
  81. uint32_t expected_ar, uint32_t expected_limit,
  82. bool verbose)
  83. {
  84. uint32_t has_limit = 0, has_ar = 0, limit, ar;
  85. uint32_t selector = (index << 3) | (ldt << 2) | 3;
  86. asm ("lsl %[selector], %[limit]\n\t"
  87. "jnz 1f\n\t"
  88. "movl $1, %[has_limit]\n\t"
  89. "1:"
  90. : [limit] "=r" (limit), [has_limit] "+rm" (has_limit)
  91. : [selector] "r" (selector));
  92. asm ("larl %[selector], %[ar]\n\t"
  93. "jnz 1f\n\t"
  94. "movl $1, %[has_ar]\n\t"
  95. "1:"
  96. : [ar] "=r" (ar), [has_ar] "+rm" (has_ar)
  97. : [selector] "r" (selector));
  98. if (!has_limit || !has_ar) {
  99. printf("[FAIL]\t%s entry %hu is invalid but should be valid\n",
  100. (ldt ? "LDT" : "GDT"), index);
  101. nerrs++;
  102. return;
  103. }
  104. /* The SDM says "bits 19:16 are undefined". Thanks. */
  105. ar &= ~0xF0000;
  106. /*
  107. * NB: Different Linux versions do different things with the
  108. * accessed bit in set_thread_area().
  109. */
  110. if (ar != expected_ar && ar != (expected_ar | AR_ACCESSED)) {
  111. printf("[FAIL]\t%s entry %hu has AR 0x%08X but expected 0x%08X\n",
  112. (ldt ? "LDT" : "GDT"), index, ar, expected_ar);
  113. nerrs++;
  114. } else if (limit != expected_limit) {
  115. printf("[FAIL]\t%s entry %hu has limit 0x%08X but expected 0x%08X\n",
  116. (ldt ? "LDT" : "GDT"), index, limit, expected_limit);
  117. nerrs++;
  118. } else if (verbose) {
  119. printf("[OK]\t%s entry %hu has AR 0x%08X and limit 0x%08X\n",
  120. (ldt ? "LDT" : "GDT"), index, ar, limit);
  121. }
  122. }
  123. static bool install_valid_mode(const struct user_desc *d, uint32_t ar,
  124. bool oldmode, bool ldt)
  125. {
  126. struct user_desc desc = *d;
  127. int ret;
  128. if (!ldt) {
  129. #ifndef __i386__
  130. /* No point testing set_thread_area in a 64-bit build */
  131. return false;
  132. #endif
  133. if (!gdt_entry_num)
  134. return false;
  135. desc.entry_number = gdt_entry_num;
  136. ret = syscall(SYS_set_thread_area, &desc);
  137. } else {
  138. ret = syscall(SYS_modify_ldt, oldmode ? 1 : 0x11,
  139. &desc, sizeof(desc));
  140. if (ret < -1)
  141. errno = -ret;
  142. if (ret != 0 && errno == ENOSYS) {
  143. printf("[OK]\tmodify_ldt returned -ENOSYS\n");
  144. return false;
  145. }
  146. }
  147. if (ret == 0) {
  148. uint32_t limit = desc.limit;
  149. if (desc.limit_in_pages)
  150. limit = (limit << 12) + 4095;
  151. check_valid_segment(desc.entry_number, ldt, ar, limit, true);
  152. return true;
  153. } else {
  154. if (desc.seg_32bit) {
  155. printf("[FAIL]\tUnexpected %s failure %d\n",
  156. ldt ? "modify_ldt" : "set_thread_area",
  157. errno);
  158. nerrs++;
  159. return false;
  160. } else {
  161. printf("[OK]\t%s rejected 16 bit segment\n",
  162. ldt ? "modify_ldt" : "set_thread_area");
  163. return false;
  164. }
  165. }
  166. }
  167. static bool install_valid(const struct user_desc *desc, uint32_t ar)
  168. {
  169. bool ret = install_valid_mode(desc, ar, false, true);
  170. if (desc->contents <= 1 && desc->seg_32bit &&
  171. !desc->seg_not_present) {
  172. /* Should work in the GDT, too. */
  173. install_valid_mode(desc, ar, false, false);
  174. }
  175. return ret;
  176. }
  177. static void install_invalid(const struct user_desc *desc, bool oldmode)
  178. {
  179. int ret = syscall(SYS_modify_ldt, oldmode ? 1 : 0x11,
  180. desc, sizeof(*desc));
  181. if (ret < -1)
  182. errno = -ret;
  183. if (ret == 0) {
  184. check_invalid_segment(desc->entry_number, 1);
  185. } else if (errno == ENOSYS) {
  186. printf("[OK]\tmodify_ldt returned -ENOSYS\n");
  187. } else {
  188. if (desc->seg_32bit) {
  189. printf("[FAIL]\tUnexpected modify_ldt failure %d\n",
  190. errno);
  191. nerrs++;
  192. } else {
  193. printf("[OK]\tmodify_ldt rejected 16 bit segment\n");
  194. }
  195. }
  196. }
  197. static int safe_modify_ldt(int func, struct user_desc *ptr,
  198. unsigned long bytecount)
  199. {
  200. int ret = syscall(SYS_modify_ldt, 0x11, ptr, bytecount);
  201. if (ret < -1)
  202. errno = -ret;
  203. return ret;
  204. }
  205. static void fail_install(struct user_desc *desc)
  206. {
  207. if (safe_modify_ldt(0x11, desc, sizeof(*desc)) == 0) {
  208. printf("[FAIL]\tmodify_ldt accepted a bad descriptor\n");
  209. nerrs++;
  210. } else if (errno == ENOSYS) {
  211. printf("[OK]\tmodify_ldt returned -ENOSYS\n");
  212. } else {
  213. printf("[OK]\tmodify_ldt failure %d\n", errno);
  214. }
  215. }
  216. static void do_simple_tests(void)
  217. {
  218. struct user_desc desc = {
  219. .entry_number = 0,
  220. .base_addr = 0,
  221. .limit = 10,
  222. .seg_32bit = 1,
  223. .contents = 2, /* Code, not conforming */
  224. .read_exec_only = 0,
  225. .limit_in_pages = 0,
  226. .seg_not_present = 0,
  227. .useable = 0
  228. };
  229. install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE | AR_S | AR_P | AR_DB);
  230. desc.limit_in_pages = 1;
  231. install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
  232. AR_S | AR_P | AR_DB | AR_G);
  233. check_invalid_segment(1, 1);
  234. desc.entry_number = 2;
  235. install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
  236. AR_S | AR_P | AR_DB | AR_G);
  237. check_invalid_segment(1, 1);
  238. desc.base_addr = 0xf0000000;
  239. install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
  240. AR_S | AR_P | AR_DB | AR_G);
  241. desc.useable = 1;
  242. install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
  243. AR_S | AR_P | AR_DB | AR_G | AR_AVL);
  244. desc.seg_not_present = 1;
  245. install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
  246. AR_S | AR_DB | AR_G | AR_AVL);
  247. desc.seg_32bit = 0;
  248. install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
  249. AR_S | AR_G | AR_AVL);
  250. desc.seg_32bit = 1;
  251. desc.contents = 0;
  252. install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA |
  253. AR_S | AR_DB | AR_G | AR_AVL);
  254. desc.read_exec_only = 1;
  255. install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA |
  256. AR_S | AR_DB | AR_G | AR_AVL);
  257. desc.contents = 1;
  258. install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA_EXPDOWN |
  259. AR_S | AR_DB | AR_G | AR_AVL);
  260. desc.read_exec_only = 0;
  261. desc.limit_in_pages = 0;
  262. install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA_EXPDOWN |
  263. AR_S | AR_DB | AR_AVL);
  264. desc.contents = 3;
  265. install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE_CONF |
  266. AR_S | AR_DB | AR_AVL);
  267. desc.read_exec_only = 1;
  268. install_valid(&desc, AR_DPL3 | AR_TYPE_XOCODE_CONF |
  269. AR_S | AR_DB | AR_AVL);
  270. desc.read_exec_only = 0;
  271. desc.contents = 2;
  272. install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
  273. AR_S | AR_DB | AR_AVL);
  274. desc.read_exec_only = 1;
  275. #ifdef __x86_64__
  276. desc.lm = 1;
  277. install_valid(&desc, AR_DPL3 | AR_TYPE_XOCODE |
  278. AR_S | AR_DB | AR_AVL);
  279. desc.lm = 0;
  280. #endif
  281. bool entry1_okay = install_valid(&desc, AR_DPL3 | AR_TYPE_XOCODE |
  282. AR_S | AR_DB | AR_AVL);
  283. if (entry1_okay) {
  284. printf("[RUN]\tTest fork\n");
  285. pid_t child = fork();
  286. if (child == 0) {
  287. nerrs = 0;
  288. check_valid_segment(desc.entry_number, 1,
  289. AR_DPL3 | AR_TYPE_XOCODE |
  290. AR_S | AR_DB | AR_AVL, desc.limit,
  291. true);
  292. check_invalid_segment(1, 1);
  293. exit(nerrs ? 1 : 0);
  294. } else {
  295. int status;
  296. if (waitpid(child, &status, 0) != child ||
  297. !WIFEXITED(status)) {
  298. printf("[FAIL]\tChild died\n");
  299. nerrs++;
  300. } else if (WEXITSTATUS(status) != 0) {
  301. printf("[FAIL]\tChild failed\n");
  302. nerrs++;
  303. } else {
  304. printf("[OK]\tChild succeeded\n");
  305. }
  306. }
  307. printf("[RUN]\tTest size\n");
  308. int i;
  309. for (i = 0; i < 8192; i++) {
  310. desc.entry_number = i;
  311. desc.limit = i;
  312. if (safe_modify_ldt(0x11, &desc, sizeof(desc)) != 0) {
  313. printf("[FAIL]\tFailed to install entry %d\n", i);
  314. nerrs++;
  315. break;
  316. }
  317. }
  318. for (int j = 0; j < i; j++) {
  319. check_valid_segment(j, 1, AR_DPL3 | AR_TYPE_XOCODE |
  320. AR_S | AR_DB | AR_AVL, j, false);
  321. }
  322. printf("[DONE]\tSize test\n");
  323. } else {
  324. printf("[SKIP]\tSkipping fork and size tests because we have no LDT\n");
  325. }
  326. /* Test entry_number too high. */
  327. desc.entry_number = 8192;
  328. fail_install(&desc);
  329. /* Test deletion and actions mistakeable for deletion. */
  330. memset(&desc, 0, sizeof(desc));
  331. install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA | AR_S | AR_P);
  332. desc.seg_not_present = 1;
  333. install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA | AR_S);
  334. desc.seg_not_present = 0;
  335. desc.read_exec_only = 1;
  336. install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA | AR_S | AR_P);
  337. desc.read_exec_only = 0;
  338. desc.seg_not_present = 1;
  339. install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA | AR_S);
  340. desc.read_exec_only = 1;
  341. desc.limit = 1;
  342. install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA | AR_S);
  343. desc.limit = 0;
  344. desc.base_addr = 1;
  345. install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA | AR_S);
  346. desc.base_addr = 0;
  347. install_invalid(&desc, false);
  348. desc.seg_not_present = 0;
  349. desc.seg_32bit = 1;
  350. desc.read_exec_only = 0;
  351. desc.limit = 0xfffff;
  352. install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA | AR_S | AR_P | AR_DB);
  353. desc.limit_in_pages = 1;
  354. install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA | AR_S | AR_P | AR_DB | AR_G);
  355. desc.read_exec_only = 1;
  356. install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA | AR_S | AR_P | AR_DB | AR_G);
  357. desc.contents = 1;
  358. desc.read_exec_only = 0;
  359. install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA_EXPDOWN | AR_S | AR_P | AR_DB | AR_G);
  360. desc.read_exec_only = 1;
  361. install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA_EXPDOWN | AR_S | AR_P | AR_DB | AR_G);
  362. desc.limit = 0;
  363. install_invalid(&desc, true);
  364. }
  365. /*
  366. * 0: thread is idle
  367. * 1: thread armed
  368. * 2: thread should clear LDT entry 0
  369. * 3: thread should exit
  370. */
  371. static volatile unsigned int ftx;
  372. static void *threadproc(void *ctx)
  373. {
  374. cpu_set_t cpuset;
  375. CPU_ZERO(&cpuset);
  376. CPU_SET(1, &cpuset);
  377. if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0)
  378. err(1, "sched_setaffinity to CPU 1"); /* should never fail */
  379. while (1) {
  380. syscall(SYS_futex, &ftx, FUTEX_WAIT, 0, NULL, NULL, 0);
  381. while (ftx != 2) {
  382. if (ftx >= 3)
  383. return NULL;
  384. }
  385. /* clear LDT entry 0 */
  386. const struct user_desc desc = {};
  387. if (syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)) != 0)
  388. err(1, "modify_ldt");
  389. /* If ftx == 2, set it to zero. If ftx == 100, quit. */
  390. unsigned int x = -2;
  391. asm volatile ("lock xaddl %[x], %[ftx]" :
  392. [x] "+r" (x), [ftx] "+m" (ftx));
  393. if (x != 2)
  394. return NULL;
  395. }
  396. }
  397. #ifdef __i386__
  398. #ifndef SA_RESTORE
  399. #define SA_RESTORER 0x04000000
  400. #endif
  401. /*
  402. * The UAPI header calls this 'struct sigaction', which conflicts with
  403. * glibc. Sigh.
  404. */
  405. struct fake_ksigaction {
  406. void *handler; /* the real type is nasty */
  407. unsigned long sa_flags;
  408. void (*sa_restorer)(void);
  409. unsigned char sigset[8];
  410. };
  411. static void fix_sa_restorer(int sig)
  412. {
  413. struct fake_ksigaction ksa;
  414. if (syscall(SYS_rt_sigaction, sig, NULL, &ksa, 8) == 0) {
  415. /*
  416. * glibc has a nasty bug: it sometimes writes garbage to
  417. * sa_restorer. This interacts quite badly with anything
  418. * that fiddles with SS because it can trigger legacy
  419. * stack switching. Patch it up. See:
  420. *
  421. * https://sourceware.org/bugzilla/show_bug.cgi?id=21269
  422. */
  423. if (!(ksa.sa_flags & SA_RESTORER) && ksa.sa_restorer) {
  424. ksa.sa_restorer = NULL;
  425. if (syscall(SYS_rt_sigaction, sig, &ksa, NULL,
  426. sizeof(ksa.sigset)) != 0)
  427. err(1, "rt_sigaction");
  428. }
  429. }
  430. }
  431. #else
  432. static void fix_sa_restorer(int sig)
  433. {
  434. /* 64-bit glibc works fine. */
  435. }
  436. #endif
  437. static jmp_buf jmpbuf;
  438. static void sigsegv(int sig, siginfo_t *info, void *ctx_void)
  439. {
  440. siglongjmp(jmpbuf, 1);
  441. }
  442. static void do_multicpu_tests(void)
  443. {
  444. cpu_set_t cpuset;
  445. pthread_t thread;
  446. int failures = 0, iters = 5, i;
  447. unsigned short orig_ss;
  448. CPU_ZERO(&cpuset);
  449. CPU_SET(1, &cpuset);
  450. if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
  451. printf("[SKIP]\tCannot set affinity to CPU 1\n");
  452. return;
  453. }
  454. CPU_ZERO(&cpuset);
  455. CPU_SET(0, &cpuset);
  456. if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
  457. printf("[SKIP]\tCannot set affinity to CPU 0\n");
  458. return;
  459. }
  460. sethandler(SIGSEGV, sigsegv, 0);
  461. fix_sa_restorer(SIGSEGV);
  462. #ifdef __i386__
  463. /* True 32-bit kernels send SIGILL instead of SIGSEGV on IRET faults. */
  464. sethandler(SIGILL, sigsegv, 0);
  465. fix_sa_restorer(SIGILL);
  466. #endif
  467. printf("[RUN]\tCross-CPU LDT invalidation\n");
  468. if (pthread_create(&thread, 0, threadproc, 0) != 0)
  469. err(1, "pthread_create");
  470. asm volatile ("mov %%ss, %0" : "=rm" (orig_ss));
  471. for (i = 0; i < 5; i++) {
  472. if (sigsetjmp(jmpbuf, 1) != 0)
  473. continue;
  474. /* Make sure the thread is ready after the last test. */
  475. while (ftx != 0)
  476. ;
  477. struct user_desc desc = {
  478. .entry_number = 0,
  479. .base_addr = 0,
  480. .limit = 0xfffff,
  481. .seg_32bit = 1,
  482. .contents = 0, /* Data */
  483. .read_exec_only = 0,
  484. .limit_in_pages = 1,
  485. .seg_not_present = 0,
  486. .useable = 0
  487. };
  488. if (safe_modify_ldt(0x11, &desc, sizeof(desc)) != 0) {
  489. if (errno != ENOSYS)
  490. err(1, "modify_ldt");
  491. printf("[SKIP]\tmodify_ldt unavailable\n");
  492. break;
  493. }
  494. /* Arm the thread. */
  495. ftx = 1;
  496. syscall(SYS_futex, &ftx, FUTEX_WAKE, 0, NULL, NULL, 0);
  497. asm volatile ("mov %0, %%ss" : : "r" (0x7));
  498. /* Go! */
  499. ftx = 2;
  500. while (ftx != 0)
  501. ;
  502. /*
  503. * On success, modify_ldt will segfault us synchronously,
  504. * and we'll escape via siglongjmp.
  505. */
  506. failures++;
  507. asm volatile ("mov %0, %%ss" : : "rm" (orig_ss));
  508. }
  509. ftx = 100; /* Kill the thread. */
  510. syscall(SYS_futex, &ftx, FUTEX_WAKE, 0, NULL, NULL, 0);
  511. if (pthread_join(thread, NULL) != 0)
  512. err(1, "pthread_join");
  513. if (failures) {
  514. printf("[FAIL]\t%d of %d iterations failed\n", failures, iters);
  515. nerrs++;
  516. } else {
  517. printf("[OK]\tAll %d iterations succeeded\n", iters);
  518. }
  519. }
  520. static int finish_exec_test(void)
  521. {
  522. /*
  523. * Older kernel versions did inherit the LDT on exec() which is
  524. * wrong because exec() starts from a clean state.
  525. */
  526. check_invalid_segment(0, 1);
  527. return nerrs ? 1 : 0;
  528. }
  529. static void do_exec_test(void)
  530. {
  531. printf("[RUN]\tTest exec\n");
  532. struct user_desc desc = {
  533. .entry_number = 0,
  534. .base_addr = 0,
  535. .limit = 42,
  536. .seg_32bit = 1,
  537. .contents = 2, /* Code, not conforming */
  538. .read_exec_only = 0,
  539. .limit_in_pages = 0,
  540. .seg_not_present = 0,
  541. .useable = 0
  542. };
  543. install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE | AR_S | AR_P | AR_DB);
  544. pid_t child = fork();
  545. if (child == 0) {
  546. execl("/proc/self/exe", "ldt_gdt_test_exec", NULL);
  547. printf("[FAIL]\tCould not exec self\n");
  548. exit(1); /* exec failed */
  549. } else {
  550. int status;
  551. if (waitpid(child, &status, 0) != child ||
  552. !WIFEXITED(status)) {
  553. printf("[FAIL]\tChild died\n");
  554. nerrs++;
  555. } else if (WEXITSTATUS(status) != 0) {
  556. printf("[FAIL]\tChild failed\n");
  557. nerrs++;
  558. } else {
  559. printf("[OK]\tChild succeeded\n");
  560. }
  561. }
  562. }
  563. static void setup_counter_page(void)
  564. {
  565. unsigned int *page = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
  566. MAP_ANONYMOUS | MAP_PRIVATE | MAP_32BIT, -1, 0);
  567. if (page == MAP_FAILED)
  568. err(1, "mmap");
  569. for (int i = 0; i < 1024; i++)
  570. page[i] = i;
  571. counter_page = page;
  572. }
  573. static int invoke_set_thread_area(void)
  574. {
  575. int ret;
  576. asm volatile ("int $0x80"
  577. : "=a" (ret), "+m" (low_user_desc) :
  578. "a" (243), "b" (low_user_desc)
  579. : INT80_CLOBBERS);
  580. return ret;
  581. }
  582. static void setup_low_user_desc(void)
  583. {
  584. low_user_desc = mmap(NULL, 2 * sizeof(struct user_desc),
  585. PROT_READ | PROT_WRITE,
  586. MAP_ANONYMOUS | MAP_PRIVATE | MAP_32BIT, -1, 0);
  587. if (low_user_desc == MAP_FAILED)
  588. err(1, "mmap");
  589. low_user_desc->entry_number = -1;
  590. low_user_desc->base_addr = (unsigned long)&counter_page[1];
  591. low_user_desc->limit = 0xfffff;
  592. low_user_desc->seg_32bit = 1;
  593. low_user_desc->contents = 0; /* Data, grow-up*/
  594. low_user_desc->read_exec_only = 0;
  595. low_user_desc->limit_in_pages = 1;
  596. low_user_desc->seg_not_present = 0;
  597. low_user_desc->useable = 0;
  598. if (invoke_set_thread_area() == 0) {
  599. gdt_entry_num = low_user_desc->entry_number;
  600. printf("[NOTE]\tset_thread_area is available; will use GDT index %d\n", gdt_entry_num);
  601. } else {
  602. printf("[NOTE]\tset_thread_area is unavailable\n");
  603. }
  604. low_user_desc_clear = low_user_desc + 1;
  605. low_user_desc_clear->entry_number = gdt_entry_num;
  606. low_user_desc_clear->read_exec_only = 1;
  607. low_user_desc_clear->seg_not_present = 1;
  608. }
  609. static void test_gdt_invalidation(void)
  610. {
  611. if (!gdt_entry_num)
  612. return; /* 64-bit only system -- we can't use set_thread_area */
  613. unsigned short prev_sel;
  614. unsigned short sel;
  615. unsigned int eax;
  616. const char *result;
  617. #ifdef __x86_64__
  618. unsigned long saved_base;
  619. unsigned long new_base;
  620. #endif
  621. /* Test DS */
  622. invoke_set_thread_area();
  623. eax = 243;
  624. sel = (gdt_entry_num << 3) | 3;
  625. asm volatile ("movw %%ds, %[prev_sel]\n\t"
  626. "movw %[sel], %%ds\n\t"
  627. #ifdef __i386__
  628. "pushl %%ebx\n\t"
  629. #endif
  630. "movl %[arg1], %%ebx\n\t"
  631. "int $0x80\n\t" /* Should invalidate ds */
  632. #ifdef __i386__
  633. "popl %%ebx\n\t"
  634. #endif
  635. "movw %%ds, %[sel]\n\t"
  636. "movw %[prev_sel], %%ds"
  637. : [prev_sel] "=&r" (prev_sel), [sel] "+r" (sel),
  638. "+a" (eax)
  639. : "m" (low_user_desc_clear),
  640. [arg1] "r" ((unsigned int)(unsigned long)low_user_desc_clear)
  641. : INT80_CLOBBERS);
  642. if (sel != 0) {
  643. result = "FAIL";
  644. nerrs++;
  645. } else {
  646. result = "OK";
  647. }
  648. printf("[%s]\tInvalidate DS with set_thread_area: new DS = 0x%hx\n",
  649. result, sel);
  650. /* Test ES */
  651. invoke_set_thread_area();
  652. eax = 243;
  653. sel = (gdt_entry_num << 3) | 3;
  654. asm volatile ("movw %%es, %[prev_sel]\n\t"
  655. "movw %[sel], %%es\n\t"
  656. #ifdef __i386__
  657. "pushl %%ebx\n\t"
  658. #endif
  659. "movl %[arg1], %%ebx\n\t"
  660. "int $0x80\n\t" /* Should invalidate es */
  661. #ifdef __i386__
  662. "popl %%ebx\n\t"
  663. #endif
  664. "movw %%es, %[sel]\n\t"
  665. "movw %[prev_sel], %%es"
  666. : [prev_sel] "=&r" (prev_sel), [sel] "+r" (sel),
  667. "+a" (eax)
  668. : "m" (low_user_desc_clear),
  669. [arg1] "r" ((unsigned int)(unsigned long)low_user_desc_clear)
  670. : INT80_CLOBBERS);
  671. if (sel != 0) {
  672. result = "FAIL";
  673. nerrs++;
  674. } else {
  675. result = "OK";
  676. }
  677. printf("[%s]\tInvalidate ES with set_thread_area: new ES = 0x%hx\n",
  678. result, sel);
  679. /* Test FS */
  680. invoke_set_thread_area();
  681. eax = 243;
  682. sel = (gdt_entry_num << 3) | 3;
  683. #ifdef __x86_64__
  684. syscall(SYS_arch_prctl, ARCH_GET_FS, &saved_base);
  685. #endif
  686. asm volatile ("movw %%fs, %[prev_sel]\n\t"
  687. "movw %[sel], %%fs\n\t"
  688. #ifdef __i386__
  689. "pushl %%ebx\n\t"
  690. #endif
  691. "movl %[arg1], %%ebx\n\t"
  692. "int $0x80\n\t" /* Should invalidate fs */
  693. #ifdef __i386__
  694. "popl %%ebx\n\t"
  695. #endif
  696. "movw %%fs, %[sel]\n\t"
  697. : [prev_sel] "=&r" (prev_sel), [sel] "+r" (sel),
  698. "+a" (eax)
  699. : "m" (low_user_desc_clear),
  700. [arg1] "r" ((unsigned int)(unsigned long)low_user_desc_clear)
  701. : INT80_CLOBBERS);
  702. #ifdef __x86_64__
  703. syscall(SYS_arch_prctl, ARCH_GET_FS, &new_base);
  704. #endif
  705. /* Restore FS/BASE for glibc */
  706. asm volatile ("movw %[prev_sel], %%fs" : : [prev_sel] "rm" (prev_sel));
  707. #ifdef __x86_64__
  708. if (saved_base)
  709. syscall(SYS_arch_prctl, ARCH_SET_FS, saved_base);
  710. #endif
  711. if (sel != 0) {
  712. result = "FAIL";
  713. nerrs++;
  714. } else {
  715. result = "OK";
  716. }
  717. printf("[%s]\tInvalidate FS with set_thread_area: new FS = 0x%hx\n",
  718. result, sel);
  719. #ifdef __x86_64__
  720. if (sel == 0 && new_base != 0) {
  721. nerrs++;
  722. printf("[FAIL]\tNew FSBASE was 0x%lx\n", new_base);
  723. } else {
  724. printf("[OK]\tNew FSBASE was zero\n");
  725. }
  726. #endif
  727. /* Test GS */
  728. invoke_set_thread_area();
  729. eax = 243;
  730. sel = (gdt_entry_num << 3) | 3;
  731. #ifdef __x86_64__
  732. syscall(SYS_arch_prctl, ARCH_GET_GS, &saved_base);
  733. #endif
  734. asm volatile ("movw %%gs, %[prev_sel]\n\t"
  735. "movw %[sel], %%gs\n\t"
  736. #ifdef __i386__
  737. "pushl %%ebx\n\t"
  738. #endif
  739. "movl %[arg1], %%ebx\n\t"
  740. "int $0x80\n\t" /* Should invalidate gs */
  741. #ifdef __i386__
  742. "popl %%ebx\n\t"
  743. #endif
  744. "movw %%gs, %[sel]\n\t"
  745. : [prev_sel] "=&r" (prev_sel), [sel] "+r" (sel),
  746. "+a" (eax)
  747. : "m" (low_user_desc_clear),
  748. [arg1] "r" ((unsigned int)(unsigned long)low_user_desc_clear)
  749. : INT80_CLOBBERS);
  750. #ifdef __x86_64__
  751. syscall(SYS_arch_prctl, ARCH_GET_GS, &new_base);
  752. #endif
  753. /* Restore GS/BASE for glibc */
  754. asm volatile ("movw %[prev_sel], %%gs" : : [prev_sel] "rm" (prev_sel));
  755. #ifdef __x86_64__
  756. if (saved_base)
  757. syscall(SYS_arch_prctl, ARCH_SET_GS, saved_base);
  758. #endif
  759. if (sel != 0) {
  760. result = "FAIL";
  761. nerrs++;
  762. } else {
  763. result = "OK";
  764. }
  765. printf("[%s]\tInvalidate GS with set_thread_area: new GS = 0x%hx\n",
  766. result, sel);
  767. #ifdef __x86_64__
  768. if (sel == 0 && new_base != 0) {
  769. nerrs++;
  770. printf("[FAIL]\tNew GSBASE was 0x%lx\n", new_base);
  771. } else {
  772. printf("[OK]\tNew GSBASE was zero\n");
  773. }
  774. #endif
  775. }
  776. int main(int argc, char **argv)
  777. {
  778. if (argc == 1 && !strcmp(argv[0], "ldt_gdt_test_exec"))
  779. return finish_exec_test();
  780. setup_counter_page();
  781. setup_low_user_desc();
  782. do_simple_tests();
  783. do_multicpu_tests();
  784. do_exec_test();
  785. test_gdt_invalidation();
  786. return nerrs ? 1 : 0;
  787. }