2012-08-08 19:58:06 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=8 et :
|
|
|
|
*/
|
|
|
|
/* 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/. */
|
|
|
|
|
2012-12-22 03:53:38 -08:00
|
|
|
#include "AppProcessChecker.h"
|
2013-11-05 02:14:46 -08:00
|
|
|
#include "nsIPermissionManager.h"
|
2013-09-23 14:30:40 -07:00
|
|
|
#ifdef MOZ_CHILD_PERMISSIONS
|
2012-08-08 19:58:06 -07:00
|
|
|
#include "ContentParent.h"
|
|
|
|
#include "mozIApplication.h"
|
2012-08-16 12:34:53 -07:00
|
|
|
#include "mozilla/hal_sandbox/PHalParent.h"
|
2013-11-05 02:14:46 -08:00
|
|
|
#include "nsIAppsService.h"
|
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2012-08-08 19:58:06 -07:00
|
|
|
#include "TabParent.h"
|
|
|
|
|
2013-11-05 02:14:46 -08:00
|
|
|
#include <algorithm>
|
|
|
|
|
2012-08-08 19:58:06 -07:00
|
|
|
using namespace mozilla::dom;
|
2012-08-16 12:34:53 -07:00
|
|
|
using namespace mozilla::hal_sandbox;
|
2012-08-08 19:58:06 -07:00
|
|
|
using namespace mozilla::services;
|
2013-11-05 02:14:46 -08:00
|
|
|
#else
|
2013-11-06 09:16:56 -08:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2013-11-05 02:14:46 -08:00
|
|
|
class PContentParent;
|
2013-11-06 09:16:56 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-05 02:14:46 -08:00
|
|
|
class nsIPrincipal;
|
2013-09-23 14:30:40 -07:00
|
|
|
#endif
|
2012-08-08 19:58:06 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2013-08-22 12:52:07 -07:00
|
|
|
#ifdef MOZ_CHILD_PERMISSIONS
|
|
|
|
|
2012-08-08 19:58:06 -07:00
|
|
|
bool
|
2012-12-22 03:53:38 -08:00
|
|
|
AssertAppProcess(PBrowserParent* aActor,
|
|
|
|
AssertAppProcessType aType,
|
|
|
|
const char* aCapability)
|
2012-08-08 19:58:06 -07:00
|
|
|
{
|
|
|
|
if (!aActor) {
|
2012-12-22 03:53:38 -08:00
|
|
|
NS_WARNING("Testing process capability for null actor");
|
2012-08-08 19:58:06 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
TabParent* tab = static_cast<TabParent*>(aActor);
|
Bug 802366 - The main event: Let a browser process inherit its app's id. r=bz,cjones
The main bug fixed here is that in half of our interfaces, we use "is browser frame/element" to mean "browser or app", and in the other half, we use it to mean "is browser not app".
There's a related, functional bug also fixed here, which is that a browser process doesn't inherit its parent's app-id. This causes problems e.g. for IndexedDB: If a browser inside an app uses IndexedDB, the DB should have the app's app-id.
I also modified Tab{Parent,Child} and nsFrameLoader to call "app" "ownOrContainingApp", to emphasize that we might have inherited the app from a parent process. I left nsIDocShell::appId alone, because changing that would have necessitated changing nsILoadGroup and therefore a /lot/ of users in Necko; it's also not clear it would have clarified anything in those cases.
2012-11-10 10:32:37 -08:00
|
|
|
nsCOMPtr<mozIApplication> app = tab->GetOwnOrContainingApp();
|
2012-12-22 03:53:38 -08:00
|
|
|
bool aValid = false;
|
2012-09-28 10:29:36 -07:00
|
|
|
|
2012-08-08 19:58:06 -07:00
|
|
|
// isBrowser frames inherit their app descriptor to identify their
|
2012-12-22 03:53:38 -08:00
|
|
|
// data storage, but they don't inherit the capability associated
|
2012-08-08 19:58:06 -07:00
|
|
|
// with that descriptor.
|
2013-01-14 02:08:55 -08:00
|
|
|
if (app && (aType == ASSERT_APP_HAS_PERMISSION || !tab->IsBrowserElement())) {
|
2012-12-22 03:53:38 -08:00
|
|
|
switch (aType) {
|
2013-01-14 02:08:55 -08:00
|
|
|
case ASSERT_APP_HAS_PERMISSION:
|
2012-12-22 03:53:38 -08:00
|
|
|
case ASSERT_APP_PROCESS_PERMISSION:
|
|
|
|
if (!NS_SUCCEEDED(app->HasPermission(aCapability, &aValid))) {
|
|
|
|
aValid = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ASSERT_APP_PROCESS_MANIFEST_URL: {
|
|
|
|
nsAutoString manifestURL;
|
|
|
|
if (NS_SUCCEEDED(app->GetManifestURL(manifestURL)) &&
|
|
|
|
manifestURL.EqualsASCII(aCapability)) {
|
|
|
|
aValid = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2012-09-28 10:29:36 -07:00
|
|
|
}
|
2012-08-08 19:58:06 -07:00
|
|
|
}
|
|
|
|
|
2012-12-22 03:53:38 -08:00
|
|
|
if (!aValid) {
|
|
|
|
printf_stderr("Security problem: Content process does not have `%s'. It will be killed.\n", aCapability);
|
2013-07-10 10:07:51 -07:00
|
|
|
ContentParent* process = tab->Manager();
|
2012-09-05 15:18:48 -07:00
|
|
|
process->KillHard();
|
2012-08-25 15:38:04 -07:00
|
|
|
}
|
2012-12-22 03:53:38 -08:00
|
|
|
return aValid;
|
2012-08-08 19:58:06 -07:00
|
|
|
}
|
|
|
|
|
2013-06-29 03:52:16 -07:00
|
|
|
bool
|
|
|
|
AssertAppStatus(PBrowserParent* aActor,
|
|
|
|
unsigned short aStatus)
|
|
|
|
{
|
|
|
|
if (!aActor) {
|
|
|
|
NS_WARNING("Testing process capability for null actor");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
TabParent* tab = static_cast<TabParent*>(aActor);
|
|
|
|
nsCOMPtr<mozIApplication> app = tab->GetOwnOrContainingApp();
|
|
|
|
|
2013-12-05 15:12:23 -08:00
|
|
|
bool valid = false;
|
|
|
|
|
2013-06-29 03:52:16 -07:00
|
|
|
if (app) {
|
|
|
|
unsigned short appStatus = 0;
|
|
|
|
if (NS_SUCCEEDED(app->GetAppStatus(&appStatus))) {
|
2013-12-05 15:12:23 -08:00
|
|
|
valid = appStatus == aStatus;
|
2013-06-29 03:52:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-05 15:12:23 -08:00
|
|
|
if (!valid) {
|
|
|
|
printf_stderr("Security problem: Content process does not have `%d' status. It will be killed.\n", aStatus);
|
|
|
|
ContentParent* process = tab->Manager();
|
|
|
|
process->KillHard();
|
|
|
|
}
|
|
|
|
|
|
|
|
return valid;
|
2013-06-29 03:52:16 -07:00
|
|
|
}
|
|
|
|
|
2012-08-08 19:58:06 -07:00
|
|
|
bool
|
2012-12-22 03:53:38 -08:00
|
|
|
AssertAppProcess(PContentParent* aActor,
|
|
|
|
AssertAppProcessType aType,
|
|
|
|
const char* aCapability)
|
2012-08-08 19:58:06 -07:00
|
|
|
{
|
|
|
|
const InfallibleTArray<PBrowserParent*>& browsers =
|
|
|
|
aActor->ManagedPBrowserParent();
|
|
|
|
for (uint32_t i = 0; i < browsers.Length(); ++i) {
|
2012-12-22 03:53:38 -08:00
|
|
|
if (AssertAppProcess(browsers[i], aType, aCapability)) {
|
2012-08-08 19:58:06 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-29 03:52:16 -07:00
|
|
|
bool
|
|
|
|
AssertAppStatus(PContentParent* aActor,
|
|
|
|
unsigned short aStatus)
|
|
|
|
{
|
|
|
|
const InfallibleTArray<PBrowserParent*>& browsers =
|
|
|
|
aActor->ManagedPBrowserParent();
|
|
|
|
for (uint32_t i = 0; i < browsers.Length(); ++i) {
|
|
|
|
if (AssertAppStatus(browsers[i], aStatus)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-16 12:34:53 -07:00
|
|
|
bool
|
2012-12-22 03:53:38 -08:00
|
|
|
AssertAppProcess(PHalParent* aActor,
|
|
|
|
AssertAppProcessType aType,
|
|
|
|
const char* aCapability)
|
2012-08-16 12:34:53 -07:00
|
|
|
{
|
2012-12-22 03:53:38 -08:00
|
|
|
return AssertAppProcess(aActor->Manager(), aType, aCapability);
|
2012-08-16 12:34:53 -07:00
|
|
|
}
|
|
|
|
|
2013-11-05 02:14:46 -08:00
|
|
|
bool
|
|
|
|
AssertAppPrincipal(PContentParent* aActor,
|
|
|
|
nsIPrincipal* aPrincipal)
|
|
|
|
{
|
|
|
|
if (!aPrincipal) {
|
|
|
|
NS_WARNING("Principal is invalid, killing app process");
|
|
|
|
static_cast<ContentParent*>(aActor)->KillHard();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t principalAppId = aPrincipal->GetAppId();
|
|
|
|
bool inBrowserElement = aPrincipal->GetIsInBrowserElement();
|
|
|
|
|
|
|
|
// Check if the permission's appId matches a child we manage.
|
|
|
|
const InfallibleTArray<PBrowserParent*>& browsers =
|
|
|
|
aActor->ManagedPBrowserParent();
|
|
|
|
for (uint32_t i = 0; i < browsers.Length(); ++i) {
|
|
|
|
TabParent* tab = static_cast<TabParent*>(browsers[i]);
|
|
|
|
if (tab->OwnOrContainingAppId() == principalAppId) {
|
|
|
|
// If the child only runs inBrowserElement content and the principal claims
|
|
|
|
// it's not in a browser element, it's lying.
|
|
|
|
if (!tab->IsBrowserElement() || inBrowserElement) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_WARNING("Principal is invalid, killing app process");
|
|
|
|
static_cast<ContentParent*>(aActor)->KillHard();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIPrincipal>
|
|
|
|
GetAppPrincipal(uint32_t aAppId)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAppsService> appsService = do_GetService(APPS_SERVICE_CONTRACTID);
|
|
|
|
|
2014-01-29 14:03:50 -08:00
|
|
|
nsCOMPtr<mozIApplication> app;
|
|
|
|
nsresult rv = appsService->GetAppByLocalId(aAppId, getter_AddRefs(app));
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
|
|
|
nsString origin;
|
|
|
|
rv = app->GetOrigin(origin);
|
2013-11-05 02:14:46 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2014-01-29 14:03:50 -08:00
|
|
|
NS_NewURI(getter_AddRefs(uri), origin);
|
2013-11-05 02:14:46 -08:00
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptSecurityManager> secMan =
|
|
|
|
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> appPrincipal;
|
|
|
|
rv = secMan->GetAppCodebasePrincipal(uri, aAppId, false,
|
|
|
|
getter_AddRefs(appPrincipal));
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
return appPrincipal.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
CheckPermission(PContentParent* aActor,
|
|
|
|
nsIPrincipal* aPrincipal,
|
|
|
|
const char* aPermission)
|
|
|
|
{
|
|
|
|
if (!AssertAppPrincipal(aActor, aPrincipal)) {
|
|
|
|
return nsIPermissionManager::DENY_ACTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPermissionManager> pm =
|
|
|
|
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(pm, nsIPermissionManager::DENY_ACTION);
|
|
|
|
|
|
|
|
// Make sure that `aPermission' is an app permission before checking the origin.
|
|
|
|
nsCOMPtr<nsIPrincipal> appPrincipal = GetAppPrincipal(aPrincipal->GetAppId());
|
|
|
|
uint32_t appPerm = nsIPermissionManager::UNKNOWN_ACTION;
|
|
|
|
nsresult rv = pm->TestExactPermissionFromPrincipal(appPrincipal, aPermission, &appPerm);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nsIPermissionManager::UNKNOWN_ACTION);
|
2014-02-21 11:56:19 -08:00
|
|
|
// Setting to "deny" in the settings UI should deny everywhere.
|
2013-11-05 02:14:46 -08:00
|
|
|
if (appPerm == nsIPermissionManager::UNKNOWN_ACTION ||
|
|
|
|
appPerm == nsIPermissionManager::DENY_ACTION) {
|
|
|
|
return appPerm;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t permission = nsIPermissionManager::UNKNOWN_ACTION;
|
|
|
|
rv = pm->TestExactPermissionFromPrincipal(aPrincipal, aPermission, &permission);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nsIPermissionManager::UNKNOWN_ACTION);
|
|
|
|
if (permission == nsIPermissionManager::UNKNOWN_ACTION ||
|
|
|
|
permission == nsIPermissionManager::DENY_ACTION) {
|
|
|
|
return permission;
|
|
|
|
}
|
|
|
|
|
2014-02-21 11:56:19 -08:00
|
|
|
// For browser content (and if the app hasn't explicitly denied this),
|
|
|
|
// consider the requesting origin, not the app.
|
|
|
|
if (appPerm == nsIPermissionManager::PROMPT_ACTION &&
|
|
|
|
aPrincipal->GetIsInBrowserElement()) {
|
|
|
|
return permission;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setting to "prompt" in the settings UI should prompt everywhere in
|
|
|
|
// non-browser content.
|
2013-11-05 02:14:46 -08:00
|
|
|
if (appPerm == nsIPermissionManager::PROMPT_ACTION ||
|
|
|
|
permission == nsIPermissionManager::PROMPT_ACTION) {
|
|
|
|
return nsIPermissionManager::PROMPT_ACTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (appPerm == nsIPermissionManager::ALLOW_ACTION ||
|
|
|
|
permission == nsIPermissionManager::ALLOW_ACTION) {
|
|
|
|
return nsIPermissionManager::ALLOW_ACTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_RUNTIMEABORT("Invalid permission value");
|
|
|
|
return nsIPermissionManager::DENY_ACTION;
|
|
|
|
}
|
|
|
|
|
2013-08-22 12:52:07 -07:00
|
|
|
#else
|
|
|
|
|
|
|
|
bool
|
|
|
|
AssertAppProcess(mozilla::dom::PBrowserParent* aActor,
|
|
|
|
AssertAppProcessType aType,
|
|
|
|
const char* aCapability)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AssertAppStatus(mozilla::dom::PBrowserParent* aActor,
|
|
|
|
unsigned short aStatus)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
AssertAppProcess(mozilla::dom::PContentParent* aActor,
|
|
|
|
AssertAppProcessType aType,
|
|
|
|
const char* aCapability)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AssertAppStatus(mozilla::dom::PContentParent* aActor,
|
|
|
|
unsigned short aStatus)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AssertAppProcess(mozilla::hal_sandbox::PHalParent* aActor,
|
|
|
|
AssertAppProcessType aType,
|
|
|
|
const char* aCapability)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-05 02:14:46 -08:00
|
|
|
bool
|
2013-11-06 09:16:56 -08:00
|
|
|
AssertAppPrincipal(mozilla::dom::PContentParent* aActor,
|
2013-11-05 02:14:46 -08:00
|
|
|
nsIPrincipal* aPrincipal)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
2013-11-06 09:16:56 -08:00
|
|
|
CheckPermission(mozilla::dom::PContentParent* aActor,
|
|
|
|
nsIPrincipal* aPrincipal,
|
|
|
|
const char* aPermission)
|
2013-11-05 02:14:46 -08:00
|
|
|
{
|
|
|
|
return nsIPermissionManager::ALLOW_ACTION;
|
|
|
|
}
|
|
|
|
|
2013-08-22 12:52:07 -07:00
|
|
|
#endif
|
|
|
|
|
2012-08-08 19:58:06 -07:00
|
|
|
} // namespace mozilla
|