2013-07-03 11:24:32 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=4 sw=4 et tw=80:
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "JavaScriptParent.h"
|
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2016-01-06 03:28:27 +03:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2013-07-03 11:24:32 +04:00
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "jsfriendapi.h"
|
2013-08-18 02:50:18 +04:00
|
|
|
#include "jswrapper.h"
|
2015-02-10 21:58:18 +03:00
|
|
|
#include "js/Proxy.h"
|
2015-08-06 09:34:08 +03:00
|
|
|
#include "js/HeapAPI.h"
|
2013-07-03 11:24:32 +04:00
|
|
|
#include "xpcprivate.h"
|
|
|
|
#include "mozilla/Casting.h"
|
2016-02-16 19:21:11 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2013-07-03 11:24:32 +04:00
|
|
|
|
|
|
|
using namespace js;
|
|
|
|
using namespace JS;
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::jsipc;
|
2013-06-03 14:14:37 +04:00
|
|
|
using namespace mozilla::dom;
|
2013-07-03 11:24:32 +04:00
|
|
|
|
2014-05-17 03:40:37 +04:00
|
|
|
static void
|
2015-03-29 01:22:11 +03:00
|
|
|
TraceParent(JSTracer* trc, void* data)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2015-03-29 01:22:11 +03:00
|
|
|
static_cast<JavaScriptParent*>(data)->trace(trc);
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
JavaScriptParent::JavaScriptParent(JSRuntime* rt)
|
2014-05-17 03:40:37 +04:00
|
|
|
: JavaScriptShared(rt),
|
|
|
|
JavaScriptBase<PJavaScriptParent>(rt)
|
2013-07-11 19:23:34 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-17 03:40:37 +04:00
|
|
|
JavaScriptParent::~JavaScriptParent()
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2014-05-17 03:40:37 +04:00
|
|
|
JS_RemoveExtraGCRootsTracer(rt_, TraceParent, this);
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-05-17 03:40:37 +04:00
|
|
|
JavaScriptParent::init()
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2014-05-17 03:40:37 +04:00
|
|
|
if (!WrapperOwner::init())
|
2013-07-03 11:24:32 +04:00
|
|
|
return false;
|
|
|
|
|
2014-05-17 03:40:37 +04:00
|
|
|
JS_AddExtraGCRootsTracer(rt_, TraceParent, this);
|
2013-07-03 11:24:32 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-24 02:31:54 +03:00
|
|
|
static bool
|
|
|
|
ForbidUnsafeBrowserCPOWs()
|
|
|
|
{
|
|
|
|
static bool result;
|
|
|
|
static bool cached = false;
|
|
|
|
if (!cached) {
|
|
|
|
cached = true;
|
|
|
|
Preferences::AddBoolVarCache(&result, "dom.ipc.cpows.forbid-unsafe-from-browser", false);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
JavaScriptParent::allowMessage(JSContext* cx)
|
|
|
|
{
|
|
|
|
MessageChannel* channel = GetIPCChannel();
|
|
|
|
if (channel->IsInTransaction())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (ForbidUnsafeBrowserCPOWs()) {
|
2016-01-06 03:28:27 +03:00
|
|
|
nsIGlobalObject* global = dom::GetIncumbentGlobal();
|
|
|
|
JSObject* jsGlobal = global ? global->GetGlobalJSObject() : nullptr;
|
|
|
|
if (jsGlobal) {
|
|
|
|
JSAutoCompartment ac(cx, jsGlobal);
|
|
|
|
if (!JS::AddonIdOfObject(jsGlobal) && !xpc::CompartmentPrivate::Get(jsGlobal)->allowCPOWs) {
|
2016-02-16 19:21:11 +03:00
|
|
|
Telemetry::Accumulate(Telemetry::BROWSER_SHIM_USAGE_BLOCKED, 1);
|
2015-10-24 02:31:54 +03:00
|
|
|
JS_ReportError(cx, "unsafe CPOW usage forbidden");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool disableUnsafeCPOWWarnings = PR_GetEnv("DISABLE_UNSAFE_CPOW_WARNINGS");
|
|
|
|
if (!disableUnsafeCPOWWarnings) {
|
|
|
|
nsCOMPtr<nsIConsoleService> console(do_GetService(NS_CONSOLESERVICE_CONTRACTID));
|
|
|
|
if (console && cx) {
|
|
|
|
nsAutoString filename;
|
|
|
|
uint32_t lineno = 0, column = 0;
|
|
|
|
nsJSUtils::GetCallingLocation(cx, filename, &lineno, &column);
|
|
|
|
nsCOMPtr<nsIScriptError> error(do_CreateInstance(NS_SCRIPTERROR_CONTRACTID));
|
|
|
|
error->Init(NS_LITERAL_STRING("unsafe CPOW usage"), filename,
|
|
|
|
EmptyString(), lineno, column,
|
|
|
|
nsIScriptError::warningFlag, "chrome javascript");
|
|
|
|
console->LogMessage(error);
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Unsafe synchronous IPC message");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-03 11:24:32 +04:00
|
|
|
void
|
2015-03-29 01:22:11 +03:00
|
|
|
JavaScriptParent::trace(JSTracer* trc)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2015-01-16 20:56:30 +03:00
|
|
|
objects_.trace(trc);
|
|
|
|
unwaivedObjectIds_.trace(trc);
|
|
|
|
waivedObjectIds_.trace(trc);
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
JSObject*
|
2014-09-13 04:41:18 +04:00
|
|
|
JavaScriptParent::scopeForTargetObjects()
|
2014-08-20 23:49:10 +04:00
|
|
|
{
|
2014-09-13 04:41:18 +04:00
|
|
|
// CPWOWs from the child need to point into the parent's unprivileged junk
|
|
|
|
// scope so that a compromised child cannot compromise the parent. In
|
|
|
|
// practice, this means that a child process can only (a) hold parent
|
|
|
|
// objects alive and (b) invoke them if they are callable.
|
2014-08-20 23:49:10 +04:00
|
|
|
return xpc::UnprivilegedJunkScope();
|
|
|
|
}
|
|
|
|
|
2013-06-03 14:14:37 +04:00
|
|
|
mozilla::ipc::IProtocol*
|
|
|
|
JavaScriptParent::CloneProtocol(Channel* aChannel, ProtocolCloneContext* aCtx)
|
|
|
|
{
|
2015-03-29 01:22:11 +03:00
|
|
|
ContentParent* contentParent = aCtx->GetContentParent();
|
2013-06-03 14:14:37 +04:00
|
|
|
nsAutoPtr<PJavaScriptParent> actor(contentParent->AllocPJavaScriptParent());
|
|
|
|
if (!actor || !contentParent->RecvPJavaScriptConstructor(actor)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return actor.forget();
|
|
|
|
}
|
2015-01-27 00:32:18 +03:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
PJavaScriptParent*
|
|
|
|
mozilla::jsipc::NewJavaScriptParent(JSRuntime* rt)
|
2015-01-27 00:32:18 +03:00
|
|
|
{
|
2015-03-29 01:22:11 +03:00
|
|
|
JavaScriptParent* parent = new JavaScriptParent(rt);
|
2015-01-27 00:32:18 +03:00
|
|
|
if (!parent->init()) {
|
|
|
|
delete parent;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-03-29 01:22:11 +03:00
|
|
|
mozilla::jsipc::ReleaseJavaScriptParent(PJavaScriptParent* parent)
|
2015-01-27 00:32:18 +03:00
|
|
|
{
|
2015-03-29 01:22:11 +03:00
|
|
|
static_cast<JavaScriptParent*>(parent)->decref();
|
2015-01-27 00:32:18 +03:00
|
|
|
}
|