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