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

Side by Side Diff: content/browser/renderer_host/p2p/socket_host.cc

Issue 589183002: Fix boundary check problems in socket_host.cc. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address palmer and aedla's comments Created 6 years, 3 months 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/p2p/socket_host.h" 5 #include "content/browser/renderer_host/p2p/socket_host.h"
6 6
7 #include "base/sys_byteorder.h" 7 #include "base/sys_byteorder.h"
8 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" 8 #include "content/browser/renderer_host/p2p/socket_host_tcp.h"
9 #include "content/browser/renderer_host/p2p/socket_host_tcp_server.h" 9 #include "content/browser/renderer_host/p2p/socket_host_tcp_server.h"
10 #include "content/browser/renderer_host/p2p/socket_host_udp.h" 10 #include "content/browser/renderer_host/p2p/socket_host_udp.h"
(...skipping 15 matching lines...) Expand all
26 const int kAbsSendTimeExtnLen = 3; 26 const int kAbsSendTimeExtnLen = 3;
27 const int kOneByteHdrLen = 1; 27 const int kOneByteHdrLen = 1;
28 28
29 // Fake auth tag written by the render process if external authentication is 29 // Fake auth tag written by the render process if external authentication is
30 // enabled. HMAC in packet will be compared against this value before updating 30 // enabled. HMAC in packet will be compared against this value before updating
31 // packet with actual HMAC value. 31 // packet with actual HMAC value.
32 static const unsigned char kFakeAuthTag[10] = { 32 static const unsigned char kFakeAuthTag[10] = {
33 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd 33 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd
34 }; 34 };
35 35
36 bool IsTurnChannelData(const char* data) { 36 bool IsTurnChannelData(const char* data, size_t len) {
37 return ((*data & 0xC0) == 0x40); 37 return len >= kTurnChannelHdrLen && ((*data & 0xC0) == 0x40);
38 } 38 }
39 39
40 bool IsDtlsPacket(const char* data, int len) { 40 bool IsDtlsPacket(const char* data, size_t len) {
41 const uint8* u = reinterpret_cast<const uint8*>(data); 41 const uint8* u = reinterpret_cast<const uint8*>(data);
42 return (len >= kDtlsRecordHeaderLen && (u[0] > 19 && u[0] < 64)); 42 return (len >= kDtlsRecordHeaderLen && (u[0] > 19 && u[0] < 64));
43 } 43 }
44 44
45 bool IsRtcpPacket(const char* data) { 45 bool IsRtcpPacket(const char* data) {
juberti2 2014/09/22 22:43:57 for completeness, perhaps check len against 2 here
jiayl 2014/09/22 23:04:13 Done.
46 int type = (static_cast<uint8>(data[1]) & 0x7F); 46 int type = (static_cast<uint8>(data[1]) & 0x7F);
47 return (type >= 64 && type < 96); 47 return (type >= 64 && type < 96);
48 } 48 }
49 49
50 bool IsTurnSendIndicationPacket(const char* data) { 50 bool IsTurnSendIndicationPacket(const char* data, size_t len) {
51 if (len < content::P2PSocketHost::kStunHeaderSize)
52 return false;
53
51 uint16 type = rtc::GetBE16(data); 54 uint16 type = rtc::GetBE16(data);
52 return (type == cricket::TURN_SEND_INDICATION); 55 return (type == cricket::TURN_SEND_INDICATION);
53 } 56 }
54 57
55 bool IsRtpPacket(const char* data, int len) { 58 bool IsRtpPacket(const char* data, size_t len) {
56 return ((*data & 0xC0) == 0x80); 59 return ((*data & 0xC0) == 0x80);
57 } 60 }
58 61
59 // Verifies rtp header and message length. 62 // Verifies rtp header and message length.
60 bool ValidateRtpHeader(const char* rtp, int length, size_t* header_length) { 63 bool ValidateRtpHeader(const char* rtp, size_t length, size_t* header_length) {
61 if (header_length) 64 if (header_length)
62 *header_length = 0; 65 *header_length = 0;
63 66
64 int cc_count = rtp[0] & 0x0F; 67 size_t cc_count = rtp[0] & 0x0F;
65 int rtp_hdr_len_without_extn = kMinRtpHdrLen + 4 * cc_count; 68 size_t rtp_hdr_len_without_extn = kMinRtpHdrLen + 4 * cc_count;
66 if (rtp_hdr_len_without_extn > length) { 69 if (rtp_hdr_len_without_extn > length) {
67 return false; 70 return false;
68 } 71 }
69 72
70 // If extension bit is not set, we are done with header processing, as input 73 // If extension bit is not set, we are done with header processing, as input
71 // length is verified above. 74 // length is verified above.
72 if (!(rtp[0] & 0x10)) { 75 if (!(rtp[0] & 0x10)) {
73 if (header_length) 76 if (header_length)
74 *header_length = rtp_hdr_len_without_extn; 77 *header_length = rtp_hdr_len_without_extn;
75 78
76 return true; 79 return true;
77 } 80 }
78 81
79 rtp += rtp_hdr_len_without_extn; 82 rtp += rtp_hdr_len_without_extn;
80 83
84 if (rtp_hdr_len_without_extn + kRtpExtnHdrLen > length) {
85 return false;
86 }
87
81 // Getting extension profile length. 88 // Getting extension profile length.
82 // Length is in 32 bit words. 89 // Length is in 32 bit words.
83 uint16 extn_length = rtc::GetBE16(rtp + 2) * 4; 90 uint16 extn_len_32bit_words = rtc::GetBE16(rtp + 2);
91 size_t extn_length = extn_len_32bit_words * 4;
92
93 size_t rtp_header_length =
94 extn_length + rtp_hdr_len_without_extn + kRtpExtnHdrLen;
84 95
85 // Verify input length against total header size. 96 // Verify input length against total header size.
86 if (rtp_hdr_len_without_extn + kRtpExtnHdrLen + extn_length > length) { 97 if (rtp_header_length > length) {
87 return false; 98 return false;
88 } 99 }
89 100
90 if (header_length) 101 if (header_length)
91 *header_length = rtp_hdr_len_without_extn + kRtpExtnHdrLen + extn_length; 102 *header_length = rtp_header_length;
92 return true; 103 return true;
93 } 104 }
94 105
95 void UpdateAbsSendTimeExtnValue(char* extn_data, int len, 106 void UpdateAbsSendTimeExtnValue(char* extn_data, size_t len,
96 uint32 abs_send_time) { 107 uint32 abs_send_time) {
97 // Absolute send time in RTP streams. 108 // Absolute send time in RTP streams.
98 // 109 //
99 // The absolute send time is signaled to the receiver in-band using the 110 // The absolute send time is signaled to the receiver in-band using the
100 // general mechanism for RTP header extensions [RFC5285]. The payload 111 // general mechanism for RTP header extensions [RFC5285]. The payload
101 // of this extension (the transmitted value) is a 24-bit unsigned integer 112 // of this extension (the transmitted value) is a 24-bit unsigned integer
102 // containing the sender's current time in seconds as a fixed point number 113 // containing the sender's current time in seconds as a fixed point number
103 // with 18 bits fractional part. 114 // with 18 bits fractional part.
104 // 115 //
105 // The form of the absolute send time extension block: 116 // The form of the absolute send time extension block:
106 // 117 //
107 // 0 1 2 3 118 // 0 1 2 3
108 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 119 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
109 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 120 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110 // | ID | len=2 | absolute send time | 121 // | ID | len=2 | absolute send time |
111 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 122 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112 DCHECK_EQ(len, kAbsSendTimeExtnLen); 123 if (len != kAbsSendTimeExtnLen) {
124 NOTREACHED();
125 return;
126 }
127
113 // Now() has resolution ~1-15ms, using HighResNow(). But it is warned not to 128 // Now() has resolution ~1-15ms, using HighResNow(). But it is warned not to
114 // use it unless necessary, as it is expensive than Now(). 129 // use it unless necessary, as it is expensive than Now().
115 uint32 now_second = abs_send_time; 130 uint32 now_second = abs_send_time;
116 if (!now_second) { 131 if (!now_second) {
117 uint64 now_us = 132 uint64 now_us =
118 (base::TimeTicks::HighResNow() - base::TimeTicks()).InMicroseconds(); 133 (base::TimeTicks::HighResNow() - base::TimeTicks()).InMicroseconds();
119 // Convert second to 24-bit unsigned with 18 bit fractional part 134 // Convert second to 24-bit unsigned with 18 bit fractional part
120 now_second = 135 now_second =
121 ((now_us << 18) / base::Time::kMicrosecondsPerSecond) & 0x00FFFFFF; 136 ((now_us << 18) / base::Time::kMicrosecondsPerSecond) & 0x00FFFFFF;
122 } 137 }
123 // TODO(mallinath) - Add SetBE24 to byteorder.h in libjingle. 138 // TODO(mallinath) - Add SetBE24 to byteorder.h in libjingle.
124 extn_data[0] = static_cast<uint8>(now_second >> 16); 139 extn_data[0] = static_cast<uint8>(now_second >> 16);
125 extn_data[1] = static_cast<uint8>(now_second >> 8); 140 extn_data[1] = static_cast<uint8>(now_second >> 8);
126 extn_data[2] = static_cast<uint8>(now_second); 141 extn_data[2] = static_cast<uint8>(now_second);
127 } 142 }
128 143
129 // Assumes |len| is actual packet length + tag length. Updates HMAC at end of 144 // Assumes |len| is actual packet length + tag length. Updates HMAC at end of
130 // the RTP packet. 145 // the RTP packet.
131 void UpdateRtpAuthTag(char* rtp, int len, 146 void UpdateRtpAuthTag(char* rtp, size_t len,
132 const rtc::PacketOptions& options) { 147 const rtc::PacketOptions& options) {
133 // If there is no key, return. 148 // If there is no key, return.
134 if (options.packet_time_params.srtp_auth_key.empty()) 149 if (options.packet_time_params.srtp_auth_key.empty())
135 return; 150 return;
136 151
137 size_t tag_length = options.packet_time_params.srtp_auth_tag_len; 152 size_t tag_length = options.packet_time_params.srtp_auth_tag_len;
138 char* auth_tag = rtp + (len - tag_length);
139 153
140 // We should have a fake HMAC value @ auth_tag. 154 const size_t kRocLength = 4;
141 DCHECK_EQ(0, memcmp(auth_tag, kFakeAuthTag, tag_length)); 155 if (tag_length < kRocLength || tag_length > len) {
156 NOTREACHED();
157 return;
158 }
142 159
143 crypto::HMAC hmac(crypto::HMAC::SHA1); 160 crypto::HMAC hmac(crypto::HMAC::SHA1);
144 if (!hmac.Init(reinterpret_cast<const unsigned char*>( 161 if (!hmac.Init(reinterpret_cast<const unsigned char*>(
145 &options.packet_time_params.srtp_auth_key[0]), 162 &options.packet_time_params.srtp_auth_key[0]),
146 options.packet_time_params.srtp_auth_key.size())) { 163 options.packet_time_params.srtp_auth_key.size())) {
147 NOTREACHED(); 164 NOTREACHED();
148 return; 165 return;
149 } 166 }
150 167
151 if (hmac.DigestLength() < tag_length) { 168 if (tag_length > hmac.DigestLength()) {
152 NOTREACHED(); 169 NOTREACHED();
153 return; 170 return;
154 } 171 }
155 172
173 char* auth_tag = rtp + (len - tag_length);
174
175 // We should have a fake HMAC value @ auth_tag.
176 DCHECK_EQ(0, memcmp(auth_tag, kFakeAuthTag, tag_length));
177
156 // Copy ROC after end of rtp packet. 178 // Copy ROC after end of rtp packet.
157 memcpy(auth_tag, &options.packet_time_params.srtp_packet_index, 4); 179 memcpy(auth_tag, &options.packet_time_params.srtp_packet_index, kRocLength);
158 // Authentication of a RTP packet will have RTP packet + ROC size. 180 // Authentication of a RTP packet will have RTP packet + ROC size.
159 int auth_required_length = len - tag_length + 4; 181 int auth_required_length = len - tag_length + kRocLength;
160 182
161 unsigned char output[64]; 183 unsigned char output[64];
162 if (!hmac.Sign(base::StringPiece(rtp, auth_required_length), 184 if (!hmac.Sign(base::StringPiece(rtp, auth_required_length),
163 output, sizeof(output))) { 185 output, sizeof(output))) {
164 NOTREACHED(); 186 NOTREACHED();
165 return; 187 return;
166 } 188 }
167 // Copy HMAC from output to packet. This is required as auth tag length 189 // Copy HMAC from output to packet. This is required as auth tag length
168 // may not be equal to the actual HMAC length. 190 // may not be equal to the actual HMAC length.
169 memcpy(auth_tag, output, tag_length); 191 memcpy(auth_tag, output, tag_length);
170 } 192 }
171 193
172 } // namespace 194 } // namespace
173 195
174 namespace content { 196 namespace content {
175 197
176 namespace packet_processing_helpers { 198 namespace packet_processing_helpers {
177 199
178 bool ApplyPacketOptions(char* data, int length, 200 bool ApplyPacketOptions(char* data, size_t length,
179 const rtc::PacketOptions& options, 201 const rtc::PacketOptions& options,
180 uint32 abs_send_time) { 202 uint32 abs_send_time) {
181 DCHECK(data != NULL); 203 DCHECK(data != NULL);
182 DCHECK(length > 0); 204 DCHECK(length > 0);
183 // if there is no valid |rtp_sendtime_extension_id| and |srtp_auth_key| in 205 // if there is no valid |rtp_sendtime_extension_id| and |srtp_auth_key| in
184 // PacketOptions, nothing to be updated in this packet. 206 // PacketOptions, nothing to be updated in this packet.
185 if (options.packet_time_params.rtp_sendtime_extension_id == -1 && 207 if (options.packet_time_params.rtp_sendtime_extension_id == -1 &&
186 options.packet_time_params.srtp_auth_key.empty()) { 208 options.packet_time_params.srtp_auth_key.empty()) {
187 return true; 209 return true;
188 } 210 }
189 211
190 DCHECK(!IsDtlsPacket(data, length)); 212 DCHECK(!IsDtlsPacket(data, length));
191 DCHECK(!IsRtcpPacket(data)); 213 DCHECK(!IsRtcpPacket(data));
192 214
193 // If there is a srtp auth key present then packet must be a RTP packet. 215 // If there is a srtp auth key present then packet must be a RTP packet.
194 // RTP packet may have been wrapped in a TURN Channel Data or 216 // RTP packet may have been wrapped in a TURN Channel Data or
195 // TURN send indication. 217 // TURN send indication.
196 int rtp_start_pos; 218 size_t rtp_start_pos;
197 int rtp_length; 219 size_t rtp_length;
198 if (!GetRtpPacketStartPositionAndLength( 220 if (!GetRtpPacketStartPositionAndLength(
199 data, length, &rtp_start_pos, &rtp_length)) { 221 data, length, &rtp_start_pos, &rtp_length)) {
200 // This method should never return false. 222 // This method should never return false.
201 NOTREACHED(); 223 NOTREACHED();
202 return false; 224 return false;
203 } 225 }
204 226
205 // Skip to rtp packet. 227 // Skip to rtp packet.
206 char* start = data + rtp_start_pos; 228 char* start = data + rtp_start_pos;
207 // If packet option has non default value (-1) for sendtime extension id, 229 // If packet option has non default value (-1) for sendtime extension id,
208 // then we should parse the rtp packet to update the timestamp. Otherwise 230 // then we should parse the rtp packet to update the timestamp. Otherwise
209 // just calculate HMAC and update packet with it. 231 // just calculate HMAC and update packet with it.
210 if (options.packet_time_params.rtp_sendtime_extension_id != -1) { 232 if (options.packet_time_params.rtp_sendtime_extension_id != -1) {
211 UpdateRtpAbsSendTimeExtn( 233 UpdateRtpAbsSendTimeExtn(
212 start, rtp_length, 234 start, rtp_length,
213 options.packet_time_params.rtp_sendtime_extension_id, abs_send_time); 235 options.packet_time_params.rtp_sendtime_extension_id, abs_send_time);
214 } 236 }
215 237
216 UpdateRtpAuthTag(start, rtp_length, options); 238 UpdateRtpAuthTag(start, rtp_length, options);
217 return true; 239 return true;
218 } 240 }
219 241
220 bool GetRtpPacketStartPositionAndLength(const char* packet, 242 bool GetRtpPacketStartPositionAndLength(const char* packet,
221 int length, 243 size_t length,
222 int* rtp_start_pos, 244 size_t* rtp_start_pos,
223 int* rtp_packet_length) { 245 size_t* rtp_packet_length) {
224 int rtp_begin; 246 if (length < kMinRtpHdrLen)
225 int rtp_length = 0; 247 return false;
226 if (IsTurnChannelData(packet)) { 248
249 size_t rtp_begin;
250 size_t rtp_length = 0;
251 if (IsTurnChannelData(packet, length)) {
227 // Turn Channel Message header format. 252 // Turn Channel Message header format.
228 // 0 1 2 3 253 // 0 1 2 3
229 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 254 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
230 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 255 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
231 // | Channel Number | Length | 256 // | Channel Number | Length |
232 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 257 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
233 // | | 258 // | |
234 // / Application Data / 259 // / Application Data /
235 // / / 260 // / /
236 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 261 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
237 if (length < kTurnChannelHdrLen) {
238 return false;
239 }
240
241 rtp_begin = kTurnChannelHdrLen; 262 rtp_begin = kTurnChannelHdrLen;
242 rtp_length = rtc::GetBE16(&packet[2]); 263 rtp_length = rtc::GetBE16(&packet[2]);
243 if (length < rtp_length + kTurnChannelHdrLen) { 264 if (length < rtp_length + kTurnChannelHdrLen) {
244 return false; 265 return false;
245 } 266 }
246 } else if (IsTurnSendIndicationPacket(packet)) { 267 } else if (IsTurnSendIndicationPacket(packet, length)) {
247 if (length <= P2PSocketHost::kStunHeaderSize) {
248 // Message must be greater than 20 bytes, if it's carrying any payload.
249 return false;
250 }
251 // Validate STUN message length. 268 // Validate STUN message length.
252 int stun_msg_len = rtc::GetBE16(&packet[2]); 269 size_t stun_msg_len = rtc::GetBE16(&packet[2]);
253 if (stun_msg_len + P2PSocketHost::kStunHeaderSize != length) { 270 if (stun_msg_len + P2PSocketHost::kStunHeaderSize != length) {
254 return false; 271 return false;
255 } 272 }
256 273
257 // First skip mandatory stun header which is of 20 bytes. 274 // First skip mandatory stun header which is of 20 bytes.
258 rtp_begin = P2PSocketHost::kStunHeaderSize; 275 rtp_begin = P2PSocketHost::kStunHeaderSize;
259 // Loop through STUN attributes until we find STUN DATA attribute. 276 // Loop through STUN attributes until we find STUN DATA attribute.
260 const char* start = packet + rtp_begin; 277 const char* start = packet + rtp_begin;
261 bool data_attr_present = false; 278 bool data_attr_present = false;
262 while ((packet + rtp_begin) - start < stun_msg_len) { 279 while (packet + rtp_begin < start + stun_msg_len) {
263 // Keep reading STUN attributes until we hit DATA attribute. 280 // Keep reading STUN attributes until we hit DATA attribute.
264 // Attribute will be a TLV structure. 281 // Attribute will be a TLV structure.
265 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 282 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
266 // | Type | Length | 283 // | Type | Length |
267 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 284 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
268 // | Value (variable) .... 285 // | Value (variable) ....
269 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 286 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
270 // The value in the length field MUST contain the length of the Value 287 // The value in the length field MUST contain the length of the Value
271 // part of the attribute, prior to padding, measured in bytes. Since 288 // part of the attribute, prior to padding, measured in bytes. Since
272 // STUN aligns attributes on 32-bit boundaries, attributes whose content 289 // STUN aligns attributes on 32-bit boundaries, attributes whose content
273 // is not a multiple of 4 bytes are padded with 1, 2, or 3 bytes of 290 // is not a multiple of 4 bytes are padded with 1, 2, or 3 bytes of
274 // padding so that its value contains a multiple of 4 bytes. The 291 // padding so that its value contains a multiple of 4 bytes. The
275 // padding bits are ignored, and may be any value. 292 // padding bits are ignored, and may be any value.
276 uint16 attr_type, attr_length; 293 uint16 attr_type, attr_length;
294 const int kAttrHeaderLength = sizeof(attr_type) + sizeof(attr_length);
295
296 if (length < rtp_begin + kAttrHeaderLength) {
297 return false;
298 }
299
277 // Getting attribute type and length. 300 // Getting attribute type and length.
278 attr_type = rtc::GetBE16(&packet[rtp_begin]); 301 attr_type = rtc::GetBE16(&packet[rtp_begin]);
279 attr_length = rtc::GetBE16( 302 attr_length = rtc::GetBE16(
280 &packet[rtp_begin + sizeof(attr_type)]); 303 &packet[rtp_begin + sizeof(attr_type)]);
304
281 // Checking for bogus attribute length. 305 // Checking for bogus attribute length.
282 if (length < attr_length + rtp_begin) { 306 if (length < rtp_begin + kAttrHeaderLength + attr_length) {
283 return false; 307 return false;
284 } 308 }
285 309
286 if (attr_type != cricket::STUN_ATTR_DATA) { 310 if (attr_type != cricket::STUN_ATTR_DATA) {
287 rtp_begin += sizeof(attr_type) + sizeof(attr_length) + attr_length; 311 rtp_begin += kAttrHeaderLength + attr_length;
288 if ((attr_length % 4) != 0) { 312 if ((attr_length % 4) != 0) {
289 rtp_begin += (4 - (attr_length % 4)); 313 rtp_begin += (4 - (attr_length % 4));
290 } 314 }
291 continue; 315 continue;
292 } 316 }
293 317
294 data_attr_present = true; 318 data_attr_present = true;
295 rtp_begin += 4; // Skip STUN_DATA_ATTR header. 319 rtp_begin += kAttrHeaderLength; // Skip STUN_DATA_ATTR header.
296 rtp_length = attr_length; 320 rtp_length = attr_length;
297 // One final check of length before exiting. 321
298 if (length < rtp_length + rtp_begin) {
299 return false;
300 }
301 // We found STUN_DATA_ATTR. We can skip parsing rest of the packet. 322 // We found STUN_DATA_ATTR. We can skip parsing rest of the packet.
302 break; 323 break;
303 } 324 }
304 325
305 if (!data_attr_present) { 326 if (!data_attr_present) {
306 // There is no data attribute present in the message. We can't do anything 327 // There is no data attribute present in the message. We can't do anything
307 // with the message. 328 // with the message.
308 return false; 329 return false;
309 } 330 }
310 331
311 } else { 332 } else {
312 // This is a raw RTP packet. 333 // This is a raw RTP packet.
313 rtp_begin = 0; 334 rtp_begin = 0;
314 rtp_length = length; 335 rtp_length = length;
315 } 336 }
316 337
317 // Making sure we have a valid RTP packet at the end. 338 // Making sure we have a valid RTP packet at the end.
318 if ((rtp_length >= kMinRtpHdrLen) && 339 if ((rtp_length >= kMinRtpHdrLen) &&
319 IsRtpPacket(packet + rtp_begin, rtp_length) && 340 IsRtpPacket(packet + rtp_begin, rtp_length) &&
320 ValidateRtpHeader(packet + rtp_begin, rtp_length, NULL)) { 341 ValidateRtpHeader(packet + rtp_begin, rtp_length, NULL)) {
321 *rtp_start_pos = rtp_begin; 342 *rtp_start_pos = rtp_begin;
322 *rtp_packet_length = rtp_length; 343 *rtp_packet_length = rtp_length;
323 return true; 344 return true;
324 } 345 }
325 return false; 346 return false;
326 } 347 }
327 348
328 // ValidateRtpHeader must be called before this method to make sure, we have 349 // ValidateRtpHeader must be called before this method to make sure, we have
329 // a sane rtp packet. 350 // a sane rtp packet.
330 bool UpdateRtpAbsSendTimeExtn(char* rtp, int length, 351 bool UpdateRtpAbsSendTimeExtn(char* rtp, size_t length,
331 int extension_id, uint32 abs_send_time) { 352 int extension_id, uint32 abs_send_time) {
332 // 0 1 2 3 353 // 0 1 2 3
333 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 354 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
334 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 355 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
335 // |V=2|P|X| CC |M| PT | sequence number | 356 // |V=2|P|X| CC |M| PT | sequence number |
336 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 357 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
337 // | timestamp | 358 // | timestamp |
338 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 359 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
339 // | synchronization source (SSRC) identifier | 360 // | synchronization source (SSRC) identifier |
340 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ 361 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
341 // | contributing source (CSRC) identifiers | 362 // | contributing source (CSRC) identifiers |
342 // | .... | 363 // | .... |
343 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 364 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
344 365
345 // Return if extension bit is not set. 366 // Return if extension bit is not set.
346 if (!(rtp[0] & 0x10)) { 367 if (!(rtp[0] & 0x10)) {
347 return true; 368 return true;
348 } 369 }
349 370
350 int cc_count = rtp[0] & 0x0F; 371 size_t cc_count = rtp[0] & 0x0F;
351 int rtp_hdr_len_without_extn = kMinRtpHdrLen + 4 * cc_count; 372 size_t rtp_hdr_len_without_extn = kMinRtpHdrLen + 4 * cc_count;
352 373
353 rtp += rtp_hdr_len_without_extn; 374 rtp += rtp_hdr_len_without_extn;
354 375
355 // Getting extension profile ID and length. 376 // Getting extension profile ID and length.
356 uint16 profile_id = rtc::GetBE16(rtp); 377 uint16 profile_id = rtc::GetBE16(rtp);
357 // Length is in 32 bit words. 378 // Length is in 32 bit words.
358 uint16 extn_length = rtc::GetBE16(rtp + 2) * 4; 379 uint16 extn_len_32bit_words = rtc::GetBE16(rtp + 2);
380 size_t extn_length = extn_len_32bit_words * 4;
359 381
360 rtp += kRtpExtnHdrLen; // Moving past extn header. 382 rtp += kRtpExtnHdrLen; // Moving past extn header.
361 383
362 bool found = false; 384 bool found = false;
363 // WebRTC is using one byte header extension. 385 // WebRTC is using one byte header extension.
364 // TODO(mallinath) - Handle two byte header extension. 386 // TODO(mallinath) - Handle two byte header extension.
365 if (profile_id == 0xBEDE) { // OneByte extension header 387 if (profile_id == 0xBEDE) { // OneByte extension header
366 // 0 388 // 0
367 // 0 1 2 3 4 5 6 7 389 // 0 1 2 3 4 5 6 7
368 // +-+-+-+-+-+-+-+-+ 390 // +-+-+-+-+-+-+-+-+
369 // | ID | len | 391 // | ID | len |
370 // +-+-+-+-+-+-+-+-+ 392 // +-+-+-+-+-+-+-+-+
371 393
372 // 0 1 2 3 394 // 0 1 2 3
373 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 395 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
374 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 396 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
375 // | 0xBE | 0xDE | length=3 | 397 // | 0xBE | 0xDE | length=3 |
376 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 398 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
377 // | ID | L=0 | data | ID | L=1 | data... 399 // | ID | L=0 | data | ID | L=1 | data...
378 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 400 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
379 // ...data | 0 (pad) | 0 (pad) | ID | L=3 | 401 // ...data | 0 (pad) | 0 (pad) | ID | L=3 |
380 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 402 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
381 // | data | 403 // | data |
382 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 404 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
383 char* extn_start = rtp; 405 const char* extn_start = rtp;
384 while (rtp - extn_start < extn_length) { 406 const char* extn_end = extn_start + extn_length;
407
408 while (rtp < extn_end) {
385 const int id = (*rtp & 0xF0) >> 4; 409 const int id = (*rtp & 0xF0) >> 4;
386 const int len = (*rtp & 0x0F) + 1; 410 const size_t len = (*rtp & 0x0F) + 1;
411 if (rtp + kOneByteHdrLen + len > extn_end) {
412 return false;
413 }
387 // The 4-bit length is the number minus one of data bytes of this header 414 // The 4-bit length is the number minus one of data bytes of this header
388 // extension element following the one-byte header. 415 // extension element following the one-byte header.
389 if (id == extension_id) { 416 if (id == extension_id) {
390 UpdateAbsSendTimeExtnValue(rtp + kOneByteHdrLen, len, abs_send_time); 417 UpdateAbsSendTimeExtnValue(rtp + kOneByteHdrLen, len, abs_send_time);
391 found = true; 418 found = true;
392 break; 419 break;
393 } 420 }
394 rtp += kOneByteHdrLen + len; 421 rtp += kOneByteHdrLen + len;
395 // Counting padding bytes. 422 // Counting padding bytes.
396 while ((*rtp == 0) && (rtp - extn_start < extn_length)) { 423 while ((rtp < extn_end) && (*rtp == 0)) {
397 ++rtp; 424 ++rtp;
398 } 425 }
399 } 426 }
400 } 427 }
401 return found; 428 return found;
402 } 429 }
403 430
404 } // packet_processing_helpers 431 } // packet_processing_helpers
405 432
406 P2PSocketHost::P2PSocketHost(IPC::Sender* message_sender, int socket_id) 433 P2PSocketHost::P2PSocketHost(IPC::Sender* message_sender, int socket_id)
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 if (!dump_incoming_rtp_packet_ && !dump_outgoing_rtp_packet_) 551 if (!dump_incoming_rtp_packet_ && !dump_outgoing_rtp_packet_)
525 packet_dump_callback_.Reset(); 552 packet_dump_callback_.Reset();
526 } 553 }
527 554
528 void P2PSocketHost::DumpRtpPacket(const char* packet, 555 void P2PSocketHost::DumpRtpPacket(const char* packet,
529 size_t length, 556 size_t length,
530 bool incoming) { 557 bool incoming) {
531 if (IsDtlsPacket(packet, length) || IsRtcpPacket(packet)) 558 if (IsDtlsPacket(packet, length) || IsRtcpPacket(packet))
532 return; 559 return;
533 560
534 int rtp_packet_pos = 0; 561 size_t rtp_packet_pos = 0;
535 int rtp_packet_length = length; 562 size_t rtp_packet_length = length;
536 if (!packet_processing_helpers::GetRtpPacketStartPositionAndLength( 563 if (!packet_processing_helpers::GetRtpPacketStartPositionAndLength(
537 packet, length, &rtp_packet_pos, &rtp_packet_length)) 564 packet, length, &rtp_packet_pos, &rtp_packet_length))
538 return; 565 return;
539 566
540 packet += rtp_packet_pos; 567 packet += rtp_packet_pos;
541 568
542 size_t header_length = 0; 569 size_t header_length = 0;
543 bool valid = ValidateRtpHeader(packet, rtp_packet_length, &header_length); 570 bool valid = ValidateRtpHeader(packet, rtp_packet_length, &header_length);
544 if (!valid) { 571 if (!valid) {
545 DCHECK(false); 572 DCHECK(false);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
577 BrowserThread::PostTask(BrowserThread::UI, 604 BrowserThread::PostTask(BrowserThread::UI,
578 FROM_HERE, 605 FROM_HERE,
579 base::Bind(packet_dump_callback_, 606 base::Bind(packet_dump_callback_,
580 Passed(&packet_header), 607 Passed(&packet_header),
581 header_length, 608 header_length,
582 packet_length, 609 packet_length,
583 incoming)); 610 incoming));
584 } 611 }
585 612
586 } // namespace content 613 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/renderer_host/p2p/socket_host.h ('k') | content/browser/renderer_host/p2p/socket_host_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698