xref: /openbmc/linux/kernel/compat.c (revision d003d772)
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/export.h>
24 #include <linux/migrate.h>
25 #include <linux/posix-timers.h>
26 #include <linux/times.h>
27 #include <linux/ptrace.h>
28 #include <linux/gfp.h>
29 
30 #include <linux/uaccess.h>
31 
32 static int __compat_get_timeval(struct timeval *tv, const struct old_timeval32 __user *ctv)
33 {
34 	return (!access_ok(ctv, sizeof(*ctv)) ||
35 			__get_user(tv->tv_sec, &ctv->tv_sec) ||
36 			__get_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0;
37 }
38 
39 static int __compat_put_timeval(const struct timeval *tv, struct old_timeval32 __user *ctv)
40 {
41 	return (!access_ok(ctv, sizeof(*ctv)) ||
42 			__put_user(tv->tv_sec, &ctv->tv_sec) ||
43 			__put_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0;
44 }
45 
46 static int __compat_get_timespec(struct timespec *ts, const struct old_timespec32 __user *cts)
47 {
48 	return (!access_ok(cts, sizeof(*cts)) ||
49 			__get_user(ts->tv_sec, &cts->tv_sec) ||
50 			__get_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
51 }
52 
53 static int __compat_put_timespec(const struct timespec *ts, struct old_timespec32 __user *cts)
54 {
55 	return (!access_ok(cts, sizeof(*cts)) ||
56 			__put_user(ts->tv_sec, &cts->tv_sec) ||
57 			__put_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
58 }
59 
60 int compat_get_timeval(struct timeval *tv, const void __user *utv)
61 {
62 	if (COMPAT_USE_64BIT_TIME)
63 		return copy_from_user(tv, utv, sizeof(*tv)) ? -EFAULT : 0;
64 	else
65 		return __compat_get_timeval(tv, utv);
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)
72 		return copy_to_user(utv, tv, sizeof(*tv)) ? -EFAULT : 0;
73 	else
74 		return __compat_put_timeval(tv, utv);
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)
81 		return copy_from_user(ts, uts, sizeof(*ts)) ? -EFAULT : 0;
82 	else
83 		return __compat_get_timespec(ts, uts);
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)
90 		return copy_to_user(uts, ts, sizeof(*ts)) ? -EFAULT : 0;
91 	else
92 		return __compat_put_timespec(ts, uts);
93 }
94 EXPORT_SYMBOL_GPL(compat_put_timespec);
95 
96 int get_compat_itimerval(struct itimerval *o, const struct compat_itimerval __user *i)
97 {
98 	struct compat_itimerval v32;
99 
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;
107 }
108 
109 int put_compat_itimerval(struct compat_itimerval __user *o, const struct itimerval *i)
110 {
111 	struct compat_itimerval v32;
112 
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;
118 }
119 
120 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
121 
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)
127 {
128 	memcpy(blocked->sig, &set, sizeof(set));
129 }
130 
131 COMPAT_SYSCALL_DEFINE3(sigprocmask, int, how,
132 		       compat_old_sigset_t __user *, nset,
133 		       compat_old_sigset_t __user *, oset)
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;
170 }
171 
172 #endif
173 
174 int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru)
175 {
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)))
197 		return -EFAULT;
198 	return 0;
199 }
200 
201 static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr,
202 				    unsigned len, struct cpumask *new_mask)
203 {
204 	unsigned long *k;
205 
206 	if (len < cpumask_size())
207 		memset(new_mask, 0, cpumask_size());
208 	else if (len > cpumask_size())
209 		len = cpumask_size();
210 
211 	k = cpumask_bits(new_mask);
212 	return compat_get_bitmap(k, user_mask_ptr, len * 8);
213 }
214 
215 COMPAT_SYSCALL_DEFINE3(sched_setaffinity, compat_pid_t, pid,
216 		       unsigned int, len,
217 		       compat_ulong_t __user *, user_mask_ptr)
218 {
219 	cpumask_var_t new_mask;
220 	int retval;
221 
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);
226 	if (retval)
227 		goto out;
228 
229 	retval = sched_setaffinity(pid, new_mask);
230 out:
231 	free_cpumask_var(new_mask);
232 	return retval;
233 }
234 
235 COMPAT_SYSCALL_DEFINE3(sched_getaffinity, compat_pid_t,  pid, unsigned int, len,
236 		       compat_ulong_t __user *, user_mask_ptr)
237 {
238 	int ret;
239 	cpumask_var_t mask;
240 
241 	if ((len * BITS_PER_BYTE) < nr_cpu_ids)
242 		return -EINVAL;
243 	if (len & (sizeof(compat_ulong_t)-1))
244 		return -EINVAL;
245 
246 	if (!alloc_cpumask_var(&mask, GFP_KERNEL))
247 		return -ENOMEM;
248 
249 	ret = sched_getaffinity(pid, mask);
250 	if (ret == 0) {
251 		unsigned int retlen = min(len, cpumask_size());
252 
253 		if (compat_put_bitmap(user_mask_ptr, cpumask_bits(mask), retlen * 8))
254 			ret = -EFAULT;
255 		else
256 			ret = retlen;
257 	}
258 	free_cpumask_var(mask);
259 
260 	return ret;
261 }
262 
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 {
273 	memset(event, 0, sizeof(*event));
274 	return (!access_ok(u_event, sizeof(*u_event)) ||
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 
284 long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask,
285 		       unsigned long bitmap_size)
286 {
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);
291 	nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
292 
293 	if (!user_access_begin(umask, bitmap_size / 8))
294 		return -EFAULT;
295 
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;
302 	}
303 	if (nr_compat_longs)
304 		unsafe_get_user(*mask, umask++, Efault);
305 	user_access_end();
306 	return 0;
307 
308 Efault:
309 	user_access_end();
310 	return -EFAULT;
311 }
312 
313 long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
314 		       unsigned long bitmap_size)
315 {
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);
320 	nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
321 
322 	if (!user_access_begin(umask, bitmap_size / 8))
323 		return -EFAULT;
324 
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;
330 	}
331 	if (nr_compat_longs)
332 		unsafe_put_user((compat_ulong_t)*mask, umask++, Efault);
333 	user_access_end();
334 	return 0;
335 Efault:
336 	user_access_end();
337 	return -EFAULT;
338 }
339 
340 int
341 get_compat_sigset(sigset_t *set, const compat_sigset_t __user *compat)
342 {
343 #ifdef __BIG_ENDIAN
344 	compat_sigset_t v;
345 	if (copy_from_user(&v, compat, sizeof(compat_sigset_t)))
346 		return -EFAULT;
347 	switch (_NSIG_WORDS) {
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 );
352 	}
353 #else
354 	if (copy_from_user(set, compat, sizeof(compat_sigset_t)))
355 		return -EFAULT;
356 #endif
357 	return 0;
358 }
359 EXPORT_SYMBOL_GPL(get_compat_sigset);
360 
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 
375 	if (unlikely(!access_ok(ptr, len)))
376 		return NULL;
377 
378 	return ptr;
379 }
380 EXPORT_SYMBOL_GPL(compat_alloc_user_space);
381