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