ldt_gdt.c 20 KB

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