compat.c 24 KB

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