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 b05c0157ecca222452fb38e3b28c9d7cc6bcfbfd..02d9eabfef9521722340739bf86df3dfc30018d7 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 f2913f2d78edb2547ae21816b34936d38f1870a9..298c9c81fa110ad7900d0bd6822136bb57f0382e 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. @@ -81,6 +82,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. @@ -97,7 +99,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. @@ -343,6 +345,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. @@ -366,6 +370,7 @@ bool DisplayProfileInUseError(const base::FilePath& lock_path,
  81. NOTREACHED();
  82. return false;
  83. +#endif
  84. }
  85. bool IsChromeProcess(pid_t pid) {
  86. @@ -378,6 +383,21 @@ bool IsChromeProcess(pid_t pid) {
  87. base::FilePath(chrome::kBrowserProcessExecutableName));
  88. }
  89. +bool IsAppSandboxed() {
  90. +#if BUILDFLAG(IS_MAC)
  91. + // NB: There is no sane API for this, we have to just guess by
  92. + // reading tea leaves
  93. + base::FilePath home_dir;
  94. + if (!base::PathService::Get(base::DIR_HOME, &home_dir)) {
  95. + return false;
  96. + }
  97. +
  98. + return home_dir.value().find("Library/Containers") != std::string::npos;
  99. +#else
  100. + return false;
  101. +#endif // BUILDFLAG(IS_MAC)
  102. +}
  103. +
  104. // A helper class to hold onto a socket.
  105. class ScopedSocket {
  106. public:
  107. @@ -781,6 +801,10 @@ ProcessSingleton::~ProcessSingleton() {
  108. if (watcher_) {
  109. watcher_->OnEminentProcessSingletonDestruction();
  110. }
  111. + // Manually free resources with IO explicitly allowed.
  112. + base::ScopedAllowBlocking allow_blocking;
  113. + watcher_ = nullptr;
  114. + std::ignore = socket_dir_.Delete();
  115. }
  116. ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcess() {
  117. @@ -1047,11 +1071,32 @@ bool ProcessSingleton::Create() {
  118. // Create the socket file somewhere in /tmp which is usually mounted as a
  119. // normal filesystem. Some network filesystems (notably AFS) are screwy and
  120. // do not support Unix domain sockets.
  121. - if (!socket_dir_.CreateUniqueTempDir()) {
  122. - LOG(ERROR) << "Failed to create socket directory.";
  123. + base::FilePath tmp_dir;
  124. + if (!base::GetTempDir(&tmp_dir)) {
  125. + LOG(ERROR) << "Failed to get temporary directory.";
  126. return false;
  127. }
  128. + if (IsAppSandboxed()) {
  129. + // For sandboxed applications, the tmp dir could be too long to fit
  130. + // addr->sun_path, so we need to make it as short as possible.
  131. + if (!socket_dir_.Set(tmp_dir.Append("S"))) {
  132. + LOG(ERROR) << "Failed to set socket directory.";
  133. + return false;
  134. + }
  135. + } else {
  136. + // Create the socket file somewhere in /tmp which is usually mounted as a
  137. + // normal filesystem. Some network filesystems (notably AFS) are screwy and
  138. + // do not support Unix domain sockets.
  139. + // Prefer CreateUniqueTempDirUnderPath rather than CreateUniqueTempDir as
  140. + // the latter will calculate unique paths based on bundle ids which can
  141. + // increase the socket path length than what is allowed.
  142. + if (!socket_dir_.CreateUniqueTempDirUnderPath(tmp_dir)) {
  143. + LOG(ERROR) << "Failed to create socket directory.";
  144. + return false;
  145. + }
  146. + }
  147. +
  148. // Check that the directory was created with the correct permissions.
  149. int dir_mode = 0;
  150. CHECK(base::GetPosixFilePermissions(socket_dir_.GetPath(), &dir_mode) &&
  151. diff --git a/chrome/browser/process_singleton_win.cc b/chrome/browser/process_singleton_win.cc
  152. index 071f3c05b8f899857531f2f08c05c7f2551eb4d0..11f35769cc53b4aa111a319d155a3916f0040fa7 100644
  153. --- a/chrome/browser/process_singleton_win.cc
  154. +++ b/chrome/browser/process_singleton_win.cc
  155. @@ -28,7 +28,9 @@
  156. #include "base/win/wmi.h"
  157. #include "chrome/browser/process_singleton_internal.h"
  158. #include "chrome/browser/shell_integration.h"
  159. +#if 0
  160. #include "chrome/browser/ui/simple_message_box.h"
  161. +#endif
  162. #include "chrome/browser/win/chrome_process_finder.h"
  163. #include "chrome/common/chrome_constants.h"
  164. #include "chrome/common/chrome_paths.h"
  165. @@ -163,6 +165,7 @@ bool ProcessLaunchNotification(
  166. }
  167. bool DisplayShouldKillMessageBox() {
  168. +#if 0
  169. TRACE_EVENT0("startup", "ProcessSingleton:DisplayShouldKillMessageBox");
  170. // Ensure there is an instance of ResourceBundle that is initialized for
  171. @@ -173,6 +176,10 @@ bool DisplayShouldKillMessageBox() {
  172. NULL, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME),
  173. l10n_util::GetStringUTF16(IDS_BROWSER_HUNGBROWSER_MESSAGE)) !=
  174. chrome::MESSAGE_BOX_RESULT_NO;
  175. +#endif
  176. + // This is called when the secondary process can't ping the primary
  177. + // process.
  178. + return false;
  179. }
  180. // Function was copied from Process::Terminate.
  181. @@ -255,9 +262,13 @@ bool ProcessSingleton::EscapeVirtualization(
  182. }
  183. ProcessSingleton::ProcessSingleton(
  184. + const std::string& program_name,
  185. const base::FilePath& user_data_dir,
  186. + bool is_app_sandboxed,
  187. const NotificationCallback& notification_callback)
  188. : notification_callback_(notification_callback),
  189. + program_name_(program_name),
  190. + is_app_sandboxed_(is_app_sandboxed),
  191. is_virtualized_(false),
  192. lock_file_(INVALID_HANDLE_VALUE),
  193. user_data_dir_(user_data_dir),
  194. @@ -377,7 +388,7 @@ ProcessSingleton::NotifyOtherProcessOrCreate() {
  195. bool ProcessSingleton::Create() {
  196. TRACE_EVENT0("startup", "ProcessSingleton::Create");
  197. - static const wchar_t kMutexName[] = L"Local\\ChromeProcessSingletonStartup!";
  198. + std::wstring mutexName = base::UTF8ToWide("Local\\" + program_name_ + "ProcessSingletonStartup");
  199. remote_window_ = chrome::FindRunningChromeWindow(user_data_dir_);
  200. if (!remote_window_ && !EscapeVirtualization(user_data_dir_)) {
  201. @@ -386,7 +397,7 @@ bool ProcessSingleton::Create() {
  202. // access. As documented, it's clearer to NOT request ownership on creation
  203. // since it isn't guaranteed we will get it. It is better to create it
  204. // without ownership and explicitly get the ownership afterward.
  205. - base::win::ScopedHandle only_me(::CreateMutex(NULL, FALSE, kMutexName));
  206. + base::win::ScopedHandle only_me(::CreateMutex(NULL, FALSE, mutexName.c_str()));
  207. if (!only_me.IsValid()) {
  208. DPLOG(FATAL) << "CreateMutex failed";
  209. return false;
  210. @@ -425,6 +436,17 @@ bool ProcessSingleton::Create() {
  211. window_.CreateNamed(base::BindRepeating(&ProcessLaunchNotification,
  212. notification_callback_),
  213. user_data_dir_.value());
  214. +
  215. + // When the app is sandboxed, firstly, the app should not be in
  216. + // admin mode, and even if it somehow is, messages from an unelevated
  217. + // instance should not be able to be sent to it.
  218. + if (!is_app_sandboxed_) {
  219. + // NB: Ensure that if the primary app gets started as elevated
  220. + // admin inadvertently, secondary windows running not as elevated
  221. + // will still be able to send messages.
  222. + ::ChangeWindowMessageFilterEx(window_.hwnd(), WM_COPYDATA, MSGFLT_ALLOW,
  223. + NULL);
  224. + }
  225. CHECK(result && window_.hwnd());
  226. }
  227. }