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