64e772aabdb5f8e8d736ad730a4f02bc1e3087fa
[linux-2.6-block.git] / kernel / compat.c
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 <linux/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 timespec64 new_ts;
112         struct timeval user_tv;
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_settimeofday64(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         struct timespec64 tu64;
244         mm_segment_t oldfs;
245         long ret;
246
247         if (compat_get_timespec(&tu, rqtp))
248                 return -EFAULT;
249
250         tu64 = timespec_to_timespec64(tu);
251         if (!timespec64_valid(&tu64))
252                 return -EINVAL;
253
254         oldfs = get_fs();
255         set_fs(KERNEL_DS);
256         ret = hrtimer_nanosleep(&tu64,
257                                 rmtp ? (struct timespec __user *)&rmt : NULL,
258                                 HRTIMER_MODE_REL, CLOCK_MONOTONIC);
259         set_fs(oldfs);
260
261         /*
262          * hrtimer_nanosleep() can only return 0 or
263          * -ERESTART_RESTARTBLOCK here because:
264          *
265          * - we call it with HRTIMER_MODE_REL and therefor exclude the
266          *   -ERESTARTNOHAND return path.
267          *
268          * - we supply the rmtp argument from the task stack (due to
269          *   the necessary compat conversion. So the update cannot
270          *   fail, which excludes the -EFAULT return path as well. If
271          *   it fails nevertheless we have a bigger problem and wont
272          *   reach this place anymore.
273          *
274          * - if the return value is 0, we do not have to update rmtp
275          *    because there is no remaining time.
276          *
277          * We check for -ERESTART_RESTARTBLOCK nevertheless if the
278          * core implementation decides to return random nonsense.
279          */
280         if (ret == -ERESTART_RESTARTBLOCK) {
281                 struct restart_block *restart = &current->restart_block;
282
283                 restart->fn = compat_nanosleep_restart;
284                 restart->nanosleep.compat_rmtp = rmtp;
285
286                 if (rmtp && compat_put_timespec(&rmt, rmtp))
287                         return -EFAULT;
288         }
289         return ret;
290 }
291
292 static inline long get_compat_itimerval(struct itimerval *o,
293                 struct compat_itimerval __user *i)
294 {
295         return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
296                 (__get_user(o->it_interval.tv_sec, &i->it_interval.tv_sec) |
297                  __get_user(o->it_interval.tv_usec, &i->it_interval.tv_usec) |
298                  __get_user(o->it_value.tv_sec, &i->it_value.tv_sec) |
299                  __get_user(o->it_value.tv_usec, &i->it_value.tv_usec)));
300 }
301
302 static inline long put_compat_itimerval(struct compat_itimerval __user *o,
303                 struct itimerval *i)
304 {
305         return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
306                 (__put_user(i->it_interval.tv_sec, &o->it_interval.tv_sec) |
307                  __put_user(i->it_interval.tv_usec, &o->it_interval.tv_usec) |
308                  __put_user(i->it_value.tv_sec, &o->it_value.tv_sec) |
309                  __put_user(i->it_value.tv_usec, &o->it_value.tv_usec)));
310 }
311
312 asmlinkage long sys_ni_posix_timers(void);
313
314 COMPAT_SYSCALL_DEFINE2(getitimer, int, which,
315                 struct compat_itimerval __user *, it)
316 {
317         struct itimerval kit;
318         int error;
319
320         if (!IS_ENABLED(CONFIG_POSIX_TIMERS))
321                 return sys_ni_posix_timers();
322
323         error = do_getitimer(which, &kit);
324         if (!error && put_compat_itimerval(it, &kit))
325                 error = -EFAULT;
326         return error;
327 }
328
329 COMPAT_SYSCALL_DEFINE3(setitimer, int, which,
330                 struct compat_itimerval __user *, in,
331                 struct compat_itimerval __user *, out)
332 {
333         struct itimerval kin, kout;
334         int error;
335
336         if (!IS_ENABLED(CONFIG_POSIX_TIMERS))
337                 return sys_ni_posix_timers();
338
339         if (in) {
340                 if (get_compat_itimerval(&kin, in))
341                         return -EFAULT;
342         } else
343                 memset(&kin, 0, sizeof(kin));
344
345         error = do_setitimer(which, &kin, out ? &kout : NULL);
346         if (error || !out)
347                 return error;
348         if (put_compat_itimerval(out, &kout))
349                 return -EFAULT;
350         return 0;
351 }
352
353 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
354
355 /*
356  * sys_sigprocmask SIG_SETMASK sets the first (compat) word of the
357  * blocked set of signals to the supplied signal set
358  */
359 static inline void compat_sig_setmask(sigset_t *blocked, compat_sigset_word set)
360 {
361         memcpy(blocked->sig, &set, sizeof(set));
362 }
363
364 COMPAT_SYSCALL_DEFINE3(sigprocmask, int, how,
365                        compat_old_sigset_t __user *, nset,
366                        compat_old_sigset_t __user *, oset)
367 {
368         old_sigset_t old_set, new_set;
369         sigset_t new_blocked;
370
371         old_set = current->blocked.sig[0];
372
373         if (nset) {
374                 if (get_user(new_set, nset))
375                         return -EFAULT;
376                 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
377
378                 new_blocked = current->blocked;
379
380                 switch (how) {
381                 case SIG_BLOCK:
382                         sigaddsetmask(&new_blocked, new_set);
383                         break;
384                 case SIG_UNBLOCK:
385                         sigdelsetmask(&new_blocked, new_set);
386                         break;
387                 case SIG_SETMASK:
388                         compat_sig_setmask(&new_blocked, new_set);
389                         break;
390                 default:
391                         return -EINVAL;
392                 }
393
394                 set_current_blocked(&new_blocked);
395         }
396
397         if (oset) {
398                 if (put_user(old_set, oset))
399                         return -EFAULT;
400         }
401
402         return 0;
403 }
404
405 #endif
406
407 int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru)
408 {
409         struct compat_rusage r32;
410         memset(&r32, 0, sizeof(r32));
411         r32.ru_utime.tv_sec = r->ru_utime.tv_sec;
412         r32.ru_utime.tv_usec = r->ru_utime.tv_usec;
413         r32.ru_stime.tv_sec = r->ru_stime.tv_sec;
414         r32.ru_stime.tv_usec = r->ru_stime.tv_usec;
415         r32.ru_maxrss = r->ru_maxrss;
416         r32.ru_ixrss = r->ru_ixrss;
417         r32.ru_idrss = r->ru_idrss;
418         r32.ru_isrss = r->ru_isrss;
419         r32.ru_minflt = r->ru_minflt;
420         r32.ru_majflt = r->ru_majflt;
421         r32.ru_nswap = r->ru_nswap;
422         r32.ru_inblock = r->ru_inblock;
423         r32.ru_oublock = r->ru_oublock;
424         r32.ru_msgsnd = r->ru_msgsnd;
425         r32.ru_msgrcv = r->ru_msgrcv;
426         r32.ru_nsignals = r->ru_nsignals;
427         r32.ru_nvcsw = r->ru_nvcsw;
428         r32.ru_nivcsw = r->ru_nivcsw;
429         if (copy_to_user(ru, &r32, sizeof(r32)))
430                 return -EFAULT;
431         return 0;
432 }
433
434 COMPAT_SYSCALL_DEFINE4(wait4,
435         compat_pid_t, pid,
436         compat_uint_t __user *, stat_addr,
437         int, options,
438         struct compat_rusage __user *, ru)
439 {
440         if (!ru) {
441                 return sys_wait4(pid, stat_addr, options, NULL);
442         } else {
443                 struct rusage r;
444                 int ret;
445                 unsigned int status;
446                 mm_segment_t old_fs = get_fs();
447
448                 set_fs (KERNEL_DS);
449                 ret = sys_wait4(pid,
450                                 (stat_addr ?
451                                  (unsigned int __user *) &status : NULL),
452                                 options, (struct rusage __user *) &r);
453                 set_fs (old_fs);
454
455                 if (ret > 0) {
456                         if (put_compat_rusage(&r, ru))
457                                 return -EFAULT;
458                         if (stat_addr && put_user(status, stat_addr))
459                                 return -EFAULT;
460                 }
461                 return ret;
462         }
463 }
464
465 COMPAT_SYSCALL_DEFINE5(waitid,
466                 int, which, compat_pid_t, pid,
467                 struct compat_siginfo __user *, uinfo, int, options,
468                 struct compat_rusage __user *, uru)
469 {
470         siginfo_t info;
471         struct rusage ru;
472         long ret;
473         mm_segment_t old_fs = get_fs();
474
475         memset(&info, 0, sizeof(info));
476
477         set_fs(KERNEL_DS);
478         ret = sys_waitid(which, pid, (siginfo_t __user *)&info, options,
479                          uru ? (struct rusage __user *)&ru : NULL);
480         set_fs(old_fs);
481
482         if ((ret < 0) || (info.si_signo == 0))
483                 return ret;
484
485         if (uru) {
486                 /* sys_waitid() overwrites everything in ru */
487                 if (COMPAT_USE_64BIT_TIME)
488                         ret = copy_to_user(uru, &ru, sizeof(ru));
489                 else
490                         ret = put_compat_rusage(&ru, uru);
491                 if (ret)
492                         return -EFAULT;
493         }
494
495         BUG_ON(info.si_code & __SI_MASK);
496         info.si_code |= __SI_CHLD;
497         return copy_siginfo_to_user32(uinfo, &info);
498 }
499
500 static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr,
501                                     unsigned len, struct cpumask *new_mask)
502 {
503         unsigned long *k;
504
505         if (len < cpumask_size())
506                 memset(new_mask, 0, cpumask_size());
507         else if (len > cpumask_size())
508                 len = cpumask_size();
509
510         k = cpumask_bits(new_mask);
511         return compat_get_bitmap(k, user_mask_ptr, len * 8);
512 }
513
514 COMPAT_SYSCALL_DEFINE3(sched_setaffinity, compat_pid_t, pid,
515                        unsigned int, len,
516                        compat_ulong_t __user *, user_mask_ptr)
517 {
518         cpumask_var_t new_mask;
519         int retval;
520
521         if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
522                 return -ENOMEM;
523
524         retval = compat_get_user_cpu_mask(user_mask_ptr, len, new_mask);
525         if (retval)
526                 goto out;
527
528         retval = sched_setaffinity(pid, new_mask);
529 out:
530         free_cpumask_var(new_mask);
531         return retval;
532 }
533
534 COMPAT_SYSCALL_DEFINE3(sched_getaffinity, compat_pid_t,  pid, unsigned int, len,
535                        compat_ulong_t __user *, user_mask_ptr)
536 {
537         int ret;
538         cpumask_var_t mask;
539
540         if ((len * BITS_PER_BYTE) < nr_cpu_ids)
541                 return -EINVAL;
542         if (len & (sizeof(compat_ulong_t)-1))
543                 return -EINVAL;
544
545         if (!alloc_cpumask_var(&mask, GFP_KERNEL))
546                 return -ENOMEM;
547
548         ret = sched_getaffinity(pid, mask);
549         if (ret == 0) {
550                 size_t retlen = min_t(size_t, len, cpumask_size());
551
552                 if (compat_put_bitmap(user_mask_ptr, cpumask_bits(mask), retlen * 8))
553                         ret = -EFAULT;
554                 else
555                         ret = retlen;
556         }
557         free_cpumask_var(mask);
558
559         return ret;
560 }
561
562 int get_compat_itimerspec(struct itimerspec *dst,
563                           const struct compat_itimerspec __user *src)
564 {
565         if (__compat_get_timespec(&dst->it_interval, &src->it_interval) ||
566             __compat_get_timespec(&dst->it_value, &src->it_value))
567                 return -EFAULT;
568         return 0;
569 }
570
571 int put_compat_itimerspec(struct compat_itimerspec __user *dst,
572                           const struct itimerspec *src)
573 {
574         if (__compat_put_timespec(&src->it_interval, &dst->it_interval) ||
575             __compat_put_timespec(&src->it_value, &dst->it_value))
576                 return -EFAULT;
577         return 0;
578 }
579
580 COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock,
581                        struct compat_sigevent __user *, timer_event_spec,
582                        timer_t __user *, created_timer_id)
583 {
584         struct sigevent __user *event = NULL;
585
586         if (timer_event_spec) {
587                 struct sigevent kevent;
588
589                 event = compat_alloc_user_space(sizeof(*event));
590                 if (get_compat_sigevent(&kevent, timer_event_spec) ||
591                     copy_to_user(event, &kevent, sizeof(*event)))
592                         return -EFAULT;
593         }
594
595         return sys_timer_create(which_clock, event, created_timer_id);
596 }
597
598 COMPAT_SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
599                        struct compat_itimerspec __user *, new,
600                        struct compat_itimerspec __user *, old)
601 {
602         long err;
603         mm_segment_t oldfs;
604         struct itimerspec newts, oldts;
605
606         if (!new)
607                 return -EINVAL;
608         if (get_compat_itimerspec(&newts, new))
609                 return -EFAULT;
610         oldfs = get_fs();
611         set_fs(KERNEL_DS);
612         err = sys_timer_settime(timer_id, flags,
613                                 (struct itimerspec __user *) &newts,
614                                 (struct itimerspec __user *) &oldts);
615         set_fs(oldfs);
616         if (!err && old && put_compat_itimerspec(old, &oldts))
617                 return -EFAULT;
618         return err;
619 }
620
621 COMPAT_SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
622                        struct compat_itimerspec __user *, setting)
623 {
624         long err;
625         mm_segment_t oldfs;
626         struct itimerspec ts;
627
628         oldfs = get_fs();
629         set_fs(KERNEL_DS);
630         err = sys_timer_gettime(timer_id,
631                                 (struct itimerspec __user *) &ts);
632         set_fs(oldfs);
633         if (!err && put_compat_itimerspec(setting, &ts))
634                 return -EFAULT;
635         return err;
636 }
637
638 COMPAT_SYSCALL_DEFINE2(clock_settime, clockid_t, which_clock,
639                        struct compat_timespec __user *, tp)
640 {
641         long err;
642         mm_segment_t oldfs;
643         struct timespec ts;
644
645         if (compat_get_timespec(&ts, tp))
646                 return -EFAULT;
647         oldfs = get_fs();
648         set_fs(KERNEL_DS);
649         err = sys_clock_settime(which_clock,
650                                 (struct timespec __user *) &ts);
651         set_fs(oldfs);
652         return err;
653 }
654
655 COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock,
656                        struct compat_timespec __user *, tp)
657 {
658         long err;
659         mm_segment_t oldfs;
660         struct timespec ts;
661
662         oldfs = get_fs();
663         set_fs(KERNEL_DS);
664         err = sys_clock_gettime(which_clock,
665                                 (struct timespec __user *) &ts);
666         set_fs(oldfs);
667         if (!err && compat_put_timespec(&ts, tp))
668                 return -EFAULT;
669         return err;
670 }
671
672 COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock,
673                        struct compat_timex __user *, utp)
674 {
675         struct timex txc;
676         mm_segment_t oldfs;
677         int err, ret;
678
679         err = compat_get_timex(&txc, utp);
680         if (err)
681                 return err;
682
683         oldfs = get_fs();
684         set_fs(KERNEL_DS);
685         ret = sys_clock_adjtime(which_clock, (struct timex __user *) &txc);
686         set_fs(oldfs);
687
688         err = compat_put_timex(utp, &txc);
689         if (err)
690                 return err;
691
692         return ret;
693 }
694
695 COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock,
696                        struct compat_timespec __user *, tp)
697 {
698         long err;
699         mm_segment_t oldfs;
700         struct timespec ts;
701
702         oldfs = get_fs();
703         set_fs(KERNEL_DS);
704         err = sys_clock_getres(which_clock,
705                                (struct timespec __user *) &ts);
706         set_fs(oldfs);
707         if (!err && tp && compat_put_timespec(&ts, tp))
708                 return -EFAULT;
709         return err;
710 }
711
712 static long compat_clock_nanosleep_restart(struct restart_block *restart)
713 {
714         long err;
715         mm_segment_t oldfs;
716         struct timespec tu;
717         struct compat_timespec __user *rmtp = restart->nanosleep.compat_rmtp;
718
719         restart->nanosleep.rmtp = (struct timespec __user *) &tu;
720         oldfs = get_fs();
721         set_fs(KERNEL_DS);
722         err = clock_nanosleep_restart(restart);
723         set_fs(oldfs);
724
725         if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
726             compat_put_timespec(&tu, rmtp))
727                 return -EFAULT;
728
729         if (err == -ERESTART_RESTARTBLOCK) {
730                 restart->fn = compat_clock_nanosleep_restart;
731                 restart->nanosleep.compat_rmtp = rmtp;
732         }
733         return err;
734 }
735
736 COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags,
737                        struct compat_timespec __user *, rqtp,
738                        struct compat_timespec __user *, rmtp)
739 {
740         long err;
741         mm_segment_t oldfs;
742         struct timespec in, out;
743         struct restart_block *restart;
744
745         if (compat_get_timespec(&in, rqtp))
746                 return -EFAULT;
747
748         oldfs = get_fs();
749         set_fs(KERNEL_DS);
750         err = sys_clock_nanosleep(which_clock, flags,
751                                   (struct timespec __user *) &in,
752                                   (struct timespec __user *) &out);
753         set_fs(oldfs);
754
755         if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
756             compat_put_timespec(&out, rmtp))
757                 return -EFAULT;
758
759         if (err == -ERESTART_RESTARTBLOCK) {
760                 restart = &current->restart_block;
761                 restart->fn = compat_clock_nanosleep_restart;
762                 restart->nanosleep.compat_rmtp = rmtp;
763         }
764         return err;
765 }
766
767 /*
768  * We currently only need the following fields from the sigevent
769  * structure: sigev_value, sigev_signo, sig_notify and (sometimes
770  * sigev_notify_thread_id).  The others are handled in user mode.
771  * We also assume that copying sigev_value.sival_int is sufficient
772  * to keep all the bits of sigev_value.sival_ptr intact.
773  */
774 int get_compat_sigevent(struct sigevent *event,
775                 const struct compat_sigevent __user *u_event)
776 {
777         memset(event, 0, sizeof(*event));
778         return (!access_ok(VERIFY_READ, u_event, sizeof(*u_event)) ||
779                 __get_user(event->sigev_value.sival_int,
780                         &u_event->sigev_value.sival_int) ||
781                 __get_user(event->sigev_signo, &u_event->sigev_signo) ||
782                 __get_user(event->sigev_notify, &u_event->sigev_notify) ||
783                 __get_user(event->sigev_notify_thread_id,
784                         &u_event->sigev_notify_thread_id))
785                 ? -EFAULT : 0;
786 }
787
788 long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask,
789                        unsigned long bitmap_size)
790 {
791         unsigned long nr_compat_longs;
792
793         /* align bitmap up to nearest compat_long_t boundary */
794         bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
795         nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
796
797         if (!access_ok(VERIFY_READ, umask, bitmap_size / 8))
798                 return -EFAULT;
799
800         user_access_begin();
801         while (nr_compat_longs > 1) {
802                 compat_ulong_t l1, l2;
803                 unsafe_get_user(l1, umask++, Efault);
804                 unsafe_get_user(l2, umask++, Efault);
805                 *mask++ = ((unsigned long)l2 << BITS_PER_COMPAT_LONG) | l1;
806                 nr_compat_longs -= 2;
807         }
808         if (nr_compat_longs)
809                 unsafe_get_user(*mask, umask++, Efault);
810         user_access_end();
811         return 0;
812
813 Efault:
814         user_access_end();
815         return -EFAULT;
816 }
817
818 long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
819                        unsigned long bitmap_size)
820 {
821         unsigned long nr_compat_longs;
822
823         /* align bitmap up to nearest compat_long_t boundary */
824         bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
825         nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
826
827         if (!access_ok(VERIFY_WRITE, umask, bitmap_size / 8))
828                 return -EFAULT;
829
830         user_access_begin();
831         while (nr_compat_longs > 1) {
832                 unsigned long m = *mask++;
833                 unsafe_put_user((compat_ulong_t)m, umask++, Efault);
834                 unsafe_put_user(m >> BITS_PER_COMPAT_LONG, umask++, Efault);
835                 nr_compat_longs -= 2;
836         }
837         if (nr_compat_longs)
838                 unsafe_put_user((compat_ulong_t)*mask, umask++, Efault);
839         user_access_end();
840         return 0;
841 Efault:
842         user_access_end();
843         return -EFAULT;
844 }
845
846 void
847 sigset_from_compat(sigset_t *set, const compat_sigset_t *compat)
848 {
849         switch (_NSIG_WORDS) {
850         case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32 );
851         case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32 );
852         case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32 );
853         case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32 );
854         }
855 }
856 EXPORT_SYMBOL_GPL(sigset_from_compat);
857
858 void
859 sigset_to_compat(compat_sigset_t *compat, const sigset_t *set)
860 {
861         switch (_NSIG_WORDS) {
862         case 4: compat->sig[7] = (set->sig[3] >> 32); compat->sig[6] = set->sig[3];
863         case 3: compat->sig[5] = (set->sig[2] >> 32); compat->sig[4] = set->sig[2];
864         case 2: compat->sig[3] = (set->sig[1] >> 32); compat->sig[2] = set->sig[1];
865         case 1: compat->sig[1] = (set->sig[0] >> 32); compat->sig[0] = set->sig[0];
866         }
867 }
868
869 COMPAT_SYSCALL_DEFINE4(rt_sigtimedwait, compat_sigset_t __user *, uthese,
870                 struct compat_siginfo __user *, uinfo,
871                 struct compat_timespec __user *, uts, compat_size_t, sigsetsize)
872 {
873         compat_sigset_t s32;
874         sigset_t s;
875         struct timespec t;
876         siginfo_t info;
877         long ret;
878
879         if (sigsetsize != sizeof(sigset_t))
880                 return -EINVAL;
881
882         if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t)))
883                 return -EFAULT;
884         sigset_from_compat(&s, &s32);
885
886         if (uts) {
887                 if (compat_get_timespec(&t, uts))
888                         return -EFAULT;
889         }
890
891         ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);
892
893         if (ret > 0 && uinfo) {
894                 if (copy_siginfo_to_user32(uinfo, &info))
895                         ret = -EFAULT;
896         }
897
898         return ret;
899 }
900
901 #ifdef __ARCH_WANT_COMPAT_SYS_TIME
902
903 /* compat_time_t is a 32 bit "long" and needs to get converted. */
904
905 COMPAT_SYSCALL_DEFINE1(time, compat_time_t __user *, tloc)
906 {
907         compat_time_t i;
908         struct timeval tv;
909
910         do_gettimeofday(&tv);
911         i = tv.tv_sec;
912
913         if (tloc) {
914                 if (put_user(i,tloc))
915                         return -EFAULT;
916         }
917         force_successful_syscall_return();
918         return i;
919 }
920
921 COMPAT_SYSCALL_DEFINE1(stime, compat_time_t __user *, tptr)
922 {
923         struct timespec tv;
924         int err;
925
926         if (get_user(tv.tv_sec, tptr))
927                 return -EFAULT;
928
929         tv.tv_nsec = 0;
930
931         err = security_settime(&tv, NULL);
932         if (err)
933                 return err;
934
935         do_settimeofday(&tv);
936         return 0;
937 }
938
939 #endif /* __ARCH_WANT_COMPAT_SYS_TIME */
940
941 COMPAT_SYSCALL_DEFINE1(adjtimex, struct compat_timex __user *, utp)
942 {
943         struct timex txc;
944         int err, ret;
945
946         err = compat_get_timex(&txc, utp);
947         if (err)
948                 return err;
949
950         ret = do_adjtimex(&txc);
951
952         err = compat_put_timex(utp, &txc);
953         if (err)
954                 return err;
955
956         return ret;
957 }
958
959 #ifdef CONFIG_NUMA
960 COMPAT_SYSCALL_DEFINE6(move_pages, pid_t, pid, compat_ulong_t, nr_pages,
961                        compat_uptr_t __user *, pages32,
962                        const int __user *, nodes,
963                        int __user *, status,
964                        int, flags)
965 {
966         const void __user * __user *pages;
967         int i;
968
969         pages = compat_alloc_user_space(nr_pages * sizeof(void *));
970         for (i = 0; i < nr_pages; i++) {
971                 compat_uptr_t p;
972
973                 if (get_user(p, pages32 + i) ||
974                         put_user(compat_ptr(p), pages + i))
975                         return -EFAULT;
976         }
977         return sys_move_pages(pid, nr_pages, pages, nodes, status, flags);
978 }
979
980 COMPAT_SYSCALL_DEFINE4(migrate_pages, compat_pid_t, pid,
981                        compat_ulong_t, maxnode,
982                        const compat_ulong_t __user *, old_nodes,
983                        const compat_ulong_t __user *, new_nodes)
984 {
985         unsigned long __user *old = NULL;
986         unsigned long __user *new = NULL;
987         nodemask_t tmp_mask;
988         unsigned long nr_bits;
989         unsigned long size;
990
991         nr_bits = min_t(unsigned long, maxnode - 1, MAX_NUMNODES);
992         size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
993         if (old_nodes) {
994                 if (compat_get_bitmap(nodes_addr(tmp_mask), old_nodes, nr_bits))
995                         return -EFAULT;
996                 old = compat_alloc_user_space(new_nodes ? size * 2 : size);
997                 if (new_nodes)
998                         new = old + size / sizeof(unsigned long);
999                 if (copy_to_user(old, nodes_addr(tmp_mask), size))
1000                         return -EFAULT;
1001         }
1002         if (new_nodes) {
1003                 if (compat_get_bitmap(nodes_addr(tmp_mask), new_nodes, nr_bits))
1004                         return -EFAULT;
1005                 if (new == NULL)
1006                         new = compat_alloc_user_space(size);
1007                 if (copy_to_user(new, nodes_addr(tmp_mask), size))
1008                         return -EFAULT;
1009         }
1010         return sys_migrate_pages(pid, nr_bits + 1, old, new);
1011 }
1012 #endif
1013
1014 COMPAT_SYSCALL_DEFINE2(sched_rr_get_interval,
1015                        compat_pid_t, pid,
1016                        struct compat_timespec __user *, interval)
1017 {
1018         struct timespec t;
1019         int ret;
1020         mm_segment_t old_fs = get_fs();
1021
1022         set_fs(KERNEL_DS);
1023         ret = sys_sched_rr_get_interval(pid, (struct timespec __user *)&t);
1024         set_fs(old_fs);
1025         if (compat_put_timespec(&t, interval))
1026                 return -EFAULT;
1027         return ret;
1028 }
1029
1030 /*
1031  * Allocate user-space memory for the duration of a single system call,
1032  * in order to marshall parameters inside a compat thunk.
1033  */
1034 void __user *compat_alloc_user_space(unsigned long len)
1035 {
1036         void __user *ptr;
1037
1038         /* If len would occupy more than half of the entire compat space... */
1039         if (unlikely(len > (((compat_uptr_t)~0) >> 1)))
1040                 return NULL;
1041
1042         ptr = arch_compat_alloc_user_space(len);
1043
1044         if (unlikely(!access_ok(VERIFY_WRITE, ptr, len)))
1045                 return NULL;
1046
1047         return ptr;
1048 }
1049 EXPORT_SYMBOL_GPL(compat_alloc_user_space);