Blame view

fs/fcntl.c 23.4 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
7
8
9
10
  /*
   *  linux/fs/fcntl.c
   *
   *  Copyright (C) 1991, 1992  Linus Torvalds
   */
  
  #include <linux/syscalls.h>
  #include <linux/init.h>
  #include <linux/mm.h>
299300258   Ingo Molnar   sched/headers: Pr...
11
  #include <linux/sched/task.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
12
13
  #include <linux/fs.h>
  #include <linux/file.h>
9f3acc314   Al Viro   [PATCH] split lin...
14
  #include <linux/fdtable.h>
16f7e0fe2   Randy Dunlap   [PATCH] capable/c...
15
  #include <linux/capability.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
16
  #include <linux/dnotify.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
18
  #include <linux/slab.h>
  #include <linux/module.h>
35f3d14db   Jens Axboe   pipe: add support...
19
  #include <linux/pipe_fs_i.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
21
  #include <linux/security.h>
  #include <linux/ptrace.h>
7ed20e1ad   Jesper Juhl   [PATCH] convert t...
22
  #include <linux/signal.h>
ab2af1f50   Dipankar Sarma   [PATCH] files: fi...
23
  #include <linux/rcupdate.h>
b488893a3   Pavel Emelyanov   pid namespaces: c...
24
  #include <linux/pid_namespace.h>
1d151c337   Cyrill Gorcunov   c/r: fcntl: add F...
25
  #include <linux/user_namespace.h>
5d752600a   Mike Kravetz   mm: restructure m...
26
  #include <linux/memfd.h>
80f0cce6a   Al Viro   fcntl: move compa...
27
  #include <linux/compat.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28

cfe39442a   Al Viro   use linux/poll.h ...
29
  #include <linux/poll.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
30
  #include <asm/siginfo.h>
7c0f6ba68   Linus Torvalds   Replace <asm/uacc...
31
  #include <linux/uaccess.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32

76398425b   Jonathan Corbet   Move FASYNC bit h...
33
  #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
35
36
  
  static int setfl(int fd, struct file * filp, unsigned long arg)
  {
496ad9aa8   Al Viro   new helper: file_...
37
  	struct inode * inode = file_inode(filp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38
  	int error = 0;
7d95c8f27   dean gaudet   [PATCH] fcntl F_S...
39
40
41
42
43
  	/*
  	 * O_APPEND cannot be cleared if the file is marked as append-only
  	 * and the file is open for write.
  	 */
  	if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
44
45
46
47
  		return -EPERM;
  
  	/* O_NOATIME can only be set by the owner or superuser */
  	if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
2e1496707   Serge E. Hallyn   userns: rename is...
48
  		if (!inode_owner_or_capable(inode))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
50
51
52
53
54
  			return -EPERM;
  
  	/* required for strict SunOS emulation */
  	if (O_NONBLOCK != O_NDELAY)
  	       if (arg & O_NDELAY)
  		   arg |= O_NONBLOCK;
0dbf5f206   Stanislav Kinsburskiy   fcntl: allow to s...
55
  	/* Pipe packetized mode is controlled by O_DIRECT flag */
450630975   Al Viro   don't open-code f...
56
  	if (!S_ISFIFO(inode->i_mode) && (arg & O_DIRECT)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
57
58
59
60
  		if (!filp->f_mapping || !filp->f_mapping->a_ops ||
  			!filp->f_mapping->a_ops->direct_IO)
  				return -EINVAL;
  	}
72c2d5319   Al Viro   file->f_op is nev...
61
  	if (filp->f_op->check_flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
63
64
  		error = filp->f_op->check_flags(arg);
  	if (error)
  		return error;
218d11a8b   Jonathan Corbet   Fix a race condit...
65
  	/*
76398425b   Jonathan Corbet   Move FASYNC bit h...
66
  	 * ->fasync() is responsible for setting the FASYNC bit.
218d11a8b   Jonathan Corbet   Fix a race condit...
67
  	 */
72c2d5319   Al Viro   file->f_op is nev...
68
  	if (((arg ^ filp->f_flags) & FASYNC) && filp->f_op->fasync) {
76398425b   Jonathan Corbet   Move FASYNC bit h...
69
70
71
  		error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
  		if (error < 0)
  			goto out;
60aa49243   Jonathan Corbet   Rationalize fasyn...
72
73
  		if (error > 0)
  			error = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
74
  	}
db1dd4d37   Jonathan Corbet   Use f_lock to pro...
75
  	spin_lock(&filp->f_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
76
  	filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
db1dd4d37   Jonathan Corbet   Use f_lock to pro...
77
  	spin_unlock(&filp->f_lock);
76398425b   Jonathan Corbet   Move FASYNC bit h...
78

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
79
   out:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
80
81
  	return error;
  }
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
82
  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
2f38d70fb   Oleg Nesterov   shift current_cre...
83
                       int force)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
84
  {
80e1e8239   Linus Torvalds   Fix race in tty_f...
85
  	write_lock_irq(&filp->f_owner.lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
  	if (force || !filp->f_owner.pid) {
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
87
88
89
  		put_pid(filp->f_owner.pid);
  		filp->f_owner.pid = get_pid(pid);
  		filp->f_owner.pid_type = type;
2f38d70fb   Oleg Nesterov   shift current_cre...
90
91
92
93
94
95
  
  		if (pid) {
  			const struct cred *cred = current_cred();
  			filp->f_owner.uid = cred->uid;
  			filp->f_owner.euid = cred->euid;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
96
  	}
80e1e8239   Linus Torvalds   Fix race in tty_f...
97
  	write_unlock_irq(&filp->f_owner.lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98
  }
e0b93eddf   Jeff Layton   security: make se...
99
  void __f_setown(struct file *filp, struct pid *pid, enum pid_type type,
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
100
  		int force)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101
  {
e0b93eddf   Jeff Layton   security: make se...
102
  	security_file_set_fowner(filp);
2f38d70fb   Oleg Nesterov   shift current_cre...
103
  	f_modown(filp, pid, type, force);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
  }
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
105
  EXPORT_SYMBOL(__f_setown);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106

393cc3f51   Jiri Slaby   fs/fcntl: f_setow...
107
  int f_setown(struct file *filp, unsigned long arg, int force)
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
108
109
  {
  	enum pid_type type;
f73127356   Jeff Layton   fs/fcntl: return ...
110
111
  	struct pid *pid = NULL;
  	int who = arg, ret = 0;
019191342   Eric W. Biederman   signal: Use PIDTY...
112
  	type = PIDTYPE_TGID;
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
113
  	if (who < 0) {
fc3dc6747   Jiri Slaby   fs/fcntl: f_setow...
114
115
116
  		/* avoid overflow below */
  		if (who == INT_MIN)
  			return -EINVAL;
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
117
118
119
  		type = PIDTYPE_PGID;
  		who = -who;
  	}
f73127356   Jeff Layton   fs/fcntl: return ...
120

609d7fa95   Eric W. Biederman   [PATCH] file: mod...
121
  	rcu_read_lock();
f73127356   Jeff Layton   fs/fcntl: return ...
122
123
124
125
126
127
128
129
  	if (who) {
  		pid = find_vpid(who);
  		if (!pid)
  			ret = -ESRCH;
  	}
  
  	if (!ret)
  		__f_setown(filp, pid, type, force);
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
130
  	rcu_read_unlock();
393cc3f51   Jiri Slaby   fs/fcntl: f_setow...
131

f73127356   Jeff Layton   fs/fcntl: return ...
132
  	return ret;
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
133
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
134
135
136
137
  EXPORT_SYMBOL(f_setown);
  
  void f_delown(struct file *filp)
  {
019191342   Eric W. Biederman   signal: Use PIDTY...
138
  	f_modown(filp, NULL, PIDTYPE_TGID, 1);
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
139
140
141
142
143
  }
  
  pid_t f_getown(struct file *filp)
  {
  	pid_t pid;
43fa1adb9   Eric W. Biederman   [PATCH] file: Add...
144
  	read_lock(&filp->f_owner.lock);
6c5f3e7b4   Pavel Emelyanov   Pidns: make full ...
145
  	pid = pid_vnr(filp->f_owner.pid);
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
146
147
  	if (filp->f_owner.pid_type == PIDTYPE_PGID)
  		pid = -pid;
43fa1adb9   Eric W. Biederman   [PATCH] file: Add...
148
  	read_unlock(&filp->f_owner.lock);
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
149
  	return pid;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
150
  }
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
151
152
  static int f_setown_ex(struct file *filp, unsigned long arg)
  {
63784dd02   Al Viro   fcntl: fix misann...
153
  	struct f_owner_ex __user *owner_p = (void __user *)arg;
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
154
155
156
157
158
159
160
  	struct f_owner_ex owner;
  	struct pid *pid;
  	int type;
  	int ret;
  
  	ret = copy_from_user(&owner, owner_p, sizeof(owner));
  	if (ret)
5b54470da   Dan Carpenter   fcntl: return -EF...
161
  		return -EFAULT;
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
162
163
164
  
  	switch (owner.type) {
  	case F_OWNER_TID:
019191342   Eric W. Biederman   signal: Use PIDTY...
165
  		type = PIDTYPE_PID;
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
166
167
168
  		break;
  
  	case F_OWNER_PID:
019191342   Eric W. Biederman   signal: Use PIDTY...
169
  		type = PIDTYPE_TGID;
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
170
  		break;
978b4053a   Peter Zijlstra   fcntl: rename F_O...
171
  	case F_OWNER_PGRP:
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
172
173
174
175
176
177
178
179
180
181
182
183
  		type = PIDTYPE_PGID;
  		break;
  
  	default:
  		return -EINVAL;
  	}
  
  	rcu_read_lock();
  	pid = find_vpid(owner.pid);
  	if (owner.pid && !pid)
  		ret = -ESRCH;
  	else
e0b93eddf   Jeff Layton   security: make se...
184
  		 __f_setown(filp, pid, type, 1);
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
185
186
187
188
189
190
191
  	rcu_read_unlock();
  
  	return ret;
  }
  
  static int f_getown_ex(struct file *filp, unsigned long arg)
  {
63784dd02   Al Viro   fcntl: fix misann...
192
  	struct f_owner_ex __user *owner_p = (void __user *)arg;
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
193
194
195
196
197
198
  	struct f_owner_ex owner;
  	int ret = 0;
  
  	read_lock(&filp->f_owner.lock);
  	owner.pid = pid_vnr(filp->f_owner.pid);
  	switch (filp->f_owner.pid_type) {
019191342   Eric W. Biederman   signal: Use PIDTY...
199
  	case PIDTYPE_PID:
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
200
201
  		owner.type = F_OWNER_TID;
  		break;
019191342   Eric W. Biederman   signal: Use PIDTY...
202
  	case PIDTYPE_TGID:
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
203
204
205
206
  		owner.type = F_OWNER_PID;
  		break;
  
  	case PIDTYPE_PGID:
978b4053a   Peter Zijlstra   fcntl: rename F_O...
207
  		owner.type = F_OWNER_PGRP;
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
208
209
210
211
212
213
214
215
  		break;
  
  	default:
  		WARN_ON(1);
  		ret = -EINVAL;
  		break;
  	}
  	read_unlock(&filp->f_owner.lock);
5b54470da   Dan Carpenter   fcntl: return -EF...
216
  	if (!ret) {
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
217
  		ret = copy_to_user(owner_p, &owner, sizeof(owner));
5b54470da   Dan Carpenter   fcntl: return -EF...
218
219
220
  		if (ret)
  			ret = -EFAULT;
  	}
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
221
222
  	return ret;
  }
1d151c337   Cyrill Gorcunov   c/r: fcntl: add F...
223
224
225
226
  #ifdef CONFIG_CHECKPOINT_RESTORE
  static int f_getowner_uids(struct file *filp, unsigned long arg)
  {
  	struct user_namespace *user_ns = current_user_ns();
63784dd02   Al Viro   fcntl: fix misann...
227
  	uid_t __user *dst = (void __user *)arg;
1d151c337   Cyrill Gorcunov   c/r: fcntl: add F...
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
  	uid_t src[2];
  	int err;
  
  	read_lock(&filp->f_owner.lock);
  	src[0] = from_kuid(user_ns, filp->f_owner.uid);
  	src[1] = from_kuid(user_ns, filp->f_owner.euid);
  	read_unlock(&filp->f_owner.lock);
  
  	err  = put_user(src[0], &dst[0]);
  	err |= put_user(src[1], &dst[1]);
  
  	return err;
  }
  #else
  static int f_getowner_uids(struct file *filp, unsigned long arg)
  {
  	return -EINVAL;
  }
  #endif
c75b1d942   Jens Axboe   fs: add fcntl() i...
247
248
249
  static bool rw_hint_valid(enum rw_hint hint)
  {
  	switch (hint) {
9a7f12edf   Eugene Syromiatnikov   fcntl: fix typo i...
250
  	case RWH_WRITE_LIFE_NOT_SET:
c75b1d942   Jens Axboe   fs: add fcntl() i...
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
  	case RWH_WRITE_LIFE_NONE:
  	case RWH_WRITE_LIFE_SHORT:
  	case RWH_WRITE_LIFE_MEDIUM:
  	case RWH_WRITE_LIFE_LONG:
  	case RWH_WRITE_LIFE_EXTREME:
  		return true;
  	default:
  		return false;
  	}
  }
  
  static long fcntl_rw_hint(struct file *file, unsigned int cmd,
  			  unsigned long arg)
  {
  	struct inode *inode = file_inode(file);
e20032770   Ben Dooks   fs/fnctl: fix mis...
266
  	u64 __user *argp = (u64 __user *)arg;
c75b1d942   Jens Axboe   fs: add fcntl() i...
267
  	enum rw_hint hint;
5657cb079   Jens Axboe   fs/fcntl: use cop...
268
  	u64 h;
c75b1d942   Jens Axboe   fs: add fcntl() i...
269
270
271
  
  	switch (cmd) {
  	case F_GET_FILE_RW_HINT:
5657cb079   Jens Axboe   fs/fcntl: use cop...
272
273
  		h = file_write_hint(file);
  		if (copy_to_user(argp, &h, sizeof(*argp)))
c75b1d942   Jens Axboe   fs: add fcntl() i...
274
275
276
  			return -EFAULT;
  		return 0;
  	case F_SET_FILE_RW_HINT:
5657cb079   Jens Axboe   fs/fcntl: use cop...
277
  		if (copy_from_user(&h, argp, sizeof(h)))
c75b1d942   Jens Axboe   fs: add fcntl() i...
278
  			return -EFAULT;
5657cb079   Jens Axboe   fs/fcntl: use cop...
279
  		hint = (enum rw_hint) h;
c75b1d942   Jens Axboe   fs: add fcntl() i...
280
281
282
283
284
285
286
287
  		if (!rw_hint_valid(hint))
  			return -EINVAL;
  
  		spin_lock(&file->f_lock);
  		file->f_write_hint = hint;
  		spin_unlock(&file->f_lock);
  		return 0;
  	case F_GET_RW_HINT:
5657cb079   Jens Axboe   fs/fcntl: use cop...
288
289
  		h = inode->i_write_hint;
  		if (copy_to_user(argp, &h, sizeof(*argp)))
c75b1d942   Jens Axboe   fs: add fcntl() i...
290
291
292
  			return -EFAULT;
  		return 0;
  	case F_SET_RW_HINT:
5657cb079   Jens Axboe   fs/fcntl: use cop...
293
  		if (copy_from_user(&h, argp, sizeof(h)))
c75b1d942   Jens Axboe   fs: add fcntl() i...
294
  			return -EFAULT;
5657cb079   Jens Axboe   fs/fcntl: use cop...
295
  		hint = (enum rw_hint) h;
c75b1d942   Jens Axboe   fs: add fcntl() i...
296
297
298
299
300
301
302
303
304
305
306
  		if (!rw_hint_valid(hint))
  			return -EINVAL;
  
  		inode_lock(inode);
  		inode->i_write_hint = hint;
  		inode_unlock(inode);
  		return 0;
  	default:
  		return -EINVAL;
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
307
308
309
  static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
  		struct file *filp)
  {
a75d30c77   Christoph Hellwig   fs/locks: pass ke...
310
311
  	void __user *argp = (void __user *)arg;
  	struct flock flock;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
312
313
314
315
  	long err = -EINVAL;
  
  	switch (cmd) {
  	case F_DUPFD:
fe17f22d7   Al Viro   take purely descr...
316
317
  		err = f_dupfd(arg, filp, 0);
  		break;
22d2b35b2   Ulrich Drepper   F_DUPFD_CLOEXEC i...
318
  	case F_DUPFD_CLOEXEC:
121977187   Al Viro   Fix F_DUPFD_CLOEX...
319
  		err = f_dupfd(arg, filp, O_CLOEXEC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
320
321
322
323
324
325
326
327
328
329
330
331
332
333
  		break;
  	case F_GETFD:
  		err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
  		break;
  	case F_SETFD:
  		err = 0;
  		set_close_on_exec(fd, arg & FD_CLOEXEC);
  		break;
  	case F_GETFL:
  		err = filp->f_flags;
  		break;
  	case F_SETFL:
  		err = setfl(fd, filp, arg);
  		break;
5d50ffd7c   Jeff Layton   locks: add new fc...
334
335
  #if BITS_PER_LONG != 32
  	/* 32-bit arches must use fcntl64() */
0d3f7a2dd   Jeff Layton   locks: rename fil...
336
  	case F_OFD_GETLK:
5d50ffd7c   Jeff Layton   locks: add new fc...
337
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
338
  	case F_GETLK:
a75d30c77   Christoph Hellwig   fs/locks: pass ke...
339
340
341
342
343
  		if (copy_from_user(&flock, argp, sizeof(flock)))
  			return -EFAULT;
  		err = fcntl_getlk(filp, cmd, &flock);
  		if (!err && copy_to_user(argp, &flock, sizeof(flock)))
  			return -EFAULT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
344
  		break;
5d50ffd7c   Jeff Layton   locks: add new fc...
345
346
  #if BITS_PER_LONG != 32
  	/* 32-bit arches must use fcntl64() */
0d3f7a2dd   Jeff Layton   locks: rename fil...
347
348
  	case F_OFD_SETLK:
  	case F_OFD_SETLKW:
5d50ffd7c   Jeff Layton   locks: add new fc...
349
  #endif
df561f668   Gustavo A. R. Silva   treewide: Use fal...
350
  		fallthrough;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
351
352
  	case F_SETLK:
  	case F_SETLKW:
a75d30c77   Christoph Hellwig   fs/locks: pass ke...
353
354
355
  		if (copy_from_user(&flock, argp, sizeof(flock)))
  			return -EFAULT;
  		err = fcntl_setlk(fd, filp, cmd, &flock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
356
357
358
359
360
361
362
363
364
  		break;
  	case F_GETOWN:
  		/*
  		 * XXX If f_owner is a process group, the
  		 * negative return value will get converted
  		 * into an error.  Oops.  If we keep the
  		 * current syscall conventions, the only way
  		 * to fix this will be in libc.
  		 */
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
365
  		err = f_getown(filp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
366
367
368
  		force_successful_syscall_return();
  		break;
  	case F_SETOWN:
393cc3f51   Jiri Slaby   fs/fcntl: f_setow...
369
  		err = f_setown(filp, arg, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
370
  		break;
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
371
372
373
374
375
376
  	case F_GETOWN_EX:
  		err = f_getown_ex(filp, arg);
  		break;
  	case F_SETOWN_EX:
  		err = f_setown_ex(filp, arg);
  		break;
1d151c337   Cyrill Gorcunov   c/r: fcntl: add F...
377
378
379
  	case F_GETOWNER_UIDS:
  		err = f_getowner_uids(filp, arg);
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
380
381
382
383
384
  	case F_GETSIG:
  		err = filp->f_owner.signum;
  		break;
  	case F_SETSIG:
  		/* arg == 0 restores default behaviour. */
7ed20e1ad   Jesper Juhl   [PATCH] convert t...
385
  		if (!valid_signal(arg)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
386
387
388
389
390
391
392
393
394
395
396
397
398
399
  			break;
  		}
  		err = 0;
  		filp->f_owner.signum = arg;
  		break;
  	case F_GETLEASE:
  		err = fcntl_getlease(filp);
  		break;
  	case F_SETLEASE:
  		err = fcntl_setlease(fd, filp, arg);
  		break;
  	case F_NOTIFY:
  		err = fcntl_dirnotify(fd, filp, arg);
  		break;
35f3d14db   Jens Axboe   pipe: add support...
400
401
402
403
  	case F_SETPIPE_SZ:
  	case F_GETPIPE_SZ:
  		err = pipe_fcntl(filp, cmd, arg);
  		break;
40e041a2c   David Herrmann   shm: add sealing API
404
405
  	case F_ADD_SEALS:
  	case F_GET_SEALS:
5aadc431a   Marc-André Lureau   shmem: rename fun...
406
  		err = memfd_fcntl(filp, cmd, arg);
40e041a2c   David Herrmann   shm: add sealing API
407
  		break;
c75b1d942   Jens Axboe   fs: add fcntl() i...
408
409
410
411
412
413
  	case F_GET_RW_HINT:
  	case F_SET_RW_HINT:
  	case F_GET_FILE_RW_HINT:
  	case F_SET_FILE_RW_HINT:
  		err = fcntl_rw_hint(filp, cmd, arg);
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
414
415
416
417
418
  	default:
  		break;
  	}
  	return err;
  }
1abf0c718   Al Viro   New kind of open ...
419
420
421
422
423
424
425
426
427
428
429
430
  static int check_fcntl_cmd(unsigned cmd)
  {
  	switch (cmd) {
  	case F_DUPFD:
  	case F_DUPFD_CLOEXEC:
  	case F_GETFD:
  	case F_SETFD:
  	case F_GETFL:
  		return 1;
  	}
  	return 0;
  }
a26eab240   Heiko Carstens   [CVE-2009-0029] S...
431
  SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
432
  {	
2903ff019   Al Viro   switch simple cas...
433
  	struct fd f = fdget_raw(fd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
434
  	long err = -EBADF;
2903ff019   Al Viro   switch simple cas...
435
  	if (!f.file)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
436
  		goto out;
2903ff019   Al Viro   switch simple cas...
437
  	if (unlikely(f.file->f_mode & FMODE_PATH)) {
545ec2c79   Al Viro   switch fcntl to f...
438
439
  		if (!check_fcntl_cmd(cmd))
  			goto out1;
1abf0c718   Al Viro   New kind of open ...
440
  	}
2903ff019   Al Viro   switch simple cas...
441
  	err = security_file_fcntl(f.file, cmd, arg);
545ec2c79   Al Viro   switch fcntl to f...
442
  	if (!err)
2903ff019   Al Viro   switch simple cas...
443
  		err = do_fcntl(fd, cmd, arg, f.file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
444

545ec2c79   Al Viro   switch fcntl to f...
445
  out1:
2903ff019   Al Viro   switch simple cas...
446
   	fdput(f);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
447
448
449
450
451
  out:
  	return err;
  }
  
  #if BITS_PER_LONG == 32
a26eab240   Heiko Carstens   [CVE-2009-0029] S...
452
453
  SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
  		unsigned long, arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
454
  {	
a75d30c77   Christoph Hellwig   fs/locks: pass ke...
455
  	void __user *argp = (void __user *)arg;
2903ff019   Al Viro   switch simple cas...
456
  	struct fd f = fdget_raw(fd);
a75d30c77   Christoph Hellwig   fs/locks: pass ke...
457
  	struct flock64 flock;
545ec2c79   Al Viro   switch fcntl to f...
458
  	long err = -EBADF;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
459

2903ff019   Al Viro   switch simple cas...
460
  	if (!f.file)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
461
  		goto out;
2903ff019   Al Viro   switch simple cas...
462
  	if (unlikely(f.file->f_mode & FMODE_PATH)) {
545ec2c79   Al Viro   switch fcntl to f...
463
464
  		if (!check_fcntl_cmd(cmd))
  			goto out1;
1abf0c718   Al Viro   New kind of open ...
465
  	}
2903ff019   Al Viro   switch simple cas...
466
  	err = security_file_fcntl(f.file, cmd, arg);
545ec2c79   Al Viro   switch fcntl to f...
467
468
  	if (err)
  		goto out1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
469
470
  	
  	switch (cmd) {
5d50ffd7c   Jeff Layton   locks: add new fc...
471
  	case F_GETLK64:
0d3f7a2dd   Jeff Layton   locks: rename fil...
472
  	case F_OFD_GETLK:
a75d30c77   Christoph Hellwig   fs/locks: pass ke...
473
474
475
476
477
478
  		err = -EFAULT;
  		if (copy_from_user(&flock, argp, sizeof(flock)))
  			break;
  		err = fcntl_getlk64(f.file, cmd, &flock);
  		if (!err && copy_to_user(argp, &flock, sizeof(flock)))
  			err = -EFAULT;
5d50ffd7c   Jeff Layton   locks: add new fc...
479
480
481
  		break;
  	case F_SETLK64:
  	case F_SETLKW64:
0d3f7a2dd   Jeff Layton   locks: rename fil...
482
483
  	case F_OFD_SETLK:
  	case F_OFD_SETLKW:
a75d30c77   Christoph Hellwig   fs/locks: pass ke...
484
485
486
487
  		err = -EFAULT;
  		if (copy_from_user(&flock, argp, sizeof(flock)))
  			break;
  		err = fcntl_setlk64(fd, f.file, cmd, &flock);
5d50ffd7c   Jeff Layton   locks: add new fc...
488
489
490
491
  		break;
  	default:
  		err = do_fcntl(fd, cmd, arg, f.file);
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
492
  	}
545ec2c79   Al Viro   switch fcntl to f...
493
  out1:
2903ff019   Al Viro   switch simple cas...
494
  	fdput(f);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
495
496
497
498
  out:
  	return err;
  }
  #endif
80f0cce6a   Al Viro   fcntl: move compa...
499
  #ifdef CONFIG_COMPAT
8c6657cb5   Al Viro   Switch flock copy...
500
  /* careful - don't use anywhere else */
b59eea554   Linus Torvalds   vfs: fix flock co...
501
502
503
504
505
506
507
508
  #define copy_flock_fields(dst, src)		\
  	(dst)->l_type = (src)->l_type;		\
  	(dst)->l_whence = (src)->l_whence;	\
  	(dst)->l_start = (src)->l_start;	\
  	(dst)->l_len = (src)->l_len;		\
  	(dst)->l_pid = (src)->l_pid;
  
  static int get_compat_flock(struct flock *kfl, const struct compat_flock __user *ufl)
80f0cce6a   Al Viro   fcntl: move compa...
509
  {
8c6657cb5   Al Viro   Switch flock copy...
510
511
512
  	struct compat_flock fl;
  
  	if (copy_from_user(&fl, ufl, sizeof(struct compat_flock)))
80f0cce6a   Al Viro   fcntl: move compa...
513
  		return -EFAULT;
b59eea554   Linus Torvalds   vfs: fix flock co...
514
  	copy_flock_fields(kfl, &fl);
80f0cce6a   Al Viro   fcntl: move compa...
515
516
  	return 0;
  }
b59eea554   Linus Torvalds   vfs: fix flock co...
517
  static int get_compat_flock64(struct flock *kfl, const struct compat_flock64 __user *ufl)
80f0cce6a   Al Viro   fcntl: move compa...
518
  {
8c6657cb5   Al Viro   Switch flock copy...
519
520
521
  	struct compat_flock64 fl;
  
  	if (copy_from_user(&fl, ufl, sizeof(struct compat_flock64)))
80f0cce6a   Al Viro   fcntl: move compa...
522
  		return -EFAULT;
b59eea554   Linus Torvalds   vfs: fix flock co...
523
  	copy_flock_fields(kfl, &fl);
80f0cce6a   Al Viro   fcntl: move compa...
524
525
  	return 0;
  }
b59eea554   Linus Torvalds   vfs: fix flock co...
526
  static int put_compat_flock(const struct flock *kfl, struct compat_flock __user *ufl)
80f0cce6a   Al Viro   fcntl: move compa...
527
  {
8c6657cb5   Al Viro   Switch flock copy...
528
529
530
  	struct compat_flock fl;
  
  	memset(&fl, 0, sizeof(struct compat_flock));
b59eea554   Linus Torvalds   vfs: fix flock co...
531
  	copy_flock_fields(&fl, kfl);
8c6657cb5   Al Viro   Switch flock copy...
532
  	if (copy_to_user(ufl, &fl, sizeof(struct compat_flock)))
80f0cce6a   Al Viro   fcntl: move compa...
533
534
535
  		return -EFAULT;
  	return 0;
  }
80f0cce6a   Al Viro   fcntl: move compa...
536

b59eea554   Linus Torvalds   vfs: fix flock co...
537
  static int put_compat_flock64(const struct flock *kfl, struct compat_flock64 __user *ufl)
80f0cce6a   Al Viro   fcntl: move compa...
538
  {
8c6657cb5   Al Viro   Switch flock copy...
539
  	struct compat_flock64 fl;
4d2dc2cc7   Jeff Layton   fcntl: don't cap ...
540
541
  	BUILD_BUG_ON(sizeof(kfl->l_start) > sizeof(ufl->l_start));
  	BUILD_BUG_ON(sizeof(kfl->l_len) > sizeof(ufl->l_len));
8c6657cb5   Al Viro   Switch flock copy...
542
  	memset(&fl, 0, sizeof(struct compat_flock64));
b59eea554   Linus Torvalds   vfs: fix flock co...
543
  	copy_flock_fields(&fl, kfl);
8c6657cb5   Al Viro   Switch flock copy...
544
  	if (copy_to_user(ufl, &fl, sizeof(struct compat_flock64)))
80f0cce6a   Al Viro   fcntl: move compa...
545
546
547
  		return -EFAULT;
  	return 0;
  }
8c6657cb5   Al Viro   Switch flock copy...
548
  #undef copy_flock_fields
80f0cce6a   Al Viro   fcntl: move compa...
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
  
  static unsigned int
  convert_fcntl_cmd(unsigned int cmd)
  {
  	switch (cmd) {
  	case F_GETLK64:
  		return F_GETLK;
  	case F_SETLK64:
  		return F_SETLK;
  	case F_SETLKW64:
  		return F_SETLKW;
  	}
  
  	return cmd;
  }
94073ad77   Christoph Hellwig   fs/locks: don't m...
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
  /*
   * GETLK was successful and we need to return the data, but it needs to fit in
   * the compat structure.
   * l_start shouldn't be too big, unless the original start + end is greater than
   * COMPAT_OFF_T_MAX, in which case the app was asking for trouble, so we return
   * -EOVERFLOW in that case.  l_len could be too big, in which case we just
   * truncate it, and only allow the app to see that part of the conflicting lock
   * that might make sense to it anyway
   */
  static int fixup_compat_flock(struct flock *flock)
  {
  	if (flock->l_start > COMPAT_OFF_T_MAX)
  		return -EOVERFLOW;
  	if (flock->l_len > COMPAT_OFF_T_MAX)
  		flock->l_len = COMPAT_OFF_T_MAX;
  	return 0;
  }
e02af2ff6   Dominik Brodowski   fs: add do_compat...
581
582
  static long do_compat_fcntl64(unsigned int fd, unsigned int cmd,
  			     compat_ulong_t arg)
80f0cce6a   Al Viro   fcntl: move compa...
583
  {
94073ad77   Christoph Hellwig   fs/locks: don't m...
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
  	struct fd f = fdget_raw(fd);
  	struct flock flock;
  	long err = -EBADF;
  
  	if (!f.file)
  		return err;
  
  	if (unlikely(f.file->f_mode & FMODE_PATH)) {
  		if (!check_fcntl_cmd(cmd))
  			goto out_put;
  	}
  
  	err = security_file_fcntl(f.file, cmd, arg);
  	if (err)
  		goto out_put;
80f0cce6a   Al Viro   fcntl: move compa...
599
600
601
  
  	switch (cmd) {
  	case F_GETLK:
94073ad77   Christoph Hellwig   fs/locks: don't m...
602
603
604
605
606
607
608
  		err = get_compat_flock(&flock, compat_ptr(arg));
  		if (err)
  			break;
  		err = fcntl_getlk(f.file, convert_fcntl_cmd(cmd), &flock);
  		if (err)
  			break;
  		err = fixup_compat_flock(&flock);
9280a601e   Jeff Layton   fcntl: don't leak...
609
610
  		if (!err)
  			err = put_compat_flock(&flock, compat_ptr(arg));
94073ad77   Christoph Hellwig   fs/locks: don't m...
611
612
613
614
615
616
617
  		break;
  	case F_GETLK64:
  	case F_OFD_GETLK:
  		err = get_compat_flock64(&flock, compat_ptr(arg));
  		if (err)
  			break;
  		err = fcntl_getlk(f.file, convert_fcntl_cmd(cmd), &flock);
4d2dc2cc7   Jeff Layton   fcntl: don't cap ...
618
619
  		if (!err)
  			err = put_compat_flock64(&flock, compat_ptr(arg));
94073ad77   Christoph Hellwig   fs/locks: don't m...
620
  		break;
80f0cce6a   Al Viro   fcntl: move compa...
621
622
  	case F_SETLK:
  	case F_SETLKW:
94073ad77   Christoph Hellwig   fs/locks: don't m...
623
624
  		err = get_compat_flock(&flock, compat_ptr(arg));
  		if (err)
80f0cce6a   Al Viro   fcntl: move compa...
625
  			break;
94073ad77   Christoph Hellwig   fs/locks: don't m...
626
  		err = fcntl_setlk(fd, f.file, convert_fcntl_cmd(cmd), &flock);
80f0cce6a   Al Viro   fcntl: move compa...
627
  		break;
80f0cce6a   Al Viro   fcntl: move compa...
628
629
  	case F_SETLK64:
  	case F_SETLKW64:
80f0cce6a   Al Viro   fcntl: move compa...
630
631
  	case F_OFD_SETLK:
  	case F_OFD_SETLKW:
94073ad77   Christoph Hellwig   fs/locks: don't m...
632
633
  		err = get_compat_flock64(&flock, compat_ptr(arg));
  		if (err)
80f0cce6a   Al Viro   fcntl: move compa...
634
  			break;
94073ad77   Christoph Hellwig   fs/locks: don't m...
635
  		err = fcntl_setlk(fd, f.file, convert_fcntl_cmd(cmd), &flock);
80f0cce6a   Al Viro   fcntl: move compa...
636
  		break;
80f0cce6a   Al Viro   fcntl: move compa...
637
  	default:
94073ad77   Christoph Hellwig   fs/locks: don't m...
638
  		err = do_fcntl(fd, cmd, arg, f.file);
80f0cce6a   Al Viro   fcntl: move compa...
639
640
  		break;
  	}
94073ad77   Christoph Hellwig   fs/locks: don't m...
641
642
643
  out_put:
  	fdput(f);
  	return err;
80f0cce6a   Al Viro   fcntl: move compa...
644
  }
e02af2ff6   Dominik Brodowski   fs: add do_compat...
645
646
647
648
649
  COMPAT_SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
  		       compat_ulong_t, arg)
  {
  	return do_compat_fcntl64(fd, cmd, arg);
  }
80f0cce6a   Al Viro   fcntl: move compa...
650
651
652
653
654
655
656
657
658
659
660
661
  COMPAT_SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd,
  		       compat_ulong_t, arg)
  {
  	switch (cmd) {
  	case F_GETLK64:
  	case F_SETLK64:
  	case F_SETLKW64:
  	case F_OFD_GETLK:
  	case F_OFD_SETLK:
  	case F_OFD_SETLKW:
  		return -EINVAL;
  	}
e02af2ff6   Dominik Brodowski   fs: add do_compat...
662
  	return do_compat_fcntl64(fd, cmd, arg);
80f0cce6a   Al Viro   fcntl: move compa...
663
664
  }
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
665
  /* Table to convert sigio signal codes into poll band bitmaps */
5dc533c66   Al Viro   ->si_band gets PO...
666
  static const __poll_t band_table[NSIGPOLL] = {
a9a08845e   Linus Torvalds   vfs: do bulk POLL...
667
668
669
670
671
672
  	EPOLLIN | EPOLLRDNORM,			/* POLL_IN */
  	EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND,	/* POLL_OUT */
  	EPOLLIN | EPOLLRDNORM | EPOLLMSG,		/* POLL_MSG */
  	EPOLLERR,				/* POLL_ERR */
  	EPOLLPRI | EPOLLRDBAND,			/* POLL_PRI */
  	EPOLLHUP | EPOLLERR			/* POLL_HUP */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
673
674
675
676
677
  };
  
  static inline int sigio_perm(struct task_struct *p,
                               struct fown_struct *fown, int sig)
  {
c69e8d9c0   David Howells   CRED: Use RCU to ...
678
679
680
681
682
  	const struct cred *cred;
  	int ret;
  
  	rcu_read_lock();
  	cred = __task_cred(p);
8e96e3b7b   Eric W. Biederman   userns: Use uid_e...
683
684
685
  	ret = ((uid_eq(fown->euid, GLOBAL_ROOT_UID) ||
  		uid_eq(fown->euid, cred->suid) || uid_eq(fown->euid, cred->uid) ||
  		uid_eq(fown->uid,  cred->suid) || uid_eq(fown->uid,  cred->uid)) &&
c69e8d9c0   David Howells   CRED: Use RCU to ...
686
687
688
  	       !security_file_send_sigiotask(p, fown, sig));
  	rcu_read_unlock();
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
689
690
691
  }
  
  static void send_sigio_to_task(struct task_struct *p,
8eeee4e2f   Oleg Nesterov   send_sigio_to_tas...
692
  			       struct fown_struct *fown,
9c2db0077   Eric W. Biederman   signal: Pass pid ...
693
  			       int fd, int reason, enum pid_type type)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
694
  {
8eeee4e2f   Oleg Nesterov   send_sigio_to_tas...
695
696
697
698
  	/*
  	 * F_SETSIG can change ->signum lockless in parallel, make
  	 * sure we read it once and use the same value throughout.
  	 */
6aa7de059   Mark Rutland   locking/atomics: ...
699
  	int signum = READ_ONCE(fown->signum);
8eeee4e2f   Oleg Nesterov   send_sigio_to_tas...
700
701
  
  	if (!sigio_perm(p, fown, signum))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
702
  		return;
8eeee4e2f   Oleg Nesterov   send_sigio_to_tas...
703
  	switch (signum) {
0a68ff5e2   Kees Cook   fcntl: Distribute...
704
705
  		default: {
  			kernel_siginfo_t si;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
706
707
708
709
710
711
  			/* Queue a rt signal with the appropriate fd as its
  			   value.  We use SI_SIGIO as the source, not 
  			   SI_KERNEL, since kernel signals always get 
  			   delivered even if we can't queue.  Failure to
  			   queue in this case _should_ be reported; we fall
  			   back to SIGIO in that case. --sct */
faf1f22b6   Eric W. Biederman   signal: Ensure ge...
712
  			clear_siginfo(&si);
8eeee4e2f   Oleg Nesterov   send_sigio_to_tas...
713
  			si.si_signo = signum;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
714
715
  			si.si_errno = 0;
  		        si.si_code  = reason;
d08477aa9   Eric W. Biederman   fcntl: Don't use ...
716
717
718
719
720
721
722
723
  			/*
  			 * Posix definies POLL_IN and friends to be signal
  			 * specific si_codes for SIG_POLL.  Linux extended
  			 * these si_codes to other signals in a way that is
  			 * ambiguous if other signals also have signal
  			 * specific si_codes.  In that case use SI_SIGIO instead
  			 * to remove the ambiguity.
  			 */
54640d238   Eric W. Biederman   fcntl: Don't set ...
724
  			if ((signum != SIGPOLL) && sig_specific_sicodes(signum))
d08477aa9   Eric W. Biederman   fcntl: Don't use ...
725
  				si.si_code = SI_SIGIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
726
727
728
  			/* Make sure we are called with one of the POLL_*
  			   reasons, otherwise we could leak kernel stack into
  			   userspace.  */
d08477aa9   Eric W. Biederman   fcntl: Don't use ...
729
  			BUG_ON((reason < POLL_IN) || ((reason - POLL_IN) >= NSIGPOLL));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
730
731
732
  			if (reason - POLL_IN >= NSIGPOLL)
  				si.si_band  = ~0L;
  			else
c71d227fc   Al Viro   make kernel-side ...
733
  				si.si_band = mangle_poll(band_table[reason - POLL_IN]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
734
  			si.si_fd    = fd;
40b3b0253   Eric W. Biederman   signal: Pass pid ...
735
  			if (!do_send_sig_info(signum, &si, p, type))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
736
  				break;
0a68ff5e2   Kees Cook   fcntl: Distribute...
737
  		}
df561f668   Gustavo A. R. Silva   treewide: Use fal...
738
  			fallthrough;	/* fall back on the old plain SIGIO signal */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
739
  		case 0:
40b3b0253   Eric W. Biederman   signal: Pass pid ...
740
  			do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, type);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
741
742
743
744
745
746
  	}
  }
  
  void send_sigio(struct fown_struct *fown, int fd, int band)
  {
  	struct task_struct *p;
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
747
  	enum pid_type type;
908030501   Boqun Feng   fcntl: Fix potent...
748
  	unsigned long flags;
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
749
  	struct pid *pid;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
750
  	
908030501   Boqun Feng   fcntl: Fix potent...
751
  	read_lock_irqsave(&fown->lock, flags);
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
752

609d7fa95   Eric W. Biederman   [PATCH] file: mod...
753
  	type = fown->pid_type;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
754
755
756
  	pid = fown->pid;
  	if (!pid)
  		goto out_unlock_fown;
019191342   Eric W. Biederman   signal: Use PIDTY...
757
758
759
760
  
  	if (type <= PIDTYPE_TGID) {
  		rcu_read_lock();
  		p = pid_task(pid, PIDTYPE_PID);
84fe4cc09   Eric W. Biederman   signal: Don't sen...
761
762
  		if (p)
  			send_sigio_to_task(p, fown, fd, band, type);
019191342   Eric W. Biederman   signal: Use PIDTY...
763
764
765
766
  		rcu_read_unlock();
  	} else {
  		read_lock(&tasklist_lock);
  		do_each_pid_task(pid, type, p) {
9c2db0077   Eric W. Biederman   signal: Pass pid ...
767
  			send_sigio_to_task(p, fown, fd, band, type);
019191342   Eric W. Biederman   signal: Use PIDTY...
768
769
770
  		} while_each_pid_task(pid, type, p);
  		read_unlock(&tasklist_lock);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
771
   out_unlock_fown:
908030501   Boqun Feng   fcntl: Fix potent...
772
  	read_unlock_irqrestore(&fown->lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
773
774
775
  }
  
  static void send_sigurg_to_task(struct task_struct *p,
9c2db0077   Eric W. Biederman   signal: Pass pid ...
776
  				struct fown_struct *fown, enum pid_type type)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
777
778
  {
  	if (sigio_perm(p, fown, SIGURG))
40b3b0253   Eric W. Biederman   signal: Pass pid ...
779
  		do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, type);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
780
781
782
783
784
  }
  
  int send_sigurg(struct fown_struct *fown)
  {
  	struct task_struct *p;
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
785
786
  	enum pid_type type;
  	struct pid *pid;
908030501   Boqun Feng   fcntl: Fix potent...
787
  	unsigned long flags;
609d7fa95   Eric W. Biederman   [PATCH] file: mod...
788
  	int ret = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
789
  	
908030501   Boqun Feng   fcntl: Fix potent...
790
  	read_lock_irqsave(&fown->lock, flags);
ba0a6c9f6   Peter Zijlstra   fcntl: add F_[SG]...
791

609d7fa95   Eric W. Biederman   [PATCH] file: mod...
792
  	type = fown->pid_type;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
793
794
795
796
797
  	pid = fown->pid;
  	if (!pid)
  		goto out_unlock_fown;
  
  	ret = 1;
019191342   Eric W. Biederman   signal: Use PIDTY...
798
799
800
801
  
  	if (type <= PIDTYPE_TGID) {
  		rcu_read_lock();
  		p = pid_task(pid, PIDTYPE_PID);
84fe4cc09   Eric W. Biederman   signal: Don't sen...
802
803
  		if (p)
  			send_sigurg_to_task(p, fown, type);
019191342   Eric W. Biederman   signal: Use PIDTY...
804
805
806
807
  		rcu_read_unlock();
  	} else {
  		read_lock(&tasklist_lock);
  		do_each_pid_task(pid, type, p) {
9c2db0077   Eric W. Biederman   signal: Pass pid ...
808
  			send_sigurg_to_task(p, fown, type);
019191342   Eric W. Biederman   signal: Use PIDTY...
809
810
811
  		} while_each_pid_task(pid, type, p);
  		read_unlock(&tasklist_lock);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
812
   out_unlock_fown:
908030501   Boqun Feng   fcntl: Fix potent...
813
  	read_unlock_irqrestore(&fown->lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
814
815
  	return ret;
  }
989a29792   Eric Dumazet   fasync: RCU and f...
816
  static DEFINE_SPINLOCK(fasync_lock);
e18b890bb   Christoph Lameter   [PATCH] slab: rem...
817
  static struct kmem_cache *fasync_cache __read_mostly;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
818

989a29792   Eric Dumazet   fasync: RCU and f...
819
820
821
822
823
  static void fasync_free_rcu(struct rcu_head *head)
  {
  	kmem_cache_free(fasync_cache,
  			container_of(head, struct fasync_struct, fa_rcu));
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
824
  /*
53281b6d3   Linus Torvalds   fasync: split 'fa...
825
826
827
828
829
830
831
   * Remove a fasync entry. If successfully removed, return
   * positive and clear the FASYNC flag. If no entry exists,
   * do nothing and return 0.
   *
   * NOTE! It is very important that the FASYNC flag always
   * match the state "is the filp on a fasync list".
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
832
   */
f7347ce4e   Linus Torvalds   fasync: re-organi...
833
  int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
834
835
  {
  	struct fasync_struct *fa, **fp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
836
  	int result = 0;
53281b6d3   Linus Torvalds   fasync: split 'fa...
837
  	spin_lock(&filp->f_lock);
989a29792   Eric Dumazet   fasync: RCU and f...
838
  	spin_lock(&fasync_lock);
53281b6d3   Linus Torvalds   fasync: split 'fa...
839
840
841
  	for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
  		if (fa->fa_file != filp)
  			continue;
989a29792   Eric Dumazet   fasync: RCU and f...
842

7a107c0f5   Kirill Tkhai   fasync: Fix deadl...
843
  		write_lock_irq(&fa->fa_lock);
989a29792   Eric Dumazet   fasync: RCU and f...
844
  		fa->fa_file = NULL;
7a107c0f5   Kirill Tkhai   fasync: Fix deadl...
845
  		write_unlock_irq(&fa->fa_lock);
989a29792   Eric Dumazet   fasync: RCU and f...
846

53281b6d3   Linus Torvalds   fasync: split 'fa...
847
  		*fp = fa->fa_next;
989a29792   Eric Dumazet   fasync: RCU and f...
848
  		call_rcu(&fa->fa_rcu, fasync_free_rcu);
53281b6d3   Linus Torvalds   fasync: split 'fa...
849
850
851
  		filp->f_flags &= ~FASYNC;
  		result = 1;
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
852
  	}
989a29792   Eric Dumazet   fasync: RCU and f...
853
  	spin_unlock(&fasync_lock);
53281b6d3   Linus Torvalds   fasync: split 'fa...
854
855
856
  	spin_unlock(&filp->f_lock);
  	return result;
  }
f7347ce4e   Linus Torvalds   fasync: re-organi...
857
858
859
860
  struct fasync_struct *fasync_alloc(void)
  {
  	return kmem_cache_alloc(fasync_cache, GFP_KERNEL);
  }
53281b6d3   Linus Torvalds   fasync: split 'fa...
861
  /*
f7347ce4e   Linus Torvalds   fasync: re-organi...
862
863
864
   * NOTE! This can be used only for unused fasync entries:
   * entries that actually got inserted on the fasync list
   * need to be released by rcu - see fasync_remove_entry.
53281b6d3   Linus Torvalds   fasync: split 'fa...
865
   */
f7347ce4e   Linus Torvalds   fasync: re-organi...
866
  void fasync_free(struct fasync_struct *new)
53281b6d3   Linus Torvalds   fasync: split 'fa...
867
  {
f7347ce4e   Linus Torvalds   fasync: re-organi...
868
869
  	kmem_cache_free(fasync_cache, new);
  }
53281b6d3   Linus Torvalds   fasync: split 'fa...
870

f7347ce4e   Linus Torvalds   fasync: re-organi...
871
872
873
  /*
   * Insert a new entry into the fasync list.  Return the pointer to the
   * old one if we didn't use the new one.
55f335a88   Linus Torvalds   fasync: Fix place...
874
875
876
   *
   * NOTE! It is very important that the FASYNC flag always
   * match the state "is the filp on a fasync list".
f7347ce4e   Linus Torvalds   fasync: re-organi...
877
878
879
880
   */
  struct fasync_struct *fasync_insert_entry(int fd, struct file *filp, struct fasync_struct **fapp, struct fasync_struct *new)
  {
          struct fasync_struct *fa, **fp;
4a6a44996   Jonathan Corbet   Fix a lockdep war...
881

4a6a44996   Jonathan Corbet   Fix a lockdep war...
882
  	spin_lock(&filp->f_lock);
989a29792   Eric Dumazet   fasync: RCU and f...
883
  	spin_lock(&fasync_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
884
  	for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
53281b6d3   Linus Torvalds   fasync: split 'fa...
885
886
  		if (fa->fa_file != filp)
  			continue;
989a29792   Eric Dumazet   fasync: RCU and f...
887

7a107c0f5   Kirill Tkhai   fasync: Fix deadl...
888
  		write_lock_irq(&fa->fa_lock);
53281b6d3   Linus Torvalds   fasync: split 'fa...
889
  		fa->fa_fd = fd;
7a107c0f5   Kirill Tkhai   fasync: Fix deadl...
890
  		write_unlock_irq(&fa->fa_lock);
53281b6d3   Linus Torvalds   fasync: split 'fa...
891
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
892
  	}
7a107c0f5   Kirill Tkhai   fasync: Fix deadl...
893
  	rwlock_init(&new->fa_lock);
53281b6d3   Linus Torvalds   fasync: split 'fa...
894
895
896
897
  	new->magic = FASYNC_MAGIC;
  	new->fa_file = filp;
  	new->fa_fd = fd;
  	new->fa_next = *fapp;
989a29792   Eric Dumazet   fasync: RCU and f...
898
  	rcu_assign_pointer(*fapp, new);
53281b6d3   Linus Torvalds   fasync: split 'fa...
899
  	filp->f_flags |= FASYNC;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
900
  out:
989a29792   Eric Dumazet   fasync: RCU and f...
901
  	spin_unlock(&fasync_lock);
4a6a44996   Jonathan Corbet   Fix a lockdep war...
902
  	spin_unlock(&filp->f_lock);
f7347ce4e   Linus Torvalds   fasync: re-organi...
903
904
905
906
907
908
  	return fa;
  }
  
  /*
   * Add a fasync entry. Return negative on error, positive if
   * added, and zero if did nothing but change an existing one.
f7347ce4e   Linus Torvalds   fasync: re-organi...
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
   */
  static int fasync_add_entry(int fd, struct file *filp, struct fasync_struct **fapp)
  {
  	struct fasync_struct *new;
  
  	new = fasync_alloc();
  	if (!new)
  		return -ENOMEM;
  
  	/*
  	 * fasync_insert_entry() returns the old (update) entry if
  	 * it existed.
  	 *
  	 * So free the (unused) new entry and return 0 to let the
  	 * caller know that we didn't add any new fasync entries.
  	 */
  	if (fasync_insert_entry(fd, filp, fapp, new)) {
  		fasync_free(new);
  		return 0;
  	}
  
  	return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
931
  }
53281b6d3   Linus Torvalds   fasync: split 'fa...
932
933
934
935
936
937
938
939
940
941
942
943
  /*
   * fasync_helper() is used by almost all character device drivers
   * to set up the fasync queue, and for regular files by the file
   * lease code. It returns negative on error, 0 if it did no changes
   * and positive if it added/deleted the entry.
   */
  int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
  {
  	if (!on)
  		return fasync_remove_entry(filp, fapp);
  	return fasync_add_entry(fd, filp, fapp);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
944
  EXPORT_SYMBOL(fasync_helper);
989a29792   Eric Dumazet   fasync: RCU and f...
945
946
947
948
  /*
   * rcu_read_lock() is held
   */
  static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
949
950
  {
  	while (fa) {
989a29792   Eric Dumazet   fasync: RCU and f...
951
  		struct fown_struct *fown;
f4985dc71   Andrew Morton   fs/fcntl.c:kill_f...
952

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
953
954
955
956
957
958
  		if (fa->magic != FASYNC_MAGIC) {
  			printk(KERN_ERR "kill_fasync: bad magic number in "
  			       "fasync_struct!
  ");
  			return;
  		}
7a107c0f5   Kirill Tkhai   fasync: Fix deadl...
959
  		read_lock(&fa->fa_lock);
989a29792   Eric Dumazet   fasync: RCU and f...
960
961
962
963
964
965
966
967
  		if (fa->fa_file) {
  			fown = &fa->fa_file->f_owner;
  			/* Don't send SIGURG to processes which have not set a
  			   queued signum: SIGURG has its own default signalling
  			   mechanism. */
  			if (!(sig == SIGURG && fown->signum == 0))
  				send_sigio(fown, fa->fa_fd, band);
  		}
7a107c0f5   Kirill Tkhai   fasync: Fix deadl...
968
  		read_unlock(&fa->fa_lock);
989a29792   Eric Dumazet   fasync: RCU and f...
969
  		fa = rcu_dereference(fa->fa_next);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
970
971
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
972
973
974
975
976
977
  void kill_fasync(struct fasync_struct **fp, int sig, int band)
  {
  	/* First a quick test without locking: usually
  	 * the list is empty.
  	 */
  	if (*fp) {
989a29792   Eric Dumazet   fasync: RCU and f...
978
979
980
  		rcu_read_lock();
  		kill_fasync_rcu(rcu_dereference(*fp), sig, band);
  		rcu_read_unlock();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
981
982
983
  	}
  }
  EXPORT_SYMBOL(kill_fasync);
454eedb89   Wu Fengguang   vfs: O_* bit numb...
984
  static int __init fcntl_init(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
985
  {
3ab04d5cf   James Bottomley   vfs: take O_NONBL...
986
987
988
989
990
  	/*
  	 * Please add new bits here to ensure allocation uniqueness.
  	 * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
  	 * is defined as O_NONBLOCK on some platforms and not on others.
  	 */
80f18379a   Christoph Hellwig   fs: add a VALID_O...
991
992
993
994
  	BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ !=
  		HWEIGHT32(
  			(VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
  			__FMODE_EXEC | __FMODE_NONOTIFY));
454eedb89   Wu Fengguang   vfs: O_* bit numb...
995

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
996
  	fasync_cache = kmem_cache_create("fasync_cache",
20c2df83d   Paul Mundt   mm: Remove slab d...
997
  		sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
998
999
  	return 0;
  }
454eedb89   Wu Fengguang   vfs: O_* bit numb...
1000
  module_init(fcntl_init)