Popular sites on the NTP: Favicon improvements
[chromium-blink-merge.git] / ipc / ipc_message_attachment_set.cc
blob1a4969f505159d318d0b7d684d8368863da49f12
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ipc/ipc_message_attachment_set.h"
7 #include <algorithm>
8 #include "base/logging.h"
9 #include "base/posix/eintr_wrapper.h"
10 #include "ipc/brokerable_attachment.h"
11 #include "ipc/ipc_message_attachment.h"
13 #if defined(OS_POSIX)
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <unistd.h>
17 #include "ipc/ipc_platform_file_attachment_posix.h"
18 #endif // OS_POSIX
20 namespace IPC {
22 namespace {
24 unsigned count_attachments_of_type(
25 const std::vector<scoped_refptr<MessageAttachment>>& attachments,
26 MessageAttachment::Type type) {
27 unsigned count = 0;
28 for (const scoped_refptr<MessageAttachment>& attachment : attachments) {
29 if (attachment->GetType() == type)
30 ++count;
32 return count;
35 } // namespace
37 MessageAttachmentSet::MessageAttachmentSet()
38 : consumed_descriptor_highwater_(0) {
41 MessageAttachmentSet::~MessageAttachmentSet() {
42 if (consumed_descriptor_highwater_ == size())
43 return;
45 // We close all the owning descriptors. If this message should have
46 // been transmitted, then closing those with close flags set mirrors
47 // the expected behaviour.
49 // If this message was received with more descriptors than expected
50 // (which could a DOS against the browser by a rogue renderer) then all
51 // the descriptors have their close flag set and we free all the extra
52 // kernel resources.
53 LOG(WARNING) << "MessageAttachmentSet destroyed with unconsumed descriptors: "
54 << consumed_descriptor_highwater_ << "/" << size();
57 unsigned MessageAttachmentSet::num_descriptors() const {
58 return count_attachments_of_type(attachments_,
59 MessageAttachment::TYPE_PLATFORM_FILE);
62 unsigned MessageAttachmentSet::num_mojo_handles() const {
63 return count_attachments_of_type(attachments_,
64 MessageAttachment::TYPE_MOJO_HANDLE);
67 unsigned MessageAttachmentSet::num_brokerable_attachments() const {
68 return count_attachments_of_type(
69 attachments_, MessageAttachment::TYPE_BROKERABLE_ATTACHMENT);
72 unsigned MessageAttachmentSet::size() const {
73 return static_cast<unsigned>(attachments_.size());
76 bool MessageAttachmentSet::AddAttachment(
77 scoped_refptr<MessageAttachment> attachment) {
78 #if defined(OS_POSIX)
79 if (attachment->GetType() == MessageAttachment::TYPE_PLATFORM_FILE &&
80 num_descriptors() == kMaxDescriptorsPerMessage) {
81 DLOG(WARNING) << "Cannot add file descriptor. MessageAttachmentSet full.";
82 return false;
84 #endif
86 attachments_.push_back(attachment);
87 return true;
90 scoped_refptr<MessageAttachment> MessageAttachmentSet::GetAttachmentAt(
91 unsigned index) {
92 if (index >= size()) {
93 DLOG(WARNING) << "Accessing out of bound index:" << index << "/" << size();
94 return scoped_refptr<MessageAttachment>();
97 // We should always walk the descriptors in order, so it's reasonable to
98 // enforce this. Consider the case where a compromised renderer sends us
99 // the following message:
101 // ExampleMsg:
102 // num_fds:2 msg:FD(index = 1) control:SCM_RIGHTS {n, m}
104 // Here the renderer sent us a message which should have a descriptor, but
105 // actually sent two in an attempt to fill our fd table and kill us. By
106 // setting the index of the descriptor in the message to 1 (it should be
107 // 0), we would record a highwater of 1 and then consider all the
108 // descriptors to have been used.
110 // So we can either track of the use of each descriptor in a bitset, or we
111 // can enforce that we walk the indexes strictly in order.
113 // There's one more wrinkle: When logging messages, we may reparse them. So
114 // we have an exception: When the consumed_descriptor_highwater_ is at the
115 // end of the array and index 0 is requested, we reset the highwater value.
116 // TODO(morrita): This is absurd. This "wringle" disallow to introduce clearer
117 // ownership model. Only client is NaclIPCAdapter. See crbug.com/415294
118 if (index == 0 && consumed_descriptor_highwater_ == size())
119 consumed_descriptor_highwater_ = 0;
121 if (index != consumed_descriptor_highwater_)
122 return scoped_refptr<MessageAttachment>();
124 consumed_descriptor_highwater_ = index + 1;
126 return attachments_[index];
129 void MessageAttachmentSet::CommitAll() {
130 attachments_.clear();
131 consumed_descriptor_highwater_ = 0;
134 std::vector<const BrokerableAttachment*>
135 MessageAttachmentSet::PeekBrokerableAttachments() const {
136 std::vector<const BrokerableAttachment*> output;
137 for (const scoped_refptr<MessageAttachment>& attachment : attachments_) {
138 if (attachment->GetType() ==
139 MessageAttachment::TYPE_BROKERABLE_ATTACHMENT) {
140 output.push_back(static_cast<BrokerableAttachment*>(attachment.get()));
143 return output;
146 std::vector<scoped_refptr<BrokerableAttachment>>
147 MessageAttachmentSet::GetBrokerableAttachmentsForUpdating() {
148 std::vector<scoped_refptr<BrokerableAttachment>> output;
149 for (const scoped_refptr<MessageAttachment>& attachment : attachments_) {
150 if (attachment->GetType() ==
151 MessageAttachment::TYPE_BROKERABLE_ATTACHMENT) {
152 output.push_back(scoped_refptr<BrokerableAttachment>(
153 static_cast<BrokerableAttachment*>(attachment.get())));
156 return output;
159 #if defined(OS_POSIX)
161 void MessageAttachmentSet::PeekDescriptors(base::PlatformFile* buffer) const {
162 for (size_t i = 0; i != attachments_.size(); ++i)
163 buffer[i] = internal::GetPlatformFile(attachments_[i]);
166 bool MessageAttachmentSet::ContainsDirectoryDescriptor() const {
167 struct stat st;
169 for (auto i = attachments_.begin(); i != attachments_.end(); ++i) {
170 if (fstat(internal::GetPlatformFile(*i), &st) == 0 && S_ISDIR(st.st_mode))
171 return true;
174 return false;
177 void MessageAttachmentSet::ReleaseFDsToClose(
178 std::vector<base::PlatformFile>* fds) {
179 for (size_t i = 0; i < attachments_.size(); ++i) {
180 internal::PlatformFileAttachment* file =
181 static_cast<internal::PlatformFileAttachment*>(attachments_[i].get());
182 if (file->Owns())
183 fds->push_back(file->TakePlatformFile());
186 CommitAll();
189 void MessageAttachmentSet::AddDescriptorsToOwn(const base::PlatformFile* buffer,
190 unsigned count) {
191 DCHECK(count <= kMaxDescriptorsPerMessage);
192 DCHECK_EQ(num_descriptors(), 0u);
193 DCHECK_EQ(consumed_descriptor_highwater_, 0u);
195 attachments_.reserve(count);
196 for (unsigned i = 0; i < count; ++i)
197 AddAttachment(
198 new internal::PlatformFileAttachment(base::ScopedFD(buffer[i])));
201 #endif // OS_POSIX
203 } // namespace IPC