blob: 08e6af5c1b1f72af71f11f3ce7deb579f968e65a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/fcntl.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7#include <linux/syscalls.h>
8#include <linux/init.h>
9#include <linux/mm.h>
10#include <linux/fs.h>
11#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040012#include <linux/fdtable.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080013#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/dnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/slab.h>
16#include <linux/module.h>
Jens Axboe35f3d142010-05-20 10:43:18 +020017#include <linux/pipe_fs_i.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/security.h>
19#include <linux/ptrace.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070020#include <linux/signal.h>
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070021#include <linux/rcupdate.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070022#include <linux/pid_namespace.h>
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -070023#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/poll.h>
26#include <asm/siginfo.h>
27#include <asm/uaccess.h>
28
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -080029void set_close_on_exec(unsigned int fd, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
31 struct files_struct *files = current->files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -070032 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 spin_lock(&files->file_lock);
Dipankar Sarmabadf1662005-09-09 13:04:10 -070034 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 if (flag)
David Howells1dce27c2012-02-16 17:49:42 +000036 __set_close_on_exec(fd, fdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 else
David Howells1dce27c2012-02-16 17:49:42 +000038 __clear_close_on_exec(fd, fdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 spin_unlock(&files->file_lock);
40}
41
David Howells1dce27c2012-02-16 17:49:42 +000042static bool get_close_on_exec(unsigned int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 struct files_struct *files = current->files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -070045 struct fdtable *fdt;
David Howells1dce27c2012-02-16 17:49:42 +000046 bool res;
Dipankar Sarmab8359962005-09-09 13:04:14 -070047 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -070048 fdt = files_fdtable(files);
David Howells1dce27c2012-02-16 17:49:42 +000049 res = close_on_exec(fd, fdt);
Dipankar Sarmab8359962005-09-09 13:04:14 -070050 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 return res;
52}
53
Heiko Carstensa26eab22009-01-14 14:14:17 +010054SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 int err = -EBADF;
57 struct file * file, *tofree;
58 struct files_struct * files = current->files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -070059 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Ulrich Drepper336dd1f2008-07-23 21:29:29 -070061 if ((flags & ~O_CLOEXEC) != 0)
62 return -EINVAL;
63
Al Viro6c5d0512008-07-26 13:38:19 -040064 if (unlikely(oldfd == newfd))
65 return -EINVAL;
66
Al Virof33ff992012-08-12 16:17:59 -040067 if (newfd >= rlimit(RLIMIT_NOFILE))
68 return -EMFILE;
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 spin_lock(&files->file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 err = expand_files(files, newfd);
Al Viro1b7e1902008-07-30 06:18:03 -040072 file = fcheck(oldfd);
73 if (unlikely(!file))
74 goto Ebadf;
Al Viro4e1e0182008-07-26 16:01:20 -040075 if (unlikely(err < 0)) {
76 if (err == -EMFILE)
Al Viro1b7e1902008-07-30 06:18:03 -040077 goto Ebadf;
78 goto out_unlock;
Al Viro4e1e0182008-07-26 16:01:20 -040079 }
Al Viro1b7e1902008-07-30 06:18:03 -040080 /*
81 * We need to detect attempts to do dup2() over allocated but still
82 * not finished descriptor. NB: OpenBSD avoids that at the price of
83 * extra work in their equivalent of fget() - they insert struct
84 * file immediately after grabbing descriptor, mark it larval if
85 * more work (e.g. actual opening) is needed and make sure that
86 * fget() treats larval files as absent. Potentially interesting,
87 * but while extra work in fget() is trivial, locking implications
88 * and amount of surgery on open()-related paths in VFS are not.
89 * FreeBSD fails with -EBADF in the same situation, NetBSD "solution"
90 * deadlocks in rather amusing ways, AFAICS. All of that is out of
91 * scope of POSIX or SUS, since neither considers shared descriptor
92 * tables and this condition does not arise without those.
93 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 err = -EBUSY;
Dipankar Sarmabadf1662005-09-09 13:04:10 -070095 fdt = files_fdtable(files);
96 tofree = fdt->fd[newfd];
David Howells1dce27c2012-02-16 17:49:42 +000097 if (!tofree && fd_is_open(newfd, fdt))
Al Viro1b7e1902008-07-30 06:18:03 -040098 goto out_unlock;
99 get_file(file);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700100 rcu_assign_pointer(fdt->fd[newfd], file);
David Howells1dce27c2012-02-16 17:49:42 +0000101 __set_open_fd(newfd, fdt);
Ulrich Drepper336dd1f2008-07-23 21:29:29 -0700102 if (flags & O_CLOEXEC)
David Howells1dce27c2012-02-16 17:49:42 +0000103 __set_close_on_exec(newfd, fdt);
Ulrich Drepper336dd1f2008-07-23 21:29:29 -0700104 else
David Howells1dce27c2012-02-16 17:49:42 +0000105 __clear_close_on_exec(newfd, fdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 spin_unlock(&files->file_lock);
107
108 if (tofree)
109 filp_close(tofree, files);
Al Viro1b7e1902008-07-30 06:18:03 -0400110
111 return newfd;
112
113Ebadf:
114 err = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115out_unlock:
116 spin_unlock(&files->file_lock);
Al Viro1b7e1902008-07-30 06:18:03 -0400117 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Heiko Carstensa26eab22009-01-14 14:14:17 +0100120SYSCALL_DEFINE2(dup2, unsigned int, oldfd, unsigned int, newfd)
Ulrich Drepper336dd1f2008-07-23 21:29:29 -0700121{
Al Viro6c5d0512008-07-26 13:38:19 -0400122 if (unlikely(newfd == oldfd)) { /* corner case */
123 struct files_struct *files = current->files;
Jeff Mahoney2b79bc42009-05-11 14:25:34 -0400124 int retval = oldfd;
125
Al Viro6c5d0512008-07-26 13:38:19 -0400126 rcu_read_lock();
127 if (!fcheck_files(files, oldfd))
Jeff Mahoney2b79bc42009-05-11 14:25:34 -0400128 retval = -EBADF;
Al Viro6c5d0512008-07-26 13:38:19 -0400129 rcu_read_unlock();
Jeff Mahoney2b79bc42009-05-11 14:25:34 -0400130 return retval;
Al Viro6c5d0512008-07-26 13:38:19 -0400131 }
Ulrich Drepper336dd1f2008-07-23 21:29:29 -0700132 return sys_dup3(oldfd, newfd, 0);
133}
134
Heiko Carstensa26eab22009-01-14 14:14:17 +0100135SYSCALL_DEFINE1(dup, unsigned int, fildes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 int ret = -EBADF;
Al Viro1abf0c72011-03-13 03:51:11 -0400138 struct file *file = fget_raw(fildes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Al Viro1027abe2008-07-30 04:13:04 -0400140 if (file) {
141 ret = get_unused_fd();
142 if (ret >= 0)
143 fd_install(ret, file);
144 else
145 fput(file);
146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return ret;
148}
149
Jonathan Corbet76398422009-02-01 14:26:59 -0700150#define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152static int setfl(int fd, struct file * filp, unsigned long arg)
153{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800154 struct inode * inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 int error = 0;
156
dean gaudet7d95c8f2006-02-03 03:04:30 -0800157 /*
158 * O_APPEND cannot be cleared if the file is marked as append-only
159 * and the file is open for write.
160 */
161 if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return -EPERM;
163
164 /* O_NOATIME can only be set by the owner or superuser */
165 if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700166 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return -EPERM;
168
169 /* required for strict SunOS emulation */
170 if (O_NONBLOCK != O_NDELAY)
171 if (arg & O_NDELAY)
172 arg |= O_NONBLOCK;
173
174 if (arg & O_DIRECT) {
175 if (!filp->f_mapping || !filp->f_mapping->a_ops ||
176 !filp->f_mapping->a_ops->direct_IO)
177 return -EINVAL;
178 }
179
180 if (filp->f_op && filp->f_op->check_flags)
181 error = filp->f_op->check_flags(arg);
182 if (error)
183 return error;
184
Jonathan Corbet218d11a2008-12-05 16:12:48 -0700185 /*
Jonathan Corbet76398422009-02-01 14:26:59 -0700186 * ->fasync() is responsible for setting the FASYNC bit.
Jonathan Corbet218d11a2008-12-05 16:12:48 -0700187 */
Jonathan Corbet76398422009-02-01 14:26:59 -0700188 if (((arg ^ filp->f_flags) & FASYNC) && filp->f_op &&
189 filp->f_op->fasync) {
190 error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
191 if (error < 0)
192 goto out;
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700193 if (error > 0)
194 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -0700196 spin_lock(&filp->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -0700198 spin_unlock(&filp->f_lock);
Jonathan Corbet76398422009-02-01 14:26:59 -0700199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return error;
202}
203
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700204static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
Oleg Nesterov2f38d702009-06-16 22:07:46 +0200205 int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Linus Torvalds80e1e822010-02-07 10:11:23 -0800207 write_lock_irq(&filp->f_owner.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (force || !filp->f_owner.pid) {
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700209 put_pid(filp->f_owner.pid);
210 filp->f_owner.pid = get_pid(pid);
211 filp->f_owner.pid_type = type;
Oleg Nesterov2f38d702009-06-16 22:07:46 +0200212
213 if (pid) {
214 const struct cred *cred = current_cred();
215 filp->f_owner.uid = cred->uid;
216 filp->f_owner.euid = cred->euid;
217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
Linus Torvalds80e1e822010-02-07 10:11:23 -0800219 write_unlock_irq(&filp->f_owner.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700222int __f_setown(struct file *filp, struct pid *pid, enum pid_type type,
223 int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 int err;
Oleg Nesterov2f38d702009-06-16 22:07:46 +0200226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 err = security_file_set_fowner(filp);
228 if (err)
229 return err;
230
Oleg Nesterov2f38d702009-06-16 22:07:46 +0200231 f_modown(filp, pid, type, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return 0;
233}
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700234EXPORT_SYMBOL(__f_setown);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700236int f_setown(struct file *filp, unsigned long arg, int force)
237{
238 enum pid_type type;
239 struct pid *pid;
240 int who = arg;
241 int result;
242 type = PIDTYPE_PID;
243 if (who < 0) {
244 type = PIDTYPE_PGID;
245 who = -who;
246 }
247 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700248 pid = find_vpid(who);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700249 result = __f_setown(filp, pid, type, force);
250 rcu_read_unlock();
251 return result;
252}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253EXPORT_SYMBOL(f_setown);
254
255void f_delown(struct file *filp)
256{
Oleg Nesterov2f38d702009-06-16 22:07:46 +0200257 f_modown(filp, NULL, PIDTYPE_PID, 1);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700258}
259
260pid_t f_getown(struct file *filp)
261{
262 pid_t pid;
Eric W. Biederman43fa1ad2006-10-02 02:17:27 -0700263 read_lock(&filp->f_owner.lock);
Pavel Emelyanov6c5f3e72008-02-08 04:19:20 -0800264 pid = pid_vnr(filp->f_owner.pid);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700265 if (filp->f_owner.pid_type == PIDTYPE_PGID)
266 pid = -pid;
Eric W. Biederman43fa1ad2006-10-02 02:17:27 -0700267 read_unlock(&filp->f_owner.lock);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700268 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700271static int f_setown_ex(struct file *filp, unsigned long arg)
272{
273 struct f_owner_ex * __user owner_p = (void * __user)arg;
274 struct f_owner_ex owner;
275 struct pid *pid;
276 int type;
277 int ret;
278
279 ret = copy_from_user(&owner, owner_p, sizeof(owner));
280 if (ret)
Dan Carpenter5b544702010-06-03 12:35:42 +0200281 return -EFAULT;
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700282
283 switch (owner.type) {
284 case F_OWNER_TID:
285 type = PIDTYPE_MAX;
286 break;
287
288 case F_OWNER_PID:
289 type = PIDTYPE_PID;
290 break;
291
Peter Zijlstra978b4052009-11-17 14:06:24 -0800292 case F_OWNER_PGRP:
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700293 type = PIDTYPE_PGID;
294 break;
295
296 default:
297 return -EINVAL;
298 }
299
300 rcu_read_lock();
301 pid = find_vpid(owner.pid);
302 if (owner.pid && !pid)
303 ret = -ESRCH;
304 else
305 ret = __f_setown(filp, pid, type, 1);
306 rcu_read_unlock();
307
308 return ret;
309}
310
311static int f_getown_ex(struct file *filp, unsigned long arg)
312{
313 struct f_owner_ex * __user owner_p = (void * __user)arg;
314 struct f_owner_ex owner;
315 int ret = 0;
316
317 read_lock(&filp->f_owner.lock);
318 owner.pid = pid_vnr(filp->f_owner.pid);
319 switch (filp->f_owner.pid_type) {
320 case PIDTYPE_MAX:
321 owner.type = F_OWNER_TID;
322 break;
323
324 case PIDTYPE_PID:
325 owner.type = F_OWNER_PID;
326 break;
327
328 case PIDTYPE_PGID:
Peter Zijlstra978b4052009-11-17 14:06:24 -0800329 owner.type = F_OWNER_PGRP;
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700330 break;
331
332 default:
333 WARN_ON(1);
334 ret = -EINVAL;
335 break;
336 }
337 read_unlock(&filp->f_owner.lock);
338
Dan Carpenter5b544702010-06-03 12:35:42 +0200339 if (!ret) {
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700340 ret = copy_to_user(owner_p, &owner, sizeof(owner));
Dan Carpenter5b544702010-06-03 12:35:42 +0200341 if (ret)
342 ret = -EFAULT;
343 }
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700344 return ret;
345}
346
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -0700347#ifdef CONFIG_CHECKPOINT_RESTORE
348static int f_getowner_uids(struct file *filp, unsigned long arg)
349{
350 struct user_namespace *user_ns = current_user_ns();
351 uid_t * __user dst = (void * __user)arg;
352 uid_t src[2];
353 int err;
354
355 read_lock(&filp->f_owner.lock);
356 src[0] = from_kuid(user_ns, filp->f_owner.uid);
357 src[1] = from_kuid(user_ns, filp->f_owner.euid);
358 read_unlock(&filp->f_owner.lock);
359
360 err = put_user(src[0], &dst[0]);
361 err |= put_user(src[1], &dst[1]);
362
363 return err;
364}
365#else
366static int f_getowner_uids(struct file *filp, unsigned long arg)
367{
368 return -EINVAL;
369}
370#endif
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
373 struct file *filp)
374{
375 long err = -EINVAL;
376
377 switch (cmd) {
378 case F_DUPFD:
Ulrich Drepper22d2b352007-10-16 23:30:26 -0700379 case F_DUPFD_CLOEXEC:
Jiri Slabyd554ed892010-03-05 13:42:42 -0800380 if (arg >= rlimit(RLIMIT_NOFILE))
Al Viro4e1e0182008-07-26 16:01:20 -0400381 break;
Al Viro1027abe2008-07-30 04:13:04 -0400382 err = alloc_fd(arg, cmd == F_DUPFD_CLOEXEC ? O_CLOEXEC : 0);
383 if (err >= 0) {
384 get_file(filp);
385 fd_install(err, filp);
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 break;
388 case F_GETFD:
389 err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
390 break;
391 case F_SETFD:
392 err = 0;
393 set_close_on_exec(fd, arg & FD_CLOEXEC);
394 break;
395 case F_GETFL:
396 err = filp->f_flags;
397 break;
398 case F_SETFL:
399 err = setfl(fd, filp, arg);
400 break;
401 case F_GETLK:
402 err = fcntl_getlk(filp, (struct flock __user *) arg);
403 break;
404 case F_SETLK:
405 case F_SETLKW:
Peter Staubachc2936212005-07-27 11:45:09 -0700406 err = fcntl_setlk(fd, filp, cmd, (struct flock __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 break;
408 case F_GETOWN:
409 /*
410 * XXX If f_owner is a process group, the
411 * negative return value will get converted
412 * into an error. Oops. If we keep the
413 * current syscall conventions, the only way
414 * to fix this will be in libc.
415 */
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700416 err = f_getown(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 force_successful_syscall_return();
418 break;
419 case F_SETOWN:
420 err = f_setown(filp, arg, 1);
421 break;
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700422 case F_GETOWN_EX:
423 err = f_getown_ex(filp, arg);
424 break;
425 case F_SETOWN_EX:
426 err = f_setown_ex(filp, arg);
427 break;
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -0700428 case F_GETOWNER_UIDS:
429 err = f_getowner_uids(filp, arg);
430 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 case F_GETSIG:
432 err = filp->f_owner.signum;
433 break;
434 case F_SETSIG:
435 /* arg == 0 restores default behaviour. */
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700436 if (!valid_signal(arg)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 break;
438 }
439 err = 0;
440 filp->f_owner.signum = arg;
441 break;
442 case F_GETLEASE:
443 err = fcntl_getlease(filp);
444 break;
445 case F_SETLEASE:
446 err = fcntl_setlease(fd, filp, arg);
447 break;
448 case F_NOTIFY:
449 err = fcntl_dirnotify(fd, filp, arg);
450 break;
Jens Axboe35f3d142010-05-20 10:43:18 +0200451 case F_SETPIPE_SZ:
452 case F_GETPIPE_SZ:
453 err = pipe_fcntl(filp, cmd, arg);
454 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 default:
456 break;
457 }
458 return err;
459}
460
Al Viro1abf0c72011-03-13 03:51:11 -0400461static int check_fcntl_cmd(unsigned cmd)
462{
463 switch (cmd) {
464 case F_DUPFD:
465 case F_DUPFD_CLOEXEC:
466 case F_GETFD:
467 case F_SETFD:
468 case F_GETFL:
469 return 1;
470 }
471 return 0;
472}
473
Heiko Carstensa26eab22009-01-14 14:14:17 +0100474SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 struct file *filp;
Al Viro545ec2c2012-04-21 18:42:19 -0400477 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 long err = -EBADF;
479
Al Viro545ec2c2012-04-21 18:42:19 -0400480 filp = fget_raw_light(fd, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (!filp)
482 goto out;
483
Al Viro1abf0c72011-03-13 03:51:11 -0400484 if (unlikely(filp->f_mode & FMODE_PATH)) {
Al Viro545ec2c2012-04-21 18:42:19 -0400485 if (!check_fcntl_cmd(cmd))
486 goto out1;
Al Viro1abf0c72011-03-13 03:51:11 -0400487 }
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 err = security_file_fcntl(filp, cmd, arg);
Al Viro545ec2c2012-04-21 18:42:19 -0400490 if (!err)
491 err = do_fcntl(fd, cmd, arg, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Al Viro545ec2c2012-04-21 18:42:19 -0400493out1:
494 fput_light(filp, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495out:
496 return err;
497}
498
499#if BITS_PER_LONG == 32
Heiko Carstensa26eab22009-01-14 14:14:17 +0100500SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
501 unsigned long, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 struct file * filp;
Al Viro545ec2c2012-04-21 18:42:19 -0400504 long err = -EBADF;
505 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Al Viro545ec2c2012-04-21 18:42:19 -0400507 filp = fget_raw_light(fd, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (!filp)
509 goto out;
510
Al Viro1abf0c72011-03-13 03:51:11 -0400511 if (unlikely(filp->f_mode & FMODE_PATH)) {
Al Viro545ec2c2012-04-21 18:42:19 -0400512 if (!check_fcntl_cmd(cmd))
513 goto out1;
Al Viro1abf0c72011-03-13 03:51:11 -0400514 }
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 err = security_file_fcntl(filp, cmd, arg);
Al Viro545ec2c2012-04-21 18:42:19 -0400517 if (err)
518 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 switch (cmd) {
521 case F_GETLK64:
522 err = fcntl_getlk64(filp, (struct flock64 __user *) arg);
523 break;
524 case F_SETLK64:
525 case F_SETLKW64:
Peter Staubachc2936212005-07-27 11:45:09 -0700526 err = fcntl_setlk64(fd, filp, cmd,
527 (struct flock64 __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 break;
529 default:
530 err = do_fcntl(fd, cmd, arg, filp);
531 break;
532 }
Al Viro545ec2c2012-04-21 18:42:19 -0400533out1:
534 fput_light(filp, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535out:
536 return err;
537}
538#endif
539
540/* Table to convert sigio signal codes into poll band bitmaps */
541
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800542static const long band_table[NSIGPOLL] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 POLLIN | POLLRDNORM, /* POLL_IN */
544 POLLOUT | POLLWRNORM | POLLWRBAND, /* POLL_OUT */
545 POLLIN | POLLRDNORM | POLLMSG, /* POLL_MSG */
546 POLLERR, /* POLL_ERR */
547 POLLPRI | POLLRDBAND, /* POLL_PRI */
548 POLLHUP | POLLERR /* POLL_HUP */
549};
550
551static inline int sigio_perm(struct task_struct *p,
552 struct fown_struct *fown, int sig)
553{
David Howellsc69e8d92008-11-14 10:39:19 +1100554 const struct cred *cred;
555 int ret;
556
557 rcu_read_lock();
558 cred = __task_cred(p);
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -0800559 ret = ((uid_eq(fown->euid, GLOBAL_ROOT_UID) ||
560 uid_eq(fown->euid, cred->suid) || uid_eq(fown->euid, cred->uid) ||
561 uid_eq(fown->uid, cred->suid) || uid_eq(fown->uid, cred->uid)) &&
David Howellsc69e8d92008-11-14 10:39:19 +1100562 !security_file_send_sigiotask(p, fown, sig));
563 rcu_read_unlock();
564 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
567static void send_sigio_to_task(struct task_struct *p,
Oleg Nesterov8eeee4e2009-06-17 00:27:10 +0200568 struct fown_struct *fown,
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700569 int fd, int reason, int group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Oleg Nesterov8eeee4e2009-06-17 00:27:10 +0200571 /*
572 * F_SETSIG can change ->signum lockless in parallel, make
573 * sure we read it once and use the same value throughout.
574 */
575 int signum = ACCESS_ONCE(fown->signum);
576
577 if (!sigio_perm(p, fown, signum))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return;
579
Oleg Nesterov8eeee4e2009-06-17 00:27:10 +0200580 switch (signum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 siginfo_t si;
582 default:
583 /* Queue a rt signal with the appropriate fd as its
584 value. We use SI_SIGIO as the source, not
585 SI_KERNEL, since kernel signals always get
586 delivered even if we can't queue. Failure to
587 queue in this case _should_ be reported; we fall
588 back to SIGIO in that case. --sct */
Oleg Nesterov8eeee4e2009-06-17 00:27:10 +0200589 si.si_signo = signum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 si.si_errno = 0;
591 si.si_code = reason;
592 /* Make sure we are called with one of the POLL_*
593 reasons, otherwise we could leak kernel stack into
594 userspace. */
Eric Sesterhennf6298aa2006-04-02 13:37:19 +0200595 BUG_ON((reason & __SI_MASK) != __SI_POLL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (reason - POLL_IN >= NSIGPOLL)
597 si.si_band = ~0L;
598 else
599 si.si_band = band_table[reason - POLL_IN];
600 si.si_fd = fd;
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700601 if (!do_send_sig_info(signum, &si, p, group))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 break;
603 /* fall-through: fall back on the old plain SIGIO signal */
604 case 0:
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700605 do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607}
608
609void send_sigio(struct fown_struct *fown, int fd, int band)
610{
611 struct task_struct *p;
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700612 enum pid_type type;
613 struct pid *pid;
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700614 int group = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 read_lock(&fown->lock);
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700617
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700618 type = fown->pid_type;
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700619 if (type == PIDTYPE_MAX) {
620 group = 0;
621 type = PIDTYPE_PID;
622 }
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 pid = fown->pid;
625 if (!pid)
626 goto out_unlock_fown;
627
628 read_lock(&tasklist_lock);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700629 do_each_pid_task(pid, type, p) {
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700630 send_sigio_to_task(p, fown, fd, band, group);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700631 } while_each_pid_task(pid, type, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 read_unlock(&tasklist_lock);
633 out_unlock_fown:
634 read_unlock(&fown->lock);
635}
636
637static void send_sigurg_to_task(struct task_struct *p,
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700638 struct fown_struct *fown, int group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 if (sigio_perm(p, fown, SIGURG))
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700641 do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
644int send_sigurg(struct fown_struct *fown)
645{
646 struct task_struct *p;
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700647 enum pid_type type;
648 struct pid *pid;
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700649 int group = 1;
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700650 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 read_lock(&fown->lock);
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700653
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700654 type = fown->pid_type;
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700655 if (type == PIDTYPE_MAX) {
656 group = 0;
657 type = PIDTYPE_PID;
658 }
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 pid = fown->pid;
661 if (!pid)
662 goto out_unlock_fown;
663
664 ret = 1;
665
666 read_lock(&tasklist_lock);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700667 do_each_pid_task(pid, type, p) {
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700668 send_sigurg_to_task(p, fown, group);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700669 } while_each_pid_task(pid, type, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 read_unlock(&tasklist_lock);
671 out_unlock_fown:
672 read_unlock(&fown->lock);
673 return ret;
674}
675
Eric Dumazet989a2972010-04-14 09:55:35 +0000676static DEFINE_SPINLOCK(fasync_lock);
Christoph Lametere18b8902006-12-06 20:33:20 -0800677static struct kmem_cache *fasync_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Eric Dumazet989a2972010-04-14 09:55:35 +0000679static void fasync_free_rcu(struct rcu_head *head)
680{
681 kmem_cache_free(fasync_cache,
682 container_of(head, struct fasync_struct, fa_rcu));
683}
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685/*
Linus Torvalds53281b62009-12-16 08:23:37 -0800686 * Remove a fasync entry. If successfully removed, return
687 * positive and clear the FASYNC flag. If no entry exists,
688 * do nothing and return 0.
689 *
690 * NOTE! It is very important that the FASYNC flag always
691 * match the state "is the filp on a fasync list".
692 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 */
Linus Torvaldsf7347ce2010-10-27 12:38:12 -0400694int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 struct fasync_struct *fa, **fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 int result = 0;
698
Jonathan Corbet4a6a4492009-03-27 12:24:31 -0600699 spin_lock(&filp->f_lock);
Eric Dumazet989a2972010-04-14 09:55:35 +0000700 spin_lock(&fasync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
Linus Torvalds53281b62009-12-16 08:23:37 -0800702 if (fa->fa_file != filp)
703 continue;
Eric Dumazet989a2972010-04-14 09:55:35 +0000704
705 spin_lock_irq(&fa->fa_lock);
706 fa->fa_file = NULL;
707 spin_unlock_irq(&fa->fa_lock);
708
Linus Torvalds53281b62009-12-16 08:23:37 -0800709 *fp = fa->fa_next;
Eric Dumazet989a2972010-04-14 09:55:35 +0000710 call_rcu(&fa->fa_rcu, fasync_free_rcu);
Jonathan Corbet76398422009-02-01 14:26:59 -0700711 filp->f_flags &= ~FASYNC;
Linus Torvalds53281b62009-12-16 08:23:37 -0800712 result = 1;
713 break;
714 }
Eric Dumazet989a2972010-04-14 09:55:35 +0000715 spin_unlock(&fasync_lock);
Jonathan Corbet4a6a4492009-03-27 12:24:31 -0600716 spin_unlock(&filp->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return result;
718}
719
Linus Torvaldsf7347ce2010-10-27 12:38:12 -0400720struct fasync_struct *fasync_alloc(void)
Linus Torvalds53281b62009-12-16 08:23:37 -0800721{
Linus Torvaldsf7347ce2010-10-27 12:38:12 -0400722 return kmem_cache_alloc(fasync_cache, GFP_KERNEL);
723}
Linus Torvalds53281b62009-12-16 08:23:37 -0800724
Linus Torvaldsf7347ce2010-10-27 12:38:12 -0400725/*
726 * NOTE! This can be used only for unused fasync entries:
727 * entries that actually got inserted on the fasync list
728 * need to be released by rcu - see fasync_remove_entry.
729 */
730void fasync_free(struct fasync_struct *new)
731{
732 kmem_cache_free(fasync_cache, new);
733}
734
735/*
736 * Insert a new entry into the fasync list. Return the pointer to the
737 * old one if we didn't use the new one.
Linus Torvalds55f335a2010-10-27 18:17:02 -0700738 *
739 * NOTE! It is very important that the FASYNC flag always
740 * match the state "is the filp on a fasync list".
Linus Torvaldsf7347ce2010-10-27 12:38:12 -0400741 */
742struct fasync_struct *fasync_insert_entry(int fd, struct file *filp, struct fasync_struct **fapp, struct fasync_struct *new)
743{
744 struct fasync_struct *fa, **fp;
Linus Torvalds53281b62009-12-16 08:23:37 -0800745
746 spin_lock(&filp->f_lock);
Eric Dumazet989a2972010-04-14 09:55:35 +0000747 spin_lock(&fasync_lock);
Linus Torvalds53281b62009-12-16 08:23:37 -0800748 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
749 if (fa->fa_file != filp)
750 continue;
Eric Dumazet989a2972010-04-14 09:55:35 +0000751
752 spin_lock_irq(&fa->fa_lock);
Linus Torvalds53281b62009-12-16 08:23:37 -0800753 fa->fa_fd = fd;
Eric Dumazet989a2972010-04-14 09:55:35 +0000754 spin_unlock_irq(&fa->fa_lock);
Linus Torvalds53281b62009-12-16 08:23:37 -0800755 goto out;
756 }
757
Eric Dumazet989a2972010-04-14 09:55:35 +0000758 spin_lock_init(&new->fa_lock);
Linus Torvalds53281b62009-12-16 08:23:37 -0800759 new->magic = FASYNC_MAGIC;
760 new->fa_file = filp;
761 new->fa_fd = fd;
762 new->fa_next = *fapp;
Eric Dumazet989a2972010-04-14 09:55:35 +0000763 rcu_assign_pointer(*fapp, new);
Linus Torvalds53281b62009-12-16 08:23:37 -0800764 filp->f_flags |= FASYNC;
765
766out:
Eric Dumazet989a2972010-04-14 09:55:35 +0000767 spin_unlock(&fasync_lock);
Linus Torvalds53281b62009-12-16 08:23:37 -0800768 spin_unlock(&filp->f_lock);
Linus Torvaldsf7347ce2010-10-27 12:38:12 -0400769 return fa;
770}
771
772/*
773 * Add a fasync entry. Return negative on error, positive if
774 * added, and zero if did nothing but change an existing one.
Linus Torvaldsf7347ce2010-10-27 12:38:12 -0400775 */
776static int fasync_add_entry(int fd, struct file *filp, struct fasync_struct **fapp)
777{
778 struct fasync_struct *new;
779
780 new = fasync_alloc();
781 if (!new)
782 return -ENOMEM;
783
784 /*
785 * fasync_insert_entry() returns the old (update) entry if
786 * it existed.
787 *
788 * So free the (unused) new entry and return 0 to let the
789 * caller know that we didn't add any new fasync entries.
790 */
791 if (fasync_insert_entry(fd, filp, fapp, new)) {
792 fasync_free(new);
793 return 0;
794 }
795
796 return 1;
Linus Torvalds53281b62009-12-16 08:23:37 -0800797}
798
799/*
800 * fasync_helper() is used by almost all character device drivers
801 * to set up the fasync queue, and for regular files by the file
802 * lease code. It returns negative on error, 0 if it did no changes
803 * and positive if it added/deleted the entry.
804 */
805int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
806{
807 if (!on)
808 return fasync_remove_entry(filp, fapp);
809 return fasync_add_entry(fd, filp, fapp);
810}
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812EXPORT_SYMBOL(fasync_helper);
813
Eric Dumazet989a2972010-04-14 09:55:35 +0000814/*
815 * rcu_read_lock() is held
816 */
817static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 while (fa) {
Eric Dumazet989a2972010-04-14 09:55:35 +0000820 struct fown_struct *fown;
Andrew Mortonf4985dc2010-06-29 15:05:42 -0700821 unsigned long flags;
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (fa->magic != FASYNC_MAGIC) {
824 printk(KERN_ERR "kill_fasync: bad magic number in "
825 "fasync_struct!\n");
826 return;
827 }
Andrew Mortonf4985dc2010-06-29 15:05:42 -0700828 spin_lock_irqsave(&fa->fa_lock, flags);
Eric Dumazet989a2972010-04-14 09:55:35 +0000829 if (fa->fa_file) {
830 fown = &fa->fa_file->f_owner;
831 /* Don't send SIGURG to processes which have not set a
832 queued signum: SIGURG has its own default signalling
833 mechanism. */
834 if (!(sig == SIGURG && fown->signum == 0))
835 send_sigio(fown, fa->fa_fd, band);
836 }
Andrew Mortonf4985dc2010-06-29 15:05:42 -0700837 spin_unlock_irqrestore(&fa->fa_lock, flags);
Eric Dumazet989a2972010-04-14 09:55:35 +0000838 fa = rcu_dereference(fa->fa_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840}
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842void kill_fasync(struct fasync_struct **fp, int sig, int band)
843{
844 /* First a quick test without locking: usually
845 * the list is empty.
846 */
847 if (*fp) {
Eric Dumazet989a2972010-04-14 09:55:35 +0000848 rcu_read_lock();
849 kill_fasync_rcu(rcu_dereference(*fp), sig, band);
850 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
852}
853EXPORT_SYMBOL(kill_fasync);
854
Wu Fengguang454eedb2010-08-10 18:01:29 -0700855static int __init fcntl_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
James Bottomley3ab04d52010-09-09 16:38:12 -0700857 /*
858 * Please add new bits here to ensure allocation uniqueness.
859 * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
860 * is defined as O_NONBLOCK on some platforms and not on others.
861 */
Al Viro1abf0c72011-03-13 03:51:11 -0400862 BUILD_BUG_ON(19 - 1 /* for O_RDONLY being 0 */ != HWEIGHT32(
Wu Fengguang454eedb2010-08-10 18:01:29 -0700863 O_RDONLY | O_WRONLY | O_RDWR |
864 O_CREAT | O_EXCL | O_NOCTTY |
James Bottomley3ab04d52010-09-09 16:38:12 -0700865 O_TRUNC | O_APPEND | /* O_NONBLOCK | */
Wu Fengguang454eedb2010-08-10 18:01:29 -0700866 __O_SYNC | O_DSYNC | FASYNC |
867 O_DIRECT | O_LARGEFILE | O_DIRECTORY |
868 O_NOFOLLOW | O_NOATIME | O_CLOEXEC |
Al Viro1abf0c72011-03-13 03:51:11 -0400869 __FMODE_EXEC | O_PATH
Wu Fengguang454eedb2010-08-10 18:01:29 -0700870 ));
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 fasync_cache = kmem_cache_create("fasync_cache",
Paul Mundt20c2df82007-07-20 10:11:58 +0900873 sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return 0;
875}
876
Wu Fengguang454eedb2010-08-10 18:01:29 -0700877module_init(fcntl_init)