Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 14 | #include <linux/linkage.h> |
| 15 | #include <linux/compat.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/time.h> |
| 18 | #include <linux/signal.h> |
| 19 | #include <linux/sched.h> /* for MAX_SCHEDULE_TIMEOUT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/syscalls.h> |
| 21 | #include <linux/unistd.h> |
| 22 | #include <linux/security.h> |
Paul Gortmaker | 6e5fdee | 2011-05-26 16:00:52 -0400 | [diff] [blame] | 23 | #include <linux/export.h> |
Christoph Lameter | 1b2db9f | 2006-06-23 02:03:56 -0700 | [diff] [blame] | 24 | #include <linux/migrate.h> |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 25 | #include <linux/posix-timers.h> |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 26 | #include <linux/times.h> |
Paul Mackerras | e3d5a27 | 2009-01-06 14:41:02 -0800 | [diff] [blame] | 27 | #include <linux/ptrace.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 30 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Arnd Bergmann | 9afc5ee | 2018-07-13 12:52:28 +0200 | [diff] [blame] | 32 | static int __compat_get_timeval(struct timeval *tv, const struct old_timeval32 __user *ctv) |
H. Peter Anvin | 6684ba2 | 2012-02-19 17:38:00 -0800 | [diff] [blame] | 33 | { |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 34 | return (!access_ok(ctv, sizeof(*ctv)) || |
H. Peter Anvin | 6684ba2 | 2012-02-19 17:38:00 -0800 | [diff] [blame] | 35 | __get_user(tv->tv_sec, &ctv->tv_sec) || |
| 36 | __get_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0; |
| 37 | } |
H. Peter Anvin | 6684ba2 | 2012-02-19 17:38:00 -0800 | [diff] [blame] | 38 | |
Arnd Bergmann | 9afc5ee | 2018-07-13 12:52:28 +0200 | [diff] [blame] | 39 | static int __compat_put_timeval(const struct timeval *tv, struct old_timeval32 __user *ctv) |
H. Peter Anvin | 6684ba2 | 2012-02-19 17:38:00 -0800 | [diff] [blame] | 40 | { |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 41 | return (!access_ok(ctv, sizeof(*ctv)) || |
H. Peter Anvin | 6684ba2 | 2012-02-19 17:38:00 -0800 | [diff] [blame] | 42 | __put_user(tv->tv_sec, &ctv->tv_sec) || |
| 43 | __put_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0; |
| 44 | } |
H. Peter Anvin | 6684ba2 | 2012-02-19 17:38:00 -0800 | [diff] [blame] | 45 | |
Arnd Bergmann | 9afc5ee | 2018-07-13 12:52:28 +0200 | [diff] [blame] | 46 | static int __compat_get_timespec(struct timespec *ts, const struct old_timespec32 __user *cts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 48 | return (!access_ok(cts, sizeof(*cts)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | __get_user(ts->tv_sec, &cts->tv_sec) || |
| 50 | __get_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0; |
| 51 | } |
| 52 | |
Arnd Bergmann | 9afc5ee | 2018-07-13 12:52:28 +0200 | [diff] [blame] | 53 | static int __compat_put_timespec(const struct timespec *ts, struct old_timespec32 __user *cts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 55 | return (!access_ok(cts, sizeof(*cts)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | __put_user(ts->tv_sec, &cts->tv_sec) || |
| 57 | __put_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0; |
| 58 | } |
| 59 | |
H. Peter Anvin | 6684ba2 | 2012-02-19 17:38:00 -0800 | [diff] [blame] | 60 | int compat_get_timeval(struct timeval *tv, const void __user *utv) |
| 61 | { |
| 62 | if (COMPAT_USE_64BIT_TIME) |
Fabian Frederick | 6516a46 | 2014-06-04 16:12:02 -0700 | [diff] [blame] | 63 | return copy_from_user(tv, utv, sizeof(*tv)) ? -EFAULT : 0; |
H. Peter Anvin | 6684ba2 | 2012-02-19 17:38:00 -0800 | [diff] [blame] | 64 | else |
H. Peter Anvin | 81993e8 | 2014-02-01 18:54:11 -0800 | [diff] [blame] | 65 | return __compat_get_timeval(tv, utv); |
H. Peter Anvin | 6684ba2 | 2012-02-19 17:38:00 -0800 | [diff] [blame] | 66 | } |
| 67 | EXPORT_SYMBOL_GPL(compat_get_timeval); |
| 68 | |
| 69 | int compat_put_timeval(const struct timeval *tv, void __user *utv) |
| 70 | { |
| 71 | if (COMPAT_USE_64BIT_TIME) |
Fabian Frederick | 6516a46 | 2014-06-04 16:12:02 -0700 | [diff] [blame] | 72 | return copy_to_user(utv, tv, sizeof(*tv)) ? -EFAULT : 0; |
H. Peter Anvin | 6684ba2 | 2012-02-19 17:38:00 -0800 | [diff] [blame] | 73 | else |
H. Peter Anvin | 81993e8 | 2014-02-01 18:54:11 -0800 | [diff] [blame] | 74 | return __compat_put_timeval(tv, utv); |
H. Peter Anvin | 6684ba2 | 2012-02-19 17:38:00 -0800 | [diff] [blame] | 75 | } |
| 76 | EXPORT_SYMBOL_GPL(compat_put_timeval); |
| 77 | |
| 78 | int compat_get_timespec(struct timespec *ts, const void __user *uts) |
| 79 | { |
| 80 | if (COMPAT_USE_64BIT_TIME) |
Fabian Frederick | 6516a46 | 2014-06-04 16:12:02 -0700 | [diff] [blame] | 81 | return copy_from_user(ts, uts, sizeof(*ts)) ? -EFAULT : 0; |
H. Peter Anvin | 6684ba2 | 2012-02-19 17:38:00 -0800 | [diff] [blame] | 82 | else |
H. Peter Anvin | 81993e8 | 2014-02-01 18:54:11 -0800 | [diff] [blame] | 83 | return __compat_get_timespec(ts, uts); |
H. Peter Anvin | 6684ba2 | 2012-02-19 17:38:00 -0800 | [diff] [blame] | 84 | } |
| 85 | EXPORT_SYMBOL_GPL(compat_get_timespec); |
| 86 | |
| 87 | int compat_put_timespec(const struct timespec *ts, void __user *uts) |
| 88 | { |
| 89 | if (COMPAT_USE_64BIT_TIME) |
Fabian Frederick | 6516a46 | 2014-06-04 16:12:02 -0700 | [diff] [blame] | 90 | return copy_to_user(uts, ts, sizeof(*ts)) ? -EFAULT : 0; |
H. Peter Anvin | 6684ba2 | 2012-02-19 17:38:00 -0800 | [diff] [blame] | 91 | else |
H. Peter Anvin | 81993e8 | 2014-02-01 18:54:11 -0800 | [diff] [blame] | 92 | return __compat_put_timespec(ts, uts); |
H. Peter Anvin | 6684ba2 | 2012-02-19 17:38:00 -0800 | [diff] [blame] | 93 | } |
| 94 | EXPORT_SYMBOL_GPL(compat_put_timespec); |
| 95 | |
Al Viro | 54ad9c4 | 2017-06-07 09:42:37 +0100 | [diff] [blame] | 96 | int get_compat_itimerval(struct itimerval *o, const struct compat_itimerval __user *i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
Al Viro | 54ad9c4 | 2017-06-07 09:42:37 +0100 | [diff] [blame] | 98 | struct compat_itimerval v32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Al Viro | 54ad9c4 | 2017-06-07 09:42:37 +0100 | [diff] [blame] | 100 | if (copy_from_user(&v32, i, sizeof(struct compat_itimerval))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | return -EFAULT; |
Al Viro | 54ad9c4 | 2017-06-07 09:42:37 +0100 | [diff] [blame] | 102 | o->it_interval.tv_sec = v32.it_interval.tv_sec; |
| 103 | o->it_interval.tv_usec = v32.it_interval.tv_usec; |
| 104 | o->it_value.tv_sec = v32.it_value.tv_sec; |
| 105 | o->it_value.tv_usec = v32.it_value.tv_usec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
Al Viro | 54ad9c4 | 2017-06-07 09:42:37 +0100 | [diff] [blame] | 109 | int put_compat_itimerval(struct compat_itimerval __user *o, const struct itimerval *i) |
| 110 | { |
| 111 | struct compat_itimerval v32; |
| 112 | |
| 113 | v32.it_interval.tv_sec = i->it_interval.tv_sec; |
| 114 | v32.it_interval.tv_usec = i->it_interval.tv_usec; |
| 115 | v32.it_value.tv_sec = i->it_value.tv_sec; |
| 116 | v32.it_value.tv_usec = i->it_value.tv_usec; |
| 117 | return copy_to_user(o, &v32, sizeof(struct compat_itimerval)) ? -EFAULT : 0; |
| 118 | } |
| 119 | |
Chris Metcalf | be84cb4 | 2011-05-09 13:12:30 -0400 | [diff] [blame] | 120 | #ifdef __ARCH_WANT_SYS_SIGPROCMASK |
| 121 | |
Jan Kiszka | b7dafa0 | 2012-05-10 10:04:36 -0300 | [diff] [blame] | 122 | /* |
| 123 | * sys_sigprocmask SIG_SETMASK sets the first (compat) word of the |
| 124 | * blocked set of signals to the supplied signal set |
| 125 | */ |
| 126 | static inline void compat_sig_setmask(sigset_t *blocked, compat_sigset_word set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | { |
Jan Kiszka | b7dafa0 | 2012-05-10 10:04:36 -0300 | [diff] [blame] | 128 | memcpy(blocked->sig, &set, sizeof(set)); |
| 129 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Al Viro | 5cf2210 | 2012-11-25 19:41:01 -0500 | [diff] [blame] | 131 | COMPAT_SYSCALL_DEFINE3(sigprocmask, int, how, |
| 132 | compat_old_sigset_t __user *, nset, |
| 133 | compat_old_sigset_t __user *, oset) |
Jan Kiszka | b7dafa0 | 2012-05-10 10:04:36 -0300 | [diff] [blame] | 134 | { |
| 135 | old_sigset_t old_set, new_set; |
| 136 | sigset_t new_blocked; |
| 137 | |
| 138 | old_set = current->blocked.sig[0]; |
| 139 | |
| 140 | if (nset) { |
| 141 | if (get_user(new_set, nset)) |
| 142 | return -EFAULT; |
| 143 | new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP)); |
| 144 | |
| 145 | new_blocked = current->blocked; |
| 146 | |
| 147 | switch (how) { |
| 148 | case SIG_BLOCK: |
| 149 | sigaddsetmask(&new_blocked, new_set); |
| 150 | break; |
| 151 | case SIG_UNBLOCK: |
| 152 | sigdelsetmask(&new_blocked, new_set); |
| 153 | break; |
| 154 | case SIG_SETMASK: |
| 155 | compat_sig_setmask(&new_blocked, new_set); |
| 156 | break; |
| 157 | default: |
| 158 | return -EINVAL; |
| 159 | } |
| 160 | |
| 161 | set_current_blocked(&new_blocked); |
| 162 | } |
| 163 | |
| 164 | if (oset) { |
| 165 | if (put_user(old_set, oset)) |
| 166 | return -EFAULT; |
| 167 | } |
| 168 | |
| 169 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Chris Metcalf | be84cb4 | 2011-05-09 13:12:30 -0400 | [diff] [blame] | 172 | #endif |
| 173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru) |
| 175 | { |
Al Viro | 7668b67 | 2017-05-31 06:39:31 -0400 | [diff] [blame] | 176 | struct compat_rusage r32; |
| 177 | memset(&r32, 0, sizeof(r32)); |
| 178 | r32.ru_utime.tv_sec = r->ru_utime.tv_sec; |
| 179 | r32.ru_utime.tv_usec = r->ru_utime.tv_usec; |
| 180 | r32.ru_stime.tv_sec = r->ru_stime.tv_sec; |
| 181 | r32.ru_stime.tv_usec = r->ru_stime.tv_usec; |
| 182 | r32.ru_maxrss = r->ru_maxrss; |
| 183 | r32.ru_ixrss = r->ru_ixrss; |
| 184 | r32.ru_idrss = r->ru_idrss; |
| 185 | r32.ru_isrss = r->ru_isrss; |
| 186 | r32.ru_minflt = r->ru_minflt; |
| 187 | r32.ru_majflt = r->ru_majflt; |
| 188 | r32.ru_nswap = r->ru_nswap; |
| 189 | r32.ru_inblock = r->ru_inblock; |
| 190 | r32.ru_oublock = r->ru_oublock; |
| 191 | r32.ru_msgsnd = r->ru_msgsnd; |
| 192 | r32.ru_msgrcv = r->ru_msgrcv; |
| 193 | r32.ru_nsignals = r->ru_nsignals; |
| 194 | r32.ru_nvcsw = r->ru_nvcsw; |
| 195 | r32.ru_nivcsw = r->ru_nivcsw; |
| 196 | if (copy_to_user(ru, &r32, sizeof(r32))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | return -EFAULT; |
| 198 | return 0; |
| 199 | } |
| 200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr, |
Rusty Russell | a45185d | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 202 | unsigned len, struct cpumask *new_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
| 204 | unsigned long *k; |
| 205 | |
Rusty Russell | a45185d | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 206 | if (len < cpumask_size()) |
| 207 | memset(new_mask, 0, cpumask_size()); |
| 208 | else if (len > cpumask_size()) |
| 209 | len = cpumask_size(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
Rusty Russell | a45185d | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 211 | k = cpumask_bits(new_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | return compat_get_bitmap(k, user_mask_ptr, len * 8); |
| 213 | } |
| 214 | |
Heiko Carstens | 62a6fa9 | 2014-03-03 16:11:13 +0100 | [diff] [blame] | 215 | COMPAT_SYSCALL_DEFINE3(sched_setaffinity, compat_pid_t, pid, |
| 216 | unsigned int, len, |
| 217 | compat_ulong_t __user *, user_mask_ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
Rusty Russell | a45185d | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 219 | cpumask_var_t new_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | int retval; |
| 221 | |
Rusty Russell | a45185d | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 222 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) |
| 223 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
Rusty Russell | a45185d | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 225 | retval = compat_get_user_cpu_mask(user_mask_ptr, len, new_mask); |
| 226 | if (retval) |
| 227 | goto out; |
| 228 | |
| 229 | retval = sched_setaffinity(pid, new_mask); |
| 230 | out: |
| 231 | free_cpumask_var(new_mask); |
| 232 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Heiko Carstens | 62a6fa9 | 2014-03-03 16:11:13 +0100 | [diff] [blame] | 235 | COMPAT_SYSCALL_DEFINE3(sched_getaffinity, compat_pid_t, pid, unsigned int, len, |
| 236 | compat_ulong_t __user *, user_mask_ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | { |
| 238 | int ret; |
Rusty Russell | a45185d | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 239 | cpumask_var_t mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
KOSAKI Motohiro | fa9dc26 | 2010-05-19 09:37:41 +0900 | [diff] [blame] | 241 | if ((len * BITS_PER_BYTE) < nr_cpu_ids) |
| 242 | return -EINVAL; |
| 243 | if (len & (sizeof(compat_ulong_t)-1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | return -EINVAL; |
| 245 | |
Rusty Russell | a45185d | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 246 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) |
| 247 | return -ENOMEM; |
| 248 | |
| 249 | ret = sched_getaffinity(pid, mask); |
KOSAKI Motohiro | fa9dc26 | 2010-05-19 09:37:41 +0900 | [diff] [blame] | 250 | if (ret == 0) { |
Alexey Dobriyan | 4de373a | 2018-02-06 15:39:37 -0800 | [diff] [blame] | 251 | unsigned int retlen = min(len, cpumask_size()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
KOSAKI Motohiro | fa9dc26 | 2010-05-19 09:37:41 +0900 | [diff] [blame] | 253 | if (compat_put_bitmap(user_mask_ptr, cpumask_bits(mask), retlen * 8)) |
| 254 | ret = -EFAULT; |
| 255 | else |
| 256 | ret = retlen; |
| 257 | } |
Rusty Russell | a45185d | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 258 | free_cpumask_var(mask); |
KOSAKI Motohiro | fa9dc26 | 2010-05-19 09:37:41 +0900 | [diff] [blame] | 259 | |
Rusty Russell | a45185d | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 260 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | /* |
| 264 | * We currently only need the following fields from the sigevent |
| 265 | * structure: sigev_value, sigev_signo, sig_notify and (sometimes |
| 266 | * sigev_notify_thread_id). The others are handled in user mode. |
| 267 | * We also assume that copying sigev_value.sival_int is sufficient |
| 268 | * to keep all the bits of sigev_value.sival_ptr intact. |
| 269 | */ |
| 270 | int get_compat_sigevent(struct sigevent *event, |
| 271 | const struct compat_sigevent __user *u_event) |
| 272 | { |
David S. Miller | 51410d3 | 2005-04-16 15:24:01 -0700 | [diff] [blame] | 273 | memset(event, 0, sizeof(*event)); |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 274 | return (!access_ok(u_event, sizeof(*u_event)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | __get_user(event->sigev_value.sival_int, |
| 276 | &u_event->sigev_value.sival_int) || |
| 277 | __get_user(event->sigev_signo, &u_event->sigev_signo) || |
| 278 | __get_user(event->sigev_notify, &u_event->sigev_notify) || |
| 279 | __get_user(event->sigev_notify_thread_id, |
| 280 | &u_event->sigev_notify_thread_id)) |
| 281 | ? -EFAULT : 0; |
| 282 | } |
| 283 | |
Stephen Rothwell | 5fa3839 | 2006-10-28 10:38:46 -0700 | [diff] [blame] | 284 | long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | unsigned long bitmap_size) |
| 286 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | unsigned long nr_compat_longs; |
| 288 | |
| 289 | /* align bitmap up to nearest compat_long_t boundary */ |
| 290 | bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG); |
Al Viro | 1e1fc13 | 2017-05-30 00:29:38 -0400 | [diff] [blame] | 291 | nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
Linus Torvalds | 594cc25 | 2019-01-04 12:56:09 -0800 | [diff] [blame] | 293 | if (!user_access_begin(umask, bitmap_size / 8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | return -EFAULT; |
| 295 | |
Al Viro | 1e1fc13 | 2017-05-30 00:29:38 -0400 | [diff] [blame] | 296 | while (nr_compat_longs > 1) { |
| 297 | compat_ulong_t l1, l2; |
| 298 | unsafe_get_user(l1, umask++, Efault); |
| 299 | unsafe_get_user(l2, umask++, Efault); |
| 300 | *mask++ = ((unsigned long)l2 << BITS_PER_COMPAT_LONG) | l1; |
| 301 | nr_compat_longs -= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | } |
Al Viro | 1e1fc13 | 2017-05-30 00:29:38 -0400 | [diff] [blame] | 303 | if (nr_compat_longs) |
| 304 | unsafe_get_user(*mask, umask++, Efault); |
| 305 | user_access_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | return 0; |
Al Viro | 1e1fc13 | 2017-05-30 00:29:38 -0400 | [diff] [blame] | 307 | |
| 308 | Efault: |
| 309 | user_access_end(); |
| 310 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask, |
| 314 | unsigned long bitmap_size) |
| 315 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | unsigned long nr_compat_longs; |
| 317 | |
| 318 | /* align bitmap up to nearest compat_long_t boundary */ |
| 319 | bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG); |
Al Viro | 1e1fc13 | 2017-05-30 00:29:38 -0400 | [diff] [blame] | 320 | nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
Linus Torvalds | 594cc25 | 2019-01-04 12:56:09 -0800 | [diff] [blame] | 322 | if (!user_access_begin(umask, bitmap_size / 8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | return -EFAULT; |
| 324 | |
Al Viro | 1e1fc13 | 2017-05-30 00:29:38 -0400 | [diff] [blame] | 325 | while (nr_compat_longs > 1) { |
| 326 | unsigned long m = *mask++; |
| 327 | unsafe_put_user((compat_ulong_t)m, umask++, Efault); |
| 328 | unsafe_put_user(m >> BITS_PER_COMPAT_LONG, umask++, Efault); |
| 329 | nr_compat_longs -= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
Al Viro | 1e1fc13 | 2017-05-30 00:29:38 -0400 | [diff] [blame] | 331 | if (nr_compat_longs) |
| 332 | unsafe_put_user((compat_ulong_t)*mask, umask++, Efault); |
| 333 | user_access_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | return 0; |
Al Viro | 1e1fc13 | 2017-05-30 00:29:38 -0400 | [diff] [blame] | 335 | Efault: |
| 336 | user_access_end(); |
| 337 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Al Viro | 3968cf6 | 2017-09-03 21:45:17 -0400 | [diff] [blame] | 340 | int |
| 341 | get_compat_sigset(sigset_t *set, const compat_sigset_t __user *compat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | { |
Al Viro | 3968cf6 | 2017-09-03 21:45:17 -0400 | [diff] [blame] | 343 | #ifdef __BIG_ENDIAN |
| 344 | compat_sigset_t v; |
| 345 | if (copy_from_user(&v, compat, sizeof(compat_sigset_t))) |
| 346 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | switch (_NSIG_WORDS) { |
Al Viro | 3968cf6 | 2017-09-03 21:45:17 -0400 | [diff] [blame] | 348 | case 4: set->sig[3] = v.sig[6] | (((long)v.sig[7]) << 32 ); |
Stephen Rothwell | 89963ad | 2019-05-15 15:23:52 +1000 | [diff] [blame] | 349 | /* fall through */ |
Al Viro | 3968cf6 | 2017-09-03 21:45:17 -0400 | [diff] [blame] | 350 | case 3: set->sig[2] = v.sig[4] | (((long)v.sig[5]) << 32 ); |
Stephen Rothwell | 89963ad | 2019-05-15 15:23:52 +1000 | [diff] [blame] | 351 | /* fall through */ |
Al Viro | 3968cf6 | 2017-09-03 21:45:17 -0400 | [diff] [blame] | 352 | case 2: set->sig[1] = v.sig[2] | (((long)v.sig[3]) << 32 ); |
Stephen Rothwell | 89963ad | 2019-05-15 15:23:52 +1000 | [diff] [blame] | 353 | /* fall through */ |
Al Viro | 3968cf6 | 2017-09-03 21:45:17 -0400 | [diff] [blame] | 354 | case 1: set->sig[0] = v.sig[0] | (((long)v.sig[1]) << 32 ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | } |
Al Viro | 3968cf6 | 2017-09-03 21:45:17 -0400 | [diff] [blame] | 356 | #else |
| 357 | if (copy_from_user(set, compat, sizeof(compat_sigset_t))) |
| 358 | return -EFAULT; |
| 359 | #endif |
| 360 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | } |
Al Viro | 3968cf6 | 2017-09-03 21:45:17 -0400 | [diff] [blame] | 362 | EXPORT_SYMBOL_GPL(get_compat_sigset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
H. Peter Anvin | c41d68a | 2010-09-07 16:16:18 -0700 | [diff] [blame] | 364 | /* |
| 365 | * Allocate user-space memory for the duration of a single system call, |
| 366 | * in order to marshall parameters inside a compat thunk. |
| 367 | */ |
| 368 | void __user *compat_alloc_user_space(unsigned long len) |
| 369 | { |
| 370 | void __user *ptr; |
| 371 | |
| 372 | /* If len would occupy more than half of the entire compat space... */ |
| 373 | if (unlikely(len > (((compat_uptr_t)~0) >> 1))) |
| 374 | return NULL; |
| 375 | |
| 376 | ptr = arch_compat_alloc_user_space(len); |
| 377 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 378 | if (unlikely(!access_ok(ptr, len))) |
H. Peter Anvin | c41d68a | 2010-09-07 16:16:18 -0700 | [diff] [blame] | 379 | return NULL; |
| 380 | |
| 381 | return ptr; |
| 382 | } |
| 383 | EXPORT_SYMBOL_GPL(compat_alloc_user_space); |