blob: 9c2a8f3788d5e13e1de588c57040b4348419f05d [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>
Stephen Rothwell3158e942006-03-26 01:37:29 -080023#include <linux/timex.h>
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040024#include <linux/export.h>
Christoph Lameter1b2db9f2006-06-23 02:03:56 -070025#include <linux/migrate.h>
Toyo Abe1711ef32006-09-29 02:00:28 -070026#include <linux/posix-timers.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070027#include <linux/times.h>
Paul Mackerrase3d5a272009-01-06 14:41:02 -080028#include <linux/ptrace.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080031#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Richard Cochran65f5d802011-02-01 13:52:23 +000033static int compat_get_timex(struct timex *txc, struct compat_timex __user *utp)
34{
35 memset(txc, 0, sizeof(struct timex));
36
37 if (!access_ok(VERIFY_READ, utp, sizeof(struct compat_timex)) ||
38 __get_user(txc->modes, &utp->modes) ||
39 __get_user(txc->offset, &utp->offset) ||
40 __get_user(txc->freq, &utp->freq) ||
41 __get_user(txc->maxerror, &utp->maxerror) ||
42 __get_user(txc->esterror, &utp->esterror) ||
43 __get_user(txc->status, &utp->status) ||
44 __get_user(txc->constant, &utp->constant) ||
45 __get_user(txc->precision, &utp->precision) ||
46 __get_user(txc->tolerance, &utp->tolerance) ||
47 __get_user(txc->time.tv_sec, &utp->time.tv_sec) ||
48 __get_user(txc->time.tv_usec, &utp->time.tv_usec) ||
49 __get_user(txc->tick, &utp->tick) ||
50 __get_user(txc->ppsfreq, &utp->ppsfreq) ||
51 __get_user(txc->jitter, &utp->jitter) ||
52 __get_user(txc->shift, &utp->shift) ||
53 __get_user(txc->stabil, &utp->stabil) ||
54 __get_user(txc->jitcnt, &utp->jitcnt) ||
55 __get_user(txc->calcnt, &utp->calcnt) ||
56 __get_user(txc->errcnt, &utp->errcnt) ||
57 __get_user(txc->stbcnt, &utp->stbcnt))
58 return -EFAULT;
59
60 return 0;
61}
62
63static int compat_put_timex(struct compat_timex __user *utp, struct timex *txc)
64{
65 if (!access_ok(VERIFY_WRITE, utp, sizeof(struct compat_timex)) ||
66 __put_user(txc->modes, &utp->modes) ||
67 __put_user(txc->offset, &utp->offset) ||
68 __put_user(txc->freq, &utp->freq) ||
69 __put_user(txc->maxerror, &utp->maxerror) ||
70 __put_user(txc->esterror, &utp->esterror) ||
71 __put_user(txc->status, &utp->status) ||
72 __put_user(txc->constant, &utp->constant) ||
73 __put_user(txc->precision, &utp->precision) ||
74 __put_user(txc->tolerance, &utp->tolerance) ||
75 __put_user(txc->time.tv_sec, &utp->time.tv_sec) ||
76 __put_user(txc->time.tv_usec, &utp->time.tv_usec) ||
77 __put_user(txc->tick, &utp->tick) ||
78 __put_user(txc->ppsfreq, &utp->ppsfreq) ||
79 __put_user(txc->jitter, &utp->jitter) ||
80 __put_user(txc->shift, &utp->shift) ||
81 __put_user(txc->stabil, &utp->stabil) ||
82 __put_user(txc->jitcnt, &utp->jitcnt) ||
83 __put_user(txc->calcnt, &utp->calcnt) ||
84 __put_user(txc->errcnt, &utp->errcnt) ||
85 __put_user(txc->stbcnt, &utp->stbcnt) ||
86 __put_user(txc->tai, &utp->tai))
87 return -EFAULT;
88 return 0;
89}
90
Heiko Carstens62a6fa92014-03-03 16:11:13 +010091COMPAT_SYSCALL_DEFINE2(gettimeofday, struct compat_timeval __user *, tv,
92 struct timezone __user *, tz)
Christoph Hellwigb418da12008-10-15 22:02:06 -070093{
94 if (tv) {
95 struct timeval ktv;
96 do_gettimeofday(&ktv);
H. Peter Anvin81993e82014-02-01 18:54:11 -080097 if (compat_put_timeval(&ktv, tv))
Christoph Hellwigb418da12008-10-15 22:02:06 -070098 return -EFAULT;
99 }
100 if (tz) {
101 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
102 return -EFAULT;
103 }
104
105 return 0;
106}
107
Heiko Carstens62a6fa92014-03-03 16:11:13 +0100108COMPAT_SYSCALL_DEFINE2(settimeofday, struct compat_timeval __user *, tv,
109 struct timezone __user *, tz)
Christoph Hellwigb418da12008-10-15 22:02:06 -0700110{
Deepa Dinamani2ac00f12017-03-26 12:04:12 -0700111 struct timespec64 new_ts;
H. Peter Anvin81993e82014-02-01 18:54:11 -0800112 struct timeval user_tv;
H. Peter Anvin81993e82014-02-01 18:54:11 -0800113 struct timezone new_tz;
Christoph Hellwigb418da12008-10-15 22:02:06 -0700114
115 if (tv) {
H. Peter Anvin81993e82014-02-01 18:54:11 -0800116 if (compat_get_timeval(&user_tv, tv))
Christoph Hellwigb418da12008-10-15 22:02:06 -0700117 return -EFAULT;
H. Peter Anvin81993e82014-02-01 18:54:11 -0800118 new_ts.tv_sec = user_tv.tv_sec;
119 new_ts.tv_nsec = user_tv.tv_usec * NSEC_PER_USEC;
Christoph Hellwigb418da12008-10-15 22:02:06 -0700120 }
121 if (tz) {
H. Peter Anvin81993e82014-02-01 18:54:11 -0800122 if (copy_from_user(&new_tz, tz, sizeof(*tz)))
Christoph Hellwigb418da12008-10-15 22:02:06 -0700123 return -EFAULT;
124 }
125
Deepa Dinamani2ac00f12017-03-26 12:04:12 -0700126 return do_sys_settimeofday64(tv ? &new_ts : NULL, tz ? &new_tz : NULL);
Christoph Hellwigb418da12008-10-15 22:02:06 -0700127}
128
H. Peter Anvin81993e82014-02-01 18:54:11 -0800129static int __compat_get_timeval(struct timeval *tv, const struct compat_timeval __user *ctv)
H. Peter Anvin6684ba22012-02-19 17:38:00 -0800130{
131 return (!access_ok(VERIFY_READ, ctv, sizeof(*ctv)) ||
132 __get_user(tv->tv_sec, &ctv->tv_sec) ||
133 __get_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0;
134}
H. Peter Anvin6684ba22012-02-19 17:38:00 -0800135
H. Peter Anvin81993e82014-02-01 18:54:11 -0800136static int __compat_put_timeval(const struct timeval *tv, struct compat_timeval __user *ctv)
H. Peter Anvin6684ba22012-02-19 17:38:00 -0800137{
138 return (!access_ok(VERIFY_WRITE, ctv, sizeof(*ctv)) ||
139 __put_user(tv->tv_sec, &ctv->tv_sec) ||
140 __put_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0;
141}
H. Peter Anvin6684ba22012-02-19 17:38:00 -0800142
H. Peter Anvin81993e82014-02-01 18:54:11 -0800143static int __compat_get_timespec(struct timespec *ts, const struct compat_timespec __user *cts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 return (!access_ok(VERIFY_READ, cts, sizeof(*cts)) ||
146 __get_user(ts->tv_sec, &cts->tv_sec) ||
147 __get_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
148}
149
H. Peter Anvin81993e82014-02-01 18:54:11 -0800150static int __compat_put_timespec(const struct timespec *ts, struct compat_timespec __user *cts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 return (!access_ok(VERIFY_WRITE, cts, sizeof(*cts)) ||
153 __put_user(ts->tv_sec, &cts->tv_sec) ||
154 __put_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
155}
156
H. Peter Anvin6684ba22012-02-19 17:38:00 -0800157int compat_get_timeval(struct timeval *tv, const void __user *utv)
158{
159 if (COMPAT_USE_64BIT_TIME)
Fabian Frederick6516a462014-06-04 16:12:02 -0700160 return copy_from_user(tv, utv, sizeof(*tv)) ? -EFAULT : 0;
H. Peter Anvin6684ba22012-02-19 17:38:00 -0800161 else
H. Peter Anvin81993e82014-02-01 18:54:11 -0800162 return __compat_get_timeval(tv, utv);
H. Peter Anvin6684ba22012-02-19 17:38:00 -0800163}
164EXPORT_SYMBOL_GPL(compat_get_timeval);
165
166int compat_put_timeval(const struct timeval *tv, void __user *utv)
167{
168 if (COMPAT_USE_64BIT_TIME)
Fabian Frederick6516a462014-06-04 16:12:02 -0700169 return copy_to_user(utv, tv, sizeof(*tv)) ? -EFAULT : 0;
H. Peter Anvin6684ba22012-02-19 17:38:00 -0800170 else
H. Peter Anvin81993e82014-02-01 18:54:11 -0800171 return __compat_put_timeval(tv, utv);
H. Peter Anvin6684ba22012-02-19 17:38:00 -0800172}
173EXPORT_SYMBOL_GPL(compat_put_timeval);
174
175int compat_get_timespec(struct timespec *ts, const void __user *uts)
176{
177 if (COMPAT_USE_64BIT_TIME)
Fabian Frederick6516a462014-06-04 16:12:02 -0700178 return copy_from_user(ts, uts, sizeof(*ts)) ? -EFAULT : 0;
H. Peter Anvin6684ba22012-02-19 17:38:00 -0800179 else
H. Peter Anvin81993e82014-02-01 18:54:11 -0800180 return __compat_get_timespec(ts, uts);
H. Peter Anvin6684ba22012-02-19 17:38:00 -0800181}
182EXPORT_SYMBOL_GPL(compat_get_timespec);
183
184int compat_put_timespec(const struct timespec *ts, void __user *uts)
185{
186 if (COMPAT_USE_64BIT_TIME)
Fabian Frederick6516a462014-06-04 16:12:02 -0700187 return copy_to_user(uts, ts, sizeof(*ts)) ? -EFAULT : 0;
H. Peter Anvin6684ba22012-02-19 17:38:00 -0800188 else
H. Peter Anvin81993e82014-02-01 18:54:11 -0800189 return __compat_put_timespec(ts, uts);
H. Peter Anvin6684ba22012-02-19 17:38:00 -0800190}
191EXPORT_SYMBOL_GPL(compat_put_timespec);
192
H. Peter Anvin81993e82014-02-01 18:54:11 -0800193int compat_convert_timespec(struct timespec __user **kts,
194 const void __user *cts)
195{
196 struct timespec ts;
197 struct timespec __user *uts;
198
199 if (!cts || COMPAT_USE_64BIT_TIME) {
200 *kts = (struct timespec __user *)cts;
201 return 0;
202 }
203
204 uts = compat_alloc_user_space(sizeof(ts));
205 if (!uts)
206 return -EFAULT;
207 if (compat_get_timespec(&ts, cts))
208 return -EFAULT;
209 if (copy_to_user(uts, &ts, sizeof(ts)))
210 return -EFAULT;
211
212 *kts = uts;
213 return 0;
214}
215
Oleg Nesterov41652932008-02-01 20:35:31 +0300216static long compat_nanosleep_restart(struct restart_block *restart)
217{
218 struct compat_timespec __user *rmtp;
219 struct timespec rmt;
220 mm_segment_t oldfs;
221 long ret;
222
Thomas Gleixner029a07e2008-02-10 09:17:43 +0100223 restart->nanosleep.rmtp = (struct timespec __user *) &rmt;
Oleg Nesterov41652932008-02-01 20:35:31 +0300224 oldfs = get_fs();
225 set_fs(KERNEL_DS);
226 ret = hrtimer_nanosleep_restart(restart);
227 set_fs(oldfs);
228
Thomas Gleixner849151d2014-09-06 12:18:07 +0200229 if (ret == -ERESTART_RESTARTBLOCK) {
Thomas Gleixner029a07e2008-02-10 09:17:43 +0100230 rmtp = restart->nanosleep.compat_rmtp;
Oleg Nesterov41652932008-02-01 20:35:31 +0300231
H. Peter Anvin81993e82014-02-01 18:54:11 -0800232 if (rmtp && compat_put_timespec(&rmt, rmtp))
Oleg Nesterov41652932008-02-01 20:35:31 +0300233 return -EFAULT;
234 }
235
236 return ret;
237}
238
Heiko Carstens62a6fa92014-03-03 16:11:13 +0100239COMPAT_SYSCALL_DEFINE2(nanosleep, struct compat_timespec __user *, rqtp,
240 struct compat_timespec __user *, rmtp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Anton Blanchardc70878b2007-10-15 16:13:56 -0500242 struct timespec tu, rmt;
Deepa Dinamaniad196382017-03-26 12:04:18 -0700243 struct timespec64 tu64;
Oleg Nesterov41652932008-02-01 20:35:31 +0300244 mm_segment_t oldfs;
Anton Blanchardc70878b2007-10-15 16:13:56 -0500245 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
H. Peter Anvin81993e82014-02-01 18:54:11 -0800247 if (compat_get_timespec(&tu, rqtp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return -EFAULT;
249
Deepa Dinamaniad196382017-03-26 12:04:18 -0700250 tu64 = timespec_to_timespec64(tu);
251 if (!timespec64_valid(&tu64))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return -EINVAL;
253
Oleg Nesterov41652932008-02-01 20:35:31 +0300254 oldfs = get_fs();
255 set_fs(KERNEL_DS);
Deepa Dinamaniad196382017-03-26 12:04:18 -0700256 ret = hrtimer_nanosleep(&tu64,
Oleg Nesterov41652932008-02-01 20:35:31 +0300257 rmtp ? (struct timespec __user *)&rmt : NULL,
258 HRTIMER_MODE_REL, CLOCK_MONOTONIC);
259 set_fs(oldfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Thomas Gleixner849151d2014-09-06 12:18:07 +0200261 /*
262 * hrtimer_nanosleep() can only return 0 or
263 * -ERESTART_RESTARTBLOCK here because:
264 *
265 * - we call it with HRTIMER_MODE_REL and therefor exclude the
266 * -ERESTARTNOHAND return path.
267 *
268 * - we supply the rmtp argument from the task stack (due to
269 * the necessary compat conversion. So the update cannot
270 * fail, which excludes the -EFAULT return path as well. If
271 * it fails nevertheless we have a bigger problem and wont
272 * reach this place anymore.
273 *
274 * - if the return value is 0, we do not have to update rmtp
275 * because there is no remaining time.
276 *
277 * We check for -ERESTART_RESTARTBLOCK nevertheless if the
278 * core implementation decides to return random nonsense.
279 */
280 if (ret == -ERESTART_RESTARTBLOCK) {
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800281 struct restart_block *restart = &current->restart_block;
Oleg Nesterov41652932008-02-01 20:35:31 +0300282
283 restart->fn = compat_nanosleep_restart;
Thomas Gleixner029a07e2008-02-10 09:17:43 +0100284 restart->nanosleep.compat_rmtp = rmtp;
Oleg Nesterov41652932008-02-01 20:35:31 +0300285
H. Peter Anvin81993e82014-02-01 18:54:11 -0800286 if (rmtp && compat_put_timespec(&rmt, rmtp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return -EFAULT;
288 }
Anton Blanchardc70878b2007-10-15 16:13:56 -0500289 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
292static inline long get_compat_itimerval(struct itimerval *o,
293 struct compat_itimerval __user *i)
294{
295 return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
296 (__get_user(o->it_interval.tv_sec, &i->it_interval.tv_sec) |
297 __get_user(o->it_interval.tv_usec, &i->it_interval.tv_usec) |
298 __get_user(o->it_value.tv_sec, &i->it_value.tv_sec) |
299 __get_user(o->it_value.tv_usec, &i->it_value.tv_usec)));
300}
301
302static inline long put_compat_itimerval(struct compat_itimerval __user *o,
303 struct itimerval *i)
304{
305 return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
306 (__put_user(i->it_interval.tv_sec, &o->it_interval.tv_sec) |
307 __put_user(i->it_interval.tv_usec, &o->it_interval.tv_usec) |
308 __put_user(i->it_value.tv_sec, &o->it_value.tv_sec) |
309 __put_user(i->it_value.tv_usec, &o->it_value.tv_usec)));
310}
311
Nicolas Pitrebaa73d92016-11-11 00:10:10 -0500312asmlinkage long sys_ni_posix_timers(void);
313
Al Viro37784072012-12-24 17:28:40 -0500314COMPAT_SYSCALL_DEFINE2(getitimer, int, which,
315 struct compat_itimerval __user *, it)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 struct itimerval kit;
318 int error;
319
Nicolas Pitrebaa73d92016-11-11 00:10:10 -0500320 if (!IS_ENABLED(CONFIG_POSIX_TIMERS))
321 return sys_ni_posix_timers();
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 error = do_getitimer(which, &kit);
324 if (!error && put_compat_itimerval(it, &kit))
325 error = -EFAULT;
326 return error;
327}
328
Al Viro37784072012-12-24 17:28:40 -0500329COMPAT_SYSCALL_DEFINE3(setitimer, int, which,
330 struct compat_itimerval __user *, in,
331 struct compat_itimerval __user *, out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 struct itimerval kin, kout;
334 int error;
335
Nicolas Pitrebaa73d92016-11-11 00:10:10 -0500336 if (!IS_ENABLED(CONFIG_POSIX_TIMERS))
337 return sys_ni_posix_timers();
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (in) {
340 if (get_compat_itimerval(&kin, in))
341 return -EFAULT;
342 } else
343 memset(&kin, 0, sizeof(kin));
344
345 error = do_setitimer(which, &kin, out ? &kout : NULL);
346 if (error || !out)
347 return error;
348 if (put_compat_itimerval(out, &kout))
349 return -EFAULT;
350 return 0;
351}
352
Frank Mayharf06febc2008-09-12 09:54:39 -0700353static compat_clock_t clock_t_to_compat_clock_t(clock_t x)
354{
355 return compat_jiffies_to_clock_t(clock_t_to_jiffies(x));
356}
357
Heiko Carstens62a6fa92014-03-03 16:11:13 +0100358COMPAT_SYSCALL_DEFINE1(times, struct compat_tms __user *, tbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (tbuf) {
Frank Mayharf06febc2008-09-12 09:54:39 -0700361 struct tms tms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct compat_tms tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Frank Mayharf06febc2008-09-12 09:54:39 -0700364 do_sys_times(&tms);
365 /* Convert our struct tms to the compat version. */
366 tmp.tms_utime = clock_t_to_compat_clock_t(tms.tms_utime);
367 tmp.tms_stime = clock_t_to_compat_clock_t(tms.tms_stime);
368 tmp.tms_cutime = clock_t_to_compat_clock_t(tms.tms_cutime);
369 tmp.tms_cstime = clock_t_to_compat_clock_t(tms.tms_cstime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
371 return -EFAULT;
372 }
Paul Mackerrase3d5a272009-01-06 14:41:02 -0800373 force_successful_syscall_return();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return compat_jiffies_to_clock_t(jiffies);
375}
376
Chris Metcalfbe84cb42011-05-09 13:12:30 -0400377#ifdef __ARCH_WANT_SYS_SIGPENDING
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379/*
380 * Assumption: old_sigset_t and compat_old_sigset_t are both
381 * types that can be passed to put_user()/get_user().
382 */
383
Heiko Carstens62a6fa92014-03-03 16:11:13 +0100384COMPAT_SYSCALL_DEFINE1(sigpending, compat_old_sigset_t __user *, set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 old_sigset_t s;
387 long ret;
388 mm_segment_t old_fs = get_fs();
389
390 set_fs(KERNEL_DS);
391 ret = sys_sigpending((old_sigset_t __user *) &s);
392 set_fs(old_fs);
393 if (ret == 0)
394 ret = put_user(s, set);
395 return ret;
396}
397
Chris Metcalfbe84cb42011-05-09 13:12:30 -0400398#endif
399
400#ifdef __ARCH_WANT_SYS_SIGPROCMASK
401
Jan Kiszkab7dafa02012-05-10 10:04:36 -0300402/*
403 * sys_sigprocmask SIG_SETMASK sets the first (compat) word of the
404 * blocked set of signals to the supplied signal set
405 */
406static inline void compat_sig_setmask(sigset_t *blocked, compat_sigset_word set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Jan Kiszkab7dafa02012-05-10 10:04:36 -0300408 memcpy(blocked->sig, &set, sizeof(set));
409}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Al Viro5cf22102012-11-25 19:41:01 -0500411COMPAT_SYSCALL_DEFINE3(sigprocmask, int, how,
412 compat_old_sigset_t __user *, nset,
413 compat_old_sigset_t __user *, oset)
Jan Kiszkab7dafa02012-05-10 10:04:36 -0300414{
415 old_sigset_t old_set, new_set;
416 sigset_t new_blocked;
417
418 old_set = current->blocked.sig[0];
419
420 if (nset) {
421 if (get_user(new_set, nset))
422 return -EFAULT;
423 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
424
425 new_blocked = current->blocked;
426
427 switch (how) {
428 case SIG_BLOCK:
429 sigaddsetmask(&new_blocked, new_set);
430 break;
431 case SIG_UNBLOCK:
432 sigdelsetmask(&new_blocked, new_set);
433 break;
434 case SIG_SETMASK:
435 compat_sig_setmask(&new_blocked, new_set);
436 break;
437 default:
438 return -EINVAL;
439 }
440
441 set_current_blocked(&new_blocked);
442 }
443
444 if (oset) {
445 if (put_user(old_set, oset))
446 return -EFAULT;
447 }
448
449 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Chris Metcalfbe84cb42011-05-09 13:12:30 -0400452#endif
453
Heiko Carstens62a6fa92014-03-03 16:11:13 +0100454COMPAT_SYSCALL_DEFINE2(setrlimit, unsigned int, resource,
455 struct compat_rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 struct rlimit r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 if (!access_ok(VERIFY_READ, rlim, sizeof(*rlim)) ||
460 __get_user(r.rlim_cur, &rlim->rlim_cur) ||
461 __get_user(r.rlim_max, &rlim->rlim_max))
462 return -EFAULT;
463
464 if (r.rlim_cur == COMPAT_RLIM_INFINITY)
465 r.rlim_cur = RLIM_INFINITY;
466 if (r.rlim_max == COMPAT_RLIM_INFINITY)
467 r.rlim_max = RLIM_INFINITY;
Jiri Slabyb9518342010-05-04 11:28:25 +0200468 return do_prlimit(current, resource, &r, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Heiko Carstens62a6fa92014-03-03 16:11:13 +0100471COMPAT_SYSCALL_DEFINE2(getrlimit, unsigned int, resource,
472 struct compat_rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 struct rlimit r;
475 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Jiri Slabyb9518342010-05-04 11:28:25 +0200477 ret = do_prlimit(current, resource, NULL, &r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (!ret) {
479 if (r.rlim_cur > COMPAT_RLIM_INFINITY)
480 r.rlim_cur = COMPAT_RLIM_INFINITY;
481 if (r.rlim_max > COMPAT_RLIM_INFINITY)
482 r.rlim_max = COMPAT_RLIM_INFINITY;
483
484 if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
485 __put_user(r.rlim_cur, &rlim->rlim_cur) ||
486 __put_user(r.rlim_max, &rlim->rlim_max))
487 return -EFAULT;
488 }
489 return ret;
490}
491
492int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru)
493{
494 if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)) ||
495 __put_user(r->ru_utime.tv_sec, &ru->ru_utime.tv_sec) ||
496 __put_user(r->ru_utime.tv_usec, &ru->ru_utime.tv_usec) ||
497 __put_user(r->ru_stime.tv_sec, &ru->ru_stime.tv_sec) ||
498 __put_user(r->ru_stime.tv_usec, &ru->ru_stime.tv_usec) ||
499 __put_user(r->ru_maxrss, &ru->ru_maxrss) ||
500 __put_user(r->ru_ixrss, &ru->ru_ixrss) ||
501 __put_user(r->ru_idrss, &ru->ru_idrss) ||
502 __put_user(r->ru_isrss, &ru->ru_isrss) ||
503 __put_user(r->ru_minflt, &ru->ru_minflt) ||
504 __put_user(r->ru_majflt, &ru->ru_majflt) ||
505 __put_user(r->ru_nswap, &ru->ru_nswap) ||
506 __put_user(r->ru_inblock, &ru->ru_inblock) ||
507 __put_user(r->ru_oublock, &ru->ru_oublock) ||
508 __put_user(r->ru_msgsnd, &ru->ru_msgsnd) ||
509 __put_user(r->ru_msgrcv, &ru->ru_msgrcv) ||
510 __put_user(r->ru_nsignals, &ru->ru_nsignals) ||
511 __put_user(r->ru_nvcsw, &ru->ru_nvcsw) ||
512 __put_user(r->ru_nivcsw, &ru->ru_nivcsw))
513 return -EFAULT;
514 return 0;
515}
516
Al Viro8d9807b2012-12-23 14:56:40 -0500517COMPAT_SYSCALL_DEFINE4(wait4,
518 compat_pid_t, pid,
519 compat_uint_t __user *, stat_addr,
520 int, options,
521 struct compat_rusage __user *, ru)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 if (!ru) {
524 return sys_wait4(pid, stat_addr, options, NULL);
525 } else {
526 struct rusage r;
527 int ret;
528 unsigned int status;
529 mm_segment_t old_fs = get_fs();
530
531 set_fs (KERNEL_DS);
532 ret = sys_wait4(pid,
533 (stat_addr ?
534 (unsigned int __user *) &status : NULL),
535 options, (struct rusage __user *) &r);
536 set_fs (old_fs);
537
538 if (ret > 0) {
539 if (put_compat_rusage(&r, ru))
540 return -EFAULT;
541 if (stat_addr && put_user(status, stat_addr))
542 return -EFAULT;
543 }
544 return ret;
545 }
546}
547
Al Viro8d9807b2012-12-23 14:56:40 -0500548COMPAT_SYSCALL_DEFINE5(waitid,
549 int, which, compat_pid_t, pid,
550 struct compat_siginfo __user *, uinfo, int, options,
551 struct compat_rusage __user *, uru)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 siginfo_t info;
554 struct rusage ru;
555 long ret;
556 mm_segment_t old_fs = get_fs();
557
558 memset(&info, 0, sizeof(info));
559
560 set_fs(KERNEL_DS);
561 ret = sys_waitid(which, pid, (siginfo_t __user *)&info, options,
562 uru ? (struct rusage __user *)&ru : NULL);
563 set_fs(old_fs);
564
565 if ((ret < 0) || (info.si_signo == 0))
566 return ret;
567
568 if (uru) {
Al Viroa566c282012-12-23 23:14:49 -0500569 /* sys_waitid() overwrites everything in ru */
570 if (COMPAT_USE_64BIT_TIME)
571 ret = copy_to_user(uru, &ru, sizeof(ru));
572 else
573 ret = put_compat_rusage(&ru, uru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (ret)
Dan Carpenterbffea772013-02-21 16:41:57 -0800575 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577
578 BUG_ON(info.si_code & __SI_MASK);
579 info.si_code |= __SI_CHLD;
580 return copy_siginfo_to_user32(uinfo, &info);
581}
582
583static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr,
Rusty Russella45185d2009-01-01 10:12:24 +1030584 unsigned len, struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 unsigned long *k;
587
Rusty Russella45185d2009-01-01 10:12:24 +1030588 if (len < cpumask_size())
589 memset(new_mask, 0, cpumask_size());
590 else if (len > cpumask_size())
591 len = cpumask_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Rusty Russella45185d2009-01-01 10:12:24 +1030593 k = cpumask_bits(new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return compat_get_bitmap(k, user_mask_ptr, len * 8);
595}
596
Heiko Carstens62a6fa92014-03-03 16:11:13 +0100597COMPAT_SYSCALL_DEFINE3(sched_setaffinity, compat_pid_t, pid,
598 unsigned int, len,
599 compat_ulong_t __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Rusty Russella45185d2009-01-01 10:12:24 +1030601 cpumask_var_t new_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 int retval;
603
Rusty Russella45185d2009-01-01 10:12:24 +1030604 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
605 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Rusty Russella45185d2009-01-01 10:12:24 +1030607 retval = compat_get_user_cpu_mask(user_mask_ptr, len, new_mask);
608 if (retval)
609 goto out;
610
611 retval = sched_setaffinity(pid, new_mask);
612out:
613 free_cpumask_var(new_mask);
614 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
Heiko Carstens62a6fa92014-03-03 16:11:13 +0100617COMPAT_SYSCALL_DEFINE3(sched_getaffinity, compat_pid_t, pid, unsigned int, len,
618 compat_ulong_t __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 int ret;
Rusty Russella45185d2009-01-01 10:12:24 +1030621 cpumask_var_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
KOSAKI Motohirofa9dc262010-05-19 09:37:41 +0900623 if ((len * BITS_PER_BYTE) < nr_cpu_ids)
624 return -EINVAL;
625 if (len & (sizeof(compat_ulong_t)-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return -EINVAL;
627
Rusty Russella45185d2009-01-01 10:12:24 +1030628 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
629 return -ENOMEM;
630
631 ret = sched_getaffinity(pid, mask);
KOSAKI Motohirofa9dc262010-05-19 09:37:41 +0900632 if (ret == 0) {
633 size_t retlen = min_t(size_t, len, cpumask_size());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
KOSAKI Motohirofa9dc262010-05-19 09:37:41 +0900635 if (compat_put_bitmap(user_mask_ptr, cpumask_bits(mask), retlen * 8))
636 ret = -EFAULT;
637 else
638 ret = retlen;
639 }
Rusty Russella45185d2009-01-01 10:12:24 +1030640 free_cpumask_var(mask);
KOSAKI Motohirofa9dc262010-05-19 09:37:41 +0900641
Rusty Russella45185d2009-01-01 10:12:24 +1030642 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Davide Libenzi83f5d122007-05-10 22:23:18 -0700645int get_compat_itimerspec(struct itimerspec *dst,
646 const struct compat_itimerspec __user *src)
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700647{
H. Peter Anvin81993e82014-02-01 18:54:11 -0800648 if (__compat_get_timespec(&dst->it_interval, &src->it_interval) ||
649 __compat_get_timespec(&dst->it_value, &src->it_value))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return -EFAULT;
651 return 0;
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700652}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Davide Libenzi83f5d122007-05-10 22:23:18 -0700654int put_compat_itimerspec(struct compat_itimerspec __user *dst,
655 const struct itimerspec *src)
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700656{
H. Peter Anvin81993e82014-02-01 18:54:11 -0800657 if (__compat_put_timespec(&src->it_interval, &dst->it_interval) ||
658 __compat_put_timespec(&src->it_value, &dst->it_value))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return -EFAULT;
660 return 0;
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700661}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Heiko Carstens62a6fa92014-03-03 16:11:13 +0100663COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock,
664 struct compat_sigevent __user *, timer_event_spec,
665 timer_t __user *, created_timer_id)
Christoph Hellwig3a0f69d2006-01-09 20:52:08 -0800666{
667 struct sigevent __user *event = NULL;
668
669 if (timer_event_spec) {
670 struct sigevent kevent;
671
672 event = compat_alloc_user_space(sizeof(*event));
673 if (get_compat_sigevent(&kevent, timer_event_spec) ||
674 copy_to_user(event, &kevent, sizeof(*event)))
675 return -EFAULT;
676 }
677
678 return sys_timer_create(which_clock, event, created_timer_id);
679}
680
Heiko Carstens62a6fa92014-03-03 16:11:13 +0100681COMPAT_SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
682 struct compat_itimerspec __user *, new,
683 struct compat_itimerspec __user *, old)
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700684{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 long err;
686 mm_segment_t oldfs;
687 struct itimerspec newts, oldts;
688
689 if (!new)
690 return -EINVAL;
691 if (get_compat_itimerspec(&newts, new))
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700692 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 oldfs = get_fs();
694 set_fs(KERNEL_DS);
695 err = sys_timer_settime(timer_id, flags,
696 (struct itimerspec __user *) &newts,
697 (struct itimerspec __user *) &oldts);
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700698 set_fs(oldfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (!err && old && put_compat_itimerspec(old, &oldts))
700 return -EFAULT;
701 return err;
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700702}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Heiko Carstens62a6fa92014-03-03 16:11:13 +0100704COMPAT_SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
705 struct compat_itimerspec __user *, setting)
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700706{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 long err;
708 mm_segment_t oldfs;
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700709 struct itimerspec ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 oldfs = get_fs();
712 set_fs(KERNEL_DS);
713 err = sys_timer_gettime(timer_id,
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700714 (struct itimerspec __user *) &ts);
715 set_fs(oldfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (!err && put_compat_itimerspec(setting, &ts))
717 return -EFAULT;
718 return err;
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700719}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Heiko Carstens62a6fa92014-03-03 16:11:13 +0100721COMPAT_SYSCALL_DEFINE2(clock_settime, clockid_t, which_clock,
722 struct compat_timespec __user *, tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 long err;
725 mm_segment_t oldfs;
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700726 struct timespec ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
H. Peter Anvin81993e82014-02-01 18:54:11 -0800728 if (compat_get_timespec(&ts, tp))
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700729 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 oldfs = get_fs();
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700731 set_fs(KERNEL_DS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 err = sys_clock_settime(which_clock,
733 (struct timespec __user *) &ts);
734 set_fs(oldfs);
735 return err;
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700736}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Heiko Carstens62a6fa92014-03-03 16:11:13 +0100738COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock,
739 struct compat_timespec __user *, tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 long err;
742 mm_segment_t oldfs;
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700743 struct timespec ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 oldfs = get_fs();
746 set_fs(KERNEL_DS);
747 err = sys_clock_gettime(which_clock,
748 (struct timespec __user *) &ts);
749 set_fs(oldfs);
H. Peter Anvin81993e82014-02-01 18:54:11 -0800750 if (!err && compat_put_timespec(&ts, tp))
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700751 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return err;
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700753}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Heiko Carstens62a6fa92014-03-03 16:11:13 +0100755COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock,
756 struct compat_timex __user *, utp)
Richard Cochranf1f1d5e2011-02-01 13:52:26 +0000757{
758 struct timex txc;
759 mm_segment_t oldfs;
760 int err, ret;
761
762 err = compat_get_timex(&txc, utp);
763 if (err)
764 return err;
765
766 oldfs = get_fs();
767 set_fs(KERNEL_DS);
768 ret = sys_clock_adjtime(which_clock, (struct timex __user *) &txc);
769 set_fs(oldfs);
770
771 err = compat_put_timex(utp, &txc);
772 if (err)
773 return err;
774
775 return ret;
776}
777
Heiko Carstens62a6fa92014-03-03 16:11:13 +0100778COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock,
779 struct compat_timespec __user *, tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 long err;
782 mm_segment_t oldfs;
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700783 struct timespec ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 oldfs = get_fs();
786 set_fs(KERNEL_DS);
787 err = sys_clock_getres(which_clock,
788 (struct timespec __user *) &ts);
789 set_fs(oldfs);
H. Peter Anvin81993e82014-02-01 18:54:11 -0800790 if (!err && tp && compat_put_timespec(&ts, tp))
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700791 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return err;
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700793}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Toyo Abe1711ef32006-09-29 02:00:28 -0700795static long compat_clock_nanosleep_restart(struct restart_block *restart)
796{
797 long err;
798 mm_segment_t oldfs;
799 struct timespec tu;
H. Peter Anvindce44e02014-02-02 17:57:28 -0800800 struct compat_timespec __user *rmtp = restart->nanosleep.compat_rmtp;
Toyo Abe1711ef32006-09-29 02:00:28 -0700801
Thomas Gleixner029a07e2008-02-10 09:17:43 +0100802 restart->nanosleep.rmtp = (struct timespec __user *) &tu;
Toyo Abe1711ef32006-09-29 02:00:28 -0700803 oldfs = get_fs();
804 set_fs(KERNEL_DS);
805 err = clock_nanosleep_restart(restart);
806 set_fs(oldfs);
807
808 if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
H. Peter Anvin81993e82014-02-01 18:54:11 -0800809 compat_put_timespec(&tu, rmtp))
Toyo Abe1711ef32006-09-29 02:00:28 -0700810 return -EFAULT;
811
812 if (err == -ERESTART_RESTARTBLOCK) {
813 restart->fn = compat_clock_nanosleep_restart;
Thomas Gleixner029a07e2008-02-10 09:17:43 +0100814 restart->nanosleep.compat_rmtp = rmtp;
Toyo Abe1711ef32006-09-29 02:00:28 -0700815 }
816 return err;
817}
818
Heiko Carstens62a6fa92014-03-03 16:11:13 +0100819COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags,
820 struct compat_timespec __user *, rqtp,
821 struct compat_timespec __user *, rmtp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 long err;
824 mm_segment_t oldfs;
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700825 struct timespec in, out;
Toyo Abe1711ef32006-09-29 02:00:28 -0700826 struct restart_block *restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
H. Peter Anvin81993e82014-02-01 18:54:11 -0800828 if (compat_get_timespec(&in, rqtp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return -EFAULT;
830
831 oldfs = get_fs();
832 set_fs(KERNEL_DS);
833 err = sys_clock_nanosleep(which_clock, flags,
834 (struct timespec __user *) &in,
835 (struct timespec __user *) &out);
836 set_fs(oldfs);
Toyo Abe1711ef32006-09-29 02:00:28 -0700837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
H. Peter Anvin81993e82014-02-01 18:54:11 -0800839 compat_put_timespec(&out, rmtp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return -EFAULT;
Toyo Abe1711ef32006-09-29 02:00:28 -0700841
842 if (err == -ERESTART_RESTARTBLOCK) {
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800843 restart = &current->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -0700844 restart->fn = compat_clock_nanosleep_restart;
Thomas Gleixner029a07e2008-02-10 09:17:43 +0100845 restart->nanosleep.compat_rmtp = rmtp;
Toyo Abe1711ef32006-09-29 02:00:28 -0700846 }
Daniel Walkerbd3a8492007-10-18 03:06:09 -0700847 return err;
848}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850/*
851 * We currently only need the following fields from the sigevent
852 * structure: sigev_value, sigev_signo, sig_notify and (sometimes
853 * sigev_notify_thread_id). The others are handled in user mode.
854 * We also assume that copying sigev_value.sival_int is sufficient
855 * to keep all the bits of sigev_value.sival_ptr intact.
856 */
857int get_compat_sigevent(struct sigevent *event,
858 const struct compat_sigevent __user *u_event)
859{
David S. Miller51410d32005-04-16 15:24:01 -0700860 memset(event, 0, sizeof(*event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return (!access_ok(VERIFY_READ, u_event, sizeof(*u_event)) ||
862 __get_user(event->sigev_value.sival_int,
863 &u_event->sigev_value.sival_int) ||
864 __get_user(event->sigev_signo, &u_event->sigev_signo) ||
865 __get_user(event->sigev_notify, &u_event->sigev_notify) ||
866 __get_user(event->sigev_notify_thread_id,
867 &u_event->sigev_notify_thread_id))
868 ? -EFAULT : 0;
869}
870
Stephen Rothwell5fa38392006-10-28 10:38:46 -0700871long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 unsigned long bitmap_size)
873{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 unsigned long nr_compat_longs;
875
876 /* align bitmap up to nearest compat_long_t boundary */
877 bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
Al Viro1e1fc132017-05-30 00:29:38 -0400878 nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 if (!access_ok(VERIFY_READ, umask, bitmap_size / 8))
881 return -EFAULT;
882
Al Viro1e1fc132017-05-30 00:29:38 -0400883 user_access_begin();
884 while (nr_compat_longs > 1) {
885 compat_ulong_t l1, l2;
886 unsafe_get_user(l1, umask++, Efault);
887 unsafe_get_user(l2, umask++, Efault);
888 *mask++ = ((unsigned long)l2 << BITS_PER_COMPAT_LONG) | l1;
889 nr_compat_longs -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
Al Viro1e1fc132017-05-30 00:29:38 -0400891 if (nr_compat_longs)
892 unsafe_get_user(*mask, umask++, Efault);
893 user_access_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return 0;
Al Viro1e1fc132017-05-30 00:29:38 -0400895
896Efault:
897 user_access_end();
898 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
901long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
902 unsigned long bitmap_size)
903{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 unsigned long nr_compat_longs;
905
906 /* align bitmap up to nearest compat_long_t boundary */
907 bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
Al Viro1e1fc132017-05-30 00:29:38 -0400908 nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 if (!access_ok(VERIFY_WRITE, umask, bitmap_size / 8))
911 return -EFAULT;
912
Al Viro1e1fc132017-05-30 00:29:38 -0400913 user_access_begin();
914 while (nr_compat_longs > 1) {
915 unsigned long m = *mask++;
916 unsafe_put_user((compat_ulong_t)m, umask++, Efault);
917 unsafe_put_user(m >> BITS_PER_COMPAT_LONG, umask++, Efault);
918 nr_compat_longs -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
Al Viro1e1fc132017-05-30 00:29:38 -0400920 if (nr_compat_longs)
921 unsafe_put_user((compat_ulong_t)*mask, umask++, Efault);
922 user_access_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return 0;
Al Viro1e1fc132017-05-30 00:29:38 -0400924Efault:
925 user_access_end();
926 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
929void
Al Viro322a56c2012-12-25 13:32:58 -0500930sigset_from_compat(sigset_t *set, const compat_sigset_t *compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
932 switch (_NSIG_WORDS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32 );
934 case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32 );
935 case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32 );
936 case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938}
Alexander Graf1dda6062011-06-08 02:45:37 +0200939EXPORT_SYMBOL_GPL(sigset_from_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Al Viro322a56c2012-12-25 13:32:58 -0500941void
942sigset_to_compat(compat_sigset_t *compat, const sigset_t *set)
943{
944 switch (_NSIG_WORDS) {
945 case 4: compat->sig[7] = (set->sig[3] >> 32); compat->sig[6] = set->sig[3];
946 case 3: compat->sig[5] = (set->sig[2] >> 32); compat->sig[4] = set->sig[2];
947 case 2: compat->sig[3] = (set->sig[1] >> 32); compat->sig[2] = set->sig[1];
948 case 1: compat->sig[1] = (set->sig[0] >> 32); compat->sig[0] = set->sig[0];
949 }
950}
951
Al Viro28d27f22012-11-25 19:32:25 -0500952COMPAT_SYSCALL_DEFINE4(rt_sigtimedwait, compat_sigset_t __user *, uthese,
953 struct compat_siginfo __user *, uinfo,
954 struct compat_timespec __user *, uts, compat_size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
956 compat_sigset_t s32;
957 sigset_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 struct timespec t;
959 siginfo_t info;
Oleg Nesterov943df142011-04-27 21:44:14 +0200960 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 if (sigsetsize != sizeof(sigset_t))
963 return -EINVAL;
964
965 if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t)))
966 return -EFAULT;
967 sigset_from_compat(&s, &s32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 if (uts) {
Al Virob2ddedc2012-12-24 12:31:00 -0500970 if (compat_get_timespec(&t, uts))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973
Oleg Nesterov943df142011-04-27 21:44:14 +0200974 ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Oleg Nesterov943df142011-04-27 21:44:14 +0200976 if (ret > 0 && uinfo) {
977 if (copy_siginfo_to_user32(uinfo, &info))
978 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return ret;
Thomas Gleixner62ab4502009-04-04 21:01:06 +0000982}
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984#ifdef __ARCH_WANT_COMPAT_SYS_TIME
985
986/* compat_time_t is a 32 bit "long" and needs to get converted. */
987
Heiko Carstens62a6fa92014-03-03 16:11:13 +0100988COMPAT_SYSCALL_DEFINE1(time, compat_time_t __user *, tloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
990 compat_time_t i;
991 struct timeval tv;
992
993 do_gettimeofday(&tv);
994 i = tv.tv_sec;
995
996 if (tloc) {
997 if (put_user(i,tloc))
Paul Mackerrase3d5a272009-01-06 14:41:02 -0800998 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
Paul Mackerrase3d5a272009-01-06 14:41:02 -08001000 force_successful_syscall_return();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return i;
1002}
1003
Heiko Carstens62a6fa92014-03-03 16:11:13 +01001004COMPAT_SYSCALL_DEFINE1(stime, compat_time_t __user *, tptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
1006 struct timespec tv;
1007 int err;
1008
1009 if (get_user(tv.tv_sec, tptr))
1010 return -EFAULT;
1011
1012 tv.tv_nsec = 0;
1013
1014 err = security_settime(&tv, NULL);
1015 if (err)
1016 return err;
1017
1018 do_settimeofday(&tv);
1019 return 0;
1020}
1021
1022#endif /* __ARCH_WANT_COMPAT_SYS_TIME */
David Woodhouse150256d2006-01-18 17:43:57 -08001023
Heiko Carstens62a6fa92014-03-03 16:11:13 +01001024COMPAT_SYSCALL_DEFINE1(adjtimex, struct compat_timex __user *, utp)
Stephen Rothwell3158e942006-03-26 01:37:29 -08001025{
1026 struct timex txc;
Richard Cochran65f5d802011-02-01 13:52:23 +00001027 int err, ret;
Stephen Rothwell3158e942006-03-26 01:37:29 -08001028
Richard Cochran65f5d802011-02-01 13:52:23 +00001029 err = compat_get_timex(&txc, utp);
1030 if (err)
1031 return err;
Stephen Rothwell3158e942006-03-26 01:37:29 -08001032
1033 ret = do_adjtimex(&txc);
1034
Richard Cochran65f5d802011-02-01 13:52:23 +00001035 err = compat_put_timex(utp, &txc);
1036 if (err)
1037 return err;
Stephen Rothwell3158e942006-03-26 01:37:29 -08001038
1039 return ret;
1040}
Christoph Lameter1b2db9f2006-06-23 02:03:56 -07001041
1042#ifdef CONFIG_NUMA
Heiko Carstens2f2728f2014-03-04 17:18:23 +01001043COMPAT_SYSCALL_DEFINE6(move_pages, pid_t, pid, compat_ulong_t, nr_pages,
1044 compat_uptr_t __user *, pages32,
1045 const int __user *, nodes,
1046 int __user *, status,
1047 int, flags)
Christoph Lameter1b2db9f2006-06-23 02:03:56 -07001048{
1049 const void __user * __user *pages;
1050 int i;
1051
1052 pages = compat_alloc_user_space(nr_pages * sizeof(void *));
1053 for (i = 0; i < nr_pages; i++) {
1054 compat_uptr_t p;
1055
Christoph Lameter9216dfa2006-06-23 02:03:57 -07001056 if (get_user(p, pages32 + i) ||
Christoph Lameter1b2db9f2006-06-23 02:03:56 -07001057 put_user(compat_ptr(p), pages + i))
1058 return -EFAULT;
1059 }
1060 return sys_move_pages(pid, nr_pages, pages, nodes, status, flags);
1061}
Stephen Rothwell3fd59392006-11-02 22:07:24 -08001062
Heiko Carstens62a6fa92014-03-03 16:11:13 +01001063COMPAT_SYSCALL_DEFINE4(migrate_pages, compat_pid_t, pid,
1064 compat_ulong_t, maxnode,
1065 const compat_ulong_t __user *, old_nodes,
1066 const compat_ulong_t __user *, new_nodes)
Stephen Rothwell3fd59392006-11-02 22:07:24 -08001067{
1068 unsigned long __user *old = NULL;
1069 unsigned long __user *new = NULL;
1070 nodemask_t tmp_mask;
1071 unsigned long nr_bits;
1072 unsigned long size;
1073
1074 nr_bits = min_t(unsigned long, maxnode - 1, MAX_NUMNODES);
1075 size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
1076 if (old_nodes) {
1077 if (compat_get_bitmap(nodes_addr(tmp_mask), old_nodes, nr_bits))
1078 return -EFAULT;
1079 old = compat_alloc_user_space(new_nodes ? size * 2 : size);
1080 if (new_nodes)
1081 new = old + size / sizeof(unsigned long);
1082 if (copy_to_user(old, nodes_addr(tmp_mask), size))
1083 return -EFAULT;
1084 }
1085 if (new_nodes) {
1086 if (compat_get_bitmap(nodes_addr(tmp_mask), new_nodes, nr_bits))
1087 return -EFAULT;
1088 if (new == NULL)
1089 new = compat_alloc_user_space(size);
1090 if (copy_to_user(new, nodes_addr(tmp_mask), size))
1091 return -EFAULT;
1092 }
1093 return sys_migrate_pages(pid, nr_bits + 1, old, new);
1094}
Christoph Lameter1b2db9f2006-06-23 02:03:56 -07001095#endif
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001096
Al Viro6883da8c2012-12-25 16:59:28 -05001097COMPAT_SYSCALL_DEFINE2(sched_rr_get_interval,
1098 compat_pid_t, pid,
1099 struct compat_timespec __user *, interval)
Catalin Marinas0ad50c32012-12-17 16:01:45 -08001100{
1101 struct timespec t;
1102 int ret;
1103 mm_segment_t old_fs = get_fs();
1104
1105 set_fs(KERNEL_DS);
1106 ret = sys_sched_rr_get_interval(pid, (struct timespec __user *)&t);
1107 set_fs(old_fs);
H. Peter Anvin81993e82014-02-01 18:54:11 -08001108 if (compat_put_timespec(&t, interval))
Catalin Marinas0ad50c32012-12-17 16:01:45 -08001109 return -EFAULT;
1110 return ret;
1111}
Catalin Marinas0ad50c32012-12-17 16:01:45 -08001112
H. Peter Anvinc41d68a2010-09-07 16:16:18 -07001113/*
1114 * Allocate user-space memory for the duration of a single system call,
1115 * in order to marshall parameters inside a compat thunk.
1116 */
1117void __user *compat_alloc_user_space(unsigned long len)
1118{
1119 void __user *ptr;
1120
1121 /* If len would occupy more than half of the entire compat space... */
1122 if (unlikely(len > (((compat_uptr_t)~0) >> 1)))
1123 return NULL;
1124
1125 ptr = arch_compat_alloc_user_space(len);
1126
1127 if (unlikely(!access_ok(VERIFY_WRITE, ptr, len)))
1128 return NULL;
1129
1130 return ptr;
1131}
1132EXPORT_SYMBOL_GPL(compat_alloc_user_space);