blob: f6ac5285060d643512e808221a9c2932bb7aadde [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/fcntl.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
8#include <linux/syscalls.h>
9#include <linux/init.h>
10#include <linux/mm.h>
Ingo Molnar29930022017-02-08 18:51:36 +010011#include <linux/sched/task.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/fs.h>
13#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040014#include <linux/fdtable.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080015#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/dnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/slab.h>
18#include <linux/module.h>
Jens Axboe35f3d142010-05-20 10:43:18 +020019#include <linux/pipe_fs_i.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/security.h>
21#include <linux/ptrace.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070022#include <linux/signal.h>
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070023#include <linux/rcupdate.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070024#include <linux/pid_namespace.h>
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -070025#include <linux/user_namespace.h>
Mike Kravetz5d752602018-06-07 17:06:01 -070026#include <linux/memfd.h>
Al Viro80f0cce2017-04-08 18:10:56 -040027#include <linux/compat.h>
Christian Brauner9eccd122021-01-21 14:19:38 +010028#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Al Virocfe39442018-02-01 12:14:57 -050030#include <linux/poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/siginfo.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080032#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Jonathan Corbet76398422009-02-01 14:26:59 -070034#define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36static int setfl(int fd, struct file * filp, unsigned long arg)
37{
Al Viro496ad9a2013-01-23 17:07:38 -050038 struct inode * inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 int error = 0;
40
dean gaudet7d95c8f2006-02-03 03:04:30 -080041 /*
42 * O_APPEND cannot be cleared if the file is marked as append-only
43 * and the file is open for write.
44 */
45 if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 return -EPERM;
47
48 /* O_NOATIME can only be set by the owner or superuser */
49 if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
Christian Brauner9eccd122021-01-21 14:19:38 +010050 if (!inode_owner_or_capable(file_mnt_user_ns(filp), inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 return -EPERM;
52
53 /* required for strict SunOS emulation */
54 if (O_NONBLOCK != O_NDELAY)
55 if (arg & O_NDELAY)
56 arg |= O_NONBLOCK;
57
Stanislav Kinsburskiy0dbf5f22015-12-15 19:41:31 +040058 /* Pipe packetized mode is controlled by O_DIRECT flag */
Al Viro45063092016-12-04 18:24:56 -050059 if (!S_ISFIFO(inode->i_mode) && (arg & O_DIRECT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 if (!filp->f_mapping || !filp->f_mapping->a_ops ||
61 !filp->f_mapping->a_ops->direct_IO)
62 return -EINVAL;
63 }
64
Al Viro72c2d532013-09-22 16:27:52 -040065 if (filp->f_op->check_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 error = filp->f_op->check_flags(arg);
67 if (error)
68 return error;
69
Jonathan Corbet218d11a2008-12-05 16:12:48 -070070 /*
Jonathan Corbet76398422009-02-01 14:26:59 -070071 * ->fasync() is responsible for setting the FASYNC bit.
Jonathan Corbet218d11a2008-12-05 16:12:48 -070072 */
Al Viro72c2d532013-09-22 16:27:52 -040073 if (((arg ^ filp->f_flags) & FASYNC) && filp->f_op->fasync) {
Jonathan Corbet76398422009-02-01 14:26:59 -070074 error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
75 if (error < 0)
76 goto out;
Jonathan Corbet60aa4922009-02-01 14:52:56 -070077 if (error > 0)
78 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 }
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -070080 spin_lock(&filp->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -070082 spin_unlock(&filp->f_lock);
Jonathan Corbet76398422009-02-01 14:26:59 -070083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return error;
86}
87
Eric W. Biederman609d7fa2006-10-02 02:17:15 -070088static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
Oleg Nesterov2f38d702009-06-16 22:07:46 +020089 int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Linus Torvalds80e1e822010-02-07 10:11:23 -080091 write_lock_irq(&filp->f_owner.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (force || !filp->f_owner.pid) {
Eric W. Biederman609d7fa2006-10-02 02:17:15 -070093 put_pid(filp->f_owner.pid);
94 filp->f_owner.pid = get_pid(pid);
95 filp->f_owner.pid_type = type;
Oleg Nesterov2f38d702009-06-16 22:07:46 +020096
97 if (pid) {
98 const struct cred *cred = current_cred();
99 filp->f_owner.uid = cred->uid;
100 filp->f_owner.euid = cred->euid;
101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
Linus Torvalds80e1e822010-02-07 10:11:23 -0800103 write_unlock_irq(&filp->f_owner.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Jeff Laytone0b93ed2014-08-22 11:27:32 -0400106void __f_setown(struct file *filp, struct pid *pid, enum pid_type type,
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700107 int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Jeff Laytone0b93ed2014-08-22 11:27:32 -0400109 security_file_set_fowner(filp);
Oleg Nesterov2f38d702009-06-16 22:07:46 +0200110 f_modown(filp, pid, type, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700112EXPORT_SYMBOL(__f_setown);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Jiri Slaby393cc3f2017-06-13 13:35:50 +0200114int f_setown(struct file *filp, unsigned long arg, int force)
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700115{
116 enum pid_type type;
Jeff Laytonf7312732017-06-14 09:11:54 -0400117 struct pid *pid = NULL;
118 int who = arg, ret = 0;
119
Eric W. Biederman01919132017-07-16 22:05:57 -0500120 type = PIDTYPE_TGID;
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700121 if (who < 0) {
Jiri Slabyfc3dc672017-06-13 13:35:51 +0200122 /* avoid overflow below */
123 if (who == INT_MIN)
124 return -EINVAL;
125
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700126 type = PIDTYPE_PGID;
127 who = -who;
128 }
Jeff Laytonf7312732017-06-14 09:11:54 -0400129
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700130 rcu_read_lock();
Jeff Laytonf7312732017-06-14 09:11:54 -0400131 if (who) {
132 pid = find_vpid(who);
133 if (!pid)
134 ret = -ESRCH;
135 }
136
137 if (!ret)
138 __f_setown(filp, pid, type, force);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700139 rcu_read_unlock();
Jiri Slaby393cc3f2017-06-13 13:35:50 +0200140
Jeff Laytonf7312732017-06-14 09:11:54 -0400141 return ret;
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700142}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143EXPORT_SYMBOL(f_setown);
144
145void f_delown(struct file *filp)
146{
Eric W. Biederman01919132017-07-16 22:05:57 -0500147 f_modown(filp, NULL, PIDTYPE_TGID, 1);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700148}
149
150pid_t f_getown(struct file *filp)
151{
152 pid_t pid;
Eric W. Biederman43fa1adb2006-10-02 02:17:27 -0700153 read_lock(&filp->f_owner.lock);
Pavel Emelyanov6c5f3e72008-02-08 04:19:20 -0800154 pid = pid_vnr(filp->f_owner.pid);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700155 if (filp->f_owner.pid_type == PIDTYPE_PGID)
156 pid = -pid;
Eric W. Biederman43fa1adb2006-10-02 02:17:27 -0700157 read_unlock(&filp->f_owner.lock);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700158 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700161static int f_setown_ex(struct file *filp, unsigned long arg)
162{
Al Viro63784dd2012-09-26 21:43:05 -0400163 struct f_owner_ex __user *owner_p = (void __user *)arg;
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700164 struct f_owner_ex owner;
165 struct pid *pid;
166 int type;
167 int ret;
168
169 ret = copy_from_user(&owner, owner_p, sizeof(owner));
170 if (ret)
Dan Carpenter5b544702010-06-03 12:35:42 +0200171 return -EFAULT;
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700172
173 switch (owner.type) {
174 case F_OWNER_TID:
Eric W. Biederman01919132017-07-16 22:05:57 -0500175 type = PIDTYPE_PID;
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700176 break;
177
178 case F_OWNER_PID:
Eric W. Biederman01919132017-07-16 22:05:57 -0500179 type = PIDTYPE_TGID;
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700180 break;
181
Peter Zijlstra978b4052009-11-17 14:06:24 -0800182 case F_OWNER_PGRP:
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700183 type = PIDTYPE_PGID;
184 break;
185
186 default:
187 return -EINVAL;
188 }
189
190 rcu_read_lock();
191 pid = find_vpid(owner.pid);
192 if (owner.pid && !pid)
193 ret = -ESRCH;
194 else
Jeff Laytone0b93ed2014-08-22 11:27:32 -0400195 __f_setown(filp, pid, type, 1);
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700196 rcu_read_unlock();
197
198 return ret;
199}
200
201static int f_getown_ex(struct file *filp, unsigned long arg)
202{
Al Viro63784dd2012-09-26 21:43:05 -0400203 struct f_owner_ex __user *owner_p = (void __user *)arg;
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700204 struct f_owner_ex owner;
205 int ret = 0;
206
207 read_lock(&filp->f_owner.lock);
208 owner.pid = pid_vnr(filp->f_owner.pid);
209 switch (filp->f_owner.pid_type) {
Eric W. Biederman01919132017-07-16 22:05:57 -0500210 case PIDTYPE_PID:
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700211 owner.type = F_OWNER_TID;
212 break;
213
Eric W. Biederman01919132017-07-16 22:05:57 -0500214 case PIDTYPE_TGID:
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700215 owner.type = F_OWNER_PID;
216 break;
217
218 case PIDTYPE_PGID:
Peter Zijlstra978b4052009-11-17 14:06:24 -0800219 owner.type = F_OWNER_PGRP;
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700220 break;
221
222 default:
223 WARN_ON(1);
224 ret = -EINVAL;
225 break;
226 }
227 read_unlock(&filp->f_owner.lock);
228
Dan Carpenter5b544702010-06-03 12:35:42 +0200229 if (!ret) {
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700230 ret = copy_to_user(owner_p, &owner, sizeof(owner));
Dan Carpenter5b544702010-06-03 12:35:42 +0200231 if (ret)
232 ret = -EFAULT;
233 }
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700234 return ret;
235}
236
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -0700237#ifdef CONFIG_CHECKPOINT_RESTORE
238static int f_getowner_uids(struct file *filp, unsigned long arg)
239{
240 struct user_namespace *user_ns = current_user_ns();
Al Viro63784dd2012-09-26 21:43:05 -0400241 uid_t __user *dst = (void __user *)arg;
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -0700242 uid_t src[2];
243 int err;
244
245 read_lock(&filp->f_owner.lock);
246 src[0] = from_kuid(user_ns, filp->f_owner.uid);
247 src[1] = from_kuid(user_ns, filp->f_owner.euid);
248 read_unlock(&filp->f_owner.lock);
249
250 err = put_user(src[0], &dst[0]);
251 err |= put_user(src[1], &dst[1]);
252
253 return err;
254}
255#else
256static int f_getowner_uids(struct file *filp, unsigned long arg)
257{
258 return -EINVAL;
259}
260#endif
261
Jens Axboec75b1d92017-06-27 11:47:04 -0600262static bool rw_hint_valid(enum rw_hint hint)
263{
264 switch (hint) {
Eugene Syromiatnikov9a7f12e2019-09-20 17:58:21 +0200265 case RWH_WRITE_LIFE_NOT_SET:
Jens Axboec75b1d92017-06-27 11:47:04 -0600266 case RWH_WRITE_LIFE_NONE:
267 case RWH_WRITE_LIFE_SHORT:
268 case RWH_WRITE_LIFE_MEDIUM:
269 case RWH_WRITE_LIFE_LONG:
270 case RWH_WRITE_LIFE_EXTREME:
271 return true;
272 default:
273 return false;
274 }
275}
276
277static long fcntl_rw_hint(struct file *file, unsigned int cmd,
278 unsigned long arg)
279{
280 struct inode *inode = file_inode(file);
Ben Dookse2003272019-10-15 11:50:49 +0100281 u64 __user *argp = (u64 __user *)arg;
Jens Axboec75b1d92017-06-27 11:47:04 -0600282 enum rw_hint hint;
Jens Axboe5657cb02017-06-28 08:09:45 -0600283 u64 h;
Jens Axboec75b1d92017-06-27 11:47:04 -0600284
285 switch (cmd) {
286 case F_GET_FILE_RW_HINT:
Jens Axboe5657cb02017-06-28 08:09:45 -0600287 h = file_write_hint(file);
288 if (copy_to_user(argp, &h, sizeof(*argp)))
Jens Axboec75b1d92017-06-27 11:47:04 -0600289 return -EFAULT;
290 return 0;
291 case F_SET_FILE_RW_HINT:
Jens Axboe5657cb02017-06-28 08:09:45 -0600292 if (copy_from_user(&h, argp, sizeof(h)))
Jens Axboec75b1d92017-06-27 11:47:04 -0600293 return -EFAULT;
Jens Axboe5657cb02017-06-28 08:09:45 -0600294 hint = (enum rw_hint) h;
Jens Axboec75b1d92017-06-27 11:47:04 -0600295 if (!rw_hint_valid(hint))
296 return -EINVAL;
297
298 spin_lock(&file->f_lock);
299 file->f_write_hint = hint;
300 spin_unlock(&file->f_lock);
301 return 0;
302 case F_GET_RW_HINT:
Jens Axboe5657cb02017-06-28 08:09:45 -0600303 h = inode->i_write_hint;
304 if (copy_to_user(argp, &h, sizeof(*argp)))
Jens Axboec75b1d92017-06-27 11:47:04 -0600305 return -EFAULT;
306 return 0;
307 case F_SET_RW_HINT:
Jens Axboe5657cb02017-06-28 08:09:45 -0600308 if (copy_from_user(&h, argp, sizeof(h)))
Jens Axboec75b1d92017-06-27 11:47:04 -0600309 return -EFAULT;
Jens Axboe5657cb02017-06-28 08:09:45 -0600310 hint = (enum rw_hint) h;
Jens Axboec75b1d92017-06-27 11:47:04 -0600311 if (!rw_hint_valid(hint))
312 return -EINVAL;
313
314 inode_lock(inode);
315 inode->i_write_hint = hint;
316 inode_unlock(inode);
317 return 0;
318 default:
319 return -EINVAL;
320 }
321}
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
324 struct file *filp)
325{
Christoph Hellwiga75d30c2017-05-27 06:07:19 -0400326 void __user *argp = (void __user *)arg;
327 struct flock flock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 long err = -EINVAL;
329
330 switch (cmd) {
331 case F_DUPFD:
Al Virofe17f222012-08-21 11:48:11 -0400332 err = f_dupfd(arg, filp, 0);
333 break;
Ulrich Drepper22d2b352007-10-16 23:30:26 -0700334 case F_DUPFD_CLOEXEC:
Al Viro12197712012-10-08 23:21:58 +0100335 err = f_dupfd(arg, filp, O_CLOEXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 break;
337 case F_GETFD:
338 err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
339 break;
340 case F_SETFD:
341 err = 0;
342 set_close_on_exec(fd, arg & FD_CLOEXEC);
343 break;
344 case F_GETFL:
345 err = filp->f_flags;
346 break;
347 case F_SETFL:
348 err = setfl(fd, filp, arg);
349 break;
Jeff Layton5d50ffd2014-02-03 12:13:10 -0500350#if BITS_PER_LONG != 32
351 /* 32-bit arches must use fcntl64() */
Jeff Layton0d3f7a22014-04-22 08:23:58 -0400352 case F_OFD_GETLK:
Jeff Layton5d50ffd2014-02-03 12:13:10 -0500353#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 case F_GETLK:
Christoph Hellwiga75d30c2017-05-27 06:07:19 -0400355 if (copy_from_user(&flock, argp, sizeof(flock)))
356 return -EFAULT;
357 err = fcntl_getlk(filp, cmd, &flock);
358 if (!err && copy_to_user(argp, &flock, sizeof(flock)))
359 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 break;
Jeff Layton5d50ffd2014-02-03 12:13:10 -0500361#if BITS_PER_LONG != 32
362 /* 32-bit arches must use fcntl64() */
Jeff Layton0d3f7a22014-04-22 08:23:58 -0400363 case F_OFD_SETLK:
364 case F_OFD_SETLKW:
Jeff Layton5d50ffd2014-02-03 12:13:10 -0500365#endif
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500366 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 case F_SETLK:
368 case F_SETLKW:
Christoph Hellwiga75d30c2017-05-27 06:07:19 -0400369 if (copy_from_user(&flock, argp, sizeof(flock)))
370 return -EFAULT;
371 err = fcntl_setlk(fd, filp, cmd, &flock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 break;
373 case F_GETOWN:
374 /*
375 * XXX If f_owner is a process group, the
376 * negative return value will get converted
377 * into an error. Oops. If we keep the
378 * current syscall conventions, the only way
379 * to fix this will be in libc.
380 */
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700381 err = f_getown(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 force_successful_syscall_return();
383 break;
384 case F_SETOWN:
Jiri Slaby393cc3f2017-06-13 13:35:50 +0200385 err = f_setown(filp, arg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 break;
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700387 case F_GETOWN_EX:
388 err = f_getown_ex(filp, arg);
389 break;
390 case F_SETOWN_EX:
391 err = f_setown_ex(filp, arg);
392 break;
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -0700393 case F_GETOWNER_UIDS:
394 err = f_getowner_uids(filp, arg);
395 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 case F_GETSIG:
397 err = filp->f_owner.signum;
398 break;
399 case F_SETSIG:
400 /* arg == 0 restores default behaviour. */
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700401 if (!valid_signal(arg)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 break;
403 }
404 err = 0;
405 filp->f_owner.signum = arg;
406 break;
407 case F_GETLEASE:
408 err = fcntl_getlease(filp);
409 break;
410 case F_SETLEASE:
411 err = fcntl_setlease(fd, filp, arg);
412 break;
413 case F_NOTIFY:
414 err = fcntl_dirnotify(fd, filp, arg);
415 break;
Jens Axboe35f3d142010-05-20 10:43:18 +0200416 case F_SETPIPE_SZ:
417 case F_GETPIPE_SZ:
418 err = pipe_fcntl(filp, cmd, arg);
419 break;
David Herrmann40e041a2014-08-08 14:25:27 -0700420 case F_ADD_SEALS:
421 case F_GET_SEALS:
Marc-André Lureau5aadc432018-01-31 16:19:18 -0800422 err = memfd_fcntl(filp, cmd, arg);
David Herrmann40e041a2014-08-08 14:25:27 -0700423 break;
Jens Axboec75b1d92017-06-27 11:47:04 -0600424 case F_GET_RW_HINT:
425 case F_SET_RW_HINT:
426 case F_GET_FILE_RW_HINT:
427 case F_SET_FILE_RW_HINT:
428 err = fcntl_rw_hint(filp, cmd, arg);
429 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 default:
431 break;
432 }
433 return err;
434}
435
Al Viro1abf0c72011-03-13 03:51:11 -0400436static int check_fcntl_cmd(unsigned cmd)
437{
438 switch (cmd) {
439 case F_DUPFD:
440 case F_DUPFD_CLOEXEC:
441 case F_GETFD:
442 case F_SETFD:
443 case F_GETFL:
444 return 1;
445 }
446 return 0;
447}
448
Heiko Carstensa26eab22009-01-14 14:14:17 +0100449SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Al Viro2903ff02012-08-28 12:52:22 -0400451 struct fd f = fdget_raw(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 long err = -EBADF;
453
Al Viro2903ff02012-08-28 12:52:22 -0400454 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 goto out;
456
Al Viro2903ff02012-08-28 12:52:22 -0400457 if (unlikely(f.file->f_mode & FMODE_PATH)) {
Al Viro545ec2c2012-04-21 18:42:19 -0400458 if (!check_fcntl_cmd(cmd))
459 goto out1;
Al Viro1abf0c72011-03-13 03:51:11 -0400460 }
461
Al Viro2903ff02012-08-28 12:52:22 -0400462 err = security_file_fcntl(f.file, cmd, arg);
Al Viro545ec2c2012-04-21 18:42:19 -0400463 if (!err)
Al Viro2903ff02012-08-28 12:52:22 -0400464 err = do_fcntl(fd, cmd, arg, f.file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Al Viro545ec2c2012-04-21 18:42:19 -0400466out1:
Al Viro2903ff02012-08-28 12:52:22 -0400467 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468out:
469 return err;
470}
471
472#if BITS_PER_LONG == 32
Heiko Carstensa26eab22009-01-14 14:14:17 +0100473SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
474 unsigned long, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Christoph Hellwiga75d30c2017-05-27 06:07:19 -0400476 void __user *argp = (void __user *)arg;
Al Viro2903ff02012-08-28 12:52:22 -0400477 struct fd f = fdget_raw(fd);
Christoph Hellwiga75d30c2017-05-27 06:07:19 -0400478 struct flock64 flock;
Al Viro545ec2c2012-04-21 18:42:19 -0400479 long err = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Al Viro2903ff02012-08-28 12:52:22 -0400481 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 goto out;
483
Al Viro2903ff02012-08-28 12:52:22 -0400484 if (unlikely(f.file->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
Al Viro2903ff02012-08-28 12:52:22 -0400489 err = security_file_fcntl(f.file, cmd, arg);
Al Viro545ec2c2012-04-21 18:42:19 -0400490 if (err)
491 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 switch (cmd) {
Jeff Layton5d50ffd2014-02-03 12:13:10 -0500494 case F_GETLK64:
Jeff Layton0d3f7a22014-04-22 08:23:58 -0400495 case F_OFD_GETLK:
Christoph Hellwiga75d30c2017-05-27 06:07:19 -0400496 err = -EFAULT;
497 if (copy_from_user(&flock, argp, sizeof(flock)))
498 break;
499 err = fcntl_getlk64(f.file, cmd, &flock);
500 if (!err && copy_to_user(argp, &flock, sizeof(flock)))
501 err = -EFAULT;
Jeff Layton5d50ffd2014-02-03 12:13:10 -0500502 break;
503 case F_SETLK64:
504 case F_SETLKW64:
Jeff Layton0d3f7a22014-04-22 08:23:58 -0400505 case F_OFD_SETLK:
506 case F_OFD_SETLKW:
Christoph Hellwiga75d30c2017-05-27 06:07:19 -0400507 err = -EFAULT;
508 if (copy_from_user(&flock, argp, sizeof(flock)))
509 break;
510 err = fcntl_setlk64(fd, f.file, cmd, &flock);
Jeff Layton5d50ffd2014-02-03 12:13:10 -0500511 break;
512 default:
513 err = do_fcntl(fd, cmd, arg, f.file);
514 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
Al Viro545ec2c2012-04-21 18:42:19 -0400516out1:
Al Viro2903ff02012-08-28 12:52:22 -0400517 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518out:
519 return err;
520}
521#endif
522
Al Viro80f0cce2017-04-08 18:10:56 -0400523#ifdef CONFIG_COMPAT
Al Viro8c6657c2017-06-26 23:51:31 -0400524/* careful - don't use anywhere else */
Linus Torvaldsb59eea52017-07-07 13:48:18 -0700525#define copy_flock_fields(dst, src) \
526 (dst)->l_type = (src)->l_type; \
527 (dst)->l_whence = (src)->l_whence; \
528 (dst)->l_start = (src)->l_start; \
529 (dst)->l_len = (src)->l_len; \
530 (dst)->l_pid = (src)->l_pid;
Al Viro8c6657c2017-06-26 23:51:31 -0400531
Linus Torvaldsb59eea52017-07-07 13:48:18 -0700532static int get_compat_flock(struct flock *kfl, const struct compat_flock __user *ufl)
Al Viro80f0cce2017-04-08 18:10:56 -0400533{
Al Viro8c6657c2017-06-26 23:51:31 -0400534 struct compat_flock fl;
535
536 if (copy_from_user(&fl, ufl, sizeof(struct compat_flock)))
Al Viro80f0cce2017-04-08 18:10:56 -0400537 return -EFAULT;
Linus Torvaldsb59eea52017-07-07 13:48:18 -0700538 copy_flock_fields(kfl, &fl);
Al Viro8c6657c2017-06-26 23:51:31 -0400539 return 0;
540}
541
Linus Torvaldsb59eea52017-07-07 13:48:18 -0700542static int get_compat_flock64(struct flock *kfl, const struct compat_flock64 __user *ufl)
Al Viro8c6657c2017-06-26 23:51:31 -0400543{
544 struct compat_flock64 fl;
545
546 if (copy_from_user(&fl, ufl, sizeof(struct compat_flock64)))
547 return -EFAULT;
Linus Torvaldsb59eea52017-07-07 13:48:18 -0700548 copy_flock_fields(kfl, &fl);
Al Viro80f0cce2017-04-08 18:10:56 -0400549 return 0;
550}
551
Linus Torvaldsb59eea52017-07-07 13:48:18 -0700552static int put_compat_flock(const struct flock *kfl, struct compat_flock __user *ufl)
Al Viro80f0cce2017-04-08 18:10:56 -0400553{
Al Viro8c6657c2017-06-26 23:51:31 -0400554 struct compat_flock fl;
555
556 memset(&fl, 0, sizeof(struct compat_flock));
Linus Torvaldsb59eea52017-07-07 13:48:18 -0700557 copy_flock_fields(&fl, kfl);
Al Viro8c6657c2017-06-26 23:51:31 -0400558 if (copy_to_user(ufl, &fl, sizeof(struct compat_flock)))
Al Viro80f0cce2017-04-08 18:10:56 -0400559 return -EFAULT;
560 return 0;
561}
562
Linus Torvaldsb59eea52017-07-07 13:48:18 -0700563static int put_compat_flock64(const struct flock *kfl, struct compat_flock64 __user *ufl)
Al Viro80f0cce2017-04-08 18:10:56 -0400564{
Al Viro8c6657c2017-06-26 23:51:31 -0400565 struct compat_flock64 fl;
566
Jeff Layton4d2dc2c2017-11-14 14:42:57 -0500567 BUILD_BUG_ON(sizeof(kfl->l_start) > sizeof(ufl->l_start));
568 BUILD_BUG_ON(sizeof(kfl->l_len) > sizeof(ufl->l_len));
569
Al Viro8c6657c2017-06-26 23:51:31 -0400570 memset(&fl, 0, sizeof(struct compat_flock64));
Linus Torvaldsb59eea52017-07-07 13:48:18 -0700571 copy_flock_fields(&fl, kfl);
Al Viro8c6657c2017-06-26 23:51:31 -0400572 if (copy_to_user(ufl, &fl, sizeof(struct compat_flock64)))
Al Viro80f0cce2017-04-08 18:10:56 -0400573 return -EFAULT;
574 return 0;
575}
Al Viro8c6657c2017-06-26 23:51:31 -0400576#undef copy_flock_fields
Al Viro80f0cce2017-04-08 18:10:56 -0400577
578static unsigned int
579convert_fcntl_cmd(unsigned int cmd)
580{
581 switch (cmd) {
582 case F_GETLK64:
583 return F_GETLK;
584 case F_SETLK64:
585 return F_SETLK;
586 case F_SETLKW64:
587 return F_SETLKW;
588 }
589
590 return cmd;
591}
592
Christoph Hellwig94073ad2017-05-27 06:07:20 -0400593/*
594 * GETLK was successful and we need to return the data, but it needs to fit in
595 * the compat structure.
596 * l_start shouldn't be too big, unless the original start + end is greater than
597 * COMPAT_OFF_T_MAX, in which case the app was asking for trouble, so we return
598 * -EOVERFLOW in that case. l_len could be too big, in which case we just
599 * truncate it, and only allow the app to see that part of the conflicting lock
600 * that might make sense to it anyway
601 */
602static int fixup_compat_flock(struct flock *flock)
603{
604 if (flock->l_start > COMPAT_OFF_T_MAX)
605 return -EOVERFLOW;
606 if (flock->l_len > COMPAT_OFF_T_MAX)
607 flock->l_len = COMPAT_OFF_T_MAX;
608 return 0;
609}
610
Dominik Brodowskie02af2f2018-03-20 19:29:53 +0100611static long do_compat_fcntl64(unsigned int fd, unsigned int cmd,
612 compat_ulong_t arg)
Al Viro80f0cce2017-04-08 18:10:56 -0400613{
Christoph Hellwig94073ad2017-05-27 06:07:20 -0400614 struct fd f = fdget_raw(fd);
615 struct flock flock;
616 long err = -EBADF;
617
618 if (!f.file)
619 return err;
620
621 if (unlikely(f.file->f_mode & FMODE_PATH)) {
622 if (!check_fcntl_cmd(cmd))
623 goto out_put;
624 }
625
626 err = security_file_fcntl(f.file, cmd, arg);
627 if (err)
628 goto out_put;
Al Viro80f0cce2017-04-08 18:10:56 -0400629
630 switch (cmd) {
631 case F_GETLK:
Christoph Hellwig94073ad2017-05-27 06:07:20 -0400632 err = get_compat_flock(&flock, compat_ptr(arg));
633 if (err)
634 break;
635 err = fcntl_getlk(f.file, convert_fcntl_cmd(cmd), &flock);
636 if (err)
637 break;
638 err = fixup_compat_flock(&flock);
Jeff Layton9280a602017-11-14 14:43:56 -0500639 if (!err)
640 err = put_compat_flock(&flock, compat_ptr(arg));
Christoph Hellwig94073ad2017-05-27 06:07:20 -0400641 break;
642 case F_GETLK64:
643 case F_OFD_GETLK:
644 err = get_compat_flock64(&flock, compat_ptr(arg));
645 if (err)
646 break;
647 err = fcntl_getlk(f.file, convert_fcntl_cmd(cmd), &flock);
Jeff Layton4d2dc2c2017-11-14 14:42:57 -0500648 if (!err)
649 err = put_compat_flock64(&flock, compat_ptr(arg));
Christoph Hellwig94073ad2017-05-27 06:07:20 -0400650 break;
Al Viro80f0cce2017-04-08 18:10:56 -0400651 case F_SETLK:
652 case F_SETLKW:
Christoph Hellwig94073ad2017-05-27 06:07:20 -0400653 err = get_compat_flock(&flock, compat_ptr(arg));
654 if (err)
Al Viro80f0cce2017-04-08 18:10:56 -0400655 break;
Christoph Hellwig94073ad2017-05-27 06:07:20 -0400656 err = fcntl_setlk(fd, f.file, convert_fcntl_cmd(cmd), &flock);
Al Viro80f0cce2017-04-08 18:10:56 -0400657 break;
Al Viro80f0cce2017-04-08 18:10:56 -0400658 case F_SETLK64:
659 case F_SETLKW64:
Al Viro80f0cce2017-04-08 18:10:56 -0400660 case F_OFD_SETLK:
661 case F_OFD_SETLKW:
Christoph Hellwig94073ad2017-05-27 06:07:20 -0400662 err = get_compat_flock64(&flock, compat_ptr(arg));
663 if (err)
Al Viro80f0cce2017-04-08 18:10:56 -0400664 break;
Christoph Hellwig94073ad2017-05-27 06:07:20 -0400665 err = fcntl_setlk(fd, f.file, convert_fcntl_cmd(cmd), &flock);
Al Viro80f0cce2017-04-08 18:10:56 -0400666 break;
Al Viro80f0cce2017-04-08 18:10:56 -0400667 default:
Christoph Hellwig94073ad2017-05-27 06:07:20 -0400668 err = do_fcntl(fd, cmd, arg, f.file);
Al Viro80f0cce2017-04-08 18:10:56 -0400669 break;
670 }
Christoph Hellwig94073ad2017-05-27 06:07:20 -0400671out_put:
672 fdput(f);
673 return err;
Al Viro80f0cce2017-04-08 18:10:56 -0400674}
675
Dominik Brodowskie02af2f2018-03-20 19:29:53 +0100676COMPAT_SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
677 compat_ulong_t, arg)
678{
679 return do_compat_fcntl64(fd, cmd, arg);
680}
681
Al Viro80f0cce2017-04-08 18:10:56 -0400682COMPAT_SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd,
683 compat_ulong_t, arg)
684{
685 switch (cmd) {
686 case F_GETLK64:
687 case F_SETLK64:
688 case F_SETLKW64:
689 case F_OFD_GETLK:
690 case F_OFD_SETLK:
691 case F_OFD_SETLKW:
692 return -EINVAL;
693 }
Dominik Brodowskie02af2f2018-03-20 19:29:53 +0100694 return do_compat_fcntl64(fd, cmd, arg);
Al Viro80f0cce2017-04-08 18:10:56 -0400695}
696#endif
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698/* Table to convert sigio signal codes into poll band bitmaps */
699
Al Viro5dc533c2017-07-16 22:14:00 -0400700static const __poll_t band_table[NSIGPOLL] = {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800701 EPOLLIN | EPOLLRDNORM, /* POLL_IN */
702 EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND, /* POLL_OUT */
703 EPOLLIN | EPOLLRDNORM | EPOLLMSG, /* POLL_MSG */
704 EPOLLERR, /* POLL_ERR */
705 EPOLLPRI | EPOLLRDBAND, /* POLL_PRI */
706 EPOLLHUP | EPOLLERR /* POLL_HUP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707};
708
709static inline int sigio_perm(struct task_struct *p,
710 struct fown_struct *fown, int sig)
711{
David Howellsc69e8d92008-11-14 10:39:19 +1100712 const struct cred *cred;
713 int ret;
714
715 rcu_read_lock();
716 cred = __task_cred(p);
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -0800717 ret = ((uid_eq(fown->euid, GLOBAL_ROOT_UID) ||
718 uid_eq(fown->euid, cred->suid) || uid_eq(fown->euid, cred->uid) ||
719 uid_eq(fown->uid, cred->suid) || uid_eq(fown->uid, cred->uid)) &&
David Howellsc69e8d92008-11-14 10:39:19 +1100720 !security_file_send_sigiotask(p, fown, sig));
721 rcu_read_unlock();
722 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
725static void send_sigio_to_task(struct task_struct *p,
Oleg Nesterov8eeee4e2009-06-17 00:27:10 +0200726 struct fown_struct *fown,
Eric W. Biederman9c2db002018-07-21 08:17:29 -0500727 int fd, int reason, enum pid_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Oleg Nesterov8eeee4e2009-06-17 00:27:10 +0200729 /*
730 * F_SETSIG can change ->signum lockless in parallel, make
731 * sure we read it once and use the same value throughout.
732 */
Mark Rutland6aa7de02017-10-23 14:07:29 -0700733 int signum = READ_ONCE(fown->signum);
Oleg Nesterov8eeee4e2009-06-17 00:27:10 +0200734
735 if (!sigio_perm(p, fown, signum))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return;
737
Oleg Nesterov8eeee4e2009-06-17 00:27:10 +0200738 switch (signum) {
Kees Cook0a68ff52020-02-19 22:22:43 -0800739 default: {
740 kernel_siginfo_t si;
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 /* Queue a rt signal with the appropriate fd as its
743 value. We use SI_SIGIO as the source, not
744 SI_KERNEL, since kernel signals always get
745 delivered even if we can't queue. Failure to
746 queue in this case _should_ be reported; we fall
747 back to SIGIO in that case. --sct */
Eric W. Biedermanfaf1f222018-01-05 17:27:42 -0600748 clear_siginfo(&si);
Oleg Nesterov8eeee4e2009-06-17 00:27:10 +0200749 si.si_signo = signum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 si.si_errno = 0;
751 si.si_code = reason;
Eric W. Biedermand08477a2017-06-29 09:28:50 -0500752 /*
753 * Posix definies POLL_IN and friends to be signal
754 * specific si_codes for SIG_POLL. Linux extended
755 * these si_codes to other signals in a way that is
756 * ambiguous if other signals also have signal
757 * specific si_codes. In that case use SI_SIGIO instead
758 * to remove the ambiguity.
759 */
Eric W. Biederman54640d22017-09-18 22:51:14 -0500760 if ((signum != SIGPOLL) && sig_specific_sicodes(signum))
Eric W. Biedermand08477a2017-06-29 09:28:50 -0500761 si.si_code = SI_SIGIO;
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 /* Make sure we are called with one of the POLL_*
764 reasons, otherwise we could leak kernel stack into
765 userspace. */
Eric W. Biedermand08477a2017-06-29 09:28:50 -0500766 BUG_ON((reason < POLL_IN) || ((reason - POLL_IN) >= NSIGPOLL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (reason - POLL_IN >= NSIGPOLL)
768 si.si_band = ~0L;
769 else
Al Viroc71d2272017-11-29 19:00:41 -0500770 si.si_band = mangle_poll(band_table[reason - POLL_IN]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 si.si_fd = fd;
Eric W. Biederman40b3b022018-07-21 10:45:15 -0500772 if (!do_send_sig_info(signum, &si, p, type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 break;
Kees Cook0a68ff52020-02-19 22:22:43 -0800774 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500775 fallthrough; /* fall back on the old plain SIGIO signal */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 case 0:
Eric W. Biederman40b3b022018-07-21 10:45:15 -0500777 do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
779}
780
781void send_sigio(struct fown_struct *fown, int fd, int band)
782{
783 struct task_struct *p;
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700784 enum pid_type type;
Boqun Feng8d1ddb52020-11-05 14:23:51 +0800785 unsigned long flags;
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700786 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Boqun Feng8d1ddb52020-11-05 14:23:51 +0800788 read_lock_irqsave(&fown->lock, flags);
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700789
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700790 type = fown->pid_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 pid = fown->pid;
792 if (!pid)
793 goto out_unlock_fown;
Eric W. Biederman01919132017-07-16 22:05:57 -0500794
795 if (type <= PIDTYPE_TGID) {
796 rcu_read_lock();
797 p = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman84fe4cc2018-08-15 21:20:46 -0500798 if (p)
799 send_sigio_to_task(p, fown, fd, band, type);
Eric W. Biederman01919132017-07-16 22:05:57 -0500800 rcu_read_unlock();
801 } else {
802 read_lock(&tasklist_lock);
803 do_each_pid_task(pid, type, p) {
Eric W. Biederman9c2db002018-07-21 08:17:29 -0500804 send_sigio_to_task(p, fown, fd, band, type);
Eric W. Biederman01919132017-07-16 22:05:57 -0500805 } while_each_pid_task(pid, type, p);
806 read_unlock(&tasklist_lock);
807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 out_unlock_fown:
Boqun Feng8d1ddb52020-11-05 14:23:51 +0800809 read_unlock_irqrestore(&fown->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
812static void send_sigurg_to_task(struct task_struct *p,
Eric W. Biederman9c2db002018-07-21 08:17:29 -0500813 struct fown_struct *fown, enum pid_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
815 if (sigio_perm(p, fown, SIGURG))
Eric W. Biederman40b3b022018-07-21 10:45:15 -0500816 do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
819int send_sigurg(struct fown_struct *fown)
820{
821 struct task_struct *p;
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700822 enum pid_type type;
823 struct pid *pid;
Boqun Feng8d1ddb52020-11-05 14:23:51 +0800824 unsigned long flags;
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700825 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Boqun Feng8d1ddb52020-11-05 14:23:51 +0800827 read_lock_irqsave(&fown->lock, flags);
Peter Zijlstraba0a6c92009-09-23 15:57:03 -0700828
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700829 type = fown->pid_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 pid = fown->pid;
831 if (!pid)
832 goto out_unlock_fown;
833
834 ret = 1;
Eric W. Biederman01919132017-07-16 22:05:57 -0500835
836 if (type <= PIDTYPE_TGID) {
837 rcu_read_lock();
838 p = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman84fe4cc2018-08-15 21:20:46 -0500839 if (p)
840 send_sigurg_to_task(p, fown, type);
Eric W. Biederman01919132017-07-16 22:05:57 -0500841 rcu_read_unlock();
842 } else {
843 read_lock(&tasklist_lock);
844 do_each_pid_task(pid, type, p) {
Eric W. Biederman9c2db002018-07-21 08:17:29 -0500845 send_sigurg_to_task(p, fown, type);
Eric W. Biederman01919132017-07-16 22:05:57 -0500846 } while_each_pid_task(pid, type, p);
847 read_unlock(&tasklist_lock);
848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 out_unlock_fown:
Boqun Feng8d1ddb52020-11-05 14:23:51 +0800850 read_unlock_irqrestore(&fown->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return ret;
852}
853
Eric Dumazet989a2972010-04-14 09:55:35 +0000854static DEFINE_SPINLOCK(fasync_lock);
Christoph Lametere18b8902006-12-06 20:33:20 -0800855static struct kmem_cache *fasync_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Eric Dumazet989a2972010-04-14 09:55:35 +0000857static void fasync_free_rcu(struct rcu_head *head)
858{
859 kmem_cache_free(fasync_cache,
860 container_of(head, struct fasync_struct, fa_rcu));
861}
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863/*
Linus Torvalds53281b62009-12-16 08:23:37 -0800864 * Remove a fasync entry. If successfully removed, return
865 * positive and clear the FASYNC flag. If no entry exists,
866 * do nothing and return 0.
867 *
868 * NOTE! It is very important that the FASYNC flag always
869 * match the state "is the filp on a fasync list".
870 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 */
Linus Torvaldsf7347ce2010-10-27 12:38:12 -0400872int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 struct fasync_struct *fa, **fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 int result = 0;
876
Jonathan Corbet4a6a4492009-03-27 12:24:31 -0600877 spin_lock(&filp->f_lock);
Eric Dumazet989a2972010-04-14 09:55:35 +0000878 spin_lock(&fasync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
Linus Torvalds53281b62009-12-16 08:23:37 -0800880 if (fa->fa_file != filp)
881 continue;
Eric Dumazet989a2972010-04-14 09:55:35 +0000882
Kirill Tkhai7a107c02018-04-05 14:58:06 +0300883 write_lock_irq(&fa->fa_lock);
Eric Dumazet989a2972010-04-14 09:55:35 +0000884 fa->fa_file = NULL;
Kirill Tkhai7a107c02018-04-05 14:58:06 +0300885 write_unlock_irq(&fa->fa_lock);
Eric Dumazet989a2972010-04-14 09:55:35 +0000886
Linus Torvalds53281b62009-12-16 08:23:37 -0800887 *fp = fa->fa_next;
Eric Dumazet989a2972010-04-14 09:55:35 +0000888 call_rcu(&fa->fa_rcu, fasync_free_rcu);
Jonathan Corbet76398422009-02-01 14:26:59 -0700889 filp->f_flags &= ~FASYNC;
Linus Torvalds53281b62009-12-16 08:23:37 -0800890 result = 1;
891 break;
892 }
Eric Dumazet989a2972010-04-14 09:55:35 +0000893 spin_unlock(&fasync_lock);
Jonathan Corbet4a6a4492009-03-27 12:24:31 -0600894 spin_unlock(&filp->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return result;
896}
897
Linus Torvaldsf7347ce2010-10-27 12:38:12 -0400898struct fasync_struct *fasync_alloc(void)
Linus Torvalds53281b62009-12-16 08:23:37 -0800899{
Linus Torvaldsf7347ce2010-10-27 12:38:12 -0400900 return kmem_cache_alloc(fasync_cache, GFP_KERNEL);
901}
Linus Torvalds53281b62009-12-16 08:23:37 -0800902
Linus Torvaldsf7347ce2010-10-27 12:38:12 -0400903/*
904 * NOTE! This can be used only for unused fasync entries:
905 * entries that actually got inserted on the fasync list
906 * need to be released by rcu - see fasync_remove_entry.
907 */
908void fasync_free(struct fasync_struct *new)
909{
910 kmem_cache_free(fasync_cache, new);
911}
912
913/*
914 * Insert a new entry into the fasync list. Return the pointer to the
915 * old one if we didn't use the new one.
Linus Torvalds55f335a2010-10-27 18:17:02 -0700916 *
917 * NOTE! It is very important that the FASYNC flag always
918 * match the state "is the filp on a fasync list".
Linus Torvaldsf7347ce2010-10-27 12:38:12 -0400919 */
920struct fasync_struct *fasync_insert_entry(int fd, struct file *filp, struct fasync_struct **fapp, struct fasync_struct *new)
921{
922 struct fasync_struct *fa, **fp;
Linus Torvalds53281b62009-12-16 08:23:37 -0800923
924 spin_lock(&filp->f_lock);
Eric Dumazet989a2972010-04-14 09:55:35 +0000925 spin_lock(&fasync_lock);
Linus Torvalds53281b62009-12-16 08:23:37 -0800926 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
927 if (fa->fa_file != filp)
928 continue;
Eric Dumazet989a2972010-04-14 09:55:35 +0000929
Kirill Tkhai7a107c02018-04-05 14:58:06 +0300930 write_lock_irq(&fa->fa_lock);
Linus Torvalds53281b62009-12-16 08:23:37 -0800931 fa->fa_fd = fd;
Kirill Tkhai7a107c02018-04-05 14:58:06 +0300932 write_unlock_irq(&fa->fa_lock);
Linus Torvalds53281b62009-12-16 08:23:37 -0800933 goto out;
934 }
935
Kirill Tkhai7a107c02018-04-05 14:58:06 +0300936 rwlock_init(&new->fa_lock);
Linus Torvalds53281b62009-12-16 08:23:37 -0800937 new->magic = FASYNC_MAGIC;
938 new->fa_file = filp;
939 new->fa_fd = fd;
940 new->fa_next = *fapp;
Eric Dumazet989a2972010-04-14 09:55:35 +0000941 rcu_assign_pointer(*fapp, new);
Linus Torvalds53281b62009-12-16 08:23:37 -0800942 filp->f_flags |= FASYNC;
943
944out:
Eric Dumazet989a2972010-04-14 09:55:35 +0000945 spin_unlock(&fasync_lock);
Linus Torvalds53281b62009-12-16 08:23:37 -0800946 spin_unlock(&filp->f_lock);
Linus Torvaldsf7347ce2010-10-27 12:38:12 -0400947 return fa;
948}
949
950/*
951 * Add a fasync entry. Return negative on error, positive if
952 * added, and zero if did nothing but change an existing one.
Linus Torvaldsf7347ce2010-10-27 12:38:12 -0400953 */
954static int fasync_add_entry(int fd, struct file *filp, struct fasync_struct **fapp)
955{
956 struct fasync_struct *new;
957
958 new = fasync_alloc();
959 if (!new)
960 return -ENOMEM;
961
962 /*
963 * fasync_insert_entry() returns the old (update) entry if
964 * it existed.
965 *
966 * So free the (unused) new entry and return 0 to let the
967 * caller know that we didn't add any new fasync entries.
968 */
969 if (fasync_insert_entry(fd, filp, fapp, new)) {
970 fasync_free(new);
971 return 0;
972 }
973
974 return 1;
Linus Torvalds53281b62009-12-16 08:23:37 -0800975}
976
977/*
978 * fasync_helper() is used by almost all character device drivers
979 * to set up the fasync queue, and for regular files by the file
980 * lease code. It returns negative on error, 0 if it did no changes
981 * and positive if it added/deleted the entry.
982 */
983int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
984{
985 if (!on)
986 return fasync_remove_entry(filp, fapp);
987 return fasync_add_entry(fd, filp, fapp);
988}
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990EXPORT_SYMBOL(fasync_helper);
991
Eric Dumazet989a2972010-04-14 09:55:35 +0000992/*
993 * rcu_read_lock() is held
994 */
995static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
997 while (fa) {
Eric Dumazet989a2972010-04-14 09:55:35 +0000998 struct fown_struct *fown;
Andrew Mortonf4985dc2010-06-29 15:05:42 -0700999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (fa->magic != FASYNC_MAGIC) {
1001 printk(KERN_ERR "kill_fasync: bad magic number in "
1002 "fasync_struct!\n");
1003 return;
1004 }
Kirill Tkhai7a107c02018-04-05 14:58:06 +03001005 read_lock(&fa->fa_lock);
Eric Dumazet989a2972010-04-14 09:55:35 +00001006 if (fa->fa_file) {
1007 fown = &fa->fa_file->f_owner;
1008 /* Don't send SIGURG to processes which have not set a
1009 queued signum: SIGURG has its own default signalling
1010 mechanism. */
1011 if (!(sig == SIGURG && fown->signum == 0))
1012 send_sigio(fown, fa->fa_fd, band);
1013 }
Kirill Tkhai7a107c02018-04-05 14:58:06 +03001014 read_unlock(&fa->fa_lock);
Eric Dumazet989a2972010-04-14 09:55:35 +00001015 fa = rcu_dereference(fa->fa_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017}
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019void kill_fasync(struct fasync_struct **fp, int sig, int band)
1020{
1021 /* First a quick test without locking: usually
1022 * the list is empty.
1023 */
1024 if (*fp) {
Eric Dumazet989a2972010-04-14 09:55:35 +00001025 rcu_read_lock();
1026 kill_fasync_rcu(rcu_dereference(*fp), sig, band);
1027 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029}
1030EXPORT_SYMBOL(kill_fasync);
1031
Wu Fengguang454eedb2010-08-10 18:01:29 -07001032static int __init fcntl_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
James Bottomley3ab04d52010-09-09 16:38:12 -07001034 /*
1035 * Please add new bits here to ensure allocation uniqueness.
1036 * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
1037 * is defined as O_NONBLOCK on some platforms and not on others.
1038 */
Christoph Hellwig80f18372017-04-27 09:42:24 +02001039 BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ !=
1040 HWEIGHT32(
1041 (VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
1042 __FMODE_EXEC | __FMODE_NONOTIFY));
Wu Fengguang454eedb2010-08-10 18:01:29 -07001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 fasync_cache = kmem_cache_create("fasync_cache",
Paul Mundt20c2df82007-07-20 10:11:58 +09001045 sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return 0;
1047}
1048
Wu Fengguang454eedb2010-08-10 18:01:29 -07001049module_init(fcntl_init)