2019-06-14 19:39:55 +00:00
|
|
|
// Copyright (c) 2019 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/api/process_metric.h"
|
2019-06-14 19:39:55 +00:00
|
|
|
|
|
|
|
#include <memory>
|
2024-01-10 22:23:35 +00:00
|
|
|
#include <optional>
|
2019-06-14 19:39:55 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2019-06-14 19:39:55 +00:00
|
|
|
#include <windows.h>
|
2019-07-23 20:41:59 +00:00
|
|
|
|
|
|
|
#include <psapi.h>
|
|
|
|
#include "base/win/win_util.h"
|
2019-06-14 19:39:55 +00:00
|
|
|
#endif
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2019-07-23 20:41:59 +00:00
|
|
|
#include <mach/mach.h>
|
|
|
|
#include "base/process/port_provider_mac.h"
|
|
|
|
#include "content/public/browser/browser_child_process_host.h"
|
|
|
|
|
2019-06-14 19:39:55 +00:00
|
|
|
extern "C" int sandbox_check(pid_t pid, const char* operation, int type, ...);
|
2019-07-23 20:41:59 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
mach_port_t TaskForPid(pid_t pid) {
|
|
|
|
mach_port_t task = MACH_PORT_NULL;
|
|
|
|
if (auto* port_provider = content::BrowserChildProcessHost::GetPortProvider())
|
2024-02-14 17:33:32 +00:00
|
|
|
task = port_provider->TaskForHandle(pid);
|
2019-07-23 20:41:59 +00:00
|
|
|
if (task == MACH_PORT_NULL && pid == getpid())
|
|
|
|
task = mach_task_self();
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
|
2024-01-10 22:23:35 +00:00
|
|
|
std::optional<mach_task_basic_info_data_t> GetTaskInfo(mach_port_t task) {
|
2019-07-23 20:41:59 +00:00
|
|
|
if (task == MACH_PORT_NULL)
|
2024-01-10 22:23:35 +00:00
|
|
|
return std::nullopt;
|
2019-07-23 20:41:59 +00:00
|
|
|
mach_task_basic_info_data_t info = {};
|
|
|
|
mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
|
|
|
|
kern_return_t kr = task_info(task, MACH_TASK_BASIC_INFO,
|
|
|
|
reinterpret_cast<task_info_t>(&info), &count);
|
2024-01-10 22:23:35 +00:00
|
|
|
return (kr == KERN_SUCCESS) ? std::make_optional(info) : std::nullopt;
|
2019-07-23 20:41:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#endif // BUILDFLAG(IS_MAC)
|
2019-06-14 19:39:55 +00:00
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
|
|
|
ProcessMetric::ProcessMetric(int type,
|
|
|
|
base::ProcessHandle handle,
|
2020-07-07 18:00:45 +00:00
|
|
|
std::unique_ptr<base::ProcessMetrics> metrics,
|
2020-10-19 11:55:47 +00:00
|
|
|
const std::string& service_name,
|
2020-07-07 18:00:45 +00:00
|
|
|
const std::string& name) {
|
2019-06-14 19:39:55 +00:00
|
|
|
this->type = type;
|
|
|
|
this->metrics = std::move(metrics);
|
2020-10-19 11:55:47 +00:00
|
|
|
this->service_name = service_name;
|
2020-07-07 18:00:45 +00:00
|
|
|
this->name = name;
|
2019-06-14 19:39:55 +00:00
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2019-06-14 19:39:55 +00:00
|
|
|
HANDLE duplicate_handle = INVALID_HANDLE_VALUE;
|
|
|
|
::DuplicateHandle(::GetCurrentProcess(), handle, ::GetCurrentProcess(),
|
|
|
|
&duplicate_handle, 0, false, DUPLICATE_SAME_ACCESS);
|
|
|
|
this->process = base::Process(duplicate_handle);
|
|
|
|
#else
|
|
|
|
this->process = base::Process(handle);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
ProcessMetric::~ProcessMetric() = default;
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2019-06-14 19:39:55 +00:00
|
|
|
|
2019-07-23 20:41:59 +00:00
|
|
|
ProcessMemoryInfo ProcessMetric::GetMemoryInfo() const {
|
|
|
|
ProcessMemoryInfo result;
|
|
|
|
|
|
|
|
PROCESS_MEMORY_COUNTERS_EX info = {};
|
|
|
|
if (::GetProcessMemoryInfo(process.Handle(),
|
|
|
|
reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&info),
|
|
|
|
sizeof(info))) {
|
|
|
|
result.working_set_size = info.WorkingSetSize;
|
|
|
|
result.peak_working_set_size = info.PeakWorkingSetSize;
|
|
|
|
result.private_bytes = info.PrivateUsage;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-06-14 19:39:55 +00:00
|
|
|
ProcessIntegrityLevel ProcessMetric::GetIntegrityLevel() const {
|
|
|
|
HANDLE token = nullptr;
|
|
|
|
if (!::OpenProcessToken(process.Handle(), TOKEN_QUERY, &token)) {
|
2020-10-27 17:51:45 +00:00
|
|
|
return ProcessIntegrityLevel::kUnknown;
|
2019-06-14 19:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
base::win::ScopedHandle token_scoped(token);
|
|
|
|
|
|
|
|
DWORD token_info_length = 0;
|
|
|
|
if (::GetTokenInformation(token, TokenIntegrityLevel, nullptr, 0,
|
|
|
|
&token_info_length) ||
|
|
|
|
::GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
|
2020-10-27 17:51:45 +00:00
|
|
|
return ProcessIntegrityLevel::kUnknown;
|
2019-06-14 19:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto token_label_bytes = std::make_unique<char[]>(token_info_length);
|
2020-10-26 18:56:31 +00:00
|
|
|
auto* token_label =
|
2019-06-14 19:39:55 +00:00
|
|
|
reinterpret_cast<TOKEN_MANDATORY_LABEL*>(token_label_bytes.get());
|
|
|
|
if (!::GetTokenInformation(token, TokenIntegrityLevel, token_label,
|
|
|
|
token_info_length, &token_info_length)) {
|
2020-10-27 17:51:45 +00:00
|
|
|
return ProcessIntegrityLevel::kUnknown;
|
2019-06-14 19:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DWORD integrity_level = *::GetSidSubAuthority(
|
|
|
|
token_label->Label.Sid,
|
|
|
|
static_cast<DWORD>(*::GetSidSubAuthorityCount(token_label->Label.Sid) -
|
|
|
|
1));
|
|
|
|
|
|
|
|
if (integrity_level >= SECURITY_MANDATORY_UNTRUSTED_RID &&
|
|
|
|
integrity_level < SECURITY_MANDATORY_LOW_RID) {
|
2020-10-27 17:51:45 +00:00
|
|
|
return ProcessIntegrityLevel::kUntrusted;
|
2019-06-14 19:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (integrity_level >= SECURITY_MANDATORY_LOW_RID &&
|
|
|
|
integrity_level < SECURITY_MANDATORY_MEDIUM_RID) {
|
2020-10-27 17:51:45 +00:00
|
|
|
return ProcessIntegrityLevel::kLow;
|
2019-06-14 19:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (integrity_level >= SECURITY_MANDATORY_MEDIUM_RID &&
|
|
|
|
integrity_level < SECURITY_MANDATORY_HIGH_RID) {
|
2020-10-27 17:51:45 +00:00
|
|
|
return ProcessIntegrityLevel::kMedium;
|
2019-06-14 19:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (integrity_level >= SECURITY_MANDATORY_HIGH_RID &&
|
|
|
|
integrity_level < SECURITY_MANDATORY_SYSTEM_RID) {
|
2020-10-27 17:51:45 +00:00
|
|
|
return ProcessIntegrityLevel::kHigh;
|
2019-06-14 19:39:55 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 17:51:45 +00:00
|
|
|
return ProcessIntegrityLevel::kUnknown;
|
2019-06-14 19:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool ProcessMetric::IsSandboxed(ProcessIntegrityLevel integrity_level) {
|
2020-10-27 17:51:45 +00:00
|
|
|
return integrity_level > ProcessIntegrityLevel::kUnknown &&
|
|
|
|
integrity_level < ProcessIntegrityLevel::kMedium;
|
2019-06-14 19:39:55 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#elif BUILDFLAG(IS_MAC)
|
2019-06-14 19:39:55 +00:00
|
|
|
|
2019-07-23 20:41:59 +00:00
|
|
|
ProcessMemoryInfo ProcessMetric::GetMemoryInfo() const {
|
|
|
|
ProcessMemoryInfo result;
|
|
|
|
|
|
|
|
if (auto info = GetTaskInfo(TaskForPid(process.Pid()))) {
|
|
|
|
result.working_set_size = info->resident_size;
|
|
|
|
result.peak_working_set_size = info->resident_size_max;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-06-14 19:39:55 +00:00
|
|
|
bool ProcessMetric::IsSandboxed() const {
|
2022-11-14 20:46:52 +00:00
|
|
|
#if IS_MAS_BUILD()
|
2019-06-14 19:39:55 +00:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return sandbox_check(process.Pid(), nullptr, 0) != 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#endif // BUILDFLAG(IS_MAC)
|
2019-06-14 19:39:55 +00:00
|
|
|
|
|
|
|
} // namespace electron
|