xref: /openbmc/linux/kernel/compat.c (revision 9cfc5c90)
1 /*
2  *  linux/kernel/compat.c
3  *
4  *  Kernel compatibililty routines for e.g. 32 bit syscall support
5  *  on 64 bit kernels.
6  *
7  *  Copyright (C) 2002-2003 Stephen Rothwell, IBM Corporation
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License version 2 as
11  *  published by the Free Software Foundation.
12  */
13 
14 #include <linux/linkage.h>
15 #include <linux/compat.h>
16 #include <linux/errno.h>
17 #include <linux/time.h>
18 #include <linux/signal.h>
19 #include <linux/sched.h>	/* for MAX_SCHEDULE_TIMEOUT */
20 #include <linux/syscalls.h>
21 #include <linux/unistd.h>
22 #include <linux/security.h>
23 #include <linux/timex.h>
24 #include <linux/export.h>
25 #include <linux/migrate.h>
26 #include <linux/posix-timers.h>
27 #include <linux/times.h>
28 #include <linux/ptrace.h>
29 #include <linux/gfp.h>
30 
31 #include <asm/uaccess.h>
32 
33 static int compat_get_timex(struct timex *txc, struct compat_timex __user *utp)
34 {
35 	memset(txc, 0, sizeof(struct timex));
36 
37 	if (!access_ok(VERIFY_READ, utp, sizeof(struct compat_timex)) ||
38 			__get_user(txc->modes, &utp->modes) ||
39 			__get_user(txc->offset, &utp->offset) ||
40 			__get_user(txc->freq, &utp->freq) ||
41 			__get_user(txc->maxerror, &utp->maxerror) ||
42 			__get_user(txc->esterror, &utp->esterror) ||
43 			__get_user(txc->status, &utp->status) ||
44 			__get_user(txc->constant, &utp->constant) ||
45 			__get_user(txc->precision, &utp->precision) ||
46 			__get_user(txc->tolerance, &utp->tolerance) ||
47 			__get_user(txc->time.tv_sec, &utp->time.tv_sec) ||
48 			__get_user(txc->time.tv_usec, &utp->time.tv_usec) ||
49 			__get_user(txc->tick, &utp->tick) ||
50 			__get_user(txc->ppsfreq, &utp->ppsfreq) ||
51 			__get_user(txc->jitter, &utp->jitter) ||
52 			__get_user(txc->shift, &utp->shift) ||
53 			__get_user(txc->stabil, &utp->stabil) ||
54 			__get_user(txc->jitcnt, &utp->jitcnt) ||
55 			__get_user(txc->calcnt, &utp->calcnt) ||
56 			__get_user(txc->errcnt, &utp->errcnt) ||
57 			__get_user(txc->stbcnt, &utp->stbcnt))
58 		return -EFAULT;
59 
60 	return 0;
61 }
62 
63 static int compat_put_timex(struct compat_timex __user *utp, struct timex *txc)
64 {
65 	if (!access_ok(VERIFY_WRITE, utp, sizeof(struct compat_timex)) ||
66 			__put_user(txc->modes, &utp->modes) ||
67 			__put_user(txc->offset, &utp->offset) ||
68 			__put_user(txc->freq, &utp->freq) ||
69 			__put_user(txc->maxerror, &utp->maxerror) ||
70 			__put_user(txc->esterror, &utp->esterror) ||
71 			__put_user(txc->status, &utp->status) ||
72 			__put_user(txc->constant, &utp->constant) ||
73 			__put_user(txc->precision, &utp->precision) ||
74 			__put_user(txc->tolerance, &utp->tolerance) ||
75 			__put_user(txc->time.tv_sec, &utp->time.tv_sec) ||
76 			__put_user(txc->time.tv_usec, &utp->time.tv_usec) ||
77 			__put_user(txc->tick, &utp->tick) ||
78 			__put_user(txc->ppsfreq, &utp->ppsfreq) ||
79 			__put_user(txc->jitter, &utp->jitter) ||
80 			__put_user(txc->shift, &utp->shift) ||
81 			__put_user(txc->stabil, &utp->stabil) ||
82 			__put_user(txc->jitcnt, &utp->jitcnt) ||
83 			__put_user(txc->calcnt, &utp->calcnt) ||
84 			__put_user(txc->errcnt, &utp->errcnt) ||
85 			__put_user(txc->stbcnt, &utp->stbcnt) ||
86 			__put_user(txc->tai, &utp->tai))
87 		return -EFAULT;
88 	return 0;
89 }
90 
91 COMPAT_SYSCALL_DEFINE2(gettimeofday, struct compat_timeval __user *, tv,
92 		       struct timezone __user *, tz)
93 {
94 	if (tv) {
95 		struct timeval ktv;
96 		do_gettimeofday(&ktv);
97 		if (compat_put_timeval(&ktv, tv))
98 			return -EFAULT;
99 	}
100 	if (tz) {
101 		if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
102 			return -EFAULT;
103 	}
104 
105 	return 0;
106 }
107 
108 COMPAT_SYSCALL_DEFINE2(settimeofday, struct compat_timeval __user *, tv,
109 		       struct timezone __user *, tz)
110 {
111 	struct timeval user_tv;
112 	struct timespec	new_ts;
113 	struct timezone new_tz;
114 
115 	if (tv) {
116 		if (compat_get_timeval(&user_tv, tv))
117 			return -EFAULT;
118 		new_ts.tv_sec = user_tv.tv_sec;
119 		new_ts.tv_nsec = user_tv.tv_usec * NSEC_PER_USEC;
120 	}
121 	if (tz) {
122 		if (copy_from_user(&new_tz, tz, sizeof(*tz)))
123 			return -EFAULT;
124 	}
125 
126 	return do_sys_settimeofday(tv ? &new_ts : NULL, tz ? &new_tz : NULL);
127 }
128 
129 static int __compat_get_timeval(struct timeval *tv, const struct compat_timeval __user *ctv)
130 {
131 	return (!access_ok(VERIFY_READ, ctv, sizeof(*ctv)) ||
132 			__get_user(tv->tv_sec, &ctv->tv_sec) ||
133 			__get_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0;
134 }
135 
136 static int __compat_put_timeval(const struct timeval *tv, struct compat_timeval __user *ctv)
137 {
138 	return (!access_ok(VERIFY_WRITE, ctv, sizeof(*ctv)) ||
139 			__put_user(tv->tv_sec, &ctv->tv_sec) ||
140 			__put_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0;
141 }
142 
143 static int __compat_get_timespec(struct timespec *ts, const struct compat_timespec __user *cts)
144 {
145 	return (!access_ok(VERIFY_READ, cts, sizeof(*cts)) ||
146 			__get_user(ts->tv_sec, &cts->tv_sec) ||
147 			__get_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
148 }
149 
150 static int __compat_put_timespec(const struct timespec *ts, struct compat_timespec __user *cts)
151 {
152 	return (!access_ok(VERIFY_WRITE, cts, sizeof(*cts)) ||
153 			__put_user(ts->tv_sec, &cts->tv_sec) ||
154 			__put_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
155 }
156 
157 int compat_get_timeval(struct timeval *tv, const void __user *utv)
158 {
159 	if (COMPAT_USE_64BIT_TIME)
160 		return copy_from_user(tv, utv, sizeof(*tv)) ? -EFAULT : 0;
161 	else
162 		return __compat_get_timeval(tv, utv);
163 }
164 EXPORT_SYMBOL_GPL(compat_get_timeval);
165 
166 int compat_put_timeval(const struct timeval *tv, void __user *utv)
167 {
168 	if (COMPAT_USE_64BIT_TIME)
169 		return copy_to_user(utv, tv, sizeof(*tv)) ? -EFAULT : 0;
170 	else
171 		return __compat_put_timeval(tv, utv);
172 }
173 EXPORT_SYMBOL_GPL(compat_put_timeval);
174 
175 int compat_get_timespec(struct timespec *ts, const void __user *uts)
176 {
177 	if (COMPAT_USE_64BIT_TIME)
178 		return copy_from_user(ts, uts, sizeof(*ts)) ? -EFAULT : 0;
179 	else
180 		return __compat_get_timespec(ts, uts);
181 }
182 EXPORT_SYMBOL_GPL(compat_get_timespec);
183 
184 int compat_put_timespec(const struct timespec *ts, void __user *uts)
185 {
186 	if (COMPAT_USE_64BIT_TIME)
187 		return copy_to_user(uts, ts, sizeof(*ts)) ? -EFAULT : 0;
188 	else
189 		return __compat_put_timespec(ts, uts);
190 }
191 EXPORT_SYMBOL_GPL(compat_put_timespec);
192 
193 int compat_convert_timespec(struct timespec __user **kts,
194 			    const void __user *cts)
195 {
196 	struct timespec ts;
197 	struct timespec __user *uts;
198 
199 	if (!cts || COMPAT_USE_64BIT_TIME) {
200 		*kts = (struct timespec __user *)cts;
201 		return 0;
202 	}
203 
204 	uts = compat_alloc_user_space(sizeof(ts));
205 	if (!uts)
206 		return -EFAULT;
207 	if (compat_get_timespec(&ts, cts))
208 		return -EFAULT;
209 	if (copy_to_user(uts, &ts, sizeof(ts)))
210 		return -EFAULT;
211 
212 	*kts = uts;
213 	return 0;
214 }
215 
216 static long compat_nanosleep_restart(struct restart_block *restart)
217 {
218 	struct compat_timespec __user *rmtp;
219 	struct timespec rmt;
220 	mm_segment_t oldfs;
221 	long ret;
222 
223 	restart->nanosleep.rmtp = (struct timespec __user *) &rmt;
224 	oldfs = get_fs();
225 	set_fs(KERNEL_DS);
226 	ret = hrtimer_nanosleep_restart(restart);
227 	set_fs(oldfs);
228 
229 	if (ret == -ERESTART_RESTARTBLOCK) {
230 		rmtp = restart->nanosleep.compat_rmtp;
231 
232 		if (rmtp && compat_put_timespec(&rmt, rmtp))
233 			return -EFAULT;
234 	}
235 
236 	return ret;
237 }
238 
239 COMPAT_SYSCALL_DEFINE2(nanosleep, struct compat_timespec __user *, rqtp,
240 		       struct compat_timespec __user *, rmtp)
241 {
242 	struct timespec tu, rmt;
243 	mm_segment_t oldfs;
244 	long ret;
245 
246 	if (compat_get_timespec(&tu, rqtp))
247 		return -EFAULT;
248 
249 	if (!timespec_valid(&tu))
250 		return -EINVAL;
251 
252 	oldfs = get_fs();
253 	set_fs(KERNEL_DS);
254 	ret = hrtimer_nanosleep(&tu,
255 				rmtp ? (struct timespec __user *)&rmt : NULL,
256 				HRTIMER_MODE_REL, CLOCK_MONOTONIC);
257 	set_fs(oldfs);
258 
259 	/*
260 	 * hrtimer_nanosleep() can only return 0 or
261 	 * -ERESTART_RESTARTBLOCK here because:
262 	 *
263 	 * - we call it with HRTIMER_MODE_REL and therefor exclude the
264 	 *   -ERESTARTNOHAND return path.
265 	 *
266 	 * - we supply the rmtp argument from the task stack (due to
267 	 *   the necessary compat conversion. So the update cannot
268 	 *   fail, which excludes the -EFAULT return path as well. If
269 	 *   it fails nevertheless we have a bigger problem and wont
270 	 *   reach this place anymore.
271 	 *
272 	 * - if the return value is 0, we do not have to update rmtp
273 	 *    because there is no remaining time.
274 	 *
275 	 * We check for -ERESTART_RESTARTBLOCK nevertheless if the
276 	 * core implementation decides to return random nonsense.
277 	 */
278 	if (ret == -ERESTART_RESTARTBLOCK) {
279 		struct restart_block *restart = &current->restart_block;
280 
281 		restart->fn = compat_nanosleep_restart;
282 		restart->nanosleep.compat_rmtp = rmtp;
283 
284 		if (rmtp && compat_put_timespec(&rmt, rmtp))
285 			return -EFAULT;
286 	}
287 	return ret;
288 }
289 
290 static inline long get_compat_itimerval(struct itimerval *o,
291 		struct compat_itimerval __user *i)
292 {
293 	return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
294 		(__get_user(o->it_interval.tv_sec, &i->it_interval.tv_sec) |
295 		 __get_user(o->it_interval.tv_usec, &i->it_interval.tv_usec) |
296 		 __get_user(o->it_value.tv_sec, &i->it_value.tv_sec) |
297 		 __get_user(o->it_value.tv_usec, &i->it_value.tv_usec)));
298 }
299 
300 static inline long put_compat_itimerval(struct compat_itimerval __user *o,
301 		struct itimerval *i)
302 {
303 	return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
304 		(__put_user(i->it_interval.tv_sec, &o->it_interval.tv_sec) |
305 		 __put_user(i->it_interval.tv_usec, &o->it_interval.tv_usec) |
306 		 __put_user(i->it_value.tv_sec, &o->it_value.tv_sec) |
307 		 __put_user(i->it_value.tv_usec, &o->it_value.tv_usec)));
308 }
309 
310 COMPAT_SYSCALL_DEFINE2(getitimer, int, which,
311 		struct compat_itimerval __user *, it)
312 {
313 	struct itimerval kit;
314 	int error;
315 
316 	error = do_getitimer(which, &kit);
317 	if (!error && put_compat_itimerval(it, &kit))
318 		error = -EFAULT;
319 	return error;
320 }
321 
322 COMPAT_SYSCALL_DEFINE3(setitimer, int, which,
323 		struct compat_itimerval __user *, in,
324 		struct compat_itimerval __user *, out)
325 {
326 	struct itimerval kin, kout;
327 	int error;
328 
329 	if (in) {
330 		if (get_compat_itimerval(&kin, in))
331 			return -EFAULT;
332 	} else
333 		memset(&kin, 0, sizeof(kin));
334 
335 	error = do_setitimer(which, &kin, out ? &kout : NULL);
336 	if (error || !out)
337 		return error;
338 	if (put_compat_itimerval(out, &kout))
339 		return -EFAULT;
340 	return 0;
341 }
342 
343 static compat_clock_t clock_t_to_compat_clock_t(clock_t x)
344 {
345 	return compat_jiffies_to_clock_t(clock_t_to_jiffies(x));
346 }
347 
348 COMPAT_SYSCALL_DEFINE1(times, struct compat_tms __user *, tbuf)
349 {
350 	if (tbuf) {
351 		struct tms tms;
352 		struct compat_tms tmp;
353 
354 		do_sys_times(&tms);
355 		/* Convert our struct tms to the compat version. */
356 		tmp.tms_utime = clock_t_to_compat_clock_t(tms.tms_utime);
357 		tmp.tms_stime = clock_t_to_compat_clock_t(tms.tms_stime);
358 		tmp.tms_cutime = clock_t_to_compat_clock_t(tms.tms_cutime);
359 		tmp.tms_cstime = clock_t_to_compat_clock_t(tms.tms_cstime);
360 		if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
361 			return -EFAULT;
362 	}
363 	force_successful_syscall_return();
364 	return compat_jiffies_to_clock_t(jiffies);
365 }
366 
367 #ifdef __ARCH_WANT_SYS_SIGPENDING
368 
369 /*
370  * Assumption: old_sigset_t and compat_old_sigset_t are both
371  * types that can be passed to put_user()/get_user().
372  */
373 
374 COMPAT_SYSCALL_DEFINE1(sigpending, compat_old_sigset_t __user *, set)
375 {
376 	old_sigset_t s;
377 	long ret;
378 	mm_segment_t old_fs = get_fs();
379 
380 	set_fs(KERNEL_DS);
381 	ret = sys_sigpending((old_sigset_t __user *) &s);
382 	set_fs(old_fs);
383 	if (ret == 0)
384 		ret = put_user(s, set);
385 	return ret;
386 }
387 
388 #endif
389 
390 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
391 
392 /*
393  * sys_sigprocmask SIG_SETMASK sets the first (compat) word of the
394  * blocked set of signals to the supplied signal set
395  */
396 static inline void compat_sig_setmask(sigset_t *blocked, compat_sigset_word set)
397 {
398 	memcpy(blocked->sig, &set, sizeof(set));
399 }
400 
401 COMPAT_SYSCALL_DEFINE3(sigprocmask, int, how,
402 		       compat_old_sigset_t __user *, nset,
403 		       compat_old_sigset_t __user *, oset)
404 {
405 	old_sigset_t old_set, new_set;
406 	sigset_t new_blocked;
407 
408 	old_set = current->blocked.sig[0];
409 
410 	if (nset) {
411 		if (get_user(new_set, nset))
412 			return -EFAULT;
413 		new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
414 
415 		new_blocked = current->blocked;
416 
417 		switch (how) {
418 		case SIG_BLOCK:
419 			sigaddsetmask(&new_blocked, new_set);
420 			break;
421 		case SIG_UNBLOCK:
422 			sigdelsetmask(&new_blocked, new_set);
423 			break;
424 		case SIG_SETMASK:
425 			compat_sig_setmask(&new_blocked, new_set);
426 			break;
427 		default:
428 			return -EINVAL;
429 		}
430 
431 		set_current_blocked(&new_blocked);
432 	}
433 
434 	if (oset) {
435 		if (put_user(old_set, oset))
436 			return -EFAULT;
437 	}
438 
439 	return 0;
440 }
441 
442 #endif
443 
444 COMPAT_SYSCALL_DEFINE2(setrlimit, unsigned int, resource,
445 		       struct compat_rlimit __user *, rlim)
446 {
447 	struct rlimit r;
448 
449 	if (!access_ok(VERIFY_READ, rlim, sizeof(*rlim)) ||
450 	    __get_user(r.rlim_cur, &rlim->rlim_cur) ||
451 	    __get_user(r.rlim_max, &rlim->rlim_max))
452 		return -EFAULT;
453 
454 	if (r.rlim_cur == COMPAT_RLIM_INFINITY)
455 		r.rlim_cur = RLIM_INFINITY;
456 	if (r.rlim_max == COMPAT_RLIM_INFINITY)
457 		r.rlim_max = RLIM_INFINITY;
458 	return do_prlimit(current, resource, &r, NULL);
459 }
460 
461 #ifdef COMPAT_RLIM_OLD_INFINITY
462 
463 COMPAT_SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
464 		       struct compat_rlimit __user *, rlim)
465 {
466 	struct rlimit r;
467 	int ret;
468 	mm_segment_t old_fs = get_fs();
469 
470 	set_fs(KERNEL_DS);
471 	ret = sys_old_getrlimit(resource, (struct rlimit __user *)&r);
472 	set_fs(old_fs);
473 
474 	if (!ret) {
475 		if (r.rlim_cur > COMPAT_RLIM_OLD_INFINITY)
476 			r.rlim_cur = COMPAT_RLIM_INFINITY;
477 		if (r.rlim_max > COMPAT_RLIM_OLD_INFINITY)
478 			r.rlim_max = COMPAT_RLIM_INFINITY;
479 
480 		if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
481 		    __put_user(r.rlim_cur, &rlim->rlim_cur) ||
482 		    __put_user(r.rlim_max, &rlim->rlim_max))
483 			return -EFAULT;
484 	}
485 	return ret;
486 }
487 
488 #endif
489 
490 COMPAT_SYSCALL_DEFINE2(getrlimit, unsigned int, resource,
491 		       struct compat_rlimit __user *, rlim)
492 {
493 	struct rlimit r;
494 	int ret;
495 
496 	ret = do_prlimit(current, resource, NULL, &r);
497 	if (!ret) {
498 		if (r.rlim_cur > COMPAT_RLIM_INFINITY)
499 			r.rlim_cur = COMPAT_RLIM_INFINITY;
500 		if (r.rlim_max > COMPAT_RLIM_INFINITY)
501 			r.rlim_max = COMPAT_RLIM_INFINITY;
502 
503 		if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
504 		    __put_user(r.rlim_cur, &rlim->rlim_cur) ||
505 		    __put_user(r.rlim_max, &rlim->rlim_max))
506 			return -EFAULT;
507 	}
508 	return ret;
509 }
510 
511 int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru)
512 {
513 	if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)) ||
514 	    __put_user(r->ru_utime.tv_sec, &ru->ru_utime.tv_sec) ||
515 	    __put_user(r->ru_utime.tv_usec, &ru->ru_utime.tv_usec) ||
516 	    __put_user(r->ru_stime.tv_sec, &ru->ru_stime.tv_sec) ||
517 	    __put_user(r->ru_stime.tv_usec, &ru->ru_stime.tv_usec) ||
518 	    __put_user(r->ru_maxrss, &ru->ru_maxrss) ||
519 	    __put_user(r->ru_ixrss, &ru->ru_ixrss) ||
520 	    __put_user(r->ru_idrss, &ru->ru_idrss) ||
521 	    __put_user(r->ru_isrss, &ru->ru_isrss) ||
522 	    __put_user(r->ru_minflt, &ru->ru_minflt) ||
523 	    __put_user(r->ru_majflt, &ru->ru_majflt) ||
524 	    __put_user(r->ru_nswap, &ru->ru_nswap) ||
525 	    __put_user(r->ru_inblock, &ru->ru_inblock) ||
526 	    __put_user(r->ru_oublock, &ru->ru_oublock) ||
527 	    __put_user(r->ru_msgsnd, &ru->ru_msgsnd) ||
528 	    __put_user(r->ru_msgrcv, &ru->ru_msgrcv) ||
529 	    __put_user(r->ru_nsignals, &ru->ru_nsignals) ||
530 	    __put_user(r->ru_nvcsw, &ru->ru_nvcsw) ||
531 	    __put_user(r->ru_nivcsw, &ru->ru_nivcsw))
532 		return -EFAULT;
533 	return 0;
534 }
535 
536 COMPAT_SYSCALL_DEFINE4(wait4,
537 	compat_pid_t, pid,
538 	compat_uint_t __user *, stat_addr,
539 	int, options,
540 	struct compat_rusage __user *, ru)
541 {
542 	if (!ru) {
543 		return sys_wait4(pid, stat_addr, options, NULL);
544 	} else {
545 		struct rusage r;
546 		int ret;
547 		unsigned int status;
548 		mm_segment_t old_fs = get_fs();
549 
550 		set_fs (KERNEL_DS);
551 		ret = sys_wait4(pid,
552 				(stat_addr ?
553 				 (unsigned int __user *) &status : NULL),
554 				options, (struct rusage __user *) &r);
555 		set_fs (old_fs);
556 
557 		if (ret > 0) {
558 			if (put_compat_rusage(&r, ru))
559 				return -EFAULT;
560 			if (stat_addr && put_user(status, stat_addr))
561 				return -EFAULT;
562 		}
563 		return ret;
564 	}
565 }
566 
567 COMPAT_SYSCALL_DEFINE5(waitid,
568 		int, which, compat_pid_t, pid,
569 		struct compat_siginfo __user *, uinfo, int, options,
570 		struct compat_rusage __user *, uru)
571 {
572 	siginfo_t info;
573 	struct rusage ru;
574 	long ret;
575 	mm_segment_t old_fs = get_fs();
576 
577 	memset(&info, 0, sizeof(info));
578 
579 	set_fs(KERNEL_DS);
580 	ret = sys_waitid(which, pid, (siginfo_t __user *)&info, options,
581 			 uru ? (struct rusage __user *)&ru : NULL);
582 	set_fs(old_fs);
583 
584 	if ((ret < 0) || (info.si_signo == 0))
585 		return ret;
586 
587 	if (uru) {
588 		/* sys_waitid() overwrites everything in ru */
589 		if (COMPAT_USE_64BIT_TIME)
590 			ret = copy_to_user(uru, &ru, sizeof(ru));
591 		else
592 			ret = put_compat_rusage(&ru, uru);
593 		if (ret)
594 			return -EFAULT;
595 	}
596 
597 	BUG_ON(info.si_code & __SI_MASK);
598 	info.si_code |= __SI_CHLD;
599 	return copy_siginfo_to_user32(uinfo, &info);
600 }
601 
602 static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr,
603 				    unsigned len, struct cpumask *new_mask)
604 {
605 	unsigned long *k;
606 
607 	if (len < cpumask_size())
608 		memset(new_mask, 0, cpumask_size());
609 	else if (len > cpumask_size())
610 		len = cpumask_size();
611 
612 	k = cpumask_bits(new_mask);
613 	return compat_get_bitmap(k, user_mask_ptr, len * 8);
614 }
615 
616 COMPAT_SYSCALL_DEFINE3(sched_setaffinity, compat_pid_t, pid,
617 		       unsigned int, len,
618 		       compat_ulong_t __user *, user_mask_ptr)
619 {
620 	cpumask_var_t new_mask;
621 	int retval;
622 
623 	if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
624 		return -ENOMEM;
625 
626 	retval = compat_get_user_cpu_mask(user_mask_ptr, len, new_mask);
627 	if (retval)
628 		goto out;
629 
630 	retval = sched_setaffinity(pid, new_mask);
631 out:
632 	free_cpumask_var(new_mask);
633 	return retval;
634 }
635 
636 COMPAT_SYSCALL_DEFINE3(sched_getaffinity, compat_pid_t,  pid, unsigned int, len,
637 		       compat_ulong_t __user *, user_mask_ptr)
638 {
639 	int ret;
640 	cpumask_var_t mask;
641 
642 	if ((len * BITS_PER_BYTE) < nr_cpu_ids)
643 		return -EINVAL;
644 	if (len & (sizeof(compat_ulong_t)-1))
645 		return -EINVAL;
646 
647 	if (!alloc_cpumask_var(&mask, GFP_KERNEL))
648 		return -ENOMEM;
649 
650 	ret = sched_getaffinity(pid, mask);
651 	if (ret == 0) {
652 		size_t retlen = min_t(size_t, len, cpumask_size());
653 
654 		if (compat_put_bitmap(user_mask_ptr, cpumask_bits(mask), retlen * 8))
655 			ret = -EFAULT;
656 		else
657 			ret = retlen;
658 	}
659 	free_cpumask_var(mask);
660 
661 	return ret;
662 }
663 
664 int get_compat_itimerspec(struct itimerspec *dst,
665 			  const struct compat_itimerspec __user *src)
666 {
667 	if (__compat_get_timespec(&dst->it_interval, &src->it_interval) ||
668 	    __compat_get_timespec(&dst->it_value, &src->it_value))
669 		return -EFAULT;
670 	return 0;
671 }
672 
673 int put_compat_itimerspec(struct compat_itimerspec __user *dst,
674 			  const struct itimerspec *src)
675 {
676 	if (__compat_put_timespec(&src->it_interval, &dst->it_interval) ||
677 	    __compat_put_timespec(&src->it_value, &dst->it_value))
678 		return -EFAULT;
679 	return 0;
680 }
681 
682 COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock,
683 		       struct compat_sigevent __user *, timer_event_spec,
684 		       timer_t __user *, created_timer_id)
685 {
686 	struct sigevent __user *event = NULL;
687 
688 	if (timer_event_spec) {
689 		struct sigevent kevent;
690 
691 		event = compat_alloc_user_space(sizeof(*event));
692 		if (get_compat_sigevent(&kevent, timer_event_spec) ||
693 		    copy_to_user(event, &kevent, sizeof(*event)))
694 			return -EFAULT;
695 	}
696 
697 	return sys_timer_create(which_clock, event, created_timer_id);
698 }
699 
700 COMPAT_SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
701 		       struct compat_itimerspec __user *, new,
702 		       struct compat_itimerspec __user *, old)
703 {
704 	long err;
705 	mm_segment_t oldfs;
706 	struct itimerspec newts, oldts;
707 
708 	if (!new)
709 		return -EINVAL;
710 	if (get_compat_itimerspec(&newts, new))
711 		return -EFAULT;
712 	oldfs = get_fs();
713 	set_fs(KERNEL_DS);
714 	err = sys_timer_settime(timer_id, flags,
715 				(struct itimerspec __user *) &newts,
716 				(struct itimerspec __user *) &oldts);
717 	set_fs(oldfs);
718 	if (!err && old && put_compat_itimerspec(old, &oldts))
719 		return -EFAULT;
720 	return err;
721 }
722 
723 COMPAT_SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
724 		       struct compat_itimerspec __user *, setting)
725 {
726 	long err;
727 	mm_segment_t oldfs;
728 	struct itimerspec ts;
729 
730 	oldfs = get_fs();
731 	set_fs(KERNEL_DS);
732 	err = sys_timer_gettime(timer_id,
733 				(struct itimerspec __user *) &ts);
734 	set_fs(oldfs);
735 	if (!err && put_compat_itimerspec(setting, &ts))
736 		return -EFAULT;
737 	return err;
738 }
739 
740 COMPAT_SYSCALL_DEFINE2(clock_settime, clockid_t, which_clock,
741 		       struct compat_timespec __user *, tp)
742 {
743 	long err;
744 	mm_segment_t oldfs;
745 	struct timespec ts;
746 
747 	if (compat_get_timespec(&ts, tp))
748 		return -EFAULT;
749 	oldfs = get_fs();
750 	set_fs(KERNEL_DS);
751 	err = sys_clock_settime(which_clock,
752 				(struct timespec __user *) &ts);
753 	set_fs(oldfs);
754 	return err;
755 }
756 
757 COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock,
758 		       struct compat_timespec __user *, tp)
759 {
760 	long err;
761 	mm_segment_t oldfs;
762 	struct timespec ts;
763 
764 	oldfs = get_fs();
765 	set_fs(KERNEL_DS);
766 	err = sys_clock_gettime(which_clock,
767 				(struct timespec __user *) &ts);
768 	set_fs(oldfs);
769 	if (!err && compat_put_timespec(&ts, tp))
770 		return -EFAULT;
771 	return err;
772 }
773 
774 COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock,
775 		       struct compat_timex __user *, utp)
776 {
777 	struct timex txc;
778 	mm_segment_t oldfs;
779 	int err, ret;
780 
781 	err = compat_get_timex(&txc, utp);
782 	if (err)
783 		return err;
784 
785 	oldfs = get_fs();
786 	set_fs(KERNEL_DS);
787 	ret = sys_clock_adjtime(which_clock, (struct timex __user *) &txc);
788 	set_fs(oldfs);
789 
790 	err = compat_put_timex(utp, &txc);
791 	if (err)
792 		return err;
793 
794 	return ret;
795 }
796 
797 COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock,
798 		       struct compat_timespec __user *, tp)
799 {
800 	long err;
801 	mm_segment_t oldfs;
802 	struct timespec ts;
803 
804 	oldfs = get_fs();
805 	set_fs(KERNEL_DS);
806 	err = sys_clock_getres(which_clock,
807 			       (struct timespec __user *) &ts);
808 	set_fs(oldfs);
809 	if (!err && tp && compat_put_timespec(&ts, tp))
810 		return -EFAULT;
811 	return err;
812 }
813 
814 static long compat_clock_nanosleep_restart(struct restart_block *restart)
815 {
816 	long err;
817 	mm_segment_t oldfs;
818 	struct timespec tu;
819 	struct compat_timespec __user *rmtp = restart->nanosleep.compat_rmtp;
820 
821 	restart->nanosleep.rmtp = (struct timespec __user *) &tu;
822 	oldfs = get_fs();
823 	set_fs(KERNEL_DS);
824 	err = clock_nanosleep_restart(restart);
825 	set_fs(oldfs);
826 
827 	if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
828 	    compat_put_timespec(&tu, rmtp))
829 		return -EFAULT;
830 
831 	if (err == -ERESTART_RESTARTBLOCK) {
832 		restart->fn = compat_clock_nanosleep_restart;
833 		restart->nanosleep.compat_rmtp = rmtp;
834 	}
835 	return err;
836 }
837 
838 COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags,
839 		       struct compat_timespec __user *, rqtp,
840 		       struct compat_timespec __user *, rmtp)
841 {
842 	long err;
843 	mm_segment_t oldfs;
844 	struct timespec in, out;
845 	struct restart_block *restart;
846 
847 	if (compat_get_timespec(&in, rqtp))
848 		return -EFAULT;
849 
850 	oldfs = get_fs();
851 	set_fs(KERNEL_DS);
852 	err = sys_clock_nanosleep(which_clock, flags,
853 				  (struct timespec __user *) &in,
854 				  (struct timespec __user *) &out);
855 	set_fs(oldfs);
856 
857 	if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
858 	    compat_put_timespec(&out, rmtp))
859 		return -EFAULT;
860 
861 	if (err == -ERESTART_RESTARTBLOCK) {
862 		restart = &current->restart_block;
863 		restart->fn = compat_clock_nanosleep_restart;
864 		restart->nanosleep.compat_rmtp = rmtp;
865 	}
866 	return err;
867 }
868 
869 /*
870  * We currently only need the following fields from the sigevent
871  * structure: sigev_value, sigev_signo, sig_notify and (sometimes
872  * sigev_notify_thread_id).  The others are handled in user mode.
873  * We also assume that copying sigev_value.sival_int is sufficient
874  * to keep all the bits of sigev_value.sival_ptr intact.
875  */
876 int get_compat_sigevent(struct sigevent *event,
877 		const struct compat_sigevent __user *u_event)
878 {
879 	memset(event, 0, sizeof(*event));
880 	return (!access_ok(VERIFY_READ, u_event, sizeof(*u_event)) ||
881 		__get_user(event->sigev_value.sival_int,
882 			&u_event->sigev_value.sival_int) ||
883 		__get_user(event->sigev_signo, &u_event->sigev_signo) ||
884 		__get_user(event->sigev_notify, &u_event->sigev_notify) ||
885 		__get_user(event->sigev_notify_thread_id,
886 			&u_event->sigev_notify_thread_id))
887 		? -EFAULT : 0;
888 }
889 
890 long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask,
891 		       unsigned long bitmap_size)
892 {
893 	int i, j;
894 	unsigned long m;
895 	compat_ulong_t um;
896 	unsigned long nr_compat_longs;
897 
898 	/* align bitmap up to nearest compat_long_t boundary */
899 	bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
900 
901 	if (!access_ok(VERIFY_READ, umask, bitmap_size / 8))
902 		return -EFAULT;
903 
904 	nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
905 
906 	for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
907 		m = 0;
908 
909 		for (j = 0; j < sizeof(m)/sizeof(um); j++) {
910 			/*
911 			 * We dont want to read past the end of the userspace
912 			 * bitmap. We must however ensure the end of the
913 			 * kernel bitmap is zeroed.
914 			 */
915 			if (nr_compat_longs) {
916 				nr_compat_longs--;
917 				if (__get_user(um, umask))
918 					return -EFAULT;
919 			} else {
920 				um = 0;
921 			}
922 
923 			umask++;
924 			m |= (long)um << (j * BITS_PER_COMPAT_LONG);
925 		}
926 		*mask++ = m;
927 	}
928 
929 	return 0;
930 }
931 
932 long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
933 		       unsigned long bitmap_size)
934 {
935 	int i, j;
936 	unsigned long m;
937 	compat_ulong_t um;
938 	unsigned long nr_compat_longs;
939 
940 	/* align bitmap up to nearest compat_long_t boundary */
941 	bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
942 
943 	if (!access_ok(VERIFY_WRITE, umask, bitmap_size / 8))
944 		return -EFAULT;
945 
946 	nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
947 
948 	for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
949 		m = *mask++;
950 
951 		for (j = 0; j < sizeof(m)/sizeof(um); j++) {
952 			um = m;
953 
954 			/*
955 			 * We dont want to write past the end of the userspace
956 			 * bitmap.
957 			 */
958 			if (nr_compat_longs) {
959 				nr_compat_longs--;
960 				if (__put_user(um, umask))
961 					return -EFAULT;
962 			}
963 
964 			umask++;
965 			m >>= 4*sizeof(um);
966 			m >>= 4*sizeof(um);
967 		}
968 	}
969 
970 	return 0;
971 }
972 
973 void
974 sigset_from_compat(sigset_t *set, const compat_sigset_t *compat)
975 {
976 	switch (_NSIG_WORDS) {
977 	case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32 );
978 	case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32 );
979 	case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32 );
980 	case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32 );
981 	}
982 }
983 EXPORT_SYMBOL_GPL(sigset_from_compat);
984 
985 void
986 sigset_to_compat(compat_sigset_t *compat, const sigset_t *set)
987 {
988 	switch (_NSIG_WORDS) {
989 	case 4: compat->sig[7] = (set->sig[3] >> 32); compat->sig[6] = set->sig[3];
990 	case 3: compat->sig[5] = (set->sig[2] >> 32); compat->sig[4] = set->sig[2];
991 	case 2: compat->sig[3] = (set->sig[1] >> 32); compat->sig[2] = set->sig[1];
992 	case 1: compat->sig[1] = (set->sig[0] >> 32); compat->sig[0] = set->sig[0];
993 	}
994 }
995 
996 COMPAT_SYSCALL_DEFINE4(rt_sigtimedwait, compat_sigset_t __user *, uthese,
997 		struct compat_siginfo __user *, uinfo,
998 		struct compat_timespec __user *, uts, compat_size_t, sigsetsize)
999 {
1000 	compat_sigset_t s32;
1001 	sigset_t s;
1002 	struct timespec t;
1003 	siginfo_t info;
1004 	long ret;
1005 
1006 	if (sigsetsize != sizeof(sigset_t))
1007 		return -EINVAL;
1008 
1009 	if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t)))
1010 		return -EFAULT;
1011 	sigset_from_compat(&s, &s32);
1012 
1013 	if (uts) {
1014 		if (compat_get_timespec(&t, uts))
1015 			return -EFAULT;
1016 	}
1017 
1018 	ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);
1019 
1020 	if (ret > 0 && uinfo) {
1021 		if (copy_siginfo_to_user32(uinfo, &info))
1022 			ret = -EFAULT;
1023 	}
1024 
1025 	return ret;
1026 }
1027 
1028 #ifdef __ARCH_WANT_COMPAT_SYS_TIME
1029 
1030 /* compat_time_t is a 32 bit "long" and needs to get converted. */
1031 
1032 COMPAT_SYSCALL_DEFINE1(time, compat_time_t __user *, tloc)
1033 {
1034 	compat_time_t i;
1035 	struct timeval tv;
1036 
1037 	do_gettimeofday(&tv);
1038 	i = tv.tv_sec;
1039 
1040 	if (tloc) {
1041 		if (put_user(i,tloc))
1042 			return -EFAULT;
1043 	}
1044 	force_successful_syscall_return();
1045 	return i;
1046 }
1047 
1048 COMPAT_SYSCALL_DEFINE1(stime, compat_time_t __user *, tptr)
1049 {
1050 	struct timespec tv;
1051 	int err;
1052 
1053 	if (get_user(tv.tv_sec, tptr))
1054 		return -EFAULT;
1055 
1056 	tv.tv_nsec = 0;
1057 
1058 	err = security_settime(&tv, NULL);
1059 	if (err)
1060 		return err;
1061 
1062 	do_settimeofday(&tv);
1063 	return 0;
1064 }
1065 
1066 #endif /* __ARCH_WANT_COMPAT_SYS_TIME */
1067 
1068 COMPAT_SYSCALL_DEFINE1(adjtimex, struct compat_timex __user *, utp)
1069 {
1070 	struct timex txc;
1071 	int err, ret;
1072 
1073 	err = compat_get_timex(&txc, utp);
1074 	if (err)
1075 		return err;
1076 
1077 	ret = do_adjtimex(&txc);
1078 
1079 	err = compat_put_timex(utp, &txc);
1080 	if (err)
1081 		return err;
1082 
1083 	return ret;
1084 }
1085 
1086 #ifdef CONFIG_NUMA
1087 COMPAT_SYSCALL_DEFINE6(move_pages, pid_t, pid, compat_ulong_t, nr_pages,
1088 		       compat_uptr_t __user *, pages32,
1089 		       const int __user *, nodes,
1090 		       int __user *, status,
1091 		       int, flags)
1092 {
1093 	const void __user * __user *pages;
1094 	int i;
1095 
1096 	pages = compat_alloc_user_space(nr_pages * sizeof(void *));
1097 	for (i = 0; i < nr_pages; i++) {
1098 		compat_uptr_t p;
1099 
1100 		if (get_user(p, pages32 + i) ||
1101 			put_user(compat_ptr(p), pages + i))
1102 			return -EFAULT;
1103 	}
1104 	return sys_move_pages(pid, nr_pages, pages, nodes, status, flags);
1105 }
1106 
1107 COMPAT_SYSCALL_DEFINE4(migrate_pages, compat_pid_t, pid,
1108 		       compat_ulong_t, maxnode,
1109 		       const compat_ulong_t __user *, old_nodes,
1110 		       const compat_ulong_t __user *, new_nodes)
1111 {
1112 	unsigned long __user *old = NULL;
1113 	unsigned long __user *new = NULL;
1114 	nodemask_t tmp_mask;
1115 	unsigned long nr_bits;
1116 	unsigned long size;
1117 
1118 	nr_bits = min_t(unsigned long, maxnode - 1, MAX_NUMNODES);
1119 	size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
1120 	if (old_nodes) {
1121 		if (compat_get_bitmap(nodes_addr(tmp_mask), old_nodes, nr_bits))
1122 			return -EFAULT;
1123 		old = compat_alloc_user_space(new_nodes ? size * 2 : size);
1124 		if (new_nodes)
1125 			new = old + size / sizeof(unsigned long);
1126 		if (copy_to_user(old, nodes_addr(tmp_mask), size))
1127 			return -EFAULT;
1128 	}
1129 	if (new_nodes) {
1130 		if (compat_get_bitmap(nodes_addr(tmp_mask), new_nodes, nr_bits))
1131 			return -EFAULT;
1132 		if (new == NULL)
1133 			new = compat_alloc_user_space(size);
1134 		if (copy_to_user(new, nodes_addr(tmp_mask), size))
1135 			return -EFAULT;
1136 	}
1137 	return sys_migrate_pages(pid, nr_bits + 1, old, new);
1138 }
1139 #endif
1140 
1141 COMPAT_SYSCALL_DEFINE2(sched_rr_get_interval,
1142 		       compat_pid_t, pid,
1143 		       struct compat_timespec __user *, interval)
1144 {
1145 	struct timespec t;
1146 	int ret;
1147 	mm_segment_t old_fs = get_fs();
1148 
1149 	set_fs(KERNEL_DS);
1150 	ret = sys_sched_rr_get_interval(pid, (struct timespec __user *)&t);
1151 	set_fs(old_fs);
1152 	if (compat_put_timespec(&t, interval))
1153 		return -EFAULT;
1154 	return ret;
1155 }
1156 
1157 /*
1158  * Allocate user-space memory for the duration of a single system call,
1159  * in order to marshall parameters inside a compat thunk.
1160  */
1161 void __user *compat_alloc_user_space(unsigned long len)
1162 {
1163 	void __user *ptr;
1164 
1165 	/* If len would occupy more than half of the entire compat space... */
1166 	if (unlikely(len > (((compat_uptr_t)~0) >> 1)))
1167 		return NULL;
1168 
1169 	ptr = arch_compat_alloc_user_space(len);
1170 
1171 	if (unlikely(!access_ok(VERIFY_WRITE, ptr, len)))
1172 		return NULL;
1173 
1174 	return ptr;
1175 }
1176 EXPORT_SYMBOL_GPL(compat_alloc_user_space);
1177