blob: b5f7063c0db60dd192ca1f882e492ff16a7477f1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/syscalls.h>
21#include <linux/unistd.h>
22#include <linux/security.h>
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040023#include <linux/export.h>
Christoph Lameter1b2db9f2006-06-23 02:03:56 -070024#include <linux/migrate.h>
Toyo Abe1711ef32006-09-29 02:00:28 -070025#include <linux/posix-timers.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070026#include <linux/times.h>
Paul Mackerrase3d5a272009-01-06 14:41:02 -080027#include <linux/ptrace.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080030#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +020032static int __compat_get_timeval(struct timeval *tv, const struct old_timeval32 __user *ctv)
H. Peter Anvin6684ba22012-02-19 17:38:00 -080033{
Linus Torvalds96d4f262019-01-03 18:57:57 -080034 return (!access_ok(ctv, sizeof(*ctv)) ||
H. Peter Anvin6684ba22012-02-19 17:38:00 -080035 __get_user(tv->tv_sec, &ctv->tv_sec) ||
36 __get_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0;
37}
H. Peter Anvin6684ba22012-02-19 17:38:00 -080038
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +020039static int __compat_put_timeval(const struct timeval *tv, struct old_timeval32 __user *ctv)
H. Peter Anvin6684ba22012-02-19 17:38:00 -080040{
Linus Torvalds96d4f262019-01-03 18:57:57 -080041 return (!access_ok(ctv, sizeof(*ctv)) ||
H. Peter Anvin6684ba22012-02-19 17:38:00 -080042 __put_user(tv->tv_sec, &ctv->tv_sec) ||
43 __put_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0;
44}
H. Peter Anvin6684ba22012-02-19 17:38:00 -080045
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +020046static int __compat_get_timespec(struct timespec *ts, const struct old_timespec32 __user *cts)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Linus Torvalds96d4f262019-01-03 18:57:57 -080048 return (!access_ok(cts, sizeof(*cts)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 __get_user(ts->tv_sec, &cts->tv_sec) ||
50 __get_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
51}
52
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +020053static int __compat_put_timespec(const struct timespec *ts, struct old_timespec32 __user *cts)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Linus Torvalds96d4f262019-01-03 18:57:57 -080055 return (!access_ok(cts, sizeof(*cts)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 __put_user(ts->tv_sec, &cts->tv_sec) ||
57 __put_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
58}
59
H. Peter Anvin6684ba22012-02-19 17:38:00 -080060int compat_get_timeval(struct timeval *tv, const void __user *utv)
61{
62 if (COMPAT_USE_64BIT_TIME)
Fabian Frederick6516a462014-06-04 16:12:02 -070063 return copy_from_user(tv, utv, sizeof(*tv)) ? -EFAULT : 0;
H. Peter Anvin6684ba22012-02-19 17:38:00 -080064 else
H. Peter Anvin81993e82014-02-01 18:54:11 -080065 return __compat_get_timeval(tv, utv);
H. Peter Anvin6684ba22012-02-19 17:38:00 -080066}
67EXPORT_SYMBOL_GPL(compat_get_timeval);
68
69int compat_put_timeval(const struct timeval *tv, void __user *utv)
70{
71 if (COMPAT_USE_64BIT_TIME)
Fabian Frederick6516a462014-06-04 16:12:02 -070072 return copy_to_user(utv, tv, sizeof(*tv)) ? -EFAULT : 0;
H. Peter Anvin6684ba22012-02-19 17:38:00 -080073 else
H. Peter Anvin81993e82014-02-01 18:54:11 -080074 return __compat_put_timeval(tv, utv);
H. Peter Anvin6684ba22012-02-19 17:38:00 -080075}
76EXPORT_SYMBOL_GPL(compat_put_timeval);
77
78int compat_get_timespec(struct timespec *ts, const void __user *uts)
79{
80 if (COMPAT_USE_64BIT_TIME)
Fabian Frederick6516a462014-06-04 16:12:02 -070081 return copy_from_user(ts, uts, sizeof(*ts)) ? -EFAULT : 0;
H. Peter Anvin6684ba22012-02-19 17:38:00 -080082 else
H. Peter Anvin81993e82014-02-01 18:54:11 -080083 return __compat_get_timespec(ts, uts);
H. Peter Anvin6684ba22012-02-19 17:38:00 -080084}
85EXPORT_SYMBOL_GPL(compat_get_timespec);
86
87int compat_put_timespec(const struct timespec *ts, void __user *uts)
88{
89 if (COMPAT_USE_64BIT_TIME)
Fabian Frederick6516a462014-06-04 16:12:02 -070090 return copy_to_user(uts, ts, sizeof(*ts)) ? -EFAULT : 0;
H. Peter Anvin6684ba22012-02-19 17:38:00 -080091 else
H. Peter Anvin81993e82014-02-01 18:54:11 -080092 return __compat_put_timespec(ts, uts);
H. Peter Anvin6684ba22012-02-19 17:38:00 -080093}
94EXPORT_SYMBOL_GPL(compat_put_timespec);
95
Al Viro54ad9c42017-06-07 09:42:37 +010096int get_compat_itimerval(struct itimerval *o, const struct compat_itimerval __user *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Al Viro54ad9c42017-06-07 09:42:37 +010098 struct compat_itimerval v32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Al Viro54ad9c42017-06-07 09:42:37 +0100100 if (copy_from_user(&v32, i, sizeof(struct compat_itimerval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return -EFAULT;
Al Viro54ad9c42017-06-07 09:42:37 +0100102 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 Torvalds1da177e2005-04-16 15:20:36 -0700106 return 0;
107}
108
Al Viro54ad9c42017-06-07 09:42:37 +0100109int 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 Metcalfbe84cb42011-05-09 13:12:30 -0400120#ifdef __ARCH_WANT_SYS_SIGPROCMASK
121
Jan Kiszkab7dafa02012-05-10 10:04:36 -0300122/*
123 * sys_sigprocmask SIG_SETMASK sets the first (compat) word of the
124 * blocked set of signals to the supplied signal set
125 */
126static inline void compat_sig_setmask(sigset_t *blocked, compat_sigset_word set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Jan Kiszkab7dafa02012-05-10 10:04:36 -0300128 memcpy(blocked->sig, &set, sizeof(set));
129}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Al Viro5cf22102012-11-25 19:41:01 -0500131COMPAT_SYSCALL_DEFINE3(sigprocmask, int, how,
132 compat_old_sigset_t __user *, nset,
133 compat_old_sigset_t __user *, oset)
Jan Kiszkab7dafa02012-05-10 10:04:36 -0300134{
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 Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Chris Metcalfbe84cb42011-05-09 13:12:30 -0400172#endif
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru)
175{
Al Viro7668b672017-05-31 06:39:31 -0400176 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 Torvalds1da177e2005-04-16 15:20:36 -0700197 return -EFAULT;
198 return 0;
199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr,
Rusty Russella45185d2009-01-01 10:12:24 +1030202 unsigned len, struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 unsigned long *k;
205
Rusty Russella45185d2009-01-01 10:12:24 +1030206 if (len < cpumask_size())
207 memset(new_mask, 0, cpumask_size());
208 else if (len > cpumask_size())
209 len = cpumask_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Rusty Russella45185d2009-01-01 10:12:24 +1030211 k = cpumask_bits(new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return compat_get_bitmap(k, user_mask_ptr, len * 8);
213}
214
Heiko Carstens62a6fa92014-03-03 16:11:13 +0100215COMPAT_SYSCALL_DEFINE3(sched_setaffinity, compat_pid_t, pid,
216 unsigned int, len,
217 compat_ulong_t __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Rusty Russella45185d2009-01-01 10:12:24 +1030219 cpumask_var_t new_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 int retval;
221
Rusty Russella45185d2009-01-01 10:12:24 +1030222 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
223 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Rusty Russella45185d2009-01-01 10:12:24 +1030225 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);
230out:
231 free_cpumask_var(new_mask);
232 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Heiko Carstens62a6fa92014-03-03 16:11:13 +0100235COMPAT_SYSCALL_DEFINE3(sched_getaffinity, compat_pid_t, pid, unsigned int, len,
236 compat_ulong_t __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 int ret;
Rusty Russella45185d2009-01-01 10:12:24 +1030239 cpumask_var_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
KOSAKI Motohirofa9dc262010-05-19 09:37:41 +0900241 if ((len * BITS_PER_BYTE) < nr_cpu_ids)
242 return -EINVAL;
243 if (len & (sizeof(compat_ulong_t)-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return -EINVAL;
245
Rusty Russella45185d2009-01-01 10:12:24 +1030246 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
247 return -ENOMEM;
248
249 ret = sched_getaffinity(pid, mask);
KOSAKI Motohirofa9dc262010-05-19 09:37:41 +0900250 if (ret == 0) {
Alexey Dobriyan4de373a2018-02-06 15:39:37 -0800251 unsigned int retlen = min(len, cpumask_size());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
KOSAKI Motohirofa9dc262010-05-19 09:37:41 +0900253 if (compat_put_bitmap(user_mask_ptr, cpumask_bits(mask), retlen * 8))
254 ret = -EFAULT;
255 else
256 ret = retlen;
257 }
Rusty Russella45185d2009-01-01 10:12:24 +1030258 free_cpumask_var(mask);
KOSAKI Motohirofa9dc262010-05-19 09:37:41 +0900259
Rusty Russella45185d2009-01-01 10:12:24 +1030260 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263/*
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 */
270int get_compat_sigevent(struct sigevent *event,
271 const struct compat_sigevent __user *u_event)
272{
David S. Miller51410d32005-04-16 15:24:01 -0700273 memset(event, 0, sizeof(*event));
Linus Torvalds96d4f262019-01-03 18:57:57 -0800274 return (!access_ok(u_event, sizeof(*u_event)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 __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 Rothwell5fa38392006-10-28 10:38:46 -0700284long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 unsigned long bitmap_size)
286{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 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 Viro1e1fc132017-05-30 00:29:38 -0400291 nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Linus Torvalds594cc252019-01-04 12:56:09 -0800293 if (!user_access_begin(umask, bitmap_size / 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return -EFAULT;
295
Al Viro1e1fc132017-05-30 00:29:38 -0400296 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 Torvalds1da177e2005-04-16 15:20:36 -0700302 }
Al Viro1e1fc132017-05-30 00:29:38 -0400303 if (nr_compat_longs)
304 unsafe_get_user(*mask, umask++, Efault);
305 user_access_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return 0;
Al Viro1e1fc132017-05-30 00:29:38 -0400307
308Efault:
309 user_access_end();
310 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
314 unsigned long bitmap_size)
315{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 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 Viro1e1fc132017-05-30 00:29:38 -0400320 nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Linus Torvalds594cc252019-01-04 12:56:09 -0800322 if (!user_access_begin(umask, bitmap_size / 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return -EFAULT;
324
Al Viro1e1fc132017-05-30 00:29:38 -0400325 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 Torvalds1da177e2005-04-16 15:20:36 -0700330 }
Al Viro1e1fc132017-05-30 00:29:38 -0400331 if (nr_compat_longs)
332 unsafe_put_user((compat_ulong_t)*mask, umask++, Efault);
333 user_access_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return 0;
Al Viro1e1fc132017-05-30 00:29:38 -0400335Efault:
336 user_access_end();
337 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Al Viro3968cf62017-09-03 21:45:17 -0400340int
341get_compat_sigset(sigset_t *set, const compat_sigset_t __user *compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Al Viro3968cf62017-09-03 21:45:17 -0400343#ifdef __BIG_ENDIAN
344 compat_sigset_t v;
345 if (copy_from_user(&v, compat, sizeof(compat_sigset_t)))
346 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 switch (_NSIG_WORDS) {
Al Viro3968cf62017-09-03 21:45:17 -0400348 case 4: set->sig[3] = v.sig[6] | (((long)v.sig[7]) << 32 );
Stephen Rothwell89963ad2019-05-15 15:23:52 +1000349 /* fall through */
Al Viro3968cf62017-09-03 21:45:17 -0400350 case 3: set->sig[2] = v.sig[4] | (((long)v.sig[5]) << 32 );
Stephen Rothwell89963ad2019-05-15 15:23:52 +1000351 /* fall through */
Al Viro3968cf62017-09-03 21:45:17 -0400352 case 2: set->sig[1] = v.sig[2] | (((long)v.sig[3]) << 32 );
Stephen Rothwell89963ad2019-05-15 15:23:52 +1000353 /* fall through */
Al Viro3968cf62017-09-03 21:45:17 -0400354 case 1: set->sig[0] = v.sig[0] | (((long)v.sig[1]) << 32 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
Al Viro3968cf62017-09-03 21:45:17 -0400356#else
357 if (copy_from_user(set, compat, sizeof(compat_sigset_t)))
358 return -EFAULT;
359#endif
360 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
Al Viro3968cf62017-09-03 21:45:17 -0400362EXPORT_SYMBOL_GPL(get_compat_sigset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
H. Peter Anvinc41d68a2010-09-07 16:16:18 -0700364/*
365 * Allocate user-space memory for the duration of a single system call,
366 * in order to marshall parameters inside a compat thunk.
367 */
368void __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 Torvalds96d4f262019-01-03 18:57:57 -0800378 if (unlikely(!access_ok(ptr, len)))
H. Peter Anvinc41d68a2010-09-07 16:16:18 -0700379 return NULL;
380
381 return ptr;
382}
383EXPORT_SYMBOL_GPL(compat_alloc_user_space);