blob: 3b7eefb9a6e0f1e5492fd09c8a0434f2af32510a [file] [log] [blame]
[email protected]05094a32011-09-01 00:50:131// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]a17b7ca62009-02-12 18:09:112// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
morrita4b5c28e22015-01-14 21:17:065#include "ipc/ipc_message_attachment_set.h"
[email protected]aac449e2010-06-10 21:39:046
avi246998d82015-12-22 02:39:047#include <stddef.h>
8
morrita98b7aaa2015-01-26 22:42:549#include <algorithm>
avi246998d82015-12-22 02:39:0410
[email protected]a17b7ca62009-02-12 18:09:1111#include "base/logging.h"
[email protected]2025d002012-11-14 20:54:3512#include "base/posix/eintr_wrapper.h"
avi246998d82015-12-22 02:39:0413#include "build/build_config.h"
erikcheneece6c32015-07-07 22:13:1114#include "ipc/brokerable_attachment.h"
morrita98b7aaa2015-01-26 22:42:5415#include "ipc/ipc_message_attachment.h"
[email protected]a17b7ca62009-02-12 18:09:1116
morrita4b5c28e22015-01-14 21:17:0617#if defined(OS_POSIX)
morrita4b5c28e22015-01-14 21:17:0618#include <sys/stat.h>
avi246998d82015-12-22 02:39:0419#include <sys/types.h>
morrita4b5c28e22015-01-14 21:17:0620#include <unistd.h>
morrita1aa788c2015-01-31 05:45:4221#include "ipc/ipc_platform_file_attachment_posix.h"
morrita4b5c28e22015-01-14 21:17:0622#endif // OS_POSIX
23
24namespace IPC {
25
erikcheneece6c32015-07-07 22:13:1126namespace {
27
28unsigned count_attachments_of_type(
29 const std::vector<scoped_refptr<MessageAttachment>>& attachments,
30 MessageAttachment::Type type) {
31 unsigned count = 0;
32 for (const scoped_refptr<MessageAttachment>& attachment : attachments) {
33 if (attachment->GetType() == type)
34 ++count;
35 }
36 return count;
37}
38
39} // namespace
40
morrita4b5c28e22015-01-14 21:17:0641MessageAttachmentSet::MessageAttachmentSet()
[email protected]a17b7ca62009-02-12 18:09:1142 : consumed_descriptor_highwater_(0) {
43}
44
morrita4b5c28e22015-01-14 21:17:0645MessageAttachmentSet::~MessageAttachmentSet() {
erikchenae6d3212015-10-10 02:43:4946 if (consumed_descriptor_highwater_ == num_non_brokerable_attachments())
[email protected]a17b7ca62009-02-12 18:09:1147 return;
48
morrita96693852014-09-24 20:11:4549 // We close all the owning descriptors. If this message should have
50 // been transmitted, then closing those with close flags set mirrors
51 // the expected behaviour.
[email protected]a17b7ca62009-02-12 18:09:1152 //
53 // If this message was received with more descriptors than expected
54 // (which could a DOS against the browser by a rogue renderer) then all
55 // the descriptors have their close flag set and we free all the extra
56 // kernel resources.
morrita4b5c28e22015-01-14 21:17:0657 LOG(WARNING) << "MessageAttachmentSet destroyed with unconsumed descriptors: "
erikchenae6d3212015-10-10 02:43:4958 << consumed_descriptor_highwater_ << "/" << num_descriptors();
[email protected]a17b7ca62009-02-12 18:09:1159}
60
morrita98b7aaa2015-01-26 22:42:5461unsigned MessageAttachmentSet::num_descriptors() const {
erikcheneece6c32015-07-07 22:13:1162 return count_attachments_of_type(attachments_,
63 MessageAttachment::TYPE_PLATFORM_FILE);
morrita98b7aaa2015-01-26 22:42:5464}
65
morrita81b17e02015-02-06 00:58:3066unsigned MessageAttachmentSet::num_mojo_handles() const {
erikcheneece6c32015-07-07 22:13:1167 return count_attachments_of_type(attachments_,
68 MessageAttachment::TYPE_MOJO_HANDLE);
69}
70
71unsigned MessageAttachmentSet::num_brokerable_attachments() const {
erikchenae6d3212015-10-10 02:43:4972 return static_cast<unsigned>(brokerable_attachments_.size());
morrita81b17e02015-02-06 00:58:3073}
74
erikchenae6d3212015-10-10 02:43:4975unsigned MessageAttachmentSet::num_non_brokerable_attachments() const {
morrita98b7aaa2015-01-26 22:42:5476 return static_cast<unsigned>(attachments_.size());
morrita4b5c28e22015-01-14 21:17:0677}
78
erikchenae6d3212015-10-10 02:43:4979unsigned MessageAttachmentSet::size() const {
80 return static_cast<unsigned>(attachments_.size() +
81 brokerable_attachments_.size());
82}
83
morrita1aa788c2015-01-31 05:45:4284bool MessageAttachmentSet::AddAttachment(
erikchenae6d3212015-10-10 02:43:4985 scoped_refptr<MessageAttachment> attachment,
86 size_t* index,
87 bool* brokerable) {
morrita1aa788c2015-01-31 05:45:4288#if defined(OS_POSIX)
morrita81b17e02015-02-06 00:58:3089 if (attachment->GetType() == MessageAttachment::TYPE_PLATFORM_FILE &&
morrita1aa788c2015-01-31 05:45:4290 num_descriptors() == kMaxDescriptorsPerMessage) {
91 DLOG(WARNING) << "Cannot add file descriptor. MessageAttachmentSet full.";
92 return false;
93 }
94#endif
95
erikchenae6d3212015-10-10 02:43:4996 switch (attachment->GetType()) {
97 case MessageAttachment::TYPE_PLATFORM_FILE:
98 case MessageAttachment::TYPE_MOJO_HANDLE:
99 attachments_.push_back(attachment);
100 *index = attachments_.size() - 1;
101 *brokerable = false;
102 return true;
103 case MessageAttachment::TYPE_BROKERABLE_ATTACHMENT:
104 BrokerableAttachment* brokerable_attachment =
105 static_cast<BrokerableAttachment*>(attachment.get());
106 scoped_refptr<BrokerableAttachment> a(brokerable_attachment);
107 brokerable_attachments_.push_back(a);
108 *index = brokerable_attachments_.size() - 1;
109 *brokerable = true;
110 return true;
111 }
112 return false;
[email protected]a17b7ca62009-02-12 18:09:11113}
114
erikchenae6d3212015-10-10 02:43:49115bool MessageAttachmentSet::AddAttachment(
116 scoped_refptr<MessageAttachment> attachment) {
117 bool brokerable;
118 size_t index;
119 return AddAttachment(attachment, &index, &brokerable);
120}
121
122scoped_refptr<MessageAttachment>
123MessageAttachmentSet::GetNonBrokerableAttachmentAt(unsigned index) {
124 if (index >= num_non_brokerable_attachments()) {
125 DLOG(WARNING) << "Accessing out of bound index:" << index << "/"
126 << num_non_brokerable_attachments();
morrita98b7aaa2015-01-26 22:42:54127 return scoped_refptr<MessageAttachment>();
morrita96693852014-09-24 20:11:45128 }
129
[email protected]a17b7ca62009-02-12 18:09:11130 // We should always walk the descriptors in order, so it's reasonable to
131 // enforce this. Consider the case where a compromised renderer sends us
132 // the following message:
133 //
134 // ExampleMsg:
135 // num_fds:2 msg:FD(index = 1) control:SCM_RIGHTS {n, m}
136 //
137 // Here the renderer sent us a message which should have a descriptor, but
138 // actually sent two in an attempt to fill our fd table and kill us. By
139 // setting the index of the descriptor in the message to 1 (it should be
140 // 0), we would record a highwater of 1 and then consider all the
141 // descriptors to have been used.
142 //
143 // So we can either track of the use of each descriptor in a bitset, or we
144 // can enforce that we walk the indexes strictly in order.
145 //
146 // There's one more wrinkle: When logging messages, we may reparse them. So
147 // we have an exception: When the consumed_descriptor_highwater_ is at the
148 // end of the array and index 0 is requested, we reset the highwater value.
morrita96693852014-09-24 20:11:45149 // TODO(morrita): This is absurd. This "wringle" disallow to introduce clearer
150 // ownership model. Only client is NaclIPCAdapter. See crbug.com/415294
erikchenae6d3212015-10-10 02:43:49151 if (index == 0 &&
152 consumed_descriptor_highwater_ == num_non_brokerable_attachments()) {
[email protected]a17b7ca62009-02-12 18:09:11153 consumed_descriptor_highwater_ = 0;
erikchenae6d3212015-10-10 02:43:49154 }
[email protected]a17b7ca62009-02-12 18:09:11155
156 if (index != consumed_descriptor_highwater_)
morrita98b7aaa2015-01-26 22:42:54157 return scoped_refptr<MessageAttachment>();
[email protected]a17b7ca62009-02-12 18:09:11158
159 consumed_descriptor_highwater_ = index + 1;
morrita96693852014-09-24 20:11:45160
morrita98b7aaa2015-01-26 22:42:54161 return attachments_[index];
162}
morrita96693852014-09-24 20:11:45163
erikchenae6d3212015-10-10 02:43:49164scoped_refptr<MessageAttachment>
165MessageAttachmentSet::GetBrokerableAttachmentAt(unsigned index) {
166 if (index >= num_brokerable_attachments()) {
167 DLOG(WARNING) << "Accessing out of bound index:" << index << "/"
168 << num_brokerable_attachments();
169 return scoped_refptr<MessageAttachment>();
170 }
171
172 scoped_refptr<BrokerableAttachment> brokerable_attachment(
173 brokerable_attachments_[index]);
174 return scoped_refptr<MessageAttachment>(brokerable_attachment.get());
175}
176
177void MessageAttachmentSet::CommitAllDescriptors() {
morrita81b17e02015-02-06 00:58:30178 attachments_.clear();
179 consumed_descriptor_highwater_ = 0;
180}
181
erikchena03dde6f2015-10-29 22:37:04182std::vector<scoped_refptr<IPC::BrokerableAttachment>>
erikchen3722a322015-10-07 20:51:55183MessageAttachmentSet::GetBrokerableAttachments() const {
erikchena03dde6f2015-10-29 22:37:04184 return brokerable_attachments_;
erikcheneece6c32015-07-07 22:13:11185}
186
erikchen87351da2015-09-15 19:11:09187void MessageAttachmentSet::ReplacePlaceholderWithAttachment(
188 const scoped_refptr<BrokerableAttachment>& attachment) {
erikchena03dde6f2015-10-29 22:37:04189 DCHECK_NE(BrokerableAttachment::PLACEHOLDER, attachment->GetBrokerableType());
erikchenae6d3212015-10-10 02:43:49190 for (auto it = brokerable_attachments_.begin();
191 it != brokerable_attachments_.end(); ++it) {
erikchena03dde6f2015-10-29 22:37:04192 if ((*it)->GetBrokerableType() == BrokerableAttachment::PLACEHOLDER &&
193 (*it)->GetIdentifier() == attachment->GetIdentifier()) {
erikchen87351da2015-09-15 19:11:09194 *it = attachment;
195 return;
erikchende9412b82015-07-27 18:26:14196 }
197 }
erikchen87351da2015-09-15 19:11:09198
199 // This function should only be called if there is a placeholder ready to be
200 // replaced.
201 NOTREACHED();
erikchende9412b82015-07-27 18:26:14202}
203
morrita98b7aaa2015-01-26 22:42:54204#if defined(OS_POSIX)
205
morrita4b5c28e22015-01-14 21:17:06206void MessageAttachmentSet::PeekDescriptors(base::PlatformFile* buffer) const {
morrita98b7aaa2015-01-26 22:42:54207 for (size_t i = 0; i != attachments_.size(); ++i)
208 buffer[i] = internal::GetPlatformFile(attachments_[i]);
[email protected]a17b7ca62009-02-12 18:09:11209}
210
morrita4b5c28e22015-01-14 21:17:06211bool MessageAttachmentSet::ContainsDirectoryDescriptor() const {
[email protected]aac449e2010-06-10 21:39:04212 struct stat st;
213
morrita98b7aaa2015-01-26 22:42:54214 for (auto i = attachments_.begin(); i != attachments_.end(); ++i) {
215 if (fstat(internal::GetPlatformFile(*i), &st) == 0 && S_ISDIR(st.st_mode))
[email protected]aac449e2010-06-10 21:39:04216 return true;
217 }
218
219 return false;
220}
221
morrita4b5c28e22015-01-14 21:17:06222void MessageAttachmentSet::ReleaseFDsToClose(
morrita96693852014-09-24 20:11:45223 std::vector<base::PlatformFile>* fds) {
morrita1aa788c2015-01-31 05:45:42224 for (size_t i = 0; i < attachments_.size(); ++i) {
225 internal::PlatformFileAttachment* file =
226 static_cast<internal::PlatformFileAttachment*>(attachments_[i].get());
227 if (file->Owns())
228 fds->push_back(file->TakePlatformFile());
[email protected]dc875dc2013-10-15 00:07:00229 }
morrita96693852014-09-24 20:11:45230
erikchenae6d3212015-10-10 02:43:49231 CommitAllDescriptors();
[email protected]dc875dc2013-10-15 00:07:00232}
233
morrita4b5c28e22015-01-14 21:17:06234void MessageAttachmentSet::AddDescriptorsToOwn(const base::PlatformFile* buffer,
235 unsigned count) {
[email protected]05094a32011-09-01 00:50:13236 DCHECK(count <= kMaxDescriptorsPerMessage);
morrita98b7aaa2015-01-26 22:42:54237 DCHECK_EQ(num_descriptors(), 0u);
[email protected]a17b7ca62009-02-12 18:09:11238 DCHECK_EQ(consumed_descriptor_highwater_, 0u);
239
morrita98b7aaa2015-01-26 22:42:54240 attachments_.reserve(count);
morrita1aa788c2015-01-31 05:45:42241 for (unsigned i = 0; i < count; ++i)
242 AddAttachment(
243 new internal::PlatformFileAttachment(base::ScopedFD(buffer[i])));
[email protected]a17b7ca62009-02-12 18:09:11244}
morrita4b5c28e22015-01-14 21:17:06245
246#endif // OS_POSIX
247
248} // namespace IPC
249
250