base: Fix shared memory handle leak in IPC serialization
PlatformSharedMemoryRegion leaked handles after serializing into the IPC message
on Windows, Mac and Fuchsia. Serialization code incorrectly assumed that
HandleWin, MachPortMac and HandleFuchsia classes take ownership over the handle.
What really happens is that HandleFoo is passed to WriteParam(), which causes
ParamTraits<HandleFoo>::Write() to be invoked, before it returns, creating the
HandleAttachmentFoo, which will duplicate the handle.
By the time we return from WriteParam() the handle should have been duplicated
into the serialized message, so we can safely let the PSMR close the handle when
it goes out of scope.
This CL makes the PlatformSharedMemoryRegion serialization repeat the logic of
the base::SharedMemoryHandle serialization as if the OwnershipPassesToIPC() flag
is always set to true.
Bug: 873313, 873668
Change-Id: I4e98ff9483a48970ebcb2fcf6b150f447af82794
Reviewed-on: https://chromium-review.googlesource.com/1175918
Commit-Queue: Wez <[email protected]>
Reviewed-by: Daniel Cheng <[email protected]>
Reviewed-by: Wez <[email protected]>
Reviewed-by: Ken Rockot <[email protected]>
Cr-Commit-Position: refs/heads/master@{#583434}
diff --git a/ipc/ipc_message_utils.cc b/ipc/ipc_message_utils.cc
index a0614240..eb6023a 100644
--- a/ipc/ipc_message_utils.cc
+++ b/ipc/ipc_message_utils.cc
@@ -891,16 +891,16 @@
#if defined(OS_WIN)
base::win::ScopedHandle h = const_cast<param_type&>(p).PassPlatformHandle();
- HandleWin handle_win(h.Take());
+ HandleWin handle_win(h.Get());
WriteParam(m, handle_win);
#elif defined(OS_FUCHSIA)
zx::handle h = const_cast<param_type&>(p).PassPlatformHandle();
- HandleFuchsia handle_fuchsia(h.release());
+ HandleFuchsia handle_fuchsia(h.get());
WriteParam(m, handle_fuchsia);
#elif defined(OS_MACOSX) && !defined(OS_IOS)
base::mac::ScopedMachSendRight h =
const_cast<param_type&>(p).PassPlatformHandle();
- MachPortMac mach_port_mac(h.release());
+ MachPortMac mach_port_mac(h.get());
WriteParam(m, mach_port_mac);
#elif defined(OS_ANDROID)
m->WriteAttachment(new internal::PlatformFileAttachment(