1 /*
2  *  linux/arch/x86_64/kernel/signal.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *  Copyright (C) 2000, 2001  SuSE Labs
6  *
7  *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
8  *  2000-06-20  Pentium III FXSR, SSE support by Gareth Hughes
9  *  2000-2001   x86-64 support by Andi Kleen
10  *
11  *  $Id: signal.c,v 1.43 2003/09/06 18:10:44 ak Exp $
12  */
13 
14 #include <linux/sched.h>
15 #include <linux/mm.h>
16 #include <linux/smp.h>
17 #include <linux/smp_lock.h>
18 #include <linux/kernel.h>
19 #include <linux/signal.h>
20 #include <linux/errno.h>
21 #include <linux/wait.h>
22 #include <linux/ptrace.h>
23 #include <linux/unistd.h>
24 #include <linux/stddef.h>
25 #include <linux/tty.h>
26 #include <linux/personality.h>
27 #include <linux/compiler.h>
28 #include <linux/binfmts.h>
29 #include <asm/ucontext.h>
30 #include <asm/uaccess.h>
31 #include <asm/i387.h>
32 #include <asm/proto.h>
33 
34 #define DEBUG_SIG 0
35 
36 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
37 
38 asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset);
39 
40 void ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
41                sigset_t *set, struct pt_regs * regs);
42 void ia32_setup_frame(int sig, struct k_sigaction *ka,
43             sigset_t *set, struct pt_regs * regs);
44 void signal_fault(struct pt_regs *regs, void *frame, char *where);
45 extern int exception_trace;
46 
copy_siginfo_to_user(siginfo_t * to,siginfo_t * from)47 int copy_siginfo_to_user(siginfo_t *to, siginfo_t *from)
48 {
49 	if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
50 		return -EFAULT;
51 	if (from->si_code < 0)
52 		return __copy_to_user(to, from, sizeof(siginfo_t));
53 	else {
54 		int err;
55 
56 		/* If you change siginfo_t structure, please be sure
57 		   this code is fixed accordingly.
58 		   It should never copy any pad contained in the structure
59 		   to avoid security leaks, but must copy the generic
60 		   3 ints plus the relevant union member.  */
61 		err = __put_user(from->si_signo, &to->si_signo);
62 		err |= __put_user(from->si_errno, &to->si_errno);
63 		err |= __put_user((short)from->si_code, &to->si_code);
64 		/* First 32bits of unions are always present.  */
65 		err |= __put_user(from->si_pid, &to->si_pid);
66 		switch (from->si_code >> 16) {
67 		case __SI_CHLD >> 16:
68 			err |= __put_user(from->si_utime, &to->si_utime);
69 			err |= __put_user(from->si_stime, &to->si_stime);
70 			err |= __put_user(from->si_status, &to->si_status);
71 		default:
72 			err |= __put_user(from->si_uid, &to->si_uid);
73 			break;
74 		case __SI_POLL >> 16:
75 			err |= __put_user(from->si_band, &to->si_band);
76 			err |= __put_user(from->si_fd, &to->si_fd);
77 			break;
78 		/* case __SI_RT: This is not generated by the kernel as of now.  */
79 		}
80 		return err;
81 	}
82 }
83 
84 asmlinkage long
sys_rt_sigsuspend(sigset_t * unewset,size_t sigsetsize,struct pt_regs regs)85 sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize, struct pt_regs regs)
86 {
87 	sigset_t saveset, newset;
88 
89 	/* XXX: Don't preclude handling different sized sigset_t's.  */
90 	if (sigsetsize != sizeof(sigset_t))
91 		return -EINVAL;
92 
93 	if (copy_from_user(&newset, unewset, sizeof(newset)))
94 		return -EFAULT;
95 	sigdelsetmask(&newset, ~_BLOCKABLE);
96 
97 	spin_lock_irq(&current->sigmask_lock);
98 	saveset = current->blocked;
99 	current->blocked = newset;
100 	recalc_sigpending(current);
101 	spin_unlock_irq(&current->sigmask_lock);
102 #if DEBUG_SIG
103 	printk("rt_sigsuspend savset(%lx) newset(%lx) regs(%p) rip(%lx)\n",
104 		saveset, newset, &regs, regs.rip);
105 #endif
106 	regs.rax = -EINTR;
107 	while (1) {
108 		current->state = TASK_INTERRUPTIBLE;
109 		schedule();
110 		if (do_signal(&regs, &saveset))
111 			return -EINTR;
112 	}
113 }
114 
115 asmlinkage long
sys_sigaltstack(const stack_t * uss,stack_t * uoss,struct pt_regs regs)116 sys_sigaltstack(const stack_t *uss, stack_t *uoss, struct pt_regs regs)
117 {
118 	return do_sigaltstack(uss, uoss, regs.rsp);
119 }
120 
121 
122 /*
123  * Do a signal return; undo the signal stack.
124  */
125 
126 struct rt_sigframe
127 {
128 	char *pretcode;
129 	struct ucontext uc;
130 	struct siginfo info;
131 };
132 
133 static int
restore_sigcontext(struct pt_regs * regs,struct sigcontext * sc,unsigned long * prax)134 restore_sigcontext(struct pt_regs *regs, struct sigcontext *sc, unsigned long *prax)
135 {
136 	unsigned int err = 0;
137 
138 
139 #define COPY(x)		err |= __get_user(regs->x, &sc->x)
140 
141 	/* fs and gs are ignored because we cannot handle the 64bit base easily */
142 
143 	COPY(rdi); COPY(rsi); COPY(rbp); COPY(rsp); COPY(rbx);
144 	COPY(rdx); COPY(rcx);
145 	COPY(rip);
146 	if (regs->rip >= TASK_SIZE && regs->rip < VSYSCALL_START) {
147 		regs->rip = 0;
148 		return -EFAULT;
149 	}
150 	COPY(r8);
151 	COPY(r9);
152 	COPY(r10);
153 	COPY(r11);
154 	COPY(r12);
155 	COPY(r13);
156 	COPY(r14);
157 	COPY(r15);
158 
159 	/* do not copy CS/SS because 64bit should not need it.
160 	   also need IRET exception handling anyways. */
161 
162 	{
163 		unsigned int tmpflags;
164 		err |= __get_user(tmpflags, &sc->eflags);
165 		regs->eflags = (regs->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
166 		regs->orig_rax = -1;		/* disable syscall checks */
167 	}
168 
169 	{
170 		struct _fpstate * buf;
171 		err |= __get_user(buf, &sc->fpstate);
172 		if (buf) {
173 			if (unlikely(verify_area(VERIFY_READ, buf, sizeof(*buf))))
174 				return 1;
175 			err |= restore_i387(buf);
176 		}
177 	}
178 
179 	err |= __get_user(*prax, &sc->rax);
180 	return err;
181 }
182 #undef COPY
183 
sys_rt_sigreturn(struct pt_regs regs)184 asmlinkage long sys_rt_sigreturn(struct pt_regs regs)
185 {
186 	struct rt_sigframe *frame = (struct rt_sigframe *)(regs.rsp - 8);
187 	sigset_t set;
188 	stack_t st;
189 	long eax;
190 
191 	if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
192 		goto badframe;
193 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
194 		goto badframe;
195 
196 	sigdelsetmask(&set, ~_BLOCKABLE);
197 	spin_lock_irq(&current->sigmask_lock);
198 	current->blocked = set;
199 	recalc_sigpending(current);
200 	spin_unlock_irq(&current->sigmask_lock);
201 
202 	if (restore_sigcontext(&regs, &frame->uc.uc_mcontext, &eax))
203 		goto badframe;
204 
205 #if DEBUG_SIG
206 	printk("%d sigreturn rip:%lx rsp:%lx frame:%p rax:%lx\n",current->pid,regs.rip,regs.rsp,frame,eax);
207 #endif
208 
209 	if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
210 		goto badframe;
211 	/* It is more difficult to avoid calling this function than to
212 	   call it and ignore errors.  */
213 	do_sigaltstack(&st, NULL, regs.rsp);
214 
215 	return eax;
216 
217 badframe:
218 	signal_fault(&regs, frame, "rt_sigreturn");
219 	return 0;
220 }
221 
222 /*
223  * Set up a signal frame.
224  */
225 
226 static int
setup_sigcontext(struct sigcontext * sc,struct pt_regs * regs,unsigned long mask)227 setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs, unsigned long mask)
228 {
229 	int tmp, err = 0;
230 	struct task_struct *me = current;
231 
232 	tmp = 0;
233 	err |= __put_user(0, &sc->gs);
234 	err |= __put_user(0, &sc->fs);
235 
236 	err |= __put_user(regs->rdi, &sc->rdi);
237 	err |= __put_user(regs->rsi, &sc->rsi);
238 	err |= __put_user(regs->rbp, &sc->rbp);
239 	err |= __put_user(regs->rsp, &sc->rsp);
240 	err |= __put_user(regs->rbx, &sc->rbx);
241 	err |= __put_user(regs->rdx, &sc->rdx);
242 	err |= __put_user(regs->rcx, &sc->rcx);
243 	err |= __put_user(regs->rax, &sc->rax);
244 	err |= __put_user(regs->r8, &sc->r8);
245 	err |= __put_user(regs->r9, &sc->r9);
246 	err |= __put_user(regs->r10, &sc->r10);
247 	err |= __put_user(regs->r11, &sc->r11);
248 	err |= __put_user(regs->r12, &sc->r12);
249 	err |= __put_user(regs->r13, &sc->r13);
250 	err |= __put_user(regs->r14, &sc->r14);
251 	err |= __put_user(regs->r15, &sc->r15);
252 	err |= __put_user(me->thread.trap_no, &sc->trapno);
253 	err |= __put_user(me->thread.error_code, &sc->err);
254 	err |= __put_user(regs->rip, &sc->rip);
255 	err |= __put_user(regs->cs, &sc->cs);
256 	err |= __put_user(regs->eflags, &sc->eflags);
257 	err |= __put_user(mask, &sc->oldmask);
258 	err |= __put_user(me->thread.cr2, &sc->cr2);
259 
260 	return err;
261 }
262 
263 static void *
get_stack(struct k_sigaction * ka,struct pt_regs * regs,unsigned long size)264 get_stack(struct k_sigaction *ka, struct pt_regs *regs, unsigned long size)
265 {
266 	unsigned long rsp;
267 
268 	/* Default to using normal stack - redzone*/
269 	rsp = regs->rsp - 128;
270 
271 	/* This is the X/Open sanctioned signal stack switching.  */
272 	/* may need to subtract redzone there too */
273 	if (ka->sa.sa_flags & SA_ONSTACK) {
274 		if (sas_ss_flags(rsp) == 0)
275 			rsp = current->sas_ss_sp + current->sas_ss_size;
276 	}
277 
278 	return (void *)round_down(rsp - size, 16);
279 }
280 
setup_rt_frame(int sig,struct k_sigaction * ka,siginfo_t * info,sigset_t * set,struct pt_regs * regs)281 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
282 			   sigset_t *set, struct pt_regs * regs)
283 {
284 	struct rt_sigframe *frame;
285 	struct _fpstate *fp;
286 	int err = 0;
287 
288 	if (current->used_math) {
289 		fp = get_stack(ka, regs, sizeof(struct _fpstate));
290 		frame = (void *)round_down((unsigned long)fp - sizeof(struct rt_sigframe), 16) - 8;
291 
292 		if (!access_ok(VERIFY_WRITE, fp, sizeof(struct _fpstate))) {
293 			goto give_sigsegv;
294 		}
295 		if (save_i387(fp) < 0)
296 			err |= -1;
297 	} else {
298 		frame = get_stack(ka, regs, sizeof(struct rt_sigframe)) - 8;
299 		fp = NULL;
300 	}
301 
302 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) {
303 		goto give_sigsegv;
304 	}
305 
306 
307 	if (ka->sa.sa_flags & SA_SIGINFO) {
308 		err |= copy_siginfo_to_user(&frame->info, info);
309 		if (err)
310 			goto give_sigsegv;
311 	}
312 
313 	/* Create the ucontext.  */
314 	err |= __put_user(0, &frame->uc.uc_flags);
315 	err |= __put_user(0, &frame->uc.uc_link);
316 	err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
317 	err |= __put_user(sas_ss_flags(regs->rsp),
318 			  &frame->uc.uc_stack.ss_flags);
319 	err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
320 	err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
321 	err |= __put_user(fp, &frame->uc.uc_mcontext.fpstate);
322 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
323 
324 	/* Set up to return from userspace.  If provided, use a stub
325 	   already in userspace.  */
326 	/* x86-64 should always use SA_RESTORER. */
327 	if (ka->sa.sa_flags & SA_RESTORER) {
328 		err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
329 	} else {
330 		printk("%s forgot to set SA_RESTORER for signal %d.\n", current->comm, sig);
331 		goto give_sigsegv;
332 	}
333 
334 	if (err)
335 		goto give_sigsegv;
336 
337 #if DEBUG_SIG
338 	printk("%d old rip %lx old rsp %lx old rax %lx\n", current->pid,regs->rip,regs->rsp,regs->rax);
339 #endif
340 
341 	/* Set up registers for signal handler */
342 	{
343 		struct exec_domain *ed = current->exec_domain;
344 		if (unlikely(ed && ed->signal_invmap && sig < 32))
345 			sig = ed->signal_invmap[sig];
346 	}
347 	regs->rdi = sig;
348 
349 	/* could reload DS/ES to __USER_DS here, but assume for now
350 	   that 64bit does not care */
351 
352 	/* In case the signal handler was declared without prototypes */
353 	regs->rax = 0;
354 
355 	/* This also works for non SA_SIGINFO handlers because they expect the
356 	   next argument after the signal number on the stack. */
357 	regs->rsi = (unsigned long)&frame->info;
358 	regs->rdx = (unsigned long)&frame->uc;
359 	regs->rsp = (unsigned long) frame;
360 	regs->rip = (unsigned long) ka->sa.sa_handler;
361 	if (regs->rip >= TASK_SIZE) {
362 		if (sig == SIGSEGV)
363 			ka->sa.sa_handler = SIG_DFL;
364 		regs->rip = 0;
365 	}
366 	regs->cs = __USER_CS;
367 	regs->ss = __USER_DS;
368 
369 	set_fs(USER_DS);
370 	regs->eflags &= ~TF_MASK;
371 
372 #if DEBUG_SIG
373 	printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
374 		current->comm, current->pid, frame, regs->rip, frame->pretcode);
375 #endif
376 
377 	return;
378 
379 give_sigsegv:
380 	if (sig == SIGSEGV)
381 		ka->sa.sa_handler = SIG_DFL;
382 	signal_fault(regs, frame, "signal deliver");
383 }
384 
385 /*
386  * OK, we're invoking a handler
387  */
388 
389 static void
handle_signal(unsigned long sig,struct k_sigaction * ka,siginfo_t * info,sigset_t * oldset,struct pt_regs * regs)390 handle_signal(unsigned long sig, struct k_sigaction *ka,
391 	      siginfo_t *info, sigset_t *oldset, struct pt_regs * regs)
392 {
393 #if DEBUG_SIG
394 	printk("handle_signal pid:%d sig:%lu rip:%lx rsp:%lx regs=%p\n", current->pid, sig,
395 		regs->rip, regs->rsp, regs);
396 #endif
397 
398 	/* Are we from a system call? */
399 	if ((long)regs->orig_rax >= 0) {
400 		/* If so, check system call restarting.. */
401 		switch (regs->rax) {
402 			case -ERESTARTNOHAND:
403 				regs->rax = -EINTR;
404 				break;
405 
406 			case -ERESTARTSYS:
407 				if (!(ka->sa.sa_flags & SA_RESTART)) {
408 					regs->rax = -EINTR;
409 					break;
410 				}
411 			/* fallthrough */
412 			case -ERESTARTNOINTR:
413 				regs->rax = regs->orig_rax;
414 				regs->rip -= 2;
415 				break;
416 		}
417 	}
418 
419 #ifdef CONFIG_IA32_EMULATION
420 	if (current->thread.flags & THREAD_IA32) {
421 		if (ka->sa.sa_flags & SA_SIGINFO)
422 			ia32_setup_rt_frame(sig, ka, info, oldset, regs);
423 		else
424 			ia32_setup_frame(sig, ka, oldset, regs);
425 	} else
426 #endif
427 	setup_rt_frame(sig, ka, info, oldset, regs);
428 
429 	if (ka->sa.sa_flags & SA_ONESHOT)
430 		ka->sa.sa_handler = SIG_DFL;
431 
432 	if (!(ka->sa.sa_flags & SA_NODEFER)) {
433 		spin_lock_irq(&current->sigmask_lock);
434 		sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
435 		sigaddset(&current->blocked,sig);
436 		recalc_sigpending(current);
437 		spin_unlock_irq(&current->sigmask_lock);
438 	}
439 }
440 
441 /*
442  * Note that 'init' is a special process: it doesn't get signals it doesn't
443  * want to handle. Thus you cannot kill init even with a SIGKILL even by
444  * mistake.
445  */
do_signal(struct pt_regs * regs,sigset_t * oldset)446 int do_signal(struct pt_regs *regs, sigset_t *oldset)
447 {
448 	siginfo_t info;
449 	struct k_sigaction *ka;
450 
451 	/*
452 	 * We want the common case to go fast, which
453 	 * is why we may in certain cases get here from
454 	 * kernel mode. Just return without doing anything
455 	 * if so.
456 	 */
457 	if ((regs->cs & 3) != 3) {
458 		return 1;
459 	}
460 
461 	if (!oldset)
462 		oldset = &current->blocked;
463 
464 	for (;;) {
465 		unsigned long signr;
466 
467 		spin_lock_irq(&current->sigmask_lock);
468 		signr = dequeue_signal(&current->blocked, &info);
469 		spin_unlock_irq(&current->sigmask_lock);
470 
471 		if (!signr) {
472 			break;
473 		}
474 
475 		if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) {
476 			/* Let the debugger run.  */
477 			current->exit_code = signr;
478 			current->state = TASK_STOPPED;
479 			notify_parent(current, SIGCHLD);
480 			schedule();
481 
482 			/* We're back.  Did the debugger cancel the sig?  */
483 			if (!(signr = current->exit_code))
484 				continue;
485 			current->exit_code = 0;
486 
487 			/* The debugger continued.  Ignore SIGSTOP.  */
488 			if (signr == SIGSTOP)
489 				continue;
490 
491 			/* Update the siginfo structure.  Is this good?  */
492 			if (signr != info.si_signo) {
493 				info.si_signo = signr;
494 				info.si_errno = 0;
495 				info.si_code = SI_USER;
496 				info.si_pid = current->p_pptr->pid;
497 				info.si_uid = current->p_pptr->uid;
498 			}
499 
500 			/* If the (new) signal is now blocked, requeue it.  */
501 			if (sigismember(&current->blocked, signr)) {
502 				send_sig_info(signr, &info, current);
503 				continue;
504 			}
505 		}
506 
507 		ka = &current->sig->action[signr-1];
508 		if (ka->sa.sa_handler == SIG_IGN) {
509 			if (signr != SIGCHLD)
510 				continue;
511 			/* Check for SIGCHLD: it's special.  */
512 			while (sys_wait4(-1, NULL, WNOHANG, NULL) > 0)
513 				/* nothing */;
514 			continue;
515 		}
516 
517 		if (ka->sa.sa_handler == SIG_DFL) {
518 			int exit_code = signr;
519 
520 			/* Init gets no signals it doesn't want.  */
521 			if (current->pid == 1)
522 				continue;
523 
524 			switch (signr) {
525 			case SIGCONT: case SIGCHLD: case SIGWINCH: case SIGURG:
526 				continue;
527 
528 			case SIGTSTP: case SIGTTIN: case SIGTTOU:
529 				if (is_orphaned_pgrp(current->pgrp))
530 					continue;
531 				/* FALLTHRU */
532 
533 			case SIGSTOP: {
534 				struct signal_struct *sig;
535 				current->state = TASK_STOPPED;
536 				current->exit_code = signr;
537 				sig = current->p_pptr->sig;
538 				if (sig && !(sig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
539 					notify_parent(current, SIGCHLD);
540 				schedule();
541 				continue;
542 			}
543 
544 			case SIGQUIT: case SIGILL: case SIGTRAP:
545 			case SIGABRT: case SIGFPE: case SIGSEGV:
546 			case SIGBUS: case SIGSYS: case SIGXCPU: case SIGXFSZ:
547 				if (do_coredump(signr, regs))
548 					exit_code |= 0x80;
549 				/* FALLTHRU */
550 
551 			default:
552 				sig_exit(signr, exit_code, &info);
553 				/* NOTREACHED */
554 			}
555 		}
556 
557 		/* Reenable any watchpoints before delivering the
558 		 * signal to user space. The processor register will
559 		 * have been cleared if the watchpoint triggered
560 		 * inside the kernel.
561 		 */
562 		if (current->thread.debugreg[7])
563 			asm volatile("movq %0,%%db7" :: "r" (current->thread.debugreg[7]));
564 		/* Whee!  Actually deliver the signal.  */
565 		handle_signal(signr, ka, &info, oldset, regs);
566 		return 1;
567 	}
568 
569 	/* Did we come from a system call? */
570 	if ((long)regs->orig_rax >= 0) {
571 		/* Restart the system call - no handlers present */
572 		if (regs->rax == -ERESTARTNOHAND ||
573 		    regs->rax == -ERESTARTSYS ||
574 		    regs->rax == -ERESTARTNOINTR) {
575 			regs->rax = regs->orig_rax;
576 			regs->rip -= 2;
577 		}
578 	}
579 	return 0;
580 }
581 
582 
signal_fault(struct pt_regs * regs,void * frame,char * where)583 void signal_fault(struct pt_regs *regs, void *frame, char *where)
584 {
585 	struct task_struct *me = current;
586 	if (exception_trace)
587 		printk("%s[%d] bad frame in %s frame:%p rip:%lx rsp:%lx orax:%lx\n",
588 	       me->comm,me->pid,where,frame,regs->rip,regs->rsp,regs->orig_rax);
589 
590 	force_sig(SIGSEGV, me);
591 }
592