1 // Copyright (c) 2011 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.
5 #include "ipc/ipc_message_attachment_set.h"
8 #include "base/logging.h"
9 #include "base/posix/eintr_wrapper.h"
10 #include "ipc/brokerable_attachment.h"
11 #include "ipc/ipc_message_attachment.h"
14 #include <sys/types.h>
17 #include "ipc/ipc_platform_file_attachment_posix.h"
24 unsigned count_attachments_of_type(
25 const std::vector
<scoped_refptr
<MessageAttachment
>>& attachments
,
26 MessageAttachment::Type type
) {
28 for (const scoped_refptr
<MessageAttachment
>& attachment
: attachments
) {
29 if (attachment
->GetType() == type
)
37 MessageAttachmentSet::MessageAttachmentSet()
38 : consumed_descriptor_highwater_(0) {
41 MessageAttachmentSet::~MessageAttachmentSet() {
42 if (consumed_descriptor_highwater_
== size())
45 // We close all the owning descriptors. If this message should have
46 // been transmitted, then closing those with close flags set mirrors
47 // the expected behaviour.
49 // If this message was received with more descriptors than expected
50 // (which could a DOS against the browser by a rogue renderer) then all
51 // the descriptors have their close flag set and we free all the extra
53 LOG(WARNING
) << "MessageAttachmentSet destroyed with unconsumed descriptors: "
54 << consumed_descriptor_highwater_
<< "/" << size();
57 unsigned MessageAttachmentSet::num_descriptors() const {
58 return count_attachments_of_type(attachments_
,
59 MessageAttachment::TYPE_PLATFORM_FILE
);
62 unsigned MessageAttachmentSet::num_mojo_handles() const {
63 return count_attachments_of_type(attachments_
,
64 MessageAttachment::TYPE_MOJO_HANDLE
);
67 unsigned MessageAttachmentSet::num_brokerable_attachments() const {
68 return count_attachments_of_type(
69 attachments_
, MessageAttachment::TYPE_BROKERABLE_ATTACHMENT
);
72 unsigned MessageAttachmentSet::size() const {
73 return static_cast<unsigned>(attachments_
.size());
76 bool MessageAttachmentSet::AddAttachment(
77 scoped_refptr
<MessageAttachment
> attachment
) {
79 if (attachment
->GetType() == MessageAttachment::TYPE_PLATFORM_FILE
&&
80 num_descriptors() == kMaxDescriptorsPerMessage
) {
81 DLOG(WARNING
) << "Cannot add file descriptor. MessageAttachmentSet full.";
86 attachments_
.push_back(attachment
);
90 scoped_refptr
<MessageAttachment
> MessageAttachmentSet::GetAttachmentAt(
92 if (index
>= size()) {
93 DLOG(WARNING
) << "Accessing out of bound index:" << index
<< "/" << size();
94 return scoped_refptr
<MessageAttachment
>();
97 // We should always walk the descriptors in order, so it's reasonable to
98 // enforce this. Consider the case where a compromised renderer sends us
99 // the following message:
102 // num_fds:2 msg:FD(index = 1) control:SCM_RIGHTS {n, m}
104 // Here the renderer sent us a message which should have a descriptor, but
105 // actually sent two in an attempt to fill our fd table and kill us. By
106 // setting the index of the descriptor in the message to 1 (it should be
107 // 0), we would record a highwater of 1 and then consider all the
108 // descriptors to have been used.
110 // So we can either track of the use of each descriptor in a bitset, or we
111 // can enforce that we walk the indexes strictly in order.
113 // There's one more wrinkle: When logging messages, we may reparse them. So
114 // we have an exception: When the consumed_descriptor_highwater_ is at the
115 // end of the array and index 0 is requested, we reset the highwater value.
116 // TODO(morrita): This is absurd. This "wringle" disallow to introduce clearer
117 // ownership model. Only client is NaclIPCAdapter. See crbug.com/415294
118 if (index
== 0 && consumed_descriptor_highwater_
== size())
119 consumed_descriptor_highwater_
= 0;
121 if (index
!= consumed_descriptor_highwater_
)
122 return scoped_refptr
<MessageAttachment
>();
124 consumed_descriptor_highwater_
= index
+ 1;
126 return attachments_
[index
];
129 void MessageAttachmentSet::CommitAll() {
130 attachments_
.clear();
131 consumed_descriptor_highwater_
= 0;
134 std::vector
<const BrokerableAttachment
*>
135 MessageAttachmentSet::PeekBrokerableAttachments() const {
136 std::vector
<const BrokerableAttachment
*> output
;
137 for (const scoped_refptr
<MessageAttachment
>& attachment
: attachments_
) {
138 if (attachment
->GetType() ==
139 MessageAttachment::TYPE_BROKERABLE_ATTACHMENT
) {
140 output
.push_back(static_cast<BrokerableAttachment
*>(attachment
.get()));
146 #if defined(OS_POSIX)
148 void MessageAttachmentSet::PeekDescriptors(base::PlatformFile
* buffer
) const {
149 for (size_t i
= 0; i
!= attachments_
.size(); ++i
)
150 buffer
[i
] = internal::GetPlatformFile(attachments_
[i
]);
153 bool MessageAttachmentSet::ContainsDirectoryDescriptor() const {
156 for (auto i
= attachments_
.begin(); i
!= attachments_
.end(); ++i
) {
157 if (fstat(internal::GetPlatformFile(*i
), &st
) == 0 && S_ISDIR(st
.st_mode
))
164 void MessageAttachmentSet::ReleaseFDsToClose(
165 std::vector
<base::PlatformFile
>* fds
) {
166 for (size_t i
= 0; i
< attachments_
.size(); ++i
) {
167 internal::PlatformFileAttachment
* file
=
168 static_cast<internal::PlatformFileAttachment
*>(attachments_
[i
].get());
170 fds
->push_back(file
->TakePlatformFile());
176 void MessageAttachmentSet::AddDescriptorsToOwn(const base::PlatformFile
* buffer
,
178 DCHECK(count
<= kMaxDescriptorsPerMessage
);
179 DCHECK_EQ(num_descriptors(), 0u);
180 DCHECK_EQ(consumed_descriptor_highwater_
, 0u);
182 attachments_
.reserve(count
);
183 for (unsigned i
= 0; i
< count
; ++i
)
185 new internal::PlatformFileAttachment(base::ScopedFD(buffer
[i
])));