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/security.h>
18 #include <linux/ptrace.h>
19 #include <linux/signal.h>
20 #include <linux/rcupdate.h>
21 #include <linux/pid_namespace.h>
24 #include <asm/siginfo.h>
25 #include <asm/uaccess.h>
27 void set_close_on_exec(unsigned int fd
, int flag
)
29 struct files_struct
*files
= current
->files
;
31 spin_lock(&files
->file_lock
);
32 fdt
= files_fdtable(files
);
34 FD_SET(fd
, fdt
->close_on_exec
);
36 FD_CLR(fd
, fdt
->close_on_exec
);
37 spin_unlock(&files
->file_lock
);
40 static int get_close_on_exec(unsigned int fd
)
42 struct files_struct
*files
= current
->files
;
46 fdt
= files_fdtable(files
);
47 res
= FD_ISSET(fd
, fdt
->close_on_exec
);
52 SYSCALL_DEFINE3(dup3
, unsigned int, oldfd
, unsigned int, newfd
, int, flags
)
55 struct file
* file
, *tofree
;
56 struct files_struct
* files
= current
->files
;
59 if ((flags
& ~O_CLOEXEC
) != 0)
62 if (unlikely(oldfd
== newfd
))
65 spin_lock(&files
->file_lock
);
66 err
= expand_files(files
, newfd
);
70 if (unlikely(err
< 0)) {
76 * We need to detect attempts to do dup2() over allocated but still
77 * not finished descriptor. NB: OpenBSD avoids that at the price of
78 * extra work in their equivalent of fget() - they insert struct
79 * file immediately after grabbing descriptor, mark it larval if
80 * more work (e.g. actual opening) is needed and make sure that
81 * fget() treats larval files as absent. Potentially interesting,
82 * but while extra work in fget() is trivial, locking implications
83 * and amount of surgery on open()-related paths in VFS are not.
84 * FreeBSD fails with -EBADF in the same situation, NetBSD "solution"
85 * deadlocks in rather amusing ways, AFAICS. All of that is out of
86 * scope of POSIX or SUS, since neither considers shared descriptor
87 * tables and this condition does not arise without those.
90 fdt
= files_fdtable(files
);
91 tofree
= fdt
->fd
[newfd
];
92 if (!tofree
&& FD_ISSET(newfd
, fdt
->open_fds
))
95 rcu_assign_pointer(fdt
->fd
[newfd
], file
);
96 FD_SET(newfd
, fdt
->open_fds
);
97 if (flags
& O_CLOEXEC
)
98 FD_SET(newfd
, fdt
->close_on_exec
);
100 FD_CLR(newfd
, fdt
->close_on_exec
);
101 spin_unlock(&files
->file_lock
);
104 filp_close(tofree
, files
);
111 spin_unlock(&files
->file_lock
);
115 SYSCALL_DEFINE2(dup2
, unsigned int, oldfd
, unsigned int, newfd
)
117 if (unlikely(newfd
== oldfd
)) { /* corner case */
118 struct files_struct
*files
= current
->files
;
122 if (!fcheck_files(files
, oldfd
))
127 return sys_dup3(oldfd
, newfd
, 0);
130 SYSCALL_DEFINE1(dup
, unsigned int, fildes
)
133 struct file
*file
= fget(fildes
);
136 ret
= get_unused_fd();
138 fd_install(ret
, file
);
145 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
147 static int setfl(int fd
, struct file
* filp
, unsigned long arg
)
149 struct inode
* inode
= filp
->f_path
.dentry
->d_inode
;
153 * O_APPEND cannot be cleared if the file is marked as append-only
154 * and the file is open for write.
156 if (((arg
^ filp
->f_flags
) & O_APPEND
) && IS_APPEND(inode
))
159 /* O_NOATIME can only be set by the owner or superuser */
160 if ((arg
& O_NOATIME
) && !(filp
->f_flags
& O_NOATIME
))
161 if (!is_owner_or_cap(inode
))
164 /* required for strict SunOS emulation */
165 if (O_NONBLOCK
!= O_NDELAY
)
169 if (arg
& O_DIRECT
) {
170 if (!filp
->f_mapping
|| !filp
->f_mapping
->a_ops
||
171 !filp
->f_mapping
->a_ops
->direct_IO
)
175 if (filp
->f_op
&& filp
->f_op
->check_flags
)
176 error
= filp
->f_op
->check_flags(arg
);
181 * ->fasync() is responsible for setting the FASYNC bit.
183 if (((arg
^ filp
->f_flags
) & FASYNC
) && filp
->f_op
&&
184 filp
->f_op
->fasync
) {
185 error
= filp
->f_op
->fasync(fd
, filp
, (arg
& FASYNC
) != 0);
191 spin_lock(&filp
->f_lock
);
192 filp
->f_flags
= (arg
& SETFL_MASK
) | (filp
->f_flags
& ~SETFL_MASK
);
193 spin_unlock(&filp
->f_lock
);
199 static void f_modown(struct file
*filp
, struct pid
*pid
, enum pid_type type
,
204 write_lock_irqsave(&filp
->f_owner
.lock
, flags
);
205 if (force
|| !filp
->f_owner
.pid
) {
206 put_pid(filp
->f_owner
.pid
);
207 filp
->f_owner
.pid
= get_pid(pid
);
208 filp
->f_owner
.pid_type
= type
;
211 const struct cred
*cred
= current_cred();
212 filp
->f_owner
.uid
= cred
->uid
;
213 filp
->f_owner
.euid
= cred
->euid
;
216 write_unlock_irqrestore(&filp
->f_owner
.lock
, flags
);
219 int __f_setown(struct file
*filp
, struct pid
*pid
, enum pid_type type
,
224 err
= security_file_set_fowner(filp
);
228 f_modown(filp
, pid
, type
, force
);
231 EXPORT_SYMBOL(__f_setown
);
233 int f_setown(struct file
*filp
, unsigned long arg
, int force
)
245 pid
= find_vpid(who
);
246 result
= __f_setown(filp
, pid
, type
, force
);
250 EXPORT_SYMBOL(f_setown
);
252 void f_delown(struct file
*filp
)
254 f_modown(filp
, NULL
, PIDTYPE_PID
, 1);
257 pid_t
f_getown(struct file
*filp
)
260 read_lock(&filp
->f_owner
.lock
);
261 pid
= pid_vnr(filp
->f_owner
.pid
);
262 if (filp
->f_owner
.pid_type
== PIDTYPE_PGID
)
264 read_unlock(&filp
->f_owner
.lock
);
268 static int f_setown_ex(struct file
*filp
, unsigned long arg
)
270 struct f_owner_ex
* __user owner_p
= (void * __user
)arg
;
271 struct f_owner_ex owner
;
276 ret
= copy_from_user(&owner
, owner_p
, sizeof(owner
));
280 switch (owner
.type
) {
298 pid
= find_vpid(owner
.pid
);
299 if (owner
.pid
&& !pid
)
302 ret
= __f_setown(filp
, pid
, type
, 1);
308 static int f_getown_ex(struct file
*filp
, unsigned long arg
)
310 struct f_owner_ex
* __user owner_p
= (void * __user
)arg
;
311 struct f_owner_ex owner
;
314 read_lock(&filp
->f_owner
.lock
);
315 owner
.pid
= pid_vnr(filp
->f_owner
.pid
);
316 switch (filp
->f_owner
.pid_type
) {
318 owner
.type
= F_OWNER_TID
;
322 owner
.type
= F_OWNER_PID
;
326 owner
.type
= F_OWNER_PGRP
;
334 read_unlock(&filp
->f_owner
.lock
);
337 ret
= copy_to_user(owner_p
, &owner
, sizeof(owner
));
341 static long do_fcntl(int fd
, unsigned int cmd
, unsigned long arg
,
348 case F_DUPFD_CLOEXEC
:
349 if (arg
>= current
->signal
->rlim
[RLIMIT_NOFILE
].rlim_cur
)
351 err
= alloc_fd(arg
, cmd
== F_DUPFD_CLOEXEC
? O_CLOEXEC
: 0);
354 fd_install(err
, filp
);
358 err
= get_close_on_exec(fd
) ? FD_CLOEXEC
: 0;
362 set_close_on_exec(fd
, arg
& FD_CLOEXEC
);
368 err
= setfl(fd
, filp
, arg
);
371 err
= fcntl_getlk(filp
, (struct flock __user
*) arg
);
375 err
= fcntl_setlk(fd
, filp
, cmd
, (struct flock __user
*) arg
);
379 * XXX If f_owner is a process group, the
380 * negative return value will get converted
381 * into an error. Oops. If we keep the
382 * current syscall conventions, the only way
383 * to fix this will be in libc.
385 err
= f_getown(filp
);
386 force_successful_syscall_return();
389 err
= f_setown(filp
, arg
, 1);
392 err
= f_getown_ex(filp
, arg
);
395 err
= f_setown_ex(filp
, arg
);
398 err
= filp
->f_owner
.signum
;
401 /* arg == 0 restores default behaviour. */
402 if (!valid_signal(arg
)) {
406 filp
->f_owner
.signum
= arg
;
409 err
= fcntl_getlease(filp
);
412 err
= fcntl_setlease(fd
, filp
, arg
);
415 err
= fcntl_dirnotify(fd
, filp
, arg
);
423 SYSCALL_DEFINE3(fcntl
, unsigned int, fd
, unsigned int, cmd
, unsigned long, arg
)
432 err
= security_file_fcntl(filp
, cmd
, arg
);
438 err
= do_fcntl(fd
, cmd
, arg
, filp
);
445 #if BITS_PER_LONG == 32
446 SYSCALL_DEFINE3(fcntl64
, unsigned int, fd
, unsigned int, cmd
,
457 err
= security_file_fcntl(filp
, cmd
, arg
);
466 err
= fcntl_getlk64(filp
, (struct flock64 __user
*) arg
);
470 err
= fcntl_setlk64(fd
, filp
, cmd
,
471 (struct flock64 __user
*) arg
);
474 err
= do_fcntl(fd
, cmd
, arg
, filp
);
483 /* Table to convert sigio signal codes into poll band bitmaps */
485 static const long band_table
[NSIGPOLL
] = {
486 POLLIN
| POLLRDNORM
, /* POLL_IN */
487 POLLOUT
| POLLWRNORM
| POLLWRBAND
, /* POLL_OUT */
488 POLLIN
| POLLRDNORM
| POLLMSG
, /* POLL_MSG */
489 POLLERR
, /* POLL_ERR */
490 POLLPRI
| POLLRDBAND
, /* POLL_PRI */
491 POLLHUP
| POLLERR
/* POLL_HUP */
494 static inline int sigio_perm(struct task_struct
*p
,
495 struct fown_struct
*fown
, int sig
)
497 const struct cred
*cred
;
501 cred
= __task_cred(p
);
502 ret
= ((fown
->euid
== 0 ||
503 fown
->euid
== cred
->suid
|| fown
->euid
== cred
->uid
||
504 fown
->uid
== cred
->suid
|| fown
->uid
== cred
->uid
) &&
505 !security_file_send_sigiotask(p
, fown
, sig
));
510 static void send_sigio_to_task(struct task_struct
*p
,
511 struct fown_struct
*fown
,
512 int fd
, int reason
, int group
)
515 * F_SETSIG can change ->signum lockless in parallel, make
516 * sure we read it once and use the same value throughout.
518 int signum
= ACCESS_ONCE(fown
->signum
);
520 if (!sigio_perm(p
, fown
, signum
))
526 /* Queue a rt signal with the appropriate fd as its
527 value. We use SI_SIGIO as the source, not
528 SI_KERNEL, since kernel signals always get
529 delivered even if we can't queue. Failure to
530 queue in this case _should_ be reported; we fall
531 back to SIGIO in that case. --sct */
532 si
.si_signo
= signum
;
535 /* Make sure we are called with one of the POLL_*
536 reasons, otherwise we could leak kernel stack into
538 BUG_ON((reason
& __SI_MASK
) != __SI_POLL
);
539 if (reason
- POLL_IN
>= NSIGPOLL
)
542 si
.si_band
= band_table
[reason
- POLL_IN
];
544 if (!do_send_sig_info(signum
, &si
, p
, group
))
546 /* fall-through: fall back on the old plain SIGIO signal */
548 do_send_sig_info(SIGIO
, SEND_SIG_PRIV
, p
, group
);
552 void send_sigio(struct fown_struct
*fown
, int fd
, int band
)
554 struct task_struct
*p
;
559 read_lock(&fown
->lock
);
561 type
= fown
->pid_type
;
562 if (type
== PIDTYPE_MAX
) {
569 goto out_unlock_fown
;
571 read_lock(&tasklist_lock
);
572 do_each_pid_task(pid
, type
, p
) {
573 send_sigio_to_task(p
, fown
, fd
, band
, group
);
574 } while_each_pid_task(pid
, type
, p
);
575 read_unlock(&tasklist_lock
);
577 read_unlock(&fown
->lock
);
580 static void send_sigurg_to_task(struct task_struct
*p
,
581 struct fown_struct
*fown
, int group
)
583 if (sigio_perm(p
, fown
, SIGURG
))
584 do_send_sig_info(SIGURG
, SEND_SIG_PRIV
, p
, group
);
587 int send_sigurg(struct fown_struct
*fown
)
589 struct task_struct
*p
;
595 read_lock(&fown
->lock
);
597 type
= fown
->pid_type
;
598 if (type
== PIDTYPE_MAX
) {
605 goto out_unlock_fown
;
609 read_lock(&tasklist_lock
);
610 do_each_pid_task(pid
, type
, p
) {
611 send_sigurg_to_task(p
, fown
, group
);
612 } while_each_pid_task(pid
, type
, p
);
613 read_unlock(&tasklist_lock
);
615 read_unlock(&fown
->lock
);
619 static DEFINE_RWLOCK(fasync_lock
);
620 static struct kmem_cache
*fasync_cache __read_mostly
;
623 * Remove a fasync entry. If successfully removed, return
624 * positive and clear the FASYNC flag. If no entry exists,
625 * do nothing and return 0.
627 * NOTE! It is very important that the FASYNC flag always
628 * match the state "is the filp on a fasync list".
630 * We always take the 'filp->f_lock', in since fasync_lock
631 * needs to be irq-safe.
633 static int fasync_remove_entry(struct file
*filp
, struct fasync_struct
**fapp
)
635 struct fasync_struct
*fa
, **fp
;
638 spin_lock(&filp
->f_lock
);
639 write_lock_irq(&fasync_lock
);
640 for (fp
= fapp
; (fa
= *fp
) != NULL
; fp
= &fa
->fa_next
) {
641 if (fa
->fa_file
!= filp
)
644 kmem_cache_free(fasync_cache
, fa
);
645 filp
->f_flags
&= ~FASYNC
;
649 write_unlock_irq(&fasync_lock
);
650 spin_unlock(&filp
->f_lock
);
655 * Add a fasync entry. Return negative on error, positive if
656 * added, and zero if did nothing but change an existing one.
658 * NOTE! It is very important that the FASYNC flag always
659 * match the state "is the filp on a fasync list".
661 static int fasync_add_entry(int fd
, struct file
*filp
, struct fasync_struct
**fapp
)
663 struct fasync_struct
*new, *fa
, **fp
;
666 new = kmem_cache_alloc(fasync_cache
, GFP_KERNEL
);
670 spin_lock(&filp
->f_lock
);
671 write_lock_irq(&fasync_lock
);
672 for (fp
= fapp
; (fa
= *fp
) != NULL
; fp
= &fa
->fa_next
) {
673 if (fa
->fa_file
!= filp
)
676 kmem_cache_free(fasync_cache
, new);
680 new->magic
= FASYNC_MAGIC
;
683 new->fa_next
= *fapp
;
686 filp
->f_flags
|= FASYNC
;
689 write_unlock_irq(&fasync_lock
);
690 spin_unlock(&filp
->f_lock
);
695 * fasync_helper() is used by almost all character device drivers
696 * to set up the fasync queue, and for regular files by the file
697 * lease code. It returns negative on error, 0 if it did no changes
698 * and positive if it added/deleted the entry.
700 int fasync_helper(int fd
, struct file
* filp
, int on
, struct fasync_struct
**fapp
)
703 return fasync_remove_entry(filp
, fapp
);
704 return fasync_add_entry(fd
, filp
, fapp
);
707 EXPORT_SYMBOL(fasync_helper
);
709 void __kill_fasync(struct fasync_struct
*fa
, int sig
, int band
)
712 struct fown_struct
* fown
;
713 if (fa
->magic
!= FASYNC_MAGIC
) {
714 printk(KERN_ERR
"kill_fasync: bad magic number in "
718 fown
= &fa
->fa_file
->f_owner
;
719 /* Don't send SIGURG to processes which have not set a
720 queued signum: SIGURG has its own default signalling
722 if (!(sig
== SIGURG
&& fown
->signum
== 0))
723 send_sigio(fown
, fa
->fa_fd
, band
);
728 EXPORT_SYMBOL(__kill_fasync
);
730 void kill_fasync(struct fasync_struct
**fp
, int sig
, int band
)
732 /* First a quick test without locking: usually
736 read_lock(&fasync_lock
);
737 /* reread *fp after obtaining the lock */
738 __kill_fasync(*fp
, sig
, band
);
739 read_unlock(&fasync_lock
);
742 EXPORT_SYMBOL(kill_fasync
);
744 static int __init
fasync_init(void)
746 fasync_cache
= kmem_cache_create("fasync_cache",
747 sizeof(struct fasync_struct
), 0, SLAB_PANIC
, NULL
);
751 module_init(fasync_init
)