compat.c 29 KB

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