Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(103)

Side by Side Diff: content/browser/renderer_host/media/midi_host.cc

Issue 68353002: Use MIDIMessageQueue/IsValidWebMIDIData for MIDI byte stream validation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add IsValidMIDIMessagesForWebMIDI to avoid memory copy Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/renderer_host/media/midi_host.h" 5 #include "content/browser/renderer_host/media/midi_host.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/debug/trace_event.h" 9 #include "base/debug/trace_event.h"
10 #include "base/process/process.h" 10 #include "base/process/process.h"
11 #include "content/browser/browser_main_loop.h" 11 #include "content/browser/browser_main_loop.h"
12 #include "content/browser/child_process_security_policy_impl.h" 12 #include "content/browser/child_process_security_policy_impl.h"
13 #include "content/browser/media/media_internals.h" 13 #include "content/browser/media/media_internals.h"
14 #include "content/common/media/midi_messages.h" 14 #include "content/common/media/midi_messages.h"
15 #include "content/public/browser/content_browser_client.h" 15 #include "content/public/browser/content_browser_client.h"
16 #include "content/public/browser/media_observer.h" 16 #include "content/public/browser/media_observer.h"
17 #include "content/public/browser/user_metrics.h" 17 #include "content/public/browser/user_metrics.h"
18 #include "media/midi/midi_manager.h" 18 #include "media/midi/midi_manager.h"
19 #include "media/midi/midi_message_util.h"
19 20
20 using media::MIDIManager; 21 using media::MIDIManager;
21 using media::MIDIPortInfoList; 22 using media::MIDIPortInfoList;
22 23
23 // The total number of bytes which we're allowed to send to the OS 24 // The total number of bytes which we're allowed to send to the OS
24 // before knowing that they have been successfully sent. 25 // before knowing that they have been successfully sent.
25 static const size_t kMaxInFlightBytes = 10 * 1024 * 1024; // 10 MB. 26 static const size_t kMaxInFlightBytes = 10 * 1024 * 1024; // 10 MB.
26 27
27 // We keep track of the number of bytes successfully sent to 28 // We keep track of the number of bytes successfully sent to
28 // the hardware. Every once in a while we report back to the renderer 29 // the hardware. Every once in a while we report back to the renderer
29 // the number of bytes sent since the last report. This threshold determines 30 // the number of bytes sent since the last report. This threshold determines
30 // how many bytes will be sent before reporting back to the renderer. 31 // how many bytes will be sent before reporting back to the renderer.
31 static const size_t kAcknowledgementThresholdBytes = 1024 * 1024; // 1 MB. 32 static const size_t kAcknowledgementThresholdBytes = 1024 * 1024; // 1 MB.
32 33
33 static const uint8 kSysExMessage = 0xf0; 34 static const uint8 kSysExMessage = 0xf0;
34 35
35 namespace content { 36 namespace content {
36 37
37 MIDIHost::MIDIHost(int renderer_process_id, media::MIDIManager* midi_manager) 38 MIDIHost::MIDIHost(int renderer_process_id, media::MIDIManager* midi_manager)
38 : renderer_process_id_(renderer_process_id), 39 : renderer_process_id_(renderer_process_id),
40 sysex_allowed_(false),
Takashi Toyoshima 2013/11/20 10:04:58 I like this new approach.
yukawa 2013/11/20 11:06:08 Thanks. And I renamed |sysex_allowed_| with |has_s
39 midi_manager_(midi_manager), 41 midi_manager_(midi_manager),
40 sent_bytes_in_flight_(0), 42 sent_bytes_in_flight_(0),
41 bytes_sent_since_last_acknowledgement_(0) { 43 bytes_sent_since_last_acknowledgement_(0) {
42 } 44 }
43 45
44 MIDIHost::~MIDIHost() { 46 MIDIHost::~MIDIHost() {
45 if (midi_manager_) 47 if (midi_manager_)
46 midi_manager_->EndSession(this); 48 midi_manager_->EndSession(this);
47 } 49 }
48 50
(...skipping 19 matching lines...) Expand all
68 MIDIPortInfoList input_ports; 70 MIDIPortInfoList input_ports;
69 MIDIPortInfoList output_ports; 71 MIDIPortInfoList output_ports;
70 72
71 // Initialize devices and register to receive MIDI data. 73 // Initialize devices and register to receive MIDI data.
72 bool success = false; 74 bool success = false;
73 if (midi_manager_) { 75 if (midi_manager_) {
74 success = midi_manager_->StartSession(this); 76 success = midi_manager_->StartSession(this);
75 if (success) { 77 if (success) {
76 input_ports = midi_manager_->input_ports(); 78 input_ports = midi_manager_->input_ports();
77 output_ports = midi_manager_->output_ports(); 79 output_ports = midi_manager_->output_ports();
80 received_messages_queues_.clear();
81 received_messages_queues_.resize(input_ports.size());
82 // ChildSecurityPolicy is set just before OnStartSession. So we can safely
83 // cache the policy.
84 sysex_allowed_ = ChildProcessSecurityPolicyImpl::GetInstance()->
Takashi Toyoshima 2013/11/20 10:04:58 FYI, midi_dispatcher_host.cc is the exact place to
yukawa 2013/11/20 11:06:08 Thank you for the info! Slightly revised the comm
85 CanSendMIDISysExMessage(renderer_process_id_);
78 } 86 }
79 } 87 }
80 88
81 Send(new MIDIMsg_SessionStarted( 89 Send(new MIDIMsg_SessionStarted(
82 client_id, 90 client_id,
83 success, 91 success,
84 input_ports, 92 input_ports,
85 output_ports)); 93 output_ports));
86 } 94 }
87 95
88 void MIDIHost::OnSendData(uint32 port, 96 void MIDIHost::OnSendData(uint32 port,
89 const std::vector<uint8>& data, 97 const std::vector<uint8>& data,
90 double timestamp) { 98 double timestamp) {
91 if (!midi_manager_) 99 if (!midi_manager_)
92 return; 100 return;
93 101
94 if (data.empty()) 102 if (data.empty())
95 return; 103 return;
96 104
97 base::AutoLock auto_lock(in_flight_lock_); 105 // Blink running in a renderer checks permission to raise a SecurityError
106 // in JavaScript. The actual permission check for security purposes
107 // happens here in the browser process.
108 if (!sysex_allowed_ &&
109 (std::find(data.begin(), data.end(), kSysExMessage) != data.end())) {
110 RecordAction(UserMetricsAction("BadMessageTerminate_MIDI"));
111 BadMessageReceived();
112 return;
113 }
98 114
99 // Sanity check that we won't send too much. 115 if (!media::IsValidMIDIMessagesForWebMIDI(data))
100 if (sent_bytes_in_flight_ > kMaxInFlightBytes ||
101 data.size() > kMaxInFlightBytes ||
102 data.size() + sent_bytes_in_flight_ > kMaxInFlightBytes)
103 return; 116 return;
104 117
105 if (data[0] >= kSysExMessage) { 118 base::AutoLock auto_lock(in_flight_lock_);
106 // Blink running in a renderer checks permission to raise a SecurityError in 119 // Sanity check that we won't send too much data.
107 // JavaScript. The actual permission check for security perposes happens 120 if (data.size() + sent_bytes_in_flight_ > kMaxInFlightBytes)
Takashi Toyoshima 2013/11/20 10:04:58 Can you add TODO here? We should handle internal b
yukawa 2013/11/20 11:06:08 Done.
108 // here in the browser process. 121 return;
109 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanSendMIDISysExMessage( 122 midi_manager_->DispatchSendMIDIData(this, port, data, timestamp);
110 renderer_process_id_)) {
111 RecordAction(UserMetricsAction("BadMessageTerminate_MIDI"));
112 BadMessageReceived();
113 return;
114 }
115 }
116
117 midi_manager_->DispatchSendMIDIData(
118 this,
119 port,
120 data,
121 timestamp);
122
123 sent_bytes_in_flight_ += data.size(); 123 sent_bytes_in_flight_ += data.size();
124 } 124 }
125 125
126 void MIDIHost::ReceiveMIDIData( 126 void MIDIHost::ReceiveMIDIData(
127 uint32 port, 127 uint32 port,
128 const uint8* data, 128 const uint8* data,
129 size_t length, 129 size_t length,
130 double timestamp) { 130 double timestamp) {
131 TRACE_EVENT0("midi", "MIDIHost::ReceiveMIDIData"); 131 TRACE_EVENT0("midi", "MIDIHost::ReceiveMIDIData");
132 132
133 // Check a process security policy to receive a system exclusive message. 133 if (received_messages_queues_.size() <= port)
134 if (length > 0 && data[0] >= kSysExMessage) { 134 return;
135 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanSendMIDISysExMessage( 135
136 renderer_process_id_)) { 136 // Lazy initialization
137 // MIDI devices may send a system exclusive messages even if the renderer 137 if (received_messages_queues_[port] == NULL)
138 // doesn't have a permission to receive it. Don't kill the renderer as 138 received_messages_queues_[port] = new media::MIDIMessageQueue(true);
139 // OnSendData() does. 139
140 return; 140 received_messages_queues_[port]->Add(data, length);
141 } 141 std::vector<uint8> message;
142 while (true) {
143 received_messages_queues_[port]->Get(&message);
144 if (message.empty())
145 break;
146
147 // MIDI devices may send a system exclusive messages even if the renderer
148 // doesn't have a permission to receive it. Don't kill the renderer as
149 // OnSendData() does.
150 if (message[0] == kSysExMessage && !sysex_allowed_)
151 continue;
152
153 // Send to the renderer.
154 Send(new MIDIMsg_DataReceived(port, message, timestamp));
142 } 155 }
143
144 // Send to the renderer.
145 std::vector<uint8> v(data, data + length);
146 Send(new MIDIMsg_DataReceived(port, v, timestamp));
147 } 156 }
148 157
149 void MIDIHost::AccumulateMIDIBytesSent(size_t n) { 158 void MIDIHost::AccumulateMIDIBytesSent(size_t n) {
150 { 159 {
151 base::AutoLock auto_lock(in_flight_lock_); 160 base::AutoLock auto_lock(in_flight_lock_);
152 if (n <= sent_bytes_in_flight_) 161 if (n <= sent_bytes_in_flight_)
153 sent_bytes_in_flight_ -= n; 162 sent_bytes_in_flight_ -= n;
154 } 163 }
155 164
156 if (bytes_sent_since_last_acknowledgement_ + n >= 165 if (bytes_sent_since_last_acknowledgement_ + n >=
157 bytes_sent_since_last_acknowledgement_) 166 bytes_sent_since_last_acknowledgement_)
158 bytes_sent_since_last_acknowledgement_ += n; 167 bytes_sent_since_last_acknowledgement_ += n;
159 168
160 if (bytes_sent_since_last_acknowledgement_ >= 169 if (bytes_sent_since_last_acknowledgement_ >=
161 kAcknowledgementThresholdBytes) { 170 kAcknowledgementThresholdBytes) {
162 Send(new MIDIMsg_AcknowledgeSentData( 171 Send(new MIDIMsg_AcknowledgeSentData(
163 bytes_sent_since_last_acknowledgement_)); 172 bytes_sent_since_last_acknowledgement_));
164 bytes_sent_since_last_acknowledgement_ = 0; 173 bytes_sent_since_last_acknowledgement_ = 0;
165 } 174 }
166 } 175 }
167 176
168 } // namespace content 177 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698