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