compat.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 <linux/uaccess.h>
  30. int compat_get_timex(struct timex *txc, const struct compat_timex __user *utp)
  31. {
  32. struct compat_timex tx32;
  33. memset(txc, 0, sizeof(struct timex));
  34. if (copy_from_user(&tx32, utp, sizeof(struct compat_timex)))
  35. return -EFAULT;
  36. txc->modes = tx32.modes;
  37. txc->offset = tx32.offset;
  38. txc->freq = tx32.freq;
  39. txc->maxerror = tx32.maxerror;
  40. txc->esterror = tx32.esterror;
  41. txc->status = tx32.status;
  42. txc->constant = tx32.constant;
  43. txc->precision = tx32.precision;
  44. txc->tolerance = tx32.tolerance;
  45. txc->time.tv_sec = tx32.time.tv_sec;
  46. txc->time.tv_usec = tx32.time.tv_usec;
  47. txc->tick = tx32.tick;
  48. txc->ppsfreq = tx32.ppsfreq;
  49. txc->jitter = tx32.jitter;
  50. txc->shift = tx32.shift;
  51. txc->stabil = tx32.stabil;
  52. txc->jitcnt = tx32.jitcnt;
  53. txc->calcnt = tx32.calcnt;
  54. txc->errcnt = tx32.errcnt;
  55. txc->stbcnt = tx32.stbcnt;
  56. return 0;
  57. }
  58. int compat_put_timex(struct compat_timex __user *utp, const struct timex *txc)
  59. {
  60. struct compat_timex tx32;
  61. memset(&tx32, 0, sizeof(struct compat_timex));
  62. tx32.modes = txc->modes;
  63. tx32.offset = txc->offset;
  64. tx32.freq = txc->freq;
  65. tx32.maxerror = txc->maxerror;
  66. tx32.esterror = txc->esterror;
  67. tx32.status = txc->status;
  68. tx32.constant = txc->constant;
  69. tx32.precision = txc->precision;
  70. tx32.tolerance = txc->tolerance;
  71. tx32.time.tv_sec = txc->time.tv_sec;
  72. tx32.time.tv_usec = txc->time.tv_usec;
  73. tx32.tick = txc->tick;
  74. tx32.ppsfreq = txc->ppsfreq;
  75. tx32.jitter = txc->jitter;
  76. tx32.shift = txc->shift;
  77. tx32.stabil = txc->stabil;
  78. tx32.jitcnt = txc->jitcnt;
  79. tx32.calcnt = txc->calcnt;
  80. tx32.errcnt = txc->errcnt;
  81. tx32.stbcnt = txc->stbcnt;
  82. tx32.tai = txc->tai;
  83. if (copy_to_user(utp, &tx32, sizeof(struct compat_timex)))
  84. return -EFAULT;
  85. return 0;
  86. }
  87. static int __compat_get_timeval(struct timeval *tv, const struct compat_timeval __user *ctv)
  88. {
  89. return (!access_ok(VERIFY_READ, ctv, sizeof(*ctv)) ||
  90. __get_user(tv->tv_sec, &ctv->tv_sec) ||
  91. __get_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0;
  92. }
  93. static int __compat_put_timeval(const struct timeval *tv, struct compat_timeval __user *ctv)
  94. {
  95. return (!access_ok(VERIFY_WRITE, ctv, sizeof(*ctv)) ||
  96. __put_user(tv->tv_sec, &ctv->tv_sec) ||
  97. __put_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0;
  98. }
  99. static int __compat_get_timespec(struct timespec *ts, const struct compat_timespec __user *cts)
  100. {
  101. return (!access_ok(VERIFY_READ, cts, sizeof(*cts)) ||
  102. __get_user(ts->tv_sec, &cts->tv_sec) ||
  103. __get_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
  104. }
  105. static int __compat_put_timespec(const struct timespec *ts, struct compat_timespec __user *cts)
  106. {
  107. return (!access_ok(VERIFY_WRITE, cts, sizeof(*cts)) ||
  108. __put_user(ts->tv_sec, &cts->tv_sec) ||
  109. __put_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
  110. }
  111. int compat_get_timeval(struct timeval *tv, const void __user *utv)
  112. {
  113. if (COMPAT_USE_64BIT_TIME)
  114. return copy_from_user(tv, utv, sizeof(*tv)) ? -EFAULT : 0;
  115. else
  116. return __compat_get_timeval(tv, utv);
  117. }
  118. EXPORT_SYMBOL_GPL(compat_get_timeval);
  119. int compat_put_timeval(const struct timeval *tv, void __user *utv)
  120. {
  121. if (COMPAT_USE_64BIT_TIME)
  122. return copy_to_user(utv, tv, sizeof(*tv)) ? -EFAULT : 0;
  123. else
  124. return __compat_put_timeval(tv, utv);
  125. }
  126. EXPORT_SYMBOL_GPL(compat_put_timeval);
  127. int compat_get_timespec(struct timespec *ts, const void __user *uts)
  128. {
  129. if (COMPAT_USE_64BIT_TIME)
  130. return copy_from_user(ts, uts, sizeof(*ts)) ? -EFAULT : 0;
  131. else
  132. return __compat_get_timespec(ts, uts);
  133. }
  134. EXPORT_SYMBOL_GPL(compat_get_timespec);
  135. int compat_put_timespec(const struct timespec *ts, void __user *uts)
  136. {
  137. if (COMPAT_USE_64BIT_TIME)
  138. return copy_to_user(uts, ts, sizeof(*ts)) ? -EFAULT : 0;
  139. else
  140. return __compat_put_timespec(ts, uts);
  141. }
  142. EXPORT_SYMBOL_GPL(compat_put_timespec);
  143. int get_compat_itimerval(struct itimerval *o, const struct compat_itimerval __user *i)
  144. {
  145. struct compat_itimerval v32;
  146. if (copy_from_user(&v32, i, sizeof(struct compat_itimerval)))
  147. return -EFAULT;
  148. o->it_interval.tv_sec = v32.it_interval.tv_sec;
  149. o->it_interval.tv_usec = v32.it_interval.tv_usec;
  150. o->it_value.tv_sec = v32.it_value.tv_sec;
  151. o->it_value.tv_usec = v32.it_value.tv_usec;
  152. return 0;
  153. }
  154. int put_compat_itimerval(struct compat_itimerval __user *o, const struct itimerval *i)
  155. {
  156. struct compat_itimerval v32;
  157. v32.it_interval.tv_sec = i->it_interval.tv_sec;
  158. v32.it_interval.tv_usec = i->it_interval.tv_usec;
  159. v32.it_value.tv_sec = i->it_value.tv_sec;
  160. v32.it_value.tv_usec = i->it_value.tv_usec;
  161. return copy_to_user(o, &v32, sizeof(struct compat_itimerval)) ? -EFAULT : 0;
  162. }
  163. #ifdef __ARCH_WANT_SYS_SIGPROCMASK
  164. /*
  165. * sys_sigprocmask SIG_SETMASK sets the first (compat) word of the
  166. * blocked set of signals to the supplied signal set
  167. */
  168. static inline void compat_sig_setmask(sigset_t *blocked, compat_sigset_word set)
  169. {
  170. memcpy(blocked->sig, &set, sizeof(set));
  171. }
  172. COMPAT_SYSCALL_DEFINE3(sigprocmask, int, how,
  173. compat_old_sigset_t __user *, nset,
  174. compat_old_sigset_t __user *, oset)
  175. {
  176. old_sigset_t old_set, new_set;
  177. sigset_t new_blocked;
  178. old_set = current->blocked.sig[0];
  179. if (nset) {
  180. if (get_user(new_set, nset))
  181. return -EFAULT;
  182. new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
  183. new_blocked = current->blocked;
  184. switch (how) {
  185. case SIG_BLOCK:
  186. sigaddsetmask(&new_blocked, new_set);
  187. break;
  188. case SIG_UNBLOCK:
  189. sigdelsetmask(&new_blocked, new_set);
  190. break;
  191. case SIG_SETMASK:
  192. compat_sig_setmask(&new_blocked, new_set);
  193. break;
  194. default:
  195. return -EINVAL;
  196. }
  197. set_current_blocked(&new_blocked);
  198. }
  199. if (oset) {
  200. if (put_user(old_set, oset))
  201. return -EFAULT;
  202. }
  203. return 0;
  204. }
  205. #endif
  206. int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru)
  207. {
  208. struct compat_rusage r32;
  209. memset(&r32, 0, sizeof(r32));
  210. r32.ru_utime.tv_sec = r->ru_utime.tv_sec;
  211. r32.ru_utime.tv_usec = r->ru_utime.tv_usec;
  212. r32.ru_stime.tv_sec = r->ru_stime.tv_sec;
  213. r32.ru_stime.tv_usec = r->ru_stime.tv_usec;
  214. r32.ru_maxrss = r->ru_maxrss;
  215. r32.ru_ixrss = r->ru_ixrss;
  216. r32.ru_idrss = r->ru_idrss;
  217. r32.ru_isrss = r->ru_isrss;
  218. r32.ru_minflt = r->ru_minflt;
  219. r32.ru_majflt = r->ru_majflt;
  220. r32.ru_nswap = r->ru_nswap;
  221. r32.ru_inblock = r->ru_inblock;
  222. r32.ru_oublock = r->ru_oublock;
  223. r32.ru_msgsnd = r->ru_msgsnd;
  224. r32.ru_msgrcv = r->ru_msgrcv;
  225. r32.ru_nsignals = r->ru_nsignals;
  226. r32.ru_nvcsw = r->ru_nvcsw;
  227. r32.ru_nivcsw = r->ru_nivcsw;
  228. if (copy_to_user(ru, &r32, sizeof(r32)))
  229. return -EFAULT;
  230. return 0;
  231. }
  232. static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr,
  233. unsigned len, struct cpumask *new_mask)
  234. {
  235. unsigned long *k;
  236. if (len < cpumask_size())
  237. memset(new_mask, 0, cpumask_size());
  238. else if (len > cpumask_size())
  239. len = cpumask_size();
  240. k = cpumask_bits(new_mask);
  241. return compat_get_bitmap(k, user_mask_ptr, len * 8);
  242. }
  243. COMPAT_SYSCALL_DEFINE3(sched_setaffinity, compat_pid_t, pid,
  244. unsigned int, len,
  245. compat_ulong_t __user *, user_mask_ptr)
  246. {
  247. cpumask_var_t new_mask;
  248. int retval;
  249. if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
  250. return -ENOMEM;
  251. retval = compat_get_user_cpu_mask(user_mask_ptr, len, new_mask);
  252. if (retval)
  253. goto out;
  254. retval = sched_setaffinity(pid, new_mask);
  255. out:
  256. free_cpumask_var(new_mask);
  257. return retval;
  258. }
  259. COMPAT_SYSCALL_DEFINE3(sched_getaffinity, compat_pid_t, pid, unsigned int, len,
  260. compat_ulong_t __user *, user_mask_ptr)
  261. {
  262. int ret;
  263. cpumask_var_t mask;
  264. if ((len * BITS_PER_BYTE) < nr_cpu_ids)
  265. return -EINVAL;
  266. if (len & (sizeof(compat_ulong_t)-1))
  267. return -EINVAL;
  268. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  269. return -ENOMEM;
  270. ret = sched_getaffinity(pid, mask);
  271. if (ret == 0) {
  272. unsigned int retlen = min(len, cpumask_size());
  273. if (compat_put_bitmap(user_mask_ptr, cpumask_bits(mask), retlen * 8))
  274. ret = -EFAULT;
  275. else
  276. ret = retlen;
  277. }
  278. free_cpumask_var(mask);
  279. return ret;
  280. }
  281. /*
  282. * We currently only need the following fields from the sigevent
  283. * structure: sigev_value, sigev_signo, sig_notify and (sometimes
  284. * sigev_notify_thread_id). The others are handled in user mode.
  285. * We also assume that copying sigev_value.sival_int is sufficient
  286. * to keep all the bits of sigev_value.sival_ptr intact.
  287. */
  288. int get_compat_sigevent(struct sigevent *event,
  289. const struct compat_sigevent __user *u_event)
  290. {
  291. memset(event, 0, sizeof(*event));
  292. return (!access_ok(VERIFY_READ, u_event, sizeof(*u_event)) ||
  293. __get_user(event->sigev_value.sival_int,
  294. &u_event->sigev_value.sival_int) ||
  295. __get_user(event->sigev_signo, &u_event->sigev_signo) ||
  296. __get_user(event->sigev_notify, &u_event->sigev_notify) ||
  297. __get_user(event->sigev_notify_thread_id,
  298. &u_event->sigev_notify_thread_id))
  299. ? -EFAULT : 0;
  300. }
  301. long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask,
  302. unsigned long bitmap_size)
  303. {
  304. unsigned long nr_compat_longs;
  305. /* align bitmap up to nearest compat_long_t boundary */
  306. bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
  307. nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
  308. if (!access_ok(VERIFY_READ, umask, bitmap_size / 8))
  309. return -EFAULT;
  310. user_access_begin();
  311. while (nr_compat_longs > 1) {
  312. compat_ulong_t l1, l2;
  313. unsafe_get_user(l1, umask++, Efault);
  314. unsafe_get_user(l2, umask++, Efault);
  315. *mask++ = ((unsigned long)l2 << BITS_PER_COMPAT_LONG) | l1;
  316. nr_compat_longs -= 2;
  317. }
  318. if (nr_compat_longs)
  319. unsafe_get_user(*mask, umask++, Efault);
  320. user_access_end();
  321. return 0;
  322. Efault:
  323. user_access_end();
  324. return -EFAULT;
  325. }
  326. long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
  327. unsigned long bitmap_size)
  328. {
  329. unsigned long nr_compat_longs;
  330. /* align bitmap up to nearest compat_long_t boundary */
  331. bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
  332. nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
  333. if (!access_ok(VERIFY_WRITE, umask, bitmap_size / 8))
  334. return -EFAULT;
  335. user_access_begin();
  336. while (nr_compat_longs > 1) {
  337. unsigned long m = *mask++;
  338. unsafe_put_user((compat_ulong_t)m, umask++, Efault);
  339. unsafe_put_user(m >> BITS_PER_COMPAT_LONG, umask++, Efault);
  340. nr_compat_longs -= 2;
  341. }
  342. if (nr_compat_longs)
  343. unsafe_put_user((compat_ulong_t)*mask, umask++, Efault);
  344. user_access_end();
  345. return 0;
  346. Efault:
  347. user_access_end();
  348. return -EFAULT;
  349. }
  350. int
  351. get_compat_sigset(sigset_t *set, const compat_sigset_t __user *compat)
  352. {
  353. #ifdef __BIG_ENDIAN
  354. compat_sigset_t v;
  355. if (copy_from_user(&v, compat, sizeof(compat_sigset_t)))
  356. return -EFAULT;
  357. switch (_NSIG_WORDS) {
  358. case 4: set->sig[3] = v.sig[6] | (((long)v.sig[7]) << 32 );
  359. case 3: set->sig[2] = v.sig[4] | (((long)v.sig[5]) << 32 );
  360. case 2: set->sig[1] = v.sig[2] | (((long)v.sig[3]) << 32 );
  361. case 1: set->sig[0] = v.sig[0] | (((long)v.sig[1]) << 32 );
  362. }
  363. #else
  364. if (copy_from_user(set, compat, sizeof(compat_sigset_t)))
  365. return -EFAULT;
  366. #endif
  367. return 0;
  368. }
  369. EXPORT_SYMBOL_GPL(get_compat_sigset);
  370. /*
  371. * Allocate user-space memory for the duration of a single system call,
  372. * in order to marshall parameters inside a compat thunk.
  373. */
  374. void __user *compat_alloc_user_space(unsigned long len)
  375. {
  376. void __user *ptr;
  377. /* If len would occupy more than half of the entire compat space... */
  378. if (unlikely(len > (((compat_uptr_t)~0) >> 1)))
  379. return NULL;
  380. ptr = arch_compat_alloc_user_space(len);
  381. if (unlikely(!access_ok(VERIFY_WRITE, ptr, len)))
  382. return NULL;
  383. return ptr;
  384. }
  385. EXPORT_SYMBOL_GPL(compat_alloc_user_space);