#include "content/browser/utility_sandbox_delegate.h"
#include "base/check.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/strings/utf_string_conversions.h"
#include "components/services/screen_ai/buildflags/buildflags.h"
#include "components/services/screen_ai/public/cpp/utilities.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/sandboxed_process_launcher_delegate.h"
#include "printing/buildflags/buildflags.h"
#include "sandbox/policy/mojom/sandbox.mojom.h"
#include "sandbox/policy/win/sandbox_win.h"
#include "sandbox/win/src/app_container.h"
#include "sandbox/win/src/sandbox_policy.h"
#include "sandbox/win/src/sandbox_types.h"
#include "services/network/public/mojom/network_service.mojom.h"
namespace content {
namespace {
bool AudioInitializeConfig(sandbox::TargetConfig* config) {
DCHECK(!config->IsConfigured());
auto result = config->SetIntegrityLevel(sandbox::INTEGRITY_LEVEL_LOW);
if (result != sandbox::SBOX_ALL_OK)
return false;
config->SetLockdownDefaultDacl();
config->SetDelayedIntegrityLevel(sandbox::INTEGRITY_LEVEL_LOW);
result = config->SetTokenLevel(sandbox::USER_RESTRICTED_SAME_ACCESS,
sandbox::USER_RESTRICTED_NON_ADMIN);
if (result != sandbox::SBOX_ALL_OK)
return false;
result = config->AddRule(sandbox::SubSystem::kNamedPipes,
sandbox::Semantics::kNamedPipesAllowAny,
L"\\\\.\\pipe\\chrome.sync.*");
if (result != sandbox::SBOX_ALL_OK) {
return false;
}
config->SetDesktop(sandbox::Desktop::kAlternateWinstation);
return true;
}
bool NetworkInitializeConfig(sandbox::TargetConfig* config) {
DCHECK(!config->IsConfigured());
auto result = config->SetTokenLevel(sandbox::USER_UNPROTECTED,
sandbox::USER_UNPROTECTED);
if (result != sandbox::SBOX_ALL_OK)
return false;
auto lpac_capability =
GetContentClient()->browser()->GetLPACCapabilityNameForNetworkService();
if (lpac_capability.empty())
return false;
auto app_container = config->GetAppContainer();
if (!app_container)
return false;
app_container->AddCapability(lpac_capability.c_str());
app_container->AddCapabilitySddl(
L"S-1-15-3-893703388-718787801-2109771152-172907555-2119217564-716812919-"
L"652991501");
return true;
}
#if BUILDFLAG(ENABLE_OOP_PRINTING)
bool PrintBackendInitializeConfig(sandbox::TargetConfig* config) {
DCHECK(!config->IsConfigured());
auto result = config->SetTokenLevel(sandbox::USER_RESTRICTED_SAME_ACCESS,
sandbox::USER_LIMITED);
if (result != sandbox::SBOX_ALL_OK)
return false;
config->SetDelayedIntegrityLevel(sandbox::INTEGRITY_LEVEL_LOW);
return true;
}
#endif
std::string UtilityAppContainerId(base::CommandLine& cmd_line) {
return base::WideToUTF8(cmd_line.GetProgram().value());
}
bool IconReaderInitializeConfig(sandbox::TargetConfig* config) {
DCHECK(!config->IsConfigured());
auto result = config->SetTokenLevel(sandbox::USER_RESTRICTED_SAME_ACCESS,
sandbox::USER_LOCKDOWN);
if (result != sandbox::SBOX_ALL_OK)
return false;
config->SetDelayedIntegrityLevel(sandbox::INTEGRITY_LEVEL_UNTRUSTED);
result = config->SetIntegrityLevel(sandbox::INTEGRITY_LEVEL_LOW);
if (result != sandbox::SBOX_ALL_OK)
return false;
config->SetLockdownDefaultDacl();
config->SetDesktop(sandbox::Desktop::kAlternateWinstation);
sandbox::MitigationFlags flags = config->GetDelayedProcessMitigations();
flags |= sandbox::MITIGATION_DYNAMIC_CODE_DISABLE;
result = config->SetDelayedProcessMitigations(flags);
if (result != sandbox::SBOX_ALL_OK)
return false;
result =
config->AddRule(sandbox::SubSystem::kFiles,
sandbox::Semantics::kFilesAllowReadonly, L"\\??\\*.exe");
if (result != sandbox::SBOX_ALL_OK)
return false;
result =
config->AddRule(sandbox::SubSystem::kFiles,
sandbox::Semantics::kFilesAllowReadonly, L"\\??\\*.dll");
if (result != sandbox::SBOX_ALL_OK)
return false;
result =
config->AddRule(sandbox::SubSystem::kFiles,
sandbox::Semantics::kFilesAllowReadonly, L"\\??\\*.ico");
if (result != sandbox::SBOX_ALL_OK)
return false;
return true;
}
bool XrCompositingInitializeConfig(sandbox::TargetConfig* config,
base::CommandLine& cmd_line,
sandbox::mojom::Sandbox sandbox_type) {
DCHECK(!config->IsConfigured());
auto result = config->SetTokenLevel(sandbox::USER_UNPROTECTED,
sandbox::USER_UNPROTECTED);
if (result != sandbox::SBOX_ALL_OK)
return false;
result = sandbox::policy::SandboxWin::SetJobLevel(
sandbox_type, sandbox::JobLevel::kUnprotected, 0, config);
if (result != sandbox::SBOX_ALL_OK)
return false;
result = config->SetProcessMitigations(0);
if (result != sandbox::SBOX_ALL_OK)
return false;
result = config->SetDelayedProcessMitigations(0);
if (result != sandbox::SBOX_ALL_OK)
return false;
std::string appcontainer_id = UtilityAppContainerId(cmd_line);
result = sandbox::policy::SandboxWin::AddAppContainerProfileToConfig(
cmd_line, sandbox_type, appcontainer_id, config);
if (result != sandbox::SBOX_ALL_OK)
return false;
return true;
}
#if BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)
bool ScreenAIInitializeConfig(sandbox::TargetConfig* config,
sandbox::mojom::Sandbox sandbox_type) {
DCHECK(!config->IsConfigured());
auto result = config->SetTokenLevel(sandbox::USER_RESTRICTED_SAME_ACCESS,
sandbox::USER_LOCKDOWN);
if (result != sandbox::SBOX_ALL_OK)
return false;
result = sandbox::policy::SandboxWin::SetJobLevel(
sandbox_type, sandbox::JobLevel::kLimitedUser, 0, config);
if (result != sandbox::SBOX_ALL_OK)
return false;
base::FilePath library_binary_path =
screen_ai::GetLatestComponentBinaryPath();
if (library_binary_path.empty())
return false;
DCHECK_EQ(library_binary_path.Extension(), FILE_PATH_LITERAL(".dll"));
result = config->AddRule(sandbox::SubSystem::kFiles,
sandbox::Semantics::kFilesAllowReadonly,
library_binary_path.value().c_str());
return result == sandbox::SBOX_ALL_OK;
}
#endif
}
std::string UtilitySandboxedProcessLauncherDelegate::GetSandboxTag() {
return sandbox::policy::SandboxWin::GetSandboxTagForDelegate(
"utility", GetSandboxType());
}
bool UtilitySandboxedProcessLauncherDelegate::GetAppContainerId(
std::string* appcontainer_id) {
switch (sandbox_type_) {
case sandbox::mojom::Sandbox::kMediaFoundationCdm:
case sandbox::mojom::Sandbox::kNetwork:
case sandbox::mojom::Sandbox::kWindowsSystemProxyResolver:
case sandbox::mojom::Sandbox::kXrCompositing:
*appcontainer_id = UtilityAppContainerId(cmd_line_);
return true;
default:
return false;
}
}
bool UtilitySandboxedProcessLauncherDelegate::DisableDefaultPolicy() {
switch (sandbox_type_) {
case sandbox::mojom::Sandbox::kAudio:
return true;
case sandbox::mojom::Sandbox::kXrCompositing:
return true;
case sandbox::mojom::Sandbox::kMediaFoundationCdm:
return true;
case sandbox::mojom::Sandbox::kNetwork:
return true;
case sandbox::mojom::Sandbox::kWindowsSystemProxyResolver:
return true;
default:
return false;
}
}
bool UtilitySandboxedProcessLauncherDelegate::ShouldLaunchElevated() {
return sandbox_type_ ==
sandbox::mojom::Sandbox::kNoSandboxAndElevatedPrivileges;
}
bool UtilitySandboxedProcessLauncherDelegate::InitializeConfig(
sandbox::TargetConfig* config) {
DCHECK(!config->IsConfigured());
if (sandbox_type_ == sandbox::mojom::Sandbox::kAudio) {
if (!AudioInitializeConfig(config)) {
return false;
}
}
if (sandbox_type_ == sandbox::mojom::Sandbox::kNetwork) {
if (!NetworkInitializeConfig(config)) {
return false;
}
}
if (sandbox_type_ == sandbox::mojom::Sandbox::kIconReader) {
if (!IconReaderInitializeConfig(config)) {
return false;
}
}
if (sandbox_type_ == sandbox::mojom::Sandbox::kXrCompositing) {
if (!XrCompositingInitializeConfig(config, cmd_line_, sandbox_type_)) {
return false;
}
}
#if BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)
if (sandbox_type_ == sandbox::mojom::Sandbox::kScreenAI) {
if (!ScreenAIInitializeConfig(config, sandbox_type_)) {
return false;
}
}
#endif
if (sandbox_type_ == sandbox::mojom::Sandbox::kSpeechRecognition) {
auto result = config->SetIntegrityLevel(sandbox::INTEGRITY_LEVEL_LOW);
if (result != sandbox::SBOX_ALL_OK) {
return false;
}
config->SetDelayedIntegrityLevel(sandbox::INTEGRITY_LEVEL_LOW);
result = config->SetTokenLevel(sandbox::USER_RESTRICTED_SAME_ACCESS,
sandbox::USER_LIMITED);
if (result != sandbox::SBOX_ALL_OK) {
return false;
}
}
if (sandbox_type_ == sandbox::mojom::Sandbox::kMediaFoundationCdm ||
sandbox_type_ == sandbox::mojom::Sandbox::kWindowsSystemProxyResolver) {
auto result = config->SetTokenLevel(sandbox::USER_UNPROTECTED,
sandbox::USER_UNPROTECTED);
if (result != sandbox::SBOX_ALL_OK) {
return false;
}
}
if (sandbox_type_ == sandbox::mojom::Sandbox::kService ||
sandbox_type_ == sandbox::mojom::Sandbox::kServiceWithJit ||
sandbox_type_ == sandbox::mojom::Sandbox::kFileUtil) {
auto result = sandbox::policy::SandboxWin::AddWin32kLockdownPolicy(config);
if (result != sandbox::SBOX_ALL_OK) {
return false;
}
}
if (sandbox_type_ == sandbox::mojom::Sandbox::kService ||
sandbox_type_ == sandbox::mojom::Sandbox::kFileUtil) {
auto delayed_flags = config->GetDelayedProcessMitigations();
delayed_flags |= sandbox::MITIGATION_DYNAMIC_CODE_DISABLE;
auto result = config->SetDelayedProcessMitigations(delayed_flags);
if (result != sandbox::SBOX_ALL_OK) {
return false;
}
}
#if BUILDFLAG(ENABLE_OOP_PRINTING)
if (sandbox_type_ == sandbox::mojom::Sandbox::kPrintBackend) {
if (!PrintBackendInitializeConfig(config)) {
return false;
}
}
#endif
return GetContentClient()->browser()->PreSpawnChild(
config, sandbox_type_,
ContentBrowserClient::ChildSpawnFlags::kChildSpawnFlagNone);
}
bool UtilitySandboxedProcessLauncherDelegate::ShouldUnsandboxedRunInJob() {
auto utility_sub_type =
cmd_line_.GetSwitchValueASCII(switches::kUtilitySubType);
if (utility_sub_type == network::mojom::NetworkService::Name_)
return true;
return false;
}
bool UtilitySandboxedProcessLauncherDelegate::CetCompatible() {
if (sandbox_type_ == sandbox::mojom::Sandbox::kServiceWithJit)
return false;
auto utility_sub_type =
cmd_line_.GetSwitchValueASCII(switches::kUtilitySubType);
return GetContentClient()->browser()->IsUtilityCetCompatible(
utility_sub_type);
}
bool UtilitySandboxedProcessLauncherDelegate::AllowWindowsFontsDir() {
if (sandbox_type_ == sandbox::mojom::Sandbox::kPrintCompositor) {
return true;
}
return false;
}
}