blob: 040a8be38a4896af7334769a9cb26f852b5cc56a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/compat.c
3 *
4 * Kernel compatibililty routines for e.g. 32 bit syscall support
5 * on 64 bit kernels.
6 *
7 * Copyright (C) 2002 Stephen Rothwell, IBM Corporation
8 * Copyright (C) 1997-2000 Jakub Jelinek (jakub@redhat.com)
9 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
10 * Copyright (C) 2001,2002 Andi Kleen, SuSE Labs
11 * Copyright (C) 2003 Pavel Machek (pavel@suse.cz)
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 */
17
18#include <linux/linkage.h>
19#include <linux/compat.h>
20#include <linux/errno.h>
21#include <linux/time.h>
22#include <linux/fs.h>
23#include <linux/fcntl.h>
24#include <linux/namei.h>
25#include <linux/file.h>
26#include <linux/vfs.h>
27#include <linux/ioctl32.h>
28#include <linux/ioctl.h>
29#include <linux/init.h>
30#include <linux/sockios.h> /* for SIOCDEVPRIVATE */
31#include <linux/smb.h>
32#include <linux/smb_mount.h>
33#include <linux/ncp_mount.h>
David Howells9a9947b2005-04-18 10:54:51 -070034#include <linux/nfs4_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/smp_lock.h>
36#include <linux/syscalls.h>
37#include <linux/ctype.h>
38#include <linux/module.h>
39#include <linux/dirent.h>
Robert Love0eeca282005-07-12 17:06:03 -040040#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/highuid.h>
42#include <linux/sunrpc/svc.h>
43#include <linux/nfsd/nfsd.h>
44#include <linux/nfsd/syscall.h>
45#include <linux/personality.h>
46#include <linux/rwsem.h>
Jay Lan8f0ab512006-09-30 23:28:59 -070047#include <linux/tsacct_kern.h>
Al Viroa1f8e7f2006-10-19 16:08:53 -040048#include <linux/highmem.h>
Al Virobd01f842006-10-19 17:23:57 -040049#include <linux/poll.h>
David S. Miller4a805e82005-09-14 21:40:00 -070050#include <linux/mm.h>
Davide Libenzif6dfb4f2007-03-07 20:41:21 -080051#include <linux/eventpoll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#include <net/sock.h> /* siocdevprivate_ioctl */
54
55#include <asm/uaccess.h>
56#include <asm/mmu_context.h>
57#include <asm/ioctls.h>
David Howells07f3f052006-09-30 20:52:18 +020058#include "internal.h"
David Woodhouse9f729492006-01-18 17:44:05 -080059
Andi Kleenbebfa102006-06-26 13:56:52 +020060int compat_log = 1;
61
62int compat_printk(const char *fmt, ...)
63{
64 va_list ap;
65 int ret;
66 if (!compat_log)
67 return 0;
68 va_start(ap, fmt);
69 ret = vprintk(fmt, ap);
70 va_end(ap);
71 return ret;
72}
73
Badari Pulavartyee0b3e62006-09-30 23:28:47 -070074#include "read_write.h"
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/*
77 * Not all architectures have sys_utime, so implement this in terms
78 * of sys_utimes.
79 */
80asmlinkage long compat_sys_utime(char __user *filename, struct compat_utimbuf __user *t)
81{
82 struct timeval tv[2];
83
84 if (t) {
85 if (get_user(tv[0].tv_sec, &t->actime) ||
86 get_user(tv[1].tv_sec, &t->modtime))
87 return -EFAULT;
88 tv[0].tv_usec = 0;
89 tv[1].tv_usec = 0;
90 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -080091 return do_utimes(AT_FDCWD, filename, t ? tv : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +110094asmlinkage long compat_sys_futimesat(unsigned int dfd, char __user *filename, struct compat_timeval __user *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 struct timeval tv[2];
97
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +110098 if (t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
100 get_user(tv[0].tv_usec, &t[0].tv_usec) ||
101 get_user(tv[1].tv_sec, &t[1].tv_sec) ||
102 get_user(tv[1].tv_usec, &t[1].tv_usec))
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +1100103 return -EFAULT;
104 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800105 return do_utimes(dfd, filename, t ? tv : NULL);
106}
107
108asmlinkage long compat_sys_utimes(char __user *filename, struct compat_timeval __user *t)
109{
110 return compat_sys_futimesat(AT_FDCWD, filename, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113asmlinkage long compat_sys_newstat(char __user * filename,
114 struct compat_stat __user *statbuf)
115{
116 struct kstat stat;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800117 int error = vfs_stat_fd(AT_FDCWD, filename, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 if (!error)
120 error = cp_compat_stat(&stat, statbuf);
121 return error;
122}
123
124asmlinkage long compat_sys_newlstat(char __user * filename,
125 struct compat_stat __user *statbuf)
126{
127 struct kstat stat;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800128 int error = vfs_lstat_fd(AT_FDCWD, filename, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 if (!error)
131 error = cp_compat_stat(&stat, statbuf);
132 return error;
133}
134
Kyle McMartin82d821d2006-03-24 03:18:20 -0800135#ifndef __ARCH_WANT_STAT64
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +1100136asmlinkage long compat_sys_newfstatat(unsigned int dfd, char __user *filename,
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800137 struct compat_stat __user *statbuf, int flag)
138{
139 struct kstat stat;
140 int error = -EINVAL;
141
142 if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
143 goto out;
144
145 if (flag & AT_SYMLINK_NOFOLLOW)
146 error = vfs_lstat_fd(dfd, filename, &stat);
147 else
148 error = vfs_stat_fd(dfd, filename, &stat);
149
150 if (!error)
151 error = cp_compat_stat(&stat, statbuf);
152
153out:
154 return error;
155}
Kyle McMartin82d821d2006-03-24 03:18:20 -0800156#endif
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158asmlinkage long compat_sys_newfstat(unsigned int fd,
159 struct compat_stat __user * statbuf)
160{
161 struct kstat stat;
162 int error = vfs_fstat(fd, &stat);
163
164 if (!error)
165 error = cp_compat_stat(&stat, statbuf);
166 return error;
167}
168
169static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
170{
171
172 if (sizeof ubuf->f_blocks == 4) {
173 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) &
174 0xffffffff00000000ULL)
175 return -EOVERFLOW;
176 /* f_files and f_ffree may be -1; it's okay
177 * to stuff that into 32 bits */
178 if (kbuf->f_files != 0xffffffffffffffffULL
179 && (kbuf->f_files & 0xffffffff00000000ULL))
180 return -EOVERFLOW;
181 if (kbuf->f_ffree != 0xffffffffffffffffULL
182 && (kbuf->f_ffree & 0xffffffff00000000ULL))
183 return -EOVERFLOW;
184 }
185 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
186 __put_user(kbuf->f_type, &ubuf->f_type) ||
187 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
188 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
189 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
190 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
191 __put_user(kbuf->f_files, &ubuf->f_files) ||
192 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
193 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
194 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
195 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
196 __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
197 __put_user(0, &ubuf->f_spare[0]) ||
198 __put_user(0, &ubuf->f_spare[1]) ||
199 __put_user(0, &ubuf->f_spare[2]) ||
200 __put_user(0, &ubuf->f_spare[3]) ||
201 __put_user(0, &ubuf->f_spare[4]))
202 return -EFAULT;
203 return 0;
204}
205
206/*
207 * The following statfs calls are copies of code from fs/open.c and
208 * should be checked against those from time to time
209 */
210asmlinkage long compat_sys_statfs(const char __user *path, struct compat_statfs __user *buf)
211{
212 struct nameidata nd;
213 int error;
214
215 error = user_path_walk(path, &nd);
216 if (!error) {
217 struct kstatfs tmp;
David Howells726c3342006-06-23 02:02:58 -0700218 error = vfs_statfs(nd.dentry, &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 path_release(&nd);
222 }
223 return error;
224}
225
226asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf)
227{
228 struct file * file;
229 struct kstatfs tmp;
230 int error;
231
232 error = -EBADF;
233 file = fget(fd);
234 if (!file)
235 goto out;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800236 error = vfs_statfs(file->f_path.dentry, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800237 if (!error)
238 error = put_compat_statfs(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 fput(file);
240out:
241 return error;
242}
243
244static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
245{
246 if (sizeof ubuf->f_blocks == 4) {
247 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) &
248 0xffffffff00000000ULL)
249 return -EOVERFLOW;
250 /* f_files and f_ffree may be -1; it's okay
251 * to stuff that into 32 bits */
252 if (kbuf->f_files != 0xffffffffffffffffULL
253 && (kbuf->f_files & 0xffffffff00000000ULL))
254 return -EOVERFLOW;
255 if (kbuf->f_ffree != 0xffffffffffffffffULL
256 && (kbuf->f_ffree & 0xffffffff00000000ULL))
257 return -EOVERFLOW;
258 }
259 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
260 __put_user(kbuf->f_type, &ubuf->f_type) ||
261 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
262 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
263 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
264 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
265 __put_user(kbuf->f_files, &ubuf->f_files) ||
266 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
267 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
268 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
269 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
270 __put_user(kbuf->f_frsize, &ubuf->f_frsize))
271 return -EFAULT;
272 return 0;
273}
274
275asmlinkage long compat_sys_statfs64(const char __user *path, compat_size_t sz, struct compat_statfs64 __user *buf)
276{
277 struct nameidata nd;
278 int error;
279
280 if (sz != sizeof(*buf))
281 return -EINVAL;
282
283 error = user_path_walk(path, &nd);
284 if (!error) {
285 struct kstatfs tmp;
David Howells726c3342006-06-23 02:02:58 -0700286 error = vfs_statfs(nd.dentry, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800287 if (!error)
288 error = put_compat_statfs64(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 path_release(&nd);
290 }
291 return error;
292}
293
294asmlinkage long compat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf)
295{
296 struct file * file;
297 struct kstatfs tmp;
298 int error;
299
300 if (sz != sizeof(*buf))
301 return -EINVAL;
302
303 error = -EBADF;
304 file = fget(fd);
305 if (!file)
306 goto out;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800307 error = vfs_statfs(file->f_path.dentry, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800308 if (!error)
309 error = put_compat_statfs64(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 fput(file);
311out:
312 return error;
313}
314
315/* ioctl32 stuff, used by sparc64, parisc, s390x, ppc64, x86_64, MIPS */
316
317#define IOCTL_HASHSIZE 256
318static struct ioctl_trans *ioctl32_hash_table[IOCTL_HASHSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320static inline unsigned long ioctl32_hash(unsigned long cmd)
321{
322 return (((cmd >> 6) ^ (cmd >> 4) ^ cmd)) % IOCTL_HASHSIZE;
323}
324
325static void ioctl32_insert_translation(struct ioctl_trans *trans)
326{
327 unsigned long hash;
328 struct ioctl_trans *t;
329
330 hash = ioctl32_hash (trans->cmd);
331 if (!ioctl32_hash_table[hash])
332 ioctl32_hash_table[hash] = trans;
333 else {
334 t = ioctl32_hash_table[hash];
335 while (t->next)
336 t = t->next;
337 trans->next = NULL;
338 t->next = trans;
339 }
340}
341
342static int __init init_sys32_ioctl(void)
343{
344 int i;
345
346 for (i = 0; i < ioctl_table_size; i++) {
347 if (ioctl_start[i].next != 0) {
348 printk("ioctl translation %d bad\n",i);
349 return -1;
350 }
351
352 ioctl32_insert_translation(&ioctl_start[i]);
353 }
354 return 0;
355}
356
357__initcall(init_sys32_ioctl);
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359static void compat_ioctl_error(struct file *filp, unsigned int fd,
360 unsigned int cmd, unsigned long arg)
361{
362 char buf[10];
363 char *fn = "?";
364 char *path;
365
366 /* find the name of the device. */
367 path = (char *)__get_free_page(GFP_KERNEL);
368 if (path) {
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800369 fn = d_path(filp->f_path.dentry, filp->f_path.mnt, path, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (IS_ERR(fn))
371 fn = "?";
372 }
373
374 sprintf(buf,"'%c'", (cmd>>24) & 0x3f);
375 if (!isprint(buf[1]))
376 sprintf(buf, "%02x", buf[1]);
Andi Kleenbebfa102006-06-26 13:56:52 +0200377 compat_printk("ioctl32(%s:%d): Unknown cmd fd(%d) "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 "cmd(%08x){%s} arg(%08x) on %s\n",
379 current->comm, current->pid,
380 (int)fd, (unsigned int)cmd, buf,
381 (unsigned int)arg, fn);
382
383 if (path)
384 free_page((unsigned long)path);
385}
386
387asmlinkage long compat_sys_ioctl(unsigned int fd, unsigned int cmd,
388 unsigned long arg)
389{
390 struct file *filp;
391 int error = -EBADF;
392 struct ioctl_trans *t;
393 int fput_needed;
394
395 filp = fget_light(fd, &fput_needed);
396 if (!filp)
397 goto out;
398
399 /* RED-PEN how should LSM module know it's handling 32bit? */
400 error = security_file_ioctl(filp, cmd, arg);
401 if (error)
402 goto out_fput;
403
404 /*
405 * To allow the compat_ioctl handlers to be self contained
406 * we need to check the common ioctls here first.
407 * Just handle them with the standard handlers below.
408 */
409 switch (cmd) {
410 case FIOCLEX:
411 case FIONCLEX:
412 case FIONBIO:
413 case FIOASYNC:
414 case FIOQSIZE:
415 break;
416
417 case FIBMAP:
418 case FIGETBSZ:
419 case FIONREAD:
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800420 if (S_ISREG(filp->f_path.dentry->d_inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 break;
422 /*FALL THROUGH*/
423
424 default:
425 if (filp->f_op && filp->f_op->compat_ioctl) {
426 error = filp->f_op->compat_ioctl(filp, cmd, arg);
427 if (error != -ENOIOCTLCMD)
428 goto out_fput;
429 }
430
431 if (!filp->f_op ||
432 (!filp->f_op->ioctl && !filp->f_op->unlocked_ioctl))
433 goto do_ioctl;
434 break;
435 }
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 for (t = ioctl32_hash_table[ioctl32_hash(cmd)]; t; t = t->next) {
438 if (t->cmd == cmd)
439 goto found_handler;
440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800442 if (S_ISSOCK(filp->f_path.dentry->d_inode->i_mode) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
444 error = siocdevprivate_ioctl(fd, cmd, arg);
445 } else {
446 static int count;
447
448 if (++count <= 50)
449 compat_ioctl_error(filp, fd, cmd, arg);
450 error = -EINVAL;
451 }
452
453 goto out_fput;
454
455 found_handler:
456 if (t->handler) {
457 lock_kernel();
458 error = t->handler(fd, cmd, arg, filp);
459 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 goto out_fput;
461 }
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 do_ioctl:
464 error = vfs_ioctl(filp, fd, cmd, arg);
465 out_fput:
466 fput_light(filp, fput_needed);
467 out:
468 return error;
469}
470
471static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
472{
473 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
474 __get_user(kfl->l_type, &ufl->l_type) ||
475 __get_user(kfl->l_whence, &ufl->l_whence) ||
476 __get_user(kfl->l_start, &ufl->l_start) ||
477 __get_user(kfl->l_len, &ufl->l_len) ||
478 __get_user(kfl->l_pid, &ufl->l_pid))
479 return -EFAULT;
480 return 0;
481}
482
483static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
484{
485 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
486 __put_user(kfl->l_type, &ufl->l_type) ||
487 __put_user(kfl->l_whence, &ufl->l_whence) ||
488 __put_user(kfl->l_start, &ufl->l_start) ||
489 __put_user(kfl->l_len, &ufl->l_len) ||
490 __put_user(kfl->l_pid, &ufl->l_pid))
491 return -EFAULT;
492 return 0;
493}
494
495#ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
496static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
497{
498 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
499 __get_user(kfl->l_type, &ufl->l_type) ||
500 __get_user(kfl->l_whence, &ufl->l_whence) ||
501 __get_user(kfl->l_start, &ufl->l_start) ||
502 __get_user(kfl->l_len, &ufl->l_len) ||
503 __get_user(kfl->l_pid, &ufl->l_pid))
504 return -EFAULT;
505 return 0;
506}
507#endif
508
509#ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
510static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
511{
512 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
513 __put_user(kfl->l_type, &ufl->l_type) ||
514 __put_user(kfl->l_whence, &ufl->l_whence) ||
515 __put_user(kfl->l_start, &ufl->l_start) ||
516 __put_user(kfl->l_len, &ufl->l_len) ||
517 __put_user(kfl->l_pid, &ufl->l_pid))
518 return -EFAULT;
519 return 0;
520}
521#endif
522
523asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd,
524 unsigned long arg)
525{
526 mm_segment_t old_fs;
527 struct flock f;
528 long ret;
529
530 switch (cmd) {
531 case F_GETLK:
532 case F_SETLK:
533 case F_SETLKW:
534 ret = get_compat_flock(&f, compat_ptr(arg));
535 if (ret != 0)
536 break;
537 old_fs = get_fs();
538 set_fs(KERNEL_DS);
539 ret = sys_fcntl(fd, cmd, (unsigned long)&f);
540 set_fs(old_fs);
541 if (cmd == F_GETLK && ret == 0) {
NeilBrown2520f142006-01-08 01:02:40 -0800542 /* GETLK was successfule and we need to return the data...
543 * but it needs to fit in the compat structure.
544 * l_start shouldn't be too big, unless the original
545 * start + end is greater than COMPAT_OFF_T_MAX, in which
546 * case the app was asking for trouble, so we return
547 * -EOVERFLOW in that case.
548 * l_len could be too big, in which case we just truncate it,
549 * and only allow the app to see that part of the conflicting
550 * lock that might make sense to it anyway
551 */
552
553 if (f.l_start > COMPAT_OFF_T_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 ret = -EOVERFLOW;
NeilBrown2520f142006-01-08 01:02:40 -0800555 if (f.l_len > COMPAT_OFF_T_MAX)
556 f.l_len = COMPAT_OFF_T_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (ret == 0)
558 ret = put_compat_flock(&f, compat_ptr(arg));
559 }
560 break;
561
562 case F_GETLK64:
563 case F_SETLK64:
564 case F_SETLKW64:
565 ret = get_compat_flock64(&f, compat_ptr(arg));
566 if (ret != 0)
567 break;
568 old_fs = get_fs();
569 set_fs(KERNEL_DS);
570 ret = sys_fcntl(fd, (cmd == F_GETLK64) ? F_GETLK :
571 ((cmd == F_SETLK64) ? F_SETLK : F_SETLKW),
572 (unsigned long)&f);
573 set_fs(old_fs);
574 if (cmd == F_GETLK64 && ret == 0) {
NeilBrown2520f142006-01-08 01:02:40 -0800575 /* need to return lock information - see above for commentary */
576 if (f.l_start > COMPAT_LOFF_T_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 ret = -EOVERFLOW;
NeilBrown2520f142006-01-08 01:02:40 -0800578 if (f.l_len > COMPAT_LOFF_T_MAX)
579 f.l_len = COMPAT_LOFF_T_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (ret == 0)
581 ret = put_compat_flock64(&f, compat_ptr(arg));
582 }
583 break;
584
585 default:
586 ret = sys_fcntl(fd, cmd, arg);
587 break;
588 }
589 return ret;
590}
591
592asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd,
593 unsigned long arg)
594{
595 if ((cmd == F_GETLK64) || (cmd == F_SETLK64) || (cmd == F_SETLKW64))
596 return -EINVAL;
597 return compat_sys_fcntl64(fd, cmd, arg);
598}
599
600asmlinkage long
601compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p)
602{
603 long ret;
604 aio_context_t ctx64;
605
606 mm_segment_t oldfs = get_fs();
607 if (unlikely(get_user(ctx64, ctx32p)))
608 return -EFAULT;
609
610 set_fs(KERNEL_DS);
611 /* The __user pointer cast is valid because of the set_fs() */
612 ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64);
613 set_fs(oldfs);
614 /* truncating is ok because it's a user address */
615 if (!ret)
616 ret = put_user((u32) ctx64, ctx32p);
617 return ret;
618}
619
620asmlinkage long
621compat_sys_io_getevents(aio_context_t ctx_id,
622 unsigned long min_nr,
623 unsigned long nr,
624 struct io_event __user *events,
625 struct compat_timespec __user *timeout)
626{
627 long ret;
628 struct timespec t;
629 struct timespec __user *ut = NULL;
630
631 ret = -EFAULT;
632 if (unlikely(!access_ok(VERIFY_WRITE, events,
633 nr * sizeof(struct io_event))))
634 goto out;
635 if (timeout) {
636 if (get_compat_timespec(&t, timeout))
637 goto out;
638
639 ut = compat_alloc_user_space(sizeof(*ut));
640 if (copy_to_user(ut, &t, sizeof(t)) )
641 goto out;
642 }
643 ret = sys_io_getevents(ctx_id, min_nr, nr, events, ut);
644out:
645 return ret;
646}
647
648static inline long
649copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64)
650{
651 compat_uptr_t uptr;
652 int i;
653
654 for (i = 0; i < nr; ++i) {
655 if (get_user(uptr, ptr32 + i))
656 return -EFAULT;
657 if (put_user(compat_ptr(uptr), ptr64 + i))
658 return -EFAULT;
659 }
660 return 0;
661}
662
663#define MAX_AIO_SUBMITS (PAGE_SIZE/sizeof(struct iocb *))
664
665asmlinkage long
666compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb)
667{
668 struct iocb __user * __user *iocb64;
669 long ret;
670
671 if (unlikely(nr < 0))
672 return -EINVAL;
673
674 if (nr > MAX_AIO_SUBMITS)
675 nr = MAX_AIO_SUBMITS;
676
677 iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64));
678 ret = copy_iocb(nr, iocb, iocb64);
679 if (!ret)
680 ret = sys_io_submit(ctx_id, nr, iocb64);
681 return ret;
682}
683
684struct compat_ncp_mount_data {
685 compat_int_t version;
686 compat_uint_t ncp_fd;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700687 __compat_uid_t mounted_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 compat_pid_t wdog_pid;
689 unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
690 compat_uint_t time_out;
691 compat_uint_t retry_count;
692 compat_uint_t flags;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700693 __compat_uid_t uid;
694 __compat_gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 compat_mode_t file_mode;
696 compat_mode_t dir_mode;
697};
698
699struct compat_ncp_mount_data_v4 {
700 compat_int_t version;
701 compat_ulong_t flags;
702 compat_ulong_t mounted_uid;
703 compat_long_t wdog_pid;
704 compat_uint_t ncp_fd;
705 compat_uint_t time_out;
706 compat_uint_t retry_count;
707 compat_ulong_t uid;
708 compat_ulong_t gid;
709 compat_ulong_t file_mode;
710 compat_ulong_t dir_mode;
711};
712
713static void *do_ncp_super_data_conv(void *raw_data)
714{
715 int version = *(unsigned int *)raw_data;
716
717 if (version == 3) {
718 struct compat_ncp_mount_data *c_n = raw_data;
719 struct ncp_mount_data *n = raw_data;
720
721 n->dir_mode = c_n->dir_mode;
722 n->file_mode = c_n->file_mode;
723 n->gid = c_n->gid;
724 n->uid = c_n->uid;
725 memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
726 n->wdog_pid = c_n->wdog_pid;
727 n->mounted_uid = c_n->mounted_uid;
728 } else if (version == 4) {
729 struct compat_ncp_mount_data_v4 *c_n = raw_data;
730 struct ncp_mount_data_v4 *n = raw_data;
731
732 n->dir_mode = c_n->dir_mode;
733 n->file_mode = c_n->file_mode;
734 n->gid = c_n->gid;
735 n->uid = c_n->uid;
736 n->retry_count = c_n->retry_count;
737 n->time_out = c_n->time_out;
738 n->ncp_fd = c_n->ncp_fd;
739 n->wdog_pid = c_n->wdog_pid;
740 n->mounted_uid = c_n->mounted_uid;
741 n->flags = c_n->flags;
742 } else if (version != 5) {
743 return NULL;
744 }
745
746 return raw_data;
747}
748
749struct compat_smb_mount_data {
750 compat_int_t version;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700751 __compat_uid_t mounted_uid;
752 __compat_uid_t uid;
753 __compat_gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 compat_mode_t file_mode;
755 compat_mode_t dir_mode;
756};
757
758static void *do_smb_super_data_conv(void *raw_data)
759{
760 struct smb_mount_data *s = raw_data;
761 struct compat_smb_mount_data *c_s = raw_data;
762
763 if (c_s->version != SMB_MOUNT_OLDVERSION)
764 goto out;
765 s->dir_mode = c_s->dir_mode;
766 s->file_mode = c_s->file_mode;
767 s->gid = c_s->gid;
768 s->uid = c_s->uid;
769 s->mounted_uid = c_s->mounted_uid;
770 out:
771 return raw_data;
772}
773
David Howells9a9947b2005-04-18 10:54:51 -0700774struct compat_nfs_string {
775 compat_uint_t len;
David Howells5fc3e622005-04-27 15:39:03 -0700776 compat_uptr_t data;
David Howells9a9947b2005-04-18 10:54:51 -0700777};
778
779static inline void compat_nfs_string(struct nfs_string *dst,
780 struct compat_nfs_string *src)
781{
782 dst->data = compat_ptr(src->data);
783 dst->len = src->len;
784}
785
786struct compat_nfs4_mount_data_v1 {
787 compat_int_t version;
788 compat_int_t flags;
789 compat_int_t rsize;
790 compat_int_t wsize;
791 compat_int_t timeo;
792 compat_int_t retrans;
793 compat_int_t acregmin;
794 compat_int_t acregmax;
795 compat_int_t acdirmin;
796 compat_int_t acdirmax;
797 struct compat_nfs_string client_addr;
798 struct compat_nfs_string mnt_path;
799 struct compat_nfs_string hostname;
800 compat_uint_t host_addrlen;
David Howells5fc3e622005-04-27 15:39:03 -0700801 compat_uptr_t host_addr;
David Howells9a9947b2005-04-18 10:54:51 -0700802 compat_int_t proto;
803 compat_int_t auth_flavourlen;
David Howells5fc3e622005-04-27 15:39:03 -0700804 compat_uptr_t auth_flavours;
David Howells9a9947b2005-04-18 10:54:51 -0700805};
806
807static int do_nfs4_super_data_conv(void *raw_data)
808{
809 int version = *(compat_uint_t *) raw_data;
810
811 if (version == 1) {
812 struct compat_nfs4_mount_data_v1 *raw = raw_data;
813 struct nfs4_mount_data *real = raw_data;
814
815 /* copy the fields backwards */
816 real->auth_flavours = compat_ptr(raw->auth_flavours);
817 real->auth_flavourlen = raw->auth_flavourlen;
818 real->proto = raw->proto;
819 real->host_addr = compat_ptr(raw->host_addr);
820 real->host_addrlen = raw->host_addrlen;
821 compat_nfs_string(&real->hostname, &raw->hostname);
822 compat_nfs_string(&real->mnt_path, &raw->mnt_path);
823 compat_nfs_string(&real->client_addr, &raw->client_addr);
824 real->acdirmax = raw->acdirmax;
825 real->acdirmin = raw->acdirmin;
826 real->acregmax = raw->acregmax;
827 real->acregmin = raw->acregmin;
828 real->retrans = raw->retrans;
829 real->timeo = raw->timeo;
830 real->wsize = raw->wsize;
831 real->rsize = raw->rsize;
832 real->flags = raw->flags;
833 real->version = raw->version;
834 }
835 else {
836 return -EINVAL;
837 }
838
839 return 0;
840}
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842#define SMBFS_NAME "smbfs"
843#define NCPFS_NAME "ncpfs"
David Howells9a9947b2005-04-18 10:54:51 -0700844#define NFS4_NAME "nfs4"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846asmlinkage long compat_sys_mount(char __user * dev_name, char __user * dir_name,
847 char __user * type, unsigned long flags,
848 void __user * data)
849{
850 unsigned long type_page;
851 unsigned long data_page;
852 unsigned long dev_page;
853 char *dir_page;
854 int retval;
855
856 retval = copy_mount_options (type, &type_page);
857 if (retval < 0)
858 goto out;
859
860 dir_page = getname(dir_name);
861 retval = PTR_ERR(dir_page);
862 if (IS_ERR(dir_page))
863 goto out1;
864
865 retval = copy_mount_options (dev_name, &dev_page);
866 if (retval < 0)
867 goto out2;
868
869 retval = copy_mount_options (data, &data_page);
870 if (retval < 0)
871 goto out3;
872
873 retval = -EINVAL;
874
Andrey Mirkin822191a2006-12-06 20:31:35 -0800875 if (type_page && data_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (!strcmp((char *)type_page, SMBFS_NAME)) {
877 do_smb_super_data_conv((void *)data_page);
878 } else if (!strcmp((char *)type_page, NCPFS_NAME)) {
879 do_ncp_super_data_conv((void *)data_page);
David Howells9a9947b2005-04-18 10:54:51 -0700880 } else if (!strcmp((char *)type_page, NFS4_NAME)) {
881 if (do_nfs4_super_data_conv((void *) data_page))
882 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884 }
885
886 lock_kernel();
887 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
888 flags, (void*)data_page);
889 unlock_kernel();
890
David Howells9a9947b2005-04-18 10:54:51 -0700891 out4:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 free_page(data_page);
893 out3:
894 free_page(dev_page);
895 out2:
896 putname(dir_page);
897 out1:
898 free_page(type_page);
899 out:
900 return retval;
901}
902
903#define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
904#define COMPAT_ROUND_UP(x) (((x)+sizeof(compat_long_t)-1) & \
905 ~(sizeof(compat_long_t)-1))
906
907struct compat_old_linux_dirent {
908 compat_ulong_t d_ino;
909 compat_ulong_t d_offset;
910 unsigned short d_namlen;
911 char d_name[1];
912};
913
914struct compat_readdir_callback {
915 struct compat_old_linux_dirent __user *dirent;
916 int result;
917};
918
919static int compat_fillonedir(void *__buf, const char *name, int namlen,
David Howellsafefdbb2006-10-03 01:13:46 -0700920 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
922 struct compat_readdir_callback *buf = __buf;
923 struct compat_old_linux_dirent __user *dirent;
David Howellsafefdbb2006-10-03 01:13:46 -0700924 compat_ulong_t d_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 if (buf->result)
927 return -EINVAL;
David Howellsafefdbb2006-10-03 01:13:46 -0700928 d_ino = ino;
929 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
930 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 buf->result++;
932 dirent = buf->dirent;
933 if (!access_ok(VERIFY_WRITE, dirent,
934 (unsigned long)(dirent->d_name + namlen + 1) -
935 (unsigned long)dirent))
936 goto efault;
David Howellsafefdbb2006-10-03 01:13:46 -0700937 if ( __put_user(d_ino, &dirent->d_ino) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 __put_user(offset, &dirent->d_offset) ||
939 __put_user(namlen, &dirent->d_namlen) ||
940 __copy_to_user(dirent->d_name, name, namlen) ||
941 __put_user(0, dirent->d_name + namlen))
942 goto efault;
943 return 0;
944efault:
945 buf->result = -EFAULT;
946 return -EFAULT;
947}
948
949asmlinkage long compat_sys_old_readdir(unsigned int fd,
950 struct compat_old_linux_dirent __user *dirent, unsigned int count)
951{
952 int error;
953 struct file *file;
954 struct compat_readdir_callback buf;
955
956 error = -EBADF;
957 file = fget(fd);
958 if (!file)
959 goto out;
960
961 buf.result = 0;
962 buf.dirent = dirent;
963
964 error = vfs_readdir(file, compat_fillonedir, &buf);
965 if (error >= 0)
966 error = buf.result;
967
968 fput(file);
969out:
970 return error;
971}
972
973struct compat_linux_dirent {
974 compat_ulong_t d_ino;
975 compat_ulong_t d_off;
976 unsigned short d_reclen;
977 char d_name[1];
978};
979
980struct compat_getdents_callback {
981 struct compat_linux_dirent __user *current_dir;
982 struct compat_linux_dirent __user *previous;
983 int count;
984 int error;
985};
986
987static int compat_filldir(void *__buf, const char *name, int namlen,
David Howellsafefdbb2006-10-03 01:13:46 -0700988 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
990 struct compat_linux_dirent __user * dirent;
991 struct compat_getdents_callback *buf = __buf;
David Howellsafefdbb2006-10-03 01:13:46 -0700992 compat_ulong_t d_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 int reclen = COMPAT_ROUND_UP(NAME_OFFSET(dirent) + namlen + 2);
994
995 buf->error = -EINVAL; /* only used if we fail.. */
996 if (reclen > buf->count)
997 return -EINVAL;
David Howellsafefdbb2006-10-03 01:13:46 -0700998 d_ino = ino;
999 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
1000 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 dirent = buf->previous;
1002 if (dirent) {
1003 if (__put_user(offset, &dirent->d_off))
1004 goto efault;
1005 }
1006 dirent = buf->current_dir;
David Howellsafefdbb2006-10-03 01:13:46 -07001007 if (__put_user(d_ino, &dirent->d_ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 goto efault;
1009 if (__put_user(reclen, &dirent->d_reclen))
1010 goto efault;
1011 if (copy_to_user(dirent->d_name, name, namlen))
1012 goto efault;
1013 if (__put_user(0, dirent->d_name + namlen))
1014 goto efault;
1015 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
1016 goto efault;
1017 buf->previous = dirent;
1018 dirent = (void __user *)dirent + reclen;
1019 buf->current_dir = dirent;
1020 buf->count -= reclen;
1021 return 0;
1022efault:
1023 buf->error = -EFAULT;
1024 return -EFAULT;
1025}
1026
1027asmlinkage long compat_sys_getdents(unsigned int fd,
1028 struct compat_linux_dirent __user *dirent, unsigned int count)
1029{
1030 struct file * file;
1031 struct compat_linux_dirent __user * lastdirent;
1032 struct compat_getdents_callback buf;
1033 int error;
1034
1035 error = -EFAULT;
1036 if (!access_ok(VERIFY_WRITE, dirent, count))
1037 goto out;
1038
1039 error = -EBADF;
1040 file = fget(fd);
1041 if (!file)
1042 goto out;
1043
1044 buf.current_dir = dirent;
1045 buf.previous = NULL;
1046 buf.count = count;
1047 buf.error = 0;
1048
1049 error = vfs_readdir(file, compat_filldir, &buf);
1050 if (error < 0)
1051 goto out_putf;
1052 error = buf.error;
1053 lastdirent = buf.previous;
1054 if (lastdirent) {
1055 if (put_user(file->f_pos, &lastdirent->d_off))
1056 error = -EFAULT;
1057 else
1058 error = count - buf.count;
1059 }
1060
1061out_putf:
1062 fput(file);
1063out:
1064 return error;
1065}
1066
1067#ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64
1068#define COMPAT_ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1))
1069
1070struct compat_getdents_callback64 {
1071 struct linux_dirent64 __user *current_dir;
1072 struct linux_dirent64 __user *previous;
1073 int count;
1074 int error;
1075};
1076
1077static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset,
David Howellsafefdbb2006-10-03 01:13:46 -07001078 u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
1080 struct linux_dirent64 __user *dirent;
1081 struct compat_getdents_callback64 *buf = __buf;
1082 int jj = NAME_OFFSET(dirent);
1083 int reclen = COMPAT_ROUND_UP64(jj + namlen + 1);
1084 u64 off;
1085
1086 buf->error = -EINVAL; /* only used if we fail.. */
1087 if (reclen > buf->count)
1088 return -EINVAL;
1089 dirent = buf->previous;
1090
1091 if (dirent) {
1092 if (__put_user_unaligned(offset, &dirent->d_off))
1093 goto efault;
1094 }
1095 dirent = buf->current_dir;
1096 if (__put_user_unaligned(ino, &dirent->d_ino))
1097 goto efault;
1098 off = 0;
1099 if (__put_user_unaligned(off, &dirent->d_off))
1100 goto efault;
1101 if (__put_user(reclen, &dirent->d_reclen))
1102 goto efault;
1103 if (__put_user(d_type, &dirent->d_type))
1104 goto efault;
1105 if (copy_to_user(dirent->d_name, name, namlen))
1106 goto efault;
1107 if (__put_user(0, dirent->d_name + namlen))
1108 goto efault;
1109 buf->previous = dirent;
1110 dirent = (void __user *)dirent + reclen;
1111 buf->current_dir = dirent;
1112 buf->count -= reclen;
1113 return 0;
1114efault:
1115 buf->error = -EFAULT;
1116 return -EFAULT;
1117}
1118
1119asmlinkage long compat_sys_getdents64(unsigned int fd,
1120 struct linux_dirent64 __user * dirent, unsigned int count)
1121{
1122 struct file * file;
1123 struct linux_dirent64 __user * lastdirent;
1124 struct compat_getdents_callback64 buf;
1125 int error;
1126
1127 error = -EFAULT;
1128 if (!access_ok(VERIFY_WRITE, dirent, count))
1129 goto out;
1130
1131 error = -EBADF;
1132 file = fget(fd);
1133 if (!file)
1134 goto out;
1135
1136 buf.current_dir = dirent;
1137 buf.previous = NULL;
1138 buf.count = count;
1139 buf.error = 0;
1140
1141 error = vfs_readdir(file, compat_filldir64, &buf);
1142 if (error < 0)
1143 goto out_putf;
1144 error = buf.error;
1145 lastdirent = buf.previous;
1146 if (lastdirent) {
1147 typeof(lastdirent->d_off) d_off = file->f_pos;
Heiko Carstens7116e992006-12-06 20:36:36 -08001148 error = -EFAULT;
1149 if (__put_user_unaligned(d_off, &lastdirent->d_off))
1150 goto out_putf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 error = count - buf.count;
1152 }
1153
1154out_putf:
1155 fput(file);
1156out:
1157 return error;
1158}
1159#endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
1160
1161static ssize_t compat_do_readv_writev(int type, struct file *file,
1162 const struct compat_iovec __user *uvector,
1163 unsigned long nr_segs, loff_t *pos)
1164{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 compat_ssize_t tot_len;
1166 struct iovec iovstack[UIO_FASTIOV];
1167 struct iovec *iov=iovstack, *vector;
1168 ssize_t ret;
1169 int seg;
1170 io_fn_t fn;
1171 iov_fn_t fnv;
1172
1173 /*
1174 * SuS says "The readv() function *may* fail if the iovcnt argument
1175 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
1176 * traditionally returned zero for zero segments, so...
1177 */
1178 ret = 0;
1179 if (nr_segs == 0)
1180 goto out;
1181
1182 /*
1183 * First get the "struct iovec" from user memory and
1184 * verify all the pointers
1185 */
1186 ret = -EINVAL;
1187 if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0))
1188 goto out;
1189 if (!file->f_op)
1190 goto out;
1191 if (nr_segs > UIO_FASTIOV) {
1192 ret = -ENOMEM;
1193 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
1194 if (!iov)
1195 goto out;
1196 }
1197 ret = -EFAULT;
1198 if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
1199 goto out;
1200
1201 /*
1202 * Single unix specification:
1203 * We should -EINVAL if an element length is not >= 0 and fitting an
1204 * ssize_t. The total length is fitting an ssize_t
1205 *
1206 * Be careful here because iov_len is a size_t not an ssize_t
1207 */
1208 tot_len = 0;
1209 vector = iov;
1210 ret = -EINVAL;
1211 for (seg = 0 ; seg < nr_segs; seg++) {
1212 compat_ssize_t tmp = tot_len;
1213 compat_ssize_t len;
1214 compat_uptr_t buf;
1215
1216 if (__get_user(len, &uvector->iov_len) ||
1217 __get_user(buf, &uvector->iov_base)) {
1218 ret = -EFAULT;
1219 goto out;
1220 }
1221 if (len < 0) /* size_t not fitting an compat_ssize_t .. */
1222 goto out;
1223 tot_len += len;
1224 if (tot_len < tmp) /* maths overflow on the compat_ssize_t */
1225 goto out;
1226 vector->iov_base = compat_ptr(buf);
1227 vector->iov_len = (compat_size_t) len;
1228 uvector++;
1229 vector++;
1230 }
1231 if (tot_len == 0) {
1232 ret = 0;
1233 goto out;
1234 }
1235
1236 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001237 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 goto out;
1239
James Morrise7edf9c2006-04-26 02:45:03 -04001240 ret = security_file_permission(file, type == READ ? MAY_READ:MAY_WRITE);
1241 if (ret)
1242 goto out;
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 fnv = NULL;
1245 if (type == READ) {
1246 fn = file->f_op->read;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001247 fnv = file->f_op->aio_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 } else {
1249 fn = (io_fn_t)file->f_op->write;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001250 fnv = file->f_op->aio_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
1252
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001253 if (fnv)
1254 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
1255 pos, fnv);
1256 else
1257 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259out:
1260 if (iov != iovstack)
1261 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -04001262 if ((ret + (type == READ)) > 0) {
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001263 struct dentry *dentry = file->f_path.dentry;
Robert Love0eeca282005-07-12 17:06:03 -04001264 if (type == READ)
1265 fsnotify_access(dentry);
1266 else
1267 fsnotify_modify(dentry);
1268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return ret;
1270}
1271
1272asmlinkage ssize_t
1273compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1274{
1275 struct file *file;
1276 ssize_t ret = -EBADF;
1277
1278 file = fget(fd);
1279 if (!file)
1280 return -EBADF;
1281
1282 if (!(file->f_mode & FMODE_READ))
1283 goto out;
1284
1285 ret = -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001286 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 goto out;
1288
1289 ret = compat_do_readv_writev(READ, file, vec, vlen, &file->f_pos);
1290
1291out:
1292 fput(file);
1293 return ret;
1294}
1295
1296asmlinkage ssize_t
1297compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1298{
1299 struct file *file;
1300 ssize_t ret = -EBADF;
1301
1302 file = fget(fd);
1303 if (!file)
1304 return -EBADF;
1305 if (!(file->f_mode & FMODE_WRITE))
1306 goto out;
1307
1308 ret = -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001309 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 goto out;
1311
1312 ret = compat_do_readv_writev(WRITE, file, vec, vlen, &file->f_pos);
1313
1314out:
1315 fput(file);
1316 return ret;
1317}
1318
Andi Kleend2610202006-05-01 12:15:48 -07001319asmlinkage long
1320compat_sys_vmsplice(int fd, const struct compat_iovec __user *iov32,
1321 unsigned int nr_segs, unsigned int flags)
1322{
1323 unsigned i;
Al Viro90cbad62006-10-10 22:44:17 +01001324 struct iovec __user *iov;
Jens Axboe98232d52006-05-04 09:13:49 +02001325 if (nr_segs > UIO_MAXIOV)
Andi Kleend2610202006-05-01 12:15:48 -07001326 return -EINVAL;
1327 iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
1328 for (i = 0; i < nr_segs; i++) {
1329 struct compat_iovec v;
1330 if (get_user(v.iov_base, &iov32[i].iov_base) ||
1331 get_user(v.iov_len, &iov32[i].iov_len) ||
1332 put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
1333 put_user(v.iov_len, &iov[i].iov_len))
1334 return -EFAULT;
1335 }
1336 return sys_vmsplice(fd, iov, nr_segs, flags);
1337}
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339/*
Miklos Szeredie922efc2005-09-06 15:18:25 -07001340 * Exactly like fs/open.c:sys_open(), except that it doesn't set the
1341 * O_LARGEFILE flag.
1342 */
1343asmlinkage long
1344compat_sys_open(const char __user *filename, int flags, int mode)
1345{
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001346 return do_sys_open(AT_FDCWD, filename, flags, mode);
1347}
1348
1349/*
1350 * Exactly like fs/open.c:sys_openat(), except that it doesn't set the
1351 * O_LARGEFILE flag.
1352 */
1353asmlinkage long
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +11001354compat_sys_openat(unsigned int dfd, const char __user *filename, int flags, int mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001355{
1356 return do_sys_open(dfd, filename, flags, mode);
Miklos Szeredie922efc2005-09-06 15:18:25 -07001357}
1358
1359/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 * compat_count() counts the number of arguments/envelopes. It is basically
1361 * a copy of count() from fs/exec.c, except that it works with 32 bit argv
1362 * and envp pointers.
1363 */
1364static int compat_count(compat_uptr_t __user *argv, int max)
1365{
1366 int i = 0;
1367
1368 if (argv != NULL) {
1369 for (;;) {
1370 compat_uptr_t p;
1371
1372 if (get_user(p, argv))
1373 return -EFAULT;
1374 if (!p)
1375 break;
1376 argv++;
1377 if(++i > max)
1378 return -E2BIG;
1379 }
1380 }
1381 return i;
1382}
1383
1384/*
1385 * compat_copy_strings() is basically a copy of copy_strings() from fs/exec.c
1386 * except that it works with 32 bit argv and envp pointers.
1387 */
1388static int compat_copy_strings(int argc, compat_uptr_t __user *argv,
1389 struct linux_binprm *bprm)
1390{
1391 struct page *kmapped_page = NULL;
1392 char *kaddr = NULL;
1393 int ret;
1394
1395 while (argc-- > 0) {
1396 compat_uptr_t str;
1397 int len;
1398 unsigned long pos;
1399
1400 if (get_user(str, argv+argc) ||
1401 !(len = strnlen_user(compat_ptr(str), bprm->p))) {
1402 ret = -EFAULT;
1403 goto out;
1404 }
1405
1406 if (bprm->p < len) {
1407 ret = -E2BIG;
1408 goto out;
1409 }
1410
1411 bprm->p -= len;
1412 /* XXX: add architecture specific overflow check here. */
1413 pos = bprm->p;
1414
1415 while (len > 0) {
1416 int i, new, err;
1417 int offset, bytes_to_copy;
1418 struct page *page;
1419
1420 offset = pos % PAGE_SIZE;
1421 i = pos/PAGE_SIZE;
1422 page = bprm->page[i];
1423 new = 0;
1424 if (!page) {
1425 page = alloc_page(GFP_HIGHUSER);
1426 bprm->page[i] = page;
1427 if (!page) {
1428 ret = -ENOMEM;
1429 goto out;
1430 }
1431 new = 1;
1432 }
1433
1434 if (page != kmapped_page) {
1435 if (kmapped_page)
1436 kunmap(kmapped_page);
1437 kmapped_page = page;
1438 kaddr = kmap(kmapped_page);
1439 }
1440 if (new && offset)
1441 memset(kaddr, 0, offset);
1442 bytes_to_copy = PAGE_SIZE - offset;
1443 if (bytes_to_copy > len) {
1444 bytes_to_copy = len;
1445 if (new)
1446 memset(kaddr+offset+len, 0,
1447 PAGE_SIZE-offset-len);
1448 }
1449 err = copy_from_user(kaddr+offset, compat_ptr(str),
1450 bytes_to_copy);
1451 if (err) {
1452 ret = -EFAULT;
1453 goto out;
1454 }
1455
1456 pos += bytes_to_copy;
1457 str += bytes_to_copy;
1458 len -= bytes_to_copy;
1459 }
1460 }
1461 ret = 0;
1462out:
1463 if (kmapped_page)
1464 kunmap(kmapped_page);
1465 return ret;
1466}
1467
1468#ifdef CONFIG_MMU
1469
1470#define free_arg_pages(bprm) do { } while (0)
1471
1472#else
1473
1474static inline void free_arg_pages(struct linux_binprm *bprm)
1475{
1476 int i;
1477
1478 for (i = 0; i < MAX_ARG_PAGES; i++) {
1479 if (bprm->page[i])
1480 __free_page(bprm->page[i]);
1481 bprm->page[i] = NULL;
1482 }
1483}
1484
1485#endif /* CONFIG_MMU */
1486
1487/*
1488 * compat_do_execve() is mostly a copy of do_execve(), with the exception
1489 * that it processes 32 bit argv and envp pointers.
1490 */
1491int compat_do_execve(char * filename,
1492 compat_uptr_t __user *argv,
1493 compat_uptr_t __user *envp,
1494 struct pt_regs * regs)
1495{
1496 struct linux_binprm *bprm;
1497 struct file *file;
1498 int retval;
1499 int i;
1500
1501 retval = -ENOMEM;
Oliver Neukum11b0b5a2006-03-25 03:08:13 -08001502 bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (!bprm)
1504 goto out_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 file = open_exec(filename);
1507 retval = PTR_ERR(file);
1508 if (IS_ERR(file))
1509 goto out_kfree;
1510
1511 sched_exec();
1512
1513 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1514 bprm->file = file;
1515 bprm->filename = filename;
1516 bprm->interp = filename;
1517 bprm->mm = mm_alloc();
1518 retval = -ENOMEM;
1519 if (!bprm->mm)
1520 goto out_file;
1521
1522 retval = init_new_context(current, bprm->mm);
1523 if (retval < 0)
1524 goto out_mm;
1525
1526 bprm->argc = compat_count(argv, bprm->p / sizeof(compat_uptr_t));
1527 if ((retval = bprm->argc) < 0)
1528 goto out_mm;
1529
1530 bprm->envc = compat_count(envp, bprm->p / sizeof(compat_uptr_t));
1531 if ((retval = bprm->envc) < 0)
1532 goto out_mm;
1533
1534 retval = security_bprm_alloc(bprm);
1535 if (retval)
1536 goto out;
1537
1538 retval = prepare_binprm(bprm);
1539 if (retval < 0)
1540 goto out;
1541
1542 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1543 if (retval < 0)
1544 goto out;
1545
1546 bprm->exec = bprm->p;
1547 retval = compat_copy_strings(bprm->envc, envp, bprm);
1548 if (retval < 0)
1549 goto out;
1550
1551 retval = compat_copy_strings(bprm->argc, argv, bprm);
1552 if (retval < 0)
1553 goto out;
1554
1555 retval = search_binary_handler(bprm, regs);
1556 if (retval >= 0) {
1557 free_arg_pages(bprm);
1558
1559 /* execve success */
1560 security_bprm_free(bprm);
David S. Miller4a805e82005-09-14 21:40:00 -07001561 acct_update_integrals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 kfree(bprm);
1563 return retval;
1564 }
1565
1566out:
1567 /* Something went wrong, return the inode and free the argument pages*/
1568 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1569 struct page * page = bprm->page[i];
1570 if (page)
1571 __free_page(page);
1572 }
1573
1574 if (bprm->security)
1575 security_bprm_free(bprm);
1576
1577out_mm:
1578 if (bprm->mm)
1579 mmdrop(bprm->mm);
1580
1581out_file:
1582 if (bprm->file) {
1583 allow_write_access(bprm->file);
1584 fput(bprm->file);
1585 }
1586
1587out_kfree:
1588 kfree(bprm);
1589
1590out_ret:
1591 return retval;
1592}
1593
1594#define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
1595
1596#define ROUND_UP(x,y) (((x)+(y)-1)/(y))
1597
1598/*
1599 * Ooo, nasty. We need here to frob 32-bit unsigned longs to
1600 * 64-bit unsigned longs.
1601 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001602static
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1604 unsigned long *fdset)
1605{
1606 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1607 if (ufdset) {
1608 unsigned long odd;
1609
1610 if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
1611 return -EFAULT;
1612
1613 odd = nr & 1UL;
1614 nr &= ~1UL;
1615 while (nr) {
1616 unsigned long h, l;
Heiko Carstens7116e992006-12-06 20:36:36 -08001617 if (__get_user(l, ufdset) || __get_user(h, ufdset+1))
1618 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 ufdset += 2;
1620 *fdset++ = h << 32 | l;
1621 nr -= 2;
1622 }
Heiko Carstens7116e992006-12-06 20:36:36 -08001623 if (odd && __get_user(*fdset, ufdset))
1624 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 } else {
1626 /* Tricky, must clear full unsigned long in the
1627 * kernel fdset at the end, this makes sure that
1628 * actually happens.
1629 */
1630 memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
1631 }
1632 return 0;
1633}
1634
Arjan van de Ven858119e2006-01-14 13:20:43 -08001635static
Heiko Carstens7116e992006-12-06 20:36:36 -08001636int compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1637 unsigned long *fdset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
1639 unsigned long odd;
1640 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1641
1642 if (!ufdset)
Heiko Carstens7116e992006-12-06 20:36:36 -08001643 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 odd = nr & 1UL;
1646 nr &= ~1UL;
1647 while (nr) {
1648 unsigned long h, l;
1649 l = *fdset++;
1650 h = l >> 32;
Heiko Carstens7116e992006-12-06 20:36:36 -08001651 if (__put_user(l, ufdset) || __put_user(h, ufdset+1))
1652 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 ufdset += 2;
1654 nr -= 2;
1655 }
Heiko Carstens7116e992006-12-06 20:36:36 -08001656 if (odd && __put_user(*fdset, ufdset))
1657 return -EFAULT;
1658 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
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;
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08001683 int size, max_fds, 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
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08001689 /* max_fds 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);
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08001692 max_fds = fdt->max_fds;
Linus Torvaldsac5b8b62005-09-09 15:42:34 -07001693 rcu_read_unlock();
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08001694 if (n > max_fds)
1695 n = max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
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
Heiko Carstens7116e992006-12-06 20:36:36 -08001733 if (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 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737out:
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
Heiko Carstens87c2b7c2006-11-02 22:06:58 -08001844 if (tsp) {
Andrew Morton643a6542006-02-11 17:55:52 -08001845 struct compat_timespec rts;
1846
Heiko Carstens87c2b7c2006-11-02 22:06:58 -08001847 if (current->personality & STICKY_TIMEOUTS)
1848 goto sticky;
1849
Andrew Morton643a6542006-02-11 17:55:52 -08001850 rts.tv_sec = timeout / HZ;
1851 rts.tv_nsec = (timeout % HZ) * (NSEC_PER_SEC/HZ);
1852 if (rts.tv_nsec >= NSEC_PER_SEC) {
1853 rts.tv_sec++;
1854 rts.tv_nsec -= NSEC_PER_SEC;
David Woodhouse9f729492006-01-18 17:44:05 -08001855 }
Andrew Morton74910e62006-02-17 13:52:58 -08001856 if (compat_timespec_compare(&rts, &ts) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -08001857 rts = ts;
Heiko Carstens87c2b7c2006-11-02 22:06:58 -08001858 if (copy_to_user(tsp, &rts, sizeof(rts))) {
1859sticky:
1860 /*
1861 * If an application puts its timeval in read-only
1862 * memory, we don't want the Linux-specific update to
1863 * the timeval to cause a fault after the select has
1864 * completed successfully. However, because we're not
1865 * updating the timeval, we can't restart the system
1866 * call.
1867 */
1868 if (ret == -ERESTARTNOHAND)
1869 ret = -EINTR;
1870 }
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
Davide Libenzif6dfb4f2007-03-07 20:41:21 -08002239
2240#ifdef CONFIG_EPOLL
2241
2242#ifdef CONFIG_HAS_COMPAT_EPOLL_EVENT
2243asmlinkage long compat_sys_epoll_ctl(int epfd, int op, int fd,
2244 struct compat_epoll_event __user *event)
2245{
2246 long err = 0;
2247 struct compat_epoll_event user;
2248 struct epoll_event __user *kernel = NULL;
2249
2250 if (event) {
2251 if (copy_from_user(&user, event, sizeof(user)))
2252 return -EFAULT;
2253 kernel = compat_alloc_user_space(sizeof(struct epoll_event));
2254 err |= __put_user(user.events, &kernel->events);
2255 err |= __put_user(user.data, &kernel->data);
2256 }
2257
2258 return err ? err : sys_epoll_ctl(epfd, op, fd, kernel);
2259}
2260
2261
2262asmlinkage long compat_sys_epoll_wait(int epfd,
2263 struct compat_epoll_event __user *events,
2264 int maxevents, int timeout)
2265{
2266 long i, ret, err = 0;
2267 struct epoll_event __user *kbuf;
2268 struct epoll_event ev;
2269
2270 if ((maxevents <= 0) ||
2271 (maxevents > (INT_MAX / sizeof(struct epoll_event))))
2272 return -EINVAL;
2273 kbuf = compat_alloc_user_space(sizeof(struct epoll_event) * maxevents);
2274 ret = sys_epoll_wait(epfd, kbuf, maxevents, timeout);
2275 for (i = 0; i < ret; i++) {
2276 err |= __get_user(ev.events, &kbuf[i].events);
2277 err |= __get_user(ev.data, &kbuf[i].data);
2278 err |= __put_user(ev.events, &events->events);
2279 err |= __put_user_unaligned(ev.data, &events->data);
2280 events++;
2281 }
2282
2283 return err ? -EFAULT: ret;
2284}
2285#endif /* CONFIG_HAS_COMPAT_EPOLL_EVENT */
2286
2287#ifdef TIF_RESTORE_SIGMASK
2288asmlinkage long compat_sys_epoll_pwait(int epfd,
2289 struct compat_epoll_event __user *events,
2290 int maxevents, int timeout,
2291 const compat_sigset_t __user *sigmask,
2292 compat_size_t sigsetsize)
2293{
2294 long err;
2295 compat_sigset_t csigmask;
2296 sigset_t ksigmask, sigsaved;
2297
2298 /*
2299 * If the caller wants a certain signal mask to be set during the wait,
2300 * we apply it here.
2301 */
2302 if (sigmask) {
2303 if (sigsetsize != sizeof(compat_sigset_t))
2304 return -EINVAL;
2305 if (copy_from_user(&csigmask, sigmask, sizeof(csigmask)))
2306 return -EFAULT;
2307 sigset_from_compat(&ksigmask, &csigmask);
2308 sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
2309 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
2310 }
2311
2312#ifdef CONFIG_HAS_COMPAT_EPOLL_EVENT
2313 err = compat_sys_epoll_wait(epfd, events, maxevents, timeout);
2314#else
2315 err = sys_epoll_wait(epfd, events, maxevents, timeout);
2316#endif
2317
2318 /*
2319 * If we changed the signal mask, we need to restore the original one.
2320 * In case we've got a signal while waiting, we do not restore the
2321 * signal mask yet, and we allow do_signal() to deliver the signal on
2322 * the way back to userspace, before the signal mask is restored.
2323 */
2324 if (sigmask) {
2325 if (err == -EINTR) {
2326 memcpy(&current->saved_sigmask, &sigsaved,
2327 sizeof(sigsaved));
2328 set_thread_flag(TIF_RESTORE_SIGMASK);
2329 } else
2330 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2331 }
2332
2333 return err;
2334}
2335#endif /* TIF_RESTORE_SIGMASK */
2336
2337#endif /* CONFIG_EPOLL */