shim_sigaction.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * shim_sigaction.c
  15. *
  16. * Implementation of system call "sigaction", "sigreturn", "sigprocmask",
  17. * "kill", "tkill" and "tgkill".
  18. */
  19. #include <shim_internal.h>
  20. #include <shim_utils.h>
  21. #include <shim_table.h>
  22. #include <shim_thread.h>
  23. #include <shim_ipc.h>
  24. #include <shim_profile.h>
  25. #include <pal.h>
  26. #include <pal_error.h>
  27. #include <errno.h>
  28. #include <linux/signal.h>
  29. int shim_do_sigaction (int signum, const struct __kernel_sigaction * act,
  30. struct __kernel_sigaction * oldact, size_t sigsetsize)
  31. {
  32. /* SIGKILL and SIGSTOP cannot be caught or ignored */
  33. if (signum == SIGKILL || signum == SIGSTOP ||
  34. signum <= 0 || signum > NUM_SIGS ||
  35. sigsetsize != sizeof(__sigset_t))
  36. return -EINVAL;
  37. if (act && test_user_memory((void *) act, sizeof(*act), false))
  38. return -EFAULT;
  39. if (oldact && test_user_memory(oldact, sizeof(*oldact), false))
  40. return -EFAULT;
  41. struct shim_thread * cur = get_cur_thread();
  42. int err = 0;
  43. assert(!act || (void *) act->k_sa_handler != (void *) 0x11);
  44. struct shim_signal_handle * sighdl = &cur->signal_handles[signum - 1];
  45. lock(&cur->lock);
  46. if (oldact) {
  47. if (sighdl->action) {
  48. memcpy(oldact, sighdl->action, sizeof(struct __kernel_sigaction));
  49. } else {
  50. memset(oldact, 0, sizeof(struct __kernel_sigaction));
  51. oldact->k_sa_handler = SIG_DFL;
  52. }
  53. }
  54. if (act) {
  55. if (!(sighdl->action))
  56. sighdl->action = malloc(sizeof(struct __kernel_sigaction));
  57. if (!(sighdl->action)) {
  58. err = -ENOMEM;
  59. goto out;
  60. }
  61. memcpy(sighdl->action, act, sizeof(struct __kernel_sigaction));
  62. }
  63. err = 0;
  64. out:
  65. unlock(&cur->lock);
  66. return err;
  67. }
  68. int shim_do_sigreturn (int __unused)
  69. {
  70. __UNUSED(__unused);
  71. /* do nothing */
  72. return 0;
  73. }
  74. int shim_do_sigprocmask (int how, const __sigset_t * set, __sigset_t * oldset)
  75. {
  76. __sigset_t * old, tmp, set_tmp;
  77. if (how != SIG_BLOCK && how != SIG_UNBLOCK &&
  78. how != SIG_SETMASK)
  79. return -EINVAL;
  80. if (set && test_user_memory((void *) set, sizeof(*set), false))
  81. return -EFAULT;
  82. if (oldset && test_user_memory(oldset, sizeof(*oldset), false))
  83. return -EFAULT;
  84. struct shim_thread * cur = get_cur_thread();
  85. int err = 0;
  86. lock(&cur->lock);
  87. old = get_sig_mask(cur);
  88. if (oldset) {
  89. memcpy(&tmp, old, sizeof(__sigset_t));
  90. old = &tmp;
  91. }
  92. /* if set is NULL, then the signal mask is unchanged, but the current
  93. value of the signal mask is nevertheless returned in oldset */
  94. if (!set)
  95. goto out;
  96. memcpy(&set_tmp, old, sizeof(__sigset_t));
  97. switch (how) {
  98. case SIG_BLOCK:
  99. __sigorset(&set_tmp, &set_tmp, set);
  100. break;
  101. case SIG_UNBLOCK:
  102. __signotset(&set_tmp, &set_tmp, set);
  103. break;
  104. case SIG_SETMASK:
  105. memcpy(&set_tmp, set, sizeof(__sigset_t));
  106. break;
  107. }
  108. set_sig_mask(cur, &set_tmp);
  109. out:
  110. unlock(&cur->lock);
  111. if (!err && oldset)
  112. memcpy(oldset, old, sizeof(__sigset_t));
  113. return err;
  114. }
  115. int shim_do_sigaltstack (const stack_t * ss, stack_t * oss)
  116. {
  117. if (ss && (ss->ss_flags & ~SS_DISABLE))
  118. return -EINVAL;
  119. struct shim_thread * cur = get_cur_thread();
  120. lock(&cur->lock);
  121. stack_t * cur_ss = &cur->signal_altstack;
  122. if (oss)
  123. *oss = *cur_ss;
  124. void * sp = (void *)shim_get_tls()->context.regs->rsp;
  125. /* check if thread is currently executing on an active altstack */
  126. if (!(cur_ss->ss_flags & SS_DISABLE) &&
  127. sp &&
  128. cur_ss->ss_sp <= sp &&
  129. sp < cur_ss->ss_sp + cur_ss->ss_size) {
  130. if (oss)
  131. oss->ss_flags |= SS_ONSTACK;
  132. if (ss) {
  133. unlock(&cur->lock);
  134. return -EPERM;
  135. }
  136. }
  137. if (ss) {
  138. if (ss->ss_flags & SS_DISABLE) {
  139. memset(cur_ss, 0, sizeof(*cur_ss));
  140. cur_ss->ss_flags = SS_DISABLE;
  141. } else {
  142. if (ss->ss_size < MINSIGSTKSZ) {
  143. unlock(&cur->lock);
  144. return -ENOMEM;
  145. }
  146. *cur_ss = *ss;
  147. }
  148. }
  149. unlock(&cur->lock);
  150. return 0;
  151. }
  152. int shim_do_sigsuspend (const __sigset_t * mask)
  153. {
  154. if (!mask || test_user_memory((void *) mask, sizeof(*mask), false))
  155. return -EFAULT;
  156. __sigset_t * old, tmp;
  157. struct shim_thread * cur = get_cur_thread();
  158. lock(&cur->lock);
  159. /* return immediately on some pending unblocked signal */
  160. for (int sig = 1 ; sig <= NUM_SIGS ; sig++) {
  161. if (atomic_read(&cur->signal_logs[sig - 1].head) !=
  162. atomic_read(&cur->signal_logs[sig - 1].tail)) {
  163. /* at least one signal of type sig... */
  164. if (!__sigismember(mask, sig)) {
  165. /* ...and this type is not blocked in supplied mask */
  166. unlock(&cur->lock);
  167. return -EINTR;
  168. }
  169. }
  170. }
  171. old = get_sig_mask(cur);
  172. memcpy(&tmp, old, sizeof(__sigset_t));
  173. old = &tmp;
  174. set_sig_mask(cur, mask);
  175. cur->suspend_on_signal = true;
  176. unlock(&cur->lock);
  177. thread_setwait(NULL, NULL);
  178. thread_sleep(NO_TIMEOUT);
  179. lock(&cur->lock);
  180. set_sig_mask(cur, old);
  181. unlock(&cur->lock);
  182. return -EINTR;
  183. }
  184. int shim_do_sigpending (__sigset_t * set, size_t sigsetsize)
  185. {
  186. if (sigsetsize != sizeof(*set))
  187. return -EINVAL;
  188. if (!set || test_user_memory(set, sigsetsize, false))
  189. return -EFAULT;
  190. struct shim_thread * cur = get_cur_thread();
  191. __sigemptyset(set);
  192. if (!cur->signal_logs)
  193. return 0;
  194. for (int sig = 1 ; sig <= NUM_SIGS ; sig++) {
  195. if (atomic_read(&cur->signal_logs[sig - 1].head) !=
  196. atomic_read(&cur->signal_logs[sig - 1].tail))
  197. __sigaddset(set, sig);
  198. }
  199. return 0;
  200. }
  201. struct walk_arg {
  202. struct shim_thread * current;
  203. IDTYPE sender;
  204. IDTYPE id;
  205. int sig;
  206. bool use_ipc;
  207. };
  208. // Need to hold thread->lock
  209. static inline void __append_signal(struct shim_thread* thread, int sig, IDTYPE sender)
  210. {
  211. debug("Thread %d killed by signal %d\n", thread->tid, sig);
  212. siginfo_t info;
  213. memset(&info, 0, sizeof(siginfo_t));
  214. info.si_signo = sig;
  215. info.si_pid = sender;
  216. append_signal(thread, sig, &info, true);
  217. }
  218. static int __kill_proc (struct shim_thread * thread, void * arg,
  219. bool * unlocked)
  220. {
  221. struct walk_arg * warg = (struct walk_arg *) arg;
  222. int srched = 0;
  223. if (!warg->use_ipc && !thread->in_vm)
  224. return 0;
  225. if (thread->tgid != warg->id)
  226. return 0;
  227. if (warg->current == thread)
  228. return 1;
  229. /* DEP: Let's do a racy read of is_alive and in_vm.
  230. * If either of these are zero it is a stable condition,
  231. * and we can elide the lock acquire (which helps perf).
  232. */
  233. if (!thread->is_alive)
  234. goto out;
  235. if (!thread->in_vm) {
  236. unlock(&thread_list_lock);
  237. *unlocked = true;
  238. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PROCESS,
  239. warg->sig)) ? 1 : 0;
  240. } else {
  241. lock(&thread->lock);
  242. if (!thread->is_alive)
  243. goto out_locked;
  244. if (thread->in_vm) {
  245. if (warg->sig > 0)
  246. __append_signal(thread, warg->sig, warg->sender);
  247. srched = 1;
  248. } else {
  249. /* This double-check case is probably unnecessary, but keep it for now */
  250. unlock(&thread->lock);
  251. unlock(&thread_list_lock);
  252. *unlocked = true;
  253. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PROCESS,
  254. warg->sig)) ? 1 : 0;
  255. }
  256. }
  257. out_locked:
  258. unlock(&thread->lock);
  259. out:
  260. return srched;
  261. }
  262. static int __kill_proc_simple (struct shim_simple_thread * sthread,
  263. void * arg, bool * unlocked)
  264. {
  265. struct walk_arg * warg = (struct walk_arg *) arg;
  266. int srched = 0;
  267. if (sthread->tgid != warg->id)
  268. return 0;
  269. lock(&sthread->lock);
  270. if (sthread->is_alive) {
  271. unlock(&sthread->lock);
  272. unlock(&thread_list_lock);
  273. *unlocked = true;
  274. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PROCESS,
  275. warg->sig)) ? 1 : 0;
  276. }
  277. unlock(&sthread->lock);
  278. return srched;
  279. }
  280. int do_kill_proc (IDTYPE sender, IDTYPE tgid, int sig, bool use_ipc)
  281. {
  282. struct shim_thread * cur = get_cur_thread();
  283. if (!tgid) {
  284. /* DEP: cur->tgid never changes. No lock needed */
  285. tgid = cur->tgid;
  286. }
  287. struct walk_arg arg;
  288. arg.current = cur;
  289. arg.sender = sender;
  290. arg.id = tgid;
  291. arg.sig = sig;
  292. arg.use_ipc = use_ipc;
  293. bool srched = false;
  294. if (!walk_thread_list(__kill_proc, &arg))
  295. srched = true;
  296. if (!use_ipc || srched)
  297. goto out;
  298. if (!walk_simple_thread_list(__kill_proc_simple, &arg))
  299. srched = true;
  300. if (!srched && !ipc_pid_kill_send(sender, tgid, KILL_PROCESS, sig))
  301. srched = true;
  302. out:
  303. return srched ? 0 : -ESRCH;
  304. }
  305. static int __kill_pgroup (struct shim_thread * thread, void * arg,
  306. bool * unlocked)
  307. {
  308. struct walk_arg * warg = (struct walk_arg *) arg;
  309. int srched = 0;
  310. if (!warg->use_ipc && !thread->in_vm)
  311. return 0;
  312. if (thread->pgid != warg->id)
  313. return 0;
  314. if (warg->current == thread)
  315. return 1;
  316. lock(&thread->lock);
  317. if (!thread->is_alive)
  318. goto out;
  319. if (thread->in_vm) {
  320. if (warg->sig > 0)
  321. __append_signal(thread, warg->sig, warg->sender);
  322. srched = 1;
  323. } else {
  324. unlock(&thread->lock);
  325. unlock(&thread_list_lock);
  326. *unlocked = true;
  327. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PGROUP,
  328. warg->sig)) ? 1 : 0;
  329. }
  330. out:
  331. unlock(&thread->lock);
  332. return srched;
  333. }
  334. static int __kill_pgroup_simple (struct shim_simple_thread * sthread,
  335. void * arg, bool * unlocked)
  336. {
  337. struct walk_arg * warg = (struct walk_arg *) arg;
  338. int srched = 0;
  339. if (sthread->pgid != warg->id)
  340. return 0;
  341. lock(&sthread->lock);
  342. if (sthread->is_alive) {
  343. unlock(&sthread->lock);
  344. unlock(&thread_list_lock);
  345. *unlocked = true;
  346. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PGROUP,
  347. warg->sig)) ? 1 : 0;
  348. }
  349. unlock(&sthread->lock);
  350. return srched;
  351. }
  352. int do_kill_pgroup (IDTYPE sender, IDTYPE pgid, int sig, bool use_ipc)
  353. {
  354. struct shim_thread * cur = get_cur_thread();
  355. if (!pgid) {
  356. pgid = cur->pgid;
  357. }
  358. struct walk_arg arg;
  359. arg.current = cur;
  360. arg.sender = sender;
  361. arg.id = pgid;
  362. arg.sig = sig;
  363. arg.use_ipc = use_ipc;
  364. bool srched = false;
  365. if (!walk_thread_list(__kill_pgroup, &arg))
  366. srched = true;
  367. if (!use_ipc || srched)
  368. goto out;
  369. if (!walk_simple_thread_list(__kill_pgroup_simple, &arg))
  370. srched = true;
  371. if (!srched && !ipc_pid_kill_send(sender, pgid, KILL_PGROUP, sig))
  372. srched = true;
  373. out:
  374. return srched ? 0 : -ESRCH;
  375. }
  376. static int __kill_all_threads (struct shim_thread * thread, void * arg,
  377. bool * unlocked)
  378. {
  379. __UNUSED(unlocked); // Retained for API compatibility
  380. int srched = 0;
  381. struct walk_arg * warg = (struct walk_arg *) arg;
  382. if (thread->tgid != thread->tid)
  383. return 0;
  384. if (warg->current == thread)
  385. return 1;
  386. lock(&thread->lock);
  387. if (thread->in_vm) {
  388. __append_signal(thread, warg->sig, warg->sender);
  389. srched = 1;
  390. }
  391. unlock(&thread->lock);
  392. return srched;
  393. }
  394. int kill_all_threads (struct shim_thread * cur, IDTYPE sender, int sig)
  395. {
  396. struct walk_arg arg;
  397. arg.current = cur;
  398. arg.sender = sender;
  399. arg.id = 0;
  400. arg.sig = sig;
  401. arg.use_ipc = false;
  402. walk_thread_list(__kill_all_threads, &arg);
  403. return 0;
  404. }
  405. int shim_do_kill (pid_t pid, int sig)
  406. {
  407. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  408. if (sig < 0 || sig > NUM_SIGS)
  409. return -EINVAL;
  410. struct shim_thread * cur = get_cur_thread();
  411. int ret = 0;
  412. bool send_to_self = false;
  413. /* If pid equals 0, then sig is sent to every process in the process group
  414. of the calling process. */
  415. if (pid == 0) {
  416. ret = do_kill_pgroup(cur->tgid, 0, sig, true);
  417. send_to_self = true;
  418. }
  419. /* If pid equals -1, then sig is sent to every process for which the
  420. calling process has permission to send */
  421. else if (pid == -1) {
  422. ipc_pid_kill_send(cur->tid, /*target=*/0, KILL_ALL, sig);
  423. kill_all_threads(cur, cur->tid, sig);
  424. send_to_self = true;
  425. }
  426. /* If pid is positive, then signal sig is sent to the process with the ID
  427. specified by pid. */
  428. else if (pid > 0) {
  429. ret = do_kill_proc(cur->tid, pid, sig, true);
  430. send_to_self = ((IDTYPE) pid == cur->tgid);
  431. }
  432. /* If pid is less than -1, then sig is sent to every process in the
  433. process group whose id is -pid */
  434. else {
  435. ret = do_kill_pgroup(cur->tid, -pid, sig, true);
  436. send_to_self = ((IDTYPE) -pid == cur->pgid);
  437. }
  438. if (send_to_self) {
  439. if (ret == -ESRCH)
  440. ret = 0;
  441. if (sig) {
  442. siginfo_t info;
  443. memset(&info, 0, sizeof(siginfo_t));
  444. info.si_signo = sig;
  445. info.si_pid = cur->tid;
  446. deliver_signal(&info, NULL);
  447. }
  448. }
  449. return ret < 0 ? ret : 0;
  450. }
  451. int do_kill_thread (IDTYPE sender, IDTYPE tgid, IDTYPE tid, int sig,
  452. bool use_ipc)
  453. {
  454. if (sig < 0 || sig > NUM_SIGS)
  455. return -EINVAL;
  456. struct shim_thread * thread = lookup_thread(tid);
  457. int ret = 0;
  458. if (thread) {
  459. lock(&thread->lock);
  460. if (thread->in_vm) {
  461. if (!tgid || thread->tgid == tgid)
  462. __append_signal(thread, sig, sender);
  463. else
  464. ret = -ESRCH;
  465. } else {
  466. unlock(&thread->lock);
  467. return ipc_pid_kill_send(sender, tid, KILL_THREAD, sig);
  468. }
  469. unlock(&thread->lock);
  470. return ret;
  471. }
  472. if (!use_ipc)
  473. return -ESRCH;
  474. return ipc_pid_kill_send(sender, tid, KILL_THREAD, sig);
  475. }
  476. int shim_do_tkill (pid_t tid, int sig)
  477. {
  478. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  479. if (tid <= 0)
  480. return -EINVAL;
  481. struct shim_thread * cur = get_cur_thread();
  482. if ((IDTYPE) tid == cur->tid) {
  483. if (sig) {
  484. siginfo_t info;
  485. memset(&info, 0, sizeof(siginfo_t));
  486. info.si_signo = sig;
  487. info.si_pid = cur->tid;
  488. deliver_signal(&info, NULL);
  489. }
  490. return 0;
  491. }
  492. return do_kill_thread(cur->tgid, 0, tid, sig, true);
  493. }
  494. int shim_do_tgkill (pid_t tgid, pid_t tid, int sig)
  495. {
  496. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  497. if (tgid < -1 || tgid == 0 || tid <= 0)
  498. return -EINVAL;
  499. if (tgid == -1)
  500. tgid = 0;
  501. struct shim_thread * cur = get_cur_thread();
  502. if ((IDTYPE) tid == cur->tid) {
  503. if (sig) {
  504. siginfo_t info;
  505. memset(&info, 0, sizeof(siginfo_t));
  506. info.si_signo = sig;
  507. info.si_pid = cur->tid;
  508. deliver_signal(&info, NULL);
  509. }
  510. return 0;
  511. }
  512. return do_kill_thread(cur->tgid, tgid, tid, sig, true);
  513. }