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