ia32_signal.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * linux/arch/x86_64/ia32/ia32_signal.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. *
  6. * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
  7. * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
  8. * 2000-12-* x86-64 compatibility mode signal handling by Andi Kleen
  9. */
  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/unistd.h>
  17. #include <linux/stddef.h>
  18. #include <linux/personality.h>
  19. #include <linux/compat.h>
  20. #include <linux/binfmts.h>
  21. #include <asm/ucontext.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/fpu/internal.h>
  24. #include <asm/fpu/signal.h>
  25. #include <asm/ptrace.h>
  26. #include <asm/ia32_unistd.h>
  27. #include <asm/user32.h>
  28. #include <asm/sigcontext32.h>
  29. #include <asm/proto.h>
  30. #include <asm/vdso.h>
  31. #include <asm/sigframe.h>
  32. #include <asm/sighandling.h>
  33. #include <asm/sys_ia32.h>
  34. #include <asm/smap.h>
  35. int copy_siginfo_to_user32(compat_siginfo_t __user *to, const siginfo_t *from)
  36. {
  37. int err = 0;
  38. bool ia32 = test_thread_flag(TIF_IA32);
  39. if (!access_ok(VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
  40. return -EFAULT;
  41. put_user_try {
  42. /* If you change siginfo_t structure, please make sure that
  43. this code is fixed accordingly.
  44. It should never copy any pad contained in the structure
  45. to avoid security leaks, but must copy the generic
  46. 3 ints plus the relevant union member. */
  47. put_user_ex(from->si_signo, &to->si_signo);
  48. put_user_ex(from->si_errno, &to->si_errno);
  49. put_user_ex((short)from->si_code, &to->si_code);
  50. if (from->si_code < 0) {
  51. put_user_ex(from->si_pid, &to->si_pid);
  52. put_user_ex(from->si_uid, &to->si_uid);
  53. put_user_ex(ptr_to_compat(from->si_ptr), &to->si_ptr);
  54. } else {
  55. /*
  56. * First 32bits of unions are always present:
  57. * si_pid === si_band === si_tid === si_addr(LS half)
  58. */
  59. put_user_ex(from->_sifields._pad[0],
  60. &to->_sifields._pad[0]);
  61. switch (from->si_code >> 16) {
  62. case __SI_FAULT >> 16:
  63. break;
  64. case __SI_SYS >> 16:
  65. put_user_ex(from->si_syscall, &to->si_syscall);
  66. put_user_ex(from->si_arch, &to->si_arch);
  67. break;
  68. case __SI_CHLD >> 16:
  69. if (ia32) {
  70. put_user_ex(from->si_utime, &to->si_utime);
  71. put_user_ex(from->si_stime, &to->si_stime);
  72. } else {
  73. put_user_ex(from->si_utime, &to->_sifields._sigchld_x32._utime);
  74. put_user_ex(from->si_stime, &to->_sifields._sigchld_x32._stime);
  75. }
  76. put_user_ex(from->si_status, &to->si_status);
  77. /* FALL THROUGH */
  78. default:
  79. case __SI_KILL >> 16:
  80. put_user_ex(from->si_uid, &to->si_uid);
  81. break;
  82. case __SI_POLL >> 16:
  83. put_user_ex(from->si_fd, &to->si_fd);
  84. break;
  85. case __SI_TIMER >> 16:
  86. put_user_ex(from->si_overrun, &to->si_overrun);
  87. put_user_ex(ptr_to_compat(from->si_ptr),
  88. &to->si_ptr);
  89. break;
  90. /* This is not generated by the kernel as of now. */
  91. case __SI_RT >> 16:
  92. case __SI_MESGQ >> 16:
  93. put_user_ex(from->si_uid, &to->si_uid);
  94. put_user_ex(from->si_int, &to->si_int);
  95. break;
  96. }
  97. }
  98. } put_user_catch(err);
  99. return err;
  100. }
  101. int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
  102. {
  103. int err = 0;
  104. u32 ptr32;
  105. if (!access_ok(VERIFY_READ, from, sizeof(compat_siginfo_t)))
  106. return -EFAULT;
  107. get_user_try {
  108. get_user_ex(to->si_signo, &from->si_signo);
  109. get_user_ex(to->si_errno, &from->si_errno);
  110. get_user_ex(to->si_code, &from->si_code);
  111. get_user_ex(to->si_pid, &from->si_pid);
  112. get_user_ex(to->si_uid, &from->si_uid);
  113. get_user_ex(ptr32, &from->si_ptr);
  114. to->si_ptr = compat_ptr(ptr32);
  115. } get_user_catch(err);
  116. return err;
  117. }
  118. /*
  119. * Do a signal return; undo the signal stack.
  120. */
  121. #define loadsegment_gs(v) load_gs_index(v)
  122. #define loadsegment_fs(v) loadsegment(fs, v)
  123. #define loadsegment_ds(v) loadsegment(ds, v)
  124. #define loadsegment_es(v) loadsegment(es, v)
  125. #define get_user_seg(seg) ({ unsigned int v; savesegment(seg, v); v; })
  126. #define set_user_seg(seg, v) loadsegment_##seg(v)
  127. #define COPY(x) { \
  128. get_user_ex(regs->x, &sc->x); \
  129. }
  130. #define GET_SEG(seg) ({ \
  131. unsigned short tmp; \
  132. get_user_ex(tmp, &sc->seg); \
  133. tmp; \
  134. })
  135. #define COPY_SEG_CPL3(seg) do { \
  136. regs->seg = GET_SEG(seg) | 3; \
  137. } while (0)
  138. #define RELOAD_SEG(seg) { \
  139. unsigned int pre = GET_SEG(seg); \
  140. unsigned int cur = get_user_seg(seg); \
  141. pre |= 3; \
  142. if (pre != cur) \
  143. set_user_seg(seg, pre); \
  144. }
  145. static int ia32_restore_sigcontext(struct pt_regs *regs,
  146. struct sigcontext_ia32 __user *sc)
  147. {
  148. unsigned int tmpflags, err = 0;
  149. void __user *buf;
  150. u32 tmp;
  151. /* Always make any pending restarted system calls return -EINTR */
  152. current->restart_block.fn = do_no_restart_syscall;
  153. get_user_try {
  154. /*
  155. * Reload fs and gs if they have changed in the signal
  156. * handler. This does not handle long fs/gs base changes in
  157. * the handler, but does not clobber them at least in the
  158. * normal case.
  159. */
  160. RELOAD_SEG(gs);
  161. RELOAD_SEG(fs);
  162. RELOAD_SEG(ds);
  163. RELOAD_SEG(es);
  164. COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
  165. COPY(dx); COPY(cx); COPY(ip); COPY(ax);
  166. /* Don't touch extended registers */
  167. COPY_SEG_CPL3(cs);
  168. COPY_SEG_CPL3(ss);
  169. get_user_ex(tmpflags, &sc->flags);
  170. regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
  171. /* disable syscall checks */
  172. regs->orig_ax = -1;
  173. get_user_ex(tmp, &sc->fpstate);
  174. buf = compat_ptr(tmp);
  175. } get_user_catch(err);
  176. err |= fpu__restore_sig(buf, 1);
  177. force_iret();
  178. return err;
  179. }
  180. asmlinkage long sys32_sigreturn(void)
  181. {
  182. struct pt_regs *regs = current_pt_regs();
  183. struct sigframe_ia32 __user *frame = (struct sigframe_ia32 __user *)(regs->sp-8);
  184. sigset_t set;
  185. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  186. goto badframe;
  187. if (__get_user(set.sig[0], &frame->sc.oldmask)
  188. || (_COMPAT_NSIG_WORDS > 1
  189. && __copy_from_user((((char *) &set.sig) + 4),
  190. &frame->extramask,
  191. sizeof(frame->extramask))))
  192. goto badframe;
  193. set_current_blocked(&set);
  194. if (ia32_restore_sigcontext(regs, &frame->sc))
  195. goto badframe;
  196. return regs->ax;
  197. badframe:
  198. signal_fault(regs, frame, "32bit sigreturn");
  199. return 0;
  200. }
  201. asmlinkage long sys32_rt_sigreturn(void)
  202. {
  203. struct pt_regs *regs = current_pt_regs();
  204. struct rt_sigframe_ia32 __user *frame;
  205. sigset_t set;
  206. frame = (struct rt_sigframe_ia32 __user *)(regs->sp - 4);
  207. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  208. goto badframe;
  209. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  210. goto badframe;
  211. set_current_blocked(&set);
  212. if (ia32_restore_sigcontext(regs, &frame->uc.uc_mcontext))
  213. goto badframe;
  214. if (compat_restore_altstack(&frame->uc.uc_stack))
  215. goto badframe;
  216. return regs->ax;
  217. badframe:
  218. signal_fault(regs, frame, "32bit rt sigreturn");
  219. return 0;
  220. }
  221. /*
  222. * Set up a signal frame.
  223. */
  224. static int ia32_setup_sigcontext(struct sigcontext_ia32 __user *sc,
  225. void __user *fpstate,
  226. struct pt_regs *regs, unsigned int mask)
  227. {
  228. int err = 0;
  229. put_user_try {
  230. put_user_ex(get_user_seg(gs), (unsigned int __user *)&sc->gs);
  231. put_user_ex(get_user_seg(fs), (unsigned int __user *)&sc->fs);
  232. put_user_ex(get_user_seg(ds), (unsigned int __user *)&sc->ds);
  233. put_user_ex(get_user_seg(es), (unsigned int __user *)&sc->es);
  234. put_user_ex(regs->di, &sc->di);
  235. put_user_ex(regs->si, &sc->si);
  236. put_user_ex(regs->bp, &sc->bp);
  237. put_user_ex(regs->sp, &sc->sp);
  238. put_user_ex(regs->bx, &sc->bx);
  239. put_user_ex(regs->dx, &sc->dx);
  240. put_user_ex(regs->cx, &sc->cx);
  241. put_user_ex(regs->ax, &sc->ax);
  242. put_user_ex(current->thread.trap_nr, &sc->trapno);
  243. put_user_ex(current->thread.error_code, &sc->err);
  244. put_user_ex(regs->ip, &sc->ip);
  245. put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
  246. put_user_ex(regs->flags, &sc->flags);
  247. put_user_ex(regs->sp, &sc->sp_at_signal);
  248. put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
  249. put_user_ex(ptr_to_compat(fpstate), &sc->fpstate);
  250. /* non-iBCS2 extensions.. */
  251. put_user_ex(mask, &sc->oldmask);
  252. put_user_ex(current->thread.cr2, &sc->cr2);
  253. } put_user_catch(err);
  254. return err;
  255. }
  256. /*
  257. * Determine which stack to use..
  258. */
  259. static void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
  260. size_t frame_size,
  261. void __user **fpstate)
  262. {
  263. struct fpu *fpu = &current->thread.fpu;
  264. unsigned long sp;
  265. /* Default to using normal stack */
  266. sp = regs->sp;
  267. /* This is the X/Open sanctioned signal stack switching. */
  268. if (ksig->ka.sa.sa_flags & SA_ONSTACK)
  269. sp = sigsp(sp, ksig);
  270. /* This is the legacy signal stack switching. */
  271. else if ((regs->ss & 0xffff) != __USER32_DS &&
  272. !(ksig->ka.sa.sa_flags & SA_RESTORER) &&
  273. ksig->ka.sa.sa_restorer)
  274. sp = (unsigned long) ksig->ka.sa.sa_restorer;
  275. if (fpu->fpstate_active) {
  276. unsigned long fx_aligned, math_size;
  277. sp = fpu__alloc_mathframe(sp, 1, &fx_aligned, &math_size);
  278. *fpstate = (struct _fpstate_ia32 __user *) sp;
  279. if (copy_fpstate_to_sigframe(*fpstate, (void __user *)fx_aligned,
  280. math_size) < 0)
  281. return (void __user *) -1L;
  282. }
  283. sp -= frame_size;
  284. /* Align the stack pointer according to the i386 ABI,
  285. * i.e. so that on function entry ((sp + 4) & 15) == 0. */
  286. sp = ((sp + 4) & -16ul) - 4;
  287. return (void __user *) sp;
  288. }
  289. int ia32_setup_frame(int sig, struct ksignal *ksig,
  290. compat_sigset_t *set, struct pt_regs *regs)
  291. {
  292. struct sigframe_ia32 __user *frame;
  293. void __user *restorer;
  294. int err = 0;
  295. void __user *fpstate = NULL;
  296. /* copy_to_user optimizes that into a single 8 byte store */
  297. static const struct {
  298. u16 poplmovl;
  299. u32 val;
  300. u16 int80;
  301. } __attribute__((packed)) code = {
  302. 0xb858, /* popl %eax ; movl $...,%eax */
  303. __NR_ia32_sigreturn,
  304. 0x80cd, /* int $0x80 */
  305. };
  306. frame = get_sigframe(ksig, regs, sizeof(*frame), &fpstate);
  307. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  308. return -EFAULT;
  309. if (__put_user(sig, &frame->sig))
  310. return -EFAULT;
  311. if (ia32_setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
  312. return -EFAULT;
  313. if (_COMPAT_NSIG_WORDS > 1) {
  314. if (__copy_to_user(frame->extramask, &set->sig[1],
  315. sizeof(frame->extramask)))
  316. return -EFAULT;
  317. }
  318. if (ksig->ka.sa.sa_flags & SA_RESTORER) {
  319. restorer = ksig->ka.sa.sa_restorer;
  320. } else {
  321. /* Return stub is in 32bit vsyscall page */
  322. if (current->mm->context.vdso)
  323. restorer = current->mm->context.vdso +
  324. selected_vdso32->sym___kernel_sigreturn;
  325. else
  326. restorer = &frame->retcode;
  327. }
  328. put_user_try {
  329. put_user_ex(ptr_to_compat(restorer), &frame->pretcode);
  330. /*
  331. * These are actually not used anymore, but left because some
  332. * gdb versions depend on them as a marker.
  333. */
  334. put_user_ex(*((u64 *)&code), (u64 __user *)frame->retcode);
  335. } put_user_catch(err);
  336. if (err)
  337. return -EFAULT;
  338. /* Set up registers for signal handler */
  339. regs->sp = (unsigned long) frame;
  340. regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
  341. /* Make -mregparm=3 work */
  342. regs->ax = sig;
  343. regs->dx = 0;
  344. regs->cx = 0;
  345. loadsegment(ds, __USER32_DS);
  346. loadsegment(es, __USER32_DS);
  347. regs->cs = __USER32_CS;
  348. regs->ss = __USER32_DS;
  349. return 0;
  350. }
  351. int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
  352. compat_sigset_t *set, struct pt_regs *regs)
  353. {
  354. struct rt_sigframe_ia32 __user *frame;
  355. void __user *restorer;
  356. int err = 0;
  357. void __user *fpstate = NULL;
  358. /* __copy_to_user optimizes that into a single 8 byte store */
  359. static const struct {
  360. u8 movl;
  361. u32 val;
  362. u16 int80;
  363. u8 pad;
  364. } __attribute__((packed)) code = {
  365. 0xb8,
  366. __NR_ia32_rt_sigreturn,
  367. 0x80cd,
  368. 0,
  369. };
  370. frame = get_sigframe(ksig, regs, sizeof(*frame), &fpstate);
  371. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  372. return -EFAULT;
  373. put_user_try {
  374. put_user_ex(sig, &frame->sig);
  375. put_user_ex(ptr_to_compat(&frame->info), &frame->pinfo);
  376. put_user_ex(ptr_to_compat(&frame->uc), &frame->puc);
  377. /* Create the ucontext. */
  378. if (cpu_has_xsave)
  379. put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
  380. else
  381. put_user_ex(0, &frame->uc.uc_flags);
  382. put_user_ex(0, &frame->uc.uc_link);
  383. compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
  384. if (ksig->ka.sa.sa_flags & SA_RESTORER)
  385. restorer = ksig->ka.sa.sa_restorer;
  386. else
  387. restorer = current->mm->context.vdso +
  388. selected_vdso32->sym___kernel_rt_sigreturn;
  389. put_user_ex(ptr_to_compat(restorer), &frame->pretcode);
  390. /*
  391. * Not actually used anymore, but left because some gdb
  392. * versions need it.
  393. */
  394. put_user_ex(*((u64 *)&code), (u64 __user *)frame->retcode);
  395. } put_user_catch(err);
  396. err |= copy_siginfo_to_user32(&frame->info, &ksig->info);
  397. err |= ia32_setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
  398. regs, set->sig[0]);
  399. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  400. if (err)
  401. return -EFAULT;
  402. /* Set up registers for signal handler */
  403. regs->sp = (unsigned long) frame;
  404. regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
  405. /* Make -mregparm=3 work */
  406. regs->ax = sig;
  407. regs->dx = (unsigned long) &frame->info;
  408. regs->cx = (unsigned long) &frame->uc;
  409. loadsegment(ds, __USER32_DS);
  410. loadsegment(es, __USER32_DS);
  411. regs->cs = __USER32_CS;
  412. regs->ss = __USER32_DS;
  413. return 0;
  414. }