blob: 133ed7f5d681ef111922953fb5a1b2ca99cb5bba [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>
Al Viro9f3acc32008-04-24 07:44:08 -040027#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/vfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/ioctl.h>
30#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#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>
Christoph Hellwig6272e262007-05-08 00:29:21 -070048#include <linux/security.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040049#include <linux/highmem.h>
Davide Libenzi6d18c922007-05-10 22:23:15 -070050#include <linux/signal.h>
Al Virobd01f842006-10-19 17:23:57 -040051#include <linux/poll.h>
David S. Miller4a805e82005-09-14 21:40:00 -070052#include <linux/mm.h>
Davide Libenzif6dfb4f2007-03-07 20:41:21 -080053#include <linux/eventpoll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#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{
Ulrich Drepper1c710c82007-05-08 00:33:25 -070082 struct timespec tv[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
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;
Ulrich Drepper1c710c82007-05-08 00:33:25 -070088 tv[0].tv_nsec = 0;
89 tv[1].tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
Ulrich Drepper1c710c82007-05-08 00:33:25 -070091 return do_utimes(AT_FDCWD, filename, t ? tv : NULL, 0);
92}
93
94asmlinkage long compat_sys_utimensat(unsigned int dfd, char __user *filename, struct compat_timespec __user *t, int flags)
95{
96 struct timespec tv[2];
97
98 if (t) {
99 if (get_compat_timespec(&tv[0], &t[0]) ||
100 get_compat_timespec(&tv[1], &t[1]))
101 return -EFAULT;
102
103 if ((tv[0].tv_nsec == UTIME_OMIT || tv[0].tv_nsec == UTIME_NOW)
104 && tv[0].tv_sec != 0)
105 return -EINVAL;
106 if ((tv[1].tv_nsec == UTIME_OMIT || tv[1].tv_nsec == UTIME_NOW)
107 && tv[1].tv_sec != 0)
108 return -EINVAL;
109
110 if (tv[0].tv_nsec == UTIME_OMIT && tv[1].tv_nsec == UTIME_OMIT)
111 return 0;
112 }
113 return do_utimes(dfd, filename, t ? tv : NULL, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +1100116asmlinkage long compat_sys_futimesat(unsigned int dfd, char __user *filename, struct compat_timeval __user *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Ulrich Drepper1c710c82007-05-08 00:33:25 -0700118 struct timespec tv[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +1100120 if (t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
Ulrich Drepper1c710c82007-05-08 00:33:25 -0700122 get_user(tv[0].tv_nsec, &t[0].tv_usec) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 get_user(tv[1].tv_sec, &t[1].tv_sec) ||
Ulrich Drepper1c710c82007-05-08 00:33:25 -0700124 get_user(tv[1].tv_nsec, &t[1].tv_usec))
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +1100125 return -EFAULT;
Ulrich Drepper1c710c82007-05-08 00:33:25 -0700126 if (tv[0].tv_nsec >= 1000000 || tv[0].tv_nsec < 0 ||
127 tv[1].tv_nsec >= 1000000 || tv[1].tv_nsec < 0)
128 return -EINVAL;
129 tv[0].tv_nsec *= 1000;
130 tv[1].tv_nsec *= 1000;
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +1100131 }
Ulrich Drepper1c710c82007-05-08 00:33:25 -0700132 return do_utimes(dfd, filename, t ? tv : NULL, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800133}
134
135asmlinkage long compat_sys_utimes(char __user *filename, struct compat_timeval __user *t)
136{
137 return compat_sys_futimesat(AT_FDCWD, filename, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140asmlinkage long compat_sys_newstat(char __user * filename,
141 struct compat_stat __user *statbuf)
142{
143 struct kstat stat;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800144 int error = vfs_stat_fd(AT_FDCWD, filename, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 if (!error)
147 error = cp_compat_stat(&stat, statbuf);
148 return error;
149}
150
151asmlinkage long compat_sys_newlstat(char __user * filename,
152 struct compat_stat __user *statbuf)
153{
154 struct kstat stat;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800155 int error = vfs_lstat_fd(AT_FDCWD, filename, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 if (!error)
158 error = cp_compat_stat(&stat, statbuf);
159 return error;
160}
161
Kyle McMartin82d821d2006-03-24 03:18:20 -0800162#ifndef __ARCH_WANT_STAT64
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +1100163asmlinkage long compat_sys_newfstatat(unsigned int dfd, char __user *filename,
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800164 struct compat_stat __user *statbuf, int flag)
165{
166 struct kstat stat;
167 int error = -EINVAL;
168
169 if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
170 goto out;
171
172 if (flag & AT_SYMLINK_NOFOLLOW)
173 error = vfs_lstat_fd(dfd, filename, &stat);
174 else
175 error = vfs_stat_fd(dfd, filename, &stat);
176
177 if (!error)
178 error = cp_compat_stat(&stat, statbuf);
179
180out:
181 return error;
182}
Kyle McMartin82d821d2006-03-24 03:18:20 -0800183#endif
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185asmlinkage long compat_sys_newfstat(unsigned int fd,
186 struct compat_stat __user * statbuf)
187{
188 struct kstat stat;
189 int error = vfs_fstat(fd, &stat);
190
191 if (!error)
192 error = cp_compat_stat(&stat, statbuf);
193 return error;
194}
195
196static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
197{
198
199 if (sizeof ubuf->f_blocks == 4) {
Jon Tollefsonf4a67cc2008-07-23 21:27:55 -0700200 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail |
201 kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return -EOVERFLOW;
203 /* f_files and f_ffree may be -1; it's okay
204 * to stuff that into 32 bits */
205 if (kbuf->f_files != 0xffffffffffffffffULL
206 && (kbuf->f_files & 0xffffffff00000000ULL))
207 return -EOVERFLOW;
208 if (kbuf->f_ffree != 0xffffffffffffffffULL
209 && (kbuf->f_ffree & 0xffffffff00000000ULL))
210 return -EOVERFLOW;
211 }
212 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
213 __put_user(kbuf->f_type, &ubuf->f_type) ||
214 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
215 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
216 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
217 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
218 __put_user(kbuf->f_files, &ubuf->f_files) ||
219 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
220 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
221 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
222 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
223 __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
224 __put_user(0, &ubuf->f_spare[0]) ||
225 __put_user(0, &ubuf->f_spare[1]) ||
226 __put_user(0, &ubuf->f_spare[2]) ||
227 __put_user(0, &ubuf->f_spare[3]) ||
228 __put_user(0, &ubuf->f_spare[4]))
229 return -EFAULT;
230 return 0;
231}
232
233/*
234 * The following statfs calls are copies of code from fs/open.c and
235 * should be checked against those from time to time
236 */
Al Viro2d8f3032008-07-22 09:59:21 -0400237asmlinkage long compat_sys_statfs(const char __user *pathname, struct compat_statfs __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Al Viro2d8f3032008-07-22 09:59:21 -0400239 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 int error;
241
Al Viro2d8f3032008-07-22 09:59:21 -0400242 error = user_path(pathname, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (!error) {
244 struct kstatfs tmp;
Al Viro2d8f3032008-07-22 09:59:21 -0400245 error = vfs_statfs(path.dentry, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800246 if (!error)
247 error = put_compat_statfs(buf, &tmp);
Al Viro2d8f3032008-07-22 09:59:21 -0400248 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250 return error;
251}
252
253asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf)
254{
255 struct file * file;
256 struct kstatfs tmp;
257 int error;
258
259 error = -EBADF;
260 file = fget(fd);
261 if (!file)
262 goto out;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800263 error = vfs_statfs(file->f_path.dentry, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800264 if (!error)
265 error = put_compat_statfs(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 fput(file);
267out:
268 return error;
269}
270
271static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
272{
273 if (sizeof ubuf->f_blocks == 4) {
Jon Tollefsonf4a67cc2008-07-23 21:27:55 -0700274 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail |
275 kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return -EOVERFLOW;
277 /* f_files and f_ffree may be -1; it's okay
278 * to stuff that into 32 bits */
279 if (kbuf->f_files != 0xffffffffffffffffULL
280 && (kbuf->f_files & 0xffffffff00000000ULL))
281 return -EOVERFLOW;
282 if (kbuf->f_ffree != 0xffffffffffffffffULL
283 && (kbuf->f_ffree & 0xffffffff00000000ULL))
284 return -EOVERFLOW;
285 }
286 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
287 __put_user(kbuf->f_type, &ubuf->f_type) ||
288 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
289 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
290 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
291 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
292 __put_user(kbuf->f_files, &ubuf->f_files) ||
293 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
294 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
295 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
296 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
297 __put_user(kbuf->f_frsize, &ubuf->f_frsize))
298 return -EFAULT;
299 return 0;
300}
301
Al Viro2d8f3032008-07-22 09:59:21 -0400302asmlinkage long compat_sys_statfs64(const char __user *pathname, compat_size_t sz, struct compat_statfs64 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Al Viro2d8f3032008-07-22 09:59:21 -0400304 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 int error;
306
307 if (sz != sizeof(*buf))
308 return -EINVAL;
309
Al Viro2d8f3032008-07-22 09:59:21 -0400310 error = user_path(pathname, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (!error) {
312 struct kstatfs tmp;
Al Viro2d8f3032008-07-22 09:59:21 -0400313 error = vfs_statfs(path.dentry, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800314 if (!error)
315 error = put_compat_statfs64(buf, &tmp);
Al Viro2d8f3032008-07-22 09:59:21 -0400316 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318 return error;
319}
320
321asmlinkage long compat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf)
322{
323 struct file * file;
324 struct kstatfs tmp;
325 int error;
326
327 if (sz != sizeof(*buf))
328 return -EINVAL;
329
330 error = -EBADF;
331 file = fget(fd);
332 if (!file)
333 goto out;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800334 error = vfs_statfs(file->f_path.dentry, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800335 if (!error)
336 error = put_compat_statfs64(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 fput(file);
338out:
339 return error;
340}
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
343{
344 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
345 __get_user(kfl->l_type, &ufl->l_type) ||
346 __get_user(kfl->l_whence, &ufl->l_whence) ||
347 __get_user(kfl->l_start, &ufl->l_start) ||
348 __get_user(kfl->l_len, &ufl->l_len) ||
349 __get_user(kfl->l_pid, &ufl->l_pid))
350 return -EFAULT;
351 return 0;
352}
353
354static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
355{
356 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
357 __put_user(kfl->l_type, &ufl->l_type) ||
358 __put_user(kfl->l_whence, &ufl->l_whence) ||
359 __put_user(kfl->l_start, &ufl->l_start) ||
360 __put_user(kfl->l_len, &ufl->l_len) ||
361 __put_user(kfl->l_pid, &ufl->l_pid))
362 return -EFAULT;
363 return 0;
364}
365
366#ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
367static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
368{
369 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
370 __get_user(kfl->l_type, &ufl->l_type) ||
371 __get_user(kfl->l_whence, &ufl->l_whence) ||
372 __get_user(kfl->l_start, &ufl->l_start) ||
373 __get_user(kfl->l_len, &ufl->l_len) ||
374 __get_user(kfl->l_pid, &ufl->l_pid))
375 return -EFAULT;
376 return 0;
377}
378#endif
379
380#ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
381static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
382{
383 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
384 __put_user(kfl->l_type, &ufl->l_type) ||
385 __put_user(kfl->l_whence, &ufl->l_whence) ||
386 __put_user(kfl->l_start, &ufl->l_start) ||
387 __put_user(kfl->l_len, &ufl->l_len) ||
388 __put_user(kfl->l_pid, &ufl->l_pid))
389 return -EFAULT;
390 return 0;
391}
392#endif
393
394asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd,
395 unsigned long arg)
396{
397 mm_segment_t old_fs;
398 struct flock f;
399 long ret;
400
401 switch (cmd) {
402 case F_GETLK:
403 case F_SETLK:
404 case F_SETLKW:
405 ret = get_compat_flock(&f, compat_ptr(arg));
406 if (ret != 0)
407 break;
408 old_fs = get_fs();
409 set_fs(KERNEL_DS);
410 ret = sys_fcntl(fd, cmd, (unsigned long)&f);
411 set_fs(old_fs);
412 if (cmd == F_GETLK && ret == 0) {
NeilBrown2520f142006-01-08 01:02:40 -0800413 /* GETLK was successfule and we need to return the data...
414 * but it needs to fit in the compat structure.
415 * l_start shouldn't be too big, unless the original
416 * start + end is greater than COMPAT_OFF_T_MAX, in which
417 * case the app was asking for trouble, so we return
418 * -EOVERFLOW in that case.
419 * l_len could be too big, in which case we just truncate it,
420 * and only allow the app to see that part of the conflicting
421 * lock that might make sense to it anyway
422 */
423
424 if (f.l_start > COMPAT_OFF_T_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 ret = -EOVERFLOW;
NeilBrown2520f142006-01-08 01:02:40 -0800426 if (f.l_len > COMPAT_OFF_T_MAX)
427 f.l_len = COMPAT_OFF_T_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (ret == 0)
429 ret = put_compat_flock(&f, compat_ptr(arg));
430 }
431 break;
432
433 case F_GETLK64:
434 case F_SETLK64:
435 case F_SETLKW64:
436 ret = get_compat_flock64(&f, compat_ptr(arg));
437 if (ret != 0)
438 break;
439 old_fs = get_fs();
440 set_fs(KERNEL_DS);
441 ret = sys_fcntl(fd, (cmd == F_GETLK64) ? F_GETLK :
442 ((cmd == F_SETLK64) ? F_SETLK : F_SETLKW),
443 (unsigned long)&f);
444 set_fs(old_fs);
445 if (cmd == F_GETLK64 && ret == 0) {
NeilBrown2520f142006-01-08 01:02:40 -0800446 /* need to return lock information - see above for commentary */
447 if (f.l_start > COMPAT_LOFF_T_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 ret = -EOVERFLOW;
NeilBrown2520f142006-01-08 01:02:40 -0800449 if (f.l_len > COMPAT_LOFF_T_MAX)
450 f.l_len = COMPAT_LOFF_T_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (ret == 0)
452 ret = put_compat_flock64(&f, compat_ptr(arg));
453 }
454 break;
455
456 default:
457 ret = sys_fcntl(fd, cmd, arg);
458 break;
459 }
460 return ret;
461}
462
463asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd,
464 unsigned long arg)
465{
466 if ((cmd == F_GETLK64) || (cmd == F_SETLK64) || (cmd == F_SETLKW64))
467 return -EINVAL;
468 return compat_sys_fcntl64(fd, cmd, arg);
469}
470
471asmlinkage long
472compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p)
473{
474 long ret;
475 aio_context_t ctx64;
476
477 mm_segment_t oldfs = get_fs();
478 if (unlikely(get_user(ctx64, ctx32p)))
479 return -EFAULT;
480
481 set_fs(KERNEL_DS);
482 /* The __user pointer cast is valid because of the set_fs() */
483 ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64);
484 set_fs(oldfs);
485 /* truncating is ok because it's a user address */
486 if (!ret)
487 ret = put_user((u32) ctx64, ctx32p);
488 return ret;
489}
490
491asmlinkage long
492compat_sys_io_getevents(aio_context_t ctx_id,
493 unsigned long min_nr,
494 unsigned long nr,
495 struct io_event __user *events,
496 struct compat_timespec __user *timeout)
497{
498 long ret;
499 struct timespec t;
500 struct timespec __user *ut = NULL;
501
502 ret = -EFAULT;
503 if (unlikely(!access_ok(VERIFY_WRITE, events,
504 nr * sizeof(struct io_event))))
505 goto out;
506 if (timeout) {
507 if (get_compat_timespec(&t, timeout))
508 goto out;
509
510 ut = compat_alloc_user_space(sizeof(*ut));
511 if (copy_to_user(ut, &t, sizeof(t)) )
512 goto out;
513 }
514 ret = sys_io_getevents(ctx_id, min_nr, nr, events, ut);
515out:
516 return ret;
517}
518
519static inline long
520copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64)
521{
522 compat_uptr_t uptr;
523 int i;
524
525 for (i = 0; i < nr; ++i) {
526 if (get_user(uptr, ptr32 + i))
527 return -EFAULT;
528 if (put_user(compat_ptr(uptr), ptr64 + i))
529 return -EFAULT;
530 }
531 return 0;
532}
533
534#define MAX_AIO_SUBMITS (PAGE_SIZE/sizeof(struct iocb *))
535
536asmlinkage long
537compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb)
538{
539 struct iocb __user * __user *iocb64;
540 long ret;
541
542 if (unlikely(nr < 0))
543 return -EINVAL;
544
545 if (nr > MAX_AIO_SUBMITS)
546 nr = MAX_AIO_SUBMITS;
547
548 iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64));
549 ret = copy_iocb(nr, iocb, iocb64);
550 if (!ret)
551 ret = sys_io_submit(ctx_id, nr, iocb64);
552 return ret;
553}
554
555struct compat_ncp_mount_data {
556 compat_int_t version;
557 compat_uint_t ncp_fd;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700558 __compat_uid_t mounted_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 compat_pid_t wdog_pid;
560 unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
561 compat_uint_t time_out;
562 compat_uint_t retry_count;
563 compat_uint_t flags;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700564 __compat_uid_t uid;
565 __compat_gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 compat_mode_t file_mode;
567 compat_mode_t dir_mode;
568};
569
570struct compat_ncp_mount_data_v4 {
571 compat_int_t version;
572 compat_ulong_t flags;
573 compat_ulong_t mounted_uid;
574 compat_long_t wdog_pid;
575 compat_uint_t ncp_fd;
576 compat_uint_t time_out;
577 compat_uint_t retry_count;
578 compat_ulong_t uid;
579 compat_ulong_t gid;
580 compat_ulong_t file_mode;
581 compat_ulong_t dir_mode;
582};
583
584static void *do_ncp_super_data_conv(void *raw_data)
585{
586 int version = *(unsigned int *)raw_data;
587
588 if (version == 3) {
589 struct compat_ncp_mount_data *c_n = raw_data;
590 struct ncp_mount_data *n = raw_data;
591
592 n->dir_mode = c_n->dir_mode;
593 n->file_mode = c_n->file_mode;
594 n->gid = c_n->gid;
595 n->uid = c_n->uid;
596 memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
597 n->wdog_pid = c_n->wdog_pid;
598 n->mounted_uid = c_n->mounted_uid;
599 } else if (version == 4) {
600 struct compat_ncp_mount_data_v4 *c_n = raw_data;
601 struct ncp_mount_data_v4 *n = raw_data;
602
603 n->dir_mode = c_n->dir_mode;
604 n->file_mode = c_n->file_mode;
605 n->gid = c_n->gid;
606 n->uid = c_n->uid;
607 n->retry_count = c_n->retry_count;
608 n->time_out = c_n->time_out;
609 n->ncp_fd = c_n->ncp_fd;
610 n->wdog_pid = c_n->wdog_pid;
611 n->mounted_uid = c_n->mounted_uid;
612 n->flags = c_n->flags;
613 } else if (version != 5) {
614 return NULL;
615 }
616
617 return raw_data;
618}
619
620struct compat_smb_mount_data {
621 compat_int_t version;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700622 __compat_uid_t mounted_uid;
623 __compat_uid_t uid;
624 __compat_gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 compat_mode_t file_mode;
626 compat_mode_t dir_mode;
627};
628
629static void *do_smb_super_data_conv(void *raw_data)
630{
631 struct smb_mount_data *s = raw_data;
632 struct compat_smb_mount_data *c_s = raw_data;
633
634 if (c_s->version != SMB_MOUNT_OLDVERSION)
635 goto out;
636 s->dir_mode = c_s->dir_mode;
637 s->file_mode = c_s->file_mode;
638 s->gid = c_s->gid;
639 s->uid = c_s->uid;
640 s->mounted_uid = c_s->mounted_uid;
641 out:
642 return raw_data;
643}
644
David Howells9a9947b2005-04-18 10:54:51 -0700645struct compat_nfs_string {
646 compat_uint_t len;
David Howells5fc3e622005-04-27 15:39:03 -0700647 compat_uptr_t data;
David Howells9a9947b2005-04-18 10:54:51 -0700648};
649
650static inline void compat_nfs_string(struct nfs_string *dst,
651 struct compat_nfs_string *src)
652{
653 dst->data = compat_ptr(src->data);
654 dst->len = src->len;
655}
656
657struct compat_nfs4_mount_data_v1 {
658 compat_int_t version;
659 compat_int_t flags;
660 compat_int_t rsize;
661 compat_int_t wsize;
662 compat_int_t timeo;
663 compat_int_t retrans;
664 compat_int_t acregmin;
665 compat_int_t acregmax;
666 compat_int_t acdirmin;
667 compat_int_t acdirmax;
668 struct compat_nfs_string client_addr;
669 struct compat_nfs_string mnt_path;
670 struct compat_nfs_string hostname;
671 compat_uint_t host_addrlen;
David Howells5fc3e622005-04-27 15:39:03 -0700672 compat_uptr_t host_addr;
David Howells9a9947b2005-04-18 10:54:51 -0700673 compat_int_t proto;
674 compat_int_t auth_flavourlen;
David Howells5fc3e622005-04-27 15:39:03 -0700675 compat_uptr_t auth_flavours;
David Howells9a9947b2005-04-18 10:54:51 -0700676};
677
678static int do_nfs4_super_data_conv(void *raw_data)
679{
680 int version = *(compat_uint_t *) raw_data;
681
682 if (version == 1) {
683 struct compat_nfs4_mount_data_v1 *raw = raw_data;
684 struct nfs4_mount_data *real = raw_data;
685
686 /* copy the fields backwards */
687 real->auth_flavours = compat_ptr(raw->auth_flavours);
688 real->auth_flavourlen = raw->auth_flavourlen;
689 real->proto = raw->proto;
690 real->host_addr = compat_ptr(raw->host_addr);
691 real->host_addrlen = raw->host_addrlen;
692 compat_nfs_string(&real->hostname, &raw->hostname);
693 compat_nfs_string(&real->mnt_path, &raw->mnt_path);
694 compat_nfs_string(&real->client_addr, &raw->client_addr);
695 real->acdirmax = raw->acdirmax;
696 real->acdirmin = raw->acdirmin;
697 real->acregmax = raw->acregmax;
698 real->acregmin = raw->acregmin;
699 real->retrans = raw->retrans;
700 real->timeo = raw->timeo;
701 real->wsize = raw->wsize;
702 real->rsize = raw->rsize;
703 real->flags = raw->flags;
704 real->version = raw->version;
705 }
David Howells9a9947b2005-04-18 10:54:51 -0700706
707 return 0;
708}
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710#define SMBFS_NAME "smbfs"
711#define NCPFS_NAME "ncpfs"
David Howells9a9947b2005-04-18 10:54:51 -0700712#define NFS4_NAME "nfs4"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714asmlinkage long compat_sys_mount(char __user * dev_name, char __user * dir_name,
715 char __user * type, unsigned long flags,
716 void __user * data)
717{
718 unsigned long type_page;
719 unsigned long data_page;
720 unsigned long dev_page;
721 char *dir_page;
722 int retval;
723
724 retval = copy_mount_options (type, &type_page);
725 if (retval < 0)
726 goto out;
727
728 dir_page = getname(dir_name);
729 retval = PTR_ERR(dir_page);
730 if (IS_ERR(dir_page))
731 goto out1;
732
733 retval = copy_mount_options (dev_name, &dev_page);
734 if (retval < 0)
735 goto out2;
736
737 retval = copy_mount_options (data, &data_page);
738 if (retval < 0)
739 goto out3;
740
741 retval = -EINVAL;
742
Andrey Mirkin822191a2006-12-06 20:31:35 -0800743 if (type_page && data_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (!strcmp((char *)type_page, SMBFS_NAME)) {
745 do_smb_super_data_conv((void *)data_page);
746 } else if (!strcmp((char *)type_page, NCPFS_NAME)) {
747 do_ncp_super_data_conv((void *)data_page);
David Howells9a9947b2005-04-18 10:54:51 -0700748 } else if (!strcmp((char *)type_page, NFS4_NAME)) {
749 if (do_nfs4_super_data_conv((void *) data_page))
750 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752 }
753
754 lock_kernel();
755 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
756 flags, (void*)data_page);
757 unlock_kernel();
758
David Howells9a9947b2005-04-18 10:54:51 -0700759 out4:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 free_page(data_page);
761 out3:
762 free_page(dev_page);
763 out2:
764 putname(dir_page);
765 out1:
766 free_page(type_page);
767 out:
768 return retval;
769}
770
771#define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773struct compat_old_linux_dirent {
774 compat_ulong_t d_ino;
775 compat_ulong_t d_offset;
776 unsigned short d_namlen;
777 char d_name[1];
778};
779
780struct compat_readdir_callback {
781 struct compat_old_linux_dirent __user *dirent;
782 int result;
783};
784
785static int compat_fillonedir(void *__buf, const char *name, int namlen,
David Howellsafefdbb2006-10-03 01:13:46 -0700786 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 struct compat_readdir_callback *buf = __buf;
789 struct compat_old_linux_dirent __user *dirent;
David Howellsafefdbb2006-10-03 01:13:46 -0700790 compat_ulong_t d_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 if (buf->result)
793 return -EINVAL;
David Howellsafefdbb2006-10-03 01:13:46 -0700794 d_ino = ino;
Al Viro8f3f6552008-08-12 00:28:24 -0400795 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
796 buf->result = -EOVERFLOW;
David Howellsafefdbb2006-10-03 01:13:46 -0700797 return -EOVERFLOW;
Al Viro8f3f6552008-08-12 00:28:24 -0400798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 buf->result++;
800 dirent = buf->dirent;
801 if (!access_ok(VERIFY_WRITE, dirent,
802 (unsigned long)(dirent->d_name + namlen + 1) -
803 (unsigned long)dirent))
804 goto efault;
David Howellsafefdbb2006-10-03 01:13:46 -0700805 if ( __put_user(d_ino, &dirent->d_ino) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 __put_user(offset, &dirent->d_offset) ||
807 __put_user(namlen, &dirent->d_namlen) ||
808 __copy_to_user(dirent->d_name, name, namlen) ||
809 __put_user(0, dirent->d_name + namlen))
810 goto efault;
811 return 0;
812efault:
813 buf->result = -EFAULT;
814 return -EFAULT;
815}
816
817asmlinkage long compat_sys_old_readdir(unsigned int fd,
818 struct compat_old_linux_dirent __user *dirent, unsigned int count)
819{
820 int error;
821 struct file *file;
822 struct compat_readdir_callback buf;
823
824 error = -EBADF;
825 file = fget(fd);
826 if (!file)
827 goto out;
828
829 buf.result = 0;
830 buf.dirent = dirent;
831
832 error = vfs_readdir(file, compat_fillonedir, &buf);
833 if (error >= 0)
834 error = buf.result;
835
836 fput(file);
837out:
838 return error;
839}
840
841struct compat_linux_dirent {
842 compat_ulong_t d_ino;
843 compat_ulong_t d_off;
844 unsigned short d_reclen;
845 char d_name[1];
846};
847
848struct compat_getdents_callback {
849 struct compat_linux_dirent __user *current_dir;
850 struct compat_linux_dirent __user *previous;
851 int count;
852 int error;
853};
854
855static int compat_filldir(void *__buf, const char *name, int namlen,
David Howellsafefdbb2006-10-03 01:13:46 -0700856 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
858 struct compat_linux_dirent __user * dirent;
859 struct compat_getdents_callback *buf = __buf;
David Howellsafefdbb2006-10-03 01:13:46 -0700860 compat_ulong_t d_ino;
Milind Arun Choudhary022a1692007-05-08 00:29:02 -0700861 int reclen = ALIGN(NAME_OFFSET(dirent) + namlen + 2, sizeof(compat_long_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 buf->error = -EINVAL; /* only used if we fail.. */
864 if (reclen > buf->count)
865 return -EINVAL;
David Howellsafefdbb2006-10-03 01:13:46 -0700866 d_ino = ino;
Al Viro8f3f6552008-08-12 00:28:24 -0400867 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
868 buf->error = -EOVERFLOW;
David Howellsafefdbb2006-10-03 01:13:46 -0700869 return -EOVERFLOW;
Al Viro8f3f6552008-08-12 00:28:24 -0400870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 dirent = buf->previous;
872 if (dirent) {
873 if (__put_user(offset, &dirent->d_off))
874 goto efault;
875 }
876 dirent = buf->current_dir;
David Howellsafefdbb2006-10-03 01:13:46 -0700877 if (__put_user(d_ino, &dirent->d_ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 goto efault;
879 if (__put_user(reclen, &dirent->d_reclen))
880 goto efault;
881 if (copy_to_user(dirent->d_name, name, namlen))
882 goto efault;
883 if (__put_user(0, dirent->d_name + namlen))
884 goto efault;
885 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
886 goto efault;
887 buf->previous = dirent;
888 dirent = (void __user *)dirent + reclen;
889 buf->current_dir = dirent;
890 buf->count -= reclen;
891 return 0;
892efault:
893 buf->error = -EFAULT;
894 return -EFAULT;
895}
896
897asmlinkage long compat_sys_getdents(unsigned int fd,
898 struct compat_linux_dirent __user *dirent, unsigned int count)
899{
900 struct file * file;
901 struct compat_linux_dirent __user * lastdirent;
902 struct compat_getdents_callback buf;
903 int error;
904
905 error = -EFAULT;
906 if (!access_ok(VERIFY_WRITE, dirent, count))
907 goto out;
908
909 error = -EBADF;
910 file = fget(fd);
911 if (!file)
912 goto out;
913
914 buf.current_dir = dirent;
915 buf.previous = NULL;
916 buf.count = count;
917 buf.error = 0;
918
919 error = vfs_readdir(file, compat_filldir, &buf);
920 if (error < 0)
921 goto out_putf;
922 error = buf.error;
923 lastdirent = buf.previous;
924 if (lastdirent) {
925 if (put_user(file->f_pos, &lastdirent->d_off))
926 error = -EFAULT;
927 else
928 error = count - buf.count;
929 }
930
931out_putf:
932 fput(file);
933out:
934 return error;
935}
936
937#ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939struct compat_getdents_callback64 {
940 struct linux_dirent64 __user *current_dir;
941 struct linux_dirent64 __user *previous;
942 int count;
943 int error;
944};
945
946static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset,
David Howellsafefdbb2006-10-03 01:13:46 -0700947 u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
949 struct linux_dirent64 __user *dirent;
950 struct compat_getdents_callback64 *buf = __buf;
951 int jj = NAME_OFFSET(dirent);
Milind Arun Choudhary022a1692007-05-08 00:29:02 -0700952 int reclen = ALIGN(jj + namlen + 1, sizeof(u64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 u64 off;
954
955 buf->error = -EINVAL; /* only used if we fail.. */
956 if (reclen > buf->count)
957 return -EINVAL;
958 dirent = buf->previous;
959
960 if (dirent) {
961 if (__put_user_unaligned(offset, &dirent->d_off))
962 goto efault;
963 }
964 dirent = buf->current_dir;
965 if (__put_user_unaligned(ino, &dirent->d_ino))
966 goto efault;
967 off = 0;
968 if (__put_user_unaligned(off, &dirent->d_off))
969 goto efault;
970 if (__put_user(reclen, &dirent->d_reclen))
971 goto efault;
972 if (__put_user(d_type, &dirent->d_type))
973 goto efault;
974 if (copy_to_user(dirent->d_name, name, namlen))
975 goto efault;
976 if (__put_user(0, dirent->d_name + namlen))
977 goto efault;
978 buf->previous = dirent;
979 dirent = (void __user *)dirent + reclen;
980 buf->current_dir = dirent;
981 buf->count -= reclen;
982 return 0;
983efault:
984 buf->error = -EFAULT;
985 return -EFAULT;
986}
987
988asmlinkage long compat_sys_getdents64(unsigned int fd,
989 struct linux_dirent64 __user * dirent, unsigned int count)
990{
991 struct file * file;
992 struct linux_dirent64 __user * lastdirent;
993 struct compat_getdents_callback64 buf;
994 int error;
995
996 error = -EFAULT;
997 if (!access_ok(VERIFY_WRITE, dirent, count))
998 goto out;
999
1000 error = -EBADF;
1001 file = fget(fd);
1002 if (!file)
1003 goto out;
1004
1005 buf.current_dir = dirent;
1006 buf.previous = NULL;
1007 buf.count = count;
1008 buf.error = 0;
1009
1010 error = vfs_readdir(file, compat_filldir64, &buf);
1011 if (error < 0)
1012 goto out_putf;
1013 error = buf.error;
1014 lastdirent = buf.previous;
1015 if (lastdirent) {
1016 typeof(lastdirent->d_off) d_off = file->f_pos;
Heiko Carstens7116e992006-12-06 20:36:36 -08001017 error = -EFAULT;
1018 if (__put_user_unaligned(d_off, &lastdirent->d_off))
1019 goto out_putf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 error = count - buf.count;
1021 }
1022
1023out_putf:
1024 fput(file);
1025out:
1026 return error;
1027}
1028#endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
1029
1030static ssize_t compat_do_readv_writev(int type, struct file *file,
1031 const struct compat_iovec __user *uvector,
1032 unsigned long nr_segs, loff_t *pos)
1033{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 compat_ssize_t tot_len;
1035 struct iovec iovstack[UIO_FASTIOV];
1036 struct iovec *iov=iovstack, *vector;
1037 ssize_t ret;
1038 int seg;
1039 io_fn_t fn;
1040 iov_fn_t fnv;
1041
1042 /*
1043 * SuS says "The readv() function *may* fail if the iovcnt argument
1044 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
1045 * traditionally returned zero for zero segments, so...
1046 */
1047 ret = 0;
1048 if (nr_segs == 0)
1049 goto out;
1050
1051 /*
1052 * First get the "struct iovec" from user memory and
1053 * verify all the pointers
1054 */
1055 ret = -EINVAL;
1056 if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0))
1057 goto out;
1058 if (!file->f_op)
1059 goto out;
1060 if (nr_segs > UIO_FASTIOV) {
1061 ret = -ENOMEM;
1062 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
1063 if (!iov)
1064 goto out;
1065 }
1066 ret = -EFAULT;
1067 if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
1068 goto out;
1069
1070 /*
1071 * Single unix specification:
1072 * We should -EINVAL if an element length is not >= 0 and fitting an
1073 * ssize_t. The total length is fitting an ssize_t
1074 *
1075 * Be careful here because iov_len is a size_t not an ssize_t
1076 */
1077 tot_len = 0;
1078 vector = iov;
1079 ret = -EINVAL;
1080 for (seg = 0 ; seg < nr_segs; seg++) {
1081 compat_ssize_t tmp = tot_len;
1082 compat_ssize_t len;
1083 compat_uptr_t buf;
1084
1085 if (__get_user(len, &uvector->iov_len) ||
1086 __get_user(buf, &uvector->iov_base)) {
1087 ret = -EFAULT;
1088 goto out;
1089 }
1090 if (len < 0) /* size_t not fitting an compat_ssize_t .. */
1091 goto out;
1092 tot_len += len;
1093 if (tot_len < tmp) /* maths overflow on the compat_ssize_t */
1094 goto out;
1095 vector->iov_base = compat_ptr(buf);
1096 vector->iov_len = (compat_size_t) len;
1097 uvector++;
1098 vector++;
1099 }
1100 if (tot_len == 0) {
1101 ret = 0;
1102 goto out;
1103 }
1104
1105 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001106 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 goto out;
1108
1109 fnv = NULL;
1110 if (type == READ) {
1111 fn = file->f_op->read;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001112 fnv = file->f_op->aio_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 } else {
1114 fn = (io_fn_t)file->f_op->write;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001115 fnv = file->f_op->aio_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
1117
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001118 if (fnv)
1119 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
1120 pos, fnv);
1121 else
1122 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124out:
1125 if (iov != iovstack)
1126 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -04001127 if ((ret + (type == READ)) > 0) {
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001128 struct dentry *dentry = file->f_path.dentry;
Robert Love0eeca282005-07-12 17:06:03 -04001129 if (type == READ)
1130 fsnotify_access(dentry);
1131 else
1132 fsnotify_modify(dentry);
1133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return ret;
1135}
1136
1137asmlinkage ssize_t
1138compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1139{
1140 struct file *file;
1141 ssize_t ret = -EBADF;
1142
1143 file = fget(fd);
1144 if (!file)
1145 return -EBADF;
1146
1147 if (!(file->f_mode & FMODE_READ))
1148 goto out;
1149
1150 ret = -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001151 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 goto out;
1153
1154 ret = compat_do_readv_writev(READ, file, vec, vlen, &file->f_pos);
1155
1156out:
1157 fput(file);
1158 return ret;
1159}
1160
1161asmlinkage ssize_t
1162compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1163{
1164 struct file *file;
1165 ssize_t ret = -EBADF;
1166
1167 file = fget(fd);
1168 if (!file)
1169 return -EBADF;
1170 if (!(file->f_mode & FMODE_WRITE))
1171 goto out;
1172
1173 ret = -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001174 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 goto out;
1176
1177 ret = compat_do_readv_writev(WRITE, file, vec, vlen, &file->f_pos);
1178
1179out:
1180 fput(file);
1181 return ret;
1182}
1183
Andi Kleend2610202006-05-01 12:15:48 -07001184asmlinkage long
1185compat_sys_vmsplice(int fd, const struct compat_iovec __user *iov32,
1186 unsigned int nr_segs, unsigned int flags)
1187{
1188 unsigned i;
Al Viro90cbad62006-10-10 22:44:17 +01001189 struct iovec __user *iov;
Jens Axboe98232d52006-05-04 09:13:49 +02001190 if (nr_segs > UIO_MAXIOV)
Andi Kleend2610202006-05-01 12:15:48 -07001191 return -EINVAL;
1192 iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
1193 for (i = 0; i < nr_segs; i++) {
1194 struct compat_iovec v;
1195 if (get_user(v.iov_base, &iov32[i].iov_base) ||
1196 get_user(v.iov_len, &iov32[i].iov_len) ||
1197 put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
1198 put_user(v.iov_len, &iov[i].iov_len))
1199 return -EFAULT;
1200 }
1201 return sys_vmsplice(fd, iov, nr_segs, flags);
1202}
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204/*
Miklos Szeredie922efc2005-09-06 15:18:25 -07001205 * Exactly like fs/open.c:sys_open(), except that it doesn't set the
1206 * O_LARGEFILE flag.
1207 */
1208asmlinkage long
1209compat_sys_open(const char __user *filename, int flags, int mode)
1210{
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001211 return do_sys_open(AT_FDCWD, filename, flags, mode);
1212}
1213
1214/*
1215 * Exactly like fs/open.c:sys_openat(), except that it doesn't set the
1216 * O_LARGEFILE flag.
1217 */
1218asmlinkage long
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +11001219compat_sys_openat(unsigned int dfd, const char __user *filename, int flags, int mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001220{
1221 return do_sys_open(dfd, filename, flags, mode);
Miklos Szeredie922efc2005-09-06 15:18:25 -07001222}
1223
1224/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 * compat_count() counts the number of arguments/envelopes. It is basically
1226 * a copy of count() from fs/exec.c, except that it works with 32 bit argv
1227 * and envp pointers.
1228 */
1229static int compat_count(compat_uptr_t __user *argv, int max)
1230{
1231 int i = 0;
1232
1233 if (argv != NULL) {
1234 for (;;) {
1235 compat_uptr_t p;
1236
1237 if (get_user(p, argv))
1238 return -EFAULT;
1239 if (!p)
1240 break;
1241 argv++;
1242 if(++i > max)
1243 return -E2BIG;
1244 }
1245 }
1246 return i;
1247}
1248
1249/*
1250 * compat_copy_strings() is basically a copy of copy_strings() from fs/exec.c
1251 * except that it works with 32 bit argv and envp pointers.
1252 */
1253static int compat_copy_strings(int argc, compat_uptr_t __user *argv,
1254 struct linux_binprm *bprm)
1255{
1256 struct page *kmapped_page = NULL;
1257 char *kaddr = NULL;
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001258 unsigned long kpos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 int ret;
1260
1261 while (argc-- > 0) {
1262 compat_uptr_t str;
1263 int len;
1264 unsigned long pos;
1265
1266 if (get_user(str, argv+argc) ||
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001267 !(len = strnlen_user(compat_ptr(str), MAX_ARG_STRLEN))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 ret = -EFAULT;
1269 goto out;
1270 }
1271
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001272 if (len > MAX_ARG_STRLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 ret = -E2BIG;
1274 goto out;
1275 }
1276
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001277 /* We're going to work our way backwords. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 pos = bprm->p;
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001279 str += len;
1280 bprm->p -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 while (len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 int offset, bytes_to_copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 offset = pos % PAGE_SIZE;
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001286 if (offset == 0)
1287 offset = PAGE_SIZE;
1288
1289 bytes_to_copy = offset;
1290 if (bytes_to_copy > len)
1291 bytes_to_copy = len;
1292
1293 offset -= bytes_to_copy;
1294 pos -= bytes_to_copy;
1295 str -= bytes_to_copy;
1296 len -= bytes_to_copy;
1297
1298 if (!kmapped_page || kpos != (pos & PAGE_MASK)) {
1299 struct page *page;
1300
1301#ifdef CONFIG_STACK_GROWSUP
1302 ret = expand_stack_downwards(bprm->vma, pos);
1303 if (ret < 0) {
1304 /* We've exceed the stack rlimit. */
1305 ret = -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 goto out;
1307 }
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001308#endif
1309 ret = get_user_pages(current, bprm->mm, pos,
1310 1, 1, 1, &page, NULL);
1311 if (ret <= 0) {
1312 /* We've exceed the stack rlimit. */
1313 ret = -E2BIG;
1314 goto out;
1315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001317 if (kmapped_page) {
1318 flush_kernel_dcache_page(kmapped_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 kunmap(kmapped_page);
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001320 put_page(kmapped_page);
1321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 kmapped_page = page;
1323 kaddr = kmap(kmapped_page);
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001324 kpos = pos & PAGE_MASK;
1325 flush_cache_page(bprm->vma, kpos,
1326 page_to_pfn(kmapped_page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001328 if (copy_from_user(kaddr+offset, compat_ptr(str),
1329 bytes_to_copy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 ret = -EFAULT;
1331 goto out;
1332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
1334 }
1335 ret = 0;
1336out:
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001337 if (kmapped_page) {
1338 flush_kernel_dcache_page(kmapped_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 kunmap(kmapped_page);
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001340 put_page(kmapped_page);
1341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return ret;
1343}
1344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345/*
1346 * compat_do_execve() is mostly a copy of do_execve(), with the exception
1347 * that it processes 32 bit argv and envp pointers.
1348 */
1349int compat_do_execve(char * filename,
1350 compat_uptr_t __user *argv,
1351 compat_uptr_t __user *envp,
1352 struct pt_regs * regs)
1353{
1354 struct linux_binprm *bprm;
1355 struct file *file;
1356 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 retval = -ENOMEM;
Oliver Neukum11b0b5a2006-03-25 03:08:13 -08001359 bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (!bprm)
1361 goto out_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 file = open_exec(filename);
1364 retval = PTR_ERR(file);
1365 if (IS_ERR(file))
1366 goto out_kfree;
1367
1368 sched_exec();
1369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 bprm->file = file;
1371 bprm->filename = filename;
1372 bprm->interp = filename;
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001373
1374 retval = bprm_mm_init(bprm);
1375 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 goto out_file;
1377
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001378 bprm->argc = compat_count(argv, MAX_ARG_STRINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if ((retval = bprm->argc) < 0)
1380 goto out_mm;
1381
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001382 bprm->envc = compat_count(envp, MAX_ARG_STRINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if ((retval = bprm->envc) < 0)
1384 goto out_mm;
1385
1386 retval = security_bprm_alloc(bprm);
1387 if (retval)
1388 goto out;
1389
1390 retval = prepare_binprm(bprm);
1391 if (retval < 0)
1392 goto out;
1393
1394 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1395 if (retval < 0)
1396 goto out;
1397
1398 bprm->exec = bprm->p;
1399 retval = compat_copy_strings(bprm->envc, envp, bprm);
1400 if (retval < 0)
1401 goto out;
1402
1403 retval = compat_copy_strings(bprm->argc, argv, bprm);
1404 if (retval < 0)
1405 goto out;
1406
1407 retval = search_binary_handler(bprm, regs);
1408 if (retval >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 /* execve success */
1410 security_bprm_free(bprm);
David S. Miller4a805e82005-09-14 21:40:00 -07001411 acct_update_integrals(current);
Al Viro08a6fac2008-05-10 16:38:25 -04001412 free_bprm(bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return retval;
1414 }
1415
1416out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (bprm->security)
1418 security_bprm_free(bprm);
1419
1420out_mm:
1421 if (bprm->mm)
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001422 mmput(bprm->mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424out_file:
1425 if (bprm->file) {
1426 allow_write_access(bprm->file);
1427 fput(bprm->file);
1428 }
1429
1430out_kfree:
Al Viro08a6fac2008-05-10 16:38:25 -04001431 free_bprm(bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433out_ret:
1434 return retval;
1435}
1436
1437#define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
1438
Thomas Gleixnerb773ad42008-08-31 08:16:57 -07001439static int poll_select_copy_remaining(struct timespec *end_time, void __user *p,
1440 int timeval, int ret)
1441{
1442 struct timespec ts;
1443
1444 if (!p)
1445 return ret;
1446
1447 if (current->personality & STICKY_TIMEOUTS)
1448 goto sticky;
1449
1450 /* No update for zero timeout */
1451 if (!end_time->tv_sec && !end_time->tv_nsec)
1452 return ret;
1453
1454 ktime_get_ts(&ts);
1455 ts = timespec_sub(*end_time, ts);
1456 if (ts.tv_sec < 0)
1457 ts.tv_sec = ts.tv_nsec = 0;
1458
1459 if (timeval) {
1460 struct compat_timeval rtv;
1461
1462 rtv.tv_sec = ts.tv_sec;
1463 rtv.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
1464
1465 if (!copy_to_user(p, &rtv, sizeof(rtv)))
1466 return ret;
1467 } else {
1468 struct compat_timespec rts;
1469
1470 rts.tv_sec = ts.tv_sec;
1471 rts.tv_nsec = ts.tv_nsec;
1472
1473 if (!copy_to_user(p, &rts, sizeof(rts)))
1474 return ret;
1475 }
1476 /*
1477 * If an application puts its timeval in read-only memory, we
1478 * don't want the Linux-specific update to the timeval to
1479 * cause a fault after the select has completed
1480 * successfully. However, because we're not updating the
1481 * timeval, we can't restart the system call.
1482 */
1483
1484sticky:
1485 if (ret == -ERESTARTNOHAND)
1486 ret = -EINTR;
1487 return ret;
1488}
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490/*
1491 * Ooo, nasty. We need here to frob 32-bit unsigned longs to
1492 * 64-bit unsigned longs.
1493 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001494static
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1496 unsigned long *fdset)
1497{
Milind Arun Choudhary022a1692007-05-08 00:29:02 -07001498 nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (ufdset) {
1500 unsigned long odd;
1501
1502 if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
1503 return -EFAULT;
1504
1505 odd = nr & 1UL;
1506 nr &= ~1UL;
1507 while (nr) {
1508 unsigned long h, l;
Heiko Carstens7116e992006-12-06 20:36:36 -08001509 if (__get_user(l, ufdset) || __get_user(h, ufdset+1))
1510 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 ufdset += 2;
1512 *fdset++ = h << 32 | l;
1513 nr -= 2;
1514 }
Heiko Carstens7116e992006-12-06 20:36:36 -08001515 if (odd && __get_user(*fdset, ufdset))
1516 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 } else {
1518 /* Tricky, must clear full unsigned long in the
1519 * kernel fdset at the end, this makes sure that
1520 * actually happens.
1521 */
1522 memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
1523 }
1524 return 0;
1525}
1526
Arjan van de Ven858119e2006-01-14 13:20:43 -08001527static
Heiko Carstens7116e992006-12-06 20:36:36 -08001528int compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1529 unsigned long *fdset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
1531 unsigned long odd;
Milind Arun Choudhary022a1692007-05-08 00:29:02 -07001532 nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 if (!ufdset)
Heiko Carstens7116e992006-12-06 20:36:36 -08001535 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 odd = nr & 1UL;
1538 nr &= ~1UL;
1539 while (nr) {
1540 unsigned long h, l;
1541 l = *fdset++;
1542 h = l >> 32;
Heiko Carstens7116e992006-12-06 20:36:36 -08001543 if (__put_user(l, ufdset) || __put_user(h, ufdset+1))
1544 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 ufdset += 2;
1546 nr -= 2;
1547 }
Heiko Carstens7116e992006-12-06 20:36:36 -08001548 if (odd && __put_user(*fdset, ufdset))
1549 return -EFAULT;
1550 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
1553
1554/*
1555 * This is a virtual copy of sys_select from fs/select.c and probably
1556 * should be compared to it from time to time
1557 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559/*
1560 * We can actually return ERESTARTSYS instead of EINTR, but I'd
1561 * like to be certain this leads to no problems. So I return
1562 * EINTR just for safety.
1563 *
1564 * Update: ERESTARTSYS breaks at least the xview clock binary, so
1565 * I'm trying ERESTARTNOHAND which restart only when you want to.
1566 */
1567#define MAX_SELECT_SECONDS \
1568 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
1569
David Woodhouse9f729492006-01-18 17:44:05 -08001570int compat_core_sys_select(int n, compat_ulong_t __user *inp,
Arjan van de Ven8ff3e8e2008-08-31 08:26:40 -07001571 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1572 struct timespec *end_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
1574 fd_set_bits fds;
Badari Pulavarty6087b2d2007-05-23 13:57:45 -07001575 void *bits;
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08001576 int size, max_fds, ret = -EINVAL;
Linus Torvaldsa4531ed2005-09-09 15:10:52 -07001577 struct fdtable *fdt;
Badari Pulavarty6087b2d2007-05-23 13:57:45 -07001578 long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 if (n < 0)
1581 goto out_nofds;
1582
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08001583 /* max_fds can increase, so grab it once to avoid race */
Linus Torvaldsac5b8b62005-09-09 15:42:34 -07001584 rcu_read_lock();
Linus Torvaldsa4531ed2005-09-09 15:10:52 -07001585 fdt = files_fdtable(current->files);
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08001586 max_fds = fdt->max_fds;
Linus Torvaldsac5b8b62005-09-09 15:42:34 -07001587 rcu_read_unlock();
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08001588 if (n > max_fds)
1589 n = max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 /*
1592 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
1593 * since we used fdset we need to allocate memory in units of
1594 * long-words.
1595 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 size = FDS_BYTES(n);
Badari Pulavarty6087b2d2007-05-23 13:57:45 -07001597 bits = stack_fds;
1598 if (size > sizeof(stack_fds) / 6) {
1599 bits = kmalloc(6 * size, GFP_KERNEL);
1600 ret = -ENOMEM;
1601 if (!bits)
1602 goto out_nofds;
1603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 fds.in = (unsigned long *) bits;
1605 fds.out = (unsigned long *) (bits + size);
1606 fds.ex = (unsigned long *) (bits + 2*size);
1607 fds.res_in = (unsigned long *) (bits + 3*size);
1608 fds.res_out = (unsigned long *) (bits + 4*size);
1609 fds.res_ex = (unsigned long *) (bits + 5*size);
1610
1611 if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
1612 (ret = compat_get_fd_set(n, outp, fds.out)) ||
1613 (ret = compat_get_fd_set(n, exp, fds.ex)))
1614 goto out;
1615 zero_fd_set(n, fds.res_in);
1616 zero_fd_set(n, fds.res_out);
1617 zero_fd_set(n, fds.res_ex);
1618
Arjan van de Ven8ff3e8e2008-08-31 08:26:40 -07001619 ret = do_select(n, &fds, end_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 if (ret < 0)
1622 goto out;
1623 if (!ret) {
1624 ret = -ERESTARTNOHAND;
1625 if (signal_pending(current))
1626 goto out;
1627 ret = 0;
1628 }
1629
Heiko Carstens7116e992006-12-06 20:36:36 -08001630 if (compat_set_fd_set(n, inp, fds.res_in) ||
1631 compat_set_fd_set(n, outp, fds.res_out) ||
1632 compat_set_fd_set(n, exp, fds.res_ex))
1633 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634out:
Badari Pulavarty6087b2d2007-05-23 13:57:45 -07001635 if (bits != stack_fds)
1636 kfree(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637out_nofds:
1638 return ret;
1639}
1640
David Woodhouse9f729492006-01-18 17:44:05 -08001641asmlinkage long compat_sys_select(int n, compat_ulong_t __user *inp,
1642 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1643 struct compat_timeval __user *tvp)
1644{
Arjan van de Ven8ff3e8e2008-08-31 08:26:40 -07001645 struct timespec end_time, *to = NULL;
David Woodhouse9f729492006-01-18 17:44:05 -08001646 struct compat_timeval tv;
1647 int ret;
1648
1649 if (tvp) {
1650 if (copy_from_user(&tv, tvp, sizeof(tv)))
1651 return -EFAULT;
1652
Arjan van de Ven8ff3e8e2008-08-31 08:26:40 -07001653 to = &end_time;
1654 if (poll_select_set_timeout(to, tv.tv_sec,
1655 tv.tv_usec * NSEC_PER_USEC))
David Woodhouse9f729492006-01-18 17:44:05 -08001656 return -EINVAL;
David Woodhouse9f729492006-01-18 17:44:05 -08001657 }
1658
Arjan van de Ven8ff3e8e2008-08-31 08:26:40 -07001659 ret = compat_core_sys_select(n, inp, outp, exp, to);
1660 ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
David Woodhouse9f729492006-01-18 17:44:05 -08001661
1662 return ret;
1663}
1664
Roland McGrathf3de2722008-04-30 00:53:09 -07001665#ifdef HAVE_SET_RESTORE_SIGMASK
David Woodhouse9f729492006-01-18 17:44:05 -08001666asmlinkage long compat_sys_pselect7(int n, compat_ulong_t __user *inp,
1667 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1668 struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask,
1669 compat_size_t sigsetsize)
1670{
1671 compat_sigset_t ss32;
1672 sigset_t ksigmask, sigsaved;
David Woodhouse9f729492006-01-18 17:44:05 -08001673 struct compat_timespec ts;
Arjan van de Ven8ff3e8e2008-08-31 08:26:40 -07001674 struct timespec end_time, *to = NULL;
David Woodhouse9f729492006-01-18 17:44:05 -08001675 int ret;
1676
1677 if (tsp) {
1678 if (copy_from_user(&ts, tsp, sizeof(ts)))
1679 return -EFAULT;
1680
Arjan van de Ven8ff3e8e2008-08-31 08:26:40 -07001681 to = &end_time;
1682 if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
David Woodhouse9f729492006-01-18 17:44:05 -08001683 return -EINVAL;
1684 }
1685
1686 if (sigmask) {
1687 if (sigsetsize != sizeof(compat_sigset_t))
1688 return -EINVAL;
1689 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1690 return -EFAULT;
1691 sigset_from_compat(&ksigmask, &ss32);
1692
1693 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1694 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1695 }
1696
Arjan van de Ven8ff3e8e2008-08-31 08:26:40 -07001697 ret = compat_core_sys_select(n, inp, outp, exp, to);
1698 ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
David Woodhouse9f729492006-01-18 17:44:05 -08001699
1700 if (ret == -ERESTARTNOHAND) {
1701 /*
1702 * Don't restore the signal mask yet. Let do_signal() deliver
1703 * the signal on the way back to userspace, before the signal
1704 * mask is restored.
1705 */
1706 if (sigmask) {
1707 memcpy(&current->saved_sigmask, &sigsaved,
1708 sizeof(sigsaved));
Roland McGrath4e4c22c2008-04-30 00:53:06 -07001709 set_restore_sigmask();
David Woodhouse9f729492006-01-18 17:44:05 -08001710 }
1711 } else if (sigmask)
1712 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1713
1714 return ret;
1715}
1716
1717asmlinkage long compat_sys_pselect6(int n, compat_ulong_t __user *inp,
1718 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1719 struct compat_timespec __user *tsp, void __user *sig)
1720{
1721 compat_size_t sigsetsize = 0;
1722 compat_uptr_t up = 0;
1723
1724 if (sig) {
1725 if (!access_ok(VERIFY_READ, sig,
1726 sizeof(compat_uptr_t)+sizeof(compat_size_t)) ||
1727 __get_user(up, (compat_uptr_t __user *)sig) ||
1728 __get_user(sigsetsize,
1729 (compat_size_t __user *)(sig+sizeof(up))))
1730 return -EFAULT;
1731 }
1732 return compat_sys_pselect7(n, inp, outp, exp, tsp, compat_ptr(up),
1733 sigsetsize);
1734}
1735
1736asmlinkage long compat_sys_ppoll(struct pollfd __user *ufds,
1737 unsigned int nfds, struct compat_timespec __user *tsp,
1738 const compat_sigset_t __user *sigmask, compat_size_t sigsetsize)
1739{
1740 compat_sigset_t ss32;
1741 sigset_t ksigmask, sigsaved;
1742 struct compat_timespec ts;
Arjan van de Ven8ff3e8e2008-08-31 08:26:40 -07001743 struct timespec end_time, *to = NULL;
David Woodhouse9f729492006-01-18 17:44:05 -08001744 int ret;
1745
1746 if (tsp) {
1747 if (copy_from_user(&ts, tsp, sizeof(ts)))
1748 return -EFAULT;
1749
Arjan van de Ven8ff3e8e2008-08-31 08:26:40 -07001750 to = &end_time;
1751 if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
1752 return -EINVAL;
David Woodhouse9f729492006-01-18 17:44:05 -08001753 }
1754
1755 if (sigmask) {
Alexey Dobriyan3835a9b2006-05-15 09:44:27 -07001756 if (sigsetsize != sizeof(compat_sigset_t))
David Woodhouse9f729492006-01-18 17:44:05 -08001757 return -EINVAL;
1758 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1759 return -EFAULT;
1760 sigset_from_compat(&ksigmask, &ss32);
1761
1762 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1763 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1764 }
1765
Arjan van de Ven8ff3e8e2008-08-31 08:26:40 -07001766 ret = do_sys_poll(ufds, nfds, to);
David Woodhouse9f729492006-01-18 17:44:05 -08001767
1768 /* We can restart this syscall, usually */
1769 if (ret == -EINTR) {
1770 /*
1771 * Don't restore the signal mask yet. Let do_signal() deliver
1772 * the signal on the way back to userspace, before the signal
1773 * mask is restored.
1774 */
1775 if (sigmask) {
1776 memcpy(&current->saved_sigmask, &sigsaved,
1777 sizeof(sigsaved));
Roland McGrath4e4c22c2008-04-30 00:53:06 -07001778 set_restore_sigmask();
David Woodhouse9f729492006-01-18 17:44:05 -08001779 }
1780 ret = -ERESTARTNOHAND;
1781 } else if (sigmask)
1782 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1783
Arjan van de Ven8ff3e8e2008-08-31 08:26:40 -07001784 ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
David Woodhouse9f729492006-01-18 17:44:05 -08001785
1786 return ret;
1787}
Roland McGrathf3de2722008-04-30 00:53:09 -07001788#endif /* HAVE_SET_RESTORE_SIGMASK */
David Woodhouse9f729492006-01-18 17:44:05 -08001789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790#if defined(CONFIG_NFSD) || defined(CONFIG_NFSD_MODULE)
1791/* Stuff for NFS server syscalls... */
1792struct compat_nfsctl_svc {
1793 u16 svc32_port;
1794 s32 svc32_nthreads;
1795};
1796
1797struct compat_nfsctl_client {
1798 s8 cl32_ident[NFSCLNT_IDMAX+1];
1799 s32 cl32_naddr;
1800 struct in_addr cl32_addrlist[NFSCLNT_ADDRMAX];
1801 s32 cl32_fhkeytype;
1802 s32 cl32_fhkeylen;
1803 u8 cl32_fhkey[NFSCLNT_KEYMAX];
1804};
1805
1806struct compat_nfsctl_export {
1807 char ex32_client[NFSCLNT_IDMAX+1];
1808 char ex32_path[NFS_MAXPATHLEN+1];
1809 compat_dev_t ex32_dev;
1810 compat_ino_t ex32_ino;
1811 compat_int_t ex32_flags;
Stephen Rothwell202e5972005-09-06 15:16:40 -07001812 __compat_uid_t ex32_anon_uid;
1813 __compat_gid_t ex32_anon_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814};
1815
1816struct compat_nfsctl_fdparm {
1817 struct sockaddr gd32_addr;
1818 s8 gd32_path[NFS_MAXPATHLEN+1];
1819 compat_int_t gd32_version;
1820};
1821
1822struct compat_nfsctl_fsparm {
1823 struct sockaddr gd32_addr;
1824 s8 gd32_path[NFS_MAXPATHLEN+1];
1825 compat_int_t gd32_maxlen;
1826};
1827
1828struct compat_nfsctl_arg {
1829 compat_int_t ca32_version; /* safeguard */
1830 union {
1831 struct compat_nfsctl_svc u32_svc;
1832 struct compat_nfsctl_client u32_client;
1833 struct compat_nfsctl_export u32_export;
1834 struct compat_nfsctl_fdparm u32_getfd;
1835 struct compat_nfsctl_fsparm u32_getfs;
1836 } u;
1837#define ca32_svc u.u32_svc
1838#define ca32_client u.u32_client
1839#define ca32_export u.u32_export
1840#define ca32_getfd u.u32_getfd
1841#define ca32_getfs u.u32_getfs
1842};
1843
1844union compat_nfsctl_res {
1845 __u8 cr32_getfh[NFS_FHSIZE];
1846 struct knfsd_fh cr32_getfs;
1847};
1848
Lin Feng Shend64b1c82006-05-20 14:59:49 -07001849static int compat_nfs_svc_trans(struct nfsctl_arg *karg,
1850 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07001852 if (!access_ok(VERIFY_READ, &arg->ca32_svc, sizeof(arg->ca32_svc)) ||
1853 get_user(karg->ca_version, &arg->ca32_version) ||
1854 __get_user(karg->ca_svc.svc_port, &arg->ca32_svc.svc32_port) ||
1855 __get_user(karg->ca_svc.svc_nthreads,
1856 &arg->ca32_svc.svc32_nthreads))
1857 return -EFAULT;
1858 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
1860
Lin Feng Shend64b1c82006-05-20 14:59:49 -07001861static int compat_nfs_clnt_trans(struct nfsctl_arg *karg,
1862 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07001864 if (!access_ok(VERIFY_READ, &arg->ca32_client,
1865 sizeof(arg->ca32_client)) ||
1866 get_user(karg->ca_version, &arg->ca32_version) ||
1867 __copy_from_user(&karg->ca_client.cl_ident[0],
1868 &arg->ca32_client.cl32_ident[0],
1869 NFSCLNT_IDMAX) ||
1870 __get_user(karg->ca_client.cl_naddr,
1871 &arg->ca32_client.cl32_naddr) ||
1872 __copy_from_user(&karg->ca_client.cl_addrlist[0],
1873 &arg->ca32_client.cl32_addrlist[0],
1874 (sizeof(struct in_addr) * NFSCLNT_ADDRMAX)) ||
1875 __get_user(karg->ca_client.cl_fhkeytype,
1876 &arg->ca32_client.cl32_fhkeytype) ||
1877 __get_user(karg->ca_client.cl_fhkeylen,
1878 &arg->ca32_client.cl32_fhkeylen) ||
1879 __copy_from_user(&karg->ca_client.cl_fhkey[0],
1880 &arg->ca32_client.cl32_fhkey[0],
1881 NFSCLNT_KEYMAX))
1882 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Lin Feng Shend64b1c82006-05-20 14:59:49 -07001884 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886
Lin Feng Shend64b1c82006-05-20 14:59:49 -07001887static int compat_nfs_exp_trans(struct nfsctl_arg *karg,
1888 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07001890 if (!access_ok(VERIFY_READ, &arg->ca32_export,
1891 sizeof(arg->ca32_export)) ||
1892 get_user(karg->ca_version, &arg->ca32_version) ||
1893 __copy_from_user(&karg->ca_export.ex_client[0],
1894 &arg->ca32_export.ex32_client[0],
1895 NFSCLNT_IDMAX) ||
1896 __copy_from_user(&karg->ca_export.ex_path[0],
1897 &arg->ca32_export.ex32_path[0],
1898 NFS_MAXPATHLEN) ||
1899 __get_user(karg->ca_export.ex_dev,
1900 &arg->ca32_export.ex32_dev) ||
1901 __get_user(karg->ca_export.ex_ino,
1902 &arg->ca32_export.ex32_ino) ||
1903 __get_user(karg->ca_export.ex_flags,
1904 &arg->ca32_export.ex32_flags) ||
1905 __get_user(karg->ca_export.ex_anon_uid,
1906 &arg->ca32_export.ex32_anon_uid) ||
1907 __get_user(karg->ca_export.ex_anon_gid,
1908 &arg->ca32_export.ex32_anon_gid))
1909 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 SET_UID(karg->ca_export.ex_anon_uid, karg->ca_export.ex_anon_uid);
1911 SET_GID(karg->ca_export.ex_anon_gid, karg->ca_export.ex_anon_gid);
1912
Lin Feng Shend64b1c82006-05-20 14:59:49 -07001913 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914}
1915
Lin Feng Shend64b1c82006-05-20 14:59:49 -07001916static int compat_nfs_getfd_trans(struct nfsctl_arg *karg,
1917 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07001919 if (!access_ok(VERIFY_READ, &arg->ca32_getfd,
1920 sizeof(arg->ca32_getfd)) ||
1921 get_user(karg->ca_version, &arg->ca32_version) ||
1922 __copy_from_user(&karg->ca_getfd.gd_addr,
1923 &arg->ca32_getfd.gd32_addr,
1924 (sizeof(struct sockaddr))) ||
1925 __copy_from_user(&karg->ca_getfd.gd_path,
1926 &arg->ca32_getfd.gd32_path,
1927 (NFS_MAXPATHLEN+1)) ||
1928 __get_user(karg->ca_getfd.gd_version,
1929 &arg->ca32_getfd.gd32_version))
1930 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Lin Feng Shend64b1c82006-05-20 14:59:49 -07001932 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
1934
Lin Feng Shend64b1c82006-05-20 14:59:49 -07001935static int compat_nfs_getfs_trans(struct nfsctl_arg *karg,
1936 struct compat_nfsctl_arg __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
Lin Feng Shend64b1c82006-05-20 14:59:49 -07001938 if (!access_ok(VERIFY_READ,&arg->ca32_getfs,sizeof(arg->ca32_getfs)) ||
1939 get_user(karg->ca_version, &arg->ca32_version) ||
1940 __copy_from_user(&karg->ca_getfs.gd_addr,
1941 &arg->ca32_getfs.gd32_addr,
1942 (sizeof(struct sockaddr))) ||
1943 __copy_from_user(&karg->ca_getfs.gd_path,
1944 &arg->ca32_getfs.gd32_path,
1945 (NFS_MAXPATHLEN+1)) ||
1946 __get_user(karg->ca_getfs.gd_maxlen,
1947 &arg->ca32_getfs.gd32_maxlen))
1948 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Lin Feng Shend64b1c82006-05-20 14:59:49 -07001950 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951}
1952
1953/* This really doesn't need translations, we are only passing
1954 * back a union which contains opaque nfs file handle data.
1955 */
Lin Feng Shend64b1c82006-05-20 14:59:49 -07001956static int compat_nfs_getfh_res_trans(union nfsctl_res *kres,
1957 union compat_nfsctl_res __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
1959 int err;
1960
1961 err = copy_to_user(res, kres, sizeof(*res));
1962
1963 return (err) ? -EFAULT : 0;
1964}
1965
Lin Feng Shend64b1c82006-05-20 14:59:49 -07001966asmlinkage long compat_sys_nfsservctl(int cmd,
1967 struct compat_nfsctl_arg __user *arg,
1968 union compat_nfsctl_res __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
1970 struct nfsctl_arg *karg;
1971 union nfsctl_res *kres;
1972 mm_segment_t oldfs;
1973 int err;
1974
1975 karg = kmalloc(sizeof(*karg), GFP_USER);
1976 kres = kmalloc(sizeof(*kres), GFP_USER);
1977 if(!karg || !kres) {
1978 err = -ENOMEM;
1979 goto done;
1980 }
1981
1982 switch(cmd) {
1983 case NFSCTL_SVC:
1984 err = compat_nfs_svc_trans(karg, arg);
1985 break;
1986
1987 case NFSCTL_ADDCLIENT:
1988 err = compat_nfs_clnt_trans(karg, arg);
1989 break;
1990
1991 case NFSCTL_DELCLIENT:
1992 err = compat_nfs_clnt_trans(karg, arg);
1993 break;
1994
1995 case NFSCTL_EXPORT:
1996 case NFSCTL_UNEXPORT:
1997 err = compat_nfs_exp_trans(karg, arg);
1998 break;
1999
2000 case NFSCTL_GETFD:
2001 err = compat_nfs_getfd_trans(karg, arg);
2002 break;
2003
2004 case NFSCTL_GETFS:
2005 err = compat_nfs_getfs_trans(karg, arg);
2006 break;
2007
2008 default:
2009 err = -EINVAL;
Peter Staubach57070d02006-03-25 03:08:04 -08002010 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
2012
Peter Staubach57070d02006-03-25 03:08:04 -08002013 if (err)
2014 goto done;
2015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 oldfs = get_fs();
2017 set_fs(KERNEL_DS);
2018 /* The __user pointer casts are valid because of the set_fs() */
2019 err = sys_nfsservctl(cmd, (void __user *) karg, (void __user *) kres);
2020 set_fs(oldfs);
2021
2022 if (err)
2023 goto done;
2024
2025 if((cmd == NFSCTL_GETFD) ||
2026 (cmd == NFSCTL_GETFS))
2027 err = compat_nfs_getfh_res_trans(kres, res);
2028
2029done:
2030 kfree(karg);
2031 kfree(kres);
2032 return err;
2033}
2034#else /* !NFSD */
2035long asmlinkage compat_sys_nfsservctl(int cmd, void *notused, void *notused2)
2036{
2037 return sys_ni_syscall();
2038}
2039#endif
Davide Libenzif6dfb4f2007-03-07 20:41:21 -08002040
2041#ifdef CONFIG_EPOLL
2042
Roland McGrathf3de2722008-04-30 00:53:09 -07002043#ifdef HAVE_SET_RESTORE_SIGMASK
Davide Libenzif6dfb4f2007-03-07 20:41:21 -08002044asmlinkage long compat_sys_epoll_pwait(int epfd,
2045 struct compat_epoll_event __user *events,
2046 int maxevents, int timeout,
2047 const compat_sigset_t __user *sigmask,
2048 compat_size_t sigsetsize)
2049{
2050 long err;
2051 compat_sigset_t csigmask;
2052 sigset_t ksigmask, sigsaved;
2053
2054 /*
2055 * If the caller wants a certain signal mask to be set during the wait,
2056 * we apply it here.
2057 */
2058 if (sigmask) {
2059 if (sigsetsize != sizeof(compat_sigset_t))
2060 return -EINVAL;
2061 if (copy_from_user(&csigmask, sigmask, sizeof(csigmask)))
2062 return -EFAULT;
2063 sigset_from_compat(&ksigmask, &csigmask);
2064 sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
2065 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
2066 }
2067
Davide Libenzif6dfb4f2007-03-07 20:41:21 -08002068 err = sys_epoll_wait(epfd, events, maxevents, timeout);
Davide Libenzif6dfb4f2007-03-07 20:41:21 -08002069
2070 /*
2071 * If we changed the signal mask, we need to restore the original one.
2072 * In case we've got a signal while waiting, we do not restore the
2073 * signal mask yet, and we allow do_signal() to deliver the signal on
2074 * the way back to userspace, before the signal mask is restored.
2075 */
2076 if (sigmask) {
2077 if (err == -EINTR) {
2078 memcpy(&current->saved_sigmask, &sigsaved,
2079 sizeof(sigsaved));
Roland McGrath4e4c22c2008-04-30 00:53:06 -07002080 set_restore_sigmask();
Davide Libenzif6dfb4f2007-03-07 20:41:21 -08002081 } else
2082 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2083 }
2084
2085 return err;
2086}
Roland McGrathf3de2722008-04-30 00:53:09 -07002087#endif /* HAVE_SET_RESTORE_SIGMASK */
Davide Libenzif6dfb4f2007-03-07 20:41:21 -08002088
2089#endif /* CONFIG_EPOLL */
Davide Libenzi6d18c922007-05-10 22:23:15 -07002090
2091#ifdef CONFIG_SIGNALFD
2092
Ulrich Drepper9deb27b2008-07-23 21:29:24 -07002093asmlinkage long compat_sys_signalfd4(int ufd,
2094 const compat_sigset_t __user *sigmask,
2095 compat_size_t sigsetsize, int flags)
Davide Libenzi6d18c922007-05-10 22:23:15 -07002096{
2097 compat_sigset_t ss32;
2098 sigset_t tmp;
2099 sigset_t __user *ksigmask;
2100
2101 if (sigsetsize != sizeof(compat_sigset_t))
2102 return -EINVAL;
2103 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
2104 return -EFAULT;
2105 sigset_from_compat(&tmp, &ss32);
2106 ksigmask = compat_alloc_user_space(sizeof(sigset_t));
2107 if (copy_to_user(ksigmask, &tmp, sizeof(sigset_t)))
2108 return -EFAULT;
2109
Ulrich Drepper9deb27b2008-07-23 21:29:24 -07002110 return sys_signalfd4(ufd, ksigmask, sizeof(sigset_t), flags);
Davide Libenzi6d18c922007-05-10 22:23:15 -07002111}
2112
Ulrich Drepper9deb27b2008-07-23 21:29:24 -07002113asmlinkage long compat_sys_signalfd(int ufd,
2114 const compat_sigset_t __user *sigmask,
2115 compat_size_t sigsetsize)
2116{
2117 return compat_sys_signalfd4(ufd, sigmask, sigsetsize, 0);
2118}
Davide Libenzi6d18c922007-05-10 22:23:15 -07002119#endif /* CONFIG_SIGNALFD */
2120
Davide Libenzi83f5d122007-05-10 22:23:18 -07002121#ifdef CONFIG_TIMERFD
2122
Davide Libenzi4d672e72008-02-04 22:27:26 -08002123asmlinkage long compat_sys_timerfd_settime(int ufd, int flags,
2124 const struct compat_itimerspec __user *utmr,
2125 struct compat_itimerspec __user *otmr)
Davide Libenzi83f5d122007-05-10 22:23:18 -07002126{
Davide Libenzi4d672e72008-02-04 22:27:26 -08002127 int error;
Davide Libenzi83f5d122007-05-10 22:23:18 -07002128 struct itimerspec t;
2129 struct itimerspec __user *ut;
2130
Davide Libenzi83f5d122007-05-10 22:23:18 -07002131 if (get_compat_itimerspec(&t, utmr))
Heiko Carstens8317f142007-05-16 22:11:08 -07002132 return -EFAULT;
Davide Libenzi4d672e72008-02-04 22:27:26 -08002133 ut = compat_alloc_user_space(2 * sizeof(struct itimerspec));
2134 if (copy_to_user(&ut[0], &t, sizeof(t)))
Heiko Carstens8317f142007-05-16 22:11:08 -07002135 return -EFAULT;
Davide Libenzi4d672e72008-02-04 22:27:26 -08002136 error = sys_timerfd_settime(ufd, flags, &ut[0], &ut[1]);
2137 if (!error && otmr)
2138 error = (copy_from_user(&t, &ut[1], sizeof(struct itimerspec)) ||
2139 put_compat_itimerspec(otmr, &t)) ? -EFAULT: 0;
Davide Libenzi83f5d122007-05-10 22:23:18 -07002140
Davide Libenzi4d672e72008-02-04 22:27:26 -08002141 return error;
2142}
2143
2144asmlinkage long compat_sys_timerfd_gettime(int ufd,
2145 struct compat_itimerspec __user *otmr)
2146{
2147 int error;
2148 struct itimerspec t;
2149 struct itimerspec __user *ut;
2150
2151 ut = compat_alloc_user_space(sizeof(struct itimerspec));
2152 error = sys_timerfd_gettime(ufd, ut);
2153 if (!error)
2154 error = (copy_from_user(&t, ut, sizeof(struct itimerspec)) ||
2155 put_compat_itimerspec(otmr, &t)) ? -EFAULT: 0;
2156
2157 return error;
Davide Libenzi83f5d122007-05-10 22:23:18 -07002158}
2159
2160#endif /* CONFIG_TIMERFD */