compat.c 28 KB

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