]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * linux/kernel/compat.c | |
3 | * | |
4 | * Kernel compatibililty routines for e.g. 32 bit syscall support | |
5 | * on 64 bit kernels. | |
6 | * | |
7 | * Copyright (C) 2002-2003 Stephen Rothwell, IBM Corporation | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License version 2 as | |
11 | * published by the Free Software Foundation. | |
12 | */ | |
13 | ||
14 | #include <linux/linkage.h> | |
15 | #include <linux/compat.h> | |
16 | #include <linux/errno.h> | |
17 | #include <linux/time.h> | |
18 | #include <linux/signal.h> | |
19 | #include <linux/sched.h> /* for MAX_SCHEDULE_TIMEOUT */ | |
20 | #include <linux/syscalls.h> | |
21 | #include <linux/unistd.h> | |
22 | #include <linux/security.h> | |
23 | #include <linux/timex.h> | |
24 | #include <linux/export.h> | |
25 | #include <linux/migrate.h> | |
26 | #include <linux/posix-timers.h> | |
27 | #include <linux/times.h> | |
28 | #include <linux/ptrace.h> | |
29 | #include <linux/gfp.h> | |
30 | ||
31 | #include <linux/uaccess.h> | |
32 | ||
33 | static 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 | ||
63 | static 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 | ||
91 | COMPAT_SYSCALL_DEFINE2(gettimeofday, struct compat_timeval __user *, tv, | |
92 | struct timezone __user *, tz) | |
93 | { | |
94 | if (tv) { | |
95 | struct timeval ktv; | |
96 | do_gettimeofday(&ktv); | |
97 | if (compat_put_timeval(&ktv, tv)) | |
98 | 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 | ||
108 | COMPAT_SYSCALL_DEFINE2(settimeofday, struct compat_timeval __user *, tv, | |
109 | struct timezone __user *, tz) | |
110 | { | |
111 | struct timespec64 new_ts; | |
112 | struct timeval user_tv; | |
113 | struct timezone new_tz; | |
114 | ||
115 | if (tv) { | |
116 | if (compat_get_timeval(&user_tv, tv)) | |
117 | return -EFAULT; | |
118 | new_ts.tv_sec = user_tv.tv_sec; | |
119 | new_ts.tv_nsec = user_tv.tv_usec * NSEC_PER_USEC; | |
120 | } | |
121 | if (tz) { | |
122 | if (copy_from_user(&new_tz, tz, sizeof(*tz))) | |
123 | return -EFAULT; | |
124 | } | |
125 | ||
126 | return do_sys_settimeofday64(tv ? &new_ts : NULL, tz ? &new_tz : NULL); | |
127 | } | |
128 | ||
129 | static int __compat_get_timeval(struct timeval *tv, const struct compat_timeval __user *ctv) | |
130 | { | |
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 | } | |
135 | ||
136 | static int __compat_put_timeval(const struct timeval *tv, struct compat_timeval __user *ctv) | |
137 | { | |
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 | } | |
142 | ||
143 | static int __compat_get_timespec(struct timespec *ts, const struct compat_timespec __user *cts) | |
144 | { | |
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 | ||
150 | static int __compat_put_timespec(const struct timespec *ts, struct compat_timespec __user *cts) | |
151 | { | |
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 | ||
157 | int compat_get_timeval(struct timeval *tv, const void __user *utv) | |
158 | { | |
159 | if (COMPAT_USE_64BIT_TIME) | |
160 | return copy_from_user(tv, utv, sizeof(*tv)) ? -EFAULT : 0; | |
161 | else | |
162 | return __compat_get_timeval(tv, utv); | |
163 | } | |
164 | EXPORT_SYMBOL_GPL(compat_get_timeval); | |
165 | ||
166 | int compat_put_timeval(const struct timeval *tv, void __user *utv) | |
167 | { | |
168 | if (COMPAT_USE_64BIT_TIME) | |
169 | return copy_to_user(utv, tv, sizeof(*tv)) ? -EFAULT : 0; | |
170 | else | |
171 | return __compat_put_timeval(tv, utv); | |
172 | } | |
173 | EXPORT_SYMBOL_GPL(compat_put_timeval); | |
174 | ||
175 | int compat_get_timespec(struct timespec *ts, const void __user *uts) | |
176 | { | |
177 | if (COMPAT_USE_64BIT_TIME) | |
178 | return copy_from_user(ts, uts, sizeof(*ts)) ? -EFAULT : 0; | |
179 | else | |
180 | return __compat_get_timespec(ts, uts); | |
181 | } | |
182 | EXPORT_SYMBOL_GPL(compat_get_timespec); | |
183 | ||
184 | int compat_put_timespec(const struct timespec *ts, void __user *uts) | |
185 | { | |
186 | if (COMPAT_USE_64BIT_TIME) | |
187 | return copy_to_user(uts, ts, sizeof(*ts)) ? -EFAULT : 0; | |
188 | else | |
189 | return __compat_put_timespec(ts, uts); | |
190 | } | |
191 | EXPORT_SYMBOL_GPL(compat_put_timespec); | |
192 | ||
193 | int 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 | ||
216 | static inline long get_compat_itimerval(struct itimerval *o, | |
217 | struct compat_itimerval __user *i) | |
218 | { | |
219 | return (!access_ok(VERIFY_READ, i, sizeof(*i)) || | |
220 | (__get_user(o->it_interval.tv_sec, &i->it_interval.tv_sec) | | |
221 | __get_user(o->it_interval.tv_usec, &i->it_interval.tv_usec) | | |
222 | __get_user(o->it_value.tv_sec, &i->it_value.tv_sec) | | |
223 | __get_user(o->it_value.tv_usec, &i->it_value.tv_usec))); | |
224 | } | |
225 | ||
226 | static inline long put_compat_itimerval(struct compat_itimerval __user *o, | |
227 | struct itimerval *i) | |
228 | { | |
229 | return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) || | |
230 | (__put_user(i->it_interval.tv_sec, &o->it_interval.tv_sec) | | |
231 | __put_user(i->it_interval.tv_usec, &o->it_interval.tv_usec) | | |
232 | __put_user(i->it_value.tv_sec, &o->it_value.tv_sec) | | |
233 | __put_user(i->it_value.tv_usec, &o->it_value.tv_usec))); | |
234 | } | |
235 | ||
236 | asmlinkage long sys_ni_posix_timers(void); | |
237 | ||
238 | COMPAT_SYSCALL_DEFINE2(getitimer, int, which, | |
239 | struct compat_itimerval __user *, it) | |
240 | { | |
241 | struct itimerval kit; | |
242 | int error; | |
243 | ||
244 | if (!IS_ENABLED(CONFIG_POSIX_TIMERS)) | |
245 | return sys_ni_posix_timers(); | |
246 | ||
247 | error = do_getitimer(which, &kit); | |
248 | if (!error && put_compat_itimerval(it, &kit)) | |
249 | error = -EFAULT; | |
250 | return error; | |
251 | } | |
252 | ||
253 | COMPAT_SYSCALL_DEFINE3(setitimer, int, which, | |
254 | struct compat_itimerval __user *, in, | |
255 | struct compat_itimerval __user *, out) | |
256 | { | |
257 | struct itimerval kin, kout; | |
258 | int error; | |
259 | ||
260 | if (!IS_ENABLED(CONFIG_POSIX_TIMERS)) | |
261 | return sys_ni_posix_timers(); | |
262 | ||
263 | if (in) { | |
264 | if (get_compat_itimerval(&kin, in)) | |
265 | return -EFAULT; | |
266 | } else | |
267 | memset(&kin, 0, sizeof(kin)); | |
268 | ||
269 | error = do_setitimer(which, &kin, out ? &kout : NULL); | |
270 | if (error || !out) | |
271 | return error; | |
272 | if (put_compat_itimerval(out, &kout)) | |
273 | return -EFAULT; | |
274 | return 0; | |
275 | } | |
276 | ||
277 | static compat_clock_t clock_t_to_compat_clock_t(clock_t x) | |
278 | { | |
279 | return compat_jiffies_to_clock_t(clock_t_to_jiffies(x)); | |
280 | } | |
281 | ||
282 | COMPAT_SYSCALL_DEFINE1(times, struct compat_tms __user *, tbuf) | |
283 | { | |
284 | if (tbuf) { | |
285 | struct tms tms; | |
286 | struct compat_tms tmp; | |
287 | ||
288 | do_sys_times(&tms); | |
289 | /* Convert our struct tms to the compat version. */ | |
290 | tmp.tms_utime = clock_t_to_compat_clock_t(tms.tms_utime); | |
291 | tmp.tms_stime = clock_t_to_compat_clock_t(tms.tms_stime); | |
292 | tmp.tms_cutime = clock_t_to_compat_clock_t(tms.tms_cutime); | |
293 | tmp.tms_cstime = clock_t_to_compat_clock_t(tms.tms_cstime); | |
294 | if (copy_to_user(tbuf, &tmp, sizeof(tmp))) | |
295 | return -EFAULT; | |
296 | } | |
297 | force_successful_syscall_return(); | |
298 | return compat_jiffies_to_clock_t(jiffies); | |
299 | } | |
300 | ||
301 | #ifdef __ARCH_WANT_SYS_SIGPENDING | |
302 | ||
303 | /* | |
304 | * Assumption: old_sigset_t and compat_old_sigset_t are both | |
305 | * types that can be passed to put_user()/get_user(). | |
306 | */ | |
307 | ||
308 | COMPAT_SYSCALL_DEFINE1(sigpending, compat_old_sigset_t __user *, set) | |
309 | { | |
310 | old_sigset_t s; | |
311 | long ret; | |
312 | mm_segment_t old_fs = get_fs(); | |
313 | ||
314 | set_fs(KERNEL_DS); | |
315 | ret = sys_sigpending((old_sigset_t __user *) &s); | |
316 | set_fs(old_fs); | |
317 | if (ret == 0) | |
318 | ret = put_user(s, set); | |
319 | return ret; | |
320 | } | |
321 | ||
322 | #endif | |
323 | ||
324 | #ifdef __ARCH_WANT_SYS_SIGPROCMASK | |
325 | ||
326 | /* | |
327 | * sys_sigprocmask SIG_SETMASK sets the first (compat) word of the | |
328 | * blocked set of signals to the supplied signal set | |
329 | */ | |
330 | static inline void compat_sig_setmask(sigset_t *blocked, compat_sigset_word set) | |
331 | { | |
332 | memcpy(blocked->sig, &set, sizeof(set)); | |
333 | } | |
334 | ||
335 | COMPAT_SYSCALL_DEFINE3(sigprocmask, int, how, | |
336 | compat_old_sigset_t __user *, nset, | |
337 | compat_old_sigset_t __user *, oset) | |
338 | { | |
339 | old_sigset_t old_set, new_set; | |
340 | sigset_t new_blocked; | |
341 | ||
342 | old_set = current->blocked.sig[0]; | |
343 | ||
344 | if (nset) { | |
345 | if (get_user(new_set, nset)) | |
346 | return -EFAULT; | |
347 | new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP)); | |
348 | ||
349 | new_blocked = current->blocked; | |
350 | ||
351 | switch (how) { | |
352 | case SIG_BLOCK: | |
353 | sigaddsetmask(&new_blocked, new_set); | |
354 | break; | |
355 | case SIG_UNBLOCK: | |
356 | sigdelsetmask(&new_blocked, new_set); | |
357 | break; | |
358 | case SIG_SETMASK: | |
359 | compat_sig_setmask(&new_blocked, new_set); | |
360 | break; | |
361 | default: | |
362 | return -EINVAL; | |
363 | } | |
364 | ||
365 | set_current_blocked(&new_blocked); | |
366 | } | |
367 | ||
368 | if (oset) { | |
369 | if (put_user(old_set, oset)) | |
370 | return -EFAULT; | |
371 | } | |
372 | ||
373 | return 0; | |
374 | } | |
375 | ||
376 | #endif | |
377 | ||
378 | COMPAT_SYSCALL_DEFINE2(setrlimit, unsigned int, resource, | |
379 | struct compat_rlimit __user *, rlim) | |
380 | { | |
381 | struct rlimit r; | |
382 | ||
383 | if (!access_ok(VERIFY_READ, rlim, sizeof(*rlim)) || | |
384 | __get_user(r.rlim_cur, &rlim->rlim_cur) || | |
385 | __get_user(r.rlim_max, &rlim->rlim_max)) | |
386 | return -EFAULT; | |
387 | ||
388 | if (r.rlim_cur == COMPAT_RLIM_INFINITY) | |
389 | r.rlim_cur = RLIM_INFINITY; | |
390 | if (r.rlim_max == COMPAT_RLIM_INFINITY) | |
391 | r.rlim_max = RLIM_INFINITY; | |
392 | return do_prlimit(current, resource, &r, NULL); | |
393 | } | |
394 | ||
395 | #ifdef COMPAT_RLIM_OLD_INFINITY | |
396 | ||
397 | COMPAT_SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource, | |
398 | struct compat_rlimit __user *, rlim) | |
399 | { | |
400 | struct rlimit r; | |
401 | int ret; | |
402 | mm_segment_t old_fs = get_fs(); | |
403 | ||
404 | set_fs(KERNEL_DS); | |
405 | ret = sys_old_getrlimit(resource, (struct rlimit __user *)&r); | |
406 | set_fs(old_fs); | |
407 | ||
408 | if (!ret) { | |
409 | if (r.rlim_cur > COMPAT_RLIM_OLD_INFINITY) | |
410 | r.rlim_cur = COMPAT_RLIM_INFINITY; | |
411 | if (r.rlim_max > COMPAT_RLIM_OLD_INFINITY) | |
412 | r.rlim_max = COMPAT_RLIM_INFINITY; | |
413 | ||
414 | if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) || | |
415 | __put_user(r.rlim_cur, &rlim->rlim_cur) || | |
416 | __put_user(r.rlim_max, &rlim->rlim_max)) | |
417 | return -EFAULT; | |
418 | } | |
419 | return ret; | |
420 | } | |
421 | ||
422 | #endif | |
423 | ||
424 | COMPAT_SYSCALL_DEFINE2(getrlimit, unsigned int, resource, | |
425 | struct compat_rlimit __user *, rlim) | |
426 | { | |
427 | struct rlimit r; | |
428 | int ret; | |
429 | ||
430 | ret = do_prlimit(current, resource, NULL, &r); | |
431 | if (!ret) { | |
432 | if (r.rlim_cur > COMPAT_RLIM_INFINITY) | |
433 | r.rlim_cur = COMPAT_RLIM_INFINITY; | |
434 | if (r.rlim_max > COMPAT_RLIM_INFINITY) | |
435 | r.rlim_max = COMPAT_RLIM_INFINITY; | |
436 | ||
437 | if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) || | |
438 | __put_user(r.rlim_cur, &rlim->rlim_cur) || | |
439 | __put_user(r.rlim_max, &rlim->rlim_max)) | |
440 | return -EFAULT; | |
441 | } | |
442 | return ret; | |
443 | } | |
444 | ||
445 | int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru) | |
446 | { | |
447 | if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)) || | |
448 | __put_user(r->ru_utime.tv_sec, &ru->ru_utime.tv_sec) || | |
449 | __put_user(r->ru_utime.tv_usec, &ru->ru_utime.tv_usec) || | |
450 | __put_user(r->ru_stime.tv_sec, &ru->ru_stime.tv_sec) || | |
451 | __put_user(r->ru_stime.tv_usec, &ru->ru_stime.tv_usec) || | |
452 | __put_user(r->ru_maxrss, &ru->ru_maxrss) || | |
453 | __put_user(r->ru_ixrss, &ru->ru_ixrss) || | |
454 | __put_user(r->ru_idrss, &ru->ru_idrss) || | |
455 | __put_user(r->ru_isrss, &ru->ru_isrss) || | |
456 | __put_user(r->ru_minflt, &ru->ru_minflt) || | |
457 | __put_user(r->ru_majflt, &ru->ru_majflt) || | |
458 | __put_user(r->ru_nswap, &ru->ru_nswap) || | |
459 | __put_user(r->ru_inblock, &ru->ru_inblock) || | |
460 | __put_user(r->ru_oublock, &ru->ru_oublock) || | |
461 | __put_user(r->ru_msgsnd, &ru->ru_msgsnd) || | |
462 | __put_user(r->ru_msgrcv, &ru->ru_msgrcv) || | |
463 | __put_user(r->ru_nsignals, &ru->ru_nsignals) || | |
464 | __put_user(r->ru_nvcsw, &ru->ru_nvcsw) || | |
465 | __put_user(r->ru_nivcsw, &ru->ru_nivcsw)) | |
466 | return -EFAULT; | |
467 | return 0; | |
468 | } | |
469 | ||
470 | COMPAT_SYSCALL_DEFINE4(wait4, | |
471 | compat_pid_t, pid, | |
472 | compat_uint_t __user *, stat_addr, | |
473 | int, options, | |
474 | struct compat_rusage __user *, ru) | |
475 | { | |
476 | if (!ru) { | |
477 | return sys_wait4(pid, stat_addr, options, NULL); | |
478 | } else { | |
479 | struct rusage r; | |
480 | int ret; | |
481 | unsigned int status; | |
482 | mm_segment_t old_fs = get_fs(); | |
483 | ||
484 | set_fs (KERNEL_DS); | |
485 | ret = sys_wait4(pid, | |
486 | (stat_addr ? | |
487 | (unsigned int __user *) &status : NULL), | |
488 | options, (struct rusage __user *) &r); | |
489 | set_fs (old_fs); | |
490 | ||
491 | if (ret > 0) { | |
492 | if (put_compat_rusage(&r, ru)) | |
493 | return -EFAULT; | |
494 | if (stat_addr && put_user(status, stat_addr)) | |
495 | return -EFAULT; | |
496 | } | |
497 | return ret; | |
498 | } | |
499 | } | |
500 | ||
501 | COMPAT_SYSCALL_DEFINE5(waitid, | |
502 | int, which, compat_pid_t, pid, | |
503 | struct compat_siginfo __user *, uinfo, int, options, | |
504 | struct compat_rusage __user *, uru) | |
505 | { | |
506 | siginfo_t info; | |
507 | struct rusage ru; | |
508 | long ret; | |
509 | mm_segment_t old_fs = get_fs(); | |
510 | ||
511 | memset(&info, 0, sizeof(info)); | |
512 | ||
513 | set_fs(KERNEL_DS); | |
514 | ret = sys_waitid(which, pid, (siginfo_t __user *)&info, options, | |
515 | uru ? (struct rusage __user *)&ru : NULL); | |
516 | set_fs(old_fs); | |
517 | ||
518 | if ((ret < 0) || (info.si_signo == 0)) | |
519 | return ret; | |
520 | ||
521 | if (uru) { | |
522 | /* sys_waitid() overwrites everything in ru */ | |
523 | if (COMPAT_USE_64BIT_TIME) | |
524 | ret = copy_to_user(uru, &ru, sizeof(ru)); | |
525 | else | |
526 | ret = put_compat_rusage(&ru, uru); | |
527 | if (ret) | |
528 | return -EFAULT; | |
529 | } | |
530 | ||
531 | BUG_ON(info.si_code & __SI_MASK); | |
532 | info.si_code |= __SI_CHLD; | |
533 | return copy_siginfo_to_user32(uinfo, &info); | |
534 | } | |
535 | ||
536 | static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr, | |
537 | unsigned len, struct cpumask *new_mask) | |
538 | { | |
539 | unsigned long *k; | |
540 | ||
541 | if (len < cpumask_size()) | |
542 | memset(new_mask, 0, cpumask_size()); | |
543 | else if (len > cpumask_size()) | |
544 | len = cpumask_size(); | |
545 | ||
546 | k = cpumask_bits(new_mask); | |
547 | return compat_get_bitmap(k, user_mask_ptr, len * 8); | |
548 | } | |
549 | ||
550 | COMPAT_SYSCALL_DEFINE3(sched_setaffinity, compat_pid_t, pid, | |
551 | unsigned int, len, | |
552 | compat_ulong_t __user *, user_mask_ptr) | |
553 | { | |
554 | cpumask_var_t new_mask; | |
555 | int retval; | |
556 | ||
557 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
558 | return -ENOMEM; | |
559 | ||
560 | retval = compat_get_user_cpu_mask(user_mask_ptr, len, new_mask); | |
561 | if (retval) | |
562 | goto out; | |
563 | ||
564 | retval = sched_setaffinity(pid, new_mask); | |
565 | out: | |
566 | free_cpumask_var(new_mask); | |
567 | return retval; | |
568 | } | |
569 | ||
570 | COMPAT_SYSCALL_DEFINE3(sched_getaffinity, compat_pid_t, pid, unsigned int, len, | |
571 | compat_ulong_t __user *, user_mask_ptr) | |
572 | { | |
573 | int ret; | |
574 | cpumask_var_t mask; | |
575 | ||
576 | if ((len * BITS_PER_BYTE) < nr_cpu_ids) | |
577 | return -EINVAL; | |
578 | if (len & (sizeof(compat_ulong_t)-1)) | |
579 | return -EINVAL; | |
580 | ||
581 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) | |
582 | return -ENOMEM; | |
583 | ||
584 | ret = sched_getaffinity(pid, mask); | |
585 | if (ret == 0) { | |
586 | size_t retlen = min_t(size_t, len, cpumask_size()); | |
587 | ||
588 | if (compat_put_bitmap(user_mask_ptr, cpumask_bits(mask), retlen * 8)) | |
589 | ret = -EFAULT; | |
590 | else | |
591 | ret = retlen; | |
592 | } | |
593 | free_cpumask_var(mask); | |
594 | ||
595 | return ret; | |
596 | } | |
597 | ||
598 | int get_compat_itimerspec(struct itimerspec *dst, | |
599 | const struct compat_itimerspec __user *src) | |
600 | { | |
601 | if (__compat_get_timespec(&dst->it_interval, &src->it_interval) || | |
602 | __compat_get_timespec(&dst->it_value, &src->it_value)) | |
603 | return -EFAULT; | |
604 | return 0; | |
605 | } | |
606 | ||
607 | int put_compat_itimerspec(struct compat_itimerspec __user *dst, | |
608 | const struct itimerspec *src) | |
609 | { | |
610 | if (__compat_put_timespec(&src->it_interval, &dst->it_interval) || | |
611 | __compat_put_timespec(&src->it_value, &dst->it_value)) | |
612 | return -EFAULT; | |
613 | return 0; | |
614 | } | |
615 | ||
616 | COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock, | |
617 | struct compat_sigevent __user *, timer_event_spec, | |
618 | timer_t __user *, created_timer_id) | |
619 | { | |
620 | struct sigevent __user *event = NULL; | |
621 | ||
622 | if (timer_event_spec) { | |
623 | struct sigevent kevent; | |
624 | ||
625 | event = compat_alloc_user_space(sizeof(*event)); | |
626 | if (get_compat_sigevent(&kevent, timer_event_spec) || | |
627 | copy_to_user(event, &kevent, sizeof(*event))) | |
628 | return -EFAULT; | |
629 | } | |
630 | ||
631 | return sys_timer_create(which_clock, event, created_timer_id); | |
632 | } | |
633 | ||
634 | COMPAT_SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags, | |
635 | struct compat_itimerspec __user *, new, | |
636 | struct compat_itimerspec __user *, old) | |
637 | { | |
638 | long err; | |
639 | mm_segment_t oldfs; | |
640 | struct itimerspec newts, oldts; | |
641 | ||
642 | if (!new) | |
643 | return -EINVAL; | |
644 | if (get_compat_itimerspec(&newts, new)) | |
645 | return -EFAULT; | |
646 | oldfs = get_fs(); | |
647 | set_fs(KERNEL_DS); | |
648 | err = sys_timer_settime(timer_id, flags, | |
649 | (struct itimerspec __user *) &newts, | |
650 | (struct itimerspec __user *) &oldts); | |
651 | set_fs(oldfs); | |
652 | if (!err && old && put_compat_itimerspec(old, &oldts)) | |
653 | return -EFAULT; | |
654 | return err; | |
655 | } | |
656 | ||
657 | COMPAT_SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id, | |
658 | struct compat_itimerspec __user *, setting) | |
659 | { | |
660 | long err; | |
661 | mm_segment_t oldfs; | |
662 | struct itimerspec ts; | |
663 | ||
664 | oldfs = get_fs(); | |
665 | set_fs(KERNEL_DS); | |
666 | err = sys_timer_gettime(timer_id, | |
667 | (struct itimerspec __user *) &ts); | |
668 | set_fs(oldfs); | |
669 | if (!err && put_compat_itimerspec(setting, &ts)) | |
670 | return -EFAULT; | |
671 | return err; | |
672 | } | |
673 | ||
674 | COMPAT_SYSCALL_DEFINE2(clock_settime, clockid_t, which_clock, | |
675 | struct compat_timespec __user *, tp) | |
676 | { | |
677 | long err; | |
678 | mm_segment_t oldfs; | |
679 | struct timespec ts; | |
680 | ||
681 | if (compat_get_timespec(&ts, tp)) | |
682 | return -EFAULT; | |
683 | oldfs = get_fs(); | |
684 | set_fs(KERNEL_DS); | |
685 | err = sys_clock_settime(which_clock, | |
686 | (struct timespec __user *) &ts); | |
687 | set_fs(oldfs); | |
688 | return err; | |
689 | } | |
690 | ||
691 | COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock, | |
692 | struct compat_timespec __user *, tp) | |
693 | { | |
694 | long err; | |
695 | mm_segment_t oldfs; | |
696 | struct timespec ts; | |
697 | ||
698 | oldfs = get_fs(); | |
699 | set_fs(KERNEL_DS); | |
700 | err = sys_clock_gettime(which_clock, | |
701 | (struct timespec __user *) &ts); | |
702 | set_fs(oldfs); | |
703 | if (!err && compat_put_timespec(&ts, tp)) | |
704 | return -EFAULT; | |
705 | return err; | |
706 | } | |
707 | ||
708 | COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock, | |
709 | struct compat_timex __user *, utp) | |
710 | { | |
711 | struct timex txc; | |
712 | mm_segment_t oldfs; | |
713 | int err, ret; | |
714 | ||
715 | err = compat_get_timex(&txc, utp); | |
716 | if (err) | |
717 | return err; | |
718 | ||
719 | oldfs = get_fs(); | |
720 | set_fs(KERNEL_DS); | |
721 | ret = sys_clock_adjtime(which_clock, (struct timex __user *) &txc); | |
722 | set_fs(oldfs); | |
723 | ||
724 | err = compat_put_timex(utp, &txc); | |
725 | if (err) | |
726 | return err; | |
727 | ||
728 | return ret; | |
729 | } | |
730 | ||
731 | COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock, | |
732 | struct compat_timespec __user *, tp) | |
733 | { | |
734 | long err; | |
735 | mm_segment_t oldfs; | |
736 | struct timespec ts; | |
737 | ||
738 | oldfs = get_fs(); | |
739 | set_fs(KERNEL_DS); | |
740 | err = sys_clock_getres(which_clock, | |
741 | (struct timespec __user *) &ts); | |
742 | set_fs(oldfs); | |
743 | if (!err && tp && compat_put_timespec(&ts, tp)) | |
744 | return -EFAULT; | |
745 | return err; | |
746 | } | |
747 | ||
748 | /* | |
749 | * We currently only need the following fields from the sigevent | |
750 | * structure: sigev_value, sigev_signo, sig_notify and (sometimes | |
751 | * sigev_notify_thread_id). The others are handled in user mode. | |
752 | * We also assume that copying sigev_value.sival_int is sufficient | |
753 | * to keep all the bits of sigev_value.sival_ptr intact. | |
754 | */ | |
755 | int get_compat_sigevent(struct sigevent *event, | |
756 | const struct compat_sigevent __user *u_event) | |
757 | { | |
758 | memset(event, 0, sizeof(*event)); | |
759 | return (!access_ok(VERIFY_READ, u_event, sizeof(*u_event)) || | |
760 | __get_user(event->sigev_value.sival_int, | |
761 | &u_event->sigev_value.sival_int) || | |
762 | __get_user(event->sigev_signo, &u_event->sigev_signo) || | |
763 | __get_user(event->sigev_notify, &u_event->sigev_notify) || | |
764 | __get_user(event->sigev_notify_thread_id, | |
765 | &u_event->sigev_notify_thread_id)) | |
766 | ? -EFAULT : 0; | |
767 | } | |
768 | ||
769 | long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask, | |
770 | unsigned long bitmap_size) | |
771 | { | |
772 | int i, j; | |
773 | unsigned long m; | |
774 | compat_ulong_t um; | |
775 | unsigned long nr_compat_longs; | |
776 | ||
777 | /* align bitmap up to nearest compat_long_t boundary */ | |
778 | bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG); | |
779 | ||
780 | if (!access_ok(VERIFY_READ, umask, bitmap_size / 8)) | |
781 | return -EFAULT; | |
782 | ||
783 | nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size); | |
784 | ||
785 | for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) { | |
786 | m = 0; | |
787 | ||
788 | for (j = 0; j < sizeof(m)/sizeof(um); j++) { | |
789 | /* | |
790 | * We dont want to read past the end of the userspace | |
791 | * bitmap. We must however ensure the end of the | |
792 | * kernel bitmap is zeroed. | |
793 | */ | |
794 | if (nr_compat_longs) { | |
795 | nr_compat_longs--; | |
796 | if (__get_user(um, umask)) | |
797 | return -EFAULT; | |
798 | } else { | |
799 | um = 0; | |
800 | } | |
801 | ||
802 | umask++; | |
803 | m |= (long)um << (j * BITS_PER_COMPAT_LONG); | |
804 | } | |
805 | *mask++ = m; | |
806 | } | |
807 | ||
808 | return 0; | |
809 | } | |
810 | ||
811 | long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask, | |
812 | unsigned long bitmap_size) | |
813 | { | |
814 | int i, j; | |
815 | unsigned long m; | |
816 | compat_ulong_t um; | |
817 | unsigned long nr_compat_longs; | |
818 | ||
819 | /* align bitmap up to nearest compat_long_t boundary */ | |
820 | bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG); | |
821 | ||
822 | if (!access_ok(VERIFY_WRITE, umask, bitmap_size / 8)) | |
823 | return -EFAULT; | |
824 | ||
825 | nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size); | |
826 | ||
827 | for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) { | |
828 | m = *mask++; | |
829 | ||
830 | for (j = 0; j < sizeof(m)/sizeof(um); j++) { | |
831 | um = m; | |
832 | ||
833 | /* | |
834 | * We dont want to write past the end of the userspace | |
835 | * bitmap. | |
836 | */ | |
837 | if (nr_compat_longs) { | |
838 | nr_compat_longs--; | |
839 | if (__put_user(um, umask)) | |
840 | return -EFAULT; | |
841 | } | |
842 | ||
843 | umask++; | |
844 | m >>= 4*sizeof(um); | |
845 | m >>= 4*sizeof(um); | |
846 | } | |
847 | } | |
848 | ||
849 | return 0; | |
850 | } | |
851 | ||
852 | void | |
853 | sigset_from_compat(sigset_t *set, const compat_sigset_t *compat) | |
854 | { | |
855 | switch (_NSIG_WORDS) { | |
856 | case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32 ); | |
857 | case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32 ); | |
858 | case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32 ); | |
859 | case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32 ); | |
860 | } | |
861 | } | |
862 | EXPORT_SYMBOL_GPL(sigset_from_compat); | |
863 | ||
864 | void | |
865 | sigset_to_compat(compat_sigset_t *compat, const sigset_t *set) | |
866 | { | |
867 | switch (_NSIG_WORDS) { | |
868 | case 4: compat->sig[7] = (set->sig[3] >> 32); compat->sig[6] = set->sig[3]; | |
869 | case 3: compat->sig[5] = (set->sig[2] >> 32); compat->sig[4] = set->sig[2]; | |
870 | case 2: compat->sig[3] = (set->sig[1] >> 32); compat->sig[2] = set->sig[1]; | |
871 | case 1: compat->sig[1] = (set->sig[0] >> 32); compat->sig[0] = set->sig[0]; | |
872 | } | |
873 | } | |
874 | ||
875 | COMPAT_SYSCALL_DEFINE4(rt_sigtimedwait, compat_sigset_t __user *, uthese, | |
876 | struct compat_siginfo __user *, uinfo, | |
877 | struct compat_timespec __user *, uts, compat_size_t, sigsetsize) | |
878 | { | |
879 | compat_sigset_t s32; | |
880 | sigset_t s; | |
881 | struct timespec t; | |
882 | siginfo_t info; | |
883 | long ret; | |
884 | ||
885 | if (sigsetsize != sizeof(sigset_t)) | |
886 | return -EINVAL; | |
887 | ||
888 | if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t))) | |
889 | return -EFAULT; | |
890 | sigset_from_compat(&s, &s32); | |
891 | ||
892 | if (uts) { | |
893 | if (compat_get_timespec(&t, uts)) | |
894 | return -EFAULT; | |
895 | } | |
896 | ||
897 | ret = do_sigtimedwait(&s, &info, uts ? &t : NULL); | |
898 | ||
899 | if (ret > 0 && uinfo) { | |
900 | if (copy_siginfo_to_user32(uinfo, &info)) | |
901 | ret = -EFAULT; | |
902 | } | |
903 | ||
904 | return ret; | |
905 | } | |
906 | ||
907 | #ifdef __ARCH_WANT_COMPAT_SYS_TIME | |
908 | ||
909 | /* compat_time_t is a 32 bit "long" and needs to get converted. */ | |
910 | ||
911 | COMPAT_SYSCALL_DEFINE1(time, compat_time_t __user *, tloc) | |
912 | { | |
913 | compat_time_t i; | |
914 | struct timeval tv; | |
915 | ||
916 | do_gettimeofday(&tv); | |
917 | i = tv.tv_sec; | |
918 | ||
919 | if (tloc) { | |
920 | if (put_user(i,tloc)) | |
921 | return -EFAULT; | |
922 | } | |
923 | force_successful_syscall_return(); | |
924 | return i; | |
925 | } | |
926 | ||
927 | COMPAT_SYSCALL_DEFINE1(stime, compat_time_t __user *, tptr) | |
928 | { | |
929 | struct timespec tv; | |
930 | int err; | |
931 | ||
932 | if (get_user(tv.tv_sec, tptr)) | |
933 | return -EFAULT; | |
934 | ||
935 | tv.tv_nsec = 0; | |
936 | ||
937 | err = security_settime(&tv, NULL); | |
938 | if (err) | |
939 | return err; | |
940 | ||
941 | do_settimeofday(&tv); | |
942 | return 0; | |
943 | } | |
944 | ||
945 | #endif /* __ARCH_WANT_COMPAT_SYS_TIME */ | |
946 | ||
947 | COMPAT_SYSCALL_DEFINE1(adjtimex, struct compat_timex __user *, utp) | |
948 | { | |
949 | struct timex txc; | |
950 | int err, ret; | |
951 | ||
952 | err = compat_get_timex(&txc, utp); | |
953 | if (err) | |
954 | return err; | |
955 | ||
956 | ret = do_adjtimex(&txc); | |
957 | ||
958 | err = compat_put_timex(utp, &txc); | |
959 | if (err) | |
960 | return err; | |
961 | ||
962 | return ret; | |
963 | } | |
964 | ||
965 | #ifdef CONFIG_NUMA | |
966 | COMPAT_SYSCALL_DEFINE6(move_pages, pid_t, pid, compat_ulong_t, nr_pages, | |
967 | compat_uptr_t __user *, pages32, | |
968 | const int __user *, nodes, | |
969 | int __user *, status, | |
970 | int, flags) | |
971 | { | |
972 | const void __user * __user *pages; | |
973 | int i; | |
974 | ||
975 | pages = compat_alloc_user_space(nr_pages * sizeof(void *)); | |
976 | for (i = 0; i < nr_pages; i++) { | |
977 | compat_uptr_t p; | |
978 | ||
979 | if (get_user(p, pages32 + i) || | |
980 | put_user(compat_ptr(p), pages + i)) | |
981 | return -EFAULT; | |
982 | } | |
983 | return sys_move_pages(pid, nr_pages, pages, nodes, status, flags); | |
984 | } | |
985 | ||
986 | COMPAT_SYSCALL_DEFINE4(migrate_pages, compat_pid_t, pid, | |
987 | compat_ulong_t, maxnode, | |
988 | const compat_ulong_t __user *, old_nodes, | |
989 | const compat_ulong_t __user *, new_nodes) | |
990 | { | |
991 | unsigned long __user *old = NULL; | |
992 | unsigned long __user *new = NULL; | |
993 | nodemask_t tmp_mask; | |
994 | unsigned long nr_bits; | |
995 | unsigned long size; | |
996 | ||
997 | nr_bits = min_t(unsigned long, maxnode - 1, MAX_NUMNODES); | |
998 | size = ALIGN(nr_bits, BITS_PER_LONG) / 8; | |
999 | if (old_nodes) { | |
1000 | if (compat_get_bitmap(nodes_addr(tmp_mask), old_nodes, nr_bits)) | |
1001 | return -EFAULT; | |
1002 | old = compat_alloc_user_space(new_nodes ? size * 2 : size); | |
1003 | if (new_nodes) | |
1004 | new = old + size / sizeof(unsigned long); | |
1005 | if (copy_to_user(old, nodes_addr(tmp_mask), size)) | |
1006 | return -EFAULT; | |
1007 | } | |
1008 | if (new_nodes) { | |
1009 | if (compat_get_bitmap(nodes_addr(tmp_mask), new_nodes, nr_bits)) | |
1010 | return -EFAULT; | |
1011 | if (new == NULL) | |
1012 | new = compat_alloc_user_space(size); | |
1013 | if (copy_to_user(new, nodes_addr(tmp_mask), size)) | |
1014 | return -EFAULT; | |
1015 | } | |
1016 | return sys_migrate_pages(pid, nr_bits + 1, old, new); | |
1017 | } | |
1018 | #endif | |
1019 | ||
1020 | COMPAT_SYSCALL_DEFINE2(sched_rr_get_interval, | |
1021 | compat_pid_t, pid, | |
1022 | struct compat_timespec __user *, interval) | |
1023 | { | |
1024 | struct timespec t; | |
1025 | int ret; | |
1026 | mm_segment_t old_fs = get_fs(); | |
1027 | ||
1028 | set_fs(KERNEL_DS); | |
1029 | ret = sys_sched_rr_get_interval(pid, (struct timespec __user *)&t); | |
1030 | set_fs(old_fs); | |
1031 | if (compat_put_timespec(&t, interval)) | |
1032 | return -EFAULT; | |
1033 | return ret; | |
1034 | } | |
1035 | ||
1036 | /* | |
1037 | * Allocate user-space memory for the duration of a single system call, | |
1038 | * in order to marshall parameters inside a compat thunk. | |
1039 | */ | |
1040 | void __user *compat_alloc_user_space(unsigned long len) | |
1041 | { | |
1042 | void __user *ptr; | |
1043 | ||
1044 | /* If len would occupy more than half of the entire compat space... */ | |
1045 | if (unlikely(len > (((compat_uptr_t)~0) >> 1))) | |
1046 | return NULL; | |
1047 | ||
1048 | ptr = arch_compat_alloc_user_space(len); | |
1049 | ||
1050 | if (unlikely(!access_ok(VERIFY_WRITE, ptr, len))) | |
1051 | return NULL; | |
1052 | ||
1053 | return ptr; | |
1054 | } | |
1055 | EXPORT_SYMBOL_GPL(compat_alloc_user_space); |