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