This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
O_CLOEXEC and dynamic loading
- From: Ross Lagerwall <rosslagerwall at gmail dot com>
- To: libc-alpha at sourceware dot org
- Date: Mon, 05 Sep 2011 16:33:25 +0200
- Subject: O_CLOEXEC and dynamic loading
Hi,
What is the stance with glibc and setting file descriptors as O_CLOEXEC
for its internal stuff?
An issue occurred where a pthread function was dlopen()ing libgcc
without O_CLOEXEC which causes the open file descriptor to leak through
when another thread forks a child process.
Similarly, this could happen when doing something seemingly simple like
gethostbyname() on a separate thread and forking a process on another
thread. I could imagine Firefox doing something like this.
I would think the O_CLOEXEC flag should be added for the opening of
any .so library and the /etc/ld* files. See
http://lists.busybox.net/pipermail/uclibc-cvs/2009-October/026908.html
See http://sourceware.org/bugzilla/show_bug.cgi?id=13068 for the
original bug post and a test program.
Here is an untested patch which I would guess would be on the right
track:
diff --git a/elf/dl-load.c b/elf/dl-load.c
index 24b3359..6922b61 100644
--- a/elf/dl-load.c
+++ b/elf/dl-load.c
@@ -1704,7 +1704,7 @@ open_verify (const char *name, struct filebuf
*fbp, struct link_map *loader,
#endif
/* Open the file. We always open files read-only. */
- int fd = __open (name, O_RDONLY);
+ int fd = __open (name, O_RDONLY | O_CLOEXEC);
if (fd != -1)
{
ElfW(Ehdr) *ehdr;
diff --git a/elf/dl-misc.c b/elf/dl-misc.c
index d50537a..4e3443e 100644
--- a/elf/dl-misc.c
+++ b/elf/dl-misc.c
@@ -44,7 +44,7 @@ _dl_sysdep_read_whole_file (const char *file, size_t
*sizep, int prot)
{
void *result = MAP_FAILED;
struct stat64 st;
- int fd = __open (file, O_RDONLY);
+ int fd = __open (file, O_RDONLY | O_CLOEXEC);
if (fd >= 0)
{
if (__fxstat64 (_STAT_VER, fd, &st) >= 0)
Thanks for taking a look,
Ross