compat.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  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 <asm/uaccess.h>
  30. /*
  31. * Get/set struct timeval with struct timespec on the native side
  32. */
  33. static int compat_get_timeval_convert(struct timespec *o,
  34. struct compat_timeval __user *i)
  35. {
  36. long usec;
  37. if (get_user(o->tv_sec, &i->tv_sec) ||
  38. get_user(usec, &i->tv_usec))
  39. return -EFAULT;
  40. o->tv_nsec = usec * 1000;
  41. return 0;
  42. }
  43. static int compat_put_timeval_convert(struct compat_timeval __user *o,
  44. struct timeval *i)
  45. {
  46. return (put_user(i->tv_sec, &o->tv_sec) ||
  47. put_user(i->tv_usec, &o->tv_usec)) ? -EFAULT : 0;
  48. }
  49. static int compat_get_timex(struct timex *txc, struct compat_timex __user *utp)
  50. {
  51. memset(txc, 0, sizeof(struct timex));
  52. if (!access_ok(VERIFY_READ, utp, sizeof(struct compat_timex)) ||
  53. __get_user(txc->modes, &utp->modes) ||
  54. __get_user(txc->offset, &utp->offset) ||
  55. __get_user(txc->freq, &utp->freq) ||
  56. __get_user(txc->maxerror, &utp->maxerror) ||
  57. __get_user(txc->esterror, &utp->esterror) ||
  58. __get_user(txc->status, &utp->status) ||
  59. __get_user(txc->constant, &utp->constant) ||
  60. __get_user(txc->precision, &utp->precision) ||
  61. __get_user(txc->tolerance, &utp->tolerance) ||
  62. __get_user(txc->time.tv_sec, &utp->time.tv_sec) ||
  63. __get_user(txc->time.tv_usec, &utp->time.tv_usec) ||
  64. __get_user(txc->tick, &utp->tick) ||
  65. __get_user(txc->ppsfreq, &utp->ppsfreq) ||
  66. __get_user(txc->jitter, &utp->jitter) ||
  67. __get_user(txc->shift, &utp->shift) ||
  68. __get_user(txc->stabil, &utp->stabil) ||
  69. __get_user(txc->jitcnt, &utp->jitcnt) ||
  70. __get_user(txc->calcnt, &utp->calcnt) ||
  71. __get_user(txc->errcnt, &utp->errcnt) ||
  72. __get_user(txc->stbcnt, &utp->stbcnt))
  73. return -EFAULT;
  74. return 0;
  75. }
  76. static int compat_put_timex(struct compat_timex __user *utp, struct timex *txc)
  77. {
  78. if (!access_ok(VERIFY_WRITE, utp, sizeof(struct compat_timex)) ||
  79. __put_user(txc->modes, &utp->modes) ||
  80. __put_user(txc->offset, &utp->offset) ||
  81. __put_user(txc->freq, &utp->freq) ||
  82. __put_user(txc->maxerror, &utp->maxerror) ||
  83. __put_user(txc->esterror, &utp->esterror) ||
  84. __put_user(txc->status, &utp->status) ||
  85. __put_user(txc->constant, &utp->constant) ||
  86. __put_user(txc->precision, &utp->precision) ||
  87. __put_user(txc->tolerance, &utp->tolerance) ||
  88. __put_user(txc->time.tv_sec, &utp->time.tv_sec) ||
  89. __put_user(txc->time.tv_usec, &utp->time.tv_usec) ||
  90. __put_user(txc->tick, &utp->tick) ||
  91. __put_user(txc->ppsfreq, &utp->ppsfreq) ||
  92. __put_user(txc->jitter, &utp->jitter) ||
  93. __put_user(txc->shift, &utp->shift) ||
  94. __put_user(txc->stabil, &utp->stabil) ||
  95. __put_user(txc->jitcnt, &utp->jitcnt) ||
  96. __put_user(txc->calcnt, &utp->calcnt) ||
  97. __put_user(txc->errcnt, &utp->errcnt) ||
  98. __put_user(txc->stbcnt, &utp->stbcnt) ||
  99. __put_user(txc->tai, &utp->tai))
  100. return -EFAULT;
  101. return 0;
  102. }
  103. asmlinkage long compat_sys_gettimeofday(struct compat_timeval __user *tv,
  104. struct timezone __user *tz)
  105. {
  106. if (tv) {
  107. struct timeval ktv;
  108. do_gettimeofday(&ktv);
  109. if (compat_put_timeval_convert(tv, &ktv))
  110. return -EFAULT;
  111. }
  112. if (tz) {
  113. if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
  114. return -EFAULT;
  115. }
  116. return 0;
  117. }
  118. asmlinkage long compat_sys_settimeofday(struct compat_timeval __user *tv,
  119. struct timezone __user *tz)
  120. {
  121. struct timespec kts;
  122. struct timezone ktz;
  123. if (tv) {
  124. if (compat_get_timeval_convert(&kts, tv))
  125. return -EFAULT;
  126. }
  127. if (tz) {
  128. if (copy_from_user(&ktz, tz, sizeof(ktz)))
  129. return -EFAULT;
  130. }
  131. return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
  132. }
  133. int get_compat_timeval(struct timeval *tv, const struct compat_timeval __user *ctv)
  134. {
  135. return (!access_ok(VERIFY_READ, ctv, sizeof(*ctv)) ||
  136. __get_user(tv->tv_sec, &ctv->tv_sec) ||
  137. __get_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0;
  138. }
  139. EXPORT_SYMBOL_GPL(get_compat_timeval);
  140. int put_compat_timeval(const struct timeval *tv, struct compat_timeval __user *ctv)
  141. {
  142. return (!access_ok(VERIFY_WRITE, ctv, sizeof(*ctv)) ||
  143. __put_user(tv->tv_sec, &ctv->tv_sec) ||
  144. __put_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0;
  145. }
  146. EXPORT_SYMBOL_GPL(put_compat_timeval);
  147. int get_compat_timespec(struct timespec *ts, const struct compat_timespec __user *cts)
  148. {
  149. return (!access_ok(VERIFY_READ, cts, sizeof(*cts)) ||
  150. __get_user(ts->tv_sec, &cts->tv_sec) ||
  151. __get_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
  152. }
  153. EXPORT_SYMBOL_GPL(get_compat_timespec);
  154. int put_compat_timespec(const struct timespec *ts, struct compat_timespec __user *cts)
  155. {
  156. return (!access_ok(VERIFY_WRITE, cts, sizeof(*cts)) ||
  157. __put_user(ts->tv_sec, &cts->tv_sec) ||
  158. __put_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
  159. }
  160. EXPORT_SYMBOL_GPL(put_compat_timespec);
  161. int compat_get_timeval(struct timeval *tv, const void __user *utv)
  162. {
  163. if (COMPAT_USE_64BIT_TIME)
  164. return copy_from_user(tv, utv, sizeof *tv) ? -EFAULT : 0;
  165. else
  166. return get_compat_timeval(tv, utv);
  167. }
  168. EXPORT_SYMBOL_GPL(compat_get_timeval);
  169. int compat_put_timeval(const struct timeval *tv, void __user *utv)
  170. {
  171. if (COMPAT_USE_64BIT_TIME)
  172. return copy_to_user(utv, tv, sizeof *tv) ? -EFAULT : 0;
  173. else
  174. return put_compat_timeval(tv, utv);
  175. }
  176. EXPORT_SYMBOL_GPL(compat_put_timeval);
  177. int compat_get_timespec(struct timespec *ts, const void __user *uts)
  178. {
  179. if (COMPAT_USE_64BIT_TIME)
  180. return copy_from_user(ts, uts, sizeof *ts) ? -EFAULT : 0;
  181. else
  182. return get_compat_timespec(ts, uts);
  183. }
  184. EXPORT_SYMBOL_GPL(compat_get_timespec);
  185. int compat_put_timespec(const struct timespec *ts, void __user *uts)
  186. {
  187. if (COMPAT_USE_64BIT_TIME)
  188. return copy_to_user(uts, ts, sizeof *ts) ? -EFAULT : 0;
  189. else
  190. return put_compat_timespec(ts, uts);
  191. }
  192. EXPORT_SYMBOL_GPL(compat_put_timespec);
  193. static long compat_nanosleep_restart(struct restart_block *restart)
  194. {
  195. struct compat_timespec __user *rmtp;
  196. struct timespec rmt;
  197. mm_segment_t oldfs;
  198. long ret;
  199. restart->nanosleep.rmtp = (struct timespec __user *) &rmt;
  200. oldfs = get_fs();
  201. set_fs(KERNEL_DS);
  202. ret = hrtimer_nanosleep_restart(restart);
  203. set_fs(oldfs);
  204. if (ret) {
  205. rmtp = restart->nanosleep.compat_rmtp;
  206. if (rmtp && put_compat_timespec(&rmt, rmtp))
  207. return -EFAULT;
  208. }
  209. return ret;
  210. }
  211. asmlinkage long compat_sys_nanosleep(struct compat_timespec __user *rqtp,
  212. struct compat_timespec __user *rmtp)
  213. {
  214. struct timespec tu, rmt;
  215. mm_segment_t oldfs;
  216. long ret;
  217. if (get_compat_timespec(&tu, rqtp))
  218. return -EFAULT;
  219. if (!timespec_valid(&tu))
  220. return -EINVAL;
  221. oldfs = get_fs();
  222. set_fs(KERNEL_DS);
  223. ret = hrtimer_nanosleep(&tu,
  224. rmtp ? (struct timespec __user *)&rmt : NULL,
  225. HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  226. set_fs(oldfs);
  227. if (ret) {
  228. struct restart_block *restart
  229. = &current_thread_info()->restart_block;
  230. restart->fn = compat_nanosleep_restart;
  231. restart->nanosleep.compat_rmtp = rmtp;
  232. if (rmtp && put_compat_timespec(&rmt, rmtp))
  233. return -EFAULT;
  234. }
  235. return ret;
  236. }
  237. static inline long get_compat_itimerval(struct itimerval *o,
  238. struct compat_itimerval __user *i)
  239. {
  240. return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
  241. (__get_user(o->it_interval.tv_sec, &i->it_interval.tv_sec) |
  242. __get_user(o->it_interval.tv_usec, &i->it_interval.tv_usec) |
  243. __get_user(o->it_value.tv_sec, &i->it_value.tv_sec) |
  244. __get_user(o->it_value.tv_usec, &i->it_value.tv_usec)));
  245. }
  246. static inline long put_compat_itimerval(struct compat_itimerval __user *o,
  247. struct itimerval *i)
  248. {
  249. return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
  250. (__put_user(i->it_interval.tv_sec, &o->it_interval.tv_sec) |
  251. __put_user(i->it_interval.tv_usec, &o->it_interval.tv_usec) |
  252. __put_user(i->it_value.tv_sec, &o->it_value.tv_sec) |
  253. __put_user(i->it_value.tv_usec, &o->it_value.tv_usec)));
  254. }
  255. asmlinkage long compat_sys_getitimer(int which,
  256. struct compat_itimerval __user *it)
  257. {
  258. struct itimerval kit;
  259. int error;
  260. error = do_getitimer(which, &kit);
  261. if (!error && put_compat_itimerval(it, &kit))
  262. error = -EFAULT;
  263. return error;
  264. }
  265. asmlinkage long compat_sys_setitimer(int which,
  266. struct compat_itimerval __user *in,
  267. struct compat_itimerval __user *out)
  268. {
  269. struct itimerval kin, kout;
  270. int error;
  271. if (in) {
  272. if (get_compat_itimerval(&kin, in))
  273. return -EFAULT;
  274. } else
  275. memset(&kin, 0, sizeof(kin));
  276. error = do_setitimer(which, &kin, out ? &kout : NULL);
  277. if (error || !out)
  278. return error;
  279. if (put_compat_itimerval(out, &kout))
  280. return -EFAULT;
  281. return 0;
  282. }
  283. static compat_clock_t clock_t_to_compat_clock_t(clock_t x)
  284. {
  285. return compat_jiffies_to_clock_t(clock_t_to_jiffies(x));
  286. }
  287. asmlinkage long compat_sys_times(struct compat_tms __user *tbuf)
  288. {
  289. if (tbuf) {
  290. struct tms tms;
  291. struct compat_tms tmp;
  292. do_sys_times(&tms);
  293. /* Convert our struct tms to the compat version. */
  294. tmp.tms_utime = clock_t_to_compat_clock_t(tms.tms_utime);
  295. tmp.tms_stime = clock_t_to_compat_clock_t(tms.tms_stime);
  296. tmp.tms_cutime = clock_t_to_compat_clock_t(tms.tms_cutime);
  297. tmp.tms_cstime = clock_t_to_compat_clock_t(tms.tms_cstime);
  298. if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
  299. return -EFAULT;
  300. }
  301. force_successful_syscall_return();
  302. return compat_jiffies_to_clock_t(jiffies);
  303. }
  304. #ifdef __ARCH_WANT_SYS_SIGPENDING
  305. /*
  306. * Assumption: old_sigset_t and compat_old_sigset_t are both
  307. * types that can be passed to put_user()/get_user().
  308. */
  309. asmlinkage long compat_sys_sigpending(compat_old_sigset_t __user *set)
  310. {
  311. old_sigset_t s;
  312. long ret;
  313. mm_segment_t old_fs = get_fs();
  314. set_fs(KERNEL_DS);
  315. ret = sys_sigpending((old_sigset_t __user *) &s);
  316. set_fs(old_fs);
  317. if (ret == 0)
  318. ret = put_user(s, set);
  319. return ret;
  320. }
  321. #endif
  322. #ifdef __ARCH_WANT_SYS_SIGPROCMASK
  323. /*
  324. * sys_sigprocmask SIG_SETMASK sets the first (compat) word of the
  325. * blocked set of signals to the supplied signal set
  326. */
  327. static inline void compat_sig_setmask(sigset_t *blocked, compat_sigset_word set)
  328. {
  329. memcpy(blocked->sig, &set, sizeof(set));
  330. }
  331. asmlinkage long compat_sys_sigprocmask(int how,
  332. compat_old_sigset_t __user *nset,
  333. compat_old_sigset_t __user *oset)
  334. {
  335. old_sigset_t old_set, new_set;
  336. sigset_t new_blocked;
  337. old_set = current->blocked.sig[0];
  338. if (nset) {
  339. if (get_user(new_set, nset))
  340. return -EFAULT;
  341. new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
  342. new_blocked = current->blocked;
  343. switch (how) {
  344. case SIG_BLOCK:
  345. sigaddsetmask(&new_blocked, new_set);
  346. break;
  347. case SIG_UNBLOCK:
  348. sigdelsetmask(&new_blocked, new_set);
  349. break;
  350. case SIG_SETMASK:
  351. compat_sig_setmask(&new_blocked, new_set);
  352. break;
  353. default:
  354. return -EINVAL;
  355. }
  356. set_current_blocked(&new_blocked);
  357. }
  358. if (oset) {
  359. if (put_user(old_set, oset))
  360. return -EFAULT;
  361. }
  362. return 0;
  363. }
  364. #endif
  365. asmlinkage long compat_sys_setrlimit(unsigned int resource,
  366. struct compat_rlimit __user *rlim)
  367. {
  368. struct rlimit r;
  369. if (!access_ok(VERIFY_READ, rlim, sizeof(*rlim)) ||
  370. __get_user(r.rlim_cur, &rlim->rlim_cur) ||
  371. __get_user(r.rlim_max, &rlim->rlim_max))
  372. return -EFAULT;
  373. if (r.rlim_cur == COMPAT_RLIM_INFINITY)
  374. r.rlim_cur = RLIM_INFINITY;
  375. if (r.rlim_max == COMPAT_RLIM_INFINITY)
  376. r.rlim_max = RLIM_INFINITY;
  377. return do_prlimit(current, resource, &r, NULL);
  378. }
  379. #ifdef COMPAT_RLIM_OLD_INFINITY
  380. asmlinkage long compat_sys_old_getrlimit(unsigned int resource,
  381. struct compat_rlimit __user *rlim)
  382. {
  383. struct rlimit r;
  384. int ret;
  385. mm_segment_t old_fs = get_fs();
  386. set_fs(KERNEL_DS);
  387. ret = sys_old_getrlimit(resource, &r);
  388. set_fs(old_fs);
  389. if (!ret) {
  390. if (r.rlim_cur > COMPAT_RLIM_OLD_INFINITY)
  391. r.rlim_cur = COMPAT_RLIM_INFINITY;
  392. if (r.rlim_max > COMPAT_RLIM_OLD_INFINITY)
  393. r.rlim_max = COMPAT_RLIM_INFINITY;
  394. if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
  395. __put_user(r.rlim_cur, &rlim->rlim_cur) ||
  396. __put_user(r.rlim_max, &rlim->rlim_max))
  397. return -EFAULT;
  398. }
  399. return ret;
  400. }
  401. #endif
  402. asmlinkage long compat_sys_getrlimit(unsigned int resource,
  403. struct compat_rlimit __user *rlim)
  404. {
  405. struct rlimit r;
  406. int ret;
  407. ret = do_prlimit(current, resource, NULL, &r);
  408. if (!ret) {
  409. if (r.rlim_cur > COMPAT_RLIM_INFINITY)
  410. r.rlim_cur = COMPAT_RLIM_INFINITY;
  411. if (r.rlim_max > COMPAT_RLIM_INFINITY)
  412. r.rlim_max = COMPAT_RLIM_INFINITY;
  413. if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
  414. __put_user(r.rlim_cur, &rlim->rlim_cur) ||
  415. __put_user(r.rlim_max, &rlim->rlim_max))
  416. return -EFAULT;
  417. }
  418. return ret;
  419. }
  420. int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru)
  421. {
  422. if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)) ||
  423. __put_user(r->ru_utime.tv_sec, &ru->ru_utime.tv_sec) ||
  424. __put_user(r->ru_utime.tv_usec, &ru->ru_utime.tv_usec) ||
  425. __put_user(r->ru_stime.tv_sec, &ru->ru_stime.tv_sec) ||
  426. __put_user(r->ru_stime.tv_usec, &ru->ru_stime.tv_usec) ||
  427. __put_user(r->ru_maxrss, &ru->ru_maxrss) ||
  428. __put_user(r->ru_ixrss, &ru->ru_ixrss) ||
  429. __put_user(r->ru_idrss, &ru->ru_idrss) ||
  430. __put_user(r->ru_isrss, &ru->ru_isrss) ||
  431. __put_user(r->ru_minflt, &ru->ru_minflt) ||
  432. __put_user(r->ru_majflt, &ru->ru_majflt) ||
  433. __put_user(r->ru_nswap, &ru->ru_nswap) ||
  434. __put_user(r->ru_inblock, &ru->ru_inblock) ||
  435. __put_user(r->ru_oublock, &ru->ru_oublock) ||
  436. __put_user(r->ru_msgsnd, &ru->ru_msgsnd) ||
  437. __put_user(r->ru_msgrcv, &ru->ru_msgrcv) ||
  438. __put_user(r->ru_nsignals, &ru->ru_nsignals) ||
  439. __put_user(r->ru_nvcsw, &ru->ru_nvcsw) ||
  440. __put_user(r->ru_nivcsw, &ru->ru_nivcsw))
  441. return -EFAULT;
  442. return 0;
  443. }
  444. asmlinkage long compat_sys_getrusage(int who, struct compat_rusage __user *ru)
  445. {
  446. struct rusage r;
  447. int ret;
  448. mm_segment_t old_fs = get_fs();
  449. set_fs(KERNEL_DS);
  450. ret = sys_getrusage(who, (struct rusage __user *) &r);
  451. set_fs(old_fs);
  452. if (ret)
  453. return ret;
  454. if (put_compat_rusage(&r, ru))
  455. return -EFAULT;
  456. return 0;
  457. }
  458. asmlinkage long
  459. compat_sys_wait4(compat_pid_t pid, compat_uint_t __user *stat_addr, int options,
  460. struct compat_rusage __user *ru)
  461. {
  462. if (!ru) {
  463. return sys_wait4(pid, stat_addr, options, NULL);
  464. } else {
  465. struct rusage r;
  466. int ret;
  467. unsigned int status;
  468. mm_segment_t old_fs = get_fs();
  469. set_fs (KERNEL_DS);
  470. ret = sys_wait4(pid,
  471. (stat_addr ?
  472. (unsigned int __user *) &status : NULL),
  473. options, (struct rusage __user *) &r);
  474. set_fs (old_fs);
  475. if (ret > 0) {
  476. if (put_compat_rusage(&r, ru))
  477. return -EFAULT;
  478. if (stat_addr && put_user(status, stat_addr))
  479. return -EFAULT;
  480. }
  481. return ret;
  482. }
  483. }
  484. asmlinkage long compat_sys_waitid(int which, compat_pid_t pid,
  485. struct compat_siginfo __user *uinfo, int options,
  486. struct compat_rusage __user *uru)
  487. {
  488. siginfo_t info;
  489. struct rusage ru;
  490. long ret;
  491. mm_segment_t old_fs = get_fs();
  492. memset(&info, 0, sizeof(info));
  493. set_fs(KERNEL_DS);
  494. ret = sys_waitid(which, pid, (siginfo_t __user *)&info, options,
  495. uru ? (struct rusage __user *)&ru : NULL);
  496. set_fs(old_fs);
  497. if ((ret < 0) || (info.si_signo == 0))
  498. return ret;
  499. if (uru) {
  500. ret = put_compat_rusage(&ru, uru);
  501. if (ret)
  502. return ret;
  503. }
  504. BUG_ON(info.si_code & __SI_MASK);
  505. info.si_code |= __SI_CHLD;
  506. return copy_siginfo_to_user32(uinfo, &info);
  507. }
  508. static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr,
  509. unsigned len, struct cpumask *new_mask)
  510. {
  511. unsigned long *k;
  512. if (len < cpumask_size())
  513. memset(new_mask, 0, cpumask_size());
  514. else if (len > cpumask_size())
  515. len = cpumask_size();
  516. k = cpumask_bits(new_mask);
  517. return compat_get_bitmap(k, user_mask_ptr, len * 8);
  518. }
  519. asmlinkage long compat_sys_sched_setaffinity(compat_pid_t pid,
  520. unsigned int len,
  521. compat_ulong_t __user *user_mask_ptr)
  522. {
  523. cpumask_var_t new_mask;
  524. int retval;
  525. if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
  526. return -ENOMEM;
  527. retval = compat_get_user_cpu_mask(user_mask_ptr, len, new_mask);
  528. if (retval)
  529. goto out;
  530. retval = sched_setaffinity(pid, new_mask);
  531. out:
  532. free_cpumask_var(new_mask);
  533. return retval;
  534. }
  535. asmlinkage long compat_sys_sched_getaffinity(compat_pid_t pid, unsigned int len,
  536. compat_ulong_t __user *user_mask_ptr)
  537. {
  538. int ret;
  539. cpumask_var_t mask;
  540. if ((len * BITS_PER_BYTE) < nr_cpu_ids)
  541. return -EINVAL;
  542. if (len & (sizeof(compat_ulong_t)-1))
  543. return -EINVAL;
  544. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  545. return -ENOMEM;
  546. ret = sched_getaffinity(pid, mask);
  547. if (ret == 0) {
  548. size_t retlen = min_t(size_t, len, cpumask_size());
  549. if (compat_put_bitmap(user_mask_ptr, cpumask_bits(mask), retlen * 8))
  550. ret = -EFAULT;
  551. else
  552. ret = retlen;
  553. }
  554. free_cpumask_var(mask);
  555. return ret;
  556. }
  557. int get_compat_itimerspec(struct itimerspec *dst,
  558. const struct compat_itimerspec __user *src)
  559. {
  560. if (get_compat_timespec(&dst->it_interval, &src->it_interval) ||
  561. get_compat_timespec(&dst->it_value, &src->it_value))
  562. return -EFAULT;
  563. return 0;
  564. }
  565. int put_compat_itimerspec(struct compat_itimerspec __user *dst,
  566. const struct itimerspec *src)
  567. {
  568. if (put_compat_timespec(&src->it_interval, &dst->it_interval) ||
  569. put_compat_timespec(&src->it_value, &dst->it_value))
  570. return -EFAULT;
  571. return 0;
  572. }
  573. long compat_sys_timer_create(clockid_t which_clock,
  574. struct compat_sigevent __user *timer_event_spec,
  575. timer_t __user *created_timer_id)
  576. {
  577. struct sigevent __user *event = NULL;
  578. if (timer_event_spec) {
  579. struct sigevent kevent;
  580. event = compat_alloc_user_space(sizeof(*event));
  581. if (get_compat_sigevent(&kevent, timer_event_spec) ||
  582. copy_to_user(event, &kevent, sizeof(*event)))
  583. return -EFAULT;
  584. }
  585. return sys_timer_create(which_clock, event, created_timer_id);
  586. }
  587. long compat_sys_timer_settime(timer_t timer_id, int flags,
  588. struct compat_itimerspec __user *new,
  589. struct compat_itimerspec __user *old)
  590. {
  591. long err;
  592. mm_segment_t oldfs;
  593. struct itimerspec newts, oldts;
  594. if (!new)
  595. return -EINVAL;
  596. if (get_compat_itimerspec(&newts, new))
  597. return -EFAULT;
  598. oldfs = get_fs();
  599. set_fs(KERNEL_DS);
  600. err = sys_timer_settime(timer_id, flags,
  601. (struct itimerspec __user *) &newts,
  602. (struct itimerspec __user *) &oldts);
  603. set_fs(oldfs);
  604. if (!err && old && put_compat_itimerspec(old, &oldts))
  605. return -EFAULT;
  606. return err;
  607. }
  608. long compat_sys_timer_gettime(timer_t timer_id,
  609. struct compat_itimerspec __user *setting)
  610. {
  611. long err;
  612. mm_segment_t oldfs;
  613. struct itimerspec ts;
  614. oldfs = get_fs();
  615. set_fs(KERNEL_DS);
  616. err = sys_timer_gettime(timer_id,
  617. (struct itimerspec __user *) &ts);
  618. set_fs(oldfs);
  619. if (!err && put_compat_itimerspec(setting, &ts))
  620. return -EFAULT;
  621. return err;
  622. }
  623. long compat_sys_clock_settime(clockid_t which_clock,
  624. struct compat_timespec __user *tp)
  625. {
  626. long err;
  627. mm_segment_t oldfs;
  628. struct timespec ts;
  629. if (get_compat_timespec(&ts, tp))
  630. return -EFAULT;
  631. oldfs = get_fs();
  632. set_fs(KERNEL_DS);
  633. err = sys_clock_settime(which_clock,
  634. (struct timespec __user *) &ts);
  635. set_fs(oldfs);
  636. return err;
  637. }
  638. long compat_sys_clock_gettime(clockid_t which_clock,
  639. struct compat_timespec __user *tp)
  640. {
  641. long err;
  642. mm_segment_t oldfs;
  643. struct timespec ts;
  644. oldfs = get_fs();
  645. set_fs(KERNEL_DS);
  646. err = sys_clock_gettime(which_clock,
  647. (struct timespec __user *) &ts);
  648. set_fs(oldfs);
  649. if (!err && put_compat_timespec(&ts, tp))
  650. return -EFAULT;
  651. return err;
  652. }
  653. long compat_sys_clock_adjtime(clockid_t which_clock,
  654. struct compat_timex __user *utp)
  655. {
  656. struct timex txc;
  657. mm_segment_t oldfs;
  658. int err, ret;
  659. err = compat_get_timex(&txc, utp);
  660. if (err)
  661. return err;
  662. oldfs = get_fs();
  663. set_fs(KERNEL_DS);
  664. ret = sys_clock_adjtime(which_clock, (struct timex __user *) &txc);
  665. set_fs(oldfs);
  666. err = compat_put_timex(utp, &txc);
  667. if (err)
  668. return err;
  669. return ret;
  670. }
  671. long compat_sys_clock_getres(clockid_t which_clock,
  672. struct compat_timespec __user *tp)
  673. {
  674. long err;
  675. mm_segment_t oldfs;
  676. struct timespec ts;
  677. oldfs = get_fs();
  678. set_fs(KERNEL_DS);
  679. err = sys_clock_getres(which_clock,
  680. (struct timespec __user *) &ts);
  681. set_fs(oldfs);
  682. if (!err && tp && put_compat_timespec(&ts, tp))
  683. return -EFAULT;
  684. return err;
  685. }
  686. static long compat_clock_nanosleep_restart(struct restart_block *restart)
  687. {
  688. long err;
  689. mm_segment_t oldfs;
  690. struct timespec tu;
  691. struct compat_timespec *rmtp = restart->nanosleep.compat_rmtp;
  692. restart->nanosleep.rmtp = (struct timespec __user *) &tu;
  693. oldfs = get_fs();
  694. set_fs(KERNEL_DS);
  695. err = clock_nanosleep_restart(restart);
  696. set_fs(oldfs);
  697. if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
  698. put_compat_timespec(&tu, rmtp))
  699. return -EFAULT;
  700. if (err == -ERESTART_RESTARTBLOCK) {
  701. restart->fn = compat_clock_nanosleep_restart;
  702. restart->nanosleep.compat_rmtp = rmtp;
  703. }
  704. return err;
  705. }
  706. long compat_sys_clock_nanosleep(clockid_t which_clock, int flags,
  707. struct compat_timespec __user *rqtp,
  708. struct compat_timespec __user *rmtp)
  709. {
  710. long err;
  711. mm_segment_t oldfs;
  712. struct timespec in, out;
  713. struct restart_block *restart;
  714. if (get_compat_timespec(&in, rqtp))
  715. return -EFAULT;
  716. oldfs = get_fs();
  717. set_fs(KERNEL_DS);
  718. err = sys_clock_nanosleep(which_clock, flags,
  719. (struct timespec __user *) &in,
  720. (struct timespec __user *) &out);
  721. set_fs(oldfs);
  722. if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
  723. put_compat_timespec(&out, rmtp))
  724. return -EFAULT;
  725. if (err == -ERESTART_RESTARTBLOCK) {
  726. restart = &current_thread_info()->restart_block;
  727. restart->fn = compat_clock_nanosleep_restart;
  728. restart->nanosleep.compat_rmtp = rmtp;
  729. }
  730. return err;
  731. }
  732. /*
  733. * We currently only need the following fields from the sigevent
  734. * structure: sigev_value, sigev_signo, sig_notify and (sometimes
  735. * sigev_notify_thread_id). The others are handled in user mode.
  736. * We also assume that copying sigev_value.sival_int is sufficient
  737. * to keep all the bits of sigev_value.sival_ptr intact.
  738. */
  739. int get_compat_sigevent(struct sigevent *event,
  740. const struct compat_sigevent __user *u_event)
  741. {
  742. memset(event, 0, sizeof(*event));
  743. return (!access_ok(VERIFY_READ, u_event, sizeof(*u_event)) ||
  744. __get_user(event->sigev_value.sival_int,
  745. &u_event->sigev_value.sival_int) ||
  746. __get_user(event->sigev_signo, &u_event->sigev_signo) ||
  747. __get_user(event->sigev_notify, &u_event->sigev_notify) ||
  748. __get_user(event->sigev_notify_thread_id,
  749. &u_event->sigev_notify_thread_id))
  750. ? -EFAULT : 0;
  751. }
  752. long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask,
  753. unsigned long bitmap_size)
  754. {
  755. int i, j;
  756. unsigned long m;
  757. compat_ulong_t um;
  758. unsigned long nr_compat_longs;
  759. /* align bitmap up to nearest compat_long_t boundary */
  760. bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
  761. if (!access_ok(VERIFY_READ, umask, bitmap_size / 8))
  762. return -EFAULT;
  763. nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
  764. for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
  765. m = 0;
  766. for (j = 0; j < sizeof(m)/sizeof(um); j++) {
  767. /*
  768. * We dont want to read past the end of the userspace
  769. * bitmap. We must however ensure the end of the
  770. * kernel bitmap is zeroed.
  771. */
  772. if (nr_compat_longs-- > 0) {
  773. if (__get_user(um, umask))
  774. return -EFAULT;
  775. } else {
  776. um = 0;
  777. }
  778. umask++;
  779. m |= (long)um << (j * BITS_PER_COMPAT_LONG);
  780. }
  781. *mask++ = m;
  782. }
  783. return 0;
  784. }
  785. long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
  786. unsigned long bitmap_size)
  787. {
  788. int i, j;
  789. unsigned long m;
  790. compat_ulong_t um;
  791. unsigned long nr_compat_longs;
  792. /* align bitmap up to nearest compat_long_t boundary */
  793. bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
  794. if (!access_ok(VERIFY_WRITE, umask, bitmap_size / 8))
  795. return -EFAULT;
  796. nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
  797. for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
  798. m = *mask++;
  799. for (j = 0; j < sizeof(m)/sizeof(um); j++) {
  800. um = m;
  801. /*
  802. * We dont want to write past the end of the userspace
  803. * bitmap.
  804. */
  805. if (nr_compat_longs-- > 0) {
  806. if (__put_user(um, umask))
  807. return -EFAULT;
  808. }
  809. umask++;
  810. m >>= 4*sizeof(um);
  811. m >>= 4*sizeof(um);
  812. }
  813. }
  814. return 0;
  815. }
  816. void
  817. sigset_from_compat (sigset_t *set, compat_sigset_t *compat)
  818. {
  819. switch (_NSIG_WORDS) {
  820. case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32 );
  821. case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32 );
  822. case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32 );
  823. case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32 );
  824. }
  825. }
  826. EXPORT_SYMBOL_GPL(sigset_from_compat);
  827. asmlinkage long
  828. compat_sys_rt_sigtimedwait (compat_sigset_t __user *uthese,
  829. struct compat_siginfo __user *uinfo,
  830. struct compat_timespec __user *uts, compat_size_t sigsetsize)
  831. {
  832. compat_sigset_t s32;
  833. sigset_t s;
  834. struct timespec t;
  835. siginfo_t info;
  836. long ret;
  837. if (sigsetsize != sizeof(sigset_t))
  838. return -EINVAL;
  839. if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t)))
  840. return -EFAULT;
  841. sigset_from_compat(&s, &s32);
  842. if (uts) {
  843. if (get_compat_timespec(&t, uts))
  844. return -EFAULT;
  845. }
  846. ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);
  847. if (ret > 0 && uinfo) {
  848. if (copy_siginfo_to_user32(uinfo, &info))
  849. ret = -EFAULT;
  850. }
  851. return ret;
  852. }
  853. asmlinkage long
  854. compat_sys_rt_tgsigqueueinfo(compat_pid_t tgid, compat_pid_t pid, int sig,
  855. struct compat_siginfo __user *uinfo)
  856. {
  857. siginfo_t info;
  858. if (copy_siginfo_from_user32(&info, uinfo))
  859. return -EFAULT;
  860. return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
  861. }
  862. #ifdef __ARCH_WANT_COMPAT_SYS_TIME
  863. /* compat_time_t is a 32 bit "long" and needs to get converted. */
  864. asmlinkage long compat_sys_time(compat_time_t __user * tloc)
  865. {
  866. compat_time_t i;
  867. struct timeval tv;
  868. do_gettimeofday(&tv);
  869. i = tv.tv_sec;
  870. if (tloc) {
  871. if (put_user(i,tloc))
  872. return -EFAULT;
  873. }
  874. force_successful_syscall_return();
  875. return i;
  876. }
  877. asmlinkage long compat_sys_stime(compat_time_t __user *tptr)
  878. {
  879. struct timespec tv;
  880. int err;
  881. if (get_user(tv.tv_sec, tptr))
  882. return -EFAULT;
  883. tv.tv_nsec = 0;
  884. err = security_settime(&tv, NULL);
  885. if (err)
  886. return err;
  887. do_settimeofday(&tv);
  888. return 0;
  889. }
  890. #endif /* __ARCH_WANT_COMPAT_SYS_TIME */
  891. #ifdef __ARCH_WANT_COMPAT_SYS_RT_SIGSUSPEND
  892. asmlinkage long compat_sys_rt_sigsuspend(compat_sigset_t __user *unewset, compat_size_t sigsetsize)
  893. {
  894. sigset_t newset;
  895. compat_sigset_t newset32;
  896. /* XXX: Don't preclude handling different sized sigset_t's. */
  897. if (sigsetsize != sizeof(sigset_t))
  898. return -EINVAL;
  899. if (copy_from_user(&newset32, unewset, sizeof(compat_sigset_t)))
  900. return -EFAULT;
  901. sigset_from_compat(&newset, &newset32);
  902. sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  903. current->saved_sigmask = current->blocked;
  904. set_current_blocked(&newset);
  905. current->state = TASK_INTERRUPTIBLE;
  906. schedule();
  907. set_restore_sigmask();
  908. return -ERESTARTNOHAND;
  909. }
  910. #endif /* __ARCH_WANT_COMPAT_SYS_RT_SIGSUSPEND */
  911. asmlinkage long compat_sys_adjtimex(struct compat_timex __user *utp)
  912. {
  913. struct timex txc;
  914. int err, ret;
  915. err = compat_get_timex(&txc, utp);
  916. if (err)
  917. return err;
  918. ret = do_adjtimex(&txc);
  919. err = compat_put_timex(utp, &txc);
  920. if (err)
  921. return err;
  922. return ret;
  923. }
  924. #ifdef CONFIG_NUMA
  925. asmlinkage long compat_sys_move_pages(pid_t pid, unsigned long nr_pages,
  926. compat_uptr_t __user *pages32,
  927. const int __user *nodes,
  928. int __user *status,
  929. int flags)
  930. {
  931. const void __user * __user *pages;
  932. int i;
  933. pages = compat_alloc_user_space(nr_pages * sizeof(void *));
  934. for (i = 0; i < nr_pages; i++) {
  935. compat_uptr_t p;
  936. if (get_user(p, pages32 + i) ||
  937. put_user(compat_ptr(p), pages + i))
  938. return -EFAULT;
  939. }
  940. return sys_move_pages(pid, nr_pages, pages, nodes, status, flags);
  941. }
  942. asmlinkage long compat_sys_migrate_pages(compat_pid_t pid,
  943. compat_ulong_t maxnode,
  944. const compat_ulong_t __user *old_nodes,
  945. const compat_ulong_t __user *new_nodes)
  946. {
  947. unsigned long __user *old = NULL;
  948. unsigned long __user *new = NULL;
  949. nodemask_t tmp_mask;
  950. unsigned long nr_bits;
  951. unsigned long size;
  952. nr_bits = min_t(unsigned long, maxnode - 1, MAX_NUMNODES);
  953. size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
  954. if (old_nodes) {
  955. if (compat_get_bitmap(nodes_addr(tmp_mask), old_nodes, nr_bits))
  956. return -EFAULT;
  957. old = compat_alloc_user_space(new_nodes ? size * 2 : size);
  958. if (new_nodes)
  959. new = old + size / sizeof(unsigned long);
  960. if (copy_to_user(old, nodes_addr(tmp_mask), size))
  961. return -EFAULT;
  962. }
  963. if (new_nodes) {
  964. if (compat_get_bitmap(nodes_addr(tmp_mask), new_nodes, nr_bits))
  965. return -EFAULT;
  966. if (new == NULL)
  967. new = compat_alloc_user_space(size);
  968. if (copy_to_user(new, nodes_addr(tmp_mask), size))
  969. return -EFAULT;
  970. }
  971. return sys_migrate_pages(pid, nr_bits + 1, old, new);
  972. }
  973. #endif
  974. struct compat_sysinfo {
  975. s32 uptime;
  976. u32 loads[3];
  977. u32 totalram;
  978. u32 freeram;
  979. u32 sharedram;
  980. u32 bufferram;
  981. u32 totalswap;
  982. u32 freeswap;
  983. u16 procs;
  984. u16 pad;
  985. u32 totalhigh;
  986. u32 freehigh;
  987. u32 mem_unit;
  988. char _f[20-2*sizeof(u32)-sizeof(int)];
  989. };
  990. asmlinkage long
  991. compat_sys_sysinfo(struct compat_sysinfo __user *info)
  992. {
  993. struct sysinfo s;
  994. do_sysinfo(&s);
  995. /* Check to see if any memory value is too large for 32-bit and scale
  996. * down if needed
  997. */
  998. if ((s.totalram >> 32) || (s.totalswap >> 32)) {
  999. int bitcount = 0;
  1000. while (s.mem_unit < PAGE_SIZE) {
  1001. s.mem_unit <<= 1;
  1002. bitcount++;
  1003. }
  1004. s.totalram >>= bitcount;
  1005. s.freeram >>= bitcount;
  1006. s.sharedram >>= bitcount;
  1007. s.bufferram >>= bitcount;
  1008. s.totalswap >>= bitcount;
  1009. s.freeswap >>= bitcount;
  1010. s.totalhigh >>= bitcount;
  1011. s.freehigh >>= bitcount;
  1012. }
  1013. if (!access_ok(VERIFY_WRITE, info, sizeof(struct compat_sysinfo)) ||
  1014. __put_user (s.uptime, &info->uptime) ||
  1015. __put_user (s.loads[0], &info->loads[0]) ||
  1016. __put_user (s.loads[1], &info->loads[1]) ||
  1017. __put_user (s.loads[2], &info->loads[2]) ||
  1018. __put_user (s.totalram, &info->totalram) ||
  1019. __put_user (s.freeram, &info->freeram) ||
  1020. __put_user (s.sharedram, &info->sharedram) ||
  1021. __put_user (s.bufferram, &info->bufferram) ||
  1022. __put_user (s.totalswap, &info->totalswap) ||
  1023. __put_user (s.freeswap, &info->freeswap) ||
  1024. __put_user (s.procs, &info->procs) ||
  1025. __put_user (s.totalhigh, &info->totalhigh) ||
  1026. __put_user (s.freehigh, &info->freehigh) ||
  1027. __put_user (s.mem_unit, &info->mem_unit))
  1028. return -EFAULT;
  1029. return 0;
  1030. }
  1031. /*
  1032. * Allocate user-space memory for the duration of a single system call,
  1033. * in order to marshall parameters inside a compat thunk.
  1034. */
  1035. void __user *compat_alloc_user_space(unsigned long len)
  1036. {
  1037. void __user *ptr;
  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. ptr = arch_compat_alloc_user_space(len);
  1042. if (unlikely(!access_ok(VERIFY_WRITE, ptr, len)))
  1043. return NULL;
  1044. return ptr;
  1045. }
  1046. EXPORT_SYMBOL_GPL(compat_alloc_user_space);