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) && (arg & O_DIRECT)) {
60 if (!filp->f_mapping || !filp->f_mapping->a_ops ||
61 !filp->f_mapping->a_ops->direct_IO)
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 spin_unlock(&filp->f_lock);
88 static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
91 write_lock_irq(&filp->f_owner.lock);
92 if (force || !filp->f_owner.pid) {
93 put_pid(filp->f_owner.pid);
94 filp->f_owner.pid = get_pid(pid);
95 filp->f_owner.pid_type = type;
98 const struct cred *cred = current_cred();
99 filp->f_owner.uid = cred->uid;
100 filp->f_owner.euid = cred->euid;
103 write_unlock_irq(&filp->f_owner.lock);
106 void __f_setown(struct file *filp, struct pid *pid, enum pid_type type,
109 security_file_set_fowner(filp);
110 f_modown(filp, pid, type, force);
112 EXPORT_SYMBOL(__f_setown);
114 int f_setown(struct file *filp, unsigned long arg, int force)
117 struct pid *pid = NULL;
118 int who = arg, ret = 0;
122 /* avoid overflow below */
132 pid = find_vpid(who);
138 __f_setown(filp, pid, type, force);
143 EXPORT_SYMBOL(f_setown);
145 void f_delown(struct file *filp)
147 f_modown(filp, NULL, PIDTYPE_TGID, 1);
150 pid_t f_getown(struct file *filp)
153 read_lock(&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(&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(&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(&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(&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(&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;
293 case F_GET_FILE_RW_HINT:
294 h = file_write_hint(file);
295 if (copy_to_user(argp, &h, sizeof(*argp)))
298 case F_SET_FILE_RW_HINT:
299 if (copy_from_user(&h, argp, sizeof(h)))
301 hint = (enum rw_hint) h;
302 if (!rw_hint_valid(hint))
305 spin_lock(&file->f_lock);
306 file->f_write_hint = hint;
307 spin_unlock(&file->f_lock);
310 h = inode->i_write_hint;
311 if (copy_to_user(argp, &h, sizeof(*argp)))
315 if (copy_from_user(&h, argp, sizeof(h)))
317 hint = (enum rw_hint) h;
318 if (!rw_hint_valid(hint))
322 inode->i_write_hint = hint;
330 static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
333 void __user *argp = (void __user *)arg;
339 err = f_dupfd(arg, filp, 0);
341 case F_DUPFD_CLOEXEC:
342 err = f_dupfd(arg, filp, O_CLOEXEC);
345 err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
349 set_close_on_exec(fd, arg & FD_CLOEXEC);
355 err = setfl(fd, filp, arg);
357 #if BITS_PER_LONG != 32
358 /* 32-bit arches must use fcntl64() */
362 if (copy_from_user(&flock, argp, sizeof(flock)))
364 err = fcntl_getlk(filp, cmd, &flock);
365 if (!err && copy_to_user(argp, &flock, sizeof(flock)))
368 #if BITS_PER_LONG != 32
369 /* 32-bit arches must use fcntl64() */
376 if (copy_from_user(&flock, argp, sizeof(flock)))
378 err = fcntl_setlk(fd, filp, cmd, &flock);
382 * XXX If f_owner is a process group, the
383 * negative return value will get converted
384 * into an error. Oops. If we keep the
385 * current syscall conventions, the only way
386 * to fix this will be in libc.
388 err = f_getown(filp);
389 force_successful_syscall_return();
392 err = f_setown(filp, arg, 1);
395 err = f_getown_ex(filp, arg);
398 err = f_setown_ex(filp, arg);
400 case F_GETOWNER_UIDS:
401 err = f_getowner_uids(filp, arg);
404 err = filp->f_owner.signum;
407 /* arg == 0 restores default behaviour. */
408 if (!valid_signal(arg)) {
412 filp->f_owner.signum = arg;
415 err = fcntl_getlease(filp);
418 err = fcntl_setlease(fd, filp, arg);
421 err = fcntl_dirnotify(fd, filp, arg);
425 err = pipe_fcntl(filp, cmd, arg);
429 err = memfd_fcntl(filp, cmd, arg);
433 case F_GET_FILE_RW_HINT:
434 case F_SET_FILE_RW_HINT:
435 err = fcntl_rw_hint(filp, cmd, arg);
443 static int check_fcntl_cmd(unsigned cmd)
447 case F_DUPFD_CLOEXEC:
456 SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
458 struct fd f = fdget_raw(fd);
464 if (unlikely(f.file->f_mode & FMODE_PATH)) {
465 if (!check_fcntl_cmd(cmd))
469 err = security_file_fcntl(f.file, cmd, arg);
471 err = do_fcntl(fd, cmd, arg, f.file);
479 #if BITS_PER_LONG == 32
480 SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
483 void __user *argp = (void __user *)arg;
484 struct fd f = fdget_raw(fd);
485 struct flock64 flock;
491 if (unlikely(f.file->f_mode & FMODE_PATH)) {
492 if (!check_fcntl_cmd(cmd))
496 err = security_file_fcntl(f.file, cmd, arg);
504 if (copy_from_user(&flock, argp, sizeof(flock)))
506 err = fcntl_getlk64(f.file, cmd, &flock);
507 if (!err && copy_to_user(argp, &flock, sizeof(flock)))
515 if (copy_from_user(&flock, argp, sizeof(flock)))
517 err = fcntl_setlk64(fd, f.file, cmd, &flock);
520 err = do_fcntl(fd, cmd, arg, f.file);
531 /* careful - don't use anywhere else */
532 #define copy_flock_fields(dst, src) \
533 (dst)->l_type = (src)->l_type; \
534 (dst)->l_whence = (src)->l_whence; \
535 (dst)->l_start = (src)->l_start; \
536 (dst)->l_len = (src)->l_len; \
537 (dst)->l_pid = (src)->l_pid;
539 static int get_compat_flock(struct flock *kfl, const struct compat_flock __user *ufl)
541 struct compat_flock fl;
543 if (copy_from_user(&fl, ufl, sizeof(struct compat_flock)))
545 copy_flock_fields(kfl, &fl);
549 static int get_compat_flock64(struct flock *kfl, const struct compat_flock64 __user *ufl)
551 struct compat_flock64 fl;
553 if (copy_from_user(&fl, ufl, sizeof(struct compat_flock64)))
555 copy_flock_fields(kfl, &fl);
559 static int put_compat_flock(const struct flock *kfl, struct compat_flock __user *ufl)
561 struct compat_flock fl;
563 memset(&fl, 0, sizeof(struct compat_flock));
564 copy_flock_fields(&fl, kfl);
565 if (copy_to_user(ufl, &fl, sizeof(struct compat_flock)))
570 static int put_compat_flock64(const struct flock *kfl, struct compat_flock64 __user *ufl)
572 struct compat_flock64 fl;
574 BUILD_BUG_ON(sizeof(kfl->l_start) > sizeof(ufl->l_start));
575 BUILD_BUG_ON(sizeof(kfl->l_len) > sizeof(ufl->l_len));
577 memset(&fl, 0, sizeof(struct compat_flock64));
578 copy_flock_fields(&fl, kfl);
579 if (copy_to_user(ufl, &fl, sizeof(struct compat_flock64)))
583 #undef copy_flock_fields
586 convert_fcntl_cmd(unsigned int cmd)
601 * GETLK was successful and we need to return the data, but it needs to fit in
602 * the compat structure.
603 * l_start shouldn't be too big, unless the original start + end is greater than
604 * COMPAT_OFF_T_MAX, in which case the app was asking for trouble, so we return
605 * -EOVERFLOW in that case. l_len could be too big, in which case we just
606 * truncate it, and only allow the app to see that part of the conflicting lock
607 * that might make sense to it anyway
609 static int fixup_compat_flock(struct flock *flock)
611 if (flock->l_start > COMPAT_OFF_T_MAX)
613 if (flock->l_len > COMPAT_OFF_T_MAX)
614 flock->l_len = COMPAT_OFF_T_MAX;
618 static long do_compat_fcntl64(unsigned int fd, unsigned int cmd,
621 struct fd f = fdget_raw(fd);
628 if (unlikely(f.file->f_mode & FMODE_PATH)) {
629 if (!check_fcntl_cmd(cmd))
633 err = security_file_fcntl(f.file, cmd, arg);
639 err = get_compat_flock(&flock, compat_ptr(arg));
642 err = fcntl_getlk(f.file, convert_fcntl_cmd(cmd), &flock);
645 err = fixup_compat_flock(&flock);
647 err = put_compat_flock(&flock, compat_ptr(arg));
651 err = get_compat_flock64(&flock, compat_ptr(arg));
654 err = fcntl_getlk(f.file, convert_fcntl_cmd(cmd), &flock);
656 err = put_compat_flock64(&flock, compat_ptr(arg));
660 err = get_compat_flock(&flock, compat_ptr(arg));
663 err = fcntl_setlk(fd, f.file, convert_fcntl_cmd(cmd), &flock);
669 err = get_compat_flock64(&flock, compat_ptr(arg));
672 err = fcntl_setlk(fd, f.file, convert_fcntl_cmd(cmd), &flock);
675 err = do_fcntl(fd, cmd, arg, f.file);
683 COMPAT_SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
686 return do_compat_fcntl64(fd, cmd, arg);
689 COMPAT_SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd,
701 return do_compat_fcntl64(fd, cmd, arg);
705 /* Table to convert sigio signal codes into poll band bitmaps */
707 static const __poll_t band_table[NSIGPOLL] = {
708 EPOLLIN | EPOLLRDNORM, /* POLL_IN */
709 EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND, /* POLL_OUT */
710 EPOLLIN | EPOLLRDNORM | EPOLLMSG, /* POLL_MSG */
711 EPOLLERR, /* POLL_ERR */
712 EPOLLPRI | EPOLLRDBAND, /* POLL_PRI */
713 EPOLLHUP | EPOLLERR /* POLL_HUP */
716 static inline int sigio_perm(struct task_struct *p,
717 struct fown_struct *fown, int sig)
719 const struct cred *cred;
723 cred = __task_cred(p);
724 ret = ((uid_eq(fown->euid, GLOBAL_ROOT_UID) ||
725 uid_eq(fown->euid, cred->suid) || uid_eq(fown->euid, cred->uid) ||
726 uid_eq(fown->uid, cred->suid) || uid_eq(fown->uid, cred->uid)) &&
727 !security_file_send_sigiotask(p, fown, sig));
732 static void send_sigio_to_task(struct task_struct *p,
733 struct fown_struct *fown,
734 int fd, int reason, enum pid_type type)
737 * F_SETSIG can change ->signum lockless in parallel, make
738 * sure we read it once and use the same value throughout.
740 int signum = READ_ONCE(fown->signum);
742 if (!sigio_perm(p, fown, signum))
749 /* Queue a rt signal with the appropriate fd as its
750 value. We use SI_SIGIO as the source, not
751 SI_KERNEL, since kernel signals always get
752 delivered even if we can't queue. Failure to
753 queue in this case _should_ be reported; we fall
754 back to SIGIO in that case. --sct */
756 si.si_signo = signum;
760 * Posix definies POLL_IN and friends to be signal
761 * specific si_codes for SIG_POLL. Linux extended
762 * these si_codes to other signals in a way that is
763 * ambiguous if other signals also have signal
764 * specific si_codes. In that case use SI_SIGIO instead
765 * to remove the ambiguity.
767 if ((signum != SIGPOLL) && sig_specific_sicodes(signum))
768 si.si_code = SI_SIGIO;
770 /* Make sure we are called with one of the POLL_*
771 reasons, otherwise we could leak kernel stack into
773 BUG_ON((reason < POLL_IN) || ((reason - POLL_IN) >= NSIGPOLL));
774 if (reason - POLL_IN >= NSIGPOLL)
777 si.si_band = mangle_poll(band_table[reason - POLL_IN]);
779 if (!do_send_sig_info(signum, &si, p, type))
782 fallthrough; /* fall back on the old plain SIGIO signal */
784 do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, type);
788 void send_sigio(struct fown_struct *fown, int fd, int band)
790 struct task_struct *p;
795 read_lock_irqsave(&fown->lock, flags);
797 type = fown->pid_type;
800 goto out_unlock_fown;
802 if (type <= PIDTYPE_TGID) {
804 p = pid_task(pid, PIDTYPE_PID);
806 send_sigio_to_task(p, fown, fd, band, type);
809 read_lock(&tasklist_lock);
810 do_each_pid_task(pid, type, p) {
811 send_sigio_to_task(p, fown, fd, band, type);
812 } while_each_pid_task(pid, type, p);
813 read_unlock(&tasklist_lock);
816 read_unlock_irqrestore(&fown->lock, flags);
819 static void send_sigurg_to_task(struct task_struct *p,
820 struct fown_struct *fown, enum pid_type type)
822 if (sigio_perm(p, fown, SIGURG))
823 do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, type);
826 int send_sigurg(struct fown_struct *fown)
828 struct task_struct *p;
834 read_lock_irqsave(&fown->lock, flags);
836 type = fown->pid_type;
839 goto out_unlock_fown;
843 if (type <= PIDTYPE_TGID) {
845 p = pid_task(pid, PIDTYPE_PID);
847 send_sigurg_to_task(p, fown, type);
850 read_lock(&tasklist_lock);
851 do_each_pid_task(pid, type, p) {
852 send_sigurg_to_task(p, fown, type);
853 } while_each_pid_task(pid, type, p);
854 read_unlock(&tasklist_lock);
857 read_unlock_irqrestore(&fown->lock, flags);
861 static DEFINE_SPINLOCK(fasync_lock);
862 static struct kmem_cache *fasync_cache __read_mostly;
864 static void fasync_free_rcu(struct rcu_head *head)
866 kmem_cache_free(fasync_cache,
867 container_of(head, struct fasync_struct, fa_rcu));
871 * Remove a fasync entry. If successfully removed, return
872 * positive and clear the FASYNC flag. If no entry exists,
873 * do nothing and return 0.
875 * NOTE! It is very important that the FASYNC flag always
876 * match the state "is the filp on a fasync list".
879 int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp)
881 struct fasync_struct *fa, **fp;
884 spin_lock(&filp->f_lock);
885 spin_lock(&fasync_lock);
886 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
887 if (fa->fa_file != filp)
890 write_lock_irq(&fa->fa_lock);
892 write_unlock_irq(&fa->fa_lock);
895 call_rcu(&fa->fa_rcu, fasync_free_rcu);
896 filp->f_flags &= ~FASYNC;
900 spin_unlock(&fasync_lock);
901 spin_unlock(&filp->f_lock);
905 struct fasync_struct *fasync_alloc(void)
907 return kmem_cache_alloc(fasync_cache, GFP_KERNEL);
911 * NOTE! This can be used only for unused fasync entries:
912 * entries that actually got inserted on the fasync list
913 * need to be released by rcu - see fasync_remove_entry.
915 void fasync_free(struct fasync_struct *new)
917 kmem_cache_free(fasync_cache, new);
921 * Insert a new entry into the fasync list. Return the pointer to the
922 * old one if we didn't use the new one.
924 * NOTE! It is very important that the FASYNC flag always
925 * match the state "is the filp on a fasync list".
927 struct fasync_struct *fasync_insert_entry(int fd, struct file *filp, struct fasync_struct **fapp, struct fasync_struct *new)
929 struct fasync_struct *fa, **fp;
931 spin_lock(&filp->f_lock);
932 spin_lock(&fasync_lock);
933 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
934 if (fa->fa_file != filp)
937 write_lock_irq(&fa->fa_lock);
939 write_unlock_irq(&fa->fa_lock);
943 rwlock_init(&new->fa_lock);
944 new->magic = FASYNC_MAGIC;
947 new->fa_next = *fapp;
948 rcu_assign_pointer(*fapp, new);
949 filp->f_flags |= FASYNC;
952 spin_unlock(&fasync_lock);
953 spin_unlock(&filp->f_lock);
958 * Add a fasync entry. Return negative on error, positive if
959 * added, and zero if did nothing but change an existing one.
961 static int fasync_add_entry(int fd, struct file *filp, struct fasync_struct **fapp)
963 struct fasync_struct *new;
965 new = fasync_alloc();
970 * fasync_insert_entry() returns the old (update) entry if
973 * So free the (unused) new entry and return 0 to let the
974 * caller know that we didn't add any new fasync entries.
976 if (fasync_insert_entry(fd, filp, fapp, new)) {
985 * fasync_helper() is used by almost all character device drivers
986 * to set up the fasync queue, and for regular files by the file
987 * lease code. It returns negative on error, 0 if it did no changes
988 * and positive if it added/deleted the entry.
990 int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
993 return fasync_remove_entry(filp, fapp);
994 return fasync_add_entry(fd, filp, fapp);
997 EXPORT_SYMBOL(fasync_helper);
1000 * rcu_read_lock() is held
1002 static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band)
1005 struct fown_struct *fown;
1007 if (fa->magic != FASYNC_MAGIC) {
1008 printk(KERN_ERR "kill_fasync: bad magic number in "
1009 "fasync_struct!\n");
1012 read_lock(&fa->fa_lock);
1014 fown = &fa->fa_file->f_owner;
1015 /* Don't send SIGURG to processes which have not set a
1016 queued signum: SIGURG has its own default signalling
1018 if (!(sig == SIGURG && fown->signum == 0))
1019 send_sigio(fown, fa->fa_fd, band);
1021 read_unlock(&fa->fa_lock);
1022 fa = rcu_dereference(fa->fa_next);
1026 void kill_fasync(struct fasync_struct **fp, int sig, int band)
1028 /* First a quick test without locking: usually
1029 * the list is empty.
1033 kill_fasync_rcu(rcu_dereference(*fp), sig, band);
1037 EXPORT_SYMBOL(kill_fasync);
1039 static int __init fcntl_init(void)
1042 * Please add new bits here to ensure allocation uniqueness.
1043 * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
1044 * is defined as O_NONBLOCK on some platforms and not on others.
1046 BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ !=
1048 (VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
1049 __FMODE_EXEC | __FMODE_NONOTIFY));
1051 fasync_cache = kmem_cache_create("fasync_cache",
1052 sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
1056 module_init(fcntl_init)