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