blob: 72e5e69238288f976eefb6748624aac17ef85449 [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
Andi Kleena1060092007-05-02 19:27:21 +0200374 sprintf(buf,"'%c'", (cmd>>_IOC_TYPESHIFT) & _IOC_TYPEMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 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) "
Andi Kleena1060092007-05-02 19:27:21 +0200378 "cmd(%08x){t:%s;sz:%u} arg(%08x) on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 current->comm, current->pid,
380 (int)fd, (unsigned int)cmd, buf,
Andi Kleena1060092007-05-02 19:27:21 +0200381 (cmd >> _IOC_SIZESHIFT) & _IOC_SIZEMASK,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 (unsigned int)arg, fn);
383
384 if (path)
385 free_page((unsigned long)path);
386}
387
388asmlinkage long compat_sys_ioctl(unsigned int fd, unsigned int cmd,
389 unsigned long arg)
390{
391 struct file *filp;
392 int error = -EBADF;
393 struct ioctl_trans *t;
394 int fput_needed;
395
396 filp = fget_light(fd, &fput_needed);
397 if (!filp)
398 goto out;
399
400 /* RED-PEN how should LSM module know it's handling 32bit? */
401 error = security_file_ioctl(filp, cmd, arg);
402 if (error)
403 goto out_fput;
404
405 /*
406 * To allow the compat_ioctl handlers to be self contained
407 * we need to check the common ioctls here first.
408 * Just handle them with the standard handlers below.
409 */
410 switch (cmd) {
411 case FIOCLEX:
412 case FIONCLEX:
413 case FIONBIO:
414 case FIOASYNC:
415 case FIOQSIZE:
416 break;
417
418 case FIBMAP:
419 case FIGETBSZ:
420 case FIONREAD:
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800421 if (S_ISREG(filp->f_path.dentry->d_inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 break;
423 /*FALL THROUGH*/
424
425 default:
426 if (filp->f_op && filp->f_op->compat_ioctl) {
427 error = filp->f_op->compat_ioctl(filp, cmd, arg);
428 if (error != -ENOIOCTLCMD)
429 goto out_fput;
430 }
431
432 if (!filp->f_op ||
433 (!filp->f_op->ioctl && !filp->f_op->unlocked_ioctl))
434 goto do_ioctl;
435 break;
436 }
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 for (t = ioctl32_hash_table[ioctl32_hash(cmd)]; t; t = t->next) {
439 if (t->cmd == cmd)
440 goto found_handler;
441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800443 if (S_ISSOCK(filp->f_path.dentry->d_inode->i_mode) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
445 error = siocdevprivate_ioctl(fd, cmd, arg);
446 } else {
447 static int count;
448
449 if (++count <= 50)
450 compat_ioctl_error(filp, fd, cmd, arg);
451 error = -EINVAL;
452 }
453
454 goto out_fput;
455
456 found_handler:
457 if (t->handler) {
458 lock_kernel();
459 error = t->handler(fd, cmd, arg, filp);
460 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 goto out_fput;
462 }
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 do_ioctl:
465 error = vfs_ioctl(filp, fd, cmd, arg);
466 out_fput:
467 fput_light(filp, fput_needed);
468 out:
469 return error;
470}
471
472static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
473{
474 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
475 __get_user(kfl->l_type, &ufl->l_type) ||
476 __get_user(kfl->l_whence, &ufl->l_whence) ||
477 __get_user(kfl->l_start, &ufl->l_start) ||
478 __get_user(kfl->l_len, &ufl->l_len) ||
479 __get_user(kfl->l_pid, &ufl->l_pid))
480 return -EFAULT;
481 return 0;
482}
483
484static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
485{
486 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
487 __put_user(kfl->l_type, &ufl->l_type) ||
488 __put_user(kfl->l_whence, &ufl->l_whence) ||
489 __put_user(kfl->l_start, &ufl->l_start) ||
490 __put_user(kfl->l_len, &ufl->l_len) ||
491 __put_user(kfl->l_pid, &ufl->l_pid))
492 return -EFAULT;
493 return 0;
494}
495
496#ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
497static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
498{
499 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
500 __get_user(kfl->l_type, &ufl->l_type) ||
501 __get_user(kfl->l_whence, &ufl->l_whence) ||
502 __get_user(kfl->l_start, &ufl->l_start) ||
503 __get_user(kfl->l_len, &ufl->l_len) ||
504 __get_user(kfl->l_pid, &ufl->l_pid))
505 return -EFAULT;
506 return 0;
507}
508#endif
509
510#ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
511static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
512{
513 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
514 __put_user(kfl->l_type, &ufl->l_type) ||
515 __put_user(kfl->l_whence, &ufl->l_whence) ||
516 __put_user(kfl->l_start, &ufl->l_start) ||
517 __put_user(kfl->l_len, &ufl->l_len) ||
518 __put_user(kfl->l_pid, &ufl->l_pid))
519 return -EFAULT;
520 return 0;
521}
522#endif
523
524asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd,
525 unsigned long arg)
526{
527 mm_segment_t old_fs;
528 struct flock f;
529 long ret;
530
531 switch (cmd) {
532 case F_GETLK:
533 case F_SETLK:
534 case F_SETLKW:
535 ret = get_compat_flock(&f, compat_ptr(arg));
536 if (ret != 0)
537 break;
538 old_fs = get_fs();
539 set_fs(KERNEL_DS);
540 ret = sys_fcntl(fd, cmd, (unsigned long)&f);
541 set_fs(old_fs);
542 if (cmd == F_GETLK && ret == 0) {
NeilBrown2520f142006-01-08 01:02:40 -0800543 /* GETLK was successfule and we need to return the data...
544 * but it needs to fit in the compat structure.
545 * l_start shouldn't be too big, unless the original
546 * start + end is greater than COMPAT_OFF_T_MAX, in which
547 * case the app was asking for trouble, so we return
548 * -EOVERFLOW in that case.
549 * l_len could be too big, in which case we just truncate it,
550 * and only allow the app to see that part of the conflicting
551 * lock that might make sense to it anyway
552 */
553
554 if (f.l_start > COMPAT_OFF_T_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 ret = -EOVERFLOW;
NeilBrown2520f142006-01-08 01:02:40 -0800556 if (f.l_len > COMPAT_OFF_T_MAX)
557 f.l_len = COMPAT_OFF_T_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (ret == 0)
559 ret = put_compat_flock(&f, compat_ptr(arg));
560 }
561 break;
562
563 case F_GETLK64:
564 case F_SETLK64:
565 case F_SETLKW64:
566 ret = get_compat_flock64(&f, compat_ptr(arg));
567 if (ret != 0)
568 break;
569 old_fs = get_fs();
570 set_fs(KERNEL_DS);
571 ret = sys_fcntl(fd, (cmd == F_GETLK64) ? F_GETLK :
572 ((cmd == F_SETLK64) ? F_SETLK : F_SETLKW),
573 (unsigned long)&f);
574 set_fs(old_fs);
575 if (cmd == F_GETLK64 && ret == 0) {
NeilBrown2520f142006-01-08 01:02:40 -0800576 /* need to return lock information - see above for commentary */
577 if (f.l_start > COMPAT_LOFF_T_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 ret = -EOVERFLOW;
NeilBrown2520f142006-01-08 01:02:40 -0800579 if (f.l_len > COMPAT_LOFF_T_MAX)
580 f.l_len = COMPAT_LOFF_T_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (ret == 0)
582 ret = put_compat_flock64(&f, compat_ptr(arg));
583 }
584 break;
585
586 default:
587 ret = sys_fcntl(fd, cmd, arg);
588 break;
589 }
590 return ret;
591}
592
593asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd,
594 unsigned long arg)
595{
596 if ((cmd == F_GETLK64) || (cmd == F_SETLK64) || (cmd == F_SETLKW64))
597 return -EINVAL;
598 return compat_sys_fcntl64(fd, cmd, arg);
599}
600
601asmlinkage long
602compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p)
603{
604 long ret;
605 aio_context_t ctx64;
606
607 mm_segment_t oldfs = get_fs();
608 if (unlikely(get_user(ctx64, ctx32p)))
609 return -EFAULT;
610
611 set_fs(KERNEL_DS);
612 /* The __user pointer cast is valid because of the set_fs() */
613 ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64);
614 set_fs(oldfs);
615 /* truncating is ok because it's a user address */
616 if (!ret)
617 ret = put_user((u32) ctx64, ctx32p);
618 return ret;
619}
620
621asmlinkage long
622compat_sys_io_getevents(aio_context_t ctx_id,
623 unsigned long min_nr,
624 unsigned long nr,
625 struct io_event __user *events,
626 struct compat_timespec __user *timeout)
627{
628 long ret;
629 struct timespec t;
630 struct timespec __user *ut = NULL;
631
632 ret = -EFAULT;
633 if (unlikely(!access_ok(VERIFY_WRITE, events,
634 nr * sizeof(struct io_event))))
635 goto out;
636 if (timeout) {
637 if (get_compat_timespec(&t, timeout))
638 goto out;
639
640 ut = compat_alloc_user_space(sizeof(*ut));
641 if (copy_to_user(ut, &t, sizeof(t)) )
642 goto out;
643 }
644 ret = sys_io_getevents(ctx_id, min_nr, nr, events, ut);
645out:
646 return ret;
647}
648
649static inline long
650copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64)
651{
652 compat_uptr_t uptr;
653 int i;
654
655 for (i = 0; i < nr; ++i) {
656 if (get_user(uptr, ptr32 + i))
657 return -EFAULT;
658 if (put_user(compat_ptr(uptr), ptr64 + i))
659 return -EFAULT;
660 }
661 return 0;
662}
663
664#define MAX_AIO_SUBMITS (PAGE_SIZE/sizeof(struct iocb *))
665
666asmlinkage long
667compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb)
668{
669 struct iocb __user * __user *iocb64;
670 long ret;
671
672 if (unlikely(nr < 0))
673 return -EINVAL;
674
675 if (nr > MAX_AIO_SUBMITS)
676 nr = MAX_AIO_SUBMITS;
677
678 iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64));
679 ret = copy_iocb(nr, iocb, iocb64);
680 if (!ret)
681 ret = sys_io_submit(ctx_id, nr, iocb64);
682 return ret;
683}
684
685struct compat_ncp_mount_data {
686 compat_int_t version;
687 compat_uint_t ncp_fd;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700688 __compat_uid_t mounted_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 compat_pid_t wdog_pid;
690 unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
691 compat_uint_t time_out;
692 compat_uint_t retry_count;
693 compat_uint_t flags;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700694 __compat_uid_t uid;
695 __compat_gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 compat_mode_t file_mode;
697 compat_mode_t dir_mode;
698};
699
700struct compat_ncp_mount_data_v4 {
701 compat_int_t version;
702 compat_ulong_t flags;
703 compat_ulong_t mounted_uid;
704 compat_long_t wdog_pid;
705 compat_uint_t ncp_fd;
706 compat_uint_t time_out;
707 compat_uint_t retry_count;
708 compat_ulong_t uid;
709 compat_ulong_t gid;
710 compat_ulong_t file_mode;
711 compat_ulong_t dir_mode;
712};
713
714static void *do_ncp_super_data_conv(void *raw_data)
715{
716 int version = *(unsigned int *)raw_data;
717
718 if (version == 3) {
719 struct compat_ncp_mount_data *c_n = raw_data;
720 struct ncp_mount_data *n = raw_data;
721
722 n->dir_mode = c_n->dir_mode;
723 n->file_mode = c_n->file_mode;
724 n->gid = c_n->gid;
725 n->uid = c_n->uid;
726 memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
727 n->wdog_pid = c_n->wdog_pid;
728 n->mounted_uid = c_n->mounted_uid;
729 } else if (version == 4) {
730 struct compat_ncp_mount_data_v4 *c_n = raw_data;
731 struct ncp_mount_data_v4 *n = raw_data;
732
733 n->dir_mode = c_n->dir_mode;
734 n->file_mode = c_n->file_mode;
735 n->gid = c_n->gid;
736 n->uid = c_n->uid;
737 n->retry_count = c_n->retry_count;
738 n->time_out = c_n->time_out;
739 n->ncp_fd = c_n->ncp_fd;
740 n->wdog_pid = c_n->wdog_pid;
741 n->mounted_uid = c_n->mounted_uid;
742 n->flags = c_n->flags;
743 } else if (version != 5) {
744 return NULL;
745 }
746
747 return raw_data;
748}
749
750struct compat_smb_mount_data {
751 compat_int_t version;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700752 __compat_uid_t mounted_uid;
753 __compat_uid_t uid;
754 __compat_gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 compat_mode_t file_mode;
756 compat_mode_t dir_mode;
757};
758
759static void *do_smb_super_data_conv(void *raw_data)
760{
761 struct smb_mount_data *s = raw_data;
762 struct compat_smb_mount_data *c_s = raw_data;
763
764 if (c_s->version != SMB_MOUNT_OLDVERSION)
765 goto out;
766 s->dir_mode = c_s->dir_mode;
767 s->file_mode = c_s->file_mode;
768 s->gid = c_s->gid;
769 s->uid = c_s->uid;
770 s->mounted_uid = c_s->mounted_uid;
771 out:
772 return raw_data;
773}
774
David Howells9a9947b2005-04-18 10:54:51 -0700775struct compat_nfs_string {
776 compat_uint_t len;
David Howells5fc3e622005-04-27 15:39:03 -0700777 compat_uptr_t data;
David Howells9a9947b2005-04-18 10:54:51 -0700778};
779
780static inline void compat_nfs_string(struct nfs_string *dst,
781 struct compat_nfs_string *src)
782{
783 dst->data = compat_ptr(src->data);
784 dst->len = src->len;
785}
786
787struct compat_nfs4_mount_data_v1 {
788 compat_int_t version;
789 compat_int_t flags;
790 compat_int_t rsize;
791 compat_int_t wsize;
792 compat_int_t timeo;
793 compat_int_t retrans;
794 compat_int_t acregmin;
795 compat_int_t acregmax;
796 compat_int_t acdirmin;
797 compat_int_t acdirmax;
798 struct compat_nfs_string client_addr;
799 struct compat_nfs_string mnt_path;
800 struct compat_nfs_string hostname;
801 compat_uint_t host_addrlen;
David Howells5fc3e622005-04-27 15:39:03 -0700802 compat_uptr_t host_addr;
David Howells9a9947b2005-04-18 10:54:51 -0700803 compat_int_t proto;
804 compat_int_t auth_flavourlen;
David Howells5fc3e622005-04-27 15:39:03 -0700805 compat_uptr_t auth_flavours;
David Howells9a9947b2005-04-18 10:54:51 -0700806};
807
808static int do_nfs4_super_data_conv(void *raw_data)
809{
810 int version = *(compat_uint_t *) raw_data;
811
812 if (version == 1) {
813 struct compat_nfs4_mount_data_v1 *raw = raw_data;
814 struct nfs4_mount_data *real = raw_data;
815
816 /* copy the fields backwards */
817 real->auth_flavours = compat_ptr(raw->auth_flavours);
818 real->auth_flavourlen = raw->auth_flavourlen;
819 real->proto = raw->proto;
820 real->host_addr = compat_ptr(raw->host_addr);
821 real->host_addrlen = raw->host_addrlen;
822 compat_nfs_string(&real->hostname, &raw->hostname);
823 compat_nfs_string(&real->mnt_path, &raw->mnt_path);
824 compat_nfs_string(&real->client_addr, &raw->client_addr);
825 real->acdirmax = raw->acdirmax;
826 real->acdirmin = raw->acdirmin;
827 real->acregmax = raw->acregmax;
828 real->acregmin = raw->acregmin;
829 real->retrans = raw->retrans;
830 real->timeo = raw->timeo;
831 real->wsize = raw->wsize;
832 real->rsize = raw->rsize;
833 real->flags = raw->flags;
834 real->version = raw->version;
835 }
836 else {
837 return -EINVAL;
838 }
839
840 return 0;
841}
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843#define SMBFS_NAME "smbfs"
844#define NCPFS_NAME "ncpfs"
David Howells9a9947b2005-04-18 10:54:51 -0700845#define NFS4_NAME "nfs4"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847asmlinkage long compat_sys_mount(char __user * dev_name, char __user * dir_name,
848 char __user * type, unsigned long flags,
849 void __user * data)
850{
851 unsigned long type_page;
852 unsigned long data_page;
853 unsigned long dev_page;
854 char *dir_page;
855 int retval;
856
857 retval = copy_mount_options (type, &type_page);
858 if (retval < 0)
859 goto out;
860
861 dir_page = getname(dir_name);
862 retval = PTR_ERR(dir_page);
863 if (IS_ERR(dir_page))
864 goto out1;
865
866 retval = copy_mount_options (dev_name, &dev_page);
867 if (retval < 0)
868 goto out2;
869
870 retval = copy_mount_options (data, &data_page);
871 if (retval < 0)
872 goto out3;
873
874 retval = -EINVAL;
875
Andrey Mirkin822191a2006-12-06 20:31:35 -0800876 if (type_page && data_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (!strcmp((char *)type_page, SMBFS_NAME)) {
878 do_smb_super_data_conv((void *)data_page);
879 } else if (!strcmp((char *)type_page, NCPFS_NAME)) {
880 do_ncp_super_data_conv((void *)data_page);
David Howells9a9947b2005-04-18 10:54:51 -0700881 } else if (!strcmp((char *)type_page, NFS4_NAME)) {
882 if (do_nfs4_super_data_conv((void *) data_page))
883 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885 }
886
887 lock_kernel();
888 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
889 flags, (void*)data_page);
890 unlock_kernel();
891
David Howells9a9947b2005-04-18 10:54:51 -0700892 out4:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 free_page(data_page);
894 out3:
895 free_page(dev_page);
896 out2:
897 putname(dir_page);
898 out1:
899 free_page(type_page);
900 out:
901 return retval;
902}
903
904#define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
905#define COMPAT_ROUND_UP(x) (((x)+sizeof(compat_long_t)-1) & \
906 ~(sizeof(compat_long_t)-1))
907
908struct compat_old_linux_dirent {
909 compat_ulong_t d_ino;
910 compat_ulong_t d_offset;
911 unsigned short d_namlen;
912 char d_name[1];
913};
914
915struct compat_readdir_callback {
916 struct compat_old_linux_dirent __user *dirent;
917 int result;
918};
919
920static int compat_fillonedir(void *__buf, const char *name, int namlen,
David Howellsafefdbb2006-10-03 01:13:46 -0700921 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 struct compat_readdir_callback *buf = __buf;
924 struct compat_old_linux_dirent __user *dirent;
David Howellsafefdbb2006-10-03 01:13:46 -0700925 compat_ulong_t d_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 if (buf->result)
928 return -EINVAL;
David Howellsafefdbb2006-10-03 01:13:46 -0700929 d_ino = ino;
930 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
931 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 buf->result++;
933 dirent = buf->dirent;
934 if (!access_ok(VERIFY_WRITE, dirent,
935 (unsigned long)(dirent->d_name + namlen + 1) -
936 (unsigned long)dirent))
937 goto efault;
David Howellsafefdbb2006-10-03 01:13:46 -0700938 if ( __put_user(d_ino, &dirent->d_ino) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 __put_user(offset, &dirent->d_offset) ||
940 __put_user(namlen, &dirent->d_namlen) ||
941 __copy_to_user(dirent->d_name, name, namlen) ||
942 __put_user(0, dirent->d_name + namlen))
943 goto efault;
944 return 0;
945efault:
946 buf->result = -EFAULT;
947 return -EFAULT;
948}
949
950asmlinkage long compat_sys_old_readdir(unsigned int fd,
951 struct compat_old_linux_dirent __user *dirent, unsigned int count)
952{
953 int error;
954 struct file *file;
955 struct compat_readdir_callback buf;
956
957 error = -EBADF;
958 file = fget(fd);
959 if (!file)
960 goto out;
961
962 buf.result = 0;
963 buf.dirent = dirent;
964
965 error = vfs_readdir(file, compat_fillonedir, &buf);
966 if (error >= 0)
967 error = buf.result;
968
969 fput(file);
970out:
971 return error;
972}
973
974struct compat_linux_dirent {
975 compat_ulong_t d_ino;
976 compat_ulong_t d_off;
977 unsigned short d_reclen;
978 char d_name[1];
979};
980
981struct compat_getdents_callback {
982 struct compat_linux_dirent __user *current_dir;
983 struct compat_linux_dirent __user *previous;
984 int count;
985 int error;
986};
987
988static int compat_filldir(void *__buf, const char *name, int namlen,
David Howellsafefdbb2006-10-03 01:13:46 -0700989 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 struct compat_linux_dirent __user * dirent;
992 struct compat_getdents_callback *buf = __buf;
David Howellsafefdbb2006-10-03 01:13:46 -0700993 compat_ulong_t d_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 int reclen = COMPAT_ROUND_UP(NAME_OFFSET(dirent) + namlen + 2);
995
996 buf->error = -EINVAL; /* only used if we fail.. */
997 if (reclen > buf->count)
998 return -EINVAL;
David Howellsafefdbb2006-10-03 01:13:46 -0700999 d_ino = ino;
1000 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
1001 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 dirent = buf->previous;
1003 if (dirent) {
1004 if (__put_user(offset, &dirent->d_off))
1005 goto efault;
1006 }
1007 dirent = buf->current_dir;
David Howellsafefdbb2006-10-03 01:13:46 -07001008 if (__put_user(d_ino, &dirent->d_ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 goto efault;
1010 if (__put_user(reclen, &dirent->d_reclen))
1011 goto efault;
1012 if (copy_to_user(dirent->d_name, name, namlen))
1013 goto efault;
1014 if (__put_user(0, dirent->d_name + namlen))
1015 goto efault;
1016 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
1017 goto efault;
1018 buf->previous = dirent;
1019 dirent = (void __user *)dirent + reclen;
1020 buf->current_dir = dirent;
1021 buf->count -= reclen;
1022 return 0;
1023efault:
1024 buf->error = -EFAULT;
1025 return -EFAULT;
1026}
1027
1028asmlinkage long compat_sys_getdents(unsigned int fd,
1029 struct compat_linux_dirent __user *dirent, unsigned int count)
1030{
1031 struct file * file;
1032 struct compat_linux_dirent __user * lastdirent;
1033 struct compat_getdents_callback buf;
1034 int error;
1035
1036 error = -EFAULT;
1037 if (!access_ok(VERIFY_WRITE, dirent, count))
1038 goto out;
1039
1040 error = -EBADF;
1041 file = fget(fd);
1042 if (!file)
1043 goto out;
1044
1045 buf.current_dir = dirent;
1046 buf.previous = NULL;
1047 buf.count = count;
1048 buf.error = 0;
1049
1050 error = vfs_readdir(file, compat_filldir, &buf);
1051 if (error < 0)
1052 goto out_putf;
1053 error = buf.error;
1054 lastdirent = buf.previous;
1055 if (lastdirent) {
1056 if (put_user(file->f_pos, &lastdirent->d_off))
1057 error = -EFAULT;
1058 else
1059 error = count - buf.count;
1060 }
1061
1062out_putf:
1063 fput(file);
1064out:
1065 return error;
1066}
1067
1068#ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64
1069#define COMPAT_ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1))
1070
1071struct compat_getdents_callback64 {
1072 struct linux_dirent64 __user *current_dir;
1073 struct linux_dirent64 __user *previous;
1074 int count;
1075 int error;
1076};
1077
1078static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset,
David Howellsafefdbb2006-10-03 01:13:46 -07001079 u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 struct linux_dirent64 __user *dirent;
1082 struct compat_getdents_callback64 *buf = __buf;
1083 int jj = NAME_OFFSET(dirent);
1084 int reclen = COMPAT_ROUND_UP64(jj + namlen + 1);
1085 u64 off;
1086
1087 buf->error = -EINVAL; /* only used if we fail.. */
1088 if (reclen > buf->count)
1089 return -EINVAL;
1090 dirent = buf->previous;
1091
1092 if (dirent) {
1093 if (__put_user_unaligned(offset, &dirent->d_off))
1094 goto efault;
1095 }
1096 dirent = buf->current_dir;
1097 if (__put_user_unaligned(ino, &dirent->d_ino))
1098 goto efault;
1099 off = 0;
1100 if (__put_user_unaligned(off, &dirent->d_off))
1101 goto efault;
1102 if (__put_user(reclen, &dirent->d_reclen))
1103 goto efault;
1104 if (__put_user(d_type, &dirent->d_type))
1105 goto efault;
1106 if (copy_to_user(dirent->d_name, name, namlen))
1107 goto efault;
1108 if (__put_user(0, dirent->d_name + namlen))
1109 goto efault;
1110 buf->previous = dirent;
1111 dirent = (void __user *)dirent + reclen;
1112 buf->current_dir = dirent;
1113 buf->count -= reclen;
1114 return 0;
1115efault:
1116 buf->error = -EFAULT;
1117 return -EFAULT;
1118}
1119
1120asmlinkage long compat_sys_getdents64(unsigned int fd,
1121 struct linux_dirent64 __user * dirent, unsigned int count)
1122{
1123 struct file * file;
1124 struct linux_dirent64 __user * lastdirent;
1125 struct compat_getdents_callback64 buf;
1126 int error;
1127
1128 error = -EFAULT;
1129 if (!access_ok(VERIFY_WRITE, dirent, count))
1130 goto out;
1131
1132 error = -EBADF;
1133 file = fget(fd);
1134 if (!file)
1135 goto out;
1136
1137 buf.current_dir = dirent;
1138 buf.previous = NULL;
1139 buf.count = count;
1140 buf.error = 0;
1141
1142 error = vfs_readdir(file, compat_filldir64, &buf);
1143 if (error < 0)
1144 goto out_putf;
1145 error = buf.error;
1146 lastdirent = buf.previous;
1147 if (lastdirent) {
1148 typeof(lastdirent->d_off) d_off = file->f_pos;
Heiko Carstens7116e992006-12-06 20:36:36 -08001149 error = -EFAULT;
1150 if (__put_user_unaligned(d_off, &lastdirent->d_off))
1151 goto out_putf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 error = count - buf.count;
1153 }
1154
1155out_putf:
1156 fput(file);
1157out:
1158 return error;
1159}
1160#endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
1161
1162static ssize_t compat_do_readv_writev(int type, struct file *file,
1163 const struct compat_iovec __user *uvector,
1164 unsigned long nr_segs, loff_t *pos)
1165{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 compat_ssize_t tot_len;
1167 struct iovec iovstack[UIO_FASTIOV];
1168 struct iovec *iov=iovstack, *vector;
1169 ssize_t ret;
1170 int seg;
1171 io_fn_t fn;
1172 iov_fn_t fnv;
1173
1174 /*
1175 * SuS says "The readv() function *may* fail if the iovcnt argument
1176 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
1177 * traditionally returned zero for zero segments, so...
1178 */
1179 ret = 0;
1180 if (nr_segs == 0)
1181 goto out;
1182
1183 /*
1184 * First get the "struct iovec" from user memory and
1185 * verify all the pointers
1186 */
1187 ret = -EINVAL;
1188 if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0))
1189 goto out;
1190 if (!file->f_op)
1191 goto out;
1192 if (nr_segs > UIO_FASTIOV) {
1193 ret = -ENOMEM;
1194 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
1195 if (!iov)
1196 goto out;
1197 }
1198 ret = -EFAULT;
1199 if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
1200 goto out;
1201
1202 /*
1203 * Single unix specification:
1204 * We should -EINVAL if an element length is not >= 0 and fitting an
1205 * ssize_t. The total length is fitting an ssize_t
1206 *
1207 * Be careful here because iov_len is a size_t not an ssize_t
1208 */
1209 tot_len = 0;
1210 vector = iov;
1211 ret = -EINVAL;
1212 for (seg = 0 ; seg < nr_segs; seg++) {
1213 compat_ssize_t tmp = tot_len;
1214 compat_ssize_t len;
1215 compat_uptr_t buf;
1216
1217 if (__get_user(len, &uvector->iov_len) ||
1218 __get_user(buf, &uvector->iov_base)) {
1219 ret = -EFAULT;
1220 goto out;
1221 }
1222 if (len < 0) /* size_t not fitting an compat_ssize_t .. */
1223 goto out;
1224 tot_len += len;
1225 if (tot_len < tmp) /* maths overflow on the compat_ssize_t */
1226 goto out;
1227 vector->iov_base = compat_ptr(buf);
1228 vector->iov_len = (compat_size_t) len;
1229 uvector++;
1230 vector++;
1231 }
1232 if (tot_len == 0) {
1233 ret = 0;
1234 goto out;
1235 }
1236
1237 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001238 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 goto out;
1240
James Morrise7edf9c2006-04-26 02:45:03 -04001241 ret = security_file_permission(file, type == READ ? MAY_READ:MAY_WRITE);
1242 if (ret)
1243 goto out;
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 fnv = NULL;
1246 if (type == READ) {
1247 fn = file->f_op->read;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001248 fnv = file->f_op->aio_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 } else {
1250 fn = (io_fn_t)file->f_op->write;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001251 fnv = file->f_op->aio_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001254 if (fnv)
1255 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
1256 pos, fnv);
1257 else
1258 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260out:
1261 if (iov != iovstack)
1262 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -04001263 if ((ret + (type == READ)) > 0) {
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001264 struct dentry *dentry = file->f_path.dentry;
Robert Love0eeca282005-07-12 17:06:03 -04001265 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;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001287 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 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;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001310 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 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;
Al Viro90cbad62006-10-10 22:44:17 +01001325 struct iovec __user *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;
Heiko Carstens7116e992006-12-06 20:36:36 -08001618 if (__get_user(l, ufdset) || __get_user(h, ufdset+1))
1619 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 ufdset += 2;
1621 *fdset++ = h << 32 | l;
1622 nr -= 2;
1623 }
Heiko Carstens7116e992006-12-06 20:36:36 -08001624 if (odd && __get_user(*fdset, ufdset))
1625 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 } 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
Heiko Carstens7116e992006-12-06 20:36:36 -08001637int compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1638 unsigned long *fdset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
1640 unsigned long odd;
1641 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1642
1643 if (!ufdset)
Heiko Carstens7116e992006-12-06 20:36:36 -08001644 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 odd = nr & 1UL;
1647 nr &= ~1UL;
1648 while (nr) {
1649 unsigned long h, l;
1650 l = *fdset++;
1651 h = l >> 32;
Heiko Carstens7116e992006-12-06 20:36:36 -08001652 if (__put_user(l, ufdset) || __put_user(h, ufdset+1))
1653 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 ufdset += 2;
1655 nr -= 2;
1656 }
Heiko Carstens7116e992006-12-06 20:36:36 -08001657 if (odd && __put_user(*fdset, ufdset))
1658 return -EFAULT;
1659 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660}
1661
1662
1663/*
1664 * This is a virtual copy of sys_select from fs/select.c and probably
1665 * should be compared to it from time to time
1666 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668/*
1669 * We can actually return ERESTARTSYS instead of EINTR, but I'd
1670 * like to be certain this leads to no problems. So I return
1671 * EINTR just for safety.
1672 *
1673 * Update: ERESTARTSYS breaks at least the xview clock binary, so
1674 * I'm trying ERESTARTNOHAND which restart only when you want to.
1675 */
1676#define MAX_SELECT_SECONDS \
1677 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
1678
David Woodhouse9f729492006-01-18 17:44:05 -08001679int compat_core_sys_select(int n, compat_ulong_t __user *inp,
1680 compat_ulong_t __user *outp, compat_ulong_t __user *exp, s64 *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
1682 fd_set_bits fds;
1683 char *bits;
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08001684 int size, max_fds, ret = -EINVAL;
Linus Torvaldsa4531ed2005-09-09 15:10:52 -07001685 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (n < 0)
1688 goto out_nofds;
1689
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08001690 /* max_fds can increase, so grab it once to avoid race */
Linus Torvaldsac5b8b62005-09-09 15:42:34 -07001691 rcu_read_lock();
Linus Torvaldsa4531ed2005-09-09 15:10:52 -07001692 fdt = files_fdtable(current->files);
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08001693 max_fds = fdt->max_fds;
Linus Torvaldsac5b8b62005-09-09 15:42:34 -07001694 rcu_read_unlock();
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08001695 if (n > max_fds)
1696 n = max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698 /*
1699 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
1700 * since we used fdset we need to allocate memory in units of
1701 * long-words.
1702 */
1703 ret = -ENOMEM;
1704 size = FDS_BYTES(n);
Vadim Lobanov68c34312006-03-28 01:56:35 -08001705 bits = kmalloc(6 * size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (!bits)
1707 goto out_nofds;
1708 fds.in = (unsigned long *) bits;
1709 fds.out = (unsigned long *) (bits + size);
1710 fds.ex = (unsigned long *) (bits + 2*size);
1711 fds.res_in = (unsigned long *) (bits + 3*size);
1712 fds.res_out = (unsigned long *) (bits + 4*size);
1713 fds.res_ex = (unsigned long *) (bits + 5*size);
1714
1715 if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
1716 (ret = compat_get_fd_set(n, outp, fds.out)) ||
1717 (ret = compat_get_fd_set(n, exp, fds.ex)))
1718 goto out;
1719 zero_fd_set(n, fds.res_in);
1720 zero_fd_set(n, fds.res_out);
1721 zero_fd_set(n, fds.res_ex);
1722
David Woodhouse9f729492006-01-18 17:44:05 -08001723 ret = do_select(n, &fds, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 if (ret < 0)
1726 goto out;
1727 if (!ret) {
1728 ret = -ERESTARTNOHAND;
1729 if (signal_pending(current))
1730 goto out;
1731 ret = 0;
1732 }
1733
Heiko Carstens7116e992006-12-06 20:36:36 -08001734 if (compat_set_fd_set(n, inp, fds.res_in) ||
1735 compat_set_fd_set(n, outp, fds.res_out) ||
1736 compat_set_fd_set(n, exp, fds.res_ex))
1737 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738out:
Vadim Lobanov68c34312006-03-28 01:56:35 -08001739 kfree(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740out_nofds:
1741 return ret;
1742}
1743
David Woodhouse9f729492006-01-18 17:44:05 -08001744asmlinkage long compat_sys_select(int n, compat_ulong_t __user *inp,
1745 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1746 struct compat_timeval __user *tvp)
1747{
1748 s64 timeout = -1;
1749 struct compat_timeval tv;
1750 int ret;
1751
1752 if (tvp) {
1753 if (copy_from_user(&tv, tvp, sizeof(tv)))
1754 return -EFAULT;
1755
1756 if (tv.tv_sec < 0 || tv.tv_usec < 0)
1757 return -EINVAL;
1758
1759 /* Cast to u64 to make GCC stop complaining */
1760 if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
1761 timeout = -1; /* infinite */
1762 else {
David S. Miller7e732bf2006-01-19 16:40:42 -08001763 timeout = ROUND_UP(tv.tv_usec, 1000000/HZ);
David Woodhouse9f729492006-01-18 17:44:05 -08001764 timeout += tv.tv_sec * HZ;
1765 }
1766 }
1767
1768 ret = compat_core_sys_select(n, inp, outp, exp, &timeout);
1769
1770 if (tvp) {
Andrew Morton643a6542006-02-11 17:55:52 -08001771 struct compat_timeval rtv;
1772
David Woodhouse9f729492006-01-18 17:44:05 -08001773 if (current->personality & STICKY_TIMEOUTS)
1774 goto sticky;
Andrew Morton643a6542006-02-11 17:55:52 -08001775 rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
1776 rtv.tv_sec = timeout;
Andrew Morton74910e62006-02-17 13:52:58 -08001777 if (compat_timeval_compare(&rtv, &tv) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -08001778 rtv = tv;
1779 if (copy_to_user(tvp, &rtv, sizeof(rtv))) {
David Woodhouse9f729492006-01-18 17:44:05 -08001780sticky:
1781 /*
1782 * If an application puts its timeval in read-only
1783 * memory, we don't want the Linux-specific update to
1784 * the timeval to cause a fault after the select has
1785 * completed successfully. However, because we're not
1786 * updating the timeval, we can't restart the system
1787 * call.
1788 */
1789 if (ret == -ERESTARTNOHAND)
1790 ret = -EINTR;
1791 }
1792 }
1793
1794 return ret;
1795}
1796
1797#ifdef TIF_RESTORE_SIGMASK
1798asmlinkage long compat_sys_pselect7(int n, compat_ulong_t __user *inp,
1799 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1800 struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask,
1801 compat_size_t sigsetsize)
1802{
1803 compat_sigset_t ss32;
1804 sigset_t ksigmask, sigsaved;
Andrew Mortoncb82a6c2006-02-01 03:04:49 -08001805 s64 timeout = MAX_SCHEDULE_TIMEOUT;
David Woodhouse9f729492006-01-18 17:44:05 -08001806 struct compat_timespec ts;
1807 int ret;
1808
1809 if (tsp) {
1810 if (copy_from_user(&ts, tsp, sizeof(ts)))
1811 return -EFAULT;
1812
1813 if (ts.tv_sec < 0 || ts.tv_nsec < 0)
1814 return -EINVAL;
1815 }
1816
1817 if (sigmask) {
1818 if (sigsetsize != sizeof(compat_sigset_t))
1819 return -EINVAL;
1820 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1821 return -EFAULT;
1822 sigset_from_compat(&ksigmask, &ss32);
1823
1824 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1825 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1826 }
1827
1828 do {
1829 if (tsp) {
1830 if ((unsigned long)ts.tv_sec < MAX_SELECT_SECONDS) {
1831 timeout = ROUND_UP(ts.tv_nsec, 1000000000/HZ);
1832 timeout += ts.tv_sec * (unsigned long)HZ;
1833 ts.tv_sec = 0;
1834 ts.tv_nsec = 0;
1835 } else {
1836 ts.tv_sec -= MAX_SELECT_SECONDS;
1837 timeout = MAX_SELECT_SECONDS * HZ;
1838 }
1839 }
1840
1841 ret = compat_core_sys_select(n, inp, outp, exp, &timeout);
1842
1843 } while (!ret && !timeout && tsp && (ts.tv_sec || ts.tv_nsec));
1844
Heiko Carstens87c2b7c2006-11-02 22:06:58 -08001845 if (tsp) {
Andrew Morton643a6542006-02-11 17:55:52 -08001846 struct compat_timespec rts;
1847
Heiko Carstens87c2b7c2006-11-02 22:06:58 -08001848 if (current->personality & STICKY_TIMEOUTS)
1849 goto sticky;
1850
Andrew Morton643a6542006-02-11 17:55:52 -08001851 rts.tv_sec = timeout / HZ;
1852 rts.tv_nsec = (timeout % HZ) * (NSEC_PER_SEC/HZ);
1853 if (rts.tv_nsec >= NSEC_PER_SEC) {
1854 rts.tv_sec++;
1855 rts.tv_nsec -= NSEC_PER_SEC;
David Woodhouse9f729492006-01-18 17:44:05 -08001856 }
Andrew Morton74910e62006-02-17 13:52:58 -08001857 if (compat_timespec_compare(&rts, &ts) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -08001858 rts = ts;
Heiko Carstens87c2b7c2006-11-02 22:06:58 -08001859 if (copy_to_user(tsp, &rts, sizeof(rts))) {
1860sticky:
1861 /*
1862 * If an application puts its timeval in read-only
1863 * memory, we don't want the Linux-specific update to
1864 * the timeval to cause a fault after the select has
1865 * completed successfully. However, because we're not
1866 * updating the timeval, we can't restart the system
1867 * call.
1868 */
1869 if (ret == -ERESTARTNOHAND)
1870 ret = -EINTR;
1871 }
David Woodhouse9f729492006-01-18 17:44:05 -08001872 }
1873
1874 if (ret == -ERESTARTNOHAND) {
1875 /*
1876 * Don't restore the signal mask yet. Let do_signal() deliver
1877 * the signal on the way back to userspace, before the signal
1878 * mask is restored.
1879 */
1880 if (sigmask) {
1881 memcpy(&current->saved_sigmask, &sigsaved,
1882 sizeof(sigsaved));
1883 set_thread_flag(TIF_RESTORE_SIGMASK);
1884 }
1885 } else if (sigmask)
1886 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1887
1888 return ret;
1889}
1890
1891asmlinkage long compat_sys_pselect6(int n, compat_ulong_t __user *inp,
1892 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1893 struct compat_timespec __user *tsp, void __user *sig)
1894{
1895 compat_size_t sigsetsize = 0;
1896 compat_uptr_t up = 0;
1897
1898 if (sig) {
1899 if (!access_ok(VERIFY_READ, sig,
1900 sizeof(compat_uptr_t)+sizeof(compat_size_t)) ||
1901 __get_user(up, (compat_uptr_t __user *)sig) ||
1902 __get_user(sigsetsize,
1903 (compat_size_t __user *)(sig+sizeof(up))))
1904 return -EFAULT;
1905 }
1906 return compat_sys_pselect7(n, inp, outp, exp, tsp, compat_ptr(up),
1907 sigsetsize);
1908}
1909
1910asmlinkage long compat_sys_ppoll(struct pollfd __user *ufds,
1911 unsigned int nfds, struct compat_timespec __user *tsp,
1912 const compat_sigset_t __user *sigmask, compat_size_t sigsetsize)
1913{
1914 compat_sigset_t ss32;
1915 sigset_t ksigmask, sigsaved;
1916 struct compat_timespec ts;
1917 s64 timeout = -1;
1918 int ret;
1919
1920 if (tsp) {
1921 if (copy_from_user(&ts, tsp, sizeof(ts)))
1922 return -EFAULT;
1923
1924 /* We assume that ts.tv_sec is always lower than
1925 the number of seconds that can be expressed in
1926 an s64. Otherwise the compiler bitches at us */
David S. Miller7e732bf2006-01-19 16:40:42 -08001927 timeout = ROUND_UP(ts.tv_nsec, 1000000000/HZ);
David Woodhouse9f729492006-01-18 17:44:05 -08001928 timeout += ts.tv_sec * HZ;
1929 }
1930
1931 if (sigmask) {
Alexey Dobriyan3835a9b2006-05-15 09:44:27 -07001932 if (sigsetsize != sizeof(compat_sigset_t))
David Woodhouse9f729492006-01-18 17:44:05 -08001933 return -EINVAL;
1934 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1935 return -EFAULT;
1936 sigset_from_compat(&ksigmask, &ss32);
1937
1938 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1939 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1940 }
1941
1942 ret = do_sys_poll(ufds, nfds, &timeout);
1943
1944 /* We can restart this syscall, usually */
1945 if (ret == -EINTR) {
1946 /*
1947 * Don't restore the signal mask yet. Let do_signal() deliver
1948 * the signal on the way back to userspace, before the signal
1949 * mask is restored.
1950 */
1951 if (sigmask) {
1952 memcpy(&current->saved_sigmask, &sigsaved,
1953 sizeof(sigsaved));
1954 set_thread_flag(TIF_RESTORE_SIGMASK);
1955 }
1956 ret = -ERESTARTNOHAND;
1957 } else if (sigmask)
1958 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1959
1960 if (tsp && timeout >= 0) {
Andrew Morton643a6542006-02-11 17:55:52 -08001961 struct compat_timespec rts;
1962
David Woodhouse9f729492006-01-18 17:44:05 -08001963 if (current->personality & STICKY_TIMEOUTS)
1964 goto sticky;
1965 /* Yes, we know it's actually an s64, but it's also positive. */
Andrew Morton643a6542006-02-11 17:55:52 -08001966 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
1967 1000;
1968 rts.tv_sec = timeout;
Andrew Morton74910e62006-02-17 13:52:58 -08001969 if (compat_timespec_compare(&rts, &ts) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -08001970 rts = ts;
1971 if (copy_to_user(tsp, &rts, sizeof(rts))) {
David Woodhouse9f729492006-01-18 17:44:05 -08001972sticky:
1973 /*
1974 * If an application puts its timeval in read-only
1975 * memory, we don't want the Linux-specific update to
1976 * the timeval to cause a fault after the select has
1977 * completed successfully. However, because we're not
1978 * updating the timeval, we can't restart the system
1979 * call.
1980 */
1981 if (ret == -ERESTARTNOHAND && timeout >= 0)
1982 ret = -EINTR;
1983 }
1984 }
1985
1986 return ret;
1987}
1988#endif /* TIF_RESTORE_SIGMASK */
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990#if defined(CONFIG_NFSD) || defined(CONFIG_NFSD_MODULE)
1991/* Stuff for NFS server syscalls... */
1992struct compat_nfsctl_svc {
1993 u16 svc32_port;
1994 s32 svc32_nthreads;
1995};
1996
1997struct compat_nfsctl_client {
1998 s8 cl32_ident[NFSCLNT_IDMAX+1];
1999 s32 cl32_naddr;
2000 struct in_addr cl32_addrlist[NFSCLNT_ADDRMAX];
2001 s32 cl32_fhkeytype;
2002 s32 cl32_fhkeylen;
2003 u8 cl32_fhkey[NFSCLNT_KEYMAX];
2004};
2005
2006struct compat_nfsctl_export {
2007 char ex32_client[NFSCLNT_IDMAX+1];
2008 char ex32_path[NFS_MAXPATHLEN+1];
2009 compat_dev_t ex32_dev;
2010 compat_ino_t ex32_ino;
2011 compat_int_t ex32_flags;
Stephen Rothwell202e5972005-09-06 15:16:40 -07002012 __compat_uid_t ex32_anon_uid;
2013 __compat_gid_t ex32_anon_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014};
2015
2016struct compat_nfsctl_fdparm {
2017 struct sockaddr gd32_addr;
2018 s8 gd32_path[NFS_MAXPATHLEN+1];
2019 compat_int_t gd32_version;
2020};
2021
2022struct compat_nfsctl_fsparm {
2023 struct sockaddr gd32_addr;
2024 s8 gd32_path[NFS_MAXPATHLEN+1];
2025 compat_int_t gd32_maxlen;
2026};
2027
2028struct compat_nfsctl_arg {
2029 compat_int_t ca32_version; /* safeguard */
2030 union {
2031 struct compat_nfsctl_svc u32_svc;
2032 struct compat_nfsctl_client u32_client;
2033 struct compat_nfsctl_export u32_export;
2034 struct compat_nfsctl_fdparm u32_getfd;
2035 struct compat_nfsctl_fsparm u32_getfs;
2036 } u;
2037#define ca32_svc u.u32_svc
2038#define ca32_client u.u32_client
2039#define ca32_export u.u32_export
2040#define ca32_getfd u.u32_getfd
2041#define ca32_getfs u.u32_getfs
2042};
2043
2044union compat_nfsctl_res {
2045 __u8 cr32_getfh[NFS_FHSIZE];
2046 struct knfsd_fh cr32_getfs;
2047};
2048
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002049static int compat_nfs_svc_trans(struct nfsctl_arg *karg,
2050 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002052 if (!access_ok(VERIFY_READ, &arg->ca32_svc, sizeof(arg->ca32_svc)) ||
2053 get_user(karg->ca_version, &arg->ca32_version) ||
2054 __get_user(karg->ca_svc.svc_port, &arg->ca32_svc.svc32_port) ||
2055 __get_user(karg->ca_svc.svc_nthreads,
2056 &arg->ca32_svc.svc32_nthreads))
2057 return -EFAULT;
2058 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002061static int compat_nfs_clnt_trans(struct nfsctl_arg *karg,
2062 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002064 if (!access_ok(VERIFY_READ, &arg->ca32_client,
2065 sizeof(arg->ca32_client)) ||
2066 get_user(karg->ca_version, &arg->ca32_version) ||
2067 __copy_from_user(&karg->ca_client.cl_ident[0],
2068 &arg->ca32_client.cl32_ident[0],
2069 NFSCLNT_IDMAX) ||
2070 __get_user(karg->ca_client.cl_naddr,
2071 &arg->ca32_client.cl32_naddr) ||
2072 __copy_from_user(&karg->ca_client.cl_addrlist[0],
2073 &arg->ca32_client.cl32_addrlist[0],
2074 (sizeof(struct in_addr) * NFSCLNT_ADDRMAX)) ||
2075 __get_user(karg->ca_client.cl_fhkeytype,
2076 &arg->ca32_client.cl32_fhkeytype) ||
2077 __get_user(karg->ca_client.cl_fhkeylen,
2078 &arg->ca32_client.cl32_fhkeylen) ||
2079 __copy_from_user(&karg->ca_client.cl_fhkey[0],
2080 &arg->ca32_client.cl32_fhkey[0],
2081 NFSCLNT_KEYMAX))
2082 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002084 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085}
2086
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002087static int compat_nfs_exp_trans(struct nfsctl_arg *karg,
2088 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002090 if (!access_ok(VERIFY_READ, &arg->ca32_export,
2091 sizeof(arg->ca32_export)) ||
2092 get_user(karg->ca_version, &arg->ca32_version) ||
2093 __copy_from_user(&karg->ca_export.ex_client[0],
2094 &arg->ca32_export.ex32_client[0],
2095 NFSCLNT_IDMAX) ||
2096 __copy_from_user(&karg->ca_export.ex_path[0],
2097 &arg->ca32_export.ex32_path[0],
2098 NFS_MAXPATHLEN) ||
2099 __get_user(karg->ca_export.ex_dev,
2100 &arg->ca32_export.ex32_dev) ||
2101 __get_user(karg->ca_export.ex_ino,
2102 &arg->ca32_export.ex32_ino) ||
2103 __get_user(karg->ca_export.ex_flags,
2104 &arg->ca32_export.ex32_flags) ||
2105 __get_user(karg->ca_export.ex_anon_uid,
2106 &arg->ca32_export.ex32_anon_uid) ||
2107 __get_user(karg->ca_export.ex_anon_gid,
2108 &arg->ca32_export.ex32_anon_gid))
2109 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 SET_UID(karg->ca_export.ex_anon_uid, karg->ca_export.ex_anon_uid);
2111 SET_GID(karg->ca_export.ex_anon_gid, karg->ca_export.ex_anon_gid);
2112
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002113 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002116static int compat_nfs_getfd_trans(struct nfsctl_arg *karg,
2117 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002119 if (!access_ok(VERIFY_READ, &arg->ca32_getfd,
2120 sizeof(arg->ca32_getfd)) ||
2121 get_user(karg->ca_version, &arg->ca32_version) ||
2122 __copy_from_user(&karg->ca_getfd.gd_addr,
2123 &arg->ca32_getfd.gd32_addr,
2124 (sizeof(struct sockaddr))) ||
2125 __copy_from_user(&karg->ca_getfd.gd_path,
2126 &arg->ca32_getfd.gd32_path,
2127 (NFS_MAXPATHLEN+1)) ||
2128 __get_user(karg->ca_getfd.gd_version,
2129 &arg->ca32_getfd.gd32_version))
2130 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133}
2134
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002135static int compat_nfs_getfs_trans(struct nfsctl_arg *karg,
2136 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002138 if (!access_ok(VERIFY_READ,&arg->ca32_getfs,sizeof(arg->ca32_getfs)) ||
2139 get_user(karg->ca_version, &arg->ca32_version) ||
2140 __copy_from_user(&karg->ca_getfs.gd_addr,
2141 &arg->ca32_getfs.gd32_addr,
2142 (sizeof(struct sockaddr))) ||
2143 __copy_from_user(&karg->ca_getfs.gd_path,
2144 &arg->ca32_getfs.gd32_path,
2145 (NFS_MAXPATHLEN+1)) ||
2146 __get_user(karg->ca_getfs.gd_maxlen,
2147 &arg->ca32_getfs.gd32_maxlen))
2148 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002150 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
2152
2153/* This really doesn't need translations, we are only passing
2154 * back a union which contains opaque nfs file handle data.
2155 */
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002156static int compat_nfs_getfh_res_trans(union nfsctl_res *kres,
2157 union compat_nfsctl_res __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
2159 int err;
2160
2161 err = copy_to_user(res, kres, sizeof(*res));
2162
2163 return (err) ? -EFAULT : 0;
2164}
2165
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002166asmlinkage long compat_sys_nfsservctl(int cmd,
2167 struct compat_nfsctl_arg __user *arg,
2168 union compat_nfsctl_res __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
2170 struct nfsctl_arg *karg;
2171 union nfsctl_res *kres;
2172 mm_segment_t oldfs;
2173 int err;
2174
2175 karg = kmalloc(sizeof(*karg), GFP_USER);
2176 kres = kmalloc(sizeof(*kres), GFP_USER);
2177 if(!karg || !kres) {
2178 err = -ENOMEM;
2179 goto done;
2180 }
2181
2182 switch(cmd) {
2183 case NFSCTL_SVC:
2184 err = compat_nfs_svc_trans(karg, arg);
2185 break;
2186
2187 case NFSCTL_ADDCLIENT:
2188 err = compat_nfs_clnt_trans(karg, arg);
2189 break;
2190
2191 case NFSCTL_DELCLIENT:
2192 err = compat_nfs_clnt_trans(karg, arg);
2193 break;
2194
2195 case NFSCTL_EXPORT:
2196 case NFSCTL_UNEXPORT:
2197 err = compat_nfs_exp_trans(karg, arg);
2198 break;
2199
2200 case NFSCTL_GETFD:
2201 err = compat_nfs_getfd_trans(karg, arg);
2202 break;
2203
2204 case NFSCTL_GETFS:
2205 err = compat_nfs_getfs_trans(karg, arg);
2206 break;
2207
2208 default:
2209 err = -EINVAL;
Peter Staubach57070d02006-03-25 03:08:04 -08002210 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 }
2212
Peter Staubach57070d02006-03-25 03:08:04 -08002213 if (err)
2214 goto done;
2215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 oldfs = get_fs();
2217 set_fs(KERNEL_DS);
2218 /* The __user pointer casts are valid because of the set_fs() */
2219 err = sys_nfsservctl(cmd, (void __user *) karg, (void __user *) kres);
2220 set_fs(oldfs);
2221
2222 if (err)
2223 goto done;
2224
2225 if((cmd == NFSCTL_GETFD) ||
2226 (cmd == NFSCTL_GETFS))
2227 err = compat_nfs_getfh_res_trans(kres, res);
2228
2229done:
2230 kfree(karg);
2231 kfree(kres);
2232 return err;
2233}
2234#else /* !NFSD */
2235long asmlinkage compat_sys_nfsservctl(int cmd, void *notused, void *notused2)
2236{
2237 return sys_ni_syscall();
2238}
2239#endif
Davide Libenzif6dfb4f2007-03-07 20:41:21 -08002240
2241#ifdef CONFIG_EPOLL
2242
2243#ifdef CONFIG_HAS_COMPAT_EPOLL_EVENT
2244asmlinkage long compat_sys_epoll_ctl(int epfd, int op, int fd,
2245 struct compat_epoll_event __user *event)
2246{
2247 long err = 0;
2248 struct compat_epoll_event user;
2249 struct epoll_event __user *kernel = NULL;
2250
2251 if (event) {
2252 if (copy_from_user(&user, event, sizeof(user)))
2253 return -EFAULT;
2254 kernel = compat_alloc_user_space(sizeof(struct epoll_event));
2255 err |= __put_user(user.events, &kernel->events);
2256 err |= __put_user(user.data, &kernel->data);
2257 }
2258
2259 return err ? err : sys_epoll_ctl(epfd, op, fd, kernel);
2260}
2261
2262
2263asmlinkage long compat_sys_epoll_wait(int epfd,
2264 struct compat_epoll_event __user *events,
2265 int maxevents, int timeout)
2266{
2267 long i, ret, err = 0;
2268 struct epoll_event __user *kbuf;
2269 struct epoll_event ev;
2270
2271 if ((maxevents <= 0) ||
2272 (maxevents > (INT_MAX / sizeof(struct epoll_event))))
2273 return -EINVAL;
2274 kbuf = compat_alloc_user_space(sizeof(struct epoll_event) * maxevents);
2275 ret = sys_epoll_wait(epfd, kbuf, maxevents, timeout);
2276 for (i = 0; i < ret; i++) {
2277 err |= __get_user(ev.events, &kbuf[i].events);
2278 err |= __get_user(ev.data, &kbuf[i].data);
2279 err |= __put_user(ev.events, &events->events);
2280 err |= __put_user_unaligned(ev.data, &events->data);
2281 events++;
2282 }
2283
2284 return err ? -EFAULT: ret;
2285}
2286#endif /* CONFIG_HAS_COMPAT_EPOLL_EVENT */
2287
2288#ifdef TIF_RESTORE_SIGMASK
2289asmlinkage long compat_sys_epoll_pwait(int epfd,
2290 struct compat_epoll_event __user *events,
2291 int maxevents, int timeout,
2292 const compat_sigset_t __user *sigmask,
2293 compat_size_t sigsetsize)
2294{
2295 long err;
2296 compat_sigset_t csigmask;
2297 sigset_t ksigmask, sigsaved;
2298
2299 /*
2300 * If the caller wants a certain signal mask to be set during the wait,
2301 * we apply it here.
2302 */
2303 if (sigmask) {
2304 if (sigsetsize != sizeof(compat_sigset_t))
2305 return -EINVAL;
2306 if (copy_from_user(&csigmask, sigmask, sizeof(csigmask)))
2307 return -EFAULT;
2308 sigset_from_compat(&ksigmask, &csigmask);
2309 sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
2310 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
2311 }
2312
2313#ifdef CONFIG_HAS_COMPAT_EPOLL_EVENT
2314 err = compat_sys_epoll_wait(epfd, events, maxevents, timeout);
2315#else
2316 err = sys_epoll_wait(epfd, events, maxevents, timeout);
2317#endif
2318
2319 /*
2320 * If we changed the signal mask, we need to restore the original one.
2321 * In case we've got a signal while waiting, we do not restore the
2322 * signal mask yet, and we allow do_signal() to deliver the signal on
2323 * the way back to userspace, before the signal mask is restored.
2324 */
2325 if (sigmask) {
2326 if (err == -EINTR) {
2327 memcpy(&current->saved_sigmask, &sigsaved,
2328 sizeof(sigsaved));
2329 set_thread_flag(TIF_RESTORE_SIGMASK);
2330 } else
2331 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2332 }
2333
2334 return err;
2335}
2336#endif /* TIF_RESTORE_SIGMASK */
2337
2338#endif /* CONFIG_EPOLL */