compat.c 29 KB

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