From d7ee7196fb236c616cc64de0486cf8d659e235a2 Mon Sep 17 00:00:00 2001 From: Francis Deslauriers Date: Tue, 28 Aug 2018 16:01:02 -0400 Subject: [PATCH] Fix: passing negative param to dup(2) on error MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Reported-by: Coverity (1395195) Signed-off-by: Francis Deslauriers Signed-off-by: Jérémie Galarneau --- src/common/userspace-probe.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/common/userspace-probe.c b/src/common/userspace-probe.c index 6331ff135..e764d137b 100644 --- a/src/common/userspace-probe.c +++ b/src/common/userspace-probe.c @@ -404,7 +404,7 @@ lttng_userspace_probe_location_function_copy( struct lttng_userspace_probe_location_lookup_method *lookup_method = NULL; const char *binary_path = NULL; const char *function_name = NULL; - int fd; + int fd, new_fd; assert(location); assert(location->type == LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION); @@ -423,8 +423,14 @@ lttng_userspace_probe_location_function_copy( } /* Duplicate the binary fd */ - fd = dup(lttng_userspace_probe_location_function_get_binary_fd(location)); + fd = lttng_userspace_probe_location_function_get_binary_fd(location); if (fd == -1) { + ERR("Error getting file descriptor to binary"); + goto error; + } + + new_fd = dup(fd); + if (new_fd == -1) { PERROR("Error duplicating file descriptor to binary"); goto error; } @@ -456,7 +462,7 @@ lttng_userspace_probe_location_function_copy( } /* Set the duplicated fd to the new probe_location */ - if (lttng_userspace_probe_location_function_set_binary_fd(new_location, fd) < 0) { + if (lttng_userspace_probe_location_function_set_binary_fd(new_location, new_fd) < 0) { goto destroy_probe_location; } @@ -467,7 +473,7 @@ destroy_probe_location: destroy_lookup_method: lttng_userspace_probe_location_lookup_method_destroy(lookup_method); close_fd: - if (close(fd) < 0) { + if (close(new_fd) < 0) { PERROR("Error closing duplicated file descriptor in error path"); } error: @@ -486,7 +492,7 @@ lttng_userspace_probe_location_tracepoint_copy( const char *binary_path = NULL; const char *probe_name = NULL; const char *provider_name = NULL; - int fd; + int fd, new_fd; assert(location); assert(location->type == LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT); @@ -511,8 +517,14 @@ lttng_userspace_probe_location_tracepoint_copy( } /* Duplicate the binary fd */ - fd = dup(lttng_userspace_probe_location_tracepoint_get_binary_fd(location)); + fd = lttng_userspace_probe_location_tracepoint_get_binary_fd(location); if (fd == -1) { + ERR("Error getting file descriptor to binary"); + goto error; + } + + new_fd = dup(fd); + if (new_fd == -1) { PERROR("Error duplicating file descriptor to binary"); goto error; } @@ -544,7 +556,7 @@ lttng_userspace_probe_location_tracepoint_copy( } /* Set the duplicated fd to the new probe_location */ - if (lttng_userspace_probe_location_tracepoint_set_binary_fd(new_location, fd) < 0) { + if (lttng_userspace_probe_location_tracepoint_set_binary_fd(new_location, new_fd) < 0) { goto destroy_probe_location; } @@ -555,7 +567,7 @@ destroy_probe_location: destroy_lookup_method: lttng_userspace_probe_location_lookup_method_destroy(lookup_method); close_fd: - if (close(fd) < 0) { + if (close(new_fd) < 0) { PERROR("Error closing duplicated file descriptor in error path"); } error: -- 2.34.1