compat.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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. #include <linux/linkage.h>
  14. #include <linux/compat.h>
  15. #include <linux/errno.h>
  16. #include <linux/time.h>
  17. #include <linux/signal.h>
  18. #include <linux/sched.h> /* for MAX_SCHEDULE_TIMEOUT */
  19. #include <linux/syscalls.h>
  20. #include <linux/unistd.h>
  21. #include <linux/security.h>
  22. #include <linux/timex.h>
  23. #include <linux/export.h>
  24. #include <linux/migrate.h>
  25. #include <linux/posix-timers.h>
  26. #include <linux/times.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/gfp.h>
  29. #include <linux/uaccess.h>
  30. int compat_get_timex(struct timex *txc, const struct compat_timex __user *utp)
  31. {
  32. struct compat_timex tx32;
  33. if (copy_from_user(&tx32, utp, sizeof(struct compat_timex)))
  34. return -EFAULT;
  35. txc->modes = tx32.modes;
  36. txc->offset = tx32.offset;
  37. txc->freq = tx32.freq;
  38. txc->maxerror = tx32.maxerror;
  39. txc->esterror = tx32.esterror;
  40. txc->status = tx32.status;
  41. txc->constant = tx32.constant;
  42. txc->precision = tx32.precision;
  43. txc->tolerance = tx32.tolerance;
  44. txc->time.tv_sec = tx32.time.tv_sec;
  45. txc->time.tv_usec = tx32.time.tv_usec;
  46. txc->tick = tx32.tick;
  47. txc->ppsfreq = tx32.ppsfreq;
  48. txc->jitter = tx32.jitter;
  49. txc->shift = tx32.shift;
  50. txc->stabil = tx32.stabil;
  51. txc->jitcnt = tx32.jitcnt;
  52. txc->calcnt = tx32.calcnt;
  53. txc->errcnt = tx32.errcnt;
  54. txc->stbcnt = tx32.stbcnt;
  55. return 0;
  56. }
  57. int compat_put_timex(struct compat_timex __user *utp, const struct timex *txc)
  58. {
  59. struct compat_timex tx32;
  60. memset(&tx32, 0, sizeof(struct compat_timex));
  61. tx32.modes = txc->modes;
  62. tx32.offset = txc->offset;
  63. tx32.freq = txc->freq;
  64. tx32.maxerror = txc->maxerror;
  65. tx32.esterror = txc->esterror;
  66. tx32.status = txc->status;
  67. tx32.constant = txc->constant;
  68. tx32.precision = txc->precision;
  69. tx32.tolerance = txc->tolerance;
  70. tx32.time.tv_sec = txc->time.tv_sec;
  71. tx32.time.tv_usec = txc->time.tv_usec;
  72. tx32.tick = txc->tick;
  73. tx32.ppsfreq = txc->ppsfreq;
  74. tx32.jitter = txc->jitter;
  75. tx32.shift = txc->shift;
  76. tx32.stabil = txc->stabil;
  77. tx32.jitcnt = txc->jitcnt;
  78. tx32.calcnt = txc->calcnt;
  79. tx32.errcnt = txc->errcnt;
  80. tx32.stbcnt = txc->stbcnt;
  81. tx32.tai = txc->tai;
  82. if (copy_to_user(utp, &tx32, sizeof(struct compat_timex)))
  83. return -EFAULT;
  84. return 0;
  85. }
  86. static int __compat_get_timeval(struct timeval *tv, const struct compat_timeval __user *ctv)
  87. {
  88. return (!access_ok(VERIFY_READ, ctv, sizeof(*ctv)) ||
  89. __get_user(tv->tv_sec, &ctv->tv_sec) ||
  90. __get_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0;
  91. }
  92. static int __compat_put_timeval(const struct timeval *tv, struct compat_timeval __user *ctv)
  93. {
  94. return (!access_ok(VERIFY_WRITE, ctv, sizeof(*ctv)) ||
  95. __put_user(tv->tv_sec, &ctv->tv_sec) ||
  96. __put_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0;
  97. }
  98. static int __compat_get_timespec(struct timespec *ts, const struct compat_timespec __user *cts)
  99. {
  100. return (!access_ok(VERIFY_READ, cts, sizeof(*cts)) ||
  101. __get_user(ts->tv_sec, &cts->tv_sec) ||
  102. __get_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
  103. }
  104. static int __compat_put_timespec(const struct timespec *ts, struct compat_timespec __user *cts)
  105. {
  106. return (!access_ok(VERIFY_WRITE, cts, sizeof(*cts)) ||
  107. __put_user(ts->tv_sec, &cts->tv_sec) ||
  108. __put_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
  109. }
  110. int compat_get_timeval(struct timeval *tv, const void __user *utv)
  111. {
  112. if (COMPAT_USE_64BIT_TIME)
  113. return copy_from_user(tv, utv, sizeof(*tv)) ? -EFAULT : 0;
  114. else
  115. return __compat_get_timeval(tv, utv);
  116. }
  117. EXPORT_SYMBOL_GPL(compat_get_timeval);
  118. int compat_put_timeval(const struct timeval *tv, void __user *utv)
  119. {
  120. if (COMPAT_USE_64BIT_TIME)
  121. return copy_to_user(utv, tv, sizeof(*tv)) ? -EFAULT : 0;
  122. else
  123. return __compat_put_timeval(tv, utv);
  124. }
  125. EXPORT_SYMBOL_GPL(compat_put_timeval);
  126. int compat_get_timespec(struct timespec *ts, const void __user *uts)
  127. {
  128. if (COMPAT_USE_64BIT_TIME)
  129. return copy_from_user(ts, uts, sizeof(*ts)) ? -EFAULT : 0;
  130. else
  131. return __compat_get_timespec(ts, uts);
  132. }
  133. EXPORT_SYMBOL_GPL(compat_get_timespec);
  134. int compat_put_timespec(const struct timespec *ts, void __user *uts)
  135. {
  136. if (COMPAT_USE_64BIT_TIME)
  137. return copy_to_user(uts, ts, sizeof(*ts)) ? -EFAULT : 0;
  138. else
  139. return __compat_put_timespec(ts, uts);
  140. }
  141. EXPORT_SYMBOL_GPL(compat_put_timespec);
  142. int compat_convert_timespec(struct timespec __user **kts,
  143. const void __user *cts)
  144. {
  145. struct timespec ts;
  146. struct timespec __user *uts;
  147. if (!cts || COMPAT_USE_64BIT_TIME) {
  148. *kts = (struct timespec __user *)cts;
  149. return 0;
  150. }
  151. uts = compat_alloc_user_space(sizeof(ts));
  152. if (!uts)
  153. return -EFAULT;
  154. if (compat_get_timespec(&ts, cts))
  155. return -EFAULT;
  156. if (copy_to_user(uts, &ts, sizeof(ts)))
  157. return -EFAULT;
  158. *kts = uts;
  159. return 0;
  160. }
  161. int get_compat_itimerval(struct itimerval *o, const struct compat_itimerval __user *i)
  162. {
  163. struct compat_itimerval v32;
  164. if (copy_from_user(&v32, i, sizeof(struct compat_itimerval)))
  165. return -EFAULT;
  166. o->it_interval.tv_sec = v32.it_interval.tv_sec;
  167. o->it_interval.tv_usec = v32.it_interval.tv_usec;
  168. o->it_value.tv_sec = v32.it_value.tv_sec;
  169. o->it_value.tv_usec = v32.it_value.tv_usec;
  170. return 0;
  171. }
  172. int put_compat_itimerval(struct compat_itimerval __user *o, const struct itimerval *i)
  173. {
  174. struct compat_itimerval v32;
  175. v32.it_interval.tv_sec = i->it_interval.tv_sec;
  176. v32.it_interval.tv_usec = i->it_interval.tv_usec;
  177. v32.it_value.tv_sec = i->it_value.tv_sec;
  178. v32.it_value.tv_usec = i->it_value.tv_usec;
  179. return copy_to_user(o, &v32, sizeof(struct compat_itimerval)) ? -EFAULT : 0;
  180. }
  181. static compat_clock_t clock_t_to_compat_clock_t(clock_t x)
  182. {
  183. return compat_jiffies_to_clock_t(clock_t_to_jiffies(x));
  184. }
  185. COMPAT_SYSCALL_DEFINE1(times, struct compat_tms __user *, tbuf)
  186. {
  187. if (tbuf) {
  188. struct tms tms;
  189. struct compat_tms tmp;
  190. do_sys_times(&tms);
  191. /* Convert our struct tms to the compat version. */
  192. tmp.tms_utime = clock_t_to_compat_clock_t(tms.tms_utime);
  193. tmp.tms_stime = clock_t_to_compat_clock_t(tms.tms_stime);
  194. tmp.tms_cutime = clock_t_to_compat_clock_t(tms.tms_cutime);
  195. tmp.tms_cstime = clock_t_to_compat_clock_t(tms.tms_cstime);
  196. if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
  197. return -EFAULT;
  198. }
  199. force_successful_syscall_return();
  200. return compat_jiffies_to_clock_t(jiffies);
  201. }
  202. #ifdef __ARCH_WANT_SYS_SIGPENDING
  203. /*
  204. * Assumption: old_sigset_t and compat_old_sigset_t are both
  205. * types that can be passed to put_user()/get_user().
  206. */
  207. COMPAT_SYSCALL_DEFINE1(sigpending, compat_old_sigset_t __user *, set)
  208. {
  209. old_sigset_t s;
  210. long ret;
  211. mm_segment_t old_fs = get_fs();
  212. set_fs(KERNEL_DS);
  213. ret = sys_sigpending((old_sigset_t __user *) &s);
  214. set_fs(old_fs);
  215. if (ret == 0)
  216. ret = put_user(s, set);
  217. return ret;
  218. }
  219. #endif
  220. #ifdef __ARCH_WANT_SYS_SIGPROCMASK
  221. /*
  222. * sys_sigprocmask SIG_SETMASK sets the first (compat) word of the
  223. * blocked set of signals to the supplied signal set
  224. */
  225. static inline void compat_sig_setmask(sigset_t *blocked, compat_sigset_word set)
  226. {
  227. memcpy(blocked->sig, &set, sizeof(set));
  228. }
  229. COMPAT_SYSCALL_DEFINE3(sigprocmask, int, how,
  230. compat_old_sigset_t __user *, nset,
  231. compat_old_sigset_t __user *, oset)
  232. {
  233. old_sigset_t old_set, new_set;
  234. sigset_t new_blocked;
  235. old_set = current->blocked.sig[0];
  236. if (nset) {
  237. if (get_user(new_set, nset))
  238. return -EFAULT;
  239. new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
  240. new_blocked = current->blocked;
  241. switch (how) {
  242. case SIG_BLOCK:
  243. sigaddsetmask(&new_blocked, new_set);
  244. break;
  245. case SIG_UNBLOCK:
  246. sigdelsetmask(&new_blocked, new_set);
  247. break;
  248. case SIG_SETMASK:
  249. compat_sig_setmask(&new_blocked, new_set);
  250. break;
  251. default:
  252. return -EINVAL;
  253. }
  254. set_current_blocked(&new_blocked);
  255. }
  256. if (oset) {
  257. if (put_user(old_set, oset))
  258. return -EFAULT;
  259. }
  260. return 0;
  261. }
  262. #endif
  263. COMPAT_SYSCALL_DEFINE2(setrlimit, unsigned int, resource,
  264. struct compat_rlimit __user *, rlim)
  265. {
  266. struct rlimit r;
  267. if (!access_ok(VERIFY_READ, rlim, sizeof(*rlim)) ||
  268. __get_user(r.rlim_cur, &rlim->rlim_cur) ||
  269. __get_user(r.rlim_max, &rlim->rlim_max))
  270. return -EFAULT;
  271. if (r.rlim_cur == COMPAT_RLIM_INFINITY)
  272. r.rlim_cur = RLIM_INFINITY;
  273. if (r.rlim_max == COMPAT_RLIM_INFINITY)
  274. r.rlim_max = RLIM_INFINITY;
  275. return do_prlimit(current, resource, &r, NULL);
  276. }
  277. #ifdef COMPAT_RLIM_OLD_INFINITY
  278. COMPAT_SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
  279. struct compat_rlimit __user *, rlim)
  280. {
  281. struct rlimit r;
  282. int ret;
  283. mm_segment_t old_fs = get_fs();
  284. set_fs(KERNEL_DS);
  285. ret = sys_old_getrlimit(resource, (struct rlimit __user *)&r);
  286. set_fs(old_fs);
  287. if (!ret) {
  288. if (r.rlim_cur > COMPAT_RLIM_OLD_INFINITY)
  289. r.rlim_cur = COMPAT_RLIM_INFINITY;
  290. if (r.rlim_max > COMPAT_RLIM_OLD_INFINITY)
  291. r.rlim_max = COMPAT_RLIM_INFINITY;
  292. if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
  293. __put_user(r.rlim_cur, &rlim->rlim_cur) ||
  294. __put_user(r.rlim_max, &rlim->rlim_max))
  295. return -EFAULT;
  296. }
  297. return ret;
  298. }
  299. #endif
  300. COMPAT_SYSCALL_DEFINE2(getrlimit, unsigned int, resource,
  301. struct compat_rlimit __user *, rlim)
  302. {
  303. struct rlimit r;
  304. int ret;
  305. ret = do_prlimit(current, resource, NULL, &r);
  306. if (!ret) {
  307. if (r.rlim_cur > COMPAT_RLIM_INFINITY)
  308. r.rlim_cur = COMPAT_RLIM_INFINITY;
  309. if (r.rlim_max > COMPAT_RLIM_INFINITY)
  310. r.rlim_max = COMPAT_RLIM_INFINITY;
  311. if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
  312. __put_user(r.rlim_cur, &rlim->rlim_cur) ||
  313. __put_user(r.rlim_max, &rlim->rlim_max))
  314. return -EFAULT;
  315. }
  316. return ret;
  317. }
  318. int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru)
  319. {
  320. if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)) ||
  321. __put_user(r->ru_utime.tv_sec, &ru->ru_utime.tv_sec) ||
  322. __put_user(r->ru_utime.tv_usec, &ru->ru_utime.tv_usec) ||
  323. __put_user(r->ru_stime.tv_sec, &ru->ru_stime.tv_sec) ||
  324. __put_user(r->ru_stime.tv_usec, &ru->ru_stime.tv_usec) ||
  325. __put_user(r->ru_maxrss, &ru->ru_maxrss) ||
  326. __put_user(r->ru_ixrss, &ru->ru_ixrss) ||
  327. __put_user(r->ru_idrss, &ru->ru_idrss) ||
  328. __put_user(r->ru_isrss, &ru->ru_isrss) ||
  329. __put_user(r->ru_minflt, &ru->ru_minflt) ||
  330. __put_user(r->ru_majflt, &ru->ru_majflt) ||
  331. __put_user(r->ru_nswap, &ru->ru_nswap) ||
  332. __put_user(r->ru_inblock, &ru->ru_inblock) ||
  333. __put_user(r->ru_oublock, &ru->ru_oublock) ||
  334. __put_user(r->ru_msgsnd, &ru->ru_msgsnd) ||
  335. __put_user(r->ru_msgrcv, &ru->ru_msgrcv) ||
  336. __put_user(r->ru_nsignals, &ru->ru_nsignals) ||
  337. __put_user(r->ru_nvcsw, &ru->ru_nvcsw) ||
  338. __put_user(r->ru_nivcsw, &ru->ru_nivcsw))
  339. return -EFAULT;
  340. return 0;
  341. }
  342. static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr,
  343. unsigned len, struct cpumask *new_mask)
  344. {
  345. unsigned long *k;
  346. if (len < cpumask_size())
  347. memset(new_mask, 0, cpumask_size());
  348. else if (len > cpumask_size())
  349. len = cpumask_size();
  350. k = cpumask_bits(new_mask);
  351. return compat_get_bitmap(k, user_mask_ptr, len * 8);
  352. }
  353. COMPAT_SYSCALL_DEFINE3(sched_setaffinity, compat_pid_t, pid,
  354. unsigned int, len,
  355. compat_ulong_t __user *, user_mask_ptr)
  356. {
  357. cpumask_var_t new_mask;
  358. int retval;
  359. if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
  360. return -ENOMEM;
  361. retval = compat_get_user_cpu_mask(user_mask_ptr, len, new_mask);
  362. if (retval)
  363. goto out;
  364. retval = sched_setaffinity(pid, new_mask);
  365. out:
  366. free_cpumask_var(new_mask);
  367. return retval;
  368. }
  369. COMPAT_SYSCALL_DEFINE3(sched_getaffinity, compat_pid_t, pid, unsigned int, len,
  370. compat_ulong_t __user *, user_mask_ptr)
  371. {
  372. int ret;
  373. cpumask_var_t mask;
  374. if ((len * BITS_PER_BYTE) < nr_cpu_ids)
  375. return -EINVAL;
  376. if (len & (sizeof(compat_ulong_t)-1))
  377. return -EINVAL;
  378. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  379. return -ENOMEM;
  380. ret = sched_getaffinity(pid, mask);
  381. if (ret == 0) {
  382. size_t retlen = min_t(size_t, len, cpumask_size());
  383. if (compat_put_bitmap(user_mask_ptr, cpumask_bits(mask), retlen * 8))
  384. ret = -EFAULT;
  385. else
  386. ret = retlen;
  387. }
  388. free_cpumask_var(mask);
  389. return ret;
  390. }
  391. int get_compat_itimerspec(struct itimerspec *dst,
  392. const struct compat_itimerspec __user *src)
  393. {
  394. if (__compat_get_timespec(&dst->it_interval, &src->it_interval) ||
  395. __compat_get_timespec(&dst->it_value, &src->it_value))
  396. return -EFAULT;
  397. return 0;
  398. }
  399. int put_compat_itimerspec(struct compat_itimerspec __user *dst,
  400. const struct itimerspec *src)
  401. {
  402. if (__compat_put_timespec(&src->it_interval, &dst->it_interval) ||
  403. __compat_put_timespec(&src->it_value, &dst->it_value))
  404. return -EFAULT;
  405. return 0;
  406. }
  407. /*
  408. * We currently only need the following fields from the sigevent
  409. * structure: sigev_value, sigev_signo, sig_notify and (sometimes
  410. * sigev_notify_thread_id). The others are handled in user mode.
  411. * We also assume that copying sigev_value.sival_int is sufficient
  412. * to keep all the bits of sigev_value.sival_ptr intact.
  413. */
  414. int get_compat_sigevent(struct sigevent *event,
  415. const struct compat_sigevent __user *u_event)
  416. {
  417. memset(event, 0, sizeof(*event));
  418. return (!access_ok(VERIFY_READ, u_event, sizeof(*u_event)) ||
  419. __get_user(event->sigev_value.sival_int,
  420. &u_event->sigev_value.sival_int) ||
  421. __get_user(event->sigev_signo, &u_event->sigev_signo) ||
  422. __get_user(event->sigev_notify, &u_event->sigev_notify) ||
  423. __get_user(event->sigev_notify_thread_id,
  424. &u_event->sigev_notify_thread_id))
  425. ? -EFAULT : 0;
  426. }
  427. long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask,
  428. unsigned long bitmap_size)
  429. {
  430. int i, j;
  431. unsigned long m;
  432. compat_ulong_t um;
  433. unsigned long nr_compat_longs;
  434. /* align bitmap up to nearest compat_long_t boundary */
  435. bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
  436. if (!access_ok(VERIFY_READ, umask, bitmap_size / 8))
  437. return -EFAULT;
  438. nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
  439. for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
  440. m = 0;
  441. for (j = 0; j < sizeof(m)/sizeof(um); j++) {
  442. /*
  443. * We dont want to read past the end of the userspace
  444. * bitmap. We must however ensure the end of the
  445. * kernel bitmap is zeroed.
  446. */
  447. if (nr_compat_longs) {
  448. nr_compat_longs--;
  449. if (__get_user(um, umask))
  450. return -EFAULT;
  451. } else {
  452. um = 0;
  453. }
  454. umask++;
  455. m |= (long)um << (j * BITS_PER_COMPAT_LONG);
  456. }
  457. *mask++ = m;
  458. }
  459. return 0;
  460. }
  461. long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
  462. unsigned long bitmap_size)
  463. {
  464. int i, j;
  465. unsigned long m;
  466. compat_ulong_t um;
  467. unsigned long nr_compat_longs;
  468. /* align bitmap up to nearest compat_long_t boundary */
  469. bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
  470. if (!access_ok(VERIFY_WRITE, umask, bitmap_size / 8))
  471. return -EFAULT;
  472. nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
  473. for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
  474. m = *mask++;
  475. for (j = 0; j < sizeof(m)/sizeof(um); j++) {
  476. um = m;
  477. /*
  478. * We dont want to write past the end of the userspace
  479. * bitmap.
  480. */
  481. if (nr_compat_longs) {
  482. nr_compat_longs--;
  483. if (__put_user(um, umask))
  484. return -EFAULT;
  485. }
  486. umask++;
  487. m >>= 4*sizeof(um);
  488. m >>= 4*sizeof(um);
  489. }
  490. }
  491. return 0;
  492. }
  493. void
  494. sigset_from_compat(sigset_t *set, const compat_sigset_t *compat)
  495. {
  496. switch (_NSIG_WORDS) {
  497. case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32 );
  498. case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32 );
  499. case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32 );
  500. case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32 );
  501. }
  502. }
  503. EXPORT_SYMBOL_GPL(sigset_from_compat);
  504. void
  505. sigset_to_compat(compat_sigset_t *compat, const sigset_t *set)
  506. {
  507. switch (_NSIG_WORDS) {
  508. case 4: compat->sig[7] = (set->sig[3] >> 32); compat->sig[6] = set->sig[3];
  509. case 3: compat->sig[5] = (set->sig[2] >> 32); compat->sig[4] = set->sig[2];
  510. case 2: compat->sig[3] = (set->sig[1] >> 32); compat->sig[2] = set->sig[1];
  511. case 1: compat->sig[1] = (set->sig[0] >> 32); compat->sig[0] = set->sig[0];
  512. }
  513. }
  514. COMPAT_SYSCALL_DEFINE4(rt_sigtimedwait, compat_sigset_t __user *, uthese,
  515. struct compat_siginfo __user *, uinfo,
  516. struct compat_timespec __user *, uts, compat_size_t, sigsetsize)
  517. {
  518. compat_sigset_t s32;
  519. sigset_t s;
  520. struct timespec t;
  521. siginfo_t info;
  522. long ret;
  523. if (sigsetsize != sizeof(sigset_t))
  524. return -EINVAL;
  525. if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t)))
  526. return -EFAULT;
  527. sigset_from_compat(&s, &s32);
  528. if (uts) {
  529. if (compat_get_timespec(&t, uts))
  530. return -EFAULT;
  531. }
  532. ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);
  533. if (ret > 0 && uinfo) {
  534. if (copy_siginfo_to_user32(uinfo, &info))
  535. ret = -EFAULT;
  536. }
  537. return ret;
  538. }
  539. #ifdef CONFIG_NUMA
  540. COMPAT_SYSCALL_DEFINE6(move_pages, pid_t, pid, compat_ulong_t, nr_pages,
  541. compat_uptr_t __user *, pages32,
  542. const int __user *, nodes,
  543. int __user *, status,
  544. int, flags)
  545. {
  546. const void __user * __user *pages;
  547. int i;
  548. pages = compat_alloc_user_space(nr_pages * sizeof(void *));
  549. for (i = 0; i < nr_pages; i++) {
  550. compat_uptr_t p;
  551. if (get_user(p, pages32 + i) ||
  552. put_user(compat_ptr(p), pages + i))
  553. return -EFAULT;
  554. }
  555. return sys_move_pages(pid, nr_pages, pages, nodes, status, flags);
  556. }
  557. COMPAT_SYSCALL_DEFINE4(migrate_pages, compat_pid_t, pid,
  558. compat_ulong_t, maxnode,
  559. const compat_ulong_t __user *, old_nodes,
  560. const compat_ulong_t __user *, new_nodes)
  561. {
  562. unsigned long __user *old = NULL;
  563. unsigned long __user *new = NULL;
  564. nodemask_t tmp_mask;
  565. unsigned long nr_bits;
  566. unsigned long size;
  567. nr_bits = min_t(unsigned long, maxnode - 1, MAX_NUMNODES);
  568. size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
  569. if (old_nodes) {
  570. if (compat_get_bitmap(nodes_addr(tmp_mask), old_nodes, nr_bits))
  571. return -EFAULT;
  572. old = compat_alloc_user_space(new_nodes ? size * 2 : size);
  573. if (new_nodes)
  574. new = old + size / sizeof(unsigned long);
  575. if (copy_to_user(old, nodes_addr(tmp_mask), size))
  576. return -EFAULT;
  577. }
  578. if (new_nodes) {
  579. if (compat_get_bitmap(nodes_addr(tmp_mask), new_nodes, nr_bits))
  580. return -EFAULT;
  581. if (new == NULL)
  582. new = compat_alloc_user_space(size);
  583. if (copy_to_user(new, nodes_addr(tmp_mask), size))
  584. return -EFAULT;
  585. }
  586. return sys_migrate_pages(pid, nr_bits + 1, old, new);
  587. }
  588. #endif
  589. COMPAT_SYSCALL_DEFINE2(sched_rr_get_interval,
  590. compat_pid_t, pid,
  591. struct compat_timespec __user *, interval)
  592. {
  593. struct timespec t;
  594. int ret;
  595. mm_segment_t old_fs = get_fs();
  596. set_fs(KERNEL_DS);
  597. ret = sys_sched_rr_get_interval(pid, (struct timespec __user *)&t);
  598. set_fs(old_fs);
  599. if (compat_put_timespec(&t, interval))
  600. return -EFAULT;
  601. return ret;
  602. }
  603. /*
  604. * Allocate user-space memory for the duration of a single system call,
  605. * in order to marshall parameters inside a compat thunk.
  606. */
  607. void __user *compat_alloc_user_space(unsigned long len)
  608. {
  609. void __user *ptr;
  610. /* If len would occupy more than half of the entire compat space... */
  611. if (unlikely(len > (((compat_uptr_t)~0) >> 1)))
  612. return NULL;
  613. ptr = arch_compat_alloc_user_space(len);
  614. if (unlikely(!access_ok(VERIFY_WRITE, ptr, len)))
  615. return NULL;
  616. return ptr;
  617. }
  618. EXPORT_SYMBOL_GPL(compat_alloc_user_space);