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