blob: 665f939355ef42fb49fbf388d4d233bed227284b [file] [log] [blame]
[email protected]05094a32011-09-01 00:50:131// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]a17b7ca62009-02-12 18:09:112// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
morrita4b5c28e22015-01-14 21:17:065#include "ipc/ipc_message_attachment_set.h"
[email protected]aac449e2010-06-10 21:39:046
[email protected]a17b7ca62009-02-12 18:09:117#include "base/logging.h"
[email protected]2025d002012-11-14 20:54:358#include "base/posix/eintr_wrapper.h"
[email protected]a17b7ca62009-02-12 18:09:119
morrita4b5c28e22015-01-14 21:17:0610#if defined(OS_POSIX)
11#include <sys/types.h>
12#include <sys/stat.h>
13#include <unistd.h>
14#endif // OS_POSIX
15
16namespace IPC {
17
18MessageAttachmentSet::MessageAttachmentSet()
[email protected]a17b7ca62009-02-12 18:09:1119 : consumed_descriptor_highwater_(0) {
20}
21
morrita4b5c28e22015-01-14 21:17:0622MessageAttachmentSet::~MessageAttachmentSet() {
morrita96693852014-09-24 20:11:4523 if (consumed_descriptor_highwater_ == size())
[email protected]a17b7ca62009-02-12 18:09:1124 return;
25
morrita96693852014-09-24 20:11:4526 // 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]a17b7ca62009-02-12 18:09:1129 //
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.
morrita4b5c28e22015-01-14 21:17:0634 LOG(WARNING) << "MessageAttachmentSet destroyed with unconsumed descriptors: "
morrita96693852014-09-24 20:11:4535 << consumed_descriptor_highwater_ << "/" << size();
[email protected]a17b7ca62009-02-12 18:09:1136}
37
morrita4b5c28e22015-01-14 21:17:0638unsigned 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
48bool MessageAttachmentSet::AddToBorrow(base::PlatformFile fd) {
morrita96693852014-09-24 20:11:4549 DCHECK_EQ(consumed_descriptor_highwater_, 0u);
50
51 if (size() == kMaxDescriptorsPerMessage) {
morrita4b5c28e22015-01-14 21:17:0652 DLOG(WARNING) << "Cannot add file descriptor. MessageAttachmentSet full.";
[email protected]a17b7ca62009-02-12 18:09:1153 return false;
[email protected]ab25b3342013-08-29 21:15:4454 }
[email protected]a17b7ca62009-02-12 18:09:1155
morrita96693852014-09-24 20:11:4556 descriptors_.push_back(fd);
[email protected]a17b7ca62009-02-12 18:09:1157 return true;
58}
59
morrita4b5c28e22015-01-14 21:17:0660bool MessageAttachmentSet::AddToOwn(base::ScopedFD fd) {
morrita96693852014-09-24 20:11:4561 DCHECK_EQ(consumed_descriptor_highwater_, 0u);
62
63 if (size() == kMaxDescriptorsPerMessage) {
morrita4b5c28e22015-01-14 21:17:0664 DLOG(WARNING) << "Cannot add file descriptor. MessageAttachmentSet full.";
[email protected]a17b7ca62009-02-12 18:09:1165 return false;
[email protected]ab25b3342013-08-29 21:15:4466 }
[email protected]a17b7ca62009-02-12 18:09:1167
morrita96693852014-09-24 20:11:4568 descriptors_.push_back(fd.get());
69 owned_descriptors_.push_back(new base::ScopedFD(fd.Pass()));
70 DCHECK(size() <= kMaxDescriptorsPerMessage);
[email protected]a17b7ca62009-02-12 18:09:1171 return true;
72}
73
morrita4b5c28e22015-01-14 21:17:0674base::PlatformFile MessageAttachmentSet::TakeDescriptorAt(unsigned index) {
morrita96693852014-09-24 20:11:4575 if (index >= size()) {
morrita4b5c28e22015-01-14 21:17:0676 DLOG(WARNING) << "Accessing out of bound index:" << index << "/" << size();
[email protected]a17b7ca62009-02-12 18:09:1177 return -1;
morrita96693852014-09-24 20:11:4578 }
79
[email protected]a17b7ca62009-02-12 18:09:1180 // 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.
morrita96693852014-09-24 20:11:4599 // TODO(morrita): This is absurd. This "wringle" disallow to introduce clearer
100 // ownership model. Only client is NaclIPCAdapter. See crbug.com/415294
[email protected]a17b7ca62009-02-12 18:09:11101 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;
morrita96693852014-09-24 20:11:45108
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();
morrita4b5c28e22015-01-14 21:17:06117 i != owned_descriptors_.end(); ++i) {
morrita96693852014-09-24 20:11:45118 if ((*i)->get() == file) {
119 ignore_result((*i)->release());
120 break;
121 }
122 }
123
124 return file;
[email protected]a17b7ca62009-02-12 18:09:11125}
126
morrita4b5c28e22015-01-14 21:17:06127void MessageAttachmentSet::PeekDescriptors(base::PlatformFile* buffer) const {
morrita96693852014-09-24 20:11:45128 std::copy(descriptors_.begin(), descriptors_.end(), buffer);
[email protected]a17b7ca62009-02-12 18:09:11129}
130
morrita4b5c28e22015-01-14 21:17:06131bool MessageAttachmentSet::ContainsDirectoryDescriptor() const {
[email protected]aac449e2010-06-10 21:39:04132 struct stat st;
133
morrita96693852014-09-24 20:11:45134 for (std::vector<base::PlatformFile>::const_iterator i = descriptors_.begin();
morrita4b5c28e22015-01-14 21:17:06135 i != descriptors_.end(); ++i) {
morrita96693852014-09-24 20:11:45136 if (fstat(*i, &st) == 0 && S_ISDIR(st.st_mode))
[email protected]aac449e2010-06-10 21:39:04137 return true;
138 }
139
140 return false;
141}
142
morrita4b5c28e22015-01-14 21:17:06143void MessageAttachmentSet::CommitAll() {
[email protected]a17b7ca62009-02-12 18:09:11144 descriptors_.clear();
morrita96693852014-09-24 20:11:45145 owned_descriptors_.clear();
[email protected]a17b7ca62009-02-12 18:09:11146 consumed_descriptor_highwater_ = 0;
147}
148
morrita4b5c28e22015-01-14 21:17:06149void MessageAttachmentSet::ReleaseFDsToClose(
morrita96693852014-09-24 20:11:45150 std::vector<base::PlatformFile>* fds) {
151 for (ScopedVector<base::ScopedFD>::iterator i = owned_descriptors_.begin();
morrita4b5c28e22015-01-14 21:17:06152 i != owned_descriptors_.end(); ++i) {
morrita96693852014-09-24 20:11:45153 fds->push_back((*i)->release());
[email protected]dc875dc2013-10-15 00:07:00154 }
morrita96693852014-09-24 20:11:45155
156 CommitAll();
[email protected]dc875dc2013-10-15 00:07:00157}
158
morrita4b5c28e22015-01-14 21:17:06159void MessageAttachmentSet::AddDescriptorsToOwn(const base::PlatformFile* buffer,
160 unsigned count) {
[email protected]05094a32011-09-01 00:50:13161 DCHECK(count <= kMaxDescriptorsPerMessage);
morrita96693852014-09-24 20:11:45162 DCHECK_EQ(size(), 0u);
[email protected]a17b7ca62009-02-12 18:09:11163 DCHECK_EQ(consumed_descriptor_highwater_, 0u);
164
165 descriptors_.reserve(count);
morrita96693852014-09-24 20:11:45166 owned_descriptors_.reserve(count);
[email protected]a17b7ca62009-02-12 18:09:11167 for (unsigned i = 0; i < count; ++i) {
morrita96693852014-09-24 20:11:45168 descriptors_.push_back(buffer[i]);
169 owned_descriptors_.push_back(new base::ScopedFD(buffer[i]));
[email protected]a17b7ca62009-02-12 18:09:11170 }
171}
morrita4b5c28e22015-01-14 21:17:06172
173#endif // OS_POSIX
174
175} // namespace IPC
176
177