blob: dd881c5843f1090c210134ab3821c20e85da75bf [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
morrita98b7aaa2015-01-26 22:42:547#include <algorithm>
[email protected]a17b7ca62009-02-12 18:09:118#include "base/logging.h"
[email protected]2025d002012-11-14 20:54:359#include "base/posix/eintr_wrapper.h"
morrita98b7aaa2015-01-26 22:42:5410#include "ipc/ipc_message_attachment.h"
[email protected]a17b7ca62009-02-12 18:09:1111
morrita4b5c28e22015-01-14 21:17:0612#if defined(OS_POSIX)
13#include <sys/types.h>
14#include <sys/stat.h>
15#include <unistd.h>
morrita1aa788c2015-01-31 05:45:4216#include "ipc/ipc_platform_file_attachment_posix.h"
morrita4b5c28e22015-01-14 21:17:0617#endif // OS_POSIX
18
19namespace IPC {
20
21MessageAttachmentSet::MessageAttachmentSet()
[email protected]a17b7ca62009-02-12 18:09:1122 : consumed_descriptor_highwater_(0) {
23}
24
morrita4b5c28e22015-01-14 21:17:0625MessageAttachmentSet::~MessageAttachmentSet() {
morrita96693852014-09-24 20:11:4526 if (consumed_descriptor_highwater_ == size())
[email protected]a17b7ca62009-02-12 18:09:1127 return;
28
morrita96693852014-09-24 20:11:4529 // We close all the owning descriptors. If this message should have
30 // been transmitted, then closing those with close flags set mirrors
31 // the expected behaviour.
[email protected]a17b7ca62009-02-12 18:09:1132 //
33 // If this message was received with more descriptors than expected
34 // (which could a DOS against the browser by a rogue renderer) then all
35 // the descriptors have their close flag set and we free all the extra
36 // kernel resources.
morrita4b5c28e22015-01-14 21:17:0637 LOG(WARNING) << "MessageAttachmentSet destroyed with unconsumed descriptors: "
morrita96693852014-09-24 20:11:4538 << consumed_descriptor_highwater_ << "/" << size();
[email protected]a17b7ca62009-02-12 18:09:1139}
40
morrita98b7aaa2015-01-26 22:42:5441unsigned MessageAttachmentSet::num_descriptors() const {
42 return std::count_if(attachments_.begin(), attachments_.end(),
43 [](scoped_refptr<MessageAttachment> i) {
44 return i->GetType() == MessageAttachment::TYPE_PLATFORM_FILE;
45 });
46}
47
morrita4b5c28e22015-01-14 21:17:0648unsigned MessageAttachmentSet::size() const {
morrita98b7aaa2015-01-26 22:42:5449 return static_cast<unsigned>(attachments_.size());
morrita4b5c28e22015-01-14 21:17:0650}
51
morrita1aa788c2015-01-31 05:45:4252bool MessageAttachmentSet::AddAttachment(
morrita98b7aaa2015-01-26 22:42:5453 scoped_refptr<MessageAttachment> attachment) {
morrita1aa788c2015-01-31 05:45:4254#if defined(OS_POSIX)
55 if (attachment->GetType() != MessageAttachment::TYPE_PLATFORM_FILE ||
56 num_descriptors() == kMaxDescriptorsPerMessage) {
57 DLOG(WARNING) << "Cannot add file descriptor. MessageAttachmentSet full.";
58 return false;
59 }
60#endif
61
morrita98b7aaa2015-01-26 22:42:5462 attachments_.push_back(attachment);
morrita1aa788c2015-01-31 05:45:4263 return true;
[email protected]a17b7ca62009-02-12 18:09:1164}
65
morrita98b7aaa2015-01-26 22:42:5466scoped_refptr<MessageAttachment> MessageAttachmentSet::GetAttachmentAt(
67 unsigned index) {
morrita96693852014-09-24 20:11:4568 if (index >= size()) {
morrita4b5c28e22015-01-14 21:17:0669 DLOG(WARNING) << "Accessing out of bound index:" << index << "/" << size();
morrita98b7aaa2015-01-26 22:42:5470 return scoped_refptr<MessageAttachment>();
morrita96693852014-09-24 20:11:4571 }
72
[email protected]a17b7ca62009-02-12 18:09:1173 // We should always walk the descriptors in order, so it's reasonable to
74 // enforce this. Consider the case where a compromised renderer sends us
75 // the following message:
76 //
77 // ExampleMsg:
78 // num_fds:2 msg:FD(index = 1) control:SCM_RIGHTS {n, m}
79 //
80 // Here the renderer sent us a message which should have a descriptor, but
81 // actually sent two in an attempt to fill our fd table and kill us. By
82 // setting the index of the descriptor in the message to 1 (it should be
83 // 0), we would record a highwater of 1 and then consider all the
84 // descriptors to have been used.
85 //
86 // So we can either track of the use of each descriptor in a bitset, or we
87 // can enforce that we walk the indexes strictly in order.
88 //
89 // There's one more wrinkle: When logging messages, we may reparse them. So
90 // we have an exception: When the consumed_descriptor_highwater_ is at the
91 // end of the array and index 0 is requested, we reset the highwater value.
morrita96693852014-09-24 20:11:4592 // TODO(morrita): This is absurd. This "wringle" disallow to introduce clearer
93 // ownership model. Only client is NaclIPCAdapter. See crbug.com/415294
morrita98b7aaa2015-01-26 22:42:5494 if (index == 0 && consumed_descriptor_highwater_ == size())
[email protected]a17b7ca62009-02-12 18:09:1195 consumed_descriptor_highwater_ = 0;
96
97 if (index != consumed_descriptor_highwater_)
morrita98b7aaa2015-01-26 22:42:5498 return scoped_refptr<MessageAttachment>();
[email protected]a17b7ca62009-02-12 18:09:1199
100 consumed_descriptor_highwater_ = index + 1;
morrita96693852014-09-24 20:11:45101
morrita98b7aaa2015-01-26 22:42:54102 return attachments_[index];
103}
morrita96693852014-09-24 20:11:45104
morrita98b7aaa2015-01-26 22:42:54105#if defined(OS_POSIX)
106
morrita4b5c28e22015-01-14 21:17:06107void MessageAttachmentSet::PeekDescriptors(base::PlatformFile* buffer) const {
morrita98b7aaa2015-01-26 22:42:54108 for (size_t i = 0; i != attachments_.size(); ++i)
109 buffer[i] = internal::GetPlatformFile(attachments_[i]);
[email protected]a17b7ca62009-02-12 18:09:11110}
111
morrita4b5c28e22015-01-14 21:17:06112bool MessageAttachmentSet::ContainsDirectoryDescriptor() const {
[email protected]aac449e2010-06-10 21:39:04113 struct stat st;
114
morrita98b7aaa2015-01-26 22:42:54115 for (auto i = attachments_.begin(); i != attachments_.end(); ++i) {
116 if (fstat(internal::GetPlatformFile(*i), &st) == 0 && S_ISDIR(st.st_mode))
[email protected]aac449e2010-06-10 21:39:04117 return true;
118 }
119
120 return false;
121}
122
morrita4b5c28e22015-01-14 21:17:06123void MessageAttachmentSet::CommitAll() {
morrita98b7aaa2015-01-26 22:42:54124 attachments_.clear();
[email protected]a17b7ca62009-02-12 18:09:11125 consumed_descriptor_highwater_ = 0;
126}
127
morrita4b5c28e22015-01-14 21:17:06128void MessageAttachmentSet::ReleaseFDsToClose(
morrita96693852014-09-24 20:11:45129 std::vector<base::PlatformFile>* fds) {
morrita1aa788c2015-01-31 05:45:42130 for (size_t i = 0; i < attachments_.size(); ++i) {
131 internal::PlatformFileAttachment* file =
132 static_cast<internal::PlatformFileAttachment*>(attachments_[i].get());
133 if (file->Owns())
134 fds->push_back(file->TakePlatformFile());
[email protected]dc875dc2013-10-15 00:07:00135 }
morrita96693852014-09-24 20:11:45136
137 CommitAll();
[email protected]dc875dc2013-10-15 00:07:00138}
139
morrita4b5c28e22015-01-14 21:17:06140void MessageAttachmentSet::AddDescriptorsToOwn(const base::PlatformFile* buffer,
141 unsigned count) {
[email protected]05094a32011-09-01 00:50:13142 DCHECK(count <= kMaxDescriptorsPerMessage);
morrita98b7aaa2015-01-26 22:42:54143 DCHECK_EQ(num_descriptors(), 0u);
[email protected]a17b7ca62009-02-12 18:09:11144 DCHECK_EQ(consumed_descriptor_highwater_, 0u);
145
morrita98b7aaa2015-01-26 22:42:54146 attachments_.reserve(count);
morrita1aa788c2015-01-31 05:45:42147 for (unsigned i = 0; i < count; ++i)
148 AddAttachment(
149 new internal::PlatformFileAttachment(base::ScopedFD(buffer[i])));
[email protected]a17b7ca62009-02-12 18:09:11150}
morrita4b5c28e22015-01-14 21:17:06151
152#endif // OS_POSIX
153
154} // namespace IPC
155
156