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 | |
a2531293 | 11 | * Copyright (C) 2003 Pavel Machek (pavel@ucw.cz) |
1da177e4 LT |
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 | ||
85c9fe8f | 18 | #include <linux/stddef.h> |
022a1692 | 19 | #include <linux/kernel.h> |
1da177e4 LT |
20 | #include <linux/linkage.h> |
21 | #include <linux/compat.h> | |
22 | #include <linux/errno.h> | |
23 | #include <linux/time.h> | |
24 | #include <linux/fs.h> | |
25 | #include <linux/fcntl.h> | |
26 | #include <linux/namei.h> | |
27 | #include <linux/file.h> | |
9f3acc31 | 28 | #include <linux/fdtable.h> |
1da177e4 | 29 | #include <linux/vfs.h> |
1da177e4 LT |
30 | #include <linux/ioctl.h> |
31 | #include <linux/init.h> | |
1da177e4 | 32 | #include <linux/ncp_mount.h> |
9a9947bf | 33 | #include <linux/nfs4_mount.h> |
1da177e4 LT |
34 | #include <linux/syscalls.h> |
35 | #include <linux/ctype.h> | |
1da177e4 | 36 | #include <linux/dirent.h> |
0eeca283 | 37 | #include <linux/fsnotify.h> |
1da177e4 | 38 | #include <linux/highuid.h> |
1da177e4 LT |
39 | #include <linux/personality.h> |
40 | #include <linux/rwsem.h> | |
8f0ab514 | 41 | #include <linux/tsacct_kern.h> |
6272e266 | 42 | #include <linux/security.h> |
a1f8e7f7 | 43 | #include <linux/highmem.h> |
6d18c922 | 44 | #include <linux/signal.h> |
bd01f843 | 45 | #include <linux/poll.h> |
4a805e86 | 46 | #include <linux/mm.h> |
498052bb | 47 | #include <linux/fs_struct.h> |
5a0e3ad6 | 48 | #include <linux/slab.h> |
504b701b | 49 | #include <linux/pagemap.h> |
1da177e4 | 50 | |
1da177e4 LT |
51 | #include <asm/uaccess.h> |
52 | #include <asm/mmu_context.h> | |
53 | #include <asm/ioctls.h> | |
07f3f05c | 54 | #include "internal.h" |
9f72949f | 55 | |
bebfa101 AK |
56 | int compat_log = 1; |
57 | ||
58 | int compat_printk(const char *fmt, ...) | |
59 | { | |
60 | va_list ap; | |
61 | int ret; | |
62 | if (!compat_log) | |
63 | return 0; | |
64 | va_start(ap, fmt); | |
65 | ret = vprintk(fmt, ap); | |
66 | va_end(ap); | |
67 | return ret; | |
68 | } | |
69 | ||
ee0b3e67 BP |
70 | #include "read_write.h" |
71 | ||
1da177e4 LT |
72 | /* |
73 | * Not all architectures have sys_utime, so implement this in terms | |
74 | * of sys_utimes. | |
75 | */ | |
c7887325 DH |
76 | asmlinkage long compat_sys_utime(const char __user *filename, |
77 | struct compat_utimbuf __user *t) | |
1da177e4 | 78 | { |
1c710c89 | 79 | struct timespec tv[2]; |
1da177e4 LT |
80 | |
81 | if (t) { | |
82 | if (get_user(tv[0].tv_sec, &t->actime) || | |
83 | get_user(tv[1].tv_sec, &t->modtime)) | |
84 | return -EFAULT; | |
1c710c89 UD |
85 | tv[0].tv_nsec = 0; |
86 | tv[1].tv_nsec = 0; | |
1da177e4 | 87 | } |
1c710c89 UD |
88 | return do_utimes(AT_FDCWD, filename, t ? tv : NULL, 0); |
89 | } | |
90 | ||
c7887325 | 91 | asmlinkage long compat_sys_utimensat(unsigned int dfd, const char __user *filename, struct compat_timespec __user *t, int flags) |
1c710c89 UD |
92 | { |
93 | struct timespec tv[2]; | |
94 | ||
95 | if (t) { | |
96 | if (get_compat_timespec(&tv[0], &t[0]) || | |
97 | get_compat_timespec(&tv[1], &t[1])) | |
98 | return -EFAULT; | |
99 | ||
1c710c89 UD |
100 | if (tv[0].tv_nsec == UTIME_OMIT && tv[1].tv_nsec == UTIME_OMIT) |
101 | return 0; | |
102 | } | |
103 | return do_utimes(dfd, filename, t ? tv : NULL, flags); | |
1da177e4 LT |
104 | } |
105 | ||
c7887325 | 106 | asmlinkage long compat_sys_futimesat(unsigned int dfd, const char __user *filename, struct compat_timeval __user *t) |
1da177e4 | 107 | { |
1c710c89 | 108 | struct timespec tv[2]; |
1da177e4 | 109 | |
9ad11ab4 | 110 | if (t) { |
1da177e4 | 111 | if (get_user(tv[0].tv_sec, &t[0].tv_sec) || |
1c710c89 | 112 | get_user(tv[0].tv_nsec, &t[0].tv_usec) || |
1da177e4 | 113 | get_user(tv[1].tv_sec, &t[1].tv_sec) || |
1c710c89 | 114 | get_user(tv[1].tv_nsec, &t[1].tv_usec)) |
9ad11ab4 | 115 | return -EFAULT; |
1c710c89 UD |
116 | if (tv[0].tv_nsec >= 1000000 || tv[0].tv_nsec < 0 || |
117 | tv[1].tv_nsec >= 1000000 || tv[1].tv_nsec < 0) | |
118 | return -EINVAL; | |
119 | tv[0].tv_nsec *= 1000; | |
120 | tv[1].tv_nsec *= 1000; | |
9ad11ab4 | 121 | } |
1c710c89 | 122 | return do_utimes(dfd, filename, t ? tv : NULL, 0); |
5590ff0d UD |
123 | } |
124 | ||
c7887325 | 125 | asmlinkage long compat_sys_utimes(const char __user *filename, struct compat_timeval __user *t) |
5590ff0d UD |
126 | { |
127 | return compat_sys_futimesat(AT_FDCWD, filename, t); | |
1da177e4 LT |
128 | } |
129 | ||
f7a5000f CH |
130 | static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf) |
131 | { | |
fcf83067 | 132 | struct compat_stat tmp; |
f7a5000f | 133 | |
fcf83067 AV |
134 | if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev)) |
135 | return -EOVERFLOW; | |
f7a5000f | 136 | |
fcf83067 AV |
137 | memset(&tmp, 0, sizeof(tmp)); |
138 | tmp.st_dev = old_encode_dev(stat->dev); | |
139 | tmp.st_ino = stat->ino; | |
140 | if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino) | |
f7a5000f | 141 | return -EOVERFLOW; |
fcf83067 AV |
142 | tmp.st_mode = stat->mode; |
143 | tmp.st_nlink = stat->nlink; | |
144 | if (tmp.st_nlink != stat->nlink) | |
f7a5000f | 145 | return -EOVERFLOW; |
a7c1938e EB |
146 | SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid)); |
147 | SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid)); | |
fcf83067 AV |
148 | tmp.st_rdev = old_encode_dev(stat->rdev); |
149 | if ((u64) stat->size > MAX_NON_LFS) | |
150 | return -EOVERFLOW; | |
151 | tmp.st_size = stat->size; | |
152 | tmp.st_atime = stat->atime.tv_sec; | |
153 | tmp.st_atime_nsec = stat->atime.tv_nsec; | |
154 | tmp.st_mtime = stat->mtime.tv_sec; | |
155 | tmp.st_mtime_nsec = stat->mtime.tv_nsec; | |
156 | tmp.st_ctime = stat->ctime.tv_sec; | |
157 | tmp.st_ctime_nsec = stat->ctime.tv_nsec; | |
158 | tmp.st_blocks = stat->blocks; | |
159 | tmp.st_blksize = stat->blksize; | |
160 | return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0; | |
f7a5000f CH |
161 | } |
162 | ||
c7887325 | 163 | asmlinkage long compat_sys_newstat(const char __user * filename, |
1da177e4 LT |
164 | struct compat_stat __user *statbuf) |
165 | { | |
166 | struct kstat stat; | |
2eae7a18 | 167 | int error; |
1da177e4 | 168 | |
2eae7a18 CH |
169 | error = vfs_stat(filename, &stat); |
170 | if (error) | |
171 | return error; | |
172 | return cp_compat_stat(&stat, statbuf); | |
1da177e4 LT |
173 | } |
174 | ||
c7887325 | 175 | asmlinkage long compat_sys_newlstat(const char __user * filename, |
1da177e4 LT |
176 | struct compat_stat __user *statbuf) |
177 | { | |
178 | struct kstat stat; | |
2eae7a18 | 179 | int error; |
1da177e4 | 180 | |
2eae7a18 CH |
181 | error = vfs_lstat(filename, &stat); |
182 | if (error) | |
183 | return error; | |
184 | return cp_compat_stat(&stat, statbuf); | |
1da177e4 LT |
185 | } |
186 | ||
82d821dd | 187 | #ifndef __ARCH_WANT_STAT64 |
c7887325 DH |
188 | asmlinkage long compat_sys_newfstatat(unsigned int dfd, |
189 | const char __user *filename, | |
5590ff0d UD |
190 | struct compat_stat __user *statbuf, int flag) |
191 | { | |
192 | struct kstat stat; | |
0112fc22 | 193 | int error; |
5590ff0d | 194 | |
0112fc22 OD |
195 | error = vfs_fstatat(dfd, filename, &stat, flag); |
196 | if (error) | |
197 | return error; | |
198 | return cp_compat_stat(&stat, statbuf); | |
5590ff0d | 199 | } |
82d821dd | 200 | #endif |
5590ff0d | 201 | |
1da177e4 LT |
202 | asmlinkage long compat_sys_newfstat(unsigned int fd, |
203 | struct compat_stat __user * statbuf) | |
204 | { | |
205 | struct kstat stat; | |
206 | int error = vfs_fstat(fd, &stat); | |
207 | ||
208 | if (!error) | |
209 | error = cp_compat_stat(&stat, statbuf); | |
210 | return error; | |
211 | } | |
212 | ||
213 | static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf) | |
214 | { | |
215 | ||
216 | if (sizeof ubuf->f_blocks == 4) { | |
f4a67cce JT |
217 | if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail | |
218 | kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL) | |
1da177e4 LT |
219 | return -EOVERFLOW; |
220 | /* f_files and f_ffree may be -1; it's okay | |
221 | * to stuff that into 32 bits */ | |
222 | if (kbuf->f_files != 0xffffffffffffffffULL | |
223 | && (kbuf->f_files & 0xffffffff00000000ULL)) | |
224 | return -EOVERFLOW; | |
225 | if (kbuf->f_ffree != 0xffffffffffffffffULL | |
226 | && (kbuf->f_ffree & 0xffffffff00000000ULL)) | |
227 | return -EOVERFLOW; | |
228 | } | |
229 | if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) || | |
230 | __put_user(kbuf->f_type, &ubuf->f_type) || | |
231 | __put_user(kbuf->f_bsize, &ubuf->f_bsize) || | |
232 | __put_user(kbuf->f_blocks, &ubuf->f_blocks) || | |
233 | __put_user(kbuf->f_bfree, &ubuf->f_bfree) || | |
234 | __put_user(kbuf->f_bavail, &ubuf->f_bavail) || | |
235 | __put_user(kbuf->f_files, &ubuf->f_files) || | |
236 | __put_user(kbuf->f_ffree, &ubuf->f_ffree) || | |
237 | __put_user(kbuf->f_namelen, &ubuf->f_namelen) || | |
238 | __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) || | |
239 | __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) || | |
240 | __put_user(kbuf->f_frsize, &ubuf->f_frsize) || | |
1448c721 EB |
241 | __put_user(kbuf->f_flags, &ubuf->f_flags) || |
242 | __clear_user(ubuf->f_spare, sizeof(ubuf->f_spare))) | |
1da177e4 LT |
243 | return -EFAULT; |
244 | return 0; | |
245 | } | |
246 | ||
247 | /* | |
974d879e | 248 | * The following statfs calls are copies of code from fs/statfs.c and |
1da177e4 LT |
249 | * should be checked against those from time to time |
250 | */ | |
2d8f3038 | 251 | asmlinkage long compat_sys_statfs(const char __user *pathname, struct compat_statfs __user *buf) |
1da177e4 | 252 | { |
c8b91acc AV |
253 | struct kstatfs tmp; |
254 | int error = user_statfs(pathname, &tmp); | |
255 | if (!error) | |
256 | error = put_compat_statfs(buf, &tmp); | |
1da177e4 LT |
257 | return error; |
258 | } | |
259 | ||
260 | asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf) | |
261 | { | |
1da177e4 | 262 | struct kstatfs tmp; |
c8b91acc | 263 | int error = fd_statfs(fd, &tmp); |
86e07ce7 DG |
264 | if (!error) |
265 | error = put_compat_statfs(buf, &tmp); | |
1da177e4 LT |
266 | return error; |
267 | } | |
268 | ||
269 | static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf) | |
270 | { | |
271 | if (sizeof ubuf->f_blocks == 4) { | |
f4a67cce JT |
272 | if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail | |
273 | kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL) | |
1da177e4 LT |
274 | return -EOVERFLOW; |
275 | /* f_files and f_ffree may be -1; it's okay | |
276 | * to stuff that into 32 bits */ | |
277 | if (kbuf->f_files != 0xffffffffffffffffULL | |
278 | && (kbuf->f_files & 0xffffffff00000000ULL)) | |
279 | return -EOVERFLOW; | |
280 | if (kbuf->f_ffree != 0xffffffffffffffffULL | |
281 | && (kbuf->f_ffree & 0xffffffff00000000ULL)) | |
282 | return -EOVERFLOW; | |
283 | } | |
284 | if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) || | |
285 | __put_user(kbuf->f_type, &ubuf->f_type) || | |
286 | __put_user(kbuf->f_bsize, &ubuf->f_bsize) || | |
287 | __put_user(kbuf->f_blocks, &ubuf->f_blocks) || | |
288 | __put_user(kbuf->f_bfree, &ubuf->f_bfree) || | |
289 | __put_user(kbuf->f_bavail, &ubuf->f_bavail) || | |
290 | __put_user(kbuf->f_files, &ubuf->f_files) || | |
291 | __put_user(kbuf->f_ffree, &ubuf->f_ffree) || | |
292 | __put_user(kbuf->f_namelen, &ubuf->f_namelen) || | |
293 | __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) || | |
294 | __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) || | |
e0bb6bda NK |
295 | __put_user(kbuf->f_frsize, &ubuf->f_frsize) || |
296 | __put_user(kbuf->f_flags, &ubuf->f_flags) || | |
297 | __clear_user(ubuf->f_spare, sizeof(ubuf->f_spare))) | |
1da177e4 LT |
298 | return -EFAULT; |
299 | return 0; | |
300 | } | |
301 | ||
2d8f3038 | 302 | asmlinkage long compat_sys_statfs64(const char __user *pathname, compat_size_t sz, struct compat_statfs64 __user *buf) |
1da177e4 | 303 | { |
c8b91acc | 304 | struct kstatfs tmp; |
1da177e4 LT |
305 | int error; |
306 | ||
307 | if (sz != sizeof(*buf)) | |
308 | return -EINVAL; | |
309 | ||
c8b91acc AV |
310 | error = user_statfs(pathname, &tmp); |
311 | if (!error) | |
312 | error = put_compat_statfs64(buf, &tmp); | |
1da177e4 LT |
313 | return error; |
314 | } | |
315 | ||
316 | asmlinkage long compat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf) | |
317 | { | |
1da177e4 LT |
318 | struct kstatfs tmp; |
319 | int error; | |
320 | ||
321 | if (sz != sizeof(*buf)) | |
322 | return -EINVAL; | |
323 | ||
c8b91acc | 324 | error = fd_statfs(fd, &tmp); |
86e07ce7 DG |
325 | if (!error) |
326 | error = put_compat_statfs64(buf, &tmp); | |
1da177e4 LT |
327 | return error; |
328 | } | |
329 | ||
2b1c6bd7 CH |
330 | /* |
331 | * This is a copy of sys_ustat, just dealing with a structure layout. | |
332 | * Given how simple this syscall is that apporach is more maintainable | |
333 | * than the various conversion hacks. | |
334 | */ | |
335 | asmlinkage long compat_sys_ustat(unsigned dev, struct compat_ustat __user *u) | |
336 | { | |
2b1c6bd7 CH |
337 | struct compat_ustat tmp; |
338 | struct kstatfs sbuf; | |
cf31e70d | 339 | int err = vfs_ustat(new_decode_dev(dev), &sbuf); |
2b1c6bd7 CH |
340 | if (err) |
341 | return err; | |
342 | ||
343 | memset(&tmp, 0, sizeof(struct compat_ustat)); | |
344 | tmp.f_tfree = sbuf.f_bfree; | |
345 | tmp.f_tinode = sbuf.f_ffree; | |
346 | if (copy_to_user(u, &tmp, sizeof(struct compat_ustat))) | |
347 | return -EFAULT; | |
348 | return 0; | |
349 | } | |
350 | ||
1da177e4 LT |
351 | static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl) |
352 | { | |
353 | if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) || | |
354 | __get_user(kfl->l_type, &ufl->l_type) || | |
355 | __get_user(kfl->l_whence, &ufl->l_whence) || | |
356 | __get_user(kfl->l_start, &ufl->l_start) || | |
357 | __get_user(kfl->l_len, &ufl->l_len) || | |
358 | __get_user(kfl->l_pid, &ufl->l_pid)) | |
359 | return -EFAULT; | |
360 | return 0; | |
361 | } | |
362 | ||
363 | static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl) | |
364 | { | |
365 | if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) || | |
366 | __put_user(kfl->l_type, &ufl->l_type) || | |
367 | __put_user(kfl->l_whence, &ufl->l_whence) || | |
368 | __put_user(kfl->l_start, &ufl->l_start) || | |
369 | __put_user(kfl->l_len, &ufl->l_len) || | |
370 | __put_user(kfl->l_pid, &ufl->l_pid)) | |
371 | return -EFAULT; | |
372 | return 0; | |
373 | } | |
374 | ||
375 | #ifndef HAVE_ARCH_GET_COMPAT_FLOCK64 | |
376 | static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl) | |
377 | { | |
378 | if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) || | |
379 | __get_user(kfl->l_type, &ufl->l_type) || | |
380 | __get_user(kfl->l_whence, &ufl->l_whence) || | |
381 | __get_user(kfl->l_start, &ufl->l_start) || | |
382 | __get_user(kfl->l_len, &ufl->l_len) || | |
383 | __get_user(kfl->l_pid, &ufl->l_pid)) | |
384 | return -EFAULT; | |
385 | return 0; | |
386 | } | |
387 | #endif | |
388 | ||
389 | #ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64 | |
390 | static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl) | |
391 | { | |
392 | if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) || | |
393 | __put_user(kfl->l_type, &ufl->l_type) || | |
394 | __put_user(kfl->l_whence, &ufl->l_whence) || | |
395 | __put_user(kfl->l_start, &ufl->l_start) || | |
396 | __put_user(kfl->l_len, &ufl->l_len) || | |
397 | __put_user(kfl->l_pid, &ufl->l_pid)) | |
398 | return -EFAULT; | |
399 | return 0; | |
400 | } | |
401 | #endif | |
402 | ||
403 | asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd, | |
404 | unsigned long arg) | |
405 | { | |
406 | mm_segment_t old_fs; | |
407 | struct flock f; | |
408 | long ret; | |
409 | ||
410 | switch (cmd) { | |
411 | case F_GETLK: | |
412 | case F_SETLK: | |
413 | case F_SETLKW: | |
414 | ret = get_compat_flock(&f, compat_ptr(arg)); | |
415 | if (ret != 0) | |
416 | break; | |
417 | old_fs = get_fs(); | |
418 | set_fs(KERNEL_DS); | |
419 | ret = sys_fcntl(fd, cmd, (unsigned long)&f); | |
420 | set_fs(old_fs); | |
421 | if (cmd == F_GETLK && ret == 0) { | |
ff677f8d | 422 | /* GETLK was successful and we need to return the data... |
2520f14c N |
423 | * but it needs to fit in the compat structure. |
424 | * l_start shouldn't be too big, unless the original | |
425 | * start + end is greater than COMPAT_OFF_T_MAX, in which | |
426 | * case the app was asking for trouble, so we return | |
427 | * -EOVERFLOW in that case. | |
428 | * l_len could be too big, in which case we just truncate it, | |
429 | * and only allow the app to see that part of the conflicting | |
430 | * lock that might make sense to it anyway | |
431 | */ | |
432 | ||
433 | if (f.l_start > COMPAT_OFF_T_MAX) | |
1da177e4 | 434 | ret = -EOVERFLOW; |
2520f14c N |
435 | if (f.l_len > COMPAT_OFF_T_MAX) |
436 | f.l_len = COMPAT_OFF_T_MAX; | |
1da177e4 LT |
437 | if (ret == 0) |
438 | ret = put_compat_flock(&f, compat_ptr(arg)); | |
439 | } | |
440 | break; | |
441 | ||
442 | case F_GETLK64: | |
443 | case F_SETLK64: | |
444 | case F_SETLKW64: | |
445 | ret = get_compat_flock64(&f, compat_ptr(arg)); | |
446 | if (ret != 0) | |
447 | break; | |
448 | old_fs = get_fs(); | |
449 | set_fs(KERNEL_DS); | |
450 | ret = sys_fcntl(fd, (cmd == F_GETLK64) ? F_GETLK : | |
451 | ((cmd == F_SETLK64) ? F_SETLK : F_SETLKW), | |
452 | (unsigned long)&f); | |
453 | set_fs(old_fs); | |
454 | if (cmd == F_GETLK64 && ret == 0) { | |
2520f14c N |
455 | /* need to return lock information - see above for commentary */ |
456 | if (f.l_start > COMPAT_LOFF_T_MAX) | |
1da177e4 | 457 | ret = -EOVERFLOW; |
2520f14c N |
458 | if (f.l_len > COMPAT_LOFF_T_MAX) |
459 | f.l_len = COMPAT_LOFF_T_MAX; | |
1da177e4 LT |
460 | if (ret == 0) |
461 | ret = put_compat_flock64(&f, compat_ptr(arg)); | |
462 | } | |
463 | break; | |
464 | ||
465 | default: | |
466 | ret = sys_fcntl(fd, cmd, arg); | |
467 | break; | |
468 | } | |
469 | return ret; | |
470 | } | |
471 | ||
472 | asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd, | |
473 | unsigned long arg) | |
474 | { | |
475 | if ((cmd == F_GETLK64) || (cmd == F_SETLK64) || (cmd == F_SETLKW64)) | |
476 | return -EINVAL; | |
477 | return compat_sys_fcntl64(fd, cmd, arg); | |
478 | } | |
479 | ||
480 | asmlinkage long | |
481 | compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p) | |
482 | { | |
483 | long ret; | |
484 | aio_context_t ctx64; | |
485 | ||
486 | mm_segment_t oldfs = get_fs(); | |
487 | if (unlikely(get_user(ctx64, ctx32p))) | |
488 | return -EFAULT; | |
489 | ||
490 | set_fs(KERNEL_DS); | |
491 | /* The __user pointer cast is valid because of the set_fs() */ | |
492 | ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64); | |
493 | set_fs(oldfs); | |
494 | /* truncating is ok because it's a user address */ | |
495 | if (!ret) | |
496 | ret = put_user((u32) ctx64, ctx32p); | |
497 | return ret; | |
498 | } | |
499 | ||
500 | asmlinkage long | |
501 | compat_sys_io_getevents(aio_context_t ctx_id, | |
502 | unsigned long min_nr, | |
503 | unsigned long nr, | |
504 | struct io_event __user *events, | |
505 | struct compat_timespec __user *timeout) | |
506 | { | |
507 | long ret; | |
508 | struct timespec t; | |
509 | struct timespec __user *ut = NULL; | |
510 | ||
511 | ret = -EFAULT; | |
512 | if (unlikely(!access_ok(VERIFY_WRITE, events, | |
513 | nr * sizeof(struct io_event)))) | |
514 | goto out; | |
515 | if (timeout) { | |
516 | if (get_compat_timespec(&t, timeout)) | |
517 | goto out; | |
518 | ||
519 | ut = compat_alloc_user_space(sizeof(*ut)); | |
520 | if (copy_to_user(ut, &t, sizeof(t)) ) | |
521 | goto out; | |
522 | } | |
523 | ret = sys_io_getevents(ctx_id, min_nr, nr, events, ut); | |
524 | out: | |
525 | return ret; | |
526 | } | |
527 | ||
b8373363 JM |
528 | /* A write operation does a read from user space and vice versa */ |
529 | #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ) | |
530 | ||
531 | ssize_t compat_rw_copy_check_uvector(int type, | |
532 | const struct compat_iovec __user *uvector, unsigned long nr_segs, | |
533 | unsigned long fast_segs, struct iovec *fast_pointer, | |
ac34ebb3 | 534 | struct iovec **ret_pointer) |
b8373363 JM |
535 | { |
536 | compat_ssize_t tot_len; | |
537 | struct iovec *iov = *ret_pointer = fast_pointer; | |
538 | ssize_t ret = 0; | |
539 | int seg; | |
540 | ||
541 | /* | |
542 | * SuS says "The readv() function *may* fail if the iovcnt argument | |
543 | * was less than or equal to 0, or greater than {IOV_MAX}. Linux has | |
544 | * traditionally returned zero for zero segments, so... | |
545 | */ | |
546 | if (nr_segs == 0) | |
547 | goto out; | |
548 | ||
549 | ret = -EINVAL; | |
550 | if (nr_segs > UIO_MAXIOV || nr_segs < 0) | |
551 | goto out; | |
552 | if (nr_segs > fast_segs) { | |
553 | ret = -ENOMEM; | |
554 | iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL); | |
6a5640f1 | 555 | if (iov == NULL) |
b8373363 | 556 | goto out; |
b8373363 JM |
557 | } |
558 | *ret_pointer = iov; | |
559 | ||
8aec0f5d MD |
560 | ret = -EFAULT; |
561 | if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector))) | |
562 | goto out; | |
563 | ||
b8373363 JM |
564 | /* |
565 | * Single unix specification: | |
566 | * We should -EINVAL if an element length is not >= 0 and fitting an | |
435f49a5 | 567 | * ssize_t. |
b8373363 | 568 | * |
435f49a5 LT |
569 | * In Linux, the total length is limited to MAX_RW_COUNT, there is |
570 | * no overflow possibility. | |
b8373363 JM |
571 | */ |
572 | tot_len = 0; | |
573 | ret = -EINVAL; | |
574 | for (seg = 0; seg < nr_segs; seg++) { | |
b8373363 JM |
575 | compat_uptr_t buf; |
576 | compat_ssize_t len; | |
577 | ||
578 | if (__get_user(len, &uvector->iov_len) || | |
579 | __get_user(buf, &uvector->iov_base)) { | |
580 | ret = -EFAULT; | |
581 | goto out; | |
582 | } | |
583 | if (len < 0) /* size_t not fitting in compat_ssize_t .. */ | |
584 | goto out; | |
ac34ebb3 | 585 | if (type >= 0 && |
fcf63409 | 586 | !access_ok(vrfy_dir(type), compat_ptr(buf), len)) { |
b8373363 JM |
587 | ret = -EFAULT; |
588 | goto out; | |
589 | } | |
435f49a5 LT |
590 | if (len > MAX_RW_COUNT - tot_len) |
591 | len = MAX_RW_COUNT - tot_len; | |
592 | tot_len += len; | |
b8373363 JM |
593 | iov->iov_base = compat_ptr(buf); |
594 | iov->iov_len = (compat_size_t) len; | |
595 | uvector++; | |
596 | iov++; | |
597 | } | |
598 | ret = tot_len; | |
599 | ||
600 | out: | |
601 | return ret; | |
602 | } | |
603 | ||
1da177e4 LT |
604 | static inline long |
605 | copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64) | |
606 | { | |
607 | compat_uptr_t uptr; | |
608 | int i; | |
609 | ||
610 | for (i = 0; i < nr; ++i) { | |
611 | if (get_user(uptr, ptr32 + i)) | |
612 | return -EFAULT; | |
613 | if (put_user(compat_ptr(uptr), ptr64 + i)) | |
614 | return -EFAULT; | |
615 | } | |
616 | return 0; | |
617 | } | |
618 | ||
619 | #define MAX_AIO_SUBMITS (PAGE_SIZE/sizeof(struct iocb *)) | |
620 | ||
621 | asmlinkage long | |
622 | compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb) | |
623 | { | |
624 | struct iocb __user * __user *iocb64; | |
625 | long ret; | |
626 | ||
627 | if (unlikely(nr < 0)) | |
628 | return -EINVAL; | |
629 | ||
630 | if (nr > MAX_AIO_SUBMITS) | |
631 | nr = MAX_AIO_SUBMITS; | |
632 | ||
633 | iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64)); | |
634 | ret = copy_iocb(nr, iocb, iocb64); | |
635 | if (!ret) | |
9d85cba7 | 636 | ret = do_io_submit(ctx_id, nr, iocb64, 1); |
1da177e4 LT |
637 | return ret; |
638 | } | |
639 | ||
640 | struct compat_ncp_mount_data { | |
641 | compat_int_t version; | |
642 | compat_uint_t ncp_fd; | |
202e5979 | 643 | __compat_uid_t mounted_uid; |
1da177e4 LT |
644 | compat_pid_t wdog_pid; |
645 | unsigned char mounted_vol[NCP_VOLNAME_LEN + 1]; | |
646 | compat_uint_t time_out; | |
647 | compat_uint_t retry_count; | |
648 | compat_uint_t flags; | |
202e5979 SR |
649 | __compat_uid_t uid; |
650 | __compat_gid_t gid; | |
1da177e4 LT |
651 | compat_mode_t file_mode; |
652 | compat_mode_t dir_mode; | |
653 | }; | |
654 | ||
655 | struct compat_ncp_mount_data_v4 { | |
656 | compat_int_t version; | |
657 | compat_ulong_t flags; | |
658 | compat_ulong_t mounted_uid; | |
659 | compat_long_t wdog_pid; | |
660 | compat_uint_t ncp_fd; | |
661 | compat_uint_t time_out; | |
662 | compat_uint_t retry_count; | |
663 | compat_ulong_t uid; | |
664 | compat_ulong_t gid; | |
665 | compat_ulong_t file_mode; | |
666 | compat_ulong_t dir_mode; | |
667 | }; | |
668 | ||
669 | static void *do_ncp_super_data_conv(void *raw_data) | |
670 | { | |
671 | int version = *(unsigned int *)raw_data; | |
672 | ||
673 | if (version == 3) { | |
674 | struct compat_ncp_mount_data *c_n = raw_data; | |
675 | struct ncp_mount_data *n = raw_data; | |
676 | ||
677 | n->dir_mode = c_n->dir_mode; | |
678 | n->file_mode = c_n->file_mode; | |
679 | n->gid = c_n->gid; | |
680 | n->uid = c_n->uid; | |
681 | memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int))); | |
682 | n->wdog_pid = c_n->wdog_pid; | |
683 | n->mounted_uid = c_n->mounted_uid; | |
684 | } else if (version == 4) { | |
685 | struct compat_ncp_mount_data_v4 *c_n = raw_data; | |
686 | struct ncp_mount_data_v4 *n = raw_data; | |
687 | ||
688 | n->dir_mode = c_n->dir_mode; | |
689 | n->file_mode = c_n->file_mode; | |
690 | n->gid = c_n->gid; | |
691 | n->uid = c_n->uid; | |
692 | n->retry_count = c_n->retry_count; | |
693 | n->time_out = c_n->time_out; | |
694 | n->ncp_fd = c_n->ncp_fd; | |
695 | n->wdog_pid = c_n->wdog_pid; | |
696 | n->mounted_uid = c_n->mounted_uid; | |
697 | n->flags = c_n->flags; | |
698 | } else if (version != 5) { | |
699 | return NULL; | |
700 | } | |
701 | ||
702 | return raw_data; | |
703 | } | |
704 | ||
1da177e4 | 705 | |
9a9947bf DH |
706 | struct compat_nfs_string { |
707 | compat_uint_t len; | |
5fc3e624 | 708 | compat_uptr_t data; |
9a9947bf DH |
709 | }; |
710 | ||
711 | static inline void compat_nfs_string(struct nfs_string *dst, | |
712 | struct compat_nfs_string *src) | |
713 | { | |
714 | dst->data = compat_ptr(src->data); | |
715 | dst->len = src->len; | |
716 | } | |
717 | ||
718 | struct compat_nfs4_mount_data_v1 { | |
719 | compat_int_t version; | |
720 | compat_int_t flags; | |
721 | compat_int_t rsize; | |
722 | compat_int_t wsize; | |
723 | compat_int_t timeo; | |
724 | compat_int_t retrans; | |
725 | compat_int_t acregmin; | |
726 | compat_int_t acregmax; | |
727 | compat_int_t acdirmin; | |
728 | compat_int_t acdirmax; | |
729 | struct compat_nfs_string client_addr; | |
730 | struct compat_nfs_string mnt_path; | |
731 | struct compat_nfs_string hostname; | |
732 | compat_uint_t host_addrlen; | |
5fc3e624 | 733 | compat_uptr_t host_addr; |
9a9947bf DH |
734 | compat_int_t proto; |
735 | compat_int_t auth_flavourlen; | |
5fc3e624 | 736 | compat_uptr_t auth_flavours; |
9a9947bf DH |
737 | }; |
738 | ||
739 | static int do_nfs4_super_data_conv(void *raw_data) | |
740 | { | |
741 | int version = *(compat_uint_t *) raw_data; | |
742 | ||
743 | if (version == 1) { | |
744 | struct compat_nfs4_mount_data_v1 *raw = raw_data; | |
745 | struct nfs4_mount_data *real = raw_data; | |
746 | ||
747 | /* copy the fields backwards */ | |
748 | real->auth_flavours = compat_ptr(raw->auth_flavours); | |
749 | real->auth_flavourlen = raw->auth_flavourlen; | |
750 | real->proto = raw->proto; | |
751 | real->host_addr = compat_ptr(raw->host_addr); | |
752 | real->host_addrlen = raw->host_addrlen; | |
753 | compat_nfs_string(&real->hostname, &raw->hostname); | |
754 | compat_nfs_string(&real->mnt_path, &raw->mnt_path); | |
755 | compat_nfs_string(&real->client_addr, &raw->client_addr); | |
756 | real->acdirmax = raw->acdirmax; | |
757 | real->acdirmin = raw->acdirmin; | |
758 | real->acregmax = raw->acregmax; | |
759 | real->acregmin = raw->acregmin; | |
760 | real->retrans = raw->retrans; | |
761 | real->timeo = raw->timeo; | |
762 | real->wsize = raw->wsize; | |
763 | real->rsize = raw->rsize; | |
764 | real->flags = raw->flags; | |
765 | real->version = raw->version; | |
766 | } | |
9a9947bf DH |
767 | |
768 | return 0; | |
769 | } | |
770 | ||
1da177e4 | 771 | #define NCPFS_NAME "ncpfs" |
9a9947bf | 772 | #define NFS4_NAME "nfs4" |
1da177e4 | 773 | |
c7887325 DH |
774 | asmlinkage long compat_sys_mount(const char __user * dev_name, |
775 | const char __user * dir_name, | |
776 | const char __user * type, unsigned long flags, | |
777 | const void __user * data) | |
1da177e4 | 778 | { |
eca6f534 | 779 | char *kernel_type; |
1da177e4 | 780 | unsigned long data_page; |
eca6f534 | 781 | char *kernel_dev; |
91a27b2a | 782 | struct filename *dir; |
1da177e4 LT |
783 | int retval; |
784 | ||
eca6f534 | 785 | retval = copy_mount_string(type, &kernel_type); |
1da177e4 LT |
786 | if (retval < 0) |
787 | goto out; | |
788 | ||
91a27b2a JL |
789 | dir = getname(dir_name); |
790 | retval = PTR_ERR(dir); | |
791 | if (IS_ERR(dir)) | |
1da177e4 LT |
792 | goto out1; |
793 | ||
eca6f534 | 794 | retval = copy_mount_string(dev_name, &kernel_dev); |
1da177e4 LT |
795 | if (retval < 0) |
796 | goto out2; | |
797 | ||
eca6f534 | 798 | retval = copy_mount_options(data, &data_page); |
1da177e4 LT |
799 | if (retval < 0) |
800 | goto out3; | |
801 | ||
802 | retval = -EINVAL; | |
803 | ||
eca6f534 | 804 | if (kernel_type && data_page) { |
2116b7a4 | 805 | if (!strcmp(kernel_type, NCPFS_NAME)) { |
1da177e4 | 806 | do_ncp_super_data_conv((void *)data_page); |
eca6f534 | 807 | } else if (!strcmp(kernel_type, NFS4_NAME)) { |
9a9947bf DH |
808 | if (do_nfs4_super_data_conv((void *) data_page)) |
809 | goto out4; | |
1da177e4 LT |
810 | } |
811 | } | |
812 | ||
91a27b2a | 813 | retval = do_mount(kernel_dev, dir->name, kernel_type, |
1da177e4 | 814 | flags, (void*)data_page); |
1da177e4 | 815 | |
9a9947bf | 816 | out4: |
1da177e4 LT |
817 | free_page(data_page); |
818 | out3: | |
eca6f534 | 819 | kfree(kernel_dev); |
1da177e4 | 820 | out2: |
91a27b2a | 821 | putname(dir); |
1da177e4 | 822 | out1: |
eca6f534 | 823 | kfree(kernel_type); |
1da177e4 LT |
824 | out: |
825 | return retval; | |
826 | } | |
827 | ||
1da177e4 LT |
828 | struct compat_old_linux_dirent { |
829 | compat_ulong_t d_ino; | |
830 | compat_ulong_t d_offset; | |
831 | unsigned short d_namlen; | |
832 | char d_name[1]; | |
833 | }; | |
834 | ||
835 | struct compat_readdir_callback { | |
836 | struct compat_old_linux_dirent __user *dirent; | |
837 | int result; | |
838 | }; | |
839 | ||
840 | static int compat_fillonedir(void *__buf, const char *name, int namlen, | |
afefdbb2 | 841 | loff_t offset, u64 ino, unsigned int d_type) |
1da177e4 LT |
842 | { |
843 | struct compat_readdir_callback *buf = __buf; | |
844 | struct compat_old_linux_dirent __user *dirent; | |
afefdbb2 | 845 | compat_ulong_t d_ino; |
1da177e4 LT |
846 | |
847 | if (buf->result) | |
848 | return -EINVAL; | |
afefdbb2 | 849 | d_ino = ino; |
8f3f655d AV |
850 | if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) { |
851 | buf->result = -EOVERFLOW; | |
afefdbb2 | 852 | return -EOVERFLOW; |
8f3f655d | 853 | } |
1da177e4 LT |
854 | buf->result++; |
855 | dirent = buf->dirent; | |
856 | if (!access_ok(VERIFY_WRITE, dirent, | |
857 | (unsigned long)(dirent->d_name + namlen + 1) - | |
858 | (unsigned long)dirent)) | |
859 | goto efault; | |
afefdbb2 | 860 | if ( __put_user(d_ino, &dirent->d_ino) || |
1da177e4 LT |
861 | __put_user(offset, &dirent->d_offset) || |
862 | __put_user(namlen, &dirent->d_namlen) || | |
863 | __copy_to_user(dirent->d_name, name, namlen) || | |
864 | __put_user(0, dirent->d_name + namlen)) | |
865 | goto efault; | |
866 | return 0; | |
867 | efault: | |
868 | buf->result = -EFAULT; | |
869 | return -EFAULT; | |
870 | } | |
871 | ||
872 | asmlinkage long compat_sys_old_readdir(unsigned int fd, | |
873 | struct compat_old_linux_dirent __user *dirent, unsigned int count) | |
874 | { | |
875 | int error; | |
2903ff01 | 876 | struct fd f = fdget(fd); |
1da177e4 LT |
877 | struct compat_readdir_callback buf; |
878 | ||
2903ff01 | 879 | if (!f.file) |
863ced7f | 880 | return -EBADF; |
1da177e4 LT |
881 | |
882 | buf.result = 0; | |
883 | buf.dirent = dirent; | |
884 | ||
2903ff01 | 885 | error = vfs_readdir(f.file, compat_fillonedir, &buf); |
53c9c5c0 | 886 | if (buf.result) |
1da177e4 LT |
887 | error = buf.result; |
888 | ||
2903ff01 | 889 | fdput(f); |
1da177e4 LT |
890 | return error; |
891 | } | |
892 | ||
893 | struct compat_linux_dirent { | |
894 | compat_ulong_t d_ino; | |
895 | compat_ulong_t d_off; | |
896 | unsigned short d_reclen; | |
897 | char d_name[1]; | |
898 | }; | |
899 | ||
900 | struct compat_getdents_callback { | |
901 | struct compat_linux_dirent __user *current_dir; | |
902 | struct compat_linux_dirent __user *previous; | |
903 | int count; | |
904 | int error; | |
905 | }; | |
906 | ||
907 | static int compat_filldir(void *__buf, const char *name, int namlen, | |
afefdbb2 | 908 | loff_t offset, u64 ino, unsigned int d_type) |
1da177e4 LT |
909 | { |
910 | struct compat_linux_dirent __user * dirent; | |
911 | struct compat_getdents_callback *buf = __buf; | |
afefdbb2 | 912 | compat_ulong_t d_ino; |
85c9fe8f KW |
913 | int reclen = ALIGN(offsetof(struct compat_linux_dirent, d_name) + |
914 | namlen + 2, sizeof(compat_long_t)); | |
1da177e4 LT |
915 | |
916 | buf->error = -EINVAL; /* only used if we fail.. */ | |
917 | if (reclen > buf->count) | |
918 | return -EINVAL; | |
afefdbb2 | 919 | d_ino = ino; |
8f3f655d AV |
920 | if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) { |
921 | buf->error = -EOVERFLOW; | |
afefdbb2 | 922 | return -EOVERFLOW; |
8f3f655d | 923 | } |
1da177e4 LT |
924 | dirent = buf->previous; |
925 | if (dirent) { | |
926 | if (__put_user(offset, &dirent->d_off)) | |
927 | goto efault; | |
928 | } | |
929 | dirent = buf->current_dir; | |
afefdbb2 | 930 | if (__put_user(d_ino, &dirent->d_ino)) |
1da177e4 LT |
931 | goto efault; |
932 | if (__put_user(reclen, &dirent->d_reclen)) | |
933 | goto efault; | |
934 | if (copy_to_user(dirent->d_name, name, namlen)) | |
935 | goto efault; | |
936 | if (__put_user(0, dirent->d_name + namlen)) | |
937 | goto efault; | |
938 | if (__put_user(d_type, (char __user *) dirent + reclen - 1)) | |
939 | goto efault; | |
940 | buf->previous = dirent; | |
941 | dirent = (void __user *)dirent + reclen; | |
942 | buf->current_dir = dirent; | |
943 | buf->count -= reclen; | |
944 | return 0; | |
945 | efault: | |
946 | buf->error = -EFAULT; | |
947 | return -EFAULT; | |
948 | } | |
949 | ||
950 | asmlinkage long compat_sys_getdents(unsigned int fd, | |
951 | struct compat_linux_dirent __user *dirent, unsigned int count) | |
952 | { | |
2903ff01 | 953 | struct fd f; |
1da177e4 LT |
954 | struct compat_linux_dirent __user * lastdirent; |
955 | struct compat_getdents_callback buf; | |
956 | int error; | |
957 | ||
1da177e4 | 958 | if (!access_ok(VERIFY_WRITE, dirent, count)) |
863ced7f | 959 | return -EFAULT; |
1da177e4 | 960 | |
2903ff01 AV |
961 | f = fdget(fd); |
962 | if (!f.file) | |
863ced7f | 963 | return -EBADF; |
1da177e4 LT |
964 | |
965 | buf.current_dir = dirent; | |
966 | buf.previous = NULL; | |
967 | buf.count = count; | |
968 | buf.error = 0; | |
969 | ||
2903ff01 | 970 | error = vfs_readdir(f.file, compat_filldir, &buf); |
53c9c5c0 AV |
971 | if (error >= 0) |
972 | error = buf.error; | |
1da177e4 LT |
973 | lastdirent = buf.previous; |
974 | if (lastdirent) { | |
2903ff01 | 975 | if (put_user(f.file->f_pos, &lastdirent->d_off)) |
1da177e4 LT |
976 | error = -EFAULT; |
977 | else | |
978 | error = count - buf.count; | |
979 | } | |
2903ff01 | 980 | fdput(f); |
1da177e4 LT |
981 | return error; |
982 | } | |
983 | ||
984 | #ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64 | |
1da177e4 LT |
985 | |
986 | struct compat_getdents_callback64 { | |
987 | struct linux_dirent64 __user *current_dir; | |
988 | struct linux_dirent64 __user *previous; | |
989 | int count; | |
990 | int error; | |
991 | }; | |
992 | ||
993 | static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset, | |
afefdbb2 | 994 | u64 ino, unsigned int d_type) |
1da177e4 LT |
995 | { |
996 | struct linux_dirent64 __user *dirent; | |
997 | struct compat_getdents_callback64 *buf = __buf; | |
85c9fe8f KW |
998 | int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1, |
999 | sizeof(u64)); | |
1da177e4 LT |
1000 | u64 off; |
1001 | ||
1002 | buf->error = -EINVAL; /* only used if we fail.. */ | |
1003 | if (reclen > buf->count) | |
1004 | return -EINVAL; | |
1005 | dirent = buf->previous; | |
1006 | ||
1007 | if (dirent) { | |
1008 | if (__put_user_unaligned(offset, &dirent->d_off)) | |
1009 | goto efault; | |
1010 | } | |
1011 | dirent = buf->current_dir; | |
1012 | if (__put_user_unaligned(ino, &dirent->d_ino)) | |
1013 | goto efault; | |
1014 | off = 0; | |
1015 | if (__put_user_unaligned(off, &dirent->d_off)) | |
1016 | goto efault; | |
1017 | if (__put_user(reclen, &dirent->d_reclen)) | |
1018 | goto efault; | |
1019 | if (__put_user(d_type, &dirent->d_type)) | |
1020 | goto efault; | |
1021 | if (copy_to_user(dirent->d_name, name, namlen)) | |
1022 | goto efault; | |
1023 | if (__put_user(0, dirent->d_name + namlen)) | |
1024 | goto efault; | |
1025 | buf->previous = dirent; | |
1026 | dirent = (void __user *)dirent + reclen; | |
1027 | buf->current_dir = dirent; | |
1028 | buf->count -= reclen; | |
1029 | return 0; | |
1030 | efault: | |
1031 | buf->error = -EFAULT; | |
1032 | return -EFAULT; | |
1033 | } | |
1034 | ||
1035 | asmlinkage long compat_sys_getdents64(unsigned int fd, | |
1036 | struct linux_dirent64 __user * dirent, unsigned int count) | |
1037 | { | |
2903ff01 | 1038 | struct fd f; |
1da177e4 LT |
1039 | struct linux_dirent64 __user * lastdirent; |
1040 | struct compat_getdents_callback64 buf; | |
1041 | int error; | |
1042 | ||
1da177e4 | 1043 | if (!access_ok(VERIFY_WRITE, dirent, count)) |
863ced7f | 1044 | return -EFAULT; |
1da177e4 | 1045 | |
2903ff01 AV |
1046 | f = fdget(fd); |
1047 | if (!f.file) | |
863ced7f | 1048 | return -EBADF; |
1da177e4 LT |
1049 | |
1050 | buf.current_dir = dirent; | |
1051 | buf.previous = NULL; | |
1052 | buf.count = count; | |
1053 | buf.error = 0; | |
1054 | ||
2903ff01 | 1055 | error = vfs_readdir(f.file, compat_filldir64, &buf); |
53c9c5c0 AV |
1056 | if (error >= 0) |
1057 | error = buf.error; | |
1da177e4 LT |
1058 | lastdirent = buf.previous; |
1059 | if (lastdirent) { | |
2903ff01 | 1060 | typeof(lastdirent->d_off) d_off = f.file->f_pos; |
7116e994 | 1061 | if (__put_user_unaligned(d_off, &lastdirent->d_off)) |
53c9c5c0 AV |
1062 | error = -EFAULT; |
1063 | else | |
1064 | error = count - buf.count; | |
1da177e4 | 1065 | } |
2903ff01 | 1066 | fdput(f); |
1da177e4 LT |
1067 | return error; |
1068 | } | |
1069 | #endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */ | |
1070 | ||
e922efc3 MS |
1071 | /* |
1072 | * Exactly like fs/open.c:sys_open(), except that it doesn't set the | |
1073 | * O_LARGEFILE flag. | |
1074 | */ | |
f482e1b4 | 1075 | COMPAT_SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode) |
e922efc3 | 1076 | { |
5590ff0d UD |
1077 | return do_sys_open(AT_FDCWD, filename, flags, mode); |
1078 | } | |
1079 | ||
1080 | /* | |
1081 | * Exactly like fs/open.c:sys_openat(), except that it doesn't set the | |
1082 | * O_LARGEFILE flag. | |
1083 | */ | |
f482e1b4 | 1084 | COMPAT_SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, umode_t, mode) |
5590ff0d UD |
1085 | { |
1086 | return do_sys_open(dfd, filename, flags, mode); | |
e922efc3 MS |
1087 | } |
1088 | ||
1da177e4 LT |
1089 | #define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t)) |
1090 | ||
b773ad40 TG |
1091 | static int poll_select_copy_remaining(struct timespec *end_time, void __user *p, |
1092 | int timeval, int ret) | |
1093 | { | |
1094 | struct timespec ts; | |
1095 | ||
1096 | if (!p) | |
1097 | return ret; | |
1098 | ||
1099 | if (current->personality & STICKY_TIMEOUTS) | |
1100 | goto sticky; | |
1101 | ||
1102 | /* No update for zero timeout */ | |
1103 | if (!end_time->tv_sec && !end_time->tv_nsec) | |
1104 | return ret; | |
1105 | ||
1106 | ktime_get_ts(&ts); | |
1107 | ts = timespec_sub(*end_time, ts); | |
1108 | if (ts.tv_sec < 0) | |
1109 | ts.tv_sec = ts.tv_nsec = 0; | |
1110 | ||
1111 | if (timeval) { | |
1112 | struct compat_timeval rtv; | |
1113 | ||
1114 | rtv.tv_sec = ts.tv_sec; | |
1115 | rtv.tv_usec = ts.tv_nsec / NSEC_PER_USEC; | |
1116 | ||
1117 | if (!copy_to_user(p, &rtv, sizeof(rtv))) | |
1118 | return ret; | |
1119 | } else { | |
1120 | struct compat_timespec rts; | |
1121 | ||
1122 | rts.tv_sec = ts.tv_sec; | |
1123 | rts.tv_nsec = ts.tv_nsec; | |
1124 | ||
1125 | if (!copy_to_user(p, &rts, sizeof(rts))) | |
1126 | return ret; | |
1127 | } | |
1128 | /* | |
1129 | * If an application puts its timeval in read-only memory, we | |
1130 | * don't want the Linux-specific update to the timeval to | |
1131 | * cause a fault after the select has completed | |
1132 | * successfully. However, because we're not updating the | |
1133 | * timeval, we can't restart the system call. | |
1134 | */ | |
1135 | ||
1136 | sticky: | |
1137 | if (ret == -ERESTARTNOHAND) | |
1138 | ret = -EINTR; | |
1139 | return ret; | |
1140 | } | |
1141 | ||
1da177e4 LT |
1142 | /* |
1143 | * Ooo, nasty. We need here to frob 32-bit unsigned longs to | |
1144 | * 64-bit unsigned longs. | |
1145 | */ | |
858119e1 | 1146 | static |
1da177e4 LT |
1147 | int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset, |
1148 | unsigned long *fdset) | |
1149 | { | |
022a1692 | 1150 | nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS); |
1da177e4 LT |
1151 | if (ufdset) { |
1152 | unsigned long odd; | |
1153 | ||
1154 | if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t))) | |
1155 | return -EFAULT; | |
1156 | ||
1157 | odd = nr & 1UL; | |
1158 | nr &= ~1UL; | |
1159 | while (nr) { | |
1160 | unsigned long h, l; | |
7116e994 HC |
1161 | if (__get_user(l, ufdset) || __get_user(h, ufdset+1)) |
1162 | return -EFAULT; | |
1da177e4 LT |
1163 | ufdset += 2; |
1164 | *fdset++ = h << 32 | l; | |
1165 | nr -= 2; | |
1166 | } | |
7116e994 HC |
1167 | if (odd && __get_user(*fdset, ufdset)) |
1168 | return -EFAULT; | |
1da177e4 LT |
1169 | } else { |
1170 | /* Tricky, must clear full unsigned long in the | |
1171 | * kernel fdset at the end, this makes sure that | |
1172 | * actually happens. | |
1173 | */ | |
1174 | memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t)); | |
1175 | } | |
1176 | return 0; | |
1177 | } | |
1178 | ||
858119e1 | 1179 | static |
7116e994 HC |
1180 | int compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset, |
1181 | unsigned long *fdset) | |
1da177e4 LT |
1182 | { |
1183 | unsigned long odd; | |
022a1692 | 1184 | nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS); |
1da177e4 LT |
1185 | |
1186 | if (!ufdset) | |
7116e994 | 1187 | return 0; |
1da177e4 LT |
1188 | |
1189 | odd = nr & 1UL; | |
1190 | nr &= ~1UL; | |
1191 | while (nr) { | |
1192 | unsigned long h, l; | |
1193 | l = *fdset++; | |
1194 | h = l >> 32; | |
7116e994 HC |
1195 | if (__put_user(l, ufdset) || __put_user(h, ufdset+1)) |
1196 | return -EFAULT; | |
1da177e4 LT |
1197 | ufdset += 2; |
1198 | nr -= 2; | |
1199 | } | |
7116e994 HC |
1200 | if (odd && __put_user(*fdset, ufdset)) |
1201 | return -EFAULT; | |
1202 | return 0; | |
1da177e4 LT |
1203 | } |
1204 | ||
1205 | ||
1206 | /* | |
1207 | * This is a virtual copy of sys_select from fs/select.c and probably | |
1208 | * should be compared to it from time to time | |
1209 | */ | |
1da177e4 LT |
1210 | |
1211 | /* | |
1212 | * We can actually return ERESTARTSYS instead of EINTR, but I'd | |
1213 | * like to be certain this leads to no problems. So I return | |
1214 | * EINTR just for safety. | |
1215 | * | |
1216 | * Update: ERESTARTSYS breaks at least the xview clock binary, so | |
1217 | * I'm trying ERESTARTNOHAND which restart only when you want to. | |
1218 | */ | |
9f72949f | 1219 | int compat_core_sys_select(int n, compat_ulong_t __user *inp, |
8ff3e8e8 AV |
1220 | compat_ulong_t __user *outp, compat_ulong_t __user *exp, |
1221 | struct timespec *end_time) | |
1da177e4 LT |
1222 | { |
1223 | fd_set_bits fds; | |
6087b2da | 1224 | void *bits; |
bbea9f69 | 1225 | int size, max_fds, ret = -EINVAL; |
a4531edd | 1226 | struct fdtable *fdt; |
6087b2da | 1227 | long stack_fds[SELECT_STACK_ALLOC/sizeof(long)]; |
1da177e4 | 1228 | |
1da177e4 LT |
1229 | if (n < 0) |
1230 | goto out_nofds; | |
1231 | ||
bbea9f69 | 1232 | /* max_fds can increase, so grab it once to avoid race */ |
ac5b8b6f | 1233 | rcu_read_lock(); |
a4531edd | 1234 | fdt = files_fdtable(current->files); |
bbea9f69 | 1235 | max_fds = fdt->max_fds; |
ac5b8b6f | 1236 | rcu_read_unlock(); |
bbea9f69 VL |
1237 | if (n > max_fds) |
1238 | n = max_fds; | |
1da177e4 LT |
1239 | |
1240 | /* | |
1241 | * We need 6 bitmaps (in/out/ex for both incoming and outgoing), | |
1242 | * since we used fdset we need to allocate memory in units of | |
1243 | * long-words. | |
1244 | */ | |
1da177e4 | 1245 | size = FDS_BYTES(n); |
6087b2da BP |
1246 | bits = stack_fds; |
1247 | if (size > sizeof(stack_fds) / 6) { | |
1248 | bits = kmalloc(6 * size, GFP_KERNEL); | |
1249 | ret = -ENOMEM; | |
1250 | if (!bits) | |
1251 | goto out_nofds; | |
1252 | } | |
1da177e4 LT |
1253 | fds.in = (unsigned long *) bits; |
1254 | fds.out = (unsigned long *) (bits + size); | |
1255 | fds.ex = (unsigned long *) (bits + 2*size); | |
1256 | fds.res_in = (unsigned long *) (bits + 3*size); | |
1257 | fds.res_out = (unsigned long *) (bits + 4*size); | |
1258 | fds.res_ex = (unsigned long *) (bits + 5*size); | |
1259 | ||
1260 | if ((ret = compat_get_fd_set(n, inp, fds.in)) || | |
1261 | (ret = compat_get_fd_set(n, outp, fds.out)) || | |
1262 | (ret = compat_get_fd_set(n, exp, fds.ex))) | |
1263 | goto out; | |
1264 | zero_fd_set(n, fds.res_in); | |
1265 | zero_fd_set(n, fds.res_out); | |
1266 | zero_fd_set(n, fds.res_ex); | |
1267 | ||
8ff3e8e8 | 1268 | ret = do_select(n, &fds, end_time); |
1da177e4 LT |
1269 | |
1270 | if (ret < 0) | |
1271 | goto out; | |
1272 | if (!ret) { | |
1273 | ret = -ERESTARTNOHAND; | |
1274 | if (signal_pending(current)) | |
1275 | goto out; | |
1276 | ret = 0; | |
1277 | } | |
1278 | ||
7116e994 HC |
1279 | if (compat_set_fd_set(n, inp, fds.res_in) || |
1280 | compat_set_fd_set(n, outp, fds.res_out) || | |
1281 | compat_set_fd_set(n, exp, fds.res_ex)) | |
1282 | ret = -EFAULT; | |
1da177e4 | 1283 | out: |
6087b2da BP |
1284 | if (bits != stack_fds) |
1285 | kfree(bits); | |
1da177e4 LT |
1286 | out_nofds: |
1287 | return ret; | |
1288 | } | |
1289 | ||
9f72949f DW |
1290 | asmlinkage long compat_sys_select(int n, compat_ulong_t __user *inp, |
1291 | compat_ulong_t __user *outp, compat_ulong_t __user *exp, | |
1292 | struct compat_timeval __user *tvp) | |
1293 | { | |
8ff3e8e8 | 1294 | struct timespec end_time, *to = NULL; |
9f72949f DW |
1295 | struct compat_timeval tv; |
1296 | int ret; | |
1297 | ||
1298 | if (tvp) { | |
1299 | if (copy_from_user(&tv, tvp, sizeof(tv))) | |
1300 | return -EFAULT; | |
1301 | ||
8ff3e8e8 | 1302 | to = &end_time; |
4d36a9e6 AV |
1303 | if (poll_select_set_timeout(to, |
1304 | tv.tv_sec + (tv.tv_usec / USEC_PER_SEC), | |
1305 | (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC)) | |
9f72949f | 1306 | return -EINVAL; |
9f72949f DW |
1307 | } |
1308 | ||
8ff3e8e8 AV |
1309 | ret = compat_core_sys_select(n, inp, outp, exp, to); |
1310 | ret = poll_select_copy_remaining(&end_time, tvp, 1, ret); | |
9f72949f DW |
1311 | |
1312 | return ret; | |
1313 | } | |
1314 | ||
5d0e5283 CH |
1315 | struct compat_sel_arg_struct { |
1316 | compat_ulong_t n; | |
1317 | compat_uptr_t inp; | |
1318 | compat_uptr_t outp; | |
1319 | compat_uptr_t exp; | |
1320 | compat_uptr_t tvp; | |
1321 | }; | |
1322 | ||
1323 | asmlinkage long compat_sys_old_select(struct compat_sel_arg_struct __user *arg) | |
1324 | { | |
1325 | struct compat_sel_arg_struct a; | |
1326 | ||
1327 | if (copy_from_user(&a, arg, sizeof(a))) | |
1328 | return -EFAULT; | |
1329 | return compat_sys_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp), | |
1330 | compat_ptr(a.exp), compat_ptr(a.tvp)); | |
1331 | } | |
1332 | ||
c9da9f21 | 1333 | static long do_compat_pselect(int n, compat_ulong_t __user *inp, |
9f72949f DW |
1334 | compat_ulong_t __user *outp, compat_ulong_t __user *exp, |
1335 | struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask, | |
1336 | compat_size_t sigsetsize) | |
1337 | { | |
1338 | compat_sigset_t ss32; | |
1339 | sigset_t ksigmask, sigsaved; | |
9f72949f | 1340 | struct compat_timespec ts; |
8ff3e8e8 | 1341 | struct timespec end_time, *to = NULL; |
9f72949f DW |
1342 | int ret; |
1343 | ||
1344 | if (tsp) { | |
1345 | if (copy_from_user(&ts, tsp, sizeof(ts))) | |
1346 | return -EFAULT; | |
1347 | ||
8ff3e8e8 AV |
1348 | to = &end_time; |
1349 | if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec)) | |
9f72949f DW |
1350 | return -EINVAL; |
1351 | } | |
1352 | ||
1353 | if (sigmask) { | |
1354 | if (sigsetsize != sizeof(compat_sigset_t)) | |
1355 | return -EINVAL; | |
1356 | if (copy_from_user(&ss32, sigmask, sizeof(ss32))) | |
1357 | return -EFAULT; | |
1358 | sigset_from_compat(&ksigmask, &ss32); | |
1359 | ||
1360 | sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP)); | |
1361 | sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved); | |
1362 | } | |
1363 | ||
8ff3e8e8 AV |
1364 | ret = compat_core_sys_select(n, inp, outp, exp, to); |
1365 | ret = poll_select_copy_remaining(&end_time, tsp, 0, ret); | |
9f72949f DW |
1366 | |
1367 | if (ret == -ERESTARTNOHAND) { | |
1368 | /* | |
1369 | * Don't restore the signal mask yet. Let do_signal() deliver | |
1370 | * the signal on the way back to userspace, before the signal | |
1371 | * mask is restored. | |
1372 | */ | |
1373 | if (sigmask) { | |
1374 | memcpy(¤t->saved_sigmask, &sigsaved, | |
1375 | sizeof(sigsaved)); | |
4e4c22c7 | 1376 | set_restore_sigmask(); |
9f72949f DW |
1377 | } |
1378 | } else if (sigmask) | |
1379 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); | |
1380 | ||
1381 | return ret; | |
1382 | } | |
1383 | ||
1384 | asmlinkage long compat_sys_pselect6(int n, compat_ulong_t __user *inp, | |
1385 | compat_ulong_t __user *outp, compat_ulong_t __user *exp, | |
1386 | struct compat_timespec __user *tsp, void __user *sig) | |
1387 | { | |
1388 | compat_size_t sigsetsize = 0; | |
1389 | compat_uptr_t up = 0; | |
1390 | ||
1391 | if (sig) { | |
1392 | if (!access_ok(VERIFY_READ, sig, | |
1393 | sizeof(compat_uptr_t)+sizeof(compat_size_t)) || | |
1394 | __get_user(up, (compat_uptr_t __user *)sig) || | |
1395 | __get_user(sigsetsize, | |
1396 | (compat_size_t __user *)(sig+sizeof(up)))) | |
1397 | return -EFAULT; | |
1398 | } | |
c9da9f21 HC |
1399 | return do_compat_pselect(n, inp, outp, exp, tsp, compat_ptr(up), |
1400 | sigsetsize); | |
9f72949f DW |
1401 | } |
1402 | ||
1403 | asmlinkage long compat_sys_ppoll(struct pollfd __user *ufds, | |
1404 | unsigned int nfds, struct compat_timespec __user *tsp, | |
1405 | const compat_sigset_t __user *sigmask, compat_size_t sigsetsize) | |
1406 | { | |
1407 | compat_sigset_t ss32; | |
1408 | sigset_t ksigmask, sigsaved; | |
1409 | struct compat_timespec ts; | |
8ff3e8e8 | 1410 | struct timespec end_time, *to = NULL; |
9f72949f DW |
1411 | int ret; |
1412 | ||
1413 | if (tsp) { | |
1414 | if (copy_from_user(&ts, tsp, sizeof(ts))) | |
1415 | return -EFAULT; | |
1416 | ||
8ff3e8e8 AV |
1417 | to = &end_time; |
1418 | if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec)) | |
1419 | return -EINVAL; | |
9f72949f DW |
1420 | } |
1421 | ||
1422 | if (sigmask) { | |
3835a9bd | 1423 | if (sigsetsize != sizeof(compat_sigset_t)) |
9f72949f DW |
1424 | return -EINVAL; |
1425 | if (copy_from_user(&ss32, sigmask, sizeof(ss32))) | |
1426 | return -EFAULT; | |
1427 | sigset_from_compat(&ksigmask, &ss32); | |
1428 | ||
1429 | sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP)); | |
1430 | sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved); | |
1431 | } | |
1432 | ||
8ff3e8e8 | 1433 | ret = do_sys_poll(ufds, nfds, to); |
9f72949f DW |
1434 | |
1435 | /* We can restart this syscall, usually */ | |
1436 | if (ret == -EINTR) { | |
1437 | /* | |
1438 | * Don't restore the signal mask yet. Let do_signal() deliver | |
1439 | * the signal on the way back to userspace, before the signal | |
1440 | * mask is restored. | |
1441 | */ | |
1442 | if (sigmask) { | |
1443 | memcpy(¤t->saved_sigmask, &sigsaved, | |
1444 | sizeof(sigsaved)); | |
4e4c22c7 | 1445 | set_restore_sigmask(); |
9f72949f DW |
1446 | } |
1447 | ret = -ERESTARTNOHAND; | |
1448 | } else if (sigmask) | |
1449 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); | |
1450 | ||
8ff3e8e8 | 1451 | ret = poll_select_copy_remaining(&end_time, tsp, 0, ret); |
9f72949f DW |
1452 | |
1453 | return ret; | |
1454 | } | |
9f72949f | 1455 | |
becfd1f3 AK |
1456 | #ifdef CONFIG_FHANDLE |
1457 | /* | |
1458 | * Exactly like fs/open.c:sys_open_by_handle_at(), except that it | |
1459 | * doesn't set the O_LARGEFILE flag. | |
1460 | */ | |
f482e1b4 AV |
1461 | COMPAT_SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd, |
1462 | struct file_handle __user *, handle, int, flags) | |
becfd1f3 AK |
1463 | { |
1464 | return do_handle_open(mountdirfd, handle, flags); | |
1465 | } | |
1466 | #endif |