process_singleton.patch 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Raymond Zhao <[email protected]>
  3. Date: Wed, 18 Aug 2021 08:24:10 -0700
  4. Subject: extend ProcessSingleton
  5. This patch applies Electron ProcessSingleton changes onto the Chromium
  6. files.
  7. This patch adds a few changes to the Chromium code:
  8. 1. It adds a parameter `program_name` to the Windows constructor, making
  9. the generated mutex name on the Windows-side program-dependent,
  10. rather than shared between all Electron applications.
  11. 2. It adds an `IsAppSandboxed` check for macOS so that
  12. sandboxed applications generate shorter temp paths.
  13. 3. It adds a `ChangeWindowMessageFilterEx` call to the Windows
  14. implementation, along with a parameter `is_app_sandboxed` in the
  15. constructor, to handle the case when the primary app is run with
  16. admin permissions.
  17. diff --git a/chrome/browser/process_singleton.h b/chrome/browser/process_singleton.h
  18. index 7cd82d27a741f194da5d0b3fcfd9c15c8ea1fa5c..9de82e5422428d6419a24401e0479fbd19a15147 100644
  19. --- a/chrome/browser/process_singleton.h
  20. +++ b/chrome/browser/process_singleton.h
  21. @@ -102,12 +102,19 @@ class ProcessSingleton {
  22. base::RepeatingCallback<bool(const base::CommandLine& command_line,
  23. const base::FilePath& current_directory)>;
  24. +#if BUILDFLAG(IS_WIN)
  25. + ProcessSingleton(const std::string& program_name,
  26. + const base::FilePath& user_data_dir,
  27. + bool is_sandboxed,
  28. + const NotificationCallback& notification_callback);
  29. +#else
  30. ProcessSingleton(const base::FilePath& user_data_dir,
  31. const NotificationCallback& notification_callback);
  32. ProcessSingleton(const ProcessSingleton&) = delete;
  33. ProcessSingleton& operator=(const ProcessSingleton&) = delete;
  34. +#endif
  35. ~ProcessSingleton();
  36. // Notify another process, if available. Otherwise sets ourselves as the
  37. @@ -176,6 +183,8 @@ class ProcessSingleton {
  38. #if BUILDFLAG(IS_WIN)
  39. bool EscapeVirtualization(const base::FilePath& user_data_dir);
  40. + std::string program_name_; // Used for mutexName.
  41. + bool is_app_sandboxed_; // Whether the Electron app is sandboxed.
  42. HWND remote_window_; // The HWND_MESSAGE of another browser.
  43. base::win::MessageWindow window_; // The message-only window.
  44. bool is_virtualized_; // Stuck inside Microsoft Softricity VM environment.
  45. diff --git a/chrome/browser/process_singleton_posix.cc b/chrome/browser/process_singleton_posix.cc
  46. index a7647b1f3a9d8421dae277ad1d5c35e3c8147942..99b42b0e8f81c6a5696d56fede3e168cfc282cc3 100644
  47. --- a/chrome/browser/process_singleton_posix.cc
  48. +++ b/chrome/browser/process_singleton_posix.cc
  49. @@ -54,6 +54,7 @@
  50. #include <memory>
  51. #include <set>
  52. #include <string>
  53. +#include <tuple>
  54. #include <type_traits>
  55. #include "base/base_paths.h"
  56. @@ -97,9 +98,11 @@
  57. #include "net/base/network_interfaces.h"
  58. #include "ui/base/l10n/l10n_util.h"
  59. +#if 0
  60. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  61. #include "chrome/browser/ui/process_singleton_dialog_linux.h"
  62. #endif
  63. +#endif
  64. using content::BrowserThread;
  65. @@ -343,6 +346,9 @@ bool SymlinkPath(const base::FilePath& target, const base::FilePath& path) {
  66. bool DisplayProfileInUseError(const base::FilePath& lock_path,
  67. const std::string& hostname,
  68. int pid) {
  69. + return true;
  70. +
  71. +#if 0
  72. std::u16string error = l10n_util::GetStringFUTF16(
  73. IDS_PROFILE_IN_USE_POSIX, base::NumberToString16(pid),
  74. base::ASCIIToUTF16(hostname));
  75. @@ -362,6 +368,7 @@ bool DisplayProfileInUseError(const base::FilePath& lock_path,
  76. NOTREACHED();
  77. return false;
  78. +#endif
  79. }
  80. bool IsChromeProcess(pid_t pid) {
  81. @@ -402,6 +409,21 @@ bool CheckCookie(const base::FilePath& path, const base::FilePath& cookie) {
  82. return (cookie == ReadLink(path));
  83. }
  84. +bool IsAppSandboxed() {
  85. +#if BUILDFLAG(IS_MAC)
  86. + // NB: There is no sane API for this, we have to just guess by
  87. + // reading tea leaves
  88. + base::FilePath home_dir;
  89. + if (!base::PathService::Get(base::DIR_HOME, &home_dir)) {
  90. + return false;
  91. + }
  92. +
  93. + return home_dir.value().find("Library/Containers") != std::string::npos;
  94. +#else
  95. + return false;
  96. +#endif // BUILDFLAG(IS_MAC)
  97. +}
  98. +
  99. bool ConnectSocket(ScopedSocket* socket,
  100. const base::FilePath& socket_path,
  101. const base::FilePath& cookie_path) {
  102. @@ -768,6 +790,10 @@ ProcessSingleton::ProcessSingleton(
  103. ProcessSingleton::~ProcessSingleton() {
  104. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  105. + // Manually free resources with IO explicitly allowed.
  106. + base::ThreadRestrictions::ScopedAllowIO allow_io;
  107. + watcher_ = nullptr;
  108. + std::ignore = socket_dir_.Delete();
  109. }
  110. ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcess() {
  111. @@ -1031,14 +1057,32 @@ bool ProcessSingleton::Create() {
  112. #endif
  113. }
  114. - // Create the socket file somewhere in /tmp which is usually mounted as a
  115. - // normal filesystem. Some network filesystems (notably AFS) are screwy and
  116. - // do not support Unix domain sockets.
  117. - if (!socket_dir_.CreateUniqueTempDir()) {
  118. - LOG(ERROR) << "Failed to create socket directory.";
  119. + base::FilePath tmp_dir;
  120. + if (!base::GetTempDir(&tmp_dir)) {
  121. + LOG(ERROR) << "Failed to get temporary directory.";
  122. return false;
  123. }
  124. + if (IsAppSandboxed()) {
  125. + // For sandboxed applications, the tmp dir could be too long to fit
  126. + // addr->sun_path, so we need to make it as short as possible.
  127. + if (!socket_dir_.Set(tmp_dir.Append("S"))) {
  128. + LOG(ERROR) << "Failed to set socket directory.";
  129. + return false;
  130. + }
  131. + } else {
  132. + // Create the socket file somewhere in /tmp which is usually mounted as a
  133. + // normal filesystem. Some network filesystems (notably AFS) are screwy and
  134. + // do not support Unix domain sockets.
  135. + // Prefer CreateUniqueTempDirUnderPath rather than CreateUniqueTempDir as
  136. + // the latter will calculate unique paths based on bundle ids which can
  137. + // increase the socket path length than what is allowed.
  138. + if (!socket_dir_.CreateUniqueTempDirUnderPath(tmp_dir)) {
  139. + LOG(ERROR) << "Failed to create socket directory.";
  140. + return false;
  141. + }
  142. + }
  143. +
  144. // Check that the directory was created with the correct permissions.
  145. int dir_mode = 0;
  146. CHECK(base::GetPosixFilePermissions(socket_dir_.GetPath(), &dir_mode) &&
  147. diff --git a/chrome/browser/process_singleton_win.cc b/chrome/browser/process_singleton_win.cc
  148. index 41bc176510f93ef667e4f1373eab61142f3e264f..c375a587abd3d575b5c75a0863b01c9611e8287c 100644
  149. --- a/chrome/browser/process_singleton_win.cc
  150. +++ b/chrome/browser/process_singleton_win.cc
  151. @@ -29,7 +29,9 @@
  152. #include "base/win/wmi.h"
  153. #include "chrome/browser/process_singleton_internal.h"
  154. #include "chrome/browser/shell_integration.h"
  155. +#if 0
  156. #include "chrome/browser/ui/simple_message_box.h"
  157. +#endif
  158. #include "chrome/browser/win/chrome_process_finder.h"
  159. #include "chrome/common/chrome_constants.h"
  160. #include "chrome/common/chrome_paths.h"
  161. @@ -163,11 +165,16 @@ bool ProcessLaunchNotification(
  162. }
  163. bool DisplayShouldKillMessageBox() {
  164. +#if 0
  165. TRACE_EVENT0("startup", "ProcessSingleton:DisplayShouldKillMessageBox");
  166. return chrome::ShowQuestionMessageBoxSync(
  167. NULL, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME),
  168. l10n_util::GetStringUTF16(IDS_BROWSER_HUNGBROWSER_MESSAGE)) !=
  169. chrome::MESSAGE_BOX_RESULT_NO;
  170. +#endif
  171. + // This is called when the secondary process can't ping the primary
  172. + // process.
  173. + return false;
  174. }
  175. // Function was copied from Process::Terminate.
  176. @@ -252,9 +259,13 @@ bool ProcessSingleton::EscapeVirtualization(
  177. }
  178. ProcessSingleton::ProcessSingleton(
  179. + const std::string& program_name,
  180. const base::FilePath& user_data_dir,
  181. + bool is_app_sandboxed,
  182. const NotificationCallback& notification_callback)
  183. : notification_callback_(notification_callback),
  184. + program_name_(program_name),
  185. + is_app_sandboxed_(is_app_sandboxed),
  186. is_virtualized_(false),
  187. lock_file_(INVALID_HANDLE_VALUE),
  188. user_data_dir_(user_data_dir),
  189. @@ -374,7 +385,7 @@ ProcessSingleton::NotifyOtherProcessOrCreate() {
  190. bool ProcessSingleton::Create() {
  191. TRACE_EVENT0("startup", "ProcessSingleton::Create");
  192. - static const wchar_t kMutexName[] = L"Local\\ChromeProcessSingletonStartup!";
  193. + std::wstring mutexName = base::UTF8ToWide("Local\\" + program_name_ + "ProcessSingletonStartup");
  194. remote_window_ = chrome::FindRunningChromeWindow(user_data_dir_);
  195. if (!remote_window_ && !EscapeVirtualization(user_data_dir_)) {
  196. @@ -383,7 +394,7 @@ bool ProcessSingleton::Create() {
  197. // access. As documented, it's clearer to NOT request ownership on creation
  198. // since it isn't guaranteed we will get it. It is better to create it
  199. // without ownership and explicitly get the ownership afterward.
  200. - base::win::ScopedHandle only_me(::CreateMutex(NULL, FALSE, kMutexName));
  201. + base::win::ScopedHandle only_me(::CreateMutex(NULL, FALSE, mutexName.c_str()));
  202. if (!only_me.IsValid()) {
  203. DPLOG(FATAL) << "CreateMutex failed";
  204. return false;
  205. @@ -422,6 +433,17 @@ bool ProcessSingleton::Create() {
  206. window_.CreateNamed(base::BindRepeating(&ProcessLaunchNotification,
  207. notification_callback_),
  208. user_data_dir_.value());
  209. +
  210. + // When the app is sandboxed, firstly, the app should not be in
  211. + // admin mode, and even if it somehow is, messages from an unelevated
  212. + // instance should not be able to be sent to it.
  213. + if (!is_app_sandboxed_) {
  214. + // NB: Ensure that if the primary app gets started as elevated
  215. + // admin inadvertently, secondary windows running not as elevated
  216. + // will still be able to send messages.
  217. + ::ChangeWindowMessageFilterEx(window_.hwnd(), WM_COPYDATA, MSGFLT_ALLOW,
  218. + NULL);
  219. + }
  220. CHECK(result && window_.hwnd());
  221. }
  222. }