Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* vim: set ts=8 sts=4 et sw=4 tw=99: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /* Implement global service to track stack of JSContext. */ |
michael@0 | 8 | |
michael@0 | 9 | #include "xpcprivate.h" |
michael@0 | 10 | #include "XPCWrapper.h" |
michael@0 | 11 | #include "nsDOMJSUtils.h" |
michael@0 | 12 | #include "nsNullPrincipal.h" |
michael@0 | 13 | #include "mozilla/dom/BindingUtils.h" |
michael@0 | 14 | |
michael@0 | 15 | using namespace mozilla; |
michael@0 | 16 | using namespace JS; |
michael@0 | 17 | using namespace xpc; |
michael@0 | 18 | using mozilla::dom::DestroyProtoAndIfaceCache; |
michael@0 | 19 | |
michael@0 | 20 | /***************************************************************************/ |
michael@0 | 21 | |
michael@0 | 22 | XPCJSContextStack::~XPCJSContextStack() |
michael@0 | 23 | { |
michael@0 | 24 | if (mSafeJSContext) { |
michael@0 | 25 | mSafeJSContextGlobal = nullptr; |
michael@0 | 26 | JS_DestroyContextNoGC(mSafeJSContext); |
michael@0 | 27 | mSafeJSContext = nullptr; |
michael@0 | 28 | } |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | JSContext* |
michael@0 | 32 | XPCJSContextStack::Pop() |
michael@0 | 33 | { |
michael@0 | 34 | MOZ_ASSERT(!mStack.IsEmpty()); |
michael@0 | 35 | |
michael@0 | 36 | uint32_t idx = mStack.Length() - 1; // The thing we're popping |
michael@0 | 37 | |
michael@0 | 38 | JSContext *cx = mStack[idx].cx; |
michael@0 | 39 | |
michael@0 | 40 | mStack.RemoveElementAt(idx); |
michael@0 | 41 | if (idx == 0) { |
michael@0 | 42 | js::Debug_SetActiveJSContext(mRuntime->Runtime(), nullptr); |
michael@0 | 43 | return cx; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | --idx; // Advance to new top of the stack |
michael@0 | 47 | |
michael@0 | 48 | XPCJSContextInfo &e = mStack[idx]; |
michael@0 | 49 | if (e.cx && e.savedFrameChain) { |
michael@0 | 50 | // Pop() can be called outside any request for e.cx. |
michael@0 | 51 | JSAutoRequest ar(e.cx); |
michael@0 | 52 | JS_RestoreFrameChain(e.cx); |
michael@0 | 53 | e.savedFrameChain = false; |
michael@0 | 54 | } |
michael@0 | 55 | js::Debug_SetActiveJSContext(mRuntime->Runtime(), e.cx); |
michael@0 | 56 | return cx; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | bool |
michael@0 | 60 | XPCJSContextStack::Push(JSContext *cx) |
michael@0 | 61 | { |
michael@0 | 62 | js::Debug_SetActiveJSContext(mRuntime->Runtime(), cx); |
michael@0 | 63 | if (mStack.Length() == 0) { |
michael@0 | 64 | mStack.AppendElement(cx); |
michael@0 | 65 | return true; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | XPCJSContextInfo &e = mStack[mStack.Length() - 1]; |
michael@0 | 69 | if (e.cx) { |
michael@0 | 70 | // The cx we're pushing is also stack-top. In general we still need to |
michael@0 | 71 | // call JS_SaveFrameChain here. But if that would put us in a |
michael@0 | 72 | // compartment that's same-origin with the current one, we can skip it. |
michael@0 | 73 | nsIScriptSecurityManager* ssm = XPCWrapper::GetSecurityManager(); |
michael@0 | 74 | if ((e.cx == cx) && ssm) { |
michael@0 | 75 | // DOM JSContexts don't store their default compartment object on |
michael@0 | 76 | // the cx, so in those cases we need to fetch it via the scx |
michael@0 | 77 | // instead. And in some cases (i.e. the SafeJSContext), we have no |
michael@0 | 78 | // default compartment object at all. |
michael@0 | 79 | RootedObject defaultScope(cx, GetDefaultScopeFromJSContext(cx)); |
michael@0 | 80 | if (defaultScope) { |
michael@0 | 81 | nsIPrincipal *currentPrincipal = |
michael@0 | 82 | GetCompartmentPrincipal(js::GetContextCompartment(cx)); |
michael@0 | 83 | nsIPrincipal *defaultPrincipal = GetObjectPrincipal(defaultScope); |
michael@0 | 84 | if (currentPrincipal->Equals(defaultPrincipal)) { |
michael@0 | 85 | mStack.AppendElement(cx); |
michael@0 | 86 | return true; |
michael@0 | 87 | } |
michael@0 | 88 | } |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | { |
michael@0 | 92 | // Push() can be called outside any request for e.cx. |
michael@0 | 93 | JSAutoRequest ar(e.cx); |
michael@0 | 94 | if (!JS_SaveFrameChain(e.cx)) |
michael@0 | 95 | return false; |
michael@0 | 96 | e.savedFrameChain = true; |
michael@0 | 97 | } |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | mStack.AppendElement(cx); |
michael@0 | 101 | return true; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | bool |
michael@0 | 105 | XPCJSContextStack::HasJSContext(JSContext *cx) |
michael@0 | 106 | { |
michael@0 | 107 | for (uint32_t i = 0; i < mStack.Length(); i++) |
michael@0 | 108 | if (cx == mStack[i].cx) |
michael@0 | 109 | return true; |
michael@0 | 110 | return false; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | static bool |
michael@0 | 114 | SafeGlobalResolve(JSContext *cx, HandleObject obj, HandleId id) |
michael@0 | 115 | { |
michael@0 | 116 | bool resolved; |
michael@0 | 117 | return JS_ResolveStandardClass(cx, obj, id, &resolved); |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | static void |
michael@0 | 121 | SafeFinalize(JSFreeOp *fop, JSObject* obj) |
michael@0 | 122 | { |
michael@0 | 123 | SandboxPrivate* sop = |
michael@0 | 124 | static_cast<SandboxPrivate*>(xpc_GetJSPrivate(obj)); |
michael@0 | 125 | sop->ForgetGlobalObject(); |
michael@0 | 126 | NS_IF_RELEASE(sop); |
michael@0 | 127 | DestroyProtoAndIfaceCache(obj); |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | const JSClass xpc::SafeJSContextGlobalClass = { |
michael@0 | 131 | "global_for_XPCJSContextStack_SafeJSContext", |
michael@0 | 132 | XPCONNECT_GLOBAL_FLAGS, |
michael@0 | 133 | JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub, |
michael@0 | 134 | JS_EnumerateStub, SafeGlobalResolve, JS_ConvertStub, SafeFinalize, |
michael@0 | 135 | nullptr, nullptr, nullptr, JS_GlobalObjectTraceHook |
michael@0 | 136 | }; |
michael@0 | 137 | |
michael@0 | 138 | JSContext* |
michael@0 | 139 | XPCJSContextStack::GetSafeJSContext() |
michael@0 | 140 | { |
michael@0 | 141 | MOZ_ASSERT(mSafeJSContext); |
michael@0 | 142 | return mSafeJSContext; |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | JSObject* |
michael@0 | 146 | XPCJSContextStack::GetSafeJSContextGlobal() |
michael@0 | 147 | { |
michael@0 | 148 | MOZ_ASSERT(mSafeJSContextGlobal); |
michael@0 | 149 | return mSafeJSContextGlobal; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | JSContext* |
michael@0 | 153 | XPCJSContextStack::InitSafeJSContext() |
michael@0 | 154 | { |
michael@0 | 155 | MOZ_ASSERT(!mSafeJSContext); |
michael@0 | 156 | |
michael@0 | 157 | // Start by getting the principal holder and principal for this |
michael@0 | 158 | // context. If we can't manage that, don't bother with the rest. |
michael@0 | 159 | nsRefPtr<nsNullPrincipal> principal = new nsNullPrincipal(); |
michael@0 | 160 | nsresult rv = principal->Init(); |
michael@0 | 161 | if (NS_FAILED(rv)) |
michael@0 | 162 | MOZ_CRASH(); |
michael@0 | 163 | |
michael@0 | 164 | nsXPConnect* xpc = nsXPConnect::XPConnect(); |
michael@0 | 165 | JSRuntime *rt = xpc->GetRuntime()->Runtime(); |
michael@0 | 166 | if (!rt) |
michael@0 | 167 | MOZ_CRASH(); |
michael@0 | 168 | |
michael@0 | 169 | mSafeJSContext = JS_NewContext(rt, 8192); |
michael@0 | 170 | if (!mSafeJSContext) |
michael@0 | 171 | MOZ_CRASH(); |
michael@0 | 172 | JSAutoRequest req(mSafeJSContext); |
michael@0 | 173 | ContextOptionsRef(mSafeJSContext).setNoDefaultCompartmentObject(true); |
michael@0 | 174 | |
michael@0 | 175 | JS_SetErrorReporter(mSafeJSContext, xpc::SystemErrorReporter); |
michael@0 | 176 | |
michael@0 | 177 | JS::CompartmentOptions options; |
michael@0 | 178 | options.setZone(JS::SystemZone) |
michael@0 | 179 | .setTrace(TraceXPCGlobal); |
michael@0 | 180 | mSafeJSContextGlobal = CreateGlobalObject(mSafeJSContext, |
michael@0 | 181 | &SafeJSContextGlobalClass, |
michael@0 | 182 | principal, options); |
michael@0 | 183 | if (!mSafeJSContextGlobal) |
michael@0 | 184 | MOZ_CRASH(); |
michael@0 | 185 | |
michael@0 | 186 | // Note: make sure to set the private before calling |
michael@0 | 187 | // InitClasses |
michael@0 | 188 | nsRefPtr<SandboxPrivate> sp = new SandboxPrivate(principal, mSafeJSContextGlobal); |
michael@0 | 189 | JS_SetPrivate(mSafeJSContextGlobal, sp.forget().take()); |
michael@0 | 190 | |
michael@0 | 191 | // After this point either glob is null and the |
michael@0 | 192 | // nsIScriptObjectPrincipal ownership is either handled by the |
michael@0 | 193 | // nsCOMPtr or dealt with, or we'll release in the finalize |
michael@0 | 194 | // hook. |
michael@0 | 195 | if (NS_FAILED(xpc->InitClasses(mSafeJSContext, mSafeJSContextGlobal))) |
michael@0 | 196 | MOZ_CRASH(); |
michael@0 | 197 | |
michael@0 | 198 | JS::RootedObject glob(mSafeJSContext, mSafeJSContextGlobal); |
michael@0 | 199 | JS_FireOnNewGlobalObject(mSafeJSContext, glob); |
michael@0 | 200 | |
michael@0 | 201 | return mSafeJSContext; |
michael@0 | 202 | } |