]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/fcntl.c
x86/msr-index: Cleanup bit defines
[mirror_ubuntu-bionic-kernel.git] / fs / fcntl.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
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>
29930025 11#include <linux/sched/task.h>
1da177e4
LT
12#include <linux/fs.h>
13#include <linux/file.h>
9f3acc31 14#include <linux/fdtable.h>
16f7e0fe 15#include <linux/capability.h>
1da177e4 16#include <linux/dnotify.h>
1da177e4
LT
17#include <linux/slab.h>
18#include <linux/module.h>
35f3d14d 19#include <linux/pipe_fs_i.h>
1da177e4
LT
20#include <linux/security.h>
21#include <linux/ptrace.h>
7ed20e1a 22#include <linux/signal.h>
ab2af1f5 23#include <linux/rcupdate.h>
b488893a 24#include <linux/pid_namespace.h>
1d151c33 25#include <linux/user_namespace.h>
40e041a2 26#include <linux/shmem_fs.h>
80f0cce6 27#include <linux/compat.h>
1da177e4
LT
28
29#include <asm/poll.h>
30#include <asm/siginfo.h>
7c0f6ba6 31#include <linux/uaccess.h>
1da177e4 32
76398425 33#define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
1da177e4 34
c088e31d 35int setfl(int fd, struct file * filp, unsigned long arg)
1da177e4 36{
496ad9aa 37 struct inode * inode = file_inode(filp);
1da177e4
LT
38 int error = 0;
39
7d95c8f2 40 /*
41 * O_APPEND cannot be cleared if the file is marked as append-only
42 * and the file is open for write.
43 */
44 if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode))
1da177e4
LT
45 return -EPERM;
46
47 /* O_NOATIME can only be set by the owner or superuser */
48 if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
2e149670 49 if (!inode_owner_or_capable(inode))
1da177e4
LT
50 return -EPERM;
51
52 /* required for strict SunOS emulation */
53 if (O_NONBLOCK != O_NDELAY)
54 if (arg & O_NDELAY)
55 arg |= O_NONBLOCK;
56
0dbf5f20 57 /* Pipe packetized mode is controlled by O_DIRECT flag */
45063097 58 if (!S_ISFIFO(inode->i_mode) && (arg & O_DIRECT)) {
1da177e4
LT
59 if (!filp->f_mapping || !filp->f_mapping->a_ops ||
60 !filp->f_mapping->a_ops->direct_IO)
61 return -EINVAL;
62 }
63
72c2d531 64 if (filp->f_op->check_flags)
1da177e4 65 error = filp->f_op->check_flags(arg);
c088e31d
SF
66 if (!error && filp->f_op->setfl)
67 error = filp->f_op->setfl(filp, arg);
1da177e4
LT
68 if (error)
69 return error;
70
218d11a8 71 /*
76398425 72 * ->fasync() is responsible for setting the FASYNC bit.
218d11a8 73 */
72c2d531 74 if (((arg ^ filp->f_flags) & FASYNC) && filp->f_op->fasync) {
76398425
JC
75 error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
76 if (error < 0)
77 goto out;
60aa4924
JC
78 if (error > 0)
79 error = 0;
1da177e4 80 }
db1dd4d3 81 spin_lock(&filp->f_lock);
1da177e4 82 filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
db1dd4d3 83 spin_unlock(&filp->f_lock);
76398425 84
1da177e4 85 out:
1da177e4
LT
86 return error;
87}
c088e31d 88EXPORT_SYMBOL_GPL(setfl);
1da177e4 89
609d7fa9 90static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
2f38d70f 91 int force)
1da177e4 92{
80e1e823 93 write_lock_irq(&filp->f_owner.lock);
1da177e4 94 if (force || !filp->f_owner.pid) {
609d7fa9
EB
95 put_pid(filp->f_owner.pid);
96 filp->f_owner.pid = get_pid(pid);
97 filp->f_owner.pid_type = type;
2f38d70f
ON
98
99 if (pid) {
100 const struct cred *cred = current_cred();
101 filp->f_owner.uid = cred->uid;
102 filp->f_owner.euid = cred->euid;
103 }
1da177e4 104 }
80e1e823 105 write_unlock_irq(&filp->f_owner.lock);
1da177e4
LT
106}
107
e0b93edd 108void __f_setown(struct file *filp, struct pid *pid, enum pid_type type,
609d7fa9 109 int force)
1da177e4 110{
e0b93edd 111 security_file_set_fowner(filp);
2f38d70f 112 f_modown(filp, pid, type, force);
1da177e4 113}
609d7fa9 114EXPORT_SYMBOL(__f_setown);
1da177e4 115
393cc3f5 116int f_setown(struct file *filp, unsigned long arg, int force)
609d7fa9
EB
117{
118 enum pid_type type;
f7312735
JL
119 struct pid *pid = NULL;
120 int who = arg, ret = 0;
121
609d7fa9
EB
122 type = PIDTYPE_PID;
123 if (who < 0) {
fc3dc674
JS
124 /* avoid overflow below */
125 if (who == INT_MIN)
126 return -EINVAL;
127
609d7fa9
EB
128 type = PIDTYPE_PGID;
129 who = -who;
130 }
f7312735 131
609d7fa9 132 rcu_read_lock();
f7312735
JL
133 if (who) {
134 pid = find_vpid(who);
135 if (!pid)
136 ret = -ESRCH;
137 }
138
139 if (!ret)
140 __f_setown(filp, pid, type, force);
609d7fa9 141 rcu_read_unlock();
393cc3f5 142
f7312735 143 return ret;
609d7fa9 144}
1da177e4
LT
145EXPORT_SYMBOL(f_setown);
146
147void f_delown(struct file *filp)
148{
2f38d70f 149 f_modown(filp, NULL, PIDTYPE_PID, 1);
609d7fa9
EB
150}
151
152pid_t f_getown(struct file *filp)
153{
154 pid_t pid;
43fa1adb 155 read_lock(&filp->f_owner.lock);
6c5f3e7b 156 pid = pid_vnr(filp->f_owner.pid);
609d7fa9
EB
157 if (filp->f_owner.pid_type == PIDTYPE_PGID)
158 pid = -pid;
43fa1adb 159 read_unlock(&filp->f_owner.lock);
609d7fa9 160 return pid;
1da177e4
LT
161}
162
ba0a6c9f
PZ
163static int f_setown_ex(struct file *filp, unsigned long arg)
164{
63784dd0 165 struct f_owner_ex __user *owner_p = (void __user *)arg;
ba0a6c9f
PZ
166 struct f_owner_ex owner;
167 struct pid *pid;
168 int type;
169 int ret;
170
171 ret = copy_from_user(&owner, owner_p, sizeof(owner));
172 if (ret)
5b54470d 173 return -EFAULT;
ba0a6c9f
PZ
174
175 switch (owner.type) {
176 case F_OWNER_TID:
177 type = PIDTYPE_MAX;
178 break;
179
180 case F_OWNER_PID:
181 type = PIDTYPE_PID;
182 break;
183
978b4053 184 case F_OWNER_PGRP:
ba0a6c9f
PZ
185 type = PIDTYPE_PGID;
186 break;
187
188 default:
189 return -EINVAL;
190 }
191
192 rcu_read_lock();
193 pid = find_vpid(owner.pid);
194 if (owner.pid && !pid)
195 ret = -ESRCH;
196 else
e0b93edd 197 __f_setown(filp, pid, type, 1);
ba0a6c9f
PZ
198 rcu_read_unlock();
199
200 return ret;
201}
202
203static int f_getown_ex(struct file *filp, unsigned long arg)
204{
63784dd0 205 struct f_owner_ex __user *owner_p = (void __user *)arg;
ba0a6c9f
PZ
206 struct f_owner_ex owner;
207 int ret = 0;
208
209 read_lock(&filp->f_owner.lock);
210 owner.pid = pid_vnr(filp->f_owner.pid);
211 switch (filp->f_owner.pid_type) {
212 case PIDTYPE_MAX:
213 owner.type = F_OWNER_TID;
214 break;
215
216 case PIDTYPE_PID:
217 owner.type = F_OWNER_PID;
218 break;
219
220 case PIDTYPE_PGID:
978b4053 221 owner.type = F_OWNER_PGRP;
ba0a6c9f
PZ
222 break;
223
224 default:
225 WARN_ON(1);
226 ret = -EINVAL;
227 break;
228 }
229 read_unlock(&filp->f_owner.lock);
230
5b54470d 231 if (!ret) {
ba0a6c9f 232 ret = copy_to_user(owner_p, &owner, sizeof(owner));
5b54470d
DC
233 if (ret)
234 ret = -EFAULT;
235 }
ba0a6c9f
PZ
236 return ret;
237}
238
1d151c33
CG
239#ifdef CONFIG_CHECKPOINT_RESTORE
240static int f_getowner_uids(struct file *filp, unsigned long arg)
241{
242 struct user_namespace *user_ns = current_user_ns();
63784dd0 243 uid_t __user *dst = (void __user *)arg;
1d151c33
CG
244 uid_t src[2];
245 int err;
246
247 read_lock(&filp->f_owner.lock);
248 src[0] = from_kuid(user_ns, filp->f_owner.uid);
249 src[1] = from_kuid(user_ns, filp->f_owner.euid);
250 read_unlock(&filp->f_owner.lock);
251
252 err = put_user(src[0], &dst[0]);
253 err |= put_user(src[1], &dst[1]);
254
255 return err;
256}
257#else
258static int f_getowner_uids(struct file *filp, unsigned long arg)
259{
260 return -EINVAL;
261}
262#endif
263
c75b1d94
JA
264static bool rw_hint_valid(enum rw_hint hint)
265{
266 switch (hint) {
267 case RWF_WRITE_LIFE_NOT_SET:
268 case RWH_WRITE_LIFE_NONE:
269 case RWH_WRITE_LIFE_SHORT:
270 case RWH_WRITE_LIFE_MEDIUM:
271 case RWH_WRITE_LIFE_LONG:
272 case RWH_WRITE_LIFE_EXTREME:
273 return true;
274 default:
275 return false;
276 }
277}
278
279static long fcntl_rw_hint(struct file *file, unsigned int cmd,
280 unsigned long arg)
281{
282 struct inode *inode = file_inode(file);
283 u64 *argp = (u64 __user *)arg;
284 enum rw_hint hint;
5657cb07 285 u64 h;
c75b1d94
JA
286
287 switch (cmd) {
288 case F_GET_FILE_RW_HINT:
5657cb07
JA
289 h = file_write_hint(file);
290 if (copy_to_user(argp, &h, sizeof(*argp)))
c75b1d94
JA
291 return -EFAULT;
292 return 0;
293 case F_SET_FILE_RW_HINT:
5657cb07 294 if (copy_from_user(&h, argp, sizeof(h)))
c75b1d94 295 return -EFAULT;
5657cb07 296 hint = (enum rw_hint) h;
c75b1d94
JA
297 if (!rw_hint_valid(hint))
298 return -EINVAL;
299
300 spin_lock(&file->f_lock);
301 file->f_write_hint = hint;
302 spin_unlock(&file->f_lock);
303 return 0;
304 case F_GET_RW_HINT:
5657cb07
JA
305 h = inode->i_write_hint;
306 if (copy_to_user(argp, &h, sizeof(*argp)))
c75b1d94
JA
307 return -EFAULT;
308 return 0;
309 case F_SET_RW_HINT:
5657cb07 310 if (copy_from_user(&h, argp, sizeof(h)))
c75b1d94 311 return -EFAULT;
5657cb07 312 hint = (enum rw_hint) h;
c75b1d94
JA
313 if (!rw_hint_valid(hint))
314 return -EINVAL;
315
316 inode_lock(inode);
317 inode->i_write_hint = hint;
318 inode_unlock(inode);
319 return 0;
320 default:
321 return -EINVAL;
322 }
323}
324
1da177e4
LT
325static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
326 struct file *filp)
327{
a75d30c7
CH
328 void __user *argp = (void __user *)arg;
329 struct flock flock;
1da177e4
LT
330 long err = -EINVAL;
331
332 switch (cmd) {
333 case F_DUPFD:
fe17f22d
AV
334 err = f_dupfd(arg, filp, 0);
335 break;
22d2b35b 336 case F_DUPFD_CLOEXEC:
12197718 337 err = f_dupfd(arg, filp, O_CLOEXEC);
1da177e4
LT
338 break;
339 case F_GETFD:
340 err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
341 break;
342 case F_SETFD:
343 err = 0;
344 set_close_on_exec(fd, arg & FD_CLOEXEC);
345 break;
346 case F_GETFL:
347 err = filp->f_flags;
348 break;
349 case F_SETFL:
350 err = setfl(fd, filp, arg);
351 break;
5d50ffd7
JL
352#if BITS_PER_LONG != 32
353 /* 32-bit arches must use fcntl64() */
0d3f7a2d 354 case F_OFD_GETLK:
5d50ffd7 355#endif
1da177e4 356 case F_GETLK:
a75d30c7
CH
357 if (copy_from_user(&flock, argp, sizeof(flock)))
358 return -EFAULT;
359 err = fcntl_getlk(filp, cmd, &flock);
360 if (!err && copy_to_user(argp, &flock, sizeof(flock)))
361 return -EFAULT;
1da177e4 362 break;
5d50ffd7
JL
363#if BITS_PER_LONG != 32
364 /* 32-bit arches must use fcntl64() */
0d3f7a2d
JL
365 case F_OFD_SETLK:
366 case F_OFD_SETLKW:
5d50ffd7
JL
367#endif
368 /* Fallthrough */
1da177e4
LT
369 case F_SETLK:
370 case F_SETLKW:
a75d30c7
CH
371 if (copy_from_user(&flock, argp, sizeof(flock)))
372 return -EFAULT;
373 err = fcntl_setlk(fd, filp, cmd, &flock);
1da177e4
LT
374 break;
375 case F_GETOWN:
376 /*
377 * XXX If f_owner is a process group, the
378 * negative return value will get converted
379 * into an error. Oops. If we keep the
380 * current syscall conventions, the only way
381 * to fix this will be in libc.
382 */
609d7fa9 383 err = f_getown(filp);
1da177e4
LT
384 force_successful_syscall_return();
385 break;
386 case F_SETOWN:
393cc3f5 387 err = f_setown(filp, arg, 1);
1da177e4 388 break;
ba0a6c9f
PZ
389 case F_GETOWN_EX:
390 err = f_getown_ex(filp, arg);
391 break;
392 case F_SETOWN_EX:
393 err = f_setown_ex(filp, arg);
394 break;
1d151c33
CG
395 case F_GETOWNER_UIDS:
396 err = f_getowner_uids(filp, arg);
397 break;
1da177e4
LT
398 case F_GETSIG:
399 err = filp->f_owner.signum;
400 break;
401 case F_SETSIG:
402 /* arg == 0 restores default behaviour. */
7ed20e1a 403 if (!valid_signal(arg)) {
1da177e4
LT
404 break;
405 }
406 err = 0;
407 filp->f_owner.signum = arg;
408 break;
409 case F_GETLEASE:
410 err = fcntl_getlease(filp);
411 break;
412 case F_SETLEASE:
413 err = fcntl_setlease(fd, filp, arg);
414 break;
415 case F_NOTIFY:
416 err = fcntl_dirnotify(fd, filp, arg);
417 break;
35f3d14d
JA
418 case F_SETPIPE_SZ:
419 case F_GETPIPE_SZ:
420 err = pipe_fcntl(filp, cmd, arg);
421 break;
40e041a2
DH
422 case F_ADD_SEALS:
423 case F_GET_SEALS:
424 err = shmem_fcntl(filp, cmd, arg);
425 break;
c75b1d94
JA
426 case F_GET_RW_HINT:
427 case F_SET_RW_HINT:
428 case F_GET_FILE_RW_HINT:
429 case F_SET_FILE_RW_HINT:
430 err = fcntl_rw_hint(filp, cmd, arg);
431 break;
1da177e4
LT
432 default:
433 break;
434 }
435 return err;
436}
437
1abf0c71
AV
438static int check_fcntl_cmd(unsigned cmd)
439{
440 switch (cmd) {
441 case F_DUPFD:
442 case F_DUPFD_CLOEXEC:
443 case F_GETFD:
444 case F_SETFD:
445 case F_GETFL:
446 return 1;
447 }
448 return 0;
449}
450
a26eab24 451SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
1da177e4 452{
2903ff01 453 struct fd f = fdget_raw(fd);
1da177e4
LT
454 long err = -EBADF;
455
2903ff01 456 if (!f.file)
1da177e4
LT
457 goto out;
458
2903ff01 459 if (unlikely(f.file->f_mode & FMODE_PATH)) {
545ec2c7
AV
460 if (!check_fcntl_cmd(cmd))
461 goto out1;
1abf0c71
AV
462 }
463
2903ff01 464 err = security_file_fcntl(f.file, cmd, arg);
545ec2c7 465 if (!err)
2903ff01 466 err = do_fcntl(fd, cmd, arg, f.file);
1da177e4 467
545ec2c7 468out1:
2903ff01 469 fdput(f);
1da177e4
LT
470out:
471 return err;
472}
473
474#if BITS_PER_LONG == 32
a26eab24
HC
475SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
476 unsigned long, arg)
1da177e4 477{
a75d30c7 478 void __user *argp = (void __user *)arg;
2903ff01 479 struct fd f = fdget_raw(fd);
a75d30c7 480 struct flock64 flock;
545ec2c7 481 long err = -EBADF;
1da177e4 482
2903ff01 483 if (!f.file)
1da177e4
LT
484 goto out;
485
2903ff01 486 if (unlikely(f.file->f_mode & FMODE_PATH)) {
545ec2c7
AV
487 if (!check_fcntl_cmd(cmd))
488 goto out1;
1abf0c71
AV
489 }
490
2903ff01 491 err = security_file_fcntl(f.file, cmd, arg);
545ec2c7
AV
492 if (err)
493 goto out1;
1da177e4
LT
494
495 switch (cmd) {
5d50ffd7 496 case F_GETLK64:
0d3f7a2d 497 case F_OFD_GETLK:
a75d30c7
CH
498 err = -EFAULT;
499 if (copy_from_user(&flock, argp, sizeof(flock)))
500 break;
501 err = fcntl_getlk64(f.file, cmd, &flock);
502 if (!err && copy_to_user(argp, &flock, sizeof(flock)))
503 err = -EFAULT;
5d50ffd7
JL
504 break;
505 case F_SETLK64:
506 case F_SETLKW64:
0d3f7a2d
JL
507 case F_OFD_SETLK:
508 case F_OFD_SETLKW:
a75d30c7
CH
509 err = -EFAULT;
510 if (copy_from_user(&flock, argp, sizeof(flock)))
511 break;
512 err = fcntl_setlk64(fd, f.file, cmd, &flock);
5d50ffd7
JL
513 break;
514 default:
515 err = do_fcntl(fd, cmd, arg, f.file);
516 break;
1da177e4 517 }
545ec2c7 518out1:
2903ff01 519 fdput(f);
1da177e4
LT
520out:
521 return err;
522}
523#endif
524
80f0cce6 525#ifdef CONFIG_COMPAT
8c6657cb 526/* careful - don't use anywhere else */
b59eea55
LT
527#define copy_flock_fields(dst, src) \
528 (dst)->l_type = (src)->l_type; \
529 (dst)->l_whence = (src)->l_whence; \
530 (dst)->l_start = (src)->l_start; \
531 (dst)->l_len = (src)->l_len; \
532 (dst)->l_pid = (src)->l_pid;
533
534static int get_compat_flock(struct flock *kfl, const struct compat_flock __user *ufl)
80f0cce6 535{
8c6657cb
AV
536 struct compat_flock fl;
537
538 if (copy_from_user(&fl, ufl, sizeof(struct compat_flock)))
80f0cce6 539 return -EFAULT;
b59eea55 540 copy_flock_fields(kfl, &fl);
80f0cce6
AV
541 return 0;
542}
543
b59eea55 544static int get_compat_flock64(struct flock *kfl, const struct compat_flock64 __user *ufl)
80f0cce6 545{
8c6657cb
AV
546 struct compat_flock64 fl;
547
548 if (copy_from_user(&fl, ufl, sizeof(struct compat_flock64)))
80f0cce6 549 return -EFAULT;
b59eea55 550 copy_flock_fields(kfl, &fl);
80f0cce6
AV
551 return 0;
552}
553
b59eea55 554static int put_compat_flock(const struct flock *kfl, struct compat_flock __user *ufl)
80f0cce6 555{
8c6657cb
AV
556 struct compat_flock fl;
557
558 memset(&fl, 0, sizeof(struct compat_flock));
b59eea55 559 copy_flock_fields(&fl, kfl);
8c6657cb 560 if (copy_to_user(ufl, &fl, sizeof(struct compat_flock)))
80f0cce6
AV
561 return -EFAULT;
562 return 0;
563}
80f0cce6 564
b59eea55 565static int put_compat_flock64(const struct flock *kfl, struct compat_flock64 __user *ufl)
80f0cce6 566{
8c6657cb
AV
567 struct compat_flock64 fl;
568
4d2dc2cc
JL
569 BUILD_BUG_ON(sizeof(kfl->l_start) > sizeof(ufl->l_start));
570 BUILD_BUG_ON(sizeof(kfl->l_len) > sizeof(ufl->l_len));
571
8c6657cb 572 memset(&fl, 0, sizeof(struct compat_flock64));
b59eea55 573 copy_flock_fields(&fl, kfl);
8c6657cb 574 if (copy_to_user(ufl, &fl, sizeof(struct compat_flock64)))
80f0cce6
AV
575 return -EFAULT;
576 return 0;
577}
8c6657cb 578#undef copy_flock_fields
80f0cce6
AV
579
580static unsigned int
581convert_fcntl_cmd(unsigned int cmd)
582{
583 switch (cmd) {
584 case F_GETLK64:
585 return F_GETLK;
586 case F_SETLK64:
587 return F_SETLK;
588 case F_SETLKW64:
589 return F_SETLKW;
590 }
591
592 return cmd;
593}
594
94073ad7
CH
595/*
596 * GETLK was successful and we need to return the data, but it needs to fit in
597 * the compat structure.
598 * l_start shouldn't be too big, unless the original start + end is greater than
599 * COMPAT_OFF_T_MAX, in which case the app was asking for trouble, so we return
600 * -EOVERFLOW in that case. l_len could be too big, in which case we just
601 * truncate it, and only allow the app to see that part of the conflicting lock
602 * that might make sense to it anyway
603 */
604static int fixup_compat_flock(struct flock *flock)
605{
606 if (flock->l_start > COMPAT_OFF_T_MAX)
607 return -EOVERFLOW;
608 if (flock->l_len > COMPAT_OFF_T_MAX)
609 flock->l_len = COMPAT_OFF_T_MAX;
610 return 0;
611}
612
80f0cce6
AV
613COMPAT_SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
614 compat_ulong_t, arg)
615{
94073ad7
CH
616 struct fd f = fdget_raw(fd);
617 struct flock flock;
618 long err = -EBADF;
619
620 if (!f.file)
621 return err;
622
623 if (unlikely(f.file->f_mode & FMODE_PATH)) {
624 if (!check_fcntl_cmd(cmd))
625 goto out_put;
626 }
627
628 err = security_file_fcntl(f.file, cmd, arg);
629 if (err)
630 goto out_put;
80f0cce6
AV
631
632 switch (cmd) {
633 case F_GETLK:
94073ad7
CH
634 err = get_compat_flock(&flock, compat_ptr(arg));
635 if (err)
636 break;
637 err = fcntl_getlk(f.file, convert_fcntl_cmd(cmd), &flock);
638 if (err)
639 break;
640 err = fixup_compat_flock(&flock);
9280a601
JL
641 if (!err)
642 err = put_compat_flock(&flock, compat_ptr(arg));
94073ad7
CH
643 break;
644 case F_GETLK64:
645 case F_OFD_GETLK:
646 err = get_compat_flock64(&flock, compat_ptr(arg));
647 if (err)
648 break;
649 err = fcntl_getlk(f.file, convert_fcntl_cmd(cmd), &flock);
4d2dc2cc
JL
650 if (!err)
651 err = put_compat_flock64(&flock, compat_ptr(arg));
94073ad7 652 break;
80f0cce6
AV
653 case F_SETLK:
654 case F_SETLKW:
94073ad7
CH
655 err = get_compat_flock(&flock, compat_ptr(arg));
656 if (err)
80f0cce6 657 break;
94073ad7 658 err = fcntl_setlk(fd, f.file, convert_fcntl_cmd(cmd), &flock);
80f0cce6 659 break;
80f0cce6
AV
660 case F_SETLK64:
661 case F_SETLKW64:
80f0cce6
AV
662 case F_OFD_SETLK:
663 case F_OFD_SETLKW:
94073ad7
CH
664 err = get_compat_flock64(&flock, compat_ptr(arg));
665 if (err)
80f0cce6 666 break;
94073ad7 667 err = fcntl_setlk(fd, f.file, convert_fcntl_cmd(cmd), &flock);
80f0cce6 668 break;
80f0cce6 669 default:
94073ad7 670 err = do_fcntl(fd, cmd, arg, f.file);
80f0cce6
AV
671 break;
672 }
94073ad7
CH
673out_put:
674 fdput(f);
675 return err;
80f0cce6
AV
676}
677
678COMPAT_SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd,
679 compat_ulong_t, arg)
680{
681 switch (cmd) {
682 case F_GETLK64:
683 case F_SETLK64:
684 case F_SETLKW64:
685 case F_OFD_GETLK:
686 case F_OFD_SETLK:
687 case F_OFD_SETLKW:
688 return -EINVAL;
689 }
690 return compat_sys_fcntl64(fd, cmd, arg);
691}
692#endif
693
1da177e4
LT
694/* Table to convert sigio signal codes into poll band bitmaps */
695
fa3536cc 696static const long band_table[NSIGPOLL] = {
1da177e4
LT
697 POLLIN | POLLRDNORM, /* POLL_IN */
698 POLLOUT | POLLWRNORM | POLLWRBAND, /* POLL_OUT */
699 POLLIN | POLLRDNORM | POLLMSG, /* POLL_MSG */
700 POLLERR, /* POLL_ERR */
701 POLLPRI | POLLRDBAND, /* POLL_PRI */
702 POLLHUP | POLLERR /* POLL_HUP */
703};
704
705static inline int sigio_perm(struct task_struct *p,
706 struct fown_struct *fown, int sig)
707{
c69e8d9c
DH
708 const struct cred *cred;
709 int ret;
710
711 rcu_read_lock();
712 cred = __task_cred(p);
8e96e3b7
EB
713 ret = ((uid_eq(fown->euid, GLOBAL_ROOT_UID) ||
714 uid_eq(fown->euid, cred->suid) || uid_eq(fown->euid, cred->uid) ||
715 uid_eq(fown->uid, cred->suid) || uid_eq(fown->uid, cred->uid)) &&
c69e8d9c
DH
716 !security_file_send_sigiotask(p, fown, sig));
717 rcu_read_unlock();
718 return ret;
1da177e4
LT
719}
720
721static void send_sigio_to_task(struct task_struct *p,
8eeee4e2 722 struct fown_struct *fown,
ba0a6c9f 723 int fd, int reason, int group)
1da177e4 724{
8eeee4e2
ON
725 /*
726 * F_SETSIG can change ->signum lockless in parallel, make
727 * sure we read it once and use the same value throughout.
728 */
6aa7de05 729 int signum = READ_ONCE(fown->signum);
8eeee4e2
ON
730
731 if (!sigio_perm(p, fown, signum))
1da177e4
LT
732 return;
733
8eeee4e2 734 switch (signum) {
1da177e4
LT
735 siginfo_t si;
736 default:
737 /* Queue a rt signal with the appropriate fd as its
738 value. We use SI_SIGIO as the source, not
739 SI_KERNEL, since kernel signals always get
740 delivered even if we can't queue. Failure to
741 queue in this case _should_ be reported; we fall
742 back to SIGIO in that case. --sct */
8eeee4e2 743 si.si_signo = signum;
1da177e4
LT
744 si.si_errno = 0;
745 si.si_code = reason;
d08477aa
EB
746 /*
747 * Posix definies POLL_IN and friends to be signal
748 * specific si_codes for SIG_POLL. Linux extended
749 * these si_codes to other signals in a way that is
750 * ambiguous if other signals also have signal
751 * specific si_codes. In that case use SI_SIGIO instead
752 * to remove the ambiguity.
753 */
54640d23 754 if ((signum != SIGPOLL) && sig_specific_sicodes(signum))
d08477aa
EB
755 si.si_code = SI_SIGIO;
756
1da177e4
LT
757 /* Make sure we are called with one of the POLL_*
758 reasons, otherwise we could leak kernel stack into
759 userspace. */
d08477aa 760 BUG_ON((reason < POLL_IN) || ((reason - POLL_IN) >= NSIGPOLL));
1da177e4
LT
761 if (reason - POLL_IN >= NSIGPOLL)
762 si.si_band = ~0L;
763 else
764 si.si_band = band_table[reason - POLL_IN];
765 si.si_fd = fd;
ba0a6c9f 766 if (!do_send_sig_info(signum, &si, p, group))
1da177e4
LT
767 break;
768 /* fall-through: fall back on the old plain SIGIO signal */
769 case 0:
ba0a6c9f 770 do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, group);
1da177e4
LT
771 }
772}
773
774void send_sigio(struct fown_struct *fown, int fd, int band)
775{
776 struct task_struct *p;
609d7fa9
EB
777 enum pid_type type;
778 struct pid *pid;
ba0a6c9f 779 int group = 1;
1da177e4
LT
780
781 read_lock(&fown->lock);
ba0a6c9f 782
609d7fa9 783 type = fown->pid_type;
ba0a6c9f
PZ
784 if (type == PIDTYPE_MAX) {
785 group = 0;
786 type = PIDTYPE_PID;
787 }
788
1da177e4
LT
789 pid = fown->pid;
790 if (!pid)
791 goto out_unlock_fown;
792
793 read_lock(&tasklist_lock);
609d7fa9 794 do_each_pid_task(pid, type, p) {
ba0a6c9f 795 send_sigio_to_task(p, fown, fd, band, group);
609d7fa9 796 } while_each_pid_task(pid, type, p);
1da177e4
LT
797 read_unlock(&tasklist_lock);
798 out_unlock_fown:
799 read_unlock(&fown->lock);
800}
801
802static void send_sigurg_to_task(struct task_struct *p,
ba0a6c9f 803 struct fown_struct *fown, int group)
1da177e4
LT
804{
805 if (sigio_perm(p, fown, SIGURG))
ba0a6c9f 806 do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, group);
1da177e4
LT
807}
808
809int send_sigurg(struct fown_struct *fown)
810{
811 struct task_struct *p;
609d7fa9
EB
812 enum pid_type type;
813 struct pid *pid;
ba0a6c9f 814 int group = 1;
609d7fa9 815 int ret = 0;
1da177e4
LT
816
817 read_lock(&fown->lock);
ba0a6c9f 818
609d7fa9 819 type = fown->pid_type;
ba0a6c9f
PZ
820 if (type == PIDTYPE_MAX) {
821 group = 0;
822 type = PIDTYPE_PID;
823 }
824
1da177e4
LT
825 pid = fown->pid;
826 if (!pid)
827 goto out_unlock_fown;
828
829 ret = 1;
830
831 read_lock(&tasklist_lock);
609d7fa9 832 do_each_pid_task(pid, type, p) {
ba0a6c9f 833 send_sigurg_to_task(p, fown, group);
609d7fa9 834 } while_each_pid_task(pid, type, p);
1da177e4
LT
835 read_unlock(&tasklist_lock);
836 out_unlock_fown:
837 read_unlock(&fown->lock);
838 return ret;
839}
840
989a2979 841static DEFINE_SPINLOCK(fasync_lock);
e18b890b 842static struct kmem_cache *fasync_cache __read_mostly;
1da177e4 843
989a2979
ED
844static void fasync_free_rcu(struct rcu_head *head)
845{
846 kmem_cache_free(fasync_cache,
847 container_of(head, struct fasync_struct, fa_rcu));
848}
849
1da177e4 850/*
53281b6d
LT
851 * Remove a fasync entry. If successfully removed, return
852 * positive and clear the FASYNC flag. If no entry exists,
853 * do nothing and return 0.
854 *
855 * NOTE! It is very important that the FASYNC flag always
856 * match the state "is the filp on a fasync list".
857 *
1da177e4 858 */
f7347ce4 859int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp)
1da177e4
LT
860{
861 struct fasync_struct *fa, **fp;
1da177e4
LT
862 int result = 0;
863
53281b6d 864 spin_lock(&filp->f_lock);
989a2979 865 spin_lock(&fasync_lock);
53281b6d
LT
866 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
867 if (fa->fa_file != filp)
868 continue;
989a2979
ED
869
870 spin_lock_irq(&fa->fa_lock);
871 fa->fa_file = NULL;
872 spin_unlock_irq(&fa->fa_lock);
873
53281b6d 874 *fp = fa->fa_next;
989a2979 875 call_rcu(&fa->fa_rcu, fasync_free_rcu);
53281b6d
LT
876 filp->f_flags &= ~FASYNC;
877 result = 1;
878 break;
1da177e4 879 }
989a2979 880 spin_unlock(&fasync_lock);
53281b6d
LT
881 spin_unlock(&filp->f_lock);
882 return result;
883}
884
f7347ce4
LT
885struct fasync_struct *fasync_alloc(void)
886{
887 return kmem_cache_alloc(fasync_cache, GFP_KERNEL);
888}
889
53281b6d 890/*
f7347ce4
LT
891 * NOTE! This can be used only for unused fasync entries:
892 * entries that actually got inserted on the fasync list
893 * need to be released by rcu - see fasync_remove_entry.
53281b6d 894 */
f7347ce4 895void fasync_free(struct fasync_struct *new)
53281b6d 896{
f7347ce4
LT
897 kmem_cache_free(fasync_cache, new);
898}
53281b6d 899
f7347ce4
LT
900/*
901 * Insert a new entry into the fasync list. Return the pointer to the
902 * old one if we didn't use the new one.
55f335a8
LT
903 *
904 * NOTE! It is very important that the FASYNC flag always
905 * match the state "is the filp on a fasync list".
f7347ce4
LT
906 */
907struct fasync_struct *fasync_insert_entry(int fd, struct file *filp, struct fasync_struct **fapp, struct fasync_struct *new)
908{
909 struct fasync_struct *fa, **fp;
4a6a4499 910
4a6a4499 911 spin_lock(&filp->f_lock);
989a2979 912 spin_lock(&fasync_lock);
1da177e4 913 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
53281b6d
LT
914 if (fa->fa_file != filp)
915 continue;
989a2979
ED
916
917 spin_lock_irq(&fa->fa_lock);
53281b6d 918 fa->fa_fd = fd;
989a2979 919 spin_unlock_irq(&fa->fa_lock);
53281b6d 920 goto out;
1da177e4
LT
921 }
922
989a2979 923 spin_lock_init(&new->fa_lock);
53281b6d
LT
924 new->magic = FASYNC_MAGIC;
925 new->fa_file = filp;
926 new->fa_fd = fd;
927 new->fa_next = *fapp;
989a2979 928 rcu_assign_pointer(*fapp, new);
53281b6d
LT
929 filp->f_flags |= FASYNC;
930
1da177e4 931out:
989a2979 932 spin_unlock(&fasync_lock);
4a6a4499 933 spin_unlock(&filp->f_lock);
f7347ce4
LT
934 return fa;
935}
936
937/*
938 * Add a fasync entry. Return negative on error, positive if
939 * added, and zero if did nothing but change an existing one.
f7347ce4
LT
940 */
941static int fasync_add_entry(int fd, struct file *filp, struct fasync_struct **fapp)
942{
943 struct fasync_struct *new;
944
945 new = fasync_alloc();
946 if (!new)
947 return -ENOMEM;
948
949 /*
950 * fasync_insert_entry() returns the old (update) entry if
951 * it existed.
952 *
953 * So free the (unused) new entry and return 0 to let the
954 * caller know that we didn't add any new fasync entries.
955 */
956 if (fasync_insert_entry(fd, filp, fapp, new)) {
957 fasync_free(new);
958 return 0;
959 }
960
961 return 1;
1da177e4
LT
962}
963
53281b6d
LT
964/*
965 * fasync_helper() is used by almost all character device drivers
966 * to set up the fasync queue, and for regular files by the file
967 * lease code. It returns negative on error, 0 if it did no changes
968 * and positive if it added/deleted the entry.
969 */
970int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
971{
972 if (!on)
973 return fasync_remove_entry(filp, fapp);
974 return fasync_add_entry(fd, filp, fapp);
975}
976
1da177e4
LT
977EXPORT_SYMBOL(fasync_helper);
978
989a2979
ED
979/*
980 * rcu_read_lock() is held
981 */
982static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band)
1da177e4
LT
983{
984 while (fa) {
989a2979 985 struct fown_struct *fown;
f4985dc7
AM
986 unsigned long flags;
987
1da177e4
LT
988 if (fa->magic != FASYNC_MAGIC) {
989 printk(KERN_ERR "kill_fasync: bad magic number in "
990 "fasync_struct!\n");
991 return;
992 }
f4985dc7 993 spin_lock_irqsave(&fa->fa_lock, flags);
989a2979
ED
994 if (fa->fa_file) {
995 fown = &fa->fa_file->f_owner;
996 /* Don't send SIGURG to processes which have not set a
997 queued signum: SIGURG has its own default signalling
998 mechanism. */
999 if (!(sig == SIGURG && fown->signum == 0))
1000 send_sigio(fown, fa->fa_fd, band);
1001 }
f4985dc7 1002 spin_unlock_irqrestore(&fa->fa_lock, flags);
989a2979 1003 fa = rcu_dereference(fa->fa_next);
1da177e4
LT
1004 }
1005}
1006
1da177e4
LT
1007void kill_fasync(struct fasync_struct **fp, int sig, int band)
1008{
1009 /* First a quick test without locking: usually
1010 * the list is empty.
1011 */
1012 if (*fp) {
989a2979
ED
1013 rcu_read_lock();
1014 kill_fasync_rcu(rcu_dereference(*fp), sig, band);
1015 rcu_read_unlock();
1da177e4
LT
1016 }
1017}
1018EXPORT_SYMBOL(kill_fasync);
1019
454eedb8 1020static int __init fcntl_init(void)
1da177e4 1021{
3ab04d5c
JB
1022 /*
1023 * Please add new bits here to ensure allocation uniqueness.
1024 * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
1025 * is defined as O_NONBLOCK on some platforms and not on others.
1026 */
80f18379
CH
1027 BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ !=
1028 HWEIGHT32(
1029 (VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
1030 __FMODE_EXEC | __FMODE_NONOTIFY));
454eedb8 1031
1da177e4 1032 fasync_cache = kmem_cache_create("fasync_cache",
20c2df83 1033 sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
1da177e4
LT
1034 return 0;
1035}
1036
454eedb8 1037module_init(fcntl_init)