blob: 7f17322de47b2f46095604b3be6763db0d71ee96 [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
[email protected]946d1b22009-07-22 23:57:215#include "ipc/file_descriptor_set_posix.h"
[email protected]a17b7ca62009-02-12 18:09:116
[email protected]aac449e2010-06-10 21:39:047#include <sys/types.h>
8#include <sys/stat.h>
9
[email protected]157c61b2009-05-01 21:37:3110#include "base/eintr_wrapper.h"
[email protected]a17b7ca62009-02-12 18:09:1111#include "base/logging.h"
12
[email protected]d0c0b1d2009-02-12 18:32:4513FileDescriptorSet::FileDescriptorSet()
[email protected]a17b7ca62009-02-12 18:09:1114 : consumed_descriptor_highwater_(0) {
15}
16
[email protected]d0c0b1d2009-02-12 18:32:4517FileDescriptorSet::~FileDescriptorSet() {
[email protected]a17b7ca62009-02-12 18:09:1118 if (consumed_descriptor_highwater_ == descriptors_.size())
19 return;
20
[email protected]d0c0b1d2009-02-12 18:32:4521 LOG(WARNING) << "FileDescriptorSet destroyed with unconsumed descriptors";
[email protected]a17b7ca62009-02-12 18:09:1122 // We close all the descriptors where the close flag is set. If this
23 // message should have been transmitted, then closing those with close
24 // flags set mirrors the expected behaviour.
25 //
26 // If this message was received with more descriptors than expected
27 // (which could a DOS against the browser by a rogue renderer) then all
28 // the descriptors have their close flag set and we free all the extra
29 // kernel resources.
30 for (unsigned i = consumed_descriptor_highwater_;
31 i < descriptors_.size(); ++i) {
32 if (descriptors_[i].auto_close)
[email protected]70eb6572010-06-23 00:37:4633 if (HANDLE_EINTR(close(descriptors_[i].fd)) < 0)
34 PLOG(ERROR) << "close";
[email protected]a17b7ca62009-02-12 18:09:1135 }
36}
37
[email protected]d0c0b1d2009-02-12 18:32:4538bool FileDescriptorSet::Add(int fd) {
[email protected]a17b7ca62009-02-12 18:09:1139 if (descriptors_.size() == MAX_DESCRIPTORS_PER_MESSAGE)
40 return false;
41
42 struct base::FileDescriptor sd;
43 sd.fd = fd;
44 sd.auto_close = false;
45 descriptors_.push_back(sd);
46 return true;
47}
48
[email protected]d0c0b1d2009-02-12 18:32:4549bool FileDescriptorSet::AddAndAutoClose(int fd) {
[email protected]a17b7ca62009-02-12 18:09:1150 if (descriptors_.size() == MAX_DESCRIPTORS_PER_MESSAGE)
51 return false;
52
53 struct base::FileDescriptor sd;
54 sd.fd = fd;
55 sd.auto_close = true;
56 descriptors_.push_back(sd);
57 DCHECK(descriptors_.size() <= MAX_DESCRIPTORS_PER_MESSAGE);
58 return true;
59}
60
[email protected]d0c0b1d2009-02-12 18:32:4561int FileDescriptorSet::GetDescriptorAt(unsigned index) const {
[email protected]a17b7ca62009-02-12 18:09:1162 if (index >= descriptors_.size())
63 return -1;
64
65 // We should always walk the descriptors in order, so it's reasonable to
66 // enforce this. Consider the case where a compromised renderer sends us
67 // the following message:
68 //
69 // ExampleMsg:
70 // num_fds:2 msg:FD(index = 1) control:SCM_RIGHTS {n, m}
71 //
72 // Here the renderer sent us a message which should have a descriptor, but
73 // actually sent two in an attempt to fill our fd table and kill us. By
74 // setting the index of the descriptor in the message to 1 (it should be
75 // 0), we would record a highwater of 1 and then consider all the
76 // descriptors to have been used.
77 //
78 // So we can either track of the use of each descriptor in a bitset, or we
79 // can enforce that we walk the indexes strictly in order.
80 //
81 // There's one more wrinkle: When logging messages, we may reparse them. So
82 // we have an exception: When the consumed_descriptor_highwater_ is at the
83 // end of the array and index 0 is requested, we reset the highwater value.
84 if (index == 0 && consumed_descriptor_highwater_ == descriptors_.size())
85 consumed_descriptor_highwater_ = 0;
86
87 if (index != consumed_descriptor_highwater_)
88 return -1;
89
90 consumed_descriptor_highwater_ = index + 1;
91 return descriptors_[index].fd;
92}
93
[email protected]d0c0b1d2009-02-12 18:32:4594void FileDescriptorSet::GetDescriptors(int* buffer) const {
[email protected]a17b7ca62009-02-12 18:09:1195 for (std::vector<base::FileDescriptor>::const_iterator
96 i = descriptors_.begin(); i != descriptors_.end(); ++i) {
97 *(buffer++) = i->fd;
98 }
99}
100
[email protected]aac449e2010-06-10 21:39:04101bool FileDescriptorSet::ContainsDirectoryDescriptor() const {
102 struct stat st;
103
104 for (std::vector<base::FileDescriptor>::const_iterator
105 i = descriptors_.begin(); i != descriptors_.end(); ++i) {
106 if (fstat(i->fd, &st) == 0 && S_ISDIR(st.st_mode))
107 return true;
108 }
109
110 return false;
111}
112
[email protected]d0c0b1d2009-02-12 18:32:45113void FileDescriptorSet::CommitAll() {
[email protected]a17b7ca62009-02-12 18:09:11114 for (std::vector<base::FileDescriptor>::iterator
115 i = descriptors_.begin(); i != descriptors_.end(); ++i) {
116 if (i->auto_close)
[email protected]70eb6572010-06-23 00:37:46117 if (HANDLE_EINTR(close(i->fd)) < 0)
118 PLOG(ERROR) << "close";
[email protected]a17b7ca62009-02-12 18:09:11119 }
120 descriptors_.clear();
121 consumed_descriptor_highwater_ = 0;
122}
123
[email protected]d0c0b1d2009-02-12 18:32:45124void FileDescriptorSet::SetDescriptors(const int* buffer, unsigned count) {
[email protected]a17b7ca62009-02-12 18:09:11125 DCHECK_LE(count, MAX_DESCRIPTORS_PER_MESSAGE);
126 DCHECK_EQ(descriptors_.size(), 0u);
127 DCHECK_EQ(consumed_descriptor_highwater_, 0u);
128
129 descriptors_.reserve(count);
130 for (unsigned i = 0; i < count; ++i) {
131 struct base::FileDescriptor sd;
132 sd.fd = buffer[i];
133 sd.auto_close = true;
134 descriptors_.push_back(sd);
135 }
136}