1 /*
2  * sys_ia32.c: Conversion between 32bit and 64bit native syscalls. Based on
3  *             sys_sparc32
4  *
5  * Copyright (C) 2000		VA Linux Co
6  * Copyright (C) 2000		Don Dugger <n0ano@valinux.com>
7  * Copyright (C) 1999		Arun Sharma <arun.sharma@intel.com>
8  * Copyright (C) 1997,1998	Jakub Jelinek (jj@sunsite.mff.cuni.cz)
9  * Copyright (C) 1997		David S. Miller (davem@caip.rutgers.edu)
10  * Copyright (C) 2000		Hewlett-Packard Co.
11  * Copyright (C) 2000		David Mosberger-Tang <davidm@hpl.hp.com>
12  * Copyright (C) 2000,2001,2002	Andi Kleen, SuSE Labs (x86-64 port)
13  *
14  * These routines maintain argument size conversion between 32bit and 64bit
15  * environment. In 2.5 most of this should be moved to a generic directory.
16  *
17  * This file assumes that there is a hole at the end of user address space.
18  *
19  * Some of the functions are LE specific currently. These are
20  * hopefully all marked.  This should be fixed.
21  */
22 
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/fs.h>
26 #include <linux/file.h>
27 #include <linux/signal.h>
28 #include <linux/syscalls.h>
29 #include <linux/times.h>
30 #include <linux/utsname.h>
31 #include <linux/mm.h>
32 #include <linux/uio.h>
33 #include <linux/poll.h>
34 #include <linux/personality.h>
35 #include <linux/stat.h>
36 #include <linux/rwsem.h>
37 #include <linux/compat.h>
38 #include <linux/vfs.h>
39 #include <linux/ptrace.h>
40 #include <linux/highuid.h>
41 #include <linux/sysctl.h>
42 #include <linux/slab.h>
43 #include <asm/mman.h>
44 #include <asm/types.h>
45 #include <asm/uaccess.h>
46 #include <linux/atomic.h>
47 #include <asm/vgtod.h>
48 #include <asm/sys_ia32.h>
49 
50 #define AA(__x)		((unsigned long)(__x))
51 
52 
sys32_truncate64(const char __user * filename,unsigned long offset_low,unsigned long offset_high)53 asmlinkage long sys32_truncate64(const char __user *filename,
54 				 unsigned long offset_low,
55 				 unsigned long offset_high)
56 {
57        return sys_truncate(filename, ((loff_t) offset_high << 32) | offset_low);
58 }
59 
sys32_ftruncate64(unsigned int fd,unsigned long offset_low,unsigned long offset_high)60 asmlinkage long sys32_ftruncate64(unsigned int fd, unsigned long offset_low,
61 				  unsigned long offset_high)
62 {
63        return sys_ftruncate(fd, ((loff_t) offset_high << 32) | offset_low);
64 }
65 
66 /*
67  * Another set for IA32/LFS -- x86_64 struct stat is different due to
68  * support for 64bit inode numbers.
69  */
cp_stat64(struct stat64 __user * ubuf,struct kstat * stat)70 static int cp_stat64(struct stat64 __user *ubuf, struct kstat *stat)
71 {
72 	typeof(ubuf->st_uid) uid = 0;
73 	typeof(ubuf->st_gid) gid = 0;
74 	SET_UID(uid, stat->uid);
75 	SET_GID(gid, stat->gid);
76 	if (!access_ok(VERIFY_WRITE, ubuf, sizeof(struct stat64)) ||
77 	    __put_user(huge_encode_dev(stat->dev), &ubuf->st_dev) ||
78 	    __put_user(stat->ino, &ubuf->__st_ino) ||
79 	    __put_user(stat->ino, &ubuf->st_ino) ||
80 	    __put_user(stat->mode, &ubuf->st_mode) ||
81 	    __put_user(stat->nlink, &ubuf->st_nlink) ||
82 	    __put_user(uid, &ubuf->st_uid) ||
83 	    __put_user(gid, &ubuf->st_gid) ||
84 	    __put_user(huge_encode_dev(stat->rdev), &ubuf->st_rdev) ||
85 	    __put_user(stat->size, &ubuf->st_size) ||
86 	    __put_user(stat->atime.tv_sec, &ubuf->st_atime) ||
87 	    __put_user(stat->atime.tv_nsec, &ubuf->st_atime_nsec) ||
88 	    __put_user(stat->mtime.tv_sec, &ubuf->st_mtime) ||
89 	    __put_user(stat->mtime.tv_nsec, &ubuf->st_mtime_nsec) ||
90 	    __put_user(stat->ctime.tv_sec, &ubuf->st_ctime) ||
91 	    __put_user(stat->ctime.tv_nsec, &ubuf->st_ctime_nsec) ||
92 	    __put_user(stat->blksize, &ubuf->st_blksize) ||
93 	    __put_user(stat->blocks, &ubuf->st_blocks))
94 		return -EFAULT;
95 	return 0;
96 }
97 
sys32_stat64(const char __user * filename,struct stat64 __user * statbuf)98 asmlinkage long sys32_stat64(const char __user *filename,
99 			     struct stat64 __user *statbuf)
100 {
101 	struct kstat stat;
102 	int ret = vfs_stat(filename, &stat);
103 
104 	if (!ret)
105 		ret = cp_stat64(statbuf, &stat);
106 	return ret;
107 }
108 
sys32_lstat64(const char __user * filename,struct stat64 __user * statbuf)109 asmlinkage long sys32_lstat64(const char __user *filename,
110 			      struct stat64 __user *statbuf)
111 {
112 	struct kstat stat;
113 	int ret = vfs_lstat(filename, &stat);
114 	if (!ret)
115 		ret = cp_stat64(statbuf, &stat);
116 	return ret;
117 }
118 
sys32_fstat64(unsigned int fd,struct stat64 __user * statbuf)119 asmlinkage long sys32_fstat64(unsigned int fd, struct stat64 __user *statbuf)
120 {
121 	struct kstat stat;
122 	int ret = vfs_fstat(fd, &stat);
123 	if (!ret)
124 		ret = cp_stat64(statbuf, &stat);
125 	return ret;
126 }
127 
sys32_fstatat(unsigned int dfd,const char __user * filename,struct stat64 __user * statbuf,int flag)128 asmlinkage long sys32_fstatat(unsigned int dfd, const char __user *filename,
129 			      struct stat64 __user *statbuf, int flag)
130 {
131 	struct kstat stat;
132 	int error;
133 
134 	error = vfs_fstatat(dfd, filename, &stat, flag);
135 	if (error)
136 		return error;
137 	return cp_stat64(statbuf, &stat);
138 }
139 
140 /*
141  * Linux/i386 didn't use to be able to handle more than
142  * 4 system call parameters, so these system calls used a memory
143  * block for parameter passing..
144  */
145 
146 struct mmap_arg_struct32 {
147 	unsigned int addr;
148 	unsigned int len;
149 	unsigned int prot;
150 	unsigned int flags;
151 	unsigned int fd;
152 	unsigned int offset;
153 };
154 
sys32_mmap(struct mmap_arg_struct32 __user * arg)155 asmlinkage long sys32_mmap(struct mmap_arg_struct32 __user *arg)
156 {
157 	struct mmap_arg_struct32 a;
158 
159 	if (copy_from_user(&a, arg, sizeof(a)))
160 		return -EFAULT;
161 
162 	if (a.offset & ~PAGE_MASK)
163 		return -EINVAL;
164 
165 	return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
166 			       a.offset>>PAGE_SHIFT);
167 }
168 
sys32_mprotect(unsigned long start,size_t len,unsigned long prot)169 asmlinkage long sys32_mprotect(unsigned long start, size_t len,
170 			       unsigned long prot)
171 {
172 	return sys_mprotect(start, len, prot);
173 }
174 
sys32_rt_sigaction(int sig,struct sigaction32 __user * act,struct sigaction32 __user * oact,unsigned int sigsetsize)175 asmlinkage long sys32_rt_sigaction(int sig, struct sigaction32 __user *act,
176 				   struct sigaction32 __user *oact,
177 				   unsigned int sigsetsize)
178 {
179 	struct k_sigaction new_ka, old_ka;
180 	int ret;
181 	compat_sigset_t set32;
182 
183 	/* XXX: Don't preclude handling different sized sigset_t's.  */
184 	if (sigsetsize != sizeof(compat_sigset_t))
185 		return -EINVAL;
186 
187 	if (act) {
188 		compat_uptr_t handler, restorer;
189 
190 		if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
191 		    __get_user(handler, &act->sa_handler) ||
192 		    __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
193 		    __get_user(restorer, &act->sa_restorer) ||
194 		    __copy_from_user(&set32, &act->sa_mask,
195 				     sizeof(compat_sigset_t)))
196 			return -EFAULT;
197 		new_ka.sa.sa_handler = compat_ptr(handler);
198 		new_ka.sa.sa_restorer = compat_ptr(restorer);
199 
200 		/*
201 		 * FIXME: here we rely on _COMPAT_NSIG_WORS to be >=
202 		 * than _NSIG_WORDS << 1
203 		 */
204 		switch (_NSIG_WORDS) {
205 		case 4: new_ka.sa.sa_mask.sig[3] = set32.sig[6]
206 				| (((long)set32.sig[7]) << 32);
207 		case 3: new_ka.sa.sa_mask.sig[2] = set32.sig[4]
208 				| (((long)set32.sig[5]) << 32);
209 		case 2: new_ka.sa.sa_mask.sig[1] = set32.sig[2]
210 				| (((long)set32.sig[3]) << 32);
211 		case 1: new_ka.sa.sa_mask.sig[0] = set32.sig[0]
212 				| (((long)set32.sig[1]) << 32);
213 		}
214 	}
215 
216 	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
217 
218 	if (!ret && oact) {
219 		/*
220 		 * FIXME: here we rely on _COMPAT_NSIG_WORS to be >=
221 		 * than _NSIG_WORDS << 1
222 		 */
223 		switch (_NSIG_WORDS) {
224 		case 4:
225 			set32.sig[7] = (old_ka.sa.sa_mask.sig[3] >> 32);
226 			set32.sig[6] = old_ka.sa.sa_mask.sig[3];
227 		case 3:
228 			set32.sig[5] = (old_ka.sa.sa_mask.sig[2] >> 32);
229 			set32.sig[4] = old_ka.sa.sa_mask.sig[2];
230 		case 2:
231 			set32.sig[3] = (old_ka.sa.sa_mask.sig[1] >> 32);
232 			set32.sig[2] = old_ka.sa.sa_mask.sig[1];
233 		case 1:
234 			set32.sig[1] = (old_ka.sa.sa_mask.sig[0] >> 32);
235 			set32.sig[0] = old_ka.sa.sa_mask.sig[0];
236 		}
237 		if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
238 		    __put_user(ptr_to_compat(old_ka.sa.sa_handler),
239 			       &oact->sa_handler) ||
240 		    __put_user(ptr_to_compat(old_ka.sa.sa_restorer),
241 			       &oact->sa_restorer) ||
242 		    __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
243 		    __copy_to_user(&oact->sa_mask, &set32,
244 				   sizeof(compat_sigset_t)))
245 			return -EFAULT;
246 	}
247 
248 	return ret;
249 }
250 
sys32_sigaction(int sig,struct old_sigaction32 __user * act,struct old_sigaction32 __user * oact)251 asmlinkage long sys32_sigaction(int sig, struct old_sigaction32 __user *act,
252 				struct old_sigaction32 __user *oact)
253 {
254 	struct k_sigaction new_ka, old_ka;
255 	int ret;
256 
257 	if (act) {
258 		compat_old_sigset_t mask;
259 		compat_uptr_t handler, restorer;
260 
261 		if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
262 		    __get_user(handler, &act->sa_handler) ||
263 		    __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
264 		    __get_user(restorer, &act->sa_restorer) ||
265 		    __get_user(mask, &act->sa_mask))
266 			return -EFAULT;
267 
268 		new_ka.sa.sa_handler = compat_ptr(handler);
269 		new_ka.sa.sa_restorer = compat_ptr(restorer);
270 
271 		siginitset(&new_ka.sa.sa_mask, mask);
272 	}
273 
274 	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
275 
276 	if (!ret && oact) {
277 		if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
278 		    __put_user(ptr_to_compat(old_ka.sa.sa_handler),
279 			       &oact->sa_handler) ||
280 		    __put_user(ptr_to_compat(old_ka.sa.sa_restorer),
281 			       &oact->sa_restorer) ||
282 		    __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
283 		    __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
284 			return -EFAULT;
285 	}
286 
287 	return ret;
288 }
289 
sys32_alarm(unsigned int seconds)290 asmlinkage long sys32_alarm(unsigned int seconds)
291 {
292 	return alarm_setitimer(seconds);
293 }
294 
sys32_waitpid(compat_pid_t pid,unsigned int * stat_addr,int options)295 asmlinkage long sys32_waitpid(compat_pid_t pid, unsigned int *stat_addr,
296 			      int options)
297 {
298 	return compat_sys_wait4(pid, stat_addr, options, NULL);
299 }
300 
301 /* 32-bit timeval and related flotsam.  */
302 
sys32_sysfs(int option,u32 arg1,u32 arg2)303 asmlinkage long sys32_sysfs(int option, u32 arg1, u32 arg2)
304 {
305 	return sys_sysfs(option, arg1, arg2);
306 }
307 
sys32_sched_rr_get_interval(compat_pid_t pid,struct compat_timespec __user * interval)308 asmlinkage long sys32_sched_rr_get_interval(compat_pid_t pid,
309 				    struct compat_timespec __user *interval)
310 {
311 	struct timespec t;
312 	int ret;
313 	mm_segment_t old_fs = get_fs();
314 
315 	set_fs(KERNEL_DS);
316 	ret = sys_sched_rr_get_interval(pid, (struct timespec __user *)&t);
317 	set_fs(old_fs);
318 	if (put_compat_timespec(&t, interval))
319 		return -EFAULT;
320 	return ret;
321 }
322 
sys32_rt_sigpending(compat_sigset_t __user * set,compat_size_t sigsetsize)323 asmlinkage long sys32_rt_sigpending(compat_sigset_t __user *set,
324 				    compat_size_t sigsetsize)
325 {
326 	sigset_t s;
327 	compat_sigset_t s32;
328 	int ret;
329 	mm_segment_t old_fs = get_fs();
330 
331 	set_fs(KERNEL_DS);
332 	ret = sys_rt_sigpending((sigset_t __user *)&s, sigsetsize);
333 	set_fs(old_fs);
334 	if (!ret) {
335 		switch (_NSIG_WORDS) {
336 		case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
337 		case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
338 		case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
339 		case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
340 		}
341 		if (copy_to_user(set, &s32, sizeof(compat_sigset_t)))
342 			return -EFAULT;
343 	}
344 	return ret;
345 }
346 
sys32_rt_sigqueueinfo(int pid,int sig,compat_siginfo_t __user * uinfo)347 asmlinkage long sys32_rt_sigqueueinfo(int pid, int sig,
348 				      compat_siginfo_t __user *uinfo)
349 {
350 	siginfo_t info;
351 	int ret;
352 	mm_segment_t old_fs = get_fs();
353 
354 	if (copy_siginfo_from_user32(&info, uinfo))
355 		return -EFAULT;
356 	set_fs(KERNEL_DS);
357 	ret = sys_rt_sigqueueinfo(pid, sig, (siginfo_t __user *)&info);
358 	set_fs(old_fs);
359 	return ret;
360 }
361 
362 /* warning: next two assume little endian */
sys32_pread(unsigned int fd,char __user * ubuf,u32 count,u32 poslo,u32 poshi)363 asmlinkage long sys32_pread(unsigned int fd, char __user *ubuf, u32 count,
364 			    u32 poslo, u32 poshi)
365 {
366 	return sys_pread64(fd, ubuf, count,
367 			 ((loff_t)AA(poshi) << 32) | AA(poslo));
368 }
369 
sys32_pwrite(unsigned int fd,const char __user * ubuf,u32 count,u32 poslo,u32 poshi)370 asmlinkage long sys32_pwrite(unsigned int fd, const char __user *ubuf,
371 			     u32 count, u32 poslo, u32 poshi)
372 {
373 	return sys_pwrite64(fd, ubuf, count,
374 			  ((loff_t)AA(poshi) << 32) | AA(poslo));
375 }
376 
377 
sys32_personality(unsigned long personality)378 asmlinkage long sys32_personality(unsigned long personality)
379 {
380 	int ret;
381 
382 	if (personality(current->personality) == PER_LINUX32 &&
383 		personality == PER_LINUX)
384 		personality = PER_LINUX32;
385 	ret = sys_personality(personality);
386 	if (ret == PER_LINUX32)
387 		ret = PER_LINUX;
388 	return ret;
389 }
390 
sys32_sendfile(int out_fd,int in_fd,compat_off_t __user * offset,s32 count)391 asmlinkage long sys32_sendfile(int out_fd, int in_fd,
392 			       compat_off_t __user *offset, s32 count)
393 {
394 	mm_segment_t old_fs = get_fs();
395 	int ret;
396 	off_t of;
397 
398 	if (offset && get_user(of, offset))
399 		return -EFAULT;
400 
401 	set_fs(KERNEL_DS);
402 	ret = sys_sendfile(out_fd, in_fd, offset ? (off_t __user *)&of : NULL,
403 			   count);
404 	set_fs(old_fs);
405 
406 	if (offset && put_user(of, offset))
407 		return -EFAULT;
408 	return ret;
409 }
410 
sys32_execve(const char __user * name,compat_uptr_t __user * argv,compat_uptr_t __user * envp,struct pt_regs * regs)411 asmlinkage long sys32_execve(const char __user *name, compat_uptr_t __user *argv,
412 			     compat_uptr_t __user *envp, struct pt_regs *regs)
413 {
414 	long error;
415 	char *filename;
416 
417 	filename = getname(name);
418 	error = PTR_ERR(filename);
419 	if (IS_ERR(filename))
420 		return error;
421 	error = compat_do_execve(filename, argv, envp, regs);
422 	putname(filename);
423 	return error;
424 }
425 
sys32_clone(unsigned int clone_flags,unsigned int newsp,struct pt_regs * regs)426 asmlinkage long sys32_clone(unsigned int clone_flags, unsigned int newsp,
427 			    struct pt_regs *regs)
428 {
429 	void __user *parent_tid = (void __user *)regs->dx;
430 	void __user *child_tid = (void __user *)regs->di;
431 
432 	if (!newsp)
433 		newsp = regs->sp;
434 	return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
435 }
436 
437 /*
438  * Some system calls that need sign extended arguments. This could be
439  * done by a generic wrapper.
440  */
sys32_lseek(unsigned int fd,int offset,unsigned int whence)441 long sys32_lseek(unsigned int fd, int offset, unsigned int whence)
442 {
443 	return sys_lseek(fd, offset, whence);
444 }
445 
sys32_kill(int pid,int sig)446 long sys32_kill(int pid, int sig)
447 {
448 	return sys_kill(pid, sig);
449 }
450 
sys32_fadvise64_64(int fd,__u32 offset_low,__u32 offset_high,__u32 len_low,__u32 len_high,int advice)451 long sys32_fadvise64_64(int fd, __u32 offset_low, __u32 offset_high,
452 			__u32 len_low, __u32 len_high, int advice)
453 {
454 	return sys_fadvise64_64(fd,
455 			       (((u64)offset_high)<<32) | offset_low,
456 			       (((u64)len_high)<<32) | len_low,
457 				advice);
458 }
459 
sys32_vm86_warning(void)460 long sys32_vm86_warning(void)
461 {
462 	struct task_struct *me = current;
463 	static char lastcomm[sizeof(me->comm)];
464 
465 	if (strncmp(lastcomm, me->comm, sizeof(lastcomm))) {
466 		compat_printk(KERN_INFO
467 			      "%s: vm86 mode not supported on 64 bit kernel\n",
468 			      me->comm);
469 		strncpy(lastcomm, me->comm, sizeof(lastcomm));
470 	}
471 	return -ENOSYS;
472 }
473 
sys32_lookup_dcookie(u32 addr_low,u32 addr_high,char __user * buf,size_t len)474 long sys32_lookup_dcookie(u32 addr_low, u32 addr_high,
475 			  char __user *buf, size_t len)
476 {
477 	return sys_lookup_dcookie(((u64)addr_high << 32) | addr_low, buf, len);
478 }
479 
sys32_readahead(int fd,unsigned off_lo,unsigned off_hi,size_t count)480 asmlinkage ssize_t sys32_readahead(int fd, unsigned off_lo, unsigned off_hi,
481 				   size_t count)
482 {
483 	return sys_readahead(fd, ((u64)off_hi << 32) | off_lo, count);
484 }
485 
sys32_sync_file_range(int fd,unsigned off_low,unsigned off_hi,unsigned n_low,unsigned n_hi,int flags)486 asmlinkage long sys32_sync_file_range(int fd, unsigned off_low, unsigned off_hi,
487 				      unsigned n_low, unsigned n_hi,  int flags)
488 {
489 	return sys_sync_file_range(fd,
490 				   ((u64)off_hi << 32) | off_low,
491 				   ((u64)n_hi << 32) | n_low, flags);
492 }
493 
sys32_fadvise64(int fd,unsigned offset_lo,unsigned offset_hi,size_t len,int advice)494 asmlinkage long sys32_fadvise64(int fd, unsigned offset_lo, unsigned offset_hi,
495 				size_t len, int advice)
496 {
497 	return sys_fadvise64_64(fd, ((u64)offset_hi << 32) | offset_lo,
498 				len, advice);
499 }
500 
sys32_fallocate(int fd,int mode,unsigned offset_lo,unsigned offset_hi,unsigned len_lo,unsigned len_hi)501 asmlinkage long sys32_fallocate(int fd, int mode, unsigned offset_lo,
502 				unsigned offset_hi, unsigned len_lo,
503 				unsigned len_hi)
504 {
505 	return sys_fallocate(fd, mode, ((u64)offset_hi << 32) | offset_lo,
506 			     ((u64)len_hi << 32) | len_lo);
507 }
508 
sys32_fanotify_mark(int fanotify_fd,unsigned int flags,u32 mask_lo,u32 mask_hi,int fd,const char __user * pathname)509 asmlinkage long sys32_fanotify_mark(int fanotify_fd, unsigned int flags,
510 				    u32 mask_lo, u32 mask_hi,
511 				    int fd, const char  __user *pathname)
512 {
513 	return sys_fanotify_mark(fanotify_fd, flags,
514 				 ((u64)mask_hi << 32) | mask_lo,
515 				 fd, pathname);
516 }
517