From c189ffaaec45efb49e785517e504b41a5e57b088 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 26 Aug 2014 16:20:59 -0700 Subject: [PATCH] More cases where libc should use O_CLOEXEC. (cherry picked from commit f73183f1a34df22b62a3d0bbf82e18d5797c9cde) (cherry picked from commit 21ce3f506f3b54e4f57a37847375cef9f8aff57f) Change-Id: I70b240bd40ad8d2ba33ae7ab2618782709fd0d6a --- libc/bionic/bionic_systrace.cpp | 2 +- libc/bionic/dirent.cpp | 2 +- libc/bionic/malloc_debug_qemu.cpp | 2 +- libc/bionic/pthread_setname_np.cpp | 2 +- libc/bionic/system_properties.cpp | 21 ++------------------- 5 files changed, 6 insertions(+), 23 deletions(-) diff --git a/libc/bionic/bionic_systrace.cpp b/libc/bionic/bionic_systrace.cpp index b8e892e72..f5be41553 100644 --- a/libc/bionic/bionic_systrace.cpp +++ b/libc/bionic/bionic_systrace.cpp @@ -74,7 +74,7 @@ ScopedTrace::ScopedTrace(const char* message) { } if (g_trace_marker_fd == -1) { - g_trace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_WRONLY | O_CLOEXEC); + g_trace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_CLOEXEC | O_WRONLY); if (g_trace_marker_fd == -1) { __libc_fatal("Could not open kernel trace file: %s\n", strerror(errno)); } diff --git a/libc/bionic/dirent.cpp b/libc/bionic/dirent.cpp index 7abc7f3ec..5e1c7a565 100644 --- a/libc/bionic/dirent.cpp +++ b/libc/bionic/dirent.cpp @@ -78,7 +78,7 @@ DIR* fdopendir(int fd) { } DIR* opendir(const char* path) { - int fd = open(path, O_RDONLY | O_DIRECTORY); + int fd = open(path, O_CLOEXEC | O_DIRECTORY | O_RDONLY); return (fd != -1) ? __allocate_DIR(fd) : NULL; } diff --git a/libc/bionic/malloc_debug_qemu.cpp b/libc/bionic/malloc_debug_qemu.cpp index b3b604d86..2f4949b12 100644 --- a/libc/bionic/malloc_debug_qemu.cpp +++ b/libc/bionic/malloc_debug_qemu.cpp @@ -606,7 +606,7 @@ extern "C" bool malloc_debug_initialize(HashTable*, const MallocDebug* malloc_di * the memory mapped spaces into writes to an I/O port that emulator * "listens to" on the other end. Note that until we open and map that * device, logging to emulator's stdout will not be available. */ - int fd = open("/dev/qemu_trace", O_RDWR); + int fd = open("/dev/qemu_trace", O_CLOEXEC | O_RDWR); if (fd < 0) { error_log("Unable to open /dev/qemu_trace"); return false; diff --git a/libc/bionic/pthread_setname_np.cpp b/libc/bionic/pthread_setname_np.cpp index 1ddf81044..7b2fa6b0a 100644 --- a/libc/bionic/pthread_setname_np.cpp +++ b/libc/bionic/pthread_setname_np.cpp @@ -67,7 +67,7 @@ int pthread_setname_np(pthread_t t, const char* thread_name) { } char comm_name[sizeof(TASK_COMM_FMT) + 8]; snprintf(comm_name, sizeof(comm_name), TASK_COMM_FMT, tid); - int fd = open(comm_name, O_WRONLY); + int fd = open(comm_name, O_CLOEXEC | O_WRONLY); if (fd == -1) { return errno; } diff --git a/libc/bionic/system_properties.cpp b/libc/bionic/system_properties.cpp index ad69cf5f9..411d6bf34 100644 --- a/libc/bionic/system_properties.cpp +++ b/libc/bionic/system_properties.cpp @@ -201,14 +201,6 @@ static int map_prop_area_rw() return -1; } - // TODO: Is this really required ? Does android run on any kernels that - // don't support O_CLOEXEC ? - const int ret = fcntl(fd, F_SETFD, FD_CLOEXEC); - if (ret < 0) { - close(fd); - return -1; - } - if (ftruncate(fd, PA_SIZE) < 0) { close(fd); return -1; @@ -271,18 +263,9 @@ static int map_fd_ro(const int fd) { static int map_prop_area() { - int fd(open(property_filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC)); - if (fd >= 0) { - /* For old kernels that don't support O_CLOEXEC */ - const int ret = fcntl(fd, F_SETFD, FD_CLOEXEC); - if (ret < 0) { - close(fd); - return -1; - } - } - + int fd = open(property_filename, O_CLOEXEC | O_NOFOLLOW | O_RDONLY); bool close_fd = true; - if ((fd < 0) && (errno == ENOENT)) { + if (fd == -1 && errno == ENOENT) { /* * For backwards compatibility, if the file doesn't * exist, we use the environment to get the file descriptor.