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/ipc_message_attachment.h"
13 #include <sys/types.h>
16 #include "ipc/ipc_platform_file_attachment_posix.h"
21 MessageAttachmentSet::MessageAttachmentSet()
22 : consumed_descriptor_highwater_(0) {
25 MessageAttachmentSet::~MessageAttachmentSet() {
26 if (consumed_descriptor_highwater_
== size())
29 // 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.
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
37 LOG(WARNING
) << "MessageAttachmentSet destroyed with unconsumed descriptors: "
38 << consumed_descriptor_highwater_
<< "/" << size();
41 unsigned 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
;
48 unsigned MessageAttachmentSet::size() const {
49 return static_cast<unsigned>(attachments_
.size());
52 bool MessageAttachmentSet::AddAttachment(
53 scoped_refptr
<MessageAttachment
> attachment
) {
55 if (attachment
->GetType() != MessageAttachment::TYPE_PLATFORM_FILE
||
56 num_descriptors() == kMaxDescriptorsPerMessage
) {
57 DLOG(WARNING
) << "Cannot add file descriptor. MessageAttachmentSet full.";
62 attachments_
.push_back(attachment
);
66 scoped_refptr
<MessageAttachment
> MessageAttachmentSet::GetAttachmentAt(
68 if (index
>= size()) {
69 DLOG(WARNING
) << "Accessing out of bound index:" << index
<< "/" << size();
70 return scoped_refptr
<MessageAttachment
>();
73 // 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:
78 // num_fds:2 msg:FD(index = 1) control:SCM_RIGHTS {n, m}
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.
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.
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.
92 // TODO(morrita): This is absurd. This "wringle" disallow to introduce clearer
93 // ownership model. Only client is NaclIPCAdapter. See crbug.com/415294
94 if (index
== 0 && consumed_descriptor_highwater_
== size())
95 consumed_descriptor_highwater_
= 0;
97 if (index
!= consumed_descriptor_highwater_
)
98 return scoped_refptr
<MessageAttachment
>();
100 consumed_descriptor_highwater_
= index
+ 1;
102 return attachments_
[index
];
105 #if defined(OS_POSIX)
107 void MessageAttachmentSet::PeekDescriptors(base::PlatformFile
* buffer
) const {
108 for (size_t i
= 0; i
!= attachments_
.size(); ++i
)
109 buffer
[i
] = internal::GetPlatformFile(attachments_
[i
]);
112 bool MessageAttachmentSet::ContainsDirectoryDescriptor() const {
115 for (auto i
= attachments_
.begin(); i
!= attachments_
.end(); ++i
) {
116 if (fstat(internal::GetPlatformFile(*i
), &st
) == 0 && S_ISDIR(st
.st_mode
))
123 void MessageAttachmentSet::CommitAll() {
124 attachments_
.clear();
125 consumed_descriptor_highwater_
= 0;
128 void MessageAttachmentSet::ReleaseFDsToClose(
129 std::vector
<base::PlatformFile
>* fds
) {
130 for (size_t i
= 0; i
< attachments_
.size(); ++i
) {
131 internal::PlatformFileAttachment
* file
=
132 static_cast<internal::PlatformFileAttachment
*>(attachments_
[i
].get());
134 fds
->push_back(file
->TakePlatformFile());
140 void MessageAttachmentSet::AddDescriptorsToOwn(const base::PlatformFile
* buffer
,
142 DCHECK(count
<= kMaxDescriptorsPerMessage
);
143 DCHECK_EQ(num_descriptors(), 0u);
144 DCHECK_EQ(consumed_descriptor_highwater_
, 0u);
146 attachments_
.reserve(count
);
147 for (unsigned i
= 0; i
< count
; ++i
)
149 new internal::PlatformFileAttachment(base::ScopedFD(buffer
[i
])));