db_exception.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * db_signal.c
  17. *
  18. * This file contains APIs to set up handlers of exceptions issued by the
  19. * host, and the methods to pass the exceptions to the upcalls.
  20. */
  21. #include "pal_defs.h"
  22. #include "pal_linux_defs.h"
  23. #include "pal.h"
  24. #include "pal_internal.h"
  25. #include "pal_linux.h"
  26. #include "pal_error.h"
  27. #include "pal_security.h"
  28. #include "api.h"
  29. #include "linux_list.h"
  30. #include <atomic.h>
  31. #include <sigset.h>
  32. #include <linux/signal.h>
  33. #include <ucontext.h>
  34. #include <asm-errno.h>
  35. #if !defined(__i386__)
  36. /* In x86_64 kernels, sigaction is required to have a user-defined
  37. * restorer. Also, they not yet support SA_INFO. The reference:
  38. * http://lxr.linux.no/linux+v2.6.35/arch/x86/kernel/signal.c#L448
  39. *
  40. * / * x86-64 should always use SA_RESTORER. * /
  41. * if (ka->sa.sa_flags & SA_RESTORER) {
  42. * put_user_ex(ka->sa.sa_restorer, &frame->pretcode);
  43. * } else {
  44. * / * could use a vstub here * /
  45. * err |= -EFAULT;
  46. * }
  47. */
  48. void restore_rt (void) asm ("__restore_rt");
  49. #ifndef SA_RESTORER
  50. #define SA_RESTORER 0x04000000
  51. #endif
  52. #define DEFINE_RESTORE_RT(syscall) DEFINE_RESTORE_RT2(syscall)
  53. # define DEFINE_RESTORE_RT2(syscall) \
  54. asm ( \
  55. " nop\n" \
  56. ".align 16\n" \
  57. ".LSTART_restore_rt:\n" \
  58. " .type __restore_rt,@function\n" \
  59. "__restore_rt:\n" \
  60. " movq $" #syscall ", %rax\n" \
  61. " syscall\n");
  62. DEFINE_RESTORE_RT(__NR_rt_sigreturn)
  63. #endif
  64. int set_sighandler (int * sigs, int nsig, void * handler)
  65. {
  66. struct sigaction action;
  67. action.sa_handler = (void (*)(int)) handler;
  68. action.sa_flags = SA_SIGINFO;
  69. #if !defined(__i386__)
  70. action.sa_flags |= SA_RESTORER;
  71. action.sa_restorer = restore_rt;
  72. #endif
  73. __sigemptyset((__sigset_t *) &action.sa_mask);
  74. __sigaddset((__sigset_t *) &action.sa_mask, SIGCONT);
  75. for (int i = 0 ; i < nsig ; i++) {
  76. if (sigs[i] == SIGCHLD)
  77. action.sa_flags |= SA_NOCLDSTOP|SA_NOCLDWAIT;
  78. #if defined(__i386__)
  79. int ret = INLINE_SYSCALL(sigaction, 3, sigs[i], &action, NULL)
  80. #else
  81. int ret = INLINE_SYSCALL(rt_sigaction, 4, sigs[i], &action, NULL,
  82. sizeof(sigset_t));
  83. #endif
  84. if (IS_ERR(ret))
  85. return -PAL_ERROR_DENIED;
  86. action.sa_flags &= ~(SA_NOCLDSTOP|SA_NOCLDWAIT);
  87. }
  88. bool maskset = false;
  89. int ret = 0;
  90. __sigset_t mask;
  91. __sigemptyset(&mask);
  92. for (int i = 0 ; i < nsig ; i++)
  93. if (__sigismember(&pal_linux_config.sigset, sigs[i])) {
  94. __sigdelset(&pal_linux_config.sigset, sigs[i]);
  95. __sigaddset(&mask, sigs[i]);
  96. maskset = true;
  97. }
  98. if (maskset) {
  99. #if defined(__i386__)
  100. ret = INLINE_SYSCALL(sigprocmask, 3, SIG_UNBLOCK, &mask, NULL)
  101. #else
  102. ret = INLINE_SYSCALL(rt_sigprocmask, 4, SIG_UNBLOCK, &mask, NULL,
  103. sizeof(sigset_t));
  104. #endif
  105. }
  106. if (IS_ERR(ret))
  107. return -PAL_ERROR_DENIED;
  108. return 0;
  109. }
  110. int block_signals (int * sigs, int nsig)
  111. {
  112. bool maskset = false;
  113. int ret = 0;
  114. __sigset_t mask;
  115. __sigemptyset(&mask);
  116. for (int i = 0 ; i < nsig ; i++)
  117. if (!__sigismember(&pal_linux_config.sigset, sigs[i])) {
  118. __sigaddset(&pal_linux_config.sigset, sigs[i]);
  119. __sigaddset(&mask, sigs[i]);
  120. maskset = true;
  121. }
  122. if (maskset) {
  123. #if defined(__i386__)
  124. ret = INLINE_SYSCALL(sigprocmask, 3, SIG_BLOCK, &mask, NULL)
  125. #else
  126. ret = INLINE_SYSCALL(rt_sigprocmask, 4, SIG_BLOCK, &mask, NULL,
  127. sizeof(sigset_t));
  128. #endif
  129. }
  130. if (IS_ERR(ret))
  131. return -PAL_ERROR_DENIED;
  132. return 0;
  133. }
  134. int unblock_signals (int * sigs, int nsig)
  135. {
  136. bool maskset = false;
  137. int ret = 0;
  138. __sigset_t mask;
  139. __sigemptyset(&mask);
  140. for (int i = 0 ; i < nsig ; i++)
  141. if (__sigismember(&pal_linux_config.sigset, sigs[i])) {
  142. __sigdelset(&pal_linux_config.sigset, sigs[i]);
  143. __sigaddset(&mask, sigs[i]);
  144. maskset = true;
  145. }
  146. if (maskset) {
  147. #if defined(__i386__)
  148. ret = INLINE_SYSCALL(sigprocmask, 3, SIG_UNBLOCK, &mask, NULL)
  149. #else
  150. ret = INLINE_SYSCALL(rt_sigprocmask, 4, SIG_UNBLOCK, &mask, NULL,
  151. sizeof(sigset_t));
  152. #endif
  153. }
  154. if (IS_ERR(ret))
  155. return -PAL_ERROR_DENIED;
  156. return 0;
  157. }
  158. int unset_sighandler (int * sigs, int nsig)
  159. {
  160. for (int i = 0 ; i < nsig ; i++) {
  161. #if defined(__i386__)
  162. int ret = INLINE_SYSCALL(sigaction, 4, sigs[i], SIG_DFL, NULL)
  163. #else
  164. int ret = INLINE_SYSCALL(rt_sigaction, 4, sigs[i], SIG_DFL, NULL,
  165. sizeof(__sigset_t));
  166. #endif
  167. if (IS_ERR(ret))
  168. return -PAL_ERROR_DENIED;
  169. }
  170. return 0;
  171. }
  172. struct handler {
  173. struct mutex_handle lock;
  174. PAL_UPCALL upcall;
  175. int flags;
  176. } __attribute__((aligned(sizeof(int))));
  177. struct event {
  178. unsigned long instance;
  179. int event_num;
  180. int flags;
  181. struct pal_frame * frame;
  182. };
  183. #define DECLARE_HANDLER_HEAD(event) \
  184. static struct handler handler_##event = \
  185. { .lock = MUTEX_HANDLE_INIT, \
  186. .upcall = NULL, \
  187. .flags = 0, };
  188. DECLARE_HANDLER_HEAD(DivZero);
  189. DECLARE_HANDLER_HEAD(MemFault);
  190. DECLARE_HANDLER_HEAD(Illegal);
  191. DECLARE_HANDLER_HEAD(Quit);
  192. DECLARE_HANDLER_HEAD(Suspend);
  193. DECLARE_HANDLER_HEAD(Resume);
  194. DECLARE_HANDLER_HEAD(Failure);
  195. struct handler * pal_handlers [PAL_EVENT_NUM_BOUND] = {
  196. NULL, /* reserved */
  197. &handler_DivZero,
  198. &handler_MemFault,
  199. &handler_Illegal,
  200. &handler_Quit,
  201. &handler_Suspend,
  202. &handler_Resume,
  203. &handler_Failure,
  204. };
  205. #define SIGNAL_MASK_TIME 1000
  206. #define save_return_point(ptr) \
  207. asm volatile ("leaq 0(%%rip), %%rax\r\n" \
  208. "movq %%rax, %0\r\n" \
  209. : "=b"(ptr) :: "memory", "rax")
  210. static int get_event_num (int signum)
  211. {
  212. switch(signum) {
  213. case SIGFPE: return PAL_EVENT_DIVZERO;
  214. case SIGSEGV: case SIGBUS: return PAL_EVENT_MEMFAULT;
  215. case SIGILL: return PAL_EVENT_ILLEGAL;
  216. case SIGTERM: return PAL_EVENT_QUIT;
  217. case SIGINT: return PAL_EVENT_SUSPEND;
  218. case SIGCONT: return PAL_EVENT_RESUME;
  219. default: return -1;
  220. }
  221. }
  222. void _DkGenericEventTrigger (int event_num, PAL_UPCALL upcall, int flags,
  223. PAL_NUM arg, struct pal_frame * frame)
  224. {
  225. struct event event;
  226. event.event_num = event_num;
  227. event.instance = pal_sec_info.domain_id;
  228. event.flags = flags;
  229. event.frame = frame;
  230. (*upcall) (&event, arg, frame ? frame->context : NULL);
  231. }
  232. static bool _DkGenericSignalHandle (int event_num, siginfo_t * info,
  233. struct pal_frame * frame)
  234. {
  235. struct handler * handler = pal_handlers[event_num];
  236. _DkMutexLock(&handler->lock);
  237. PAL_UPCALL upcall = handler->upcall;
  238. int flags = handler->flags;
  239. _DkMutexUnlock(&handler->lock);
  240. PAL_NUM arg = 0;
  241. if (upcall) {
  242. if (event_num == PAL_EVENT_DIVZERO ||
  243. event_num == PAL_EVENT_MEMFAULT ||
  244. event_num == PAL_EVENT_ILLEGAL)
  245. arg = (PAL_NUM) (info ? info->si_addr : 0);
  246. _DkGenericEventTrigger(event_num, upcall, flags, arg, frame);
  247. return true;
  248. }
  249. return false;
  250. }
  251. #define ADDR_IN_PAL(addr) \
  252. ((void *) (addr) > pal_config.lib_text_start && \
  253. (void *) (addr) < pal_config.lib_text_end)
  254. static struct pal_frame * get_frame (struct ucontext * uc)
  255. {
  256. unsigned long rip = uc->uc_mcontext.gregs[REG_RIP];
  257. unsigned long rbp = uc->uc_mcontext.gregs[REG_RBP];
  258. if (!ADDR_IN_PAL(rip))
  259. return NULL;
  260. while (ADDR_IN_PAL(((unsigned long *) rbp)[1]))
  261. rbp = *(unsigned long *) rbp;
  262. struct pal_frame * frame = (struct pal_frame *) rbp - 1;
  263. for (int i = 0 ; i < 8 ; i++) {
  264. if (frame->self == frame)
  265. return frame;
  266. frame = (struct pal_frame *) ((void *) frame - 8);
  267. }
  268. return NULL;
  269. }
  270. static void return_frame (struct pal_frame * frame, int err)
  271. {
  272. notify_failure(err);
  273. arch_restore_frame(&frame->arch);
  274. asm volatile ("leaveq\r\n"
  275. "retq\r\n"
  276. ::: "memory");
  277. }
  278. static void _DkGenericSighandler (int signum, siginfo_t * info,
  279. struct ucontext * uc)
  280. {
  281. #if 0
  282. /* reseurrect this code if signal handler if giving segmentation fault */
  283. if (signum == SIGSEGV) {
  284. int pid = INLINE_SYSCALL(getpid, 0);
  285. char msg[24] = "--- SIGSEGV --- [ ]\n";
  286. msg[17] = '0' + pid / 10000;
  287. msg[18] = '0' + (pid / 1000) % 10;
  288. msg[19] = '0' + (pid / 100) % 10;
  289. msg[20] = '0' + (pid / 10) % 10;
  290. msg[21] = '0' + pid % 10;
  291. INLINE_SYSCALL(write, 3, 1, msg, 24);
  292. bool go = false;
  293. while (!go);
  294. }
  295. #endif
  296. struct pal_frame * frame = get_frame(uc);
  297. if (frame && frame->func != &_DkGenericSighandler &&
  298. signum != SIGCONT &&
  299. signum != SIGINT &&
  300. signum != SIGTERM) {
  301. return_frame(frame, PAL_ERROR_BADADDR);
  302. return;
  303. }
  304. int event_num = get_event_num(signum);
  305. if (event_num == -1)
  306. return;
  307. if (!frame) {
  308. frame = __alloca(sizeof(struct pal_frame));
  309. frame->self = frame;
  310. frame->func = &_DkGenericSighandler;
  311. frame->funcname = "DkGenericSighandler";
  312. frame->context = NULL;
  313. frame->retval = NULL;
  314. arch_store_frame(&frame->arch);
  315. }
  316. if (uc) {
  317. frame->context = __alloca(sizeof(PAL_CONTEXT));
  318. memcpy(frame->context, uc->uc_mcontext.gregs, sizeof(PAL_CONTEXT));
  319. } else {
  320. frame->context = NULL;
  321. }
  322. _DkGenericSignalHandle(event_num, info, frame);
  323. }
  324. static void _DkTerminateSighandler (int signum, siginfo_t * info,
  325. struct ucontext * uc)
  326. {
  327. struct pal_frame * frame = get_frame(uc);
  328. if (!frame) {
  329. frame = __alloca(sizeof(struct pal_frame));
  330. frame->self = frame;
  331. frame->func = &_DkTerminateSighandler;
  332. frame->funcname = "DkTerminateSighandler";
  333. frame->context = NULL;
  334. frame->retval = NULL;
  335. arch_store_frame(&frame->arch);
  336. }
  337. if (uc) {
  338. frame->context = __alloca(sizeof(PAL_CONTEXT));
  339. memcpy(frame->context, uc->uc_mcontext.gregs, sizeof(PAL_CONTEXT));
  340. } else {
  341. frame->context = NULL;
  342. }
  343. int event_num = get_event_num(signum);
  344. if (event_num == -1)
  345. return;
  346. if (!_DkGenericSignalHandle(event_num, NULL, frame))
  347. _DkThreadExit(0);
  348. }
  349. static void _DkPipeSighandler (int signum, siginfo_t * info,
  350. struct ucontext * uc)
  351. {
  352. struct pal_frame * frame = get_frame(uc);
  353. if (frame)
  354. return_frame(frame, PAL_ERROR_CONNFAILED);
  355. }
  356. void notify_failure (unsigned long error)
  357. {
  358. _DkMutexLock(&handler_Failure.lock);
  359. PAL_UPCALL upcall = handler_Failure.upcall;
  360. int flags = handler_Failure.flags;
  361. _DkMutexUnlock(&handler_Failure.lock);
  362. if (upcall)
  363. _DkGenericEventTrigger(PAL_EVENT_FAILURE, upcall, flags,
  364. error, NULL);
  365. }
  366. struct signal_ops {
  367. int signum[3];
  368. void (*handler) (int signum, siginfo_t * info,
  369. struct ucontext * uc);
  370. };
  371. struct signal_ops on_signals[PAL_EVENT_NUM_BOUND] = {
  372. /* reserved */ { .signum = { 0 }, .handler = NULL },
  373. /* DivZero */ { .signum = { SIGFPE, 0 },
  374. .handler = _DkGenericSighandler },
  375. /* MemFault */ { .signum = { SIGSEGV, SIGBUS, 0 },
  376. .handler = _DkGenericSighandler },
  377. /* Illegal */ { .signum = { SIGILL, 0 },
  378. .handler = _DkGenericSighandler },
  379. /* Quit */ { .signum = { SIGTERM, 0, 0 },
  380. .handler = _DkTerminateSighandler },
  381. /* Suspend */ { .signum = { SIGINT, 0 },
  382. .handler = _DkTerminateSighandler },
  383. /* Resume */ { .signum = { SIGCONT, 0 },
  384. .handler = _DkGenericSighandler },
  385. /* Failure */ { .signum = { 0 }, .handler = NULL },
  386. };
  387. static int _DkPersistentSighandlerSetup (int event_num)
  388. {
  389. int nsigs, * sigs = on_signals[event_num].signum;
  390. for (nsigs = 0 ; sigs[nsigs] ; nsigs++);
  391. void * sighandler = on_signals[event_num].handler;
  392. int ret = set_sighandler (sigs, nsigs, sighandler);
  393. if (ret < 0)
  394. return ret;
  395. return 0;
  396. }
  397. static int _DkPersistentEventUpcall (int event_num, PAL_UPCALL upcall,
  398. int flags)
  399. {
  400. struct handler * handler = pal_handlers[event_num];
  401. _DkMutexLock(&handler->lock);
  402. handler->upcall = upcall;
  403. handler->flags = flags;
  404. _DkMutexUnlock(&handler->lock);
  405. return _DkPersistentSighandlerSetup(event_num);
  406. }
  407. static int _DkGenericEventUpcall (int event_num, PAL_UPCALL upcall,
  408. int flags)
  409. {
  410. int nsigs, * sigs = on_signals[event_num].signum;
  411. for (nsigs = 0 ; sigs[nsigs] ; nsigs++);
  412. void * sighandler = on_signals[event_num].handler;
  413. struct handler * handler = pal_handlers[event_num];
  414. int ret = 0;
  415. _DkMutexLock(&handler->lock);
  416. handler->upcall = upcall;
  417. handler->flags = flags;
  418. _DkMutexUnlock(&handler->lock);
  419. if (upcall)
  420. ret = set_sighandler (sigs, nsigs, sighandler);
  421. else
  422. ret = block_signals (sigs, nsigs);
  423. return ret;
  424. }
  425. static int _DkDummyEventUpcall (int event_num, PAL_UPCALL upcall,
  426. int flags)
  427. {
  428. struct handler * handler = pal_handlers[event_num];
  429. _DkMutexLock(&handler->lock);
  430. handler->upcall = upcall;
  431. handler->flags = flags;
  432. _DkMutexUnlock(&handler->lock);
  433. return 0;
  434. }
  435. typedef void (*PAL_UPCALL) (PAL_PTR, PAL_NUM, PAL_CONTEXT *);
  436. int (*_DkExceptionHandlers[PAL_EVENT_NUM_BOUND])
  437. (int, PAL_UPCALL, int) = {
  438. /* reserved */ NULL,
  439. /* DivZero */ &_DkPersistentEventUpcall,
  440. /* MemFault */ &_DkPersistentEventUpcall,
  441. /* Illegal */ &_DkPersistentEventUpcall,
  442. /* Quit */ &_DkGenericEventUpcall,
  443. /* Suspend */ &_DkGenericEventUpcall,
  444. /* Resume */ &_DkGenericEventUpcall,
  445. /* Failure */ &_DkDummyEventUpcall,
  446. };
  447. static void _DkCompatibilitySighandler (int signum, siginfo_t * info,
  448. struct ucontext * uc)
  449. {
  450. printf("compatibility support: detected an unintercepted system call\n");
  451. if (!pal_config.syscall_sym_addr)
  452. _DkProcessExit(-1);
  453. asm volatile ("movq %6, %%r10\r\n"
  454. "movq %7, %%r8\r\n"
  455. "movq %8, %%r9\r\n"
  456. "callq *%1\r\n"
  457. "movq %%rax, %0\r\n"
  458. : "=a" (uc->uc_mcontext.gregs[REG_RAX])
  459. : "r"(pal_config.syscall_sym_addr),
  460. "a" (uc->uc_mcontext.gregs[REG_RAX]),
  461. "D" (uc->uc_mcontext.gregs[REG_RDI]),
  462. "S" (uc->uc_mcontext.gregs[REG_RSI]),
  463. "d" (uc->uc_mcontext.gregs[REG_RDX]),
  464. "r" (uc->uc_mcontext.gregs[REG_R10]),
  465. "r" (uc->uc_mcontext.gregs[REG_R8]),
  466. "r" (uc->uc_mcontext.gregs[REG_R9])
  467. : "memory", "r10", "r8", "r9");
  468. }
  469. int signal_setup (void)
  470. {
  471. int ret, sig;
  472. __sigemptyset(&pal_linux_config.sigset);
  473. if ((ret = _DkPersistentEventUpcall(PAL_EVENT_DIVZERO, NULL, 0)) < 0)
  474. goto err;
  475. if ((ret = _DkPersistentEventUpcall(PAL_EVENT_MEMFAULT, NULL, 0)) < 0)
  476. goto err;
  477. if ((ret = _DkPersistentEventUpcall(PAL_EVENT_ILLEGAL, NULL, 0)) < 0)
  478. goto err;
  479. sig = SIGPIPE;
  480. if ((ret = set_sighandler(&sig, 1, &_DkPipeSighandler)) < 0)
  481. goto err;
  482. sig = SIGSYS;
  483. if ((ret = set_sighandler(&sig, 1, &_DkCompatibilitySighandler)) < 0)
  484. goto err;
  485. return 0;
  486. err:
  487. return ret;
  488. }
  489. void _DkExceptionReturn (const void * event)
  490. {
  491. const struct event * e = (const struct event *) event;
  492. if (e->instance == pal_sec_info.domain_id)
  493. return;
  494. int event_n = e->event_num;
  495. if (event_n > 0 && event_n < PAL_EVENT_NUM_BOUND) {
  496. arch_restore_frame(&e->frame->arch);
  497. asm volatile ("leaveq\r\n"
  498. "retq\r\n"
  499. ::: "memory");
  500. }
  501. }