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