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