blob: a32e2ae737fb0d0acc8d8537b98c27aa24ba3467 [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
Milind Arun Choudhary022a1692007-05-08 00:29:02 -070018#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/linkage.h>
20#include <linux/compat.h>
21#include <linux/errno.h>
22#include <linux/time.h>
23#include <linux/fs.h>
24#include <linux/fcntl.h>
25#include <linux/namei.h>
26#include <linux/file.h>
27#include <linux/vfs.h>
28#include <linux/ioctl32.h>
29#include <linux/ioctl.h>
30#include <linux/init.h>
31#include <linux/sockios.h> /* for SIOCDEVPRIVATE */
32#include <linux/smb.h>
33#include <linux/smb_mount.h>
34#include <linux/ncp_mount.h>
David Howells9a9947b2005-04-18 10:54:51 -070035#include <linux/nfs4_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/smp_lock.h>
37#include <linux/syscalls.h>
38#include <linux/ctype.h>
39#include <linux/module.h>
40#include <linux/dirent.h>
Robert Love0eeca282005-07-12 17:06:03 -040041#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/highuid.h>
43#include <linux/sunrpc/svc.h>
44#include <linux/nfsd/nfsd.h>
45#include <linux/nfsd/syscall.h>
46#include <linux/personality.h>
47#include <linux/rwsem.h>
Jay Lan8f0ab512006-09-30 23:28:59 -070048#include <linux/tsacct_kern.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040049#include <linux/highmem.h>
Al Virobd01f842006-10-19 17:23:57 -040050#include <linux/poll.h>
David S. Miller4a805e82005-09-14 21:40:00 -070051#include <linux/mm.h>
Davide Libenzif6dfb4f2007-03-07 20:41:21 -080052#include <linux/eventpoll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#include <net/sock.h> /* siocdevprivate_ioctl */
55
56#include <asm/uaccess.h>
57#include <asm/mmu_context.h>
58#include <asm/ioctls.h>
David Howells07f3f052006-09-30 20:52:18 +020059#include "internal.h"
David Woodhouse9f729492006-01-18 17:44:05 -080060
Andi Kleenbebfa102006-06-26 13:56:52 +020061int compat_log = 1;
62
63int compat_printk(const char *fmt, ...)
64{
65 va_list ap;
66 int ret;
67 if (!compat_log)
68 return 0;
69 va_start(ap, fmt);
70 ret = vprintk(fmt, ap);
71 va_end(ap);
72 return ret;
73}
74
Badari Pulavartyee0b3e62006-09-30 23:28:47 -070075#include "read_write.h"
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/*
78 * Not all architectures have sys_utime, so implement this in terms
79 * of sys_utimes.
80 */
81asmlinkage long compat_sys_utime(char __user *filename, struct compat_utimbuf __user *t)
82{
83 struct timeval tv[2];
84
85 if (t) {
86 if (get_user(tv[0].tv_sec, &t->actime) ||
87 get_user(tv[1].tv_sec, &t->modtime))
88 return -EFAULT;
89 tv[0].tv_usec = 0;
90 tv[1].tv_usec = 0;
91 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -080092 return do_utimes(AT_FDCWD, filename, t ? tv : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +110095asmlinkage long compat_sys_futimesat(unsigned int dfd, char __user *filename, struct compat_timeval __user *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 struct timeval tv[2];
98
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +110099 if (t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
101 get_user(tv[0].tv_usec, &t[0].tv_usec) ||
102 get_user(tv[1].tv_sec, &t[1].tv_sec) ||
103 get_user(tv[1].tv_usec, &t[1].tv_usec))
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +1100104 return -EFAULT;
105 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800106 return do_utimes(dfd, filename, t ? tv : NULL);
107}
108
109asmlinkage long compat_sys_utimes(char __user *filename, struct compat_timeval __user *t)
110{
111 return compat_sys_futimesat(AT_FDCWD, filename, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114asmlinkage long compat_sys_newstat(char __user * filename,
115 struct compat_stat __user *statbuf)
116{
117 struct kstat stat;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800118 int error = vfs_stat_fd(AT_FDCWD, filename, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 if (!error)
121 error = cp_compat_stat(&stat, statbuf);
122 return error;
123}
124
125asmlinkage long compat_sys_newlstat(char __user * filename,
126 struct compat_stat __user *statbuf)
127{
128 struct kstat stat;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800129 int error = vfs_lstat_fd(AT_FDCWD, filename, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 if (!error)
132 error = cp_compat_stat(&stat, statbuf);
133 return error;
134}
135
Kyle McMartin82d821d2006-03-24 03:18:20 -0800136#ifndef __ARCH_WANT_STAT64
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +1100137asmlinkage long compat_sys_newfstatat(unsigned int dfd, char __user *filename,
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800138 struct compat_stat __user *statbuf, int flag)
139{
140 struct kstat stat;
141 int error = -EINVAL;
142
143 if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
144 goto out;
145
146 if (flag & AT_SYMLINK_NOFOLLOW)
147 error = vfs_lstat_fd(dfd, filename, &stat);
148 else
149 error = vfs_stat_fd(dfd, filename, &stat);
150
151 if (!error)
152 error = cp_compat_stat(&stat, statbuf);
153
154out:
155 return error;
156}
Kyle McMartin82d821d2006-03-24 03:18:20 -0800157#endif
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159asmlinkage long compat_sys_newfstat(unsigned int fd,
160 struct compat_stat __user * statbuf)
161{
162 struct kstat stat;
163 int error = vfs_fstat(fd, &stat);
164
165 if (!error)
166 error = cp_compat_stat(&stat, statbuf);
167 return error;
168}
169
170static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
171{
172
173 if (sizeof ubuf->f_blocks == 4) {
174 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) &
175 0xffffffff00000000ULL)
176 return -EOVERFLOW;
177 /* f_files and f_ffree may be -1; it's okay
178 * to stuff that into 32 bits */
179 if (kbuf->f_files != 0xffffffffffffffffULL
180 && (kbuf->f_files & 0xffffffff00000000ULL))
181 return -EOVERFLOW;
182 if (kbuf->f_ffree != 0xffffffffffffffffULL
183 && (kbuf->f_ffree & 0xffffffff00000000ULL))
184 return -EOVERFLOW;
185 }
186 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
187 __put_user(kbuf->f_type, &ubuf->f_type) ||
188 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
189 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
190 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
191 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
192 __put_user(kbuf->f_files, &ubuf->f_files) ||
193 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
194 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
195 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
196 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
197 __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
198 __put_user(0, &ubuf->f_spare[0]) ||
199 __put_user(0, &ubuf->f_spare[1]) ||
200 __put_user(0, &ubuf->f_spare[2]) ||
201 __put_user(0, &ubuf->f_spare[3]) ||
202 __put_user(0, &ubuf->f_spare[4]))
203 return -EFAULT;
204 return 0;
205}
206
207/*
208 * The following statfs calls are copies of code from fs/open.c and
209 * should be checked against those from time to time
210 */
211asmlinkage long compat_sys_statfs(const char __user *path, struct compat_statfs __user *buf)
212{
213 struct nameidata nd;
214 int error;
215
216 error = user_path_walk(path, &nd);
217 if (!error) {
218 struct kstatfs tmp;
David Howells726c3342006-06-23 02:02:58 -0700219 error = vfs_statfs(nd.dentry, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800220 if (!error)
221 error = put_compat_statfs(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 path_release(&nd);
223 }
224 return error;
225}
226
227asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf)
228{
229 struct file * file;
230 struct kstatfs tmp;
231 int error;
232
233 error = -EBADF;
234 file = fget(fd);
235 if (!file)
236 goto out;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800237 error = vfs_statfs(file->f_path.dentry, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800238 if (!error)
239 error = put_compat_statfs(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 fput(file);
241out:
242 return error;
243}
244
245static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
246{
247 if (sizeof ubuf->f_blocks == 4) {
248 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) &
249 0xffffffff00000000ULL)
250 return -EOVERFLOW;
251 /* f_files and f_ffree may be -1; it's okay
252 * to stuff that into 32 bits */
253 if (kbuf->f_files != 0xffffffffffffffffULL
254 && (kbuf->f_files & 0xffffffff00000000ULL))
255 return -EOVERFLOW;
256 if (kbuf->f_ffree != 0xffffffffffffffffULL
257 && (kbuf->f_ffree & 0xffffffff00000000ULL))
258 return -EOVERFLOW;
259 }
260 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
261 __put_user(kbuf->f_type, &ubuf->f_type) ||
262 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
263 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
264 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
265 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
266 __put_user(kbuf->f_files, &ubuf->f_files) ||
267 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
268 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
269 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
270 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
271 __put_user(kbuf->f_frsize, &ubuf->f_frsize))
272 return -EFAULT;
273 return 0;
274}
275
276asmlinkage long compat_sys_statfs64(const char __user *path, compat_size_t sz, struct compat_statfs64 __user *buf)
277{
278 struct nameidata nd;
279 int error;
280
281 if (sz != sizeof(*buf))
282 return -EINVAL;
283
284 error = user_path_walk(path, &nd);
285 if (!error) {
286 struct kstatfs tmp;
David Howells726c3342006-06-23 02:02:58 -0700287 error = vfs_statfs(nd.dentry, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800288 if (!error)
289 error = put_compat_statfs64(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 path_release(&nd);
291 }
292 return error;
293}
294
295asmlinkage long compat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf)
296{
297 struct file * file;
298 struct kstatfs tmp;
299 int error;
300
301 if (sz != sizeof(*buf))
302 return -EINVAL;
303
304 error = -EBADF;
305 file = fget(fd);
306 if (!file)
307 goto out;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800308 error = vfs_statfs(file->f_path.dentry, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800309 if (!error)
310 error = put_compat_statfs64(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 fput(file);
312out:
313 return error;
314}
315
316/* ioctl32 stuff, used by sparc64, parisc, s390x, ppc64, x86_64, MIPS */
317
318#define IOCTL_HASHSIZE 256
319static struct ioctl_trans *ioctl32_hash_table[IOCTL_HASHSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321static inline unsigned long ioctl32_hash(unsigned long cmd)
322{
323 return (((cmd >> 6) ^ (cmd >> 4) ^ cmd)) % IOCTL_HASHSIZE;
324}
325
326static void ioctl32_insert_translation(struct ioctl_trans *trans)
327{
328 unsigned long hash;
329 struct ioctl_trans *t;
330
331 hash = ioctl32_hash (trans->cmd);
332 if (!ioctl32_hash_table[hash])
333 ioctl32_hash_table[hash] = trans;
334 else {
335 t = ioctl32_hash_table[hash];
336 while (t->next)
337 t = t->next;
338 trans->next = NULL;
339 t->next = trans;
340 }
341}
342
343static int __init init_sys32_ioctl(void)
344{
345 int i;
346
347 for (i = 0; i < ioctl_table_size; i++) {
348 if (ioctl_start[i].next != 0) {
349 printk("ioctl translation %d bad\n",i);
350 return -1;
351 }
352
353 ioctl32_insert_translation(&ioctl_start[i]);
354 }
355 return 0;
356}
357
358__initcall(init_sys32_ioctl);
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360static void compat_ioctl_error(struct file *filp, unsigned int fd,
361 unsigned int cmd, unsigned long arg)
362{
363 char buf[10];
364 char *fn = "?";
365 char *path;
366
367 /* find the name of the device. */
368 path = (char *)__get_free_page(GFP_KERNEL);
369 if (path) {
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800370 fn = d_path(filp->f_path.dentry, filp->f_path.mnt, path, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (IS_ERR(fn))
372 fn = "?";
373 }
374
Andi Kleena1060092007-05-02 19:27:21 +0200375 sprintf(buf,"'%c'", (cmd>>_IOC_TYPESHIFT) & _IOC_TYPEMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (!isprint(buf[1]))
377 sprintf(buf, "%02x", buf[1]);
Andi Kleenbebfa102006-06-26 13:56:52 +0200378 compat_printk("ioctl32(%s:%d): Unknown cmd fd(%d) "
Andi Kleena1060092007-05-02 19:27:21 +0200379 "cmd(%08x){t:%s;sz:%u} arg(%08x) on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 current->comm, current->pid,
381 (int)fd, (unsigned int)cmd, buf,
Andi Kleena1060092007-05-02 19:27:21 +0200382 (cmd >> _IOC_SIZESHIFT) & _IOC_SIZEMASK,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 (unsigned int)arg, fn);
384
385 if (path)
386 free_page((unsigned long)path);
387}
388
389asmlinkage long compat_sys_ioctl(unsigned int fd, unsigned int cmd,
390 unsigned long arg)
391{
392 struct file *filp;
393 int error = -EBADF;
394 struct ioctl_trans *t;
395 int fput_needed;
396
397 filp = fget_light(fd, &fput_needed);
398 if (!filp)
399 goto out;
400
401 /* RED-PEN how should LSM module know it's handling 32bit? */
402 error = security_file_ioctl(filp, cmd, arg);
403 if (error)
404 goto out_fput;
405
406 /*
407 * To allow the compat_ioctl handlers to be self contained
408 * we need to check the common ioctls here first.
409 * Just handle them with the standard handlers below.
410 */
411 switch (cmd) {
412 case FIOCLEX:
413 case FIONCLEX:
414 case FIONBIO:
415 case FIOASYNC:
416 case FIOQSIZE:
417 break;
418
419 case FIBMAP:
420 case FIGETBSZ:
421 case FIONREAD:
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800422 if (S_ISREG(filp->f_path.dentry->d_inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 break;
424 /*FALL THROUGH*/
425
426 default:
427 if (filp->f_op && filp->f_op->compat_ioctl) {
428 error = filp->f_op->compat_ioctl(filp, cmd, arg);
429 if (error != -ENOIOCTLCMD)
430 goto out_fput;
431 }
432
433 if (!filp->f_op ||
434 (!filp->f_op->ioctl && !filp->f_op->unlocked_ioctl))
435 goto do_ioctl;
436 break;
437 }
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 for (t = ioctl32_hash_table[ioctl32_hash(cmd)]; t; t = t->next) {
440 if (t->cmd == cmd)
441 goto found_handler;
442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800444 if (S_ISSOCK(filp->f_path.dentry->d_inode->i_mode) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
446 error = siocdevprivate_ioctl(fd, cmd, arg);
447 } else {
448 static int count;
449
450 if (++count <= 50)
451 compat_ioctl_error(filp, fd, cmd, arg);
452 error = -EINVAL;
453 }
454
455 goto out_fput;
456
457 found_handler:
458 if (t->handler) {
459 lock_kernel();
460 error = t->handler(fd, cmd, arg, filp);
461 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 goto out_fput;
463 }
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 do_ioctl:
466 error = vfs_ioctl(filp, fd, cmd, arg);
467 out_fput:
468 fput_light(filp, fput_needed);
469 out:
470 return error;
471}
472
473static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
474{
475 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
476 __get_user(kfl->l_type, &ufl->l_type) ||
477 __get_user(kfl->l_whence, &ufl->l_whence) ||
478 __get_user(kfl->l_start, &ufl->l_start) ||
479 __get_user(kfl->l_len, &ufl->l_len) ||
480 __get_user(kfl->l_pid, &ufl->l_pid))
481 return -EFAULT;
482 return 0;
483}
484
485static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
486{
487 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
488 __put_user(kfl->l_type, &ufl->l_type) ||
489 __put_user(kfl->l_whence, &ufl->l_whence) ||
490 __put_user(kfl->l_start, &ufl->l_start) ||
491 __put_user(kfl->l_len, &ufl->l_len) ||
492 __put_user(kfl->l_pid, &ufl->l_pid))
493 return -EFAULT;
494 return 0;
495}
496
497#ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
498static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
499{
500 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
501 __get_user(kfl->l_type, &ufl->l_type) ||
502 __get_user(kfl->l_whence, &ufl->l_whence) ||
503 __get_user(kfl->l_start, &ufl->l_start) ||
504 __get_user(kfl->l_len, &ufl->l_len) ||
505 __get_user(kfl->l_pid, &ufl->l_pid))
506 return -EFAULT;
507 return 0;
508}
509#endif
510
511#ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
512static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
513{
514 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
515 __put_user(kfl->l_type, &ufl->l_type) ||
516 __put_user(kfl->l_whence, &ufl->l_whence) ||
517 __put_user(kfl->l_start, &ufl->l_start) ||
518 __put_user(kfl->l_len, &ufl->l_len) ||
519 __put_user(kfl->l_pid, &ufl->l_pid))
520 return -EFAULT;
521 return 0;
522}
523#endif
524
525asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd,
526 unsigned long arg)
527{
528 mm_segment_t old_fs;
529 struct flock f;
530 long ret;
531
532 switch (cmd) {
533 case F_GETLK:
534 case F_SETLK:
535 case F_SETLKW:
536 ret = get_compat_flock(&f, compat_ptr(arg));
537 if (ret != 0)
538 break;
539 old_fs = get_fs();
540 set_fs(KERNEL_DS);
541 ret = sys_fcntl(fd, cmd, (unsigned long)&f);
542 set_fs(old_fs);
543 if (cmd == F_GETLK && ret == 0) {
NeilBrown2520f142006-01-08 01:02:40 -0800544 /* GETLK was successfule and we need to return the data...
545 * but it needs to fit in the compat structure.
546 * l_start shouldn't be too big, unless the original
547 * start + end is greater than COMPAT_OFF_T_MAX, in which
548 * case the app was asking for trouble, so we return
549 * -EOVERFLOW in that case.
550 * l_len could be too big, in which case we just truncate it,
551 * and only allow the app to see that part of the conflicting
552 * lock that might make sense to it anyway
553 */
554
555 if (f.l_start > COMPAT_OFF_T_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 ret = -EOVERFLOW;
NeilBrown2520f142006-01-08 01:02:40 -0800557 if (f.l_len > COMPAT_OFF_T_MAX)
558 f.l_len = COMPAT_OFF_T_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (ret == 0)
560 ret = put_compat_flock(&f, compat_ptr(arg));
561 }
562 break;
563
564 case F_GETLK64:
565 case F_SETLK64:
566 case F_SETLKW64:
567 ret = get_compat_flock64(&f, compat_ptr(arg));
568 if (ret != 0)
569 break;
570 old_fs = get_fs();
571 set_fs(KERNEL_DS);
572 ret = sys_fcntl(fd, (cmd == F_GETLK64) ? F_GETLK :
573 ((cmd == F_SETLK64) ? F_SETLK : F_SETLKW),
574 (unsigned long)&f);
575 set_fs(old_fs);
576 if (cmd == F_GETLK64 && ret == 0) {
NeilBrown2520f142006-01-08 01:02:40 -0800577 /* need to return lock information - see above for commentary */
578 if (f.l_start > COMPAT_LOFF_T_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 ret = -EOVERFLOW;
NeilBrown2520f142006-01-08 01:02:40 -0800580 if (f.l_len > COMPAT_LOFF_T_MAX)
581 f.l_len = COMPAT_LOFF_T_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (ret == 0)
583 ret = put_compat_flock64(&f, compat_ptr(arg));
584 }
585 break;
586
587 default:
588 ret = sys_fcntl(fd, cmd, arg);
589 break;
590 }
591 return ret;
592}
593
594asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd,
595 unsigned long arg)
596{
597 if ((cmd == F_GETLK64) || (cmd == F_SETLK64) || (cmd == F_SETLKW64))
598 return -EINVAL;
599 return compat_sys_fcntl64(fd, cmd, arg);
600}
601
602asmlinkage long
603compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p)
604{
605 long ret;
606 aio_context_t ctx64;
607
608 mm_segment_t oldfs = get_fs();
609 if (unlikely(get_user(ctx64, ctx32p)))
610 return -EFAULT;
611
612 set_fs(KERNEL_DS);
613 /* The __user pointer cast is valid because of the set_fs() */
614 ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64);
615 set_fs(oldfs);
616 /* truncating is ok because it's a user address */
617 if (!ret)
618 ret = put_user((u32) ctx64, ctx32p);
619 return ret;
620}
621
622asmlinkage long
623compat_sys_io_getevents(aio_context_t ctx_id,
624 unsigned long min_nr,
625 unsigned long nr,
626 struct io_event __user *events,
627 struct compat_timespec __user *timeout)
628{
629 long ret;
630 struct timespec t;
631 struct timespec __user *ut = NULL;
632
633 ret = -EFAULT;
634 if (unlikely(!access_ok(VERIFY_WRITE, events,
635 nr * sizeof(struct io_event))))
636 goto out;
637 if (timeout) {
638 if (get_compat_timespec(&t, timeout))
639 goto out;
640
641 ut = compat_alloc_user_space(sizeof(*ut));
642 if (copy_to_user(ut, &t, sizeof(t)) )
643 goto out;
644 }
645 ret = sys_io_getevents(ctx_id, min_nr, nr, events, ut);
646out:
647 return ret;
648}
649
650static inline long
651copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64)
652{
653 compat_uptr_t uptr;
654 int i;
655
656 for (i = 0; i < nr; ++i) {
657 if (get_user(uptr, ptr32 + i))
658 return -EFAULT;
659 if (put_user(compat_ptr(uptr), ptr64 + i))
660 return -EFAULT;
661 }
662 return 0;
663}
664
665#define MAX_AIO_SUBMITS (PAGE_SIZE/sizeof(struct iocb *))
666
667asmlinkage long
668compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb)
669{
670 struct iocb __user * __user *iocb64;
671 long ret;
672
673 if (unlikely(nr < 0))
674 return -EINVAL;
675
676 if (nr > MAX_AIO_SUBMITS)
677 nr = MAX_AIO_SUBMITS;
678
679 iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64));
680 ret = copy_iocb(nr, iocb, iocb64);
681 if (!ret)
682 ret = sys_io_submit(ctx_id, nr, iocb64);
683 return ret;
684}
685
686struct compat_ncp_mount_data {
687 compat_int_t version;
688 compat_uint_t ncp_fd;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700689 __compat_uid_t mounted_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 compat_pid_t wdog_pid;
691 unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
692 compat_uint_t time_out;
693 compat_uint_t retry_count;
694 compat_uint_t flags;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700695 __compat_uid_t uid;
696 __compat_gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 compat_mode_t file_mode;
698 compat_mode_t dir_mode;
699};
700
701struct compat_ncp_mount_data_v4 {
702 compat_int_t version;
703 compat_ulong_t flags;
704 compat_ulong_t mounted_uid;
705 compat_long_t wdog_pid;
706 compat_uint_t ncp_fd;
707 compat_uint_t time_out;
708 compat_uint_t retry_count;
709 compat_ulong_t uid;
710 compat_ulong_t gid;
711 compat_ulong_t file_mode;
712 compat_ulong_t dir_mode;
713};
714
715static void *do_ncp_super_data_conv(void *raw_data)
716{
717 int version = *(unsigned int *)raw_data;
718
719 if (version == 3) {
720 struct compat_ncp_mount_data *c_n = raw_data;
721 struct ncp_mount_data *n = raw_data;
722
723 n->dir_mode = c_n->dir_mode;
724 n->file_mode = c_n->file_mode;
725 n->gid = c_n->gid;
726 n->uid = c_n->uid;
727 memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
728 n->wdog_pid = c_n->wdog_pid;
729 n->mounted_uid = c_n->mounted_uid;
730 } else if (version == 4) {
731 struct compat_ncp_mount_data_v4 *c_n = raw_data;
732 struct ncp_mount_data_v4 *n = raw_data;
733
734 n->dir_mode = c_n->dir_mode;
735 n->file_mode = c_n->file_mode;
736 n->gid = c_n->gid;
737 n->uid = c_n->uid;
738 n->retry_count = c_n->retry_count;
739 n->time_out = c_n->time_out;
740 n->ncp_fd = c_n->ncp_fd;
741 n->wdog_pid = c_n->wdog_pid;
742 n->mounted_uid = c_n->mounted_uid;
743 n->flags = c_n->flags;
744 } else if (version != 5) {
745 return NULL;
746 }
747
748 return raw_data;
749}
750
751struct compat_smb_mount_data {
752 compat_int_t version;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700753 __compat_uid_t mounted_uid;
754 __compat_uid_t uid;
755 __compat_gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 compat_mode_t file_mode;
757 compat_mode_t dir_mode;
758};
759
760static void *do_smb_super_data_conv(void *raw_data)
761{
762 struct smb_mount_data *s = raw_data;
763 struct compat_smb_mount_data *c_s = raw_data;
764
765 if (c_s->version != SMB_MOUNT_OLDVERSION)
766 goto out;
767 s->dir_mode = c_s->dir_mode;
768 s->file_mode = c_s->file_mode;
769 s->gid = c_s->gid;
770 s->uid = c_s->uid;
771 s->mounted_uid = c_s->mounted_uid;
772 out:
773 return raw_data;
774}
775
David Howells9a9947b2005-04-18 10:54:51 -0700776struct compat_nfs_string {
777 compat_uint_t len;
David Howells5fc3e622005-04-27 15:39:03 -0700778 compat_uptr_t data;
David Howells9a9947b2005-04-18 10:54:51 -0700779};
780
781static inline void compat_nfs_string(struct nfs_string *dst,
782 struct compat_nfs_string *src)
783{
784 dst->data = compat_ptr(src->data);
785 dst->len = src->len;
786}
787
788struct compat_nfs4_mount_data_v1 {
789 compat_int_t version;
790 compat_int_t flags;
791 compat_int_t rsize;
792 compat_int_t wsize;
793 compat_int_t timeo;
794 compat_int_t retrans;
795 compat_int_t acregmin;
796 compat_int_t acregmax;
797 compat_int_t acdirmin;
798 compat_int_t acdirmax;
799 struct compat_nfs_string client_addr;
800 struct compat_nfs_string mnt_path;
801 struct compat_nfs_string hostname;
802 compat_uint_t host_addrlen;
David Howells5fc3e622005-04-27 15:39:03 -0700803 compat_uptr_t host_addr;
David Howells9a9947b2005-04-18 10:54:51 -0700804 compat_int_t proto;
805 compat_int_t auth_flavourlen;
David Howells5fc3e622005-04-27 15:39:03 -0700806 compat_uptr_t auth_flavours;
David Howells9a9947b2005-04-18 10:54:51 -0700807};
808
809static int do_nfs4_super_data_conv(void *raw_data)
810{
811 int version = *(compat_uint_t *) raw_data;
812
813 if (version == 1) {
814 struct compat_nfs4_mount_data_v1 *raw = raw_data;
815 struct nfs4_mount_data *real = raw_data;
816
817 /* copy the fields backwards */
818 real->auth_flavours = compat_ptr(raw->auth_flavours);
819 real->auth_flavourlen = raw->auth_flavourlen;
820 real->proto = raw->proto;
821 real->host_addr = compat_ptr(raw->host_addr);
822 real->host_addrlen = raw->host_addrlen;
823 compat_nfs_string(&real->hostname, &raw->hostname);
824 compat_nfs_string(&real->mnt_path, &raw->mnt_path);
825 compat_nfs_string(&real->client_addr, &raw->client_addr);
826 real->acdirmax = raw->acdirmax;
827 real->acdirmin = raw->acdirmin;
828 real->acregmax = raw->acregmax;
829 real->acregmin = raw->acregmin;
830 real->retrans = raw->retrans;
831 real->timeo = raw->timeo;
832 real->wsize = raw->wsize;
833 real->rsize = raw->rsize;
834 real->flags = raw->flags;
835 real->version = raw->version;
836 }
837 else {
838 return -EINVAL;
839 }
840
841 return 0;
842}
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844#define SMBFS_NAME "smbfs"
845#define NCPFS_NAME "ncpfs"
David Howells9a9947b2005-04-18 10:54:51 -0700846#define NFS4_NAME "nfs4"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848asmlinkage long compat_sys_mount(char __user * dev_name, char __user * dir_name,
849 char __user * type, unsigned long flags,
850 void __user * data)
851{
852 unsigned long type_page;
853 unsigned long data_page;
854 unsigned long dev_page;
855 char *dir_page;
856 int retval;
857
858 retval = copy_mount_options (type, &type_page);
859 if (retval < 0)
860 goto out;
861
862 dir_page = getname(dir_name);
863 retval = PTR_ERR(dir_page);
864 if (IS_ERR(dir_page))
865 goto out1;
866
867 retval = copy_mount_options (dev_name, &dev_page);
868 if (retval < 0)
869 goto out2;
870
871 retval = copy_mount_options (data, &data_page);
872 if (retval < 0)
873 goto out3;
874
875 retval = -EINVAL;
876
Andrey Mirkin822191a2006-12-06 20:31:35 -0800877 if (type_page && data_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (!strcmp((char *)type_page, SMBFS_NAME)) {
879 do_smb_super_data_conv((void *)data_page);
880 } else if (!strcmp((char *)type_page, NCPFS_NAME)) {
881 do_ncp_super_data_conv((void *)data_page);
David Howells9a9947b2005-04-18 10:54:51 -0700882 } else if (!strcmp((char *)type_page, NFS4_NAME)) {
883 if (do_nfs4_super_data_conv((void *) data_page))
884 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
886 }
887
888 lock_kernel();
889 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
890 flags, (void*)data_page);
891 unlock_kernel();
892
David Howells9a9947b2005-04-18 10:54:51 -0700893 out4:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 free_page(data_page);
895 out3:
896 free_page(dev_page);
897 out2:
898 putname(dir_page);
899 out1:
900 free_page(type_page);
901 out:
902 return retval;
903}
904
905#define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
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;
Milind Arun Choudhary022a1692007-05-08 00:29:02 -0700993 int reclen = ALIGN(NAME_OFFSET(dirent) + namlen + 2, sizeof(compat_long_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069struct compat_getdents_callback64 {
1070 struct linux_dirent64 __user *current_dir;
1071 struct linux_dirent64 __user *previous;
1072 int count;
1073 int error;
1074};
1075
1076static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset,
David Howellsafefdbb2006-10-03 01:13:46 -07001077 u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
1079 struct linux_dirent64 __user *dirent;
1080 struct compat_getdents_callback64 *buf = __buf;
1081 int jj = NAME_OFFSET(dirent);
Milind Arun Choudhary022a1692007-05-08 00:29:02 -07001082 int reclen = ALIGN(jj + namlen + 1, sizeof(u64));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 u64 off;
1084
1085 buf->error = -EINVAL; /* only used if we fail.. */
1086 if (reclen > buf->count)
1087 return -EINVAL;
1088 dirent = buf->previous;
1089
1090 if (dirent) {
1091 if (__put_user_unaligned(offset, &dirent->d_off))
1092 goto efault;
1093 }
1094 dirent = buf->current_dir;
1095 if (__put_user_unaligned(ino, &dirent->d_ino))
1096 goto efault;
1097 off = 0;
1098 if (__put_user_unaligned(off, &dirent->d_off))
1099 goto efault;
1100 if (__put_user(reclen, &dirent->d_reclen))
1101 goto efault;
1102 if (__put_user(d_type, &dirent->d_type))
1103 goto efault;
1104 if (copy_to_user(dirent->d_name, name, namlen))
1105 goto efault;
1106 if (__put_user(0, dirent->d_name + namlen))
1107 goto efault;
1108 buf->previous = dirent;
1109 dirent = (void __user *)dirent + reclen;
1110 buf->current_dir = dirent;
1111 buf->count -= reclen;
1112 return 0;
1113efault:
1114 buf->error = -EFAULT;
1115 return -EFAULT;
1116}
1117
1118asmlinkage long compat_sys_getdents64(unsigned int fd,
1119 struct linux_dirent64 __user * dirent, unsigned int count)
1120{
1121 struct file * file;
1122 struct linux_dirent64 __user * lastdirent;
1123 struct compat_getdents_callback64 buf;
1124 int error;
1125
1126 error = -EFAULT;
1127 if (!access_ok(VERIFY_WRITE, dirent, count))
1128 goto out;
1129
1130 error = -EBADF;
1131 file = fget(fd);
1132 if (!file)
1133 goto out;
1134
1135 buf.current_dir = dirent;
1136 buf.previous = NULL;
1137 buf.count = count;
1138 buf.error = 0;
1139
1140 error = vfs_readdir(file, compat_filldir64, &buf);
1141 if (error < 0)
1142 goto out_putf;
1143 error = buf.error;
1144 lastdirent = buf.previous;
1145 if (lastdirent) {
1146 typeof(lastdirent->d_off) d_off = file->f_pos;
Heiko Carstens7116e992006-12-06 20:36:36 -08001147 error = -EFAULT;
1148 if (__put_user_unaligned(d_off, &lastdirent->d_off))
1149 goto out_putf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 error = count - buf.count;
1151 }
1152
1153out_putf:
1154 fput(file);
1155out:
1156 return error;
1157}
1158#endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
1159
1160static ssize_t compat_do_readv_writev(int type, struct file *file,
1161 const struct compat_iovec __user *uvector,
1162 unsigned long nr_segs, loff_t *pos)
1163{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 compat_ssize_t tot_len;
1165 struct iovec iovstack[UIO_FASTIOV];
1166 struct iovec *iov=iovstack, *vector;
1167 ssize_t ret;
1168 int seg;
1169 io_fn_t fn;
1170 iov_fn_t fnv;
1171
1172 /*
1173 * SuS says "The readv() function *may* fail if the iovcnt argument
1174 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
1175 * traditionally returned zero for zero segments, so...
1176 */
1177 ret = 0;
1178 if (nr_segs == 0)
1179 goto out;
1180
1181 /*
1182 * First get the "struct iovec" from user memory and
1183 * verify all the pointers
1184 */
1185 ret = -EINVAL;
1186 if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0))
1187 goto out;
1188 if (!file->f_op)
1189 goto out;
1190 if (nr_segs > UIO_FASTIOV) {
1191 ret = -ENOMEM;
1192 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
1193 if (!iov)
1194 goto out;
1195 }
1196 ret = -EFAULT;
1197 if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
1198 goto out;
1199
1200 /*
1201 * Single unix specification:
1202 * We should -EINVAL if an element length is not >= 0 and fitting an
1203 * ssize_t. The total length is fitting an ssize_t
1204 *
1205 * Be careful here because iov_len is a size_t not an ssize_t
1206 */
1207 tot_len = 0;
1208 vector = iov;
1209 ret = -EINVAL;
1210 for (seg = 0 ; seg < nr_segs; seg++) {
1211 compat_ssize_t tmp = tot_len;
1212 compat_ssize_t len;
1213 compat_uptr_t buf;
1214
1215 if (__get_user(len, &uvector->iov_len) ||
1216 __get_user(buf, &uvector->iov_base)) {
1217 ret = -EFAULT;
1218 goto out;
1219 }
1220 if (len < 0) /* size_t not fitting an compat_ssize_t .. */
1221 goto out;
1222 tot_len += len;
1223 if (tot_len < tmp) /* maths overflow on the compat_ssize_t */
1224 goto out;
1225 vector->iov_base = compat_ptr(buf);
1226 vector->iov_len = (compat_size_t) len;
1227 uvector++;
1228 vector++;
1229 }
1230 if (tot_len == 0) {
1231 ret = 0;
1232 goto out;
1233 }
1234
1235 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001236 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 goto out;
1238
James Morrise7edf9c2006-04-26 02:45:03 -04001239 ret = security_file_permission(file, type == READ ? MAY_READ:MAY_WRITE);
1240 if (ret)
1241 goto out;
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 fnv = NULL;
1244 if (type == READ) {
1245 fn = file->f_op->read;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001246 fnv = file->f_op->aio_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 } else {
1248 fn = (io_fn_t)file->f_op->write;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001249 fnv = file->f_op->aio_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
1251
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001252 if (fnv)
1253 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
1254 pos, fnv);
1255 else
1256 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258out:
1259 if (iov != iovstack)
1260 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -04001261 if ((ret + (type == READ)) > 0) {
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001262 struct dentry *dentry = file->f_path.dentry;
Robert Love0eeca282005-07-12 17:06:03 -04001263 if (type == READ)
1264 fsnotify_access(dentry);
1265 else
1266 fsnotify_modify(dentry);
1267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 return ret;
1269}
1270
1271asmlinkage ssize_t
1272compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1273{
1274 struct file *file;
1275 ssize_t ret = -EBADF;
1276
1277 file = fget(fd);
1278 if (!file)
1279 return -EBADF;
1280
1281 if (!(file->f_mode & FMODE_READ))
1282 goto out;
1283
1284 ret = -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001285 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 goto out;
1287
1288 ret = compat_do_readv_writev(READ, file, vec, vlen, &file->f_pos);
1289
1290out:
1291 fput(file);
1292 return ret;
1293}
1294
1295asmlinkage ssize_t
1296compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1297{
1298 struct file *file;
1299 ssize_t ret = -EBADF;
1300
1301 file = fget(fd);
1302 if (!file)
1303 return -EBADF;
1304 if (!(file->f_mode & FMODE_WRITE))
1305 goto out;
1306
1307 ret = -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001308 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 goto out;
1310
1311 ret = compat_do_readv_writev(WRITE, file, vec, vlen, &file->f_pos);
1312
1313out:
1314 fput(file);
1315 return ret;
1316}
1317
Andi Kleend2610202006-05-01 12:15:48 -07001318asmlinkage long
1319compat_sys_vmsplice(int fd, const struct compat_iovec __user *iov32,
1320 unsigned int nr_segs, unsigned int flags)
1321{
1322 unsigned i;
Al Viro90cbad62006-10-10 22:44:17 +01001323 struct iovec __user *iov;
Jens Axboe98232d52006-05-04 09:13:49 +02001324 if (nr_segs > UIO_MAXIOV)
Andi Kleend2610202006-05-01 12:15:48 -07001325 return -EINVAL;
1326 iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
1327 for (i = 0; i < nr_segs; i++) {
1328 struct compat_iovec v;
1329 if (get_user(v.iov_base, &iov32[i].iov_base) ||
1330 get_user(v.iov_len, &iov32[i].iov_len) ||
1331 put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
1332 put_user(v.iov_len, &iov[i].iov_len))
1333 return -EFAULT;
1334 }
1335 return sys_vmsplice(fd, iov, nr_segs, flags);
1336}
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338/*
Miklos Szeredie922efc2005-09-06 15:18:25 -07001339 * Exactly like fs/open.c:sys_open(), except that it doesn't set the
1340 * O_LARGEFILE flag.
1341 */
1342asmlinkage long
1343compat_sys_open(const char __user *filename, int flags, int mode)
1344{
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001345 return do_sys_open(AT_FDCWD, filename, flags, mode);
1346}
1347
1348/*
1349 * Exactly like fs/open.c:sys_openat(), except that it doesn't set the
1350 * O_LARGEFILE flag.
1351 */
1352asmlinkage long
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +11001353compat_sys_openat(unsigned int dfd, const char __user *filename, int flags, int mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001354{
1355 return do_sys_open(dfd, filename, flags, mode);
Miklos Szeredie922efc2005-09-06 15:18:25 -07001356}
1357
1358/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 * compat_count() counts the number of arguments/envelopes. It is basically
1360 * a copy of count() from fs/exec.c, except that it works with 32 bit argv
1361 * and envp pointers.
1362 */
1363static int compat_count(compat_uptr_t __user *argv, int max)
1364{
1365 int i = 0;
1366
1367 if (argv != NULL) {
1368 for (;;) {
1369 compat_uptr_t p;
1370
1371 if (get_user(p, argv))
1372 return -EFAULT;
1373 if (!p)
1374 break;
1375 argv++;
1376 if(++i > max)
1377 return -E2BIG;
1378 }
1379 }
1380 return i;
1381}
1382
1383/*
1384 * compat_copy_strings() is basically a copy of copy_strings() from fs/exec.c
1385 * except that it works with 32 bit argv and envp pointers.
1386 */
1387static int compat_copy_strings(int argc, compat_uptr_t __user *argv,
1388 struct linux_binprm *bprm)
1389{
1390 struct page *kmapped_page = NULL;
1391 char *kaddr = NULL;
1392 int ret;
1393
1394 while (argc-- > 0) {
1395 compat_uptr_t str;
1396 int len;
1397 unsigned long pos;
1398
1399 if (get_user(str, argv+argc) ||
1400 !(len = strnlen_user(compat_ptr(str), bprm->p))) {
1401 ret = -EFAULT;
1402 goto out;
1403 }
1404
1405 if (bprm->p < len) {
1406 ret = -E2BIG;
1407 goto out;
1408 }
1409
1410 bprm->p -= len;
1411 /* XXX: add architecture specific overflow check here. */
1412 pos = bprm->p;
1413
1414 while (len > 0) {
1415 int i, new, err;
1416 int offset, bytes_to_copy;
1417 struct page *page;
1418
1419 offset = pos % PAGE_SIZE;
1420 i = pos/PAGE_SIZE;
1421 page = bprm->page[i];
1422 new = 0;
1423 if (!page) {
1424 page = alloc_page(GFP_HIGHUSER);
1425 bprm->page[i] = page;
1426 if (!page) {
1427 ret = -ENOMEM;
1428 goto out;
1429 }
1430 new = 1;
1431 }
1432
1433 if (page != kmapped_page) {
1434 if (kmapped_page)
1435 kunmap(kmapped_page);
1436 kmapped_page = page;
1437 kaddr = kmap(kmapped_page);
1438 }
1439 if (new && offset)
1440 memset(kaddr, 0, offset);
1441 bytes_to_copy = PAGE_SIZE - offset;
1442 if (bytes_to_copy > len) {
1443 bytes_to_copy = len;
1444 if (new)
1445 memset(kaddr+offset+len, 0,
1446 PAGE_SIZE-offset-len);
1447 }
1448 err = copy_from_user(kaddr+offset, compat_ptr(str),
1449 bytes_to_copy);
1450 if (err) {
1451 ret = -EFAULT;
1452 goto out;
1453 }
1454
1455 pos += bytes_to_copy;
1456 str += bytes_to_copy;
1457 len -= bytes_to_copy;
1458 }
1459 }
1460 ret = 0;
1461out:
1462 if (kmapped_page)
1463 kunmap(kmapped_page);
1464 return ret;
1465}
1466
1467#ifdef CONFIG_MMU
1468
1469#define free_arg_pages(bprm) do { } while (0)
1470
1471#else
1472
1473static inline void free_arg_pages(struct linux_binprm *bprm)
1474{
1475 int i;
1476
1477 for (i = 0; i < MAX_ARG_PAGES; i++) {
1478 if (bprm->page[i])
1479 __free_page(bprm->page[i]);
1480 bprm->page[i] = NULL;
1481 }
1482}
1483
1484#endif /* CONFIG_MMU */
1485
1486/*
1487 * compat_do_execve() is mostly a copy of do_execve(), with the exception
1488 * that it processes 32 bit argv and envp pointers.
1489 */
1490int compat_do_execve(char * filename,
1491 compat_uptr_t __user *argv,
1492 compat_uptr_t __user *envp,
1493 struct pt_regs * regs)
1494{
1495 struct linux_binprm *bprm;
1496 struct file *file;
1497 int retval;
1498 int i;
1499
1500 retval = -ENOMEM;
Oliver Neukum11b0b5a2006-03-25 03:08:13 -08001501 bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (!bprm)
1503 goto out_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 file = open_exec(filename);
1506 retval = PTR_ERR(file);
1507 if (IS_ERR(file))
1508 goto out_kfree;
1509
1510 sched_exec();
1511
1512 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1513 bprm->file = file;
1514 bprm->filename = filename;
1515 bprm->interp = filename;
1516 bprm->mm = mm_alloc();
1517 retval = -ENOMEM;
1518 if (!bprm->mm)
1519 goto out_file;
1520
1521 retval = init_new_context(current, bprm->mm);
1522 if (retval < 0)
1523 goto out_mm;
1524
1525 bprm->argc = compat_count(argv, bprm->p / sizeof(compat_uptr_t));
1526 if ((retval = bprm->argc) < 0)
1527 goto out_mm;
1528
1529 bprm->envc = compat_count(envp, bprm->p / sizeof(compat_uptr_t));
1530 if ((retval = bprm->envc) < 0)
1531 goto out_mm;
1532
1533 retval = security_bprm_alloc(bprm);
1534 if (retval)
1535 goto out;
1536
1537 retval = prepare_binprm(bprm);
1538 if (retval < 0)
1539 goto out;
1540
1541 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1542 if (retval < 0)
1543 goto out;
1544
1545 bprm->exec = bprm->p;
1546 retval = compat_copy_strings(bprm->envc, envp, bprm);
1547 if (retval < 0)
1548 goto out;
1549
1550 retval = compat_copy_strings(bprm->argc, argv, bprm);
1551 if (retval < 0)
1552 goto out;
1553
1554 retval = search_binary_handler(bprm, regs);
1555 if (retval >= 0) {
1556 free_arg_pages(bprm);
1557
1558 /* execve success */
1559 security_bprm_free(bprm);
David S. Miller4a805e82005-09-14 21:40:00 -07001560 acct_update_integrals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 kfree(bprm);
1562 return retval;
1563 }
1564
1565out:
1566 /* Something went wrong, return the inode and free the argument pages*/
1567 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1568 struct page * page = bprm->page[i];
1569 if (page)
1570 __free_page(page);
1571 }
1572
1573 if (bprm->security)
1574 security_bprm_free(bprm);
1575
1576out_mm:
1577 if (bprm->mm)
1578 mmdrop(bprm->mm);
1579
1580out_file:
1581 if (bprm->file) {
1582 allow_write_access(bprm->file);
1583 fput(bprm->file);
1584 }
1585
1586out_kfree:
1587 kfree(bprm);
1588
1589out_ret:
1590 return retval;
1591}
1592
1593#define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595/*
1596 * Ooo, nasty. We need here to frob 32-bit unsigned longs to
1597 * 64-bit unsigned longs.
1598 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001599static
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1601 unsigned long *fdset)
1602{
Milind Arun Choudhary022a1692007-05-08 00:29:02 -07001603 nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 if (ufdset) {
1605 unsigned long odd;
1606
1607 if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
1608 return -EFAULT;
1609
1610 odd = nr & 1UL;
1611 nr &= ~1UL;
1612 while (nr) {
1613 unsigned long h, l;
Heiko Carstens7116e992006-12-06 20:36:36 -08001614 if (__get_user(l, ufdset) || __get_user(h, ufdset+1))
1615 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 ufdset += 2;
1617 *fdset++ = h << 32 | l;
1618 nr -= 2;
1619 }
Heiko Carstens7116e992006-12-06 20:36:36 -08001620 if (odd && __get_user(*fdset, ufdset))
1621 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 } else {
1623 /* Tricky, must clear full unsigned long in the
1624 * kernel fdset at the end, this makes sure that
1625 * actually happens.
1626 */
1627 memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
1628 }
1629 return 0;
1630}
1631
Arjan van de Ven858119e2006-01-14 13:20:43 -08001632static
Heiko Carstens7116e992006-12-06 20:36:36 -08001633int compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1634 unsigned long *fdset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
1636 unsigned long odd;
Milind Arun Choudhary022a1692007-05-08 00:29:02 -07001637 nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 if (!ufdset)
Heiko Carstens7116e992006-12-06 20:36:36 -08001640 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 odd = nr & 1UL;
1643 nr &= ~1UL;
1644 while (nr) {
1645 unsigned long h, l;
1646 l = *fdset++;
1647 h = l >> 32;
Heiko Carstens7116e992006-12-06 20:36:36 -08001648 if (__put_user(l, ufdset) || __put_user(h, ufdset+1))
1649 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 ufdset += 2;
1651 nr -= 2;
1652 }
Heiko Carstens7116e992006-12-06 20:36:36 -08001653 if (odd && __put_user(*fdset, ufdset))
1654 return -EFAULT;
1655 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
1658
1659/*
1660 * This is a virtual copy of sys_select from fs/select.c and probably
1661 * should be compared to it from time to time
1662 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664/*
1665 * We can actually return ERESTARTSYS instead of EINTR, but I'd
1666 * like to be certain this leads to no problems. So I return
1667 * EINTR just for safety.
1668 *
1669 * Update: ERESTARTSYS breaks at least the xview clock binary, so
1670 * I'm trying ERESTARTNOHAND which restart only when you want to.
1671 */
1672#define MAX_SELECT_SECONDS \
1673 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
1674
David Woodhouse9f729492006-01-18 17:44:05 -08001675int compat_core_sys_select(int n, compat_ulong_t __user *inp,
1676 compat_ulong_t __user *outp, compat_ulong_t __user *exp, s64 *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
1678 fd_set_bits fds;
1679 char *bits;
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08001680 int size, max_fds, ret = -EINVAL;
Linus Torvaldsa4531ed2005-09-09 15:10:52 -07001681 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if (n < 0)
1684 goto out_nofds;
1685
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08001686 /* max_fds can increase, so grab it once to avoid race */
Linus Torvaldsac5b8b62005-09-09 15:42:34 -07001687 rcu_read_lock();
Linus Torvaldsa4531ed2005-09-09 15:10:52 -07001688 fdt = files_fdtable(current->files);
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08001689 max_fds = fdt->max_fds;
Linus Torvaldsac5b8b62005-09-09 15:42:34 -07001690 rcu_read_unlock();
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08001691 if (n > max_fds)
1692 n = max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 /*
1695 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
1696 * since we used fdset we need to allocate memory in units of
1697 * long-words.
1698 */
1699 ret = -ENOMEM;
1700 size = FDS_BYTES(n);
Vadim Lobanov68c34312006-03-28 01:56:35 -08001701 bits = kmalloc(6 * size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 if (!bits)
1703 goto out_nofds;
1704 fds.in = (unsigned long *) bits;
1705 fds.out = (unsigned long *) (bits + size);
1706 fds.ex = (unsigned long *) (bits + 2*size);
1707 fds.res_in = (unsigned long *) (bits + 3*size);
1708 fds.res_out = (unsigned long *) (bits + 4*size);
1709 fds.res_ex = (unsigned long *) (bits + 5*size);
1710
1711 if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
1712 (ret = compat_get_fd_set(n, outp, fds.out)) ||
1713 (ret = compat_get_fd_set(n, exp, fds.ex)))
1714 goto out;
1715 zero_fd_set(n, fds.res_in);
1716 zero_fd_set(n, fds.res_out);
1717 zero_fd_set(n, fds.res_ex);
1718
David Woodhouse9f729492006-01-18 17:44:05 -08001719 ret = do_select(n, &fds, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 if (ret < 0)
1722 goto out;
1723 if (!ret) {
1724 ret = -ERESTARTNOHAND;
1725 if (signal_pending(current))
1726 goto out;
1727 ret = 0;
1728 }
1729
Heiko Carstens7116e992006-12-06 20:36:36 -08001730 if (compat_set_fd_set(n, inp, fds.res_in) ||
1731 compat_set_fd_set(n, outp, fds.res_out) ||
1732 compat_set_fd_set(n, exp, fds.res_ex))
1733 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734out:
Vadim Lobanov68c34312006-03-28 01:56:35 -08001735 kfree(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736out_nofds:
1737 return ret;
1738}
1739
David Woodhouse9f729492006-01-18 17:44:05 -08001740asmlinkage long compat_sys_select(int n, compat_ulong_t __user *inp,
1741 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1742 struct compat_timeval __user *tvp)
1743{
1744 s64 timeout = -1;
1745 struct compat_timeval tv;
1746 int ret;
1747
1748 if (tvp) {
1749 if (copy_from_user(&tv, tvp, sizeof(tv)))
1750 return -EFAULT;
1751
1752 if (tv.tv_sec < 0 || tv.tv_usec < 0)
1753 return -EINVAL;
1754
1755 /* Cast to u64 to make GCC stop complaining */
1756 if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
1757 timeout = -1; /* infinite */
1758 else {
Milind Arun Choudhary022a1692007-05-08 00:29:02 -07001759 timeout = DIV_ROUND_UP(tv.tv_usec, 1000000/HZ);
David Woodhouse9f729492006-01-18 17:44:05 -08001760 timeout += tv.tv_sec * HZ;
1761 }
1762 }
1763
1764 ret = compat_core_sys_select(n, inp, outp, exp, &timeout);
1765
1766 if (tvp) {
Andrew Morton643a6542006-02-11 17:55:52 -08001767 struct compat_timeval rtv;
1768
David Woodhouse9f729492006-01-18 17:44:05 -08001769 if (current->personality & STICKY_TIMEOUTS)
1770 goto sticky;
Andrew Morton643a6542006-02-11 17:55:52 -08001771 rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
1772 rtv.tv_sec = timeout;
Andrew Morton74910e62006-02-17 13:52:58 -08001773 if (compat_timeval_compare(&rtv, &tv) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -08001774 rtv = tv;
1775 if (copy_to_user(tvp, &rtv, sizeof(rtv))) {
David Woodhouse9f729492006-01-18 17:44:05 -08001776sticky:
1777 /*
1778 * If an application puts its timeval in read-only
1779 * memory, we don't want the Linux-specific update to
1780 * the timeval to cause a fault after the select has
1781 * completed successfully. However, because we're not
1782 * updating the timeval, we can't restart the system
1783 * call.
1784 */
1785 if (ret == -ERESTARTNOHAND)
1786 ret = -EINTR;
1787 }
1788 }
1789
1790 return ret;
1791}
1792
1793#ifdef TIF_RESTORE_SIGMASK
1794asmlinkage long compat_sys_pselect7(int n, compat_ulong_t __user *inp,
1795 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1796 struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask,
1797 compat_size_t sigsetsize)
1798{
1799 compat_sigset_t ss32;
1800 sigset_t ksigmask, sigsaved;
Andrew Mortoncb82a6c2006-02-01 03:04:49 -08001801 s64 timeout = MAX_SCHEDULE_TIMEOUT;
David Woodhouse9f729492006-01-18 17:44:05 -08001802 struct compat_timespec ts;
1803 int ret;
1804
1805 if (tsp) {
1806 if (copy_from_user(&ts, tsp, sizeof(ts)))
1807 return -EFAULT;
1808
1809 if (ts.tv_sec < 0 || ts.tv_nsec < 0)
1810 return -EINVAL;
1811 }
1812
1813 if (sigmask) {
1814 if (sigsetsize != sizeof(compat_sigset_t))
1815 return -EINVAL;
1816 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1817 return -EFAULT;
1818 sigset_from_compat(&ksigmask, &ss32);
1819
1820 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1821 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1822 }
1823
1824 do {
1825 if (tsp) {
1826 if ((unsigned long)ts.tv_sec < MAX_SELECT_SECONDS) {
Milind Arun Choudhary022a1692007-05-08 00:29:02 -07001827 timeout = DIV_ROUND_UP(ts.tv_nsec, 1000000000/HZ);
David Woodhouse9f729492006-01-18 17:44:05 -08001828 timeout += ts.tv_sec * (unsigned long)HZ;
1829 ts.tv_sec = 0;
1830 ts.tv_nsec = 0;
1831 } else {
1832 ts.tv_sec -= MAX_SELECT_SECONDS;
1833 timeout = MAX_SELECT_SECONDS * HZ;
1834 }
1835 }
1836
1837 ret = compat_core_sys_select(n, inp, outp, exp, &timeout);
1838
1839 } while (!ret && !timeout && tsp && (ts.tv_sec || ts.tv_nsec));
1840
Heiko Carstens87c2b7c2006-11-02 22:06:58 -08001841 if (tsp) {
Andrew Morton643a6542006-02-11 17:55:52 -08001842 struct compat_timespec rts;
1843
Heiko Carstens87c2b7c2006-11-02 22:06:58 -08001844 if (current->personality & STICKY_TIMEOUTS)
1845 goto sticky;
1846
Andrew Morton643a6542006-02-11 17:55:52 -08001847 rts.tv_sec = timeout / HZ;
1848 rts.tv_nsec = (timeout % HZ) * (NSEC_PER_SEC/HZ);
1849 if (rts.tv_nsec >= NSEC_PER_SEC) {
1850 rts.tv_sec++;
1851 rts.tv_nsec -= NSEC_PER_SEC;
David Woodhouse9f729492006-01-18 17:44:05 -08001852 }
Andrew Morton74910e62006-02-17 13:52:58 -08001853 if (compat_timespec_compare(&rts, &ts) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -08001854 rts = ts;
Heiko Carstens87c2b7c2006-11-02 22:06:58 -08001855 if (copy_to_user(tsp, &rts, sizeof(rts))) {
1856sticky:
1857 /*
1858 * If an application puts its timeval in read-only
1859 * memory, we don't want the Linux-specific update to
1860 * the timeval to cause a fault after the select has
1861 * completed successfully. However, because we're not
1862 * updating the timeval, we can't restart the system
1863 * call.
1864 */
1865 if (ret == -ERESTARTNOHAND)
1866 ret = -EINTR;
1867 }
David Woodhouse9f729492006-01-18 17:44:05 -08001868 }
1869
1870 if (ret == -ERESTARTNOHAND) {
1871 /*
1872 * Don't restore the signal mask yet. Let do_signal() deliver
1873 * the signal on the way back to userspace, before the signal
1874 * mask is restored.
1875 */
1876 if (sigmask) {
1877 memcpy(&current->saved_sigmask, &sigsaved,
1878 sizeof(sigsaved));
1879 set_thread_flag(TIF_RESTORE_SIGMASK);
1880 }
1881 } else if (sigmask)
1882 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1883
1884 return ret;
1885}
1886
1887asmlinkage long compat_sys_pselect6(int n, compat_ulong_t __user *inp,
1888 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1889 struct compat_timespec __user *tsp, void __user *sig)
1890{
1891 compat_size_t sigsetsize = 0;
1892 compat_uptr_t up = 0;
1893
1894 if (sig) {
1895 if (!access_ok(VERIFY_READ, sig,
1896 sizeof(compat_uptr_t)+sizeof(compat_size_t)) ||
1897 __get_user(up, (compat_uptr_t __user *)sig) ||
1898 __get_user(sigsetsize,
1899 (compat_size_t __user *)(sig+sizeof(up))))
1900 return -EFAULT;
1901 }
1902 return compat_sys_pselect7(n, inp, outp, exp, tsp, compat_ptr(up),
1903 sigsetsize);
1904}
1905
1906asmlinkage long compat_sys_ppoll(struct pollfd __user *ufds,
1907 unsigned int nfds, struct compat_timespec __user *tsp,
1908 const compat_sigset_t __user *sigmask, compat_size_t sigsetsize)
1909{
1910 compat_sigset_t ss32;
1911 sigset_t ksigmask, sigsaved;
1912 struct compat_timespec ts;
1913 s64 timeout = -1;
1914 int ret;
1915
1916 if (tsp) {
1917 if (copy_from_user(&ts, tsp, sizeof(ts)))
1918 return -EFAULT;
1919
1920 /* We assume that ts.tv_sec is always lower than
1921 the number of seconds that can be expressed in
1922 an s64. Otherwise the compiler bitches at us */
Milind Arun Choudhary022a1692007-05-08 00:29:02 -07001923 timeout = DIV_ROUND_UP(ts.tv_nsec, 1000000000/HZ);
David Woodhouse9f729492006-01-18 17:44:05 -08001924 timeout += ts.tv_sec * HZ;
1925 }
1926
1927 if (sigmask) {
Alexey Dobriyan3835a9b2006-05-15 09:44:27 -07001928 if (sigsetsize != sizeof(compat_sigset_t))
David Woodhouse9f729492006-01-18 17:44:05 -08001929 return -EINVAL;
1930 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1931 return -EFAULT;
1932 sigset_from_compat(&ksigmask, &ss32);
1933
1934 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1935 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1936 }
1937
1938 ret = do_sys_poll(ufds, nfds, &timeout);
1939
1940 /* We can restart this syscall, usually */
1941 if (ret == -EINTR) {
1942 /*
1943 * Don't restore the signal mask yet. Let do_signal() deliver
1944 * the signal on the way back to userspace, before the signal
1945 * mask is restored.
1946 */
1947 if (sigmask) {
1948 memcpy(&current->saved_sigmask, &sigsaved,
1949 sizeof(sigsaved));
1950 set_thread_flag(TIF_RESTORE_SIGMASK);
1951 }
1952 ret = -ERESTARTNOHAND;
1953 } else if (sigmask)
1954 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1955
1956 if (tsp && timeout >= 0) {
Andrew Morton643a6542006-02-11 17:55:52 -08001957 struct compat_timespec rts;
1958
David Woodhouse9f729492006-01-18 17:44:05 -08001959 if (current->personality & STICKY_TIMEOUTS)
1960 goto sticky;
1961 /* Yes, we know it's actually an s64, but it's also positive. */
Andrew Morton643a6542006-02-11 17:55:52 -08001962 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
1963 1000;
1964 rts.tv_sec = timeout;
Andrew Morton74910e62006-02-17 13:52:58 -08001965 if (compat_timespec_compare(&rts, &ts) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -08001966 rts = ts;
1967 if (copy_to_user(tsp, &rts, sizeof(rts))) {
David Woodhouse9f729492006-01-18 17:44:05 -08001968sticky:
1969 /*
1970 * If an application puts its timeval in read-only
1971 * memory, we don't want the Linux-specific update to
1972 * the timeval to cause a fault after the select has
1973 * completed successfully. However, because we're not
1974 * updating the timeval, we can't restart the system
1975 * call.
1976 */
1977 if (ret == -ERESTARTNOHAND && timeout >= 0)
1978 ret = -EINTR;
1979 }
1980 }
1981
1982 return ret;
1983}
1984#endif /* TIF_RESTORE_SIGMASK */
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986#if defined(CONFIG_NFSD) || defined(CONFIG_NFSD_MODULE)
1987/* Stuff for NFS server syscalls... */
1988struct compat_nfsctl_svc {
1989 u16 svc32_port;
1990 s32 svc32_nthreads;
1991};
1992
1993struct compat_nfsctl_client {
1994 s8 cl32_ident[NFSCLNT_IDMAX+1];
1995 s32 cl32_naddr;
1996 struct in_addr cl32_addrlist[NFSCLNT_ADDRMAX];
1997 s32 cl32_fhkeytype;
1998 s32 cl32_fhkeylen;
1999 u8 cl32_fhkey[NFSCLNT_KEYMAX];
2000};
2001
2002struct compat_nfsctl_export {
2003 char ex32_client[NFSCLNT_IDMAX+1];
2004 char ex32_path[NFS_MAXPATHLEN+1];
2005 compat_dev_t ex32_dev;
2006 compat_ino_t ex32_ino;
2007 compat_int_t ex32_flags;
Stephen Rothwell202e5972005-09-06 15:16:40 -07002008 __compat_uid_t ex32_anon_uid;
2009 __compat_gid_t ex32_anon_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010};
2011
2012struct compat_nfsctl_fdparm {
2013 struct sockaddr gd32_addr;
2014 s8 gd32_path[NFS_MAXPATHLEN+1];
2015 compat_int_t gd32_version;
2016};
2017
2018struct compat_nfsctl_fsparm {
2019 struct sockaddr gd32_addr;
2020 s8 gd32_path[NFS_MAXPATHLEN+1];
2021 compat_int_t gd32_maxlen;
2022};
2023
2024struct compat_nfsctl_arg {
2025 compat_int_t ca32_version; /* safeguard */
2026 union {
2027 struct compat_nfsctl_svc u32_svc;
2028 struct compat_nfsctl_client u32_client;
2029 struct compat_nfsctl_export u32_export;
2030 struct compat_nfsctl_fdparm u32_getfd;
2031 struct compat_nfsctl_fsparm u32_getfs;
2032 } u;
2033#define ca32_svc u.u32_svc
2034#define ca32_client u.u32_client
2035#define ca32_export u.u32_export
2036#define ca32_getfd u.u32_getfd
2037#define ca32_getfs u.u32_getfs
2038};
2039
2040union compat_nfsctl_res {
2041 __u8 cr32_getfh[NFS_FHSIZE];
2042 struct knfsd_fh cr32_getfs;
2043};
2044
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002045static int compat_nfs_svc_trans(struct nfsctl_arg *karg,
2046 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002048 if (!access_ok(VERIFY_READ, &arg->ca32_svc, sizeof(arg->ca32_svc)) ||
2049 get_user(karg->ca_version, &arg->ca32_version) ||
2050 __get_user(karg->ca_svc.svc_port, &arg->ca32_svc.svc32_port) ||
2051 __get_user(karg->ca_svc.svc_nthreads,
2052 &arg->ca32_svc.svc32_nthreads))
2053 return -EFAULT;
2054 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002057static int compat_nfs_clnt_trans(struct nfsctl_arg *karg,
2058 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002060 if (!access_ok(VERIFY_READ, &arg->ca32_client,
2061 sizeof(arg->ca32_client)) ||
2062 get_user(karg->ca_version, &arg->ca32_version) ||
2063 __copy_from_user(&karg->ca_client.cl_ident[0],
2064 &arg->ca32_client.cl32_ident[0],
2065 NFSCLNT_IDMAX) ||
2066 __get_user(karg->ca_client.cl_naddr,
2067 &arg->ca32_client.cl32_naddr) ||
2068 __copy_from_user(&karg->ca_client.cl_addrlist[0],
2069 &arg->ca32_client.cl32_addrlist[0],
2070 (sizeof(struct in_addr) * NFSCLNT_ADDRMAX)) ||
2071 __get_user(karg->ca_client.cl_fhkeytype,
2072 &arg->ca32_client.cl32_fhkeytype) ||
2073 __get_user(karg->ca_client.cl_fhkeylen,
2074 &arg->ca32_client.cl32_fhkeylen) ||
2075 __copy_from_user(&karg->ca_client.cl_fhkey[0],
2076 &arg->ca32_client.cl32_fhkey[0],
2077 NFSCLNT_KEYMAX))
2078 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002080 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081}
2082
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002083static int compat_nfs_exp_trans(struct nfsctl_arg *karg,
2084 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002086 if (!access_ok(VERIFY_READ, &arg->ca32_export,
2087 sizeof(arg->ca32_export)) ||
2088 get_user(karg->ca_version, &arg->ca32_version) ||
2089 __copy_from_user(&karg->ca_export.ex_client[0],
2090 &arg->ca32_export.ex32_client[0],
2091 NFSCLNT_IDMAX) ||
2092 __copy_from_user(&karg->ca_export.ex_path[0],
2093 &arg->ca32_export.ex32_path[0],
2094 NFS_MAXPATHLEN) ||
2095 __get_user(karg->ca_export.ex_dev,
2096 &arg->ca32_export.ex32_dev) ||
2097 __get_user(karg->ca_export.ex_ino,
2098 &arg->ca32_export.ex32_ino) ||
2099 __get_user(karg->ca_export.ex_flags,
2100 &arg->ca32_export.ex32_flags) ||
2101 __get_user(karg->ca_export.ex_anon_uid,
2102 &arg->ca32_export.ex32_anon_uid) ||
2103 __get_user(karg->ca_export.ex_anon_gid,
2104 &arg->ca32_export.ex32_anon_gid))
2105 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 SET_UID(karg->ca_export.ex_anon_uid, karg->ca_export.ex_anon_uid);
2107 SET_GID(karg->ca_export.ex_anon_gid, karg->ca_export.ex_anon_gid);
2108
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002109 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002112static int compat_nfs_getfd_trans(struct nfsctl_arg *karg,
2113 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002115 if (!access_ok(VERIFY_READ, &arg->ca32_getfd,
2116 sizeof(arg->ca32_getfd)) ||
2117 get_user(karg->ca_version, &arg->ca32_version) ||
2118 __copy_from_user(&karg->ca_getfd.gd_addr,
2119 &arg->ca32_getfd.gd32_addr,
2120 (sizeof(struct sockaddr))) ||
2121 __copy_from_user(&karg->ca_getfd.gd_path,
2122 &arg->ca32_getfd.gd32_path,
2123 (NFS_MAXPATHLEN+1)) ||
2124 __get_user(karg->ca_getfd.gd_version,
2125 &arg->ca32_getfd.gd32_version))
2126 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129}
2130
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002131static int compat_nfs_getfs_trans(struct nfsctl_arg *karg,
2132 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002134 if (!access_ok(VERIFY_READ,&arg->ca32_getfs,sizeof(arg->ca32_getfs)) ||
2135 get_user(karg->ca_version, &arg->ca32_version) ||
2136 __copy_from_user(&karg->ca_getfs.gd_addr,
2137 &arg->ca32_getfs.gd32_addr,
2138 (sizeof(struct sockaddr))) ||
2139 __copy_from_user(&karg->ca_getfs.gd_path,
2140 &arg->ca32_getfs.gd32_path,
2141 (NFS_MAXPATHLEN+1)) ||
2142 __get_user(karg->ca_getfs.gd_maxlen,
2143 &arg->ca32_getfs.gd32_maxlen))
2144 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
2149/* This really doesn't need translations, we are only passing
2150 * back a union which contains opaque nfs file handle data.
2151 */
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002152static int compat_nfs_getfh_res_trans(union nfsctl_res *kres,
2153 union compat_nfsctl_res __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
2155 int err;
2156
2157 err = copy_to_user(res, kres, sizeof(*res));
2158
2159 return (err) ? -EFAULT : 0;
2160}
2161
Lin Feng Shend64b1c82006-05-20 14:59:49 -07002162asmlinkage long compat_sys_nfsservctl(int cmd,
2163 struct compat_nfsctl_arg __user *arg,
2164 union compat_nfsctl_res __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
2166 struct nfsctl_arg *karg;
2167 union nfsctl_res *kres;
2168 mm_segment_t oldfs;
2169 int err;
2170
2171 karg = kmalloc(sizeof(*karg), GFP_USER);
2172 kres = kmalloc(sizeof(*kres), GFP_USER);
2173 if(!karg || !kres) {
2174 err = -ENOMEM;
2175 goto done;
2176 }
2177
2178 switch(cmd) {
2179 case NFSCTL_SVC:
2180 err = compat_nfs_svc_trans(karg, arg);
2181 break;
2182
2183 case NFSCTL_ADDCLIENT:
2184 err = compat_nfs_clnt_trans(karg, arg);
2185 break;
2186
2187 case NFSCTL_DELCLIENT:
2188 err = compat_nfs_clnt_trans(karg, arg);
2189 break;
2190
2191 case NFSCTL_EXPORT:
2192 case NFSCTL_UNEXPORT:
2193 err = compat_nfs_exp_trans(karg, arg);
2194 break;
2195
2196 case NFSCTL_GETFD:
2197 err = compat_nfs_getfd_trans(karg, arg);
2198 break;
2199
2200 case NFSCTL_GETFS:
2201 err = compat_nfs_getfs_trans(karg, arg);
2202 break;
2203
2204 default:
2205 err = -EINVAL;
Peter Staubach57070d02006-03-25 03:08:04 -08002206 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 }
2208
Peter Staubach57070d02006-03-25 03:08:04 -08002209 if (err)
2210 goto done;
2211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 oldfs = get_fs();
2213 set_fs(KERNEL_DS);
2214 /* The __user pointer casts are valid because of the set_fs() */
2215 err = sys_nfsservctl(cmd, (void __user *) karg, (void __user *) kres);
2216 set_fs(oldfs);
2217
2218 if (err)
2219 goto done;
2220
2221 if((cmd == NFSCTL_GETFD) ||
2222 (cmd == NFSCTL_GETFS))
2223 err = compat_nfs_getfh_res_trans(kres, res);
2224
2225done:
2226 kfree(karg);
2227 kfree(kres);
2228 return err;
2229}
2230#else /* !NFSD */
2231long asmlinkage compat_sys_nfsservctl(int cmd, void *notused, void *notused2)
2232{
2233 return sys_ni_syscall();
2234}
2235#endif
Davide Libenzif6dfb4f2007-03-07 20:41:21 -08002236
2237#ifdef CONFIG_EPOLL
2238
2239#ifdef CONFIG_HAS_COMPAT_EPOLL_EVENT
2240asmlinkage long compat_sys_epoll_ctl(int epfd, int op, int fd,
2241 struct compat_epoll_event __user *event)
2242{
2243 long err = 0;
2244 struct compat_epoll_event user;
2245 struct epoll_event __user *kernel = NULL;
2246
2247 if (event) {
2248 if (copy_from_user(&user, event, sizeof(user)))
2249 return -EFAULT;
2250 kernel = compat_alloc_user_space(sizeof(struct epoll_event));
2251 err |= __put_user(user.events, &kernel->events);
2252 err |= __put_user(user.data, &kernel->data);
2253 }
2254
2255 return err ? err : sys_epoll_ctl(epfd, op, fd, kernel);
2256}
2257
2258
2259asmlinkage long compat_sys_epoll_wait(int epfd,
2260 struct compat_epoll_event __user *events,
2261 int maxevents, int timeout)
2262{
2263 long i, ret, err = 0;
2264 struct epoll_event __user *kbuf;
2265 struct epoll_event ev;
2266
2267 if ((maxevents <= 0) ||
2268 (maxevents > (INT_MAX / sizeof(struct epoll_event))))
2269 return -EINVAL;
2270 kbuf = compat_alloc_user_space(sizeof(struct epoll_event) * maxevents);
2271 ret = sys_epoll_wait(epfd, kbuf, maxevents, timeout);
2272 for (i = 0; i < ret; i++) {
2273 err |= __get_user(ev.events, &kbuf[i].events);
2274 err |= __get_user(ev.data, &kbuf[i].data);
2275 err |= __put_user(ev.events, &events->events);
2276 err |= __put_user_unaligned(ev.data, &events->data);
2277 events++;
2278 }
2279
2280 return err ? -EFAULT: ret;
2281}
2282#endif /* CONFIG_HAS_COMPAT_EPOLL_EVENT */
2283
2284#ifdef TIF_RESTORE_SIGMASK
2285asmlinkage long compat_sys_epoll_pwait(int epfd,
2286 struct compat_epoll_event __user *events,
2287 int maxevents, int timeout,
2288 const compat_sigset_t __user *sigmask,
2289 compat_size_t sigsetsize)
2290{
2291 long err;
2292 compat_sigset_t csigmask;
2293 sigset_t ksigmask, sigsaved;
2294
2295 /*
2296 * If the caller wants a certain signal mask to be set during the wait,
2297 * we apply it here.
2298 */
2299 if (sigmask) {
2300 if (sigsetsize != sizeof(compat_sigset_t))
2301 return -EINVAL;
2302 if (copy_from_user(&csigmask, sigmask, sizeof(csigmask)))
2303 return -EFAULT;
2304 sigset_from_compat(&ksigmask, &csigmask);
2305 sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
2306 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
2307 }
2308
2309#ifdef CONFIG_HAS_COMPAT_EPOLL_EVENT
2310 err = compat_sys_epoll_wait(epfd, events, maxevents, timeout);
2311#else
2312 err = sys_epoll_wait(epfd, events, maxevents, timeout);
2313#endif
2314
2315 /*
2316 * If we changed the signal mask, we need to restore the original one.
2317 * In case we've got a signal while waiting, we do not restore the
2318 * signal mask yet, and we allow do_signal() to deliver the signal on
2319 * the way back to userspace, before the signal mask is restored.
2320 */
2321 if (sigmask) {
2322 if (err == -EINTR) {
2323 memcpy(&current->saved_sigmask, &sigsaved,
2324 sizeof(sigsaved));
2325 set_thread_flag(TIF_RESTORE_SIGMASK);
2326 } else
2327 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2328 }
2329
2330 return err;
2331}
2332#endif /* TIF_RESTORE_SIGMASK */
2333
2334#endif /* CONFIG_EPOLL */