#include "sandbox/policy/sandbox.h"
#include "base/command_line.h"
#include "base/metrics/histogram_functions.h"
#include "build/build_config.h"
#include "sandbox/policy/mojom/sandbox.mojom.h"
#include "sandbox/policy/switches.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/android/jni_android.h"
#endif
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_OHOS)
#if !defined(COMPONENT_BUILD)
#include "sandbox/policy/linux/sandbox_linux.h"
#endif
#endif
#if BUILDFLAG(IS_MAC)
#include "sandbox/mac/seatbelt.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "base/check_op.h"
#include "base/process/process_info.h"
#include "sandbox/policy/win/sandbox_win.h"
#include "sandbox/win/src/sandbox.h"
#include "sandbox/win/src/sandbox_factory.h"
#include "sandbox/win/src/target_services.h"
#endif
namespace sandbox {
namespace policy {
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_OHOS)
#if !defined(COMPONENT_BUILD)
bool Sandbox::Initialize(sandbox::mojom::Sandbox sandbox_type,
SandboxLinux::PreSandboxHook hook,
const SandboxLinux::Options& options) {
return SandboxLinux::GetInstance()->InitializeSandbox(
sandbox_type, std::move(hook), options);
}
#endif
#endif
#if BUILDFLAG(IS_WIN)
bool Sandbox::Initialize(sandbox::mojom::Sandbox sandbox_type,
SandboxInterfaceInfo* sandbox_info) {
BrokerServices* broker_services = sandbox_info->broker_services;
if (broker_services) {
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
if (!SandboxWin::InitBrokerServices(broker_services))
return false;
if (!command_line.HasSwitch(switches::kNoSandbox)) {
ResultCode result = broker_services->CreateAlternateDesktop(
Desktop::kAlternateWinstation);
CHECK(result == SBOX_ALL_OK);
}
return true;
}
return IsUnsandboxedSandboxType(sandbox_type) ||
SandboxWin::InitTargetServices(sandbox_info->target_services);
}
#endif
bool Sandbox::IsProcessSandboxed() {
auto* command_line = base::CommandLine::ForCurrentProcess();
bool is_browser = !command_line->HasSwitch(switches::kProcessType);
if (!is_browser &&
base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoSandbox)) {
return true;
}
#if BUILDFLAG(IS_ANDROID)
JNIEnv* env = base::android::AttachCurrentThread();
base::android::ScopedJavaLocalRef<jclass> process_class =
base::android::GetClass(env, "android/os/Process");
jmethodID is_isolated =
base::android::MethodID::Get<base::android::MethodID::TYPE_STATIC>(
env, process_class.obj(), "isIsolated", "()Z");
return env->CallStaticBooleanMethod(process_class.obj(), is_isolated);
#elif BUILDFLAG(IS_FUCHSIA)
return !is_browser;
#elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_OHOS)
#if !defined(COMPONENT_BUILD)
int status = SandboxLinux::GetInstance()->GetStatus();
constexpr int kLayer1Flags = SandboxLinux::Status::kSUID |
SandboxLinux::Status::kPIDNS |
SandboxLinux::Status::kUserNS;
constexpr int kLayer2Flags =
SandboxLinux::Status::kSeccompBPF | SandboxLinux::Status::kSeccompTSYNC;
return (status & kLayer1Flags) != 0 && (status & kLayer2Flags) != 0;
#endif
#elif BUILDFLAG(IS_MAC)
return Seatbelt::IsSandboxed();
#elif BUILDFLAG(IS_IOS)
return !is_browser;
#elif BUILDFLAG(IS_WIN)
#if !defined(COMPONENT_BUILD)
auto* target_services = sandbox::SandboxFactory::GetTargetServices();
if (!target_services || !target_services->GetState()->InitCompleted()) {
return false;
}
#endif
const auto integrity_level = base::GetCurrentProcessIntegrityLevel();
return integrity_level != base::INTEGRITY_UNKNOWN &&
integrity_level < base::MEDIUM_INTEGRITY;
#else
return false;
#endif
}
}
}