From 7c23f706733b12405c0cb8793866b57ba5800c98 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Thu, 6 May 2021 19:26:38 +0200 Subject: [PATCH] Fix various close-on-exec bugs. --- acceptor.cpp | 2 +- client.cpp | 2 +- httpinput.cpp | 2 +- serverpool.cpp | 1 + stream.cpp | 8 ++++++++ udpinput.cpp | 2 +- 6 files changed, 13 insertions(+), 4 deletions(-) diff --git a/acceptor.cpp b/acceptor.cpp index 2fef914..31901dd 100644 --- a/acceptor.cpp +++ b/acceptor.cpp @@ -104,7 +104,7 @@ Acceptor::Acceptor(const AcceptorProto &serialized) { // Set back the close-on-exec flag for the socket. // (This can't leak into a child, since we haven't been started yet.) - fcntl(server_sock, F_SETFD, 1); + fcntl(server_sock, F_SETFD, O_CLOEXEC); } AcceptorProto Acceptor::serialize() const diff --git a/client.cpp b/client.cpp index 26806d6..5d5c179 100644 --- a/client.cpp +++ b/client.cpp @@ -72,7 +72,7 @@ Client::Client(const ClientProto &serialized, const vectorpacing_rate, sizeof(stream->pacing_rate)) == -1) { diff --git a/httpinput.cpp b/httpinput.cpp index 3a6b3c8..8d443c7 100644 --- a/httpinput.cpp +++ b/httpinput.cpp @@ -80,7 +80,7 @@ HTTPInput::HTTPInput(const InputProto &serialized) { // Set back the close-on-exec flag for the socket. // (This can't leak into a child, since we haven't been started yet.) - fcntl(sock, F_SETFD, 1); + fcntl(sock, F_SETFD, O_CLOEXEC); pending_data.resize(serialized.pending_data().size()); memcpy(&pending_data[0], serialized.pending_data().data(), serialized.pending_data().size()); diff --git a/serverpool.cpp b/serverpool.cpp index e9fea12..c7ff10c 100644 --- a/serverpool.cpp +++ b/serverpool.cpp @@ -1,4 +1,5 @@ #include +#include #include #include diff --git a/stream.cpp b/stream.cpp index 3ee1a43..4e5ca24 100644 --- a/stream.cpp +++ b/stream.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -67,6 +68,9 @@ Stream::Stream(const StreamProto &serialized, int data_fd) exit(1); } + // Set the close-on-exec parameter back on the backlog fd. + fcntl(data_fd, F_SETFD, O_CLOEXEC); + for (ssize_t point : serialized.suitable_starting_point()) { if (point == -1) { // Can happen when upgrading from before 1.1.3, @@ -103,6 +107,10 @@ StreamProto Stream::serialize() serialized.set_first_fragment_index(first_fragment_index); serialized.set_discontinuity_counter(discontinuity_counter); + // Unset the close-on-exec flag for the backlog fd. + // (This can't leak into a child, since there's only one thread left.) + fcntl(data_fd, F_SETFD, 0); + serialized.set_url(url); data_fd = -1; return serialized; diff --git a/udpinput.cpp b/udpinput.cpp index ecd099a..873b0b9 100644 --- a/udpinput.cpp +++ b/udpinput.cpp @@ -123,7 +123,7 @@ UDPInput::UDPInput(const InputProto &serialized) { // Set back the close-on-exec flag for the socket. // (This can't leak into a child, since we haven't been started yet.) - fcntl(sock, F_SETFD, 1); + fcntl(sock, F_SETFD, O_CLOEXEC); // Should be verified by the caller. string protocol; -- 2.39.2