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/file.h>
14 #include <linux/fdtable.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>
30 #include <linux/poll.h>
31 #include <asm/siginfo.h>
32 #include <linux/uaccess.h>
34 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
36 static int setfl(int fd, struct file * filp, unsigned long arg)
38 struct inode * inode = file_inode(filp);
42 * O_APPEND cannot be cleared if the file is marked as append-only
43 * and the file is open for write.
45 if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode))
48 /* O_NOATIME can only be set by the owner or superuser */
49 if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
50 if (!inode_owner_or_capable(file_mnt_user_ns(filp), inode))
53 /* required for strict SunOS emulation */
54 if (O_NONBLOCK != O_NDELAY)
58 /* Pipe packetized mode is controlled by O_DIRECT flag */
59 if (!S_ISFIFO(inode->i_mode) &&
61 !(filp->f_mode & FMODE_CAN_ODIRECT))
64 if (filp->f_op->check_flags)
65 error = filp->f_op->check_flags(arg);
70 * ->fasync() is responsible for setting the FASYNC bit.
72 if (((arg ^ filp->f_flags) & FASYNC) && filp->f_op->fasync) {
73 error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
79 spin_lock(&filp->f_lock);
80 filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
81 spin_unlock(&filp->f_lock);
87 static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
90 write_lock_irq(&filp->f_owner.lock);
91 if (force || !filp->f_owner.pid) {
92 put_pid(filp->f_owner.pid);
93 filp->f_owner.pid = get_pid(pid);
94 filp->f_owner.pid_type = type;
97 const struct cred *cred = current_cred();
98 filp->f_owner.uid = cred->uid;
99 filp->f_owner.euid = cred->euid;
102 write_unlock_irq(&filp->f_owner.lock);
105 void __f_setown(struct file *filp, struct pid *pid, enum pid_type type,
108 security_file_set_fowner(filp);
109 f_modown(filp, pid, type, force);
111 EXPORT_SYMBOL(__f_setown);
113 int f_setown(struct file *filp, unsigned long arg, int force)
116 struct pid *pid = NULL;
117 int who = arg, ret = 0;
121 /* avoid overflow below */
131 pid = find_vpid(who);
137 __f_setown(filp, pid, type, force);
142 EXPORT_SYMBOL(f_setown);
144 void f_delown(struct file *filp)
146 f_modown(filp, NULL, PIDTYPE_TGID, 1);
149 pid_t f_getown(struct file *filp)
153 read_lock_irq(&filp->f_owner.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)
161 read_unlock_irq(&filp->f_owner.lock);
165 static int f_setown_ex(struct file *filp, unsigned long arg)
167 struct f_owner_ex __user *owner_p = (void __user *)arg;
168 struct f_owner_ex owner;
173 ret = copy_from_user(&owner, owner_p, sizeof(owner));
177 switch (owner.type) {
195 pid = find_vpid(owner.pid);
196 if (owner.pid && !pid)
199 __f_setown(filp, pid, type, 1);
205 static int f_getown_ex(struct file *filp, unsigned long arg)
207 struct f_owner_ex __user *owner_p = (void __user *)arg;
208 struct f_owner_ex owner = {};
211 read_lock_irq(&filp->f_owner.lock);
213 if (pid_task(filp->f_owner.pid, filp->f_owner.pid_type))
214 owner.pid = pid_vnr(filp->f_owner.pid);
216 switch (filp->f_owner.pid_type) {
218 owner.type = F_OWNER_TID;
222 owner.type = F_OWNER_PID;
226 owner.type = F_OWNER_PGRP;
234 read_unlock_irq(&filp->f_owner.lock);
237 ret = copy_to_user(owner_p, &owner, sizeof(owner));
244 #ifdef CONFIG_CHECKPOINT_RESTORE
245 static int f_getowner_uids(struct file *filp, unsigned long arg)
247 struct user_namespace *user_ns = current_user_ns();
248 uid_t __user *dst = (void __user *)arg;
252 read_lock_irq(&filp->f_owner.lock);
253 src[0] = from_kuid(user_ns, filp->f_owner.uid);
254 src[1] = from_kuid(user_ns, filp->f_owner.euid);
255 read_unlock_irq(&filp->f_owner.lock);
257 err = put_user(src[0], &dst[0]);
258 err |= put_user(src[1], &dst[1]);
263 static int f_getowner_uids(struct file *filp, unsigned long arg)
269 static bool rw_hint_valid(enum rw_hint hint)
272 case RWH_WRITE_LIFE_NOT_SET:
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:
284 static long fcntl_rw_hint(struct file *file, unsigned int cmd,
287 struct inode *inode = file_inode(file);
288 u64 __user *argp = (u64 __user *)arg;
294 h = inode->i_write_hint;
295 if (copy_to_user(argp, &h, sizeof(*argp)))
299 if (copy_from_user(&h, argp, sizeof(h)))
301 hint = (enum rw_hint) h;
302 if (!rw_hint_valid(hint))
306 inode->i_write_hint = hint;
314 static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
317 void __user *argp = (void __user *)arg;
323 err = f_dupfd(arg, filp, 0);
325 case F_DUPFD_CLOEXEC:
326 err = f_dupfd(arg, filp, O_CLOEXEC);
329 err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
333 set_close_on_exec(fd, arg & FD_CLOEXEC);
339 err = setfl(fd, filp, arg);
341 #if BITS_PER_LONG != 32
342 /* 32-bit arches must use fcntl64() */
346 if (copy_from_user(&flock, argp, sizeof(flock)))
348 err = fcntl_getlk(filp, cmd, &flock);
349 if (!err && copy_to_user(argp, &flock, sizeof(flock)))
352 #if BITS_PER_LONG != 32
353 /* 32-bit arches must use fcntl64() */
360 if (copy_from_user(&flock, argp, sizeof(flock)))
362 err = fcntl_setlk(fd, filp, cmd, &flock);
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.
372 err = f_getown(filp);
373 force_successful_syscall_return();
376 err = f_setown(filp, arg, 1);
379 err = f_getown_ex(filp, arg);
382 err = f_setown_ex(filp, arg);
384 case F_GETOWNER_UIDS:
385 err = f_getowner_uids(filp, arg);
388 err = filp->f_owner.signum;
391 /* arg == 0 restores default behaviour. */
392 if (!valid_signal(arg)) {
396 filp->f_owner.signum = arg;
399 err = fcntl_getlease(filp);
402 err = fcntl_setlease(fd, filp, arg);
405 err = fcntl_dirnotify(fd, filp, arg);
409 err = pipe_fcntl(filp, cmd, arg);
413 err = memfd_fcntl(filp, cmd, arg);
417 err = fcntl_rw_hint(filp, cmd, arg);
425 static int check_fcntl_cmd(unsigned cmd)
429 case F_DUPFD_CLOEXEC:
438 SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
440 struct fd f = fdget_raw(fd);
446 if (unlikely(f.file->f_mode & FMODE_PATH)) {
447 if (!check_fcntl_cmd(cmd))
451 err = security_file_fcntl(f.file, cmd, arg);
453 err = do_fcntl(fd, cmd, arg, f.file);
461 #if BITS_PER_LONG == 32
462 SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
465 void __user *argp = (void __user *)arg;
466 struct fd f = fdget_raw(fd);
467 struct flock64 flock;
473 if (unlikely(f.file->f_mode & FMODE_PATH)) {
474 if (!check_fcntl_cmd(cmd))
478 err = security_file_fcntl(f.file, cmd, arg);
486 if (copy_from_user(&flock, argp, sizeof(flock)))
488 err = fcntl_getlk64(f.file, cmd, &flock);
489 if (!err && copy_to_user(argp, &flock, sizeof(flock)))
497 if (copy_from_user(&flock, argp, sizeof(flock)))
499 err = fcntl_setlk64(fd, f.file, cmd, &flock);
502 err = do_fcntl(fd, cmd, arg, f.file);
513 /* careful - don't use anywhere else */
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;
521 static int get_compat_flock(struct flock *kfl, const struct compat_flock __user *ufl)
523 struct compat_flock fl;
525 if (copy_from_user(&fl, ufl, sizeof(struct compat_flock)))
527 copy_flock_fields(kfl, &fl);
531 static int get_compat_flock64(struct flock *kfl, const struct compat_flock64 __user *ufl)
533 struct compat_flock64 fl;
535 if (copy_from_user(&fl, ufl, sizeof(struct compat_flock64)))
537 copy_flock_fields(kfl, &fl);
541 static int put_compat_flock(const struct flock *kfl, struct compat_flock __user *ufl)
543 struct compat_flock fl;
545 memset(&fl, 0, sizeof(struct compat_flock));
546 copy_flock_fields(&fl, kfl);
547 if (copy_to_user(ufl, &fl, sizeof(struct compat_flock)))
552 static int put_compat_flock64(const struct flock *kfl, struct compat_flock64 __user *ufl)
554 struct compat_flock64 fl;
556 BUILD_BUG_ON(sizeof(kfl->l_start) > sizeof(ufl->l_start));
557 BUILD_BUG_ON(sizeof(kfl->l_len) > sizeof(ufl->l_len));
559 memset(&fl, 0, sizeof(struct compat_flock64));
560 copy_flock_fields(&fl, kfl);
561 if (copy_to_user(ufl, &fl, sizeof(struct compat_flock64)))
565 #undef copy_flock_fields
568 convert_fcntl_cmd(unsigned int cmd)
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
591 static int fixup_compat_flock(struct flock *flock)
593 if (flock->l_start > COMPAT_OFF_T_MAX)
595 if (flock->l_len > COMPAT_OFF_T_MAX)
596 flock->l_len = COMPAT_OFF_T_MAX;
600 static long do_compat_fcntl64(unsigned int fd, unsigned int cmd,
603 struct fd f = fdget_raw(fd);
610 if (unlikely(f.file->f_mode & FMODE_PATH)) {
611 if (!check_fcntl_cmd(cmd))
615 err = security_file_fcntl(f.file, cmd, arg);
621 err = get_compat_flock(&flock, compat_ptr(arg));
624 err = fcntl_getlk(f.file, convert_fcntl_cmd(cmd), &flock);
627 err = fixup_compat_flock(&flock);
629 err = put_compat_flock(&flock, compat_ptr(arg));
633 err = get_compat_flock64(&flock, compat_ptr(arg));
636 err = fcntl_getlk(f.file, convert_fcntl_cmd(cmd), &flock);
638 err = put_compat_flock64(&flock, compat_ptr(arg));
642 err = get_compat_flock(&flock, compat_ptr(arg));
645 err = fcntl_setlk(fd, f.file, convert_fcntl_cmd(cmd), &flock);
651 err = get_compat_flock64(&flock, compat_ptr(arg));
654 err = fcntl_setlk(fd, f.file, convert_fcntl_cmd(cmd), &flock);
657 err = do_fcntl(fd, cmd, arg, f.file);
665 COMPAT_SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
668 return do_compat_fcntl64(fd, cmd, arg);
671 COMPAT_SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd,
683 return do_compat_fcntl64(fd, cmd, arg);
687 /* Table to convert sigio signal codes into poll band bitmaps */
689 static const __poll_t band_table[NSIGPOLL] = {
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 */
698 static inline int sigio_perm(struct task_struct *p,
699 struct fown_struct *fown, int sig)
701 const struct cred *cred;
705 cred = __task_cred(p);
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)) &&
709 !security_file_send_sigiotask(p, fown, sig));
714 static void send_sigio_to_task(struct task_struct *p,
715 struct fown_struct *fown,
716 int fd, int reason, enum pid_type type)
719 * F_SETSIG can change ->signum lockless in parallel, make
720 * sure we read it once and use the same value throughout.
722 int signum = READ_ONCE(fown->signum);
724 if (!sigio_perm(p, fown, signum))
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 */
738 si.si_signo = signum;
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.
749 if ((signum != SIGPOLL) && sig_specific_sicodes(signum))
750 si.si_code = SI_SIGIO;
752 /* Make sure we are called with one of the POLL_*
753 reasons, otherwise we could leak kernel stack into
755 BUG_ON((reason < POLL_IN) || ((reason - POLL_IN) >= NSIGPOLL));
756 if (reason - POLL_IN >= NSIGPOLL)
759 si.si_band = mangle_poll(band_table[reason - POLL_IN]);
761 if (!do_send_sig_info(signum, &si, p, type))
764 fallthrough; /* fall back on the old plain SIGIO signal */
766 do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, type);
770 void send_sigio(struct fown_struct *fown, int fd, int band)
772 struct task_struct *p;
777 read_lock_irqsave(&fown->lock, flags);
779 type = fown->pid_type;
782 goto out_unlock_fown;
784 if (type <= PIDTYPE_TGID) {
786 p = pid_task(pid, PIDTYPE_PID);
788 send_sigio_to_task(p, fown, fd, band, type);
791 read_lock(&tasklist_lock);
792 do_each_pid_task(pid, type, p) {
793 send_sigio_to_task(p, fown, fd, band, type);
794 } while_each_pid_task(pid, type, p);
795 read_unlock(&tasklist_lock);
798 read_unlock_irqrestore(&fown->lock, flags);
801 static void send_sigurg_to_task(struct task_struct *p,
802 struct fown_struct *fown, enum pid_type type)
804 if (sigio_perm(p, fown, SIGURG))
805 do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, type);
808 int send_sigurg(struct fown_struct *fown)
810 struct task_struct *p;
816 read_lock_irqsave(&fown->lock, flags);
818 type = fown->pid_type;
821 goto out_unlock_fown;
825 if (type <= PIDTYPE_TGID) {
827 p = pid_task(pid, PIDTYPE_PID);
829 send_sigurg_to_task(p, fown, type);
832 read_lock(&tasklist_lock);
833 do_each_pid_task(pid, type, p) {
834 send_sigurg_to_task(p, fown, type);
835 } while_each_pid_task(pid, type, p);
836 read_unlock(&tasklist_lock);
839 read_unlock_irqrestore(&fown->lock, flags);
843 static DEFINE_SPINLOCK(fasync_lock);
844 static struct kmem_cache *fasync_cache __read_mostly;
846 static void fasync_free_rcu(struct rcu_head *head)
848 kmem_cache_free(fasync_cache,
849 container_of(head, struct fasync_struct, fa_rcu));
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.
857 * NOTE! It is very important that the FASYNC flag always
858 * match the state "is the filp on a fasync list".
861 int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp)
863 struct fasync_struct *fa, **fp;
866 spin_lock(&filp->f_lock);
867 spin_lock(&fasync_lock);
868 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
869 if (fa->fa_file != filp)
872 write_lock_irq(&fa->fa_lock);
874 write_unlock_irq(&fa->fa_lock);
877 call_rcu(&fa->fa_rcu, fasync_free_rcu);
878 filp->f_flags &= ~FASYNC;
882 spin_unlock(&fasync_lock);
883 spin_unlock(&filp->f_lock);
887 struct fasync_struct *fasync_alloc(void)
889 return kmem_cache_alloc(fasync_cache, GFP_KERNEL);
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.
897 void fasync_free(struct fasync_struct *new)
899 kmem_cache_free(fasync_cache, new);
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.
906 * NOTE! It is very important that the FASYNC flag always
907 * match the state "is the filp on a fasync list".
909 struct fasync_struct *fasync_insert_entry(int fd, struct file *filp, struct fasync_struct **fapp, struct fasync_struct *new)
911 struct fasync_struct *fa, **fp;
913 spin_lock(&filp->f_lock);
914 spin_lock(&fasync_lock);
915 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
916 if (fa->fa_file != filp)
919 write_lock_irq(&fa->fa_lock);
921 write_unlock_irq(&fa->fa_lock);
925 rwlock_init(&new->fa_lock);
926 new->magic = FASYNC_MAGIC;
929 new->fa_next = *fapp;
930 rcu_assign_pointer(*fapp, new);
931 filp->f_flags |= FASYNC;
934 spin_unlock(&fasync_lock);
935 spin_unlock(&filp->f_lock);
940 * Add a fasync entry. Return negative on error, positive if
941 * added, and zero if did nothing but change an existing one.
943 static int fasync_add_entry(int fd, struct file *filp, struct fasync_struct **fapp)
945 struct fasync_struct *new;
947 new = fasync_alloc();
952 * fasync_insert_entry() returns the old (update) entry if
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.
958 if (fasync_insert_entry(fd, filp, fapp, new)) {
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.
972 int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
975 return fasync_remove_entry(filp, fapp);
976 return fasync_add_entry(fd, filp, fapp);
979 EXPORT_SYMBOL(fasync_helper);
982 * rcu_read_lock() is held
984 static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band)
987 struct fown_struct *fown;
990 if (fa->magic != FASYNC_MAGIC) {
991 printk(KERN_ERR "kill_fasync: bad magic number in "
995 read_lock_irqsave(&fa->fa_lock, flags);
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
1001 if (!(sig == SIGURG && fown->signum == 0))
1002 send_sigio(fown, fa->fa_fd, band);
1004 read_unlock_irqrestore(&fa->fa_lock, flags);
1005 fa = rcu_dereference(fa->fa_next);
1009 void kill_fasync(struct fasync_struct **fp, int sig, int band)
1011 /* First a quick test without locking: usually
1012 * the list is empty.
1016 kill_fasync_rcu(rcu_dereference(*fp), sig, band);
1020 EXPORT_SYMBOL(kill_fasync);
1022 static int __init fcntl_init(void)
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.
1029 BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ !=
1031 (VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
1032 __FMODE_EXEC | __FMODE_NONOTIFY));
1034 fasync_cache = kmem_cache_create("fasync_cache",
1035 sizeof(struct fasync_struct), 0,
1036 SLAB_PANIC | SLAB_ACCOUNT, NULL);
1040 module_init(fcntl_init)