Back to home page

LXR

 
 

    


0001 /*
0002  *  linux/kernel/compat.c
0003  *
0004  *  Kernel compatibililty routines for e.g. 32 bit syscall support
0005  *  on 64 bit kernels.
0006  *
0007  *  Copyright (C) 2002-2003 Stephen Rothwell, IBM Corporation
0008  *
0009  *  This program is free software; you can redistribute it and/or modify
0010  *  it under the terms of the GNU General Public License version 2 as
0011  *  published by the Free Software Foundation.
0012  */
0013 
0014 #include <linux/linkage.h>
0015 #include <linux/compat.h>
0016 #include <linux/errno.h>
0017 #include <linux/time.h>
0018 #include <linux/signal.h>
0019 #include <linux/sched.h>    /* for MAX_SCHEDULE_TIMEOUT */
0020 #include <linux/syscalls.h>
0021 #include <linux/unistd.h>
0022 #include <linux/security.h>
0023 #include <linux/timex.h>
0024 #include <linux/export.h>
0025 #include <linux/migrate.h>
0026 #include <linux/posix-timers.h>
0027 #include <linux/times.h>
0028 #include <linux/ptrace.h>
0029 #include <linux/gfp.h>
0030 
0031 #include <linux/uaccess.h>
0032 
0033 static int compat_get_timex(struct timex *txc, struct compat_timex __user *utp)
0034 {
0035     memset(txc, 0, sizeof(struct timex));
0036 
0037     if (!access_ok(VERIFY_READ, utp, sizeof(struct compat_timex)) ||
0038             __get_user(txc->modes, &utp->modes) ||
0039             __get_user(txc->offset, &utp->offset) ||
0040             __get_user(txc->freq, &utp->freq) ||
0041             __get_user(txc->maxerror, &utp->maxerror) ||
0042             __get_user(txc->esterror, &utp->esterror) ||
0043             __get_user(txc->status, &utp->status) ||
0044             __get_user(txc->constant, &utp->constant) ||
0045             __get_user(txc->precision, &utp->precision) ||
0046             __get_user(txc->tolerance, &utp->tolerance) ||
0047             __get_user(txc->time.tv_sec, &utp->time.tv_sec) ||
0048             __get_user(txc->time.tv_usec, &utp->time.tv_usec) ||
0049             __get_user(txc->tick, &utp->tick) ||
0050             __get_user(txc->ppsfreq, &utp->ppsfreq) ||
0051             __get_user(txc->jitter, &utp->jitter) ||
0052             __get_user(txc->shift, &utp->shift) ||
0053             __get_user(txc->stabil, &utp->stabil) ||
0054             __get_user(txc->jitcnt, &utp->jitcnt) ||
0055             __get_user(txc->calcnt, &utp->calcnt) ||
0056             __get_user(txc->errcnt, &utp->errcnt) ||
0057             __get_user(txc->stbcnt, &utp->stbcnt))
0058         return -EFAULT;
0059 
0060     return 0;
0061 }
0062 
0063 static int compat_put_timex(struct compat_timex __user *utp, struct timex *txc)
0064 {
0065     if (!access_ok(VERIFY_WRITE, utp, sizeof(struct compat_timex)) ||
0066             __put_user(txc->modes, &utp->modes) ||
0067             __put_user(txc->offset, &utp->offset) ||
0068             __put_user(txc->freq, &utp->freq) ||
0069             __put_user(txc->maxerror, &utp->maxerror) ||
0070             __put_user(txc->esterror, &utp->esterror) ||
0071             __put_user(txc->status, &utp->status) ||
0072             __put_user(txc->constant, &utp->constant) ||
0073             __put_user(txc->precision, &utp->precision) ||
0074             __put_user(txc->tolerance, &utp->tolerance) ||
0075             __put_user(txc->time.tv_sec, &utp->time.tv_sec) ||
0076             __put_user(txc->time.tv_usec, &utp->time.tv_usec) ||
0077             __put_user(txc->tick, &utp->tick) ||
0078             __put_user(txc->ppsfreq, &utp->ppsfreq) ||
0079             __put_user(txc->jitter, &utp->jitter) ||
0080             __put_user(txc->shift, &utp->shift) ||
0081             __put_user(txc->stabil, &utp->stabil) ||
0082             __put_user(txc->jitcnt, &utp->jitcnt) ||
0083             __put_user(txc->calcnt, &utp->calcnt) ||
0084             __put_user(txc->errcnt, &utp->errcnt) ||
0085             __put_user(txc->stbcnt, &utp->stbcnt) ||
0086             __put_user(txc->tai, &utp->tai))
0087         return -EFAULT;
0088     return 0;
0089 }
0090 
0091 COMPAT_SYSCALL_DEFINE2(gettimeofday, struct compat_timeval __user *, tv,
0092                struct timezone __user *, tz)
0093 {
0094     if (tv) {
0095         struct timeval ktv;
0096         do_gettimeofday(&ktv);
0097         if (compat_put_timeval(&ktv, tv))
0098             return -EFAULT;
0099     }
0100     if (tz) {
0101         if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
0102             return -EFAULT;
0103     }
0104 
0105     return 0;
0106 }
0107 
0108 COMPAT_SYSCALL_DEFINE2(settimeofday, struct compat_timeval __user *, tv,
0109                struct timezone __user *, tz)
0110 {
0111     struct timeval user_tv;
0112     struct timespec new_ts;
0113     struct timezone new_tz;
0114 
0115     if (tv) {
0116         if (compat_get_timeval(&user_tv, tv))
0117             return -EFAULT;
0118         new_ts.tv_sec = user_tv.tv_sec;
0119         new_ts.tv_nsec = user_tv.tv_usec * NSEC_PER_USEC;
0120     }
0121     if (tz) {
0122         if (copy_from_user(&new_tz, tz, sizeof(*tz)))
0123             return -EFAULT;
0124     }
0125 
0126     return do_sys_settimeofday(tv ? &new_ts : NULL, tz ? &new_tz : NULL);
0127 }
0128 
0129 static int __compat_get_timeval(struct timeval *tv, const struct compat_timeval __user *ctv)
0130 {
0131     return (!access_ok(VERIFY_READ, ctv, sizeof(*ctv)) ||
0132             __get_user(tv->tv_sec, &ctv->tv_sec) ||
0133             __get_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0;
0134 }
0135 
0136 static int __compat_put_timeval(const struct timeval *tv, struct compat_timeval __user *ctv)
0137 {
0138     return (!access_ok(VERIFY_WRITE, ctv, sizeof(*ctv)) ||
0139             __put_user(tv->tv_sec, &ctv->tv_sec) ||
0140             __put_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0;
0141 }
0142 
0143 static int __compat_get_timespec(struct timespec *ts, const struct compat_timespec __user *cts)
0144 {
0145     return (!access_ok(VERIFY_READ, cts, sizeof(*cts)) ||
0146             __get_user(ts->tv_sec, &cts->tv_sec) ||
0147             __get_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
0148 }
0149 
0150 static int __compat_put_timespec(const struct timespec *ts, struct compat_timespec __user *cts)
0151 {
0152     return (!access_ok(VERIFY_WRITE, cts, sizeof(*cts)) ||
0153             __put_user(ts->tv_sec, &cts->tv_sec) ||
0154             __put_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
0155 }
0156 
0157 int compat_get_timeval(struct timeval *tv, const void __user *utv)
0158 {
0159     if (COMPAT_USE_64BIT_TIME)
0160         return copy_from_user(tv, utv, sizeof(*tv)) ? -EFAULT : 0;
0161     else
0162         return __compat_get_timeval(tv, utv);
0163 }
0164 EXPORT_SYMBOL_GPL(compat_get_timeval);
0165 
0166 int compat_put_timeval(const struct timeval *tv, void __user *utv)
0167 {
0168     if (COMPAT_USE_64BIT_TIME)
0169         return copy_to_user(utv, tv, sizeof(*tv)) ? -EFAULT : 0;
0170     else
0171         return __compat_put_timeval(tv, utv);
0172 }
0173 EXPORT_SYMBOL_GPL(compat_put_timeval);
0174 
0175 int compat_get_timespec(struct timespec *ts, const void __user *uts)
0176 {
0177     if (COMPAT_USE_64BIT_TIME)
0178         return copy_from_user(ts, uts, sizeof(*ts)) ? -EFAULT : 0;
0179     else
0180         return __compat_get_timespec(ts, uts);
0181 }
0182 EXPORT_SYMBOL_GPL(compat_get_timespec);
0183 
0184 int compat_put_timespec(const struct timespec *ts, void __user *uts)
0185 {
0186     if (COMPAT_USE_64BIT_TIME)
0187         return copy_to_user(uts, ts, sizeof(*ts)) ? -EFAULT : 0;
0188     else
0189         return __compat_put_timespec(ts, uts);
0190 }
0191 EXPORT_SYMBOL_GPL(compat_put_timespec);
0192 
0193 int compat_convert_timespec(struct timespec __user **kts,
0194                 const void __user *cts)
0195 {
0196     struct timespec ts;
0197     struct timespec __user *uts;
0198 
0199     if (!cts || COMPAT_USE_64BIT_TIME) {
0200         *kts = (struct timespec __user *)cts;
0201         return 0;
0202     }
0203 
0204     uts = compat_alloc_user_space(sizeof(ts));
0205     if (!uts)
0206         return -EFAULT;
0207     if (compat_get_timespec(&ts, cts))
0208         return -EFAULT;
0209     if (copy_to_user(uts, &ts, sizeof(ts)))
0210         return -EFAULT;
0211 
0212     *kts = uts;
0213     return 0;
0214 }
0215 
0216 static long compat_nanosleep_restart(struct restart_block *restart)
0217 {
0218     struct compat_timespec __user *rmtp;
0219     struct timespec rmt;
0220     mm_segment_t oldfs;
0221     long ret;
0222 
0223     restart->nanosleep.rmtp = (struct timespec __user *) &rmt;
0224     oldfs = get_fs();
0225     set_fs(KERNEL_DS);
0226     ret = hrtimer_nanosleep_restart(restart);
0227     set_fs(oldfs);
0228 
0229     if (ret == -ERESTART_RESTARTBLOCK) {
0230         rmtp = restart->nanosleep.compat_rmtp;
0231 
0232         if (rmtp && compat_put_timespec(&rmt, rmtp))
0233             return -EFAULT;
0234     }
0235 
0236     return ret;
0237 }
0238 
0239 COMPAT_SYSCALL_DEFINE2(nanosleep, struct compat_timespec __user *, rqtp,
0240                struct compat_timespec __user *, rmtp)
0241 {
0242     struct timespec tu, rmt;
0243     mm_segment_t oldfs;
0244     long ret;
0245 
0246     if (compat_get_timespec(&tu, rqtp))
0247         return -EFAULT;
0248 
0249     if (!timespec_valid(&tu))
0250         return -EINVAL;
0251 
0252     oldfs = get_fs();
0253     set_fs(KERNEL_DS);
0254     ret = hrtimer_nanosleep(&tu,
0255                 rmtp ? (struct timespec __user *)&rmt : NULL,
0256                 HRTIMER_MODE_REL, CLOCK_MONOTONIC);
0257     set_fs(oldfs);
0258 
0259     /*
0260      * hrtimer_nanosleep() can only return 0 or
0261      * -ERESTART_RESTARTBLOCK here because:
0262      *
0263      * - we call it with HRTIMER_MODE_REL and therefor exclude the
0264      *   -ERESTARTNOHAND return path.
0265      *
0266      * - we supply the rmtp argument from the task stack (due to
0267      *   the necessary compat conversion. So the update cannot
0268      *   fail, which excludes the -EFAULT return path as well. If
0269      *   it fails nevertheless we have a bigger problem and wont
0270      *   reach this place anymore.
0271      *
0272      * - if the return value is 0, we do not have to update rmtp
0273      *    because there is no remaining time.
0274      *
0275      * We check for -ERESTART_RESTARTBLOCK nevertheless if the
0276      * core implementation decides to return random nonsense.
0277      */
0278     if (ret == -ERESTART_RESTARTBLOCK) {
0279         struct restart_block *restart = &current->restart_block;
0280 
0281         restart->fn = compat_nanosleep_restart;
0282         restart->nanosleep.compat_rmtp = rmtp;
0283 
0284         if (rmtp && compat_put_timespec(&rmt, rmtp))
0285             return -EFAULT;
0286     }
0287     return ret;
0288 }
0289 
0290 static inline long get_compat_itimerval(struct itimerval *o,
0291         struct compat_itimerval __user *i)
0292 {
0293     return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
0294         (__get_user(o->it_interval.tv_sec, &i->it_interval.tv_sec) |
0295          __get_user(o->it_interval.tv_usec, &i->it_interval.tv_usec) |
0296          __get_user(o->it_value.tv_sec, &i->it_value.tv_sec) |
0297          __get_user(o->it_value.tv_usec, &i->it_value.tv_usec)));
0298 }
0299 
0300 static inline long put_compat_itimerval(struct compat_itimerval __user *o,
0301         struct itimerval *i)
0302 {
0303     return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
0304         (__put_user(i->it_interval.tv_sec, &o->it_interval.tv_sec) |
0305          __put_user(i->it_interval.tv_usec, &o->it_interval.tv_usec) |
0306          __put_user(i->it_value.tv_sec, &o->it_value.tv_sec) |
0307          __put_user(i->it_value.tv_usec, &o->it_value.tv_usec)));
0308 }
0309 
0310 asmlinkage long sys_ni_posix_timers(void);
0311 
0312 COMPAT_SYSCALL_DEFINE2(getitimer, int, which,
0313         struct compat_itimerval __user *, it)
0314 {
0315     struct itimerval kit;
0316     int error;
0317 
0318     if (!IS_ENABLED(CONFIG_POSIX_TIMERS))
0319         return sys_ni_posix_timers();
0320 
0321     error = do_getitimer(which, &kit);
0322     if (!error && put_compat_itimerval(it, &kit))
0323         error = -EFAULT;
0324     return error;
0325 }
0326 
0327 COMPAT_SYSCALL_DEFINE3(setitimer, int, which,
0328         struct compat_itimerval __user *, in,
0329         struct compat_itimerval __user *, out)
0330 {
0331     struct itimerval kin, kout;
0332     int error;
0333 
0334     if (!IS_ENABLED(CONFIG_POSIX_TIMERS))
0335         return sys_ni_posix_timers();
0336 
0337     if (in) {
0338         if (get_compat_itimerval(&kin, in))
0339             return -EFAULT;
0340     } else
0341         memset(&kin, 0, sizeof(kin));
0342 
0343     error = do_setitimer(which, &kin, out ? &kout : NULL);
0344     if (error || !out)
0345         return error;
0346     if (put_compat_itimerval(out, &kout))
0347         return -EFAULT;
0348     return 0;
0349 }
0350 
0351 static compat_clock_t clock_t_to_compat_clock_t(clock_t x)
0352 {
0353     return compat_jiffies_to_clock_t(clock_t_to_jiffies(x));
0354 }
0355 
0356 COMPAT_SYSCALL_DEFINE1(times, struct compat_tms __user *, tbuf)
0357 {
0358     if (tbuf) {
0359         struct tms tms;
0360         struct compat_tms tmp;
0361 
0362         do_sys_times(&tms);
0363         /* Convert our struct tms to the compat version. */
0364         tmp.tms_utime = clock_t_to_compat_clock_t(tms.tms_utime);
0365         tmp.tms_stime = clock_t_to_compat_clock_t(tms.tms_stime);
0366         tmp.tms_cutime = clock_t_to_compat_clock_t(tms.tms_cutime);
0367         tmp.tms_cstime = clock_t_to_compat_clock_t(tms.tms_cstime);
0368         if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
0369             return -EFAULT;
0370     }
0371     force_successful_syscall_return();
0372     return compat_jiffies_to_clock_t(jiffies);
0373 }
0374 
0375 #ifdef __ARCH_WANT_SYS_SIGPENDING
0376 
0377 /*
0378  * Assumption: old_sigset_t and compat_old_sigset_t are both
0379  * types that can be passed to put_user()/get_user().
0380  */
0381 
0382 COMPAT_SYSCALL_DEFINE1(sigpending, compat_old_sigset_t __user *, set)
0383 {
0384     old_sigset_t s;
0385     long ret;
0386     mm_segment_t old_fs = get_fs();
0387 
0388     set_fs(KERNEL_DS);
0389     ret = sys_sigpending((old_sigset_t __user *) &s);
0390     set_fs(old_fs);
0391     if (ret == 0)
0392         ret = put_user(s, set);
0393     return ret;
0394 }
0395 
0396 #endif
0397 
0398 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
0399 
0400 /*
0401  * sys_sigprocmask SIG_SETMASK sets the first (compat) word of the
0402  * blocked set of signals to the supplied signal set
0403  */
0404 static inline void compat_sig_setmask(sigset_t *blocked, compat_sigset_word set)
0405 {
0406     memcpy(blocked->sig, &set, sizeof(set));
0407 }
0408 
0409 COMPAT_SYSCALL_DEFINE3(sigprocmask, int, how,
0410                compat_old_sigset_t __user *, nset,
0411                compat_old_sigset_t __user *, oset)
0412 {
0413     old_sigset_t old_set, new_set;
0414     sigset_t new_blocked;
0415 
0416     old_set = current->blocked.sig[0];
0417 
0418     if (nset) {
0419         if (get_user(new_set, nset))
0420             return -EFAULT;
0421         new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
0422 
0423         new_blocked = current->blocked;
0424 
0425         switch (how) {
0426         case SIG_BLOCK:
0427             sigaddsetmask(&new_blocked, new_set);
0428             break;
0429         case SIG_UNBLOCK:
0430             sigdelsetmask(&new_blocked, new_set);
0431             break;
0432         case SIG_SETMASK:
0433             compat_sig_setmask(&new_blocked, new_set);
0434             break;
0435         default:
0436             return -EINVAL;
0437         }
0438 
0439         set_current_blocked(&new_blocked);
0440     }
0441 
0442     if (oset) {
0443         if (put_user(old_set, oset))
0444             return -EFAULT;
0445     }
0446 
0447     return 0;
0448 }
0449 
0450 #endif
0451 
0452 COMPAT_SYSCALL_DEFINE2(setrlimit, unsigned int, resource,
0453                struct compat_rlimit __user *, rlim)
0454 {
0455     struct rlimit r;
0456 
0457     if (!access_ok(VERIFY_READ, rlim, sizeof(*rlim)) ||
0458         __get_user(r.rlim_cur, &rlim->rlim_cur) ||
0459         __get_user(r.rlim_max, &rlim->rlim_max))
0460         return -EFAULT;
0461 
0462     if (r.rlim_cur == COMPAT_RLIM_INFINITY)
0463         r.rlim_cur = RLIM_INFINITY;
0464     if (r.rlim_max == COMPAT_RLIM_INFINITY)
0465         r.rlim_max = RLIM_INFINITY;
0466     return do_prlimit(current, resource, &r, NULL);
0467 }
0468 
0469 #ifdef COMPAT_RLIM_OLD_INFINITY
0470 
0471 COMPAT_SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
0472                struct compat_rlimit __user *, rlim)
0473 {
0474     struct rlimit r;
0475     int ret;
0476     mm_segment_t old_fs = get_fs();
0477 
0478     set_fs(KERNEL_DS);
0479     ret = sys_old_getrlimit(resource, (struct rlimit __user *)&r);
0480     set_fs(old_fs);
0481 
0482     if (!ret) {
0483         if (r.rlim_cur > COMPAT_RLIM_OLD_INFINITY)
0484             r.rlim_cur = COMPAT_RLIM_INFINITY;
0485         if (r.rlim_max > COMPAT_RLIM_OLD_INFINITY)
0486             r.rlim_max = COMPAT_RLIM_INFINITY;
0487 
0488         if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
0489             __put_user(r.rlim_cur, &rlim->rlim_cur) ||
0490             __put_user(r.rlim_max, &rlim->rlim_max))
0491             return -EFAULT;
0492     }
0493     return ret;
0494 }
0495 
0496 #endif
0497 
0498 COMPAT_SYSCALL_DEFINE2(getrlimit, unsigned int, resource,
0499                struct compat_rlimit __user *, rlim)
0500 {
0501     struct rlimit r;
0502     int ret;
0503 
0504     ret = do_prlimit(current, resource, NULL, &r);
0505     if (!ret) {
0506         if (r.rlim_cur > COMPAT_RLIM_INFINITY)
0507             r.rlim_cur = COMPAT_RLIM_INFINITY;
0508         if (r.rlim_max > COMPAT_RLIM_INFINITY)
0509             r.rlim_max = COMPAT_RLIM_INFINITY;
0510 
0511         if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
0512             __put_user(r.rlim_cur, &rlim->rlim_cur) ||
0513             __put_user(r.rlim_max, &rlim->rlim_max))
0514             return -EFAULT;
0515     }
0516     return ret;
0517 }
0518 
0519 int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru)
0520 {
0521     if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)) ||
0522         __put_user(r->ru_utime.tv_sec, &ru->ru_utime.tv_sec) ||
0523         __put_user(r->ru_utime.tv_usec, &ru->ru_utime.tv_usec) ||
0524         __put_user(r->ru_stime.tv_sec, &ru->ru_stime.tv_sec) ||
0525         __put_user(r->ru_stime.tv_usec, &ru->ru_stime.tv_usec) ||
0526         __put_user(r->ru_maxrss, &ru->ru_maxrss) ||
0527         __put_user(r->ru_ixrss, &ru->ru_ixrss) ||
0528         __put_user(r->ru_idrss, &ru->ru_idrss) ||
0529         __put_user(r->ru_isrss, &ru->ru_isrss) ||
0530         __put_user(r->ru_minflt, &ru->ru_minflt) ||
0531         __put_user(r->ru_majflt, &ru->ru_majflt) ||
0532         __put_user(r->ru_nswap, &ru->ru_nswap) ||
0533         __put_user(r->ru_inblock, &ru->ru_inblock) ||
0534         __put_user(r->ru_oublock, &ru->ru_oublock) ||
0535         __put_user(r->ru_msgsnd, &ru->ru_msgsnd) ||
0536         __put_user(r->ru_msgrcv, &ru->ru_msgrcv) ||
0537         __put_user(r->ru_nsignals, &ru->ru_nsignals) ||
0538         __put_user(r->ru_nvcsw, &ru->ru_nvcsw) ||
0539         __put_user(r->ru_nivcsw, &ru->ru_nivcsw))
0540         return -EFAULT;
0541     return 0;
0542 }
0543 
0544 COMPAT_SYSCALL_DEFINE4(wait4,
0545     compat_pid_t, pid,
0546     compat_uint_t __user *, stat_addr,
0547     int, options,
0548     struct compat_rusage __user *, ru)
0549 {
0550     if (!ru) {
0551         return sys_wait4(pid, stat_addr, options, NULL);
0552     } else {
0553         struct rusage r;
0554         int ret;
0555         unsigned int status;
0556         mm_segment_t old_fs = get_fs();
0557 
0558         set_fs (KERNEL_DS);
0559         ret = sys_wait4(pid,
0560                 (stat_addr ?
0561                  (unsigned int __user *) &status : NULL),
0562                 options, (struct rusage __user *) &r);
0563         set_fs (old_fs);
0564 
0565         if (ret > 0) {
0566             if (put_compat_rusage(&r, ru))
0567                 return -EFAULT;
0568             if (stat_addr && put_user(status, stat_addr))
0569                 return -EFAULT;
0570         }
0571         return ret;
0572     }
0573 }
0574 
0575 COMPAT_SYSCALL_DEFINE5(waitid,
0576         int, which, compat_pid_t, pid,
0577         struct compat_siginfo __user *, uinfo, int, options,
0578         struct compat_rusage __user *, uru)
0579 {
0580     siginfo_t info;
0581     struct rusage ru;
0582     long ret;
0583     mm_segment_t old_fs = get_fs();
0584 
0585     memset(&info, 0, sizeof(info));
0586 
0587     set_fs(KERNEL_DS);
0588     ret = sys_waitid(which, pid, (siginfo_t __user *)&info, options,
0589              uru ? (struct rusage __user *)&ru : NULL);
0590     set_fs(old_fs);
0591 
0592     if ((ret < 0) || (info.si_signo == 0))
0593         return ret;
0594 
0595     if (uru) {
0596         /* sys_waitid() overwrites everything in ru */
0597         if (COMPAT_USE_64BIT_TIME)
0598             ret = copy_to_user(uru, &ru, sizeof(ru));
0599         else
0600             ret = put_compat_rusage(&ru, uru);
0601         if (ret)
0602             return -EFAULT;
0603     }
0604 
0605     BUG_ON(info.si_code & __SI_MASK);
0606     info.si_code |= __SI_CHLD;
0607     return copy_siginfo_to_user32(uinfo, &info);
0608 }
0609 
0610 static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr,
0611                     unsigned len, struct cpumask *new_mask)
0612 {
0613     unsigned long *k;
0614 
0615     if (len < cpumask_size())
0616         memset(new_mask, 0, cpumask_size());
0617     else if (len > cpumask_size())
0618         len = cpumask_size();
0619 
0620     k = cpumask_bits(new_mask);
0621     return compat_get_bitmap(k, user_mask_ptr, len * 8);
0622 }
0623 
0624 COMPAT_SYSCALL_DEFINE3(sched_setaffinity, compat_pid_t, pid,
0625                unsigned int, len,
0626                compat_ulong_t __user *, user_mask_ptr)
0627 {
0628     cpumask_var_t new_mask;
0629     int retval;
0630 
0631     if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
0632         return -ENOMEM;
0633 
0634     retval = compat_get_user_cpu_mask(user_mask_ptr, len, new_mask);
0635     if (retval)
0636         goto out;
0637 
0638     retval = sched_setaffinity(pid, new_mask);
0639 out:
0640     free_cpumask_var(new_mask);
0641     return retval;
0642 }
0643 
0644 COMPAT_SYSCALL_DEFINE3(sched_getaffinity, compat_pid_t,  pid, unsigned int, len,
0645                compat_ulong_t __user *, user_mask_ptr)
0646 {
0647     int ret;
0648     cpumask_var_t mask;
0649 
0650     if ((len * BITS_PER_BYTE) < nr_cpu_ids)
0651         return -EINVAL;
0652     if (len & (sizeof(compat_ulong_t)-1))
0653         return -EINVAL;
0654 
0655     if (!alloc_cpumask_var(&mask, GFP_KERNEL))
0656         return -ENOMEM;
0657 
0658     ret = sched_getaffinity(pid, mask);
0659     if (ret == 0) {
0660         size_t retlen = min_t(size_t, len, cpumask_size());
0661 
0662         if (compat_put_bitmap(user_mask_ptr, cpumask_bits(mask), retlen * 8))
0663             ret = -EFAULT;
0664         else
0665             ret = retlen;
0666     }
0667     free_cpumask_var(mask);
0668 
0669     return ret;
0670 }
0671 
0672 int get_compat_itimerspec(struct itimerspec *dst,
0673               const struct compat_itimerspec __user *src)
0674 {
0675     if (__compat_get_timespec(&dst->it_interval, &src->it_interval) ||
0676         __compat_get_timespec(&dst->it_value, &src->it_value))
0677         return -EFAULT;
0678     return 0;
0679 }
0680 
0681 int put_compat_itimerspec(struct compat_itimerspec __user *dst,
0682               const struct itimerspec *src)
0683 {
0684     if (__compat_put_timespec(&src->it_interval, &dst->it_interval) ||
0685         __compat_put_timespec(&src->it_value, &dst->it_value))
0686         return -EFAULT;
0687     return 0;
0688 }
0689 
0690 COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock,
0691                struct compat_sigevent __user *, timer_event_spec,
0692                timer_t __user *, created_timer_id)
0693 {
0694     struct sigevent __user *event = NULL;
0695 
0696     if (timer_event_spec) {
0697         struct sigevent kevent;
0698 
0699         event = compat_alloc_user_space(sizeof(*event));
0700         if (get_compat_sigevent(&kevent, timer_event_spec) ||
0701             copy_to_user(event, &kevent, sizeof(*event)))
0702             return -EFAULT;
0703     }
0704 
0705     return sys_timer_create(which_clock, event, created_timer_id);
0706 }
0707 
0708 COMPAT_SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
0709                struct compat_itimerspec __user *, new,
0710                struct compat_itimerspec __user *, old)
0711 {
0712     long err;
0713     mm_segment_t oldfs;
0714     struct itimerspec newts, oldts;
0715 
0716     if (!new)
0717         return -EINVAL;
0718     if (get_compat_itimerspec(&newts, new))
0719         return -EFAULT;
0720     oldfs = get_fs();
0721     set_fs(KERNEL_DS);
0722     err = sys_timer_settime(timer_id, flags,
0723                 (struct itimerspec __user *) &newts,
0724                 (struct itimerspec __user *) &oldts);
0725     set_fs(oldfs);
0726     if (!err && old && put_compat_itimerspec(old, &oldts))
0727         return -EFAULT;
0728     return err;
0729 }
0730 
0731 COMPAT_SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
0732                struct compat_itimerspec __user *, setting)
0733 {
0734     long err;
0735     mm_segment_t oldfs;
0736     struct itimerspec ts;
0737 
0738     oldfs = get_fs();
0739     set_fs(KERNEL_DS);
0740     err = sys_timer_gettime(timer_id,
0741                 (struct itimerspec __user *) &ts);
0742     set_fs(oldfs);
0743     if (!err && put_compat_itimerspec(setting, &ts))
0744         return -EFAULT;
0745     return err;
0746 }
0747 
0748 COMPAT_SYSCALL_DEFINE2(clock_settime, clockid_t, which_clock,
0749                struct compat_timespec __user *, tp)
0750 {
0751     long err;
0752     mm_segment_t oldfs;
0753     struct timespec ts;
0754 
0755     if (compat_get_timespec(&ts, tp))
0756         return -EFAULT;
0757     oldfs = get_fs();
0758     set_fs(KERNEL_DS);
0759     err = sys_clock_settime(which_clock,
0760                 (struct timespec __user *) &ts);
0761     set_fs(oldfs);
0762     return err;
0763 }
0764 
0765 COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock,
0766                struct compat_timespec __user *, tp)
0767 {
0768     long err;
0769     mm_segment_t oldfs;
0770     struct timespec ts;
0771 
0772     oldfs = get_fs();
0773     set_fs(KERNEL_DS);
0774     err = sys_clock_gettime(which_clock,
0775                 (struct timespec __user *) &ts);
0776     set_fs(oldfs);
0777     if (!err && compat_put_timespec(&ts, tp))
0778         return -EFAULT;
0779     return err;
0780 }
0781 
0782 COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock,
0783                struct compat_timex __user *, utp)
0784 {
0785     struct timex txc;
0786     mm_segment_t oldfs;
0787     int err, ret;
0788 
0789     err = compat_get_timex(&txc, utp);
0790     if (err)
0791         return err;
0792 
0793     oldfs = get_fs();
0794     set_fs(KERNEL_DS);
0795     ret = sys_clock_adjtime(which_clock, (struct timex __user *) &txc);
0796     set_fs(oldfs);
0797 
0798     err = compat_put_timex(utp, &txc);
0799     if (err)
0800         return err;
0801 
0802     return ret;
0803 }
0804 
0805 COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock,
0806                struct compat_timespec __user *, tp)
0807 {
0808     long err;
0809     mm_segment_t oldfs;
0810     struct timespec ts;
0811 
0812     oldfs = get_fs();
0813     set_fs(KERNEL_DS);
0814     err = sys_clock_getres(which_clock,
0815                    (struct timespec __user *) &ts);
0816     set_fs(oldfs);
0817     if (!err && tp && compat_put_timespec(&ts, tp))
0818         return -EFAULT;
0819     return err;
0820 }
0821 
0822 static long compat_clock_nanosleep_restart(struct restart_block *restart)
0823 {
0824     long err;
0825     mm_segment_t oldfs;
0826     struct timespec tu;
0827     struct compat_timespec __user *rmtp = restart->nanosleep.compat_rmtp;
0828 
0829     restart->nanosleep.rmtp = (struct timespec __user *) &tu;
0830     oldfs = get_fs();
0831     set_fs(KERNEL_DS);
0832     err = clock_nanosleep_restart(restart);
0833     set_fs(oldfs);
0834 
0835     if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
0836         compat_put_timespec(&tu, rmtp))
0837         return -EFAULT;
0838 
0839     if (err == -ERESTART_RESTARTBLOCK) {
0840         restart->fn = compat_clock_nanosleep_restart;
0841         restart->nanosleep.compat_rmtp = rmtp;
0842     }
0843     return err;
0844 }
0845 
0846 COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags,
0847                struct compat_timespec __user *, rqtp,
0848                struct compat_timespec __user *, rmtp)
0849 {
0850     long err;
0851     mm_segment_t oldfs;
0852     struct timespec in, out;
0853     struct restart_block *restart;
0854 
0855     if (compat_get_timespec(&in, rqtp))
0856         return -EFAULT;
0857 
0858     oldfs = get_fs();
0859     set_fs(KERNEL_DS);
0860     err = sys_clock_nanosleep(which_clock, flags,
0861                   (struct timespec __user *) &in,
0862                   (struct timespec __user *) &out);
0863     set_fs(oldfs);
0864 
0865     if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
0866         compat_put_timespec(&out, rmtp))
0867         return -EFAULT;
0868 
0869     if (err == -ERESTART_RESTARTBLOCK) {
0870         restart = &current->restart_block;
0871         restart->fn = compat_clock_nanosleep_restart;
0872         restart->nanosleep.compat_rmtp = rmtp;
0873     }
0874     return err;
0875 }
0876 
0877 /*
0878  * We currently only need the following fields from the sigevent
0879  * structure: sigev_value, sigev_signo, sig_notify and (sometimes
0880  * sigev_notify_thread_id).  The others are handled in user mode.
0881  * We also assume that copying sigev_value.sival_int is sufficient
0882  * to keep all the bits of sigev_value.sival_ptr intact.
0883  */
0884 int get_compat_sigevent(struct sigevent *event,
0885         const struct compat_sigevent __user *u_event)
0886 {
0887     memset(event, 0, sizeof(*event));
0888     return (!access_ok(VERIFY_READ, u_event, sizeof(*u_event)) ||
0889         __get_user(event->sigev_value.sival_int,
0890             &u_event->sigev_value.sival_int) ||
0891         __get_user(event->sigev_signo, &u_event->sigev_signo) ||
0892         __get_user(event->sigev_notify, &u_event->sigev_notify) ||
0893         __get_user(event->sigev_notify_thread_id,
0894             &u_event->sigev_notify_thread_id))
0895         ? -EFAULT : 0;
0896 }
0897 
0898 long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask,
0899                unsigned long bitmap_size)
0900 {
0901     int i, j;
0902     unsigned long m;
0903     compat_ulong_t um;
0904     unsigned long nr_compat_longs;
0905 
0906     /* align bitmap up to nearest compat_long_t boundary */
0907     bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
0908 
0909     if (!access_ok(VERIFY_READ, umask, bitmap_size / 8))
0910         return -EFAULT;
0911 
0912     nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
0913 
0914     for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
0915         m = 0;
0916 
0917         for (j = 0; j < sizeof(m)/sizeof(um); j++) {
0918             /*
0919              * We dont want to read past the end of the userspace
0920              * bitmap. We must however ensure the end of the
0921              * kernel bitmap is zeroed.
0922              */
0923             if (nr_compat_longs) {
0924                 nr_compat_longs--;
0925                 if (__get_user(um, umask))
0926                     return -EFAULT;
0927             } else {
0928                 um = 0;
0929             }
0930 
0931             umask++;
0932             m |= (long)um << (j * BITS_PER_COMPAT_LONG);
0933         }
0934         *mask++ = m;
0935     }
0936 
0937     return 0;
0938 }
0939 
0940 long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
0941                unsigned long bitmap_size)
0942 {
0943     int i, j;
0944     unsigned long m;
0945     compat_ulong_t um;
0946     unsigned long nr_compat_longs;
0947 
0948     /* align bitmap up to nearest compat_long_t boundary */
0949     bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
0950 
0951     if (!access_ok(VERIFY_WRITE, umask, bitmap_size / 8))
0952         return -EFAULT;
0953 
0954     nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
0955 
0956     for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
0957         m = *mask++;
0958 
0959         for (j = 0; j < sizeof(m)/sizeof(um); j++) {
0960             um = m;
0961 
0962             /*
0963              * We dont want to write past the end of the userspace
0964              * bitmap.
0965              */
0966             if (nr_compat_longs) {
0967                 nr_compat_longs--;
0968                 if (__put_user(um, umask))
0969                     return -EFAULT;
0970             }
0971 
0972             umask++;
0973             m >>= 4*sizeof(um);
0974             m >>= 4*sizeof(um);
0975         }
0976     }
0977 
0978     return 0;
0979 }
0980 
0981 void
0982 sigset_from_compat(sigset_t *set, const compat_sigset_t *compat)
0983 {
0984     switch (_NSIG_WORDS) {
0985     case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32 );
0986     case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32 );
0987     case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32 );
0988     case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32 );
0989     }
0990 }
0991 EXPORT_SYMBOL_GPL(sigset_from_compat);
0992 
0993 void
0994 sigset_to_compat(compat_sigset_t *compat, const sigset_t *set)
0995 {
0996     switch (_NSIG_WORDS) {
0997     case 4: compat->sig[7] = (set->sig[3] >> 32); compat->sig[6] = set->sig[3];
0998     case 3: compat->sig[5] = (set->sig[2] >> 32); compat->sig[4] = set->sig[2];
0999     case 2: compat->sig[3] = (set->sig[1] >> 32); compat->sig[2] = set->sig[1];
1000     case 1: compat->sig[1] = (set->sig[0] >> 32); compat->sig[0] = set->sig[0];
1001     }
1002 }
1003 
1004 COMPAT_SYSCALL_DEFINE4(rt_sigtimedwait, compat_sigset_t __user *, uthese,
1005         struct compat_siginfo __user *, uinfo,
1006         struct compat_timespec __user *, uts, compat_size_t, sigsetsize)
1007 {
1008     compat_sigset_t s32;
1009     sigset_t s;
1010     struct timespec t;
1011     siginfo_t info;
1012     long ret;
1013 
1014     if (sigsetsize != sizeof(sigset_t))
1015         return -EINVAL;
1016 
1017     if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t)))
1018         return -EFAULT;
1019     sigset_from_compat(&s, &s32);
1020 
1021     if (uts) {
1022         if (compat_get_timespec(&t, uts))
1023             return -EFAULT;
1024     }
1025 
1026     ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);
1027 
1028     if (ret > 0 && uinfo) {
1029         if (copy_siginfo_to_user32(uinfo, &info))
1030             ret = -EFAULT;
1031     }
1032 
1033     return ret;
1034 }
1035 
1036 #ifdef __ARCH_WANT_COMPAT_SYS_TIME
1037 
1038 /* compat_time_t is a 32 bit "long" and needs to get converted. */
1039 
1040 COMPAT_SYSCALL_DEFINE1(time, compat_time_t __user *, tloc)
1041 {
1042     compat_time_t i;
1043     struct timeval tv;
1044 
1045     do_gettimeofday(&tv);
1046     i = tv.tv_sec;
1047 
1048     if (tloc) {
1049         if (put_user(i,tloc))
1050             return -EFAULT;
1051     }
1052     force_successful_syscall_return();
1053     return i;
1054 }
1055 
1056 COMPAT_SYSCALL_DEFINE1(stime, compat_time_t __user *, tptr)
1057 {
1058     struct timespec tv;
1059     int err;
1060 
1061     if (get_user(tv.tv_sec, tptr))
1062         return -EFAULT;
1063 
1064     tv.tv_nsec = 0;
1065 
1066     err = security_settime(&tv, NULL);
1067     if (err)
1068         return err;
1069 
1070     do_settimeofday(&tv);
1071     return 0;
1072 }
1073 
1074 #endif /* __ARCH_WANT_COMPAT_SYS_TIME */
1075 
1076 COMPAT_SYSCALL_DEFINE1(adjtimex, struct compat_timex __user *, utp)
1077 {
1078     struct timex txc;
1079     int err, ret;
1080 
1081     err = compat_get_timex(&txc, utp);
1082     if (err)
1083         return err;
1084 
1085     ret = do_adjtimex(&txc);
1086 
1087     err = compat_put_timex(utp, &txc);
1088     if (err)
1089         return err;
1090 
1091     return ret;
1092 }
1093 
1094 #ifdef CONFIG_NUMA
1095 COMPAT_SYSCALL_DEFINE6(move_pages, pid_t, pid, compat_ulong_t, nr_pages,
1096                compat_uptr_t __user *, pages32,
1097                const int __user *, nodes,
1098                int __user *, status,
1099                int, flags)
1100 {
1101     const void __user * __user *pages;
1102     int i;
1103 
1104     pages = compat_alloc_user_space(nr_pages * sizeof(void *));
1105     for (i = 0; i < nr_pages; i++) {
1106         compat_uptr_t p;
1107 
1108         if (get_user(p, pages32 + i) ||
1109             put_user(compat_ptr(p), pages + i))
1110             return -EFAULT;
1111     }
1112     return sys_move_pages(pid, nr_pages, pages, nodes, status, flags);
1113 }
1114 
1115 COMPAT_SYSCALL_DEFINE4(migrate_pages, compat_pid_t, pid,
1116                compat_ulong_t, maxnode,
1117                const compat_ulong_t __user *, old_nodes,
1118                const compat_ulong_t __user *, new_nodes)
1119 {
1120     unsigned long __user *old = NULL;
1121     unsigned long __user *new = NULL;
1122     nodemask_t tmp_mask;
1123     unsigned long nr_bits;
1124     unsigned long size;
1125 
1126     nr_bits = min_t(unsigned long, maxnode - 1, MAX_NUMNODES);
1127     size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
1128     if (old_nodes) {
1129         if (compat_get_bitmap(nodes_addr(tmp_mask), old_nodes, nr_bits))
1130             return -EFAULT;
1131         old = compat_alloc_user_space(new_nodes ? size * 2 : size);
1132         if (new_nodes)
1133             new = old + size / sizeof(unsigned long);
1134         if (copy_to_user(old, nodes_addr(tmp_mask), size))
1135             return -EFAULT;
1136     }
1137     if (new_nodes) {
1138         if (compat_get_bitmap(nodes_addr(tmp_mask), new_nodes, nr_bits))
1139             return -EFAULT;
1140         if (new == NULL)
1141             new = compat_alloc_user_space(size);
1142         if (copy_to_user(new, nodes_addr(tmp_mask), size))
1143             return -EFAULT;
1144     }
1145     return sys_migrate_pages(pid, nr_bits + 1, old, new);
1146 }
1147 #endif
1148 
1149 COMPAT_SYSCALL_DEFINE2(sched_rr_get_interval,
1150                compat_pid_t, pid,
1151                struct compat_timespec __user *, interval)
1152 {
1153     struct timespec t;
1154     int ret;
1155     mm_segment_t old_fs = get_fs();
1156 
1157     set_fs(KERNEL_DS);
1158     ret = sys_sched_rr_get_interval(pid, (struct timespec __user *)&t);
1159     set_fs(old_fs);
1160     if (compat_put_timespec(&t, interval))
1161         return -EFAULT;
1162     return ret;
1163 }
1164 
1165 /*
1166  * Allocate user-space memory for the duration of a single system call,
1167  * in order to marshall parameters inside a compat thunk.
1168  */
1169 void __user *compat_alloc_user_space(unsigned long len)
1170 {
1171     void __user *ptr;
1172 
1173     /* If len would occupy more than half of the entire compat space... */
1174     if (unlikely(len > (((compat_uptr_t)~0) >> 1)))
1175         return NULL;
1176 
1177     ptr = arch_compat_alloc_user_space(len);
1178 
1179     if (unlikely(!access_ok(VERIFY_WRITE, ptr, len)))
1180         return NULL;
1181 
1182     return ptr;
1183 }
1184 EXPORT_SYMBOL_GPL(compat_alloc_user_space);