Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

Commit

Permalink
ipc: Slight change to GetSenderWithProcessId().
Browse files Browse the repository at this point in the history
Previously, the implementation of the method acquired a lock. This means that
the caller could not guarantee that the returned Sender was still valid. I
changed the method to require that the caller acquire the lock, so that the
caller can guarantee the validity of the returned Sender.

BUG=561734

Review URL: https://codereview.chromium.org/1484763003

Cr-Commit-Position: refs/heads/master@{#362223}
  • Loading branch information
erikchen authored and Commit bot committed Nov 30, 2015
1 parent 39f3de4 commit 63c0fa2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ipc/attachment_broker_privileged.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void AttachmentBrokerPrivileged::DeregisterCommunicationChannel(
}

Sender* AttachmentBrokerPrivileged::GetSenderWithProcessId(base::ProcessId id) {
base::AutoLock auto_lock(*get_lock());
get_lock()->AssertAcquired();
auto it = std::find_if(endpoints_.begin(), endpoints_.end(),
[id](Endpoint* c) { return c->GetPeerPID() == id; });
if (it == endpoints_.end())
Expand Down
3 changes: 3 additions & 0 deletions ipc/attachment_broker_privileged.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class IPC_EXPORT AttachmentBrokerPrivileged : public IPC::AttachmentBroker {
protected:
// Returns the sender whose peer's process id is |id|.
// Returns nullptr if no sender is found.
// The lock returned by get_lock() must already be acquired before calling
// this method. The return value is only guaranteed to be valid while the lock
// is held.
Sender* GetSenderWithProcessId(base::ProcessId id);

// Errors that can be reported by subclasses.
Expand Down
18 changes: 12 additions & 6 deletions ipc/attachment_broker_privileged_mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "base/memory/shared_memory.h"
#include "base/process/port_provider_mac.h"
#include "base/process/process.h"
#include "base/synchronization/lock.h"
#include "ipc/attachment_broker_messages.h"
#include "ipc/brokerable_attachment.h"
#include "ipc/ipc_channel.h"
Expand Down Expand Up @@ -210,6 +211,7 @@ bool AttachmentBrokerPrivilegedMac::RouteWireFormatToAnother(

// Another process is the destination.
base::ProcessId dest = wire_format.destination_process;
base::AutoLock auto_lock(*get_lock());
Sender* sender = GetSenderWithProcessId(dest);
if (!sender) {
// Assuming that this message was not sent from a malicious process, the
Expand Down Expand Up @@ -319,6 +321,7 @@ void AttachmentBrokerPrivilegedMac::SendPrecursorsForProcess(
bool to_self = pid == base::GetCurrentProcId();

if (!to_self) {
base::AutoLock auto_lock(*get_lock());
if (!GetSenderWithProcessId(pid)) {
// If there is no sender, then the destination process is no longer
// running, or never existed to begin with.
Expand Down Expand Up @@ -387,12 +390,15 @@ void AttachmentBrokerPrivilegedMac::ProcessExtractorsForProcess(
if (it == extractors_.end())
return;

if (!GetSenderWithProcessId(pid)) {
// If there is no sender, then the source process is no longer running.
LogError(ERROR_SOURCE_NOT_FOUND);
delete it->second;
extractors_.erase(it);
return;
{
base::AutoLock auto_lock(*get_lock());
if (!GetSenderWithProcessId(pid)) {
// If there is no sender, then the source process is no longer running.
LogError(ERROR_SOURCE_NOT_FOUND);
delete it->second;
extractors_.erase(it);
return;
}
}

mach_port_t task_port = port_provider_->TaskForPid(pid);
Expand Down

0 comments on commit 63c0fa2

Please sign in to comment.