Avi Drissman | ea1be23 | 2022-09-14 23:29:06 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 5 | #include "ipc/ipc_message_attachment_set.h" |
[email protected] | aac449e | 2010-06-10 21:39:04 | [diff] [blame] | 6 | |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 9 | #include <algorithm> |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 10 | |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 11 | #include "base/logging.h" |
[email protected] | 2025d00 | 2012-11-14 20:54:35 | [diff] [blame] | 12 | #include "base/posix/eintr_wrapper.h" |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 13 | #include "build/build_config.h" |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 14 | #include "ipc/ipc_message_attachment.h" |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 15 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 16 | namespace IPC { |
| 17 | |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 18 | namespace { |
| 19 | |
| 20 | unsigned 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 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 33 | MessageAttachmentSet::MessageAttachmentSet() |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 34 | : consumed_descriptor_highwater_(0) { |
| 35 | } |
| 36 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 37 | MessageAttachmentSet::~MessageAttachmentSet() { |
sammc | 6ed3efb | 2016-11-23 03:17:35 | [diff] [blame] | 38 | if (consumed_descriptor_highwater_ == size()) |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 39 | return; |
| 40 | |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 41 | // 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] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 44 | // |
| 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. |
sammc | 6ed3efb | 2016-11-23 03:17:35 | [diff] [blame] | 49 | LOG(WARNING) << "MessageAttachmentSet destroyed with unconsumed attachments: " |
| 50 | << consumed_descriptor_highwater_ << "/" << size(); |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 51 | } |
| 52 | |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 53 | unsigned MessageAttachmentSet::num_descriptors() const { |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 54 | return count_attachments_of_type(attachments_, |
sammc | 6ed3efb | 2016-11-23 03:17:35 | [diff] [blame] | 55 | MessageAttachment::Type::PLATFORM_FILE); |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 56 | } |
| 57 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 58 | unsigned MessageAttachmentSet::size() const { |
sammc | 6ed3efb | 2016-11-23 03:17:35 | [diff] [blame] | 59 | return static_cast<unsigned>(attachments_.size()); |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 60 | } |
| 61 | |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 62 | bool MessageAttachmentSet::AddAttachment( |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 63 | scoped_refptr<MessageAttachment> attachment, |
sammc | 6ed3efb | 2016-11-23 03:17:35 | [diff] [blame] | 64 | size_t* index) { |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 65 | #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
sammc | 6ed3efb | 2016-11-23 03:17:35 | [diff] [blame] | 66 | if (attachment->GetType() == MessageAttachment::Type::PLATFORM_FILE && |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 67 | num_descriptors() == kMaxDescriptorsPerMessage) { |
| 68 | DLOG(WARNING) << "Cannot add file descriptor. MessageAttachmentSet full."; |
| 69 | return false; |
| 70 | } |
| 71 | #endif |
| 72 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 73 | switch (attachment->GetType()) { |
sammc | 6ed3efb | 2016-11-23 03:17:35 | [diff] [blame] | 74 | case MessageAttachment::Type::PLATFORM_FILE: |
| 75 | case MessageAttachment::Type::MOJO_HANDLE: |
| 76 | case MessageAttachment::Type::WIN_HANDLE: |
| 77 | case MessageAttachment::Type::MACH_PORT: |
Scott Graham | 3eebff0 | 2017-06-30 01:07:10 | [diff] [blame] | 78 | case MessageAttachment::Type::FUCHSIA_HANDLE: |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 79 | attachments_.push_back(attachment); |
| 80 | *index = attachments_.size() - 1; |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 81 | return true; |
| 82 | } |
| 83 | return false; |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 84 | } |
| 85 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 86 | bool MessageAttachmentSet::AddAttachment( |
| 87 | scoped_refptr<MessageAttachment> attachment) { |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 88 | size_t index; |
sammc | 6ed3efb | 2016-11-23 03:17:35 | [diff] [blame] | 89 | return AddAttachment(attachment, &index); |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 90 | } |
| 91 | |
sammc | 6ed3efb | 2016-11-23 03:17:35 | [diff] [blame] | 92 | scoped_refptr<MessageAttachment> MessageAttachmentSet::GetAttachmentAt( |
| 93 | unsigned index) { |
| 94 | if (index >= size()) { |
| 95 | DLOG(WARNING) << "Accessing out of bound index:" << index << "/" << size(); |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 96 | return scoped_refptr<MessageAttachment>(); |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 97 | } |
| 98 | |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 99 | // 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. |
sammc | 6ed3efb | 2016-11-23 03:17:35 | [diff] [blame] | 114 | if (index == 0 && consumed_descriptor_highwater_ == size()) { |
Alexandr Ilin | d4c1d1d6 | 2018-04-13 08:10:20 | [diff] [blame] | 115 | DLOG(WARNING) << "Attempted to double-read a message attachment, " |
| 116 | "returning a nullptr"; |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 117 | } |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 118 | |
| 119 | if (index != consumed_descriptor_highwater_) |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 120 | return scoped_refptr<MessageAttachment>(); |
[email protected] | a17b7ca6 | 2009-02-12 18:09:11 | [diff] [blame] | 121 | |
| 122 | consumed_descriptor_highwater_ = index + 1; |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 123 | |
morrita | 98b7aaa | 2015-01-26 22:42:54 | [diff] [blame] | 124 | return attachments_[index]; |
| 125 | } |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 126 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 127 | void MessageAttachmentSet::CommitAllDescriptors() { |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 128 | attachments_.clear(); |
| 129 | consumed_descriptor_highwater_ = 0; |
| 130 | } |
| 131 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 132 | } // namespace IPC |