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