$OpenBSD: patch-content_app_content_main_runner_impl_cc,v 1.2 2018/09/12 17:08:22 robert Exp $

Index: content/app/content_main_runner_impl.cc
--- content/app/content_main_runner_impl.cc.orig
+++ content/app/content_main_runner_impl.cc
@@ -85,17 +85,17 @@
 #include "base/posix/global_descriptors.h"
 #include "content/public/common/content_descriptors.h"
 
-#if !defined(OS_MACOSX)
+#if !defined(OS_MACOSX) && !defined(OS_BSD)
 #include "services/service_manager/zygote/common/zygote_fork_delegate_linux.h"
 #endif
-#if !defined(OS_MACOSX) && !defined(OS_ANDROID)
+#if !defined(OS_MACOSX) && !defined(OS_ANDROID) && !defined(OS_BSD)
 #include "sandbox/linux/services/libc_interceptor.h"
 #include "services/service_manager/zygote/zygote_main.h"
 #endif
 
 #endif  // OS_POSIX || OS_FUCHSIA
 
-#if defined(OS_LINUX)
+#if defined(OS_LINUX) || defined(OS_BSD)
 #include "base/native_library.h"
 #include "base/rand_util.h"
 #include "services/service_manager/zygote/common/common_sandbox_support_linux.h"
@@ -138,15 +138,24 @@
 #include "content/utility/in_process_utility_thread.h"
 #endif
 
-#if BUILDFLAG(USE_ZYGOTE_HANDLE)
+#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
 #include "content/browser/sandbox_host_linux.h"
 #include "media/base/media_switches.h"
 #include "services/service_manager/zygote/common/common_sandbox_support_linux.h"
+#if !defined(OS_OPENBSD)
 #include "services/service_manager/zygote/common/zygote_handle.h"
 #include "services/service_manager/zygote/host/zygote_communication_linux.h"
 #include "services/service_manager/zygote/host/zygote_host_impl_linux.h"
 #endif
+#endif
 
+#if defined(OS_BSD)
+#include "base/sys_info.h"
+#include "content/public/common/common_sandbox_support_linux.h"
+#include "content/public/common/sandbox_init.h"
+#include "services/service_manager/sandbox/linux/sandbox_linux.h"
+#endif
+
 namespace content {
 extern int GpuMain(const content::MainFunctionParams&);
 #if BUILDFLAG(ENABLE_PLUGINS)
@@ -327,7 +336,7 @@ void InitializeZygoteSandboxForBrowserProcess(
 }
 #endif  // BUILDFLAG(USE_ZYGOTE_HANDLE)
 
-#if defined(OS_LINUX)
+#if defined(OS_LINUX) || defined(OS_BSD)
 
 #if BUILDFLAG(ENABLE_PLUGINS)
 // Loads the (native) libraries but does not initialize them (i.e., does not
@@ -366,7 +375,7 @@ void PreloadLibraryCdms() {
 }
 #endif  // BUILDFLAG(ENABLE_LIBRARY_CDMS)
 
-#if BUILDFLAG(USE_ZYGOTE_HANDLE)
+#if BUILDFLAG(USE_ZYGOTE_HANDLE) || defined(OS_BSD)
 void PreSandboxInit() {
 #if defined(ARCH_CPU_ARM_FAMILY)
   // On ARM, BoringSSL requires access to /proc/cpuinfo to determine processor
@@ -374,9 +383,12 @@ void PreSandboxInit() {
   CRYPTO_library_init();
 #endif
 
+// arc4random
+#if !defined(OS_OPENBSD)
   // Pass BoringSSL a copy of the /dev/urandom file descriptor so RAND_bytes
   // will work inside the sandbox.
   RAND_set_urandom_fd(base::GetUrandomFD());
+#endif
 
 #if BUILDFLAG(ENABLE_PLUGINS)
   // Ensure access to the Pepper plugins before the sandbox is turned on.
@@ -388,6 +400,11 @@ void PreSandboxInit() {
 #endif
   InitializeWebRtcModule();
 
+#if defined(OS_BSD)
+  // "cache" the amount of physical memory before pledge(2)
+  base::SysInfo::AmountOfPhysicalMemoryMB();
+#endif
+
   // Set the android SkFontMgr for blink. We need to ensure this is done
   // before the sandbox is initialized to allow the font manager to access
   // font configuration files on disk.
@@ -688,7 +705,7 @@ int ContentMainRunnerImpl::Initialize(const ContentMai
                    base::GlobalDescriptors::kBaseDescriptor);
 #endif  // !OS_ANDROID
 
-#if defined(OS_LINUX) || defined(OS_OPENBSD)
+#if defined(OS_LINUX)
     g_fds->Set(service_manager::kCrashDumpSignal,
                service_manager::kCrashDumpSignal +
                    base::GlobalDescriptors::kBaseDescriptor);
@@ -877,6 +894,16 @@ int ContentMainRunnerImpl::Initialize(const ContentMai
     }
 #endif
 
+#if defined(OS_OPENBSD)
+    if (process_type.empty()) {
+      service_manager::SandboxLinux::Options sandbox_options;
+      service_manager::SandboxLinux::GetInstance()->InitializeSandbox(
+          service_manager::SandboxTypeFromCommandLine(
+              *base::CommandLine::ForCurrentProcess()),
+          service_manager::SandboxLinux::PreSandboxHook(), sandbox_options);
+    }
+#endif
+
     delegate_->SandboxInitialized(process_type);
 
 #if BUILDFLAG(USE_ZYGOTE_HANDLE)
@@ -918,6 +945,11 @@ int ContentMainRunnerImpl::Run(bool start_service_mana
 #endif
 
   RegisterMainThreadFactories();
+
+#if defined(OS_OPENBSD)
+  if (!process_type.empty())
+    PreSandboxInit();
+#endif
 
 #if !defined(CHROME_MULTIPLE_DLL_CHILD)
   // The thread used to start the ServiceManager is handed-off to
