Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #include "chrome/common/file_descriptor_set_posix.h" |
michael@0 | 6 | |
michael@0 | 7 | #include "base/eintr_wrapper.h" |
michael@0 | 8 | #include "base/logging.h" |
michael@0 | 9 | |
michael@0 | 10 | #include <unistd.h> |
michael@0 | 11 | |
michael@0 | 12 | FileDescriptorSet::FileDescriptorSet() |
michael@0 | 13 | : consumed_descriptor_highwater_(0) { |
michael@0 | 14 | } |
michael@0 | 15 | |
michael@0 | 16 | FileDescriptorSet::~FileDescriptorSet() { |
michael@0 | 17 | if (consumed_descriptor_highwater_ == descriptors_.size()) |
michael@0 | 18 | return; |
michael@0 | 19 | |
michael@0 | 20 | CHROMIUM_LOG(WARNING) << "FileDescriptorSet destroyed with unconsumed descriptors"; |
michael@0 | 21 | // We close all the descriptors where the close flag is set. If this |
michael@0 | 22 | // message should have been transmitted, then closing those with close |
michael@0 | 23 | // flags set mirrors the expected behaviour. |
michael@0 | 24 | // |
michael@0 | 25 | // If this message was received with more descriptors than expected |
michael@0 | 26 | // (which could a DOS against the browser by a rogue renderer) then all |
michael@0 | 27 | // the descriptors have their close flag set and we free all the extra |
michael@0 | 28 | // kernel resources. |
michael@0 | 29 | for (unsigned i = consumed_descriptor_highwater_; |
michael@0 | 30 | i < descriptors_.size(); ++i) { |
michael@0 | 31 | if (descriptors_[i].auto_close) |
michael@0 | 32 | HANDLE_EINTR(close(descriptors_[i].fd)); |
michael@0 | 33 | } |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | bool FileDescriptorSet::Add(int fd) { |
michael@0 | 37 | if (descriptors_.size() == MAX_DESCRIPTORS_PER_MESSAGE) |
michael@0 | 38 | return false; |
michael@0 | 39 | |
michael@0 | 40 | struct base::FileDescriptor sd; |
michael@0 | 41 | sd.fd = fd; |
michael@0 | 42 | sd.auto_close = false; |
michael@0 | 43 | descriptors_.push_back(sd); |
michael@0 | 44 | return true; |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | bool FileDescriptorSet::AddAndAutoClose(int fd) { |
michael@0 | 48 | if (descriptors_.size() == MAX_DESCRIPTORS_PER_MESSAGE) |
michael@0 | 49 | return false; |
michael@0 | 50 | |
michael@0 | 51 | struct base::FileDescriptor sd; |
michael@0 | 52 | sd.fd = fd; |
michael@0 | 53 | sd.auto_close = true; |
michael@0 | 54 | descriptors_.push_back(sd); |
michael@0 | 55 | DCHECK(descriptors_.size() <= MAX_DESCRIPTORS_PER_MESSAGE); |
michael@0 | 56 | return true; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | int FileDescriptorSet::GetDescriptorAt(unsigned index) const { |
michael@0 | 60 | if (index >= descriptors_.size()) |
michael@0 | 61 | return -1; |
michael@0 | 62 | |
michael@0 | 63 | // We should always walk the descriptors in order, so it's reasonable to |
michael@0 | 64 | // enforce this. Consider the case where a compromised renderer sends us |
michael@0 | 65 | // the following message: |
michael@0 | 66 | // |
michael@0 | 67 | // ExampleMsg: |
michael@0 | 68 | // num_fds:2 msg:FD(index = 1) control:SCM_RIGHTS {n, m} |
michael@0 | 69 | // |
michael@0 | 70 | // Here the renderer sent us a message which should have a descriptor, but |
michael@0 | 71 | // actually sent two in an attempt to fill our fd table and kill us. By |
michael@0 | 72 | // setting the index of the descriptor in the message to 1 (it should be |
michael@0 | 73 | // 0), we would record a highwater of 1 and then consider all the |
michael@0 | 74 | // descriptors to have been used. |
michael@0 | 75 | // |
michael@0 | 76 | // So we can either track of the use of each descriptor in a bitset, or we |
michael@0 | 77 | // can enforce that we walk the indexes strictly in order. |
michael@0 | 78 | // |
michael@0 | 79 | // There's one more wrinkle: When logging messages, we may reparse them. So |
michael@0 | 80 | // we have an exception: When the consumed_descriptor_highwater_ is at the |
michael@0 | 81 | // end of the array and index 0 is requested, we reset the highwater value. |
michael@0 | 82 | if (index == 0 && consumed_descriptor_highwater_ == descriptors_.size()) |
michael@0 | 83 | consumed_descriptor_highwater_ = 0; |
michael@0 | 84 | |
michael@0 | 85 | if (index != consumed_descriptor_highwater_) |
michael@0 | 86 | return -1; |
michael@0 | 87 | |
michael@0 | 88 | consumed_descriptor_highwater_ = index + 1; |
michael@0 | 89 | return descriptors_[index].fd; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | void FileDescriptorSet::GetDescriptors(int* buffer) const { |
michael@0 | 93 | for (std::vector<base::FileDescriptor>::const_iterator |
michael@0 | 94 | i = descriptors_.begin(); i != descriptors_.end(); ++i) { |
michael@0 | 95 | *(buffer++) = i->fd; |
michael@0 | 96 | } |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | void FileDescriptorSet::CommitAll() { |
michael@0 | 100 | for (std::vector<base::FileDescriptor>::iterator |
michael@0 | 101 | i = descriptors_.begin(); i != descriptors_.end(); ++i) { |
michael@0 | 102 | if (i->auto_close) |
michael@0 | 103 | HANDLE_EINTR(close(i->fd)); |
michael@0 | 104 | } |
michael@0 | 105 | descriptors_.clear(); |
michael@0 | 106 | consumed_descriptor_highwater_ = 0; |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | void FileDescriptorSet::SetDescriptors(const int* buffer, unsigned count) { |
michael@0 | 110 | DCHECK_LE(count, MAX_DESCRIPTORS_PER_MESSAGE); |
michael@0 | 111 | DCHECK_EQ(descriptors_.size(), 0u); |
michael@0 | 112 | DCHECK_EQ(consumed_descriptor_highwater_, 0u); |
michael@0 | 113 | |
michael@0 | 114 | descriptors_.reserve(count); |
michael@0 | 115 | for (unsigned i = 0; i < count; ++i) { |
michael@0 | 116 | struct base::FileDescriptor sd; |
michael@0 | 117 | sd.fd = buffer[i]; |
michael@0 | 118 | sd.auto_close = true; |
michael@0 | 119 | descriptors_.push_back(sd); |
michael@0 | 120 | } |
michael@0 | 121 | } |