Expose more atom_bindings.cc functions to sandbox

This commit is contained in:
Thiago de Arruda 2017-05-01 09:08:41 -03:00
parent 9e0c308b09
commit bbe21cce67
4 changed files with 59 additions and 45 deletions

View file

@ -23,51 +23,6 @@ namespace {
// Dummy class type that used for crashing the program. // Dummy class type that used for crashing the program.
struct DummyClass { bool crash; }; struct DummyClass { bool crash; };
void Hang() {
for (;;)
base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1));
}
v8::Local<v8::Value> GetProcessMemoryInfo(v8::Isolate* isolate) {
std::unique_ptr<base::ProcessMetrics> metrics(
base::ProcessMetrics::CreateCurrentProcessMetrics());
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
dict.Set("workingSetSize",
static_cast<double>(metrics->GetWorkingSetSize() >> 10));
dict.Set("peakWorkingSetSize",
static_cast<double>(metrics->GetPeakWorkingSetSize() >> 10));
size_t private_bytes, shared_bytes;
if (metrics->GetMemoryBytes(&private_bytes, &shared_bytes)) {
dict.Set("privateBytes", static_cast<double>(private_bytes >> 10));
dict.Set("sharedBytes", static_cast<double>(shared_bytes >> 10));
}
return dict.GetHandle();
}
v8::Local<v8::Value> GetSystemMemoryInfo(v8::Isolate* isolate,
mate::Arguments* args) {
base::SystemMemoryInfoKB mem_info;
if (!base::GetSystemMemoryInfo(&mem_info)) {
args->ThrowError("Unable to retrieve system memory information");
return v8::Undefined(isolate);
}
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
dict.Set("total", mem_info.total);
dict.Set("free", mem_info.free);
// NB: These return bogus values on macOS
#if !defined(OS_MACOSX)
dict.Set("swapTotal", mem_info.swap_total);
dict.Set("swapFree", mem_info.swap_free);
#endif
return dict.GetHandle();
}
// Called when there is a fatal error in V8, we just crash the process here so // Called when there is a fatal error in V8, we just crash the process here so
// we can get the stack trace. // we can get the stack trace.
void FatalErrorCallback(const char* location, const char* message) { void FatalErrorCallback(const char* location, const char* message) {
@ -168,4 +123,52 @@ void AtomBindings::Crash() {
static_cast<DummyClass*>(nullptr)->crash = true; static_cast<DummyClass*>(nullptr)->crash = true;
} }
// static
void AtomBindings::Hang() {
for (;;)
base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1));
}
// static
v8::Local<v8::Value> AtomBindings::GetProcessMemoryInfo(v8::Isolate* isolate) {
std::unique_ptr<base::ProcessMetrics> metrics(
base::ProcessMetrics::CreateCurrentProcessMetrics());
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
dict.Set("workingSetSize",
static_cast<double>(metrics->GetWorkingSetSize() >> 10));
dict.Set("peakWorkingSetSize",
static_cast<double>(metrics->GetPeakWorkingSetSize() >> 10));
size_t private_bytes, shared_bytes;
if (metrics->GetMemoryBytes(&private_bytes, &shared_bytes)) {
dict.Set("privateBytes", static_cast<double>(private_bytes >> 10));
dict.Set("sharedBytes", static_cast<double>(shared_bytes >> 10));
}
return dict.GetHandle();
}
// static
v8::Local<v8::Value> AtomBindings::GetSystemMemoryInfo(v8::Isolate* isolate,
mate::Arguments* args) {
base::SystemMemoryInfoKB mem_info;
if (!base::GetSystemMemoryInfo(&mem_info)) {
args->ThrowError("Unable to retrieve system memory information");
return v8::Undefined(isolate);
}
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
dict.Set("total", mem_info.total);
dict.Set("free", mem_info.free);
// NB: These return bogus values on macOS
#if !defined(OS_MACOSX)
dict.Set("swapTotal", mem_info.swap_total);
dict.Set("swapFree", mem_info.swap_free);
#endif
return dict.GetHandle();
}
} // namespace atom } // namespace atom

View file

@ -9,6 +9,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "native_mate/arguments.h"
#include "v8/include/v8.h" #include "v8/include/v8.h"
#include "vendor/node/deps/uv/include/uv.h" #include "vendor/node/deps/uv/include/uv.h"
@ -32,6 +33,10 @@ class AtomBindings {
static void Log(const base::string16& message); static void Log(const base::string16& message);
static void Crash(); static void Crash();
static void Hang();
static v8::Local<v8::Value> GetProcessMemoryInfo(v8::Isolate* isolate);
static v8::Local<v8::Value> GetSystemMemoryInfo(v8::Isolate* isolate,
mate::Arguments* args);
private: private:
void ActivateUVLoop(v8::Isolate* isolate); void ActivateUVLoop(v8::Isolate* isolate);

View file

@ -86,6 +86,9 @@ void InitializeBindings(v8::Local<v8::Object> binding,
mate::Dictionary b(isolate, binding); mate::Dictionary b(isolate, binding);
b.SetMethod("get", GetBinding); b.SetMethod("get", GetBinding);
b.SetMethod("crash", AtomBindings::Crash); b.SetMethod("crash", AtomBindings::Crash);
b.SetMethod("hang", AtomBindings::Hang);
b.SetMethod("getProcessMemoryInfo", &AtomBindings::GetProcessMemoryInfo);
b.SetMethod("getSystemMemoryInfo", &AtomBindings::GetSystemMemoryInfo);
} }
class AtomSandboxedRenderViewObserver : public AtomRenderViewObserver { class AtomSandboxedRenderViewObserver : public AtomRenderViewObserver {

View file

@ -38,6 +38,9 @@ const preloadSrc = fs.readFileSync(preloadPath).toString()
// access to things like `process.atomBinding`). // access to things like `process.atomBinding`).
const preloadProcess = new events.EventEmitter() const preloadProcess = new events.EventEmitter()
preloadProcess.crash = () => binding.crash() preloadProcess.crash = () => binding.crash()
preloadProcess.hang = () => binding.hang()
preloadProcess.getProcessMemoryInfo = () => binding.getProcessMemoryInfo()
preloadProcess.getSystemMemoryInfo = () => binding.getSystemMemoryInfo()
process.platform = preloadProcess.platform = electron.remote.process.platform process.platform = preloadProcess.platform = electron.remote.process.platform
process.execPath = preloadProcess.execPath = electron.remote.process.execPath process.execPath = preloadProcess.execPath = electron.remote.process.execPath
process.on('exit', () => preloadProcess.emit('exit')) process.on('exit', () => preloadProcess.emit('exit'))