Bug 820170 - SandboxPrivate. r=bholley

This commit is contained in:
Gabor Krizsanits 2013-04-04 11:27:36 +02:00
parent 015106720f
commit 04057305b5
5 changed files with 69 additions and 56 deletions

View File

@ -15,6 +15,7 @@ EXPORTS = \
xpc_map_end.h \
nsAutoJSValHolder.h \
nsTArrayHelpers.h \
SandboxPrivate.h \
$(NULL)
include $(topsrcdir)/config/rules.mk

View File

@ -0,0 +1,45 @@
/* 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/. */
#ifndef __SANDBOXPRIVATE_H__
#define __SANDBOXPRIVATE_H__
#include "nsIGlobalObject.h"
#include "nsIPrincipal.h"
// This interface is public only because it is used in jsd.
// Once jsd is gone this file should be moved back to xpconnect/src.
class SandboxPrivate : public nsIGlobalObject
{
public:
SandboxPrivate(nsIPrincipal *principal, JSObject *global)
: mPrincipal(principal)
, mGlobalJSObject(global)
{
}
virtual ~SandboxPrivate() { }
NS_DECL_ISUPPORTS
nsIPrincipal *GetPrincipal()
{
return mPrincipal;
}
JSObject *GetGlobalJSObject()
{
return mGlobalJSObject;
}
void ForgetGlobalObject()
{
mGlobalJSObject = NULL;
}
private:
nsCOMPtr<nsIPrincipal> mPrincipal;
JSObject *mGlobalJSObject;
};
#endif // __SANDBOXPRIVATE_H__

View File

@ -2837,13 +2837,7 @@ nsXPCComponents_Utils::ReportError(const JS::Value &error, JSContext *cx)
#include "nsNetUtil.h"
const char kScriptSecurityManagerContractID[] = NS_SCRIPTSECURITYMANAGER_CONTRACTID;
NS_IMPL_THREADSAFE_ISUPPORTS1(PrincipalHolder, nsIScriptObjectPrincipal)
nsIPrincipal *
PrincipalHolder::GetPrincipal()
{
return mHoldee;
}
NS_IMPL_THREADSAFE_ISUPPORTS2(SandboxPrivate, nsIScriptObjectPrincipal, nsIGlobalObject)
static JSBool
SandboxDump(JSContext *cx, unsigned argc, jsval *vp)
@ -2987,7 +2981,9 @@ static void
sandbox_finalize(JSFreeOp *fop, JSObject *obj)
{
nsIScriptObjectPrincipal *sop =
(nsIScriptObjectPrincipal *)xpc_GetJSPrivate(obj);
static_cast<nsIScriptObjectPrincipal *>(xpc_GetJSPrivate(obj));
MOZ_ASSERT(sop);
static_cast<SandboxPrivate *>(sop)->ForgetGlobalObject();
NS_IF_RELEASE(sop);
DestroyProtoAndIfaceCache(obj);
}
@ -3286,12 +3282,12 @@ xpc_CreateSandboxObject(JSContext *cx, jsval *vp, nsISupports *prinOrSop, Sandbo
if (NS_FAILED(rv))
return NS_ERROR_XPC_UNEXPECTED;
nsCOMPtr<nsIScriptObjectPrincipal> sop(do_QueryInterface(prinOrSop));
if (!sop) {
nsCOMPtr<nsIPrincipal> principal(do_QueryInterface(prinOrSop));
nsCOMPtr<nsIPrincipal> principal = do_QueryInterface(prinOrSop);
if (!principal) {
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(prinOrSop);
if (sop) {
principal = sop->GetPrincipal();
} else {
principal = do_CreateInstance("@mozilla.org/nullprincipal;1", &rv);
NS_ASSERTION(NS_FAILED(rv) || principal,
"Bad return from do_CreateInstance");
@ -3304,14 +3300,8 @@ xpc_CreateSandboxObject(JSContext *cx, jsval *vp, nsISupports *prinOrSop, Sandbo
return rv;
}
}
sop = new PrincipalHolder(principal);
if (!sop)
return NS_ERROR_OUT_OF_MEMORY;
MOZ_ASSERT(principal);
}
nsIPrincipal *principal = sop->GetPrincipal();
JSObject *sandbox;
JS::ZoneSpecifier zoneSpec = options.sameZoneAs
@ -3369,8 +3359,11 @@ xpc_CreateSandboxObject(JSContext *cx, jsval *vp, nsISupports *prinOrSop, Sandbo
return NS_ERROR_XPC_UNEXPECTED;
}
// Pass on ownership of sop to |sandbox|.
JS_SetPrivate(sandbox, sop.forget().get());
nsCOMPtr<nsIScriptObjectPrincipal> sbp =
new SandboxPrivate(principal, sandbox);
// Pass on ownership of sbp to |sandbox|.
JS_SetPrivate(sandbox, sbp.forget().get());
XPCCallContext ccx(NATIVE_CALLER, cx);
if (!ccx.IsValid())

View File

@ -124,8 +124,9 @@ SafeGlobalResolve(JSContext *cx, JSHandleObject obj, JSHandleId id)
static void
SafeFinalize(JSFreeOp *fop, JSObject* obj)
{
nsIScriptObjectPrincipal* sop =
static_cast<nsIScriptObjectPrincipal*>(xpc_GetJSPrivate(obj));
SandboxPrivate* sop =
static_cast<SandboxPrivate*>(xpc_GetJSPrivate(obj));
sop->ForgetGlobalObject();
NS_IF_RELEASE(sop);
DestroyProtoAndIfaceCache(obj);
}
@ -156,8 +157,6 @@ XPCJSContextStack::GetSafeJSContext()
if (NS_FAILED(rv))
return NULL;
nsCOMPtr<nsIScriptObjectPrincipal> sop = new PrincipalHolder(principal);
nsRefPtr<nsXPConnect> xpc = nsXPConnect::GetXPConnect();
if (!xpc)
return NULL;
@ -190,9 +189,8 @@ XPCJSContextStack::GetSafeJSContext()
// Note: make sure to set the private before calling
// InitClasses
nsIScriptObjectPrincipal* priv = nullptr;
sop.swap(priv);
JS_SetPrivate(glob, priv);
nsCOMPtr<nsIScriptObjectPrincipal> sop = new SandboxPrivate(principal, glob);
JS_SetPrivate(glob, sop.forget().get());
}
// After this point either glob is null and the

View File

@ -178,6 +178,8 @@
#include "xpcObjectHelper.h"
#include "nsIThreadInternal.h"
#include "SandboxPrivate.h"
#ifdef XP_WIN
// Nasty MS defines
#ifdef GetClassInfo
@ -4164,32 +4166,6 @@ public:
static void GetTraceName(JSTracer* trc, char *buf, size_t bufsize);
};
/***************************************************************************/
#define PRINCIPALHOLDER_IID \
{0xbf109f49, 0xf94a, 0x43d8, {0x93, 0xdb, 0xe4, 0x66, 0x49, 0xc5, 0xd9, 0x7d}}
class PrincipalHolder : public nsIScriptObjectPrincipal
{
public:
NS_DECLARE_STATIC_IID_ACCESSOR(PRINCIPALHOLDER_IID)
PrincipalHolder(nsIPrincipal *holdee)
: mHoldee(holdee)
{
}
virtual ~PrincipalHolder() { }
NS_DECL_ISUPPORTS
nsIPrincipal *GetPrincipal();
private:
nsCOMPtr<nsIPrincipal> mHoldee;
};
NS_DEFINE_STATIC_IID_ACCESSOR(PrincipalHolder, PRINCIPALHOLDER_IID)
/***************************************************************************/
// Utilities