compat.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/kernel/compat.c
  4. *
  5. * Kernel compatibililty routines for e.g. 32 bit syscall support
  6. * on 64 bit kernels.
  7. *
  8. * Copyright (C) 2002-2003 Stephen Rothwell, IBM Corporation
  9. */
  10. #include <linux/linkage.h>
  11. #include <linux/compat.h>
  12. #include <linux/errno.h>
  13. #include <linux/time.h>
  14. #include <linux/signal.h>
  15. #include <linux/sched.h> /* for MAX_SCHEDULE_TIMEOUT */
  16. #include <linux/syscalls.h>
  17. #include <linux/unistd.h>
  18. #include <linux/security.h>
  19. #include <linux/export.h>
  20. #include <linux/migrate.h>
  21. #include <linux/posix-timers.h>
  22. #include <linux/times.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/gfp.h>
  25. #include <linux/uaccess.h>
  26. #ifdef __ARCH_WANT_SYS_SIGPROCMASK
  27. /*
  28. * sys_sigprocmask SIG_SETMASK sets the first (compat) word of the
  29. * blocked set of signals to the supplied signal set
  30. */
  31. static inline void compat_sig_setmask(sigset_t *blocked, compat_sigset_word set)
  32. {
  33. memcpy(blocked->sig, &set, sizeof(set));
  34. }
  35. COMPAT_SYSCALL_DEFINE3(sigprocmask, int, how,
  36. compat_old_sigset_t __user *, nset,
  37. compat_old_sigset_t __user *, oset)
  38. {
  39. old_sigset_t old_set, new_set;
  40. sigset_t new_blocked;
  41. old_set = current->blocked.sig[0];
  42. if (nset) {
  43. if (get_user(new_set, nset))
  44. return -EFAULT;
  45. new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
  46. new_blocked = current->blocked;
  47. switch (how) {
  48. case SIG_BLOCK:
  49. sigaddsetmask(&new_blocked, new_set);
  50. break;
  51. case SIG_UNBLOCK:
  52. sigdelsetmask(&new_blocked, new_set);
  53. break;
  54. case SIG_SETMASK:
  55. compat_sig_setmask(&new_blocked, new_set);
  56. break;
  57. default:
  58. return -EINVAL;
  59. }
  60. set_current_blocked(&new_blocked);
  61. }
  62. if (oset) {
  63. if (put_user(old_set, oset))
  64. return -EFAULT;
  65. }
  66. return 0;
  67. }
  68. #endif
  69. int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru)
  70. {
  71. struct compat_rusage r32;
  72. memset(&r32, 0, sizeof(r32));
  73. r32.ru_utime.tv_sec = r->ru_utime.tv_sec;
  74. r32.ru_utime.tv_usec = r->ru_utime.tv_usec;
  75. r32.ru_stime.tv_sec = r->ru_stime.tv_sec;
  76. r32.ru_stime.tv_usec = r->ru_stime.tv_usec;
  77. r32.ru_maxrss = r->ru_maxrss;
  78. r32.ru_ixrss = r->ru_ixrss;
  79. r32.ru_idrss = r->ru_idrss;
  80. r32.ru_isrss = r->ru_isrss;
  81. r32.ru_minflt = r->ru_minflt;
  82. r32.ru_majflt = r->ru_majflt;
  83. r32.ru_nswap = r->ru_nswap;
  84. r32.ru_inblock = r->ru_inblock;
  85. r32.ru_oublock = r->ru_oublock;
  86. r32.ru_msgsnd = r->ru_msgsnd;
  87. r32.ru_msgrcv = r->ru_msgrcv;
  88. r32.ru_nsignals = r->ru_nsignals;
  89. r32.ru_nvcsw = r->ru_nvcsw;
  90. r32.ru_nivcsw = r->ru_nivcsw;
  91. if (copy_to_user(ru, &r32, sizeof(r32)))
  92. return -EFAULT;
  93. return 0;
  94. }
  95. static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr,
  96. unsigned len, struct cpumask *new_mask)
  97. {
  98. unsigned long *k;
  99. if (len < cpumask_size())
  100. memset(new_mask, 0, cpumask_size());
  101. else if (len > cpumask_size())
  102. len = cpumask_size();
  103. k = cpumask_bits(new_mask);
  104. return compat_get_bitmap(k, user_mask_ptr, len * 8);
  105. }
  106. COMPAT_SYSCALL_DEFINE3(sched_setaffinity, compat_pid_t, pid,
  107. unsigned int, len,
  108. compat_ulong_t __user *, user_mask_ptr)
  109. {
  110. cpumask_var_t new_mask;
  111. int retval;
  112. if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
  113. return -ENOMEM;
  114. retval = compat_get_user_cpu_mask(user_mask_ptr, len, new_mask);
  115. if (retval)
  116. goto out;
  117. retval = sched_setaffinity(pid, new_mask);
  118. out:
  119. free_cpumask_var(new_mask);
  120. return retval;
  121. }
  122. COMPAT_SYSCALL_DEFINE3(sched_getaffinity, compat_pid_t, pid, unsigned int, len,
  123. compat_ulong_t __user *, user_mask_ptr)
  124. {
  125. int ret;
  126. cpumask_var_t mask;
  127. if ((len * BITS_PER_BYTE) < nr_cpu_ids)
  128. return -EINVAL;
  129. if (len & (sizeof(compat_ulong_t)-1))
  130. return -EINVAL;
  131. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  132. return -ENOMEM;
  133. ret = sched_getaffinity(pid, mask);
  134. if (ret == 0) {
  135. unsigned int retlen = min(len, cpumask_size());
  136. if (compat_put_bitmap(user_mask_ptr, cpumask_bits(mask), retlen * 8))
  137. ret = -EFAULT;
  138. else
  139. ret = retlen;
  140. }
  141. free_cpumask_var(mask);
  142. return ret;
  143. }
  144. /*
  145. * We currently only need the following fields from the sigevent
  146. * structure: sigev_value, sigev_signo, sig_notify and (sometimes
  147. * sigev_notify_thread_id). The others are handled in user mode.
  148. * We also assume that copying sigev_value.sival_int is sufficient
  149. * to keep all the bits of sigev_value.sival_ptr intact.
  150. */
  151. int get_compat_sigevent(struct sigevent *event,
  152. const struct compat_sigevent __user *u_event)
  153. {
  154. memset(event, 0, sizeof(*event));
  155. return (!access_ok(u_event, sizeof(*u_event)) ||
  156. __get_user(event->sigev_value.sival_int,
  157. &u_event->sigev_value.sival_int) ||
  158. __get_user(event->sigev_signo, &u_event->sigev_signo) ||
  159. __get_user(event->sigev_notify, &u_event->sigev_notify) ||
  160. __get_user(event->sigev_notify_thread_id,
  161. &u_event->sigev_notify_thread_id))
  162. ? -EFAULT : 0;
  163. }
  164. long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask,
  165. unsigned long bitmap_size)
  166. {
  167. unsigned long nr_compat_longs;
  168. /* align bitmap up to nearest compat_long_t boundary */
  169. bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
  170. nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
  171. if (!user_read_access_begin(umask, bitmap_size / 8))
  172. return -EFAULT;
  173. while (nr_compat_longs > 1) {
  174. compat_ulong_t l1, l2;
  175. unsafe_get_user(l1, umask++, Efault);
  176. unsafe_get_user(l2, umask++, Efault);
  177. *mask++ = ((unsigned long)l2 << BITS_PER_COMPAT_LONG) | l1;
  178. nr_compat_longs -= 2;
  179. }
  180. if (nr_compat_longs)
  181. unsafe_get_user(*mask, umask++, Efault);
  182. user_read_access_end();
  183. return 0;
  184. Efault:
  185. user_read_access_end();
  186. return -EFAULT;
  187. }
  188. long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
  189. unsigned long bitmap_size)
  190. {
  191. unsigned long nr_compat_longs;
  192. /* align bitmap up to nearest compat_long_t boundary */
  193. bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
  194. nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
  195. if (!user_write_access_begin(umask, bitmap_size / 8))
  196. return -EFAULT;
  197. while (nr_compat_longs > 1) {
  198. unsigned long m = *mask++;
  199. unsafe_put_user((compat_ulong_t)m, umask++, Efault);
  200. unsafe_put_user(m >> BITS_PER_COMPAT_LONG, umask++, Efault);
  201. nr_compat_longs -= 2;
  202. }
  203. if (nr_compat_longs)
  204. unsafe_put_user((compat_ulong_t)*mask, umask++, Efault);
  205. user_write_access_end();
  206. return 0;
  207. Efault:
  208. user_write_access_end();
  209. return -EFAULT;
  210. }
  211. int
  212. get_compat_sigset(sigset_t *set, const compat_sigset_t __user *compat)
  213. {
  214. #ifdef __BIG_ENDIAN
  215. compat_sigset_t v;
  216. if (copy_from_user(&v, compat, sizeof(compat_sigset_t)))
  217. return -EFAULT;
  218. switch (_NSIG_WORDS) {
  219. case 4: set->sig[3] = v.sig[6] | (((long)v.sig[7]) << 32 );
  220. fallthrough;
  221. case 3: set->sig[2] = v.sig[4] | (((long)v.sig[5]) << 32 );
  222. fallthrough;
  223. case 2: set->sig[1] = v.sig[2] | (((long)v.sig[3]) << 32 );
  224. fallthrough;
  225. case 1: set->sig[0] = v.sig[0] | (((long)v.sig[1]) << 32 );
  226. }
  227. #else
  228. if (copy_from_user(set, compat, sizeof(compat_sigset_t)))
  229. return -EFAULT;
  230. #endif
  231. return 0;
  232. }
  233. EXPORT_SYMBOL_GPL(get_compat_sigset);
  234. /*
  235. * Allocate user-space memory for the duration of a single system call,
  236. * in order to marshall parameters inside a compat thunk.
  237. */
  238. void __user *compat_alloc_user_space(unsigned long len)
  239. {
  240. void __user *ptr;
  241. /* If len would occupy more than half of the entire compat space... */
  242. if (unlikely(len > (((compat_uptr_t)~0) >> 1)))
  243. return NULL;
  244. ptr = arch_compat_alloc_user_space(len);
  245. if (unlikely(!access_ok(ptr, len)))
  246. return NULL;
  247. return ptr;
  248. }
  249. EXPORT_SYMBOL_GPL(compat_alloc_user_space);