]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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> | |
63d9c273 | 16 | #include <linux/module.h> |
1da177e4 LT |
17 | #include <linux/errno.h> |
18 | #include <linux/time.h> | |
19 | #include <linux/signal.h> | |
20 | #include <linux/sched.h> /* for MAX_SCHEDULE_TIMEOUT */ | |
1da177e4 LT |
21 | #include <linux/syscalls.h> |
22 | #include <linux/unistd.h> | |
23 | #include <linux/security.h> | |
3158e941 | 24 | #include <linux/timex.h> |
6e5fdeed | 25 | #include <linux/export.h> |
1b2db9fb | 26 | #include <linux/migrate.h> |
1711ef38 | 27 | #include <linux/posix-timers.h> |
f06febc9 | 28 | #include <linux/times.h> |
e3d5a27d | 29 | #include <linux/ptrace.h> |
5a0e3ad6 | 30 | #include <linux/gfp.h> |
1da177e4 LT |
31 | |
32 | #include <asm/uaccess.h> | |
1da177e4 | 33 | |
65f5d80b RC |
34 | static int compat_get_timex(struct timex *txc, struct compat_timex __user *utp) |
35 | { | |
36 | memset(txc, 0, sizeof(struct timex)); | |
37 | ||
38 | if (!access_ok(VERIFY_READ, utp, sizeof(struct compat_timex)) || | |
39 | __get_user(txc->modes, &utp->modes) || | |
40 | __get_user(txc->offset, &utp->offset) || | |
41 | __get_user(txc->freq, &utp->freq) || | |
42 | __get_user(txc->maxerror, &utp->maxerror) || | |
43 | __get_user(txc->esterror, &utp->esterror) || | |
44 | __get_user(txc->status, &utp->status) || | |
45 | __get_user(txc->constant, &utp->constant) || | |
46 | __get_user(txc->precision, &utp->precision) || | |
47 | __get_user(txc->tolerance, &utp->tolerance) || | |
48 | __get_user(txc->time.tv_sec, &utp->time.tv_sec) || | |
49 | __get_user(txc->time.tv_usec, &utp->time.tv_usec) || | |
50 | __get_user(txc->tick, &utp->tick) || | |
51 | __get_user(txc->ppsfreq, &utp->ppsfreq) || | |
52 | __get_user(txc->jitter, &utp->jitter) || | |
53 | __get_user(txc->shift, &utp->shift) || | |
54 | __get_user(txc->stabil, &utp->stabil) || | |
55 | __get_user(txc->jitcnt, &utp->jitcnt) || | |
56 | __get_user(txc->calcnt, &utp->calcnt) || | |
57 | __get_user(txc->errcnt, &utp->errcnt) || | |
58 | __get_user(txc->stbcnt, &utp->stbcnt)) | |
59 | return -EFAULT; | |
60 | ||
61 | return 0; | |
62 | } | |
63 | ||
64 | static int compat_put_timex(struct compat_timex __user *utp, struct timex *txc) | |
65 | { | |
66 | if (!access_ok(VERIFY_WRITE, utp, sizeof(struct compat_timex)) || | |
67 | __put_user(txc->modes, &utp->modes) || | |
68 | __put_user(txc->offset, &utp->offset) || | |
69 | __put_user(txc->freq, &utp->freq) || | |
70 | __put_user(txc->maxerror, &utp->maxerror) || | |
71 | __put_user(txc->esterror, &utp->esterror) || | |
72 | __put_user(txc->status, &utp->status) || | |
73 | __put_user(txc->constant, &utp->constant) || | |
74 | __put_user(txc->precision, &utp->precision) || | |
75 | __put_user(txc->tolerance, &utp->tolerance) || | |
76 | __put_user(txc->time.tv_sec, &utp->time.tv_sec) || | |
77 | __put_user(txc->time.tv_usec, &utp->time.tv_usec) || | |
78 | __put_user(txc->tick, &utp->tick) || | |
79 | __put_user(txc->ppsfreq, &utp->ppsfreq) || | |
80 | __put_user(txc->jitter, &utp->jitter) || | |
81 | __put_user(txc->shift, &utp->shift) || | |
82 | __put_user(txc->stabil, &utp->stabil) || | |
83 | __put_user(txc->jitcnt, &utp->jitcnt) || | |
84 | __put_user(txc->calcnt, &utp->calcnt) || | |
85 | __put_user(txc->errcnt, &utp->errcnt) || | |
86 | __put_user(txc->stbcnt, &utp->stbcnt) || | |
87 | __put_user(txc->tai, &utp->tai)) | |
88 | return -EFAULT; | |
89 | return 0; | |
90 | } | |
91 | ||
62a6fa97 HC |
92 | COMPAT_SYSCALL_DEFINE2(gettimeofday, struct compat_timeval __user *, tv, |
93 | struct timezone __user *, tz) | |
b418da16 CH |
94 | { |
95 | if (tv) { | |
96 | struct timeval ktv; | |
97 | do_gettimeofday(&ktv); | |
81993e81 | 98 | if (compat_put_timeval(&ktv, tv)) |
b418da16 CH |
99 | return -EFAULT; |
100 | } | |
101 | if (tz) { | |
102 | if (copy_to_user(tz, &sys_tz, sizeof(sys_tz))) | |
103 | return -EFAULT; | |
104 | } | |
105 | ||
106 | return 0; | |
107 | } | |
108 | ||
62a6fa97 HC |
109 | COMPAT_SYSCALL_DEFINE2(settimeofday, struct compat_timeval __user *, tv, |
110 | struct timezone __user *, tz) | |
b418da16 | 111 | { |
81993e81 PA |
112 | struct timeval user_tv; |
113 | struct timespec new_ts; | |
114 | struct timezone new_tz; | |
b418da16 CH |
115 | |
116 | if (tv) { | |
81993e81 | 117 | if (compat_get_timeval(&user_tv, tv)) |
b418da16 | 118 | return -EFAULT; |
81993e81 PA |
119 | new_ts.tv_sec = user_tv.tv_sec; |
120 | new_ts.tv_nsec = user_tv.tv_usec * NSEC_PER_USEC; | |
b418da16 CH |
121 | } |
122 | if (tz) { | |
81993e81 | 123 | if (copy_from_user(&new_tz, tz, sizeof(*tz))) |
b418da16 CH |
124 | return -EFAULT; |
125 | } | |
126 | ||
81993e81 | 127 | return do_sys_settimeofday(tv ? &new_ts : NULL, tz ? &new_tz : NULL); |
b418da16 CH |
128 | } |
129 | ||
81993e81 | 130 | static int __compat_get_timeval(struct timeval *tv, const struct compat_timeval __user *ctv) |
6684ba20 PA |
131 | { |
132 | return (!access_ok(VERIFY_READ, ctv, sizeof(*ctv)) || | |
133 | __get_user(tv->tv_sec, &ctv->tv_sec) || | |
134 | __get_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0; | |
135 | } | |
6684ba20 | 136 | |
81993e81 | 137 | static int __compat_put_timeval(const struct timeval *tv, struct compat_timeval __user *ctv) |
6684ba20 PA |
138 | { |
139 | return (!access_ok(VERIFY_WRITE, ctv, sizeof(*ctv)) || | |
140 | __put_user(tv->tv_sec, &ctv->tv_sec) || | |
141 | __put_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0; | |
142 | } | |
6684ba20 | 143 | |
81993e81 | 144 | static int __compat_get_timespec(struct timespec *ts, const struct compat_timespec __user *cts) |
1da177e4 LT |
145 | { |
146 | return (!access_ok(VERIFY_READ, cts, sizeof(*cts)) || | |
147 | __get_user(ts->tv_sec, &cts->tv_sec) || | |
148 | __get_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0; | |
149 | } | |
150 | ||
81993e81 | 151 | static int __compat_put_timespec(const struct timespec *ts, struct compat_timespec __user *cts) |
1da177e4 LT |
152 | { |
153 | return (!access_ok(VERIFY_WRITE, cts, sizeof(*cts)) || | |
154 | __put_user(ts->tv_sec, &cts->tv_sec) || | |
155 | __put_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0; | |
156 | } | |
157 | ||
6684ba20 PA |
158 | int compat_get_timeval(struct timeval *tv, const void __user *utv) |
159 | { | |
160 | if (COMPAT_USE_64BIT_TIME) | |
6516a466 | 161 | return copy_from_user(tv, utv, sizeof(*tv)) ? -EFAULT : 0; |
6684ba20 | 162 | else |
81993e81 | 163 | return __compat_get_timeval(tv, utv); |
6684ba20 PA |
164 | } |
165 | EXPORT_SYMBOL_GPL(compat_get_timeval); | |
166 | ||
167 | int compat_put_timeval(const struct timeval *tv, void __user *utv) | |
168 | { | |
169 | if (COMPAT_USE_64BIT_TIME) | |
6516a466 | 170 | return copy_to_user(utv, tv, sizeof(*tv)) ? -EFAULT : 0; |
6684ba20 | 171 | else |
81993e81 | 172 | return __compat_put_timeval(tv, utv); |
6684ba20 PA |
173 | } |
174 | EXPORT_SYMBOL_GPL(compat_put_timeval); | |
175 | ||
176 | int compat_get_timespec(struct timespec *ts, const void __user *uts) | |
177 | { | |
178 | if (COMPAT_USE_64BIT_TIME) | |
6516a466 | 179 | return copy_from_user(ts, uts, sizeof(*ts)) ? -EFAULT : 0; |
6684ba20 | 180 | else |
81993e81 | 181 | return __compat_get_timespec(ts, uts); |
6684ba20 PA |
182 | } |
183 | EXPORT_SYMBOL_GPL(compat_get_timespec); | |
184 | ||
185 | int compat_put_timespec(const struct timespec *ts, void __user *uts) | |
186 | { | |
187 | if (COMPAT_USE_64BIT_TIME) | |
6516a466 | 188 | return copy_to_user(uts, ts, sizeof(*ts)) ? -EFAULT : 0; |
6684ba20 | 189 | else |
81993e81 | 190 | return __compat_put_timespec(ts, uts); |
6684ba20 PA |
191 | } |
192 | EXPORT_SYMBOL_GPL(compat_put_timespec); | |
193 | ||
81993e81 PA |
194 | int compat_convert_timespec(struct timespec __user **kts, |
195 | const void __user *cts) | |
196 | { | |
197 | struct timespec ts; | |
198 | struct timespec __user *uts; | |
199 | ||
200 | if (!cts || COMPAT_USE_64BIT_TIME) { | |
201 | *kts = (struct timespec __user *)cts; | |
202 | return 0; | |
203 | } | |
204 | ||
205 | uts = compat_alloc_user_space(sizeof(ts)); | |
206 | if (!uts) | |
207 | return -EFAULT; | |
208 | if (compat_get_timespec(&ts, cts)) | |
209 | return -EFAULT; | |
210 | if (copy_to_user(uts, &ts, sizeof(ts))) | |
211 | return -EFAULT; | |
212 | ||
213 | *kts = uts; | |
214 | return 0; | |
215 | } | |
216 | ||
41652937 ON |
217 | static long compat_nanosleep_restart(struct restart_block *restart) |
218 | { | |
219 | struct compat_timespec __user *rmtp; | |
220 | struct timespec rmt; | |
221 | mm_segment_t oldfs; | |
222 | long ret; | |
223 | ||
63d9c273 | 224 | restart->nanosleep.rmtp = (struct timespec __force_user *) &rmt; |
41652937 ON |
225 | oldfs = get_fs(); |
226 | set_fs(KERNEL_DS); | |
227 | ret = hrtimer_nanosleep_restart(restart); | |
228 | set_fs(oldfs); | |
229 | ||
849151dd | 230 | if (ret == -ERESTART_RESTARTBLOCK) { |
029a07e0 | 231 | rmtp = restart->nanosleep.compat_rmtp; |
41652937 | 232 | |
81993e81 | 233 | if (rmtp && compat_put_timespec(&rmt, rmtp)) |
41652937 ON |
234 | return -EFAULT; |
235 | } | |
236 | ||
237 | return ret; | |
238 | } | |
239 | ||
62a6fa97 HC |
240 | COMPAT_SYSCALL_DEFINE2(nanosleep, struct compat_timespec __user *, rqtp, |
241 | struct compat_timespec __user *, rmtp) | |
1da177e4 | 242 | { |
c70878b4 | 243 | struct timespec tu, rmt; |
41652937 | 244 | mm_segment_t oldfs; |
c70878b4 | 245 | long ret; |
1da177e4 | 246 | |
81993e81 | 247 | if (compat_get_timespec(&tu, rqtp)) |
1da177e4 LT |
248 | return -EFAULT; |
249 | ||
c70878b4 | 250 | if (!timespec_valid(&tu)) |
1da177e4 LT |
251 | return -EINVAL; |
252 | ||
41652937 ON |
253 | oldfs = get_fs(); |
254 | set_fs(KERNEL_DS); | |
255 | ret = hrtimer_nanosleep(&tu, | |
63d9c273 | 256 | rmtp ? (struct timespec __force_user *)&rmt : NULL, |
41652937 ON |
257 | HRTIMER_MODE_REL, CLOCK_MONOTONIC); |
258 | set_fs(oldfs); | |
259 | ||
849151dd TG |
260 | /* |
261 | * hrtimer_nanosleep() can only return 0 or | |
262 | * -ERESTART_RESTARTBLOCK here because: | |
263 | * | |
264 | * - we call it with HRTIMER_MODE_REL and therefor exclude the | |
265 | * -ERESTARTNOHAND return path. | |
266 | * | |
267 | * - we supply the rmtp argument from the task stack (due to | |
268 | * the necessary compat conversion. So the update cannot | |
269 | * fail, which excludes the -EFAULT return path as well. If | |
270 | * it fails nevertheless we have a bigger problem and wont | |
271 | * reach this place anymore. | |
272 | * | |
273 | * - if the return value is 0, we do not have to update rmtp | |
274 | * because there is no remaining time. | |
275 | * | |
276 | * We check for -ERESTART_RESTARTBLOCK nevertheless if the | |
277 | * core implementation decides to return random nonsense. | |
278 | */ | |
279 | if (ret == -ERESTART_RESTARTBLOCK) { | |
41652937 ON |
280 | struct restart_block *restart |
281 | = ¤t_thread_info()->restart_block; | |
282 | ||
283 | restart->fn = compat_nanosleep_restart; | |
029a07e0 | 284 | restart->nanosleep.compat_rmtp = rmtp; |
1da177e4 | 285 | |
81993e81 | 286 | if (rmtp && compat_put_timespec(&rmt, rmtp)) |
1da177e4 LT |
287 | return -EFAULT; |
288 | } | |
c70878b4 | 289 | return ret; |
1da177e4 LT |
290 | } |
291 | ||
292 | static 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 | ||
302 | static 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 | ||
37784074 AV |
312 | COMPAT_SYSCALL_DEFINE2(getitimer, int, which, |
313 | struct compat_itimerval __user *, it) | |
1da177e4 LT |
314 | { |
315 | struct itimerval kit; | |
316 | int error; | |
317 | ||
318 | error = do_getitimer(which, &kit); | |
319 | if (!error && put_compat_itimerval(it, &kit)) | |
320 | error = -EFAULT; | |
321 | return error; | |
322 | } | |
323 | ||
37784074 AV |
324 | COMPAT_SYSCALL_DEFINE3(setitimer, int, which, |
325 | struct compat_itimerval __user *, in, | |
326 | struct compat_itimerval __user *, out) | |
1da177e4 LT |
327 | { |
328 | struct itimerval kin, kout; | |
329 | int error; | |
330 | ||
331 | if (in) { | |
332 | if (get_compat_itimerval(&kin, in)) | |
333 | return -EFAULT; | |
334 | } else | |
335 | memset(&kin, 0, sizeof(kin)); | |
336 | ||
337 | error = do_setitimer(which, &kin, out ? &kout : NULL); | |
338 | if (error || !out) | |
339 | return error; | |
340 | if (put_compat_itimerval(out, &kout)) | |
341 | return -EFAULT; | |
342 | return 0; | |
343 | } | |
344 | ||
f06febc9 FM |
345 | static compat_clock_t clock_t_to_compat_clock_t(clock_t x) |
346 | { | |
347 | return compat_jiffies_to_clock_t(clock_t_to_jiffies(x)); | |
348 | } | |
349 | ||
62a6fa97 | 350 | COMPAT_SYSCALL_DEFINE1(times, struct compat_tms __user *, tbuf) |
1da177e4 | 351 | { |
1da177e4 | 352 | if (tbuf) { |
f06febc9 | 353 | struct tms tms; |
1da177e4 | 354 | struct compat_tms tmp; |
f06febc9 FM |
355 | |
356 | do_sys_times(&tms); | |
357 | /* Convert our struct tms to the compat version. */ | |
358 | tmp.tms_utime = clock_t_to_compat_clock_t(tms.tms_utime); | |
359 | tmp.tms_stime = clock_t_to_compat_clock_t(tms.tms_stime); | |
360 | tmp.tms_cutime = clock_t_to_compat_clock_t(tms.tms_cutime); | |
361 | tmp.tms_cstime = clock_t_to_compat_clock_t(tms.tms_cstime); | |
1da177e4 LT |
362 | if (copy_to_user(tbuf, &tmp, sizeof(tmp))) |
363 | return -EFAULT; | |
364 | } | |
e3d5a27d | 365 | force_successful_syscall_return(); |
1da177e4 LT |
366 | return compat_jiffies_to_clock_t(jiffies); |
367 | } | |
368 | ||
be84cb43 CM |
369 | #ifdef __ARCH_WANT_SYS_SIGPENDING |
370 | ||
1da177e4 LT |
371 | /* |
372 | * Assumption: old_sigset_t and compat_old_sigset_t are both | |
373 | * types that can be passed to put_user()/get_user(). | |
374 | */ | |
375 | ||
62a6fa97 | 376 | COMPAT_SYSCALL_DEFINE1(sigpending, compat_old_sigset_t __user *, set) |
1da177e4 LT |
377 | { |
378 | old_sigset_t s; | |
379 | long ret; | |
380 | mm_segment_t old_fs = get_fs(); | |
381 | ||
382 | set_fs(KERNEL_DS); | |
63d9c273 | 383 | ret = sys_sigpending((old_sigset_t __force_user *) &s); |
1da177e4 LT |
384 | set_fs(old_fs); |
385 | if (ret == 0) | |
386 | ret = put_user(s, set); | |
387 | return ret; | |
388 | } | |
389 | ||
be84cb43 CM |
390 | #endif |
391 | ||
392 | #ifdef __ARCH_WANT_SYS_SIGPROCMASK | |
393 | ||
b7dafa0e JK |
394 | /* |
395 | * sys_sigprocmask SIG_SETMASK sets the first (compat) word of the | |
396 | * blocked set of signals to the supplied signal set | |
397 | */ | |
398 | static inline void compat_sig_setmask(sigset_t *blocked, compat_sigset_word set) | |
1da177e4 | 399 | { |
b7dafa0e JK |
400 | memcpy(blocked->sig, &set, sizeof(set)); |
401 | } | |
1da177e4 | 402 | |
5cf22100 AV |
403 | COMPAT_SYSCALL_DEFINE3(sigprocmask, int, how, |
404 | compat_old_sigset_t __user *, nset, | |
405 | compat_old_sigset_t __user *, oset) | |
b7dafa0e JK |
406 | { |
407 | old_sigset_t old_set, new_set; | |
408 | sigset_t new_blocked; | |
409 | ||
410 | old_set = current->blocked.sig[0]; | |
411 | ||
412 | if (nset) { | |
413 | if (get_user(new_set, nset)) | |
414 | return -EFAULT; | |
415 | new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP)); | |
416 | ||
417 | new_blocked = current->blocked; | |
418 | ||
419 | switch (how) { | |
420 | case SIG_BLOCK: | |
421 | sigaddsetmask(&new_blocked, new_set); | |
422 | break; | |
423 | case SIG_UNBLOCK: | |
424 | sigdelsetmask(&new_blocked, new_set); | |
425 | break; | |
426 | case SIG_SETMASK: | |
427 | compat_sig_setmask(&new_blocked, new_set); | |
428 | break; | |
429 | default: | |
430 | return -EINVAL; | |
431 | } | |
432 | ||
433 | set_current_blocked(&new_blocked); | |
434 | } | |
435 | ||
436 | if (oset) { | |
437 | if (put_user(old_set, oset)) | |
438 | return -EFAULT; | |
439 | } | |
440 | ||
441 | return 0; | |
1da177e4 LT |
442 | } |
443 | ||
be84cb43 CM |
444 | #endif |
445 | ||
62a6fa97 HC |
446 | COMPAT_SYSCALL_DEFINE2(setrlimit, unsigned int, resource, |
447 | struct compat_rlimit __user *, rlim) | |
1da177e4 LT |
448 | { |
449 | struct rlimit r; | |
1da177e4 LT |
450 | |
451 | if (!access_ok(VERIFY_READ, rlim, sizeof(*rlim)) || | |
452 | __get_user(r.rlim_cur, &rlim->rlim_cur) || | |
453 | __get_user(r.rlim_max, &rlim->rlim_max)) | |
454 | return -EFAULT; | |
455 | ||
456 | if (r.rlim_cur == COMPAT_RLIM_INFINITY) | |
457 | r.rlim_cur = RLIM_INFINITY; | |
458 | if (r.rlim_max == COMPAT_RLIM_INFINITY) | |
459 | r.rlim_max = RLIM_INFINITY; | |
b9518345 | 460 | return do_prlimit(current, resource, &r, NULL); |
1da177e4 LT |
461 | } |
462 | ||
463 | #ifdef COMPAT_RLIM_OLD_INFINITY | |
464 | ||
62a6fa97 HC |
465 | COMPAT_SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource, |
466 | struct compat_rlimit __user *, rlim) | |
1da177e4 LT |
467 | { |
468 | struct rlimit r; | |
469 | int ret; | |
470 | mm_segment_t old_fs = get_fs(); | |
471 | ||
472 | set_fs(KERNEL_DS); | |
63d9c273 | 473 | ret = sys_old_getrlimit(resource, (struct rlimit __force_user *)&r); |
1da177e4 LT |
474 | set_fs(old_fs); |
475 | ||
476 | if (!ret) { | |
477 | if (r.rlim_cur > COMPAT_RLIM_OLD_INFINITY) | |
478 | r.rlim_cur = COMPAT_RLIM_INFINITY; | |
479 | if (r.rlim_max > COMPAT_RLIM_OLD_INFINITY) | |
480 | r.rlim_max = COMPAT_RLIM_INFINITY; | |
481 | ||
482 | if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) || | |
483 | __put_user(r.rlim_cur, &rlim->rlim_cur) || | |
484 | __put_user(r.rlim_max, &rlim->rlim_max)) | |
485 | return -EFAULT; | |
486 | } | |
487 | return ret; | |
488 | } | |
489 | ||
490 | #endif | |
491 | ||
62a6fa97 HC |
492 | COMPAT_SYSCALL_DEFINE2(getrlimit, unsigned int, resource, |
493 | struct compat_rlimit __user *, rlim) | |
1da177e4 LT |
494 | { |
495 | struct rlimit r; | |
496 | int ret; | |
1da177e4 | 497 | |
b9518345 | 498 | ret = do_prlimit(current, resource, NULL, &r); |
1da177e4 LT |
499 | if (!ret) { |
500 | if (r.rlim_cur > COMPAT_RLIM_INFINITY) | |
501 | r.rlim_cur = COMPAT_RLIM_INFINITY; | |
502 | if (r.rlim_max > COMPAT_RLIM_INFINITY) | |
503 | r.rlim_max = COMPAT_RLIM_INFINITY; | |
504 | ||
505 | if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) || | |
506 | __put_user(r.rlim_cur, &rlim->rlim_cur) || | |
507 | __put_user(r.rlim_max, &rlim->rlim_max)) | |
508 | return -EFAULT; | |
509 | } | |
510 | return ret; | |
511 | } | |
512 | ||
513 | int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru) | |
514 | { | |
515 | if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)) || | |
516 | __put_user(r->ru_utime.tv_sec, &ru->ru_utime.tv_sec) || | |
517 | __put_user(r->ru_utime.tv_usec, &ru->ru_utime.tv_usec) || | |
518 | __put_user(r->ru_stime.tv_sec, &ru->ru_stime.tv_sec) || | |
519 | __put_user(r->ru_stime.tv_usec, &ru->ru_stime.tv_usec) || | |
520 | __put_user(r->ru_maxrss, &ru->ru_maxrss) || | |
521 | __put_user(r->ru_ixrss, &ru->ru_ixrss) || | |
522 | __put_user(r->ru_idrss, &ru->ru_idrss) || | |
523 | __put_user(r->ru_isrss, &ru->ru_isrss) || | |
524 | __put_user(r->ru_minflt, &ru->ru_minflt) || | |
525 | __put_user(r->ru_majflt, &ru->ru_majflt) || | |
526 | __put_user(r->ru_nswap, &ru->ru_nswap) || | |
527 | __put_user(r->ru_inblock, &ru->ru_inblock) || | |
528 | __put_user(r->ru_oublock, &ru->ru_oublock) || | |
529 | __put_user(r->ru_msgsnd, &ru->ru_msgsnd) || | |
530 | __put_user(r->ru_msgrcv, &ru->ru_msgrcv) || | |
531 | __put_user(r->ru_nsignals, &ru->ru_nsignals) || | |
532 | __put_user(r->ru_nvcsw, &ru->ru_nvcsw) || | |
533 | __put_user(r->ru_nivcsw, &ru->ru_nivcsw)) | |
534 | return -EFAULT; | |
535 | return 0; | |
536 | } | |
537 | ||
8d9807b1 AV |
538 | COMPAT_SYSCALL_DEFINE4(wait4, |
539 | compat_pid_t, pid, | |
540 | compat_uint_t __user *, stat_addr, | |
541 | int, options, | |
542 | struct compat_rusage __user *, ru) | |
1da177e4 LT |
543 | { |
544 | if (!ru) { | |
545 | return sys_wait4(pid, stat_addr, options, NULL); | |
546 | } else { | |
547 | struct rusage r; | |
548 | int ret; | |
549 | unsigned int status; | |
550 | mm_segment_t old_fs = get_fs(); | |
551 | ||
552 | set_fs (KERNEL_DS); | |
553 | ret = sys_wait4(pid, | |
554 | (stat_addr ? | |
63d9c273 MT |
555 | (unsigned int __force_user *) &status : NULL), |
556 | options, (struct rusage __force_user *) &r); | |
1da177e4 LT |
557 | set_fs (old_fs); |
558 | ||
559 | if (ret > 0) { | |
560 | if (put_compat_rusage(&r, ru)) | |
561 | return -EFAULT; | |
562 | if (stat_addr && put_user(status, stat_addr)) | |
563 | return -EFAULT; | |
564 | } | |
565 | return ret; | |
566 | } | |
567 | } | |
568 | ||
8d9807b1 AV |
569 | COMPAT_SYSCALL_DEFINE5(waitid, |
570 | int, which, compat_pid_t, pid, | |
571 | struct compat_siginfo __user *, uinfo, int, options, | |
572 | struct compat_rusage __user *, uru) | |
1da177e4 LT |
573 | { |
574 | siginfo_t info; | |
575 | struct rusage ru; | |
576 | long ret; | |
577 | mm_segment_t old_fs = get_fs(); | |
578 | ||
579 | memset(&info, 0, sizeof(info)); | |
580 | ||
581 | set_fs(KERNEL_DS); | |
63d9c273 MT |
582 | ret = sys_waitid(which, pid, (siginfo_t __force_user *)&info, options, |
583 | uru ? (struct rusage __force_user *)&ru : NULL); | |
1da177e4 LT |
584 | set_fs(old_fs); |
585 | ||
586 | if ((ret < 0) || (info.si_signo == 0)) | |
587 | return ret; | |
588 | ||
589 | if (uru) { | |
a566c288 AV |
590 | /* sys_waitid() overwrites everything in ru */ |
591 | if (COMPAT_USE_64BIT_TIME) | |
592 | ret = copy_to_user(uru, &ru, sizeof(ru)); | |
593 | else | |
594 | ret = put_compat_rusage(&ru, uru); | |
1da177e4 | 595 | if (ret) |
bffea77c | 596 | return -EFAULT; |
1da177e4 LT |
597 | } |
598 | ||
599 | BUG_ON(info.si_code & __SI_MASK); | |
600 | info.si_code |= __SI_CHLD; | |
601 | return copy_siginfo_to_user32(uinfo, &info); | |
602 | } | |
603 | ||
604 | static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr, | |
a45185d2 | 605 | unsigned len, struct cpumask *new_mask) |
1da177e4 LT |
606 | { |
607 | unsigned long *k; | |
608 | ||
a45185d2 RR |
609 | if (len < cpumask_size()) |
610 | memset(new_mask, 0, cpumask_size()); | |
611 | else if (len > cpumask_size()) | |
612 | len = cpumask_size(); | |
1da177e4 | 613 | |
a45185d2 | 614 | k = cpumask_bits(new_mask); |
1da177e4 LT |
615 | return compat_get_bitmap(k, user_mask_ptr, len * 8); |
616 | } | |
617 | ||
62a6fa97 HC |
618 | COMPAT_SYSCALL_DEFINE3(sched_setaffinity, compat_pid_t, pid, |
619 | unsigned int, len, | |
620 | compat_ulong_t __user *, user_mask_ptr) | |
1da177e4 | 621 | { |
a45185d2 | 622 | cpumask_var_t new_mask; |
1da177e4 LT |
623 | int retval; |
624 | ||
a45185d2 RR |
625 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) |
626 | return -ENOMEM; | |
627 | ||
628 | retval = compat_get_user_cpu_mask(user_mask_ptr, len, new_mask); | |
1da177e4 | 629 | if (retval) |
a45185d2 | 630 | goto out; |
1da177e4 | 631 | |
a45185d2 RR |
632 | retval = sched_setaffinity(pid, new_mask); |
633 | out: | |
634 | free_cpumask_var(new_mask); | |
635 | return retval; | |
1da177e4 LT |
636 | } |
637 | ||
62a6fa97 HC |
638 | COMPAT_SYSCALL_DEFINE3(sched_getaffinity, compat_pid_t, pid, unsigned int, len, |
639 | compat_ulong_t __user *, user_mask_ptr) | |
1da177e4 LT |
640 | { |
641 | int ret; | |
a45185d2 | 642 | cpumask_var_t mask; |
1da177e4 | 643 | |
fa9dc265 KM |
644 | if ((len * BITS_PER_BYTE) < nr_cpu_ids) |
645 | return -EINVAL; | |
646 | if (len & (sizeof(compat_ulong_t)-1)) | |
1da177e4 LT |
647 | return -EINVAL; |
648 | ||
a45185d2 RR |
649 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) |
650 | return -ENOMEM; | |
651 | ||
652 | ret = sched_getaffinity(pid, mask); | |
fa9dc265 KM |
653 | if (ret == 0) { |
654 | size_t retlen = min_t(size_t, len, cpumask_size()); | |
1da177e4 | 655 | |
fa9dc265 KM |
656 | if (compat_put_bitmap(user_mask_ptr, cpumask_bits(mask), retlen * 8)) |
657 | ret = -EFAULT; | |
658 | else | |
659 | ret = retlen; | |
660 | } | |
a45185d2 | 661 | free_cpumask_var(mask); |
fa9dc265 | 662 | |
a45185d2 | 663 | return ret; |
1da177e4 LT |
664 | } |
665 | ||
83f5d126 DL |
666 | int get_compat_itimerspec(struct itimerspec *dst, |
667 | const struct compat_itimerspec __user *src) | |
bd3a8492 | 668 | { |
81993e81 PA |
669 | if (__compat_get_timespec(&dst->it_interval, &src->it_interval) || |
670 | __compat_get_timespec(&dst->it_value, &src->it_value)) | |
1da177e4 LT |
671 | return -EFAULT; |
672 | return 0; | |
bd3a8492 | 673 | } |
1da177e4 | 674 | |
83f5d126 DL |
675 | int put_compat_itimerspec(struct compat_itimerspec __user *dst, |
676 | const struct itimerspec *src) | |
bd3a8492 | 677 | { |
81993e81 PA |
678 | if (__compat_put_timespec(&src->it_interval, &dst->it_interval) || |
679 | __compat_put_timespec(&src->it_value, &dst->it_value)) | |
1da177e4 LT |
680 | return -EFAULT; |
681 | return 0; | |
bd3a8492 | 682 | } |
1da177e4 | 683 | |
62a6fa97 HC |
684 | COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock, |
685 | struct compat_sigevent __user *, timer_event_spec, | |
686 | timer_t __user *, created_timer_id) | |
3a0f69d5 CH |
687 | { |
688 | struct sigevent __user *event = NULL; | |
689 | ||
690 | if (timer_event_spec) { | |
691 | struct sigevent kevent; | |
692 | ||
693 | event = compat_alloc_user_space(sizeof(*event)); | |
694 | if (get_compat_sigevent(&kevent, timer_event_spec) || | |
695 | copy_to_user(event, &kevent, sizeof(*event))) | |
696 | return -EFAULT; | |
697 | } | |
698 | ||
699 | return sys_timer_create(which_clock, event, created_timer_id); | |
700 | } | |
701 | ||
62a6fa97 HC |
702 | COMPAT_SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags, |
703 | struct compat_itimerspec __user *, new, | |
704 | struct compat_itimerspec __user *, old) | |
bd3a8492 | 705 | { |
1da177e4 LT |
706 | long err; |
707 | mm_segment_t oldfs; | |
708 | struct itimerspec newts, oldts; | |
709 | ||
710 | if (!new) | |
711 | return -EINVAL; | |
712 | if (get_compat_itimerspec(&newts, new)) | |
bd3a8492 | 713 | return -EFAULT; |
1da177e4 LT |
714 | oldfs = get_fs(); |
715 | set_fs(KERNEL_DS); | |
716 | err = sys_timer_settime(timer_id, flags, | |
63d9c273 MT |
717 | (struct itimerspec __force_user *) &newts, |
718 | (struct itimerspec __force_user *) &oldts); | |
bd3a8492 | 719 | set_fs(oldfs); |
1da177e4 LT |
720 | if (!err && old && put_compat_itimerspec(old, &oldts)) |
721 | return -EFAULT; | |
722 | return err; | |
bd3a8492 | 723 | } |
1da177e4 | 724 | |
62a6fa97 HC |
725 | COMPAT_SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id, |
726 | struct compat_itimerspec __user *, setting) | |
bd3a8492 | 727 | { |
1da177e4 LT |
728 | long err; |
729 | mm_segment_t oldfs; | |
bd3a8492 | 730 | struct itimerspec ts; |
1da177e4 LT |
731 | |
732 | oldfs = get_fs(); | |
733 | set_fs(KERNEL_DS); | |
734 | err = sys_timer_gettime(timer_id, | |
63d9c273 | 735 | (struct itimerspec __force_user *) &ts); |
bd3a8492 | 736 | set_fs(oldfs); |
1da177e4 LT |
737 | if (!err && put_compat_itimerspec(setting, &ts)) |
738 | return -EFAULT; | |
739 | return err; | |
bd3a8492 | 740 | } |
1da177e4 | 741 | |
62a6fa97 HC |
742 | COMPAT_SYSCALL_DEFINE2(clock_settime, clockid_t, which_clock, |
743 | struct compat_timespec __user *, tp) | |
1da177e4 LT |
744 | { |
745 | long err; | |
746 | mm_segment_t oldfs; | |
bd3a8492 | 747 | struct timespec ts; |
1da177e4 | 748 | |
81993e81 | 749 | if (compat_get_timespec(&ts, tp)) |
bd3a8492 | 750 | return -EFAULT; |
1da177e4 | 751 | oldfs = get_fs(); |
bd3a8492 | 752 | set_fs(KERNEL_DS); |
1da177e4 | 753 | err = sys_clock_settime(which_clock, |
63d9c273 | 754 | (struct timespec __force_user *) &ts); |
1da177e4 LT |
755 | set_fs(oldfs); |
756 | return err; | |
bd3a8492 | 757 | } |
1da177e4 | 758 | |
62a6fa97 HC |
759 | COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock, |
760 | struct compat_timespec __user *, tp) | |
1da177e4 LT |
761 | { |
762 | long err; | |
763 | mm_segment_t oldfs; | |
bd3a8492 | 764 | struct timespec ts; |
1da177e4 LT |
765 | |
766 | oldfs = get_fs(); | |
767 | set_fs(KERNEL_DS); | |
768 | err = sys_clock_gettime(which_clock, | |
63d9c273 | 769 | (struct timespec __force_user *) &ts); |
1da177e4 | 770 | set_fs(oldfs); |
81993e81 | 771 | if (!err && compat_put_timespec(&ts, tp)) |
bd3a8492 | 772 | return -EFAULT; |
1da177e4 | 773 | return err; |
bd3a8492 | 774 | } |
1da177e4 | 775 | |
62a6fa97 HC |
776 | COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock, |
777 | struct compat_timex __user *, utp) | |
f1f1d5eb RC |
778 | { |
779 | struct timex txc; | |
780 | mm_segment_t oldfs; | |
781 | int err, ret; | |
782 | ||
783 | err = compat_get_timex(&txc, utp); | |
784 | if (err) | |
785 | return err; | |
786 | ||
787 | oldfs = get_fs(); | |
788 | set_fs(KERNEL_DS); | |
63d9c273 | 789 | ret = sys_clock_adjtime(which_clock, (struct timex __force_user *) &txc); |
f1f1d5eb RC |
790 | set_fs(oldfs); |
791 | ||
792 | err = compat_put_timex(utp, &txc); | |
793 | if (err) | |
794 | return err; | |
795 | ||
796 | return ret; | |
797 | } | |
798 | ||
62a6fa97 HC |
799 | COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock, |
800 | struct compat_timespec __user *, tp) | |
1da177e4 LT |
801 | { |
802 | long err; | |
803 | mm_segment_t oldfs; | |
bd3a8492 | 804 | struct timespec ts; |
1da177e4 LT |
805 | |
806 | oldfs = get_fs(); | |
807 | set_fs(KERNEL_DS); | |
808 | err = sys_clock_getres(which_clock, | |
63d9c273 | 809 | (struct timespec __force_user *) &ts); |
1da177e4 | 810 | set_fs(oldfs); |
81993e81 | 811 | if (!err && tp && compat_put_timespec(&ts, tp)) |
bd3a8492 | 812 | return -EFAULT; |
1da177e4 | 813 | return err; |
bd3a8492 | 814 | } |
1da177e4 | 815 | |
1711ef38 TA |
816 | static long compat_clock_nanosleep_restart(struct restart_block *restart) |
817 | { | |
818 | long err; | |
819 | mm_segment_t oldfs; | |
820 | struct timespec tu; | |
dce44e03 | 821 | struct compat_timespec __user *rmtp = restart->nanosleep.compat_rmtp; |
1711ef38 | 822 | |
63d9c273 | 823 | restart->nanosleep.rmtp = (struct timespec __force_user *) &tu; |
1711ef38 TA |
824 | oldfs = get_fs(); |
825 | set_fs(KERNEL_DS); | |
826 | err = clock_nanosleep_restart(restart); | |
827 | set_fs(oldfs); | |
828 | ||
829 | if ((err == -ERESTART_RESTARTBLOCK) && rmtp && | |
81993e81 | 830 | compat_put_timespec(&tu, rmtp)) |
1711ef38 TA |
831 | return -EFAULT; |
832 | ||
833 | if (err == -ERESTART_RESTARTBLOCK) { | |
834 | restart->fn = compat_clock_nanosleep_restart; | |
029a07e0 | 835 | restart->nanosleep.compat_rmtp = rmtp; |
1711ef38 TA |
836 | } |
837 | return err; | |
838 | } | |
839 | ||
62a6fa97 HC |
840 | COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags, |
841 | struct compat_timespec __user *, rqtp, | |
842 | struct compat_timespec __user *, rmtp) | |
1da177e4 LT |
843 | { |
844 | long err; | |
845 | mm_segment_t oldfs; | |
bd3a8492 | 846 | struct timespec in, out; |
1711ef38 | 847 | struct restart_block *restart; |
1da177e4 | 848 | |
81993e81 | 849 | if (compat_get_timespec(&in, rqtp)) |
1da177e4 LT |
850 | return -EFAULT; |
851 | ||
852 | oldfs = get_fs(); | |
853 | set_fs(KERNEL_DS); | |
854 | err = sys_clock_nanosleep(which_clock, flags, | |
63d9c273 MT |
855 | (struct timespec __force_user *) &in, |
856 | (struct timespec __force_user *) &out); | |
1da177e4 | 857 | set_fs(oldfs); |
1711ef38 | 858 | |
1da177e4 | 859 | if ((err == -ERESTART_RESTARTBLOCK) && rmtp && |
81993e81 | 860 | compat_put_timespec(&out, rmtp)) |
1da177e4 | 861 | return -EFAULT; |
1711ef38 TA |
862 | |
863 | if (err == -ERESTART_RESTARTBLOCK) { | |
864 | restart = ¤t_thread_info()->restart_block; | |
865 | restart->fn = compat_clock_nanosleep_restart; | |
029a07e0 | 866 | restart->nanosleep.compat_rmtp = rmtp; |
1711ef38 | 867 | } |
bd3a8492 DW |
868 | return err; |
869 | } | |
1da177e4 LT |
870 | |
871 | /* | |
872 | * We currently only need the following fields from the sigevent | |
873 | * structure: sigev_value, sigev_signo, sig_notify and (sometimes | |
874 | * sigev_notify_thread_id). The others are handled in user mode. | |
875 | * We also assume that copying sigev_value.sival_int is sufficient | |
876 | * to keep all the bits of sigev_value.sival_ptr intact. | |
877 | */ | |
878 | int get_compat_sigevent(struct sigevent *event, | |
879 | const struct compat_sigevent __user *u_event) | |
880 | { | |
51410d3c | 881 | memset(event, 0, sizeof(*event)); |
1da177e4 LT |
882 | return (!access_ok(VERIFY_READ, u_event, sizeof(*u_event)) || |
883 | __get_user(event->sigev_value.sival_int, | |
884 | &u_event->sigev_value.sival_int) || | |
885 | __get_user(event->sigev_signo, &u_event->sigev_signo) || | |
886 | __get_user(event->sigev_notify, &u_event->sigev_notify) || | |
887 | __get_user(event->sigev_notify_thread_id, | |
888 | &u_event->sigev_notify_thread_id)) | |
889 | ? -EFAULT : 0; | |
890 | } | |
891 | ||
5fa3839a | 892 | long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask, |
1da177e4 LT |
893 | unsigned long bitmap_size) |
894 | { | |
895 | int i, j; | |
896 | unsigned long m; | |
897 | compat_ulong_t um; | |
898 | unsigned long nr_compat_longs; | |
899 | ||
900 | /* align bitmap up to nearest compat_long_t boundary */ | |
901 | bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG); | |
902 | ||
903 | if (!access_ok(VERIFY_READ, umask, bitmap_size / 8)) | |
904 | return -EFAULT; | |
905 | ||
906 | nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size); | |
907 | ||
908 | for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) { | |
909 | m = 0; | |
910 | ||
911 | for (j = 0; j < sizeof(m)/sizeof(um); j++) { | |
912 | /* | |
913 | * We dont want to read past the end of the userspace | |
914 | * bitmap. We must however ensure the end of the | |
915 | * kernel bitmap is zeroed. | |
916 | */ | |
917 | if (nr_compat_longs-- > 0) { | |
918 | if (__get_user(um, umask)) | |
919 | return -EFAULT; | |
920 | } else { | |
921 | um = 0; | |
922 | } | |
923 | ||
924 | umask++; | |
925 | m |= (long)um << (j * BITS_PER_COMPAT_LONG); | |
926 | } | |
927 | *mask++ = m; | |
928 | } | |
929 | ||
930 | return 0; | |
931 | } | |
932 | ||
933 | long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask, | |
934 | unsigned long bitmap_size) | |
935 | { | |
936 | int i, j; | |
937 | unsigned long m; | |
938 | compat_ulong_t um; | |
939 | unsigned long nr_compat_longs; | |
940 | ||
941 | /* align bitmap up to nearest compat_long_t boundary */ | |
942 | bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG); | |
943 | ||
944 | if (!access_ok(VERIFY_WRITE, umask, bitmap_size / 8)) | |
945 | return -EFAULT; | |
946 | ||
947 | nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size); | |
948 | ||
949 | for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) { | |
950 | m = *mask++; | |
951 | ||
952 | for (j = 0; j < sizeof(m)/sizeof(um); j++) { | |
953 | um = m; | |
954 | ||
955 | /* | |
956 | * We dont want to write past the end of the userspace | |
957 | * bitmap. | |
958 | */ | |
959 | if (nr_compat_longs-- > 0) { | |
960 | if (__put_user(um, umask)) | |
961 | return -EFAULT; | |
962 | } | |
963 | ||
964 | umask++; | |
965 | m >>= 4*sizeof(um); | |
966 | m >>= 4*sizeof(um); | |
967 | } | |
968 | } | |
969 | ||
970 | return 0; | |
971 | } | |
972 | ||
973 | void | |
322a56cb | 974 | sigset_from_compat(sigset_t *set, const compat_sigset_t *compat) |
1da177e4 LT |
975 | { |
976 | switch (_NSIG_WORDS) { | |
1da177e4 LT |
977 | case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32 ); |
978 | case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32 ); | |
979 | case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32 ); | |
980 | case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32 ); | |
1da177e4 LT |
981 | } |
982 | } | |
1dda606c | 983 | EXPORT_SYMBOL_GPL(sigset_from_compat); |
1da177e4 | 984 | |
322a56cb AV |
985 | void |
986 | sigset_to_compat(compat_sigset_t *compat, const sigset_t *set) | |
987 | { | |
988 | switch (_NSIG_WORDS) { | |
989 | case 4: compat->sig[7] = (set->sig[3] >> 32); compat->sig[6] = set->sig[3]; | |
990 | case 3: compat->sig[5] = (set->sig[2] >> 32); compat->sig[4] = set->sig[2]; | |
991 | case 2: compat->sig[3] = (set->sig[1] >> 32); compat->sig[2] = set->sig[1]; | |
992 | case 1: compat->sig[1] = (set->sig[0] >> 32); compat->sig[0] = set->sig[0]; | |
993 | } | |
994 | } | |
995 | ||
28d27f2d AV |
996 | COMPAT_SYSCALL_DEFINE4(rt_sigtimedwait, compat_sigset_t __user *, uthese, |
997 | struct compat_siginfo __user *, uinfo, | |
998 | struct compat_timespec __user *, uts, compat_size_t, sigsetsize) | |
1da177e4 LT |
999 | { |
1000 | compat_sigset_t s32; | |
1001 | sigset_t s; | |
1da177e4 LT |
1002 | struct timespec t; |
1003 | siginfo_t info; | |
943df148 | 1004 | long ret; |
1da177e4 LT |
1005 | |
1006 | if (sigsetsize != sizeof(sigset_t)) | |
1007 | return -EINVAL; | |
1008 | ||
1009 | if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t))) | |
1010 | return -EFAULT; | |
1011 | sigset_from_compat(&s, &s32); | |
1da177e4 LT |
1012 | |
1013 | if (uts) { | |
b2ddedcd | 1014 | if (compat_get_timespec(&t, uts)) |
1da177e4 | 1015 | return -EFAULT; |
1da177e4 LT |
1016 | } |
1017 | ||
943df148 | 1018 | ret = do_sigtimedwait(&s, &info, uts ? &t : NULL); |
1da177e4 | 1019 | |
943df148 ON |
1020 | if (ret > 0 && uinfo) { |
1021 | if (copy_siginfo_to_user32(uinfo, &info)) | |
1022 | ret = -EFAULT; | |
1da177e4 | 1023 | } |
943df148 | 1024 | |
1da177e4 | 1025 | return ret; |
62ab4505 TG |
1026 | } |
1027 | ||
1da177e4 LT |
1028 | #ifdef __ARCH_WANT_COMPAT_SYS_TIME |
1029 | ||
1030 | /* compat_time_t is a 32 bit "long" and needs to get converted. */ | |
1031 | ||
62a6fa97 | 1032 | COMPAT_SYSCALL_DEFINE1(time, compat_time_t __user *, tloc) |
1da177e4 LT |
1033 | { |
1034 | compat_time_t i; | |
1035 | struct timeval tv; | |
1036 | ||
1037 | do_gettimeofday(&tv); | |
1038 | i = tv.tv_sec; | |
1039 | ||
1040 | if (tloc) { | |
1041 | if (put_user(i,tloc)) | |
e3d5a27d | 1042 | return -EFAULT; |
1da177e4 | 1043 | } |
e3d5a27d | 1044 | force_successful_syscall_return(); |
1da177e4 LT |
1045 | return i; |
1046 | } | |
1047 | ||
62a6fa97 | 1048 | COMPAT_SYSCALL_DEFINE1(stime, compat_time_t __user *, tptr) |
1da177e4 LT |
1049 | { |
1050 | struct timespec tv; | |
1051 | int err; | |
1052 | ||
1053 | if (get_user(tv.tv_sec, tptr)) | |
1054 | return -EFAULT; | |
1055 | ||
1056 | tv.tv_nsec = 0; | |
1057 | ||
1058 | err = security_settime(&tv, NULL); | |
1059 | if (err) | |
1060 | return err; | |
1061 | ||
1062 | do_settimeofday(&tv); | |
1063 | return 0; | |
1064 | } | |
1065 | ||
1066 | #endif /* __ARCH_WANT_COMPAT_SYS_TIME */ | |
150256d8 | 1067 | |
62a6fa97 | 1068 | COMPAT_SYSCALL_DEFINE1(adjtimex, struct compat_timex __user *, utp) |
3158e941 SR |
1069 | { |
1070 | struct timex txc; | |
65f5d80b | 1071 | int err, ret; |
3158e941 | 1072 | |
65f5d80b RC |
1073 | err = compat_get_timex(&txc, utp); |
1074 | if (err) | |
1075 | return err; | |
3158e941 SR |
1076 | |
1077 | ret = do_adjtimex(&txc); | |
1078 | ||
65f5d80b RC |
1079 | err = compat_put_timex(utp, &txc); |
1080 | if (err) | |
1081 | return err; | |
3158e941 SR |
1082 | |
1083 | return ret; | |
1084 | } | |
1b2db9fb CL |
1085 | |
1086 | #ifdef CONFIG_NUMA | |
2f2728f6 HC |
1087 | COMPAT_SYSCALL_DEFINE6(move_pages, pid_t, pid, compat_ulong_t, nr_pages, |
1088 | compat_uptr_t __user *, pages32, | |
1089 | const int __user *, nodes, | |
1090 | int __user *, status, | |
1091 | int, flags) | |
1b2db9fb CL |
1092 | { |
1093 | const void __user * __user *pages; | |
1094 | int i; | |
1095 | ||
1096 | pages = compat_alloc_user_space(nr_pages * sizeof(void *)); | |
1097 | for (i = 0; i < nr_pages; i++) { | |
1098 | compat_uptr_t p; | |
1099 | ||
9216dfad | 1100 | if (get_user(p, pages32 + i) || |
1b2db9fb CL |
1101 | put_user(compat_ptr(p), pages + i)) |
1102 | return -EFAULT; | |
1103 | } | |
1104 | return sys_move_pages(pid, nr_pages, pages, nodes, status, flags); | |
1105 | } | |
3fd59397 | 1106 | |
62a6fa97 HC |
1107 | COMPAT_SYSCALL_DEFINE4(migrate_pages, compat_pid_t, pid, |
1108 | compat_ulong_t, maxnode, | |
1109 | const compat_ulong_t __user *, old_nodes, | |
1110 | const compat_ulong_t __user *, new_nodes) | |
3fd59397 SR |
1111 | { |
1112 | unsigned long __user *old = NULL; | |
1113 | unsigned long __user *new = NULL; | |
1114 | nodemask_t tmp_mask; | |
1115 | unsigned long nr_bits; | |
1116 | unsigned long size; | |
1117 | ||
1118 | nr_bits = min_t(unsigned long, maxnode - 1, MAX_NUMNODES); | |
1119 | size = ALIGN(nr_bits, BITS_PER_LONG) / 8; | |
1120 | if (old_nodes) { | |
1121 | if (compat_get_bitmap(nodes_addr(tmp_mask), old_nodes, nr_bits)) | |
1122 | return -EFAULT; | |
1123 | old = compat_alloc_user_space(new_nodes ? size * 2 : size); | |
1124 | if (new_nodes) | |
1125 | new = old + size / sizeof(unsigned long); | |
1126 | if (copy_to_user(old, nodes_addr(tmp_mask), size)) | |
1127 | return -EFAULT; | |
1128 | } | |
1129 | if (new_nodes) { | |
1130 | if (compat_get_bitmap(nodes_addr(tmp_mask), new_nodes, nr_bits)) | |
1131 | return -EFAULT; | |
1132 | if (new == NULL) | |
1133 | new = compat_alloc_user_space(size); | |
1134 | if (copy_to_user(new, nodes_addr(tmp_mask), size)) | |
1135 | return -EFAULT; | |
1136 | } | |
1137 | return sys_migrate_pages(pid, nr_bits + 1, old, new); | |
1138 | } | |
1b2db9fb | 1139 | #endif |
d4d23add | 1140 | |
6883da8c AV |
1141 | COMPAT_SYSCALL_DEFINE2(sched_rr_get_interval, |
1142 | compat_pid_t, pid, | |
1143 | struct compat_timespec __user *, interval) | |
0ad50c38 CM |
1144 | { |
1145 | struct timespec t; | |
1146 | int ret; | |
1147 | mm_segment_t old_fs = get_fs(); | |
1148 | ||
1149 | set_fs(KERNEL_DS); | |
63d9c273 | 1150 | ret = sys_sched_rr_get_interval(pid, (struct timespec __force_user *)&t); |
0ad50c38 | 1151 | set_fs(old_fs); |
81993e81 | 1152 | if (compat_put_timespec(&t, interval)) |
0ad50c38 CM |
1153 | return -EFAULT; |
1154 | return ret; | |
1155 | } | |
0ad50c38 | 1156 | |
c41d68a5 PA |
1157 | /* |
1158 | * Allocate user-space memory for the duration of a single system call, | |
1159 | * in order to marshall parameters inside a compat thunk. | |
1160 | */ | |
1161 | void __user *compat_alloc_user_space(unsigned long len) | |
1162 | { | |
1163 | void __user *ptr; | |
1164 | ||
1165 | /* If len would occupy more than half of the entire compat space... */ | |
1166 | if (unlikely(len > (((compat_uptr_t)~0) >> 1))) | |
1167 | return NULL; | |
1168 | ||
1169 | ptr = arch_compat_alloc_user_space(len); | |
1170 | ||
1171 | if (unlikely(!access_ok(VERIFY_WRITE, ptr, len))) | |
1172 | return NULL; | |
1173 | ||
1174 | return ptr; | |
1175 | } | |
1176 | EXPORT_SYMBOL_GPL(compat_alloc_user_space); |