From 01b10fe24d08a931545af7b74d4fdbdaa39113c8 Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Tue, 17 Oct 2017 11:02:11 +0200 Subject: [PATCH] core: don't close input fd in nm_utils_fd_get_contents() The function should not close the input file descriptor; however fdopen() associates the fd to the new stream so that when the stream is closed, the fd is too. The result is a double close() and the second call can in certain cases affect a wrong fd. Use a duplicate fd for the stream. Fixes: 1d9bdad1dfd33601288de5510ff466acbe2c4b08 https://bugzilla.redhat.com/show_bug.cgi?id=1451236 (cherry picked from commit 597072296a19be051ea70b017fd19505ebee6530) --- src/nm-core-utils.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/nm-core-utils.c b/src/nm-core-utils.c index aaaf7b6c9a..922f69a0d3 100644 --- a/src/nm-core-utils.c +++ b/src/nm-core-utils.c @@ -2933,9 +2933,16 @@ nm_utils_fd_get_contents (int fd, nm_auto_fclose FILE *f = NULL; char buf[4096]; gsize n_have, n_alloc; + int fd2; - if (!(f = fdopen (fd, "r"))) + fd2 = dup (fd); + if (fd2 < 0) + return _get_contents_error (error, 0, "error during dup"); + + if (!(f = fdopen (fd2, "r"))) { + close (fd2); return _get_contents_error (error, 0, "failure during fdopen"); + } n_have = 0; n_alloc = 0;