diff options
| author | Ulrich Drepper <[email protected]> | 2007-07-15 23:40:32 -0700 |
|---|---|---|
| committer | Linus Torvalds <[email protected]> | 2007-07-16 09:05:45 -0700 |
| commit | f23513e8d96cf5e6cf8d2ff0cb5dd6bbc33995e4 (patch) | |
| tree | 6efce8fb88308ae4f4a65cc35a3669f32ff55248 /include/linux | |
| 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;
}
[[email protected]: parisc fix]
Signed-off-by: Ulrich Drepper <[email protected]>
Acked-by: Ingo Molnar <[email protected]>
Cc: Davide Libenzi <[email protected]>
Cc: Michael Kerrisk <[email protected]>
Cc: Chris Zankel <[email protected]>
Signed-off-by: Kyle McMartin <[email protected]>
Acked-by: David S. Miller <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
Diffstat (limited to 'include/linux')
0 files changed, 0 insertions, 0 deletions