blob: ecc54f98ba234cc0815720c88784af7739804772 [file] [log] [blame]
Avi Drissmanea1be232022-09-14 23:29:061// Copyright 2011 The Chromium Authors
[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
avi246998d82015-12-22 02:39:047#include <stddef.h>
8
morrita98b7aaa2015-01-26 22:42:549#include <algorithm>
avi246998d82015-12-22 02:39:0410
[email protected]a17b7ca62009-02-12 18:09:1111#include "base/logging.h"
[email protected]2025d002012-11-14 20:54:3512#include "base/posix/eintr_wrapper.h"
avi246998d82015-12-22 02:39:0413#include "build/build_config.h"
morrita98b7aaa2015-01-26 22:42:5414#include "ipc/ipc_message_attachment.h"
[email protected]a17b7ca62009-02-12 18:09:1115
morrita4b5c28e22015-01-14 21:17:0616namespace IPC {
17
erikcheneece6c32015-07-07 22:13:1118namespace {
19
20unsigned count_attachments_of_type(
21 const std::vector<scoped_refptr<MessageAttachment>>& attachments,
22 MessageAttachment::Type type) {
23 unsigned count = 0;
24 for (const scoped_refptr<MessageAttachment>& attachment : attachments) {
25 if (attachment->GetType() == type)
26 ++count;
27 }
28 return count;
29}
30
31} // namespace
32
morrita4b5c28e22015-01-14 21:17:0633MessageAttachmentSet::MessageAttachmentSet()
[email protected]a17b7ca62009-02-12 18:09:1134 : consumed_descriptor_highwater_(0) {
35}
36
morrita4b5c28e22015-01-14 21:17:0637MessageAttachmentSet::~MessageAttachmentSet() {
sammc6ed3efb2016-11-23 03:17:3538 if (consumed_descriptor_highwater_ == size())
[email protected]a17b7ca62009-02-12 18:09:1139 return;
40
morrita96693852014-09-24 20:11:4541 // We close all the owning descriptors. If this message should have
42 // been transmitted, then closing those with close flags set mirrors
43 // the expected behaviour.
[email protected]a17b7ca62009-02-12 18:09:1144 //
45 // If this message was received with more descriptors than expected
46 // (which could a DOS against the browser by a rogue renderer) then all
47 // the descriptors have their close flag set and we free all the extra
48 // kernel resources.
sammc6ed3efb2016-11-23 03:17:3549 LOG(WARNING) << "MessageAttachmentSet destroyed with unconsumed attachments: "
50 << consumed_descriptor_highwater_ << "/" << size();
[email protected]a17b7ca62009-02-12 18:09:1151}
52
morrita98b7aaa2015-01-26 22:42:5453unsigned MessageAttachmentSet::num_descriptors() const {
erikcheneece6c32015-07-07 22:13:1154 return count_attachments_of_type(attachments_,
sammc6ed3efb2016-11-23 03:17:3555 MessageAttachment::Type::PLATFORM_FILE);
morrita4b5c28e22015-01-14 21:17:0656}
57
erikchenae6d3212015-10-10 02:43:4958unsigned MessageAttachmentSet::size() const {
sammc6ed3efb2016-11-23 03:17:3559 return static_cast<unsigned>(attachments_.size());
erikchenae6d3212015-10-10 02:43:4960}
61
morrita1aa788c2015-01-31 05:45:4262bool MessageAttachmentSet::AddAttachment(
erikchenae6d3212015-10-10 02:43:4963 scoped_refptr<MessageAttachment> attachment,
sammc6ed3efb2016-11-23 03:17:3564 size_t* index) {
Xiaohan Wangab909b32022-01-12 17:57:3965#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
sammc6ed3efb2016-11-23 03:17:3566 if (attachment->GetType() == MessageAttachment::Type::PLATFORM_FILE &&
morrita1aa788c2015-01-31 05:45:4267 num_descriptors() == kMaxDescriptorsPerMessage) {
68 DLOG(WARNING) << "Cannot add file descriptor. MessageAttachmentSet full.";
69 return false;
70 }
71#endif
72
erikchenae6d3212015-10-10 02:43:4973 switch (attachment->GetType()) {
sammc6ed3efb2016-11-23 03:17:3574 case MessageAttachment::Type::PLATFORM_FILE:
75 case MessageAttachment::Type::MOJO_HANDLE:
76 case MessageAttachment::Type::WIN_HANDLE:
77 case MessageAttachment::Type::MACH_PORT:
Scott Graham3eebff02017-06-30 01:07:1078 case MessageAttachment::Type::FUCHSIA_HANDLE:
erikchenae6d3212015-10-10 02:43:4979 attachments_.push_back(attachment);
80 *index = attachments_.size() - 1;
erikchenae6d3212015-10-10 02:43:4981 return true;
82 }
83 return false;
[email protected]a17b7ca62009-02-12 18:09:1184}
85
erikchenae6d3212015-10-10 02:43:4986bool MessageAttachmentSet::AddAttachment(
87 scoped_refptr<MessageAttachment> attachment) {
erikchenae6d3212015-10-10 02:43:4988 size_t index;
sammc6ed3efb2016-11-23 03:17:3589 return AddAttachment(attachment, &index);
erikchenae6d3212015-10-10 02:43:4990}
91
sammc6ed3efb2016-11-23 03:17:3592scoped_refptr<MessageAttachment> MessageAttachmentSet::GetAttachmentAt(
93 unsigned index) {
94 if (index >= size()) {
95 DLOG(WARNING) << "Accessing out of bound index:" << index << "/" << size();
morrita98b7aaa2015-01-26 22:42:5496 return scoped_refptr<MessageAttachment>();
morrita96693852014-09-24 20:11:4597 }
98
[email protected]a17b7ca62009-02-12 18:09:1199 // We should always walk the descriptors in order, so it's reasonable to
100 // enforce this. Consider the case where a compromised renderer sends us
101 // the following message:
102 //
103 // ExampleMsg:
104 // num_fds:2 msg:FD(index = 1) control:SCM_RIGHTS {n, m}
105 //
106 // Here the renderer sent us a message which should have a descriptor, but
107 // actually sent two in an attempt to fill our fd table and kill us. By
108 // setting the index of the descriptor in the message to 1 (it should be
109 // 0), we would record a highwater of 1 and then consider all the
110 // descriptors to have been used.
111 //
112 // So we can either track of the use of each descriptor in a bitset, or we
113 // can enforce that we walk the indexes strictly in order.
sammc6ed3efb2016-11-23 03:17:35114 if (index == 0 && consumed_descriptor_highwater_ == size()) {
Alexandr Ilind4c1d1d62018-04-13 08:10:20115 DLOG(WARNING) << "Attempted to double-read a message attachment, "
116 "returning a nullptr";
erikchenae6d3212015-10-10 02:43:49117 }
[email protected]a17b7ca62009-02-12 18:09:11118
119 if (index != consumed_descriptor_highwater_)
morrita98b7aaa2015-01-26 22:42:54120 return scoped_refptr<MessageAttachment>();
[email protected]a17b7ca62009-02-12 18:09:11121
122 consumed_descriptor_highwater_ = index + 1;
morrita96693852014-09-24 20:11:45123
morrita98b7aaa2015-01-26 22:42:54124 return attachments_[index];
125}
morrita96693852014-09-24 20:11:45126
erikchenae6d3212015-10-10 02:43:49127void MessageAttachmentSet::CommitAllDescriptors() {
morrita81b17e02015-02-06 00:58:30128 attachments_.clear();
129 consumed_descriptor_highwater_ = 0;
130}
131
morrita4b5c28e22015-01-14 21:17:06132} // namespace IPC