xref: /openbmc/linux/arch/parisc/kernel/signal32.c (revision 9ac8d3fb)
1 /*    Signal support for 32-bit kernel builds
2  *
3  *    Copyright (C) 2001 Matthew Wilcox <willy at parisc-linux.org>
4  *    Copyright (C) 2006 Kyle McMartin <kyle at parisc-linux.org>
5  *
6  *    Code was mostly borrowed from kernel/signal.c.
7  *    See kernel/signal.c for additional Copyrights.
8  *
9  *
10  *    This program is free software; you can redistribute it and/or modify
11  *    it under the terms of the GNU General Public License as published by
12  *    the Free Software Foundation; either version 2 of the License, or
13  *    (at your option) any later version.
14  *
15  *    This program is distributed in the hope that it will be useful,
16  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *    GNU General Public License for more details.
19  *
20  *    You should have received a copy of the GNU General Public License
21  *    along with this program; if not, write to the Free Software
22  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24 
25 #include <linux/compat.h>
26 #include <linux/slab.h>
27 #include <linux/module.h>
28 #include <linux/unistd.h>
29 #include <linux/init.h>
30 #include <linux/sched.h>
31 #include <linux/syscalls.h>
32 #include <linux/types.h>
33 #include <linux/errno.h>
34 
35 #include <asm/uaccess.h>
36 
37 #include "signal32.h"
38 #include "sys32.h"
39 
40 #define DEBUG_COMPAT_SIG 0
41 #define DEBUG_COMPAT_SIG_LEVEL 2
42 
43 #if DEBUG_COMPAT_SIG
44 #define DBG(LEVEL, ...) \
45 	((DEBUG_COMPAT_SIG_LEVEL >= LEVEL) \
46 	? printk(__VA_ARGS__) : (void) 0)
47 #else
48 #define DBG(LEVEL, ...)
49 #endif
50 
51 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
52 
53 inline void
54 sigset_32to64(sigset_t *s64, compat_sigset_t *s32)
55 {
56 	s64->sig[0] = s32->sig[0] | ((unsigned long)s32->sig[1] << 32);
57 }
58 
59 inline void
60 sigset_64to32(compat_sigset_t *s32, sigset_t *s64)
61 {
62 	s32->sig[0] = s64->sig[0] & 0xffffffffUL;
63 	s32->sig[1] = (s64->sig[0] >> 32) & 0xffffffffUL;
64 }
65 
66 static int
67 put_sigset32(compat_sigset_t __user *up, sigset_t *set, size_t sz)
68 {
69 	compat_sigset_t s;
70 
71 	if (sz != sizeof *set) panic("put_sigset32()");
72 	sigset_64to32(&s, set);
73 
74 	return copy_to_user(up, &s, sizeof s);
75 }
76 
77 static int
78 get_sigset32(compat_sigset_t __user *up, sigset_t *set, size_t sz)
79 {
80 	compat_sigset_t s;
81 	int r;
82 
83 	if (sz != sizeof *set) panic("put_sigset32()");
84 
85 	if ((r = copy_from_user(&s, up, sz)) == 0) {
86 		sigset_32to64(set, &s);
87 	}
88 
89 	return r;
90 }
91 
92 int sys32_rt_sigprocmask(int how, compat_sigset_t __user *set, compat_sigset_t __user *oset,
93 				    unsigned int sigsetsize)
94 {
95 	sigset_t old_set, new_set;
96 	int ret;
97 
98 	if (set && get_sigset32(set, &new_set, sigsetsize))
99 		return -EFAULT;
100 
101 	KERNEL_SYSCALL(ret, sys_rt_sigprocmask, how, set ? (sigset_t __user *)&new_set : NULL,
102 				 oset ? (sigset_t __user *)&old_set : NULL, sigsetsize);
103 
104 	if (!ret && oset && put_sigset32(oset, &old_set, sigsetsize))
105 		return -EFAULT;
106 
107 	return ret;
108 }
109 
110 
111 int sys32_rt_sigpending(compat_sigset_t __user *uset, unsigned int sigsetsize)
112 {
113 	int ret;
114 	sigset_t set;
115 
116 	KERNEL_SYSCALL(ret, sys_rt_sigpending, (sigset_t __user *)&set, sigsetsize);
117 
118 	if (!ret && put_sigset32(uset, &set, sigsetsize))
119 		return -EFAULT;
120 
121 	return ret;
122 }
123 
124 long
125 sys32_rt_sigaction(int sig, const struct sigaction32 __user *act, struct sigaction32 __user *oact,
126                  size_t sigsetsize)
127 {
128 	struct k_sigaction32 new_sa32, old_sa32;
129 	struct k_sigaction new_sa, old_sa;
130 	int ret = -EINVAL;
131 
132 	if (act) {
133 		if (copy_from_user(&new_sa32.sa, act, sizeof new_sa32.sa))
134 			return -EFAULT;
135 		new_sa.sa.sa_handler = (__sighandler_t)(unsigned long)new_sa32.sa.sa_handler;
136 		new_sa.sa.sa_flags = new_sa32.sa.sa_flags;
137 		sigset_32to64(&new_sa.sa.sa_mask, &new_sa32.sa.sa_mask);
138 	}
139 
140 	ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
141 
142 	if (!ret && oact) {
143 		sigset_64to32(&old_sa32.sa.sa_mask, &old_sa.sa.sa_mask);
144 		old_sa32.sa.sa_flags = old_sa.sa.sa_flags;
145 		old_sa32.sa.sa_handler = (__sighandler_t32)(unsigned long)old_sa.sa.sa_handler;
146 		if (copy_to_user(oact, &old_sa32.sa, sizeof old_sa32.sa))
147 			return -EFAULT;
148 	}
149 	return ret;
150 }
151 
152 int
153 do_sigaltstack32 (const compat_stack_t __user *uss32, compat_stack_t __user *uoss32, unsigned long sp)
154 {
155 	compat_stack_t ss32, oss32;
156 	stack_t ss, oss;
157 	stack_t *ssp = NULL, *ossp = NULL;
158 	int ret;
159 
160 	if (uss32) {
161 		if (copy_from_user(&ss32, uss32, sizeof ss32))
162 			return -EFAULT;
163 
164 		ss.ss_sp = (void __user *)(unsigned long)ss32.ss_sp;
165 		ss.ss_flags = ss32.ss_flags;
166 		ss.ss_size = ss32.ss_size;
167 
168 		ssp = &ss;
169 	}
170 
171 	if (uoss32)
172 		ossp = &oss;
173 
174 	KERNEL_SYSCALL(ret, do_sigaltstack, (const stack_t __user *)ssp, (stack_t __user *)ossp, sp);
175 
176 	if (!ret && uoss32) {
177 		oss32.ss_sp = (unsigned int)(unsigned long)oss.ss_sp;
178 		oss32.ss_flags = oss.ss_flags;
179 		oss32.ss_size = oss.ss_size;
180 		if (copy_to_user(uoss32, &oss32, sizeof *uoss32))
181 			return -EFAULT;
182 	}
183 
184 	return ret;
185 }
186 
187 long
188 restore_sigcontext32(struct compat_sigcontext __user *sc, struct compat_regfile __user * rf,
189 		struct pt_regs *regs)
190 {
191 	long err = 0;
192 	compat_uint_t compat_reg;
193 	compat_uint_t compat_regt;
194 	int regn;
195 
196 	/* When loading 32-bit values into 64-bit registers make
197 	   sure to clear the upper 32-bits */
198 	DBG(2,"restore_sigcontext32: PER_LINUX32 process\n");
199 	DBG(2,"restore_sigcontext32: sc = 0x%p, rf = 0x%p, regs = 0x%p\n", sc, rf, regs);
200 	DBG(2,"restore_sigcontext32: compat_sigcontext is %#lx bytes\n", sizeof(*sc));
201 	for(regn=0; regn < 32; regn++){
202 		err |= __get_user(compat_reg,&sc->sc_gr[regn]);
203 		regs->gr[regn] = compat_reg;
204 		/* Load upper half */
205 		err |= __get_user(compat_regt,&rf->rf_gr[regn]);
206 		regs->gr[regn] = ((u64)compat_regt << 32) | (u64)compat_reg;
207 		DBG(3,"restore_sigcontext32: gr%02d = %#lx (%#x / %#x)\n",
208 				regn, regs->gr[regn], compat_regt, compat_reg);
209 	}
210 	DBG(2,"restore_sigcontext32: sc->sc_fr = 0x%p (%#lx)\n",sc->sc_fr, sizeof(sc->sc_fr));
211 	/* XXX: BE WARNED FR's are 64-BIT! */
212 	err |= __copy_from_user(regs->fr, sc->sc_fr, sizeof(regs->fr));
213 
214 	/* Better safe than sorry, pass __get_user two things of
215 	   the same size and let gcc do the upward conversion to
216 	   64-bits */
217 	err |= __get_user(compat_reg, &sc->sc_iaoq[0]);
218 	/* Load upper half */
219 	err |= __get_user(compat_regt, &rf->rf_iaoq[0]);
220 	regs->iaoq[0] = ((u64)compat_regt << 32) | (u64)compat_reg;
221 	DBG(2,"restore_sigcontext32: upper half of iaoq[0] = %#lx\n", compat_regt);
222 	DBG(2,"restore_sigcontext32: sc->sc_iaoq[0] = %p => %#x\n",
223 			&sc->sc_iaoq[0], compat_reg);
224 
225 	err |= __get_user(compat_reg, &sc->sc_iaoq[1]);
226 	/* Load upper half */
227 	err |= __get_user(compat_regt, &rf->rf_iaoq[1]);
228 	regs->iaoq[1] = ((u64)compat_regt << 32) | (u64)compat_reg;
229 	DBG(2,"restore_sigcontext32: upper half of iaoq[1] = %#lx\n", compat_regt);
230 	DBG(2,"restore_sigcontext32: sc->sc_iaoq[1] = %p => %#x\n",
231 			&sc->sc_iaoq[1],compat_reg);
232 	DBG(2,"restore_sigcontext32: iaoq is %#lx / %#lx\n",
233 			regs->iaoq[0],regs->iaoq[1]);
234 
235 	err |= __get_user(compat_reg, &sc->sc_iasq[0]);
236 	/* Load the upper half for iasq */
237 	err |= __get_user(compat_regt, &rf->rf_iasq[0]);
238 	regs->iasq[0] = ((u64)compat_regt << 32) | (u64)compat_reg;
239 	DBG(2,"restore_sigcontext32: upper half of iasq[0] = %#lx\n", compat_regt);
240 
241 	err |= __get_user(compat_reg, &sc->sc_iasq[1]);
242 	/* Load the upper half for iasq */
243 	err |= __get_user(compat_regt, &rf->rf_iasq[1]);
244 	regs->iasq[1] = ((u64)compat_regt << 32) | (u64)compat_reg;
245 	DBG(2,"restore_sigcontext32: upper half of iasq[1] = %#lx\n", compat_regt);
246 	DBG(2,"restore_sigcontext32: iasq is %#lx / %#lx\n",
247 		regs->iasq[0],regs->iasq[1]);
248 
249 	err |= __get_user(compat_reg, &sc->sc_sar);
250 	/* Load the upper half for sar */
251 	err |= __get_user(compat_regt, &rf->rf_sar);
252 	regs->sar = ((u64)compat_regt << 32) | (u64)compat_reg;
253 	DBG(2,"restore_sigcontext32: upper_half & sar = %#lx\n", compat_regt);
254 	DBG(2,"restore_sigcontext32: sar is %#lx\n", regs->sar);
255 	DBG(2,"restore_sigcontext32: r28 is %ld\n", regs->gr[28]);
256 
257 	return err;
258 }
259 
260 /*
261  * Set up the sigcontext structure for this process.
262  * This is not an easy task if the kernel is 64-bit, it will require
263  * that we examine the process personality to determine if we need to
264  * truncate for a 32-bit userspace.
265  */
266 long
267 setup_sigcontext32(struct compat_sigcontext __user *sc, struct compat_regfile __user * rf,
268 		struct pt_regs *regs, int in_syscall)
269 {
270 	compat_int_t flags = 0;
271 	long err = 0;
272 	compat_uint_t compat_reg;
273 	compat_uint_t compat_regb;
274 	int regn;
275 
276 	if (on_sig_stack((unsigned long) sc))
277 		flags |= PARISC_SC_FLAG_ONSTACK;
278 
279 	if (in_syscall) {
280 
281 		DBG(1,"setup_sigcontext32: in_syscall\n");
282 
283 		flags |= PARISC_SC_FLAG_IN_SYSCALL;
284 		/* Truncate gr31 */
285 		compat_reg = (compat_uint_t)(regs->gr[31]);
286 		/* regs->iaoq is undefined in the syscall return path */
287 		err |= __put_user(compat_reg, &sc->sc_iaoq[0]);
288 		DBG(2,"setup_sigcontext32: sc->sc_iaoq[0] = %p <= %#x\n",
289 				&sc->sc_iaoq[0], compat_reg);
290 
291 		/* Store upper half */
292 		compat_reg = (compat_uint_t)(regs->gr[31] >> 32);
293 		err |= __put_user(compat_reg, &rf->rf_iaoq[0]);
294 		DBG(2,"setup_sigcontext32: upper half iaoq[0] = %#x\n", compat_reg);
295 
296 
297 		compat_reg = (compat_uint_t)(regs->gr[31]+4);
298 		err |= __put_user(compat_reg, &sc->sc_iaoq[1]);
299 		DBG(2,"setup_sigcontext32: sc->sc_iaoq[1] = %p <= %#x\n",
300 				&sc->sc_iaoq[1], compat_reg);
301 		/* Store upper half */
302 		compat_reg = (compat_uint_t)((regs->gr[31]+4) >> 32);
303 		err |= __put_user(compat_reg, &rf->rf_iaoq[1]);
304 		DBG(2,"setup_sigcontext32: upper half iaoq[1] = %#x\n", compat_reg);
305 
306 		/* Truncate sr3 */
307 		compat_reg = (compat_uint_t)(regs->sr[3]);
308 		err |= __put_user(compat_reg, &sc->sc_iasq[0]);
309 		err |= __put_user(compat_reg, &sc->sc_iasq[1]);
310 
311 		/* Store upper half */
312 		compat_reg = (compat_uint_t)(regs->sr[3] >> 32);
313 		err |= __put_user(compat_reg, &rf->rf_iasq[0]);
314 		err |= __put_user(compat_reg, &rf->rf_iasq[1]);
315 
316 		DBG(2,"setup_sigcontext32: upper half iasq[0] = %#x\n", compat_reg);
317 		DBG(2,"setup_sigcontext32: upper half iasq[1] = %#x\n", compat_reg);
318 		DBG(1,"setup_sigcontext32: iaoq %#lx / %#lx\n",
319 			regs->gr[31], regs->gr[31]+4);
320 
321 	} else {
322 
323 		compat_reg = (compat_uint_t)(regs->iaoq[0]);
324 		err |= __put_user(compat_reg, &sc->sc_iaoq[0]);
325 		DBG(2,"setup_sigcontext32: sc->sc_iaoq[0] = %p <= %#x\n",
326 				&sc->sc_iaoq[0], compat_reg);
327 		/* Store upper half */
328 		compat_reg = (compat_uint_t)(regs->iaoq[0] >> 32);
329 		err |= __put_user(compat_reg, &rf->rf_iaoq[0]);
330 		DBG(2,"setup_sigcontext32: upper half iaoq[0] = %#x\n", compat_reg);
331 
332 		compat_reg = (compat_uint_t)(regs->iaoq[1]);
333 		err |= __put_user(compat_reg, &sc->sc_iaoq[1]);
334 		DBG(2,"setup_sigcontext32: sc->sc_iaoq[1] = %p <= %#x\n",
335 				&sc->sc_iaoq[1], compat_reg);
336 		/* Store upper half */
337 		compat_reg = (compat_uint_t)(regs->iaoq[1] >> 32);
338 		err |= __put_user(compat_reg, &rf->rf_iaoq[1]);
339 		DBG(2,"setup_sigcontext32: upper half iaoq[1] = %#x\n", compat_reg);
340 
341 
342 		compat_reg = (compat_uint_t)(regs->iasq[0]);
343 		err |= __put_user(compat_reg, &sc->sc_iasq[0]);
344 		DBG(2,"setup_sigcontext32: sc->sc_iasq[0] = %p <= %#x\n",
345 				&sc->sc_iasq[0], compat_reg);
346 		/* Store upper half */
347 		compat_reg = (compat_uint_t)(regs->iasq[0] >> 32);
348 		err |= __put_user(compat_reg, &rf->rf_iasq[0]);
349 		DBG(2,"setup_sigcontext32: upper half iasq[0] = %#x\n", compat_reg);
350 
351 
352 		compat_reg = (compat_uint_t)(regs->iasq[1]);
353 		err |= __put_user(compat_reg, &sc->sc_iasq[1]);
354 		DBG(2,"setup_sigcontext32: sc->sc_iasq[1] = %p <= %#x\n",
355 				&sc->sc_iasq[1], compat_reg);
356 		/* Store upper half */
357 		compat_reg = (compat_uint_t)(regs->iasq[1] >> 32);
358 		err |= __put_user(compat_reg, &rf->rf_iasq[1]);
359 		DBG(2,"setup_sigcontext32: upper half iasq[1] = %#x\n", compat_reg);
360 
361 		/* Print out the IAOQ for debugging */
362 		DBG(1,"setup_sigcontext32: ia0q %#lx / %#lx\n",
363 			regs->iaoq[0], regs->iaoq[1]);
364 	}
365 
366 	err |= __put_user(flags, &sc->sc_flags);
367 
368 	DBG(1,"setup_sigcontext32: Truncating general registers.\n");
369 
370 	for(regn=0; regn < 32; regn++){
371 		/* Truncate a general register */
372 		compat_reg = (compat_uint_t)(regs->gr[regn]);
373 		err |= __put_user(compat_reg, &sc->sc_gr[regn]);
374 		/* Store upper half */
375 		compat_regb = (compat_uint_t)(regs->gr[regn] >> 32);
376 		err |= __put_user(compat_regb, &rf->rf_gr[regn]);
377 
378 		/* DEBUG: Write out the "upper / lower" register data */
379 		DBG(2,"setup_sigcontext32: gr%02d = %#x / %#x\n", regn,
380 				compat_regb, compat_reg);
381 	}
382 
383 	/* Copy the floating point registers (same size)
384 	   XXX: BE WARNED FR's are 64-BIT! */
385 	DBG(1,"setup_sigcontext32: Copying from regs to sc, "
386 	      "sc->sc_fr size = %#lx, regs->fr size = %#lx\n",
387 		sizeof(regs->fr), sizeof(sc->sc_fr));
388 	err |= __copy_to_user(sc->sc_fr, regs->fr, sizeof(regs->fr));
389 
390 	compat_reg = (compat_uint_t)(regs->sar);
391 	err |= __put_user(compat_reg, &sc->sc_sar);
392 	DBG(2,"setup_sigcontext32: sar is %#x\n", compat_reg);
393 	/* Store upper half */
394 	compat_reg = (compat_uint_t)(regs->sar >> 32);
395 	err |= __put_user(compat_reg, &rf->rf_sar);
396 	DBG(2,"setup_sigcontext32: upper half sar = %#x\n", compat_reg);
397 	DBG(1,"setup_sigcontext32: r28 is %ld\n", regs->gr[28]);
398 
399 	return err;
400 }
401 
402 int
403 copy_siginfo_from_user32 (siginfo_t *to, compat_siginfo_t __user *from)
404 {
405 	compat_uptr_t addr;
406 	int err;
407 
408 	if (!access_ok(VERIFY_READ, from, sizeof(compat_siginfo_t)))
409 		return -EFAULT;
410 
411 	err = __get_user(to->si_signo, &from->si_signo);
412 	err |= __get_user(to->si_errno, &from->si_errno);
413 	err |= __get_user(to->si_code, &from->si_code);
414 
415 	if (to->si_code < 0)
416 		err |= __copy_from_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
417 	else {
418 		switch (to->si_code >> 16) {
419 		      case __SI_CHLD >> 16:
420 			err |= __get_user(to->si_utime, &from->si_utime);
421 			err |= __get_user(to->si_stime, &from->si_stime);
422 			err |= __get_user(to->si_status, &from->si_status);
423 		      default:
424 			err |= __get_user(to->si_pid, &from->si_pid);
425 			err |= __get_user(to->si_uid, &from->si_uid);
426 			break;
427 		      case __SI_FAULT >> 16:
428 			err |= __get_user(addr, &from->si_addr);
429 			to->si_addr = compat_ptr(addr);
430 			break;
431 		      case __SI_POLL >> 16:
432 			err |= __get_user(to->si_band, &from->si_band);
433 			err |= __get_user(to->si_fd, &from->si_fd);
434 			break;
435 		      case __SI_RT >> 16: /* This is not generated by the kernel as of now.  */
436 		      case __SI_MESGQ >> 16:
437 			err |= __get_user(to->si_pid, &from->si_pid);
438 			err |= __get_user(to->si_uid, &from->si_uid);
439 			err |= __get_user(to->si_int, &from->si_int);
440 			break;
441 		}
442 	}
443 	return err;
444 }
445 
446 int
447 copy_siginfo_to_user32 (compat_siginfo_t __user *to, siginfo_t *from)
448 {
449 	compat_uptr_t addr;
450 	compat_int_t val;
451 	int err;
452 
453 	if (!access_ok(VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
454 		return -EFAULT;
455 
456 	/* If you change siginfo_t structure, please be sure
457 	   this code is fixed accordingly.
458 	   It should never copy any pad contained in the structure
459 	   to avoid security leaks, but must copy the generic
460 	   3 ints plus the relevant union member.
461 	   This routine must convert siginfo from 64bit to 32bit as well
462 	   at the same time.  */
463 	err = __put_user(from->si_signo, &to->si_signo);
464 	err |= __put_user(from->si_errno, &to->si_errno);
465 	err |= __put_user((short)from->si_code, &to->si_code);
466 	if (from->si_code < 0)
467 		err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
468 	else {
469 		switch (from->si_code >> 16) {
470 		case __SI_CHLD >> 16:
471 			err |= __put_user(from->si_utime, &to->si_utime);
472 			err |= __put_user(from->si_stime, &to->si_stime);
473 			err |= __put_user(from->si_status, &to->si_status);
474 		default:
475 			err |= __put_user(from->si_pid, &to->si_pid);
476 			err |= __put_user(from->si_uid, &to->si_uid);
477 			break;
478 		case __SI_FAULT >> 16:
479 			addr = ptr_to_compat(from->si_addr);
480 			err |= __put_user(addr, &to->si_addr);
481 			break;
482 		case __SI_POLL >> 16:
483 			err |= __put_user(from->si_band, &to->si_band);
484 			err |= __put_user(from->si_fd, &to->si_fd);
485 			break;
486 		case __SI_TIMER >> 16:
487 			err |= __put_user(from->si_tid, &to->si_tid);
488 			err |= __put_user(from->si_overrun, &to->si_overrun);
489 			val = (compat_int_t)from->si_int;
490 			err |= __put_user(val, &to->si_int);
491 			break;
492 		case __SI_RT >> 16:	/* Not generated by the kernel as of now.  */
493 		case __SI_MESGQ >> 16:
494 			err |= __put_user(from->si_uid, &to->si_uid);
495 			err |= __put_user(from->si_pid, &to->si_pid);
496 			val = (compat_int_t)from->si_int;
497 			err |= __put_user(val, &to->si_int);
498 			break;
499 		}
500 	}
501 	return err;
502 }
503 
504 asmlinkage long compat_sys_rt_sigqueueinfo(int pid, int sig,
505 	struct compat_siginfo __user *uinfo)
506 {
507 	siginfo_t info;
508 
509 	if (copy_siginfo_from_user32(&info, uinfo))
510 		return -EFAULT;
511 
512 	/* Not even root can pretend to send signals from the kernel.
513 	   Nor can they impersonate a kill(), which adds source info.  */
514 	if (info.si_code >= 0)
515 		return -EPERM;
516 	info.si_signo = sig;
517 
518 	/* POSIX.1b doesn't mention process groups.  */
519 	return kill_proc_info(sig, &info, pid);
520 }
521 
522