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