From 66aeb6e59da7701eb997833ac9d75d87726e7c2d Mon Sep 17 00:00:00 2001 From: Kevin Hamacher Date: Thu, 15 Jun 2023 03:16:25 -0700 Subject: [PATCH] Error out if invalid custom forkserver path is specified PiperOrigin-RevId: 540526350 Change-Id: Id7f4ea9290074c15c700c27c2d252b9f54a282bd --- sandboxed_api/sandbox2/global_forkclient.cc | 22 ++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/sandboxed_api/sandbox2/global_forkclient.cc b/sandboxed_api/sandbox2/global_forkclient.cc index fc02e35..0cebea7 100644 --- a/sandboxed_api/sandbox2/global_forkclient.cc +++ b/sandboxed_api/sandbox2/global_forkclient.cc @@ -160,17 +160,21 @@ absl::StatusOr> StartGlobalForkServer() { // Allow passing of a spearate forkserver_bin via flag int exec_fd = -1; - if (!absl::GetFlag(FLAGS_sandbox2_forkserver_binary_path).empty()) { - exec_fd = open(absl::GetFlag(FLAGS_sandbox2_forkserver_binary_path).c_str(), - O_RDONLY); + std::string bin_path = absl::GetFlag(FLAGS_sandbox2_forkserver_binary_path); + if (!bin_path.empty()) { + exec_fd = open(bin_path.c_str(), O_RDONLY); + if (exec_fd < 0) { + return absl::ErrnoToStatus( + errno, absl::StrCat("Opening forkserver binary passed via " + "--sandbox2_forkserver_binary_path (", + bin_path, ")")); + } + } else if constexpr (sapi::host_os::IsAndroid()) { + return absl::FailedPreconditionError( + "sandbox2_forkserver_binary_path flag has to be set to the location of " + "the forkserver binary on Android"); } if (exec_fd < 0) { - // For Android we expect the forkserver_bin in the flag - if constexpr (sapi::host_os::IsAndroid()) { - return absl::ErrnoToStatus( - errno, - "Open init binary passed via --sandbox2_forkserver_binary_path"); - } // Extract the fd when it's owned by EmbedFile exec_fd = sapi::EmbedFile::instance()->GetDupFdForFileToc( forkserver_bin_embed_create());