blob: 8d0a0018a7d2cfc934934a992e98bcfabf45755c [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>
Jay Lan8f0ab512006-09-30 23:28:59 -070047#include <linux/tsacct_kern.h>
David S. Miller4a805e82005-09-14 21:40:00 -070048#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>
David Howells07f3f052006-09-30 20:52:18 +020055#include "internal.h"
David Woodhouse9f729492006-01-18 17:44:05 -080056
Andi Kleenbebfa102006-06-26 13:56:52 +020057int compat_log = 1;
58
59int compat_printk(const char *fmt, ...)
60{
61 va_list ap;
62 int ret;
63 if (!compat_log)
64 return 0;
65 va_start(ap, fmt);
66 ret = vprintk(fmt, ap);
67 va_end(ap);
68 return ret;
69}
70
Badari Pulavartyee0b3e62006-09-30 23:28:47 -070071#include "read_write.h"
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/*
74 * Not all architectures have sys_utime, so implement this in terms
75 * of sys_utimes.
76 */
77asmlinkage long compat_sys_utime(char __user *filename, struct compat_utimbuf __user *t)
78{
79 struct timeval tv[2];
80
81 if (t) {
82 if (get_user(tv[0].tv_sec, &t->actime) ||
83 get_user(tv[1].tv_sec, &t->modtime))
84 return -EFAULT;
85 tv[0].tv_usec = 0;
86 tv[1].tv_usec = 0;
87 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -080088 return do_utimes(AT_FDCWD, filename, t ? tv : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +110091asmlinkage long compat_sys_futimesat(unsigned int dfd, char __user *filename, struct compat_timeval __user *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 struct timeval tv[2];
94
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +110095 if (t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
97 get_user(tv[0].tv_usec, &t[0].tv_usec) ||
98 get_user(tv[1].tv_sec, &t[1].tv_sec) ||
99 get_user(tv[1].tv_usec, &t[1].tv_usec))
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +1100100 return -EFAULT;
101 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800102 return do_utimes(dfd, filename, t ? tv : NULL);
103}
104
105asmlinkage long compat_sys_utimes(char __user *filename, struct compat_timeval __user *t)
106{
107 return compat_sys_futimesat(AT_FDCWD, filename, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
110asmlinkage long compat_sys_newstat(char __user * filename,
111 struct compat_stat __user *statbuf)
112{
113 struct kstat stat;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800114 int error = vfs_stat_fd(AT_FDCWD, filename, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 if (!error)
117 error = cp_compat_stat(&stat, statbuf);
118 return error;
119}
120
121asmlinkage long compat_sys_newlstat(char __user * filename,
122 struct compat_stat __user *statbuf)
123{
124 struct kstat stat;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800125 int error = vfs_lstat_fd(AT_FDCWD, filename, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 if (!error)
128 error = cp_compat_stat(&stat, statbuf);
129 return error;
130}
131
Kyle McMartin82d821d2006-03-24 03:18:20 -0800132#ifndef __ARCH_WANT_STAT64
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +1100133asmlinkage long compat_sys_newfstatat(unsigned int dfd, char __user *filename,
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800134 struct compat_stat __user *statbuf, int flag)
135{
136 struct kstat stat;
137 int error = -EINVAL;
138
139 if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
140 goto out;
141
142 if (flag & AT_SYMLINK_NOFOLLOW)
143 error = vfs_lstat_fd(dfd, filename, &stat);
144 else
145 error = vfs_stat_fd(dfd, filename, &stat);
146
147 if (!error)
148 error = cp_compat_stat(&stat, statbuf);
149
150out:
151 return error;
152}
Kyle McMartin82d821d2006-03-24 03:18:20 -0800153#endif
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155asmlinkage long compat_sys_newfstat(unsigned int fd,
156 struct compat_stat __user * statbuf)
157{
158 struct kstat stat;
159 int error = vfs_fstat(fd, &stat);
160
161 if (!error)
162 error = cp_compat_stat(&stat, statbuf);
163 return error;
164}
165
166static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
167{
168
169 if (sizeof ubuf->f_blocks == 4) {
170 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) &
171 0xffffffff00000000ULL)
172 return -EOVERFLOW;
173 /* f_files and f_ffree may be -1; it's okay
174 * to stuff that into 32 bits */
175 if (kbuf->f_files != 0xffffffffffffffffULL
176 && (kbuf->f_files & 0xffffffff00000000ULL))
177 return -EOVERFLOW;
178 if (kbuf->f_ffree != 0xffffffffffffffffULL
179 && (kbuf->f_ffree & 0xffffffff00000000ULL))
180 return -EOVERFLOW;
181 }
182 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
183 __put_user(kbuf->f_type, &ubuf->f_type) ||
184 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
185 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
186 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
187 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
188 __put_user(kbuf->f_files, &ubuf->f_files) ||
189 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
190 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
191 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
192 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
193 __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
194 __put_user(0, &ubuf->f_spare[0]) ||
195 __put_user(0, &ubuf->f_spare[1]) ||
196 __put_user(0, &ubuf->f_spare[2]) ||
197 __put_user(0, &ubuf->f_spare[3]) ||
198 __put_user(0, &ubuf->f_spare[4]))
199 return -EFAULT;
200 return 0;
201}
202
203/*
204 * The following statfs calls are copies of code from fs/open.c and
205 * should be checked against those from time to time
206 */
207asmlinkage long compat_sys_statfs(const char __user *path, struct compat_statfs __user *buf)
208{
209 struct nameidata nd;
210 int error;
211
212 error = user_path_walk(path, &nd);
213 if (!error) {
214 struct kstatfs tmp;
David Howells726c3342006-06-23 02:02:58 -0700215 error = vfs_statfs(nd.dentry, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800216 if (!error)
217 error = put_compat_statfs(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 path_release(&nd);
219 }
220 return error;
221}
222
223asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf)
224{
225 struct file * file;
226 struct kstatfs tmp;
227 int error;
228
229 error = -EBADF;
230 file = fget(fd);
231 if (!file)
232 goto out;
David Howells726c3342006-06-23 02:02:58 -0700233 error = vfs_statfs(file->f_dentry, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800234 if (!error)
235 error = put_compat_statfs(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 fput(file);
237out:
238 return error;
239}
240
241static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
242{
243 if (sizeof ubuf->f_blocks == 4) {
244 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) &
245 0xffffffff00000000ULL)
246 return -EOVERFLOW;
247 /* f_files and f_ffree may be -1; it's okay
248 * to stuff that into 32 bits */
249 if (kbuf->f_files != 0xffffffffffffffffULL
250 && (kbuf->f_files & 0xffffffff00000000ULL))
251 return -EOVERFLOW;
252 if (kbuf->f_ffree != 0xffffffffffffffffULL
253 && (kbuf->f_ffree & 0xffffffff00000000ULL))
254 return -EOVERFLOW;
255 }
256 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
257 __put_user(kbuf->f_type, &ubuf->f_type) ||
258 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
259 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
260 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
261 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
262 __put_user(kbuf->f_files, &ubuf->f_files) ||
263 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
264 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
265 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
266 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
267 __put_user(kbuf->f_frsize, &ubuf->f_frsize))
268 return -EFAULT;
269 return 0;
270}
271
272asmlinkage long compat_sys_statfs64(const char __user *path, compat_size_t sz, struct compat_statfs64 __user *buf)
273{
274 struct nameidata nd;
275 int error;
276
277 if (sz != sizeof(*buf))
278 return -EINVAL;
279
280 error = user_path_walk(path, &nd);
281 if (!error) {
282 struct kstatfs tmp;
David Howells726c3342006-06-23 02:02:58 -0700283 error = vfs_statfs(nd.dentry, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800284 if (!error)
285 error = put_compat_statfs64(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 path_release(&nd);
287 }
288 return error;
289}
290
291asmlinkage long compat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf)
292{
293 struct file * file;
294 struct kstatfs tmp;
295 int error;
296
297 if (sz != sizeof(*buf))
298 return -EINVAL;
299
300 error = -EBADF;
301 file = fget(fd);
302 if (!file)
303 goto out;
David Howells726c3342006-06-23 02:02:58 -0700304 error = vfs_statfs(file->f_dentry, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800305 if (!error)
306 error = put_compat_statfs64(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 fput(file);
308out:
309 return error;
310}
311
312/* ioctl32 stuff, used by sparc64, parisc, s390x, ppc64, x86_64, MIPS */
313
314#define IOCTL_HASHSIZE 256
315static struct ioctl_trans *ioctl32_hash_table[IOCTL_HASHSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317static inline unsigned long ioctl32_hash(unsigned long cmd)
318{
319 return (((cmd >> 6) ^ (cmd >> 4) ^ cmd)) % IOCTL_HASHSIZE;
320}
321
322static void ioctl32_insert_translation(struct ioctl_trans *trans)
323{
324 unsigned long hash;
325 struct ioctl_trans *t;
326
327 hash = ioctl32_hash (trans->cmd);
328 if (!ioctl32_hash_table[hash])
329 ioctl32_hash_table[hash] = trans;
330 else {
331 t = ioctl32_hash_table[hash];
332 while (t->next)
333 t = t->next;
334 trans->next = NULL;
335 t->next = trans;
336 }
337}
338
339static int __init init_sys32_ioctl(void)
340{
341 int i;
342
343 for (i = 0; i < ioctl_table_size; i++) {
344 if (ioctl_start[i].next != 0) {
345 printk("ioctl translation %d bad\n",i);
346 return -1;
347 }
348
349 ioctl32_insert_translation(&ioctl_start[i]);
350 }
351 return 0;
352}
353
354__initcall(init_sys32_ioctl);
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356static void compat_ioctl_error(struct file *filp, unsigned int fd,
357 unsigned int cmd, unsigned long arg)
358{
359 char buf[10];
360 char *fn = "?";
361 char *path;
362
363 /* find the name of the device. */
364 path = (char *)__get_free_page(GFP_KERNEL);
365 if (path) {
366 fn = d_path(filp->f_dentry, filp->f_vfsmnt, path, PAGE_SIZE);
367 if (IS_ERR(fn))
368 fn = "?";
369 }
370
371 sprintf(buf,"'%c'", (cmd>>24) & 0x3f);
372 if (!isprint(buf[1]))
373 sprintf(buf, "%02x", buf[1]);
Andi Kleenbebfa102006-06-26 13:56:52 +0200374 compat_printk("ioctl32(%s:%d): Unknown cmd fd(%d) "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 "cmd(%08x){%s} arg(%08x) on %s\n",
376 current->comm, current->pid,
377 (int)fd, (unsigned int)cmd, buf,
378 (unsigned int)arg, fn);
379
380 if (path)
381 free_page((unsigned long)path);
382}
383
384asmlinkage long compat_sys_ioctl(unsigned int fd, unsigned int cmd,
385 unsigned long arg)
386{
387 struct file *filp;
388 int error = -EBADF;
389 struct ioctl_trans *t;
390 int fput_needed;
391
392 filp = fget_light(fd, &fput_needed);
393 if (!filp)
394 goto out;
395
396 /* RED-PEN how should LSM module know it's handling 32bit? */
397 error = security_file_ioctl(filp, cmd, arg);
398 if (error)
399 goto out_fput;
400
401 /*
402 * To allow the compat_ioctl handlers to be self contained
403 * we need to check the common ioctls here first.
404 * Just handle them with the standard handlers below.
405 */
406 switch (cmd) {
407 case FIOCLEX:
408 case FIONCLEX:
409 case FIONBIO:
410 case FIOASYNC:
411 case FIOQSIZE:
412 break;
413
414 case FIBMAP:
415 case FIGETBSZ:
416 case FIONREAD:
417 if (S_ISREG(filp->f_dentry->d_inode->i_mode))
418 break;
419 /*FALL THROUGH*/
420
421 default:
422 if (filp->f_op && filp->f_op->compat_ioctl) {
423 error = filp->f_op->compat_ioctl(filp, cmd, arg);
424 if (error != -ENOIOCTLCMD)
425 goto out_fput;
426 }
427
428 if (!filp->f_op ||
429 (!filp->f_op->ioctl && !filp->f_op->unlocked_ioctl))
430 goto do_ioctl;
431 break;
432 }
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 for (t = ioctl32_hash_table[ioctl32_hash(cmd)]; t; t = t->next) {
435 if (t->cmd == cmd)
436 goto found_handler;
437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 if (S_ISSOCK(filp->f_dentry->d_inode->i_mode) &&
440 cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
441 error = siocdevprivate_ioctl(fd, cmd, arg);
442 } else {
443 static int count;
444
445 if (++count <= 50)
446 compat_ioctl_error(filp, fd, cmd, arg);
447 error = -EINVAL;
448 }
449
450 goto out_fput;
451
452 found_handler:
453 if (t->handler) {
454 lock_kernel();
455 error = t->handler(fd, cmd, arg, filp);
456 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 goto out_fput;
458 }
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 do_ioctl:
461 error = vfs_ioctl(filp, fd, cmd, arg);
462 out_fput:
463 fput_light(filp, fput_needed);
464 out:
465 return error;
466}
467
468static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
469{
470 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
471 __get_user(kfl->l_type, &ufl->l_type) ||
472 __get_user(kfl->l_whence, &ufl->l_whence) ||
473 __get_user(kfl->l_start, &ufl->l_start) ||
474 __get_user(kfl->l_len, &ufl->l_len) ||
475 __get_user(kfl->l_pid, &ufl->l_pid))
476 return -EFAULT;
477 return 0;
478}
479
480static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
481{
482 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
483 __put_user(kfl->l_type, &ufl->l_type) ||
484 __put_user(kfl->l_whence, &ufl->l_whence) ||
485 __put_user(kfl->l_start, &ufl->l_start) ||
486 __put_user(kfl->l_len, &ufl->l_len) ||
487 __put_user(kfl->l_pid, &ufl->l_pid))
488 return -EFAULT;
489 return 0;
490}
491
492#ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
493static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
494{
495 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
496 __get_user(kfl->l_type, &ufl->l_type) ||
497 __get_user(kfl->l_whence, &ufl->l_whence) ||
498 __get_user(kfl->l_start, &ufl->l_start) ||
499 __get_user(kfl->l_len, &ufl->l_len) ||
500 __get_user(kfl->l_pid, &ufl->l_pid))
501 return -EFAULT;
502 return 0;
503}
504#endif
505
506#ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
507static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
508{
509 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
510 __put_user(kfl->l_type, &ufl->l_type) ||
511 __put_user(kfl->l_whence, &ufl->l_whence) ||
512 __put_user(kfl->l_start, &ufl->l_start) ||
513 __put_user(kfl->l_len, &ufl->l_len) ||
514 __put_user(kfl->l_pid, &ufl->l_pid))
515 return -EFAULT;
516 return 0;
517}
518#endif
519
520asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd,
521 unsigned long arg)
522{
523 mm_segment_t old_fs;
524 struct flock f;
525 long ret;
526
527 switch (cmd) {
528 case F_GETLK:
529 case F_SETLK:
530 case F_SETLKW:
531 ret = get_compat_flock(&f, compat_ptr(arg));
532 if (ret != 0)
533 break;
534 old_fs = get_fs();
535 set_fs(KERNEL_DS);
536 ret = sys_fcntl(fd, cmd, (unsigned long)&f);
537 set_fs(old_fs);
538 if (cmd == F_GETLK && ret == 0) {
NeilBrown2520f142006-01-08 01:02:40 -0800539 /* GETLK was successfule and we need to return the data...
540 * but it needs to fit in the compat structure.
541 * l_start shouldn't be too big, unless the original
542 * start + end is greater than COMPAT_OFF_T_MAX, in which
543 * case the app was asking for trouble, so we return
544 * -EOVERFLOW in that case.
545 * l_len could be too big, in which case we just truncate it,
546 * and only allow the app to see that part of the conflicting
547 * lock that might make sense to it anyway
548 */
549
550 if (f.l_start > COMPAT_OFF_T_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 ret = -EOVERFLOW;
NeilBrown2520f142006-01-08 01:02:40 -0800552 if (f.l_len > COMPAT_OFF_T_MAX)
553 f.l_len = COMPAT_OFF_T_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (ret == 0)
555 ret = put_compat_flock(&f, compat_ptr(arg));
556 }
557 break;
558
559 case F_GETLK64:
560 case F_SETLK64:
561 case F_SETLKW64:
562 ret = get_compat_flock64(&f, compat_ptr(arg));
563 if (ret != 0)
564 break;
565 old_fs = get_fs();
566 set_fs(KERNEL_DS);
567 ret = sys_fcntl(fd, (cmd == F_GETLK64) ? F_GETLK :
568 ((cmd == F_SETLK64) ? F_SETLK : F_SETLKW),
569 (unsigned long)&f);
570 set_fs(old_fs);
571 if (cmd == F_GETLK64 && ret == 0) {
NeilBrown2520f142006-01-08 01:02:40 -0800572 /* need to return lock information - see above for commentary */
573 if (f.l_start > COMPAT_LOFF_T_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 ret = -EOVERFLOW;
NeilBrown2520f142006-01-08 01:02:40 -0800575 if (f.l_len > COMPAT_LOFF_T_MAX)
576 f.l_len = COMPAT_LOFF_T_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (ret == 0)
578 ret = put_compat_flock64(&f, compat_ptr(arg));
579 }
580 break;
581
582 default:
583 ret = sys_fcntl(fd, cmd, arg);
584 break;
585 }
586 return ret;
587}
588
589asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd,
590 unsigned long arg)
591{
592 if ((cmd == F_GETLK64) || (cmd == F_SETLK64) || (cmd == F_SETLKW64))
593 return -EINVAL;
594 return compat_sys_fcntl64(fd, cmd, arg);
595}
596
597asmlinkage long
598compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p)
599{
600 long ret;
601 aio_context_t ctx64;
602
603 mm_segment_t oldfs = get_fs();
604 if (unlikely(get_user(ctx64, ctx32p)))
605 return -EFAULT;
606
607 set_fs(KERNEL_DS);
608 /* The __user pointer cast is valid because of the set_fs() */
609 ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64);
610 set_fs(oldfs);
611 /* truncating is ok because it's a user address */
612 if (!ret)
613 ret = put_user((u32) ctx64, ctx32p);
614 return ret;
615}
616
617asmlinkage long
618compat_sys_io_getevents(aio_context_t ctx_id,
619 unsigned long min_nr,
620 unsigned long nr,
621 struct io_event __user *events,
622 struct compat_timespec __user *timeout)
623{
624 long ret;
625 struct timespec t;
626 struct timespec __user *ut = NULL;
627
628 ret = -EFAULT;
629 if (unlikely(!access_ok(VERIFY_WRITE, events,
630 nr * sizeof(struct io_event))))
631 goto out;
632 if (timeout) {
633 if (get_compat_timespec(&t, timeout))
634 goto out;
635
636 ut = compat_alloc_user_space(sizeof(*ut));
637 if (copy_to_user(ut, &t, sizeof(t)) )
638 goto out;
639 }
640 ret = sys_io_getevents(ctx_id, min_nr, nr, events, ut);
641out:
642 return ret;
643}
644
645static inline long
646copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64)
647{
648 compat_uptr_t uptr;
649 int i;
650
651 for (i = 0; i < nr; ++i) {
652 if (get_user(uptr, ptr32 + i))
653 return -EFAULT;
654 if (put_user(compat_ptr(uptr), ptr64 + i))
655 return -EFAULT;
656 }
657 return 0;
658}
659
660#define MAX_AIO_SUBMITS (PAGE_SIZE/sizeof(struct iocb *))
661
662asmlinkage long
663compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb)
664{
665 struct iocb __user * __user *iocb64;
666 long ret;
667
668 if (unlikely(nr < 0))
669 return -EINVAL;
670
671 if (nr > MAX_AIO_SUBMITS)
672 nr = MAX_AIO_SUBMITS;
673
674 iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64));
675 ret = copy_iocb(nr, iocb, iocb64);
676 if (!ret)
677 ret = sys_io_submit(ctx_id, nr, iocb64);
678 return ret;
679}
680
681struct compat_ncp_mount_data {
682 compat_int_t version;
683 compat_uint_t ncp_fd;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700684 __compat_uid_t mounted_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 compat_pid_t wdog_pid;
686 unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
687 compat_uint_t time_out;
688 compat_uint_t retry_count;
689 compat_uint_t flags;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700690 __compat_uid_t uid;
691 __compat_gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 compat_mode_t file_mode;
693 compat_mode_t dir_mode;
694};
695
696struct compat_ncp_mount_data_v4 {
697 compat_int_t version;
698 compat_ulong_t flags;
699 compat_ulong_t mounted_uid;
700 compat_long_t wdog_pid;
701 compat_uint_t ncp_fd;
702 compat_uint_t time_out;
703 compat_uint_t retry_count;
704 compat_ulong_t uid;
705 compat_ulong_t gid;
706 compat_ulong_t file_mode;
707 compat_ulong_t dir_mode;
708};
709
710static void *do_ncp_super_data_conv(void *raw_data)
711{
712 int version = *(unsigned int *)raw_data;
713
714 if (version == 3) {
715 struct compat_ncp_mount_data *c_n = raw_data;
716 struct ncp_mount_data *n = raw_data;
717
718 n->dir_mode = c_n->dir_mode;
719 n->file_mode = c_n->file_mode;
720 n->gid = c_n->gid;
721 n->uid = c_n->uid;
722 memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
723 n->wdog_pid = c_n->wdog_pid;
724 n->mounted_uid = c_n->mounted_uid;
725 } else if (version == 4) {
726 struct compat_ncp_mount_data_v4 *c_n = raw_data;
727 struct ncp_mount_data_v4 *n = raw_data;
728
729 n->dir_mode = c_n->dir_mode;
730 n->file_mode = c_n->file_mode;
731 n->gid = c_n->gid;
732 n->uid = c_n->uid;
733 n->retry_count = c_n->retry_count;
734 n->time_out = c_n->time_out;
735 n->ncp_fd = c_n->ncp_fd;
736 n->wdog_pid = c_n->wdog_pid;
737 n->mounted_uid = c_n->mounted_uid;
738 n->flags = c_n->flags;
739 } else if (version != 5) {
740 return NULL;
741 }
742
743 return raw_data;
744}
745
746struct compat_smb_mount_data {
747 compat_int_t version;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700748 __compat_uid_t mounted_uid;
749 __compat_uid_t uid;
750 __compat_gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 compat_mode_t file_mode;
752 compat_mode_t dir_mode;
753};
754
755static void *do_smb_super_data_conv(void *raw_data)
756{
757 struct smb_mount_data *s = raw_data;
758 struct compat_smb_mount_data *c_s = raw_data;
759
760 if (c_s->version != SMB_MOUNT_OLDVERSION)
761 goto out;
762 s->dir_mode = c_s->dir_mode;
763 s->file_mode = c_s->file_mode;
764 s->gid = c_s->gid;
765 s->uid = c_s->uid;
766 s->mounted_uid = c_s->mounted_uid;
767 out:
768 return raw_data;
769}
770
David Howells9a9947b2005-04-18 10:54:51 -0700771struct compat_nfs_string {
772 compat_uint_t len;
David Howells5fc3e622005-04-27 15:39:03 -0700773 compat_uptr_t data;
David Howells9a9947b2005-04-18 10:54:51 -0700774};
775
776static inline void compat_nfs_string(struct nfs_string *dst,
777 struct compat_nfs_string *src)
778{
779 dst->data = compat_ptr(src->data);
780 dst->len = src->len;
781}
782
783struct compat_nfs4_mount_data_v1 {
784 compat_int_t version;
785 compat_int_t flags;
786 compat_int_t rsize;
787 compat_int_t wsize;
788 compat_int_t timeo;
789 compat_int_t retrans;
790 compat_int_t acregmin;
791 compat_int_t acregmax;
792 compat_int_t acdirmin;
793 compat_int_t acdirmax;
794 struct compat_nfs_string client_addr;
795 struct compat_nfs_string mnt_path;
796 struct compat_nfs_string hostname;
797 compat_uint_t host_addrlen;
David Howells5fc3e622005-04-27 15:39:03 -0700798 compat_uptr_t host_addr;
David Howells9a9947b2005-04-18 10:54:51 -0700799 compat_int_t proto;
800 compat_int_t auth_flavourlen;
David Howells5fc3e622005-04-27 15:39:03 -0700801 compat_uptr_t auth_flavours;
David Howells9a9947b2005-04-18 10:54:51 -0700802};
803
804static int do_nfs4_super_data_conv(void *raw_data)
805{
806 int version = *(compat_uint_t *) raw_data;
807
808 if (version == 1) {
809 struct compat_nfs4_mount_data_v1 *raw = raw_data;
810 struct nfs4_mount_data *real = raw_data;
811
812 /* copy the fields backwards */
813 real->auth_flavours = compat_ptr(raw->auth_flavours);
814 real->auth_flavourlen = raw->auth_flavourlen;
815 real->proto = raw->proto;
816 real->host_addr = compat_ptr(raw->host_addr);
817 real->host_addrlen = raw->host_addrlen;
818 compat_nfs_string(&real->hostname, &raw->hostname);
819 compat_nfs_string(&real->mnt_path, &raw->mnt_path);
820 compat_nfs_string(&real->client_addr, &raw->client_addr);
821 real->acdirmax = raw->acdirmax;
822 real->acdirmin = raw->acdirmin;
823 real->acregmax = raw->acregmax;
824 real->acregmin = raw->acregmin;
825 real->retrans = raw->retrans;
826 real->timeo = raw->timeo;
827 real->wsize = raw->wsize;
828 real->rsize = raw->rsize;
829 real->flags = raw->flags;
830 real->version = raw->version;
831 }
832 else {
833 return -EINVAL;
834 }
835
836 return 0;
837}
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839#define SMBFS_NAME "smbfs"
840#define NCPFS_NAME "ncpfs"
David Howells9a9947b2005-04-18 10:54:51 -0700841#define NFS4_NAME "nfs4"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843asmlinkage long compat_sys_mount(char __user * dev_name, char __user * dir_name,
844 char __user * type, unsigned long flags,
845 void __user * data)
846{
847 unsigned long type_page;
848 unsigned long data_page;
849 unsigned long dev_page;
850 char *dir_page;
851 int retval;
852
853 retval = copy_mount_options (type, &type_page);
854 if (retval < 0)
855 goto out;
856
857 dir_page = getname(dir_name);
858 retval = PTR_ERR(dir_page);
859 if (IS_ERR(dir_page))
860 goto out1;
861
862 retval = copy_mount_options (dev_name, &dev_page);
863 if (retval < 0)
864 goto out2;
865
866 retval = copy_mount_options (data, &data_page);
867 if (retval < 0)
868 goto out3;
869
870 retval = -EINVAL;
871
872 if (type_page) {
873 if (!strcmp((char *)type_page, SMBFS_NAME)) {
874 do_smb_super_data_conv((void *)data_page);
875 } else if (!strcmp((char *)type_page, NCPFS_NAME)) {
876 do_ncp_super_data_conv((void *)data_page);
David Howells9a9947b2005-04-18 10:54:51 -0700877 } else if (!strcmp((char *)type_page, NFS4_NAME)) {
878 if (do_nfs4_super_data_conv((void *) data_page))
879 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881 }
882
883 lock_kernel();
884 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
885 flags, (void*)data_page);
886 unlock_kernel();
887
David Howells9a9947b2005-04-18 10:54:51 -0700888 out4:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 free_page(data_page);
890 out3:
891 free_page(dev_page);
892 out2:
893 putname(dir_page);
894 out1:
895 free_page(type_page);
896 out:
897 return retval;
898}
899
900#define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
901#define COMPAT_ROUND_UP(x) (((x)+sizeof(compat_long_t)-1) & \
902 ~(sizeof(compat_long_t)-1))
903
904struct compat_old_linux_dirent {
905 compat_ulong_t d_ino;
906 compat_ulong_t d_offset;
907 unsigned short d_namlen;
908 char d_name[1];
909};
910
911struct compat_readdir_callback {
912 struct compat_old_linux_dirent __user *dirent;
913 int result;
914};
915
916static int compat_fillonedir(void *__buf, const char *name, int namlen,
David Howellsafefdbb2006-10-03 01:13:46 -0700917 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919 struct compat_readdir_callback *buf = __buf;
920 struct compat_old_linux_dirent __user *dirent;
David Howellsafefdbb2006-10-03 01:13:46 -0700921 compat_ulong_t d_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 if (buf->result)
924 return -EINVAL;
David Howellsafefdbb2006-10-03 01:13:46 -0700925 d_ino = ino;
926 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
927 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 buf->result++;
929 dirent = buf->dirent;
930 if (!access_ok(VERIFY_WRITE, dirent,
931 (unsigned long)(dirent->d_name + namlen + 1) -
932 (unsigned long)dirent))
933 goto efault;
David Howellsafefdbb2006-10-03 01:13:46 -0700934 if ( __put_user(d_ino, &dirent->d_ino) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 __put_user(offset, &dirent->d_offset) ||
936 __put_user(namlen, &dirent->d_namlen) ||
937 __copy_to_user(dirent->d_name, name, namlen) ||
938 __put_user(0, dirent->d_name + namlen))
939 goto efault;
940 return 0;
941efault:
942 buf->result = -EFAULT;
943 return -EFAULT;
944}
945
946asmlinkage long compat_sys_old_readdir(unsigned int fd,
947 struct compat_old_linux_dirent __user *dirent, unsigned int count)
948{
949 int error;
950 struct file *file;
951 struct compat_readdir_callback buf;
952
953 error = -EBADF;
954 file = fget(fd);
955 if (!file)
956 goto out;
957
958 buf.result = 0;
959 buf.dirent = dirent;
960
961 error = vfs_readdir(file, compat_fillonedir, &buf);
962 if (error >= 0)
963 error = buf.result;
964
965 fput(file);
966out:
967 return error;
968}
969
970struct compat_linux_dirent {
971 compat_ulong_t d_ino;
972 compat_ulong_t d_off;
973 unsigned short d_reclen;
974 char d_name[1];
975};
976
977struct compat_getdents_callback {
978 struct compat_linux_dirent __user *current_dir;
979 struct compat_linux_dirent __user *previous;
980 int count;
981 int error;
982};
983
984static int compat_filldir(void *__buf, const char *name, int namlen,
David Howellsafefdbb2006-10-03 01:13:46 -0700985 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
987 struct compat_linux_dirent __user * dirent;
988 struct compat_getdents_callback *buf = __buf;
David Howellsafefdbb2006-10-03 01:13:46 -0700989 compat_ulong_t d_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 int reclen = COMPAT_ROUND_UP(NAME_OFFSET(dirent) + namlen + 2);
991
992 buf->error = -EINVAL; /* only used if we fail.. */
993 if (reclen > buf->count)
994 return -EINVAL;
David Howellsafefdbb2006-10-03 01:13:46 -0700995 d_ino = ino;
996 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
997 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 dirent = buf->previous;
999 if (dirent) {
1000 if (__put_user(offset, &dirent->d_off))
1001 goto efault;
1002 }
1003 dirent = buf->current_dir;
David Howellsafefdbb2006-10-03 01:13:46 -07001004 if (__put_user(d_ino, &dirent->d_ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 goto efault;
1006 if (__put_user(reclen, &dirent->d_reclen))
1007 goto efault;
1008 if (copy_to_user(dirent->d_name, name, namlen))
1009 goto efault;
1010 if (__put_user(0, dirent->d_name + namlen))
1011 goto efault;
1012 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
1013 goto efault;
1014 buf->previous = dirent;
1015 dirent = (void __user *)dirent + reclen;
1016 buf->current_dir = dirent;
1017 buf->count -= reclen;
1018 return 0;
1019efault:
1020 buf->error = -EFAULT;
1021 return -EFAULT;
1022}
1023
1024asmlinkage long compat_sys_getdents(unsigned int fd,
1025 struct compat_linux_dirent __user *dirent, unsigned int count)
1026{
1027 struct file * file;
1028 struct compat_linux_dirent __user * lastdirent;
1029 struct compat_getdents_callback buf;
1030 int error;
1031
1032 error = -EFAULT;
1033 if (!access_ok(VERIFY_WRITE, dirent, count))
1034 goto out;
1035
1036 error = -EBADF;
1037 file = fget(fd);
1038 if (!file)
1039 goto out;
1040
1041 buf.current_dir = dirent;
1042 buf.previous = NULL;
1043 buf.count = count;
1044 buf.error = 0;
1045
1046 error = vfs_readdir(file, compat_filldir, &buf);
1047 if (error < 0)
1048 goto out_putf;
1049 error = buf.error;
1050 lastdirent = buf.previous;
1051 if (lastdirent) {
1052 if (put_user(file->f_pos, &lastdirent->d_off))
1053 error = -EFAULT;
1054 else
1055 error = count - buf.count;
1056 }
1057
1058out_putf:
1059 fput(file);
1060out:
1061 return error;
1062}
1063
1064#ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64
1065#define COMPAT_ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1))
1066
1067struct compat_getdents_callback64 {
1068 struct linux_dirent64 __user *current_dir;
1069 struct linux_dirent64 __user *previous;
1070 int count;
1071 int error;
1072};
1073
1074static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset,
David Howellsafefdbb2006-10-03 01:13:46 -07001075 u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
1077 struct linux_dirent64 __user *dirent;
1078 struct compat_getdents_callback64 *buf = __buf;
1079 int jj = NAME_OFFSET(dirent);
1080 int reclen = COMPAT_ROUND_UP64(jj + namlen + 1);
1081 u64 off;
1082
1083 buf->error = -EINVAL; /* only used if we fail.. */
1084 if (reclen > buf->count)
1085 return -EINVAL;
1086 dirent = buf->previous;
1087
1088 if (dirent) {
1089 if (__put_user_unaligned(offset, &dirent->d_off))
1090 goto efault;
1091 }
1092 dirent = buf->current_dir;
1093 if (__put_user_unaligned(ino, &dirent->d_ino))
1094 goto efault;
1095 off = 0;
1096 if (__put_user_unaligned(off, &dirent->d_off))
1097 goto efault;
1098 if (__put_user(reclen, &dirent->d_reclen))
1099 goto efault;
1100 if (__put_user(d_type, &dirent->d_type))
1101 goto efault;
1102 if (copy_to_user(dirent->d_name, name, namlen))
1103 goto efault;
1104 if (__put_user(0, dirent->d_name + namlen))
1105 goto efault;
1106 buf->previous = dirent;
1107 dirent = (void __user *)dirent + reclen;
1108 buf->current_dir = dirent;
1109 buf->count -= reclen;
1110 return 0;
1111efault:
1112 buf->error = -EFAULT;
1113 return -EFAULT;
1114}
1115
1116asmlinkage long compat_sys_getdents64(unsigned int fd,
1117 struct linux_dirent64 __user * dirent, unsigned int count)
1118{
1119 struct file * file;
1120 struct linux_dirent64 __user * lastdirent;
1121 struct compat_getdents_callback64 buf;
1122 int error;
1123
1124 error = -EFAULT;
1125 if (!access_ok(VERIFY_WRITE, dirent, count))
1126 goto out;
1127
1128 error = -EBADF;
1129 file = fget(fd);
1130 if (!file)
1131 goto out;
1132
1133 buf.current_dir = dirent;
1134 buf.previous = NULL;
1135 buf.count = count;
1136 buf.error = 0;
1137
1138 error = vfs_readdir(file, compat_filldir64, &buf);
1139 if (error < 0)
1140 goto out_putf;
1141 error = buf.error;
1142 lastdirent = buf.previous;
1143 if (lastdirent) {
1144 typeof(lastdirent->d_off) d_off = file->f_pos;
1145 __put_user_unaligned(d_off, &lastdirent->d_off);
1146 error = count - buf.count;
1147 }
1148
1149out_putf:
1150 fput(file);
1151out:
1152 return error;
1153}
1154#endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
1155
1156static ssize_t compat_do_readv_writev(int type, struct file *file,
1157 const struct compat_iovec __user *uvector,
1158 unsigned long nr_segs, loff_t *pos)
1159{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 compat_ssize_t tot_len;
1161 struct iovec iovstack[UIO_FASTIOV];
1162 struct iovec *iov=iovstack, *vector;
1163 ssize_t ret;
1164 int seg;
1165 io_fn_t fn;
1166 iov_fn_t fnv;
1167
1168 /*
1169 * SuS says "The readv() function *may* fail if the iovcnt argument
1170 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
1171 * traditionally returned zero for zero segments, so...
1172 */
1173 ret = 0;
1174 if (nr_segs == 0)
1175 goto out;
1176
1177 /*
1178 * First get the "struct iovec" from user memory and
1179 * verify all the pointers
1180 */
1181 ret = -EINVAL;
1182 if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0))
1183 goto out;
1184 if (!file->f_op)
1185 goto out;
1186 if (nr_segs > UIO_FASTIOV) {
1187 ret = -ENOMEM;
1188 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
1189 if (!iov)
1190 goto out;
1191 }
1192 ret = -EFAULT;
1193 if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
1194 goto out;
1195
1196 /*
1197 * Single unix specification:
1198 * We should -EINVAL if an element length is not >= 0 and fitting an
1199 * ssize_t. The total length is fitting an ssize_t
1200 *
1201 * Be careful here because iov_len is a size_t not an ssize_t
1202 */
1203 tot_len = 0;
1204 vector = iov;
1205 ret = -EINVAL;
1206 for (seg = 0 ; seg < nr_segs; seg++) {
1207 compat_ssize_t tmp = tot_len;
1208 compat_ssize_t len;
1209 compat_uptr_t buf;
1210
1211 if (__get_user(len, &uvector->iov_len) ||
1212 __get_user(buf, &uvector->iov_base)) {
1213 ret = -EFAULT;
1214 goto out;
1215 }
1216 if (len < 0) /* size_t not fitting an compat_ssize_t .. */
1217 goto out;
1218 tot_len += len;
1219 if (tot_len < tmp) /* maths overflow on the compat_ssize_t */
1220 goto out;
1221 vector->iov_base = compat_ptr(buf);
1222 vector->iov_len = (compat_size_t) len;
1223 uvector++;
1224 vector++;
1225 }
1226 if (tot_len == 0) {
1227 ret = 0;
1228 goto out;
1229 }
1230
1231 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001232 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 goto out;
1234
James Morrise7edf9c2006-04-26 02:45:03 -04001235 ret = security_file_permission(file, type == READ ? MAY_READ:MAY_WRITE);
1236 if (ret)
1237 goto out;
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 fnv = NULL;
1240 if (type == READ) {
1241 fn = file->f_op->read;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001242 fnv = file->f_op->aio_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 } else {
1244 fn = (io_fn_t)file->f_op->write;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001245 fnv = file->f_op->aio_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001248 if (fnv)
1249 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
1250 pos, fnv);
1251 else
1252 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254out:
1255 if (iov != iovstack)
1256 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -04001257 if ((ret + (type == READ)) > 0) {
1258 struct dentry *dentry = file->f_dentry;
1259 if (type == READ)
1260 fsnotify_access(dentry);
1261 else
1262 fsnotify_modify(dentry);
1263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return ret;
1265}
1266
1267asmlinkage ssize_t
1268compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1269{
1270 struct file *file;
1271 ssize_t ret = -EBADF;
1272
1273 file = fget(fd);
1274 if (!file)
1275 return -EBADF;
1276
1277 if (!(file->f_mode & FMODE_READ))
1278 goto out;
1279
1280 ret = -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001281 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 goto out;
1283
1284 ret = compat_do_readv_writev(READ, file, vec, vlen, &file->f_pos);
1285
1286out:
1287 fput(file);
1288 return ret;
1289}
1290
1291asmlinkage ssize_t
1292compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1293{
1294 struct file *file;
1295 ssize_t ret = -EBADF;
1296
1297 file = fget(fd);
1298 if (!file)
1299 return -EBADF;
1300 if (!(file->f_mode & FMODE_WRITE))
1301 goto out;
1302
1303 ret = -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001304 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 goto out;
1306
1307 ret = compat_do_readv_writev(WRITE, file, vec, vlen, &file->f_pos);
1308
1309out:
1310 fput(file);
1311 return ret;
1312}
1313
Andi Kleend2610202006-05-01 12:15:48 -07001314asmlinkage long
1315compat_sys_vmsplice(int fd, const struct compat_iovec __user *iov32,
1316 unsigned int nr_segs, unsigned int flags)
1317{
1318 unsigned i;
Al Viro90cbad62006-10-10 22:44:17 +01001319 struct iovec __user *iov;
Jens Axboe98232d52006-05-04 09:13:49 +02001320 if (nr_segs > UIO_MAXIOV)
Andi Kleend2610202006-05-01 12:15:48 -07001321 return -EINVAL;
1322 iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
1323 for (i = 0; i < nr_segs; i++) {
1324 struct compat_iovec v;
1325 if (get_user(v.iov_base, &iov32[i].iov_base) ||
1326 get_user(v.iov_len, &iov32[i].iov_len) ||
1327 put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
1328 put_user(v.iov_len, &iov[i].iov_len))
1329 return -EFAULT;
1330 }
1331 return sys_vmsplice(fd, iov, nr_segs, flags);
1332}
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334/*
Miklos Szeredie922efc2005-09-06 15:18:25 -07001335 * Exactly like fs/open.c:sys_open(), except that it doesn't set the
1336 * O_LARGEFILE flag.
1337 */
1338asmlinkage long
1339compat_sys_open(const char __user *filename, int flags, int mode)
1340{
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001341 return do_sys_open(AT_FDCWD, filename, flags, mode);
1342}
1343
1344/*
1345 * Exactly like fs/open.c:sys_openat(), except that it doesn't set the
1346 * O_LARGEFILE flag.
1347 */
1348asmlinkage long
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +11001349compat_sys_openat(unsigned int dfd, const char __user *filename, int flags, int mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001350{
1351 return do_sys_open(dfd, filename, flags, mode);
Miklos Szeredie922efc2005-09-06 15:18:25 -07001352}
1353
1354/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 * compat_count() counts the number of arguments/envelopes. It is basically
1356 * a copy of count() from fs/exec.c, except that it works with 32 bit argv
1357 * and envp pointers.
1358 */
1359static int compat_count(compat_uptr_t __user *argv, int max)
1360{
1361 int i = 0;
1362
1363 if (argv != NULL) {
1364 for (;;) {
1365 compat_uptr_t p;
1366
1367 if (get_user(p, argv))
1368 return -EFAULT;
1369 if (!p)
1370 break;
1371 argv++;
1372 if(++i > max)
1373 return -E2BIG;
1374 }
1375 }
1376 return i;
1377}
1378
1379/*
1380 * compat_copy_strings() is basically a copy of copy_strings() from fs/exec.c
1381 * except that it works with 32 bit argv and envp pointers.
1382 */
1383static int compat_copy_strings(int argc, compat_uptr_t __user *argv,
1384 struct linux_binprm *bprm)
1385{
1386 struct page *kmapped_page = NULL;
1387 char *kaddr = NULL;
1388 int ret;
1389
1390 while (argc-- > 0) {
1391 compat_uptr_t str;
1392 int len;
1393 unsigned long pos;
1394
1395 if (get_user(str, argv+argc) ||
1396 !(len = strnlen_user(compat_ptr(str), bprm->p))) {
1397 ret = -EFAULT;
1398 goto out;
1399 }
1400
1401 if (bprm->p < len) {
1402 ret = -E2BIG;
1403 goto out;
1404 }
1405
1406 bprm->p -= len;
1407 /* XXX: add architecture specific overflow check here. */
1408 pos = bprm->p;
1409
1410 while (len > 0) {
1411 int i, new, err;
1412 int offset, bytes_to_copy;
1413 struct page *page;
1414
1415 offset = pos % PAGE_SIZE;
1416 i = pos/PAGE_SIZE;
1417 page = bprm->page[i];
1418 new = 0;
1419 if (!page) {
1420 page = alloc_page(GFP_HIGHUSER);
1421 bprm->page[i] = page;
1422 if (!page) {
1423 ret = -ENOMEM;
1424 goto out;
1425 }
1426 new = 1;
1427 }
1428
1429 if (page != kmapped_page) {
1430 if (kmapped_page)
1431 kunmap(kmapped_page);
1432 kmapped_page = page;
1433 kaddr = kmap(kmapped_page);
1434 }
1435 if (new && offset)
1436 memset(kaddr, 0, offset);
1437 bytes_to_copy = PAGE_SIZE - offset;
1438 if (bytes_to_copy > len) {
1439 bytes_to_copy = len;
1440 if (new)
1441 memset(kaddr+offset+len, 0,
1442 PAGE_SIZE-offset-len);
1443 }
1444 err = copy_from_user(kaddr+offset, compat_ptr(str),
1445 bytes_to_copy);
1446 if (err) {
1447 ret = -EFAULT;
1448 goto out;
1449 }
1450
1451 pos += bytes_to_copy;
1452 str += bytes_to_copy;
1453 len -= bytes_to_copy;
1454 }
1455 }
1456 ret = 0;
1457out:
1458 if (kmapped_page)
1459 kunmap(kmapped_page);
1460 return ret;
1461}
1462
1463#ifdef CONFIG_MMU
1464
1465#define free_arg_pages(bprm) do { } while (0)
1466
1467#else
1468
1469static inline void free_arg_pages(struct linux_binprm *bprm)
1470{
1471 int i;
1472
1473 for (i = 0; i < MAX_ARG_PAGES; i++) {
1474 if (bprm->page[i])
1475 __free_page(bprm->page[i]);
1476 bprm->page[i] = NULL;
1477 }
1478}
1479
1480#endif /* CONFIG_MMU */
1481
1482/*
1483 * compat_do_execve() is mostly a copy of do_execve(), with the exception
1484 * that it processes 32 bit argv and envp pointers.
1485 */
1486int compat_do_execve(char * filename,
1487 compat_uptr_t __user *argv,
1488 compat_uptr_t __user *envp,
1489 struct pt_regs * regs)
1490{
1491 struct linux_binprm *bprm;
1492 struct file *file;
1493 int retval;
1494 int i;
1495
1496 retval = -ENOMEM;
Oliver Neukum11b0b5a2006-03-25 03:08:13 -08001497 bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 if (!bprm)
1499 goto out_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 file = open_exec(filename);
1502 retval = PTR_ERR(file);
1503 if (IS_ERR(file))
1504 goto out_kfree;
1505
1506 sched_exec();
1507
1508 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1509 bprm->file = file;
1510 bprm->filename = filename;
1511 bprm->interp = filename;
1512 bprm->mm = mm_alloc();
1513 retval = -ENOMEM;
1514 if (!bprm->mm)
1515 goto out_file;
1516
1517 retval = init_new_context(current, bprm->mm);
1518 if (retval < 0)
1519 goto out_mm;
1520
1521 bprm->argc = compat_count(argv, bprm->p / sizeof(compat_uptr_t));
1522 if ((retval = bprm->argc) < 0)
1523 goto out_mm;
1524
1525 bprm->envc = compat_count(envp, bprm->p / sizeof(compat_uptr_t));
1526 if ((retval = bprm->envc) < 0)
1527 goto out_mm;
1528
1529 retval = security_bprm_alloc(bprm);
1530 if (retval)
1531 goto out;
1532
1533 retval = prepare_binprm(bprm);
1534 if (retval < 0)
1535 goto out;
1536
1537 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1538 if (retval < 0)
1539 goto out;
1540
1541 bprm->exec = bprm->p;
1542 retval = compat_copy_strings(bprm->envc, envp, bprm);
1543 if (retval < 0)
1544 goto out;
1545
1546 retval = compat_copy_strings(bprm->argc, argv, bprm);
1547 if (retval < 0)
1548 goto out;
1549
1550 retval = search_binary_handler(bprm, regs);
1551 if (retval >= 0) {
1552 free_arg_pages(bprm);
1553
1554 /* execve success */
1555 security_bprm_free(bprm);
David S. Miller4a805e82005-09-14 21:40:00 -07001556 acct_update_integrals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 kfree(bprm);
1558 return retval;
1559 }
1560
1561out:
1562 /* Something went wrong, return the inode and free the argument pages*/
1563 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1564 struct page * page = bprm->page[i];
1565 if (page)
1566 __free_page(page);
1567 }
1568
1569 if (bprm->security)
1570 security_bprm_free(bprm);
1571
1572out_mm:
1573 if (bprm->mm)
1574 mmdrop(bprm->mm);
1575
1576out_file:
1577 if (bprm->file) {
1578 allow_write_access(bprm->file);
1579 fput(bprm->file);
1580 }
1581
1582out_kfree:
1583 kfree(bprm);
1584
1585out_ret:
1586 return retval;
1587}
1588
1589#define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
1590
1591#define ROUND_UP(x,y) (((x)+(y)-1)/(y))
1592
1593/*
1594 * Ooo, nasty. We need here to frob 32-bit unsigned longs to
1595 * 64-bit unsigned longs.
1596 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001597static
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1599 unsigned long *fdset)
1600{
1601 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1602 if (ufdset) {
1603 unsigned long odd;
1604
1605 if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
1606 return -EFAULT;
1607
1608 odd = nr & 1UL;
1609 nr &= ~1UL;
1610 while (nr) {
1611 unsigned long h, l;
1612 __get_user(l, ufdset);
1613 __get_user(h, ufdset+1);
1614 ufdset += 2;
1615 *fdset++ = h << 32 | l;
1616 nr -= 2;
1617 }
1618 if (odd)
1619 __get_user(*fdset, ufdset);
1620 } else {
1621 /* Tricky, must clear full unsigned long in the
1622 * kernel fdset at the end, this makes sure that
1623 * actually happens.
1624 */
1625 memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
1626 }
1627 return 0;
1628}
1629
Arjan van de Ven858119e2006-01-14 13:20:43 -08001630static
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631void compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1632 unsigned long *fdset)
1633{
1634 unsigned long odd;
1635 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1636
1637 if (!ufdset)
1638 return;
1639
1640 odd = nr & 1UL;
1641 nr &= ~1UL;
1642 while (nr) {
1643 unsigned long h, l;
1644 l = *fdset++;
1645 h = l >> 32;
1646 __put_user(l, ufdset);
1647 __put_user(h, ufdset+1);
1648 ufdset += 2;
1649 nr -= 2;
1650 }
1651 if (odd)
1652 __put_user(*fdset, ufdset);
1653}
1654
1655
1656/*
1657 * This is a virtual copy of sys_select from fs/select.c and probably
1658 * should be compared to it from time to time
1659 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661/*
1662 * We can actually return ERESTARTSYS instead of EINTR, but I'd
1663 * like to be certain this leads to no problems. So I return
1664 * EINTR just for safety.
1665 *
1666 * Update: ERESTARTSYS breaks at least the xview clock binary, so
1667 * I'm trying ERESTARTNOHAND which restart only when you want to.
1668 */
1669#define MAX_SELECT_SECONDS \
1670 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
1671
David Woodhouse9f729492006-01-18 17:44:05 -08001672int compat_core_sys_select(int n, compat_ulong_t __user *inp,
1673 compat_ulong_t __user *outp, compat_ulong_t __user *exp, s64 *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
1675 fd_set_bits fds;
1676 char *bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 int size, max_fdset, ret = -EINVAL;
Linus Torvaldsa4531ed2005-09-09 15:10:52 -07001678 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 if (n < 0)
1681 goto out_nofds;
1682
1683 /* max_fdset can increase, so grab it once to avoid race */
Linus Torvaldsac5b8b62005-09-09 15:42:34 -07001684 rcu_read_lock();
Linus Torvaldsa4531ed2005-09-09 15:10:52 -07001685 fdt = files_fdtable(current->files);
1686 max_fdset = fdt->max_fdset;
Linus Torvaldsac5b8b62005-09-09 15:42:34 -07001687 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if (n > max_fdset)
1689 n = max_fdset;
1690
1691 /*
1692 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
1693 * since we used fdset we need to allocate memory in units of
1694 * long-words.
1695 */
1696 ret = -ENOMEM;
1697 size = FDS_BYTES(n);
Vadim Lobanov68c34312006-03-28 01:56:35 -08001698 bits = kmalloc(6 * size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (!bits)
1700 goto out_nofds;
1701 fds.in = (unsigned long *) bits;
1702 fds.out = (unsigned long *) (bits + size);
1703 fds.ex = (unsigned long *) (bits + 2*size);
1704 fds.res_in = (unsigned long *) (bits + 3*size);
1705 fds.res_out = (unsigned long *) (bits + 4*size);
1706 fds.res_ex = (unsigned long *) (bits + 5*size);
1707
1708 if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
1709 (ret = compat_get_fd_set(n, outp, fds.out)) ||
1710 (ret = compat_get_fd_set(n, exp, fds.ex)))
1711 goto out;
1712 zero_fd_set(n, fds.res_in);
1713 zero_fd_set(n, fds.res_out);
1714 zero_fd_set(n, fds.res_ex);
1715
David Woodhouse9f729492006-01-18 17:44:05 -08001716 ret = do_select(n, &fds, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 if (ret < 0)
1719 goto out;
1720 if (!ret) {
1721 ret = -ERESTARTNOHAND;
1722 if (signal_pending(current))
1723 goto out;
1724 ret = 0;
1725 }
1726
1727 compat_set_fd_set(n, inp, fds.res_in);
1728 compat_set_fd_set(n, outp, fds.res_out);
1729 compat_set_fd_set(n, exp, fds.res_ex);
1730
1731out:
Vadim Lobanov68c34312006-03-28 01:56:35 -08001732 kfree(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733out_nofds:
1734 return ret;
1735}
1736
David Woodhouse9f729492006-01-18 17:44:05 -08001737asmlinkage long compat_sys_select(int n, compat_ulong_t __user *inp,
1738 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1739 struct compat_timeval __user *tvp)
1740{
1741 s64 timeout = -1;
1742 struct compat_timeval tv;
1743 int ret;
1744
1745 if (tvp) {
1746 if (copy_from_user(&tv, tvp, sizeof(tv)))
1747 return -EFAULT;
1748
1749 if (tv.tv_sec < 0 || tv.tv_usec < 0)
1750 return -EINVAL;
1751
1752 /* Cast to u64 to make GCC stop complaining */
1753 if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
1754 timeout = -1; /* infinite */
1755 else {
David S. Miller7e732bf2006-01-19 16:40:42 -08001756 timeout = ROUND_UP(tv.tv_usec, 1000000/HZ);
David Woodhouse9f729492006-01-18 17:44:05 -08001757 timeout += tv.tv_sec * HZ;
1758 }
1759 }
1760
1761 ret = compat_core_sys_select(n, inp, outp, exp, &timeout);
1762
1763 if (tvp) {
Andrew Morton643a6542006-02-11 17:55:52 -08001764 struct compat_timeval rtv;
1765
David Woodhouse9f729492006-01-18 17:44:05 -08001766 if (current->personality & STICKY_TIMEOUTS)
1767 goto sticky;
Andrew Morton643a6542006-02-11 17:55:52 -08001768 rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
1769 rtv.tv_sec = timeout;
Andrew Morton74910e62006-02-17 13:52:58 -08001770 if (compat_timeval_compare(&rtv, &tv) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -08001771 rtv = tv;
1772 if (copy_to_user(tvp, &rtv, sizeof(rtv))) {
David Woodhouse9f729492006-01-18 17:44:05 -08001773sticky:
1774 /*
1775 * If an application puts its timeval in read-only
1776 * memory, we don't want the Linux-specific update to
1777 * the timeval to cause a fault after the select has
1778 * completed successfully. However, because we're not
1779 * updating the timeval, we can't restart the system
1780 * call.
1781 */
1782 if (ret == -ERESTARTNOHAND)
1783 ret = -EINTR;
1784 }
1785 }
1786
1787 return ret;
1788}
1789
1790#ifdef TIF_RESTORE_SIGMASK
1791asmlinkage long compat_sys_pselect7(int n, compat_ulong_t __user *inp,
1792 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1793 struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask,
1794 compat_size_t sigsetsize)
1795{
1796 compat_sigset_t ss32;
1797 sigset_t ksigmask, sigsaved;
Andrew Mortoncb82a6c2006-02-01 03:04:49 -08001798 s64 timeout = MAX_SCHEDULE_TIMEOUT;
David Woodhouse9f729492006-01-18 17:44:05 -08001799 struct compat_timespec ts;
1800 int ret;
1801
1802 if (tsp) {
1803 if (copy_from_user(&ts, tsp, sizeof(ts)))
1804 return -EFAULT;
1805
1806 if (ts.tv_sec < 0 || ts.tv_nsec < 0)
1807 return -EINVAL;
1808 }
1809
1810 if (sigmask) {
1811 if (sigsetsize != sizeof(compat_sigset_t))
1812 return -EINVAL;
1813 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1814 return -EFAULT;
1815 sigset_from_compat(&ksigmask, &ss32);
1816
1817 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1818 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1819 }
1820
1821 do {
1822 if (tsp) {
1823 if ((unsigned long)ts.tv_sec < MAX_SELECT_SECONDS) {
1824 timeout = ROUND_UP(ts.tv_nsec, 1000000000/HZ);
1825 timeout += ts.tv_sec * (unsigned long)HZ;
1826 ts.tv_sec = 0;
1827 ts.tv_nsec = 0;
1828 } else {
1829 ts.tv_sec -= MAX_SELECT_SECONDS;
1830 timeout = MAX_SELECT_SECONDS * HZ;
1831 }
1832 }
1833
1834 ret = compat_core_sys_select(n, inp, outp, exp, &timeout);
1835
1836 } while (!ret && !timeout && tsp && (ts.tv_sec || ts.tv_nsec));
1837
Heiko Carstens87c2b7c2006-11-02 22:06:58 -08001838 if (tsp) {
Andrew Morton643a6542006-02-11 17:55:52 -08001839 struct compat_timespec rts;
1840
Heiko Carstens87c2b7c2006-11-02 22:06:58 -08001841 if (current->personality & STICKY_TIMEOUTS)
1842 goto sticky;
1843
Andrew Morton643a6542006-02-11 17:55:52 -08001844 rts.tv_sec = timeout / HZ;
1845 rts.tv_nsec = (timeout % HZ) * (NSEC_PER_SEC/HZ);
1846 if (rts.tv_nsec >= NSEC_PER_SEC) {
1847 rts.tv_sec++;
1848 rts.tv_nsec -= NSEC_PER_SEC;
David Woodhouse9f729492006-01-18 17:44:05 -08001849 }
Andrew Morton74910e62006-02-17 13:52:58 -08001850 if (compat_timespec_compare(&rts, &ts) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -08001851 rts = ts;
Heiko Carstens87c2b7c2006-11-02 22:06:58 -08001852 if (copy_to_user(tsp, &rts, sizeof(rts))) {
1853sticky:
1854 /*
1855 * If an application puts its timeval in read-only
1856 * memory, we don't want the Linux-specific update to
1857 * the timeval to cause a fault after the select has
1858 * completed successfully. However, because we're not
1859 * updating the timeval, we can't restart the system
1860 * call.
1861 */
1862 if (ret == -ERESTARTNOHAND)
1863 ret = -EINTR;
1864 }
David Woodhouse9f729492006-01-18 17:44:05 -08001865 }
1866
1867 if (ret == -ERESTARTNOHAND) {
1868 /*
1869 * Don't restore the signal mask yet. Let do_signal() deliver
1870 * the signal on the way back to userspace, before the signal
1871 * mask is restored.
1872 */
1873 if (sigmask) {
1874 memcpy(&current->saved_sigmask, &sigsaved,
1875 sizeof(sigsaved));
1876 set_thread_flag(TIF_RESTORE_SIGMASK);
1877 }
1878 } else if (sigmask)
1879 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1880
1881 return ret;
1882}
1883
1884asmlinkage long compat_sys_pselect6(int n, compat_ulong_t __user *inp,
1885 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1886 struct compat_timespec __user *tsp, void __user *sig)
1887{
1888 compat_size_t sigsetsize = 0;
1889 compat_uptr_t up = 0;
1890
1891 if (sig) {
1892 if (!access_ok(VERIFY_READ, sig,
1893 sizeof(compat_uptr_t)+sizeof(compat_size_t)) ||
1894 __get_user(up, (compat_uptr_t __user *)sig) ||
1895 __get_user(sigsetsize,
1896 (compat_size_t __user *)(sig+sizeof(up))))
1897 return -EFAULT;
1898 }
1899 return compat_sys_pselect7(n, inp, outp, exp, tsp, compat_ptr(up),
1900 sigsetsize);
1901}
1902
1903asmlinkage long compat_sys_ppoll(struct pollfd __user *ufds,
1904 unsigned int nfds, struct compat_timespec __user *tsp,
1905 const compat_sigset_t __user *sigmask, compat_size_t sigsetsize)
1906{
1907 compat_sigset_t ss32;
1908 sigset_t ksigmask, sigsaved;
1909 struct compat_timespec ts;
1910 s64 timeout = -1;
1911 int ret;
1912
1913 if (tsp) {
1914 if (copy_from_user(&ts, tsp, sizeof(ts)))
1915 return -EFAULT;
1916
1917 /* We assume that ts.tv_sec is always lower than
1918 the number of seconds that can be expressed in
1919 an s64. Otherwise the compiler bitches at us */
David S. Miller7e732bf2006-01-19 16:40:42 -08001920 timeout = ROUND_UP(ts.tv_nsec, 1000000000/HZ);
David Woodhouse9f729492006-01-18 17:44:05 -08001921 timeout += ts.tv_sec * HZ;
1922 }
1923
1924 if (sigmask) {
Alexey Dobriyan3835a9b2006-05-15 09:44:27 -07001925 if (sigsetsize != sizeof(compat_sigset_t))
David Woodhouse9f729492006-01-18 17:44:05 -08001926 return -EINVAL;
1927 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1928 return -EFAULT;
1929 sigset_from_compat(&ksigmask, &ss32);
1930
1931 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1932 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1933 }
1934
1935 ret = do_sys_poll(ufds, nfds, &timeout);
1936
1937 /* We can restart this syscall, usually */
1938 if (ret == -EINTR) {
1939 /*
1940 * Don't restore the signal mask yet. Let do_signal() deliver
1941 * the signal on the way back to userspace, before the signal
1942 * mask is restored.
1943 */
1944 if (sigmask) {
1945 memcpy(&current->saved_sigmask, &sigsaved,
1946 sizeof(sigsaved));
1947 set_thread_flag(TIF_RESTORE_SIGMASK);
1948 }
1949 ret = -ERESTARTNOHAND;
1950 } else if (sigmask)
1951 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1952
1953 if (tsp && timeout >= 0) {
Andrew Morton643a6542006-02-11 17:55:52 -08001954 struct compat_timespec rts;
1955
David Woodhouse9f729492006-01-18 17:44:05 -08001956 if (current->personality & STICKY_TIMEOUTS)
1957 goto sticky;
1958 /* Yes, we know it's actually an s64, but it's also positive. */
Andrew Morton643a6542006-02-11 17:55:52 -08001959 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
1960 1000;
1961 rts.tv_sec = timeout;
Andrew Morton74910e62006-02-17 13:52:58 -08001962 if (compat_timespec_compare(&rts, &ts) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -08001963 rts = ts;
1964 if (copy_to_user(tsp, &rts, sizeof(rts))) {
David Woodhouse9f729492006-01-18 17:44:05 -08001965sticky:
1966 /*
1967 * If an application puts its timeval in read-only
1968 * memory, we don't want the Linux-specific update to
1969 * the timeval to cause a fault after the select has
1970 * completed successfully. However, because we're not
1971 * updating the timeval, we can't restart the system
1972 * call.
1973 */
1974 if (ret == -ERESTARTNOHAND && timeout >= 0)
1975 ret = -EINTR;
1976 }
1977 }
1978
1979 return ret;
1980}
1981#endif /* TIF_RESTORE_SIGMASK */
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983#if defined(CONFIG_NFSD) || defined(CONFIG_NFSD_MODULE)
1984/* Stuff for NFS server syscalls... */
1985struct compat_nfsctl_svc {
1986 u16 svc32_port;
1987 s32 svc32_nthreads;
1988};
1989
1990struct compat_nfsctl_client {
1991 s8 cl32_ident[NFSCLNT_IDMAX+1];
1992 s32 cl32_naddr;
1993 struct in_addr cl32_addrlist[NFSCLNT_ADDRMAX];
1994 s32 cl32_fhkeytype;
1995 s32 cl32_fhkeylen;
1996 u8 cl32_fhkey[NFSCLNT_KEYMAX];
1997};
1998
1999struct compat_nfsctl_export {
2000 char ex32_client[NFSCLNT_IDMAX+1];
2001 char ex32_path[NFS_MAXPATHLEN+1];
2002 compat_dev_t ex32_dev;
2003 compat_ino_t ex32_ino;
2004 compat_int_t ex32_flags;
Stephen Rothwell202e5972005-09-06 15:16:40 -07002005 __compat_uid_t ex32_anon_uid;
2006 __compat_gid_t ex32_anon_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007};
2008
2009struct compat_nfsctl_fdparm {
2010 struct sockaddr gd32_addr;
2011 s8 gd32_path[NFS_MAXPATHLEN+1];
2012 compat_int_t gd32_version;
2013};
2014
2015struct compat_nfsctl_fsparm {
2016 struct sockaddr gd32_addr;
2017 s8 gd32_path[NFS_MAXPATHLEN+1];
2018 compat_int_t gd32_maxlen;
2019};
2020
2021struct compat_nfsctl_arg {
2022 compat_int_t ca32_version; /* safeguard */
2023 union {
2024 struct compat_nfsctl_svc u32_svc;
2025 struct compat_nfsctl_client u32_client;
2026 struct compat_nfsctl_export u32_export;
2027 struct compat_nfsctl_fdparm u32_getfd;
2028 struct compat_nfsctl_fsparm u32_getfs;
2029 } u;
2030#define ca32_svc u.u32_svc
2031#define ca32_client u.u32_client
2032#define ca32_export u.u32_export
2033#define ca32_getfd u.u32_getfd
2034#define ca32_getfs u.u32_getfs
2035};
2036
2037union compat_nfsctl_res {
2038 __u8 cr32_getfh[NFS_FHSIZE];
2039 struct knfsd_fh cr32_getfs;
2040};
2041
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002042static int compat_nfs_svc_trans(struct nfsctl_arg *karg,
2043 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002045 if (!access_ok(VERIFY_READ, &arg->ca32_svc, sizeof(arg->ca32_svc)) ||
2046 get_user(karg->ca_version, &arg->ca32_version) ||
2047 __get_user(karg->ca_svc.svc_port, &arg->ca32_svc.svc32_port) ||
2048 __get_user(karg->ca_svc.svc_nthreads,
2049 &arg->ca32_svc.svc32_nthreads))
2050 return -EFAULT;
2051 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
2053
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002054static int compat_nfs_clnt_trans(struct nfsctl_arg *karg,
2055 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002057 if (!access_ok(VERIFY_READ, &arg->ca32_client,
2058 sizeof(arg->ca32_client)) ||
2059 get_user(karg->ca_version, &arg->ca32_version) ||
2060 __copy_from_user(&karg->ca_client.cl_ident[0],
2061 &arg->ca32_client.cl32_ident[0],
2062 NFSCLNT_IDMAX) ||
2063 __get_user(karg->ca_client.cl_naddr,
2064 &arg->ca32_client.cl32_naddr) ||
2065 __copy_from_user(&karg->ca_client.cl_addrlist[0],
2066 &arg->ca32_client.cl32_addrlist[0],
2067 (sizeof(struct in_addr) * NFSCLNT_ADDRMAX)) ||
2068 __get_user(karg->ca_client.cl_fhkeytype,
2069 &arg->ca32_client.cl32_fhkeytype) ||
2070 __get_user(karg->ca_client.cl_fhkeylen,
2071 &arg->ca32_client.cl32_fhkeylen) ||
2072 __copy_from_user(&karg->ca_client.cl_fhkey[0],
2073 &arg->ca32_client.cl32_fhkey[0],
2074 NFSCLNT_KEYMAX))
2075 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002080static int compat_nfs_exp_trans(struct nfsctl_arg *karg,
2081 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002083 if (!access_ok(VERIFY_READ, &arg->ca32_export,
2084 sizeof(arg->ca32_export)) ||
2085 get_user(karg->ca_version, &arg->ca32_version) ||
2086 __copy_from_user(&karg->ca_export.ex_client[0],
2087 &arg->ca32_export.ex32_client[0],
2088 NFSCLNT_IDMAX) ||
2089 __copy_from_user(&karg->ca_export.ex_path[0],
2090 &arg->ca32_export.ex32_path[0],
2091 NFS_MAXPATHLEN) ||
2092 __get_user(karg->ca_export.ex_dev,
2093 &arg->ca32_export.ex32_dev) ||
2094 __get_user(karg->ca_export.ex_ino,
2095 &arg->ca32_export.ex32_ino) ||
2096 __get_user(karg->ca_export.ex_flags,
2097 &arg->ca32_export.ex32_flags) ||
2098 __get_user(karg->ca_export.ex_anon_uid,
2099 &arg->ca32_export.ex32_anon_uid) ||
2100 __get_user(karg->ca_export.ex_anon_gid,
2101 &arg->ca32_export.ex32_anon_gid))
2102 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 SET_UID(karg->ca_export.ex_anon_uid, karg->ca_export.ex_anon_uid);
2104 SET_GID(karg->ca_export.ex_anon_gid, karg->ca_export.ex_anon_gid);
2105
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002106 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107}
2108
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002109static int compat_nfs_getfd_trans(struct nfsctl_arg *karg,
2110 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002112 if (!access_ok(VERIFY_READ, &arg->ca32_getfd,
2113 sizeof(arg->ca32_getfd)) ||
2114 get_user(karg->ca_version, &arg->ca32_version) ||
2115 __copy_from_user(&karg->ca_getfd.gd_addr,
2116 &arg->ca32_getfd.gd32_addr,
2117 (sizeof(struct sockaddr))) ||
2118 __copy_from_user(&karg->ca_getfd.gd_path,
2119 &arg->ca32_getfd.gd32_path,
2120 (NFS_MAXPATHLEN+1)) ||
2121 __get_user(karg->ca_getfd.gd_version,
2122 &arg->ca32_getfd.gd32_version))
2123 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002128static int compat_nfs_getfs_trans(struct nfsctl_arg *karg,
2129 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002131 if (!access_ok(VERIFY_READ,&arg->ca32_getfs,sizeof(arg->ca32_getfs)) ||
2132 get_user(karg->ca_version, &arg->ca32_version) ||
2133 __copy_from_user(&karg->ca_getfs.gd_addr,
2134 &arg->ca32_getfs.gd32_addr,
2135 (sizeof(struct sockaddr))) ||
2136 __copy_from_user(&karg->ca_getfs.gd_path,
2137 &arg->ca32_getfs.gd32_path,
2138 (NFS_MAXPATHLEN+1)) ||
2139 __get_user(karg->ca_getfs.gd_maxlen,
2140 &arg->ca32_getfs.gd32_maxlen))
2141 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
2146/* This really doesn't need translations, we are only passing
2147 * back a union which contains opaque nfs file handle data.
2148 */
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002149static int compat_nfs_getfh_res_trans(union nfsctl_res *kres,
2150 union compat_nfsctl_res __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
2152 int err;
2153
2154 err = copy_to_user(res, kres, sizeof(*res));
2155
2156 return (err) ? -EFAULT : 0;
2157}
2158
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002159asmlinkage long compat_sys_nfsservctl(int cmd,
2160 struct compat_nfsctl_arg __user *arg,
2161 union compat_nfsctl_res __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
2163 struct nfsctl_arg *karg;
2164 union nfsctl_res *kres;
2165 mm_segment_t oldfs;
2166 int err;
2167
2168 karg = kmalloc(sizeof(*karg), GFP_USER);
2169 kres = kmalloc(sizeof(*kres), GFP_USER);
2170 if(!karg || !kres) {
2171 err = -ENOMEM;
2172 goto done;
2173 }
2174
2175 switch(cmd) {
2176 case NFSCTL_SVC:
2177 err = compat_nfs_svc_trans(karg, arg);
2178 break;
2179
2180 case NFSCTL_ADDCLIENT:
2181 err = compat_nfs_clnt_trans(karg, arg);
2182 break;
2183
2184 case NFSCTL_DELCLIENT:
2185 err = compat_nfs_clnt_trans(karg, arg);
2186 break;
2187
2188 case NFSCTL_EXPORT:
2189 case NFSCTL_UNEXPORT:
2190 err = compat_nfs_exp_trans(karg, arg);
2191 break;
2192
2193 case NFSCTL_GETFD:
2194 err = compat_nfs_getfd_trans(karg, arg);
2195 break;
2196
2197 case NFSCTL_GETFS:
2198 err = compat_nfs_getfs_trans(karg, arg);
2199 break;
2200
2201 default:
2202 err = -EINVAL;
Peter Staubach57070d02006-03-25 03:08:04 -08002203 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 }
2205
Peter Staubach57070d02006-03-25 03:08:04 -08002206 if (err)
2207 goto done;
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 oldfs = get_fs();
2210 set_fs(KERNEL_DS);
2211 /* The __user pointer casts are valid because of the set_fs() */
2212 err = sys_nfsservctl(cmd, (void __user *) karg, (void __user *) kres);
2213 set_fs(oldfs);
2214
2215 if (err)
2216 goto done;
2217
2218 if((cmd == NFSCTL_GETFD) ||
2219 (cmd == NFSCTL_GETFS))
2220 err = compat_nfs_getfh_res_trans(kres, res);
2221
2222done:
2223 kfree(karg);
2224 kfree(kres);
2225 return err;
2226}
2227#else /* !NFSD */
2228long asmlinkage compat_sys_nfsservctl(int cmd, void *notused, void *notused2)
2229{
2230 return sys_ni_syscall();
2231}
2232#endif