compat.c 29 KB

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