blob: 7aef5412f49cd0f8a88238d50122dae2b7f4a21f [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>
Al Viroa1f8e7f2006-10-19 16:08:53 -040048#include <linux/highmem.h>
Al Virobd01f842006-10-19 17:23:57 -040049#include <linux/poll.h>
David S. Miller4a805e82005-09-14 21:40:00 -070050#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#include <net/sock.h> /* siocdevprivate_ioctl */
53
54#include <asm/uaccess.h>
55#include <asm/mmu_context.h>
56#include <asm/ioctls.h>
David Howells07f3f052006-09-30 20:52:18 +020057#include "internal.h"
David Woodhouse9f729492006-01-18 17:44:05 -080058
Andi Kleenbebfa102006-06-26 13:56:52 +020059int compat_log = 1;
60
61int 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
Badari Pulavartyee0b3e62006-09-30 23:28:47 -070073#include "read_write.h"
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/*
76 * Not all architectures have sys_utime, so implement this in terms
77 * of sys_utimes.
78 */
79asmlinkage long compat_sys_utime(char __user *filename, struct compat_utimbuf __user *t)
80{
81 struct timeval tv[2];
82
83 if (t) {
84 if (get_user(tv[0].tv_sec, &t->actime) ||
85 get_user(tv[1].tv_sec, &t->modtime))
86 return -EFAULT;
87 tv[0].tv_usec = 0;
88 tv[1].tv_usec = 0;
89 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -080090 return do_utimes(AT_FDCWD, filename, t ? tv : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +110093asmlinkage long compat_sys_futimesat(unsigned int dfd, char __user *filename, struct compat_timeval __user *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 struct timeval tv[2];
96
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +110097 if (t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
99 get_user(tv[0].tv_usec, &t[0].tv_usec) ||
100 get_user(tv[1].tv_sec, &t[1].tv_sec) ||
101 get_user(tv[1].tv_usec, &t[1].tv_usec))
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +1100102 return -EFAULT;
103 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800104 return do_utimes(dfd, filename, t ? tv : NULL);
105}
106
107asmlinkage long compat_sys_utimes(char __user *filename, struct compat_timeval __user *t)
108{
109 return compat_sys_futimesat(AT_FDCWD, filename, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
112asmlinkage long compat_sys_newstat(char __user * filename,
113 struct compat_stat __user *statbuf)
114{
115 struct kstat stat;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800116 int error = vfs_stat_fd(AT_FDCWD, filename, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 if (!error)
119 error = cp_compat_stat(&stat, statbuf);
120 return error;
121}
122
123asmlinkage long compat_sys_newlstat(char __user * filename,
124 struct compat_stat __user *statbuf)
125{
126 struct kstat stat;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800127 int error = vfs_lstat_fd(AT_FDCWD, filename, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 if (!error)
130 error = cp_compat_stat(&stat, statbuf);
131 return error;
132}
133
Kyle McMartin82d821d2006-03-24 03:18:20 -0800134#ifndef __ARCH_WANT_STAT64
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +1100135asmlinkage long compat_sys_newfstatat(unsigned int dfd, char __user *filename,
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800136 struct compat_stat __user *statbuf, int flag)
137{
138 struct kstat stat;
139 int error = -EINVAL;
140
141 if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
142 goto out;
143
144 if (flag & AT_SYMLINK_NOFOLLOW)
145 error = vfs_lstat_fd(dfd, filename, &stat);
146 else
147 error = vfs_stat_fd(dfd, filename, &stat);
148
149 if (!error)
150 error = cp_compat_stat(&stat, statbuf);
151
152out:
153 return error;
154}
Kyle McMartin82d821d2006-03-24 03:18:20 -0800155#endif
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157asmlinkage long compat_sys_newfstat(unsigned int fd,
158 struct compat_stat __user * statbuf)
159{
160 struct kstat stat;
161 int error = vfs_fstat(fd, &stat);
162
163 if (!error)
164 error = cp_compat_stat(&stat, statbuf);
165 return error;
166}
167
168static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
169{
170
171 if (sizeof ubuf->f_blocks == 4) {
172 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) &
173 0xffffffff00000000ULL)
174 return -EOVERFLOW;
175 /* f_files and f_ffree may be -1; it's okay
176 * to stuff that into 32 bits */
177 if (kbuf->f_files != 0xffffffffffffffffULL
178 && (kbuf->f_files & 0xffffffff00000000ULL))
179 return -EOVERFLOW;
180 if (kbuf->f_ffree != 0xffffffffffffffffULL
181 && (kbuf->f_ffree & 0xffffffff00000000ULL))
182 return -EOVERFLOW;
183 }
184 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
185 __put_user(kbuf->f_type, &ubuf->f_type) ||
186 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
187 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
188 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
189 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
190 __put_user(kbuf->f_files, &ubuf->f_files) ||
191 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
192 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
193 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
194 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
195 __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
196 __put_user(0, &ubuf->f_spare[0]) ||
197 __put_user(0, &ubuf->f_spare[1]) ||
198 __put_user(0, &ubuf->f_spare[2]) ||
199 __put_user(0, &ubuf->f_spare[3]) ||
200 __put_user(0, &ubuf->f_spare[4]))
201 return -EFAULT;
202 return 0;
203}
204
205/*
206 * The following statfs calls are copies of code from fs/open.c and
207 * should be checked against those from time to time
208 */
209asmlinkage long compat_sys_statfs(const char __user *path, struct compat_statfs __user *buf)
210{
211 struct nameidata nd;
212 int error;
213
214 error = user_path_walk(path, &nd);
215 if (!error) {
216 struct kstatfs tmp;
David Howells726c3342006-06-23 02:02:58 -0700217 error = vfs_statfs(nd.dentry, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800218 if (!error)
219 error = put_compat_statfs(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 path_release(&nd);
221 }
222 return error;
223}
224
225asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf)
226{
227 struct file * file;
228 struct kstatfs tmp;
229 int error;
230
231 error = -EBADF;
232 file = fget(fd);
233 if (!file)
234 goto out;
David Howells726c3342006-06-23 02:02:58 -0700235 error = vfs_statfs(file->f_dentry, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800236 if (!error)
237 error = put_compat_statfs(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 fput(file);
239out:
240 return error;
241}
242
243static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
244{
245 if (sizeof ubuf->f_blocks == 4) {
246 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) &
247 0xffffffff00000000ULL)
248 return -EOVERFLOW;
249 /* f_files and f_ffree may be -1; it's okay
250 * to stuff that into 32 bits */
251 if (kbuf->f_files != 0xffffffffffffffffULL
252 && (kbuf->f_files & 0xffffffff00000000ULL))
253 return -EOVERFLOW;
254 if (kbuf->f_ffree != 0xffffffffffffffffULL
255 && (kbuf->f_ffree & 0xffffffff00000000ULL))
256 return -EOVERFLOW;
257 }
258 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
259 __put_user(kbuf->f_type, &ubuf->f_type) ||
260 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
261 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
262 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
263 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
264 __put_user(kbuf->f_files, &ubuf->f_files) ||
265 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
266 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
267 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
268 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
269 __put_user(kbuf->f_frsize, &ubuf->f_frsize))
270 return -EFAULT;
271 return 0;
272}
273
274asmlinkage long compat_sys_statfs64(const char __user *path, compat_size_t sz, struct compat_statfs64 __user *buf)
275{
276 struct nameidata nd;
277 int error;
278
279 if (sz != sizeof(*buf))
280 return -EINVAL;
281
282 error = user_path_walk(path, &nd);
283 if (!error) {
284 struct kstatfs tmp;
David Howells726c3342006-06-23 02:02:58 -0700285 error = vfs_statfs(nd.dentry, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800286 if (!error)
287 error = put_compat_statfs64(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 path_release(&nd);
289 }
290 return error;
291}
292
293asmlinkage long compat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf)
294{
295 struct file * file;
296 struct kstatfs tmp;
297 int error;
298
299 if (sz != sizeof(*buf))
300 return -EINVAL;
301
302 error = -EBADF;
303 file = fget(fd);
304 if (!file)
305 goto out;
David Howells726c3342006-06-23 02:02:58 -0700306 error = vfs_statfs(file->f_dentry, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800307 if (!error)
308 error = put_compat_statfs64(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 fput(file);
310out:
311 return error;
312}
313
314/* ioctl32 stuff, used by sparc64, parisc, s390x, ppc64, x86_64, MIPS */
315
316#define IOCTL_HASHSIZE 256
317static struct ioctl_trans *ioctl32_hash_table[IOCTL_HASHSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319static inline unsigned long ioctl32_hash(unsigned long cmd)
320{
321 return (((cmd >> 6) ^ (cmd >> 4) ^ cmd)) % IOCTL_HASHSIZE;
322}
323
324static void ioctl32_insert_translation(struct ioctl_trans *trans)
325{
326 unsigned long hash;
327 struct ioctl_trans *t;
328
329 hash = ioctl32_hash (trans->cmd);
330 if (!ioctl32_hash_table[hash])
331 ioctl32_hash_table[hash] = trans;
332 else {
333 t = ioctl32_hash_table[hash];
334 while (t->next)
335 t = t->next;
336 trans->next = NULL;
337 t->next = trans;
338 }
339}
340
341static int __init init_sys32_ioctl(void)
342{
343 int i;
344
345 for (i = 0; i < ioctl_table_size; i++) {
346 if (ioctl_start[i].next != 0) {
347 printk("ioctl translation %d bad\n",i);
348 return -1;
349 }
350
351 ioctl32_insert_translation(&ioctl_start[i]);
352 }
353 return 0;
354}
355
356__initcall(init_sys32_ioctl);
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358static void compat_ioctl_error(struct file *filp, unsigned int fd,
359 unsigned int cmd, unsigned long arg)
360{
361 char buf[10];
362 char *fn = "?";
363 char *path;
364
365 /* find the name of the device. */
366 path = (char *)__get_free_page(GFP_KERNEL);
367 if (path) {
368 fn = d_path(filp->f_dentry, filp->f_vfsmnt, path, PAGE_SIZE);
369 if (IS_ERR(fn))
370 fn = "?";
371 }
372
373 sprintf(buf,"'%c'", (cmd>>24) & 0x3f);
374 if (!isprint(buf[1]))
375 sprintf(buf, "%02x", buf[1]);
Andi Kleenbebfa102006-06-26 13:56:52 +0200376 compat_printk("ioctl32(%s:%d): Unknown cmd fd(%d) "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 "cmd(%08x){%s} arg(%08x) on %s\n",
378 current->comm, current->pid,
379 (int)fd, (unsigned int)cmd, buf,
380 (unsigned int)arg, fn);
381
382 if (path)
383 free_page((unsigned long)path);
384}
385
386asmlinkage long compat_sys_ioctl(unsigned int fd, unsigned int cmd,
387 unsigned long arg)
388{
389 struct file *filp;
390 int error = -EBADF;
391 struct ioctl_trans *t;
392 int fput_needed;
393
394 filp = fget_light(fd, &fput_needed);
395 if (!filp)
396 goto out;
397
398 /* RED-PEN how should LSM module know it's handling 32bit? */
399 error = security_file_ioctl(filp, cmd, arg);
400 if (error)
401 goto out_fput;
402
403 /*
404 * To allow the compat_ioctl handlers to be self contained
405 * we need to check the common ioctls here first.
406 * Just handle them with the standard handlers below.
407 */
408 switch (cmd) {
409 case FIOCLEX:
410 case FIONCLEX:
411 case FIONBIO:
412 case FIOASYNC:
413 case FIOQSIZE:
414 break;
415
416 case FIBMAP:
417 case FIGETBSZ:
418 case FIONREAD:
419 if (S_ISREG(filp->f_dentry->d_inode->i_mode))
420 break;
421 /*FALL THROUGH*/
422
423 default:
424 if (filp->f_op && filp->f_op->compat_ioctl) {
425 error = filp->f_op->compat_ioctl(filp, cmd, arg);
426 if (error != -ENOIOCTLCMD)
427 goto out_fput;
428 }
429
430 if (!filp->f_op ||
431 (!filp->f_op->ioctl && !filp->f_op->unlocked_ioctl))
432 goto do_ioctl;
433 break;
434 }
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 for (t = ioctl32_hash_table[ioctl32_hash(cmd)]; t; t = t->next) {
437 if (t->cmd == cmd)
438 goto found_handler;
439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if (S_ISSOCK(filp->f_dentry->d_inode->i_mode) &&
442 cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
443 error = siocdevprivate_ioctl(fd, cmd, arg);
444 } else {
445 static int count;
446
447 if (++count <= 50)
448 compat_ioctl_error(filp, fd, cmd, arg);
449 error = -EINVAL;
450 }
451
452 goto out_fput;
453
454 found_handler:
455 if (t->handler) {
456 lock_kernel();
457 error = t->handler(fd, cmd, arg, filp);
458 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 goto out_fput;
460 }
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 do_ioctl:
463 error = vfs_ioctl(filp, fd, cmd, arg);
464 out_fput:
465 fput_light(filp, fput_needed);
466 out:
467 return error;
468}
469
470static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
471{
472 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
473 __get_user(kfl->l_type, &ufl->l_type) ||
474 __get_user(kfl->l_whence, &ufl->l_whence) ||
475 __get_user(kfl->l_start, &ufl->l_start) ||
476 __get_user(kfl->l_len, &ufl->l_len) ||
477 __get_user(kfl->l_pid, &ufl->l_pid))
478 return -EFAULT;
479 return 0;
480}
481
482static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
483{
484 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
485 __put_user(kfl->l_type, &ufl->l_type) ||
486 __put_user(kfl->l_whence, &ufl->l_whence) ||
487 __put_user(kfl->l_start, &ufl->l_start) ||
488 __put_user(kfl->l_len, &ufl->l_len) ||
489 __put_user(kfl->l_pid, &ufl->l_pid))
490 return -EFAULT;
491 return 0;
492}
493
494#ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
495static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
496{
497 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
498 __get_user(kfl->l_type, &ufl->l_type) ||
499 __get_user(kfl->l_whence, &ufl->l_whence) ||
500 __get_user(kfl->l_start, &ufl->l_start) ||
501 __get_user(kfl->l_len, &ufl->l_len) ||
502 __get_user(kfl->l_pid, &ufl->l_pid))
503 return -EFAULT;
504 return 0;
505}
506#endif
507
508#ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
509static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
510{
511 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
512 __put_user(kfl->l_type, &ufl->l_type) ||
513 __put_user(kfl->l_whence, &ufl->l_whence) ||
514 __put_user(kfl->l_start, &ufl->l_start) ||
515 __put_user(kfl->l_len, &ufl->l_len) ||
516 __put_user(kfl->l_pid, &ufl->l_pid))
517 return -EFAULT;
518 return 0;
519}
520#endif
521
522asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd,
523 unsigned long arg)
524{
525 mm_segment_t old_fs;
526 struct flock f;
527 long ret;
528
529 switch (cmd) {
530 case F_GETLK:
531 case F_SETLK:
532 case F_SETLKW:
533 ret = get_compat_flock(&f, compat_ptr(arg));
534 if (ret != 0)
535 break;
536 old_fs = get_fs();
537 set_fs(KERNEL_DS);
538 ret = sys_fcntl(fd, cmd, (unsigned long)&f);
539 set_fs(old_fs);
540 if (cmd == F_GETLK && ret == 0) {
NeilBrown2520f142006-01-08 01:02:40 -0800541 /* GETLK was successfule and we need to return the data...
542 * but it needs to fit in the compat structure.
543 * l_start shouldn't be too big, unless the original
544 * start + end is greater than COMPAT_OFF_T_MAX, in which
545 * case the app was asking for trouble, so we return
546 * -EOVERFLOW in that case.
547 * l_len could be too big, in which case we just truncate it,
548 * and only allow the app to see that part of the conflicting
549 * lock that might make sense to it anyway
550 */
551
552 if (f.l_start > COMPAT_OFF_T_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 ret = -EOVERFLOW;
NeilBrown2520f142006-01-08 01:02:40 -0800554 if (f.l_len > COMPAT_OFF_T_MAX)
555 f.l_len = COMPAT_OFF_T_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (ret == 0)
557 ret = put_compat_flock(&f, compat_ptr(arg));
558 }
559 break;
560
561 case F_GETLK64:
562 case F_SETLK64:
563 case F_SETLKW64:
564 ret = get_compat_flock64(&f, compat_ptr(arg));
565 if (ret != 0)
566 break;
567 old_fs = get_fs();
568 set_fs(KERNEL_DS);
569 ret = sys_fcntl(fd, (cmd == F_GETLK64) ? F_GETLK :
570 ((cmd == F_SETLK64) ? F_SETLK : F_SETLKW),
571 (unsigned long)&f);
572 set_fs(old_fs);
573 if (cmd == F_GETLK64 && ret == 0) {
NeilBrown2520f142006-01-08 01:02:40 -0800574 /* need to return lock information - see above for commentary */
575 if (f.l_start > COMPAT_LOFF_T_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 ret = -EOVERFLOW;
NeilBrown2520f142006-01-08 01:02:40 -0800577 if (f.l_len > COMPAT_LOFF_T_MAX)
578 f.l_len = COMPAT_LOFF_T_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (ret == 0)
580 ret = put_compat_flock64(&f, compat_ptr(arg));
581 }
582 break;
583
584 default:
585 ret = sys_fcntl(fd, cmd, arg);
586 break;
587 }
588 return ret;
589}
590
591asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd,
592 unsigned long arg)
593{
594 if ((cmd == F_GETLK64) || (cmd == F_SETLK64) || (cmd == F_SETLKW64))
595 return -EINVAL;
596 return compat_sys_fcntl64(fd, cmd, arg);
597}
598
599asmlinkage long
600compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p)
601{
602 long ret;
603 aio_context_t ctx64;
604
605 mm_segment_t oldfs = get_fs();
606 if (unlikely(get_user(ctx64, ctx32p)))
607 return -EFAULT;
608
609 set_fs(KERNEL_DS);
610 /* The __user pointer cast is valid because of the set_fs() */
611 ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64);
612 set_fs(oldfs);
613 /* truncating is ok because it's a user address */
614 if (!ret)
615 ret = put_user((u32) ctx64, ctx32p);
616 return ret;
617}
618
619asmlinkage long
620compat_sys_io_getevents(aio_context_t ctx_id,
621 unsigned long min_nr,
622 unsigned long nr,
623 struct io_event __user *events,
624 struct compat_timespec __user *timeout)
625{
626 long ret;
627 struct timespec t;
628 struct timespec __user *ut = NULL;
629
630 ret = -EFAULT;
631 if (unlikely(!access_ok(VERIFY_WRITE, events,
632 nr * sizeof(struct io_event))))
633 goto out;
634 if (timeout) {
635 if (get_compat_timespec(&t, timeout))
636 goto out;
637
638 ut = compat_alloc_user_space(sizeof(*ut));
639 if (copy_to_user(ut, &t, sizeof(t)) )
640 goto out;
641 }
642 ret = sys_io_getevents(ctx_id, min_nr, nr, events, ut);
643out:
644 return ret;
645}
646
647static inline long
648copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64)
649{
650 compat_uptr_t uptr;
651 int i;
652
653 for (i = 0; i < nr; ++i) {
654 if (get_user(uptr, ptr32 + i))
655 return -EFAULT;
656 if (put_user(compat_ptr(uptr), ptr64 + i))
657 return -EFAULT;
658 }
659 return 0;
660}
661
662#define MAX_AIO_SUBMITS (PAGE_SIZE/sizeof(struct iocb *))
663
664asmlinkage long
665compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb)
666{
667 struct iocb __user * __user *iocb64;
668 long ret;
669
670 if (unlikely(nr < 0))
671 return -EINVAL;
672
673 if (nr > MAX_AIO_SUBMITS)
674 nr = MAX_AIO_SUBMITS;
675
676 iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64));
677 ret = copy_iocb(nr, iocb, iocb64);
678 if (!ret)
679 ret = sys_io_submit(ctx_id, nr, iocb64);
680 return ret;
681}
682
683struct compat_ncp_mount_data {
684 compat_int_t version;
685 compat_uint_t ncp_fd;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700686 __compat_uid_t mounted_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 compat_pid_t wdog_pid;
688 unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
689 compat_uint_t time_out;
690 compat_uint_t retry_count;
691 compat_uint_t flags;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700692 __compat_uid_t uid;
693 __compat_gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 compat_mode_t file_mode;
695 compat_mode_t dir_mode;
696};
697
698struct compat_ncp_mount_data_v4 {
699 compat_int_t version;
700 compat_ulong_t flags;
701 compat_ulong_t mounted_uid;
702 compat_long_t wdog_pid;
703 compat_uint_t ncp_fd;
704 compat_uint_t time_out;
705 compat_uint_t retry_count;
706 compat_ulong_t uid;
707 compat_ulong_t gid;
708 compat_ulong_t file_mode;
709 compat_ulong_t dir_mode;
710};
711
712static void *do_ncp_super_data_conv(void *raw_data)
713{
714 int version = *(unsigned int *)raw_data;
715
716 if (version == 3) {
717 struct compat_ncp_mount_data *c_n = raw_data;
718 struct ncp_mount_data *n = raw_data;
719
720 n->dir_mode = c_n->dir_mode;
721 n->file_mode = c_n->file_mode;
722 n->gid = c_n->gid;
723 n->uid = c_n->uid;
724 memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
725 n->wdog_pid = c_n->wdog_pid;
726 n->mounted_uid = c_n->mounted_uid;
727 } else if (version == 4) {
728 struct compat_ncp_mount_data_v4 *c_n = raw_data;
729 struct ncp_mount_data_v4 *n = raw_data;
730
731 n->dir_mode = c_n->dir_mode;
732 n->file_mode = c_n->file_mode;
733 n->gid = c_n->gid;
734 n->uid = c_n->uid;
735 n->retry_count = c_n->retry_count;
736 n->time_out = c_n->time_out;
737 n->ncp_fd = c_n->ncp_fd;
738 n->wdog_pid = c_n->wdog_pid;
739 n->mounted_uid = c_n->mounted_uid;
740 n->flags = c_n->flags;
741 } else if (version != 5) {
742 return NULL;
743 }
744
745 return raw_data;
746}
747
748struct compat_smb_mount_data {
749 compat_int_t version;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700750 __compat_uid_t mounted_uid;
751 __compat_uid_t uid;
752 __compat_gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 compat_mode_t file_mode;
754 compat_mode_t dir_mode;
755};
756
757static void *do_smb_super_data_conv(void *raw_data)
758{
759 struct smb_mount_data *s = raw_data;
760 struct compat_smb_mount_data *c_s = raw_data;
761
762 if (c_s->version != SMB_MOUNT_OLDVERSION)
763 goto out;
764 s->dir_mode = c_s->dir_mode;
765 s->file_mode = c_s->file_mode;
766 s->gid = c_s->gid;
767 s->uid = c_s->uid;
768 s->mounted_uid = c_s->mounted_uid;
769 out:
770 return raw_data;
771}
772
David Howells9a9947b2005-04-18 10:54:51 -0700773struct compat_nfs_string {
774 compat_uint_t len;
David Howells5fc3e622005-04-27 15:39:03 -0700775 compat_uptr_t data;
David Howells9a9947b2005-04-18 10:54:51 -0700776};
777
778static inline void compat_nfs_string(struct nfs_string *dst,
779 struct compat_nfs_string *src)
780{
781 dst->data = compat_ptr(src->data);
782 dst->len = src->len;
783}
784
785struct compat_nfs4_mount_data_v1 {
786 compat_int_t version;
787 compat_int_t flags;
788 compat_int_t rsize;
789 compat_int_t wsize;
790 compat_int_t timeo;
791 compat_int_t retrans;
792 compat_int_t acregmin;
793 compat_int_t acregmax;
794 compat_int_t acdirmin;
795 compat_int_t acdirmax;
796 struct compat_nfs_string client_addr;
797 struct compat_nfs_string mnt_path;
798 struct compat_nfs_string hostname;
799 compat_uint_t host_addrlen;
David Howells5fc3e622005-04-27 15:39:03 -0700800 compat_uptr_t host_addr;
David Howells9a9947b2005-04-18 10:54:51 -0700801 compat_int_t proto;
802 compat_int_t auth_flavourlen;
David Howells5fc3e622005-04-27 15:39:03 -0700803 compat_uptr_t auth_flavours;
David Howells9a9947b2005-04-18 10:54:51 -0700804};
805
806static int do_nfs4_super_data_conv(void *raw_data)
807{
808 int version = *(compat_uint_t *) raw_data;
809
810 if (version == 1) {
811 struct compat_nfs4_mount_data_v1 *raw = raw_data;
812 struct nfs4_mount_data *real = raw_data;
813
814 /* copy the fields backwards */
815 real->auth_flavours = compat_ptr(raw->auth_flavours);
816 real->auth_flavourlen = raw->auth_flavourlen;
817 real->proto = raw->proto;
818 real->host_addr = compat_ptr(raw->host_addr);
819 real->host_addrlen = raw->host_addrlen;
820 compat_nfs_string(&real->hostname, &raw->hostname);
821 compat_nfs_string(&real->mnt_path, &raw->mnt_path);
822 compat_nfs_string(&real->client_addr, &raw->client_addr);
823 real->acdirmax = raw->acdirmax;
824 real->acdirmin = raw->acdirmin;
825 real->acregmax = raw->acregmax;
826 real->acregmin = raw->acregmin;
827 real->retrans = raw->retrans;
828 real->timeo = raw->timeo;
829 real->wsize = raw->wsize;
830 real->rsize = raw->rsize;
831 real->flags = raw->flags;
832 real->version = raw->version;
833 }
834 else {
835 return -EINVAL;
836 }
837
838 return 0;
839}
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841#define SMBFS_NAME "smbfs"
842#define NCPFS_NAME "ncpfs"
David Howells9a9947b2005-04-18 10:54:51 -0700843#define NFS4_NAME "nfs4"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845asmlinkage long compat_sys_mount(char __user * dev_name, char __user * dir_name,
846 char __user * type, unsigned long flags,
847 void __user * data)
848{
849 unsigned long type_page;
850 unsigned long data_page;
851 unsigned long dev_page;
852 char *dir_page;
853 int retval;
854
855 retval = copy_mount_options (type, &type_page);
856 if (retval < 0)
857 goto out;
858
859 dir_page = getname(dir_name);
860 retval = PTR_ERR(dir_page);
861 if (IS_ERR(dir_page))
862 goto out1;
863
864 retval = copy_mount_options (dev_name, &dev_page);
865 if (retval < 0)
866 goto out2;
867
868 retval = copy_mount_options (data, &data_page);
869 if (retval < 0)
870 goto out3;
871
872 retval = -EINVAL;
873
Andrey Mirkin822191a2006-12-06 20:31:35 -0800874 if (type_page && data_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (!strcmp((char *)type_page, SMBFS_NAME)) {
876 do_smb_super_data_conv((void *)data_page);
877 } else if (!strcmp((char *)type_page, NCPFS_NAME)) {
878 do_ncp_super_data_conv((void *)data_page);
David Howells9a9947b2005-04-18 10:54:51 -0700879 } else if (!strcmp((char *)type_page, NFS4_NAME)) {
880 if (do_nfs4_super_data_conv((void *) data_page))
881 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883 }
884
885 lock_kernel();
886 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
887 flags, (void*)data_page);
888 unlock_kernel();
889
David Howells9a9947b2005-04-18 10:54:51 -0700890 out4:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 free_page(data_page);
892 out3:
893 free_page(dev_page);
894 out2:
895 putname(dir_page);
896 out1:
897 free_page(type_page);
898 out:
899 return retval;
900}
901
902#define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
903#define COMPAT_ROUND_UP(x) (((x)+sizeof(compat_long_t)-1) & \
904 ~(sizeof(compat_long_t)-1))
905
906struct compat_old_linux_dirent {
907 compat_ulong_t d_ino;
908 compat_ulong_t d_offset;
909 unsigned short d_namlen;
910 char d_name[1];
911};
912
913struct compat_readdir_callback {
914 struct compat_old_linux_dirent __user *dirent;
915 int result;
916};
917
918static int compat_fillonedir(void *__buf, const char *name, int namlen,
David Howellsafefdbb2006-10-03 01:13:46 -0700919 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 struct compat_readdir_callback *buf = __buf;
922 struct compat_old_linux_dirent __user *dirent;
David Howellsafefdbb2006-10-03 01:13:46 -0700923 compat_ulong_t d_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 if (buf->result)
926 return -EINVAL;
David Howellsafefdbb2006-10-03 01:13:46 -0700927 d_ino = ino;
928 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
929 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 buf->result++;
931 dirent = buf->dirent;
932 if (!access_ok(VERIFY_WRITE, dirent,
933 (unsigned long)(dirent->d_name + namlen + 1) -
934 (unsigned long)dirent))
935 goto efault;
David Howellsafefdbb2006-10-03 01:13:46 -0700936 if ( __put_user(d_ino, &dirent->d_ino) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 __put_user(offset, &dirent->d_offset) ||
938 __put_user(namlen, &dirent->d_namlen) ||
939 __copy_to_user(dirent->d_name, name, namlen) ||
940 __put_user(0, dirent->d_name + namlen))
941 goto efault;
942 return 0;
943efault:
944 buf->result = -EFAULT;
945 return -EFAULT;
946}
947
948asmlinkage long compat_sys_old_readdir(unsigned int fd,
949 struct compat_old_linux_dirent __user *dirent, unsigned int count)
950{
951 int error;
952 struct file *file;
953 struct compat_readdir_callback buf;
954
955 error = -EBADF;
956 file = fget(fd);
957 if (!file)
958 goto out;
959
960 buf.result = 0;
961 buf.dirent = dirent;
962
963 error = vfs_readdir(file, compat_fillonedir, &buf);
964 if (error >= 0)
965 error = buf.result;
966
967 fput(file);
968out:
969 return error;
970}
971
972struct compat_linux_dirent {
973 compat_ulong_t d_ino;
974 compat_ulong_t d_off;
975 unsigned short d_reclen;
976 char d_name[1];
977};
978
979struct compat_getdents_callback {
980 struct compat_linux_dirent __user *current_dir;
981 struct compat_linux_dirent __user *previous;
982 int count;
983 int error;
984};
985
986static int compat_filldir(void *__buf, const char *name, int namlen,
David Howellsafefdbb2006-10-03 01:13:46 -0700987 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
989 struct compat_linux_dirent __user * dirent;
990 struct compat_getdents_callback *buf = __buf;
David Howellsafefdbb2006-10-03 01:13:46 -0700991 compat_ulong_t d_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 int reclen = COMPAT_ROUND_UP(NAME_OFFSET(dirent) + namlen + 2);
993
994 buf->error = -EINVAL; /* only used if we fail.. */
995 if (reclen > buf->count)
996 return -EINVAL;
David Howellsafefdbb2006-10-03 01:13:46 -0700997 d_ino = ino;
998 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
999 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 dirent = buf->previous;
1001 if (dirent) {
1002 if (__put_user(offset, &dirent->d_off))
1003 goto efault;
1004 }
1005 dirent = buf->current_dir;
David Howellsafefdbb2006-10-03 01:13:46 -07001006 if (__put_user(d_ino, &dirent->d_ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 goto efault;
1008 if (__put_user(reclen, &dirent->d_reclen))
1009 goto efault;
1010 if (copy_to_user(dirent->d_name, name, namlen))
1011 goto efault;
1012 if (__put_user(0, dirent->d_name + namlen))
1013 goto efault;
1014 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
1015 goto efault;
1016 buf->previous = dirent;
1017 dirent = (void __user *)dirent + reclen;
1018 buf->current_dir = dirent;
1019 buf->count -= reclen;
1020 return 0;
1021efault:
1022 buf->error = -EFAULT;
1023 return -EFAULT;
1024}
1025
1026asmlinkage long compat_sys_getdents(unsigned int fd,
1027 struct compat_linux_dirent __user *dirent, unsigned int count)
1028{
1029 struct file * file;
1030 struct compat_linux_dirent __user * lastdirent;
1031 struct compat_getdents_callback buf;
1032 int error;
1033
1034 error = -EFAULT;
1035 if (!access_ok(VERIFY_WRITE, dirent, count))
1036 goto out;
1037
1038 error = -EBADF;
1039 file = fget(fd);
1040 if (!file)
1041 goto out;
1042
1043 buf.current_dir = dirent;
1044 buf.previous = NULL;
1045 buf.count = count;
1046 buf.error = 0;
1047
1048 error = vfs_readdir(file, compat_filldir, &buf);
1049 if (error < 0)
1050 goto out_putf;
1051 error = buf.error;
1052 lastdirent = buf.previous;
1053 if (lastdirent) {
1054 if (put_user(file->f_pos, &lastdirent->d_off))
1055 error = -EFAULT;
1056 else
1057 error = count - buf.count;
1058 }
1059
1060out_putf:
1061 fput(file);
1062out:
1063 return error;
1064}
1065
1066#ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64
1067#define COMPAT_ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1))
1068
1069struct compat_getdents_callback64 {
1070 struct linux_dirent64 __user *current_dir;
1071 struct linux_dirent64 __user *previous;
1072 int count;
1073 int error;
1074};
1075
1076static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset,
David Howellsafefdbb2006-10-03 01:13:46 -07001077 u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
1079 struct linux_dirent64 __user *dirent;
1080 struct compat_getdents_callback64 *buf = __buf;
1081 int jj = NAME_OFFSET(dirent);
1082 int reclen = COMPAT_ROUND_UP64(jj + namlen + 1);
1083 u64 off;
1084
1085 buf->error = -EINVAL; /* only used if we fail.. */
1086 if (reclen > buf->count)
1087 return -EINVAL;
1088 dirent = buf->previous;
1089
1090 if (dirent) {
1091 if (__put_user_unaligned(offset, &dirent->d_off))
1092 goto efault;
1093 }
1094 dirent = buf->current_dir;
1095 if (__put_user_unaligned(ino, &dirent->d_ino))
1096 goto efault;
1097 off = 0;
1098 if (__put_user_unaligned(off, &dirent->d_off))
1099 goto efault;
1100 if (__put_user(reclen, &dirent->d_reclen))
1101 goto efault;
1102 if (__put_user(d_type, &dirent->d_type))
1103 goto efault;
1104 if (copy_to_user(dirent->d_name, name, namlen))
1105 goto efault;
1106 if (__put_user(0, dirent->d_name + namlen))
1107 goto efault;
1108 buf->previous = dirent;
1109 dirent = (void __user *)dirent + reclen;
1110 buf->current_dir = dirent;
1111 buf->count -= reclen;
1112 return 0;
1113efault:
1114 buf->error = -EFAULT;
1115 return -EFAULT;
1116}
1117
1118asmlinkage long compat_sys_getdents64(unsigned int fd,
1119 struct linux_dirent64 __user * dirent, unsigned int count)
1120{
1121 struct file * file;
1122 struct linux_dirent64 __user * lastdirent;
1123 struct compat_getdents_callback64 buf;
1124 int error;
1125
1126 error = -EFAULT;
1127 if (!access_ok(VERIFY_WRITE, dirent, count))
1128 goto out;
1129
1130 error = -EBADF;
1131 file = fget(fd);
1132 if (!file)
1133 goto out;
1134
1135 buf.current_dir = dirent;
1136 buf.previous = NULL;
1137 buf.count = count;
1138 buf.error = 0;
1139
1140 error = vfs_readdir(file, compat_filldir64, &buf);
1141 if (error < 0)
1142 goto out_putf;
1143 error = buf.error;
1144 lastdirent = buf.previous;
1145 if (lastdirent) {
1146 typeof(lastdirent->d_off) d_off = file->f_pos;
1147 __put_user_unaligned(d_off, &lastdirent->d_off);
1148 error = count - buf.count;
1149 }
1150
1151out_putf:
1152 fput(file);
1153out:
1154 return error;
1155}
1156#endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
1157
1158static ssize_t compat_do_readv_writev(int type, struct file *file,
1159 const struct compat_iovec __user *uvector,
1160 unsigned long nr_segs, loff_t *pos)
1161{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 compat_ssize_t tot_len;
1163 struct iovec iovstack[UIO_FASTIOV];
1164 struct iovec *iov=iovstack, *vector;
1165 ssize_t ret;
1166 int seg;
1167 io_fn_t fn;
1168 iov_fn_t fnv;
1169
1170 /*
1171 * SuS says "The readv() function *may* fail if the iovcnt argument
1172 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
1173 * traditionally returned zero for zero segments, so...
1174 */
1175 ret = 0;
1176 if (nr_segs == 0)
1177 goto out;
1178
1179 /*
1180 * First get the "struct iovec" from user memory and
1181 * verify all the pointers
1182 */
1183 ret = -EINVAL;
1184 if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0))
1185 goto out;
1186 if (!file->f_op)
1187 goto out;
1188 if (nr_segs > UIO_FASTIOV) {
1189 ret = -ENOMEM;
1190 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
1191 if (!iov)
1192 goto out;
1193 }
1194 ret = -EFAULT;
1195 if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
1196 goto out;
1197
1198 /*
1199 * Single unix specification:
1200 * We should -EINVAL if an element length is not >= 0 and fitting an
1201 * ssize_t. The total length is fitting an ssize_t
1202 *
1203 * Be careful here because iov_len is a size_t not an ssize_t
1204 */
1205 tot_len = 0;
1206 vector = iov;
1207 ret = -EINVAL;
1208 for (seg = 0 ; seg < nr_segs; seg++) {
1209 compat_ssize_t tmp = tot_len;
1210 compat_ssize_t len;
1211 compat_uptr_t buf;
1212
1213 if (__get_user(len, &uvector->iov_len) ||
1214 __get_user(buf, &uvector->iov_base)) {
1215 ret = -EFAULT;
1216 goto out;
1217 }
1218 if (len < 0) /* size_t not fitting an compat_ssize_t .. */
1219 goto out;
1220 tot_len += len;
1221 if (tot_len < tmp) /* maths overflow on the compat_ssize_t */
1222 goto out;
1223 vector->iov_base = compat_ptr(buf);
1224 vector->iov_len = (compat_size_t) len;
1225 uvector++;
1226 vector++;
1227 }
1228 if (tot_len == 0) {
1229 ret = 0;
1230 goto out;
1231 }
1232
1233 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001234 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 goto out;
1236
James Morrise7edf9c2006-04-26 02:45:03 -04001237 ret = security_file_permission(file, type == READ ? MAY_READ:MAY_WRITE);
1238 if (ret)
1239 goto out;
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 fnv = NULL;
1242 if (type == READ) {
1243 fn = file->f_op->read;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001244 fnv = file->f_op->aio_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 } else {
1246 fn = (io_fn_t)file->f_op->write;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001247 fnv = file->f_op->aio_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001250 if (fnv)
1251 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
1252 pos, fnv);
1253 else
1254 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256out:
1257 if (iov != iovstack)
1258 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -04001259 if ((ret + (type == READ)) > 0) {
1260 struct dentry *dentry = file->f_dentry;
1261 if (type == READ)
1262 fsnotify_access(dentry);
1263 else
1264 fsnotify_modify(dentry);
1265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return ret;
1267}
1268
1269asmlinkage ssize_t
1270compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1271{
1272 struct file *file;
1273 ssize_t ret = -EBADF;
1274
1275 file = fget(fd);
1276 if (!file)
1277 return -EBADF;
1278
1279 if (!(file->f_mode & FMODE_READ))
1280 goto out;
1281
1282 ret = -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001283 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 goto out;
1285
1286 ret = compat_do_readv_writev(READ, file, vec, vlen, &file->f_pos);
1287
1288out:
1289 fput(file);
1290 return ret;
1291}
1292
1293asmlinkage ssize_t
1294compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1295{
1296 struct file *file;
1297 ssize_t ret = -EBADF;
1298
1299 file = fget(fd);
1300 if (!file)
1301 return -EBADF;
1302 if (!(file->f_mode & FMODE_WRITE))
1303 goto out;
1304
1305 ret = -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001306 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 goto out;
1308
1309 ret = compat_do_readv_writev(WRITE, file, vec, vlen, &file->f_pos);
1310
1311out:
1312 fput(file);
1313 return ret;
1314}
1315
Andi Kleend2610202006-05-01 12:15:48 -07001316asmlinkage long
1317compat_sys_vmsplice(int fd, const struct compat_iovec __user *iov32,
1318 unsigned int nr_segs, unsigned int flags)
1319{
1320 unsigned i;
Al Viro90cbad62006-10-10 22:44:17 +01001321 struct iovec __user *iov;
Jens Axboe98232d52006-05-04 09:13:49 +02001322 if (nr_segs > UIO_MAXIOV)
Andi Kleend2610202006-05-01 12:15:48 -07001323 return -EINVAL;
1324 iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
1325 for (i = 0; i < nr_segs; i++) {
1326 struct compat_iovec v;
1327 if (get_user(v.iov_base, &iov32[i].iov_base) ||
1328 get_user(v.iov_len, &iov32[i].iov_len) ||
1329 put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
1330 put_user(v.iov_len, &iov[i].iov_len))
1331 return -EFAULT;
1332 }
1333 return sys_vmsplice(fd, iov, nr_segs, flags);
1334}
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336/*
Miklos Szeredie922efc2005-09-06 15:18:25 -07001337 * Exactly like fs/open.c:sys_open(), except that it doesn't set the
1338 * O_LARGEFILE flag.
1339 */
1340asmlinkage long
1341compat_sys_open(const char __user *filename, int flags, int mode)
1342{
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001343 return do_sys_open(AT_FDCWD, filename, flags, mode);
1344}
1345
1346/*
1347 * Exactly like fs/open.c:sys_openat(), except that it doesn't set the
1348 * O_LARGEFILE flag.
1349 */
1350asmlinkage long
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +11001351compat_sys_openat(unsigned int dfd, const char __user *filename, int flags, int mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001352{
1353 return do_sys_open(dfd, filename, flags, mode);
Miklos Szeredie922efc2005-09-06 15:18:25 -07001354}
1355
1356/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 * compat_count() counts the number of arguments/envelopes. It is basically
1358 * a copy of count() from fs/exec.c, except that it works with 32 bit argv
1359 * and envp pointers.
1360 */
1361static int compat_count(compat_uptr_t __user *argv, int max)
1362{
1363 int i = 0;
1364
1365 if (argv != NULL) {
1366 for (;;) {
1367 compat_uptr_t p;
1368
1369 if (get_user(p, argv))
1370 return -EFAULT;
1371 if (!p)
1372 break;
1373 argv++;
1374 if(++i > max)
1375 return -E2BIG;
1376 }
1377 }
1378 return i;
1379}
1380
1381/*
1382 * compat_copy_strings() is basically a copy of copy_strings() from fs/exec.c
1383 * except that it works with 32 bit argv and envp pointers.
1384 */
1385static int compat_copy_strings(int argc, compat_uptr_t __user *argv,
1386 struct linux_binprm *bprm)
1387{
1388 struct page *kmapped_page = NULL;
1389 char *kaddr = NULL;
1390 int ret;
1391
1392 while (argc-- > 0) {
1393 compat_uptr_t str;
1394 int len;
1395 unsigned long pos;
1396
1397 if (get_user(str, argv+argc) ||
1398 !(len = strnlen_user(compat_ptr(str), bprm->p))) {
1399 ret = -EFAULT;
1400 goto out;
1401 }
1402
1403 if (bprm->p < len) {
1404 ret = -E2BIG;
1405 goto out;
1406 }
1407
1408 bprm->p -= len;
1409 /* XXX: add architecture specific overflow check here. */
1410 pos = bprm->p;
1411
1412 while (len > 0) {
1413 int i, new, err;
1414 int offset, bytes_to_copy;
1415 struct page *page;
1416
1417 offset = pos % PAGE_SIZE;
1418 i = pos/PAGE_SIZE;
1419 page = bprm->page[i];
1420 new = 0;
1421 if (!page) {
1422 page = alloc_page(GFP_HIGHUSER);
1423 bprm->page[i] = page;
1424 if (!page) {
1425 ret = -ENOMEM;
1426 goto out;
1427 }
1428 new = 1;
1429 }
1430
1431 if (page != kmapped_page) {
1432 if (kmapped_page)
1433 kunmap(kmapped_page);
1434 kmapped_page = page;
1435 kaddr = kmap(kmapped_page);
1436 }
1437 if (new && offset)
1438 memset(kaddr, 0, offset);
1439 bytes_to_copy = PAGE_SIZE - offset;
1440 if (bytes_to_copy > len) {
1441 bytes_to_copy = len;
1442 if (new)
1443 memset(kaddr+offset+len, 0,
1444 PAGE_SIZE-offset-len);
1445 }
1446 err = copy_from_user(kaddr+offset, compat_ptr(str),
1447 bytes_to_copy);
1448 if (err) {
1449 ret = -EFAULT;
1450 goto out;
1451 }
1452
1453 pos += bytes_to_copy;
1454 str += bytes_to_copy;
1455 len -= bytes_to_copy;
1456 }
1457 }
1458 ret = 0;
1459out:
1460 if (kmapped_page)
1461 kunmap(kmapped_page);
1462 return ret;
1463}
1464
1465#ifdef CONFIG_MMU
1466
1467#define free_arg_pages(bprm) do { } while (0)
1468
1469#else
1470
1471static inline void free_arg_pages(struct linux_binprm *bprm)
1472{
1473 int i;
1474
1475 for (i = 0; i < MAX_ARG_PAGES; i++) {
1476 if (bprm->page[i])
1477 __free_page(bprm->page[i]);
1478 bprm->page[i] = NULL;
1479 }
1480}
1481
1482#endif /* CONFIG_MMU */
1483
1484/*
1485 * compat_do_execve() is mostly a copy of do_execve(), with the exception
1486 * that it processes 32 bit argv and envp pointers.
1487 */
1488int compat_do_execve(char * filename,
1489 compat_uptr_t __user *argv,
1490 compat_uptr_t __user *envp,
1491 struct pt_regs * regs)
1492{
1493 struct linux_binprm *bprm;
1494 struct file *file;
1495 int retval;
1496 int i;
1497
1498 retval = -ENOMEM;
Oliver Neukum11b0b5a2006-03-25 03:08:13 -08001499 bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (!bprm)
1501 goto out_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 file = open_exec(filename);
1504 retval = PTR_ERR(file);
1505 if (IS_ERR(file))
1506 goto out_kfree;
1507
1508 sched_exec();
1509
1510 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1511 bprm->file = file;
1512 bprm->filename = filename;
1513 bprm->interp = filename;
1514 bprm->mm = mm_alloc();
1515 retval = -ENOMEM;
1516 if (!bprm->mm)
1517 goto out_file;
1518
1519 retval = init_new_context(current, bprm->mm);
1520 if (retval < 0)
1521 goto out_mm;
1522
1523 bprm->argc = compat_count(argv, bprm->p / sizeof(compat_uptr_t));
1524 if ((retval = bprm->argc) < 0)
1525 goto out_mm;
1526
1527 bprm->envc = compat_count(envp, bprm->p / sizeof(compat_uptr_t));
1528 if ((retval = bprm->envc) < 0)
1529 goto out_mm;
1530
1531 retval = security_bprm_alloc(bprm);
1532 if (retval)
1533 goto out;
1534
1535 retval = prepare_binprm(bprm);
1536 if (retval < 0)
1537 goto out;
1538
1539 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1540 if (retval < 0)
1541 goto out;
1542
1543 bprm->exec = bprm->p;
1544 retval = compat_copy_strings(bprm->envc, envp, bprm);
1545 if (retval < 0)
1546 goto out;
1547
1548 retval = compat_copy_strings(bprm->argc, argv, bprm);
1549 if (retval < 0)
1550 goto out;
1551
1552 retval = search_binary_handler(bprm, regs);
1553 if (retval >= 0) {
1554 free_arg_pages(bprm);
1555
1556 /* execve success */
1557 security_bprm_free(bprm);
David S. Miller4a805e82005-09-14 21:40:00 -07001558 acct_update_integrals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 kfree(bprm);
1560 return retval;
1561 }
1562
1563out:
1564 /* Something went wrong, return the inode and free the argument pages*/
1565 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1566 struct page * page = bprm->page[i];
1567 if (page)
1568 __free_page(page);
1569 }
1570
1571 if (bprm->security)
1572 security_bprm_free(bprm);
1573
1574out_mm:
1575 if (bprm->mm)
1576 mmdrop(bprm->mm);
1577
1578out_file:
1579 if (bprm->file) {
1580 allow_write_access(bprm->file);
1581 fput(bprm->file);
1582 }
1583
1584out_kfree:
1585 kfree(bprm);
1586
1587out_ret:
1588 return retval;
1589}
1590
1591#define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
1592
1593#define ROUND_UP(x,y) (((x)+(y)-1)/(y))
1594
1595/*
1596 * Ooo, nasty. We need here to frob 32-bit unsigned longs to
1597 * 64-bit unsigned longs.
1598 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001599static
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1601 unsigned long *fdset)
1602{
1603 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1604 if (ufdset) {
1605 unsigned long odd;
1606
1607 if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
1608 return -EFAULT;
1609
1610 odd = nr & 1UL;
1611 nr &= ~1UL;
1612 while (nr) {
1613 unsigned long h, l;
1614 __get_user(l, ufdset);
1615 __get_user(h, ufdset+1);
1616 ufdset += 2;
1617 *fdset++ = h << 32 | l;
1618 nr -= 2;
1619 }
1620 if (odd)
1621 __get_user(*fdset, ufdset);
1622 } else {
1623 /* Tricky, must clear full unsigned long in the
1624 * kernel fdset at the end, this makes sure that
1625 * actually happens.
1626 */
1627 memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
1628 }
1629 return 0;
1630}
1631
Arjan van de Ven858119e2006-01-14 13:20:43 -08001632static
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633void compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1634 unsigned long *fdset)
1635{
1636 unsigned long odd;
1637 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1638
1639 if (!ufdset)
1640 return;
1641
1642 odd = nr & 1UL;
1643 nr &= ~1UL;
1644 while (nr) {
1645 unsigned long h, l;
1646 l = *fdset++;
1647 h = l >> 32;
1648 __put_user(l, ufdset);
1649 __put_user(h, ufdset+1);
1650 ufdset += 2;
1651 nr -= 2;
1652 }
1653 if (odd)
1654 __put_user(*fdset, ufdset);
1655}
1656
1657
1658/*
1659 * This is a virtual copy of sys_select from fs/select.c and probably
1660 * should be compared to it from time to time
1661 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663/*
1664 * We can actually return ERESTARTSYS instead of EINTR, but I'd
1665 * like to be certain this leads to no problems. So I return
1666 * EINTR just for safety.
1667 *
1668 * Update: ERESTARTSYS breaks at least the xview clock binary, so
1669 * I'm trying ERESTARTNOHAND which restart only when you want to.
1670 */
1671#define MAX_SELECT_SECONDS \
1672 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
1673
David Woodhouse9f729492006-01-18 17:44:05 -08001674int compat_core_sys_select(int n, compat_ulong_t __user *inp,
1675 compat_ulong_t __user *outp, compat_ulong_t __user *exp, s64 *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
1677 fd_set_bits fds;
1678 char *bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 int size, max_fdset, ret = -EINVAL;
Linus Torvaldsa4531ed2005-09-09 15:10:52 -07001680 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 if (n < 0)
1683 goto out_nofds;
1684
1685 /* max_fdset can increase, so grab it once to avoid race */
Linus Torvaldsac5b8b62005-09-09 15:42:34 -07001686 rcu_read_lock();
Linus Torvaldsa4531ed2005-09-09 15:10:52 -07001687 fdt = files_fdtable(current->files);
1688 max_fdset = fdt->max_fdset;
Linus Torvaldsac5b8b62005-09-09 15:42:34 -07001689 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (n > max_fdset)
1691 n = max_fdset;
1692
1693 /*
1694 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
1695 * since we used fdset we need to allocate memory in units of
1696 * long-words.
1697 */
1698 ret = -ENOMEM;
1699 size = FDS_BYTES(n);
Vadim Lobanov68c34312006-03-28 01:56:35 -08001700 bits = kmalloc(6 * size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (!bits)
1702 goto out_nofds;
1703 fds.in = (unsigned long *) bits;
1704 fds.out = (unsigned long *) (bits + size);
1705 fds.ex = (unsigned long *) (bits + 2*size);
1706 fds.res_in = (unsigned long *) (bits + 3*size);
1707 fds.res_out = (unsigned long *) (bits + 4*size);
1708 fds.res_ex = (unsigned long *) (bits + 5*size);
1709
1710 if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
1711 (ret = compat_get_fd_set(n, outp, fds.out)) ||
1712 (ret = compat_get_fd_set(n, exp, fds.ex)))
1713 goto out;
1714 zero_fd_set(n, fds.res_in);
1715 zero_fd_set(n, fds.res_out);
1716 zero_fd_set(n, fds.res_ex);
1717
David Woodhouse9f729492006-01-18 17:44:05 -08001718 ret = do_select(n, &fds, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 if (ret < 0)
1721 goto out;
1722 if (!ret) {
1723 ret = -ERESTARTNOHAND;
1724 if (signal_pending(current))
1725 goto out;
1726 ret = 0;
1727 }
1728
1729 compat_set_fd_set(n, inp, fds.res_in);
1730 compat_set_fd_set(n, outp, fds.res_out);
1731 compat_set_fd_set(n, exp, fds.res_ex);
1732
1733out:
Vadim Lobanov68c34312006-03-28 01:56:35 -08001734 kfree(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735out_nofds:
1736 return ret;
1737}
1738
David Woodhouse9f729492006-01-18 17:44:05 -08001739asmlinkage long compat_sys_select(int n, compat_ulong_t __user *inp,
1740 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1741 struct compat_timeval __user *tvp)
1742{
1743 s64 timeout = -1;
1744 struct compat_timeval tv;
1745 int ret;
1746
1747 if (tvp) {
1748 if (copy_from_user(&tv, tvp, sizeof(tv)))
1749 return -EFAULT;
1750
1751 if (tv.tv_sec < 0 || tv.tv_usec < 0)
1752 return -EINVAL;
1753
1754 /* Cast to u64 to make GCC stop complaining */
1755 if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
1756 timeout = -1; /* infinite */
1757 else {
David S. Miller7e732bf2006-01-19 16:40:42 -08001758 timeout = ROUND_UP(tv.tv_usec, 1000000/HZ);
David Woodhouse9f729492006-01-18 17:44:05 -08001759 timeout += tv.tv_sec * HZ;
1760 }
1761 }
1762
1763 ret = compat_core_sys_select(n, inp, outp, exp, &timeout);
1764
1765 if (tvp) {
Andrew Morton643a6542006-02-11 17:55:52 -08001766 struct compat_timeval rtv;
1767
David Woodhouse9f729492006-01-18 17:44:05 -08001768 if (current->personality & STICKY_TIMEOUTS)
1769 goto sticky;
Andrew Morton643a6542006-02-11 17:55:52 -08001770 rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
1771 rtv.tv_sec = timeout;
Andrew Morton74910e62006-02-17 13:52:58 -08001772 if (compat_timeval_compare(&rtv, &tv) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -08001773 rtv = tv;
1774 if (copy_to_user(tvp, &rtv, sizeof(rtv))) {
David Woodhouse9f729492006-01-18 17:44:05 -08001775sticky:
1776 /*
1777 * If an application puts its timeval in read-only
1778 * memory, we don't want the Linux-specific update to
1779 * the timeval to cause a fault after the select has
1780 * completed successfully. However, because we're not
1781 * updating the timeval, we can't restart the system
1782 * call.
1783 */
1784 if (ret == -ERESTARTNOHAND)
1785 ret = -EINTR;
1786 }
1787 }
1788
1789 return ret;
1790}
1791
1792#ifdef TIF_RESTORE_SIGMASK
1793asmlinkage long compat_sys_pselect7(int n, compat_ulong_t __user *inp,
1794 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1795 struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask,
1796 compat_size_t sigsetsize)
1797{
1798 compat_sigset_t ss32;
1799 sigset_t ksigmask, sigsaved;
Andrew Mortoncb82a6c2006-02-01 03:04:49 -08001800 s64 timeout = MAX_SCHEDULE_TIMEOUT;
David Woodhouse9f729492006-01-18 17:44:05 -08001801 struct compat_timespec ts;
1802 int ret;
1803
1804 if (tsp) {
1805 if (copy_from_user(&ts, tsp, sizeof(ts)))
1806 return -EFAULT;
1807
1808 if (ts.tv_sec < 0 || ts.tv_nsec < 0)
1809 return -EINVAL;
1810 }
1811
1812 if (sigmask) {
1813 if (sigsetsize != sizeof(compat_sigset_t))
1814 return -EINVAL;
1815 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1816 return -EFAULT;
1817 sigset_from_compat(&ksigmask, &ss32);
1818
1819 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1820 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1821 }
1822
1823 do {
1824 if (tsp) {
1825 if ((unsigned long)ts.tv_sec < MAX_SELECT_SECONDS) {
1826 timeout = ROUND_UP(ts.tv_nsec, 1000000000/HZ);
1827 timeout += ts.tv_sec * (unsigned long)HZ;
1828 ts.tv_sec = 0;
1829 ts.tv_nsec = 0;
1830 } else {
1831 ts.tv_sec -= MAX_SELECT_SECONDS;
1832 timeout = MAX_SELECT_SECONDS * HZ;
1833 }
1834 }
1835
1836 ret = compat_core_sys_select(n, inp, outp, exp, &timeout);
1837
1838 } while (!ret && !timeout && tsp && (ts.tv_sec || ts.tv_nsec));
1839
Heiko Carstens87c2b7c2006-11-02 22:06:58 -08001840 if (tsp) {
Andrew Morton643a6542006-02-11 17:55:52 -08001841 struct compat_timespec rts;
1842
Heiko Carstens87c2b7c2006-11-02 22:06:58 -08001843 if (current->personality & STICKY_TIMEOUTS)
1844 goto sticky;
1845
Andrew Morton643a6542006-02-11 17:55:52 -08001846 rts.tv_sec = timeout / HZ;
1847 rts.tv_nsec = (timeout % HZ) * (NSEC_PER_SEC/HZ);
1848 if (rts.tv_nsec >= NSEC_PER_SEC) {
1849 rts.tv_sec++;
1850 rts.tv_nsec -= NSEC_PER_SEC;
David Woodhouse9f729492006-01-18 17:44:05 -08001851 }
Andrew Morton74910e62006-02-17 13:52:58 -08001852 if (compat_timespec_compare(&rts, &ts) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -08001853 rts = ts;
Heiko Carstens87c2b7c2006-11-02 22:06:58 -08001854 if (copy_to_user(tsp, &rts, sizeof(rts))) {
1855sticky:
1856 /*
1857 * If an application puts its timeval in read-only
1858 * memory, we don't want the Linux-specific update to
1859 * the timeval to cause a fault after the select has
1860 * completed successfully. However, because we're not
1861 * updating the timeval, we can't restart the system
1862 * call.
1863 */
1864 if (ret == -ERESTARTNOHAND)
1865 ret = -EINTR;
1866 }
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