From 907939a2788f6cc82588ba7acd88ef82320c3cf7 Mon Sep 17 00:00:00 2001 From: Bob Owen Date: Thu, 7 Apr 2016 08:28:14 +0100 Subject: [PATCH] Bug 1256992 Part 2: Move SandboxBroker Initialization earlier and add telemetry and extra null checks. r=aklotz MozReview-Commit-ID: Fu05wLn27UG --- .../win/src/sandboxbroker/sandboxBroker.cpp | 16 ++++++++ toolkit/components/telemetry/Histograms.json | 7 ++++ toolkit/xre/nsAppRunner.cpp | 38 +++++++++---------- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/security/sandbox/win/src/sandboxbroker/sandboxBroker.cpp b/security/sandbox/win/src/sandboxbroker/sandboxBroker.cpp index 8744055fac95..0c5bd0765510 100644 --- a/security/sandbox/win/src/sandboxbroker/sandboxBroker.cpp +++ b/security/sandbox/win/src/sandboxbroker/sandboxBroker.cpp @@ -434,6 +434,10 @@ SandboxBroker::SetSecurityLevelForGMPlugin() bool SandboxBroker::AllowReadFile(wchar_t const *file) { + if (!mPolicy) { + return false; + } + auto result = mPolicy->AddRule(sandbox::TargetPolicy::SUBSYS_FILES, sandbox::TargetPolicy::FILES_ALLOW_READONLY, @@ -444,6 +448,10 @@ SandboxBroker::AllowReadFile(wchar_t const *file) bool SandboxBroker::AllowReadWriteFile(wchar_t const *file) { + if (!mPolicy) { + return false; + } + auto result = mPolicy->AddRule(sandbox::TargetPolicy::SUBSYS_FILES, sandbox::TargetPolicy::FILES_ALLOW_ANY, @@ -454,6 +462,10 @@ SandboxBroker::AllowReadWriteFile(wchar_t const *file) bool SandboxBroker::AllowDirectory(wchar_t const *dir) { + if (!mPolicy) { + return false; + } + auto result = mPolicy->AddRule(sandbox::TargetPolicy::SUBSYS_FILES, sandbox::TargetPolicy::FILES_ALLOW_DIR_ANY, @@ -464,6 +476,10 @@ SandboxBroker::AllowDirectory(wchar_t const *dir) bool SandboxBroker::AddTargetPeer(HANDLE aPeerProcess) { + if (!sBrokerService) { + return false; + } + sandbox::ResultCode result = sBrokerService->AddTargetPeer(aPeerProcess); return (sandbox::SBOX_ALL_OK == result); } diff --git a/toolkit/components/telemetry/Histograms.json b/toolkit/components/telemetry/Histograms.json index 8182870b3101..510e435de80d 100644 --- a/toolkit/components/telemetry/Histograms.json +++ b/toolkit/components/telemetry/Histograms.json @@ -10465,5 +10465,12 @@ "n_buckets": 50, "keyed": true, "description": "Measures the size of message manager messages by message name" + }, + "SANDBOX_BROKER_INITIALIZED": { + "alert_emails": ["bowen@mozilla.com"], + "bug_numbers": [1256992], + "expires_in_version": "55", + "kind": "boolean", + "description": "Result of call to SandboxBroker::Initialize" } } diff --git a/toolkit/xre/nsAppRunner.cpp b/toolkit/xre/nsAppRunner.cpp index b1790f240cd3..a1eeaa2f4fab 100644 --- a/toolkit/xre/nsAppRunner.cpp +++ b/toolkit/xre/nsAppRunner.cpp @@ -3366,6 +3366,24 @@ XREMain::XRE_mainInit(bool* aExitFlag) } #endif +#if defined(MOZ_SANDBOX) && defined(XP_WIN) + bool brokerInitialized = SandboxBroker::Initialize(); + Telemetry::Accumulate(Telemetry::SANDBOX_BROKER_INITIALIZED, + brokerInitialized); + if (!brokerInitialized) { +#if defined(MOZ_CONTENT_SANDBOX) + // If we're sandboxing content and we fail to initialize, then crashing here + // seems like the sensible option. + if (BrowserTabsRemoteAutostart()) { + MOZ_CRASH("Failed to initialize broker services, can't continue."); + } +#endif + // Otherwise just warn for the moment, as most things will work. + NS_WARNING("Failed to initialize broker services, sandboxed processes will " + "fail to start."); + } +#endif + #ifdef XP_MACOSX if (EnvHasValue("MOZ_LAUNCHED_CHILD")) { // This is needed, on relaunch, to force the OS to use the "Cocoa Dock @@ -3729,12 +3747,6 @@ XREMain::XRE_mainStartup(bool* aExitFlag) int result; #ifdef XP_WIN UseParentConsole(); -#if defined(MOZ_SANDBOX) - if (!SandboxBroker::Initialize()) { - NS_WARNING("Failed to initialize broker services, sandboxed processes " - "will fail to start."); - } -#endif #endif // RunGTest will only be set if we're in xul-unit if (mozilla::RunGTest) { @@ -4320,20 +4332,6 @@ XREMain::XRE_mainRun() } #endif /* MOZ_INSTRUMENT_EVENT_LOOP */ -#if defined(MOZ_SANDBOX) && defined(XP_WIN) - if (!SandboxBroker::Initialize()) { -#if defined(MOZ_CONTENT_SANDBOX) - // If we're sandboxing content and we fail to initialize, then crashing here - // seems like the sensible option. - if (BrowserTabsRemoteAutostart()) { - MOZ_CRASH("Failed to initialize broker services, can't continue."); - } -#endif - // Otherwise just warn for the moment, as most things will work. - NS_WARNING("Failed to initialize broker services, sandboxed processes will " - "fail to start."); - } -#endif #if (defined(XP_WIN) || defined(XP_MACOSX)) && defined(MOZ_CONTENT_SANDBOX) SetUpSandboxEnvironment(); #endif