xref: /linux/kernel/compat.c (revision f79e4d5f92a129a1159c973735007d4ddc8541f3)
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 int compat_get_timex(struct timex *txc, const struct compat_timex __user *utp)
34 {
35 	struct compat_timex tx32;
36 
37 	memset(txc, 0, sizeof(struct timex));
38 	if (copy_from_user(&tx32, utp, sizeof(struct compat_timex)))
39 		return -EFAULT;
40 
41 	txc->modes = tx32.modes;
42 	txc->offset = tx32.offset;
43 	txc->freq = tx32.freq;
44 	txc->maxerror = tx32.maxerror;
45 	txc->esterror = tx32.esterror;
46 	txc->status = tx32.status;
47 	txc->constant = tx32.constant;
48 	txc->precision = tx32.precision;
49 	txc->tolerance = tx32.tolerance;
50 	txc->time.tv_sec = tx32.time.tv_sec;
51 	txc->time.tv_usec = tx32.time.tv_usec;
52 	txc->tick = tx32.tick;
53 	txc->ppsfreq = tx32.ppsfreq;
54 	txc->jitter = tx32.jitter;
55 	txc->shift = tx32.shift;
56 	txc->stabil = tx32.stabil;
57 	txc->jitcnt = tx32.jitcnt;
58 	txc->calcnt = tx32.calcnt;
59 	txc->errcnt = tx32.errcnt;
60 	txc->stbcnt = tx32.stbcnt;
61 
62 	return 0;
63 }
64 
65 int compat_put_timex(struct compat_timex __user *utp, const struct timex *txc)
66 {
67 	struct compat_timex tx32;
68 
69 	memset(&tx32, 0, sizeof(struct compat_timex));
70 	tx32.modes = txc->modes;
71 	tx32.offset = txc->offset;
72 	tx32.freq = txc->freq;
73 	tx32.maxerror = txc->maxerror;
74 	tx32.esterror = txc->esterror;
75 	tx32.status = txc->status;
76 	tx32.constant = txc->constant;
77 	tx32.precision = txc->precision;
78 	tx32.tolerance = txc->tolerance;
79 	tx32.time.tv_sec = txc->time.tv_sec;
80 	tx32.time.tv_usec = txc->time.tv_usec;
81 	tx32.tick = txc->tick;
82 	tx32.ppsfreq = txc->ppsfreq;
83 	tx32.jitter = txc->jitter;
84 	tx32.shift = txc->shift;
85 	tx32.stabil = txc->stabil;
86 	tx32.jitcnt = txc->jitcnt;
87 	tx32.calcnt = txc->calcnt;
88 	tx32.errcnt = txc->errcnt;
89 	tx32.stbcnt = txc->stbcnt;
90 	tx32.tai = txc->tai;
91 	if (copy_to_user(utp, &tx32, sizeof(struct compat_timex)))
92 		return -EFAULT;
93 	return 0;
94 }
95 
96 static int __compat_get_timeval(struct timeval *tv, const struct compat_timeval __user *ctv)
97 {
98 	return (!access_ok(VERIFY_READ, ctv, sizeof(*ctv)) ||
99 			__get_user(tv->tv_sec, &ctv->tv_sec) ||
100 			__get_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0;
101 }
102 
103 static int __compat_put_timeval(const struct timeval *tv, struct compat_timeval __user *ctv)
104 {
105 	return (!access_ok(VERIFY_WRITE, ctv, sizeof(*ctv)) ||
106 			__put_user(tv->tv_sec, &ctv->tv_sec) ||
107 			__put_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0;
108 }
109 
110 static int __compat_get_timespec(struct timespec *ts, const struct compat_timespec __user *cts)
111 {
112 	return (!access_ok(VERIFY_READ, cts, sizeof(*cts)) ||
113 			__get_user(ts->tv_sec, &cts->tv_sec) ||
114 			__get_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
115 }
116 
117 static int __compat_put_timespec(const struct timespec *ts, struct compat_timespec __user *cts)
118 {
119 	return (!access_ok(VERIFY_WRITE, cts, sizeof(*cts)) ||
120 			__put_user(ts->tv_sec, &cts->tv_sec) ||
121 			__put_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
122 }
123 
124 int compat_get_timeval(struct timeval *tv, const void __user *utv)
125 {
126 	if (COMPAT_USE_64BIT_TIME)
127 		return copy_from_user(tv, utv, sizeof(*tv)) ? -EFAULT : 0;
128 	else
129 		return __compat_get_timeval(tv, utv);
130 }
131 EXPORT_SYMBOL_GPL(compat_get_timeval);
132 
133 int compat_put_timeval(const struct timeval *tv, void __user *utv)
134 {
135 	if (COMPAT_USE_64BIT_TIME)
136 		return copy_to_user(utv, tv, sizeof(*tv)) ? -EFAULT : 0;
137 	else
138 		return __compat_put_timeval(tv, utv);
139 }
140 EXPORT_SYMBOL_GPL(compat_put_timeval);
141 
142 int compat_get_timespec(struct timespec *ts, const void __user *uts)
143 {
144 	if (COMPAT_USE_64BIT_TIME)
145 		return copy_from_user(ts, uts, sizeof(*ts)) ? -EFAULT : 0;
146 	else
147 		return __compat_get_timespec(ts, uts);
148 }
149 EXPORT_SYMBOL_GPL(compat_get_timespec);
150 
151 int compat_put_timespec(const struct timespec *ts, void __user *uts)
152 {
153 	if (COMPAT_USE_64BIT_TIME)
154 		return copy_to_user(uts, ts, sizeof(*ts)) ? -EFAULT : 0;
155 	else
156 		return __compat_put_timespec(ts, uts);
157 }
158 EXPORT_SYMBOL_GPL(compat_put_timespec);
159 
160 int get_compat_itimerval(struct itimerval *o, const struct compat_itimerval __user *i)
161 {
162 	struct compat_itimerval v32;
163 
164 	if (copy_from_user(&v32, i, sizeof(struct compat_itimerval)))
165 		return -EFAULT;
166 	o->it_interval.tv_sec = v32.it_interval.tv_sec;
167 	o->it_interval.tv_usec = v32.it_interval.tv_usec;
168 	o->it_value.tv_sec = v32.it_value.tv_sec;
169 	o->it_value.tv_usec = v32.it_value.tv_usec;
170 	return 0;
171 }
172 
173 int put_compat_itimerval(struct compat_itimerval __user *o, const struct itimerval *i)
174 {
175 	struct compat_itimerval v32;
176 
177 	v32.it_interval.tv_sec = i->it_interval.tv_sec;
178 	v32.it_interval.tv_usec = i->it_interval.tv_usec;
179 	v32.it_value.tv_sec = i->it_value.tv_sec;
180 	v32.it_value.tv_usec = i->it_value.tv_usec;
181 	return copy_to_user(o, &v32, sizeof(struct compat_itimerval)) ? -EFAULT : 0;
182 }
183 
184 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
185 
186 /*
187  * sys_sigprocmask SIG_SETMASK sets the first (compat) word of the
188  * blocked set of signals to the supplied signal set
189  */
190 static inline void compat_sig_setmask(sigset_t *blocked, compat_sigset_word set)
191 {
192 	memcpy(blocked->sig, &set, sizeof(set));
193 }
194 
195 COMPAT_SYSCALL_DEFINE3(sigprocmask, int, how,
196 		       compat_old_sigset_t __user *, nset,
197 		       compat_old_sigset_t __user *, oset)
198 {
199 	old_sigset_t old_set, new_set;
200 	sigset_t new_blocked;
201 
202 	old_set = current->blocked.sig[0];
203 
204 	if (nset) {
205 		if (get_user(new_set, nset))
206 			return -EFAULT;
207 		new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
208 
209 		new_blocked = current->blocked;
210 
211 		switch (how) {
212 		case SIG_BLOCK:
213 			sigaddsetmask(&new_blocked, new_set);
214 			break;
215 		case SIG_UNBLOCK:
216 			sigdelsetmask(&new_blocked, new_set);
217 			break;
218 		case SIG_SETMASK:
219 			compat_sig_setmask(&new_blocked, new_set);
220 			break;
221 		default:
222 			return -EINVAL;
223 		}
224 
225 		set_current_blocked(&new_blocked);
226 	}
227 
228 	if (oset) {
229 		if (put_user(old_set, oset))
230 			return -EFAULT;
231 	}
232 
233 	return 0;
234 }
235 
236 #endif
237 
238 int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru)
239 {
240 	struct compat_rusage r32;
241 	memset(&r32, 0, sizeof(r32));
242 	r32.ru_utime.tv_sec = r->ru_utime.tv_sec;
243 	r32.ru_utime.tv_usec = r->ru_utime.tv_usec;
244 	r32.ru_stime.tv_sec = r->ru_stime.tv_sec;
245 	r32.ru_stime.tv_usec = r->ru_stime.tv_usec;
246 	r32.ru_maxrss = r->ru_maxrss;
247 	r32.ru_ixrss = r->ru_ixrss;
248 	r32.ru_idrss = r->ru_idrss;
249 	r32.ru_isrss = r->ru_isrss;
250 	r32.ru_minflt = r->ru_minflt;
251 	r32.ru_majflt = r->ru_majflt;
252 	r32.ru_nswap = r->ru_nswap;
253 	r32.ru_inblock = r->ru_inblock;
254 	r32.ru_oublock = r->ru_oublock;
255 	r32.ru_msgsnd = r->ru_msgsnd;
256 	r32.ru_msgrcv = r->ru_msgrcv;
257 	r32.ru_nsignals = r->ru_nsignals;
258 	r32.ru_nvcsw = r->ru_nvcsw;
259 	r32.ru_nivcsw = r->ru_nivcsw;
260 	if (copy_to_user(ru, &r32, sizeof(r32)))
261 		return -EFAULT;
262 	return 0;
263 }
264 
265 static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr,
266 				    unsigned len, struct cpumask *new_mask)
267 {
268 	unsigned long *k;
269 
270 	if (len < cpumask_size())
271 		memset(new_mask, 0, cpumask_size());
272 	else if (len > cpumask_size())
273 		len = cpumask_size();
274 
275 	k = cpumask_bits(new_mask);
276 	return compat_get_bitmap(k, user_mask_ptr, len * 8);
277 }
278 
279 COMPAT_SYSCALL_DEFINE3(sched_setaffinity, compat_pid_t, pid,
280 		       unsigned int, len,
281 		       compat_ulong_t __user *, user_mask_ptr)
282 {
283 	cpumask_var_t new_mask;
284 	int retval;
285 
286 	if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
287 		return -ENOMEM;
288 
289 	retval = compat_get_user_cpu_mask(user_mask_ptr, len, new_mask);
290 	if (retval)
291 		goto out;
292 
293 	retval = sched_setaffinity(pid, new_mask);
294 out:
295 	free_cpumask_var(new_mask);
296 	return retval;
297 }
298 
299 COMPAT_SYSCALL_DEFINE3(sched_getaffinity, compat_pid_t,  pid, unsigned int, len,
300 		       compat_ulong_t __user *, user_mask_ptr)
301 {
302 	int ret;
303 	cpumask_var_t mask;
304 
305 	if ((len * BITS_PER_BYTE) < nr_cpu_ids)
306 		return -EINVAL;
307 	if (len & (sizeof(compat_ulong_t)-1))
308 		return -EINVAL;
309 
310 	if (!alloc_cpumask_var(&mask, GFP_KERNEL))
311 		return -ENOMEM;
312 
313 	ret = sched_getaffinity(pid, mask);
314 	if (ret == 0) {
315 		unsigned int retlen = min(len, cpumask_size());
316 
317 		if (compat_put_bitmap(user_mask_ptr, cpumask_bits(mask), retlen * 8))
318 			ret = -EFAULT;
319 		else
320 			ret = retlen;
321 	}
322 	free_cpumask_var(mask);
323 
324 	return ret;
325 }
326 
327 /* Todo: Delete these extern declarations when get/put_compat_itimerspec64()
328  * are moved to kernel/time/time.c .
329  */
330 extern int __compat_get_timespec64(struct timespec64 *ts64,
331 				   const struct compat_timespec __user *cts);
332 extern int __compat_put_timespec64(const struct timespec64 *ts64,
333 				   struct compat_timespec __user *cts);
334 
335 int get_compat_itimerspec64(struct itimerspec64 *its,
336 			const struct compat_itimerspec __user *uits)
337 {
338 
339 	if (__compat_get_timespec64(&its->it_interval, &uits->it_interval) ||
340 	    __compat_get_timespec64(&its->it_value, &uits->it_value))
341 		return -EFAULT;
342 	return 0;
343 }
344 EXPORT_SYMBOL_GPL(get_compat_itimerspec64);
345 
346 int put_compat_itimerspec64(const struct itimerspec64 *its,
347 			struct compat_itimerspec __user *uits)
348 {
349 	if (__compat_put_timespec64(&its->it_interval, &uits->it_interval) ||
350 	    __compat_put_timespec64(&its->it_value, &uits->it_value))
351 		return -EFAULT;
352 	return 0;
353 }
354 EXPORT_SYMBOL_GPL(put_compat_itimerspec64);
355 
356 /*
357  * We currently only need the following fields from the sigevent
358  * structure: sigev_value, sigev_signo, sig_notify and (sometimes
359  * sigev_notify_thread_id).  The others are handled in user mode.
360  * We also assume that copying sigev_value.sival_int is sufficient
361  * to keep all the bits of sigev_value.sival_ptr intact.
362  */
363 int get_compat_sigevent(struct sigevent *event,
364 		const struct compat_sigevent __user *u_event)
365 {
366 	memset(event, 0, sizeof(*event));
367 	return (!access_ok(VERIFY_READ, u_event, sizeof(*u_event)) ||
368 		__get_user(event->sigev_value.sival_int,
369 			&u_event->sigev_value.sival_int) ||
370 		__get_user(event->sigev_signo, &u_event->sigev_signo) ||
371 		__get_user(event->sigev_notify, &u_event->sigev_notify) ||
372 		__get_user(event->sigev_notify_thread_id,
373 			&u_event->sigev_notify_thread_id))
374 		? -EFAULT : 0;
375 }
376 
377 long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask,
378 		       unsigned long bitmap_size)
379 {
380 	unsigned long nr_compat_longs;
381 
382 	/* align bitmap up to nearest compat_long_t boundary */
383 	bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
384 	nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
385 
386 	if (!access_ok(VERIFY_READ, umask, bitmap_size / 8))
387 		return -EFAULT;
388 
389 	user_access_begin();
390 	while (nr_compat_longs > 1) {
391 		compat_ulong_t l1, l2;
392 		unsafe_get_user(l1, umask++, Efault);
393 		unsafe_get_user(l2, umask++, Efault);
394 		*mask++ = ((unsigned long)l2 << BITS_PER_COMPAT_LONG) | l1;
395 		nr_compat_longs -= 2;
396 	}
397 	if (nr_compat_longs)
398 		unsafe_get_user(*mask, umask++, Efault);
399 	user_access_end();
400 	return 0;
401 
402 Efault:
403 	user_access_end();
404 	return -EFAULT;
405 }
406 
407 long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
408 		       unsigned long bitmap_size)
409 {
410 	unsigned long nr_compat_longs;
411 
412 	/* align bitmap up to nearest compat_long_t boundary */
413 	bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
414 	nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
415 
416 	if (!access_ok(VERIFY_WRITE, umask, bitmap_size / 8))
417 		return -EFAULT;
418 
419 	user_access_begin();
420 	while (nr_compat_longs > 1) {
421 		unsigned long m = *mask++;
422 		unsafe_put_user((compat_ulong_t)m, umask++, Efault);
423 		unsafe_put_user(m >> BITS_PER_COMPAT_LONG, umask++, Efault);
424 		nr_compat_longs -= 2;
425 	}
426 	if (nr_compat_longs)
427 		unsafe_put_user((compat_ulong_t)*mask, umask++, Efault);
428 	user_access_end();
429 	return 0;
430 Efault:
431 	user_access_end();
432 	return -EFAULT;
433 }
434 
435 int
436 get_compat_sigset(sigset_t *set, const compat_sigset_t __user *compat)
437 {
438 #ifdef __BIG_ENDIAN
439 	compat_sigset_t v;
440 	if (copy_from_user(&v, compat, sizeof(compat_sigset_t)))
441 		return -EFAULT;
442 	switch (_NSIG_WORDS) {
443 	case 4: set->sig[3] = v.sig[6] | (((long)v.sig[7]) << 32 );
444 	case 3: set->sig[2] = v.sig[4] | (((long)v.sig[5]) << 32 );
445 	case 2: set->sig[1] = v.sig[2] | (((long)v.sig[3]) << 32 );
446 	case 1: set->sig[0] = v.sig[0] | (((long)v.sig[1]) << 32 );
447 	}
448 #else
449 	if (copy_from_user(set, compat, sizeof(compat_sigset_t)))
450 		return -EFAULT;
451 #endif
452 	return 0;
453 }
454 EXPORT_SYMBOL_GPL(get_compat_sigset);
455 
456 /*
457  * Allocate user-space memory for the duration of a single system call,
458  * in order to marshall parameters inside a compat thunk.
459  */
460 void __user *compat_alloc_user_space(unsigned long len)
461 {
462 	void __user *ptr;
463 
464 	/* If len would occupy more than half of the entire compat space... */
465 	if (unlikely(len > (((compat_uptr_t)~0) >> 1)))
466 		return NULL;
467 
468 	ptr = arch_compat_alloc_user_space(len);
469 
470 	if (unlikely(!access_ok(VERIFY_WRITE, ptr, len)))
471 		return NULL;
472 
473 	return ptr;
474 }
475 EXPORT_SYMBOL_GPL(compat_alloc_user_space);
476