Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/compat.c |
| 3 | * |
| 4 | * Kernel compatibililty routines for e.g. 32 bit syscall support |
| 5 | * on 64 bit kernels. |
| 6 | * |
| 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@suse.cz) |
| 12 | * |
| 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. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/linkage.h> |
| 19 | #include <linux/compat.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/time.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/fcntl.h> |
| 24 | #include <linux/namei.h> |
| 25 | #include <linux/file.h> |
| 26 | #include <linux/vfs.h> |
| 27 | #include <linux/ioctl32.h> |
| 28 | #include <linux/ioctl.h> |
| 29 | #include <linux/init.h> |
| 30 | #include <linux/sockios.h> /* for SIOCDEVPRIVATE */ |
| 31 | #include <linux/smb.h> |
| 32 | #include <linux/smb_mount.h> |
| 33 | #include <linux/ncp_mount.h> |
David Howells | 9a9947b | 2005-04-18 10:54:51 -0700 | [diff] [blame] | 34 | #include <linux/nfs4_mount.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/smp_lock.h> |
| 36 | #include <linux/syscalls.h> |
| 37 | #include <linux/ctype.h> |
| 38 | #include <linux/module.h> |
| 39 | #include <linux/dirent.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 40 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/highuid.h> |
| 42 | #include <linux/sunrpc/svc.h> |
| 43 | #include <linux/nfsd/nfsd.h> |
| 44 | #include <linux/nfsd/syscall.h> |
| 45 | #include <linux/personality.h> |
| 46 | #include <linux/rwsem.h> |
David S. Miller | 4a805e8 | 2005-09-14 21:40:00 -0700 | [diff] [blame] | 47 | #include <linux/acct.h> |
| 48 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | #include <net/sock.h> /* siocdevprivate_ioctl */ |
| 51 | |
| 52 | #include <asm/uaccess.h> |
| 53 | #include <asm/mmu_context.h> |
| 54 | #include <asm/ioctls.h> |
| 55 | |
| 56 | /* |
| 57 | * Not all architectures have sys_utime, so implement this in terms |
| 58 | * of sys_utimes. |
| 59 | */ |
| 60 | asmlinkage long compat_sys_utime(char __user *filename, struct compat_utimbuf __user *t) |
| 61 | { |
| 62 | struct timeval tv[2]; |
| 63 | |
| 64 | if (t) { |
| 65 | if (get_user(tv[0].tv_sec, &t->actime) || |
| 66 | get_user(tv[1].tv_sec, &t->modtime)) |
| 67 | return -EFAULT; |
| 68 | tv[0].tv_usec = 0; |
| 69 | tv[1].tv_usec = 0; |
| 70 | } |
| 71 | return do_utimes(filename, t ? tv : NULL); |
| 72 | } |
| 73 | |
| 74 | asmlinkage long compat_sys_utimes(char __user *filename, struct compat_timeval __user *t) |
| 75 | { |
| 76 | struct timeval tv[2]; |
| 77 | |
| 78 | if (t) { |
| 79 | if (get_user(tv[0].tv_sec, &t[0].tv_sec) || |
| 80 | get_user(tv[0].tv_usec, &t[0].tv_usec) || |
| 81 | get_user(tv[1].tv_sec, &t[1].tv_sec) || |
| 82 | get_user(tv[1].tv_usec, &t[1].tv_usec)) |
| 83 | return -EFAULT; |
| 84 | } |
| 85 | return do_utimes(filename, t ? tv : NULL); |
| 86 | } |
| 87 | |
| 88 | asmlinkage long compat_sys_newstat(char __user * filename, |
| 89 | struct compat_stat __user *statbuf) |
| 90 | { |
| 91 | struct kstat stat; |
| 92 | int error = vfs_stat(filename, &stat); |
| 93 | |
| 94 | if (!error) |
| 95 | error = cp_compat_stat(&stat, statbuf); |
| 96 | return error; |
| 97 | } |
| 98 | |
| 99 | asmlinkage long compat_sys_newlstat(char __user * filename, |
| 100 | struct compat_stat __user *statbuf) |
| 101 | { |
| 102 | struct kstat stat; |
| 103 | int error = vfs_lstat(filename, &stat); |
| 104 | |
| 105 | if (!error) |
| 106 | error = cp_compat_stat(&stat, statbuf); |
| 107 | return error; |
| 108 | } |
| 109 | |
| 110 | asmlinkage long compat_sys_newfstat(unsigned int fd, |
| 111 | struct compat_stat __user * statbuf) |
| 112 | { |
| 113 | struct kstat stat; |
| 114 | int error = vfs_fstat(fd, &stat); |
| 115 | |
| 116 | if (!error) |
| 117 | error = cp_compat_stat(&stat, statbuf); |
| 118 | return error; |
| 119 | } |
| 120 | |
| 121 | static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf) |
| 122 | { |
| 123 | |
| 124 | if (sizeof ubuf->f_blocks == 4) { |
| 125 | if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) & |
| 126 | 0xffffffff00000000ULL) |
| 127 | return -EOVERFLOW; |
| 128 | /* f_files and f_ffree may be -1; it's okay |
| 129 | * to stuff that into 32 bits */ |
| 130 | if (kbuf->f_files != 0xffffffffffffffffULL |
| 131 | && (kbuf->f_files & 0xffffffff00000000ULL)) |
| 132 | return -EOVERFLOW; |
| 133 | if (kbuf->f_ffree != 0xffffffffffffffffULL |
| 134 | && (kbuf->f_ffree & 0xffffffff00000000ULL)) |
| 135 | return -EOVERFLOW; |
| 136 | } |
| 137 | if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) || |
| 138 | __put_user(kbuf->f_type, &ubuf->f_type) || |
| 139 | __put_user(kbuf->f_bsize, &ubuf->f_bsize) || |
| 140 | __put_user(kbuf->f_blocks, &ubuf->f_blocks) || |
| 141 | __put_user(kbuf->f_bfree, &ubuf->f_bfree) || |
| 142 | __put_user(kbuf->f_bavail, &ubuf->f_bavail) || |
| 143 | __put_user(kbuf->f_files, &ubuf->f_files) || |
| 144 | __put_user(kbuf->f_ffree, &ubuf->f_ffree) || |
| 145 | __put_user(kbuf->f_namelen, &ubuf->f_namelen) || |
| 146 | __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) || |
| 147 | __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) || |
| 148 | __put_user(kbuf->f_frsize, &ubuf->f_frsize) || |
| 149 | __put_user(0, &ubuf->f_spare[0]) || |
| 150 | __put_user(0, &ubuf->f_spare[1]) || |
| 151 | __put_user(0, &ubuf->f_spare[2]) || |
| 152 | __put_user(0, &ubuf->f_spare[3]) || |
| 153 | __put_user(0, &ubuf->f_spare[4])) |
| 154 | return -EFAULT; |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * The following statfs calls are copies of code from fs/open.c and |
| 160 | * should be checked against those from time to time |
| 161 | */ |
| 162 | asmlinkage long compat_sys_statfs(const char __user *path, struct compat_statfs __user *buf) |
| 163 | { |
| 164 | struct nameidata nd; |
| 165 | int error; |
| 166 | |
| 167 | error = user_path_walk(path, &nd); |
| 168 | if (!error) { |
| 169 | struct kstatfs tmp; |
| 170 | error = vfs_statfs(nd.dentry->d_inode->i_sb, &tmp); |
David Gibson | 86e07ce | 2005-11-21 21:32:23 -0800 | [diff] [blame] | 171 | if (!error) |
| 172 | error = put_compat_statfs(buf, &tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | path_release(&nd); |
| 174 | } |
| 175 | return error; |
| 176 | } |
| 177 | |
| 178 | asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf) |
| 179 | { |
| 180 | struct file * file; |
| 181 | struct kstatfs tmp; |
| 182 | int error; |
| 183 | |
| 184 | error = -EBADF; |
| 185 | file = fget(fd); |
| 186 | if (!file) |
| 187 | goto out; |
| 188 | error = vfs_statfs(file->f_dentry->d_inode->i_sb, &tmp); |
David Gibson | 86e07ce | 2005-11-21 21:32:23 -0800 | [diff] [blame] | 189 | if (!error) |
| 190 | error = put_compat_statfs(buf, &tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | fput(file); |
| 192 | out: |
| 193 | return error; |
| 194 | } |
| 195 | |
| 196 | static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf) |
| 197 | { |
| 198 | if (sizeof ubuf->f_blocks == 4) { |
| 199 | if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) & |
| 200 | 0xffffffff00000000ULL) |
| 201 | return -EOVERFLOW; |
| 202 | /* f_files and f_ffree may be -1; it's okay |
| 203 | * to stuff that into 32 bits */ |
| 204 | if (kbuf->f_files != 0xffffffffffffffffULL |
| 205 | && (kbuf->f_files & 0xffffffff00000000ULL)) |
| 206 | return -EOVERFLOW; |
| 207 | if (kbuf->f_ffree != 0xffffffffffffffffULL |
| 208 | && (kbuf->f_ffree & 0xffffffff00000000ULL)) |
| 209 | return -EOVERFLOW; |
| 210 | } |
| 211 | if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) || |
| 212 | __put_user(kbuf->f_type, &ubuf->f_type) || |
| 213 | __put_user(kbuf->f_bsize, &ubuf->f_bsize) || |
| 214 | __put_user(kbuf->f_blocks, &ubuf->f_blocks) || |
| 215 | __put_user(kbuf->f_bfree, &ubuf->f_bfree) || |
| 216 | __put_user(kbuf->f_bavail, &ubuf->f_bavail) || |
| 217 | __put_user(kbuf->f_files, &ubuf->f_files) || |
| 218 | __put_user(kbuf->f_ffree, &ubuf->f_ffree) || |
| 219 | __put_user(kbuf->f_namelen, &ubuf->f_namelen) || |
| 220 | __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) || |
| 221 | __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) || |
| 222 | __put_user(kbuf->f_frsize, &ubuf->f_frsize)) |
| 223 | return -EFAULT; |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | asmlinkage long compat_sys_statfs64(const char __user *path, compat_size_t sz, struct compat_statfs64 __user *buf) |
| 228 | { |
| 229 | struct nameidata nd; |
| 230 | int error; |
| 231 | |
| 232 | if (sz != sizeof(*buf)) |
| 233 | return -EINVAL; |
| 234 | |
| 235 | error = user_path_walk(path, &nd); |
| 236 | if (!error) { |
| 237 | struct kstatfs tmp; |
| 238 | error = vfs_statfs(nd.dentry->d_inode->i_sb, &tmp); |
David Gibson | 86e07ce | 2005-11-21 21:32:23 -0800 | [diff] [blame] | 239 | if (!error) |
| 240 | error = put_compat_statfs64(buf, &tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | path_release(&nd); |
| 242 | } |
| 243 | return error; |
| 244 | } |
| 245 | |
| 246 | asmlinkage long compat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf) |
| 247 | { |
| 248 | struct file * file; |
| 249 | struct kstatfs tmp; |
| 250 | int error; |
| 251 | |
| 252 | if (sz != sizeof(*buf)) |
| 253 | return -EINVAL; |
| 254 | |
| 255 | error = -EBADF; |
| 256 | file = fget(fd); |
| 257 | if (!file) |
| 258 | goto out; |
| 259 | error = vfs_statfs(file->f_dentry->d_inode->i_sb, &tmp); |
David Gibson | 86e07ce | 2005-11-21 21:32:23 -0800 | [diff] [blame] | 260 | if (!error) |
| 261 | error = put_compat_statfs64(buf, &tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | fput(file); |
| 263 | out: |
| 264 | return error; |
| 265 | } |
| 266 | |
| 267 | /* ioctl32 stuff, used by sparc64, parisc, s390x, ppc64, x86_64, MIPS */ |
| 268 | |
| 269 | #define IOCTL_HASHSIZE 256 |
| 270 | static struct ioctl_trans *ioctl32_hash_table[IOCTL_HASHSIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
| 272 | extern struct ioctl_trans ioctl_start[]; |
| 273 | extern int ioctl_table_size; |
| 274 | |
| 275 | static inline unsigned long ioctl32_hash(unsigned long cmd) |
| 276 | { |
| 277 | return (((cmd >> 6) ^ (cmd >> 4) ^ cmd)) % IOCTL_HASHSIZE; |
| 278 | } |
| 279 | |
| 280 | static void ioctl32_insert_translation(struct ioctl_trans *trans) |
| 281 | { |
| 282 | unsigned long hash; |
| 283 | struct ioctl_trans *t; |
| 284 | |
| 285 | hash = ioctl32_hash (trans->cmd); |
| 286 | if (!ioctl32_hash_table[hash]) |
| 287 | ioctl32_hash_table[hash] = trans; |
| 288 | else { |
| 289 | t = ioctl32_hash_table[hash]; |
| 290 | while (t->next) |
| 291 | t = t->next; |
| 292 | trans->next = NULL; |
| 293 | t->next = trans; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | static int __init init_sys32_ioctl(void) |
| 298 | { |
| 299 | int i; |
| 300 | |
| 301 | for (i = 0; i < ioctl_table_size; i++) { |
| 302 | if (ioctl_start[i].next != 0) { |
| 303 | printk("ioctl translation %d bad\n",i); |
| 304 | return -1; |
| 305 | } |
| 306 | |
| 307 | ioctl32_insert_translation(&ioctl_start[i]); |
| 308 | } |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | __initcall(init_sys32_ioctl); |
| 313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | static void compat_ioctl_error(struct file *filp, unsigned int fd, |
| 315 | unsigned int cmd, unsigned long arg) |
| 316 | { |
| 317 | char buf[10]; |
| 318 | char *fn = "?"; |
| 319 | char *path; |
| 320 | |
| 321 | /* find the name of the device. */ |
| 322 | path = (char *)__get_free_page(GFP_KERNEL); |
| 323 | if (path) { |
| 324 | fn = d_path(filp->f_dentry, filp->f_vfsmnt, path, PAGE_SIZE); |
| 325 | if (IS_ERR(fn)) |
| 326 | fn = "?"; |
| 327 | } |
| 328 | |
| 329 | sprintf(buf,"'%c'", (cmd>>24) & 0x3f); |
| 330 | if (!isprint(buf[1])) |
| 331 | sprintf(buf, "%02x", buf[1]); |
| 332 | printk("ioctl32(%s:%d): Unknown cmd fd(%d) " |
| 333 | "cmd(%08x){%s} arg(%08x) on %s\n", |
| 334 | current->comm, current->pid, |
| 335 | (int)fd, (unsigned int)cmd, buf, |
| 336 | (unsigned int)arg, fn); |
| 337 | |
| 338 | if (path) |
| 339 | free_page((unsigned long)path); |
| 340 | } |
| 341 | |
| 342 | asmlinkage long compat_sys_ioctl(unsigned int fd, unsigned int cmd, |
| 343 | unsigned long arg) |
| 344 | { |
| 345 | struct file *filp; |
| 346 | int error = -EBADF; |
| 347 | struct ioctl_trans *t; |
| 348 | int fput_needed; |
| 349 | |
| 350 | filp = fget_light(fd, &fput_needed); |
| 351 | if (!filp) |
| 352 | goto out; |
| 353 | |
| 354 | /* RED-PEN how should LSM module know it's handling 32bit? */ |
| 355 | error = security_file_ioctl(filp, cmd, arg); |
| 356 | if (error) |
| 357 | goto out_fput; |
| 358 | |
| 359 | /* |
| 360 | * To allow the compat_ioctl handlers to be self contained |
| 361 | * we need to check the common ioctls here first. |
| 362 | * Just handle them with the standard handlers below. |
| 363 | */ |
| 364 | switch (cmd) { |
| 365 | case FIOCLEX: |
| 366 | case FIONCLEX: |
| 367 | case FIONBIO: |
| 368 | case FIOASYNC: |
| 369 | case FIOQSIZE: |
| 370 | break; |
| 371 | |
| 372 | case FIBMAP: |
| 373 | case FIGETBSZ: |
| 374 | case FIONREAD: |
| 375 | if (S_ISREG(filp->f_dentry->d_inode->i_mode)) |
| 376 | break; |
| 377 | /*FALL THROUGH*/ |
| 378 | |
| 379 | default: |
| 380 | if (filp->f_op && filp->f_op->compat_ioctl) { |
| 381 | error = filp->f_op->compat_ioctl(filp, cmd, arg); |
| 382 | if (error != -ENOIOCTLCMD) |
| 383 | goto out_fput; |
| 384 | } |
| 385 | |
| 386 | if (!filp->f_op || |
| 387 | (!filp->f_op->ioctl && !filp->f_op->unlocked_ioctl)) |
| 388 | goto do_ioctl; |
| 389 | break; |
| 390 | } |
| 391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | for (t = ioctl32_hash_table[ioctl32_hash(cmd)]; t; t = t->next) { |
| 393 | if (t->cmd == cmd) |
| 394 | goto found_handler; |
| 395 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
| 397 | if (S_ISSOCK(filp->f_dentry->d_inode->i_mode) && |
| 398 | cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) { |
| 399 | error = siocdevprivate_ioctl(fd, cmd, arg); |
| 400 | } else { |
| 401 | static int count; |
| 402 | |
| 403 | if (++count <= 50) |
| 404 | compat_ioctl_error(filp, fd, cmd, arg); |
| 405 | error = -EINVAL; |
| 406 | } |
| 407 | |
| 408 | goto out_fput; |
| 409 | |
| 410 | found_handler: |
| 411 | if (t->handler) { |
| 412 | lock_kernel(); |
| 413 | error = t->handler(fd, cmd, arg, filp); |
| 414 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | goto out_fput; |
| 416 | } |
| 417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | do_ioctl: |
| 419 | error = vfs_ioctl(filp, fd, cmd, arg); |
| 420 | out_fput: |
| 421 | fput_light(filp, fput_needed); |
| 422 | out: |
| 423 | return error; |
| 424 | } |
| 425 | |
| 426 | static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl) |
| 427 | { |
| 428 | if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) || |
| 429 | __get_user(kfl->l_type, &ufl->l_type) || |
| 430 | __get_user(kfl->l_whence, &ufl->l_whence) || |
| 431 | __get_user(kfl->l_start, &ufl->l_start) || |
| 432 | __get_user(kfl->l_len, &ufl->l_len) || |
| 433 | __get_user(kfl->l_pid, &ufl->l_pid)) |
| 434 | return -EFAULT; |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl) |
| 439 | { |
| 440 | if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) || |
| 441 | __put_user(kfl->l_type, &ufl->l_type) || |
| 442 | __put_user(kfl->l_whence, &ufl->l_whence) || |
| 443 | __put_user(kfl->l_start, &ufl->l_start) || |
| 444 | __put_user(kfl->l_len, &ufl->l_len) || |
| 445 | __put_user(kfl->l_pid, &ufl->l_pid)) |
| 446 | return -EFAULT; |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | #ifndef HAVE_ARCH_GET_COMPAT_FLOCK64 |
| 451 | static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl) |
| 452 | { |
| 453 | if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) || |
| 454 | __get_user(kfl->l_type, &ufl->l_type) || |
| 455 | __get_user(kfl->l_whence, &ufl->l_whence) || |
| 456 | __get_user(kfl->l_start, &ufl->l_start) || |
| 457 | __get_user(kfl->l_len, &ufl->l_len) || |
| 458 | __get_user(kfl->l_pid, &ufl->l_pid)) |
| 459 | return -EFAULT; |
| 460 | return 0; |
| 461 | } |
| 462 | #endif |
| 463 | |
| 464 | #ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64 |
| 465 | static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl) |
| 466 | { |
| 467 | if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) || |
| 468 | __put_user(kfl->l_type, &ufl->l_type) || |
| 469 | __put_user(kfl->l_whence, &ufl->l_whence) || |
| 470 | __put_user(kfl->l_start, &ufl->l_start) || |
| 471 | __put_user(kfl->l_len, &ufl->l_len) || |
| 472 | __put_user(kfl->l_pid, &ufl->l_pid)) |
| 473 | return -EFAULT; |
| 474 | return 0; |
| 475 | } |
| 476 | #endif |
| 477 | |
| 478 | asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd, |
| 479 | unsigned long arg) |
| 480 | { |
| 481 | mm_segment_t old_fs; |
| 482 | struct flock f; |
| 483 | long ret; |
| 484 | |
| 485 | switch (cmd) { |
| 486 | case F_GETLK: |
| 487 | case F_SETLK: |
| 488 | case F_SETLKW: |
| 489 | ret = get_compat_flock(&f, compat_ptr(arg)); |
| 490 | if (ret != 0) |
| 491 | break; |
| 492 | old_fs = get_fs(); |
| 493 | set_fs(KERNEL_DS); |
| 494 | ret = sys_fcntl(fd, cmd, (unsigned long)&f); |
| 495 | set_fs(old_fs); |
| 496 | if (cmd == F_GETLK && ret == 0) { |
| 497 | if ((f.l_start >= COMPAT_OFF_T_MAX) || |
| 498 | ((f.l_start + f.l_len) > COMPAT_OFF_T_MAX)) |
| 499 | ret = -EOVERFLOW; |
| 500 | if (ret == 0) |
| 501 | ret = put_compat_flock(&f, compat_ptr(arg)); |
| 502 | } |
| 503 | break; |
| 504 | |
| 505 | case F_GETLK64: |
| 506 | case F_SETLK64: |
| 507 | case F_SETLKW64: |
| 508 | ret = get_compat_flock64(&f, compat_ptr(arg)); |
| 509 | if (ret != 0) |
| 510 | break; |
| 511 | old_fs = get_fs(); |
| 512 | set_fs(KERNEL_DS); |
| 513 | ret = sys_fcntl(fd, (cmd == F_GETLK64) ? F_GETLK : |
| 514 | ((cmd == F_SETLK64) ? F_SETLK : F_SETLKW), |
| 515 | (unsigned long)&f); |
| 516 | set_fs(old_fs); |
| 517 | if (cmd == F_GETLK64 && ret == 0) { |
| 518 | if ((f.l_start >= COMPAT_LOFF_T_MAX) || |
| 519 | ((f.l_start + f.l_len) > COMPAT_LOFF_T_MAX)) |
| 520 | ret = -EOVERFLOW; |
| 521 | if (ret == 0) |
| 522 | ret = put_compat_flock64(&f, compat_ptr(arg)); |
| 523 | } |
| 524 | break; |
| 525 | |
| 526 | default: |
| 527 | ret = sys_fcntl(fd, cmd, arg); |
| 528 | break; |
| 529 | } |
| 530 | return ret; |
| 531 | } |
| 532 | |
| 533 | asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd, |
| 534 | unsigned long arg) |
| 535 | { |
| 536 | if ((cmd == F_GETLK64) || (cmd == F_SETLK64) || (cmd == F_SETLKW64)) |
| 537 | return -EINVAL; |
| 538 | return compat_sys_fcntl64(fd, cmd, arg); |
| 539 | } |
| 540 | |
| 541 | asmlinkage long |
| 542 | compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p) |
| 543 | { |
| 544 | long ret; |
| 545 | aio_context_t ctx64; |
| 546 | |
| 547 | mm_segment_t oldfs = get_fs(); |
| 548 | if (unlikely(get_user(ctx64, ctx32p))) |
| 549 | return -EFAULT; |
| 550 | |
| 551 | set_fs(KERNEL_DS); |
| 552 | /* The __user pointer cast is valid because of the set_fs() */ |
| 553 | ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64); |
| 554 | set_fs(oldfs); |
| 555 | /* truncating is ok because it's a user address */ |
| 556 | if (!ret) |
| 557 | ret = put_user((u32) ctx64, ctx32p); |
| 558 | return ret; |
| 559 | } |
| 560 | |
| 561 | asmlinkage long |
| 562 | compat_sys_io_getevents(aio_context_t ctx_id, |
| 563 | unsigned long min_nr, |
| 564 | unsigned long nr, |
| 565 | struct io_event __user *events, |
| 566 | struct compat_timespec __user *timeout) |
| 567 | { |
| 568 | long ret; |
| 569 | struct timespec t; |
| 570 | struct timespec __user *ut = NULL; |
| 571 | |
| 572 | ret = -EFAULT; |
| 573 | if (unlikely(!access_ok(VERIFY_WRITE, events, |
| 574 | nr * sizeof(struct io_event)))) |
| 575 | goto out; |
| 576 | if (timeout) { |
| 577 | if (get_compat_timespec(&t, timeout)) |
| 578 | goto out; |
| 579 | |
| 580 | ut = compat_alloc_user_space(sizeof(*ut)); |
| 581 | if (copy_to_user(ut, &t, sizeof(t)) ) |
| 582 | goto out; |
| 583 | } |
| 584 | ret = sys_io_getevents(ctx_id, min_nr, nr, events, ut); |
| 585 | out: |
| 586 | return ret; |
| 587 | } |
| 588 | |
| 589 | static inline long |
| 590 | copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64) |
| 591 | { |
| 592 | compat_uptr_t uptr; |
| 593 | int i; |
| 594 | |
| 595 | for (i = 0; i < nr; ++i) { |
| 596 | if (get_user(uptr, ptr32 + i)) |
| 597 | return -EFAULT; |
| 598 | if (put_user(compat_ptr(uptr), ptr64 + i)) |
| 599 | return -EFAULT; |
| 600 | } |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | #define MAX_AIO_SUBMITS (PAGE_SIZE/sizeof(struct iocb *)) |
| 605 | |
| 606 | asmlinkage long |
| 607 | compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb) |
| 608 | { |
| 609 | struct iocb __user * __user *iocb64; |
| 610 | long ret; |
| 611 | |
| 612 | if (unlikely(nr < 0)) |
| 613 | return -EINVAL; |
| 614 | |
| 615 | if (nr > MAX_AIO_SUBMITS) |
| 616 | nr = MAX_AIO_SUBMITS; |
| 617 | |
| 618 | iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64)); |
| 619 | ret = copy_iocb(nr, iocb, iocb64); |
| 620 | if (!ret) |
| 621 | ret = sys_io_submit(ctx_id, nr, iocb64); |
| 622 | return ret; |
| 623 | } |
| 624 | |
| 625 | struct compat_ncp_mount_data { |
| 626 | compat_int_t version; |
| 627 | compat_uint_t ncp_fd; |
Stephen Rothwell | 202e597 | 2005-09-06 15:16:40 -0700 | [diff] [blame] | 628 | __compat_uid_t mounted_uid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | compat_pid_t wdog_pid; |
| 630 | unsigned char mounted_vol[NCP_VOLNAME_LEN + 1]; |
| 631 | compat_uint_t time_out; |
| 632 | compat_uint_t retry_count; |
| 633 | compat_uint_t flags; |
Stephen Rothwell | 202e597 | 2005-09-06 15:16:40 -0700 | [diff] [blame] | 634 | __compat_uid_t uid; |
| 635 | __compat_gid_t gid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | compat_mode_t file_mode; |
| 637 | compat_mode_t dir_mode; |
| 638 | }; |
| 639 | |
| 640 | struct compat_ncp_mount_data_v4 { |
| 641 | compat_int_t version; |
| 642 | compat_ulong_t flags; |
| 643 | compat_ulong_t mounted_uid; |
| 644 | compat_long_t wdog_pid; |
| 645 | compat_uint_t ncp_fd; |
| 646 | compat_uint_t time_out; |
| 647 | compat_uint_t retry_count; |
| 648 | compat_ulong_t uid; |
| 649 | compat_ulong_t gid; |
| 650 | compat_ulong_t file_mode; |
| 651 | compat_ulong_t dir_mode; |
| 652 | }; |
| 653 | |
| 654 | static void *do_ncp_super_data_conv(void *raw_data) |
| 655 | { |
| 656 | int version = *(unsigned int *)raw_data; |
| 657 | |
| 658 | if (version == 3) { |
| 659 | struct compat_ncp_mount_data *c_n = raw_data; |
| 660 | struct ncp_mount_data *n = raw_data; |
| 661 | |
| 662 | n->dir_mode = c_n->dir_mode; |
| 663 | n->file_mode = c_n->file_mode; |
| 664 | n->gid = c_n->gid; |
| 665 | n->uid = c_n->uid; |
| 666 | memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int))); |
| 667 | n->wdog_pid = c_n->wdog_pid; |
| 668 | n->mounted_uid = c_n->mounted_uid; |
| 669 | } else if (version == 4) { |
| 670 | struct compat_ncp_mount_data_v4 *c_n = raw_data; |
| 671 | struct ncp_mount_data_v4 *n = raw_data; |
| 672 | |
| 673 | n->dir_mode = c_n->dir_mode; |
| 674 | n->file_mode = c_n->file_mode; |
| 675 | n->gid = c_n->gid; |
| 676 | n->uid = c_n->uid; |
| 677 | n->retry_count = c_n->retry_count; |
| 678 | n->time_out = c_n->time_out; |
| 679 | n->ncp_fd = c_n->ncp_fd; |
| 680 | n->wdog_pid = c_n->wdog_pid; |
| 681 | n->mounted_uid = c_n->mounted_uid; |
| 682 | n->flags = c_n->flags; |
| 683 | } else if (version != 5) { |
| 684 | return NULL; |
| 685 | } |
| 686 | |
| 687 | return raw_data; |
| 688 | } |
| 689 | |
| 690 | struct compat_smb_mount_data { |
| 691 | compat_int_t version; |
Stephen Rothwell | 202e597 | 2005-09-06 15:16:40 -0700 | [diff] [blame] | 692 | __compat_uid_t mounted_uid; |
| 693 | __compat_uid_t uid; |
| 694 | __compat_gid_t gid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | compat_mode_t file_mode; |
| 696 | compat_mode_t dir_mode; |
| 697 | }; |
| 698 | |
| 699 | static void *do_smb_super_data_conv(void *raw_data) |
| 700 | { |
| 701 | struct smb_mount_data *s = raw_data; |
| 702 | struct compat_smb_mount_data *c_s = raw_data; |
| 703 | |
| 704 | if (c_s->version != SMB_MOUNT_OLDVERSION) |
| 705 | goto out; |
| 706 | s->dir_mode = c_s->dir_mode; |
| 707 | s->file_mode = c_s->file_mode; |
| 708 | s->gid = c_s->gid; |
| 709 | s->uid = c_s->uid; |
| 710 | s->mounted_uid = c_s->mounted_uid; |
| 711 | out: |
| 712 | return raw_data; |
| 713 | } |
| 714 | |
David Howells | 9a9947b | 2005-04-18 10:54:51 -0700 | [diff] [blame] | 715 | struct compat_nfs_string { |
| 716 | compat_uint_t len; |
David Howells | 5fc3e62 | 2005-04-27 15:39:03 -0700 | [diff] [blame] | 717 | compat_uptr_t data; |
David Howells | 9a9947b | 2005-04-18 10:54:51 -0700 | [diff] [blame] | 718 | }; |
| 719 | |
| 720 | static inline void compat_nfs_string(struct nfs_string *dst, |
| 721 | struct compat_nfs_string *src) |
| 722 | { |
| 723 | dst->data = compat_ptr(src->data); |
| 724 | dst->len = src->len; |
| 725 | } |
| 726 | |
| 727 | struct compat_nfs4_mount_data_v1 { |
| 728 | compat_int_t version; |
| 729 | compat_int_t flags; |
| 730 | compat_int_t rsize; |
| 731 | compat_int_t wsize; |
| 732 | compat_int_t timeo; |
| 733 | compat_int_t retrans; |
| 734 | compat_int_t acregmin; |
| 735 | compat_int_t acregmax; |
| 736 | compat_int_t acdirmin; |
| 737 | compat_int_t acdirmax; |
| 738 | struct compat_nfs_string client_addr; |
| 739 | struct compat_nfs_string mnt_path; |
| 740 | struct compat_nfs_string hostname; |
| 741 | compat_uint_t host_addrlen; |
David Howells | 5fc3e62 | 2005-04-27 15:39:03 -0700 | [diff] [blame] | 742 | compat_uptr_t host_addr; |
David Howells | 9a9947b | 2005-04-18 10:54:51 -0700 | [diff] [blame] | 743 | compat_int_t proto; |
| 744 | compat_int_t auth_flavourlen; |
David Howells | 5fc3e62 | 2005-04-27 15:39:03 -0700 | [diff] [blame] | 745 | compat_uptr_t auth_flavours; |
David Howells | 9a9947b | 2005-04-18 10:54:51 -0700 | [diff] [blame] | 746 | }; |
| 747 | |
| 748 | static int do_nfs4_super_data_conv(void *raw_data) |
| 749 | { |
| 750 | int version = *(compat_uint_t *) raw_data; |
| 751 | |
| 752 | if (version == 1) { |
| 753 | struct compat_nfs4_mount_data_v1 *raw = raw_data; |
| 754 | struct nfs4_mount_data *real = raw_data; |
| 755 | |
| 756 | /* copy the fields backwards */ |
| 757 | real->auth_flavours = compat_ptr(raw->auth_flavours); |
| 758 | real->auth_flavourlen = raw->auth_flavourlen; |
| 759 | real->proto = raw->proto; |
| 760 | real->host_addr = compat_ptr(raw->host_addr); |
| 761 | real->host_addrlen = raw->host_addrlen; |
| 762 | compat_nfs_string(&real->hostname, &raw->hostname); |
| 763 | compat_nfs_string(&real->mnt_path, &raw->mnt_path); |
| 764 | compat_nfs_string(&real->client_addr, &raw->client_addr); |
| 765 | real->acdirmax = raw->acdirmax; |
| 766 | real->acdirmin = raw->acdirmin; |
| 767 | real->acregmax = raw->acregmax; |
| 768 | real->acregmin = raw->acregmin; |
| 769 | real->retrans = raw->retrans; |
| 770 | real->timeo = raw->timeo; |
| 771 | real->wsize = raw->wsize; |
| 772 | real->rsize = raw->rsize; |
| 773 | real->flags = raw->flags; |
| 774 | real->version = raw->version; |
| 775 | } |
| 776 | else { |
| 777 | return -EINVAL; |
| 778 | } |
| 779 | |
| 780 | return 0; |
| 781 | } |
| 782 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | extern int copy_mount_options (const void __user *, unsigned long *); |
| 784 | |
| 785 | #define SMBFS_NAME "smbfs" |
| 786 | #define NCPFS_NAME "ncpfs" |
David Howells | 9a9947b | 2005-04-18 10:54:51 -0700 | [diff] [blame] | 787 | #define NFS4_NAME "nfs4" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | |
| 789 | asmlinkage long compat_sys_mount(char __user * dev_name, char __user * dir_name, |
| 790 | char __user * type, unsigned long flags, |
| 791 | void __user * data) |
| 792 | { |
| 793 | unsigned long type_page; |
| 794 | unsigned long data_page; |
| 795 | unsigned long dev_page; |
| 796 | char *dir_page; |
| 797 | int retval; |
| 798 | |
| 799 | retval = copy_mount_options (type, &type_page); |
| 800 | if (retval < 0) |
| 801 | goto out; |
| 802 | |
| 803 | dir_page = getname(dir_name); |
| 804 | retval = PTR_ERR(dir_page); |
| 805 | if (IS_ERR(dir_page)) |
| 806 | goto out1; |
| 807 | |
| 808 | retval = copy_mount_options (dev_name, &dev_page); |
| 809 | if (retval < 0) |
| 810 | goto out2; |
| 811 | |
| 812 | retval = copy_mount_options (data, &data_page); |
| 813 | if (retval < 0) |
| 814 | goto out3; |
| 815 | |
| 816 | retval = -EINVAL; |
| 817 | |
| 818 | if (type_page) { |
| 819 | if (!strcmp((char *)type_page, SMBFS_NAME)) { |
| 820 | do_smb_super_data_conv((void *)data_page); |
| 821 | } else if (!strcmp((char *)type_page, NCPFS_NAME)) { |
| 822 | do_ncp_super_data_conv((void *)data_page); |
David Howells | 9a9947b | 2005-04-18 10:54:51 -0700 | [diff] [blame] | 823 | } else if (!strcmp((char *)type_page, NFS4_NAME)) { |
| 824 | if (do_nfs4_super_data_conv((void *) data_page)) |
| 825 | goto out4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | |
| 829 | lock_kernel(); |
| 830 | retval = do_mount((char*)dev_page, dir_page, (char*)type_page, |
| 831 | flags, (void*)data_page); |
| 832 | unlock_kernel(); |
| 833 | |
David Howells | 9a9947b | 2005-04-18 10:54:51 -0700 | [diff] [blame] | 834 | out4: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | free_page(data_page); |
| 836 | out3: |
| 837 | free_page(dev_page); |
| 838 | out2: |
| 839 | putname(dir_page); |
| 840 | out1: |
| 841 | free_page(type_page); |
| 842 | out: |
| 843 | return retval; |
| 844 | } |
| 845 | |
| 846 | #define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de))) |
| 847 | #define COMPAT_ROUND_UP(x) (((x)+sizeof(compat_long_t)-1) & \ |
| 848 | ~(sizeof(compat_long_t)-1)) |
| 849 | |
| 850 | struct compat_old_linux_dirent { |
| 851 | compat_ulong_t d_ino; |
| 852 | compat_ulong_t d_offset; |
| 853 | unsigned short d_namlen; |
| 854 | char d_name[1]; |
| 855 | }; |
| 856 | |
| 857 | struct compat_readdir_callback { |
| 858 | struct compat_old_linux_dirent __user *dirent; |
| 859 | int result; |
| 860 | }; |
| 861 | |
| 862 | static int compat_fillonedir(void *__buf, const char *name, int namlen, |
| 863 | loff_t offset, ino_t ino, unsigned int d_type) |
| 864 | { |
| 865 | struct compat_readdir_callback *buf = __buf; |
| 866 | struct compat_old_linux_dirent __user *dirent; |
| 867 | |
| 868 | if (buf->result) |
| 869 | return -EINVAL; |
| 870 | buf->result++; |
| 871 | dirent = buf->dirent; |
| 872 | if (!access_ok(VERIFY_WRITE, dirent, |
| 873 | (unsigned long)(dirent->d_name + namlen + 1) - |
| 874 | (unsigned long)dirent)) |
| 875 | goto efault; |
| 876 | if ( __put_user(ino, &dirent->d_ino) || |
| 877 | __put_user(offset, &dirent->d_offset) || |
| 878 | __put_user(namlen, &dirent->d_namlen) || |
| 879 | __copy_to_user(dirent->d_name, name, namlen) || |
| 880 | __put_user(0, dirent->d_name + namlen)) |
| 881 | goto efault; |
| 882 | return 0; |
| 883 | efault: |
| 884 | buf->result = -EFAULT; |
| 885 | return -EFAULT; |
| 886 | } |
| 887 | |
| 888 | asmlinkage long compat_sys_old_readdir(unsigned int fd, |
| 889 | struct compat_old_linux_dirent __user *dirent, unsigned int count) |
| 890 | { |
| 891 | int error; |
| 892 | struct file *file; |
| 893 | struct compat_readdir_callback buf; |
| 894 | |
| 895 | error = -EBADF; |
| 896 | file = fget(fd); |
| 897 | if (!file) |
| 898 | goto out; |
| 899 | |
| 900 | buf.result = 0; |
| 901 | buf.dirent = dirent; |
| 902 | |
| 903 | error = vfs_readdir(file, compat_fillonedir, &buf); |
| 904 | if (error >= 0) |
| 905 | error = buf.result; |
| 906 | |
| 907 | fput(file); |
| 908 | out: |
| 909 | return error; |
| 910 | } |
| 911 | |
| 912 | struct compat_linux_dirent { |
| 913 | compat_ulong_t d_ino; |
| 914 | compat_ulong_t d_off; |
| 915 | unsigned short d_reclen; |
| 916 | char d_name[1]; |
| 917 | }; |
| 918 | |
| 919 | struct compat_getdents_callback { |
| 920 | struct compat_linux_dirent __user *current_dir; |
| 921 | struct compat_linux_dirent __user *previous; |
| 922 | int count; |
| 923 | int error; |
| 924 | }; |
| 925 | |
| 926 | static int compat_filldir(void *__buf, const char *name, int namlen, |
| 927 | loff_t offset, ino_t ino, unsigned int d_type) |
| 928 | { |
| 929 | struct compat_linux_dirent __user * dirent; |
| 930 | struct compat_getdents_callback *buf = __buf; |
| 931 | int reclen = COMPAT_ROUND_UP(NAME_OFFSET(dirent) + namlen + 2); |
| 932 | |
| 933 | buf->error = -EINVAL; /* only used if we fail.. */ |
| 934 | if (reclen > buf->count) |
| 935 | return -EINVAL; |
| 936 | dirent = buf->previous; |
| 937 | if (dirent) { |
| 938 | if (__put_user(offset, &dirent->d_off)) |
| 939 | goto efault; |
| 940 | } |
| 941 | dirent = buf->current_dir; |
| 942 | if (__put_user(ino, &dirent->d_ino)) |
| 943 | goto efault; |
| 944 | if (__put_user(reclen, &dirent->d_reclen)) |
| 945 | goto efault; |
| 946 | if (copy_to_user(dirent->d_name, name, namlen)) |
| 947 | goto efault; |
| 948 | if (__put_user(0, dirent->d_name + namlen)) |
| 949 | goto efault; |
| 950 | if (__put_user(d_type, (char __user *) dirent + reclen - 1)) |
| 951 | goto efault; |
| 952 | buf->previous = dirent; |
| 953 | dirent = (void __user *)dirent + reclen; |
| 954 | buf->current_dir = dirent; |
| 955 | buf->count -= reclen; |
| 956 | return 0; |
| 957 | efault: |
| 958 | buf->error = -EFAULT; |
| 959 | return -EFAULT; |
| 960 | } |
| 961 | |
| 962 | asmlinkage long compat_sys_getdents(unsigned int fd, |
| 963 | struct compat_linux_dirent __user *dirent, unsigned int count) |
| 964 | { |
| 965 | struct file * file; |
| 966 | struct compat_linux_dirent __user * lastdirent; |
| 967 | struct compat_getdents_callback buf; |
| 968 | int error; |
| 969 | |
| 970 | error = -EFAULT; |
| 971 | if (!access_ok(VERIFY_WRITE, dirent, count)) |
| 972 | goto out; |
| 973 | |
| 974 | error = -EBADF; |
| 975 | file = fget(fd); |
| 976 | if (!file) |
| 977 | goto out; |
| 978 | |
| 979 | buf.current_dir = dirent; |
| 980 | buf.previous = NULL; |
| 981 | buf.count = count; |
| 982 | buf.error = 0; |
| 983 | |
| 984 | error = vfs_readdir(file, compat_filldir, &buf); |
| 985 | if (error < 0) |
| 986 | goto out_putf; |
| 987 | error = buf.error; |
| 988 | lastdirent = buf.previous; |
| 989 | if (lastdirent) { |
| 990 | if (put_user(file->f_pos, &lastdirent->d_off)) |
| 991 | error = -EFAULT; |
| 992 | else |
| 993 | error = count - buf.count; |
| 994 | } |
| 995 | |
| 996 | out_putf: |
| 997 | fput(file); |
| 998 | out: |
| 999 | return error; |
| 1000 | } |
| 1001 | |
| 1002 | #ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64 |
| 1003 | #define COMPAT_ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1)) |
| 1004 | |
| 1005 | struct compat_getdents_callback64 { |
| 1006 | struct linux_dirent64 __user *current_dir; |
| 1007 | struct linux_dirent64 __user *previous; |
| 1008 | int count; |
| 1009 | int error; |
| 1010 | }; |
| 1011 | |
| 1012 | static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset, |
| 1013 | ino_t ino, unsigned int d_type) |
| 1014 | { |
| 1015 | struct linux_dirent64 __user *dirent; |
| 1016 | struct compat_getdents_callback64 *buf = __buf; |
| 1017 | int jj = NAME_OFFSET(dirent); |
| 1018 | int reclen = COMPAT_ROUND_UP64(jj + namlen + 1); |
| 1019 | u64 off; |
| 1020 | |
| 1021 | buf->error = -EINVAL; /* only used if we fail.. */ |
| 1022 | if (reclen > buf->count) |
| 1023 | return -EINVAL; |
| 1024 | dirent = buf->previous; |
| 1025 | |
| 1026 | if (dirent) { |
| 1027 | if (__put_user_unaligned(offset, &dirent->d_off)) |
| 1028 | goto efault; |
| 1029 | } |
| 1030 | dirent = buf->current_dir; |
| 1031 | if (__put_user_unaligned(ino, &dirent->d_ino)) |
| 1032 | goto efault; |
| 1033 | off = 0; |
| 1034 | if (__put_user_unaligned(off, &dirent->d_off)) |
| 1035 | goto efault; |
| 1036 | if (__put_user(reclen, &dirent->d_reclen)) |
| 1037 | goto efault; |
| 1038 | if (__put_user(d_type, &dirent->d_type)) |
| 1039 | goto efault; |
| 1040 | if (copy_to_user(dirent->d_name, name, namlen)) |
| 1041 | goto efault; |
| 1042 | if (__put_user(0, dirent->d_name + namlen)) |
| 1043 | goto efault; |
| 1044 | buf->previous = dirent; |
| 1045 | dirent = (void __user *)dirent + reclen; |
| 1046 | buf->current_dir = dirent; |
| 1047 | buf->count -= reclen; |
| 1048 | return 0; |
| 1049 | efault: |
| 1050 | buf->error = -EFAULT; |
| 1051 | return -EFAULT; |
| 1052 | } |
| 1053 | |
| 1054 | asmlinkage long compat_sys_getdents64(unsigned int fd, |
| 1055 | struct linux_dirent64 __user * dirent, unsigned int count) |
| 1056 | { |
| 1057 | struct file * file; |
| 1058 | struct linux_dirent64 __user * lastdirent; |
| 1059 | struct compat_getdents_callback64 buf; |
| 1060 | int error; |
| 1061 | |
| 1062 | error = -EFAULT; |
| 1063 | if (!access_ok(VERIFY_WRITE, dirent, count)) |
| 1064 | goto out; |
| 1065 | |
| 1066 | error = -EBADF; |
| 1067 | file = fget(fd); |
| 1068 | if (!file) |
| 1069 | goto out; |
| 1070 | |
| 1071 | buf.current_dir = dirent; |
| 1072 | buf.previous = NULL; |
| 1073 | buf.count = count; |
| 1074 | buf.error = 0; |
| 1075 | |
| 1076 | error = vfs_readdir(file, compat_filldir64, &buf); |
| 1077 | if (error < 0) |
| 1078 | goto out_putf; |
| 1079 | error = buf.error; |
| 1080 | lastdirent = buf.previous; |
| 1081 | if (lastdirent) { |
| 1082 | typeof(lastdirent->d_off) d_off = file->f_pos; |
| 1083 | __put_user_unaligned(d_off, &lastdirent->d_off); |
| 1084 | error = count - buf.count; |
| 1085 | } |
| 1086 | |
| 1087 | out_putf: |
| 1088 | fput(file); |
| 1089 | out: |
| 1090 | return error; |
| 1091 | } |
| 1092 | #endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */ |
| 1093 | |
| 1094 | static ssize_t compat_do_readv_writev(int type, struct file *file, |
| 1095 | const struct compat_iovec __user *uvector, |
| 1096 | unsigned long nr_segs, loff_t *pos) |
| 1097 | { |
| 1098 | typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *); |
| 1099 | typedef ssize_t (*iov_fn_t)(struct file *, const struct iovec *, unsigned long, loff_t *); |
| 1100 | |
| 1101 | compat_ssize_t tot_len; |
| 1102 | struct iovec iovstack[UIO_FASTIOV]; |
| 1103 | struct iovec *iov=iovstack, *vector; |
| 1104 | ssize_t ret; |
| 1105 | int seg; |
| 1106 | io_fn_t fn; |
| 1107 | iov_fn_t fnv; |
| 1108 | |
| 1109 | /* |
| 1110 | * SuS says "The readv() function *may* fail if the iovcnt argument |
| 1111 | * was less than or equal to 0, or greater than {IOV_MAX}. Linux has |
| 1112 | * traditionally returned zero for zero segments, so... |
| 1113 | */ |
| 1114 | ret = 0; |
| 1115 | if (nr_segs == 0) |
| 1116 | goto out; |
| 1117 | |
| 1118 | /* |
| 1119 | * First get the "struct iovec" from user memory and |
| 1120 | * verify all the pointers |
| 1121 | */ |
| 1122 | ret = -EINVAL; |
| 1123 | if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0)) |
| 1124 | goto out; |
| 1125 | if (!file->f_op) |
| 1126 | goto out; |
| 1127 | if (nr_segs > UIO_FASTIOV) { |
| 1128 | ret = -ENOMEM; |
| 1129 | iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL); |
| 1130 | if (!iov) |
| 1131 | goto out; |
| 1132 | } |
| 1133 | ret = -EFAULT; |
| 1134 | if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector))) |
| 1135 | goto out; |
| 1136 | |
| 1137 | /* |
| 1138 | * Single unix specification: |
| 1139 | * We should -EINVAL if an element length is not >= 0 and fitting an |
| 1140 | * ssize_t. The total length is fitting an ssize_t |
| 1141 | * |
| 1142 | * Be careful here because iov_len is a size_t not an ssize_t |
| 1143 | */ |
| 1144 | tot_len = 0; |
| 1145 | vector = iov; |
| 1146 | ret = -EINVAL; |
| 1147 | for (seg = 0 ; seg < nr_segs; seg++) { |
| 1148 | compat_ssize_t tmp = tot_len; |
| 1149 | compat_ssize_t len; |
| 1150 | compat_uptr_t buf; |
| 1151 | |
| 1152 | if (__get_user(len, &uvector->iov_len) || |
| 1153 | __get_user(buf, &uvector->iov_base)) { |
| 1154 | ret = -EFAULT; |
| 1155 | goto out; |
| 1156 | } |
| 1157 | if (len < 0) /* size_t not fitting an compat_ssize_t .. */ |
| 1158 | goto out; |
| 1159 | tot_len += len; |
| 1160 | if (tot_len < tmp) /* maths overflow on the compat_ssize_t */ |
| 1161 | goto out; |
| 1162 | vector->iov_base = compat_ptr(buf); |
| 1163 | vector->iov_len = (compat_size_t) len; |
| 1164 | uvector++; |
| 1165 | vector++; |
| 1166 | } |
| 1167 | if (tot_len == 0) { |
| 1168 | ret = 0; |
| 1169 | goto out; |
| 1170 | } |
| 1171 | |
| 1172 | ret = rw_verify_area(type, file, pos, tot_len); |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 1173 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | goto out; |
| 1175 | |
| 1176 | fnv = NULL; |
| 1177 | if (type == READ) { |
| 1178 | fn = file->f_op->read; |
| 1179 | fnv = file->f_op->readv; |
| 1180 | } else { |
| 1181 | fn = (io_fn_t)file->f_op->write; |
| 1182 | fnv = file->f_op->writev; |
| 1183 | } |
| 1184 | if (fnv) { |
| 1185 | ret = fnv(file, iov, nr_segs, pos); |
| 1186 | goto out; |
| 1187 | } |
| 1188 | |
| 1189 | /* Do it by hand, with file-ops */ |
| 1190 | ret = 0; |
| 1191 | vector = iov; |
| 1192 | while (nr_segs > 0) { |
| 1193 | void __user * base; |
| 1194 | size_t len; |
| 1195 | ssize_t nr; |
| 1196 | |
| 1197 | base = vector->iov_base; |
| 1198 | len = vector->iov_len; |
| 1199 | vector++; |
| 1200 | nr_segs--; |
| 1201 | |
| 1202 | nr = fn(file, base, len, pos); |
| 1203 | |
| 1204 | if (nr < 0) { |
| 1205 | if (!ret) ret = nr; |
| 1206 | break; |
| 1207 | } |
| 1208 | ret += nr; |
| 1209 | if (nr != len) |
| 1210 | break; |
| 1211 | } |
| 1212 | out: |
| 1213 | if (iov != iovstack) |
| 1214 | kfree(iov); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 1215 | if ((ret + (type == READ)) > 0) { |
| 1216 | struct dentry *dentry = file->f_dentry; |
| 1217 | if (type == READ) |
| 1218 | fsnotify_access(dentry); |
| 1219 | else |
| 1220 | fsnotify_modify(dentry); |
| 1221 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | return ret; |
| 1223 | } |
| 1224 | |
| 1225 | asmlinkage ssize_t |
| 1226 | compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen) |
| 1227 | { |
| 1228 | struct file *file; |
| 1229 | ssize_t ret = -EBADF; |
| 1230 | |
| 1231 | file = fget(fd); |
| 1232 | if (!file) |
| 1233 | return -EBADF; |
| 1234 | |
| 1235 | if (!(file->f_mode & FMODE_READ)) |
| 1236 | goto out; |
| 1237 | |
| 1238 | ret = -EINVAL; |
| 1239 | if (!file->f_op || (!file->f_op->readv && !file->f_op->read)) |
| 1240 | goto out; |
| 1241 | |
| 1242 | ret = compat_do_readv_writev(READ, file, vec, vlen, &file->f_pos); |
| 1243 | |
| 1244 | out: |
| 1245 | fput(file); |
| 1246 | return ret; |
| 1247 | } |
| 1248 | |
| 1249 | asmlinkage ssize_t |
| 1250 | compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen) |
| 1251 | { |
| 1252 | struct file *file; |
| 1253 | ssize_t ret = -EBADF; |
| 1254 | |
| 1255 | file = fget(fd); |
| 1256 | if (!file) |
| 1257 | return -EBADF; |
| 1258 | if (!(file->f_mode & FMODE_WRITE)) |
| 1259 | goto out; |
| 1260 | |
| 1261 | ret = -EINVAL; |
| 1262 | if (!file->f_op || (!file->f_op->writev && !file->f_op->write)) |
| 1263 | goto out; |
| 1264 | |
| 1265 | ret = compat_do_readv_writev(WRITE, file, vec, vlen, &file->f_pos); |
| 1266 | |
| 1267 | out: |
| 1268 | fput(file); |
| 1269 | return ret; |
| 1270 | } |
| 1271 | |
| 1272 | /* |
Miklos Szeredi | e922efc | 2005-09-06 15:18:25 -0700 | [diff] [blame] | 1273 | * Exactly like fs/open.c:sys_open(), except that it doesn't set the |
| 1274 | * O_LARGEFILE flag. |
| 1275 | */ |
| 1276 | asmlinkage long |
| 1277 | compat_sys_open(const char __user *filename, int flags, int mode) |
| 1278 | { |
| 1279 | return do_sys_open(filename, flags, mode); |
| 1280 | } |
| 1281 | |
| 1282 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | * compat_count() counts the number of arguments/envelopes. It is basically |
| 1284 | * a copy of count() from fs/exec.c, except that it works with 32 bit argv |
| 1285 | * and envp pointers. |
| 1286 | */ |
| 1287 | static int compat_count(compat_uptr_t __user *argv, int max) |
| 1288 | { |
| 1289 | int i = 0; |
| 1290 | |
| 1291 | if (argv != NULL) { |
| 1292 | for (;;) { |
| 1293 | compat_uptr_t p; |
| 1294 | |
| 1295 | if (get_user(p, argv)) |
| 1296 | return -EFAULT; |
| 1297 | if (!p) |
| 1298 | break; |
| 1299 | argv++; |
| 1300 | if(++i > max) |
| 1301 | return -E2BIG; |
| 1302 | } |
| 1303 | } |
| 1304 | return i; |
| 1305 | } |
| 1306 | |
| 1307 | /* |
| 1308 | * compat_copy_strings() is basically a copy of copy_strings() from fs/exec.c |
| 1309 | * except that it works with 32 bit argv and envp pointers. |
| 1310 | */ |
| 1311 | static int compat_copy_strings(int argc, compat_uptr_t __user *argv, |
| 1312 | struct linux_binprm *bprm) |
| 1313 | { |
| 1314 | struct page *kmapped_page = NULL; |
| 1315 | char *kaddr = NULL; |
| 1316 | int ret; |
| 1317 | |
| 1318 | while (argc-- > 0) { |
| 1319 | compat_uptr_t str; |
| 1320 | int len; |
| 1321 | unsigned long pos; |
| 1322 | |
| 1323 | if (get_user(str, argv+argc) || |
| 1324 | !(len = strnlen_user(compat_ptr(str), bprm->p))) { |
| 1325 | ret = -EFAULT; |
| 1326 | goto out; |
| 1327 | } |
| 1328 | |
| 1329 | if (bprm->p < len) { |
| 1330 | ret = -E2BIG; |
| 1331 | goto out; |
| 1332 | } |
| 1333 | |
| 1334 | bprm->p -= len; |
| 1335 | /* XXX: add architecture specific overflow check here. */ |
| 1336 | pos = bprm->p; |
| 1337 | |
| 1338 | while (len > 0) { |
| 1339 | int i, new, err; |
| 1340 | int offset, bytes_to_copy; |
| 1341 | struct page *page; |
| 1342 | |
| 1343 | offset = pos % PAGE_SIZE; |
| 1344 | i = pos/PAGE_SIZE; |
| 1345 | page = bprm->page[i]; |
| 1346 | new = 0; |
| 1347 | if (!page) { |
| 1348 | page = alloc_page(GFP_HIGHUSER); |
| 1349 | bprm->page[i] = page; |
| 1350 | if (!page) { |
| 1351 | ret = -ENOMEM; |
| 1352 | goto out; |
| 1353 | } |
| 1354 | new = 1; |
| 1355 | } |
| 1356 | |
| 1357 | if (page != kmapped_page) { |
| 1358 | if (kmapped_page) |
| 1359 | kunmap(kmapped_page); |
| 1360 | kmapped_page = page; |
| 1361 | kaddr = kmap(kmapped_page); |
| 1362 | } |
| 1363 | if (new && offset) |
| 1364 | memset(kaddr, 0, offset); |
| 1365 | bytes_to_copy = PAGE_SIZE - offset; |
| 1366 | if (bytes_to_copy > len) { |
| 1367 | bytes_to_copy = len; |
| 1368 | if (new) |
| 1369 | memset(kaddr+offset+len, 0, |
| 1370 | PAGE_SIZE-offset-len); |
| 1371 | } |
| 1372 | err = copy_from_user(kaddr+offset, compat_ptr(str), |
| 1373 | bytes_to_copy); |
| 1374 | if (err) { |
| 1375 | ret = -EFAULT; |
| 1376 | goto out; |
| 1377 | } |
| 1378 | |
| 1379 | pos += bytes_to_copy; |
| 1380 | str += bytes_to_copy; |
| 1381 | len -= bytes_to_copy; |
| 1382 | } |
| 1383 | } |
| 1384 | ret = 0; |
| 1385 | out: |
| 1386 | if (kmapped_page) |
| 1387 | kunmap(kmapped_page); |
| 1388 | return ret; |
| 1389 | } |
| 1390 | |
| 1391 | #ifdef CONFIG_MMU |
| 1392 | |
| 1393 | #define free_arg_pages(bprm) do { } while (0) |
| 1394 | |
| 1395 | #else |
| 1396 | |
| 1397 | static inline void free_arg_pages(struct linux_binprm *bprm) |
| 1398 | { |
| 1399 | int i; |
| 1400 | |
| 1401 | for (i = 0; i < MAX_ARG_PAGES; i++) { |
| 1402 | if (bprm->page[i]) |
| 1403 | __free_page(bprm->page[i]); |
| 1404 | bprm->page[i] = NULL; |
| 1405 | } |
| 1406 | } |
| 1407 | |
| 1408 | #endif /* CONFIG_MMU */ |
| 1409 | |
| 1410 | /* |
| 1411 | * compat_do_execve() is mostly a copy of do_execve(), with the exception |
| 1412 | * that it processes 32 bit argv and envp pointers. |
| 1413 | */ |
| 1414 | int compat_do_execve(char * filename, |
| 1415 | compat_uptr_t __user *argv, |
| 1416 | compat_uptr_t __user *envp, |
| 1417 | struct pt_regs * regs) |
| 1418 | { |
| 1419 | struct linux_binprm *bprm; |
| 1420 | struct file *file; |
| 1421 | int retval; |
| 1422 | int i; |
| 1423 | |
| 1424 | retval = -ENOMEM; |
| 1425 | bprm = kmalloc(sizeof(*bprm), GFP_KERNEL); |
| 1426 | if (!bprm) |
| 1427 | goto out_ret; |
| 1428 | memset(bprm, 0, sizeof(*bprm)); |
| 1429 | |
| 1430 | file = open_exec(filename); |
| 1431 | retval = PTR_ERR(file); |
| 1432 | if (IS_ERR(file)) |
| 1433 | goto out_kfree; |
| 1434 | |
| 1435 | sched_exec(); |
| 1436 | |
| 1437 | bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *); |
| 1438 | bprm->file = file; |
| 1439 | bprm->filename = filename; |
| 1440 | bprm->interp = filename; |
| 1441 | bprm->mm = mm_alloc(); |
| 1442 | retval = -ENOMEM; |
| 1443 | if (!bprm->mm) |
| 1444 | goto out_file; |
| 1445 | |
| 1446 | retval = init_new_context(current, bprm->mm); |
| 1447 | if (retval < 0) |
| 1448 | goto out_mm; |
| 1449 | |
| 1450 | bprm->argc = compat_count(argv, bprm->p / sizeof(compat_uptr_t)); |
| 1451 | if ((retval = bprm->argc) < 0) |
| 1452 | goto out_mm; |
| 1453 | |
| 1454 | bprm->envc = compat_count(envp, bprm->p / sizeof(compat_uptr_t)); |
| 1455 | if ((retval = bprm->envc) < 0) |
| 1456 | goto out_mm; |
| 1457 | |
| 1458 | retval = security_bprm_alloc(bprm); |
| 1459 | if (retval) |
| 1460 | goto out; |
| 1461 | |
| 1462 | retval = prepare_binprm(bprm); |
| 1463 | if (retval < 0) |
| 1464 | goto out; |
| 1465 | |
| 1466 | retval = copy_strings_kernel(1, &bprm->filename, bprm); |
| 1467 | if (retval < 0) |
| 1468 | goto out; |
| 1469 | |
| 1470 | bprm->exec = bprm->p; |
| 1471 | retval = compat_copy_strings(bprm->envc, envp, bprm); |
| 1472 | if (retval < 0) |
| 1473 | goto out; |
| 1474 | |
| 1475 | retval = compat_copy_strings(bprm->argc, argv, bprm); |
| 1476 | if (retval < 0) |
| 1477 | goto out; |
| 1478 | |
| 1479 | retval = search_binary_handler(bprm, regs); |
| 1480 | if (retval >= 0) { |
| 1481 | free_arg_pages(bprm); |
| 1482 | |
| 1483 | /* execve success */ |
| 1484 | security_bprm_free(bprm); |
David S. Miller | 4a805e8 | 2005-09-14 21:40:00 -0700 | [diff] [blame] | 1485 | acct_update_integrals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | kfree(bprm); |
| 1487 | return retval; |
| 1488 | } |
| 1489 | |
| 1490 | out: |
| 1491 | /* Something went wrong, return the inode and free the argument pages*/ |
| 1492 | for (i = 0 ; i < MAX_ARG_PAGES ; i++) { |
| 1493 | struct page * page = bprm->page[i]; |
| 1494 | if (page) |
| 1495 | __free_page(page); |
| 1496 | } |
| 1497 | |
| 1498 | if (bprm->security) |
| 1499 | security_bprm_free(bprm); |
| 1500 | |
| 1501 | out_mm: |
| 1502 | if (bprm->mm) |
| 1503 | mmdrop(bprm->mm); |
| 1504 | |
| 1505 | out_file: |
| 1506 | if (bprm->file) { |
| 1507 | allow_write_access(bprm->file); |
| 1508 | fput(bprm->file); |
| 1509 | } |
| 1510 | |
| 1511 | out_kfree: |
| 1512 | kfree(bprm); |
| 1513 | |
| 1514 | out_ret: |
| 1515 | return retval; |
| 1516 | } |
| 1517 | |
| 1518 | #define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t)) |
| 1519 | |
| 1520 | #define ROUND_UP(x,y) (((x)+(y)-1)/(y)) |
| 1521 | |
| 1522 | /* |
| 1523 | * Ooo, nasty. We need here to frob 32-bit unsigned longs to |
| 1524 | * 64-bit unsigned longs. |
| 1525 | */ |
| 1526 | static inline |
| 1527 | int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset, |
| 1528 | unsigned long *fdset) |
| 1529 | { |
| 1530 | nr = ROUND_UP(nr, __COMPAT_NFDBITS); |
| 1531 | if (ufdset) { |
| 1532 | unsigned long odd; |
| 1533 | |
| 1534 | if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t))) |
| 1535 | return -EFAULT; |
| 1536 | |
| 1537 | odd = nr & 1UL; |
| 1538 | nr &= ~1UL; |
| 1539 | while (nr) { |
| 1540 | unsigned long h, l; |
| 1541 | __get_user(l, ufdset); |
| 1542 | __get_user(h, ufdset+1); |
| 1543 | ufdset += 2; |
| 1544 | *fdset++ = h << 32 | l; |
| 1545 | nr -= 2; |
| 1546 | } |
| 1547 | if (odd) |
| 1548 | __get_user(*fdset, ufdset); |
| 1549 | } else { |
| 1550 | /* Tricky, must clear full unsigned long in the |
| 1551 | * kernel fdset at the end, this makes sure that |
| 1552 | * actually happens. |
| 1553 | */ |
| 1554 | memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t)); |
| 1555 | } |
| 1556 | return 0; |
| 1557 | } |
| 1558 | |
| 1559 | static inline |
| 1560 | void compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset, |
| 1561 | unsigned long *fdset) |
| 1562 | { |
| 1563 | unsigned long odd; |
| 1564 | nr = ROUND_UP(nr, __COMPAT_NFDBITS); |
| 1565 | |
| 1566 | if (!ufdset) |
| 1567 | return; |
| 1568 | |
| 1569 | odd = nr & 1UL; |
| 1570 | nr &= ~1UL; |
| 1571 | while (nr) { |
| 1572 | unsigned long h, l; |
| 1573 | l = *fdset++; |
| 1574 | h = l >> 32; |
| 1575 | __put_user(l, ufdset); |
| 1576 | __put_user(h, ufdset+1); |
| 1577 | ufdset += 2; |
| 1578 | nr -= 2; |
| 1579 | } |
| 1580 | if (odd) |
| 1581 | __put_user(*fdset, ufdset); |
| 1582 | } |
| 1583 | |
| 1584 | |
| 1585 | /* |
| 1586 | * This is a virtual copy of sys_select from fs/select.c and probably |
| 1587 | * should be compared to it from time to time |
| 1588 | */ |
| 1589 | static void *select_bits_alloc(int size) |
| 1590 | { |
| 1591 | return kmalloc(6 * size, GFP_KERNEL); |
| 1592 | } |
| 1593 | |
| 1594 | static void select_bits_free(void *bits, int size) |
| 1595 | { |
| 1596 | kfree(bits); |
| 1597 | } |
| 1598 | |
| 1599 | /* |
| 1600 | * We can actually return ERESTARTSYS instead of EINTR, but I'd |
| 1601 | * like to be certain this leads to no problems. So I return |
| 1602 | * EINTR just for safety. |
| 1603 | * |
| 1604 | * Update: ERESTARTSYS breaks at least the xview clock binary, so |
| 1605 | * I'm trying ERESTARTNOHAND which restart only when you want to. |
| 1606 | */ |
| 1607 | #define MAX_SELECT_SECONDS \ |
| 1608 | ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1) |
| 1609 | |
| 1610 | asmlinkage long |
| 1611 | compat_sys_select(int n, compat_ulong_t __user *inp, compat_ulong_t __user *outp, |
| 1612 | compat_ulong_t __user *exp, struct compat_timeval __user *tvp) |
| 1613 | { |
| 1614 | fd_set_bits fds; |
| 1615 | char *bits; |
| 1616 | long timeout; |
| 1617 | int size, max_fdset, ret = -EINVAL; |
Linus Torvalds | a4531ed | 2005-09-09 15:10:52 -0700 | [diff] [blame] | 1618 | struct fdtable *fdt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | |
| 1620 | timeout = MAX_SCHEDULE_TIMEOUT; |
| 1621 | if (tvp) { |
| 1622 | time_t sec, usec; |
| 1623 | |
| 1624 | if (!access_ok(VERIFY_READ, tvp, sizeof(*tvp)) |
| 1625 | || __get_user(sec, &tvp->tv_sec) |
| 1626 | || __get_user(usec, &tvp->tv_usec)) { |
| 1627 | ret = -EFAULT; |
| 1628 | goto out_nofds; |
| 1629 | } |
| 1630 | |
| 1631 | if (sec < 0 || usec < 0) |
| 1632 | goto out_nofds; |
| 1633 | |
| 1634 | if ((unsigned long) sec < MAX_SELECT_SECONDS) { |
| 1635 | timeout = ROUND_UP(usec, 1000000/HZ); |
| 1636 | timeout += sec * (unsigned long) HZ; |
| 1637 | } |
| 1638 | } |
| 1639 | |
| 1640 | if (n < 0) |
| 1641 | goto out_nofds; |
| 1642 | |
| 1643 | /* max_fdset can increase, so grab it once to avoid race */ |
Linus Torvalds | ac5b8b6 | 2005-09-09 15:42:34 -0700 | [diff] [blame] | 1644 | rcu_read_lock(); |
Linus Torvalds | a4531ed | 2005-09-09 15:10:52 -0700 | [diff] [blame] | 1645 | fdt = files_fdtable(current->files); |
| 1646 | max_fdset = fdt->max_fdset; |
Linus Torvalds | ac5b8b6 | 2005-09-09 15:42:34 -0700 | [diff] [blame] | 1647 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | if (n > max_fdset) |
| 1649 | n = max_fdset; |
| 1650 | |
| 1651 | /* |
| 1652 | * We need 6 bitmaps (in/out/ex for both incoming and outgoing), |
| 1653 | * since we used fdset we need to allocate memory in units of |
| 1654 | * long-words. |
| 1655 | */ |
| 1656 | ret = -ENOMEM; |
| 1657 | size = FDS_BYTES(n); |
| 1658 | bits = select_bits_alloc(size); |
| 1659 | if (!bits) |
| 1660 | goto out_nofds; |
| 1661 | fds.in = (unsigned long *) bits; |
| 1662 | fds.out = (unsigned long *) (bits + size); |
| 1663 | fds.ex = (unsigned long *) (bits + 2*size); |
| 1664 | fds.res_in = (unsigned long *) (bits + 3*size); |
| 1665 | fds.res_out = (unsigned long *) (bits + 4*size); |
| 1666 | fds.res_ex = (unsigned long *) (bits + 5*size); |
| 1667 | |
| 1668 | if ((ret = compat_get_fd_set(n, inp, fds.in)) || |
| 1669 | (ret = compat_get_fd_set(n, outp, fds.out)) || |
| 1670 | (ret = compat_get_fd_set(n, exp, fds.ex))) |
| 1671 | goto out; |
| 1672 | zero_fd_set(n, fds.res_in); |
| 1673 | zero_fd_set(n, fds.res_out); |
| 1674 | zero_fd_set(n, fds.res_ex); |
| 1675 | |
| 1676 | ret = do_select(n, &fds, &timeout); |
| 1677 | |
| 1678 | if (tvp && !(current->personality & STICKY_TIMEOUTS)) { |
| 1679 | time_t sec = 0, usec = 0; |
| 1680 | if (timeout) { |
| 1681 | sec = timeout / HZ; |
| 1682 | usec = timeout % HZ; |
| 1683 | usec *= (1000000/HZ); |
| 1684 | } |
| 1685 | if (put_user(sec, &tvp->tv_sec) || |
| 1686 | put_user(usec, &tvp->tv_usec)) |
| 1687 | ret = -EFAULT; |
| 1688 | } |
| 1689 | |
| 1690 | if (ret < 0) |
| 1691 | goto out; |
| 1692 | if (!ret) { |
| 1693 | ret = -ERESTARTNOHAND; |
| 1694 | if (signal_pending(current)) |
| 1695 | goto out; |
| 1696 | ret = 0; |
| 1697 | } |
| 1698 | |
| 1699 | compat_set_fd_set(n, inp, fds.res_in); |
| 1700 | compat_set_fd_set(n, outp, fds.res_out); |
| 1701 | compat_set_fd_set(n, exp, fds.res_ex); |
| 1702 | |
| 1703 | out: |
| 1704 | select_bits_free(bits, size); |
| 1705 | out_nofds: |
| 1706 | return ret; |
| 1707 | } |
| 1708 | |
| 1709 | #if defined(CONFIG_NFSD) || defined(CONFIG_NFSD_MODULE) |
| 1710 | /* Stuff for NFS server syscalls... */ |
| 1711 | struct compat_nfsctl_svc { |
| 1712 | u16 svc32_port; |
| 1713 | s32 svc32_nthreads; |
| 1714 | }; |
| 1715 | |
| 1716 | struct compat_nfsctl_client { |
| 1717 | s8 cl32_ident[NFSCLNT_IDMAX+1]; |
| 1718 | s32 cl32_naddr; |
| 1719 | struct in_addr cl32_addrlist[NFSCLNT_ADDRMAX]; |
| 1720 | s32 cl32_fhkeytype; |
| 1721 | s32 cl32_fhkeylen; |
| 1722 | u8 cl32_fhkey[NFSCLNT_KEYMAX]; |
| 1723 | }; |
| 1724 | |
| 1725 | struct compat_nfsctl_export { |
| 1726 | char ex32_client[NFSCLNT_IDMAX+1]; |
| 1727 | char ex32_path[NFS_MAXPATHLEN+1]; |
| 1728 | compat_dev_t ex32_dev; |
| 1729 | compat_ino_t ex32_ino; |
| 1730 | compat_int_t ex32_flags; |
Stephen Rothwell | 202e597 | 2005-09-06 15:16:40 -0700 | [diff] [blame] | 1731 | __compat_uid_t ex32_anon_uid; |
| 1732 | __compat_gid_t ex32_anon_gid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | }; |
| 1734 | |
| 1735 | struct compat_nfsctl_fdparm { |
| 1736 | struct sockaddr gd32_addr; |
| 1737 | s8 gd32_path[NFS_MAXPATHLEN+1]; |
| 1738 | compat_int_t gd32_version; |
| 1739 | }; |
| 1740 | |
| 1741 | struct compat_nfsctl_fsparm { |
| 1742 | struct sockaddr gd32_addr; |
| 1743 | s8 gd32_path[NFS_MAXPATHLEN+1]; |
| 1744 | compat_int_t gd32_maxlen; |
| 1745 | }; |
| 1746 | |
| 1747 | struct compat_nfsctl_arg { |
| 1748 | compat_int_t ca32_version; /* safeguard */ |
| 1749 | union { |
| 1750 | struct compat_nfsctl_svc u32_svc; |
| 1751 | struct compat_nfsctl_client u32_client; |
| 1752 | struct compat_nfsctl_export u32_export; |
| 1753 | struct compat_nfsctl_fdparm u32_getfd; |
| 1754 | struct compat_nfsctl_fsparm u32_getfs; |
| 1755 | } u; |
| 1756 | #define ca32_svc u.u32_svc |
| 1757 | #define ca32_client u.u32_client |
| 1758 | #define ca32_export u.u32_export |
| 1759 | #define ca32_getfd u.u32_getfd |
| 1760 | #define ca32_getfs u.u32_getfs |
| 1761 | }; |
| 1762 | |
| 1763 | union compat_nfsctl_res { |
| 1764 | __u8 cr32_getfh[NFS_FHSIZE]; |
| 1765 | struct knfsd_fh cr32_getfs; |
| 1766 | }; |
| 1767 | |
| 1768 | static int compat_nfs_svc_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg) |
| 1769 | { |
| 1770 | int err; |
| 1771 | |
| 1772 | err = access_ok(VERIFY_READ, &arg->ca32_svc, sizeof(arg->ca32_svc)); |
| 1773 | err |= get_user(karg->ca_version, &arg->ca32_version); |
| 1774 | err |= __get_user(karg->ca_svc.svc_port, &arg->ca32_svc.svc32_port); |
| 1775 | err |= __get_user(karg->ca_svc.svc_nthreads, &arg->ca32_svc.svc32_nthreads); |
| 1776 | return (err) ? -EFAULT : 0; |
| 1777 | } |
| 1778 | |
| 1779 | static int compat_nfs_clnt_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg) |
| 1780 | { |
| 1781 | int err; |
| 1782 | |
| 1783 | err = access_ok(VERIFY_READ, &arg->ca32_client, sizeof(arg->ca32_client)); |
| 1784 | err |= get_user(karg->ca_version, &arg->ca32_version); |
| 1785 | err |= __copy_from_user(&karg->ca_client.cl_ident[0], |
| 1786 | &arg->ca32_client.cl32_ident[0], |
| 1787 | NFSCLNT_IDMAX); |
| 1788 | err |= __get_user(karg->ca_client.cl_naddr, &arg->ca32_client.cl32_naddr); |
| 1789 | err |= __copy_from_user(&karg->ca_client.cl_addrlist[0], |
| 1790 | &arg->ca32_client.cl32_addrlist[0], |
| 1791 | (sizeof(struct in_addr) * NFSCLNT_ADDRMAX)); |
| 1792 | err |= __get_user(karg->ca_client.cl_fhkeytype, |
| 1793 | &arg->ca32_client.cl32_fhkeytype); |
| 1794 | err |= __get_user(karg->ca_client.cl_fhkeylen, |
| 1795 | &arg->ca32_client.cl32_fhkeylen); |
| 1796 | err |= __copy_from_user(&karg->ca_client.cl_fhkey[0], |
| 1797 | &arg->ca32_client.cl32_fhkey[0], |
| 1798 | NFSCLNT_KEYMAX); |
| 1799 | |
| 1800 | return (err) ? -EFAULT : 0; |
| 1801 | } |
| 1802 | |
| 1803 | static int compat_nfs_exp_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg) |
| 1804 | { |
| 1805 | int err; |
| 1806 | |
| 1807 | err = access_ok(VERIFY_READ, &arg->ca32_export, sizeof(arg->ca32_export)); |
| 1808 | err |= get_user(karg->ca_version, &arg->ca32_version); |
| 1809 | err |= __copy_from_user(&karg->ca_export.ex_client[0], |
| 1810 | &arg->ca32_export.ex32_client[0], |
| 1811 | NFSCLNT_IDMAX); |
| 1812 | err |= __copy_from_user(&karg->ca_export.ex_path[0], |
| 1813 | &arg->ca32_export.ex32_path[0], |
| 1814 | NFS_MAXPATHLEN); |
| 1815 | err |= __get_user(karg->ca_export.ex_dev, |
| 1816 | &arg->ca32_export.ex32_dev); |
| 1817 | err |= __get_user(karg->ca_export.ex_ino, |
| 1818 | &arg->ca32_export.ex32_ino); |
| 1819 | err |= __get_user(karg->ca_export.ex_flags, |
| 1820 | &arg->ca32_export.ex32_flags); |
| 1821 | err |= __get_user(karg->ca_export.ex_anon_uid, |
| 1822 | &arg->ca32_export.ex32_anon_uid); |
| 1823 | err |= __get_user(karg->ca_export.ex_anon_gid, |
| 1824 | &arg->ca32_export.ex32_anon_gid); |
| 1825 | SET_UID(karg->ca_export.ex_anon_uid, karg->ca_export.ex_anon_uid); |
| 1826 | SET_GID(karg->ca_export.ex_anon_gid, karg->ca_export.ex_anon_gid); |
| 1827 | |
| 1828 | return (err) ? -EFAULT : 0; |
| 1829 | } |
| 1830 | |
| 1831 | static int compat_nfs_getfd_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg) |
| 1832 | { |
| 1833 | int err; |
| 1834 | |
| 1835 | err = access_ok(VERIFY_READ, &arg->ca32_getfd, sizeof(arg->ca32_getfd)); |
| 1836 | err |= get_user(karg->ca_version, &arg->ca32_version); |
| 1837 | err |= __copy_from_user(&karg->ca_getfd.gd_addr, |
| 1838 | &arg->ca32_getfd.gd32_addr, |
| 1839 | (sizeof(struct sockaddr))); |
| 1840 | err |= __copy_from_user(&karg->ca_getfd.gd_path, |
| 1841 | &arg->ca32_getfd.gd32_path, |
| 1842 | (NFS_MAXPATHLEN+1)); |
| 1843 | err |= __get_user(karg->ca_getfd.gd_version, |
| 1844 | &arg->ca32_getfd.gd32_version); |
| 1845 | |
| 1846 | return (err) ? -EFAULT : 0; |
| 1847 | } |
| 1848 | |
| 1849 | static int compat_nfs_getfs_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg) |
| 1850 | { |
| 1851 | int err; |
| 1852 | |
| 1853 | err = access_ok(VERIFY_READ, &arg->ca32_getfs, sizeof(arg->ca32_getfs)); |
| 1854 | err |= get_user(karg->ca_version, &arg->ca32_version); |
| 1855 | err |= __copy_from_user(&karg->ca_getfs.gd_addr, |
| 1856 | &arg->ca32_getfs.gd32_addr, |
| 1857 | (sizeof(struct sockaddr))); |
| 1858 | err |= __copy_from_user(&karg->ca_getfs.gd_path, |
| 1859 | &arg->ca32_getfs.gd32_path, |
| 1860 | (NFS_MAXPATHLEN+1)); |
| 1861 | err |= __get_user(karg->ca_getfs.gd_maxlen, |
| 1862 | &arg->ca32_getfs.gd32_maxlen); |
| 1863 | |
| 1864 | return (err) ? -EFAULT : 0; |
| 1865 | } |
| 1866 | |
| 1867 | /* This really doesn't need translations, we are only passing |
| 1868 | * back a union which contains opaque nfs file handle data. |
| 1869 | */ |
| 1870 | static int compat_nfs_getfh_res_trans(union nfsctl_res *kres, union compat_nfsctl_res __user *res) |
| 1871 | { |
| 1872 | int err; |
| 1873 | |
| 1874 | err = copy_to_user(res, kres, sizeof(*res)); |
| 1875 | |
| 1876 | return (err) ? -EFAULT : 0; |
| 1877 | } |
| 1878 | |
| 1879 | asmlinkage long compat_sys_nfsservctl(int cmd, struct compat_nfsctl_arg __user *arg, |
| 1880 | union compat_nfsctl_res __user *res) |
| 1881 | { |
| 1882 | struct nfsctl_arg *karg; |
| 1883 | union nfsctl_res *kres; |
| 1884 | mm_segment_t oldfs; |
| 1885 | int err; |
| 1886 | |
| 1887 | karg = kmalloc(sizeof(*karg), GFP_USER); |
| 1888 | kres = kmalloc(sizeof(*kres), GFP_USER); |
| 1889 | if(!karg || !kres) { |
| 1890 | err = -ENOMEM; |
| 1891 | goto done; |
| 1892 | } |
| 1893 | |
| 1894 | switch(cmd) { |
| 1895 | case NFSCTL_SVC: |
| 1896 | err = compat_nfs_svc_trans(karg, arg); |
| 1897 | break; |
| 1898 | |
| 1899 | case NFSCTL_ADDCLIENT: |
| 1900 | err = compat_nfs_clnt_trans(karg, arg); |
| 1901 | break; |
| 1902 | |
| 1903 | case NFSCTL_DELCLIENT: |
| 1904 | err = compat_nfs_clnt_trans(karg, arg); |
| 1905 | break; |
| 1906 | |
| 1907 | case NFSCTL_EXPORT: |
| 1908 | case NFSCTL_UNEXPORT: |
| 1909 | err = compat_nfs_exp_trans(karg, arg); |
| 1910 | break; |
| 1911 | |
| 1912 | case NFSCTL_GETFD: |
| 1913 | err = compat_nfs_getfd_trans(karg, arg); |
| 1914 | break; |
| 1915 | |
| 1916 | case NFSCTL_GETFS: |
| 1917 | err = compat_nfs_getfs_trans(karg, arg); |
| 1918 | break; |
| 1919 | |
| 1920 | default: |
| 1921 | err = -EINVAL; |
| 1922 | goto done; |
| 1923 | } |
| 1924 | |
| 1925 | oldfs = get_fs(); |
| 1926 | set_fs(KERNEL_DS); |
| 1927 | /* The __user pointer casts are valid because of the set_fs() */ |
| 1928 | err = sys_nfsservctl(cmd, (void __user *) karg, (void __user *) kres); |
| 1929 | set_fs(oldfs); |
| 1930 | |
| 1931 | if (err) |
| 1932 | goto done; |
| 1933 | |
| 1934 | if((cmd == NFSCTL_GETFD) || |
| 1935 | (cmd == NFSCTL_GETFS)) |
| 1936 | err = compat_nfs_getfh_res_trans(kres, res); |
| 1937 | |
| 1938 | done: |
| 1939 | kfree(karg); |
| 1940 | kfree(kres); |
| 1941 | return err; |
| 1942 | } |
| 1943 | #else /* !NFSD */ |
| 1944 | long asmlinkage compat_sys_nfsservctl(int cmd, void *notused, void *notused2) |
| 1945 | { |
| 1946 | return sys_ni_syscall(); |
| 1947 | } |
| 1948 | #endif |