xref: /linux-6.15/kernel/compat.c (revision f7275650)
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/migrate.h>
25 #include <linux/posix-timers.h>
26 
27 #include <asm/uaccess.h>
28 
29 /*
30  * Note that the native side is already converted to a timespec, because
31  * that's what we want anyway.
32  */
33 static int compat_get_timeval(struct timespec *o,
34 		struct compat_timeval __user *i)
35 {
36 	long usec;
37 
38 	if (get_user(o->tv_sec, &i->tv_sec) ||
39 	    get_user(usec, &i->tv_usec))
40 		return -EFAULT;
41 	o->tv_nsec = usec * 1000;
42 	return 0;
43 }
44 
45 static int compat_put_timeval(struct compat_timeval __user *o,
46 		struct timeval *i)
47 {
48 	return (put_user(i->tv_sec, &o->tv_sec) ||
49 		put_user(i->tv_usec, &o->tv_usec)) ? -EFAULT : 0;
50 }
51 
52 asmlinkage long compat_sys_gettimeofday(struct compat_timeval __user *tv,
53 		struct timezone __user *tz)
54 {
55 	if (tv) {
56 		struct timeval ktv;
57 		do_gettimeofday(&ktv);
58 		if (compat_put_timeval(tv, &ktv))
59 			return -EFAULT;
60 	}
61 	if (tz) {
62 		if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
63 			return -EFAULT;
64 	}
65 
66 	return 0;
67 }
68 
69 asmlinkage long compat_sys_settimeofday(struct compat_timeval __user *tv,
70 		struct timezone __user *tz)
71 {
72 	struct timespec kts;
73 	struct timezone ktz;
74 
75 	if (tv) {
76 		if (compat_get_timeval(&kts, tv))
77 			return -EFAULT;
78 	}
79 	if (tz) {
80 		if (copy_from_user(&ktz, tz, sizeof(ktz)))
81 			return -EFAULT;
82 	}
83 
84 	return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
85 }
86 
87 int get_compat_timespec(struct timespec *ts, const struct compat_timespec __user *cts)
88 {
89 	return (!access_ok(VERIFY_READ, cts, sizeof(*cts)) ||
90 			__get_user(ts->tv_sec, &cts->tv_sec) ||
91 			__get_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
92 }
93 
94 int put_compat_timespec(const struct timespec *ts, struct compat_timespec __user *cts)
95 {
96 	return (!access_ok(VERIFY_WRITE, cts, sizeof(*cts)) ||
97 			__put_user(ts->tv_sec, &cts->tv_sec) ||
98 			__put_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
99 }
100 
101 static long compat_nanosleep_restart(struct restart_block *restart)
102 {
103 	struct compat_timespec __user *rmtp;
104 	struct timespec rmt;
105 	mm_segment_t oldfs;
106 	long ret;
107 
108 	restart->nanosleep.rmtp = (struct timespec __user *) &rmt;
109 	oldfs = get_fs();
110 	set_fs(KERNEL_DS);
111 	ret = hrtimer_nanosleep_restart(restart);
112 	set_fs(oldfs);
113 
114 	if (ret) {
115 		rmtp = restart->nanosleep.compat_rmtp;
116 
117 		if (rmtp && put_compat_timespec(&rmt, rmtp))
118 			return -EFAULT;
119 	}
120 
121 	return ret;
122 }
123 
124 asmlinkage long compat_sys_nanosleep(struct compat_timespec __user *rqtp,
125 				     struct compat_timespec __user *rmtp)
126 {
127 	struct timespec tu, rmt;
128 	mm_segment_t oldfs;
129 	long ret;
130 
131 	if (get_compat_timespec(&tu, rqtp))
132 		return -EFAULT;
133 
134 	if (!timespec_valid(&tu))
135 		return -EINVAL;
136 
137 	oldfs = get_fs();
138 	set_fs(KERNEL_DS);
139 	ret = hrtimer_nanosleep(&tu,
140 				rmtp ? (struct timespec __user *)&rmt : NULL,
141 				HRTIMER_MODE_REL, CLOCK_MONOTONIC);
142 	set_fs(oldfs);
143 
144 	if (ret) {
145 		struct restart_block *restart
146 			= &current_thread_info()->restart_block;
147 
148 		restart->fn = compat_nanosleep_restart;
149 		restart->nanosleep.compat_rmtp = rmtp;
150 
151 		if (rmtp && put_compat_timespec(&rmt, rmtp))
152 			return -EFAULT;
153 	}
154 
155 	return ret;
156 }
157 
158 static inline long get_compat_itimerval(struct itimerval *o,
159 		struct compat_itimerval __user *i)
160 {
161 	return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
162 		(__get_user(o->it_interval.tv_sec, &i->it_interval.tv_sec) |
163 		 __get_user(o->it_interval.tv_usec, &i->it_interval.tv_usec) |
164 		 __get_user(o->it_value.tv_sec, &i->it_value.tv_sec) |
165 		 __get_user(o->it_value.tv_usec, &i->it_value.tv_usec)));
166 }
167 
168 static inline long put_compat_itimerval(struct compat_itimerval __user *o,
169 		struct itimerval *i)
170 {
171 	return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
172 		(__put_user(i->it_interval.tv_sec, &o->it_interval.tv_sec) |
173 		 __put_user(i->it_interval.tv_usec, &o->it_interval.tv_usec) |
174 		 __put_user(i->it_value.tv_sec, &o->it_value.tv_sec) |
175 		 __put_user(i->it_value.tv_usec, &o->it_value.tv_usec)));
176 }
177 
178 asmlinkage long compat_sys_getitimer(int which,
179 		struct compat_itimerval __user *it)
180 {
181 	struct itimerval kit;
182 	int error;
183 
184 	error = do_getitimer(which, &kit);
185 	if (!error && put_compat_itimerval(it, &kit))
186 		error = -EFAULT;
187 	return error;
188 }
189 
190 asmlinkage long compat_sys_setitimer(int which,
191 		struct compat_itimerval __user *in,
192 		struct compat_itimerval __user *out)
193 {
194 	struct itimerval kin, kout;
195 	int error;
196 
197 	if (in) {
198 		if (get_compat_itimerval(&kin, in))
199 			return -EFAULT;
200 	} else
201 		memset(&kin, 0, sizeof(kin));
202 
203 	error = do_setitimer(which, &kin, out ? &kout : NULL);
204 	if (error || !out)
205 		return error;
206 	if (put_compat_itimerval(out, &kout))
207 		return -EFAULT;
208 	return 0;
209 }
210 
211 asmlinkage long compat_sys_times(struct compat_tms __user *tbuf)
212 {
213 	/*
214 	 *	In the SMP world we might just be unlucky and have one of
215 	 *	the times increment as we use it. Since the value is an
216 	 *	atomically safe type this is just fine. Conceptually its
217 	 *	as if the syscall took an instant longer to occur.
218 	 */
219 	if (tbuf) {
220 		struct compat_tms tmp;
221 		struct task_struct *tsk = current;
222 		struct task_struct *t;
223 		cputime_t utime, stime, cutime, cstime;
224 
225 		read_lock(&tasklist_lock);
226 		utime = tsk->signal->utime;
227 		stime = tsk->signal->stime;
228 		t = tsk;
229 		do {
230 			utime = cputime_add(utime, t->utime);
231 			stime = cputime_add(stime, t->stime);
232 			t = next_thread(t);
233 		} while (t != tsk);
234 
235 		/*
236 		 * While we have tasklist_lock read-locked, no dying thread
237 		 * can be updating current->signal->[us]time.  Instead,
238 		 * we got their counts included in the live thread loop.
239 		 * However, another thread can come in right now and
240 		 * do a wait call that updates current->signal->c[us]time.
241 		 * To make sure we always see that pair updated atomically,
242 		 * we take the siglock around fetching them.
243 		 */
244 		spin_lock_irq(&tsk->sighand->siglock);
245 		cutime = tsk->signal->cutime;
246 		cstime = tsk->signal->cstime;
247 		spin_unlock_irq(&tsk->sighand->siglock);
248 		read_unlock(&tasklist_lock);
249 
250 		tmp.tms_utime = compat_jiffies_to_clock_t(cputime_to_jiffies(utime));
251 		tmp.tms_stime = compat_jiffies_to_clock_t(cputime_to_jiffies(stime));
252 		tmp.tms_cutime = compat_jiffies_to_clock_t(cputime_to_jiffies(cutime));
253 		tmp.tms_cstime = compat_jiffies_to_clock_t(cputime_to_jiffies(cstime));
254 		if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
255 			return -EFAULT;
256 	}
257 	return compat_jiffies_to_clock_t(jiffies);
258 }
259 
260 /*
261  * Assumption: old_sigset_t and compat_old_sigset_t are both
262  * types that can be passed to put_user()/get_user().
263  */
264 
265 asmlinkage long compat_sys_sigpending(compat_old_sigset_t __user *set)
266 {
267 	old_sigset_t s;
268 	long ret;
269 	mm_segment_t old_fs = get_fs();
270 
271 	set_fs(KERNEL_DS);
272 	ret = sys_sigpending((old_sigset_t __user *) &s);
273 	set_fs(old_fs);
274 	if (ret == 0)
275 		ret = put_user(s, set);
276 	return ret;
277 }
278 
279 asmlinkage long compat_sys_sigprocmask(int how, compat_old_sigset_t __user *set,
280 		compat_old_sigset_t __user *oset)
281 {
282 	old_sigset_t s;
283 	long ret;
284 	mm_segment_t old_fs;
285 
286 	if (set && get_user(s, set))
287 		return -EFAULT;
288 	old_fs = get_fs();
289 	set_fs(KERNEL_DS);
290 	ret = sys_sigprocmask(how,
291 			      set ? (old_sigset_t __user *) &s : NULL,
292 			      oset ? (old_sigset_t __user *) &s : NULL);
293 	set_fs(old_fs);
294 	if (ret == 0)
295 		if (oset)
296 			ret = put_user(s, oset);
297 	return ret;
298 }
299 
300 asmlinkage long compat_sys_setrlimit(unsigned int resource,
301 		struct compat_rlimit __user *rlim)
302 {
303 	struct rlimit r;
304 	int ret;
305 	mm_segment_t old_fs = get_fs ();
306 
307 	if (resource >= RLIM_NLIMITS)
308 		return -EINVAL;
309 
310 	if (!access_ok(VERIFY_READ, rlim, sizeof(*rlim)) ||
311 	    __get_user(r.rlim_cur, &rlim->rlim_cur) ||
312 	    __get_user(r.rlim_max, &rlim->rlim_max))
313 		return -EFAULT;
314 
315 	if (r.rlim_cur == COMPAT_RLIM_INFINITY)
316 		r.rlim_cur = RLIM_INFINITY;
317 	if (r.rlim_max == COMPAT_RLIM_INFINITY)
318 		r.rlim_max = RLIM_INFINITY;
319 	set_fs(KERNEL_DS);
320 	ret = sys_setrlimit(resource, (struct rlimit __user *) &r);
321 	set_fs(old_fs);
322 	return ret;
323 }
324 
325 #ifdef COMPAT_RLIM_OLD_INFINITY
326 
327 asmlinkage long compat_sys_old_getrlimit(unsigned int resource,
328 		struct compat_rlimit __user *rlim)
329 {
330 	struct rlimit r;
331 	int ret;
332 	mm_segment_t old_fs = get_fs();
333 
334 	set_fs(KERNEL_DS);
335 	ret = sys_old_getrlimit(resource, &r);
336 	set_fs(old_fs);
337 
338 	if (!ret) {
339 		if (r.rlim_cur > COMPAT_RLIM_OLD_INFINITY)
340 			r.rlim_cur = COMPAT_RLIM_INFINITY;
341 		if (r.rlim_max > COMPAT_RLIM_OLD_INFINITY)
342 			r.rlim_max = COMPAT_RLIM_INFINITY;
343 
344 		if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
345 		    __put_user(r.rlim_cur, &rlim->rlim_cur) ||
346 		    __put_user(r.rlim_max, &rlim->rlim_max))
347 			return -EFAULT;
348 	}
349 	return ret;
350 }
351 
352 #endif
353 
354 asmlinkage long compat_sys_getrlimit (unsigned int resource,
355 		struct compat_rlimit __user *rlim)
356 {
357 	struct rlimit r;
358 	int ret;
359 	mm_segment_t old_fs = get_fs();
360 
361 	set_fs(KERNEL_DS);
362 	ret = sys_getrlimit(resource, (struct rlimit __user *) &r);
363 	set_fs(old_fs);
364 	if (!ret) {
365 		if (r.rlim_cur > COMPAT_RLIM_INFINITY)
366 			r.rlim_cur = COMPAT_RLIM_INFINITY;
367 		if (r.rlim_max > COMPAT_RLIM_INFINITY)
368 			r.rlim_max = COMPAT_RLIM_INFINITY;
369 
370 		if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
371 		    __put_user(r.rlim_cur, &rlim->rlim_cur) ||
372 		    __put_user(r.rlim_max, &rlim->rlim_max))
373 			return -EFAULT;
374 	}
375 	return ret;
376 }
377 
378 int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru)
379 {
380 	if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)) ||
381 	    __put_user(r->ru_utime.tv_sec, &ru->ru_utime.tv_sec) ||
382 	    __put_user(r->ru_utime.tv_usec, &ru->ru_utime.tv_usec) ||
383 	    __put_user(r->ru_stime.tv_sec, &ru->ru_stime.tv_sec) ||
384 	    __put_user(r->ru_stime.tv_usec, &ru->ru_stime.tv_usec) ||
385 	    __put_user(r->ru_maxrss, &ru->ru_maxrss) ||
386 	    __put_user(r->ru_ixrss, &ru->ru_ixrss) ||
387 	    __put_user(r->ru_idrss, &ru->ru_idrss) ||
388 	    __put_user(r->ru_isrss, &ru->ru_isrss) ||
389 	    __put_user(r->ru_minflt, &ru->ru_minflt) ||
390 	    __put_user(r->ru_majflt, &ru->ru_majflt) ||
391 	    __put_user(r->ru_nswap, &ru->ru_nswap) ||
392 	    __put_user(r->ru_inblock, &ru->ru_inblock) ||
393 	    __put_user(r->ru_oublock, &ru->ru_oublock) ||
394 	    __put_user(r->ru_msgsnd, &ru->ru_msgsnd) ||
395 	    __put_user(r->ru_msgrcv, &ru->ru_msgrcv) ||
396 	    __put_user(r->ru_nsignals, &ru->ru_nsignals) ||
397 	    __put_user(r->ru_nvcsw, &ru->ru_nvcsw) ||
398 	    __put_user(r->ru_nivcsw, &ru->ru_nivcsw))
399 		return -EFAULT;
400 	return 0;
401 }
402 
403 asmlinkage long compat_sys_getrusage(int who, struct compat_rusage __user *ru)
404 {
405 	struct rusage r;
406 	int ret;
407 	mm_segment_t old_fs = get_fs();
408 
409 	set_fs(KERNEL_DS);
410 	ret = sys_getrusage(who, (struct rusage __user *) &r);
411 	set_fs(old_fs);
412 
413 	if (ret)
414 		return ret;
415 
416 	if (put_compat_rusage(&r, ru))
417 		return -EFAULT;
418 
419 	return 0;
420 }
421 
422 asmlinkage long
423 compat_sys_wait4(compat_pid_t pid, compat_uint_t __user *stat_addr, int options,
424 	struct compat_rusage __user *ru)
425 {
426 	if (!ru) {
427 		return sys_wait4(pid, stat_addr, options, NULL);
428 	} else {
429 		struct rusage r;
430 		int ret;
431 		unsigned int status;
432 		mm_segment_t old_fs = get_fs();
433 
434 		set_fs (KERNEL_DS);
435 		ret = sys_wait4(pid,
436 				(stat_addr ?
437 				 (unsigned int __user *) &status : NULL),
438 				options, (struct rusage __user *) &r);
439 		set_fs (old_fs);
440 
441 		if (ret > 0) {
442 			if (put_compat_rusage(&r, ru))
443 				return -EFAULT;
444 			if (stat_addr && put_user(status, stat_addr))
445 				return -EFAULT;
446 		}
447 		return ret;
448 	}
449 }
450 
451 asmlinkage long compat_sys_waitid(int which, compat_pid_t pid,
452 		struct compat_siginfo __user *uinfo, int options,
453 		struct compat_rusage __user *uru)
454 {
455 	siginfo_t info;
456 	struct rusage ru;
457 	long ret;
458 	mm_segment_t old_fs = get_fs();
459 
460 	memset(&info, 0, sizeof(info));
461 
462 	set_fs(KERNEL_DS);
463 	ret = sys_waitid(which, pid, (siginfo_t __user *)&info, options,
464 			 uru ? (struct rusage __user *)&ru : NULL);
465 	set_fs(old_fs);
466 
467 	if ((ret < 0) || (info.si_signo == 0))
468 		return ret;
469 
470 	if (uru) {
471 		ret = put_compat_rusage(&ru, uru);
472 		if (ret)
473 			return ret;
474 	}
475 
476 	BUG_ON(info.si_code & __SI_MASK);
477 	info.si_code |= __SI_CHLD;
478 	return copy_siginfo_to_user32(uinfo, &info);
479 }
480 
481 static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr,
482 				    unsigned len, cpumask_t *new_mask)
483 {
484 	unsigned long *k;
485 
486 	if (len < sizeof(cpumask_t))
487 		memset(new_mask, 0, sizeof(cpumask_t));
488 	else if (len > sizeof(cpumask_t))
489 		len = sizeof(cpumask_t);
490 
491 	k = cpus_addr(*new_mask);
492 	return compat_get_bitmap(k, user_mask_ptr, len * 8);
493 }
494 
495 asmlinkage long compat_sys_sched_setaffinity(compat_pid_t pid,
496 					     unsigned int len,
497 					     compat_ulong_t __user *user_mask_ptr)
498 {
499 	cpumask_t new_mask;
500 	int retval;
501 
502 	retval = compat_get_user_cpu_mask(user_mask_ptr, len, &new_mask);
503 	if (retval)
504 		return retval;
505 
506 	return sched_setaffinity(pid, &new_mask);
507 }
508 
509 asmlinkage long compat_sys_sched_getaffinity(compat_pid_t pid, unsigned int len,
510 					     compat_ulong_t __user *user_mask_ptr)
511 {
512 	int ret;
513 	cpumask_t mask;
514 	unsigned long *k;
515 	unsigned int min_length = sizeof(cpumask_t);
516 
517 	if (NR_CPUS <= BITS_PER_COMPAT_LONG)
518 		min_length = sizeof(compat_ulong_t);
519 
520 	if (len < min_length)
521 		return -EINVAL;
522 
523 	ret = sched_getaffinity(pid, &mask);
524 	if (ret < 0)
525 		return ret;
526 
527 	k = cpus_addr(mask);
528 	ret = compat_put_bitmap(user_mask_ptr, k, min_length * 8);
529 	if (ret)
530 		return ret;
531 
532 	return min_length;
533 }
534 
535 int get_compat_itimerspec(struct itimerspec *dst,
536 			  const struct compat_itimerspec __user *src)
537 {
538 	if (get_compat_timespec(&dst->it_interval, &src->it_interval) ||
539 	    get_compat_timespec(&dst->it_value, &src->it_value))
540 		return -EFAULT;
541 	return 0;
542 }
543 
544 int put_compat_itimerspec(struct compat_itimerspec __user *dst,
545 			  const struct itimerspec *src)
546 {
547 	if (put_compat_timespec(&src->it_interval, &dst->it_interval) ||
548 	    put_compat_timespec(&src->it_value, &dst->it_value))
549 		return -EFAULT;
550 	return 0;
551 }
552 
553 long compat_sys_timer_create(clockid_t which_clock,
554 			struct compat_sigevent __user *timer_event_spec,
555 			timer_t __user *created_timer_id)
556 {
557 	struct sigevent __user *event = NULL;
558 
559 	if (timer_event_spec) {
560 		struct sigevent kevent;
561 
562 		event = compat_alloc_user_space(sizeof(*event));
563 		if (get_compat_sigevent(&kevent, timer_event_spec) ||
564 		    copy_to_user(event, &kevent, sizeof(*event)))
565 			return -EFAULT;
566 	}
567 
568 	return sys_timer_create(which_clock, event, created_timer_id);
569 }
570 
571 long compat_sys_timer_settime(timer_t timer_id, int flags,
572 			  struct compat_itimerspec __user *new,
573 			  struct compat_itimerspec __user *old)
574 {
575 	long err;
576 	mm_segment_t oldfs;
577 	struct itimerspec newts, oldts;
578 
579 	if (!new)
580 		return -EINVAL;
581 	if (get_compat_itimerspec(&newts, new))
582 		return -EFAULT;
583 	oldfs = get_fs();
584 	set_fs(KERNEL_DS);
585 	err = sys_timer_settime(timer_id, flags,
586 				(struct itimerspec __user *) &newts,
587 				(struct itimerspec __user *) &oldts);
588 	set_fs(oldfs);
589 	if (!err && old && put_compat_itimerspec(old, &oldts))
590 		return -EFAULT;
591 	return err;
592 }
593 
594 long compat_sys_timer_gettime(timer_t timer_id,
595 		struct compat_itimerspec __user *setting)
596 {
597 	long err;
598 	mm_segment_t oldfs;
599 	struct itimerspec ts;
600 
601 	oldfs = get_fs();
602 	set_fs(KERNEL_DS);
603 	err = sys_timer_gettime(timer_id,
604 				(struct itimerspec __user *) &ts);
605 	set_fs(oldfs);
606 	if (!err && put_compat_itimerspec(setting, &ts))
607 		return -EFAULT;
608 	return err;
609 }
610 
611 long compat_sys_clock_settime(clockid_t which_clock,
612 		struct compat_timespec __user *tp)
613 {
614 	long err;
615 	mm_segment_t oldfs;
616 	struct timespec ts;
617 
618 	if (get_compat_timespec(&ts, tp))
619 		return -EFAULT;
620 	oldfs = get_fs();
621 	set_fs(KERNEL_DS);
622 	err = sys_clock_settime(which_clock,
623 				(struct timespec __user *) &ts);
624 	set_fs(oldfs);
625 	return err;
626 }
627 
628 long compat_sys_clock_gettime(clockid_t which_clock,
629 		struct compat_timespec __user *tp)
630 {
631 	long err;
632 	mm_segment_t oldfs;
633 	struct timespec ts;
634 
635 	oldfs = get_fs();
636 	set_fs(KERNEL_DS);
637 	err = sys_clock_gettime(which_clock,
638 				(struct timespec __user *) &ts);
639 	set_fs(oldfs);
640 	if (!err && put_compat_timespec(&ts, tp))
641 		return -EFAULT;
642 	return err;
643 }
644 
645 long compat_sys_clock_getres(clockid_t which_clock,
646 		struct compat_timespec __user *tp)
647 {
648 	long err;
649 	mm_segment_t oldfs;
650 	struct timespec ts;
651 
652 	oldfs = get_fs();
653 	set_fs(KERNEL_DS);
654 	err = sys_clock_getres(which_clock,
655 			       (struct timespec __user *) &ts);
656 	set_fs(oldfs);
657 	if (!err && tp && put_compat_timespec(&ts, tp))
658 		return -EFAULT;
659 	return err;
660 }
661 
662 static long compat_clock_nanosleep_restart(struct restart_block *restart)
663 {
664 	long err;
665 	mm_segment_t oldfs;
666 	struct timespec tu;
667 	struct compat_timespec *rmtp = restart->nanosleep.compat_rmtp;
668 
669 	restart->nanosleep.rmtp = (struct timespec __user *) &tu;
670 	oldfs = get_fs();
671 	set_fs(KERNEL_DS);
672 	err = clock_nanosleep_restart(restart);
673 	set_fs(oldfs);
674 
675 	if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
676 	    put_compat_timespec(&tu, rmtp))
677 		return -EFAULT;
678 
679 	if (err == -ERESTART_RESTARTBLOCK) {
680 		restart->fn = compat_clock_nanosleep_restart;
681 		restart->nanosleep.compat_rmtp = rmtp;
682 	}
683 	return err;
684 }
685 
686 long compat_sys_clock_nanosleep(clockid_t which_clock, int flags,
687 			    struct compat_timespec __user *rqtp,
688 			    struct compat_timespec __user *rmtp)
689 {
690 	long err;
691 	mm_segment_t oldfs;
692 	struct timespec in, out;
693 	struct restart_block *restart;
694 
695 	if (get_compat_timespec(&in, rqtp))
696 		return -EFAULT;
697 
698 	oldfs = get_fs();
699 	set_fs(KERNEL_DS);
700 	err = sys_clock_nanosleep(which_clock, flags,
701 				  (struct timespec __user *) &in,
702 				  (struct timespec __user *) &out);
703 	set_fs(oldfs);
704 
705 	if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
706 	    put_compat_timespec(&out, rmtp))
707 		return -EFAULT;
708 
709 	if (err == -ERESTART_RESTARTBLOCK) {
710 		restart = &current_thread_info()->restart_block;
711 		restart->fn = compat_clock_nanosleep_restart;
712 		restart->nanosleep.compat_rmtp = rmtp;
713 	}
714 	return err;
715 }
716 
717 /*
718  * We currently only need the following fields from the sigevent
719  * structure: sigev_value, sigev_signo, sig_notify and (sometimes
720  * sigev_notify_thread_id).  The others are handled in user mode.
721  * We also assume that copying sigev_value.sival_int is sufficient
722  * to keep all the bits of sigev_value.sival_ptr intact.
723  */
724 int get_compat_sigevent(struct sigevent *event,
725 		const struct compat_sigevent __user *u_event)
726 {
727 	memset(event, 0, sizeof(*event));
728 	return (!access_ok(VERIFY_READ, u_event, sizeof(*u_event)) ||
729 		__get_user(event->sigev_value.sival_int,
730 			&u_event->sigev_value.sival_int) ||
731 		__get_user(event->sigev_signo, &u_event->sigev_signo) ||
732 		__get_user(event->sigev_notify, &u_event->sigev_notify) ||
733 		__get_user(event->sigev_notify_thread_id,
734 			&u_event->sigev_notify_thread_id))
735 		? -EFAULT : 0;
736 }
737 
738 long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask,
739 		       unsigned long bitmap_size)
740 {
741 	int i, j;
742 	unsigned long m;
743 	compat_ulong_t um;
744 	unsigned long nr_compat_longs;
745 
746 	/* align bitmap up to nearest compat_long_t boundary */
747 	bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
748 
749 	if (!access_ok(VERIFY_READ, umask, bitmap_size / 8))
750 		return -EFAULT;
751 
752 	nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
753 
754 	for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
755 		m = 0;
756 
757 		for (j = 0; j < sizeof(m)/sizeof(um); j++) {
758 			/*
759 			 * We dont want to read past the end of the userspace
760 			 * bitmap. We must however ensure the end of the
761 			 * kernel bitmap is zeroed.
762 			 */
763 			if (nr_compat_longs-- > 0) {
764 				if (__get_user(um, umask))
765 					return -EFAULT;
766 			} else {
767 				um = 0;
768 			}
769 
770 			umask++;
771 			m |= (long)um << (j * BITS_PER_COMPAT_LONG);
772 		}
773 		*mask++ = m;
774 	}
775 
776 	return 0;
777 }
778 
779 long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
780 		       unsigned long bitmap_size)
781 {
782 	int i, j;
783 	unsigned long m;
784 	compat_ulong_t um;
785 	unsigned long nr_compat_longs;
786 
787 	/* align bitmap up to nearest compat_long_t boundary */
788 	bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
789 
790 	if (!access_ok(VERIFY_WRITE, umask, bitmap_size / 8))
791 		return -EFAULT;
792 
793 	nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
794 
795 	for (i = 0; i < BITS_TO_LONGS(bitmap_size); i++) {
796 		m = *mask++;
797 
798 		for (j = 0; j < sizeof(m)/sizeof(um); j++) {
799 			um = m;
800 
801 			/*
802 			 * We dont want to write past the end of the userspace
803 			 * bitmap.
804 			 */
805 			if (nr_compat_longs-- > 0) {
806 				if (__put_user(um, umask))
807 					return -EFAULT;
808 			}
809 
810 			umask++;
811 			m >>= 4*sizeof(um);
812 			m >>= 4*sizeof(um);
813 		}
814 	}
815 
816 	return 0;
817 }
818 
819 void
820 sigset_from_compat (sigset_t *set, compat_sigset_t *compat)
821 {
822 	switch (_NSIG_WORDS) {
823 	case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32 );
824 	case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32 );
825 	case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32 );
826 	case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32 );
827 	}
828 }
829 
830 asmlinkage long
831 compat_sys_rt_sigtimedwait (compat_sigset_t __user *uthese,
832 		struct compat_siginfo __user *uinfo,
833 		struct compat_timespec __user *uts, compat_size_t sigsetsize)
834 {
835 	compat_sigset_t s32;
836 	sigset_t s;
837 	int sig;
838 	struct timespec t;
839 	siginfo_t info;
840 	long ret, timeout = 0;
841 
842 	if (sigsetsize != sizeof(sigset_t))
843 		return -EINVAL;
844 
845 	if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t)))
846 		return -EFAULT;
847 	sigset_from_compat(&s, &s32);
848 	sigdelsetmask(&s,sigmask(SIGKILL)|sigmask(SIGSTOP));
849 	signotset(&s);
850 
851 	if (uts) {
852 		if (get_compat_timespec (&t, uts))
853 			return -EFAULT;
854 		if (t.tv_nsec >= 1000000000L || t.tv_nsec < 0
855 				|| t.tv_sec < 0)
856 			return -EINVAL;
857 	}
858 
859 	spin_lock_irq(&current->sighand->siglock);
860 	sig = dequeue_signal(current, &s, &info);
861 	if (!sig) {
862 		timeout = MAX_SCHEDULE_TIMEOUT;
863 		if (uts)
864 			timeout = timespec_to_jiffies(&t)
865 				+(t.tv_sec || t.tv_nsec);
866 		if (timeout) {
867 			current->real_blocked = current->blocked;
868 			sigandsets(&current->blocked, &current->blocked, &s);
869 
870 			recalc_sigpending();
871 			spin_unlock_irq(&current->sighand->siglock);
872 
873 			timeout = schedule_timeout_interruptible(timeout);
874 
875 			spin_lock_irq(&current->sighand->siglock);
876 			sig = dequeue_signal(current, &s, &info);
877 			current->blocked = current->real_blocked;
878 			siginitset(&current->real_blocked, 0);
879 			recalc_sigpending();
880 		}
881 	}
882 	spin_unlock_irq(&current->sighand->siglock);
883 
884 	if (sig) {
885 		ret = sig;
886 		if (uinfo) {
887 			if (copy_siginfo_to_user32(uinfo, &info))
888 				ret = -EFAULT;
889 		}
890 	}else {
891 		ret = timeout?-EINTR:-EAGAIN;
892 	}
893 	return ret;
894 
895 }
896 
897 #ifdef __ARCH_WANT_COMPAT_SYS_TIME
898 
899 /* compat_time_t is a 32 bit "long" and needs to get converted. */
900 
901 asmlinkage long compat_sys_time(compat_time_t __user * tloc)
902 {
903 	compat_time_t i;
904 	struct timeval tv;
905 
906 	do_gettimeofday(&tv);
907 	i = tv.tv_sec;
908 
909 	if (tloc) {
910 		if (put_user(i,tloc))
911 			i = -EFAULT;
912 	}
913 	return i;
914 }
915 
916 asmlinkage long compat_sys_stime(compat_time_t __user *tptr)
917 {
918 	struct timespec tv;
919 	int err;
920 
921 	if (get_user(tv.tv_sec, tptr))
922 		return -EFAULT;
923 
924 	tv.tv_nsec = 0;
925 
926 	err = security_settime(&tv, NULL);
927 	if (err)
928 		return err;
929 
930 	do_settimeofday(&tv);
931 	return 0;
932 }
933 
934 #endif /* __ARCH_WANT_COMPAT_SYS_TIME */
935 
936 #ifdef __ARCH_WANT_COMPAT_SYS_RT_SIGSUSPEND
937 asmlinkage long compat_sys_rt_sigsuspend(compat_sigset_t __user *unewset, compat_size_t sigsetsize)
938 {
939 	sigset_t newset;
940 	compat_sigset_t newset32;
941 
942 	/* XXX: Don't preclude handling different sized sigset_t's.  */
943 	if (sigsetsize != sizeof(sigset_t))
944 		return -EINVAL;
945 
946 	if (copy_from_user(&newset32, unewset, sizeof(compat_sigset_t)))
947 		return -EFAULT;
948 	sigset_from_compat(&newset, &newset32);
949 	sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
950 
951 	spin_lock_irq(&current->sighand->siglock);
952 	current->saved_sigmask = current->blocked;
953 	current->blocked = newset;
954 	recalc_sigpending();
955 	spin_unlock_irq(&current->sighand->siglock);
956 
957 	current->state = TASK_INTERRUPTIBLE;
958 	schedule();
959 	set_restore_sigmask();
960 	return -ERESTARTNOHAND;
961 }
962 #endif /* __ARCH_WANT_COMPAT_SYS_RT_SIGSUSPEND */
963 
964 asmlinkage long compat_sys_adjtimex(struct compat_timex __user *utp)
965 {
966 	struct timex txc;
967 	int ret;
968 
969 	memset(&txc, 0, sizeof(struct timex));
970 
971 	if (!access_ok(VERIFY_READ, utp, sizeof(struct compat_timex)) ||
972 			__get_user(txc.modes, &utp->modes) ||
973 			__get_user(txc.offset, &utp->offset) ||
974 			__get_user(txc.freq, &utp->freq) ||
975 			__get_user(txc.maxerror, &utp->maxerror) ||
976 			__get_user(txc.esterror, &utp->esterror) ||
977 			__get_user(txc.status, &utp->status) ||
978 			__get_user(txc.constant, &utp->constant) ||
979 			__get_user(txc.precision, &utp->precision) ||
980 			__get_user(txc.tolerance, &utp->tolerance) ||
981 			__get_user(txc.time.tv_sec, &utp->time.tv_sec) ||
982 			__get_user(txc.time.tv_usec, &utp->time.tv_usec) ||
983 			__get_user(txc.tick, &utp->tick) ||
984 			__get_user(txc.ppsfreq, &utp->ppsfreq) ||
985 			__get_user(txc.jitter, &utp->jitter) ||
986 			__get_user(txc.shift, &utp->shift) ||
987 			__get_user(txc.stabil, &utp->stabil) ||
988 			__get_user(txc.jitcnt, &utp->jitcnt) ||
989 			__get_user(txc.calcnt, &utp->calcnt) ||
990 			__get_user(txc.errcnt, &utp->errcnt) ||
991 			__get_user(txc.stbcnt, &utp->stbcnt))
992 		return -EFAULT;
993 
994 	ret = do_adjtimex(&txc);
995 
996 	if (!access_ok(VERIFY_WRITE, utp, sizeof(struct compat_timex)) ||
997 			__put_user(txc.modes, &utp->modes) ||
998 			__put_user(txc.offset, &utp->offset) ||
999 			__put_user(txc.freq, &utp->freq) ||
1000 			__put_user(txc.maxerror, &utp->maxerror) ||
1001 			__put_user(txc.esterror, &utp->esterror) ||
1002 			__put_user(txc.status, &utp->status) ||
1003 			__put_user(txc.constant, &utp->constant) ||
1004 			__put_user(txc.precision, &utp->precision) ||
1005 			__put_user(txc.tolerance, &utp->tolerance) ||
1006 			__put_user(txc.time.tv_sec, &utp->time.tv_sec) ||
1007 			__put_user(txc.time.tv_usec, &utp->time.tv_usec) ||
1008 			__put_user(txc.tick, &utp->tick) ||
1009 			__put_user(txc.ppsfreq, &utp->ppsfreq) ||
1010 			__put_user(txc.jitter, &utp->jitter) ||
1011 			__put_user(txc.shift, &utp->shift) ||
1012 			__put_user(txc.stabil, &utp->stabil) ||
1013 			__put_user(txc.jitcnt, &utp->jitcnt) ||
1014 			__put_user(txc.calcnt, &utp->calcnt) ||
1015 			__put_user(txc.errcnt, &utp->errcnt) ||
1016 			__put_user(txc.stbcnt, &utp->stbcnt) ||
1017 			__put_user(txc.tai, &utp->tai))
1018 		ret = -EFAULT;
1019 
1020 	return ret;
1021 }
1022 
1023 #ifdef CONFIG_NUMA
1024 asmlinkage long compat_sys_move_pages(pid_t pid, unsigned long nr_pages,
1025 		compat_uptr_t __user *pages32,
1026 		const int __user *nodes,
1027 		int __user *status,
1028 		int flags)
1029 {
1030 	const void __user * __user *pages;
1031 	int i;
1032 
1033 	pages = compat_alloc_user_space(nr_pages * sizeof(void *));
1034 	for (i = 0; i < nr_pages; i++) {
1035 		compat_uptr_t p;
1036 
1037 		if (get_user(p, pages32 + i) ||
1038 			put_user(compat_ptr(p), pages + i))
1039 			return -EFAULT;
1040 	}
1041 	return sys_move_pages(pid, nr_pages, pages, nodes, status, flags);
1042 }
1043 
1044 asmlinkage long compat_sys_migrate_pages(compat_pid_t pid,
1045 			compat_ulong_t maxnode,
1046 			const compat_ulong_t __user *old_nodes,
1047 			const compat_ulong_t __user *new_nodes)
1048 {
1049 	unsigned long __user *old = NULL;
1050 	unsigned long __user *new = NULL;
1051 	nodemask_t tmp_mask;
1052 	unsigned long nr_bits;
1053 	unsigned long size;
1054 
1055 	nr_bits = min_t(unsigned long, maxnode - 1, MAX_NUMNODES);
1056 	size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
1057 	if (old_nodes) {
1058 		if (compat_get_bitmap(nodes_addr(tmp_mask), old_nodes, nr_bits))
1059 			return -EFAULT;
1060 		old = compat_alloc_user_space(new_nodes ? size * 2 : size);
1061 		if (new_nodes)
1062 			new = old + size / sizeof(unsigned long);
1063 		if (copy_to_user(old, nodes_addr(tmp_mask), size))
1064 			return -EFAULT;
1065 	}
1066 	if (new_nodes) {
1067 		if (compat_get_bitmap(nodes_addr(tmp_mask), new_nodes, nr_bits))
1068 			return -EFAULT;
1069 		if (new == NULL)
1070 			new = compat_alloc_user_space(size);
1071 		if (copy_to_user(new, nodes_addr(tmp_mask), size))
1072 			return -EFAULT;
1073 	}
1074 	return sys_migrate_pages(pid, nr_bits + 1, old, new);
1075 }
1076 #endif
1077 
1078 struct compat_sysinfo {
1079 	s32 uptime;
1080 	u32 loads[3];
1081 	u32 totalram;
1082 	u32 freeram;
1083 	u32 sharedram;
1084 	u32 bufferram;
1085 	u32 totalswap;
1086 	u32 freeswap;
1087 	u16 procs;
1088 	u16 pad;
1089 	u32 totalhigh;
1090 	u32 freehigh;
1091 	u32 mem_unit;
1092 	char _f[20-2*sizeof(u32)-sizeof(int)];
1093 };
1094 
1095 asmlinkage long
1096 compat_sys_sysinfo(struct compat_sysinfo __user *info)
1097 {
1098 	struct sysinfo s;
1099 
1100 	do_sysinfo(&s);
1101 
1102 	/* Check to see if any memory value is too large for 32-bit and scale
1103 	 *  down if needed
1104 	 */
1105 	if ((s.totalram >> 32) || (s.totalswap >> 32)) {
1106 		int bitcount = 0;
1107 
1108 		while (s.mem_unit < PAGE_SIZE) {
1109 			s.mem_unit <<= 1;
1110 			bitcount++;
1111 		}
1112 
1113 		s.totalram >>= bitcount;
1114 		s.freeram >>= bitcount;
1115 		s.sharedram >>= bitcount;
1116 		s.bufferram >>= bitcount;
1117 		s.totalswap >>= bitcount;
1118 		s.freeswap >>= bitcount;
1119 		s.totalhigh >>= bitcount;
1120 		s.freehigh >>= bitcount;
1121 	}
1122 
1123 	if (!access_ok(VERIFY_WRITE, info, sizeof(struct compat_sysinfo)) ||
1124 	    __put_user (s.uptime, &info->uptime) ||
1125 	    __put_user (s.loads[0], &info->loads[0]) ||
1126 	    __put_user (s.loads[1], &info->loads[1]) ||
1127 	    __put_user (s.loads[2], &info->loads[2]) ||
1128 	    __put_user (s.totalram, &info->totalram) ||
1129 	    __put_user (s.freeram, &info->freeram) ||
1130 	    __put_user (s.sharedram, &info->sharedram) ||
1131 	    __put_user (s.bufferram, &info->bufferram) ||
1132 	    __put_user (s.totalswap, &info->totalswap) ||
1133 	    __put_user (s.freeswap, &info->freeswap) ||
1134 	    __put_user (s.procs, &info->procs) ||
1135 	    __put_user (s.totalhigh, &info->totalhigh) ||
1136 	    __put_user (s.freehigh, &info->freehigh) ||
1137 	    __put_user (s.mem_unit, &info->mem_unit))
1138 		return -EFAULT;
1139 
1140 	return 0;
1141 }
1142