compat.c 28 KB

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