blob: 122b4e3992b54b322d40ef84814c95048ca00472 [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>
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
David Howells07f3f052006-09-30 20:52:18 +020059extern void sigset_from_compat(sigset_t *set, compat_sigset_t *compat);
60
Andi Kleenbebfa102006-06-26 13:56:52 +020061int compat_printk(const char *fmt, ...)
62{
63 va_list ap;
64 int ret;
65 if (!compat_log)
66 return 0;
67 va_start(ap, fmt);
68 ret = vprintk(fmt, ap);
69 va_end(ap);
70 return ret;
71}
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,
917 loff_t offset, ino_t ino, unsigned int d_type)
918{
919 struct compat_readdir_callback *buf = __buf;
920 struct compat_old_linux_dirent __user *dirent;
921
922 if (buf->result)
923 return -EINVAL;
924 buf->result++;
925 dirent = buf->dirent;
926 if (!access_ok(VERIFY_WRITE, dirent,
927 (unsigned long)(dirent->d_name + namlen + 1) -
928 (unsigned long)dirent))
929 goto efault;
930 if ( __put_user(ino, &dirent->d_ino) ||
931 __put_user(offset, &dirent->d_offset) ||
932 __put_user(namlen, &dirent->d_namlen) ||
933 __copy_to_user(dirent->d_name, name, namlen) ||
934 __put_user(0, dirent->d_name + namlen))
935 goto efault;
936 return 0;
937efault:
938 buf->result = -EFAULT;
939 return -EFAULT;
940}
941
942asmlinkage long compat_sys_old_readdir(unsigned int fd,
943 struct compat_old_linux_dirent __user *dirent, unsigned int count)
944{
945 int error;
946 struct file *file;
947 struct compat_readdir_callback buf;
948
949 error = -EBADF;
950 file = fget(fd);
951 if (!file)
952 goto out;
953
954 buf.result = 0;
955 buf.dirent = dirent;
956
957 error = vfs_readdir(file, compat_fillonedir, &buf);
958 if (error >= 0)
959 error = buf.result;
960
961 fput(file);
962out:
963 return error;
964}
965
966struct compat_linux_dirent {
967 compat_ulong_t d_ino;
968 compat_ulong_t d_off;
969 unsigned short d_reclen;
970 char d_name[1];
971};
972
973struct compat_getdents_callback {
974 struct compat_linux_dirent __user *current_dir;
975 struct compat_linux_dirent __user *previous;
976 int count;
977 int error;
978};
979
980static int compat_filldir(void *__buf, const char *name, int namlen,
981 loff_t offset, ino_t ino, unsigned int d_type)
982{
983 struct compat_linux_dirent __user * dirent;
984 struct compat_getdents_callback *buf = __buf;
985 int reclen = COMPAT_ROUND_UP(NAME_OFFSET(dirent) + namlen + 2);
986
987 buf->error = -EINVAL; /* only used if we fail.. */
988 if (reclen > buf->count)
989 return -EINVAL;
990 dirent = buf->previous;
991 if (dirent) {
992 if (__put_user(offset, &dirent->d_off))
993 goto efault;
994 }
995 dirent = buf->current_dir;
996 if (__put_user(ino, &dirent->d_ino))
997 goto efault;
998 if (__put_user(reclen, &dirent->d_reclen))
999 goto efault;
1000 if (copy_to_user(dirent->d_name, name, namlen))
1001 goto efault;
1002 if (__put_user(0, dirent->d_name + namlen))
1003 goto efault;
1004 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
1005 goto efault;
1006 buf->previous = dirent;
1007 dirent = (void __user *)dirent + reclen;
1008 buf->current_dir = dirent;
1009 buf->count -= reclen;
1010 return 0;
1011efault:
1012 buf->error = -EFAULT;
1013 return -EFAULT;
1014}
1015
1016asmlinkage long compat_sys_getdents(unsigned int fd,
1017 struct compat_linux_dirent __user *dirent, unsigned int count)
1018{
1019 struct file * file;
1020 struct compat_linux_dirent __user * lastdirent;
1021 struct compat_getdents_callback buf;
1022 int error;
1023
1024 error = -EFAULT;
1025 if (!access_ok(VERIFY_WRITE, dirent, count))
1026 goto out;
1027
1028 error = -EBADF;
1029 file = fget(fd);
1030 if (!file)
1031 goto out;
1032
1033 buf.current_dir = dirent;
1034 buf.previous = NULL;
1035 buf.count = count;
1036 buf.error = 0;
1037
1038 error = vfs_readdir(file, compat_filldir, &buf);
1039 if (error < 0)
1040 goto out_putf;
1041 error = buf.error;
1042 lastdirent = buf.previous;
1043 if (lastdirent) {
1044 if (put_user(file->f_pos, &lastdirent->d_off))
1045 error = -EFAULT;
1046 else
1047 error = count - buf.count;
1048 }
1049
1050out_putf:
1051 fput(file);
1052out:
1053 return error;
1054}
1055
1056#ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64
1057#define COMPAT_ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1))
1058
1059struct compat_getdents_callback64 {
1060 struct linux_dirent64 __user *current_dir;
1061 struct linux_dirent64 __user *previous;
1062 int count;
1063 int error;
1064};
1065
1066static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset,
1067 ino_t ino, unsigned int d_type)
1068{
1069 struct linux_dirent64 __user *dirent;
1070 struct compat_getdents_callback64 *buf = __buf;
1071 int jj = NAME_OFFSET(dirent);
1072 int reclen = COMPAT_ROUND_UP64(jj + namlen + 1);
1073 u64 off;
1074
1075 buf->error = -EINVAL; /* only used if we fail.. */
1076 if (reclen > buf->count)
1077 return -EINVAL;
1078 dirent = buf->previous;
1079
1080 if (dirent) {
1081 if (__put_user_unaligned(offset, &dirent->d_off))
1082 goto efault;
1083 }
1084 dirent = buf->current_dir;
1085 if (__put_user_unaligned(ino, &dirent->d_ino))
1086 goto efault;
1087 off = 0;
1088 if (__put_user_unaligned(off, &dirent->d_off))
1089 goto efault;
1090 if (__put_user(reclen, &dirent->d_reclen))
1091 goto efault;
1092 if (__put_user(d_type, &dirent->d_type))
1093 goto efault;
1094 if (copy_to_user(dirent->d_name, name, namlen))
1095 goto efault;
1096 if (__put_user(0, dirent->d_name + namlen))
1097 goto efault;
1098 buf->previous = dirent;
1099 dirent = (void __user *)dirent + reclen;
1100 buf->current_dir = dirent;
1101 buf->count -= reclen;
1102 return 0;
1103efault:
1104 buf->error = -EFAULT;
1105 return -EFAULT;
1106}
1107
1108asmlinkage long compat_sys_getdents64(unsigned int fd,
1109 struct linux_dirent64 __user * dirent, unsigned int count)
1110{
1111 struct file * file;
1112 struct linux_dirent64 __user * lastdirent;
1113 struct compat_getdents_callback64 buf;
1114 int error;
1115
1116 error = -EFAULT;
1117 if (!access_ok(VERIFY_WRITE, dirent, count))
1118 goto out;
1119
1120 error = -EBADF;
1121 file = fget(fd);
1122 if (!file)
1123 goto out;
1124
1125 buf.current_dir = dirent;
1126 buf.previous = NULL;
1127 buf.count = count;
1128 buf.error = 0;
1129
1130 error = vfs_readdir(file, compat_filldir64, &buf);
1131 if (error < 0)
1132 goto out_putf;
1133 error = buf.error;
1134 lastdirent = buf.previous;
1135 if (lastdirent) {
1136 typeof(lastdirent->d_off) d_off = file->f_pos;
1137 __put_user_unaligned(d_off, &lastdirent->d_off);
1138 error = count - buf.count;
1139 }
1140
1141out_putf:
1142 fput(file);
1143out:
1144 return error;
1145}
1146#endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
1147
1148static ssize_t compat_do_readv_writev(int type, struct file *file,
1149 const struct compat_iovec __user *uvector,
1150 unsigned long nr_segs, loff_t *pos)
1151{
1152 typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
1153 typedef ssize_t (*iov_fn_t)(struct file *, const struct iovec *, unsigned long, loff_t *);
1154
1155 compat_ssize_t tot_len;
1156 struct iovec iovstack[UIO_FASTIOV];
1157 struct iovec *iov=iovstack, *vector;
1158 ssize_t ret;
1159 int seg;
1160 io_fn_t fn;
1161 iov_fn_t fnv;
1162
1163 /*
1164 * SuS says "The readv() function *may* fail if the iovcnt argument
1165 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
1166 * traditionally returned zero for zero segments, so...
1167 */
1168 ret = 0;
1169 if (nr_segs == 0)
1170 goto out;
1171
1172 /*
1173 * First get the "struct iovec" from user memory and
1174 * verify all the pointers
1175 */
1176 ret = -EINVAL;
1177 if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0))
1178 goto out;
1179 if (!file->f_op)
1180 goto out;
1181 if (nr_segs > UIO_FASTIOV) {
1182 ret = -ENOMEM;
1183 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
1184 if (!iov)
1185 goto out;
1186 }
1187 ret = -EFAULT;
1188 if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
1189 goto out;
1190
1191 /*
1192 * Single unix specification:
1193 * We should -EINVAL if an element length is not >= 0 and fitting an
1194 * ssize_t. The total length is fitting an ssize_t
1195 *
1196 * Be careful here because iov_len is a size_t not an ssize_t
1197 */
1198 tot_len = 0;
1199 vector = iov;
1200 ret = -EINVAL;
1201 for (seg = 0 ; seg < nr_segs; seg++) {
1202 compat_ssize_t tmp = tot_len;
1203 compat_ssize_t len;
1204 compat_uptr_t buf;
1205
1206 if (__get_user(len, &uvector->iov_len) ||
1207 __get_user(buf, &uvector->iov_base)) {
1208 ret = -EFAULT;
1209 goto out;
1210 }
1211 if (len < 0) /* size_t not fitting an compat_ssize_t .. */
1212 goto out;
1213 tot_len += len;
1214 if (tot_len < tmp) /* maths overflow on the compat_ssize_t */
1215 goto out;
1216 vector->iov_base = compat_ptr(buf);
1217 vector->iov_len = (compat_size_t) len;
1218 uvector++;
1219 vector++;
1220 }
1221 if (tot_len == 0) {
1222 ret = 0;
1223 goto out;
1224 }
1225
1226 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001227 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 goto out;
1229
James Morrise7edf9c2006-04-26 02:45:03 -04001230 ret = security_file_permission(file, type == READ ? MAY_READ:MAY_WRITE);
1231 if (ret)
1232 goto out;
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 fnv = NULL;
1235 if (type == READ) {
1236 fn = file->f_op->read;
1237 fnv = file->f_op->readv;
1238 } else {
1239 fn = (io_fn_t)file->f_op->write;
1240 fnv = file->f_op->writev;
1241 }
1242 if (fnv) {
1243 ret = fnv(file, iov, nr_segs, pos);
1244 goto out;
1245 }
1246
1247 /* Do it by hand, with file-ops */
1248 ret = 0;
1249 vector = iov;
1250 while (nr_segs > 0) {
1251 void __user * base;
1252 size_t len;
1253 ssize_t nr;
1254
1255 base = vector->iov_base;
1256 len = vector->iov_len;
1257 vector++;
1258 nr_segs--;
1259
1260 nr = fn(file, base, len, pos);
1261
1262 if (nr < 0) {
1263 if (!ret) ret = nr;
1264 break;
1265 }
1266 ret += nr;
1267 if (nr != len)
1268 break;
1269 }
1270out:
1271 if (iov != iovstack)
1272 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -04001273 if ((ret + (type == READ)) > 0) {
1274 struct dentry *dentry = file->f_dentry;
1275 if (type == READ)
1276 fsnotify_access(dentry);
1277 else
1278 fsnotify_modify(dentry);
1279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return ret;
1281}
1282
1283asmlinkage ssize_t
1284compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1285{
1286 struct file *file;
1287 ssize_t ret = -EBADF;
1288
1289 file = fget(fd);
1290 if (!file)
1291 return -EBADF;
1292
1293 if (!(file->f_mode & FMODE_READ))
1294 goto out;
1295
1296 ret = -EINVAL;
1297 if (!file->f_op || (!file->f_op->readv && !file->f_op->read))
1298 goto out;
1299
1300 ret = compat_do_readv_writev(READ, file, vec, vlen, &file->f_pos);
1301
1302out:
1303 fput(file);
1304 return ret;
1305}
1306
1307asmlinkage ssize_t
1308compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1309{
1310 struct file *file;
1311 ssize_t ret = -EBADF;
1312
1313 file = fget(fd);
1314 if (!file)
1315 return -EBADF;
1316 if (!(file->f_mode & FMODE_WRITE))
1317 goto out;
1318
1319 ret = -EINVAL;
1320 if (!file->f_op || (!file->f_op->writev && !file->f_op->write))
1321 goto out;
1322
1323 ret = compat_do_readv_writev(WRITE, file, vec, vlen, &file->f_pos);
1324
1325out:
1326 fput(file);
1327 return ret;
1328}
1329
Andi Kleend2610202006-05-01 12:15:48 -07001330asmlinkage long
1331compat_sys_vmsplice(int fd, const struct compat_iovec __user *iov32,
1332 unsigned int nr_segs, unsigned int flags)
1333{
1334 unsigned i;
1335 struct iovec *iov;
Jens Axboe98232d52006-05-04 09:13:49 +02001336 if (nr_segs > UIO_MAXIOV)
Andi Kleend2610202006-05-01 12:15:48 -07001337 return -EINVAL;
1338 iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
1339 for (i = 0; i < nr_segs; i++) {
1340 struct compat_iovec v;
1341 if (get_user(v.iov_base, &iov32[i].iov_base) ||
1342 get_user(v.iov_len, &iov32[i].iov_len) ||
1343 put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
1344 put_user(v.iov_len, &iov[i].iov_len))
1345 return -EFAULT;
1346 }
1347 return sys_vmsplice(fd, iov, nr_segs, flags);
1348}
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350/*
Miklos Szeredie922efc2005-09-06 15:18:25 -07001351 * Exactly like fs/open.c:sys_open(), except that it doesn't set the
1352 * O_LARGEFILE flag.
1353 */
1354asmlinkage long
1355compat_sys_open(const char __user *filename, int flags, int mode)
1356{
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001357 return do_sys_open(AT_FDCWD, filename, flags, mode);
1358}
1359
1360/*
1361 * Exactly like fs/open.c:sys_openat(), except that it doesn't set the
1362 * O_LARGEFILE flag.
1363 */
1364asmlinkage long
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +11001365compat_sys_openat(unsigned int dfd, const char __user *filename, int flags, int mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001366{
1367 return do_sys_open(dfd, filename, flags, mode);
Miklos Szeredie922efc2005-09-06 15:18:25 -07001368}
1369
1370/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 * compat_count() counts the number of arguments/envelopes. It is basically
1372 * a copy of count() from fs/exec.c, except that it works with 32 bit argv
1373 * and envp pointers.
1374 */
1375static int compat_count(compat_uptr_t __user *argv, int max)
1376{
1377 int i = 0;
1378
1379 if (argv != NULL) {
1380 for (;;) {
1381 compat_uptr_t p;
1382
1383 if (get_user(p, argv))
1384 return -EFAULT;
1385 if (!p)
1386 break;
1387 argv++;
1388 if(++i > max)
1389 return -E2BIG;
1390 }
1391 }
1392 return i;
1393}
1394
1395/*
1396 * compat_copy_strings() is basically a copy of copy_strings() from fs/exec.c
1397 * except that it works with 32 bit argv and envp pointers.
1398 */
1399static int compat_copy_strings(int argc, compat_uptr_t __user *argv,
1400 struct linux_binprm *bprm)
1401{
1402 struct page *kmapped_page = NULL;
1403 char *kaddr = NULL;
1404 int ret;
1405
1406 while (argc-- > 0) {
1407 compat_uptr_t str;
1408 int len;
1409 unsigned long pos;
1410
1411 if (get_user(str, argv+argc) ||
1412 !(len = strnlen_user(compat_ptr(str), bprm->p))) {
1413 ret = -EFAULT;
1414 goto out;
1415 }
1416
1417 if (bprm->p < len) {
1418 ret = -E2BIG;
1419 goto out;
1420 }
1421
1422 bprm->p -= len;
1423 /* XXX: add architecture specific overflow check here. */
1424 pos = bprm->p;
1425
1426 while (len > 0) {
1427 int i, new, err;
1428 int offset, bytes_to_copy;
1429 struct page *page;
1430
1431 offset = pos % PAGE_SIZE;
1432 i = pos/PAGE_SIZE;
1433 page = bprm->page[i];
1434 new = 0;
1435 if (!page) {
1436 page = alloc_page(GFP_HIGHUSER);
1437 bprm->page[i] = page;
1438 if (!page) {
1439 ret = -ENOMEM;
1440 goto out;
1441 }
1442 new = 1;
1443 }
1444
1445 if (page != kmapped_page) {
1446 if (kmapped_page)
1447 kunmap(kmapped_page);
1448 kmapped_page = page;
1449 kaddr = kmap(kmapped_page);
1450 }
1451 if (new && offset)
1452 memset(kaddr, 0, offset);
1453 bytes_to_copy = PAGE_SIZE - offset;
1454 if (bytes_to_copy > len) {
1455 bytes_to_copy = len;
1456 if (new)
1457 memset(kaddr+offset+len, 0,
1458 PAGE_SIZE-offset-len);
1459 }
1460 err = copy_from_user(kaddr+offset, compat_ptr(str),
1461 bytes_to_copy);
1462 if (err) {
1463 ret = -EFAULT;
1464 goto out;
1465 }
1466
1467 pos += bytes_to_copy;
1468 str += bytes_to_copy;
1469 len -= bytes_to_copy;
1470 }
1471 }
1472 ret = 0;
1473out:
1474 if (kmapped_page)
1475 kunmap(kmapped_page);
1476 return ret;
1477}
1478
1479#ifdef CONFIG_MMU
1480
1481#define free_arg_pages(bprm) do { } while (0)
1482
1483#else
1484
1485static inline void free_arg_pages(struct linux_binprm *bprm)
1486{
1487 int i;
1488
1489 for (i = 0; i < MAX_ARG_PAGES; i++) {
1490 if (bprm->page[i])
1491 __free_page(bprm->page[i]);
1492 bprm->page[i] = NULL;
1493 }
1494}
1495
1496#endif /* CONFIG_MMU */
1497
1498/*
1499 * compat_do_execve() is mostly a copy of do_execve(), with the exception
1500 * that it processes 32 bit argv and envp pointers.
1501 */
1502int compat_do_execve(char * filename,
1503 compat_uptr_t __user *argv,
1504 compat_uptr_t __user *envp,
1505 struct pt_regs * regs)
1506{
1507 struct linux_binprm *bprm;
1508 struct file *file;
1509 int retval;
1510 int i;
1511
1512 retval = -ENOMEM;
Oliver Neukum11b0b5a2006-03-25 03:08:13 -08001513 bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (!bprm)
1515 goto out_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517 file = open_exec(filename);
1518 retval = PTR_ERR(file);
1519 if (IS_ERR(file))
1520 goto out_kfree;
1521
1522 sched_exec();
1523
1524 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1525 bprm->file = file;
1526 bprm->filename = filename;
1527 bprm->interp = filename;
1528 bprm->mm = mm_alloc();
1529 retval = -ENOMEM;
1530 if (!bprm->mm)
1531 goto out_file;
1532
1533 retval = init_new_context(current, bprm->mm);
1534 if (retval < 0)
1535 goto out_mm;
1536
1537 bprm->argc = compat_count(argv, bprm->p / sizeof(compat_uptr_t));
1538 if ((retval = bprm->argc) < 0)
1539 goto out_mm;
1540
1541 bprm->envc = compat_count(envp, bprm->p / sizeof(compat_uptr_t));
1542 if ((retval = bprm->envc) < 0)
1543 goto out_mm;
1544
1545 retval = security_bprm_alloc(bprm);
1546 if (retval)
1547 goto out;
1548
1549 retval = prepare_binprm(bprm);
1550 if (retval < 0)
1551 goto out;
1552
1553 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1554 if (retval < 0)
1555 goto out;
1556
1557 bprm->exec = bprm->p;
1558 retval = compat_copy_strings(bprm->envc, envp, bprm);
1559 if (retval < 0)
1560 goto out;
1561
1562 retval = compat_copy_strings(bprm->argc, argv, bprm);
1563 if (retval < 0)
1564 goto out;
1565
1566 retval = search_binary_handler(bprm, regs);
1567 if (retval >= 0) {
1568 free_arg_pages(bprm);
1569
1570 /* execve success */
1571 security_bprm_free(bprm);
David S. Miller4a805e82005-09-14 21:40:00 -07001572 acct_update_integrals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 kfree(bprm);
1574 return retval;
1575 }
1576
1577out:
1578 /* Something went wrong, return the inode and free the argument pages*/
1579 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1580 struct page * page = bprm->page[i];
1581 if (page)
1582 __free_page(page);
1583 }
1584
1585 if (bprm->security)
1586 security_bprm_free(bprm);
1587
1588out_mm:
1589 if (bprm->mm)
1590 mmdrop(bprm->mm);
1591
1592out_file:
1593 if (bprm->file) {
1594 allow_write_access(bprm->file);
1595 fput(bprm->file);
1596 }
1597
1598out_kfree:
1599 kfree(bprm);
1600
1601out_ret:
1602 return retval;
1603}
1604
1605#define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
1606
1607#define ROUND_UP(x,y) (((x)+(y)-1)/(y))
1608
1609/*
1610 * Ooo, nasty. We need here to frob 32-bit unsigned longs to
1611 * 64-bit unsigned longs.
1612 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001613static
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1615 unsigned long *fdset)
1616{
1617 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1618 if (ufdset) {
1619 unsigned long odd;
1620
1621 if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
1622 return -EFAULT;
1623
1624 odd = nr & 1UL;
1625 nr &= ~1UL;
1626 while (nr) {
1627 unsigned long h, l;
1628 __get_user(l, ufdset);
1629 __get_user(h, ufdset+1);
1630 ufdset += 2;
1631 *fdset++ = h << 32 | l;
1632 nr -= 2;
1633 }
1634 if (odd)
1635 __get_user(*fdset, ufdset);
1636 } else {
1637 /* Tricky, must clear full unsigned long in the
1638 * kernel fdset at the end, this makes sure that
1639 * actually happens.
1640 */
1641 memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
1642 }
1643 return 0;
1644}
1645
Arjan van de Ven858119e2006-01-14 13:20:43 -08001646static
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647void compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1648 unsigned long *fdset)
1649{
1650 unsigned long odd;
1651 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1652
1653 if (!ufdset)
1654 return;
1655
1656 odd = nr & 1UL;
1657 nr &= ~1UL;
1658 while (nr) {
1659 unsigned long h, l;
1660 l = *fdset++;
1661 h = l >> 32;
1662 __put_user(l, ufdset);
1663 __put_user(h, ufdset+1);
1664 ufdset += 2;
1665 nr -= 2;
1666 }
1667 if (odd)
1668 __put_user(*fdset, ufdset);
1669}
1670
1671
1672/*
1673 * This is a virtual copy of sys_select from fs/select.c and probably
1674 * should be compared to it from time to time
1675 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677/*
1678 * We can actually return ERESTARTSYS instead of EINTR, but I'd
1679 * like to be certain this leads to no problems. So I return
1680 * EINTR just for safety.
1681 *
1682 * Update: ERESTARTSYS breaks at least the xview clock binary, so
1683 * I'm trying ERESTARTNOHAND which restart only when you want to.
1684 */
1685#define MAX_SELECT_SECONDS \
1686 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
1687
David Woodhouse9f729492006-01-18 17:44:05 -08001688int compat_core_sys_select(int n, compat_ulong_t __user *inp,
1689 compat_ulong_t __user *outp, compat_ulong_t __user *exp, s64 *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
1691 fd_set_bits fds;
1692 char *bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 int size, max_fdset, ret = -EINVAL;
Linus Torvaldsa4531ed2005-09-09 15:10:52 -07001694 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if (n < 0)
1697 goto out_nofds;
1698
1699 /* max_fdset can increase, so grab it once to avoid race */
Linus Torvaldsac5b8b62005-09-09 15:42:34 -07001700 rcu_read_lock();
Linus Torvaldsa4531ed2005-09-09 15:10:52 -07001701 fdt = files_fdtable(current->files);
1702 max_fdset = fdt->max_fdset;
Linus Torvaldsac5b8b62005-09-09 15:42:34 -07001703 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (n > max_fdset)
1705 n = max_fdset;
1706
1707 /*
1708 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
1709 * since we used fdset we need to allocate memory in units of
1710 * long-words.
1711 */
1712 ret = -ENOMEM;
1713 size = FDS_BYTES(n);
Vadim Lobanov68c34312006-03-28 01:56:35 -08001714 bits = kmalloc(6 * size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 if (!bits)
1716 goto out_nofds;
1717 fds.in = (unsigned long *) bits;
1718 fds.out = (unsigned long *) (bits + size);
1719 fds.ex = (unsigned long *) (bits + 2*size);
1720 fds.res_in = (unsigned long *) (bits + 3*size);
1721 fds.res_out = (unsigned long *) (bits + 4*size);
1722 fds.res_ex = (unsigned long *) (bits + 5*size);
1723
1724 if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
1725 (ret = compat_get_fd_set(n, outp, fds.out)) ||
1726 (ret = compat_get_fd_set(n, exp, fds.ex)))
1727 goto out;
1728 zero_fd_set(n, fds.res_in);
1729 zero_fd_set(n, fds.res_out);
1730 zero_fd_set(n, fds.res_ex);
1731
David Woodhouse9f729492006-01-18 17:44:05 -08001732 ret = do_select(n, &fds, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 if (ret < 0)
1735 goto out;
1736 if (!ret) {
1737 ret = -ERESTARTNOHAND;
1738 if (signal_pending(current))
1739 goto out;
1740 ret = 0;
1741 }
1742
1743 compat_set_fd_set(n, inp, fds.res_in);
1744 compat_set_fd_set(n, outp, fds.res_out);
1745 compat_set_fd_set(n, exp, fds.res_ex);
1746
1747out:
Vadim Lobanov68c34312006-03-28 01:56:35 -08001748 kfree(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749out_nofds:
1750 return ret;
1751}
1752
David Woodhouse9f729492006-01-18 17:44:05 -08001753asmlinkage long compat_sys_select(int n, compat_ulong_t __user *inp,
1754 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1755 struct compat_timeval __user *tvp)
1756{
1757 s64 timeout = -1;
1758 struct compat_timeval tv;
1759 int ret;
1760
1761 if (tvp) {
1762 if (copy_from_user(&tv, tvp, sizeof(tv)))
1763 return -EFAULT;
1764
1765 if (tv.tv_sec < 0 || tv.tv_usec < 0)
1766 return -EINVAL;
1767
1768 /* Cast to u64 to make GCC stop complaining */
1769 if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
1770 timeout = -1; /* infinite */
1771 else {
David S. Miller7e732bf2006-01-19 16:40:42 -08001772 timeout = ROUND_UP(tv.tv_usec, 1000000/HZ);
David Woodhouse9f729492006-01-18 17:44:05 -08001773 timeout += tv.tv_sec * HZ;
1774 }
1775 }
1776
1777 ret = compat_core_sys_select(n, inp, outp, exp, &timeout);
1778
1779 if (tvp) {
Andrew Morton643a6542006-02-11 17:55:52 -08001780 struct compat_timeval rtv;
1781
David Woodhouse9f729492006-01-18 17:44:05 -08001782 if (current->personality & STICKY_TIMEOUTS)
1783 goto sticky;
Andrew Morton643a6542006-02-11 17:55:52 -08001784 rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
1785 rtv.tv_sec = timeout;
Andrew Morton74910e62006-02-17 13:52:58 -08001786 if (compat_timeval_compare(&rtv, &tv) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -08001787 rtv = tv;
1788 if (copy_to_user(tvp, &rtv, sizeof(rtv))) {
David Woodhouse9f729492006-01-18 17:44:05 -08001789sticky:
1790 /*
1791 * If an application puts its timeval in read-only
1792 * memory, we don't want the Linux-specific update to
1793 * the timeval to cause a fault after the select has
1794 * completed successfully. However, because we're not
1795 * updating the timeval, we can't restart the system
1796 * call.
1797 */
1798 if (ret == -ERESTARTNOHAND)
1799 ret = -EINTR;
1800 }
1801 }
1802
1803 return ret;
1804}
1805
1806#ifdef TIF_RESTORE_SIGMASK
1807asmlinkage long compat_sys_pselect7(int n, compat_ulong_t __user *inp,
1808 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1809 struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask,
1810 compat_size_t sigsetsize)
1811{
1812 compat_sigset_t ss32;
1813 sigset_t ksigmask, sigsaved;
Andrew Mortoncb82a6c2006-02-01 03:04:49 -08001814 s64 timeout = MAX_SCHEDULE_TIMEOUT;
David Woodhouse9f729492006-01-18 17:44:05 -08001815 struct compat_timespec ts;
1816 int ret;
1817
1818 if (tsp) {
1819 if (copy_from_user(&ts, tsp, sizeof(ts)))
1820 return -EFAULT;
1821
1822 if (ts.tv_sec < 0 || ts.tv_nsec < 0)
1823 return -EINVAL;
1824 }
1825
1826 if (sigmask) {
1827 if (sigsetsize != sizeof(compat_sigset_t))
1828 return -EINVAL;
1829 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1830 return -EFAULT;
1831 sigset_from_compat(&ksigmask, &ss32);
1832
1833 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1834 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1835 }
1836
1837 do {
1838 if (tsp) {
1839 if ((unsigned long)ts.tv_sec < MAX_SELECT_SECONDS) {
1840 timeout = ROUND_UP(ts.tv_nsec, 1000000000/HZ);
1841 timeout += ts.tv_sec * (unsigned long)HZ;
1842 ts.tv_sec = 0;
1843 ts.tv_nsec = 0;
1844 } else {
1845 ts.tv_sec -= MAX_SELECT_SECONDS;
1846 timeout = MAX_SELECT_SECONDS * HZ;
1847 }
1848 }
1849
1850 ret = compat_core_sys_select(n, inp, outp, exp, &timeout);
1851
1852 } while (!ret && !timeout && tsp && (ts.tv_sec || ts.tv_nsec));
1853
Andi Kleen75833342006-09-26 10:52:39 +02001854 if (ret == 0 && tsp && !(current->personality & STICKY_TIMEOUTS)) {
Andrew Morton643a6542006-02-11 17:55:52 -08001855 struct compat_timespec rts;
1856
1857 rts.tv_sec = timeout / HZ;
1858 rts.tv_nsec = (timeout % HZ) * (NSEC_PER_SEC/HZ);
1859 if (rts.tv_nsec >= NSEC_PER_SEC) {
1860 rts.tv_sec++;
1861 rts.tv_nsec -= NSEC_PER_SEC;
David Woodhouse9f729492006-01-18 17:44:05 -08001862 }
Andrew Morton74910e62006-02-17 13:52:58 -08001863 if (compat_timespec_compare(&rts, &ts) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -08001864 rts = ts;
Andi Kleen75833342006-09-26 10:52:39 +02001865 if (copy_to_user(tsp, &rts, sizeof(rts)))
1866 ret = -EFAULT;
David Woodhouse9f729492006-01-18 17:44:05 -08001867 }
1868
1869 if (ret == -ERESTARTNOHAND) {
1870 /*
1871 * Don't restore the signal mask yet. Let do_signal() deliver
1872 * the signal on the way back to userspace, before the signal
1873 * mask is restored.
1874 */
1875 if (sigmask) {
1876 memcpy(&current->saved_sigmask, &sigsaved,
1877 sizeof(sigsaved));
1878 set_thread_flag(TIF_RESTORE_SIGMASK);
1879 }
1880 } else if (sigmask)
1881 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1882
1883 return ret;
1884}
1885
1886asmlinkage long compat_sys_pselect6(int n, compat_ulong_t __user *inp,
1887 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1888 struct compat_timespec __user *tsp, void __user *sig)
1889{
1890 compat_size_t sigsetsize = 0;
1891 compat_uptr_t up = 0;
1892
1893 if (sig) {
1894 if (!access_ok(VERIFY_READ, sig,
1895 sizeof(compat_uptr_t)+sizeof(compat_size_t)) ||
1896 __get_user(up, (compat_uptr_t __user *)sig) ||
1897 __get_user(sigsetsize,
1898 (compat_size_t __user *)(sig+sizeof(up))))
1899 return -EFAULT;
1900 }
1901 return compat_sys_pselect7(n, inp, outp, exp, tsp, compat_ptr(up),
1902 sigsetsize);
1903}
1904
1905asmlinkage long compat_sys_ppoll(struct pollfd __user *ufds,
1906 unsigned int nfds, struct compat_timespec __user *tsp,
1907 const compat_sigset_t __user *sigmask, compat_size_t sigsetsize)
1908{
1909 compat_sigset_t ss32;
1910 sigset_t ksigmask, sigsaved;
1911 struct compat_timespec ts;
1912 s64 timeout = -1;
1913 int ret;
1914
1915 if (tsp) {
1916 if (copy_from_user(&ts, tsp, sizeof(ts)))
1917 return -EFAULT;
1918
1919 /* We assume that ts.tv_sec is always lower than
1920 the number of seconds that can be expressed in
1921 an s64. Otherwise the compiler bitches at us */
David S. Miller7e732bf2006-01-19 16:40:42 -08001922 timeout = ROUND_UP(ts.tv_nsec, 1000000000/HZ);
David Woodhouse9f729492006-01-18 17:44:05 -08001923 timeout += ts.tv_sec * HZ;
1924 }
1925
1926 if (sigmask) {
Alexey Dobriyan3835a9b2006-05-15 09:44:27 -07001927 if (sigsetsize != sizeof(compat_sigset_t))
David Woodhouse9f729492006-01-18 17:44:05 -08001928 return -EINVAL;
1929 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1930 return -EFAULT;
1931 sigset_from_compat(&ksigmask, &ss32);
1932
1933 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1934 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1935 }
1936
1937 ret = do_sys_poll(ufds, nfds, &timeout);
1938
1939 /* We can restart this syscall, usually */
1940 if (ret == -EINTR) {
1941 /*
1942 * Don't restore the signal mask yet. Let do_signal() deliver
1943 * the signal on the way back to userspace, before the signal
1944 * mask is restored.
1945 */
1946 if (sigmask) {
1947 memcpy(&current->saved_sigmask, &sigsaved,
1948 sizeof(sigsaved));
1949 set_thread_flag(TIF_RESTORE_SIGMASK);
1950 }
1951 ret = -ERESTARTNOHAND;
1952 } else if (sigmask)
1953 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1954
1955 if (tsp && timeout >= 0) {
Andrew Morton643a6542006-02-11 17:55:52 -08001956 struct compat_timespec rts;
1957
David Woodhouse9f729492006-01-18 17:44:05 -08001958 if (current->personality & STICKY_TIMEOUTS)
1959 goto sticky;
1960 /* Yes, we know it's actually an s64, but it's also positive. */
Andrew Morton643a6542006-02-11 17:55:52 -08001961 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
1962 1000;
1963 rts.tv_sec = timeout;
Andrew Morton74910e62006-02-17 13:52:58 -08001964 if (compat_timespec_compare(&rts, &ts) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -08001965 rts = ts;
1966 if (copy_to_user(tsp, &rts, sizeof(rts))) {
David Woodhouse9f729492006-01-18 17:44:05 -08001967sticky:
1968 /*
1969 * If an application puts its timeval in read-only
1970 * memory, we don't want the Linux-specific update to
1971 * the timeval to cause a fault after the select has
1972 * completed successfully. However, because we're not
1973 * updating the timeval, we can't restart the system
1974 * call.
1975 */
1976 if (ret == -ERESTARTNOHAND && timeout >= 0)
1977 ret = -EINTR;
1978 }
1979 }
1980
1981 return ret;
1982}
1983#endif /* TIF_RESTORE_SIGMASK */
1984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985#if defined(CONFIG_NFSD) || defined(CONFIG_NFSD_MODULE)
1986/* Stuff for NFS server syscalls... */
1987struct compat_nfsctl_svc {
1988 u16 svc32_port;
1989 s32 svc32_nthreads;
1990};
1991
1992struct compat_nfsctl_client {
1993 s8 cl32_ident[NFSCLNT_IDMAX+1];
1994 s32 cl32_naddr;
1995 struct in_addr cl32_addrlist[NFSCLNT_ADDRMAX];
1996 s32 cl32_fhkeytype;
1997 s32 cl32_fhkeylen;
1998 u8 cl32_fhkey[NFSCLNT_KEYMAX];
1999};
2000
2001struct compat_nfsctl_export {
2002 char ex32_client[NFSCLNT_IDMAX+1];
2003 char ex32_path[NFS_MAXPATHLEN+1];
2004 compat_dev_t ex32_dev;
2005 compat_ino_t ex32_ino;
2006 compat_int_t ex32_flags;
Stephen Rothwell202e5972005-09-06 15:16:40 -07002007 __compat_uid_t ex32_anon_uid;
2008 __compat_gid_t ex32_anon_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009};
2010
2011struct compat_nfsctl_fdparm {
2012 struct sockaddr gd32_addr;
2013 s8 gd32_path[NFS_MAXPATHLEN+1];
2014 compat_int_t gd32_version;
2015};
2016
2017struct compat_nfsctl_fsparm {
2018 struct sockaddr gd32_addr;
2019 s8 gd32_path[NFS_MAXPATHLEN+1];
2020 compat_int_t gd32_maxlen;
2021};
2022
2023struct compat_nfsctl_arg {
2024 compat_int_t ca32_version; /* safeguard */
2025 union {
2026 struct compat_nfsctl_svc u32_svc;
2027 struct compat_nfsctl_client u32_client;
2028 struct compat_nfsctl_export u32_export;
2029 struct compat_nfsctl_fdparm u32_getfd;
2030 struct compat_nfsctl_fsparm u32_getfs;
2031 } u;
2032#define ca32_svc u.u32_svc
2033#define ca32_client u.u32_client
2034#define ca32_export u.u32_export
2035#define ca32_getfd u.u32_getfd
2036#define ca32_getfs u.u32_getfs
2037};
2038
2039union compat_nfsctl_res {
2040 __u8 cr32_getfh[NFS_FHSIZE];
2041 struct knfsd_fh cr32_getfs;
2042};
2043
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002044static int compat_nfs_svc_trans(struct nfsctl_arg *karg,
2045 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002047 if (!access_ok(VERIFY_READ, &arg->ca32_svc, sizeof(arg->ca32_svc)) ||
2048 get_user(karg->ca_version, &arg->ca32_version) ||
2049 __get_user(karg->ca_svc.svc_port, &arg->ca32_svc.svc32_port) ||
2050 __get_user(karg->ca_svc.svc_nthreads,
2051 &arg->ca32_svc.svc32_nthreads))
2052 return -EFAULT;
2053 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054}
2055
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002056static int compat_nfs_clnt_trans(struct nfsctl_arg *karg,
2057 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002059 if (!access_ok(VERIFY_READ, &arg->ca32_client,
2060 sizeof(arg->ca32_client)) ||
2061 get_user(karg->ca_version, &arg->ca32_version) ||
2062 __copy_from_user(&karg->ca_client.cl_ident[0],
2063 &arg->ca32_client.cl32_ident[0],
2064 NFSCLNT_IDMAX) ||
2065 __get_user(karg->ca_client.cl_naddr,
2066 &arg->ca32_client.cl32_naddr) ||
2067 __copy_from_user(&karg->ca_client.cl_addrlist[0],
2068 &arg->ca32_client.cl32_addrlist[0],
2069 (sizeof(struct in_addr) * NFSCLNT_ADDRMAX)) ||
2070 __get_user(karg->ca_client.cl_fhkeytype,
2071 &arg->ca32_client.cl32_fhkeytype) ||
2072 __get_user(karg->ca_client.cl_fhkeylen,
2073 &arg->ca32_client.cl32_fhkeylen) ||
2074 __copy_from_user(&karg->ca_client.cl_fhkey[0],
2075 &arg->ca32_client.cl32_fhkey[0],
2076 NFSCLNT_KEYMAX))
2077 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002079 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080}
2081
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002082static int compat_nfs_exp_trans(struct nfsctl_arg *karg,
2083 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002085 if (!access_ok(VERIFY_READ, &arg->ca32_export,
2086 sizeof(arg->ca32_export)) ||
2087 get_user(karg->ca_version, &arg->ca32_version) ||
2088 __copy_from_user(&karg->ca_export.ex_client[0],
2089 &arg->ca32_export.ex32_client[0],
2090 NFSCLNT_IDMAX) ||
2091 __copy_from_user(&karg->ca_export.ex_path[0],
2092 &arg->ca32_export.ex32_path[0],
2093 NFS_MAXPATHLEN) ||
2094 __get_user(karg->ca_export.ex_dev,
2095 &arg->ca32_export.ex32_dev) ||
2096 __get_user(karg->ca_export.ex_ino,
2097 &arg->ca32_export.ex32_ino) ||
2098 __get_user(karg->ca_export.ex_flags,
2099 &arg->ca32_export.ex32_flags) ||
2100 __get_user(karg->ca_export.ex_anon_uid,
2101 &arg->ca32_export.ex32_anon_uid) ||
2102 __get_user(karg->ca_export.ex_anon_gid,
2103 &arg->ca32_export.ex32_anon_gid))
2104 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 SET_UID(karg->ca_export.ex_anon_uid, karg->ca_export.ex_anon_uid);
2106 SET_GID(karg->ca_export.ex_anon_gid, karg->ca_export.ex_anon_gid);
2107
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002108 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109}
2110
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002111static int compat_nfs_getfd_trans(struct nfsctl_arg *karg,
2112 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002114 if (!access_ok(VERIFY_READ, &arg->ca32_getfd,
2115 sizeof(arg->ca32_getfd)) ||
2116 get_user(karg->ca_version, &arg->ca32_version) ||
2117 __copy_from_user(&karg->ca_getfd.gd_addr,
2118 &arg->ca32_getfd.gd32_addr,
2119 (sizeof(struct sockaddr))) ||
2120 __copy_from_user(&karg->ca_getfd.gd_path,
2121 &arg->ca32_getfd.gd32_path,
2122 (NFS_MAXPATHLEN+1)) ||
2123 __get_user(karg->ca_getfd.gd_version,
2124 &arg->ca32_getfd.gd32_version))
2125 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002127 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
2129
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002130static int compat_nfs_getfs_trans(struct nfsctl_arg *karg,
2131 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002133 if (!access_ok(VERIFY_READ,&arg->ca32_getfs,sizeof(arg->ca32_getfs)) ||
2134 get_user(karg->ca_version, &arg->ca32_version) ||
2135 __copy_from_user(&karg->ca_getfs.gd_addr,
2136 &arg->ca32_getfs.gd32_addr,
2137 (sizeof(struct sockaddr))) ||
2138 __copy_from_user(&karg->ca_getfs.gd_path,
2139 &arg->ca32_getfs.gd32_path,
2140 (NFS_MAXPATHLEN+1)) ||
2141 __get_user(karg->ca_getfs.gd_maxlen,
2142 &arg->ca32_getfs.gd32_maxlen))
2143 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
2147
2148/* This really doesn't need translations, we are only passing
2149 * back a union which contains opaque nfs file handle data.
2150 */
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002151static int compat_nfs_getfh_res_trans(union nfsctl_res *kres,
2152 union compat_nfsctl_res __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
2154 int err;
2155
2156 err = copy_to_user(res, kres, sizeof(*res));
2157
2158 return (err) ? -EFAULT : 0;
2159}
2160
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002161asmlinkage long compat_sys_nfsservctl(int cmd,
2162 struct compat_nfsctl_arg __user *arg,
2163 union compat_nfsctl_res __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
2165 struct nfsctl_arg *karg;
2166 union nfsctl_res *kres;
2167 mm_segment_t oldfs;
2168 int err;
2169
2170 karg = kmalloc(sizeof(*karg), GFP_USER);
2171 kres = kmalloc(sizeof(*kres), GFP_USER);
2172 if(!karg || !kres) {
2173 err = -ENOMEM;
2174 goto done;
2175 }
2176
2177 switch(cmd) {
2178 case NFSCTL_SVC:
2179 err = compat_nfs_svc_trans(karg, arg);
2180 break;
2181
2182 case NFSCTL_ADDCLIENT:
2183 err = compat_nfs_clnt_trans(karg, arg);
2184 break;
2185
2186 case NFSCTL_DELCLIENT:
2187 err = compat_nfs_clnt_trans(karg, arg);
2188 break;
2189
2190 case NFSCTL_EXPORT:
2191 case NFSCTL_UNEXPORT:
2192 err = compat_nfs_exp_trans(karg, arg);
2193 break;
2194
2195 case NFSCTL_GETFD:
2196 err = compat_nfs_getfd_trans(karg, arg);
2197 break;
2198
2199 case NFSCTL_GETFS:
2200 err = compat_nfs_getfs_trans(karg, arg);
2201 break;
2202
2203 default:
2204 err = -EINVAL;
Peter Staubach57070d02006-03-25 03:08:04 -08002205 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 }
2207
Peter Staubach57070d02006-03-25 03:08:04 -08002208 if (err)
2209 goto done;
2210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 oldfs = get_fs();
2212 set_fs(KERNEL_DS);
2213 /* The __user pointer casts are valid because of the set_fs() */
2214 err = sys_nfsservctl(cmd, (void __user *) karg, (void __user *) kres);
2215 set_fs(oldfs);
2216
2217 if (err)
2218 goto done;
2219
2220 if((cmd == NFSCTL_GETFD) ||
2221 (cmd == NFSCTL_GETFS))
2222 err = compat_nfs_getfh_res_trans(kres, res);
2223
2224done:
2225 kfree(karg);
2226 kfree(kres);
2227 return err;
2228}
2229#else /* !NFSD */
2230long asmlinkage compat_sys_nfsservctl(int cmd, void *notused, void *notused2)
2231{
2232 return sys_ni_syscall();
2233}
2234#endif