ldt_gdt.c 22 KB

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