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