4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/syscalls.h>
8 #include <linux/init.h>
11 #include <linux/file.h>
12 #include <linux/fdtable.h>
13 #include <linux/capability.h>
14 #include <linux/dnotify.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/pipe_fs_i.h>
18 #include <linux/security.h>
19 #include <linux/ptrace.h>
20 #include <linux/signal.h>
21 #include <linux/rcupdate.h>
22 #include <linux/pid_namespace.h>
23 #include <linux/user_namespace.h>
26 #include <asm/siginfo.h>
27 #include <asm/uaccess.h>
29 void set_close_on_exec(unsigned int fd, int flag)
31 struct files_struct *files = current->files;
33 spin_lock(&files->file_lock);
34 fdt = files_fdtable(files);
36 __set_close_on_exec(fd, fdt);
38 __clear_close_on_exec(fd, fdt);
39 spin_unlock(&files->file_lock);
42 static bool get_close_on_exec(unsigned int fd)
44 struct files_struct *files = current->files;
48 fdt = files_fdtable(files);
49 res = close_on_exec(fd, fdt);
54 SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags)
57 struct file * file, *tofree;
58 struct files_struct * files = current->files;
61 if ((flags & ~O_CLOEXEC) != 0)
64 if (unlikely(oldfd == newfd))
67 if (newfd >= rlimit(RLIMIT_NOFILE))
70 spin_lock(&files->file_lock);
71 err = expand_files(files, newfd);
75 if (unlikely(err < 0)) {
81 * We need to detect attempts to do dup2() over allocated but still
82 * not finished descriptor. NB: OpenBSD avoids that at the price of
83 * extra work in their equivalent of fget() - they insert struct
84 * file immediately after grabbing descriptor, mark it larval if
85 * more work (e.g. actual opening) is needed and make sure that
86 * fget() treats larval files as absent. Potentially interesting,
87 * but while extra work in fget() is trivial, locking implications
88 * and amount of surgery on open()-related paths in VFS are not.
89 * FreeBSD fails with -EBADF in the same situation, NetBSD "solution"
90 * deadlocks in rather amusing ways, AFAICS. All of that is out of
91 * scope of POSIX or SUS, since neither considers shared descriptor
92 * tables and this condition does not arise without those.
95 fdt = files_fdtable(files);
96 tofree = fdt->fd[newfd];
97 if (!tofree && fd_is_open(newfd, fdt))
100 rcu_assign_pointer(fdt->fd[newfd], file);
101 __set_open_fd(newfd, fdt);
102 if (flags & O_CLOEXEC)
103 __set_close_on_exec(newfd, fdt);
105 __clear_close_on_exec(newfd, fdt);
106 spin_unlock(&files->file_lock);
109 filp_close(tofree, files);
116 spin_unlock(&files->file_lock);
120 SYSCALL_DEFINE2(dup2, unsigned int, oldfd, unsigned int, newfd)
122 if (unlikely(newfd == oldfd)) { /* corner case */
123 struct files_struct *files = current->files;
127 if (!fcheck_files(files, oldfd))
132 return sys_dup3(oldfd, newfd, 0);
135 SYSCALL_DEFINE1(dup, unsigned int, fildes)
138 struct file *file = fget_raw(fildes);
141 ret = get_unused_fd();
143 fd_install(ret, file);
150 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
152 static int setfl(int fd, struct file * filp, unsigned long arg)
154 struct inode * inode = filp->f_path.dentry->d_inode;
158 * O_APPEND cannot be cleared if the file is marked as append-only
159 * and the file is open for write.
161 if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode))
164 /* O_NOATIME can only be set by the owner or superuser */
165 if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
166 if (!inode_owner_or_capable(inode))
169 /* required for strict SunOS emulation */
170 if (O_NONBLOCK != O_NDELAY)
174 if (arg & O_DIRECT) {
175 if (!filp->f_mapping || !filp->f_mapping->a_ops ||
176 !filp->f_mapping->a_ops->direct_IO)
180 if (filp->f_op && filp->f_op->check_flags)
181 error = filp->f_op->check_flags(arg);
186 * ->fasync() is responsible for setting the FASYNC bit.
188 if (((arg ^ filp->f_flags) & FASYNC) && filp->f_op &&
189 filp->f_op->fasync) {
190 error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
196 spin_lock(&filp->f_lock);
197 filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
198 spin_unlock(&filp->f_lock);
204 static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
207 write_lock_irq(&filp->f_owner.lock);
208 if (force || !filp->f_owner.pid) {
209 put_pid(filp->f_owner.pid);
210 filp->f_owner.pid = get_pid(pid);
211 filp->f_owner.pid_type = type;
214 const struct cred *cred = current_cred();
215 filp->f_owner.uid = cred->uid;
216 filp->f_owner.euid = cred->euid;
219 write_unlock_irq(&filp->f_owner.lock);
222 int __f_setown(struct file *filp, struct pid *pid, enum pid_type type,
227 err = security_file_set_fowner(filp);
231 f_modown(filp, pid, type, force);
234 EXPORT_SYMBOL(__f_setown);
236 int f_setown(struct file *filp, unsigned long arg, int force)
248 pid = find_vpid(who);
249 result = __f_setown(filp, pid, type, force);
253 EXPORT_SYMBOL(f_setown);
255 void f_delown(struct file *filp)
257 f_modown(filp, NULL, PIDTYPE_PID, 1);
260 pid_t f_getown(struct file *filp)
263 read_lock(&filp->f_owner.lock);
264 pid = pid_vnr(filp->f_owner.pid);
265 if (filp->f_owner.pid_type == PIDTYPE_PGID)
267 read_unlock(&filp->f_owner.lock);
271 static int f_setown_ex(struct file *filp, unsigned long arg)
273 struct f_owner_ex * __user owner_p = (void * __user)arg;
274 struct f_owner_ex owner;
279 ret = copy_from_user(&owner, owner_p, sizeof(owner));
283 switch (owner.type) {
301 pid = find_vpid(owner.pid);
302 if (owner.pid && !pid)
305 ret = __f_setown(filp, pid, type, 1);
311 static int f_getown_ex(struct file *filp, unsigned long arg)
313 struct f_owner_ex * __user owner_p = (void * __user)arg;
314 struct f_owner_ex owner;
317 read_lock(&filp->f_owner.lock);
318 owner.pid = pid_vnr(filp->f_owner.pid);
319 switch (filp->f_owner.pid_type) {
321 owner.type = F_OWNER_TID;
325 owner.type = F_OWNER_PID;
329 owner.type = F_OWNER_PGRP;
337 read_unlock(&filp->f_owner.lock);
340 ret = copy_to_user(owner_p, &owner, sizeof(owner));
347 #ifdef CONFIG_CHECKPOINT_RESTORE
348 static int f_getowner_uids(struct file *filp, unsigned long arg)
350 struct user_namespace *user_ns = current_user_ns();
351 uid_t * __user dst = (void * __user)arg;
355 read_lock(&filp->f_owner.lock);
356 src[0] = from_kuid(user_ns, filp->f_owner.uid);
357 src[1] = from_kuid(user_ns, filp->f_owner.euid);
358 read_unlock(&filp->f_owner.lock);
360 err = put_user(src[0], &dst[0]);
361 err |= put_user(src[1], &dst[1]);
366 static int f_getowner_uids(struct file *filp, unsigned long arg)
372 static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
379 case F_DUPFD_CLOEXEC:
380 if (arg >= rlimit(RLIMIT_NOFILE))
382 err = alloc_fd(arg, cmd == F_DUPFD_CLOEXEC ? O_CLOEXEC : 0);
385 fd_install(err, filp);
389 err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
393 set_close_on_exec(fd, arg & FD_CLOEXEC);
399 err = setfl(fd, filp, arg);
402 err = fcntl_getlk(filp, (struct flock __user *) arg);
406 err = fcntl_setlk(fd, filp, cmd, (struct flock __user *) arg);
410 * XXX If f_owner is a process group, the
411 * negative return value will get converted
412 * into an error. Oops. If we keep the
413 * current syscall conventions, the only way
414 * to fix this will be in libc.
416 err = f_getown(filp);
417 force_successful_syscall_return();
420 err = f_setown(filp, arg, 1);
423 err = f_getown_ex(filp, arg);
426 err = f_setown_ex(filp, arg);
428 case F_GETOWNER_UIDS:
429 err = f_getowner_uids(filp, arg);
432 err = filp->f_owner.signum;
435 /* arg == 0 restores default behaviour. */
436 if (!valid_signal(arg)) {
440 filp->f_owner.signum = arg;
443 err = fcntl_getlease(filp);
446 err = fcntl_setlease(fd, filp, arg);
449 err = fcntl_dirnotify(fd, filp, arg);
453 err = pipe_fcntl(filp, cmd, arg);
461 static int check_fcntl_cmd(unsigned cmd)
465 case F_DUPFD_CLOEXEC:
474 SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
480 filp = fget_raw_light(fd, &fput_needed);
484 if (unlikely(filp->f_mode & FMODE_PATH)) {
485 if (!check_fcntl_cmd(cmd))
489 err = security_file_fcntl(filp, cmd, arg);
491 err = do_fcntl(fd, cmd, arg, filp);
494 fput_light(filp, fput_needed);
499 #if BITS_PER_LONG == 32
500 SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
507 filp = fget_raw_light(fd, &fput_needed);
511 if (unlikely(filp->f_mode & FMODE_PATH)) {
512 if (!check_fcntl_cmd(cmd))
516 err = security_file_fcntl(filp, cmd, arg);
522 err = fcntl_getlk64(filp, (struct flock64 __user *) arg);
526 err = fcntl_setlk64(fd, filp, cmd,
527 (struct flock64 __user *) arg);
530 err = do_fcntl(fd, cmd, arg, filp);
534 fput_light(filp, fput_needed);
540 /* Table to convert sigio signal codes into poll band bitmaps */
542 static const long band_table[NSIGPOLL] = {
543 POLLIN | POLLRDNORM, /* POLL_IN */
544 POLLOUT | POLLWRNORM | POLLWRBAND, /* POLL_OUT */
545 POLLIN | POLLRDNORM | POLLMSG, /* POLL_MSG */
546 POLLERR, /* POLL_ERR */
547 POLLPRI | POLLRDBAND, /* POLL_PRI */
548 POLLHUP | POLLERR /* POLL_HUP */
551 static inline int sigio_perm(struct task_struct *p,
552 struct fown_struct *fown, int sig)
554 const struct cred *cred;
558 cred = __task_cred(p);
559 ret = ((uid_eq(fown->euid, GLOBAL_ROOT_UID) ||
560 uid_eq(fown->euid, cred->suid) || uid_eq(fown->euid, cred->uid) ||
561 uid_eq(fown->uid, cred->suid) || uid_eq(fown->uid, cred->uid)) &&
562 !security_file_send_sigiotask(p, fown, sig));
567 static void send_sigio_to_task(struct task_struct *p,
568 struct fown_struct *fown,
569 int fd, int reason, int group)
572 * F_SETSIG can change ->signum lockless in parallel, make
573 * sure we read it once and use the same value throughout.
575 int signum = ACCESS_ONCE(fown->signum);
577 if (!sigio_perm(p, fown, signum))
583 /* Queue a rt signal with the appropriate fd as its
584 value. We use SI_SIGIO as the source, not
585 SI_KERNEL, since kernel signals always get
586 delivered even if we can't queue. Failure to
587 queue in this case _should_ be reported; we fall
588 back to SIGIO in that case. --sct */
589 si.si_signo = signum;
592 /* Make sure we are called with one of the POLL_*
593 reasons, otherwise we could leak kernel stack into
595 BUG_ON((reason & __SI_MASK) != __SI_POLL);
596 if (reason - POLL_IN >= NSIGPOLL)
599 si.si_band = band_table[reason - POLL_IN];
601 if (!do_send_sig_info(signum, &si, p, group))
603 /* fall-through: fall back on the old plain SIGIO signal */
605 do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, group);
609 void send_sigio(struct fown_struct *fown, int fd, int band)
611 struct task_struct *p;
616 read_lock(&fown->lock);
618 type = fown->pid_type;
619 if (type == PIDTYPE_MAX) {
626 goto out_unlock_fown;
628 read_lock(&tasklist_lock);
629 do_each_pid_task(pid, type, p) {
630 send_sigio_to_task(p, fown, fd, band, group);
631 } while_each_pid_task(pid, type, p);
632 read_unlock(&tasklist_lock);
634 read_unlock(&fown->lock);
637 static void send_sigurg_to_task(struct task_struct *p,
638 struct fown_struct *fown, int group)
640 if (sigio_perm(p, fown, SIGURG))
641 do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, group);
644 int send_sigurg(struct fown_struct *fown)
646 struct task_struct *p;
652 read_lock(&fown->lock);
654 type = fown->pid_type;
655 if (type == PIDTYPE_MAX) {
662 goto out_unlock_fown;
666 read_lock(&tasklist_lock);
667 do_each_pid_task(pid, type, p) {
668 send_sigurg_to_task(p, fown, group);
669 } while_each_pid_task(pid, type, p);
670 read_unlock(&tasklist_lock);
672 read_unlock(&fown->lock);
676 static DEFINE_SPINLOCK(fasync_lock);
677 static struct kmem_cache *fasync_cache __read_mostly;
679 static void fasync_free_rcu(struct rcu_head *head)
681 kmem_cache_free(fasync_cache,
682 container_of(head, struct fasync_struct, fa_rcu));
686 * Remove a fasync entry. If successfully removed, return
687 * positive and clear the FASYNC flag. If no entry exists,
688 * do nothing and return 0.
690 * NOTE! It is very important that the FASYNC flag always
691 * match the state "is the filp on a fasync list".
694 int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp)
696 struct fasync_struct *fa, **fp;
699 spin_lock(&filp->f_lock);
700 spin_lock(&fasync_lock);
701 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
702 if (fa->fa_file != filp)
705 spin_lock_irq(&fa->fa_lock);
707 spin_unlock_irq(&fa->fa_lock);
710 call_rcu(&fa->fa_rcu, fasync_free_rcu);
711 filp->f_flags &= ~FASYNC;
715 spin_unlock(&fasync_lock);
716 spin_unlock(&filp->f_lock);
720 struct fasync_struct *fasync_alloc(void)
722 return kmem_cache_alloc(fasync_cache, GFP_KERNEL);
726 * NOTE! This can be used only for unused fasync entries:
727 * entries that actually got inserted on the fasync list
728 * need to be released by rcu - see fasync_remove_entry.
730 void fasync_free(struct fasync_struct *new)
732 kmem_cache_free(fasync_cache, new);
736 * Insert a new entry into the fasync list. Return the pointer to the
737 * old one if we didn't use the new one.
739 * NOTE! It is very important that the FASYNC flag always
740 * match the state "is the filp on a fasync list".
742 struct fasync_struct *fasync_insert_entry(int fd, struct file *filp, struct fasync_struct **fapp, struct fasync_struct *new)
744 struct fasync_struct *fa, **fp;
746 spin_lock(&filp->f_lock);
747 spin_lock(&fasync_lock);
748 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
749 if (fa->fa_file != filp)
752 spin_lock_irq(&fa->fa_lock);
754 spin_unlock_irq(&fa->fa_lock);
758 spin_lock_init(&new->fa_lock);
759 new->magic = FASYNC_MAGIC;
762 new->fa_next = *fapp;
763 rcu_assign_pointer(*fapp, new);
764 filp->f_flags |= FASYNC;
767 spin_unlock(&fasync_lock);
768 spin_unlock(&filp->f_lock);
773 * Add a fasync entry. Return negative on error, positive if
774 * added, and zero if did nothing but change an existing one.
776 static int fasync_add_entry(int fd, struct file *filp, struct fasync_struct **fapp)
778 struct fasync_struct *new;
780 new = fasync_alloc();
785 * fasync_insert_entry() returns the old (update) entry if
788 * So free the (unused) new entry and return 0 to let the
789 * caller know that we didn't add any new fasync entries.
791 if (fasync_insert_entry(fd, filp, fapp, new)) {
800 * fasync_helper() is used by almost all character device drivers
801 * to set up the fasync queue, and for regular files by the file
802 * lease code. It returns negative on error, 0 if it did no changes
803 * and positive if it added/deleted the entry.
805 int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
808 return fasync_remove_entry(filp, fapp);
809 return fasync_add_entry(fd, filp, fapp);
812 EXPORT_SYMBOL(fasync_helper);
815 * rcu_read_lock() is held
817 static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band)
820 struct fown_struct *fown;
823 if (fa->magic != FASYNC_MAGIC) {
824 printk(KERN_ERR "kill_fasync: bad magic number in "
828 spin_lock_irqsave(&fa->fa_lock, flags);
830 fown = &fa->fa_file->f_owner;
831 /* Don't send SIGURG to processes which have not set a
832 queued signum: SIGURG has its own default signalling
834 if (!(sig == SIGURG && fown->signum == 0))
835 send_sigio(fown, fa->fa_fd, band);
837 spin_unlock_irqrestore(&fa->fa_lock, flags);
838 fa = rcu_dereference(fa->fa_next);
842 void kill_fasync(struct fasync_struct **fp, int sig, int band)
844 /* First a quick test without locking: usually
849 kill_fasync_rcu(rcu_dereference(*fp), sig, band);
853 EXPORT_SYMBOL(kill_fasync);
855 static int __init fcntl_init(void)
858 * Please add new bits here to ensure allocation uniqueness.
859 * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
860 * is defined as O_NONBLOCK on some platforms and not on others.
862 BUILD_BUG_ON(19 - 1 /* for O_RDONLY being 0 */ != HWEIGHT32(
863 O_RDONLY | O_WRONLY | O_RDWR |
864 O_CREAT | O_EXCL | O_NOCTTY |
865 O_TRUNC | O_APPEND | /* O_NONBLOCK | */
866 __O_SYNC | O_DSYNC | FASYNC |
867 O_DIRECT | O_LARGEFILE | O_DIRECTORY |
868 O_NOFOLLOW | O_NOATIME | O_CLOEXEC |
869 __FMODE_EXEC | O_PATH
872 fasync_cache = kmem_cache_create("fasync_cache",
873 sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
877 module_init(fcntl_init)