blob: c61b506f5bc94ba0dcac0d9a28a7ab1070ac3a66 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/compat.c
3 *
4 * Kernel compatibililty routines for e.g. 32 bit syscall support
5 * on 64 bit kernels.
6 *
7 * Copyright (C) 2002 Stephen Rothwell, IBM Corporation
8 * Copyright (C) 1997-2000 Jakub Jelinek (jakub@redhat.com)
9 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
10 * Copyright (C) 2001,2002 Andi Kleen, SuSE Labs
Pavel Macheka2531292010-07-18 14:27:13 +020011 * Copyright (C) 2003 Pavel Machek (pavel@ucw.cz)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
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
Kevin Winchester85c9fe82010-08-09 17:20:22 -070018#include <linux/stddef.h>
Milind Arun Choudhary022a1692007-05-08 00:29:02 -070019#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/linkage.h>
21#include <linux/compat.h>
22#include <linux/errno.h>
23#include <linux/time.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010024#include <linux/cred.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/fs.h>
26#include <linux/fcntl.h>
27#include <linux/namei.h>
28#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040029#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/vfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/ioctl.h>
32#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/ncp_mount.h>
David Howells9a9947b2005-04-18 10:54:51 -070034#include <linux/nfs4_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/syscalls.h>
36#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/dirent.h>
Robert Love0eeca282005-07-12 17:06:03 -040038#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/highuid.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/personality.h>
41#include <linux/rwsem.h>
Jay Lan8f0ab512006-09-30 23:28:59 -070042#include <linux/tsacct_kern.h>
Christoph Hellwig6272e262007-05-08 00:29:21 -070043#include <linux/security.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040044#include <linux/highmem.h>
Davide Libenzi6d18c922007-05-10 22:23:15 -070045#include <linux/signal.h>
Al Virobd01f842006-10-19 17:23:57 -040046#include <linux/poll.h>
David S. Miller4a805e82005-09-14 21:40:00 -070047#include <linux/mm.h>
Al Viro498052b2009-03-30 07:20:30 -040048#include <linux/fs_struct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
wu zhangjin504b7012010-10-30 08:19:35 -070050#include <linux/pagemap.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070051#include <linux/aio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080053#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/mmu_context.h>
55#include <asm/ioctls.h>
David Howells07f3f05c2006-09-30 20:52:18 +020056#include "internal.h"
David Woodhouse9f729492006-01-18 17:44:05 -080057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/*
59 * Not all architectures have sys_utime, so implement this in terms
60 * of sys_utimes.
61 */
Heiko Carstens625b1d72014-03-04 10:53:50 +010062COMPAT_SYSCALL_DEFINE2(utime, const char __user *, filename,
63 struct compat_utimbuf __user *, t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Ulrich Drepper1c710c82007-05-08 00:33:25 -070065 struct timespec tv[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 if (t) {
68 if (get_user(tv[0].tv_sec, &t->actime) ||
69 get_user(tv[1].tv_sec, &t->modtime))
70 return -EFAULT;
Ulrich Drepper1c710c82007-05-08 00:33:25 -070071 tv[0].tv_nsec = 0;
72 tv[1].tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
Ulrich Drepper1c710c82007-05-08 00:33:25 -070074 return do_utimes(AT_FDCWD, filename, t ? tv : NULL, 0);
75}
76
Heiko Carstens625b1d72014-03-04 10:53:50 +010077COMPAT_SYSCALL_DEFINE4(utimensat, unsigned int, dfd, const char __user *, filename, struct compat_timespec __user *, t, int, flags)
Ulrich Drepper1c710c82007-05-08 00:33:25 -070078{
79 struct timespec tv[2];
80
81 if (t) {
H. Peter Anvin81993e82014-02-01 18:54:11 -080082 if (compat_get_timespec(&tv[0], &t[0]) ||
83 compat_get_timespec(&tv[1], &t[1]))
Ulrich Drepper1c710c82007-05-08 00:33:25 -070084 return -EFAULT;
85
Ulrich Drepper1c710c82007-05-08 00:33:25 -070086 if (tv[0].tv_nsec == UTIME_OMIT && tv[1].tv_nsec == UTIME_OMIT)
87 return 0;
88 }
89 return do_utimes(dfd, filename, t ? tv : NULL, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Heiko Carstens625b1d72014-03-04 10:53:50 +010092COMPAT_SYSCALL_DEFINE3(futimesat, unsigned int, dfd, const char __user *, filename, struct compat_timeval __user *, t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Ulrich Drepper1c710c82007-05-08 00:33:25 -070094 struct timespec tv[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +110096 if (t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
Ulrich Drepper1c710c82007-05-08 00:33:25 -070098 get_user(tv[0].tv_nsec, &t[0].tv_usec) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 get_user(tv[1].tv_sec, &t[1].tv_sec) ||
Ulrich Drepper1c710c82007-05-08 00:33:25 -0700100 get_user(tv[1].tv_nsec, &t[1].tv_usec))
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +1100101 return -EFAULT;
Ulrich Drepper1c710c82007-05-08 00:33:25 -0700102 if (tv[0].tv_nsec >= 1000000 || tv[0].tv_nsec < 0 ||
103 tv[1].tv_nsec >= 1000000 || tv[1].tv_nsec < 0)
104 return -EINVAL;
105 tv[0].tv_nsec *= 1000;
106 tv[1].tv_nsec *= 1000;
Stephen Rothwell9ad11ab2006-02-02 16:11:51 +1100107 }
Ulrich Drepper1c710c82007-05-08 00:33:25 -0700108 return do_utimes(dfd, filename, t ? tv : NULL, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800109}
110
Heiko Carstens625b1d72014-03-04 10:53:50 +0100111COMPAT_SYSCALL_DEFINE2(utimes, const char __user *, filename, struct compat_timeval __user *, t)
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800112{
113 return compat_sys_futimesat(AT_FDCWD, filename, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Christoph Hellwigf7a50002008-10-15 22:02:05 -0700116static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
117{
Al Virofcf83062012-02-12 20:56:29 -0500118 struct compat_stat tmp;
Christoph Hellwigf7a50002008-10-15 22:02:05 -0700119
Al Virofcf83062012-02-12 20:56:29 -0500120 if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
Christoph Hellwigf7a50002008-10-15 22:02:05 -0700121 return -EOVERFLOW;
122
Al Virofcf83062012-02-12 20:56:29 -0500123 memset(&tmp, 0, sizeof(tmp));
124 tmp.st_dev = old_encode_dev(stat->dev);
125 tmp.st_ino = stat->ino;
126 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
127 return -EOVERFLOW;
128 tmp.st_mode = stat->mode;
129 tmp.st_nlink = stat->nlink;
130 if (tmp.st_nlink != stat->nlink)
131 return -EOVERFLOW;
Eric W. Biedermana7c19382012-02-09 09:10:30 -0800132 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
133 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
Al Virofcf83062012-02-12 20:56:29 -0500134 tmp.st_rdev = old_encode_dev(stat->rdev);
135 if ((u64) stat->size > MAX_NON_LFS)
136 return -EOVERFLOW;
137 tmp.st_size = stat->size;
138 tmp.st_atime = stat->atime.tv_sec;
139 tmp.st_atime_nsec = stat->atime.tv_nsec;
140 tmp.st_mtime = stat->mtime.tv_sec;
141 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
142 tmp.st_ctime = stat->ctime.tv_sec;
143 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
144 tmp.st_blocks = stat->blocks;
145 tmp.st_blksize = stat->blksize;
146 return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
Christoph Hellwigf7a50002008-10-15 22:02:05 -0700147}
148
Heiko Carstens625b1d72014-03-04 10:53:50 +0100149COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename,
150 struct compat_stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 struct kstat stat;
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400153 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400155 error = vfs_stat(filename, &stat);
156 if (error)
157 return error;
158 return cp_compat_stat(&stat, statbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Heiko Carstens625b1d72014-03-04 10:53:50 +0100161COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename,
162 struct compat_stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 struct kstat stat;
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400165 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400167 error = vfs_lstat(filename, &stat);
168 if (error)
169 return error;
170 return cp_compat_stat(&stat, statbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Kyle McMartin82d821d2006-03-24 03:18:20 -0800173#ifndef __ARCH_WANT_STAT64
Heiko Carstens625b1d72014-03-04 10:53:50 +0100174COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
175 const char __user *, filename,
176 struct compat_stat __user *, statbuf, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800177{
178 struct kstat stat;
Oleg Drokin0112fc22009-04-08 20:05:42 +0400179 int error;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800180
Oleg Drokin0112fc22009-04-08 20:05:42 +0400181 error = vfs_fstatat(dfd, filename, &stat, flag);
182 if (error)
183 return error;
184 return cp_compat_stat(&stat, statbuf);
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800185}
Kyle McMartin82d821d2006-03-24 03:18:20 -0800186#endif
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800187
Heiko Carstens625b1d72014-03-04 10:53:50 +0100188COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd,
189 struct compat_stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 struct kstat stat;
192 int error = vfs_fstat(fd, &stat);
193
194 if (!error)
195 error = cp_compat_stat(&stat, statbuf);
196 return error;
197}
198
199static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
200{
201
202 if (sizeof ubuf->f_blocks == 4) {
Jon Tollefsonf4a67cc2008-07-23 21:27:55 -0700203 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail |
204 kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return -EOVERFLOW;
206 /* f_files and f_ffree may be -1; it's okay
207 * to stuff that into 32 bits */
208 if (kbuf->f_files != 0xffffffffffffffffULL
209 && (kbuf->f_files & 0xffffffff00000000ULL))
210 return -EOVERFLOW;
211 if (kbuf->f_ffree != 0xffffffffffffffffULL
212 && (kbuf->f_ffree & 0xffffffff00000000ULL))
213 return -EOVERFLOW;
214 }
215 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
216 __put_user(kbuf->f_type, &ubuf->f_type) ||
217 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
218 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
219 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
220 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
221 __put_user(kbuf->f_files, &ubuf->f_files) ||
222 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
223 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
224 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
225 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
226 __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
Eric W. Biederman1448c722011-10-17 13:40:02 -0700227 __put_user(kbuf->f_flags, &ubuf->f_flags) ||
228 __clear_user(ubuf->f_spare, sizeof(ubuf->f_spare)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return -EFAULT;
230 return 0;
231}
232
233/*
Namhyung Kim974d8792010-12-27 01:41:53 +0900234 * The following statfs calls are copies of code from fs/statfs.c and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 * should be checked against those from time to time
236 */
Heiko Carstens625b1d72014-03-04 10:53:50 +0100237COMPAT_SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct compat_statfs __user *, buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Al Viroc8b91ac2011-03-12 10:41:39 -0500239 struct kstatfs tmp;
240 int error = user_statfs(pathname, &tmp);
241 if (!error)
242 error = put_compat_statfs(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return error;
244}
245
Heiko Carstens625b1d72014-03-04 10:53:50 +0100246COMPAT_SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct compat_statfs __user *, buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 struct kstatfs tmp;
Al Viroc8b91ac2011-03-12 10:41:39 -0500249 int error = fd_statfs(fd, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800250 if (!error)
251 error = put_compat_statfs(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return error;
253}
254
255static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
256{
Li Wang64d2ab32016-11-15 17:19:01 +0800257 if (sizeof(ubuf->f_bsize) == 4) {
258 if ((kbuf->f_type | kbuf->f_bsize | kbuf->f_namelen |
259 kbuf->f_frsize | kbuf->f_flags) & 0xffffffff00000000ULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return -EOVERFLOW;
261 /* f_files and f_ffree may be -1; it's okay
262 * to stuff that into 32 bits */
263 if (kbuf->f_files != 0xffffffffffffffffULL
264 && (kbuf->f_files & 0xffffffff00000000ULL))
265 return -EOVERFLOW;
266 if (kbuf->f_ffree != 0xffffffffffffffffULL
267 && (kbuf->f_ffree & 0xffffffff00000000ULL))
268 return -EOVERFLOW;
269 }
270 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
271 __put_user(kbuf->f_type, &ubuf->f_type) ||
272 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
273 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
274 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
275 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
276 __put_user(kbuf->f_files, &ubuf->f_files) ||
277 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
278 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
279 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
280 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
Namhyung Kime0bb6bd2010-12-27 01:41:54 +0900281 __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
282 __put_user(kbuf->f_flags, &ubuf->f_flags) ||
283 __clear_user(ubuf->f_spare, sizeof(ubuf->f_spare)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return -EFAULT;
285 return 0;
286}
287
Heiko Carstens625b1d72014-03-04 10:53:50 +0100288COMPAT_SYSCALL_DEFINE3(statfs64, const char __user *, pathname, compat_size_t, sz, struct compat_statfs64 __user *, buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 struct kstatfs tmp;
291 int error;
292
293 if (sz != sizeof(*buf))
294 return -EINVAL;
295
Al Viroc8b91ac2011-03-12 10:41:39 -0500296 error = user_statfs(pathname, &tmp);
David Gibson86e07ce2005-11-21 21:32:23 -0800297 if (!error)
298 error = put_compat_statfs64(buf, &tmp);
Al Viroc8b91ac2011-03-12 10:41:39 -0500299 return error;
300}
301
Heiko Carstens625b1d72014-03-04 10:53:50 +0100302COMPAT_SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, compat_size_t, sz, struct compat_statfs64 __user *, buf)
Al Viroc8b91ac2011-03-12 10:41:39 -0500303{
304 struct kstatfs tmp;
305 int error;
306
307 if (sz != sizeof(*buf))
308 return -EINVAL;
309
310 error = fd_statfs(fd, &tmp);
311 if (!error)
312 error = put_compat_statfs64(buf, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return error;
314}
315
Christoph Hellwig2b1c6bd2008-11-28 10:09:09 +0100316/*
317 * This is a copy of sys_ustat, just dealing with a structure layout.
318 * Given how simple this syscall is that apporach is more maintainable
319 * than the various conversion hacks.
320 */
Heiko Carstens625b1d72014-03-04 10:53:50 +0100321COMPAT_SYSCALL_DEFINE2(ustat, unsigned, dev, struct compat_ustat __user *, u)
Christoph Hellwig2b1c6bd2008-11-28 10:09:09 +0100322{
Christoph Hellwig2b1c6bd2008-11-28 10:09:09 +0100323 struct compat_ustat tmp;
324 struct kstatfs sbuf;
Al Virocf31e702012-01-02 22:28:36 -0500325 int err = vfs_ustat(new_decode_dev(dev), &sbuf);
Christoph Hellwig2b1c6bd2008-11-28 10:09:09 +0100326 if (err)
327 return err;
328
329 memset(&tmp, 0, sizeof(struct compat_ustat));
330 tmp.f_tfree = sbuf.f_bfree;
331 tmp.f_tinode = sbuf.f_ffree;
332 if (copy_to_user(u, &tmp, sizeof(struct compat_ustat)))
333 return -EFAULT;
334 return 0;
335}
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
338{
339 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
340 __get_user(kfl->l_type, &ufl->l_type) ||
341 __get_user(kfl->l_whence, &ufl->l_whence) ||
342 __get_user(kfl->l_start, &ufl->l_start) ||
343 __get_user(kfl->l_len, &ufl->l_len) ||
344 __get_user(kfl->l_pid, &ufl->l_pid))
345 return -EFAULT;
346 return 0;
347}
348
349static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
350{
351 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
352 __put_user(kfl->l_type, &ufl->l_type) ||
353 __put_user(kfl->l_whence, &ufl->l_whence) ||
354 __put_user(kfl->l_start, &ufl->l_start) ||
355 __put_user(kfl->l_len, &ufl->l_len) ||
356 __put_user(kfl->l_pid, &ufl->l_pid))
357 return -EFAULT;
358 return 0;
359}
360
361#ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
362static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
363{
364 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
365 __get_user(kfl->l_type, &ufl->l_type) ||
366 __get_user(kfl->l_whence, &ufl->l_whence) ||
367 __get_user(kfl->l_start, &ufl->l_start) ||
368 __get_user(kfl->l_len, &ufl->l_len) ||
369 __get_user(kfl->l_pid, &ufl->l_pid))
370 return -EFAULT;
371 return 0;
372}
373#endif
374
375#ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
376static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
377{
378 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
379 __put_user(kfl->l_type, &ufl->l_type) ||
380 __put_user(kfl->l_whence, &ufl->l_whence) ||
381 __put_user(kfl->l_start, &ufl->l_start) ||
382 __put_user(kfl->l_len, &ufl->l_len) ||
383 __put_user(kfl->l_pid, &ufl->l_pid))
384 return -EFAULT;
385 return 0;
386}
387#endif
388
Jeff Layton5d50ffd2014-02-03 12:13:10 -0500389static unsigned int
390convert_fcntl_cmd(unsigned int cmd)
391{
392 switch (cmd) {
393 case F_GETLK64:
394 return F_GETLK;
395 case F_SETLK64:
396 return F_SETLK;
397 case F_SETLKW64:
398 return F_SETLKW;
399 }
400
401 return cmd;
402}
403
Heiko Carstens932602e2014-03-04 16:07:52 +0100404COMPAT_SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
405 compat_ulong_t, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 mm_segment_t old_fs;
408 struct flock f;
409 long ret;
Jeff Layton5d50ffd2014-02-03 12:13:10 -0500410 unsigned int conv_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 switch (cmd) {
413 case F_GETLK:
414 case F_SETLK:
415 case F_SETLKW:
416 ret = get_compat_flock(&f, compat_ptr(arg));
417 if (ret != 0)
418 break;
419 old_fs = get_fs();
420 set_fs(KERNEL_DS);
421 ret = sys_fcntl(fd, cmd, (unsigned long)&f);
422 set_fs(old_fs);
423 if (cmd == F_GETLK && ret == 0) {
Nikanth Karthikesanff677f82009-04-01 14:40:51 +0530424 /* GETLK was successful and we need to return the data...
NeilBrown2520f142006-01-08 01:02:40 -0800425 * but it needs to fit in the compat structure.
426 * l_start shouldn't be too big, unless the original
427 * start + end is greater than COMPAT_OFF_T_MAX, in which
428 * case the app was asking for trouble, so we return
429 * -EOVERFLOW in that case.
430 * l_len could be too big, in which case we just truncate it,
431 * and only allow the app to see that part of the conflicting
432 * lock that might make sense to it anyway
433 */
434
435 if (f.l_start > COMPAT_OFF_T_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 ret = -EOVERFLOW;
NeilBrown2520f142006-01-08 01:02:40 -0800437 if (f.l_len > COMPAT_OFF_T_MAX)
438 f.l_len = COMPAT_OFF_T_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (ret == 0)
440 ret = put_compat_flock(&f, compat_ptr(arg));
441 }
442 break;
443
444 case F_GETLK64:
445 case F_SETLK64:
446 case F_SETLKW64:
Jeff Layton0d3f7a22014-04-22 08:23:58 -0400447 case F_OFD_GETLK:
448 case F_OFD_SETLK:
449 case F_OFD_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 ret = get_compat_flock64(&f, compat_ptr(arg));
451 if (ret != 0)
452 break;
453 old_fs = get_fs();
454 set_fs(KERNEL_DS);
Jeff Layton5d50ffd2014-02-03 12:13:10 -0500455 conv_cmd = convert_fcntl_cmd(cmd);
456 ret = sys_fcntl(fd, conv_cmd, (unsigned long)&f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 set_fs(old_fs);
Jeff Layton0d3f7a22014-04-22 08:23:58 -0400458 if ((conv_cmd == F_GETLK || conv_cmd == F_OFD_GETLK) && ret == 0) {
NeilBrown2520f142006-01-08 01:02:40 -0800459 /* need to return lock information - see above for commentary */
460 if (f.l_start > COMPAT_LOFF_T_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 ret = -EOVERFLOW;
NeilBrown2520f142006-01-08 01:02:40 -0800462 if (f.l_len > COMPAT_LOFF_T_MAX)
463 f.l_len = COMPAT_LOFF_T_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (ret == 0)
465 ret = put_compat_flock64(&f, compat_ptr(arg));
466 }
467 break;
468
469 default:
470 ret = sys_fcntl(fd, cmd, arg);
471 break;
472 }
473 return ret;
474}
475
Heiko Carstens932602e2014-03-04 16:07:52 +0100476COMPAT_SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd,
477 compat_ulong_t, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Jeff Layton5d50ffd2014-02-03 12:13:10 -0500479 switch (cmd) {
480 case F_GETLK64:
481 case F_SETLK64:
482 case F_SETLKW64:
Jeff Layton0d3f7a22014-04-22 08:23:58 -0400483 case F_OFD_GETLK:
484 case F_OFD_SETLK:
485 case F_OFD_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return -EINVAL;
Jeff Layton5d50ffd2014-02-03 12:13:10 -0500487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return compat_sys_fcntl64(fd, cmd, arg);
489}
490
Jeff Moyerb8373362010-05-26 14:44:25 -0700491/* A write operation does a read from user space and vice versa */
492#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
493
494ssize_t compat_rw_copy_check_uvector(int type,
495 const struct compat_iovec __user *uvector, unsigned long nr_segs,
496 unsigned long fast_segs, struct iovec *fast_pointer,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700497 struct iovec **ret_pointer)
Jeff Moyerb8373362010-05-26 14:44:25 -0700498{
499 compat_ssize_t tot_len;
500 struct iovec *iov = *ret_pointer = fast_pointer;
501 ssize_t ret = 0;
502 int seg;
503
504 /*
505 * SuS says "The readv() function *may* fail if the iovcnt argument
506 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
507 * traditionally returned zero for zero segments, so...
508 */
509 if (nr_segs == 0)
510 goto out;
511
512 ret = -EINVAL;
Shawn Lin85e73402016-08-22 15:45:59 +0800513 if (nr_segs > UIO_MAXIOV)
Jeff Moyerb8373362010-05-26 14:44:25 -0700514 goto out;
515 if (nr_segs > fast_segs) {
516 ret = -ENOMEM;
517 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
Namhyung Kim6a5640f2010-12-27 01:41:52 +0900518 if (iov == NULL)
Jeff Moyerb8373362010-05-26 14:44:25 -0700519 goto out;
Jeff Moyerb8373362010-05-26 14:44:25 -0700520 }
521 *ret_pointer = iov;
522
Mathieu Desnoyers8aec0f52013-02-25 10:20:36 -0500523 ret = -EFAULT;
524 if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
525 goto out;
526
Jeff Moyerb8373362010-05-26 14:44:25 -0700527 /*
528 * Single unix specification:
529 * We should -EINVAL if an element length is not >= 0 and fitting an
Linus Torvalds435f49a2010-10-29 10:36:49 -0700530 * ssize_t.
Jeff Moyerb8373362010-05-26 14:44:25 -0700531 *
Linus Torvalds435f49a2010-10-29 10:36:49 -0700532 * In Linux, the total length is limited to MAX_RW_COUNT, there is
533 * no overflow possibility.
Jeff Moyerb8373362010-05-26 14:44:25 -0700534 */
535 tot_len = 0;
536 ret = -EINVAL;
537 for (seg = 0; seg < nr_segs; seg++) {
Jeff Moyerb8373362010-05-26 14:44:25 -0700538 compat_uptr_t buf;
539 compat_ssize_t len;
540
541 if (__get_user(len, &uvector->iov_len) ||
542 __get_user(buf, &uvector->iov_base)) {
543 ret = -EFAULT;
544 goto out;
545 }
546 if (len < 0) /* size_t not fitting in compat_ssize_t .. */
547 goto out;
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700548 if (type >= 0 &&
Christopher Yeohfcf63402011-10-31 17:06:39 -0700549 !access_ok(vrfy_dir(type), compat_ptr(buf), len)) {
Jeff Moyerb8373362010-05-26 14:44:25 -0700550 ret = -EFAULT;
551 goto out;
552 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700553 if (len > MAX_RW_COUNT - tot_len)
554 len = MAX_RW_COUNT - tot_len;
555 tot_len += len;
Jeff Moyerb8373362010-05-26 14:44:25 -0700556 iov->iov_base = compat_ptr(buf);
557 iov->iov_len = (compat_size_t) len;
558 uvector++;
559 iov++;
560 }
561 ret = tot_len;
562
563out:
564 return ret;
565}
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567struct compat_ncp_mount_data {
568 compat_int_t version;
569 compat_uint_t ncp_fd;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700570 __compat_uid_t mounted_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 compat_pid_t wdog_pid;
572 unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
573 compat_uint_t time_out;
574 compat_uint_t retry_count;
575 compat_uint_t flags;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700576 __compat_uid_t uid;
577 __compat_gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 compat_mode_t file_mode;
579 compat_mode_t dir_mode;
580};
581
582struct compat_ncp_mount_data_v4 {
583 compat_int_t version;
584 compat_ulong_t flags;
585 compat_ulong_t mounted_uid;
586 compat_long_t wdog_pid;
587 compat_uint_t ncp_fd;
588 compat_uint_t time_out;
589 compat_uint_t retry_count;
590 compat_ulong_t uid;
591 compat_ulong_t gid;
592 compat_ulong_t file_mode;
593 compat_ulong_t dir_mode;
594};
595
596static void *do_ncp_super_data_conv(void *raw_data)
597{
598 int version = *(unsigned int *)raw_data;
599
600 if (version == 3) {
601 struct compat_ncp_mount_data *c_n = raw_data;
602 struct ncp_mount_data *n = raw_data;
603
604 n->dir_mode = c_n->dir_mode;
605 n->file_mode = c_n->file_mode;
606 n->gid = c_n->gid;
607 n->uid = c_n->uid;
608 memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
609 n->wdog_pid = c_n->wdog_pid;
610 n->mounted_uid = c_n->mounted_uid;
611 } else if (version == 4) {
612 struct compat_ncp_mount_data_v4 *c_n = raw_data;
613 struct ncp_mount_data_v4 *n = raw_data;
614
615 n->dir_mode = c_n->dir_mode;
616 n->file_mode = c_n->file_mode;
617 n->gid = c_n->gid;
618 n->uid = c_n->uid;
619 n->retry_count = c_n->retry_count;
620 n->time_out = c_n->time_out;
621 n->ncp_fd = c_n->ncp_fd;
622 n->wdog_pid = c_n->wdog_pid;
623 n->mounted_uid = c_n->mounted_uid;
624 n->flags = c_n->flags;
625 } else if (version != 5) {
626 return NULL;
627 }
628
629 return raw_data;
630}
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
David Howells9a9947b2005-04-18 10:54:51 -0700633struct compat_nfs_string {
634 compat_uint_t len;
David Howells5fc3e622005-04-27 15:39:03 -0700635 compat_uptr_t data;
David Howells9a9947b2005-04-18 10:54:51 -0700636};
637
638static inline void compat_nfs_string(struct nfs_string *dst,
639 struct compat_nfs_string *src)
640{
641 dst->data = compat_ptr(src->data);
642 dst->len = src->len;
643}
644
645struct compat_nfs4_mount_data_v1 {
646 compat_int_t version;
647 compat_int_t flags;
648 compat_int_t rsize;
649 compat_int_t wsize;
650 compat_int_t timeo;
651 compat_int_t retrans;
652 compat_int_t acregmin;
653 compat_int_t acregmax;
654 compat_int_t acdirmin;
655 compat_int_t acdirmax;
656 struct compat_nfs_string client_addr;
657 struct compat_nfs_string mnt_path;
658 struct compat_nfs_string hostname;
659 compat_uint_t host_addrlen;
David Howells5fc3e622005-04-27 15:39:03 -0700660 compat_uptr_t host_addr;
David Howells9a9947b2005-04-18 10:54:51 -0700661 compat_int_t proto;
662 compat_int_t auth_flavourlen;
David Howells5fc3e622005-04-27 15:39:03 -0700663 compat_uptr_t auth_flavours;
David Howells9a9947b2005-04-18 10:54:51 -0700664};
665
666static int do_nfs4_super_data_conv(void *raw_data)
667{
668 int version = *(compat_uint_t *) raw_data;
669
670 if (version == 1) {
671 struct compat_nfs4_mount_data_v1 *raw = raw_data;
672 struct nfs4_mount_data *real = raw_data;
673
674 /* copy the fields backwards */
675 real->auth_flavours = compat_ptr(raw->auth_flavours);
676 real->auth_flavourlen = raw->auth_flavourlen;
677 real->proto = raw->proto;
678 real->host_addr = compat_ptr(raw->host_addr);
679 real->host_addrlen = raw->host_addrlen;
680 compat_nfs_string(&real->hostname, &raw->hostname);
681 compat_nfs_string(&real->mnt_path, &raw->mnt_path);
682 compat_nfs_string(&real->client_addr, &raw->client_addr);
683 real->acdirmax = raw->acdirmax;
684 real->acdirmin = raw->acdirmin;
685 real->acregmax = raw->acregmax;
686 real->acregmin = raw->acregmin;
687 real->retrans = raw->retrans;
688 real->timeo = raw->timeo;
689 real->wsize = raw->wsize;
690 real->rsize = raw->rsize;
691 real->flags = raw->flags;
692 real->version = raw->version;
693 }
David Howells9a9947b2005-04-18 10:54:51 -0700694
695 return 0;
696}
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698#define NCPFS_NAME "ncpfs"
David Howells9a9947b2005-04-18 10:54:51 -0700699#define NFS4_NAME "nfs4"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Heiko Carstens932602e2014-03-04 16:07:52 +0100701COMPAT_SYSCALL_DEFINE5(mount, const char __user *, dev_name,
702 const char __user *, dir_name,
703 const char __user *, type, compat_ulong_t, flags,
704 const void __user *, data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Vegard Nossumeca6f532009-09-18 13:05:45 -0700706 char *kernel_type;
Al Virob40ef862015-12-14 18:44:44 -0500707 void *options;
Vegard Nossumeca6f532009-09-18 13:05:45 -0700708 char *kernel_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 int retval;
710
Tim Gardnerb8850d12014-08-28 11:26:03 -0600711 kernel_type = copy_mount_string(type);
712 retval = PTR_ERR(kernel_type);
713 if (IS_ERR(kernel_type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 goto out;
715
Tim Gardnerb8850d12014-08-28 11:26:03 -0600716 kernel_dev = copy_mount_string(dev_name);
717 retval = PTR_ERR(kernel_dev);
718 if (IS_ERR(kernel_dev))
Seunghun Lee5e6123f2014-09-14 22:15:10 +0900719 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Al Virob40ef862015-12-14 18:44:44 -0500721 options = copy_mount_options(data);
722 retval = PTR_ERR(options);
723 if (IS_ERR(options))
Seunghun Lee5e6123f2014-09-14 22:15:10 +0900724 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Al Virob40ef862015-12-14 18:44:44 -0500726 if (kernel_type && options) {
Arnd Bergmann2116b7a2010-10-04 22:55:57 +0200727 if (!strcmp(kernel_type, NCPFS_NAME)) {
Al Virob40ef862015-12-14 18:44:44 -0500728 do_ncp_super_data_conv(options);
Vegard Nossumeca6f532009-09-18 13:05:45 -0700729 } else if (!strcmp(kernel_type, NFS4_NAME)) {
Al Virob40ef862015-12-14 18:44:44 -0500730 retval = -EINVAL;
731 if (do_nfs4_super_data_conv(options))
Seunghun Lee5e6123f2014-09-14 22:15:10 +0900732 goto out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734 }
735
Al Virob40ef862015-12-14 18:44:44 -0500736 retval = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 out3:
Al Virob40ef862015-12-14 18:44:44 -0500739 kfree(options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 out2:
Seunghun Lee5e6123f2014-09-14 22:15:10 +0900741 kfree(kernel_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 out1:
Vegard Nossumeca6f532009-09-18 13:05:45 -0700743 kfree(kernel_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 out:
745 return retval;
746}
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748struct compat_old_linux_dirent {
749 compat_ulong_t d_ino;
750 compat_ulong_t d_offset;
751 unsigned short d_namlen;
752 char d_name[1];
753};
754
755struct compat_readdir_callback {
Al Viro5c0ba4e2013-05-15 13:52:59 -0400756 struct dir_context ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 struct compat_old_linux_dirent __user *dirent;
758 int result;
759};
760
Miklos Szerediac7576f2014-10-30 17:37:34 +0100761static int compat_fillonedir(struct dir_context *ctx, const char *name,
762 int namlen, loff_t offset, u64 ino,
763 unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Miklos Szerediac7576f2014-10-30 17:37:34 +0100765 struct compat_readdir_callback *buf =
766 container_of(ctx, struct compat_readdir_callback, ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 struct compat_old_linux_dirent __user *dirent;
David Howellsafefdbb2006-10-03 01:13:46 -0700768 compat_ulong_t d_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 if (buf->result)
771 return -EINVAL;
David Howellsafefdbb2006-10-03 01:13:46 -0700772 d_ino = ino;
Al Viro8f3f6552008-08-12 00:28:24 -0400773 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
774 buf->result = -EOVERFLOW;
David Howellsafefdbb2006-10-03 01:13:46 -0700775 return -EOVERFLOW;
Al Viro8f3f6552008-08-12 00:28:24 -0400776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 buf->result++;
778 dirent = buf->dirent;
779 if (!access_ok(VERIFY_WRITE, dirent,
780 (unsigned long)(dirent->d_name + namlen + 1) -
781 (unsigned long)dirent))
782 goto efault;
David Howellsafefdbb2006-10-03 01:13:46 -0700783 if ( __put_user(d_ino, &dirent->d_ino) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 __put_user(offset, &dirent->d_offset) ||
785 __put_user(namlen, &dirent->d_namlen) ||
786 __copy_to_user(dirent->d_name, name, namlen) ||
787 __put_user(0, dirent->d_name + namlen))
788 goto efault;
789 return 0;
790efault:
791 buf->result = -EFAULT;
792 return -EFAULT;
793}
794
Heiko Carstens625b1d72014-03-04 10:53:50 +0100795COMPAT_SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
796 struct compat_old_linux_dirent __user *, dirent, unsigned int, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
798 int error;
Al Viro63b6df12016-04-20 17:08:21 -0400799 struct fd f = fdget_pos(fd);
Al Viroac6614b2013-05-22 22:22:04 -0400800 struct compat_readdir_callback buf = {
801 .ctx.actor = compat_fillonedir,
802 .dirent = dirent
803 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Al Viro2903ff02012-08-28 12:52:22 -0400805 if (!f.file)
Al Viro863ced72012-04-21 18:40:32 -0400806 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Al Viro5c0ba4e2013-05-15 13:52:59 -0400808 error = iterate_dir(f.file, &buf.ctx);
Al Viro53c9c5c2008-08-24 07:29:52 -0400809 if (buf.result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 error = buf.result;
811
Al Viro63b6df12016-04-20 17:08:21 -0400812 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return error;
814}
815
816struct compat_linux_dirent {
817 compat_ulong_t d_ino;
818 compat_ulong_t d_off;
819 unsigned short d_reclen;
820 char d_name[1];
821};
822
823struct compat_getdents_callback {
Al Viro5c0ba4e2013-05-15 13:52:59 -0400824 struct dir_context ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 struct compat_linux_dirent __user *current_dir;
826 struct compat_linux_dirent __user *previous;
827 int count;
828 int error;
829};
830
Miklos Szerediac7576f2014-10-30 17:37:34 +0100831static int compat_filldir(struct dir_context *ctx, const char *name, int namlen,
David Howellsafefdbb2006-10-03 01:13:46 -0700832 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 struct compat_linux_dirent __user * dirent;
Miklos Szerediac7576f2014-10-30 17:37:34 +0100835 struct compat_getdents_callback *buf =
836 container_of(ctx, struct compat_getdents_callback, ctx);
David Howellsafefdbb2006-10-03 01:13:46 -0700837 compat_ulong_t d_ino;
Kevin Winchester85c9fe82010-08-09 17:20:22 -0700838 int reclen = ALIGN(offsetof(struct compat_linux_dirent, d_name) +
839 namlen + 2, sizeof(compat_long_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 buf->error = -EINVAL; /* only used if we fail.. */
842 if (reclen > buf->count)
843 return -EINVAL;
David Howellsafefdbb2006-10-03 01:13:46 -0700844 d_ino = ino;
Al Viro8f3f6552008-08-12 00:28:24 -0400845 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
846 buf->error = -EOVERFLOW;
David Howellsafefdbb2006-10-03 01:13:46 -0700847 return -EOVERFLOW;
Al Viro8f3f6552008-08-12 00:28:24 -0400848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 dirent = buf->previous;
850 if (dirent) {
Theodore Ts'o1f60fbe2016-04-23 22:50:07 -0400851 if (signal_pending(current))
852 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (__put_user(offset, &dirent->d_off))
854 goto efault;
855 }
856 dirent = buf->current_dir;
David Howellsafefdbb2006-10-03 01:13:46 -0700857 if (__put_user(d_ino, &dirent->d_ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 goto efault;
859 if (__put_user(reclen, &dirent->d_reclen))
860 goto efault;
861 if (copy_to_user(dirent->d_name, name, namlen))
862 goto efault;
863 if (__put_user(0, dirent->d_name + namlen))
864 goto efault;
865 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
866 goto efault;
867 buf->previous = dirent;
868 dirent = (void __user *)dirent + reclen;
869 buf->current_dir = dirent;
870 buf->count -= reclen;
871 return 0;
872efault:
873 buf->error = -EFAULT;
874 return -EFAULT;
875}
876
Heiko Carstens625b1d72014-03-04 10:53:50 +0100877COMPAT_SYSCALL_DEFINE3(getdents, unsigned int, fd,
878 struct compat_linux_dirent __user *, dirent, unsigned int, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Al Viro2903ff02012-08-28 12:52:22 -0400880 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 struct compat_linux_dirent __user * lastdirent;
Al Viroac6614b2013-05-22 22:22:04 -0400882 struct compat_getdents_callback buf = {
883 .ctx.actor = compat_filldir,
884 .current_dir = dirent,
885 .count = count
886 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 int error;
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (!access_ok(VERIFY_WRITE, dirent, count))
Al Viro863ced72012-04-21 18:40:32 -0400890 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Al Viro63b6df12016-04-20 17:08:21 -0400892 f = fdget_pos(fd);
Al Viro2903ff02012-08-28 12:52:22 -0400893 if (!f.file)
Al Viro863ced72012-04-21 18:40:32 -0400894 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Al Viro5c0ba4e2013-05-15 13:52:59 -0400896 error = iterate_dir(f.file, &buf.ctx);
Al Viro53c9c5c2008-08-24 07:29:52 -0400897 if (error >= 0)
898 error = buf.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 lastdirent = buf.previous;
900 if (lastdirent) {
Al Virobb6f6192013-05-15 18:49:12 -0400901 if (put_user(buf.ctx.pos, &lastdirent->d_off))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 error = -EFAULT;
903 else
904 error = count - buf.count;
905 }
Al Viro63b6df12016-04-20 17:08:21 -0400906 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return error;
908}
909
Heiko Carstens0473c9b2014-03-03 10:44:03 +0100910#ifdef __ARCH_WANT_COMPAT_SYS_GETDENTS64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912struct compat_getdents_callback64 {
Al Viro5c0ba4e2013-05-15 13:52:59 -0400913 struct dir_context ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 struct linux_dirent64 __user *current_dir;
915 struct linux_dirent64 __user *previous;
916 int count;
917 int error;
918};
919
Miklos Szerediac7576f2014-10-30 17:37:34 +0100920static int compat_filldir64(struct dir_context *ctx, const char *name,
921 int namlen, loff_t offset, u64 ino,
922 unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
924 struct linux_dirent64 __user *dirent;
Miklos Szerediac7576f2014-10-30 17:37:34 +0100925 struct compat_getdents_callback64 *buf =
926 container_of(ctx, struct compat_getdents_callback64, ctx);
Kevin Winchester85c9fe82010-08-09 17:20:22 -0700927 int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
928 sizeof(u64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 u64 off;
930
931 buf->error = -EINVAL; /* only used if we fail.. */
932 if (reclen > buf->count)
933 return -EINVAL;
934 dirent = buf->previous;
935
936 if (dirent) {
Theodore Ts'o1f60fbe2016-04-23 22:50:07 -0400937 if (signal_pending(current))
938 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (__put_user_unaligned(offset, &dirent->d_off))
940 goto efault;
941 }
942 dirent = buf->current_dir;
943 if (__put_user_unaligned(ino, &dirent->d_ino))
944 goto efault;
945 off = 0;
946 if (__put_user_unaligned(off, &dirent->d_off))
947 goto efault;
948 if (__put_user(reclen, &dirent->d_reclen))
949 goto efault;
950 if (__put_user(d_type, &dirent->d_type))
951 goto efault;
952 if (copy_to_user(dirent->d_name, name, namlen))
953 goto efault;
954 if (__put_user(0, dirent->d_name + namlen))
955 goto efault;
956 buf->previous = dirent;
957 dirent = (void __user *)dirent + reclen;
958 buf->current_dir = dirent;
959 buf->count -= reclen;
960 return 0;
961efault:
962 buf->error = -EFAULT;
963 return -EFAULT;
964}
965
Heiko Carstens625b1d72014-03-04 10:53:50 +0100966COMPAT_SYSCALL_DEFINE3(getdents64, unsigned int, fd,
967 struct linux_dirent64 __user *, dirent, unsigned int, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Al Viro2903ff02012-08-28 12:52:22 -0400969 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 struct linux_dirent64 __user * lastdirent;
Al Viroac6614b2013-05-22 22:22:04 -0400971 struct compat_getdents_callback64 buf = {
972 .ctx.actor = compat_filldir64,
973 .current_dir = dirent,
974 .count = count
975 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 int error;
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (!access_ok(VERIFY_WRITE, dirent, count))
Al Viro863ced72012-04-21 18:40:32 -0400979 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Al Viro63b6df12016-04-20 17:08:21 -0400981 f = fdget_pos(fd);
Al Viro2903ff02012-08-28 12:52:22 -0400982 if (!f.file)
Al Viro863ced72012-04-21 18:40:32 -0400983 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Al Viro5c0ba4e2013-05-15 13:52:59 -0400985 error = iterate_dir(f.file, &buf.ctx);
Al Viro53c9c5c2008-08-24 07:29:52 -0400986 if (error >= 0)
987 error = buf.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 lastdirent = buf.previous;
989 if (lastdirent) {
Al Virobb6f6192013-05-15 18:49:12 -0400990 typeof(lastdirent->d_off) d_off = buf.ctx.pos;
Heiko Carstens7116e992006-12-06 20:36:36 -0800991 if (__put_user_unaligned(d_off, &lastdirent->d_off))
Al Viro53c9c5c2008-08-24 07:29:52 -0400992 error = -EFAULT;
993 else
994 error = count - buf.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
Al Viro63b6df12016-04-20 17:08:21 -0400996 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return error;
998}
Heiko Carstens0473c9b2014-03-03 10:44:03 +0100999#endif /* __ARCH_WANT_COMPAT_SYS_GETDENTS64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001/*
Miklos Szeredie922efc2005-09-06 15:18:25 -07001002 * Exactly like fs/open.c:sys_open(), except that it doesn't set the
1003 * O_LARGEFILE flag.
1004 */
Al Virof482e1b2012-12-25 20:24:38 -05001005COMPAT_SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
Miklos Szeredie922efc2005-09-06 15:18:25 -07001006{
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001007 return do_sys_open(AT_FDCWD, filename, flags, mode);
1008}
1009
1010/*
1011 * Exactly like fs/open.c:sys_openat(), except that it doesn't set the
1012 * O_LARGEFILE flag.
1013 */
Al Virof482e1b2012-12-25 20:24:38 -05001014COMPAT_SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001015{
1016 return do_sys_open(dfd, filename, flags, mode);
Miklos Szeredie922efc2005-09-06 15:18:25 -07001017}
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019#define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
1020
Thomas Gleixnerb773ad42008-08-31 08:16:57 -07001021static int poll_select_copy_remaining(struct timespec *end_time, void __user *p,
1022 int timeval, int ret)
1023{
1024 struct timespec ts;
1025
1026 if (!p)
1027 return ret;
1028
1029 if (current->personality & STICKY_TIMEOUTS)
1030 goto sticky;
1031
1032 /* No update for zero timeout */
1033 if (!end_time->tv_sec && !end_time->tv_nsec)
1034 return ret;
1035
1036 ktime_get_ts(&ts);
1037 ts = timespec_sub(*end_time, ts);
1038 if (ts.tv_sec < 0)
1039 ts.tv_sec = ts.tv_nsec = 0;
1040
1041 if (timeval) {
1042 struct compat_timeval rtv;
1043
1044 rtv.tv_sec = ts.tv_sec;
1045 rtv.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
1046
1047 if (!copy_to_user(p, &rtv, sizeof(rtv)))
1048 return ret;
1049 } else {
1050 struct compat_timespec rts;
1051
1052 rts.tv_sec = ts.tv_sec;
1053 rts.tv_nsec = ts.tv_nsec;
1054
1055 if (!copy_to_user(p, &rts, sizeof(rts)))
1056 return ret;
1057 }
1058 /*
1059 * If an application puts its timeval in read-only memory, we
1060 * don't want the Linux-specific update to the timeval to
1061 * cause a fault after the select has completed
1062 * successfully. However, because we're not updating the
1063 * timeval, we can't restart the system call.
1064 */
1065
1066sticky:
1067 if (ret == -ERESTARTNOHAND)
1068 ret = -EINTR;
1069 return ret;
1070}
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072/*
1073 * Ooo, nasty. We need here to frob 32-bit unsigned longs to
1074 * 64-bit unsigned longs.
1075 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001076static
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1078 unsigned long *fdset)
1079{
Milind Arun Choudhary022a1692007-05-08 00:29:02 -07001080 nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (ufdset) {
1082 unsigned long odd;
1083
1084 if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
1085 return -EFAULT;
1086
1087 odd = nr & 1UL;
1088 nr &= ~1UL;
1089 while (nr) {
1090 unsigned long h, l;
Heiko Carstens7116e992006-12-06 20:36:36 -08001091 if (__get_user(l, ufdset) || __get_user(h, ufdset+1))
1092 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 ufdset += 2;
1094 *fdset++ = h << 32 | l;
1095 nr -= 2;
1096 }
Heiko Carstens7116e992006-12-06 20:36:36 -08001097 if (odd && __get_user(*fdset, ufdset))
1098 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 } else {
1100 /* Tricky, must clear full unsigned long in the
1101 * kernel fdset at the end, this makes sure that
1102 * actually happens.
1103 */
1104 memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
1105 }
1106 return 0;
1107}
1108
Arjan van de Ven858119e2006-01-14 13:20:43 -08001109static
Heiko Carstens7116e992006-12-06 20:36:36 -08001110int compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1111 unsigned long *fdset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
1113 unsigned long odd;
Milind Arun Choudhary022a1692007-05-08 00:29:02 -07001114 nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 if (!ufdset)
Heiko Carstens7116e992006-12-06 20:36:36 -08001117 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 odd = nr & 1UL;
1120 nr &= ~1UL;
1121 while (nr) {
1122 unsigned long h, l;
1123 l = *fdset++;
1124 h = l >> 32;
Heiko Carstens7116e992006-12-06 20:36:36 -08001125 if (__put_user(l, ufdset) || __put_user(h, ufdset+1))
1126 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 ufdset += 2;
1128 nr -= 2;
1129 }
Heiko Carstens7116e992006-12-06 20:36:36 -08001130 if (odd && __put_user(*fdset, ufdset))
1131 return -EFAULT;
1132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
1134
1135
1136/*
1137 * This is a virtual copy of sys_select from fs/select.c and probably
1138 * should be compared to it from time to time
1139 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141/*
1142 * We can actually return ERESTARTSYS instead of EINTR, but I'd
1143 * like to be certain this leads to no problems. So I return
1144 * EINTR just for safety.
1145 *
1146 * Update: ERESTARTSYS breaks at least the xview clock binary, so
1147 * I'm trying ERESTARTNOHAND which restart only when you want to.
1148 */
David Woodhouse9f729492006-01-18 17:44:05 -08001149int compat_core_sys_select(int n, compat_ulong_t __user *inp,
Arjan van de Ven8ff3e8e2008-08-31 08:26:40 -07001150 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1151 struct timespec *end_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
1153 fd_set_bits fds;
Badari Pulavarty6087b2d2007-05-23 13:57:45 -07001154 void *bits;
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08001155 int size, max_fds, ret = -EINVAL;
Linus Torvaldsa4531ed2005-09-09 15:10:52 -07001156 struct fdtable *fdt;
Badari Pulavarty6087b2d2007-05-23 13:57:45 -07001157 long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (n < 0)
1160 goto out_nofds;
1161
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08001162 /* max_fds can increase, so grab it once to avoid race */
Linus Torvaldsac5b8b62005-09-09 15:42:34 -07001163 rcu_read_lock();
Linus Torvaldsa4531ed2005-09-09 15:10:52 -07001164 fdt = files_fdtable(current->files);
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08001165 max_fds = fdt->max_fds;
Linus Torvaldsac5b8b62005-09-09 15:42:34 -07001166 rcu_read_unlock();
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08001167 if (n > max_fds)
1168 n = max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 /*
1171 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
1172 * since we used fdset we need to allocate memory in units of
1173 * long-words.
1174 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 size = FDS_BYTES(n);
Badari Pulavarty6087b2d2007-05-23 13:57:45 -07001176 bits = stack_fds;
1177 if (size > sizeof(stack_fds) / 6) {
1178 bits = kmalloc(6 * size, GFP_KERNEL);
1179 ret = -ENOMEM;
1180 if (!bits)
1181 goto out_nofds;
1182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 fds.in = (unsigned long *) bits;
1184 fds.out = (unsigned long *) (bits + size);
1185 fds.ex = (unsigned long *) (bits + 2*size);
1186 fds.res_in = (unsigned long *) (bits + 3*size);
1187 fds.res_out = (unsigned long *) (bits + 4*size);
1188 fds.res_ex = (unsigned long *) (bits + 5*size);
1189
1190 if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
1191 (ret = compat_get_fd_set(n, outp, fds.out)) ||
1192 (ret = compat_get_fd_set(n, exp, fds.ex)))
1193 goto out;
1194 zero_fd_set(n, fds.res_in);
1195 zero_fd_set(n, fds.res_out);
1196 zero_fd_set(n, fds.res_ex);
1197
Arjan van de Ven8ff3e8e2008-08-31 08:26:40 -07001198 ret = do_select(n, &fds, end_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 if (ret < 0)
1201 goto out;
1202 if (!ret) {
1203 ret = -ERESTARTNOHAND;
1204 if (signal_pending(current))
1205 goto out;
1206 ret = 0;
1207 }
1208
Heiko Carstens7116e992006-12-06 20:36:36 -08001209 if (compat_set_fd_set(n, inp, fds.res_in) ||
1210 compat_set_fd_set(n, outp, fds.res_out) ||
1211 compat_set_fd_set(n, exp, fds.res_ex))
1212 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213out:
Badari Pulavarty6087b2d2007-05-23 13:57:45 -07001214 if (bits != stack_fds)
1215 kfree(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216out_nofds:
1217 return ret;
1218}
1219
Heiko Carstens625b1d72014-03-04 10:53:50 +01001220COMPAT_SYSCALL_DEFINE5(select, int, n, compat_ulong_t __user *, inp,
1221 compat_ulong_t __user *, outp, compat_ulong_t __user *, exp,
1222 struct compat_timeval __user *, tvp)
David Woodhouse9f729492006-01-18 17:44:05 -08001223{
Arjan van de Ven8ff3e8e2008-08-31 08:26:40 -07001224 struct timespec end_time, *to = NULL;
David Woodhouse9f729492006-01-18 17:44:05 -08001225 struct compat_timeval tv;
1226 int ret;
1227
1228 if (tvp) {
1229 if (copy_from_user(&tv, tvp, sizeof(tv)))
1230 return -EFAULT;
1231
Arjan van de Ven8ff3e8e2008-08-31 08:26:40 -07001232 to = &end_time;
Arjan van de Ven4d36a9e2008-10-25 12:41:41 -07001233 if (poll_select_set_timeout(to,
1234 tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
1235 (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
David Woodhouse9f729492006-01-18 17:44:05 -08001236 return -EINVAL;
David Woodhouse9f729492006-01-18 17:44:05 -08001237 }
1238
Arjan van de Ven8ff3e8e2008-08-31 08:26:40 -07001239 ret = compat_core_sys_select(n, inp, outp, exp, to);
1240 ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
David Woodhouse9f729492006-01-18 17:44:05 -08001241
1242 return ret;
1243}
1244
Christoph Hellwig5d0e5282010-03-10 15:21:13 -08001245struct compat_sel_arg_struct {
1246 compat_ulong_t n;
1247 compat_uptr_t inp;
1248 compat_uptr_t outp;
1249 compat_uptr_t exp;
1250 compat_uptr_t tvp;
1251};
1252
Heiko Carstens625b1d72014-03-04 10:53:50 +01001253COMPAT_SYSCALL_DEFINE1(old_select, struct compat_sel_arg_struct __user *, arg)
Christoph Hellwig5d0e5282010-03-10 15:21:13 -08001254{
1255 struct compat_sel_arg_struct a;
1256
1257 if (copy_from_user(&a, arg, sizeof(a)))
1258 return -EFAULT;
1259 return compat_sys_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp),
1260 compat_ptr(a.exp), compat_ptr(a.tvp));
1261}
1262
Heiko Carstensc9da9f22009-01-14 14:13:57 +01001263static long do_compat_pselect(int n, compat_ulong_t __user *inp,
David Woodhouse9f729492006-01-18 17:44:05 -08001264 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1265 struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask,
1266 compat_size_t sigsetsize)
1267{
1268 compat_sigset_t ss32;
1269 sigset_t ksigmask, sigsaved;
David Woodhouse9f729492006-01-18 17:44:05 -08001270 struct compat_timespec ts;
Arjan van de Ven8ff3e8e2008-08-31 08:26:40 -07001271 struct timespec end_time, *to = NULL;
David Woodhouse9f729492006-01-18 17:44:05 -08001272 int ret;
1273
1274 if (tsp) {
1275 if (copy_from_user(&ts, tsp, sizeof(ts)))
1276 return -EFAULT;
1277
Arjan van de Ven8ff3e8e2008-08-31 08:26:40 -07001278 to = &end_time;
1279 if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
David Woodhouse9f729492006-01-18 17:44:05 -08001280 return -EINVAL;
1281 }
1282
1283 if (sigmask) {
1284 if (sigsetsize != sizeof(compat_sigset_t))
1285 return -EINVAL;
1286 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1287 return -EFAULT;
1288 sigset_from_compat(&ksigmask, &ss32);
1289
1290 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1291 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1292 }
1293
Arjan van de Ven8ff3e8e2008-08-31 08:26:40 -07001294 ret = compat_core_sys_select(n, inp, outp, exp, to);
1295 ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
David Woodhouse9f729492006-01-18 17:44:05 -08001296
1297 if (ret == -ERESTARTNOHAND) {
1298 /*
1299 * Don't restore the signal mask yet. Let do_signal() deliver
1300 * the signal on the way back to userspace, before the signal
1301 * mask is restored.
1302 */
1303 if (sigmask) {
1304 memcpy(&current->saved_sigmask, &sigsaved,
1305 sizeof(sigsaved));
Roland McGrath4e4c22c2008-04-30 00:53:06 -07001306 set_restore_sigmask();
David Woodhouse9f729492006-01-18 17:44:05 -08001307 }
1308 } else if (sigmask)
1309 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1310
1311 return ret;
1312}
1313
Heiko Carstens625b1d72014-03-04 10:53:50 +01001314COMPAT_SYSCALL_DEFINE6(pselect6, int, n, compat_ulong_t __user *, inp,
1315 compat_ulong_t __user *, outp, compat_ulong_t __user *, exp,
1316 struct compat_timespec __user *, tsp, void __user *, sig)
David Woodhouse9f729492006-01-18 17:44:05 -08001317{
1318 compat_size_t sigsetsize = 0;
1319 compat_uptr_t up = 0;
1320
1321 if (sig) {
1322 if (!access_ok(VERIFY_READ, sig,
1323 sizeof(compat_uptr_t)+sizeof(compat_size_t)) ||
1324 __get_user(up, (compat_uptr_t __user *)sig) ||
1325 __get_user(sigsetsize,
1326 (compat_size_t __user *)(sig+sizeof(up))))
1327 return -EFAULT;
1328 }
Heiko Carstensc9da9f22009-01-14 14:13:57 +01001329 return do_compat_pselect(n, inp, outp, exp, tsp, compat_ptr(up),
1330 sigsetsize);
David Woodhouse9f729492006-01-18 17:44:05 -08001331}
1332
Heiko Carstens625b1d72014-03-04 10:53:50 +01001333COMPAT_SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds,
1334 unsigned int, nfds, struct compat_timespec __user *, tsp,
1335 const compat_sigset_t __user *, sigmask, compat_size_t, sigsetsize)
David Woodhouse9f729492006-01-18 17:44:05 -08001336{
1337 compat_sigset_t ss32;
1338 sigset_t ksigmask, sigsaved;
1339 struct compat_timespec ts;
Arjan van de Ven8ff3e8e2008-08-31 08:26:40 -07001340 struct timespec end_time, *to = NULL;
David Woodhouse9f729492006-01-18 17:44:05 -08001341 int ret;
1342
1343 if (tsp) {
1344 if (copy_from_user(&ts, tsp, sizeof(ts)))
1345 return -EFAULT;
1346
Arjan van de Ven8ff3e8e2008-08-31 08:26:40 -07001347 to = &end_time;
1348 if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
1349 return -EINVAL;
David Woodhouse9f729492006-01-18 17:44:05 -08001350 }
1351
1352 if (sigmask) {
Alexey Dobriyan3835a9b2006-05-15 09:44:27 -07001353 if (sigsetsize != sizeof(compat_sigset_t))
David Woodhouse9f729492006-01-18 17:44:05 -08001354 return -EINVAL;
1355 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1356 return -EFAULT;
1357 sigset_from_compat(&ksigmask, &ss32);
1358
1359 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1360 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1361 }
1362
Arjan van de Ven8ff3e8e2008-08-31 08:26:40 -07001363 ret = do_sys_poll(ufds, nfds, to);
David Woodhouse9f729492006-01-18 17:44:05 -08001364
1365 /* We can restart this syscall, usually */
1366 if (ret == -EINTR) {
1367 /*
1368 * Don't restore the signal mask yet. Let do_signal() deliver
1369 * the signal on the way back to userspace, before the signal
1370 * mask is restored.
1371 */
1372 if (sigmask) {
1373 memcpy(&current->saved_sigmask, &sigsaved,
1374 sizeof(sigsaved));
Roland McGrath4e4c22c2008-04-30 00:53:06 -07001375 set_restore_sigmask();
David Woodhouse9f729492006-01-18 17:44:05 -08001376 }
1377 ret = -ERESTARTNOHAND;
1378 } else if (sigmask)
1379 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1380
Arjan van de Ven8ff3e8e2008-08-31 08:26:40 -07001381 ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
David Woodhouse9f729492006-01-18 17:44:05 -08001382
1383 return ret;
1384}
David Woodhouse9f729492006-01-18 17:44:05 -08001385
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +05301386#ifdef CONFIG_FHANDLE
1387/*
1388 * Exactly like fs/open.c:sys_open_by_handle_at(), except that it
1389 * doesn't set the O_LARGEFILE flag.
1390 */
Al Virof482e1b2012-12-25 20:24:38 -05001391COMPAT_SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd,
1392 struct file_handle __user *, handle, int, flags)
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +05301393{
1394 return do_handle_open(mountdirfd, handle, flags);
1395}
1396#endif