[email protected] | 05094a3 | 2011-09-01 00:50:13 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 5 | #include "ipc/ipc_message_attachment_set.h" |
[email protected] | aac449e | 2010-06-10 21:39:04 | [diff] [blame] | 6 | |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame^] | 7 | #include <stddef.h> |
| 8 | |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 9 | #include <algorithm> |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame^] | 10 | |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 11 | #include "base/logging.h" |
[email protected] | 2025d00 | 2012-11-14 20:54:35 | [diff] [blame] | 12 | #include "base/posix/eintr_wrapper.h" |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame^] | 13 | #include "build/build_config.h" |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 14 | #include "ipc/brokerable_attachment.h" |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 15 | #include "ipc/ipc_message_attachment.h" |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 16 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 17 | #if defined(OS_POSIX) |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 18 | #include <sys/stat.h> |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame^] | 19 | #include <sys/types.h> |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 20 | #include <unistd.h> |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 21 | #include "ipc/ipc_platform_file_attachment_posix.h" |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 22 | #endif // OS_POSIX |
| 23 | |
| 24 | namespace IPC { |
| 25 | |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 26 | namespace { |
| 27 | |
| 28 | unsigned 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 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 41 | MessageAttachmentSet::MessageAttachmentSet() |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 42 | : consumed_descriptor_highwater_(0) { |
| 43 | } |
| 44 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 45 | MessageAttachmentSet::~MessageAttachmentSet() { |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 46 | if (consumed_descriptor_highwater_ == num_non_brokerable_attachments()) |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 47 | return; |
| 48 | |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 49 | // 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] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 52 | // |
| 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. |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 57 | LOG(WARNING) << "MessageAttachmentSet destroyed with unconsumed descriptors: " |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 58 | << consumed_descriptor_highwater_ << "/" << num_descriptors(); |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 59 | } |
| 60 | |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 61 | unsigned MessageAttachmentSet::num_descriptors() const { |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 62 | return count_attachments_of_type(attachments_, |
| 63 | MessageAttachment::TYPE_PLATFORM_FILE); |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 64 | } |
| 65 | |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 66 | unsigned MessageAttachmentSet::num_mojo_handles() const { |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 67 | return count_attachments_of_type(attachments_, |
| 68 | MessageAttachment::TYPE_MOJO_HANDLE); |
| 69 | } |
| 70 | |
| 71 | unsigned MessageAttachmentSet::num_brokerable_attachments() const { |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 72 | return static_cast<unsigned>(brokerable_attachments_.size()); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 73 | } |
| 74 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 75 | unsigned MessageAttachmentSet::num_non_brokerable_attachments() const { |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 76 | return static_cast<unsigned>(attachments_.size()); |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 77 | } |
| 78 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 79 | unsigned MessageAttachmentSet::size() const { |
| 80 | return static_cast<unsigned>(attachments_.size() + |
| 81 | brokerable_attachments_.size()); |
| 82 | } |
| 83 | |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 84 | bool MessageAttachmentSet::AddAttachment( |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 85 | scoped_refptr<MessageAttachment> attachment, |
| 86 | size_t* index, |
| 87 | bool* brokerable) { |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 88 | #if defined(OS_POSIX) |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 89 | if (attachment->GetType() == MessageAttachment::TYPE_PLATFORM_FILE && |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 90 | num_descriptors() == kMaxDescriptorsPerMessage) { |
| 91 | DLOG(WARNING) << "Cannot add file descriptor. MessageAttachmentSet full."; |
| 92 | return false; |
| 93 | } |
| 94 | #endif |
| 95 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 96 | 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] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 113 | } |
| 114 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 115 | bool MessageAttachmentSet::AddAttachment( |
| 116 | scoped_refptr<MessageAttachment> attachment) { |
| 117 | bool brokerable; |
| 118 | size_t index; |
| 119 | return AddAttachment(attachment, &index, &brokerable); |
| 120 | } |
| 121 | |
| 122 | scoped_refptr<MessageAttachment> |
| 123 | MessageAttachmentSet::GetNonBrokerableAttachmentAt(unsigned index) { |
| 124 | if (index >= num_non_brokerable_attachments()) { |
| 125 | DLOG(WARNING) << "Accessing out of bound index:" << index << "/" |
| 126 | << num_non_brokerable_attachments(); |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 127 | return scoped_refptr<MessageAttachment>(); |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 128 | } |
| 129 | |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 130 | // 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. |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 149 | // TODO(morrita): This is absurd. This "wringle" disallow to introduce clearer |
| 150 | // ownership model. Only client is NaclIPCAdapter. See crbug.com/415294 |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 151 | if (index == 0 && |
| 152 | consumed_descriptor_highwater_ == num_non_brokerable_attachments()) { |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 153 | consumed_descriptor_highwater_ = 0; |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 154 | } |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 155 | |
| 156 | if (index != consumed_descriptor_highwater_) |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 157 | return scoped_refptr<MessageAttachment>(); |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 158 | |
| 159 | consumed_descriptor_highwater_ = index + 1; |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 160 | |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 161 | return attachments_[index]; |
| 162 | } |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 163 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 164 | scoped_refptr<MessageAttachment> |
| 165 | MessageAttachmentSet::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 | |
| 177 | void MessageAttachmentSet::CommitAllDescriptors() { |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 178 | attachments_.clear(); |
| 179 | consumed_descriptor_highwater_ = 0; |
| 180 | } |
| 181 | |
erikchen | a03dde6f | 2015-10-29 22:37:04 | [diff] [blame] | 182 | std::vector<scoped_refptr<IPC::BrokerableAttachment>> |
erikchen | 3722a32 | 2015-10-07 20:51:55 | [diff] [blame] | 183 | MessageAttachmentSet::GetBrokerableAttachments() const { |
erikchen | a03dde6f | 2015-10-29 22:37:04 | [diff] [blame] | 184 | return brokerable_attachments_; |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 185 | } |
| 186 | |
erikchen | 87351da | 2015-09-15 19:11:09 | [diff] [blame] | 187 | void MessageAttachmentSet::ReplacePlaceholderWithAttachment( |
| 188 | const scoped_refptr<BrokerableAttachment>& attachment) { |
erikchen | a03dde6f | 2015-10-29 22:37:04 | [diff] [blame] | 189 | DCHECK_NE(BrokerableAttachment::PLACEHOLDER, attachment->GetBrokerableType()); |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 190 | for (auto it = brokerable_attachments_.begin(); |
| 191 | it != brokerable_attachments_.end(); ++it) { |
erikchen | a03dde6f | 2015-10-29 22:37:04 | [diff] [blame] | 192 | if ((*it)->GetBrokerableType() == BrokerableAttachment::PLACEHOLDER && |
| 193 | (*it)->GetIdentifier() == attachment->GetIdentifier()) { |
erikchen | 87351da | 2015-09-15 19:11:09 | [diff] [blame] | 194 | *it = attachment; |
| 195 | return; |
erikchen | de9412b8 | 2015-07-27 18:26:14 | [diff] [blame] | 196 | } |
| 197 | } |
erikchen | 87351da | 2015-09-15 19:11:09 | [diff] [blame] | 198 | |
| 199 | // This function should only be called if there is a placeholder ready to be |
| 200 | // replaced. |
| 201 | NOTREACHED(); |
erikchen | de9412b8 | 2015-07-27 18:26:14 | [diff] [blame] | 202 | } |
| 203 | |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 204 | #if defined(OS_POSIX) |
| 205 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 206 | void MessageAttachmentSet::PeekDescriptors(base::PlatformFile* buffer) const { |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 207 | for (size_t i = 0; i != attachments_.size(); ++i) |
| 208 | buffer[i] = internal::GetPlatformFile(attachments_[i]); |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 209 | } |
| 210 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 211 | bool MessageAttachmentSet::ContainsDirectoryDescriptor() const { |
[email protected] | aac449e | 2010-06-10 21:39:04 | [diff] [blame] | 212 | struct stat st; |
| 213 | |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 214 | for (auto i = attachments_.begin(); i != attachments_.end(); ++i) { |
| 215 | if (fstat(internal::GetPlatformFile(*i), &st) == 0 && S_ISDIR(st.st_mode)) |
[email protected] | aac449e | 2010-06-10 21:39:04 | [diff] [blame] | 216 | return true; |
| 217 | } |
| 218 | |
| 219 | return false; |
| 220 | } |
| 221 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 222 | void MessageAttachmentSet::ReleaseFDsToClose( |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 223 | std::vector<base::PlatformFile>* fds) { |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 224 | 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] | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 229 | } |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 230 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 231 | CommitAllDescriptors(); |
[email protected] | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 232 | } |
| 233 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 234 | void MessageAttachmentSet::AddDescriptorsToOwn(const base::PlatformFile* buffer, |
| 235 | unsigned count) { |
[email protected] | 05094a3 | 2011-09-01 00:50:13 | [diff] [blame] | 236 | DCHECK(count <= kMaxDescriptorsPerMessage); |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 237 | DCHECK_EQ(num_descriptors(), 0u); |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 238 | DCHECK_EQ(consumed_descriptor_highwater_, 0u); |
| 239 | |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 240 | attachments_.reserve(count); |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 241 | for (unsigned i = 0; i < count; ++i) |
| 242 | AddAttachment( |
| 243 | new internal::PlatformFileAttachment(base::ScopedFD(buffer[i]))); |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 244 | } |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 245 | |
| 246 | #endif // OS_POSIX |
| 247 | |
| 248 | } // namespace IPC |
| 249 | |
| 250 | |