compat.c 30 KB

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