1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
8 #include <linux/syscalls.h>
9 #include <linux/init.h>
11 #include <linux/sched/task.h>
13 #include <linux/filelock.h>
14 #include <linux/file.h>
15 #include <linux/fdtable.h>
16 #include <linux/capability.h>
17 #include <linux/dnotify.h>
18 #include <linux/slab.h>
19 #include <linux/module.h>
20 #include <linux/pipe_fs_i.h>
21 #include <linux/security.h>
22 #include <linux/ptrace.h>
23 #include <linux/signal.h>
24 #include <linux/rcupdate.h>
25 #include <linux/pid_namespace.h>
26 #include <linux/user_namespace.h>
27 #include <linux/memfd.h>
28 #include <linux/compat.h>
29 #include <linux/mount.h>
31 #include <linux/poll.h>
32 #include <asm/siginfo.h>
33 #include <linux/uaccess.h>
35 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
37 static int setfl(int fd, struct file * filp, unsigned long arg)
39 struct inode * inode = file_inode(filp);
43 * O_APPEND cannot be cleared if the file is marked as append-only
44 * and the file is open for write.
46 if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode))
49 /* O_NOATIME can only be set by the owner or superuser */
50 if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
51 if (!inode_owner_or_capable(file_mnt_idmap(filp), inode))
54 /* required for strict SunOS emulation */
55 if (O_NONBLOCK != O_NDELAY)
59 /* Pipe packetized mode is controlled by O_DIRECT flag */
60 if (!S_ISFIFO(inode->i_mode) &&
62 !(filp->f_mode & FMODE_CAN_ODIRECT))
65 if (filp->f_op->check_flags)
66 error = filp->f_op->check_flags(arg);
71 * ->fasync() is responsible for setting the FASYNC bit.
73 if (((arg ^ filp->f_flags) & FASYNC) && filp->f_op->fasync) {
74 error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
80 spin_lock(&filp->f_lock);
81 filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
82 filp->f_iocb_flags = iocb_flags(filp);
83 spin_unlock(&filp->f_lock);
89 static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
92 write_lock_irq(&filp->f_owner.lock);
93 if (force || !filp->f_owner.pid) {
94 put_pid(filp->f_owner.pid);
95 filp->f_owner.pid = get_pid(pid);
96 filp->f_owner.pid_type = type;
99 const struct cred *cred = current_cred();
100 filp->f_owner.uid = cred->uid;
101 filp->f_owner.euid = cred->euid;
104 write_unlock_irq(&filp->f_owner.lock);
107 void __f_setown(struct file *filp, struct pid *pid, enum pid_type type,
110 security_file_set_fowner(filp);
111 f_modown(filp, pid, type, force);
113 EXPORT_SYMBOL(__f_setown);
115 int f_setown(struct file *filp, unsigned long arg, int force)
118 struct pid *pid = NULL;
119 int who = arg, ret = 0;
123 /* avoid overflow below */
133 pid = find_vpid(who);
139 __f_setown(filp, pid, type, force);
144 EXPORT_SYMBOL(f_setown);
146 void f_delown(struct file *filp)
148 f_modown(filp, NULL, PIDTYPE_TGID, 1);
151 pid_t f_getown(struct file *filp)
155 read_lock_irq(&filp->f_owner.lock);
157 if (pid_task(filp->f_owner.pid, filp->f_owner.pid_type)) {
158 pid = pid_vnr(filp->f_owner.pid);
159 if (filp->f_owner.pid_type == PIDTYPE_PGID)
163 read_unlock_irq(&filp->f_owner.lock);
167 static int f_setown_ex(struct file *filp, unsigned long arg)
169 struct f_owner_ex __user *owner_p = (void __user *)arg;
170 struct f_owner_ex owner;
175 ret = copy_from_user(&owner, owner_p, sizeof(owner));
179 switch (owner.type) {
197 pid = find_vpid(owner.pid);
198 if (owner.pid && !pid)
201 __f_setown(filp, pid, type, 1);
207 static int f_getown_ex(struct file *filp, unsigned long arg)
209 struct f_owner_ex __user *owner_p = (void __user *)arg;
210 struct f_owner_ex owner = {};
213 read_lock_irq(&filp->f_owner.lock);
215 if (pid_task(filp->f_owner.pid, filp->f_owner.pid_type))
216 owner.pid = pid_vnr(filp->f_owner.pid);
218 switch (filp->f_owner.pid_type) {
220 owner.type = F_OWNER_TID;
224 owner.type = F_OWNER_PID;
228 owner.type = F_OWNER_PGRP;
236 read_unlock_irq(&filp->f_owner.lock);
239 ret = copy_to_user(owner_p, &owner, sizeof(owner));
246 #ifdef CONFIG_CHECKPOINT_RESTORE
247 static int f_getowner_uids(struct file *filp, unsigned long arg)
249 struct user_namespace *user_ns = current_user_ns();
250 uid_t __user *dst = (void __user *)arg;
254 read_lock_irq(&filp->f_owner.lock);
255 src[0] = from_kuid(user_ns, filp->f_owner.uid);
256 src[1] = from_kuid(user_ns, filp->f_owner.euid);
257 read_unlock_irq(&filp->f_owner.lock);
259 err = put_user(src[0], &dst[0]);
260 err |= put_user(src[1], &dst[1]);
265 static int f_getowner_uids(struct file *filp, unsigned long arg)
271 static bool rw_hint_valid(enum rw_hint hint)
274 case RWH_WRITE_LIFE_NOT_SET:
275 case RWH_WRITE_LIFE_NONE:
276 case RWH_WRITE_LIFE_SHORT:
277 case RWH_WRITE_LIFE_MEDIUM:
278 case RWH_WRITE_LIFE_LONG:
279 case RWH_WRITE_LIFE_EXTREME:
286 static long fcntl_rw_hint(struct file *file, unsigned int cmd,
289 struct inode *inode = file_inode(file);
290 u64 __user *argp = (u64 __user *)arg;
296 h = inode->i_write_hint;
297 if (copy_to_user(argp, &h, sizeof(*argp)))
301 if (copy_from_user(&h, argp, sizeof(h)))
303 hint = (enum rw_hint) h;
304 if (!rw_hint_valid(hint))
308 inode->i_write_hint = hint;
316 static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
319 void __user *argp = (void __user *)arg;
325 err = f_dupfd(arg, filp, 0);
327 case F_DUPFD_CLOEXEC:
328 err = f_dupfd(arg, filp, O_CLOEXEC);
331 err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
335 set_close_on_exec(fd, arg & FD_CLOEXEC);
341 err = setfl(fd, filp, arg);
343 #if BITS_PER_LONG != 32
344 /* 32-bit arches must use fcntl64() */
348 if (copy_from_user(&flock, argp, sizeof(flock)))
350 err = fcntl_getlk(filp, cmd, &flock);
351 if (!err && copy_to_user(argp, &flock, sizeof(flock)))
354 #if BITS_PER_LONG != 32
355 /* 32-bit arches must use fcntl64() */
362 if (copy_from_user(&flock, argp, sizeof(flock)))
364 err = fcntl_setlk(fd, filp, cmd, &flock);
368 * XXX If f_owner is a process group, the
369 * negative return value will get converted
370 * into an error. Oops. If we keep the
371 * current syscall conventions, the only way
372 * to fix this will be in libc.
374 err = f_getown(filp);
375 force_successful_syscall_return();
378 err = f_setown(filp, arg, 1);
381 err = f_getown_ex(filp, arg);
384 err = f_setown_ex(filp, arg);
386 case F_GETOWNER_UIDS:
387 err = f_getowner_uids(filp, arg);
390 err = filp->f_owner.signum;
393 /* arg == 0 restores default behaviour. */
394 if (!valid_signal(arg)) {
398 filp->f_owner.signum = arg;
401 err = fcntl_getlease(filp);
404 err = fcntl_setlease(fd, filp, arg);
407 err = fcntl_dirnotify(fd, filp, arg);
411 err = pipe_fcntl(filp, cmd, arg);
415 err = memfd_fcntl(filp, cmd, arg);
419 err = fcntl_rw_hint(filp, cmd, arg);
427 static int check_fcntl_cmd(unsigned cmd)
431 case F_DUPFD_CLOEXEC:
440 SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
442 struct fd f = fdget_raw(fd);
448 if (unlikely(f.file->f_mode & FMODE_PATH)) {
449 if (!check_fcntl_cmd(cmd))
453 err = security_file_fcntl(f.file, cmd, arg);
455 err = do_fcntl(fd, cmd, arg, f.file);
463 #if BITS_PER_LONG == 32
464 SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
467 void __user *argp = (void __user *)arg;
468 struct fd f = fdget_raw(fd);
469 struct flock64 flock;
475 if (unlikely(f.file->f_mode & FMODE_PATH)) {
476 if (!check_fcntl_cmd(cmd))
480 err = security_file_fcntl(f.file, cmd, arg);
488 if (copy_from_user(&flock, argp, sizeof(flock)))
490 err = fcntl_getlk64(f.file, cmd, &flock);
491 if (!err && copy_to_user(argp, &flock, sizeof(flock)))
499 if (copy_from_user(&flock, argp, sizeof(flock)))
501 err = fcntl_setlk64(fd, f.file, cmd, &flock);
504 err = do_fcntl(fd, cmd, arg, f.file);
515 /* careful - don't use anywhere else */
516 #define copy_flock_fields(dst, src) \
517 (dst)->l_type = (src)->l_type; \
518 (dst)->l_whence = (src)->l_whence; \
519 (dst)->l_start = (src)->l_start; \
520 (dst)->l_len = (src)->l_len; \
521 (dst)->l_pid = (src)->l_pid;
523 static int get_compat_flock(struct flock *kfl, const struct compat_flock __user *ufl)
525 struct compat_flock fl;
527 if (copy_from_user(&fl, ufl, sizeof(struct compat_flock)))
529 copy_flock_fields(kfl, &fl);
533 static int get_compat_flock64(struct flock *kfl, const struct compat_flock64 __user *ufl)
535 struct compat_flock64 fl;
537 if (copy_from_user(&fl, ufl, sizeof(struct compat_flock64)))
539 copy_flock_fields(kfl, &fl);
543 static int put_compat_flock(const struct flock *kfl, struct compat_flock __user *ufl)
545 struct compat_flock fl;
547 memset(&fl, 0, sizeof(struct compat_flock));
548 copy_flock_fields(&fl, kfl);
549 if (copy_to_user(ufl, &fl, sizeof(struct compat_flock)))
554 static int put_compat_flock64(const struct flock *kfl, struct compat_flock64 __user *ufl)
556 struct compat_flock64 fl;
558 BUILD_BUG_ON(sizeof(kfl->l_start) > sizeof(ufl->l_start));
559 BUILD_BUG_ON(sizeof(kfl->l_len) > sizeof(ufl->l_len));
561 memset(&fl, 0, sizeof(struct compat_flock64));
562 copy_flock_fields(&fl, kfl);
563 if (copy_to_user(ufl, &fl, sizeof(struct compat_flock64)))
567 #undef copy_flock_fields
570 convert_fcntl_cmd(unsigned int cmd)
585 * GETLK was successful and we need to return the data, but it needs to fit in
586 * the compat structure.
587 * l_start shouldn't be too big, unless the original start + end is greater than
588 * COMPAT_OFF_T_MAX, in which case the app was asking for trouble, so we return
589 * -EOVERFLOW in that case. l_len could be too big, in which case we just
590 * truncate it, and only allow the app to see that part of the conflicting lock
591 * that might make sense to it anyway
593 static int fixup_compat_flock(struct flock *flock)
595 if (flock->l_start > COMPAT_OFF_T_MAX)
597 if (flock->l_len > COMPAT_OFF_T_MAX)
598 flock->l_len = COMPAT_OFF_T_MAX;
602 static long do_compat_fcntl64(unsigned int fd, unsigned int cmd,
605 struct fd f = fdget_raw(fd);
612 if (unlikely(f.file->f_mode & FMODE_PATH)) {
613 if (!check_fcntl_cmd(cmd))
617 err = security_file_fcntl(f.file, cmd, arg);
623 err = get_compat_flock(&flock, compat_ptr(arg));
626 err = fcntl_getlk(f.file, convert_fcntl_cmd(cmd), &flock);
629 err = fixup_compat_flock(&flock);
631 err = put_compat_flock(&flock, compat_ptr(arg));
635 err = get_compat_flock64(&flock, compat_ptr(arg));
638 err = fcntl_getlk(f.file, convert_fcntl_cmd(cmd), &flock);
640 err = put_compat_flock64(&flock, compat_ptr(arg));
644 err = get_compat_flock(&flock, compat_ptr(arg));
647 err = fcntl_setlk(fd, f.file, convert_fcntl_cmd(cmd), &flock);
653 err = get_compat_flock64(&flock, compat_ptr(arg));
656 err = fcntl_setlk(fd, f.file, convert_fcntl_cmd(cmd), &flock);
659 err = do_fcntl(fd, cmd, arg, f.file);
667 COMPAT_SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
670 return do_compat_fcntl64(fd, cmd, arg);
673 COMPAT_SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd,
685 return do_compat_fcntl64(fd, cmd, arg);
689 /* Table to convert sigio signal codes into poll band bitmaps */
691 static const __poll_t band_table[NSIGPOLL] = {
692 EPOLLIN | EPOLLRDNORM, /* POLL_IN */
693 EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND, /* POLL_OUT */
694 EPOLLIN | EPOLLRDNORM | EPOLLMSG, /* POLL_MSG */
695 EPOLLERR, /* POLL_ERR */
696 EPOLLPRI | EPOLLRDBAND, /* POLL_PRI */
697 EPOLLHUP | EPOLLERR /* POLL_HUP */
700 static inline int sigio_perm(struct task_struct *p,
701 struct fown_struct *fown, int sig)
703 const struct cred *cred;
707 cred = __task_cred(p);
708 ret = ((uid_eq(fown->euid, GLOBAL_ROOT_UID) ||
709 uid_eq(fown->euid, cred->suid) || uid_eq(fown->euid, cred->uid) ||
710 uid_eq(fown->uid, cred->suid) || uid_eq(fown->uid, cred->uid)) &&
711 !security_file_send_sigiotask(p, fown, sig));
716 static void send_sigio_to_task(struct task_struct *p,
717 struct fown_struct *fown,
718 int fd, int reason, enum pid_type type)
721 * F_SETSIG can change ->signum lockless in parallel, make
722 * sure we read it once and use the same value throughout.
724 int signum = READ_ONCE(fown->signum);
726 if (!sigio_perm(p, fown, signum))
733 /* Queue a rt signal with the appropriate fd as its
734 value. We use SI_SIGIO as the source, not
735 SI_KERNEL, since kernel signals always get
736 delivered even if we can't queue. Failure to
737 queue in this case _should_ be reported; we fall
738 back to SIGIO in that case. --sct */
740 si.si_signo = signum;
744 * Posix definies POLL_IN and friends to be signal
745 * specific si_codes for SIG_POLL. Linux extended
746 * these si_codes to other signals in a way that is
747 * ambiguous if other signals also have signal
748 * specific si_codes. In that case use SI_SIGIO instead
749 * to remove the ambiguity.
751 if ((signum != SIGPOLL) && sig_specific_sicodes(signum))
752 si.si_code = SI_SIGIO;
754 /* Make sure we are called with one of the POLL_*
755 reasons, otherwise we could leak kernel stack into
757 BUG_ON((reason < POLL_IN) || ((reason - POLL_IN) >= NSIGPOLL));
758 if (reason - POLL_IN >= NSIGPOLL)
761 si.si_band = mangle_poll(band_table[reason - POLL_IN]);
763 if (!do_send_sig_info(signum, &si, p, type))
766 fallthrough; /* fall back on the old plain SIGIO signal */
768 do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, type);
772 void send_sigio(struct fown_struct *fown, int fd, int band)
774 struct task_struct *p;
779 read_lock_irqsave(&fown->lock, flags);
781 type = fown->pid_type;
784 goto out_unlock_fown;
786 if (type <= PIDTYPE_TGID) {
788 p = pid_task(pid, PIDTYPE_PID);
790 send_sigio_to_task(p, fown, fd, band, type);
793 read_lock(&tasklist_lock);
794 do_each_pid_task(pid, type, p) {
795 send_sigio_to_task(p, fown, fd, band, type);
796 } while_each_pid_task(pid, type, p);
797 read_unlock(&tasklist_lock);
800 read_unlock_irqrestore(&fown->lock, flags);
803 static void send_sigurg_to_task(struct task_struct *p,
804 struct fown_struct *fown, enum pid_type type)
806 if (sigio_perm(p, fown, SIGURG))
807 do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, type);
810 int send_sigurg(struct fown_struct *fown)
812 struct task_struct *p;
818 read_lock_irqsave(&fown->lock, flags);
820 type = fown->pid_type;
823 goto out_unlock_fown;
827 if (type <= PIDTYPE_TGID) {
829 p = pid_task(pid, PIDTYPE_PID);
831 send_sigurg_to_task(p, fown, type);
834 read_lock(&tasklist_lock);
835 do_each_pid_task(pid, type, p) {
836 send_sigurg_to_task(p, fown, type);
837 } while_each_pid_task(pid, type, p);
838 read_unlock(&tasklist_lock);
841 read_unlock_irqrestore(&fown->lock, flags);
845 static DEFINE_SPINLOCK(fasync_lock);
846 static struct kmem_cache *fasync_cache __read_mostly;
848 static void fasync_free_rcu(struct rcu_head *head)
850 kmem_cache_free(fasync_cache,
851 container_of(head, struct fasync_struct, fa_rcu));
855 * Remove a fasync entry. If successfully removed, return
856 * positive and clear the FASYNC flag. If no entry exists,
857 * do nothing and return 0.
859 * NOTE! It is very important that the FASYNC flag always
860 * match the state "is the filp on a fasync list".
863 int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp)
865 struct fasync_struct *fa, **fp;
868 spin_lock(&filp->f_lock);
869 spin_lock(&fasync_lock);
870 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
871 if (fa->fa_file != filp)
874 write_lock_irq(&fa->fa_lock);
876 write_unlock_irq(&fa->fa_lock);
879 call_rcu(&fa->fa_rcu, fasync_free_rcu);
880 filp->f_flags &= ~FASYNC;
884 spin_unlock(&fasync_lock);
885 spin_unlock(&filp->f_lock);
889 struct fasync_struct *fasync_alloc(void)
891 return kmem_cache_alloc(fasync_cache, GFP_KERNEL);
895 * NOTE! This can be used only for unused fasync entries:
896 * entries that actually got inserted on the fasync list
897 * need to be released by rcu - see fasync_remove_entry.
899 void fasync_free(struct fasync_struct *new)
901 kmem_cache_free(fasync_cache, new);
905 * Insert a new entry into the fasync list. Return the pointer to the
906 * old one if we didn't use the new one.
908 * NOTE! It is very important that the FASYNC flag always
909 * match the state "is the filp on a fasync list".
911 struct fasync_struct *fasync_insert_entry(int fd, struct file *filp, struct fasync_struct **fapp, struct fasync_struct *new)
913 struct fasync_struct *fa, **fp;
915 spin_lock(&filp->f_lock);
916 spin_lock(&fasync_lock);
917 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
918 if (fa->fa_file != filp)
921 write_lock_irq(&fa->fa_lock);
923 write_unlock_irq(&fa->fa_lock);
927 rwlock_init(&new->fa_lock);
928 new->magic = FASYNC_MAGIC;
931 new->fa_next = *fapp;
932 rcu_assign_pointer(*fapp, new);
933 filp->f_flags |= FASYNC;
936 spin_unlock(&fasync_lock);
937 spin_unlock(&filp->f_lock);
942 * Add a fasync entry. Return negative on error, positive if
943 * added, and zero if did nothing but change an existing one.
945 static int fasync_add_entry(int fd, struct file *filp, struct fasync_struct **fapp)
947 struct fasync_struct *new;
949 new = fasync_alloc();
954 * fasync_insert_entry() returns the old (update) entry if
957 * So free the (unused) new entry and return 0 to let the
958 * caller know that we didn't add any new fasync entries.
960 if (fasync_insert_entry(fd, filp, fapp, new)) {
969 * fasync_helper() is used by almost all character device drivers
970 * to set up the fasync queue, and for regular files by the file
971 * lease code. It returns negative on error, 0 if it did no changes
972 * and positive if it added/deleted the entry.
974 int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
977 return fasync_remove_entry(filp, fapp);
978 return fasync_add_entry(fd, filp, fapp);
981 EXPORT_SYMBOL(fasync_helper);
984 * rcu_read_lock() is held
986 static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band)
989 struct fown_struct *fown;
992 if (fa->magic != FASYNC_MAGIC) {
993 printk(KERN_ERR "kill_fasync: bad magic number in "
997 read_lock_irqsave(&fa->fa_lock, flags);
999 fown = &fa->fa_file->f_owner;
1000 /* Don't send SIGURG to processes which have not set a
1001 queued signum: SIGURG has its own default signalling
1003 if (!(sig == SIGURG && fown->signum == 0))
1004 send_sigio(fown, fa->fa_fd, band);
1006 read_unlock_irqrestore(&fa->fa_lock, flags);
1007 fa = rcu_dereference(fa->fa_next);
1011 void kill_fasync(struct fasync_struct **fp, int sig, int band)
1013 /* First a quick test without locking: usually
1014 * the list is empty.
1018 kill_fasync_rcu(rcu_dereference(*fp), sig, band);
1022 EXPORT_SYMBOL(kill_fasync);
1024 static int __init fcntl_init(void)
1027 * Please add new bits here to ensure allocation uniqueness.
1028 * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
1029 * is defined as O_NONBLOCK on some platforms and not on others.
1031 BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ !=
1033 (VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
1034 __FMODE_EXEC | __FMODE_NONOTIFY));
1036 fasync_cache = kmem_cache_create("fasync_cache",
1037 sizeof(struct fasync_struct), 0,
1038 SLAB_PANIC | SLAB_ACCOUNT, NULL);
1042 module_init(fcntl_init)