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