| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ipc/file_descriptor_set_posix.h" | 5 #include "ipc/file_descriptor_set_posix.h" |
| 6 | 6 |
| 7 #include <sys/types.h> | 7 #include <sys/types.h> |
| 8 #include <sys/stat.h> | 8 #include <sys/stat.h> |
| 9 #include <unistd.h> | 9 #include <unistd.h> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/posix/eintr_wrapper.h" | 12 #include "base/posix/eintr_wrapper.h" |
| 13 | 13 |
| 14 FileDescriptorSet::FileDescriptorSet() | 14 FileDescriptorSet::FileDescriptorSet() |
| 15 : consumed_descriptor_highwater_(0) { | 15 : consumed_descriptor_highwater_(0) { |
| 16 } | 16 } |
| 17 | 17 |
| 18 FileDescriptorSet::~FileDescriptorSet() { | 18 FileDescriptorSet::~FileDescriptorSet() { |
| 19 if (consumed_descriptor_highwater_ == descriptors_.size()) | 19 if (consumed_descriptor_highwater_ == size()) |
| 20 return; | 20 return; |
| 21 | 21 |
| 22 LOG(WARNING) << "FileDescriptorSet destroyed with unconsumed descriptors"; | 22 // We close all the owning descriptors. If this message should have |
| 23 // We close all the descriptors where the close flag is set. If this | 23 // been transmitted, then closing those with close flags set mirrors |
| 24 // message should have been transmitted, then closing those with close | 24 // the expected behaviour. |
| 25 // flags set mirrors the expected behaviour. | |
| 26 // | 25 // |
| 27 // If this message was received with more descriptors than expected | 26 // If this message was received with more descriptors than expected |
| 28 // (which could a DOS against the browser by a rogue renderer) then all | 27 // (which could a DOS against the browser by a rogue renderer) then all |
| 29 // the descriptors have their close flag set and we free all the extra | 28 // the descriptors have their close flag set and we free all the extra |
| 30 // kernel resources. | 29 // kernel resources. |
| 31 for (unsigned i = consumed_descriptor_highwater_; | 30 LOG(WARNING) << "FileDescriptorSet destroyed with unconsumed descriptors: " |
| 32 i < descriptors_.size(); ++i) { | 31 << consumed_descriptor_highwater_ << "/" << size(); |
| 33 if (descriptors_[i].auto_close) | |
| 34 if (IGNORE_EINTR(close(descriptors_[i].fd)) < 0) | |
| 35 PLOG(ERROR) << "close"; | |
| 36 } | |
| 37 } | 32 } |
| 38 | 33 |
| 39 bool FileDescriptorSet::Add(int fd) { | 34 bool FileDescriptorSet::AddToBorrow(base::PlatformFile fd) { |
| 40 if (descriptors_.size() == kMaxDescriptorsPerMessage) { | 35 DCHECK_EQ(consumed_descriptor_highwater_, 0u); |
| 36 |
| 37 if (size() == kMaxDescriptorsPerMessage) { |
| 41 DLOG(WARNING) << "Cannot add file descriptor. FileDescriptorSet full."; | 38 DLOG(WARNING) << "Cannot add file descriptor. FileDescriptorSet full."; |
| 42 return false; | 39 return false; |
| 43 } | 40 } |
| 44 | 41 |
| 45 struct base::FileDescriptor sd; | 42 borrowed_descriptors_.push_back(fd); |
| 46 sd.fd = fd; | |
| 47 sd.auto_close = false; | |
| 48 descriptors_.push_back(sd); | |
| 49 return true; | 43 return true; |
| 50 } | 44 } |
| 51 | 45 |
| 52 bool FileDescriptorSet::AddAndAutoClose(int fd) { | 46 bool FileDescriptorSet::AddToOwn(base::File fd) { |
| 53 if (descriptors_.size() == kMaxDescriptorsPerMessage) { | 47 DCHECK_EQ(consumed_descriptor_highwater_, 0u); |
| 48 |
| 49 if (size() == kMaxDescriptorsPerMessage) { |
| 54 DLOG(WARNING) << "Cannot add file descriptor. FileDescriptorSet full."; | 50 DLOG(WARNING) << "Cannot add file descriptor. FileDescriptorSet full."; |
| 55 return false; | 51 return false; |
| 56 } | 52 } |
| 57 | 53 |
| 58 struct base::FileDescriptor sd; | 54 owned_descriptors_.push_back(new base::File(fd.Pass())); |
| 59 sd.fd = fd; | 55 DCHECK(size() <= kMaxDescriptorsPerMessage); |
| 60 sd.auto_close = true; | |
| 61 descriptors_.push_back(sd); | |
| 62 DCHECK(descriptors_.size() <= kMaxDescriptorsPerMessage); | |
| 63 return true; | 56 return true; |
| 64 } | 57 } |
| 65 | 58 |
| 66 int FileDescriptorSet::GetDescriptorAt(unsigned index) const { | 59 base::PlatformFile FileDescriptorSet::TakeDescriptorAt(unsigned index) { |
| 67 if (index >= descriptors_.size()) | 60 if (index >= size()) |
| 68 return -1; | 61 return -1; |
| 69 | 62 |
| 70 // We should always walk the descriptors in order, so it's reasonable to | 63 // We should always walk the descriptors in order, so it's reasonable to |
| 71 // enforce this. Consider the case where a compromised renderer sends us | 64 // enforce this. Consider the case where a compromised renderer sends us |
| 72 // the following message: | 65 // the following message: |
| 73 // | 66 // |
| 74 // ExampleMsg: | 67 // ExampleMsg: |
| 75 // num_fds:2 msg:FD(index = 1) control:SCM_RIGHTS {n, m} | 68 // num_fds:2 msg:FD(index = 1) control:SCM_RIGHTS {n, m} |
| 76 // | 69 // |
| 77 // Here the renderer sent us a message which should have a descriptor, but | 70 // Here the renderer sent us a message which should have a descriptor, but |
| 78 // actually sent two in an attempt to fill our fd table and kill us. By | 71 // actually sent two in an attempt to fill our fd table and kill us. By |
| 79 // setting the index of the descriptor in the message to 1 (it should be | 72 // setting the index of the descriptor in the message to 1 (it should be |
| 80 // 0), we would record a highwater of 1 and then consider all the | 73 // 0), we would record a highwater of 1 and then consider all the |
| 81 // descriptors to have been used. | 74 // descriptors to have been used. |
| 82 // | 75 // |
| 83 // So we can either track of the use of each descriptor in a bitset, or we | 76 // So we can either track of the use of each descriptor in a bitset, or we |
| 84 // can enforce that we walk the indexes strictly in order. | 77 // can enforce that we walk the indexes strictly in order. |
| 85 // | 78 // |
| 86 // There's one more wrinkle: When logging messages, we may reparse them. So | |
| 87 // we have an exception: When the consumed_descriptor_highwater_ is at the | |
| 88 // end of the array and index 0 is requested, we reset the highwater value. | |
| 89 if (index == 0 && consumed_descriptor_highwater_ == descriptors_.size()) | |
| 90 consumed_descriptor_highwater_ = 0; | |
| 91 | |
| 92 if (index != consumed_descriptor_highwater_) | 79 if (index != consumed_descriptor_highwater_) |
| 93 return -1; | 80 return -1; |
| 94 | 81 |
| 95 consumed_descriptor_highwater_ = index + 1; | 82 consumed_descriptor_highwater_ = index + 1; |
| 96 return descriptors_[index].fd; | 83 |
| 84 if (index < owned_descriptors_.size()) |
| 85 return owned_descriptors_[index]->TakePlatformFile(); |
| 86 |
| 87 // TODO(morrita): This only happens with unit tests and should never |
| 88 // be hit in production as all deserialized descriptors are owned by |
| 89 // IPC::Message. Once there is no borrowed descriptors being taken, |
| 90 // we can return File instead of PlatformFile here. Also see TODOs |
| 91 // in file_descriptor_set_posix_unittest.cc |
| 92 base::PlatformFile file = |
| 93 borrowed_descriptors_[index - owned_descriptors_.size()]; |
| 94 borrowed_descriptors_[index - owned_descriptors_.size()] = -1; |
| 95 return file; |
| 97 } | 96 } |
| 98 | 97 |
| 99 void FileDescriptorSet::GetDescriptors(int* buffer) const { | 98 void FileDescriptorSet::GetDescriptors(int* buffer) const { |
| 100 for (std::vector<base::FileDescriptor>::const_iterator | 99 for (ScopedVector<base::File>::const_iterator i = owned_descriptors_.begin(); |
| 101 i = descriptors_.begin(); i != descriptors_.end(); ++i) { | 100 i != owned_descriptors_.end(); |
| 102 *(buffer++) = i->fd; | 101 ++i) { |
| 102 *(buffer++) = (*i)->GetPlatformFile(); |
| 103 } |
| 104 |
| 105 for (std::vector<base::PlatformFile>::const_iterator i = |
| 106 borrowed_descriptors_.begin(); |
| 107 i != borrowed_descriptors_.end(); |
| 108 ++i) { |
| 109 *(buffer++) = *i; |
| 103 } | 110 } |
| 104 } | 111 } |
| 105 | 112 |
| 106 bool FileDescriptorSet::ContainsDirectoryDescriptor() const { | 113 bool FileDescriptorSet::ContainsDirectoryDescriptor() const { |
| 107 struct stat st; | 114 struct stat st; |
| 108 | 115 |
| 109 for (std::vector<base::FileDescriptor>::const_iterator | 116 for (ScopedVector<base::File>::const_iterator i = owned_descriptors_.begin(); |
| 110 i = descriptors_.begin(); i != descriptors_.end(); ++i) { | 117 i != owned_descriptors_.end(); |
| 111 if (fstat(i->fd, &st) == 0 && S_ISDIR(st.st_mode)) | 118 ++i) { |
| 119 if (fstat((*i)->GetPlatformFile(), &st) == 0 && S_ISDIR(st.st_mode)) |
| 112 return true; | 120 return true; |
| 113 } | 121 } |
| 114 | 122 |
| 123 for (std::vector<base::PlatformFile>::const_iterator i = |
| 124 borrowed_descriptors_.begin(); |
| 125 i != borrowed_descriptors_.end(); |
| 126 ++i) { |
| 127 if (fstat(*i, &st) == 0 && S_ISDIR(st.st_mode)) |
| 128 return true; |
| 129 } |
| 130 |
| 115 return false; | 131 return false; |
| 116 } | 132 } |
| 117 | 133 |
| 118 void FileDescriptorSet::CommitAll() { | 134 void FileDescriptorSet::CommitAll() { |
| 119 for (std::vector<base::FileDescriptor>::iterator | 135 owned_descriptors_.clear(); |
| 120 i = descriptors_.begin(); i != descriptors_.end(); ++i) { | 136 borrowed_descriptors_.clear(); |
| 121 if (i->auto_close) | |
| 122 if (IGNORE_EINTR(close(i->fd)) < 0) | |
| 123 PLOG(ERROR) << "close"; | |
| 124 } | |
| 125 descriptors_.clear(); | |
| 126 consumed_descriptor_highwater_ = 0; | 137 consumed_descriptor_highwater_ = 0; |
| 127 } | 138 } |
| 128 | 139 |
| 129 void FileDescriptorSet::ReleaseFDsToClose(std::vector<int>* fds) { | 140 void FileDescriptorSet::ReleaseFDsToClose( |
| 130 for (std::vector<base::FileDescriptor>::iterator | 141 std::vector<base::PlatformFile>* fds) { |
| 131 i = descriptors_.begin(); i != descriptors_.end(); ++i) { | 142 for (ScopedVector<base::File>::iterator i = owned_descriptors_.begin(); |
| 132 if (i->auto_close) | 143 i != owned_descriptors_.end(); |
| 133 fds->push_back(i->fd); | 144 ++i) { |
| 145 fds->push_back((*i)->TakePlatformFile()); |
| 134 } | 146 } |
| 135 descriptors_.clear(); | 147 |
| 136 consumed_descriptor_highwater_ = 0; | 148 CommitAll(); |
| 137 } | 149 } |
| 138 | 150 |
| 139 void FileDescriptorSet::SetDescriptors(const int* buffer, unsigned count) { | 151 void FileDescriptorSet::SetDescriptors(const int* buffer, unsigned count) { |
| 140 DCHECK(count <= kMaxDescriptorsPerMessage); | 152 DCHECK(count <= kMaxDescriptorsPerMessage); |
| 141 DCHECK_EQ(descriptors_.size(), 0u); | 153 DCHECK_EQ(size(), 0u); |
| 142 DCHECK_EQ(consumed_descriptor_highwater_, 0u); | 154 DCHECK_EQ(consumed_descriptor_highwater_, 0u); |
| 143 | 155 |
| 144 descriptors_.reserve(count); | 156 owned_descriptors_.reserve(count); |
| 145 for (unsigned i = 0; i < count; ++i) { | 157 for (unsigned i = 0; i < count; ++i) |
| 146 struct base::FileDescriptor sd; | 158 owned_descriptors_.push_back(new base::File(buffer[i])); |
| 147 sd.fd = buffer[i]; | |
| 148 sd.auto_close = true; | |
| 149 descriptors_.push_back(sd); | |
| 150 } | |
| 151 } | 159 } |
| OLD | NEW |