signal.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. /*
  2. * Copyright (C) 1991, 1992 Linus Torvalds
  3. * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
  4. *
  5. * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
  6. * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
  7. * 2000-2002 x86-64 support by Andi Kleen
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/sched.h>
  11. #include <linux/mm.h>
  12. #include <linux/smp.h>
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/wait.h>
  16. #include <linux/tracehook.h>
  17. #include <linux/unistd.h>
  18. #include <linux/stddef.h>
  19. #include <linux/personality.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/user-return-notifier.h>
  22. #include <linux/uprobes.h>
  23. #include <linux/context_tracking.h>
  24. #include <asm/processor.h>
  25. #include <asm/ucontext.h>
  26. #include <asm/fpu/internal.h>
  27. #include <asm/fpu/signal.h>
  28. #include <asm/vdso.h>
  29. #include <asm/mce.h>
  30. #include <asm/sighandling.h>
  31. #include <asm/vm86.h>
  32. #ifdef CONFIG_X86_64
  33. #include <asm/proto.h>
  34. #include <asm/ia32_unistd.h>
  35. #endif /* CONFIG_X86_64 */
  36. #include <asm/syscall.h>
  37. #include <asm/syscalls.h>
  38. #include <asm/sigframe.h>
  39. #include <asm/signal.h>
  40. #define COPY(x) do { \
  41. get_user_ex(regs->x, &sc->x); \
  42. } while (0)
  43. #define GET_SEG(seg) ({ \
  44. unsigned short tmp; \
  45. get_user_ex(tmp, &sc->seg); \
  46. tmp; \
  47. })
  48. #define COPY_SEG(seg) do { \
  49. regs->seg = GET_SEG(seg); \
  50. } while (0)
  51. #define COPY_SEG_CPL3(seg) do { \
  52. regs->seg = GET_SEG(seg) | 3; \
  53. } while (0)
  54. #ifdef CONFIG_X86_64
  55. /*
  56. * If regs->ss will cause an IRET fault, change it. Otherwise leave it
  57. * alone. Using this generally makes no sense unless
  58. * user_64bit_mode(regs) would return true.
  59. */
  60. static void force_valid_ss(struct pt_regs *regs)
  61. {
  62. u32 ar;
  63. asm volatile ("lar %[old_ss], %[ar]\n\t"
  64. "jz 1f\n\t" /* If invalid: */
  65. "xorl %[ar], %[ar]\n\t" /* set ar = 0 */
  66. "1:"
  67. : [ar] "=r" (ar)
  68. : [old_ss] "rm" ((u16)regs->ss));
  69. /*
  70. * For a valid 64-bit user context, we need DPL 3, type
  71. * read-write data or read-write exp-down data, and S and P
  72. * set. We can't use VERW because VERW doesn't check the
  73. * P bit.
  74. */
  75. ar &= AR_DPL_MASK | AR_S | AR_P | AR_TYPE_MASK;
  76. if (ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA) &&
  77. ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA_EXPDOWN))
  78. regs->ss = __USER_DS;
  79. }
  80. #endif
  81. static int restore_sigcontext(struct pt_regs *regs,
  82. struct sigcontext __user *sc,
  83. unsigned long uc_flags)
  84. {
  85. unsigned long buf_val;
  86. void __user *buf;
  87. unsigned int tmpflags;
  88. unsigned int err = 0;
  89. /* Always make any pending restarted system calls return -EINTR */
  90. current->restart_block.fn = do_no_restart_syscall;
  91. get_user_try {
  92. #ifdef CONFIG_X86_32
  93. set_user_gs(regs, GET_SEG(gs));
  94. COPY_SEG(fs);
  95. COPY_SEG(es);
  96. COPY_SEG(ds);
  97. #endif /* CONFIG_X86_32 */
  98. COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
  99. COPY(dx); COPY(cx); COPY(ip); COPY(ax);
  100. #ifdef CONFIG_X86_64
  101. COPY(r8);
  102. COPY(r9);
  103. COPY(r10);
  104. COPY(r11);
  105. COPY(r12);
  106. COPY(r13);
  107. COPY(r14);
  108. COPY(r15);
  109. #endif /* CONFIG_X86_64 */
  110. COPY_SEG_CPL3(cs);
  111. COPY_SEG_CPL3(ss);
  112. #ifdef CONFIG_X86_64
  113. /*
  114. * Fix up SS if needed for the benefit of old DOSEMU and
  115. * CRIU.
  116. */
  117. if (unlikely(!(uc_flags & UC_STRICT_RESTORE_SS) &&
  118. user_64bit_mode(regs)))
  119. force_valid_ss(regs);
  120. #endif
  121. get_user_ex(tmpflags, &sc->flags);
  122. regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
  123. regs->orig_ax = -1; /* disable syscall checks */
  124. get_user_ex(buf_val, &sc->fpstate);
  125. buf = (void __user *)buf_val;
  126. } get_user_catch(err);
  127. err |= fpu__restore_sig(buf, IS_ENABLED(CONFIG_X86_32));
  128. force_iret();
  129. return err;
  130. }
  131. int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
  132. struct pt_regs *regs, unsigned long mask)
  133. {
  134. int err = 0;
  135. put_user_try {
  136. #ifdef CONFIG_X86_32
  137. put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
  138. put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
  139. put_user_ex(regs->es, (unsigned int __user *)&sc->es);
  140. put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
  141. #endif /* CONFIG_X86_32 */
  142. put_user_ex(regs->di, &sc->di);
  143. put_user_ex(regs->si, &sc->si);
  144. put_user_ex(regs->bp, &sc->bp);
  145. put_user_ex(regs->sp, &sc->sp);
  146. put_user_ex(regs->bx, &sc->bx);
  147. put_user_ex(regs->dx, &sc->dx);
  148. put_user_ex(regs->cx, &sc->cx);
  149. put_user_ex(regs->ax, &sc->ax);
  150. #ifdef CONFIG_X86_64
  151. put_user_ex(regs->r8, &sc->r8);
  152. put_user_ex(regs->r9, &sc->r9);
  153. put_user_ex(regs->r10, &sc->r10);
  154. put_user_ex(regs->r11, &sc->r11);
  155. put_user_ex(regs->r12, &sc->r12);
  156. put_user_ex(regs->r13, &sc->r13);
  157. put_user_ex(regs->r14, &sc->r14);
  158. put_user_ex(regs->r15, &sc->r15);
  159. #endif /* CONFIG_X86_64 */
  160. put_user_ex(current->thread.trap_nr, &sc->trapno);
  161. put_user_ex(current->thread.error_code, &sc->err);
  162. put_user_ex(regs->ip, &sc->ip);
  163. #ifdef CONFIG_X86_32
  164. put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
  165. put_user_ex(regs->flags, &sc->flags);
  166. put_user_ex(regs->sp, &sc->sp_at_signal);
  167. put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
  168. #else /* !CONFIG_X86_32 */
  169. put_user_ex(regs->flags, &sc->flags);
  170. put_user_ex(regs->cs, &sc->cs);
  171. put_user_ex(0, &sc->gs);
  172. put_user_ex(0, &sc->fs);
  173. put_user_ex(regs->ss, &sc->ss);
  174. #endif /* CONFIG_X86_32 */
  175. put_user_ex(fpstate, &sc->fpstate);
  176. /* non-iBCS2 extensions.. */
  177. put_user_ex(mask, &sc->oldmask);
  178. put_user_ex(current->thread.cr2, &sc->cr2);
  179. } put_user_catch(err);
  180. return err;
  181. }
  182. /*
  183. * Set up a signal frame.
  184. */
  185. /*
  186. * Determine which stack to use..
  187. */
  188. static unsigned long align_sigframe(unsigned long sp)
  189. {
  190. #ifdef CONFIG_X86_32
  191. /*
  192. * Align the stack pointer according to the i386 ABI,
  193. * i.e. so that on function entry ((sp + 4) & 15) == 0.
  194. */
  195. sp = ((sp + 4) & -16ul) - 4;
  196. #else /* !CONFIG_X86_32 */
  197. sp = round_down(sp, 16) - 8;
  198. #endif
  199. return sp;
  200. }
  201. static void __user *
  202. get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
  203. void __user **fpstate)
  204. {
  205. /* Default to using normal stack */
  206. unsigned long math_size = 0;
  207. unsigned long sp = regs->sp;
  208. unsigned long buf_fx = 0;
  209. int onsigstack = on_sig_stack(sp);
  210. struct fpu *fpu = &current->thread.fpu;
  211. /* redzone */
  212. if (IS_ENABLED(CONFIG_X86_64))
  213. sp -= 128;
  214. /* This is the X/Open sanctioned signal stack switching. */
  215. if (ka->sa.sa_flags & SA_ONSTACK) {
  216. if (sas_ss_flags(sp) == 0)
  217. sp = current->sas_ss_sp + current->sas_ss_size;
  218. } else if (IS_ENABLED(CONFIG_X86_32) &&
  219. !onsigstack &&
  220. (regs->ss & 0xffff) != __USER_DS &&
  221. !(ka->sa.sa_flags & SA_RESTORER) &&
  222. ka->sa.sa_restorer) {
  223. /* This is the legacy signal stack switching. */
  224. sp = (unsigned long) ka->sa.sa_restorer;
  225. }
  226. if (fpu->fpstate_active) {
  227. sp = fpu__alloc_mathframe(sp, IS_ENABLED(CONFIG_X86_32),
  228. &buf_fx, &math_size);
  229. *fpstate = (void __user *)sp;
  230. }
  231. sp = align_sigframe(sp - frame_size);
  232. /*
  233. * If we are on the alternate signal stack and would overflow it, don't.
  234. * Return an always-bogus address instead so we will die with SIGSEGV.
  235. */
  236. if (onsigstack && !likely(on_sig_stack(sp)))
  237. return (void __user *)-1L;
  238. /* save i387 and extended state */
  239. if (fpu->fpstate_active &&
  240. copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size) < 0)
  241. return (void __user *)-1L;
  242. return (void __user *)sp;
  243. }
  244. #ifdef CONFIG_X86_32
  245. static const struct {
  246. u16 poplmovl;
  247. u32 val;
  248. u16 int80;
  249. } __attribute__((packed)) retcode = {
  250. 0xb858, /* popl %eax; movl $..., %eax */
  251. __NR_sigreturn,
  252. 0x80cd, /* int $0x80 */
  253. };
  254. static const struct {
  255. u8 movl;
  256. u32 val;
  257. u16 int80;
  258. u8 pad;
  259. } __attribute__((packed)) rt_retcode = {
  260. 0xb8, /* movl $..., %eax */
  261. __NR_rt_sigreturn,
  262. 0x80cd, /* int $0x80 */
  263. 0
  264. };
  265. static int
  266. __setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
  267. struct pt_regs *regs)
  268. {
  269. struct sigframe __user *frame;
  270. void __user *restorer;
  271. int err = 0;
  272. void __user *fpstate = NULL;
  273. frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
  274. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  275. return -EFAULT;
  276. if (__put_user(sig, &frame->sig))
  277. return -EFAULT;
  278. if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
  279. return -EFAULT;
  280. if (_NSIG_WORDS > 1) {
  281. if (__copy_to_user(&frame->extramask, &set->sig[1],
  282. sizeof(frame->extramask)))
  283. return -EFAULT;
  284. }
  285. if (current->mm->context.vdso)
  286. restorer = current->mm->context.vdso +
  287. vdso_image_32.sym___kernel_sigreturn;
  288. else
  289. restorer = &frame->retcode;
  290. if (ksig->ka.sa.sa_flags & SA_RESTORER)
  291. restorer = ksig->ka.sa.sa_restorer;
  292. /* Set up to return from userspace. */
  293. err |= __put_user(restorer, &frame->pretcode);
  294. /*
  295. * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
  296. *
  297. * WE DO NOT USE IT ANY MORE! It's only left here for historical
  298. * reasons and because gdb uses it as a signature to notice
  299. * signal handler stack frames.
  300. */
  301. err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
  302. if (err)
  303. return -EFAULT;
  304. /* Set up registers for signal handler */
  305. regs->sp = (unsigned long)frame;
  306. regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
  307. regs->ax = (unsigned long)sig;
  308. regs->dx = 0;
  309. regs->cx = 0;
  310. regs->ds = __USER_DS;
  311. regs->es = __USER_DS;
  312. regs->ss = __USER_DS;
  313. regs->cs = __USER_CS;
  314. return 0;
  315. }
  316. static int __setup_rt_frame(int sig, struct ksignal *ksig,
  317. sigset_t *set, struct pt_regs *regs)
  318. {
  319. struct rt_sigframe __user *frame;
  320. void __user *restorer;
  321. int err = 0;
  322. void __user *fpstate = NULL;
  323. frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
  324. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  325. return -EFAULT;
  326. put_user_try {
  327. put_user_ex(sig, &frame->sig);
  328. put_user_ex(&frame->info, &frame->pinfo);
  329. put_user_ex(&frame->uc, &frame->puc);
  330. /* Create the ucontext. */
  331. if (boot_cpu_has(X86_FEATURE_XSAVE))
  332. put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
  333. else
  334. put_user_ex(0, &frame->uc.uc_flags);
  335. put_user_ex(0, &frame->uc.uc_link);
  336. save_altstack_ex(&frame->uc.uc_stack, regs->sp);
  337. /* Set up to return from userspace. */
  338. restorer = current->mm->context.vdso +
  339. vdso_image_32.sym___kernel_rt_sigreturn;
  340. if (ksig->ka.sa.sa_flags & SA_RESTORER)
  341. restorer = ksig->ka.sa.sa_restorer;
  342. put_user_ex(restorer, &frame->pretcode);
  343. /*
  344. * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
  345. *
  346. * WE DO NOT USE IT ANY MORE! It's only left here for historical
  347. * reasons and because gdb uses it as a signature to notice
  348. * signal handler stack frames.
  349. */
  350. put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
  351. } put_user_catch(err);
  352. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  353. err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
  354. regs, set->sig[0]);
  355. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  356. if (err)
  357. return -EFAULT;
  358. /* Set up registers for signal handler */
  359. regs->sp = (unsigned long)frame;
  360. regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
  361. regs->ax = (unsigned long)sig;
  362. regs->dx = (unsigned long)&frame->info;
  363. regs->cx = (unsigned long)&frame->uc;
  364. regs->ds = __USER_DS;
  365. regs->es = __USER_DS;
  366. regs->ss = __USER_DS;
  367. regs->cs = __USER_CS;
  368. return 0;
  369. }
  370. #else /* !CONFIG_X86_32 */
  371. static unsigned long frame_uc_flags(struct pt_regs *regs)
  372. {
  373. unsigned long flags;
  374. if (boot_cpu_has(X86_FEATURE_XSAVE))
  375. flags = UC_FP_XSTATE | UC_SIGCONTEXT_SS;
  376. else
  377. flags = UC_SIGCONTEXT_SS;
  378. if (likely(user_64bit_mode(regs)))
  379. flags |= UC_STRICT_RESTORE_SS;
  380. return flags;
  381. }
  382. static int __setup_rt_frame(int sig, struct ksignal *ksig,
  383. sigset_t *set, struct pt_regs *regs)
  384. {
  385. struct rt_sigframe __user *frame;
  386. void __user *fp = NULL;
  387. int err = 0;
  388. frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
  389. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  390. return -EFAULT;
  391. if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
  392. if (copy_siginfo_to_user(&frame->info, &ksig->info))
  393. return -EFAULT;
  394. }
  395. put_user_try {
  396. /* Create the ucontext. */
  397. put_user_ex(frame_uc_flags(regs), &frame->uc.uc_flags);
  398. put_user_ex(0, &frame->uc.uc_link);
  399. save_altstack_ex(&frame->uc.uc_stack, regs->sp);
  400. /* Set up to return from userspace. If provided, use a stub
  401. already in userspace. */
  402. /* x86-64 should always use SA_RESTORER. */
  403. if (ksig->ka.sa.sa_flags & SA_RESTORER) {
  404. put_user_ex(ksig->ka.sa.sa_restorer, &frame->pretcode);
  405. } else {
  406. /* could use a vstub here */
  407. err |= -EFAULT;
  408. }
  409. } put_user_catch(err);
  410. err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
  411. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  412. if (err)
  413. return -EFAULT;
  414. /* Set up registers for signal handler */
  415. regs->di = sig;
  416. /* In case the signal handler was declared without prototypes */
  417. regs->ax = 0;
  418. /* This also works for non SA_SIGINFO handlers because they expect the
  419. next argument after the signal number on the stack. */
  420. regs->si = (unsigned long)&frame->info;
  421. regs->dx = (unsigned long)&frame->uc;
  422. regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
  423. regs->sp = (unsigned long)frame;
  424. /*
  425. * Set up the CS and SS registers to run signal handlers in
  426. * 64-bit mode, even if the handler happens to be interrupting
  427. * 32-bit or 16-bit code.
  428. *
  429. * SS is subtle. In 64-bit mode, we don't need any particular
  430. * SS descriptor, but we do need SS to be valid. It's possible
  431. * that the old SS is entirely bogus -- this can happen if the
  432. * signal we're trying to deliver is #GP or #SS caused by a bad
  433. * SS value. We also have a compatbility issue here: DOSEMU
  434. * relies on the contents of the SS register indicating the
  435. * SS value at the time of the signal, even though that code in
  436. * DOSEMU predates sigreturn's ability to restore SS. (DOSEMU
  437. * avoids relying on sigreturn to restore SS; instead it uses
  438. * a trampoline.) So we do our best: if the old SS was valid,
  439. * we keep it. Otherwise we replace it.
  440. */
  441. regs->cs = __USER_CS;
  442. if (unlikely(regs->ss != __USER_DS))
  443. force_valid_ss(regs);
  444. return 0;
  445. }
  446. #endif /* CONFIG_X86_32 */
  447. static int x32_setup_rt_frame(struct ksignal *ksig,
  448. compat_sigset_t *set,
  449. struct pt_regs *regs)
  450. {
  451. #ifdef CONFIG_X86_X32_ABI
  452. struct rt_sigframe_x32 __user *frame;
  453. void __user *restorer;
  454. int err = 0;
  455. void __user *fpstate = NULL;
  456. frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
  457. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  458. return -EFAULT;
  459. if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
  460. if (__copy_siginfo_to_user32(&frame->info, &ksig->info, true))
  461. return -EFAULT;
  462. }
  463. put_user_try {
  464. /* Create the ucontext. */
  465. put_user_ex(frame_uc_flags(regs), &frame->uc.uc_flags);
  466. put_user_ex(0, &frame->uc.uc_link);
  467. compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
  468. put_user_ex(0, &frame->uc.uc__pad0);
  469. if (ksig->ka.sa.sa_flags & SA_RESTORER) {
  470. restorer = ksig->ka.sa.sa_restorer;
  471. } else {
  472. /* could use a vstub here */
  473. restorer = NULL;
  474. err |= -EFAULT;
  475. }
  476. put_user_ex(restorer, &frame->pretcode);
  477. } put_user_catch(err);
  478. err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
  479. regs, set->sig[0]);
  480. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  481. if (err)
  482. return -EFAULT;
  483. /* Set up registers for signal handler */
  484. regs->sp = (unsigned long) frame;
  485. regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
  486. /* We use the x32 calling convention here... */
  487. regs->di = ksig->sig;
  488. regs->si = (unsigned long) &frame->info;
  489. regs->dx = (unsigned long) &frame->uc;
  490. loadsegment(ds, __USER_DS);
  491. loadsegment(es, __USER_DS);
  492. regs->cs = __USER_CS;
  493. regs->ss = __USER_DS;
  494. #endif /* CONFIG_X86_X32_ABI */
  495. return 0;
  496. }
  497. /*
  498. * Do a signal return; undo the signal stack.
  499. */
  500. #ifdef CONFIG_X86_32
  501. asmlinkage unsigned long sys_sigreturn(void)
  502. {
  503. struct pt_regs *regs = current_pt_regs();
  504. struct sigframe __user *frame;
  505. sigset_t set;
  506. frame = (struct sigframe __user *)(regs->sp - 8);
  507. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  508. goto badframe;
  509. if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
  510. && __copy_from_user(&set.sig[1], &frame->extramask,
  511. sizeof(frame->extramask))))
  512. goto badframe;
  513. set_current_blocked(&set);
  514. /*
  515. * x86_32 has no uc_flags bits relevant to restore_sigcontext.
  516. * Save a few cycles by skipping the __get_user.
  517. */
  518. if (restore_sigcontext(regs, &frame->sc, 0))
  519. goto badframe;
  520. return regs->ax;
  521. badframe:
  522. signal_fault(regs, frame, "sigreturn");
  523. return 0;
  524. }
  525. #endif /* CONFIG_X86_32 */
  526. asmlinkage long sys_rt_sigreturn(void)
  527. {
  528. struct pt_regs *regs = current_pt_regs();
  529. struct rt_sigframe __user *frame;
  530. sigset_t set;
  531. unsigned long uc_flags;
  532. frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
  533. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  534. goto badframe;
  535. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  536. goto badframe;
  537. if (__get_user(uc_flags, &frame->uc.uc_flags))
  538. goto badframe;
  539. set_current_blocked(&set);
  540. if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
  541. goto badframe;
  542. if (restore_altstack(&frame->uc.uc_stack))
  543. goto badframe;
  544. return regs->ax;
  545. badframe:
  546. signal_fault(regs, frame, "rt_sigreturn");
  547. return 0;
  548. }
  549. static inline int is_ia32_compat_frame(struct ksignal *ksig)
  550. {
  551. return IS_ENABLED(CONFIG_IA32_EMULATION) &&
  552. ksig->ka.sa.sa_flags & SA_IA32_ABI;
  553. }
  554. static inline int is_ia32_frame(struct ksignal *ksig)
  555. {
  556. return IS_ENABLED(CONFIG_X86_32) || is_ia32_compat_frame(ksig);
  557. }
  558. static inline int is_x32_frame(struct ksignal *ksig)
  559. {
  560. return IS_ENABLED(CONFIG_X86_X32_ABI) &&
  561. ksig->ka.sa.sa_flags & SA_X32_ABI;
  562. }
  563. static int
  564. setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
  565. {
  566. int usig = ksig->sig;
  567. sigset_t *set = sigmask_to_save();
  568. compat_sigset_t *cset = (compat_sigset_t *) set;
  569. /* Set up the stack frame */
  570. if (is_ia32_frame(ksig)) {
  571. if (ksig->ka.sa.sa_flags & SA_SIGINFO)
  572. return ia32_setup_rt_frame(usig, ksig, cset, regs);
  573. else
  574. return ia32_setup_frame(usig, ksig, cset, regs);
  575. } else if (is_x32_frame(ksig)) {
  576. return x32_setup_rt_frame(ksig, cset, regs);
  577. } else {
  578. return __setup_rt_frame(ksig->sig, ksig, set, regs);
  579. }
  580. }
  581. static void
  582. handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  583. {
  584. bool stepping, failed;
  585. struct fpu *fpu = &current->thread.fpu;
  586. if (v8086_mode(regs))
  587. save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
  588. /* Are we from a system call? */
  589. if (syscall_get_nr(current, regs) >= 0) {
  590. /* If so, check system call restarting.. */
  591. switch (syscall_get_error(current, regs)) {
  592. case -ERESTART_RESTARTBLOCK:
  593. case -ERESTARTNOHAND:
  594. regs->ax = -EINTR;
  595. break;
  596. case -ERESTARTSYS:
  597. if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
  598. regs->ax = -EINTR;
  599. break;
  600. }
  601. /* fallthrough */
  602. case -ERESTARTNOINTR:
  603. regs->ax = regs->orig_ax;
  604. regs->ip -= 2;
  605. break;
  606. }
  607. }
  608. /*
  609. * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
  610. * so that register information in the sigcontext is correct and
  611. * then notify the tracer before entering the signal handler.
  612. */
  613. stepping = test_thread_flag(TIF_SINGLESTEP);
  614. if (stepping)
  615. user_disable_single_step(current);
  616. failed = (setup_rt_frame(ksig, regs) < 0);
  617. if (!failed) {
  618. /*
  619. * Clear the direction flag as per the ABI for function entry.
  620. *
  621. * Clear RF when entering the signal handler, because
  622. * it might disable possible debug exception from the
  623. * signal handler.
  624. *
  625. * Clear TF for the case when it wasn't set by debugger to
  626. * avoid the recursive send_sigtrap() in SIGTRAP handler.
  627. */
  628. regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
  629. /*
  630. * Ensure the signal handler starts with the new fpu state.
  631. */
  632. if (fpu->fpstate_active)
  633. fpu__clear(fpu);
  634. }
  635. signal_setup_done(failed, ksig, stepping);
  636. }
  637. static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
  638. {
  639. /*
  640. * This function is fundamentally broken as currently
  641. * implemented.
  642. *
  643. * The idea is that we want to trigger a call to the
  644. * restart_block() syscall and that we want in_ia32_syscall(),
  645. * in_x32_syscall(), etc. to match whatever they were in the
  646. * syscall being restarted. We assume that the syscall
  647. * instruction at (regs->ip - 2) matches whatever syscall
  648. * instruction we used to enter in the first place.
  649. *
  650. * The problem is that we can get here when ptrace pokes
  651. * syscall-like values into regs even if we're not in a syscall
  652. * at all.
  653. *
  654. * For now, we maintain historical behavior and guess based on
  655. * stored state. We could do better by saving the actual
  656. * syscall arch in restart_block or (with caveats on x32) by
  657. * checking if regs->ip points to 'int $0x80'. The current
  658. * behavior is incorrect if a tracer has a different bitness
  659. * than the tracee.
  660. */
  661. #ifdef CONFIG_IA32_EMULATION
  662. if (current_thread_info()->status & (TS_COMPAT|TS_I386_REGS_POKED))
  663. return __NR_ia32_restart_syscall;
  664. #endif
  665. #ifdef CONFIG_X86_X32_ABI
  666. return __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
  667. #else
  668. return __NR_restart_syscall;
  669. #endif
  670. }
  671. /*
  672. * Note that 'init' is a special process: it doesn't get signals it doesn't
  673. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  674. * mistake.
  675. */
  676. void do_signal(struct pt_regs *regs)
  677. {
  678. struct ksignal ksig;
  679. if (get_signal(&ksig)) {
  680. /* Whee! Actually deliver the signal. */
  681. handle_signal(&ksig, regs);
  682. return;
  683. }
  684. /* Did we come from a system call? */
  685. if (syscall_get_nr(current, regs) >= 0) {
  686. /* Restart the system call - no handlers present */
  687. switch (syscall_get_error(current, regs)) {
  688. case -ERESTARTNOHAND:
  689. case -ERESTARTSYS:
  690. case -ERESTARTNOINTR:
  691. regs->ax = regs->orig_ax;
  692. regs->ip -= 2;
  693. break;
  694. case -ERESTART_RESTARTBLOCK:
  695. regs->ax = get_nr_restart_syscall(regs);
  696. regs->ip -= 2;
  697. break;
  698. }
  699. }
  700. /*
  701. * If there's no signal to deliver, we just put the saved sigmask
  702. * back.
  703. */
  704. restore_saved_sigmask();
  705. }
  706. void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
  707. {
  708. struct task_struct *me = current;
  709. if (show_unhandled_signals && printk_ratelimit()) {
  710. printk("%s"
  711. "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
  712. task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
  713. me->comm, me->pid, where, frame,
  714. regs->ip, regs->sp, regs->orig_ax);
  715. print_vma_addr(" in ", regs->ip);
  716. pr_cont("\n");
  717. }
  718. force_sig(SIGSEGV, me);
  719. }
  720. #ifdef CONFIG_X86_X32_ABI
  721. asmlinkage long sys32_x32_rt_sigreturn(void)
  722. {
  723. struct pt_regs *regs = current_pt_regs();
  724. struct rt_sigframe_x32 __user *frame;
  725. sigset_t set;
  726. unsigned long uc_flags;
  727. frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
  728. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  729. goto badframe;
  730. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  731. goto badframe;
  732. if (__get_user(uc_flags, &frame->uc.uc_flags))
  733. goto badframe;
  734. set_current_blocked(&set);
  735. if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
  736. goto badframe;
  737. if (compat_restore_altstack(&frame->uc.uc_stack))
  738. goto badframe;
  739. return regs->ax;
  740. badframe:
  741. signal_fault(regs, frame, "x32 rt_sigreturn");
  742. return 0;
  743. }
  744. #endif