Bug 119646 - Rewrite of the security manager policy database for improved

performance. r=jst, sr=jband.


git-svn-id: svn://10.0.0.236/trunk@114377 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
mstoltz%netscape.com
2002-02-13 04:20:46 +00:00
parent 37652babe4
commit 904896ca95
15 changed files with 1130 additions and 667 deletions

View File

@@ -51,7 +51,7 @@ interface nsIScriptSecurityManager : nsIXPCSecurityManager
[noscript] void checkPropertyAccess(in JSContextPtr aJSContext,
in JSObjectPtr aJSObject,
in string aClassName,
in string aProperty,
in JSVal aProperty,
in PRUint32 aAction);
/**
@@ -60,7 +60,7 @@ interface nsIScriptSecurityManager : nsIXPCSecurityManager
[noscript] void checkConnect(in JSContextPtr aJSContext,
in nsIURI aTargetURI,
in string aClassName,
in string aPropertyName);
in string aProperty);
/**
* Check that the script currently running in context "cx" can load "uri".
@@ -197,6 +197,12 @@ interface nsIScriptSecurityManager : nsIXPCSecurityManager
*/
[noscript] nsIPrincipal getObjectPrincipal(in JSContextPtr cx,
in JSObjectPtr obj);
/**
* Forget all currently stored security policies and reread from prefs.
* This must be called after any capability.policy prefs have changed.
*/
void reloadSecurityPolicies();
};
%{C++

View File

@@ -20,7 +20,8 @@
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Norris Boyd <nboyd@atg.com>
* Mitch Stoltz <mstoltz@netscape.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@@ -50,12 +51,13 @@
#include "nsISecurityPref.h"
#include "nsIJSContextStack.h"
#include "nsIObserver.h"
#include "nsWeakPtr.h"
#include "pldhash.h"
class nsIDocShell;
class nsString;
class nsIClassInfo;
class nsSystemPrincipal;
struct ClassPolicy;
/////////////////////
// nsIPrincipalKey //
@@ -92,15 +94,172 @@ protected:
nsIPrincipal* mKey;
};
////////////////////
// Policy Storage //
////////////////////
// Property Policy
union SecurityLevel
{
long level;
char* capability;
};
// Security levels
// These values all have the low bit set (except UNDEFINED_ACCESS)
// to distinguish them from pointer values, because no pointer
// to allocated memory ever has the low bit set. A SecurityLevel
// contains either one of these constants or a pointer to a string
// representing the name of a capability.
#define SCRIPT_SECURITY_UNDEFINED_ACCESS 0
#define SCRIPT_SECURITY_ACCESS_IS_SET_BIT 1
#define SCRIPT_SECURITY_NO_ACCESS \
((1 << 0) | SCRIPT_SECURITY_ACCESS_IS_SET_BIT)
#define SCRIPT_SECURITY_SAME_ORIGIN_ACCESS \
((1 << 1) | SCRIPT_SECURITY_ACCESS_IS_SET_BIT)
#define SCRIPT_SECURITY_ALL_ACCESS \
((1 << 2) | SCRIPT_SECURITY_ACCESS_IS_SET_BIT)
#define SECURITY_ACCESS_LEVEL_FLAG(_sl) \
((_sl.level == 0) || \
(_sl.level & SCRIPT_SECURITY_ACCESS_IS_SET_BIT))
struct PropertyPolicy : public PLDHashEntryHdr
{
jsval key; // property name as jsval
SecurityLevel mGet;
SecurityLevel mSet;
};
PR_STATIC_CALLBACK(void)
InitPropertyPolicyEntry(PLDHashTable *table,
PLDHashEntryHdr *entry,
const void *key)
{
PropertyPolicy* pp = (PropertyPolicy*)entry;
pp->key = (jsval)key;
pp->mGet.level = SCRIPT_SECURITY_UNDEFINED_ACCESS;
pp->mSet.level = SCRIPT_SECURITY_UNDEFINED_ACCESS;
}
PR_STATIC_CALLBACK(void)
ClearPropertyPolicyEntry(PLDHashTable *table, PLDHashEntryHdr *entry)
{
PropertyPolicy* pp = (PropertyPolicy*)entry;
pp->key = JSVAL_VOID;
}
// Class Policy
#define NO_POLICY_FOR_CLASS (ClassPolicy*)1
struct ClassPolicy : public PLDHashEntryHdr
{
char* key;
PLDHashTable mPolicy;
ClassPolicy* mDefault;
ClassPolicy* mWildcard;
};
PR_STATIC_CALLBACK(PRBool)
MatchClassPolicyKey(PLDHashTable *table,
const PLDHashEntryHdr *entry,
const void *key)
{
ClassPolicy* cp = (ClassPolicy *)entry;
return (cp->key == (char*)key) || (PL_strcmp(cp->key, (char*)key) == 0);
}
PR_STATIC_CALLBACK(void)
ClearClassPolicyEntry(PLDHashTable *table, PLDHashEntryHdr *entry)
{
ClassPolicy* cp = (ClassPolicy *)entry;
if (cp->key)
{
PL_strfree(cp->key);
cp->key = nsnull;
}
PL_DHashTableFinish(&cp->mPolicy);
}
PR_STATIC_CALLBACK(void)
InitClassPolicyEntry(PLDHashTable *table,
PLDHashEntryHdr *entry,
const void *key)
{
static PLDHashTableOps classPolicyOps =
{
PL_DHashAllocTable,
PL_DHashFreeTable,
PL_DHashGetKeyStub,
PL_DHashVoidPtrKeyStub,
PL_DHashMatchEntryStub,
PL_DHashMoveEntryStub,
ClearPropertyPolicyEntry,
PL_DHashFinalizeStub,
InitPropertyPolicyEntry
};
ClassPolicy* cp = (ClassPolicy*)entry;
cp->key = PL_strdup((const char*)key);
PL_DHashTableInit(&cp->mPolicy, &classPolicyOps, nsnull,
sizeof(PropertyPolicy), 16);
}
// Domain Policy
class DomainPolicy : public PLDHashTable
{
public:
DomainPolicy() : mRefCount(0)
{
static PLDHashTableOps domainPolicyOps =
{
PL_DHashAllocTable,
PL_DHashFreeTable,
PL_DHashGetKeyStub,
PL_DHashStringKey,
MatchClassPolicyKey,
PL_DHashMoveEntryStub,
ClearClassPolicyEntry,
PL_DHashFinalizeStub,
InitClassPolicyEntry
};
PL_DHashTableInit(this, &domainPolicyOps, nsnull,
sizeof(ClassPolicy), 16);
}
void Hold()
{
mRefCount++;
}
void Drop()
{
if (--mRefCount == 0)
delete this;
}
private:
PRUint32 mRefCount;
};
/////////////////////////////
// nsScriptSecurityManager //
/////////////////////////////
#define NS_SCRIPTSECURITYMANAGER_CID \
{ 0x7ee2a4c0, 0x4b93, 0x17d3, \
{ 0xba, 0x18, 0x00, 0x60, 0xb0, 0xf1, 0x99, 0xa2 }}
class nsScriptSecurityManager : public nsIScriptSecurityManager, public nsIObserver
class nsScriptSecurityManager : public nsIScriptSecurityManager,
public nsIObserver
{
public:
nsScriptSecurityManager();
virtual ~nsScriptSecurityManager();
static void Shutdown();
NS_DEFINE_STATIC_CID_ACCESSOR(NS_SCRIPTSECURITYMANAGER_CID)
@@ -115,11 +274,19 @@ public:
static nsSystemPrincipal*
SystemPrincipalSingletonConstructor();
JSContext* GetCurrentContextQuick();
JSContext* GetCurrentJSContext();
JSContext* GetSafeJSContext();
private:
static PRBool IsDOMClass(nsIClassInfo* aClassInfo);
static JSBool
CheckJSFunctionCallerAccess(JSContext *cx, JSObject *obj,
jsval id, JSAccessMode mode,
jsval *vp);
static PRBool
IsDOMClass(nsIClassInfo* aClassInfo);
nsresult
GetBaseURIScheme(nsIURI* aURI, char** aScheme);
@@ -131,12 +298,13 @@ private:
GetRootDocShell(JSContext* cx, nsIDocShell **result);
nsresult
CheckPropertyAccessImpl(PRUint32 aAction, nsIXPCNativeCallContext* aCallContext,
JSContext* aJSContext, JSObject* aJSObject,
CheckPropertyAccessImpl(PRUint32 aAction,
nsIXPCNativeCallContext* aCallContext,
JSContext* cx, JSObject* aJSObject,
nsISupports* aObj, nsIURI* aTargetURI,
nsIClassInfo* aClassInfo,
jsval aName, const char* aClassName,
const char* aProperty, void** aPolicy);
const char* aClassName, jsval aProperty,
void** aCachedClassPolicy);
nsresult
CheckSameOrigin(JSContext* aCx, nsIPrincipal* aSubject,
@@ -148,18 +316,13 @@ private:
const char* aClassName, const char* aProperty,
PRUint32 aAction, nsCString &capability, void** aPolicy);
static nsresult
TryToGetPref(nsISecurityPref* aSecurityPref,
nsCString &aPrefName,
const char* aClassName,
const char* aPropertyName,
PRInt32 aClassPolicy,
PRUint32 aAction, char** result);
nsresult
GetPolicy(nsIPrincipal* principal,
const char* aClassName, const char* aPropertyName,
PRInt32 aClassPolicy, PRUint32 aAction, char** result);
GetClassPolicy(nsIPrincipal* principal, const char* aClassName,
ClassPolicy** result);
SecurityLevel
GetPropertyPolicy(jsval aProperty, ClassPolicy* aClassPolicy,
PRUint32 aAction);
nsresult
CreateCodebasePrincipal(nsIURI* aURI, nsIPrincipal** result);
@@ -196,13 +359,25 @@ private:
PrincipalPrefNames(const char* pref, char** grantedPref, char** deniedPref);
nsresult
InitPolicies(PRUint32 prefCount, const char** prefNames,
nsISecurityPref* securityPref);
InitPolicies();
nsresult
InitDomainPolicy(JSContext* cx,const char* aPolicyName,
DomainPolicy* aDomainPolicy);
nsresult
InitPrincipals(PRUint32 prefCount, const char** prefNames,
nsISecurityPref* securityPref);
#ifdef DEBUG_mstoltz
void
PrintPolicyDB();
#endif
// JS strings we need to clean up on shutdown
static jsval sCallerID;
static jsval sEnabledID;
inline void
JSEnabledPrefChanged(nsISecurityPref* aSecurityPref);
@@ -210,15 +385,18 @@ private:
static const char* sJSMailEnabledPrefName;
nsObjectHashtable* mOriginToPolicyMap;
nsHashtable* mClassPolicies;
nsWeakPtr mPrefBranchWeakRef;
DomainPolicy* mDefaultPolicy;
nsObjectHashtable* mCapabilities;
nsCOMPtr<nsIPrefBranch> mPrefBranch;
nsCOMPtr<nsISecurityPref> mSecurityPref;
nsIPrincipal* mSystemPrincipal;
nsCOMPtr<nsIPrincipal> mSystemCertificate;
nsSupportsHashtable* mPrincipals;
PRBool mIsJavaScriptEnabled;
PRBool mIsMailJavaScriptEnabled;
PRBool mIsWritingPrefs;
nsCOMPtr<nsIJSContextStack> mThreadJSContextStack;
nsCOMPtr<nsIThreadJSContextStack> mJSContextStack;
PRBool mNameSetRegistered;
};

View File

@@ -57,6 +57,7 @@ REQUIRES = xpcom \
intl \
docshell \
windowwatcher \
embedding \
$(NULL)
#//------------------------------------------------------------------------

File diff suppressed because it is too large Load Diff

View File

@@ -364,11 +364,17 @@ RegisterSecurityNameSet(nsIComponentManager *aCompMgr,
PR_TRUE, PR_TRUE, getter_Copies(previous));
NS_ENSURE_SUCCESS(rv, rv);
rv = catman->AddCategoryEntry("app-startup", "service",
NS_SCRIPTSECURITYMANAGER_CONTRACTID,
PR_TRUE, PR_TRUE,
getter_Copies(previous));
NS_ENSURE_SUCCESS(rv, rv);
return rv;
}
static const nsModuleComponentInfo components[] =
static const nsModuleComponentInfo capsComponentInfo[] =
{
{ NS_SCRIPTSECURITYMANAGER_CLASSNAME,
NS_SCRIPTSECURITYMANAGER_CID,
@@ -451,5 +457,12 @@ static const nsModuleComponentInfo components[] =
};
NS_IMPL_NSGETMODULE(nsSecurityManagerModule, components);
void PR_CALLBACK
CapsModuleDtor(nsIModule* thisModules)
{
nsScriptSecurityManager::Shutdown();
}
NS_IMPL_NSGETMODULE_WITH_DTOR(nsSecurityManagerModule, capsComponentInfo,
CapsModuleDtor);

View File

@@ -61,7 +61,8 @@ REQUIRES = xpcom \
xuldoc \
xultmpl \
timer \
webbrwsr \
webbrwsr \
caps \
$(NULL)
CPPSRCS = \

View File

@@ -53,6 +53,7 @@ REQUIRES = xpcom \
gfx \
layout_xul \
content_xul \
caps \
$(NULL)
LCFLAGS = \

View File

@@ -49,6 +49,7 @@
#include "nsDOMCID.h"
#include "nsCSSOMFactory.h"
#include "nsEventStateManager.h"
#include "nsEventListenerManager.h"
#include "nsGenericElement.h"
#include "nsGenericDOMDataNode.h"
#include "nsHTMLAtoms.h"
@@ -225,6 +226,7 @@ Shutdown(nsIModule* aSelf)
nsRange::Shutdown();
nsGenericElement::Shutdown();
nsGenericDOMDataNode::Shutdown();
nsEventListenerManager::Shutdown();
// Release all of our atoms
nsColorNames::ReleaseTable();

View File

@@ -180,6 +180,11 @@ nsresult nsEventListenerManager::RemoveAllListeners(PRBool aScriptOnly)
return NS_OK;
}
void nsEventListenerManager::Shutdown()
{
sAddListenerID = JSVAL_VOID;
}
NS_IMPL_ADDREF(nsEventListenerManager)
NS_IMPL_RELEASE(nsEventListenerManager)
@@ -951,6 +956,9 @@ nsEventListenerManager::RemoveScriptEventListener(nsIAtom *aName)
return result;
}
jsval
nsEventListenerManager::sAddListenerID = JSVAL_VOID;
NS_IMETHODIMP
nsEventListenerManager::RegisterScriptEventListener(nsIScriptContext *aContext,
nsISupports *aObject,
@@ -992,8 +1000,12 @@ nsEventListenerManager::RegisterScriptEventListener(nsIScriptContext *aContext,
nsCOMPtr<nsIClassInfo> classInfo = do_QueryInterface(aObject);
if (sAddListenerID == JSVAL_VOID) {
sAddListenerID = STRING_TO_JSVAL(::JS_InternString(cx, "addEventListener"));
}
if (NS_FAILED(rv = securityManager->CheckPropertyAccess(cx, jsobj,
"EventTarget","addEventListener",
"EventTarget", sAddListenerID,
nsIXPCSecurityManager::ACCESS_SET_PROPERTY))) {
// XXX set pending exception on the native call context?
return rv;

View File

@@ -178,6 +178,8 @@ public:
NS_IMETHOD GetListenerManager(nsIEventListenerManager** aInstancePtrResult);
NS_IMETHOD HandleEvent(nsIDOMEvent *aEvent);
static void Shutdown();
protected:
nsresult HandleEventSubType(nsListenerStruct* aListenerStruct,
nsIDOMEvent* aDOMEvent,
@@ -217,6 +219,8 @@ protected:
nsCOMPtr<nsIPrincipal> mPrincipal;
nsISupports* mTarget; //WEAK
static jsval sAddListenerID;
};

View File

@@ -871,6 +871,8 @@ JSString *nsDOMClassInfo::sOnresize_id = nsnull;
JSString *nsDOMClassInfo::sOnscroll_id = nsnull;
JSString *nsDOMClassInfo::sScrollIntoView_id = nsnull;
JSString *nsDOMClassInfo::sOpen_id = nsnull;
JSString *nsDOMClassInfo::sItem_id = nsnull;
JSString *nsDOMClassInfo::sEnumerate_id = nsnull;
const JSClass *nsDOMClassInfo::sObjectClass = nsnull;
@@ -940,6 +942,8 @@ nsDOMClassInfo::DefineStaticJSStrings(JSContext *cx)
sOnscroll_id = ::JS_InternString(cx, "onscroll");
sScrollIntoView_id = ::JS_InternString(cx, "scrollIntoView");
sOpen_id = ::JS_InternString(cx, "open");
sItem_id = ::JS_InternString(cx, "item");
sEnumerate_id = ::JS_InternString(cx, "enumerateProperties");
return NS_OK;
}
@@ -2391,7 +2395,7 @@ nsDOMClassInfo::Enumerate(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
// Ask the security manager if it's OK to enumerate
nsresult rv =
sSecMan->CheckPropertyAccess(cx, obj, sClassInfoData[mID].mName,
"enumerateProperties",
STRING_TO_JSVAL(sEnumerate_id),
nsIXPCSecurityManager::ACCESS_GET_PROPERTY);
if (NS_FAILED(rv)) {
@@ -2441,21 +2445,6 @@ nsDOMClassInfo::Finalize(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
return NS_ERROR_UNEXPECTED;
}
// Result of this function should not be freed.
static inline const PRUnichar *
JSValIDToString(JSContext *aJSContext, const jsval idval)
{
JSString *str = JS_ValueToString(aJSContext, idval);
if(!str) {
NS_ERROR("JS_ValueToString() returned null!");
return nsnull;
}
return NS_REINTERPRET_CAST(const PRUnichar*, JS_GetStringChars(str));
}
NS_IMETHODIMP
nsDOMClassInfo::CheckAccess(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
JSObject *obj, jsval id, PRUint32 mode,
@@ -2463,23 +2452,13 @@ nsDOMClassInfo::CheckAccess(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
{
if ((mode == JSACC_WATCH || mode == JSACC_PROTO || mode == JSACC_PARENT) &&
sSecMan) {
JSString *str = ::JS_ValueToString(cx, id);
if (!str)
return NS_ERROR_UNEXPECTED;
JSObject *real_obj = nsnull;
nsresult rv = wrapper->GetJSObject(&real_obj);
NS_ENSURE_SUCCESS(rv, rv);
NS_ConvertUCS2toUTF8
prop_name(NS_REINTERPRET_CAST(const PRUnichar *,
::JS_GetStringChars(str)),
::JS_GetStringLength(str));
rv =
sSecMan->CheckPropertyAccess(cx, real_obj, sClassInfoData[mID].mName,
prop_name.get(),
sSecMan->CheckPropertyAccess(cx, real_obj, sClassInfoData[mID].mName, id,
nsIXPCSecurityManager::ACCESS_GET_PROPERTY);
if (NS_FAILED(rv)) {
@@ -2629,6 +2608,8 @@ nsDOMClassInfo::ShutDown()
sOnscroll_id = jsnullstring;
sScrollIntoView_id = jsnullstring;
sOpen_id = jsnullstring;
sItem_id = jsnullstring;
sEnumerate_id = jsnullstring;
NS_IF_RELEASE(sXPConnect);
NS_IF_RELEASE(sSecMan);
@@ -2754,11 +2735,9 @@ nsWindowSH::doCheckPropertyAccess(JSContext *cx, JSObject *obj, jsval id,
JSObject *global = sgo->GetGlobalJSObject();
NS_ConvertUCS2toUTF8 prop_name(JSValIDToString(cx, id));
return sSecMan->CheckPropertyAccess(cx, global,
sClassInfoData[mID].mName,
prop_name.get(), accessMode);
id, accessMode);
}
NS_IMETHODIMP
@@ -2943,7 +2922,7 @@ nsWindowSH::AddProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
}
nsresult rv = doCheckPropertyAccess(cx, obj, id, wrapper,
nsIXPCSecurityManager::ACCESS_SET_PROPERTY);
nsIXPCSecurityManager::ACCESS_SET_PROPERTY);
if (NS_FAILED(rv)) {
// Security check failed. The security manager set a JS
@@ -5231,7 +5210,8 @@ nsHistorySH::GetProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
}
nsresult rv =
sSecMan->CheckPropertyAccess(cx, obj, sClassInfoData[mID].mName, "item",
sSecMan->CheckPropertyAccess(cx, obj, sClassInfoData[mID].mName,
STRING_TO_JSVAL(sItem_id),
nsIXPCSecurityManager::ACCESS_CALL_METHOD);
if (NS_FAILED(rv)) {

View File

@@ -185,6 +185,9 @@ protected:
static JSString *sOnscroll_id;
static JSString *sScrollIntoView_id;
static JSString *sOpen_id;
static JSString *sItem_id;
static JSString *sEnumerate_id;
static const JSClass *sObjectClass;
};

View File

@@ -4620,6 +4620,7 @@ NavigatorImpl::~NavigatorImpl()
{
NS_IF_RELEASE(mMimeTypes);
NS_IF_RELEASE(mPlugins);
sPrefInternal_id = JSVAL_VOID;
}
//*****************************************************************************
@@ -4949,6 +4950,9 @@ NavigatorImpl::TaintEnabled(PRBool *aReturn)
return NS_OK;
}
jsval
NavigatorImpl::sPrefInternal_id = JSVAL_VOID;
NS_IMETHODIMP
NavigatorImpl::Preference()
{
@@ -4985,7 +4989,10 @@ NavigatorImpl::Preference()
NS_ENSURE_SUCCESS(rv, rv);
//--Check to see if the caller is allowed to access prefs
nsCOMPtr<nsIScriptSecurityManager> secMan =
if (sPrefInternal_id = JSVAL_VOID)
sPrefInternal_id = STRING_TO_JSVAL(::JS_InternString(cx, "preferenceinternal"));
nsCOMPtr<nsIScriptSecurityManager> secMan =
do_GetService("@mozilla.org/scriptsecuritymanager;1", &rv);
NS_ENSURE_SUCCESS(rv, rv);
PRUint32 action;
@@ -4993,8 +5000,9 @@ NavigatorImpl::Preference()
action = nsIXPCSecurityManager::ACCESS_GET_PROPERTY;
else
action = nsIXPCSecurityManager::ACCESS_SET_PROPERTY;
rv = secMan->CheckPropertyAccess(cx, nsnull,
"Navigator", "preferenceinternal", action);
rv = secMan->CheckPropertyAccess(cx, nsnull,
"Navigator", sPrefInternal_id, action);
if (NS_FAILED(rv))
{
return NS_OK;

View File

@@ -373,6 +373,8 @@ protected:
MimeTypeArrayImpl* mMimeTypes;
PluginArrayImpl* mPlugins;
nsIDocShell* mDocShell; // weak reference
static jsval sPrefInternal_id;
};
class nsIURI;

View File

@@ -237,6 +237,9 @@ pref("print.print_edge_bottom", 0); // 1/100 of an inch
// Editing these may create a security risk - be sure you know what you're doing
//pref("capability.policy.default.barprop.visible.set", "UniversalBrowserWrite");
pref("capability.policy.default_policynames", "mailnews");
pref("capability.policy.policynames", "");
pref("capability.policy.default.DOMException.code", "allAccess");
pref("capability.policy.default.DOMException.message", "allAccess");
pref("capability.policy.default.DOMException.name", "allAccess");