[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 | |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 7 | #include <algorithm> |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 8 | #include "base/logging.h" |
[email protected] | 2025d00 | 2012-11-14 20:54:35 | [diff] [blame] | 9 | #include "base/posix/eintr_wrapper.h" |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 10 | #include "ipc/ipc_message_attachment.h" |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 11 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 12 | #if defined(OS_POSIX) |
| 13 | #include <sys/types.h> |
| 14 | #include <sys/stat.h> |
| 15 | #include <unistd.h> |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 16 | #include "ipc/ipc_platform_file_attachment_posix.h" |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 17 | #endif // OS_POSIX |
| 18 | |
| 19 | namespace IPC { |
| 20 | |
| 21 | MessageAttachmentSet::MessageAttachmentSet() |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 22 | : consumed_descriptor_highwater_(0) { |
| 23 | } |
| 24 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 25 | MessageAttachmentSet::~MessageAttachmentSet() { |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 26 | if (consumed_descriptor_highwater_ == size()) |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 27 | return; |
| 28 | |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 29 | // We close all the owning descriptors. If this message should have |
| 30 | // been transmitted, then closing those with close flags set mirrors |
| 31 | // the expected behaviour. |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 32 | // |
| 33 | // If this message was received with more descriptors than expected |
| 34 | // (which could a DOS against the browser by a rogue renderer) then all |
| 35 | // the descriptors have their close flag set and we free all the extra |
| 36 | // kernel resources. |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 37 | LOG(WARNING) << "MessageAttachmentSet destroyed with unconsumed descriptors: " |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 38 | << consumed_descriptor_highwater_ << "/" << size(); |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 39 | } |
| 40 | |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 41 | unsigned MessageAttachmentSet::num_descriptors() const { |
| 42 | return std::count_if(attachments_.begin(), attachments_.end(), |
| 43 | [](scoped_refptr<MessageAttachment> i) { |
| 44 | return i->GetType() == MessageAttachment::TYPE_PLATFORM_FILE; |
| 45 | }); |
| 46 | } |
| 47 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 48 | unsigned MessageAttachmentSet::size() const { |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 49 | return static_cast<unsigned>(attachments_.size()); |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 50 | } |
| 51 | |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 52 | bool MessageAttachmentSet::AddAttachment( |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 53 | scoped_refptr<MessageAttachment> attachment) { |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 54 | #if defined(OS_POSIX) |
| 55 | if (attachment->GetType() != MessageAttachment::TYPE_PLATFORM_FILE || |
| 56 | num_descriptors() == kMaxDescriptorsPerMessage) { |
| 57 | DLOG(WARNING) << "Cannot add file descriptor. MessageAttachmentSet full."; |
| 58 | return false; |
| 59 | } |
| 60 | #endif |
| 61 | |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 62 | attachments_.push_back(attachment); |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 63 | return true; |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 64 | } |
| 65 | |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 66 | scoped_refptr<MessageAttachment> MessageAttachmentSet::GetAttachmentAt( |
| 67 | unsigned index) { |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 68 | if (index >= size()) { |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 69 | DLOG(WARNING) << "Accessing out of bound index:" << index << "/" << size(); |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 70 | return scoped_refptr<MessageAttachment>(); |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 71 | } |
| 72 | |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 73 | // We should always walk the descriptors in order, so it's reasonable to |
| 74 | // enforce this. Consider the case where a compromised renderer sends us |
| 75 | // the following message: |
| 76 | // |
| 77 | // ExampleMsg: |
| 78 | // num_fds:2 msg:FD(index = 1) control:SCM_RIGHTS {n, m} |
| 79 | // |
| 80 | // Here the renderer sent us a message which should have a descriptor, but |
| 81 | // actually sent two in an attempt to fill our fd table and kill us. By |
| 82 | // setting the index of the descriptor in the message to 1 (it should be |
| 83 | // 0), we would record a highwater of 1 and then consider all the |
| 84 | // descriptors to have been used. |
| 85 | // |
| 86 | // So we can either track of the use of each descriptor in a bitset, or we |
| 87 | // can enforce that we walk the indexes strictly in order. |
| 88 | // |
| 89 | // There's one more wrinkle: When logging messages, we may reparse them. So |
| 90 | // we have an exception: When the consumed_descriptor_highwater_ is at the |
| 91 | // end of the array and index 0 is requested, we reset the highwater value. |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 92 | // TODO(morrita): This is absurd. This "wringle" disallow to introduce clearer |
| 93 | // ownership model. Only client is NaclIPCAdapter. See crbug.com/415294 |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 94 | if (index == 0 && consumed_descriptor_highwater_ == size()) |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 95 | consumed_descriptor_highwater_ = 0; |
| 96 | |
| 97 | if (index != consumed_descriptor_highwater_) |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 98 | return scoped_refptr<MessageAttachment>(); |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 99 | |
| 100 | consumed_descriptor_highwater_ = index + 1; |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 101 | |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 102 | return attachments_[index]; |
| 103 | } |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 104 | |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 105 | #if defined(OS_POSIX) |
| 106 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 107 | void MessageAttachmentSet::PeekDescriptors(base::PlatformFile* buffer) const { |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 108 | for (size_t i = 0; i != attachments_.size(); ++i) |
| 109 | buffer[i] = internal::GetPlatformFile(attachments_[i]); |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 110 | } |
| 111 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 112 | bool MessageAttachmentSet::ContainsDirectoryDescriptor() const { |
[email protected] | aac449e | 2010-06-10 21:39:04 | [diff] [blame] | 113 | struct stat st; |
| 114 | |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 115 | for (auto i = attachments_.begin(); i != attachments_.end(); ++i) { |
| 116 | if (fstat(internal::GetPlatformFile(*i), &st) == 0 && S_ISDIR(st.st_mode)) |
[email protected] | aac449e | 2010-06-10 21:39:04 | [diff] [blame] | 117 | return true; |
| 118 | } |
| 119 | |
| 120 | return false; |
| 121 | } |
| 122 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 123 | void MessageAttachmentSet::CommitAll() { |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 124 | attachments_.clear(); |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 125 | consumed_descriptor_highwater_ = 0; |
| 126 | } |
| 127 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 128 | void MessageAttachmentSet::ReleaseFDsToClose( |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 129 | std::vector<base::PlatformFile>* fds) { |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 130 | for (size_t i = 0; i < attachments_.size(); ++i) { |
| 131 | internal::PlatformFileAttachment* file = |
| 132 | static_cast<internal::PlatformFileAttachment*>(attachments_[i].get()); |
| 133 | if (file->Owns()) |
| 134 | fds->push_back(file->TakePlatformFile()); |
[email protected] | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 135 | } |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 136 | |
| 137 | CommitAll(); |
[email protected] | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 138 | } |
| 139 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 140 | void MessageAttachmentSet::AddDescriptorsToOwn(const base::PlatformFile* buffer, |
| 141 | unsigned count) { |
[email protected] | 05094a3 | 2011-09-01 00:50:13 | [diff] [blame] | 142 | DCHECK(count <= kMaxDescriptorsPerMessage); |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 143 | DCHECK_EQ(num_descriptors(), 0u); |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 144 | DCHECK_EQ(consumed_descriptor_highwater_, 0u); |
| 145 | |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 146 | attachments_.reserve(count); |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 147 | for (unsigned i = 0; i < count; ++i) |
| 148 | AddAttachment( |
| 149 | new internal::PlatformFileAttachment(base::ScopedFD(buffer[i]))); |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 150 | } |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 151 | |
| 152 | #endif // OS_POSIX |
| 153 | |
| 154 | } // namespace IPC |
| 155 | |
| 156 | |