process_singleton.patch 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 23a8257aa2a0a671cf7af35aff9906891091606d..31f5b160e4cd755cfb56a62b04261ee1bee80277 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(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 e0cfeb40e887545830a3294d865c2df466dda8fe..08cbe32a258bf478f1da0a07064d3e9ef14c44a5 100644
  47. --- a/chrome/browser/process_singleton_posix.cc
  48. +++ b/chrome/browser/process_singleton_posix.cc
  49. @@ -59,6 +59,7 @@
  50. #include <memory>
  51. #include <set>
  52. #include <string>
  53. +#include <tuple>
  54. #include <type_traits>
  55. #include "base/base_paths.h"
  56. @@ -86,6 +87,7 @@
  57. #include "base/strings/utf_string_conversions.h"
  58. #include "base/task/sequenced_task_runner_helpers.h"
  59. #include "base/task/single_thread_task_runner.h"
  60. +#include "base/threading/thread_restrictions.h"
  61. #include "base/threading/platform_thread.h"
  62. #include "base/time/time.h"
  63. #include "base/timer/timer.h"
  64. @@ -102,7 +104,7 @@
  65. #include "ui/base/l10n/l10n_util.h"
  66. #include "ui/base/resource/scoped_startup_resource_bundle.h"
  67. -#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  68. +#if 0
  69. #include "chrome/browser/ui/process_singleton_dialog_linux.h"
  70. #endif
  71. @@ -348,6 +350,8 @@ bool SymlinkPath(const base::FilePath& target, const base::FilePath& path) {
  72. bool DisplayProfileInUseError(const base::FilePath& lock_path,
  73. const std::string& hostname,
  74. int pid) {
  75. + return true;
  76. +#if 0
  77. // Ensure there is an instance of ResourceBundle that is initialized for
  78. // localized string resource accesses.
  79. ui::ScopedStartupResourceBundle ensure_startup_resource_bundle;
  80. @@ -370,6 +374,7 @@ bool DisplayProfileInUseError(const base::FilePath& lock_path,
  81. #else
  82. NOTREACHED();
  83. #endif
  84. +#endif
  85. }
  86. bool IsChromeProcess(pid_t pid) {
  87. @@ -382,6 +387,21 @@ bool IsChromeProcess(pid_t pid) {
  88. base::FilePath(chrome::kBrowserProcessExecutableName));
  89. }
  90. +bool IsAppSandboxed() {
  91. +#if BUILDFLAG(IS_MAC)
  92. + // NB: There is no sane API for this, we have to just guess by
  93. + // reading tea leaves
  94. + base::FilePath home_dir;
  95. + if (!base::PathService::Get(base::DIR_HOME, &home_dir)) {
  96. + return false;
  97. + }
  98. +
  99. + return home_dir.value().find("Library/Containers") != std::string::npos;
  100. +#else
  101. + return false;
  102. +#endif // BUILDFLAG(IS_MAC)
  103. +}
  104. +
  105. // A helper class to hold onto a socket.
  106. class ScopedSocket {
  107. public:
  108. @@ -785,6 +805,10 @@ ProcessSingleton::~ProcessSingleton() {
  109. if (watcher_) {
  110. watcher_->OnEminentProcessSingletonDestruction();
  111. }
  112. + // Manually free resources with IO explicitly allowed.
  113. + base::ScopedAllowBlocking allow_blocking;
  114. + watcher_ = nullptr;
  115. + std::ignore = socket_dir_.Delete();
  116. }
  117. ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcess() {
  118. @@ -1055,11 +1079,32 @@ bool ProcessSingleton::Create() {
  119. // Create the socket file somewhere in /tmp which is usually mounted as a
  120. // normal filesystem. Some network filesystems (notably AFS) are screwy and
  121. // do not support Unix domain sockets.
  122. - if (!socket_dir_.CreateUniqueTempDir()) {
  123. - LOG(ERROR) << "Failed to create socket directory.";
  124. + base::FilePath tmp_dir;
  125. + if (!base::GetTempDir(&tmp_dir)) {
  126. + LOG(ERROR) << "Failed to get temporary directory.";
  127. return false;
  128. }
  129. + if (IsAppSandboxed()) {
  130. + // For sandboxed applications, the tmp dir could be too long to fit
  131. + // addr->sun_path, so we need to make it as short as possible.
  132. + if (!socket_dir_.Set(tmp_dir.Append("S"))) {
  133. + LOG(ERROR) << "Failed to set socket directory.";
  134. + return false;
  135. + }
  136. + } else {
  137. + // Create the socket file somewhere in /tmp which is usually mounted as a
  138. + // normal filesystem. Some network filesystems (notably AFS) are screwy and
  139. + // do not support Unix domain sockets.
  140. + // Prefer CreateUniqueTempDirUnderPath rather than CreateUniqueTempDir as
  141. + // the latter will calculate unique paths based on bundle ids which can
  142. + // increase the socket path length than what is allowed.
  143. + if (!socket_dir_.CreateUniqueTempDirUnderPath(tmp_dir)) {
  144. + LOG(ERROR) << "Failed to create socket directory.";
  145. + return false;
  146. + }
  147. + }
  148. +
  149. // Check that the directory was created with the correct permissions.
  150. int dir_mode = 0;
  151. CHECK(base::GetPosixFilePermissions(socket_dir_.GetPath(), &dir_mode) &&
  152. diff --git a/chrome/browser/process_singleton_win.cc b/chrome/browser/process_singleton_win.cc
  153. index 68ca956bdd251f1fadccbd2f4e336918141cadf1..d91f58ebe3a024bc41ed72121c49172f68e0d862 100644
  154. --- a/chrome/browser/process_singleton_win.cc
  155. +++ b/chrome/browser/process_singleton_win.cc
  156. @@ -29,7 +29,9 @@
  157. #include "base/win/wmi.h"
  158. #include "chrome/browser/process_singleton_internal.h"
  159. #include "chrome/browser/shell_integration.h"
  160. +#if 0
  161. #include "chrome/browser/ui/simple_message_box.h"
  162. +#endif
  163. #include "chrome/browser/win/chrome_process_finder.h"
  164. #include "chrome/common/chrome_constants.h"
  165. #include "chrome/common/chrome_paths.h"
  166. @@ -164,6 +166,7 @@ bool ProcessLaunchNotification(
  167. }
  168. bool DisplayShouldKillMessageBox() {
  169. +#if 0
  170. TRACE_EVENT0("startup", "ProcessSingleton:DisplayShouldKillMessageBox");
  171. // Ensure there is an instance of ResourceBundle that is initialized for
  172. @@ -174,6 +177,10 @@ bool DisplayShouldKillMessageBox() {
  173. NULL, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME),
  174. l10n_util::GetStringUTF16(IDS_BROWSER_HUNGBROWSER_MESSAGE)) !=
  175. chrome::MESSAGE_BOX_RESULT_NO;
  176. +#endif
  177. + // This is called when the secondary process can't ping the primary
  178. + // process.
  179. + return false;
  180. }
  181. // Function was copied from Process::Terminate.
  182. @@ -256,9 +263,13 @@ bool ProcessSingleton::EscapeVirtualization(
  183. }
  184. ProcessSingleton::ProcessSingleton(
  185. + const std::string& program_name,
  186. const base::FilePath& user_data_dir,
  187. + bool is_app_sandboxed,
  188. const NotificationCallback& notification_callback)
  189. : notification_callback_(notification_callback),
  190. + program_name_(program_name),
  191. + is_app_sandboxed_(is_app_sandboxed),
  192. is_virtualized_(false),
  193. lock_file_(INVALID_HANDLE_VALUE),
  194. user_data_dir_(user_data_dir),
  195. @@ -381,7 +392,7 @@ ProcessSingleton::NotifyOtherProcessOrCreate() {
  196. bool ProcessSingleton::Create() {
  197. TRACE_EVENT0("startup", "ProcessSingleton::Create");
  198. - static const wchar_t kMutexName[] = L"Local\\ChromeProcessSingletonStartup!";
  199. + std::wstring mutexName = base::UTF8ToWide("Local\\" + program_name_ + "ProcessSingletonStartup");
  200. remote_window_ = FindRunningChromeWindow(user_data_dir_);
  201. if (!remote_window_ && !EscapeVirtualization(user_data_dir_)) {
  202. @@ -390,7 +401,7 @@ bool ProcessSingleton::Create() {
  203. // access. As documented, it's clearer to NOT request ownership on creation
  204. // since it isn't guaranteed we will get it. It is better to create it
  205. // without ownership and explicitly get the ownership afterward.
  206. - base::win::ScopedHandle only_me(::CreateMutex(NULL, FALSE, kMutexName));
  207. + base::win::ScopedHandle only_me(::CreateMutex(NULL, FALSE, mutexName.c_str()));
  208. if (!only_me.IsValid()) {
  209. DPLOG(FATAL) << "CreateMutex failed";
  210. return false;
  211. @@ -429,6 +440,17 @@ bool ProcessSingleton::Create() {
  212. window_.CreateNamed(base::BindRepeating(&ProcessLaunchNotification,
  213. notification_callback_),
  214. user_data_dir_.value());
  215. +
  216. + // When the app is sandboxed, firstly, the app should not be in
  217. + // admin mode, and even if it somehow is, messages from an unelevated
  218. + // instance should not be able to be sent to it.
  219. + if (!is_app_sandboxed_) {
  220. + // NB: Ensure that if the primary app gets started as elevated
  221. + // admin inadvertently, secondary windows running not as elevated
  222. + // will still be able to send messages.
  223. + ::ChangeWindowMessageFilterEx(window_.hwnd(), WM_COPYDATA, MSGFLT_ALLOW,
  224. + NULL);
  225. + }
  226. CHECK(result && window_.hwnd());
  227. }
  228. }