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: 1d9bdad1df

https://bugzilla.redhat.com/show_bug.cgi?id=1451236
(cherry picked from commit 597072296a)
This commit is contained in:
Beniamino Galvani 2017-10-17 11:02:11 +02:00
parent bb4b6be912
commit 01b10fe24d

View file

@ -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;