compat.c 24 KB

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