diff options
author | Ulrich Drepper <drepper@redhat.com> | 2007-07-16 02:40:32 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@woody.linux-foundation.org> | 2007-07-16 12:05:45 -0400 |
commit | f23513e8d96cf5e6cf8d2ff0cb5dd6bbc33995e4 (patch) | |
tree | 6efce8fb88308ae4f4a65cc35a3669f32ff55248 | |
parent | 4a2d44590a603be292addce9c263982043416666 (diff) |
Introduce O_CLOEXEC
The problem is as follows: in multi-threaded code (or more correctly: all
code using clone() with CLONE_FILES) we have a race when exec'ing.
thread #1 thread #2
fd=open()
fork + exec
fcntl(fd,F_SETFD,FD_CLOEXEC)
In some applications this can happen frequently. Take a web browser. One
thread opens a file and another thread starts, say, an external PDF viewer.
The result can even be a security issue if that open file descriptor
refers to a sensitive file and the external program can somehow be tricked
into using that descriptor.
Just adding O_CLOEXEC support to open() doesn't solve the whole set of
problems. There are other ways to create file descriptors (socket,
epoll_create, Unix domain socket transfer, etc). These can and should be
addressed separately though. open() is such an easy case that it makes not
much sense putting the fix off.
The test program:
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#ifndef O_CLOEXEC
# define O_CLOEXEC 02000000
#endif
int
main (int argc, char *argv[])
{
int fd;
if (argc > 1)
{
fd = atol (argv[1]);
printf ("child: fd = %d\n", fd);
if (fcntl (fd, F_GETFD) == 0 || errno != EBADF)
{
puts ("file descriptor valid in child");
return 1;
}
return 0;
}
fd = open ("/proc/self/exe", O_RDONLY | O_CLOEXEC);
printf ("in parent: new fd = %d\n", fd);
char buf[20];
snprintf (buf, sizeof (buf), "%d", fd);
execl ("/proc/self/exe", argv[0], buf, NULL);
puts ("execl failed");
return 1;
}
[kyle@parisc-linux.org: parisc fix]
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Ingo Molnar <mingo@elte.hu>
Cc: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk-manpages@gmx.net>
Cc: Chris Zankel <chris@zankel.net>
Signed-off-by: Kyle McMartin <kyle@parisc-linux.org>
Acked-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r-- | fs/open.c | 14 | ||||
-rw-r--r-- | include/asm-generic/fcntl.h | 3 | ||||
-rw-r--r-- | include/asm-parisc/fcntl.h | 29 |
3 files changed, 29 insertions, 17 deletions
@@ -855,7 +855,7 @@ EXPORT_SYMBOL(dentry_open); | |||
855 | /* | 855 | /* |
856 | * Find an empty file descriptor entry, and mark it busy. | 856 | * Find an empty file descriptor entry, and mark it busy. |
857 | */ | 857 | */ |
858 | int get_unused_fd(void) | 858 | static int get_unused_fd_flags(int flags) |
859 | { | 859 | { |
860 | struct files_struct * files = current->files; | 860 | struct files_struct * files = current->files; |
861 | int fd, error; | 861 | int fd, error; |
@@ -891,7 +891,10 @@ repeat: | |||
891 | } | 891 | } |
892 | 892 | ||
893 | FD_SET(fd, fdt->open_fds); | 893 | FD_SET(fd, fdt->open_fds); |
894 | FD_CLR(fd, fdt->close_on_exec); | 894 | if (flags & O_CLOEXEC) |
895 | FD_SET(fd, fdt->close_on_exec); | ||
896 | else | ||
897 | FD_CLR(fd, fdt->close_on_exec); | ||
895 | files->next_fd = fd + 1; | 898 | files->next_fd = fd + 1; |
896 | #if 1 | 899 | #if 1 |
897 | /* Sanity check */ | 900 | /* Sanity check */ |
@@ -907,6 +910,11 @@ out: | |||
907 | return error; | 910 | return error; |
908 | } | 911 | } |
909 | 912 | ||
913 | int get_unused_fd(void) | ||
914 | { | ||
915 | return get_unused_fd_flags(0); | ||
916 | } | ||
917 | |||
910 | EXPORT_SYMBOL(get_unused_fd); | 918 | EXPORT_SYMBOL(get_unused_fd); |
911 | 919 | ||
912 | static void __put_unused_fd(struct files_struct *files, unsigned int fd) | 920 | static void __put_unused_fd(struct files_struct *files, unsigned int fd) |
@@ -959,7 +967,7 @@ long do_sys_open(int dfd, const char __user *filename, int flags, int mode) | |||
959 | int fd = PTR_ERR(tmp); | 967 | int fd = PTR_ERR(tmp); |
960 | 968 | ||
961 | if (!IS_ERR(tmp)) { | 969 | if (!IS_ERR(tmp)) { |
962 | fd = get_unused_fd(); | 970 | fd = get_unused_fd_flags(flags); |
963 | if (fd >= 0) { | 971 | if (fd >= 0) { |
964 | struct file *f = do_filp_open(dfd, tmp, flags, mode); | 972 | struct file *f = do_filp_open(dfd, tmp, flags, mode); |
965 | if (IS_ERR(f)) { | 973 | if (IS_ERR(f)) { |
diff --git a/include/asm-generic/fcntl.h b/include/asm-generic/fcntl.h index c154b9d6e7e5..b8477414c5c8 100644 --- a/include/asm-generic/fcntl.h +++ b/include/asm-generic/fcntl.h | |||
@@ -48,6 +48,9 @@ | |||
48 | #ifndef O_NOATIME | 48 | #ifndef O_NOATIME |
49 | #define O_NOATIME 01000000 | 49 | #define O_NOATIME 01000000 |
50 | #endif | 50 | #endif |
51 | #ifndef O_CLOEXEC | ||
52 | #define O_CLOEXEC 02000000 /* set close_on_exec */ | ||
53 | #endif | ||
51 | #ifndef O_NDELAY | 54 | #ifndef O_NDELAY |
52 | #define O_NDELAY O_NONBLOCK | 55 | #define O_NDELAY O_NONBLOCK |
53 | #endif | 56 | #endif |
diff --git a/include/asm-parisc/fcntl.h b/include/asm-parisc/fcntl.h index 317851fa78f3..1e1c824764ee 100644 --- a/include/asm-parisc/fcntl.h +++ b/include/asm-parisc/fcntl.h | |||
@@ -3,21 +3,22 @@ | |||
3 | 3 | ||
4 | /* open/fcntl - O_SYNC is only implemented on blocks devices and on files | 4 | /* open/fcntl - O_SYNC is only implemented on blocks devices and on files |
5 | located on an ext2 file system */ | 5 | located on an ext2 file system */ |
6 | #define O_APPEND 00000010 | 6 | #define O_APPEND 000000010 |
7 | #define O_BLKSEEK 00000100 /* HPUX only */ | 7 | #define O_BLKSEEK 000000100 /* HPUX only */ |
8 | #define O_CREAT 00000400 /* not fcntl */ | 8 | #define O_CREAT 000000400 /* not fcntl */ |
9 | #define O_EXCL 00002000 /* not fcntl */ | 9 | #define O_EXCL 000002000 /* not fcntl */ |
10 | #define O_LARGEFILE 00004000 | 10 | #define O_LARGEFILE 000004000 |
11 | #define O_SYNC 00100000 | 11 | #define O_SYNC 000100000 |
12 | #define O_NONBLOCK 00200004 /* HPUX has separate NDELAY & NONBLOCK */ | 12 | #define O_NONBLOCK 000200004 /* HPUX has separate NDELAY & NONBLOCK */ |
13 | #define O_NOCTTY 00400000 /* not fcntl */ | 13 | #define O_NOCTTY 000400000 /* not fcntl */ |
14 | #define O_DSYNC 01000000 /* HPUX only */ | 14 | #define O_DSYNC 001000000 /* HPUX only */ |
15 | #define O_RSYNC 02000000 /* HPUX only */ | 15 | #define O_RSYNC 002000000 /* HPUX only */ |
16 | #define O_NOATIME 04000000 | 16 | #define O_NOATIME 004000000 |
17 | #define O_CLOEXEC 010000000 /* set close_on_exec */ | ||
17 | 18 | ||
18 | #define O_DIRECTORY 00010000 /* must be a directory */ | 19 | #define O_DIRECTORY 000010000 /* must be a directory */ |
19 | #define O_NOFOLLOW 00000200 /* don't follow links */ | 20 | #define O_NOFOLLOW 000000200 /* don't follow links */ |
20 | #define O_INVISIBLE 04000000 /* invisible I/O, for DMAPI/XDSM */ | 21 | #define O_INVISIBLE 004000000 /* invisible I/O, for DMAPI/XDSM */ |
21 | 22 | ||
22 | #define F_GETLK64 8 | 23 | #define F_GETLK64 8 |
23 | #define F_SETLK64 9 | 24 | #define F_SETLK64 9 |