compat.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  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) {
  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. if (ret) {
  227. struct restart_block *restart
  228. = &current_thread_info()->restart_block;
  229. restart->fn = compat_nanosleep_restart;
  230. restart->nanosleep.compat_rmtp = rmtp;
  231. if (rmtp && compat_put_timespec(&rmt, rmtp))
  232. return -EFAULT;
  233. }
  234. return ret;
  235. }
  236. static inline long get_compat_itimerval(struct itimerval *o,
  237. struct compat_itimerval __user *i)
  238. {
  239. return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
  240. (__get_user(o->it_interval.tv_sec, &i->it_interval.tv_sec) |
  241. __get_user(o->it_interval.tv_usec, &i->it_interval.tv_usec) |
  242. __get_user(o->it_value.tv_sec, &i->it_value.tv_sec) |
  243. __get_user(o->it_value.tv_usec, &i->it_value.tv_usec)));
  244. }
  245. static inline long put_compat_itimerval(struct compat_itimerval __user *o,
  246. struct itimerval *i)
  247. {
  248. return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
  249. (__put_user(i->it_interval.tv_sec, &o->it_interval.tv_sec) |
  250. __put_user(i->it_interval.tv_usec, &o->it_interval.tv_usec) |
  251. __put_user(i->it_value.tv_sec, &o->it_value.tv_sec) |
  252. __put_user(i->it_value.tv_usec, &o->it_value.tv_usec)));
  253. }
  254. COMPAT_SYSCALL_DEFINE2(getitimer, int, which,
  255. struct compat_itimerval __user *, it)
  256. {
  257. struct itimerval kit;
  258. int error;
  259. error = do_getitimer(which, &kit);
  260. if (!error && put_compat_itimerval(it, &kit))
  261. error = -EFAULT;
  262. return error;
  263. }
  264. COMPAT_SYSCALL_DEFINE3(setitimer, int, which,
  265. struct compat_itimerval __user *, in,
  266. struct compat_itimerval __user *, out)
  267. {
  268. struct itimerval kin, kout;
  269. int error;
  270. if (in) {
  271. if (get_compat_itimerval(&kin, in))
  272. return -EFAULT;
  273. } else
  274. memset(&kin, 0, sizeof(kin));
  275. error = do_setitimer(which, &kin, out ? &kout : NULL);
  276. if (error || !out)
  277. return error;
  278. if (put_compat_itimerval(out, &kout))
  279. return -EFAULT;
  280. return 0;
  281. }
  282. static compat_clock_t clock_t_to_compat_clock_t(clock_t x)
  283. {
  284. return compat_jiffies_to_clock_t(clock_t_to_jiffies(x));
  285. }
  286. COMPAT_SYSCALL_DEFINE1(times, struct compat_tms __user *, tbuf)
  287. {
  288. if (tbuf) {
  289. struct tms tms;
  290. struct compat_tms tmp;
  291. do_sys_times(&tms);
  292. /* Convert our struct tms to the compat version. */
  293. tmp.tms_utime = clock_t_to_compat_clock_t(tms.tms_utime);
  294. tmp.tms_stime = clock_t_to_compat_clock_t(tms.tms_stime);
  295. tmp.tms_cutime = clock_t_to_compat_clock_t(tms.tms_cutime);
  296. tmp.tms_cstime = clock_t_to_compat_clock_t(tms.tms_cstime);
  297. if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
  298. return -EFAULT;
  299. }
  300. force_successful_syscall_return();
  301. return compat_jiffies_to_clock_t(jiffies);
  302. }
  303. #ifdef __ARCH_WANT_SYS_SIGPENDING
  304. /*
  305. * Assumption: old_sigset_t and compat_old_sigset_t are both
  306. * types that can be passed to put_user()/get_user().
  307. */
  308. COMPAT_SYSCALL_DEFINE1(sigpending, compat_old_sigset_t __user *, set)
  309. {
  310. old_sigset_t s;
  311. long ret;
  312. mm_segment_t old_fs = get_fs();
  313. set_fs(KERNEL_DS);
  314. ret = sys_sigpending((old_sigset_t __user *) &s);
  315. set_fs(old_fs);
  316. if (ret == 0)
  317. ret = put_user(s, set);
  318. return ret;
  319. }
  320. #endif
  321. #ifdef __ARCH_WANT_SYS_SIGPROCMASK
  322. /*
  323. * sys_sigprocmask SIG_SETMASK sets the first (compat) word of the
  324. * blocked set of signals to the supplied signal set
  325. */
  326. static inline void compat_sig_setmask(sigset_t *blocked, compat_sigset_word set)
  327. {
  328. memcpy(blocked->sig, &set, sizeof(set));
  329. }
  330. COMPAT_SYSCALL_DEFINE3(sigprocmask, int, how,
  331. compat_old_sigset_t __user *, nset,
  332. compat_old_sigset_t __user *, oset)
  333. {
  334. old_sigset_t old_set, new_set;
  335. sigset_t new_blocked;
  336. old_set = current->blocked.sig[0];
  337. if (nset) {
  338. if (get_user(new_set, nset))
  339. return -EFAULT;
  340. new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
  341. new_blocked = current->blocked;
  342. switch (how) {
  343. case SIG_BLOCK:
  344. sigaddsetmask(&new_blocked, new_set);
  345. break;
  346. case SIG_UNBLOCK:
  347. sigdelsetmask(&new_blocked, new_set);
  348. break;
  349. case SIG_SETMASK:
  350. compat_sig_setmask(&new_blocked, new_set);
  351. break;
  352. default:
  353. return -EINVAL;
  354. }
  355. set_current_blocked(&new_blocked);
  356. }
  357. if (oset) {
  358. if (put_user(old_set, oset))
  359. return -EFAULT;
  360. }
  361. return 0;
  362. }
  363. #endif
  364. COMPAT_SYSCALL_DEFINE2(setrlimit, unsigned int, resource,
  365. struct compat_rlimit __user *, rlim)
  366. {
  367. struct rlimit r;
  368. if (!access_ok(VERIFY_READ, rlim, sizeof(*rlim)) ||
  369. __get_user(r.rlim_cur, &rlim->rlim_cur) ||
  370. __get_user(r.rlim_max, &rlim->rlim_max))
  371. return -EFAULT;
  372. if (r.rlim_cur == COMPAT_RLIM_INFINITY)
  373. r.rlim_cur = RLIM_INFINITY;
  374. if (r.rlim_max == COMPAT_RLIM_INFINITY)
  375. r.rlim_max = RLIM_INFINITY;
  376. return do_prlimit(current, resource, &r, NULL);
  377. }
  378. #ifdef COMPAT_RLIM_OLD_INFINITY
  379. COMPAT_SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
  380. struct compat_rlimit __user *, rlim)
  381. {
  382. struct rlimit r;
  383. int ret;
  384. mm_segment_t old_fs = get_fs();
  385. set_fs(KERNEL_DS);
  386. ret = sys_old_getrlimit(resource, (struct rlimit __user *)&r);
  387. set_fs(old_fs);
  388. if (!ret) {
  389. if (r.rlim_cur > COMPAT_RLIM_OLD_INFINITY)
  390. r.rlim_cur = COMPAT_RLIM_INFINITY;
  391. if (r.rlim_max > COMPAT_RLIM_OLD_INFINITY)
  392. r.rlim_max = COMPAT_RLIM_INFINITY;
  393. if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
  394. __put_user(r.rlim_cur, &rlim->rlim_cur) ||
  395. __put_user(r.rlim_max, &rlim->rlim_max))
  396. return -EFAULT;
  397. }
  398. return ret;
  399. }
  400. #endif
  401. COMPAT_SYSCALL_DEFINE2(getrlimit, unsigned int, resource,
  402. struct compat_rlimit __user *, rlim)
  403. {
  404. struct rlimit r;
  405. int ret;
  406. ret = do_prlimit(current, resource, NULL, &r);
  407. if (!ret) {
  408. if (r.rlim_cur > COMPAT_RLIM_INFINITY)
  409. r.rlim_cur = COMPAT_RLIM_INFINITY;
  410. if (r.rlim_max > COMPAT_RLIM_INFINITY)
  411. r.rlim_max = COMPAT_RLIM_INFINITY;
  412. if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
  413. __put_user(r.rlim_cur, &rlim->rlim_cur) ||
  414. __put_user(r.rlim_max, &rlim->rlim_max))
  415. return -EFAULT;
  416. }
  417. return ret;
  418. }
  419. int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru)
  420. {
  421. if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)) ||
  422. __put_user(r->ru_utime.tv_sec, &ru->ru_utime.tv_sec) ||
  423. __put_user(r->ru_utime.tv_usec, &ru->ru_utime.tv_usec) ||
  424. __put_user(r->ru_stime.tv_sec, &ru->ru_stime.tv_sec) ||
  425. __put_user(r->ru_stime.tv_usec, &ru->ru_stime.tv_usec) ||
  426. __put_user(r->ru_maxrss, &ru->ru_maxrss) ||
  427. __put_user(r->ru_ixrss, &ru->ru_ixrss) ||
  428. __put_user(r->ru_idrss, &ru->ru_idrss) ||
  429. __put_user(r->ru_isrss, &ru->ru_isrss) ||
  430. __put_user(r->ru_minflt, &ru->ru_minflt) ||
  431. __put_user(r->ru_majflt, &ru->ru_majflt) ||
  432. __put_user(r->ru_nswap, &ru->ru_nswap) ||
  433. __put_user(r->ru_inblock, &ru->ru_inblock) ||
  434. __put_user(r->ru_oublock, &ru->ru_oublock) ||
  435. __put_user(r->ru_msgsnd, &ru->ru_msgsnd) ||
  436. __put_user(r->ru_msgrcv, &ru->ru_msgrcv) ||
  437. __put_user(r->ru_nsignals, &ru->ru_nsignals) ||
  438. __put_user(r->ru_nvcsw, &ru->ru_nvcsw) ||
  439. __put_user(r->ru_nivcsw, &ru->ru_nivcsw))
  440. return -EFAULT;
  441. return 0;
  442. }
  443. COMPAT_SYSCALL_DEFINE4(wait4,
  444. compat_pid_t, pid,
  445. compat_uint_t __user *, stat_addr,
  446. int, options,
  447. struct compat_rusage __user *, ru)
  448. {
  449. if (!ru) {
  450. return sys_wait4(pid, stat_addr, options, NULL);
  451. } else {
  452. struct rusage r;
  453. int ret;
  454. unsigned int status;
  455. mm_segment_t old_fs = get_fs();
  456. set_fs (KERNEL_DS);
  457. ret = sys_wait4(pid,
  458. (stat_addr ?
  459. (unsigned int __user *) &status : NULL),
  460. options, (struct rusage __user *) &r);
  461. set_fs (old_fs);
  462. if (ret > 0) {
  463. if (put_compat_rusage(&r, ru))
  464. return -EFAULT;
  465. if (stat_addr && put_user(status, stat_addr))
  466. return -EFAULT;
  467. }
  468. return ret;
  469. }
  470. }
  471. COMPAT_SYSCALL_DEFINE5(waitid,
  472. int, which, compat_pid_t, pid,
  473. struct compat_siginfo __user *, uinfo, int, options,
  474. struct compat_rusage __user *, uru)
  475. {
  476. siginfo_t info;
  477. struct rusage ru;
  478. long ret;
  479. mm_segment_t old_fs = get_fs();
  480. memset(&info, 0, sizeof(info));
  481. set_fs(KERNEL_DS);
  482. ret = sys_waitid(which, pid, (siginfo_t __user *)&info, options,
  483. uru ? (struct rusage __user *)&ru : NULL);
  484. set_fs(old_fs);
  485. if ((ret < 0) || (info.si_signo == 0))
  486. return ret;
  487. if (uru) {
  488. /* sys_waitid() overwrites everything in ru */
  489. if (COMPAT_USE_64BIT_TIME)
  490. ret = copy_to_user(uru, &ru, sizeof(ru));
  491. else
  492. ret = put_compat_rusage(&ru, uru);
  493. if (ret)
  494. return -EFAULT;
  495. }
  496. BUG_ON(info.si_code & __SI_MASK);
  497. info.si_code |= __SI_CHLD;
  498. return copy_siginfo_to_user32(uinfo, &info);
  499. }
  500. static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr,
  501. unsigned len, struct cpumask *new_mask)
  502. {
  503. unsigned long *k;
  504. if (len < cpumask_size())
  505. memset(new_mask, 0, cpumask_size());
  506. else if (len > cpumask_size())
  507. len = cpumask_size();
  508. k = cpumask_bits(new_mask);
  509. return compat_get_bitmap(k, user_mask_ptr, len * 8);
  510. }
  511. COMPAT_SYSCALL_DEFINE3(sched_setaffinity, compat_pid_t, pid,
  512. unsigned int, len,
  513. compat_ulong_t __user *, user_mask_ptr)
  514. {
  515. cpumask_var_t new_mask;
  516. int retval;
  517. if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
  518. return -ENOMEM;
  519. retval = compat_get_user_cpu_mask(user_mask_ptr, len, new_mask);
  520. if (retval)
  521. goto out;
  522. retval = sched_setaffinity(pid, new_mask);
  523. out:
  524. free_cpumask_var(new_mask);
  525. return retval;
  526. }
  527. COMPAT_SYSCALL_DEFINE3(sched_getaffinity, compat_pid_t, pid, unsigned int, len,
  528. compat_ulong_t __user *, user_mask_ptr)
  529. {
  530. int ret;
  531. cpumask_var_t mask;
  532. if ((len * BITS_PER_BYTE) < nr_cpu_ids)
  533. return -EINVAL;
  534. if (len & (sizeof(compat_ulong_t)-1))
  535. return -EINVAL;
  536. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  537. return -ENOMEM;
  538. ret = sched_getaffinity(pid, mask);
  539. if (ret == 0) {
  540. size_t retlen = min_t(size_t, len, cpumask_size());
  541. if (compat_put_bitmap(user_mask_ptr, cpumask_bits(mask), retlen * 8))
  542. ret = -EFAULT;
  543. else
  544. ret = retlen;
  545. }
  546. free_cpumask_var(mask);
  547. return ret;
  548. }
  549. int get_compat_itimerspec(struct itimerspec *dst,
  550. const struct compat_itimerspec __user *src)
  551. {
  552. if (__compat_get_timespec(&dst->it_interval, &src->it_interval) ||
  553. __compat_get_timespec(&dst->it_value, &src->it_value))
  554. return -EFAULT;
  555. return 0;
  556. }
  557. int put_compat_itimerspec(struct compat_itimerspec __user *dst,
  558. const struct itimerspec *src)
  559. {
  560. if (__compat_put_timespec(&src->it_interval, &dst->it_interval) ||
  561. __compat_put_timespec(&src->it_value, &dst->it_value))
  562. return -EFAULT;
  563. return 0;
  564. }
  565. COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock,
  566. struct compat_sigevent __user *, timer_event_spec,
  567. timer_t __user *, created_timer_id)
  568. {
  569. struct sigevent __user *event = NULL;
  570. if (timer_event_spec) {
  571. struct sigevent kevent;
  572. event = compat_alloc_user_space(sizeof(*event));
  573. if (get_compat_sigevent(&kevent, timer_event_spec) ||
  574. copy_to_user(event, &kevent, sizeof(*event)))
  575. return -EFAULT;
  576. }
  577. return sys_timer_create(which_clock, event, created_timer_id);
  578. }
  579. COMPAT_SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
  580. struct compat_itimerspec __user *, new,
  581. struct compat_itimerspec __user *, old)
  582. {
  583. long err;
  584. mm_segment_t oldfs;
  585. struct itimerspec newts, oldts;
  586. if (!new)
  587. return -EINVAL;
  588. if (get_compat_itimerspec(&newts, new))
  589. return -EFAULT;
  590. oldfs = get_fs();
  591. set_fs(KERNEL_DS);
  592. err = sys_timer_settime(timer_id, flags,
  593. (struct itimerspec __user *) &newts,
  594. (struct itimerspec __user *) &oldts);
  595. set_fs(oldfs);
  596. if (!err && old && put_compat_itimerspec(old, &oldts))
  597. return -EFAULT;
  598. return err;
  599. }
  600. COMPAT_SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
  601. struct compat_itimerspec __user *, setting)
  602. {
  603. long err;
  604. mm_segment_t oldfs;
  605. struct itimerspec ts;
  606. oldfs = get_fs();
  607. set_fs(KERNEL_DS);
  608. err = sys_timer_gettime(timer_id,
  609. (struct itimerspec __user *) &ts);
  610. set_fs(oldfs);
  611. if (!err && put_compat_itimerspec(setting, &ts))
  612. return -EFAULT;
  613. return err;
  614. }
  615. COMPAT_SYSCALL_DEFINE2(clock_settime, clockid_t, which_clock,
  616. struct compat_timespec __user *, tp)
  617. {
  618. long err;
  619. mm_segment_t oldfs;
  620. struct timespec ts;
  621. if (compat_get_timespec(&ts, tp))
  622. return -EFAULT;
  623. oldfs = get_fs();
  624. set_fs(KERNEL_DS);
  625. err = sys_clock_settime(which_clock,
  626. (struct timespec __user *) &ts);
  627. set_fs(oldfs);
  628. return err;
  629. }
  630. COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock,
  631. struct compat_timespec __user *, tp)
  632. {
  633. long err;
  634. mm_segment_t oldfs;
  635. struct timespec ts;
  636. oldfs = get_fs();
  637. set_fs(KERNEL_DS);
  638. err = sys_clock_gettime(which_clock,
  639. (struct timespec __user *) &ts);
  640. set_fs(oldfs);
  641. if (!err && compat_put_timespec(&ts, tp))
  642. return -EFAULT;
  643. return err;
  644. }
  645. COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock,
  646. struct compat_timex __user *, utp)
  647. {
  648. struct timex txc;
  649. mm_segment_t oldfs;
  650. int err, ret;
  651. err = compat_get_timex(&txc, utp);
  652. if (err)
  653. return err;
  654. oldfs = get_fs();
  655. set_fs(KERNEL_DS);
  656. ret = sys_clock_adjtime(which_clock, (struct timex __user *) &txc);
  657. set_fs(oldfs);
  658. err = compat_put_timex(utp, &txc);
  659. if (err)
  660. return err;
  661. return ret;
  662. }
  663. COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock,
  664. struct compat_timespec __user *, tp)
  665. {
  666. long err;
  667. mm_segment_t oldfs;
  668. struct timespec ts;
  669. oldfs = get_fs();
  670. set_fs(KERNEL_DS);
  671. err = sys_clock_getres(which_clock,
  672. (struct timespec __user *) &ts);
  673. set_fs(oldfs);
  674. if (!err && tp && compat_put_timespec(&ts, tp))
  675. return -EFAULT;
  676. return err;
  677. }
  678. static long compat_clock_nanosleep_restart(struct restart_block *restart)
  679. {
  680. long err;
  681. mm_segment_t oldfs;
  682. struct timespec tu;
  683. struct compat_timespec __user *rmtp = restart->nanosleep.compat_rmtp;
  684. restart->nanosleep.rmtp = (struct timespec __user *) &tu;
  685. oldfs = get_fs();
  686. set_fs(KERNEL_DS);
  687. err = clock_nanosleep_restart(restart);
  688. set_fs(oldfs);
  689. if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
  690. compat_put_timespec(&tu, rmtp))
  691. return -EFAULT;
  692. if (err == -ERESTART_RESTARTBLOCK) {
  693. restart->fn = compat_clock_nanosleep_restart;
  694. restart->nanosleep.compat_rmtp = rmtp;
  695. }
  696. return err;
  697. }
  698. COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags,
  699. struct compat_timespec __user *, rqtp,
  700. struct compat_timespec __user *, rmtp)
  701. {
  702. long err;
  703. mm_segment_t oldfs;
  704. struct timespec in, out;
  705. struct restart_block *restart;
  706. if (compat_get_timespec(&in, rqtp))
  707. return -EFAULT;
  708. oldfs = get_fs();
  709. set_fs(KERNEL_DS);
  710. err = sys_clock_nanosleep(which_clock, flags,
  711. (struct timespec __user *) &in,
  712. (struct timespec __user *) &out);
  713. set_fs(oldfs);
  714. if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
  715. compat_put_timespec(&out, rmtp))
  716. return -EFAULT;
  717. if (err == -ERESTART_RESTARTBLOCK) {
  718. restart = &current_thread_info()->restart_block;
  719. restart->fn = compat_clock_nanosleep_restart;
  720. restart->nanosleep.compat_rmtp = rmtp;
  721. }
  722. return err;
  723. }
  724. /*
  725. * We currently only need the following fields from the sigevent
  726. * structure: sigev_value, sigev_signo, sig_notify and (sometimes
  727. * sigev_notify_thread_id). The others are handled in user mode.
  728. * We also assume that copying sigev_value.sival_int is sufficient
  729. * to keep all the bits of sigev_value.sival_ptr intact.
  730. */
  731. int get_compat_sigevent(struct sigevent *event,
  732. const struct compat_sigevent __user *u_event)
  733. {
  734. memset(event, 0, sizeof(*event));
  735. return (!access_ok(VERIFY_READ, u_event, sizeof(*u_event)) ||
  736. __get_user(event->sigev_value.sival_int,
  737. &u_event->sigev_value.sival_int) ||
  738. __get_user(event->sigev_signo, &u_event->sigev_signo) ||
  739. __get_user(event->sigev_notify, &u_event->sigev_notify) ||
  740. __get_user(event->sigev_notify_thread_id,
  741. &u_event->sigev_notify_thread_id))
  742. ? -EFAULT : 0;
  743. }
  744. long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask,
  745. unsigned long bitmap_size)
  746. {
  747. int i, j;
  748. unsigned long m;
  749. compat_ulong_t um;
  750. unsigned long nr_compat_longs;
  751. /* align bitmap up to nearest compat_long_t boundary */
  752. bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
  753. if (!access_ok(VERIFY_READ, umask, bitmap_size / 8))
  754. return -EFAULT;
  755. nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
  756. for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
  757. m = 0;
  758. for (j = 0; j < sizeof(m)/sizeof(um); j++) {
  759. /*
  760. * We dont want to read past the end of the userspace
  761. * bitmap. We must however ensure the end of the
  762. * kernel bitmap is zeroed.
  763. */
  764. if (nr_compat_longs-- > 0) {
  765. if (__get_user(um, umask))
  766. return -EFAULT;
  767. } else {
  768. um = 0;
  769. }
  770. umask++;
  771. m |= (long)um << (j * BITS_PER_COMPAT_LONG);
  772. }
  773. *mask++ = m;
  774. }
  775. return 0;
  776. }
  777. long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
  778. unsigned long bitmap_size)
  779. {
  780. int i, j;
  781. unsigned long m;
  782. compat_ulong_t um;
  783. unsigned long nr_compat_longs;
  784. /* align bitmap up to nearest compat_long_t boundary */
  785. bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
  786. if (!access_ok(VERIFY_WRITE, umask, bitmap_size / 8))
  787. return -EFAULT;
  788. nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
  789. for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
  790. m = *mask++;
  791. for (j = 0; j < sizeof(m)/sizeof(um); j++) {
  792. um = m;
  793. /*
  794. * We dont want to write past the end of the userspace
  795. * bitmap.
  796. */
  797. if (nr_compat_longs-- > 0) {
  798. if (__put_user(um, umask))
  799. return -EFAULT;
  800. }
  801. umask++;
  802. m >>= 4*sizeof(um);
  803. m >>= 4*sizeof(um);
  804. }
  805. }
  806. return 0;
  807. }
  808. void
  809. sigset_from_compat(sigset_t *set, const compat_sigset_t *compat)
  810. {
  811. switch (_NSIG_WORDS) {
  812. case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32 );
  813. case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32 );
  814. case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32 );
  815. case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32 );
  816. }
  817. }
  818. EXPORT_SYMBOL_GPL(sigset_from_compat);
  819. void
  820. sigset_to_compat(compat_sigset_t *compat, const sigset_t *set)
  821. {
  822. switch (_NSIG_WORDS) {
  823. case 4: compat->sig[7] = (set->sig[3] >> 32); compat->sig[6] = set->sig[3];
  824. case 3: compat->sig[5] = (set->sig[2] >> 32); compat->sig[4] = set->sig[2];
  825. case 2: compat->sig[3] = (set->sig[1] >> 32); compat->sig[2] = set->sig[1];
  826. case 1: compat->sig[1] = (set->sig[0] >> 32); compat->sig[0] = set->sig[0];
  827. }
  828. }
  829. COMPAT_SYSCALL_DEFINE4(rt_sigtimedwait, compat_sigset_t __user *, uthese,
  830. struct compat_siginfo __user *, uinfo,
  831. struct compat_timespec __user *, uts, compat_size_t, sigsetsize)
  832. {
  833. compat_sigset_t s32;
  834. sigset_t s;
  835. struct timespec t;
  836. siginfo_t info;
  837. long ret;
  838. if (sigsetsize != sizeof(sigset_t))
  839. return -EINVAL;
  840. if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t)))
  841. return -EFAULT;
  842. sigset_from_compat(&s, &s32);
  843. if (uts) {
  844. if (compat_get_timespec(&t, uts))
  845. return -EFAULT;
  846. }
  847. ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);
  848. if (ret > 0 && uinfo) {
  849. if (copy_siginfo_to_user32(uinfo, &info))
  850. ret = -EFAULT;
  851. }
  852. return ret;
  853. }
  854. #ifdef __ARCH_WANT_COMPAT_SYS_TIME
  855. /* compat_time_t is a 32 bit "long" and needs to get converted. */
  856. COMPAT_SYSCALL_DEFINE1(time, compat_time_t __user *, tloc)
  857. {
  858. compat_time_t i;
  859. struct timeval tv;
  860. do_gettimeofday(&tv);
  861. i = tv.tv_sec;
  862. if (tloc) {
  863. if (put_user(i,tloc))
  864. return -EFAULT;
  865. }
  866. force_successful_syscall_return();
  867. return i;
  868. }
  869. COMPAT_SYSCALL_DEFINE1(stime, compat_time_t __user *, tptr)
  870. {
  871. struct timespec tv;
  872. int err;
  873. if (get_user(tv.tv_sec, tptr))
  874. return -EFAULT;
  875. tv.tv_nsec = 0;
  876. err = security_settime(&tv, NULL);
  877. if (err)
  878. return err;
  879. do_settimeofday(&tv);
  880. return 0;
  881. }
  882. #endif /* __ARCH_WANT_COMPAT_SYS_TIME */
  883. COMPAT_SYSCALL_DEFINE1(adjtimex, struct compat_timex __user *, utp)
  884. {
  885. struct timex txc;
  886. int err, ret;
  887. err = compat_get_timex(&txc, utp);
  888. if (err)
  889. return err;
  890. ret = do_adjtimex(&txc);
  891. err = compat_put_timex(utp, &txc);
  892. if (err)
  893. return err;
  894. return ret;
  895. }
  896. #ifdef CONFIG_NUMA
  897. COMPAT_SYSCALL_DEFINE6(move_pages, pid_t, pid, compat_ulong_t, nr_pages,
  898. compat_uptr_t __user *, pages32,
  899. const int __user *, nodes,
  900. int __user *, status,
  901. int, flags)
  902. {
  903. const void __user * __user *pages;
  904. int i;
  905. pages = compat_alloc_user_space(nr_pages * sizeof(void *));
  906. for (i = 0; i < nr_pages; i++) {
  907. compat_uptr_t p;
  908. if (get_user(p, pages32 + i) ||
  909. put_user(compat_ptr(p), pages + i))
  910. return -EFAULT;
  911. }
  912. return sys_move_pages(pid, nr_pages, pages, nodes, status, flags);
  913. }
  914. COMPAT_SYSCALL_DEFINE4(migrate_pages, compat_pid_t, pid,
  915. compat_ulong_t, maxnode,
  916. const compat_ulong_t __user *, old_nodes,
  917. const compat_ulong_t __user *, new_nodes)
  918. {
  919. unsigned long __user *old = NULL;
  920. unsigned long __user *new = NULL;
  921. nodemask_t tmp_mask;
  922. unsigned long nr_bits;
  923. unsigned long size;
  924. nr_bits = min_t(unsigned long, maxnode - 1, MAX_NUMNODES);
  925. size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
  926. if (old_nodes) {
  927. if (compat_get_bitmap(nodes_addr(tmp_mask), old_nodes, nr_bits))
  928. return -EFAULT;
  929. old = compat_alloc_user_space(new_nodes ? size * 2 : size);
  930. if (new_nodes)
  931. new = old + size / sizeof(unsigned long);
  932. if (copy_to_user(old, nodes_addr(tmp_mask), size))
  933. return -EFAULT;
  934. }
  935. if (new_nodes) {
  936. if (compat_get_bitmap(nodes_addr(tmp_mask), new_nodes, nr_bits))
  937. return -EFAULT;
  938. if (new == NULL)
  939. new = compat_alloc_user_space(size);
  940. if (copy_to_user(new, nodes_addr(tmp_mask), size))
  941. return -EFAULT;
  942. }
  943. return sys_migrate_pages(pid, nr_bits + 1, old, new);
  944. }
  945. #endif
  946. COMPAT_SYSCALL_DEFINE2(sched_rr_get_interval,
  947. compat_pid_t, pid,
  948. struct compat_timespec __user *, interval)
  949. {
  950. struct timespec t;
  951. int ret;
  952. mm_segment_t old_fs = get_fs();
  953. set_fs(KERNEL_DS);
  954. ret = sys_sched_rr_get_interval(pid, (struct timespec __user *)&t);
  955. set_fs(old_fs);
  956. if (compat_put_timespec(&t, interval))
  957. return -EFAULT;
  958. return ret;
  959. }
  960. /*
  961. * Allocate user-space memory for the duration of a single system call,
  962. * in order to marshall parameters inside a compat thunk.
  963. */
  964. void __user *compat_alloc_user_space(unsigned long len)
  965. {
  966. void __user *ptr;
  967. /* If len would occupy more than half of the entire compat space... */
  968. if (unlikely(len > (((compat_uptr_t)~0) >> 1)))
  969. return NULL;
  970. ptr = arch_compat_alloc_user_space(len);
  971. if (unlikely(!access_ok(VERIFY_WRITE, ptr, len)))
  972. return NULL;
  973. return ptr;
  974. }
  975. EXPORT_SYMBOL_GPL(compat_alloc_user_space);