blob: c2e0813164b458bb9fa93e383be7f967f63aff21 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313static void compat_ioctl_error(struct file *filp, unsigned int fd,
314 unsigned int cmd, unsigned long arg)
315{
316 char buf[10];
317 char *fn = "?";
318 char *path;
319
320 /* find the name of the device. */
321 path = (char *)__get_free_page(GFP_KERNEL);
322 if (path) {
323 fn = d_path(filp->f_dentry, filp->f_vfsmnt, path, PAGE_SIZE);
324 if (IS_ERR(fn))
325 fn = "?";
326 }
327
328 sprintf(buf,"'%c'", (cmd>>24) & 0x3f);
329 if (!isprint(buf[1]))
330 sprintf(buf, "%02x", buf[1]);
331 printk("ioctl32(%s:%d): Unknown cmd fd(%d) "
332 "cmd(%08x){%s} arg(%08x) on %s\n",
333 current->comm, current->pid,
334 (int)fd, (unsigned int)cmd, buf,
335 (unsigned int)arg, fn);
336
337 if (path)
338 free_page((unsigned long)path);
339}
340
341asmlinkage long compat_sys_ioctl(unsigned int fd, unsigned int cmd,
342 unsigned long arg)
343{
344 struct file *filp;
345 int error = -EBADF;
346 struct ioctl_trans *t;
347 int fput_needed;
348
349 filp = fget_light(fd, &fput_needed);
350 if (!filp)
351 goto out;
352
353 /* RED-PEN how should LSM module know it's handling 32bit? */
354 error = security_file_ioctl(filp, cmd, arg);
355 if (error)
356 goto out_fput;
357
358 /*
359 * To allow the compat_ioctl handlers to be self contained
360 * we need to check the common ioctls here first.
361 * Just handle them with the standard handlers below.
362 */
363 switch (cmd) {
364 case FIOCLEX:
365 case FIONCLEX:
366 case FIONBIO:
367 case FIOASYNC:
368 case FIOQSIZE:
369 break;
370
371 case FIBMAP:
372 case FIGETBSZ:
373 case FIONREAD:
374 if (S_ISREG(filp->f_dentry->d_inode->i_mode))
375 break;
376 /*FALL THROUGH*/
377
378 default:
379 if (filp->f_op && filp->f_op->compat_ioctl) {
380 error = filp->f_op->compat_ioctl(filp, cmd, arg);
381 if (error != -ENOIOCTLCMD)
382 goto out_fput;
383 }
384
385 if (!filp->f_op ||
386 (!filp->f_op->ioctl && !filp->f_op->unlocked_ioctl))
387 goto do_ioctl;
388 break;
389 }
390
391 /* When register_ioctl32_conversion is finally gone remove
392 this lock! -AK */
393 down_read(&ioctl32_sem);
394 for (t = ioctl32_hash_table[ioctl32_hash(cmd)]; t; t = t->next) {
395 if (t->cmd == cmd)
396 goto found_handler;
397 }
398 up_read(&ioctl32_sem);
399
400 if (S_ISSOCK(filp->f_dentry->d_inode->i_mode) &&
401 cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
402 error = siocdevprivate_ioctl(fd, cmd, arg);
403 } else {
404 static int count;
405
406 if (++count <= 50)
407 compat_ioctl_error(filp, fd, cmd, arg);
408 error = -EINVAL;
409 }
410
411 goto out_fput;
412
413 found_handler:
414 if (t->handler) {
415 lock_kernel();
416 error = t->handler(fd, cmd, arg, filp);
417 unlock_kernel();
418 up_read(&ioctl32_sem);
419 goto out_fput;
420 }
421
422 up_read(&ioctl32_sem);
423 do_ioctl:
424 error = vfs_ioctl(filp, fd, cmd, arg);
425 out_fput:
426 fput_light(filp, fput_needed);
427 out:
428 return error;
429}
430
431static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
432{
433 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
434 __get_user(kfl->l_type, &ufl->l_type) ||
435 __get_user(kfl->l_whence, &ufl->l_whence) ||
436 __get_user(kfl->l_start, &ufl->l_start) ||
437 __get_user(kfl->l_len, &ufl->l_len) ||
438 __get_user(kfl->l_pid, &ufl->l_pid))
439 return -EFAULT;
440 return 0;
441}
442
443static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
444{
445 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
446 __put_user(kfl->l_type, &ufl->l_type) ||
447 __put_user(kfl->l_whence, &ufl->l_whence) ||
448 __put_user(kfl->l_start, &ufl->l_start) ||
449 __put_user(kfl->l_len, &ufl->l_len) ||
450 __put_user(kfl->l_pid, &ufl->l_pid))
451 return -EFAULT;
452 return 0;
453}
454
455#ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
456static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
457{
458 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
459 __get_user(kfl->l_type, &ufl->l_type) ||
460 __get_user(kfl->l_whence, &ufl->l_whence) ||
461 __get_user(kfl->l_start, &ufl->l_start) ||
462 __get_user(kfl->l_len, &ufl->l_len) ||
463 __get_user(kfl->l_pid, &ufl->l_pid))
464 return -EFAULT;
465 return 0;
466}
467#endif
468
469#ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
470static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
471{
472 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
473 __put_user(kfl->l_type, &ufl->l_type) ||
474 __put_user(kfl->l_whence, &ufl->l_whence) ||
475 __put_user(kfl->l_start, &ufl->l_start) ||
476 __put_user(kfl->l_len, &ufl->l_len) ||
477 __put_user(kfl->l_pid, &ufl->l_pid))
478 return -EFAULT;
479 return 0;
480}
481#endif
482
483asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd,
484 unsigned long arg)
485{
486 mm_segment_t old_fs;
487 struct flock f;
488 long ret;
489
490 switch (cmd) {
491 case F_GETLK:
492 case F_SETLK:
493 case F_SETLKW:
494 ret = get_compat_flock(&f, compat_ptr(arg));
495 if (ret != 0)
496 break;
497 old_fs = get_fs();
498 set_fs(KERNEL_DS);
499 ret = sys_fcntl(fd, cmd, (unsigned long)&f);
500 set_fs(old_fs);
501 if (cmd == F_GETLK && ret == 0) {
502 if ((f.l_start >= COMPAT_OFF_T_MAX) ||
503 ((f.l_start + f.l_len) > COMPAT_OFF_T_MAX))
504 ret = -EOVERFLOW;
505 if (ret == 0)
506 ret = put_compat_flock(&f, compat_ptr(arg));
507 }
508 break;
509
510 case F_GETLK64:
511 case F_SETLK64:
512 case F_SETLKW64:
513 ret = get_compat_flock64(&f, compat_ptr(arg));
514 if (ret != 0)
515 break;
516 old_fs = get_fs();
517 set_fs(KERNEL_DS);
518 ret = sys_fcntl(fd, (cmd == F_GETLK64) ? F_GETLK :
519 ((cmd == F_SETLK64) ? F_SETLK : F_SETLKW),
520 (unsigned long)&f);
521 set_fs(old_fs);
522 if (cmd == F_GETLK64 && ret == 0) {
523 if ((f.l_start >= COMPAT_LOFF_T_MAX) ||
524 ((f.l_start + f.l_len) > COMPAT_LOFF_T_MAX))
525 ret = -EOVERFLOW;
526 if (ret == 0)
527 ret = put_compat_flock64(&f, compat_ptr(arg));
528 }
529 break;
530
531 default:
532 ret = sys_fcntl(fd, cmd, arg);
533 break;
534 }
535 return ret;
536}
537
538asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd,
539 unsigned long arg)
540{
541 if ((cmd == F_GETLK64) || (cmd == F_SETLK64) || (cmd == F_SETLKW64))
542 return -EINVAL;
543 return compat_sys_fcntl64(fd, cmd, arg);
544}
545
546asmlinkage long
547compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p)
548{
549 long ret;
550 aio_context_t ctx64;
551
552 mm_segment_t oldfs = get_fs();
553 if (unlikely(get_user(ctx64, ctx32p)))
554 return -EFAULT;
555
556 set_fs(KERNEL_DS);
557 /* The __user pointer cast is valid because of the set_fs() */
558 ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64);
559 set_fs(oldfs);
560 /* truncating is ok because it's a user address */
561 if (!ret)
562 ret = put_user((u32) ctx64, ctx32p);
563 return ret;
564}
565
566asmlinkage long
567compat_sys_io_getevents(aio_context_t ctx_id,
568 unsigned long min_nr,
569 unsigned long nr,
570 struct io_event __user *events,
571 struct compat_timespec __user *timeout)
572{
573 long ret;
574 struct timespec t;
575 struct timespec __user *ut = NULL;
576
577 ret = -EFAULT;
578 if (unlikely(!access_ok(VERIFY_WRITE, events,
579 nr * sizeof(struct io_event))))
580 goto out;
581 if (timeout) {
582 if (get_compat_timespec(&t, timeout))
583 goto out;
584
585 ut = compat_alloc_user_space(sizeof(*ut));
586 if (copy_to_user(ut, &t, sizeof(t)) )
587 goto out;
588 }
589 ret = sys_io_getevents(ctx_id, min_nr, nr, events, ut);
590out:
591 return ret;
592}
593
594static inline long
595copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64)
596{
597 compat_uptr_t uptr;
598 int i;
599
600 for (i = 0; i < nr; ++i) {
601 if (get_user(uptr, ptr32 + i))
602 return -EFAULT;
603 if (put_user(compat_ptr(uptr), ptr64 + i))
604 return -EFAULT;
605 }
606 return 0;
607}
608
609#define MAX_AIO_SUBMITS (PAGE_SIZE/sizeof(struct iocb *))
610
611asmlinkage long
612compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb)
613{
614 struct iocb __user * __user *iocb64;
615 long ret;
616
617 if (unlikely(nr < 0))
618 return -EINVAL;
619
620 if (nr > MAX_AIO_SUBMITS)
621 nr = MAX_AIO_SUBMITS;
622
623 iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64));
624 ret = copy_iocb(nr, iocb, iocb64);
625 if (!ret)
626 ret = sys_io_submit(ctx_id, nr, iocb64);
627 return ret;
628}
629
630struct compat_ncp_mount_data {
631 compat_int_t version;
632 compat_uint_t ncp_fd;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700633 __compat_uid_t mounted_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 compat_pid_t wdog_pid;
635 unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
636 compat_uint_t time_out;
637 compat_uint_t retry_count;
638 compat_uint_t flags;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700639 __compat_uid_t uid;
640 __compat_gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 compat_mode_t file_mode;
642 compat_mode_t dir_mode;
643};
644
645struct compat_ncp_mount_data_v4 {
646 compat_int_t version;
647 compat_ulong_t flags;
648 compat_ulong_t mounted_uid;
649 compat_long_t wdog_pid;
650 compat_uint_t ncp_fd;
651 compat_uint_t time_out;
652 compat_uint_t retry_count;
653 compat_ulong_t uid;
654 compat_ulong_t gid;
655 compat_ulong_t file_mode;
656 compat_ulong_t dir_mode;
657};
658
659static void *do_ncp_super_data_conv(void *raw_data)
660{
661 int version = *(unsigned int *)raw_data;
662
663 if (version == 3) {
664 struct compat_ncp_mount_data *c_n = raw_data;
665 struct ncp_mount_data *n = raw_data;
666
667 n->dir_mode = c_n->dir_mode;
668 n->file_mode = c_n->file_mode;
669 n->gid = c_n->gid;
670 n->uid = c_n->uid;
671 memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
672 n->wdog_pid = c_n->wdog_pid;
673 n->mounted_uid = c_n->mounted_uid;
674 } else if (version == 4) {
675 struct compat_ncp_mount_data_v4 *c_n = raw_data;
676 struct ncp_mount_data_v4 *n = raw_data;
677
678 n->dir_mode = c_n->dir_mode;
679 n->file_mode = c_n->file_mode;
680 n->gid = c_n->gid;
681 n->uid = c_n->uid;
682 n->retry_count = c_n->retry_count;
683 n->time_out = c_n->time_out;
684 n->ncp_fd = c_n->ncp_fd;
685 n->wdog_pid = c_n->wdog_pid;
686 n->mounted_uid = c_n->mounted_uid;
687 n->flags = c_n->flags;
688 } else if (version != 5) {
689 return NULL;
690 }
691
692 return raw_data;
693}
694
695struct compat_smb_mount_data {
696 compat_int_t version;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700697 __compat_uid_t mounted_uid;
698 __compat_uid_t uid;
699 __compat_gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 compat_mode_t file_mode;
701 compat_mode_t dir_mode;
702};
703
704static void *do_smb_super_data_conv(void *raw_data)
705{
706 struct smb_mount_data *s = raw_data;
707 struct compat_smb_mount_data *c_s = raw_data;
708
709 if (c_s->version != SMB_MOUNT_OLDVERSION)
710 goto out;
711 s->dir_mode = c_s->dir_mode;
712 s->file_mode = c_s->file_mode;
713 s->gid = c_s->gid;
714 s->uid = c_s->uid;
715 s->mounted_uid = c_s->mounted_uid;
716 out:
717 return raw_data;
718}
719
David Howells9a9947b2005-04-18 10:54:51 -0700720struct compat_nfs_string {
721 compat_uint_t len;
David Howells5fc3e622005-04-27 15:39:03 -0700722 compat_uptr_t data;
David Howells9a9947b2005-04-18 10:54:51 -0700723};
724
725static inline void compat_nfs_string(struct nfs_string *dst,
726 struct compat_nfs_string *src)
727{
728 dst->data = compat_ptr(src->data);
729 dst->len = src->len;
730}
731
732struct compat_nfs4_mount_data_v1 {
733 compat_int_t version;
734 compat_int_t flags;
735 compat_int_t rsize;
736 compat_int_t wsize;
737 compat_int_t timeo;
738 compat_int_t retrans;
739 compat_int_t acregmin;
740 compat_int_t acregmax;
741 compat_int_t acdirmin;
742 compat_int_t acdirmax;
743 struct compat_nfs_string client_addr;
744 struct compat_nfs_string mnt_path;
745 struct compat_nfs_string hostname;
746 compat_uint_t host_addrlen;
David Howells5fc3e622005-04-27 15:39:03 -0700747 compat_uptr_t host_addr;
David Howells9a9947b2005-04-18 10:54:51 -0700748 compat_int_t proto;
749 compat_int_t auth_flavourlen;
David Howells5fc3e622005-04-27 15:39:03 -0700750 compat_uptr_t auth_flavours;
David Howells9a9947b2005-04-18 10:54:51 -0700751};
752
753static int do_nfs4_super_data_conv(void *raw_data)
754{
755 int version = *(compat_uint_t *) raw_data;
756
757 if (version == 1) {
758 struct compat_nfs4_mount_data_v1 *raw = raw_data;
759 struct nfs4_mount_data *real = raw_data;
760
761 /* copy the fields backwards */
762 real->auth_flavours = compat_ptr(raw->auth_flavours);
763 real->auth_flavourlen = raw->auth_flavourlen;
764 real->proto = raw->proto;
765 real->host_addr = compat_ptr(raw->host_addr);
766 real->host_addrlen = raw->host_addrlen;
767 compat_nfs_string(&real->hostname, &raw->hostname);
768 compat_nfs_string(&real->mnt_path, &raw->mnt_path);
769 compat_nfs_string(&real->client_addr, &raw->client_addr);
770 real->acdirmax = raw->acdirmax;
771 real->acdirmin = raw->acdirmin;
772 real->acregmax = raw->acregmax;
773 real->acregmin = raw->acregmin;
774 real->retrans = raw->retrans;
775 real->timeo = raw->timeo;
776 real->wsize = raw->wsize;
777 real->rsize = raw->rsize;
778 real->flags = raw->flags;
779 real->version = raw->version;
780 }
781 else {
782 return -EINVAL;
783 }
784
785 return 0;
786}
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788extern int copy_mount_options (const void __user *, unsigned long *);
789
790#define SMBFS_NAME "smbfs"
791#define NCPFS_NAME "ncpfs"
David Howells9a9947b2005-04-18 10:54:51 -0700792#define NFS4_NAME "nfs4"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794asmlinkage long compat_sys_mount(char __user * dev_name, char __user * dir_name,
795 char __user * type, unsigned long flags,
796 void __user * data)
797{
798 unsigned long type_page;
799 unsigned long data_page;
800 unsigned long dev_page;
801 char *dir_page;
802 int retval;
803
804 retval = copy_mount_options (type, &type_page);
805 if (retval < 0)
806 goto out;
807
808 dir_page = getname(dir_name);
809 retval = PTR_ERR(dir_page);
810 if (IS_ERR(dir_page))
811 goto out1;
812
813 retval = copy_mount_options (dev_name, &dev_page);
814 if (retval < 0)
815 goto out2;
816
817 retval = copy_mount_options (data, &data_page);
818 if (retval < 0)
819 goto out3;
820
821 retval = -EINVAL;
822
823 if (type_page) {
824 if (!strcmp((char *)type_page, SMBFS_NAME)) {
825 do_smb_super_data_conv((void *)data_page);
826 } else if (!strcmp((char *)type_page, NCPFS_NAME)) {
827 do_ncp_super_data_conv((void *)data_page);
David Howells9a9947b2005-04-18 10:54:51 -0700828 } else if (!strcmp((char *)type_page, NFS4_NAME)) {
829 if (do_nfs4_super_data_conv((void *) data_page))
830 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832 }
833
834 lock_kernel();
835 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
836 flags, (void*)data_page);
837 unlock_kernel();
838
David Howells9a9947b2005-04-18 10:54:51 -0700839 out4:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 free_page(data_page);
841 out3:
842 free_page(dev_page);
843 out2:
844 putname(dir_page);
845 out1:
846 free_page(type_page);
847 out:
848 return retval;
849}
850
851#define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
852#define COMPAT_ROUND_UP(x) (((x)+sizeof(compat_long_t)-1) & \
853 ~(sizeof(compat_long_t)-1))
854
855struct compat_old_linux_dirent {
856 compat_ulong_t d_ino;
857 compat_ulong_t d_offset;
858 unsigned short d_namlen;
859 char d_name[1];
860};
861
862struct compat_readdir_callback {
863 struct compat_old_linux_dirent __user *dirent;
864 int result;
865};
866
867static int compat_fillonedir(void *__buf, const char *name, int namlen,
868 loff_t offset, ino_t ino, unsigned int d_type)
869{
870 struct compat_readdir_callback *buf = __buf;
871 struct compat_old_linux_dirent __user *dirent;
872
873 if (buf->result)
874 return -EINVAL;
875 buf->result++;
876 dirent = buf->dirent;
877 if (!access_ok(VERIFY_WRITE, dirent,
878 (unsigned long)(dirent->d_name + namlen + 1) -
879 (unsigned long)dirent))
880 goto efault;
881 if ( __put_user(ino, &dirent->d_ino) ||
882 __put_user(offset, &dirent->d_offset) ||
883 __put_user(namlen, &dirent->d_namlen) ||
884 __copy_to_user(dirent->d_name, name, namlen) ||
885 __put_user(0, dirent->d_name + namlen))
886 goto efault;
887 return 0;
888efault:
889 buf->result = -EFAULT;
890 return -EFAULT;
891}
892
893asmlinkage long compat_sys_old_readdir(unsigned int fd,
894 struct compat_old_linux_dirent __user *dirent, unsigned int count)
895{
896 int error;
897 struct file *file;
898 struct compat_readdir_callback buf;
899
900 error = -EBADF;
901 file = fget(fd);
902 if (!file)
903 goto out;
904
905 buf.result = 0;
906 buf.dirent = dirent;
907
908 error = vfs_readdir(file, compat_fillonedir, &buf);
909 if (error >= 0)
910 error = buf.result;
911
912 fput(file);
913out:
914 return error;
915}
916
917struct compat_linux_dirent {
918 compat_ulong_t d_ino;
919 compat_ulong_t d_off;
920 unsigned short d_reclen;
921 char d_name[1];
922};
923
924struct compat_getdents_callback {
925 struct compat_linux_dirent __user *current_dir;
926 struct compat_linux_dirent __user *previous;
927 int count;
928 int error;
929};
930
931static int compat_filldir(void *__buf, const char *name, int namlen,
932 loff_t offset, ino_t ino, unsigned int d_type)
933{
934 struct compat_linux_dirent __user * dirent;
935 struct compat_getdents_callback *buf = __buf;
936 int reclen = COMPAT_ROUND_UP(NAME_OFFSET(dirent) + namlen + 2);
937
938 buf->error = -EINVAL; /* only used if we fail.. */
939 if (reclen > buf->count)
940 return -EINVAL;
941 dirent = buf->previous;
942 if (dirent) {
943 if (__put_user(offset, &dirent->d_off))
944 goto efault;
945 }
946 dirent = buf->current_dir;
947 if (__put_user(ino, &dirent->d_ino))
948 goto efault;
949 if (__put_user(reclen, &dirent->d_reclen))
950 goto efault;
951 if (copy_to_user(dirent->d_name, name, namlen))
952 goto efault;
953 if (__put_user(0, dirent->d_name + namlen))
954 goto efault;
955 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
956 goto efault;
957 buf->previous = dirent;
958 dirent = (void __user *)dirent + reclen;
959 buf->current_dir = dirent;
960 buf->count -= reclen;
961 return 0;
962efault:
963 buf->error = -EFAULT;
964 return -EFAULT;
965}
966
967asmlinkage long compat_sys_getdents(unsigned int fd,
968 struct compat_linux_dirent __user *dirent, unsigned int count)
969{
970 struct file * file;
971 struct compat_linux_dirent __user * lastdirent;
972 struct compat_getdents_callback buf;
973 int error;
974
975 error = -EFAULT;
976 if (!access_ok(VERIFY_WRITE, dirent, count))
977 goto out;
978
979 error = -EBADF;
980 file = fget(fd);
981 if (!file)
982 goto out;
983
984 buf.current_dir = dirent;
985 buf.previous = NULL;
986 buf.count = count;
987 buf.error = 0;
988
989 error = vfs_readdir(file, compat_filldir, &buf);
990 if (error < 0)
991 goto out_putf;
992 error = buf.error;
993 lastdirent = buf.previous;
994 if (lastdirent) {
995 if (put_user(file->f_pos, &lastdirent->d_off))
996 error = -EFAULT;
997 else
998 error = count - buf.count;
999 }
1000
1001out_putf:
1002 fput(file);
1003out:
1004 return error;
1005}
1006
1007#ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64
1008#define COMPAT_ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1))
1009
1010struct compat_getdents_callback64 {
1011 struct linux_dirent64 __user *current_dir;
1012 struct linux_dirent64 __user *previous;
1013 int count;
1014 int error;
1015};
1016
1017static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset,
1018 ino_t ino, unsigned int d_type)
1019{
1020 struct linux_dirent64 __user *dirent;
1021 struct compat_getdents_callback64 *buf = __buf;
1022 int jj = NAME_OFFSET(dirent);
1023 int reclen = COMPAT_ROUND_UP64(jj + namlen + 1);
1024 u64 off;
1025
1026 buf->error = -EINVAL; /* only used if we fail.. */
1027 if (reclen > buf->count)
1028 return -EINVAL;
1029 dirent = buf->previous;
1030
1031 if (dirent) {
1032 if (__put_user_unaligned(offset, &dirent->d_off))
1033 goto efault;
1034 }
1035 dirent = buf->current_dir;
1036 if (__put_user_unaligned(ino, &dirent->d_ino))
1037 goto efault;
1038 off = 0;
1039 if (__put_user_unaligned(off, &dirent->d_off))
1040 goto efault;
1041 if (__put_user(reclen, &dirent->d_reclen))
1042 goto efault;
1043 if (__put_user(d_type, &dirent->d_type))
1044 goto efault;
1045 if (copy_to_user(dirent->d_name, name, namlen))
1046 goto efault;
1047 if (__put_user(0, dirent->d_name + namlen))
1048 goto efault;
1049 buf->previous = dirent;
1050 dirent = (void __user *)dirent + reclen;
1051 buf->current_dir = dirent;
1052 buf->count -= reclen;
1053 return 0;
1054efault:
1055 buf->error = -EFAULT;
1056 return -EFAULT;
1057}
1058
1059asmlinkage long compat_sys_getdents64(unsigned int fd,
1060 struct linux_dirent64 __user * dirent, unsigned int count)
1061{
1062 struct file * file;
1063 struct linux_dirent64 __user * lastdirent;
1064 struct compat_getdents_callback64 buf;
1065 int error;
1066
1067 error = -EFAULT;
1068 if (!access_ok(VERIFY_WRITE, dirent, count))
1069 goto out;
1070
1071 error = -EBADF;
1072 file = fget(fd);
1073 if (!file)
1074 goto out;
1075
1076 buf.current_dir = dirent;
1077 buf.previous = NULL;
1078 buf.count = count;
1079 buf.error = 0;
1080
1081 error = vfs_readdir(file, compat_filldir64, &buf);
1082 if (error < 0)
1083 goto out_putf;
1084 error = buf.error;
1085 lastdirent = buf.previous;
1086 if (lastdirent) {
1087 typeof(lastdirent->d_off) d_off = file->f_pos;
1088 __put_user_unaligned(d_off, &lastdirent->d_off);
1089 error = count - buf.count;
1090 }
1091
1092out_putf:
1093 fput(file);
1094out:
1095 return error;
1096}
1097#endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
1098
1099static ssize_t compat_do_readv_writev(int type, struct file *file,
1100 const struct compat_iovec __user *uvector,
1101 unsigned long nr_segs, loff_t *pos)
1102{
1103 typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
1104 typedef ssize_t (*iov_fn_t)(struct file *, const struct iovec *, unsigned long, loff_t *);
1105
1106 compat_ssize_t tot_len;
1107 struct iovec iovstack[UIO_FASTIOV];
1108 struct iovec *iov=iovstack, *vector;
1109 ssize_t ret;
1110 int seg;
1111 io_fn_t fn;
1112 iov_fn_t fnv;
1113
1114 /*
1115 * SuS says "The readv() function *may* fail if the iovcnt argument
1116 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
1117 * traditionally returned zero for zero segments, so...
1118 */
1119 ret = 0;
1120 if (nr_segs == 0)
1121 goto out;
1122
1123 /*
1124 * First get the "struct iovec" from user memory and
1125 * verify all the pointers
1126 */
1127 ret = -EINVAL;
1128 if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0))
1129 goto out;
1130 if (!file->f_op)
1131 goto out;
1132 if (nr_segs > UIO_FASTIOV) {
1133 ret = -ENOMEM;
1134 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
1135 if (!iov)
1136 goto out;
1137 }
1138 ret = -EFAULT;
1139 if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
1140 goto out;
1141
1142 /*
1143 * Single unix specification:
1144 * We should -EINVAL if an element length is not >= 0 and fitting an
1145 * ssize_t. The total length is fitting an ssize_t
1146 *
1147 * Be careful here because iov_len is a size_t not an ssize_t
1148 */
1149 tot_len = 0;
1150 vector = iov;
1151 ret = -EINVAL;
1152 for (seg = 0 ; seg < nr_segs; seg++) {
1153 compat_ssize_t tmp = tot_len;
1154 compat_ssize_t len;
1155 compat_uptr_t buf;
1156
1157 if (__get_user(len, &uvector->iov_len) ||
1158 __get_user(buf, &uvector->iov_base)) {
1159 ret = -EFAULT;
1160 goto out;
1161 }
1162 if (len < 0) /* size_t not fitting an compat_ssize_t .. */
1163 goto out;
1164 tot_len += len;
1165 if (tot_len < tmp) /* maths overflow on the compat_ssize_t */
1166 goto out;
1167 vector->iov_base = compat_ptr(buf);
1168 vector->iov_len = (compat_size_t) len;
1169 uvector++;
1170 vector++;
1171 }
1172 if (tot_len == 0) {
1173 ret = 0;
1174 goto out;
1175 }
1176
1177 ret = rw_verify_area(type, file, pos, tot_len);
1178 if (ret)
1179 goto out;
1180
1181 fnv = NULL;
1182 if (type == READ) {
1183 fn = file->f_op->read;
1184 fnv = file->f_op->readv;
1185 } else {
1186 fn = (io_fn_t)file->f_op->write;
1187 fnv = file->f_op->writev;
1188 }
1189 if (fnv) {
1190 ret = fnv(file, iov, nr_segs, pos);
1191 goto out;
1192 }
1193
1194 /* Do it by hand, with file-ops */
1195 ret = 0;
1196 vector = iov;
1197 while (nr_segs > 0) {
1198 void __user * base;
1199 size_t len;
1200 ssize_t nr;
1201
1202 base = vector->iov_base;
1203 len = vector->iov_len;
1204 vector++;
1205 nr_segs--;
1206
1207 nr = fn(file, base, len, pos);
1208
1209 if (nr < 0) {
1210 if (!ret) ret = nr;
1211 break;
1212 }
1213 ret += nr;
1214 if (nr != len)
1215 break;
1216 }
1217out:
1218 if (iov != iovstack)
1219 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -04001220 if ((ret + (type == READ)) > 0) {
1221 struct dentry *dentry = file->f_dentry;
1222 if (type == READ)
1223 fsnotify_access(dentry);
1224 else
1225 fsnotify_modify(dentry);
1226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return ret;
1228}
1229
1230asmlinkage ssize_t
1231compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1232{
1233 struct file *file;
1234 ssize_t ret = -EBADF;
1235
1236 file = fget(fd);
1237 if (!file)
1238 return -EBADF;
1239
1240 if (!(file->f_mode & FMODE_READ))
1241 goto out;
1242
1243 ret = -EINVAL;
1244 if (!file->f_op || (!file->f_op->readv && !file->f_op->read))
1245 goto out;
1246
1247 ret = compat_do_readv_writev(READ, file, vec, vlen, &file->f_pos);
1248
1249out:
1250 fput(file);
1251 return ret;
1252}
1253
1254asmlinkage ssize_t
1255compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1256{
1257 struct file *file;
1258 ssize_t ret = -EBADF;
1259
1260 file = fget(fd);
1261 if (!file)
1262 return -EBADF;
1263 if (!(file->f_mode & FMODE_WRITE))
1264 goto out;
1265
1266 ret = -EINVAL;
1267 if (!file->f_op || (!file->f_op->writev && !file->f_op->write))
1268 goto out;
1269
1270 ret = compat_do_readv_writev(WRITE, file, vec, vlen, &file->f_pos);
1271
1272out:
1273 fput(file);
1274 return ret;
1275}
1276
1277/*
Miklos Szeredie922efc2005-09-06 15:18:25 -07001278 * Exactly like fs/open.c:sys_open(), except that it doesn't set the
1279 * O_LARGEFILE flag.
1280 */
1281asmlinkage long
1282compat_sys_open(const char __user *filename, int flags, int mode)
1283{
1284 return do_sys_open(filename, flags, mode);
1285}
1286
1287/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 * compat_count() counts the number of arguments/envelopes. It is basically
1289 * a copy of count() from fs/exec.c, except that it works with 32 bit argv
1290 * and envp pointers.
1291 */
1292static int compat_count(compat_uptr_t __user *argv, int max)
1293{
1294 int i = 0;
1295
1296 if (argv != NULL) {
1297 for (;;) {
1298 compat_uptr_t p;
1299
1300 if (get_user(p, argv))
1301 return -EFAULT;
1302 if (!p)
1303 break;
1304 argv++;
1305 if(++i > max)
1306 return -E2BIG;
1307 }
1308 }
1309 return i;
1310}
1311
1312/*
1313 * compat_copy_strings() is basically a copy of copy_strings() from fs/exec.c
1314 * except that it works with 32 bit argv and envp pointers.
1315 */
1316static int compat_copy_strings(int argc, compat_uptr_t __user *argv,
1317 struct linux_binprm *bprm)
1318{
1319 struct page *kmapped_page = NULL;
1320 char *kaddr = NULL;
1321 int ret;
1322
1323 while (argc-- > 0) {
1324 compat_uptr_t str;
1325 int len;
1326 unsigned long pos;
1327
1328 if (get_user(str, argv+argc) ||
1329 !(len = strnlen_user(compat_ptr(str), bprm->p))) {
1330 ret = -EFAULT;
1331 goto out;
1332 }
1333
1334 if (bprm->p < len) {
1335 ret = -E2BIG;
1336 goto out;
1337 }
1338
1339 bprm->p -= len;
1340 /* XXX: add architecture specific overflow check here. */
1341 pos = bprm->p;
1342
1343 while (len > 0) {
1344 int i, new, err;
1345 int offset, bytes_to_copy;
1346 struct page *page;
1347
1348 offset = pos % PAGE_SIZE;
1349 i = pos/PAGE_SIZE;
1350 page = bprm->page[i];
1351 new = 0;
1352 if (!page) {
1353 page = alloc_page(GFP_HIGHUSER);
1354 bprm->page[i] = page;
1355 if (!page) {
1356 ret = -ENOMEM;
1357 goto out;
1358 }
1359 new = 1;
1360 }
1361
1362 if (page != kmapped_page) {
1363 if (kmapped_page)
1364 kunmap(kmapped_page);
1365 kmapped_page = page;
1366 kaddr = kmap(kmapped_page);
1367 }
1368 if (new && offset)
1369 memset(kaddr, 0, offset);
1370 bytes_to_copy = PAGE_SIZE - offset;
1371 if (bytes_to_copy > len) {
1372 bytes_to_copy = len;
1373 if (new)
1374 memset(kaddr+offset+len, 0,
1375 PAGE_SIZE-offset-len);
1376 }
1377 err = copy_from_user(kaddr+offset, compat_ptr(str),
1378 bytes_to_copy);
1379 if (err) {
1380 ret = -EFAULT;
1381 goto out;
1382 }
1383
1384 pos += bytes_to_copy;
1385 str += bytes_to_copy;
1386 len -= bytes_to_copy;
1387 }
1388 }
1389 ret = 0;
1390out:
1391 if (kmapped_page)
1392 kunmap(kmapped_page);
1393 return ret;
1394}
1395
1396#ifdef CONFIG_MMU
1397
1398#define free_arg_pages(bprm) do { } while (0)
1399
1400#else
1401
1402static inline void free_arg_pages(struct linux_binprm *bprm)
1403{
1404 int i;
1405
1406 for (i = 0; i < MAX_ARG_PAGES; i++) {
1407 if (bprm->page[i])
1408 __free_page(bprm->page[i]);
1409 bprm->page[i] = NULL;
1410 }
1411}
1412
1413#endif /* CONFIG_MMU */
1414
1415/*
1416 * compat_do_execve() is mostly a copy of do_execve(), with the exception
1417 * that it processes 32 bit argv and envp pointers.
1418 */
1419int compat_do_execve(char * filename,
1420 compat_uptr_t __user *argv,
1421 compat_uptr_t __user *envp,
1422 struct pt_regs * regs)
1423{
1424 struct linux_binprm *bprm;
1425 struct file *file;
1426 int retval;
1427 int i;
1428
1429 retval = -ENOMEM;
1430 bprm = kmalloc(sizeof(*bprm), GFP_KERNEL);
1431 if (!bprm)
1432 goto out_ret;
1433 memset(bprm, 0, sizeof(*bprm));
1434
1435 file = open_exec(filename);
1436 retval = PTR_ERR(file);
1437 if (IS_ERR(file))
1438 goto out_kfree;
1439
1440 sched_exec();
1441
1442 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1443 bprm->file = file;
1444 bprm->filename = filename;
1445 bprm->interp = filename;
1446 bprm->mm = mm_alloc();
1447 retval = -ENOMEM;
1448 if (!bprm->mm)
1449 goto out_file;
1450
1451 retval = init_new_context(current, bprm->mm);
1452 if (retval < 0)
1453 goto out_mm;
1454
1455 bprm->argc = compat_count(argv, bprm->p / sizeof(compat_uptr_t));
1456 if ((retval = bprm->argc) < 0)
1457 goto out_mm;
1458
1459 bprm->envc = compat_count(envp, bprm->p / sizeof(compat_uptr_t));
1460 if ((retval = bprm->envc) < 0)
1461 goto out_mm;
1462
1463 retval = security_bprm_alloc(bprm);
1464 if (retval)
1465 goto out;
1466
1467 retval = prepare_binprm(bprm);
1468 if (retval < 0)
1469 goto out;
1470
1471 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1472 if (retval < 0)
1473 goto out;
1474
1475 bprm->exec = bprm->p;
1476 retval = compat_copy_strings(bprm->envc, envp, bprm);
1477 if (retval < 0)
1478 goto out;
1479
1480 retval = compat_copy_strings(bprm->argc, argv, bprm);
1481 if (retval < 0)
1482 goto out;
1483
1484 retval = search_binary_handler(bprm, regs);
1485 if (retval >= 0) {
1486 free_arg_pages(bprm);
1487
1488 /* execve success */
1489 security_bprm_free(bprm);
1490 kfree(bprm);
1491 return retval;
1492 }
1493
1494out:
1495 /* Something went wrong, return the inode and free the argument pages*/
1496 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1497 struct page * page = bprm->page[i];
1498 if (page)
1499 __free_page(page);
1500 }
1501
1502 if (bprm->security)
1503 security_bprm_free(bprm);
1504
1505out_mm:
1506 if (bprm->mm)
1507 mmdrop(bprm->mm);
1508
1509out_file:
1510 if (bprm->file) {
1511 allow_write_access(bprm->file);
1512 fput(bprm->file);
1513 }
1514
1515out_kfree:
1516 kfree(bprm);
1517
1518out_ret:
1519 return retval;
1520}
1521
1522#define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
1523
1524#define ROUND_UP(x,y) (((x)+(y)-1)/(y))
1525
1526/*
1527 * Ooo, nasty. We need here to frob 32-bit unsigned longs to
1528 * 64-bit unsigned longs.
1529 */
1530static inline
1531int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1532 unsigned long *fdset)
1533{
1534 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1535 if (ufdset) {
1536 unsigned long odd;
1537
1538 if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
1539 return -EFAULT;
1540
1541 odd = nr & 1UL;
1542 nr &= ~1UL;
1543 while (nr) {
1544 unsigned long h, l;
1545 __get_user(l, ufdset);
1546 __get_user(h, ufdset+1);
1547 ufdset += 2;
1548 *fdset++ = h << 32 | l;
1549 nr -= 2;
1550 }
1551 if (odd)
1552 __get_user(*fdset, ufdset);
1553 } else {
1554 /* Tricky, must clear full unsigned long in the
1555 * kernel fdset at the end, this makes sure that
1556 * actually happens.
1557 */
1558 memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
1559 }
1560 return 0;
1561}
1562
1563static inline
1564void compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1565 unsigned long *fdset)
1566{
1567 unsigned long odd;
1568 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1569
1570 if (!ufdset)
1571 return;
1572
1573 odd = nr & 1UL;
1574 nr &= ~1UL;
1575 while (nr) {
1576 unsigned long h, l;
1577 l = *fdset++;
1578 h = l >> 32;
1579 __put_user(l, ufdset);
1580 __put_user(h, ufdset+1);
1581 ufdset += 2;
1582 nr -= 2;
1583 }
1584 if (odd)
1585 __put_user(*fdset, ufdset);
1586}
1587
1588
1589/*
1590 * This is a virtual copy of sys_select from fs/select.c and probably
1591 * should be compared to it from time to time
1592 */
1593static void *select_bits_alloc(int size)
1594{
1595 return kmalloc(6 * size, GFP_KERNEL);
1596}
1597
1598static void select_bits_free(void *bits, int size)
1599{
1600 kfree(bits);
1601}
1602
1603/*
1604 * We can actually return ERESTARTSYS instead of EINTR, but I'd
1605 * like to be certain this leads to no problems. So I return
1606 * EINTR just for safety.
1607 *
1608 * Update: ERESTARTSYS breaks at least the xview clock binary, so
1609 * I'm trying ERESTARTNOHAND which restart only when you want to.
1610 */
1611#define MAX_SELECT_SECONDS \
1612 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
1613
1614asmlinkage long
1615compat_sys_select(int n, compat_ulong_t __user *inp, compat_ulong_t __user *outp,
1616 compat_ulong_t __user *exp, struct compat_timeval __user *tvp)
1617{
1618 fd_set_bits fds;
1619 char *bits;
1620 long timeout;
1621 int size, max_fdset, ret = -EINVAL;
Linus Torvaldsa4531ed2005-09-09 15:10:52 -07001622 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 timeout = MAX_SCHEDULE_TIMEOUT;
1625 if (tvp) {
1626 time_t sec, usec;
1627
1628 if (!access_ok(VERIFY_READ, tvp, sizeof(*tvp))
1629 || __get_user(sec, &tvp->tv_sec)
1630 || __get_user(usec, &tvp->tv_usec)) {
1631 ret = -EFAULT;
1632 goto out_nofds;
1633 }
1634
1635 if (sec < 0 || usec < 0)
1636 goto out_nofds;
1637
1638 if ((unsigned long) sec < MAX_SELECT_SECONDS) {
1639 timeout = ROUND_UP(usec, 1000000/HZ);
1640 timeout += sec * (unsigned long) HZ;
1641 }
1642 }
1643
1644 if (n < 0)
1645 goto out_nofds;
1646
1647 /* max_fdset can increase, so grab it once to avoid race */
Linus Torvaldsa4531ed2005-09-09 15:10:52 -07001648 fdt = files_fdtable(current->files);
1649 max_fdset = fdt->max_fdset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 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;
Stephen Rothwell202e5972005-09-06 15:16:40 -07001733 __compat_uid_t ex32_anon_uid;
1734 __compat_gid_t ex32_anon_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735};
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