4 * Kernel compatibililty routines for e.g. 32 bit syscall support
7 * Copyright (C) 2002 Stephen Rothwell, IBM Corporation
8 * Copyright (C) 1997-2000 Jakub Jelinek (jakub@redhat.com)
9 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
10 * Copyright (C) 2001,2002 Andi Kleen, SuSE Labs
11 * Copyright (C) 2003 Pavel Machek (pavel@ucw.cz)
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
18 #include <linux/stddef.h>
19 #include <linux/kernel.h>
20 #include <linux/linkage.h>
21 #include <linux/compat.h>
22 #include <linux/errno.h>
23 #include <linux/time.h>
24 #include <linux/cred.h>
26 #include <linux/fcntl.h>
27 #include <linux/namei.h>
28 #include <linux/file.h>
29 #include <linux/fdtable.h>
30 #include <linux/vfs.h>
31 #include <linux/ioctl.h>
32 #include <linux/init.h>
33 #include <linux/ncp_mount.h>
34 #include <linux/nfs4_mount.h>
35 #include <linux/syscalls.h>
36 #include <linux/ctype.h>
37 #include <linux/dirent.h>
38 #include <linux/fsnotify.h>
39 #include <linux/highuid.h>
40 #include <linux/personality.h>
41 #include <linux/rwsem.h>
42 #include <linux/tsacct_kern.h>
43 #include <linux/security.h>
44 #include <linux/highmem.h>
45 #include <linux/signal.h>
47 #include <linux/fs_struct.h>
48 #include <linux/slab.h>
49 #include <linux/pagemap.h>
50 #include <linux/aio.h>
52 #include <linux/uaccess.h>
53 #include <asm/mmu_context.h>
54 #include <asm/ioctls.h>
57 static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
59 struct compat_stat tmp;
61 if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
64 memset(&tmp, 0, sizeof(tmp));
65 tmp.st_dev = old_encode_dev(stat->dev);
66 tmp.st_ino = stat->ino;
67 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
69 tmp.st_mode = stat->mode;
70 tmp.st_nlink = stat->nlink;
71 if (tmp.st_nlink != stat->nlink)
73 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
74 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
75 tmp.st_rdev = old_encode_dev(stat->rdev);
76 if ((u64) stat->size > MAX_NON_LFS)
78 tmp.st_size = stat->size;
79 tmp.st_atime = stat->atime.tv_sec;
80 tmp.st_atime_nsec = stat->atime.tv_nsec;
81 tmp.st_mtime = stat->mtime.tv_sec;
82 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
83 tmp.st_ctime = stat->ctime.tv_sec;
84 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
85 tmp.st_blocks = stat->blocks;
86 tmp.st_blksize = stat->blksize;
87 return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
90 COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename,
91 struct compat_stat __user *, statbuf)
96 error = vfs_stat(filename, &stat);
99 return cp_compat_stat(&stat, statbuf);
102 COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename,
103 struct compat_stat __user *, statbuf)
108 error = vfs_lstat(filename, &stat);
111 return cp_compat_stat(&stat, statbuf);
114 #ifndef __ARCH_WANT_STAT64
115 COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
116 const char __user *, filename,
117 struct compat_stat __user *, statbuf, int, flag)
122 error = vfs_fstatat(dfd, filename, &stat, flag);
125 return cp_compat_stat(&stat, statbuf);
129 COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd,
130 struct compat_stat __user *, statbuf)
133 int error = vfs_fstat(fd, &stat);
136 error = cp_compat_stat(&stat, statbuf);
140 static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
142 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
143 __get_user(kfl->l_type, &ufl->l_type) ||
144 __get_user(kfl->l_whence, &ufl->l_whence) ||
145 __get_user(kfl->l_start, &ufl->l_start) ||
146 __get_user(kfl->l_len, &ufl->l_len) ||
147 __get_user(kfl->l_pid, &ufl->l_pid))
152 static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
154 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
155 __put_user(kfl->l_type, &ufl->l_type) ||
156 __put_user(kfl->l_whence, &ufl->l_whence) ||
157 __put_user(kfl->l_start, &ufl->l_start) ||
158 __put_user(kfl->l_len, &ufl->l_len) ||
159 __put_user(kfl->l_pid, &ufl->l_pid))
164 #ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
165 static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
167 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
168 __get_user(kfl->l_type, &ufl->l_type) ||
169 __get_user(kfl->l_whence, &ufl->l_whence) ||
170 __get_user(kfl->l_start, &ufl->l_start) ||
171 __get_user(kfl->l_len, &ufl->l_len) ||
172 __get_user(kfl->l_pid, &ufl->l_pid))
178 #ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
179 static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
181 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
182 __put_user(kfl->l_type, &ufl->l_type) ||
183 __put_user(kfl->l_whence, &ufl->l_whence) ||
184 __put_user(kfl->l_start, &ufl->l_start) ||
185 __put_user(kfl->l_len, &ufl->l_len) ||
186 __put_user(kfl->l_pid, &ufl->l_pid))
193 convert_fcntl_cmd(unsigned int cmd)
207 COMPAT_SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
213 unsigned int conv_cmd;
219 ret = get_compat_flock(&f, compat_ptr(arg));
224 ret = sys_fcntl(fd, cmd, (unsigned long)&f);
226 if (cmd == F_GETLK && ret == 0) {
227 /* GETLK was successful and we need to return the data...
228 * but it needs to fit in the compat structure.
229 * l_start shouldn't be too big, unless the original
230 * start + end is greater than COMPAT_OFF_T_MAX, in which
231 * case the app was asking for trouble, so we return
232 * -EOVERFLOW in that case.
233 * l_len could be too big, in which case we just truncate it,
234 * and only allow the app to see that part of the conflicting
235 * lock that might make sense to it anyway
238 if (f.l_start > COMPAT_OFF_T_MAX)
240 if (f.l_len > COMPAT_OFF_T_MAX)
241 f.l_len = COMPAT_OFF_T_MAX;
243 ret = put_compat_flock(&f, compat_ptr(arg));
253 ret = get_compat_flock64(&f, compat_ptr(arg));
258 conv_cmd = convert_fcntl_cmd(cmd);
259 ret = sys_fcntl(fd, conv_cmd, (unsigned long)&f);
261 if ((conv_cmd == F_GETLK || conv_cmd == F_OFD_GETLK) && ret == 0) {
262 /* need to return lock information - see above for commentary */
263 if (f.l_start > COMPAT_LOFF_T_MAX)
265 if (f.l_len > COMPAT_LOFF_T_MAX)
266 f.l_len = COMPAT_LOFF_T_MAX;
268 ret = put_compat_flock64(&f, compat_ptr(arg));
273 ret = sys_fcntl(fd, cmd, arg);
279 COMPAT_SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd,
291 return compat_sys_fcntl64(fd, cmd, arg);
294 /* A write operation does a read from user space and vice versa */
295 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
297 ssize_t compat_rw_copy_check_uvector(int type,
298 const struct compat_iovec __user *uvector, unsigned long nr_segs,
299 unsigned long fast_segs, struct iovec *fast_pointer,
300 struct iovec **ret_pointer)
302 compat_ssize_t tot_len;
303 struct iovec *iov = *ret_pointer = fast_pointer;
308 * SuS says "The readv() function *may* fail if the iovcnt argument
309 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
310 * traditionally returned zero for zero segments, so...
316 if (nr_segs > UIO_MAXIOV)
318 if (nr_segs > fast_segs) {
320 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
327 if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
331 * Single unix specification:
332 * We should -EINVAL if an element length is not >= 0 and fitting an
335 * In Linux, the total length is limited to MAX_RW_COUNT, there is
336 * no overflow possibility.
340 for (seg = 0; seg < nr_segs; seg++) {
344 if (__get_user(len, &uvector->iov_len) ||
345 __get_user(buf, &uvector->iov_base)) {
349 if (len < 0) /* size_t not fitting in compat_ssize_t .. */
352 !access_ok(vrfy_dir(type), compat_ptr(buf), len)) {
356 if (len > MAX_RW_COUNT - tot_len)
357 len = MAX_RW_COUNT - tot_len;
359 iov->iov_base = compat_ptr(buf);
360 iov->iov_len = (compat_size_t) len;
370 struct compat_ncp_mount_data {
371 compat_int_t version;
372 compat_uint_t ncp_fd;
373 __compat_uid_t mounted_uid;
374 compat_pid_t wdog_pid;
375 unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
376 compat_uint_t time_out;
377 compat_uint_t retry_count;
381 compat_mode_t file_mode;
382 compat_mode_t dir_mode;
385 struct compat_ncp_mount_data_v4 {
386 compat_int_t version;
387 compat_ulong_t flags;
388 compat_ulong_t mounted_uid;
389 compat_long_t wdog_pid;
390 compat_uint_t ncp_fd;
391 compat_uint_t time_out;
392 compat_uint_t retry_count;
395 compat_ulong_t file_mode;
396 compat_ulong_t dir_mode;
399 static void *do_ncp_super_data_conv(void *raw_data)
401 int version = *(unsigned int *)raw_data;
404 struct compat_ncp_mount_data *c_n = raw_data;
405 struct ncp_mount_data *n = raw_data;
407 n->dir_mode = c_n->dir_mode;
408 n->file_mode = c_n->file_mode;
411 memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
412 n->wdog_pid = c_n->wdog_pid;
413 n->mounted_uid = c_n->mounted_uid;
414 } else if (version == 4) {
415 struct compat_ncp_mount_data_v4 *c_n = raw_data;
416 struct ncp_mount_data_v4 *n = raw_data;
418 n->dir_mode = c_n->dir_mode;
419 n->file_mode = c_n->file_mode;
422 n->retry_count = c_n->retry_count;
423 n->time_out = c_n->time_out;
424 n->ncp_fd = c_n->ncp_fd;
425 n->wdog_pid = c_n->wdog_pid;
426 n->mounted_uid = c_n->mounted_uid;
427 n->flags = c_n->flags;
428 } else if (version != 5) {
436 struct compat_nfs_string {
441 static inline void compat_nfs_string(struct nfs_string *dst,
442 struct compat_nfs_string *src)
444 dst->data = compat_ptr(src->data);
448 struct compat_nfs4_mount_data_v1 {
449 compat_int_t version;
454 compat_int_t retrans;
455 compat_int_t acregmin;
456 compat_int_t acregmax;
457 compat_int_t acdirmin;
458 compat_int_t acdirmax;
459 struct compat_nfs_string client_addr;
460 struct compat_nfs_string mnt_path;
461 struct compat_nfs_string hostname;
462 compat_uint_t host_addrlen;
463 compat_uptr_t host_addr;
465 compat_int_t auth_flavourlen;
466 compat_uptr_t auth_flavours;
469 static int do_nfs4_super_data_conv(void *raw_data)
471 int version = *(compat_uint_t *) raw_data;
474 struct compat_nfs4_mount_data_v1 *raw = raw_data;
475 struct nfs4_mount_data *real = raw_data;
477 /* copy the fields backwards */
478 real->auth_flavours = compat_ptr(raw->auth_flavours);
479 real->auth_flavourlen = raw->auth_flavourlen;
480 real->proto = raw->proto;
481 real->host_addr = compat_ptr(raw->host_addr);
482 real->host_addrlen = raw->host_addrlen;
483 compat_nfs_string(&real->hostname, &raw->hostname);
484 compat_nfs_string(&real->mnt_path, &raw->mnt_path);
485 compat_nfs_string(&real->client_addr, &raw->client_addr);
486 real->acdirmax = raw->acdirmax;
487 real->acdirmin = raw->acdirmin;
488 real->acregmax = raw->acregmax;
489 real->acregmin = raw->acregmin;
490 real->retrans = raw->retrans;
491 real->timeo = raw->timeo;
492 real->wsize = raw->wsize;
493 real->rsize = raw->rsize;
494 real->flags = raw->flags;
495 real->version = raw->version;
501 #define NCPFS_NAME "ncpfs"
502 #define NFS4_NAME "nfs4"
504 COMPAT_SYSCALL_DEFINE5(mount, const char __user *, dev_name,
505 const char __user *, dir_name,
506 const char __user *, type, compat_ulong_t, flags,
507 const void __user *, data)
514 kernel_type = copy_mount_string(type);
515 retval = PTR_ERR(kernel_type);
516 if (IS_ERR(kernel_type))
519 kernel_dev = copy_mount_string(dev_name);
520 retval = PTR_ERR(kernel_dev);
521 if (IS_ERR(kernel_dev))
524 options = copy_mount_options(data);
525 retval = PTR_ERR(options);
529 if (kernel_type && options) {
530 if (!strcmp(kernel_type, NCPFS_NAME)) {
531 do_ncp_super_data_conv(options);
532 } else if (!strcmp(kernel_type, NFS4_NAME)) {
534 if (do_nfs4_super_data_conv(options))
539 retval = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
551 struct compat_old_linux_dirent {
552 compat_ulong_t d_ino;
553 compat_ulong_t d_offset;
554 unsigned short d_namlen;
558 struct compat_readdir_callback {
559 struct dir_context ctx;
560 struct compat_old_linux_dirent __user *dirent;
564 static int compat_fillonedir(struct dir_context *ctx, const char *name,
565 int namlen, loff_t offset, u64 ino,
568 struct compat_readdir_callback *buf =
569 container_of(ctx, struct compat_readdir_callback, ctx);
570 struct compat_old_linux_dirent __user *dirent;
571 compat_ulong_t d_ino;
576 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
577 buf->result = -EOVERFLOW;
581 dirent = buf->dirent;
582 if (!access_ok(VERIFY_WRITE, dirent,
583 (unsigned long)(dirent->d_name + namlen + 1) -
584 (unsigned long)dirent))
586 if ( __put_user(d_ino, &dirent->d_ino) ||
587 __put_user(offset, &dirent->d_offset) ||
588 __put_user(namlen, &dirent->d_namlen) ||
589 __copy_to_user(dirent->d_name, name, namlen) ||
590 __put_user(0, dirent->d_name + namlen))
594 buf->result = -EFAULT;
598 COMPAT_SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
599 struct compat_old_linux_dirent __user *, dirent, unsigned int, count)
602 struct fd f = fdget_pos(fd);
603 struct compat_readdir_callback buf = {
604 .ctx.actor = compat_fillonedir,
611 error = iterate_dir(f.file, &buf.ctx);
619 struct compat_linux_dirent {
620 compat_ulong_t d_ino;
621 compat_ulong_t d_off;
622 unsigned short d_reclen;
626 struct compat_getdents_callback {
627 struct dir_context ctx;
628 struct compat_linux_dirent __user *current_dir;
629 struct compat_linux_dirent __user *previous;
634 static int compat_filldir(struct dir_context *ctx, const char *name, int namlen,
635 loff_t offset, u64 ino, unsigned int d_type)
637 struct compat_linux_dirent __user * dirent;
638 struct compat_getdents_callback *buf =
639 container_of(ctx, struct compat_getdents_callback, ctx);
640 compat_ulong_t d_ino;
641 int reclen = ALIGN(offsetof(struct compat_linux_dirent, d_name) +
642 namlen + 2, sizeof(compat_long_t));
644 buf->error = -EINVAL; /* only used if we fail.. */
645 if (reclen > buf->count)
648 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
649 buf->error = -EOVERFLOW;
652 dirent = buf->previous;
654 if (signal_pending(current))
656 if (__put_user(offset, &dirent->d_off))
659 dirent = buf->current_dir;
660 if (__put_user(d_ino, &dirent->d_ino))
662 if (__put_user(reclen, &dirent->d_reclen))
664 if (copy_to_user(dirent->d_name, name, namlen))
666 if (__put_user(0, dirent->d_name + namlen))
668 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
670 buf->previous = dirent;
671 dirent = (void __user *)dirent + reclen;
672 buf->current_dir = dirent;
673 buf->count -= reclen;
676 buf->error = -EFAULT;
680 COMPAT_SYSCALL_DEFINE3(getdents, unsigned int, fd,
681 struct compat_linux_dirent __user *, dirent, unsigned int, count)
684 struct compat_linux_dirent __user * lastdirent;
685 struct compat_getdents_callback buf = {
686 .ctx.actor = compat_filldir,
687 .current_dir = dirent,
692 if (!access_ok(VERIFY_WRITE, dirent, count))
699 error = iterate_dir(f.file, &buf.ctx);
702 lastdirent = buf.previous;
704 if (put_user(buf.ctx.pos, &lastdirent->d_off))
707 error = count - buf.count;
714 * Exactly like fs/open.c:sys_open(), except that it doesn't set the
717 COMPAT_SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
719 return do_sys_open(AT_FDCWD, filename, flags, mode);
723 * Exactly like fs/open.c:sys_openat(), except that it doesn't set the
726 COMPAT_SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, umode_t, mode)
728 return do_sys_open(dfd, filename, flags, mode);
731 #ifdef CONFIG_FHANDLE
733 * Exactly like fs/open.c:sys_open_by_handle_at(), except that it
734 * doesn't set the O_LARGEFILE flag.
736 COMPAT_SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd,
737 struct file_handle __user *, handle, int, flags)
739 return do_handle_open(mountdirfd, handle, flags);