summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsin-ack <sin-ack@users.noreply.github.com>2022-01-21 14:11:36 +0000
committerAndreas Kling <kling@serenityos.org>2022-01-21 15:21:52 +0100
commit3de51a4b997a73854cc9626a6176093062f70cb0 (patch)
tree0f5a7003c148d7dea1a3209b68fbf81497a13c71
parent483ab7fe2655c087ca094c9b2674b4d6d0f6647f (diff)
downloadserenity-3de51a4b997a73854cc9626a6176093062f70cb0.zip
LibCore: Make sockets close-on-exec by default
This mirrors the previous default in Core::LocalSocket, and is the safer default anyway. This prevents fds from living on in other processes when exec() is called in certain programs such as Assistant. Fixes #12029.
-rw-r--r--Userland/Libraries/LibCore/Stream.cpp9
1 files changed, 8 insertions, 1 deletions
diff --git a/Userland/Libraries/LibCore/Stream.cpp b/Userland/Libraries/LibCore/Stream.cpp
index 76643d9c9e..e8f26757b2 100644
--- a/Userland/Libraries/LibCore/Stream.cpp
+++ b/Userland/Libraries/LibCore/Stream.cpp
@@ -241,7 +241,14 @@ ErrorOr<int> Socket::create_fd(SocketDomain domain, SocketType type)
VERIFY_NOT_REACHED();
}
- return System::socket(socket_domain, socket_type, 0);
+ // Let's have a safe default of CLOEXEC. :^)
+#ifdef SOCK_CLOEXEC
+ return System::socket(socket_domain, socket_type | SOCK_CLOEXEC, 0);
+#else
+ auto fd = TRY(System::socket(socket_domain, socket_type, 0));
+ TRY(System::fcntl(fd, F_SETFD, FD_CLOEXEC));
+ return fd;
+#endif
}
ErrorOr<IPv4Address> Socket::resolve_host(String const& host, SocketType type)