blob: ce982f6e8c80270be3fd23f8f9553c486626986e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/compat.c
3 *
4 * Kernel compatibililty routines for e.g. 32 bit syscall support
5 * on 64 bit kernels.
6 *
7 * Copyright (C) 2002 Stephen Rothwell, IBM Corporation
8 * Copyright (C) 1997-2000 Jakub Jelinek (jakub@redhat.com)
9 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
10 * Copyright (C) 2001,2002 Andi Kleen, SuSE Labs
11 * Copyright (C) 2003 Pavel Machek (pavel@suse.cz)
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 */
17
18#include <linux/linkage.h>
19#include <linux/compat.h>
20#include <linux/errno.h>
21#include <linux/time.h>
22#include <linux/fs.h>
23#include <linux/fcntl.h>
24#include <linux/namei.h>
25#include <linux/file.h>
26#include <linux/vfs.h>
27#include <linux/ioctl32.h>
28#include <linux/ioctl.h>
29#include <linux/init.h>
30#include <linux/sockios.h> /* for SIOCDEVPRIVATE */
31#include <linux/smb.h>
32#include <linux/smb_mount.h>
33#include <linux/ncp_mount.h>
David Howells9a9947b2005-04-18 10:54:51 -070034#include <linux/nfs4_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/smp_lock.h>
36#include <linux/syscalls.h>
37#include <linux/ctype.h>
38#include <linux/module.h>
39#include <linux/dirent.h>
Robert Love0eeca282005-07-12 17:06:03 -040040#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/highuid.h>
42#include <linux/sunrpc/svc.h>
43#include <linux/nfsd/nfsd.h>
44#include <linux/nfsd/syscall.h>
45#include <linux/personality.h>
46#include <linux/rwsem.h>
David S. Miller4a805e82005-09-14 21:40:00 -070047#include <linux/acct.h>
48#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <net/sock.h> /* siocdevprivate_ioctl */
51
52#include <asm/uaccess.h>
53#include <asm/mmu_context.h>
54#include <asm/ioctls.h>
55
David Woodhouse9f729492006-01-18 17:44:05 -080056extern void sigset_from_compat(sigset_t *set, compat_sigset_t *compat);
57
Andi Kleenbebfa102006-06-26 13:56:52 +020058int compat_log = 1;
59
60int compat_printk(const char *fmt, ...)
61{
62 va_list ap;
63 int ret;
64 if (!compat_log)
65 return 0;
66 va_start(ap, fmt);
67 ret = vprintk(fmt, ap);
68 va_end(ap);
69 return ret;
70}
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
73 * Not all architectures have sys_utime, so implement this in terms
74 * of sys_utimes.
75 */
76asmlinkage long compat_sys_utime(char __user *filename, struct compat_utimbuf __user *t)
77{
78 struct timeval tv[2];
79
80 if (t) {
81 if (get_user(tv[0].tv_sec, &t->actime) ||
82 get_user(tv[1].tv_sec, &t->modtime))
83 return -EFAULT;
84 tv[0].tv_usec = 0;
85 tv[1].tv_usec = 0;
86 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -080087 return do_utimes(AT_FDCWD, filename, t ? tv : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +110090asmlinkage long compat_sys_futimesat(unsigned int dfd, char __user *filename, struct compat_timeval __user *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 struct timeval tv[2];
93
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +110094 if (t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
96 get_user(tv[0].tv_usec, &t[0].tv_usec) ||
97 get_user(tv[1].tv_sec, &t[1].tv_sec) ||
98 get_user(tv[1].tv_usec, &t[1].tv_usec))
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +110099 return -EFAULT;
100 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800101 return do_utimes(dfd, filename, t ? tv : NULL);
102}
103
104asmlinkage long compat_sys_utimes(char __user *filename, struct compat_timeval __user *t)
105{
106 return compat_sys_futimesat(AT_FDCWD, filename, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109asmlinkage long compat_sys_newstat(char __user * filename,
110 struct compat_stat __user *statbuf)
111{
112 struct kstat stat;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800113 int error = vfs_stat_fd(AT_FDCWD, filename, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 if (!error)
116 error = cp_compat_stat(&stat, statbuf);
117 return error;
118}
119
120asmlinkage long compat_sys_newlstat(char __user * filename,
121 struct compat_stat __user *statbuf)
122{
123 struct kstat stat;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800124 int error = vfs_lstat_fd(AT_FDCWD, filename, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 if (!error)
127 error = cp_compat_stat(&stat, statbuf);
128 return error;
129}
130
Kyle McMartin82d821d2006-03-24 03:18:20 -0800131#ifndef __ARCH_WANT_STAT64
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +1100132asmlinkage long compat_sys_newfstatat(unsigned int dfd, char __user *filename,
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800133 struct compat_stat __user *statbuf, int flag)
134{
135 struct kstat stat;
136 int error = -EINVAL;
137
138 if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
139 goto out;
140
141 if (flag & AT_SYMLINK_NOFOLLOW)
142 error = vfs_lstat_fd(dfd, filename, &stat);
143 else
144 error = vfs_stat_fd(dfd, filename, &stat);
145
146 if (!error)
147 error = cp_compat_stat(&stat, statbuf);
148
149out:
150 return error;
151}
Kyle McMartin82d821d2006-03-24 03:18:20 -0800152#endif
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154asmlinkage long compat_sys_newfstat(unsigned int fd,
155 struct compat_stat __user * statbuf)
156{
157 struct kstat stat;
158 int error = vfs_fstat(fd, &stat);
159
160 if (!error)
161 error = cp_compat_stat(&stat, statbuf);
162 return error;
163}
164
165static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
166{
167
168 if (sizeof ubuf->f_blocks == 4) {
169 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) &
170 0xffffffff00000000ULL)
171 return -EOVERFLOW;
172 /* f_files and f_ffree may be -1; it's okay
173 * to stuff that into 32 bits */
174 if (kbuf->f_files != 0xffffffffffffffffULL
175 && (kbuf->f_files & 0xffffffff00000000ULL))
176 return -EOVERFLOW;
177 if (kbuf->f_ffree != 0xffffffffffffffffULL
178 && (kbuf->f_ffree & 0xffffffff00000000ULL))
179 return -EOVERFLOW;
180 }
181 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
182 __put_user(kbuf->f_type, &ubuf->f_type) ||
183 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
184 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
185 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
186 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
187 __put_user(kbuf->f_files, &ubuf->f_files) ||
188 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
189 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
190 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
191 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
192 __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
193 __put_user(0, &ubuf->f_spare[0]) ||
194 __put_user(0, &ubuf->f_spare[1]) ||
195 __put_user(0, &ubuf->f_spare[2]) ||
196 __put_user(0, &ubuf->f_spare[3]) ||
197 __put_user(0, &ubuf->f_spare[4]))
198 return -EFAULT;
199 return 0;
200}
201
202/*
203 * The following statfs calls are copies of code from fs/open.c and
204 * should be checked against those from time to time
205 */
206asmlinkage long compat_sys_statfs(const char __user *path, struct compat_statfs __user *buf)
207{
208 struct nameidata nd;
209 int error;
210
211 error = user_path_walk(path, &nd);
212 if (!error) {
213 struct kstatfs tmp;
David Howells726c3342006-06-23 02:02:58 -0700214 error = vfs_statfs(nd.dentry, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800215 if (!error)
216 error = put_compat_statfs(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 path_release(&nd);
218 }
219 return error;
220}
221
222asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf)
223{
224 struct file * file;
225 struct kstatfs tmp;
226 int error;
227
228 error = -EBADF;
229 file = fget(fd);
230 if (!file)
231 goto out;
David Howells726c3342006-06-23 02:02:58 -0700232 error = vfs_statfs(file->f_dentry, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800233 if (!error)
234 error = put_compat_statfs(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 fput(file);
236out:
237 return error;
238}
239
240static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
241{
242 if (sizeof ubuf->f_blocks == 4) {
243 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) &
244 0xffffffff00000000ULL)
245 return -EOVERFLOW;
246 /* f_files and f_ffree may be -1; it's okay
247 * to stuff that into 32 bits */
248 if (kbuf->f_files != 0xffffffffffffffffULL
249 && (kbuf->f_files & 0xffffffff00000000ULL))
250 return -EOVERFLOW;
251 if (kbuf->f_ffree != 0xffffffffffffffffULL
252 && (kbuf->f_ffree & 0xffffffff00000000ULL))
253 return -EOVERFLOW;
254 }
255 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
256 __put_user(kbuf->f_type, &ubuf->f_type) ||
257 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
258 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
259 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
260 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
261 __put_user(kbuf->f_files, &ubuf->f_files) ||
262 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
263 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
264 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
265 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
266 __put_user(kbuf->f_frsize, &ubuf->f_frsize))
267 return -EFAULT;
268 return 0;
269}
270
271asmlinkage long compat_sys_statfs64(const char __user *path, compat_size_t sz, struct compat_statfs64 __user *buf)
272{
273 struct nameidata nd;
274 int error;
275
276 if (sz != sizeof(*buf))
277 return -EINVAL;
278
279 error = user_path_walk(path, &nd);
280 if (!error) {
281 struct kstatfs tmp;
David Howells726c3342006-06-23 02:02:58 -0700282 error = vfs_statfs(nd.dentry, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800283 if (!error)
284 error = put_compat_statfs64(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 path_release(&nd);
286 }
287 return error;
288}
289
290asmlinkage long compat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf)
291{
292 struct file * file;
293 struct kstatfs tmp;
294 int error;
295
296 if (sz != sizeof(*buf))
297 return -EINVAL;
298
299 error = -EBADF;
300 file = fget(fd);
301 if (!file)
302 goto out;
David Howells726c3342006-06-23 02:02:58 -0700303 error = vfs_statfs(file->f_dentry, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800304 if (!error)
305 error = put_compat_statfs64(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 fput(file);
307out:
308 return error;
309}
310
311/* ioctl32 stuff, used by sparc64, parisc, s390x, ppc64, x86_64, MIPS */
312
313#define IOCTL_HASHSIZE 256
314static struct ioctl_trans *ioctl32_hash_table[IOCTL_HASHSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316extern struct ioctl_trans ioctl_start[];
317extern int ioctl_table_size;
318
319static 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 -0700841extern int copy_mount_options (const void __user *, unsigned long *);
842
843#define SMBFS_NAME "smbfs"
844#define NCPFS_NAME "ncpfs"
David Howells9a9947b2005-04-18 10:54:51 -0700845#define NFS4_NAME "nfs4"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847asmlinkage long compat_sys_mount(char __user * dev_name, char __user * dir_name,
848 char __user * type, unsigned long flags,
849 void __user * data)
850{
851 unsigned long type_page;
852 unsigned long data_page;
853 unsigned long dev_page;
854 char *dir_page;
855 int retval;
856
857 retval = copy_mount_options (type, &type_page);
858 if (retval < 0)
859 goto out;
860
861 dir_page = getname(dir_name);
862 retval = PTR_ERR(dir_page);
863 if (IS_ERR(dir_page))
864 goto out1;
865
866 retval = copy_mount_options (dev_name, &dev_page);
867 if (retval < 0)
868 goto out2;
869
870 retval = copy_mount_options (data, &data_page);
871 if (retval < 0)
872 goto out3;
873
874 retval = -EINVAL;
875
876 if (type_page) {
877 if (!strcmp((char *)type_page, SMBFS_NAME)) {
878 do_smb_super_data_conv((void *)data_page);
879 } else if (!strcmp((char *)type_page, NCPFS_NAME)) {
880 do_ncp_super_data_conv((void *)data_page);
David Howells9a9947b2005-04-18 10:54:51 -0700881 } else if (!strcmp((char *)type_page, NFS4_NAME)) {
882 if (do_nfs4_super_data_conv((void *) data_page))
883 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885 }
886
887 lock_kernel();
888 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
889 flags, (void*)data_page);
890 unlock_kernel();
891
David Howells9a9947b2005-04-18 10:54:51 -0700892 out4:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 free_page(data_page);
894 out3:
895 free_page(dev_page);
896 out2:
897 putname(dir_page);
898 out1:
899 free_page(type_page);
900 out:
901 return retval;
902}
903
904#define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
905#define COMPAT_ROUND_UP(x) (((x)+sizeof(compat_long_t)-1) & \
906 ~(sizeof(compat_long_t)-1))
907
908struct compat_old_linux_dirent {
909 compat_ulong_t d_ino;
910 compat_ulong_t d_offset;
911 unsigned short d_namlen;
912 char d_name[1];
913};
914
915struct compat_readdir_callback {
916 struct compat_old_linux_dirent __user *dirent;
917 int result;
918};
919
920static int compat_fillonedir(void *__buf, const char *name, int namlen,
921 loff_t offset, ino_t ino, unsigned int d_type)
922{
923 struct compat_readdir_callback *buf = __buf;
924 struct compat_old_linux_dirent __user *dirent;
925
926 if (buf->result)
927 return -EINVAL;
928 buf->result++;
929 dirent = buf->dirent;
930 if (!access_ok(VERIFY_WRITE, dirent,
931 (unsigned long)(dirent->d_name + namlen + 1) -
932 (unsigned long)dirent))
933 goto efault;
934 if ( __put_user(ino, &dirent->d_ino) ||
935 __put_user(offset, &dirent->d_offset) ||
936 __put_user(namlen, &dirent->d_namlen) ||
937 __copy_to_user(dirent->d_name, name, namlen) ||
938 __put_user(0, dirent->d_name + namlen))
939 goto efault;
940 return 0;
941efault:
942 buf->result = -EFAULT;
943 return -EFAULT;
944}
945
946asmlinkage long compat_sys_old_readdir(unsigned int fd,
947 struct compat_old_linux_dirent __user *dirent, unsigned int count)
948{
949 int error;
950 struct file *file;
951 struct compat_readdir_callback buf;
952
953 error = -EBADF;
954 file = fget(fd);
955 if (!file)
956 goto out;
957
958 buf.result = 0;
959 buf.dirent = dirent;
960
961 error = vfs_readdir(file, compat_fillonedir, &buf);
962 if (error >= 0)
963 error = buf.result;
964
965 fput(file);
966out:
967 return error;
968}
969
970struct compat_linux_dirent {
971 compat_ulong_t d_ino;
972 compat_ulong_t d_off;
973 unsigned short d_reclen;
974 char d_name[1];
975};
976
977struct compat_getdents_callback {
978 struct compat_linux_dirent __user *current_dir;
979 struct compat_linux_dirent __user *previous;
980 int count;
981 int error;
982};
983
984static int compat_filldir(void *__buf, const char *name, int namlen,
985 loff_t offset, ino_t ino, unsigned int d_type)
986{
987 struct compat_linux_dirent __user * dirent;
988 struct compat_getdents_callback *buf = __buf;
989 int reclen = COMPAT_ROUND_UP(NAME_OFFSET(dirent) + namlen + 2);
990
991 buf->error = -EINVAL; /* only used if we fail.. */
992 if (reclen > buf->count)
993 return -EINVAL;
994 dirent = buf->previous;
995 if (dirent) {
996 if (__put_user(offset, &dirent->d_off))
997 goto efault;
998 }
999 dirent = buf->current_dir;
1000 if (__put_user(ino, &dirent->d_ino))
1001 goto efault;
1002 if (__put_user(reclen, &dirent->d_reclen))
1003 goto efault;
1004 if (copy_to_user(dirent->d_name, name, namlen))
1005 goto efault;
1006 if (__put_user(0, dirent->d_name + namlen))
1007 goto efault;
1008 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
1009 goto efault;
1010 buf->previous = dirent;
1011 dirent = (void __user *)dirent + reclen;
1012 buf->current_dir = dirent;
1013 buf->count -= reclen;
1014 return 0;
1015efault:
1016 buf->error = -EFAULT;
1017 return -EFAULT;
1018}
1019
1020asmlinkage long compat_sys_getdents(unsigned int fd,
1021 struct compat_linux_dirent __user *dirent, unsigned int count)
1022{
1023 struct file * file;
1024 struct compat_linux_dirent __user * lastdirent;
1025 struct compat_getdents_callback buf;
1026 int error;
1027
1028 error = -EFAULT;
1029 if (!access_ok(VERIFY_WRITE, dirent, count))
1030 goto out;
1031
1032 error = -EBADF;
1033 file = fget(fd);
1034 if (!file)
1035 goto out;
1036
1037 buf.current_dir = dirent;
1038 buf.previous = NULL;
1039 buf.count = count;
1040 buf.error = 0;
1041
1042 error = vfs_readdir(file, compat_filldir, &buf);
1043 if (error < 0)
1044 goto out_putf;
1045 error = buf.error;
1046 lastdirent = buf.previous;
1047 if (lastdirent) {
1048 if (put_user(file->f_pos, &lastdirent->d_off))
1049 error = -EFAULT;
1050 else
1051 error = count - buf.count;
1052 }
1053
1054out_putf:
1055 fput(file);
1056out:
1057 return error;
1058}
1059
1060#ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64
1061#define COMPAT_ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1))
1062
1063struct compat_getdents_callback64 {
1064 struct linux_dirent64 __user *current_dir;
1065 struct linux_dirent64 __user *previous;
1066 int count;
1067 int error;
1068};
1069
1070static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset,
1071 ino_t ino, unsigned int d_type)
1072{
1073 struct linux_dirent64 __user *dirent;
1074 struct compat_getdents_callback64 *buf = __buf;
1075 int jj = NAME_OFFSET(dirent);
1076 int reclen = COMPAT_ROUND_UP64(jj + namlen + 1);
1077 u64 off;
1078
1079 buf->error = -EINVAL; /* only used if we fail.. */
1080 if (reclen > buf->count)
1081 return -EINVAL;
1082 dirent = buf->previous;
1083
1084 if (dirent) {
1085 if (__put_user_unaligned(offset, &dirent->d_off))
1086 goto efault;
1087 }
1088 dirent = buf->current_dir;
1089 if (__put_user_unaligned(ino, &dirent->d_ino))
1090 goto efault;
1091 off = 0;
1092 if (__put_user_unaligned(off, &dirent->d_off))
1093 goto efault;
1094 if (__put_user(reclen, &dirent->d_reclen))
1095 goto efault;
1096 if (__put_user(d_type, &dirent->d_type))
1097 goto efault;
1098 if (copy_to_user(dirent->d_name, name, namlen))
1099 goto efault;
1100 if (__put_user(0, dirent->d_name + namlen))
1101 goto efault;
1102 buf->previous = dirent;
1103 dirent = (void __user *)dirent + reclen;
1104 buf->current_dir = dirent;
1105 buf->count -= reclen;
1106 return 0;
1107efault:
1108 buf->error = -EFAULT;
1109 return -EFAULT;
1110}
1111
1112asmlinkage long compat_sys_getdents64(unsigned int fd,
1113 struct linux_dirent64 __user * dirent, unsigned int count)
1114{
1115 struct file * file;
1116 struct linux_dirent64 __user * lastdirent;
1117 struct compat_getdents_callback64 buf;
1118 int error;
1119
1120 error = -EFAULT;
1121 if (!access_ok(VERIFY_WRITE, dirent, count))
1122 goto out;
1123
1124 error = -EBADF;
1125 file = fget(fd);
1126 if (!file)
1127 goto out;
1128
1129 buf.current_dir = dirent;
1130 buf.previous = NULL;
1131 buf.count = count;
1132 buf.error = 0;
1133
1134 error = vfs_readdir(file, compat_filldir64, &buf);
1135 if (error < 0)
1136 goto out_putf;
1137 error = buf.error;
1138 lastdirent = buf.previous;
1139 if (lastdirent) {
1140 typeof(lastdirent->d_off) d_off = file->f_pos;
1141 __put_user_unaligned(d_off, &lastdirent->d_off);
1142 error = count - buf.count;
1143 }
1144
1145out_putf:
1146 fput(file);
1147out:
1148 return error;
1149}
1150#endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
1151
1152static ssize_t compat_do_readv_writev(int type, struct file *file,
1153 const struct compat_iovec __user *uvector,
1154 unsigned long nr_segs, loff_t *pos)
1155{
1156 typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
1157 typedef ssize_t (*iov_fn_t)(struct file *, const struct iovec *, unsigned long, loff_t *);
1158
1159 compat_ssize_t tot_len;
1160 struct iovec iovstack[UIO_FASTIOV];
1161 struct iovec *iov=iovstack, *vector;
1162 ssize_t ret;
1163 int seg;
1164 io_fn_t fn;
1165 iov_fn_t fnv;
1166
1167 /*
1168 * SuS says "The readv() function *may* fail if the iovcnt argument
1169 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
1170 * traditionally returned zero for zero segments, so...
1171 */
1172 ret = 0;
1173 if (nr_segs == 0)
1174 goto out;
1175
1176 /*
1177 * First get the "struct iovec" from user memory and
1178 * verify all the pointers
1179 */
1180 ret = -EINVAL;
1181 if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0))
1182 goto out;
1183 if (!file->f_op)
1184 goto out;
1185 if (nr_segs > UIO_FASTIOV) {
1186 ret = -ENOMEM;
1187 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
1188 if (!iov)
1189 goto out;
1190 }
1191 ret = -EFAULT;
1192 if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
1193 goto out;
1194
1195 /*
1196 * Single unix specification:
1197 * We should -EINVAL if an element length is not >= 0 and fitting an
1198 * ssize_t. The total length is fitting an ssize_t
1199 *
1200 * Be careful here because iov_len is a size_t not an ssize_t
1201 */
1202 tot_len = 0;
1203 vector = iov;
1204 ret = -EINVAL;
1205 for (seg = 0 ; seg < nr_segs; seg++) {
1206 compat_ssize_t tmp = tot_len;
1207 compat_ssize_t len;
1208 compat_uptr_t buf;
1209
1210 if (__get_user(len, &uvector->iov_len) ||
1211 __get_user(buf, &uvector->iov_base)) {
1212 ret = -EFAULT;
1213 goto out;
1214 }
1215 if (len < 0) /* size_t not fitting an compat_ssize_t .. */
1216 goto out;
1217 tot_len += len;
1218 if (tot_len < tmp) /* maths overflow on the compat_ssize_t */
1219 goto out;
1220 vector->iov_base = compat_ptr(buf);
1221 vector->iov_len = (compat_size_t) len;
1222 uvector++;
1223 vector++;
1224 }
1225 if (tot_len == 0) {
1226 ret = 0;
1227 goto out;
1228 }
1229
1230 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001231 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 goto out;
1233
James Morrise7edf9c2006-04-26 02:45:03 -04001234 ret = security_file_permission(file, type == READ ? MAY_READ:MAY_WRITE);
1235 if (ret)
1236 goto out;
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 fnv = NULL;
1239 if (type == READ) {
1240 fn = file->f_op->read;
1241 fnv = file->f_op->readv;
1242 } else {
1243 fn = (io_fn_t)file->f_op->write;
1244 fnv = file->f_op->writev;
1245 }
1246 if (fnv) {
1247 ret = fnv(file, iov, nr_segs, pos);
1248 goto out;
1249 }
1250
1251 /* Do it by hand, with file-ops */
1252 ret = 0;
1253 vector = iov;
1254 while (nr_segs > 0) {
1255 void __user * base;
1256 size_t len;
1257 ssize_t nr;
1258
1259 base = vector->iov_base;
1260 len = vector->iov_len;
1261 vector++;
1262 nr_segs--;
1263
1264 nr = fn(file, base, len, pos);
1265
1266 if (nr < 0) {
1267 if (!ret) ret = nr;
1268 break;
1269 }
1270 ret += nr;
1271 if (nr != len)
1272 break;
1273 }
1274out:
1275 if (iov != iovstack)
1276 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -04001277 if ((ret + (type == READ)) > 0) {
1278 struct dentry *dentry = file->f_dentry;
1279 if (type == READ)
1280 fsnotify_access(dentry);
1281 else
1282 fsnotify_modify(dentry);
1283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return ret;
1285}
1286
1287asmlinkage ssize_t
1288compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1289{
1290 struct file *file;
1291 ssize_t ret = -EBADF;
1292
1293 file = fget(fd);
1294 if (!file)
1295 return -EBADF;
1296
1297 if (!(file->f_mode & FMODE_READ))
1298 goto out;
1299
1300 ret = -EINVAL;
1301 if (!file->f_op || (!file->f_op->readv && !file->f_op->read))
1302 goto out;
1303
1304 ret = compat_do_readv_writev(READ, file, vec, vlen, &file->f_pos);
1305
1306out:
1307 fput(file);
1308 return ret;
1309}
1310
1311asmlinkage ssize_t
1312compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1313{
1314 struct file *file;
1315 ssize_t ret = -EBADF;
1316
1317 file = fget(fd);
1318 if (!file)
1319 return -EBADF;
1320 if (!(file->f_mode & FMODE_WRITE))
1321 goto out;
1322
1323 ret = -EINVAL;
1324 if (!file->f_op || (!file->f_op->writev && !file->f_op->write))
1325 goto out;
1326
1327 ret = compat_do_readv_writev(WRITE, file, vec, vlen, &file->f_pos);
1328
1329out:
1330 fput(file);
1331 return ret;
1332}
1333
Andi Kleend2610202006-05-01 12:15:48 -07001334asmlinkage long
1335compat_sys_vmsplice(int fd, const struct compat_iovec __user *iov32,
1336 unsigned int nr_segs, unsigned int flags)
1337{
1338 unsigned i;
1339 struct iovec *iov;
Jens Axboe98232d52006-05-04 09:13:49 +02001340 if (nr_segs > UIO_MAXIOV)
Andi Kleend2610202006-05-01 12:15:48 -07001341 return -EINVAL;
1342 iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
1343 for (i = 0; i < nr_segs; i++) {
1344 struct compat_iovec v;
1345 if (get_user(v.iov_base, &iov32[i].iov_base) ||
1346 get_user(v.iov_len, &iov32[i].iov_len) ||
1347 put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
1348 put_user(v.iov_len, &iov[i].iov_len))
1349 return -EFAULT;
1350 }
1351 return sys_vmsplice(fd, iov, nr_segs, flags);
1352}
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354/*
Miklos Szeredie922efc2005-09-06 15:18:25 -07001355 * Exactly like fs/open.c:sys_open(), except that it doesn't set the
1356 * O_LARGEFILE flag.
1357 */
1358asmlinkage long
1359compat_sys_open(const char __user *filename, int flags, int mode)
1360{
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001361 return do_sys_open(AT_FDCWD, filename, flags, mode);
1362}
1363
1364/*
1365 * Exactly like fs/open.c:sys_openat(), except that it doesn't set the
1366 * O_LARGEFILE flag.
1367 */
1368asmlinkage long
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +11001369compat_sys_openat(unsigned int dfd, const char __user *filename, int flags, int mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001370{
1371 return do_sys_open(dfd, filename, flags, mode);
Miklos Szeredie922efc2005-09-06 15:18:25 -07001372}
1373
1374/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 * compat_count() counts the number of arguments/envelopes. It is basically
1376 * a copy of count() from fs/exec.c, except that it works with 32 bit argv
1377 * and envp pointers.
1378 */
1379static int compat_count(compat_uptr_t __user *argv, int max)
1380{
1381 int i = 0;
1382
1383 if (argv != NULL) {
1384 for (;;) {
1385 compat_uptr_t p;
1386
1387 if (get_user(p, argv))
1388 return -EFAULT;
1389 if (!p)
1390 break;
1391 argv++;
1392 if(++i > max)
1393 return -E2BIG;
1394 }
1395 }
1396 return i;
1397}
1398
1399/*
1400 * compat_copy_strings() is basically a copy of copy_strings() from fs/exec.c
1401 * except that it works with 32 bit argv and envp pointers.
1402 */
1403static int compat_copy_strings(int argc, compat_uptr_t __user *argv,
1404 struct linux_binprm *bprm)
1405{
1406 struct page *kmapped_page = NULL;
1407 char *kaddr = NULL;
1408 int ret;
1409
1410 while (argc-- > 0) {
1411 compat_uptr_t str;
1412 int len;
1413 unsigned long pos;
1414
1415 if (get_user(str, argv+argc) ||
1416 !(len = strnlen_user(compat_ptr(str), bprm->p))) {
1417 ret = -EFAULT;
1418 goto out;
1419 }
1420
1421 if (bprm->p < len) {
1422 ret = -E2BIG;
1423 goto out;
1424 }
1425
1426 bprm->p -= len;
1427 /* XXX: add architecture specific overflow check here. */
1428 pos = bprm->p;
1429
1430 while (len > 0) {
1431 int i, new, err;
1432 int offset, bytes_to_copy;
1433 struct page *page;
1434
1435 offset = pos % PAGE_SIZE;
1436 i = pos/PAGE_SIZE;
1437 page = bprm->page[i];
1438 new = 0;
1439 if (!page) {
1440 page = alloc_page(GFP_HIGHUSER);
1441 bprm->page[i] = page;
1442 if (!page) {
1443 ret = -ENOMEM;
1444 goto out;
1445 }
1446 new = 1;
1447 }
1448
1449 if (page != kmapped_page) {
1450 if (kmapped_page)
1451 kunmap(kmapped_page);
1452 kmapped_page = page;
1453 kaddr = kmap(kmapped_page);
1454 }
1455 if (new && offset)
1456 memset(kaddr, 0, offset);
1457 bytes_to_copy = PAGE_SIZE - offset;
1458 if (bytes_to_copy > len) {
1459 bytes_to_copy = len;
1460 if (new)
1461 memset(kaddr+offset+len, 0,
1462 PAGE_SIZE-offset-len);
1463 }
1464 err = copy_from_user(kaddr+offset, compat_ptr(str),
1465 bytes_to_copy);
1466 if (err) {
1467 ret = -EFAULT;
1468 goto out;
1469 }
1470
1471 pos += bytes_to_copy;
1472 str += bytes_to_copy;
1473 len -= bytes_to_copy;
1474 }
1475 }
1476 ret = 0;
1477out:
1478 if (kmapped_page)
1479 kunmap(kmapped_page);
1480 return ret;
1481}
1482
1483#ifdef CONFIG_MMU
1484
1485#define free_arg_pages(bprm) do { } while (0)
1486
1487#else
1488
1489static inline void free_arg_pages(struct linux_binprm *bprm)
1490{
1491 int i;
1492
1493 for (i = 0; i < MAX_ARG_PAGES; i++) {
1494 if (bprm->page[i])
1495 __free_page(bprm->page[i]);
1496 bprm->page[i] = NULL;
1497 }
1498}
1499
1500#endif /* CONFIG_MMU */
1501
1502/*
1503 * compat_do_execve() is mostly a copy of do_execve(), with the exception
1504 * that it processes 32 bit argv and envp pointers.
1505 */
1506int compat_do_execve(char * filename,
1507 compat_uptr_t __user *argv,
1508 compat_uptr_t __user *envp,
1509 struct pt_regs * regs)
1510{
1511 struct linux_binprm *bprm;
1512 struct file *file;
1513 int retval;
1514 int i;
1515
1516 retval = -ENOMEM;
Oliver Neukum11b0b5a2006-03-25 03:08:13 -08001517 bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 if (!bprm)
1519 goto out_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 file = open_exec(filename);
1522 retval = PTR_ERR(file);
1523 if (IS_ERR(file))
1524 goto out_kfree;
1525
1526 sched_exec();
1527
1528 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1529 bprm->file = file;
1530 bprm->filename = filename;
1531 bprm->interp = filename;
1532 bprm->mm = mm_alloc();
1533 retval = -ENOMEM;
1534 if (!bprm->mm)
1535 goto out_file;
1536
1537 retval = init_new_context(current, bprm->mm);
1538 if (retval < 0)
1539 goto out_mm;
1540
1541 bprm->argc = compat_count(argv, bprm->p / sizeof(compat_uptr_t));
1542 if ((retval = bprm->argc) < 0)
1543 goto out_mm;
1544
1545 bprm->envc = compat_count(envp, bprm->p / sizeof(compat_uptr_t));
1546 if ((retval = bprm->envc) < 0)
1547 goto out_mm;
1548
1549 retval = security_bprm_alloc(bprm);
1550 if (retval)
1551 goto out;
1552
1553 retval = prepare_binprm(bprm);
1554 if (retval < 0)
1555 goto out;
1556
1557 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1558 if (retval < 0)
1559 goto out;
1560
1561 bprm->exec = bprm->p;
1562 retval = compat_copy_strings(bprm->envc, envp, bprm);
1563 if (retval < 0)
1564 goto out;
1565
1566 retval = compat_copy_strings(bprm->argc, argv, bprm);
1567 if (retval < 0)
1568 goto out;
1569
1570 retval = search_binary_handler(bprm, regs);
1571 if (retval >= 0) {
1572 free_arg_pages(bprm);
1573
1574 /* execve success */
1575 security_bprm_free(bprm);
David S. Miller4a805e82005-09-14 21:40:00 -07001576 acct_update_integrals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 kfree(bprm);
1578 return retval;
1579 }
1580
1581out:
1582 /* Something went wrong, return the inode and free the argument pages*/
1583 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1584 struct page * page = bprm->page[i];
1585 if (page)
1586 __free_page(page);
1587 }
1588
1589 if (bprm->security)
1590 security_bprm_free(bprm);
1591
1592out_mm:
1593 if (bprm->mm)
1594 mmdrop(bprm->mm);
1595
1596out_file:
1597 if (bprm->file) {
1598 allow_write_access(bprm->file);
1599 fput(bprm->file);
1600 }
1601
1602out_kfree:
1603 kfree(bprm);
1604
1605out_ret:
1606 return retval;
1607}
1608
1609#define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
1610
1611#define ROUND_UP(x,y) (((x)+(y)-1)/(y))
1612
1613/*
1614 * Ooo, nasty. We need here to frob 32-bit unsigned longs to
1615 * 64-bit unsigned longs.
1616 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001617static
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1619 unsigned long *fdset)
1620{
1621 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1622 if (ufdset) {
1623 unsigned long odd;
1624
1625 if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
1626 return -EFAULT;
1627
1628 odd = nr & 1UL;
1629 nr &= ~1UL;
1630 while (nr) {
1631 unsigned long h, l;
1632 __get_user(l, ufdset);
1633 __get_user(h, ufdset+1);
1634 ufdset += 2;
1635 *fdset++ = h << 32 | l;
1636 nr -= 2;
1637 }
1638 if (odd)
1639 __get_user(*fdset, ufdset);
1640 } else {
1641 /* Tricky, must clear full unsigned long in the
1642 * kernel fdset at the end, this makes sure that
1643 * actually happens.
1644 */
1645 memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
1646 }
1647 return 0;
1648}
1649
Arjan van de Ven858119e2006-01-14 13:20:43 -08001650static
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651void compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1652 unsigned long *fdset)
1653{
1654 unsigned long odd;
1655 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1656
1657 if (!ufdset)
1658 return;
1659
1660 odd = nr & 1UL;
1661 nr &= ~1UL;
1662 while (nr) {
1663 unsigned long h, l;
1664 l = *fdset++;
1665 h = l >> 32;
1666 __put_user(l, ufdset);
1667 __put_user(h, ufdset+1);
1668 ufdset += 2;
1669 nr -= 2;
1670 }
1671 if (odd)
1672 __put_user(*fdset, ufdset);
1673}
1674
1675
1676/*
1677 * This is a virtual copy of sys_select from fs/select.c and probably
1678 * should be compared to it from time to time
1679 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681/*
1682 * We can actually return ERESTARTSYS instead of EINTR, but I'd
1683 * like to be certain this leads to no problems. So I return
1684 * EINTR just for safety.
1685 *
1686 * Update: ERESTARTSYS breaks at least the xview clock binary, so
1687 * I'm trying ERESTARTNOHAND which restart only when you want to.
1688 */
1689#define MAX_SELECT_SECONDS \
1690 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
1691
David Woodhouse9f729492006-01-18 17:44:05 -08001692int compat_core_sys_select(int n, compat_ulong_t __user *inp,
1693 compat_ulong_t __user *outp, compat_ulong_t __user *exp, s64 *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
1695 fd_set_bits fds;
1696 char *bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 int size, max_fdset, ret = -EINVAL;
Linus Torvaldsa4531ed2005-09-09 15:10:52 -07001698 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (n < 0)
1701 goto out_nofds;
1702
1703 /* max_fdset can increase, so grab it once to avoid race */
Linus Torvaldsac5b8b62005-09-09 15:42:34 -07001704 rcu_read_lock();
Linus Torvaldsa4531ed2005-09-09 15:10:52 -07001705 fdt = files_fdtable(current->files);
1706 max_fdset = fdt->max_fdset;
Linus Torvaldsac5b8b62005-09-09 15:42:34 -07001707 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (n > max_fdset)
1709 n = max_fdset;
1710
1711 /*
1712 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
1713 * since we used fdset we need to allocate memory in units of
1714 * long-words.
1715 */
1716 ret = -ENOMEM;
1717 size = FDS_BYTES(n);
Vadim Lobanov68c34312006-03-28 01:56:35 -08001718 bits = kmalloc(6 * size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 if (!bits)
1720 goto out_nofds;
1721 fds.in = (unsigned long *) bits;
1722 fds.out = (unsigned long *) (bits + size);
1723 fds.ex = (unsigned long *) (bits + 2*size);
1724 fds.res_in = (unsigned long *) (bits + 3*size);
1725 fds.res_out = (unsigned long *) (bits + 4*size);
1726 fds.res_ex = (unsigned long *) (bits + 5*size);
1727
1728 if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
1729 (ret = compat_get_fd_set(n, outp, fds.out)) ||
1730 (ret = compat_get_fd_set(n, exp, fds.ex)))
1731 goto out;
1732 zero_fd_set(n, fds.res_in);
1733 zero_fd_set(n, fds.res_out);
1734 zero_fd_set(n, fds.res_ex);
1735
David Woodhouse9f729492006-01-18 17:44:05 -08001736 ret = do_select(n, &fds, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 if (ret < 0)
1739 goto out;
1740 if (!ret) {
1741 ret = -ERESTARTNOHAND;
1742 if (signal_pending(current))
1743 goto out;
1744 ret = 0;
1745 }
1746
1747 compat_set_fd_set(n, inp, fds.res_in);
1748 compat_set_fd_set(n, outp, fds.res_out);
1749 compat_set_fd_set(n, exp, fds.res_ex);
1750
1751out:
Vadim Lobanov68c34312006-03-28 01:56:35 -08001752 kfree(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753out_nofds:
1754 return ret;
1755}
1756
David Woodhouse9f729492006-01-18 17:44:05 -08001757asmlinkage long compat_sys_select(int n, compat_ulong_t __user *inp,
1758 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1759 struct compat_timeval __user *tvp)
1760{
1761 s64 timeout = -1;
1762 struct compat_timeval tv;
1763 int ret;
1764
1765 if (tvp) {
1766 if (copy_from_user(&tv, tvp, sizeof(tv)))
1767 return -EFAULT;
1768
1769 if (tv.tv_sec < 0 || tv.tv_usec < 0)
1770 return -EINVAL;
1771
1772 /* Cast to u64 to make GCC stop complaining */
1773 if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
1774 timeout = -1; /* infinite */
1775 else {
David S. Miller7e732bf2006-01-19 16:40:42 -08001776 timeout = ROUND_UP(tv.tv_usec, 1000000/HZ);
David Woodhouse9f729492006-01-18 17:44:05 -08001777 timeout += tv.tv_sec * HZ;
1778 }
1779 }
1780
1781 ret = compat_core_sys_select(n, inp, outp, exp, &timeout);
1782
1783 if (tvp) {
Andrew Morton643a6542006-02-11 17:55:52 -08001784 struct compat_timeval rtv;
1785
David Woodhouse9f729492006-01-18 17:44:05 -08001786 if (current->personality & STICKY_TIMEOUTS)
1787 goto sticky;
Andrew Morton643a6542006-02-11 17:55:52 -08001788 rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
1789 rtv.tv_sec = timeout;
Andrew Morton74910e62006-02-17 13:52:58 -08001790 if (compat_timeval_compare(&rtv, &tv) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -08001791 rtv = tv;
1792 if (copy_to_user(tvp, &rtv, sizeof(rtv))) {
David Woodhouse9f729492006-01-18 17:44:05 -08001793sticky:
1794 /*
1795 * If an application puts its timeval in read-only
1796 * memory, we don't want the Linux-specific update to
1797 * the timeval to cause a fault after the select has
1798 * completed successfully. However, because we're not
1799 * updating the timeval, we can't restart the system
1800 * call.
1801 */
1802 if (ret == -ERESTARTNOHAND)
1803 ret = -EINTR;
1804 }
1805 }
1806
1807 return ret;
1808}
1809
1810#ifdef TIF_RESTORE_SIGMASK
1811asmlinkage long compat_sys_pselect7(int n, compat_ulong_t __user *inp,
1812 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1813 struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask,
1814 compat_size_t sigsetsize)
1815{
1816 compat_sigset_t ss32;
1817 sigset_t ksigmask, sigsaved;
Andrew Mortoncb82a6c2006-02-01 03:04:49 -08001818 s64 timeout = MAX_SCHEDULE_TIMEOUT;
David Woodhouse9f729492006-01-18 17:44:05 -08001819 struct compat_timespec ts;
1820 int ret;
1821
1822 if (tsp) {
1823 if (copy_from_user(&ts, tsp, sizeof(ts)))
1824 return -EFAULT;
1825
1826 if (ts.tv_sec < 0 || ts.tv_nsec < 0)
1827 return -EINVAL;
1828 }
1829
1830 if (sigmask) {
1831 if (sigsetsize != sizeof(compat_sigset_t))
1832 return -EINVAL;
1833 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1834 return -EFAULT;
1835 sigset_from_compat(&ksigmask, &ss32);
1836
1837 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1838 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1839 }
1840
1841 do {
1842 if (tsp) {
1843 if ((unsigned long)ts.tv_sec < MAX_SELECT_SECONDS) {
1844 timeout = ROUND_UP(ts.tv_nsec, 1000000000/HZ);
1845 timeout += ts.tv_sec * (unsigned long)HZ;
1846 ts.tv_sec = 0;
1847 ts.tv_nsec = 0;
1848 } else {
1849 ts.tv_sec -= MAX_SELECT_SECONDS;
1850 timeout = MAX_SELECT_SECONDS * HZ;
1851 }
1852 }
1853
1854 ret = compat_core_sys_select(n, inp, outp, exp, &timeout);
1855
1856 } while (!ret && !timeout && tsp && (ts.tv_sec || ts.tv_nsec));
1857
Andi Kleen75833342006-09-26 10:52:39 +02001858 if (ret == 0 && tsp && !(current->personality & STICKY_TIMEOUTS)) {
Andrew Morton643a6542006-02-11 17:55:52 -08001859 struct compat_timespec rts;
1860
1861 rts.tv_sec = timeout / HZ;
1862 rts.tv_nsec = (timeout % HZ) * (NSEC_PER_SEC/HZ);
1863 if (rts.tv_nsec >= NSEC_PER_SEC) {
1864 rts.tv_sec++;
1865 rts.tv_nsec -= NSEC_PER_SEC;
David Woodhouse9f729492006-01-18 17:44:05 -08001866 }
Andrew Morton74910e62006-02-17 13:52:58 -08001867 if (compat_timespec_compare(&rts, &ts) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -08001868 rts = ts;
Andi Kleen75833342006-09-26 10:52:39 +02001869 if (copy_to_user(tsp, &rts, sizeof(rts)))
1870 ret = -EFAULT;
David Woodhouse9f729492006-01-18 17:44:05 -08001871 }
1872
1873 if (ret == -ERESTARTNOHAND) {
1874 /*
1875 * Don't restore the signal mask yet. Let do_signal() deliver
1876 * the signal on the way back to userspace, before the signal
1877 * mask is restored.
1878 */
1879 if (sigmask) {
1880 memcpy(&current->saved_sigmask, &sigsaved,
1881 sizeof(sigsaved));
1882 set_thread_flag(TIF_RESTORE_SIGMASK);
1883 }
1884 } else if (sigmask)
1885 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1886
1887 return ret;
1888}
1889
1890asmlinkage long compat_sys_pselect6(int n, compat_ulong_t __user *inp,
1891 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1892 struct compat_timespec __user *tsp, void __user *sig)
1893{
1894 compat_size_t sigsetsize = 0;
1895 compat_uptr_t up = 0;
1896
1897 if (sig) {
1898 if (!access_ok(VERIFY_READ, sig,
1899 sizeof(compat_uptr_t)+sizeof(compat_size_t)) ||
1900 __get_user(up, (compat_uptr_t __user *)sig) ||
1901 __get_user(sigsetsize,
1902 (compat_size_t __user *)(sig+sizeof(up))))
1903 return -EFAULT;
1904 }
1905 return compat_sys_pselect7(n, inp, outp, exp, tsp, compat_ptr(up),
1906 sigsetsize);
1907}
1908
1909asmlinkage long compat_sys_ppoll(struct pollfd __user *ufds,
1910 unsigned int nfds, struct compat_timespec __user *tsp,
1911 const compat_sigset_t __user *sigmask, compat_size_t sigsetsize)
1912{
1913 compat_sigset_t ss32;
1914 sigset_t ksigmask, sigsaved;
1915 struct compat_timespec ts;
1916 s64 timeout = -1;
1917 int ret;
1918
1919 if (tsp) {
1920 if (copy_from_user(&ts, tsp, sizeof(ts)))
1921 return -EFAULT;
1922
1923 /* We assume that ts.tv_sec is always lower than
1924 the number of seconds that can be expressed in
1925 an s64. Otherwise the compiler bitches at us */
David S. Miller7e732bf2006-01-19 16:40:42 -08001926 timeout = ROUND_UP(ts.tv_nsec, 1000000000/HZ);
David Woodhouse9f729492006-01-18 17:44:05 -08001927 timeout += ts.tv_sec * HZ;
1928 }
1929
1930 if (sigmask) {
Alexey Dobriyan3835a9b2006-05-15 09:44:27 -07001931 if (sigsetsize != sizeof(compat_sigset_t))
David Woodhouse9f729492006-01-18 17:44:05 -08001932 return -EINVAL;
1933 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1934 return -EFAULT;
1935 sigset_from_compat(&ksigmask, &ss32);
1936
1937 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1938 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1939 }
1940
1941 ret = do_sys_poll(ufds, nfds, &timeout);
1942
1943 /* We can restart this syscall, usually */
1944 if (ret == -EINTR) {
1945 /*
1946 * Don't restore the signal mask yet. Let do_signal() deliver
1947 * the signal on the way back to userspace, before the signal
1948 * mask is restored.
1949 */
1950 if (sigmask) {
1951 memcpy(&current->saved_sigmask, &sigsaved,
1952 sizeof(sigsaved));
1953 set_thread_flag(TIF_RESTORE_SIGMASK);
1954 }
1955 ret = -ERESTARTNOHAND;
1956 } else if (sigmask)
1957 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1958
1959 if (tsp && timeout >= 0) {
Andrew Morton643a6542006-02-11 17:55:52 -08001960 struct compat_timespec rts;
1961
David Woodhouse9f729492006-01-18 17:44:05 -08001962 if (current->personality & STICKY_TIMEOUTS)
1963 goto sticky;
1964 /* Yes, we know it's actually an s64, but it's also positive. */
Andrew Morton643a6542006-02-11 17:55:52 -08001965 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
1966 1000;
1967 rts.tv_sec = timeout;
Andrew Morton74910e62006-02-17 13:52:58 -08001968 if (compat_timespec_compare(&rts, &ts) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -08001969 rts = ts;
1970 if (copy_to_user(tsp, &rts, sizeof(rts))) {
David Woodhouse9f729492006-01-18 17:44:05 -08001971sticky:
1972 /*
1973 * If an application puts its timeval in read-only
1974 * memory, we don't want the Linux-specific update to
1975 * the timeval to cause a fault after the select has
1976 * completed successfully. However, because we're not
1977 * updating the timeval, we can't restart the system
1978 * call.
1979 */
1980 if (ret == -ERESTARTNOHAND && timeout >= 0)
1981 ret = -EINTR;
1982 }
1983 }
1984
1985 return ret;
1986}
1987#endif /* TIF_RESTORE_SIGMASK */
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989#if defined(CONFIG_NFSD) || defined(CONFIG_NFSD_MODULE)
1990/* Stuff for NFS server syscalls... */
1991struct compat_nfsctl_svc {
1992 u16 svc32_port;
1993 s32 svc32_nthreads;
1994};
1995
1996struct compat_nfsctl_client {
1997 s8 cl32_ident[NFSCLNT_IDMAX+1];
1998 s32 cl32_naddr;
1999 struct in_addr cl32_addrlist[NFSCLNT_ADDRMAX];
2000 s32 cl32_fhkeytype;
2001 s32 cl32_fhkeylen;
2002 u8 cl32_fhkey[NFSCLNT_KEYMAX];
2003};
2004
2005struct compat_nfsctl_export {
2006 char ex32_client[NFSCLNT_IDMAX+1];
2007 char ex32_path[NFS_MAXPATHLEN+1];
2008 compat_dev_t ex32_dev;
2009 compat_ino_t ex32_ino;
2010 compat_int_t ex32_flags;
Stephen Rothwell202e5972005-09-06 15:16:40 -07002011 __compat_uid_t ex32_anon_uid;
2012 __compat_gid_t ex32_anon_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013};
2014
2015struct compat_nfsctl_fdparm {
2016 struct sockaddr gd32_addr;
2017 s8 gd32_path[NFS_MAXPATHLEN+1];
2018 compat_int_t gd32_version;
2019};
2020
2021struct compat_nfsctl_fsparm {
2022 struct sockaddr gd32_addr;
2023 s8 gd32_path[NFS_MAXPATHLEN+1];
2024 compat_int_t gd32_maxlen;
2025};
2026
2027struct compat_nfsctl_arg {
2028 compat_int_t ca32_version; /* safeguard */
2029 union {
2030 struct compat_nfsctl_svc u32_svc;
2031 struct compat_nfsctl_client u32_client;
2032 struct compat_nfsctl_export u32_export;
2033 struct compat_nfsctl_fdparm u32_getfd;
2034 struct compat_nfsctl_fsparm u32_getfs;
2035 } u;
2036#define ca32_svc u.u32_svc
2037#define ca32_client u.u32_client
2038#define ca32_export u.u32_export
2039#define ca32_getfd u.u32_getfd
2040#define ca32_getfs u.u32_getfs
2041};
2042
2043union compat_nfsctl_res {
2044 __u8 cr32_getfh[NFS_FHSIZE];
2045 struct knfsd_fh cr32_getfs;
2046};
2047
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002048static int compat_nfs_svc_trans(struct nfsctl_arg *karg,
2049 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002051 if (!access_ok(VERIFY_READ, &arg->ca32_svc, sizeof(arg->ca32_svc)) ||
2052 get_user(karg->ca_version, &arg->ca32_version) ||
2053 __get_user(karg->ca_svc.svc_port, &arg->ca32_svc.svc32_port) ||
2054 __get_user(karg->ca_svc.svc_nthreads,
2055 &arg->ca32_svc.svc32_nthreads))
2056 return -EFAULT;
2057 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058}
2059
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002060static int compat_nfs_clnt_trans(struct nfsctl_arg *karg,
2061 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002063 if (!access_ok(VERIFY_READ, &arg->ca32_client,
2064 sizeof(arg->ca32_client)) ||
2065 get_user(karg->ca_version, &arg->ca32_version) ||
2066 __copy_from_user(&karg->ca_client.cl_ident[0],
2067 &arg->ca32_client.cl32_ident[0],
2068 NFSCLNT_IDMAX) ||
2069 __get_user(karg->ca_client.cl_naddr,
2070 &arg->ca32_client.cl32_naddr) ||
2071 __copy_from_user(&karg->ca_client.cl_addrlist[0],
2072 &arg->ca32_client.cl32_addrlist[0],
2073 (sizeof(struct in_addr) * NFSCLNT_ADDRMAX)) ||
2074 __get_user(karg->ca_client.cl_fhkeytype,
2075 &arg->ca32_client.cl32_fhkeytype) ||
2076 __get_user(karg->ca_client.cl_fhkeylen,
2077 &arg->ca32_client.cl32_fhkeylen) ||
2078 __copy_from_user(&karg->ca_client.cl_fhkey[0],
2079 &arg->ca32_client.cl32_fhkey[0],
2080 NFSCLNT_KEYMAX))
2081 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084}
2085
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002086static int compat_nfs_exp_trans(struct nfsctl_arg *karg,
2087 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002089 if (!access_ok(VERIFY_READ, &arg->ca32_export,
2090 sizeof(arg->ca32_export)) ||
2091 get_user(karg->ca_version, &arg->ca32_version) ||
2092 __copy_from_user(&karg->ca_export.ex_client[0],
2093 &arg->ca32_export.ex32_client[0],
2094 NFSCLNT_IDMAX) ||
2095 __copy_from_user(&karg->ca_export.ex_path[0],
2096 &arg->ca32_export.ex32_path[0],
2097 NFS_MAXPATHLEN) ||
2098 __get_user(karg->ca_export.ex_dev,
2099 &arg->ca32_export.ex32_dev) ||
2100 __get_user(karg->ca_export.ex_ino,
2101 &arg->ca32_export.ex32_ino) ||
2102 __get_user(karg->ca_export.ex_flags,
2103 &arg->ca32_export.ex32_flags) ||
2104 __get_user(karg->ca_export.ex_anon_uid,
2105 &arg->ca32_export.ex32_anon_uid) ||
2106 __get_user(karg->ca_export.ex_anon_gid,
2107 &arg->ca32_export.ex32_anon_gid))
2108 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 SET_UID(karg->ca_export.ex_anon_uid, karg->ca_export.ex_anon_uid);
2110 SET_GID(karg->ca_export.ex_anon_gid, karg->ca_export.ex_anon_gid);
2111
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002112 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
2114
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002115static int compat_nfs_getfd_trans(struct nfsctl_arg *karg,
2116 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002118 if (!access_ok(VERIFY_READ, &arg->ca32_getfd,
2119 sizeof(arg->ca32_getfd)) ||
2120 get_user(karg->ca_version, &arg->ca32_version) ||
2121 __copy_from_user(&karg->ca_getfd.gd_addr,
2122 &arg->ca32_getfd.gd32_addr,
2123 (sizeof(struct sockaddr))) ||
2124 __copy_from_user(&karg->ca_getfd.gd_path,
2125 &arg->ca32_getfd.gd32_path,
2126 (NFS_MAXPATHLEN+1)) ||
2127 __get_user(karg->ca_getfd.gd_version,
2128 &arg->ca32_getfd.gd32_version))
2129 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002131 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132}
2133
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002134static int compat_nfs_getfs_trans(struct nfsctl_arg *karg,
2135 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002137 if (!access_ok(VERIFY_READ,&arg->ca32_getfs,sizeof(arg->ca32_getfs)) ||
2138 get_user(karg->ca_version, &arg->ca32_version) ||
2139 __copy_from_user(&karg->ca_getfs.gd_addr,
2140 &arg->ca32_getfs.gd32_addr,
2141 (sizeof(struct sockaddr))) ||
2142 __copy_from_user(&karg->ca_getfs.gd_path,
2143 &arg->ca32_getfs.gd32_path,
2144 (NFS_MAXPATHLEN+1)) ||
2145 __get_user(karg->ca_getfs.gd_maxlen,
2146 &arg->ca32_getfs.gd32_maxlen))
2147 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002149 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
2152/* This really doesn't need translations, we are only passing
2153 * back a union which contains opaque nfs file handle data.
2154 */
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002155static int compat_nfs_getfh_res_trans(union nfsctl_res *kres,
2156 union compat_nfsctl_res __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
2158 int err;
2159
2160 err = copy_to_user(res, kres, sizeof(*res));
2161
2162 return (err) ? -EFAULT : 0;
2163}
2164
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002165asmlinkage long compat_sys_nfsservctl(int cmd,
2166 struct compat_nfsctl_arg __user *arg,
2167 union compat_nfsctl_res __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
2169 struct nfsctl_arg *karg;
2170 union nfsctl_res *kres;
2171 mm_segment_t oldfs;
2172 int err;
2173
2174 karg = kmalloc(sizeof(*karg), GFP_USER);
2175 kres = kmalloc(sizeof(*kres), GFP_USER);
2176 if(!karg || !kres) {
2177 err = -ENOMEM;
2178 goto done;
2179 }
2180
2181 switch(cmd) {
2182 case NFSCTL_SVC:
2183 err = compat_nfs_svc_trans(karg, arg);
2184 break;
2185
2186 case NFSCTL_ADDCLIENT:
2187 err = compat_nfs_clnt_trans(karg, arg);
2188 break;
2189
2190 case NFSCTL_DELCLIENT:
2191 err = compat_nfs_clnt_trans(karg, arg);
2192 break;
2193
2194 case NFSCTL_EXPORT:
2195 case NFSCTL_UNEXPORT:
2196 err = compat_nfs_exp_trans(karg, arg);
2197 break;
2198
2199 case NFSCTL_GETFD:
2200 err = compat_nfs_getfd_trans(karg, arg);
2201 break;
2202
2203 case NFSCTL_GETFS:
2204 err = compat_nfs_getfs_trans(karg, arg);
2205 break;
2206
2207 default:
2208 err = -EINVAL;
Peter Staubach57070d02006-03-25 03:08:04 -08002209 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 }
2211
Peter Staubach57070d02006-03-25 03:08:04 -08002212 if (err)
2213 goto done;
2214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 oldfs = get_fs();
2216 set_fs(KERNEL_DS);
2217 /* The __user pointer casts are valid because of the set_fs() */
2218 err = sys_nfsservctl(cmd, (void __user *) karg, (void __user *) kres);
2219 set_fs(oldfs);
2220
2221 if (err)
2222 goto done;
2223
2224 if((cmd == NFSCTL_GETFD) ||
2225 (cmd == NFSCTL_GETFS))
2226 err = compat_nfs_getfh_res_trans(kres, res);
2227
2228done:
2229 kfree(karg);
2230 kfree(kres);
2231 return err;
2232}
2233#else /* !NFSD */
2234long asmlinkage compat_sys_nfsservctl(int cmd, void *notused, void *notused2)
2235{
2236 return sys_ni_syscall();
2237}
2238#endif