Commit b9348579 authored by Jay Civelli's avatar Jay Civelli Committed by Commit Bot

Move content presandbox init to ContentMainRunner

In preparation for the move of the zygote code from content/ to
services/service_manager, moving the content specific presandbox
initialization code to from zygote_main to ContentMainRunner.

Bug: 831846
Change-Id: I4c2f160039f21bd1814d1bd9d3fd6664bbb0cf17
Reviewed-on: https://chromium-review.googlesource.com/1008865Reviewed-by: default avatarRobert Sesek <rsesek@chromium.org>
Commit-Queue: Jay Civelli <jcivelli@chromium.org>
Cr-Commit-Position: refs/heads/master@{#550335}
parent 19327a65
......@@ -94,6 +94,33 @@
#endif // OS_POSIX
#if defined(OS_LINUX)
#include "base/native_library.h"
#include "base/rand_util.h"
#include "content/common/font_config_ipc_linux.h"
#include "content/public/common/common_sandbox_support_linux.h"
#include "third_party/blink/public/platform/web_font_render_style.h"
#include "third_party/boringssl/src/include/openssl/crypto.h"
#include "third_party/boringssl/src/include/openssl/rand.h"
#include "third_party/skia/include/ports/SkFontConfigInterface.h"
#include "third_party/skia/include/ports/SkFontMgr.h"
#include "third_party/skia/include/ports/SkFontMgr_android.h"
#if BUILDFLAG(ENABLE_PLUGINS)
#include "content/common/pepper_plugin_list.h"
#include "content/public/common/pepper_plugin_info.h"
#endif
#if BUILDFLAG(ENABLE_LIBRARY_CDMS)
#include "content/public/common/cdm_info.h"
#include "content/public/common/content_client.h"
#endif
#if BUILDFLAG(ENABLE_WEBRTC)
#include "third_party/webrtc_overrides/init_webrtc.h" // nogncheck
#endif
#endif // OS_LINUX
#if !defined(CHROME_MULTIPLE_DLL_BROWSER)
#include "content/public/gpu/content_gpu_client.h"
#include "content/public/renderer/content_renderer_client.h"
......@@ -305,6 +332,109 @@ void InitializeZygoteSandboxForBrowserProcess(
}
#endif // BUILDFLAG(USE_ZYGOTE_HANDLE)
#if defined(OS_LINUX)
#if BUILDFLAG(ENABLE_PLUGINS)
// Loads the (native) libraries but does not initialize them (i.e., does not
// call PPP_InitializeModule). This is needed by the zygote on Linux to get
// access to the plugins before entering the sandbox.
void PreloadPepperPlugins() {
std::vector<PepperPluginInfo> plugins;
ComputePepperPluginList(&plugins);
for (const auto& plugin : plugins) {
if (!plugin.is_internal) {
base::NativeLibraryLoadError error;
base::NativeLibrary library =
base::LoadNativeLibrary(plugin.path, &error);
VLOG_IF(1, !library) << "Unable to load plugin " << plugin.path.value()
<< " " << error.ToString();
ignore_result(library); // Prevent release-mode warning.
}
}
}
#endif
#if BUILDFLAG(ENABLE_LIBRARY_CDMS)
// Loads registered library CDMs but does not initialize them. This is needed by
// the zygote on Linux to get access to the CDMs before entering the sandbox.
void PreloadLibraryCdms() {
std::vector<CdmInfo> cdms;
GetContentClient()->AddContentDecryptionModules(&cdms, nullptr);
for (const auto& cdm : cdms) {
base::NativeLibraryLoadError error;
base::NativeLibrary library = base::LoadNativeLibrary(cdm.path, &error);
VLOG_IF(1, !library) << "Unable to load CDM " << cdm.path.value()
<< " (error: " << error.ToString() << ")";
ignore_result(library); // Prevent release-mode warning.
}
}
#endif // BUILDFLAG(ENABLE_LIBRARY_CDMS)
#if BUILDFLAG(USE_ZYGOTE_HANDLE)
void PreSandboxInit() {
#if defined(ARCH_CPU_ARM_FAMILY)
// On ARM, BoringSSL requires access to /proc/cpuinfo to determine processor
// features. Query this before entering the sandbox.
CRYPTO_library_init();
#endif
// Pass BoringSSL a copy of the /dev/urandom file descriptor so RAND_bytes
// will work inside the sandbox.
RAND_set_urandom_fd(base::GetUrandomFD());
#if BUILDFLAG(ENABLE_PLUGINS)
// Ensure access to the Pepper plugins before the sandbox is turned on.
PreloadPepperPlugins();
#endif
#if BUILDFLAG(ENABLE_LIBRARY_CDMS)
// Ensure access to the library CDMs before the sandbox is turned on.
PreloadLibraryCdms();
#endif
#if BUILDFLAG(ENABLE_WEBRTC)
InitializeWebRtcModule();
#endif
SkFontConfigInterface::SetGlobal(new FontConfigIPC(GetSandboxFD()))->unref();
// 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.
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kAndroidFontsPath)) {
std::string android_fonts_dir =
base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kAndroidFontsPath);
if (android_fonts_dir.size() > 0 && android_fonts_dir.back() != '/')
android_fonts_dir += '/';
SkFontMgr_Android_CustomFonts custom;
custom.fSystemFontUse =
SkFontMgr_Android_CustomFonts::SystemFontUse::kOnlyCustom;
custom.fBasePath = android_fonts_dir.c_str();
std::string font_config;
std::string fallback_font_config;
if (android_fonts_dir.find("kitkat") != std::string::npos) {
font_config = android_fonts_dir + "system_fonts.xml";
fallback_font_config = android_fonts_dir + "fallback_fonts.xml";
custom.fFallbackFontsXml = fallback_font_config.c_str();
} else {
font_config = android_fonts_dir + "fonts.xml";
custom.fFallbackFontsXml = nullptr;
}
custom.fFontsXml = font_config.c_str();
custom.fIsolated = true;
blink::WebFontRenderStyle::SetSkiaFontManager(
SkFontMgr_New_Android(&custom));
}
}
#endif // BUILDFLAG(USE_ZYGOTE_HANDLE)
#endif // OS_LINUX
} // namespace
#if !defined(CHROME_MULTIPLE_DLL_CHILD)
......@@ -393,6 +523,10 @@ int RunZygote(ContentMainDelegate* delegate) {
media::InitializeMediaLibrary();
}
#if defined(OS_LINUX)
PreSandboxInit();
#endif
// This function call can return multiple times, once per fork().
if (!ZygoteMain(std::move(zygote_fork_delegates)))
return 1;
......
......@@ -23,7 +23,6 @@
#include "base/compiler_specific.h"
#include "base/memory/protected_memory.h"
#include "base/memory/protected_memory_cfi.h"
#include "base/native_library.h"
#include "base/posix/eintr_wrapper.h"
#include "base/posix/unix_domain_socket.h"
#include "base/rand_util.h"
......@@ -31,15 +30,12 @@
#include "base/strings/string_number_conversions.h"
#include "base/sys_info.h"
#include "build/build_config.h"
#include "content/common/font_config_ipc_linux.h"
#include "content/common/zygote_commands_linux.h"
#include "content/public/common/common_sandbox_support_linux.h"
#include "content/public/common/content_descriptors.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/zygote_fork_delegate_linux.h"
#include "content/zygote/zygote_linux.h"
#include "media/media_buildflags.h"
#include "ppapi/buildflags/buildflags.h"
#include "sandbox/linux/services/credentials.h"
#include "sandbox/linux/services/init_process_reaper.h"
#include "sandbox/linux/services/libc_interceptor.h"
......@@ -49,27 +45,7 @@
#include "services/service_manager/sandbox/linux/sandbox_debug_handling_linux.h"
#include "services/service_manager/sandbox/linux/sandbox_linux.h"
#include "services/service_manager/sandbox/sandbox.h"
#include "third_party/blink/public/platform/web_font_render_style.h"
#include "third_party/boringssl/src/include/openssl/crypto.h"
#include "third_party/boringssl/src/include/openssl/rand.h"
#include "third_party/icu/source/i18n/unicode/timezone.h"
#include "third_party/skia/include/ports/SkFontConfigInterface.h"
#include "third_party/skia/include/ports/SkFontMgr.h"
#include "third_party/skia/include/ports/SkFontMgr_android.h"
#if BUILDFLAG(ENABLE_PLUGINS)
#include "content/common/pepper_plugin_list.h"
#include "content/public/common/pepper_plugin_info.h"
#endif
#if BUILDFLAG(ENABLE_LIBRARY_CDMS)
#include "content/public/common/cdm_info.h"
#include "content/public/common/content_client.h"
#endif
#if BUILDFLAG(ENABLE_WEBRTC)
#include "third_party/webrtc_overrides/init_webrtc.h" // nogncheck
#endif
namespace content {
......@@ -95,44 +71,6 @@ base::OnceClosure ClosureFromTwoClosures(base::OnceClosure one,
} // namespace
#if BUILDFLAG(ENABLE_PLUGINS)
// Loads the (native) libraries but does not initialize them (i.e., does not
// call PPP_InitializeModule). This is needed by the zygote on Linux to get
// access to the plugins before entering the sandbox.
void PreloadPepperPlugins() {
std::vector<PepperPluginInfo> plugins;
ComputePepperPluginList(&plugins);
for (const auto& plugin : plugins) {
if (!plugin.is_internal) {
base::NativeLibraryLoadError error;
base::NativeLibrary library = base::LoadNativeLibrary(plugin.path,
&error);
VLOG_IF(1, !library) << "Unable to load plugin "
<< plugin.path.value() << " "
<< error.ToString();
ignore_result(library); // Prevent release-mode warning.
}
}
}
#endif
#if BUILDFLAG(ENABLE_LIBRARY_CDMS)
// Loads registered library CDMs but does not initialize them. This is needed by
// the zygote on Linux to get access to the CDMs before entering the sandbox.
void PreloadLibraryCdms() {
std::vector<CdmInfo> cdms;
GetContentClient()->AddContentDecryptionModules(&cdms, nullptr);
for (const auto& cdm : cdms) {
base::NativeLibraryLoadError error;
base::NativeLibrary library = base::LoadNativeLibrary(cdm.path, &error);
VLOG_IF(1, !library) << "Unable to load CDM " << cdm.path.value()
<< " (error: " << error.ToString() << ")";
ignore_result(library); // Prevent release-mode warning.
}
}
#endif // BUILDFLAG(ENABLE_LIBRARY_CDMS)
// This function triggers the static and lazy construction of objects that need
// to be created before imposing the sandbox.
static void ZygotePreSandboxInit() {
......@@ -146,64 +84,6 @@ static void ZygotePreSandboxInit() {
// TimeZone::createDefault is called once here, the timezone ID is
// cached and there's no more need to access the file system.
std::unique_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
#if defined(ARCH_CPU_ARM_FAMILY)
// On ARM, BoringSSL requires access to /proc/cpuinfo to determine processor
// features. Query this before entering the sandbox.
CRYPTO_library_init();
#endif
// Pass BoringSSL a copy of the /dev/urandom file descriptor so RAND_bytes
// will work inside the sandbox.
RAND_set_urandom_fd(base::GetUrandomFD());
#if BUILDFLAG(ENABLE_PLUGINS)
// Ensure access to the Pepper plugins before the sandbox is turned on.
PreloadPepperPlugins();
#endif
#if BUILDFLAG(ENABLE_LIBRARY_CDMS)
// Ensure access to the library CDMs before the sandbox is turned on.
PreloadLibraryCdms();
#endif
#if BUILDFLAG(ENABLE_WEBRTC)
InitializeWebRtcModule();
#endif
SkFontConfigInterface::SetGlobal(new FontConfigIPC(GetSandboxFD()))->unref();
// 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.
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kAndroidFontsPath)) {
std::string android_fonts_dir =
base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kAndroidFontsPath);
if (android_fonts_dir.size() > 0 && android_fonts_dir.back() != '/')
android_fonts_dir += '/';
SkFontMgr_Android_CustomFonts custom;
custom.fSystemFontUse =
SkFontMgr_Android_CustomFonts::SystemFontUse::kOnlyCustom;
custom.fBasePath = android_fonts_dir.c_str();
std::string font_config;
std::string fallback_font_config;
if (android_fonts_dir.find("kitkat") != std::string::npos) {
font_config = android_fonts_dir + "system_fonts.xml";
fallback_font_config = android_fonts_dir + "fallback_fonts.xml";
custom.fFallbackFontsXml = fallback_font_config.c_str();
} else {
font_config = android_fonts_dir + "fonts.xml";
custom.fFallbackFontsXml = nullptr;
}
custom.fFontsXml = font_config.c_str();
custom.fIsolated = true;
blink::WebFontRenderStyle::SetSkiaFontManager(
SkFontMgr_New_Android(&custom));
}
}
static bool CreateInitProcessReaper(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment