#ifndef SANDBOX_POLICY_WIN_SANDBOX_WIN_H_
#define SANDBOX_POLICY_WIN_SANDBOX_WIN_H_
#include <stdint.h>
#include <optional>
#include <string>
#include <string_view>
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/process/launch.h"
#include "base/process/process_handle.h"
#include "base/timer/elapsed_timer.h"
#include "base/win/scoped_process_information.h"
#include "build/build_config.h"
#include "sandbox/policy/export.h"
#include "sandbox/policy/sandbox_delegate.h"
#include "sandbox/policy/sandbox_type.h"
#include "sandbox/win/src/sandbox_types.h"
#include "sandbox/win/src/security_level.h"
namespace base {
class CommandLine;
class Value;
}
namespace sandbox {
class BrokerServices;
class TargetConfig;
class TargetPolicy;
class TargetServices;
namespace mojom {
enum class Sandbox;
}
}
namespace sandbox {
namespace policy {
class SANDBOX_POLICY_EXPORT SandboxLaunchTimer final {
public:
SandboxLaunchTimer() = default;
SandboxLaunchTimer(const SandboxLaunchTimer&) = delete;
SandboxLaunchTimer(SandboxLaunchTimer&& other) = default;
SandboxLaunchTimer& operator=(const SandboxLaunchTimer&) = delete;
void OnPolicyCreated() { policy_created_ = timer_.Elapsed(); }
void OnPolicyGenerated() { policy_generated_ = timer_.Elapsed(); }
void OnProcessSpawned() { process_spawned_ = timer_.Elapsed(); }
void OnProcessResumed() { process_resumed_ = timer_.Elapsed(); }
int64_t GetStartTimeInMicroseconds() const {
return timer_.start_time().since_origin().InMicroseconds();
}
void RecordHistograms();
private:
base::ElapsedTimer timer_;
base::TimeDelta policy_created_;
base::TimeDelta policy_generated_;
base::TimeDelta process_spawned_;
base::TimeDelta process_resumed_;
};
class SANDBOX_POLICY_EXPORT SandboxWin {
public:
static ResultCode StartSandboxedProcess(
const base::CommandLine& cmd_line,
const base::HandlesToInheritVector& handles_to_inherit,
SandboxDelegate* delegate,
StartSandboxedProcessCallback result_callback);
static ResultCode GeneratePolicyForSandboxedProcess(
const base::CommandLine& cmd_line,
const base::HandlesToInheritVector& handles_to_inherit,
SandboxDelegate* delegate,
TargetPolicy* policy);
static ResultCode SetJobLevel(sandbox::mojom::Sandbox sandbox_type,
JobLevel job_level,
uint32_t ui_exceptions,
TargetConfig* config);
static void AddBaseHandleClosePolicy(TargetConfig* config);
static ResultCode AddAppContainerPolicy(TargetConfig* config,
const wchar_t* sid);
static ResultCode AddWin32kLockdownPolicy(TargetConfig* config);
static ResultCode AddAppContainerProfileToConfig(
const base::CommandLine& command_line,
sandbox::mojom::Sandbox sandbox_type,
const std::string& appcontainer_id,
TargetConfig* config);
static bool IsAppContainerEnabledForSandbox(
const base::CommandLine& command_line,
sandbox::mojom::Sandbox sandbox_type);
static bool InitBrokerServices(BrokerServices* broker_services);
static bool InitTargetServices(TargetServices* target_services);
static ResultCode GetPolicyDiagnostics(
base::OnceCallback<void(base::Value)> response);
static std::string GetSandboxTypeInEnglish(
const std::optional<sandbox::mojom::Sandbox>& sandbox_type);
static std::string GetSandboxTagForDelegate(
std::string_view prefix,
sandbox::mojom::Sandbox sandbox_type);
private:
FRIEND_TEST_ALL_PREFIXES(SandboxWinTest, GetJobMemoryLimit);
static void FinishStartSandboxedProcess(
SandboxDelegate* delegate,
SandboxLaunchTimer timer,
StartSandboxedProcessCallback result_callback,
base::win::ScopedProcessInformation target,
DWORD last_error,
ResultCode result);
static std::optional<size_t> GetJobMemoryLimit(
sandbox::mojom::Sandbox sandbox_type);
};
SANDBOX_POLICY_EXPORT
void BlocklistAddOneDllForTesting(const wchar_t* module_name,
TargetConfig* config);
}
}
#endif