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