PrincipalVerifier.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "mozilla/dom/cache/PrincipalVerifier.h"
  6. #include "mozilla/AppProcessChecker.h"
  7. #include "mozilla/dom/ContentParent.h"
  8. #include "mozilla/dom/cache/ManagerId.h"
  9. #include "mozilla/ipc/BackgroundParent.h"
  10. #include "mozilla/ipc/PBackgroundParent.h"
  11. #include "mozilla/ipc/BackgroundUtils.h"
  12. #include "nsContentUtils.h"
  13. #include "nsIPrincipal.h"
  14. #include "nsIScriptSecurityManager.h"
  15. #include "nsNetUtil.h"
  16. namespace mozilla {
  17. namespace dom {
  18. namespace cache {
  19. using mozilla::ipc::AssertIsOnBackgroundThread;
  20. using mozilla::ipc::BackgroundParent;
  21. using mozilla::ipc::PBackgroundParent;
  22. using mozilla::ipc::PrincipalInfo;
  23. using mozilla::ipc::PrincipalInfoToPrincipal;
  24. // static
  25. already_AddRefed<PrincipalVerifier>
  26. PrincipalVerifier::CreateAndDispatch(Listener* aListener,
  27. PBackgroundParent* aActor,
  28. const PrincipalInfo& aPrincipalInfo)
  29. {
  30. // We must get the ContentParent actor from the PBackgroundParent. This
  31. // only works on the PBackground thread.
  32. AssertIsOnBackgroundThread();
  33. RefPtr<PrincipalVerifier> verifier = new PrincipalVerifier(aListener,
  34. aActor,
  35. aPrincipalInfo);
  36. MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(verifier));
  37. return verifier.forget();
  38. }
  39. void
  40. PrincipalVerifier::AddListener(Listener* aListener)
  41. {
  42. AssertIsOnBackgroundThread();
  43. MOZ_DIAGNOSTIC_ASSERT(aListener);
  44. MOZ_ASSERT(!mListenerList.Contains(aListener));
  45. mListenerList.AppendElement(aListener);
  46. }
  47. void
  48. PrincipalVerifier::RemoveListener(Listener* aListener)
  49. {
  50. AssertIsOnBackgroundThread();
  51. MOZ_DIAGNOSTIC_ASSERT(aListener);
  52. MOZ_ALWAYS_TRUE(mListenerList.RemoveElement(aListener));
  53. }
  54. PrincipalVerifier::PrincipalVerifier(Listener* aListener,
  55. PBackgroundParent* aActor,
  56. const PrincipalInfo& aPrincipalInfo)
  57. : mActor(BackgroundParent::GetContentParent(aActor))
  58. , mPrincipalInfo(aPrincipalInfo)
  59. , mInitiatingThread(NS_GetCurrentThread())
  60. , mResult(NS_OK)
  61. {
  62. AssertIsOnBackgroundThread();
  63. MOZ_DIAGNOSTIC_ASSERT(mInitiatingThread);
  64. MOZ_DIAGNOSTIC_ASSERT(aListener);
  65. mListenerList.AppendElement(aListener);
  66. }
  67. PrincipalVerifier::~PrincipalVerifier()
  68. {
  69. // Since the PrincipalVerifier is a Runnable that executes on multiple
  70. // threads, its a race to see which thread de-refs us last. Therefore
  71. // we cannot guarantee which thread we destruct on.
  72. MOZ_DIAGNOSTIC_ASSERT(mListenerList.IsEmpty());
  73. // We should always be able to explicitly release the actor on the main
  74. // thread.
  75. MOZ_DIAGNOSTIC_ASSERT(!mActor);
  76. }
  77. NS_IMETHODIMP
  78. PrincipalVerifier::Run()
  79. {
  80. // Executed twice. First, on the main thread and then back on the
  81. // originating thread.
  82. if (NS_IsMainThread()) {
  83. VerifyOnMainThread();
  84. return NS_OK;
  85. }
  86. CompleteOnInitiatingThread();
  87. return NS_OK;
  88. }
  89. void
  90. PrincipalVerifier::VerifyOnMainThread()
  91. {
  92. MOZ_ASSERT(NS_IsMainThread());
  93. // No matter what happens, we need to release the actor before leaving
  94. // this method.
  95. RefPtr<ContentParent> actor;
  96. actor.swap(mActor);
  97. nsresult rv;
  98. RefPtr<nsIPrincipal> principal = PrincipalInfoToPrincipal(mPrincipalInfo,
  99. &rv);
  100. if (NS_WARN_IF(NS_FAILED(rv))) {
  101. DispatchToInitiatingThread(rv);
  102. return;
  103. }
  104. // We disallow null principal and unknown app IDs on the client side, but
  105. // double-check here.
  106. if (NS_WARN_IF(principal->GetIsNullPrincipal() ||
  107. principal->GetUnknownAppId())) {
  108. DispatchToInitiatingThread(NS_ERROR_FAILURE);
  109. return;
  110. }
  111. nsCOMPtr<nsIScriptSecurityManager> ssm = nsContentUtils::GetSecurityManager();
  112. if (NS_WARN_IF(!ssm)) {
  113. DispatchToInitiatingThread(NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
  114. return;
  115. }
  116. // Verify if a child process uses system principal, which is not allowed
  117. // to prevent system principal is spoofed.
  118. if (NS_WARN_IF(actor && ssm->IsSystemPrincipal(principal))) {
  119. DispatchToInitiatingThread(NS_ERROR_FAILURE);
  120. return;
  121. }
  122. // Verify that a child process claims to own the app for this principal
  123. if (NS_WARN_IF(actor && !AssertAppPrincipal(actor, principal))) {
  124. DispatchToInitiatingThread(NS_ERROR_FAILURE);
  125. return;
  126. }
  127. actor = nullptr;
  128. #ifdef DEBUG
  129. // Sanity check principal origin by using it to construct a URI and security
  130. // checking it. Don't do this for the system principal, though, as its origin
  131. // is a synthetic [System Principal] string.
  132. if (!ssm->IsSystemPrincipal(principal)) {
  133. nsAutoCString origin;
  134. rv = principal->GetOriginNoSuffix(origin);
  135. if (NS_WARN_IF(NS_FAILED(rv))) {
  136. DispatchToInitiatingThread(rv);
  137. return;
  138. }
  139. nsCOMPtr<nsIURI> uri;
  140. rv = NS_NewURI(getter_AddRefs(uri), origin);
  141. if (NS_WARN_IF(NS_FAILED(rv))) {
  142. DispatchToInitiatingThread(rv);
  143. return;
  144. }
  145. rv = principal->CheckMayLoad(uri, false, false);
  146. if (NS_WARN_IF(NS_FAILED(rv))) {
  147. DispatchToInitiatingThread(rv);
  148. return;
  149. }
  150. }
  151. #endif
  152. rv = ManagerId::Create(principal, getter_AddRefs(mManagerId));
  153. if (NS_WARN_IF(NS_FAILED(rv))) {
  154. DispatchToInitiatingThread(rv);
  155. return;
  156. }
  157. DispatchToInitiatingThread(NS_OK);
  158. }
  159. void
  160. PrincipalVerifier::CompleteOnInitiatingThread()
  161. {
  162. AssertIsOnBackgroundThread();
  163. ListenerList::ForwardIterator iter(mListenerList);
  164. while (iter.HasMore()) {
  165. iter.GetNext()->OnPrincipalVerified(mResult, mManagerId);
  166. }
  167. // The listener must clear its reference in OnPrincipalVerified()
  168. MOZ_DIAGNOSTIC_ASSERT(mListenerList.IsEmpty());
  169. }
  170. void
  171. PrincipalVerifier::DispatchToInitiatingThread(nsresult aRv)
  172. {
  173. MOZ_ASSERT(NS_IsMainThread());
  174. mResult = aRv;
  175. // The Cache ShutdownObserver does not track all principal verifiers, so we
  176. // cannot ensure this always succeeds. Instead, simply warn on failures.
  177. // This will result in a new CacheStorage object delaying operations until
  178. // shutdown completes and the browser goes away. This is as graceful as
  179. // we can get here.
  180. nsresult rv = mInitiatingThread->Dispatch(this, nsIThread::DISPATCH_NORMAL);
  181. if (NS_FAILED(rv)) {
  182. NS_WARNING("Cache unable to complete principal verification due to shutdown.");
  183. }
  184. }
  185. } // namespace cache
  186. } // namespace dom
  187. } // namespace mozilla