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

Side by Side Diff: content/browser/renderer_host/p2p_socket_host_udp.cc

Issue 6800023: Security restrictions for P2P UDP Sockets. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: - Created 9 years, 8 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 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_udp.h" 5 #include "content/browser/renderer_host/p2p_socket_host_udp.h"
6 6
7 #include "content/browser/renderer_host/p2p_sockets_host.h"
8 #include "content/common/p2p_messages.h" 7 #include "content/common/p2p_messages.h"
9 #include "net/base/io_buffer.h" 8 #include "net/base/io_buffer.h"
10 #include "net/base/net_errors.h" 9 #include "net/base/net_errors.h"
11 #include "net/base/net_util.h" 10 #include "net/base/net_util.h"
12 11
13 namespace { 12 namespace {
14 13
14 // UDP packets cannot be bigger than 64k.
15 const int kReadBufferSize = 65536; 15 const int kReadBufferSize = 65536;
16 16
17 } // namespace 17 } // namespace
18 18
19 P2PSocketHostUdp::P2PSocketHostUdp(P2PSocketsHost* host, int routing_id, int id) 19 P2PSocketHostUdp::P2PSocketHostUdp(IPC::Message::Sender* message_sender,
20 : P2PSocketHost(host, routing_id, id), 20 int routing_id, int id)
21 : P2PSocketHost(message_sender, routing_id, id),
21 state_(STATE_UNINITIALIZED), 22 state_(STATE_UNINITIALIZED),
23 socket_(new net::UDPServerSocket(NULL, net::NetLog::Source())),
22 send_pending_(false), 24 send_pending_(false),
23 ALLOW_THIS_IN_INITIALIZER_LIST( 25 ALLOW_THIS_IN_INITIALIZER_LIST(
24 recv_callback_(this, &P2PSocketHostUdp::OnRecv)), 26 recv_callback_(this, &P2PSocketHostUdp::OnRecv)),
25 ALLOW_THIS_IN_INITIALIZER_LIST( 27 ALLOW_THIS_IN_INITIALIZER_LIST(
26 send_callback_(this, &P2PSocketHostUdp::OnSend)) { 28 send_callback_(this, &P2PSocketHostUdp::OnSend)) {
27 } 29 }
28 30
29 P2PSocketHostUdp::~P2PSocketHostUdp() { 31 P2PSocketHostUdp::~P2PSocketHostUdp() {
30 if (state_ == STATE_OPEN) { 32 if (state_ == STATE_OPEN) {
31 DCHECK(socket_.get()); 33 DCHECK(socket_.get());
32 socket_.reset(); 34 socket_.reset();
33 } 35 }
34 } 36 }
35 37
36 bool P2PSocketHostUdp::Init(const net::IPEndPoint& local_address) { 38 bool P2PSocketHostUdp::Init(const net::IPEndPoint& local_address) {
37 net::UDPServerSocket* socket = new net::UDPServerSocket( 39 DCHECK_EQ(state_, STATE_UNINITIALIZED);
38 NULL, net::NetLog::Source());
39 socket_.reset(socket);
40 40
41 int result = socket_->Listen(local_address); 41 int result = socket_->Listen(local_address);
42 if (result < 0) { 42 if (result < 0) {
43 LOG(ERROR) << "bind() failed: " << result; 43 LOG(ERROR) << "bind() failed: " << result;
44 OnError(); 44 OnError();
45 return false; 45 return false;
46 } 46 }
47 47
48 net::IPEndPoint address; 48 net::IPEndPoint address;
49 result = socket_->GetLocalAddress(&address); 49 result = socket_->GetLocalAddress(&address);
50 if (result < 0) { 50 if (result < 0) {
51 LOG(ERROR) << "P2PSocket::Init(): unable to get local address: " 51 LOG(ERROR) << "P2PSocket::Init(): unable to get local address: "
52 << result; 52 << result;
53 OnError(); 53 OnError();
54 return false; 54 return false;
55 } 55 }
56 56
57 VLOG(1) << "Local address: " << address.ToString(); 57 VLOG(1) << "Local address: " << address.ToString();
58 58
59 state_ = STATE_OPEN; 59 state_ = STATE_OPEN;
60 60
61 recv_buffer_ = new net::IOBuffer(kReadBufferSize); 61 recv_buffer_ = new net::IOBuffer(kReadBufferSize);
62 DoRead(); 62 DoRead();
63 63
64 host_->Send(new P2PMsg_OnSocketCreated(routing_id_, id_, address)); 64 message_sender_->Send(new P2PMsg_OnSocketCreated(routing_id_, id_, address));
65 65
66 return true; 66 return true;
67 } 67 }
68 68
69 void P2PSocketHostUdp::OnError() { 69 void P2PSocketHostUdp::OnError() {
70 socket_.reset(); 70 socket_.reset();
71 71
72 if (state_ == STATE_UNINITIALIZED || state_ == STATE_OPEN) 72 if (state_ == STATE_UNINITIALIZED || state_ == STATE_OPEN)
73 host_->Send(new P2PMsg_OnError(routing_id_, id_)); 73 message_sender_->Send(new P2PMsg_OnError(routing_id_, id_));
74 74
75 state_ = STATE_ERROR; 75 state_ = STATE_ERROR;
76 } 76 }
77 77
78 void P2PSocketHostUdp::DoRead() { 78 void P2PSocketHostUdp::DoRead() {
79 int result; 79 int result;
80 do { 80 do {
81 result = socket_->RecvFrom(recv_buffer_, kReadBufferSize, &recv_address_, 81 result = socket_->RecvFrom(recv_buffer_, kReadBufferSize, &recv_address_,
82 &recv_callback_); 82 &recv_callback_);
83 DidCompleteRead(result); 83 DidCompleteRead(result);
84 } while (result > 0); 84 } while (result > 0);
85 } 85 }
86 86
87 void P2PSocketHostUdp::OnRecv(int result) { 87 void P2PSocketHostUdp::OnRecv(int result) {
88 DidCompleteRead(result); 88 DidCompleteRead(result);
89 if (state_ == STATE_OPEN) { 89 if (state_ == STATE_OPEN) {
90 DoRead(); 90 DoRead();
91 } 91 }
92 } 92 }
93 93
94 void P2PSocketHostUdp::DidCompleteRead(int result) { 94 void P2PSocketHostUdp::DidCompleteRead(int result) {
95 DCHECK_EQ(state_, STATE_OPEN); 95 DCHECK_EQ(state_, STATE_OPEN);
96 96
97 if (result > 0) { 97 if (result > 0) {
98 std::vector<char> data(recv_buffer_->data(), recv_buffer_->data() + result); 98 std::vector<char> data(recv_buffer_->data(), recv_buffer_->data() + result);
99 host_->Send(new P2PMsg_OnDataReceived(routing_id_, id_, 99
100 recv_address_, data)); 100 if (authorized_peers_.find(recv_address_) == authorized_peers_.end()) {
101 P2PSocketHost::StunMessageType type;
102 bool stun = GetStunPacketType(&*data.begin(), data.size(), &type);
103 if (stun && (type == STUN_BINDING_REQUEST ||
104 type == STUN_BINDING_RESPONSE)) {
105 authorized_peers_.insert(recv_address_);
106 } else if (!stun || type == STUN_DATA_INDICATION) {
107 LOG(ERROR) << "Received unexpected data packet from "
108 << recv_address_.ToString()
109 << " before STUN binding is finished.";
110 return;
111 }
112 }
113
114 message_sender_->Send(new P2PMsg_OnDataReceived(routing_id_, id_,
115 recv_address_, data));
101 } else if (result < 0 && result != net::ERR_IO_PENDING) { 116 } else if (result < 0 && result != net::ERR_IO_PENDING) {
102 LOG(ERROR) << "Error when reading from UDP socket: " << result; 117 LOG(ERROR) << "Error when reading from UDP socket: " << result;
103 OnError(); 118 OnError();
104 } 119 }
105 } 120 }
106 121
107 void P2PSocketHostUdp::Send(const net::IPEndPoint& socket_address, 122 void P2PSocketHostUdp::Send(const net::IPEndPoint& to,
108 const std::vector<char>& data) { 123 const std::vector<char>& data) {
109 if (send_pending_) { 124 if (send_pending_) {
110 // Silently drop packet if previous send hasn't finished. 125 // Silently drop packet if previous send hasn't finished.
111 VLOG(1) << "Dropping UDP packet."; 126 VLOG(1) << "Dropping UDP packet.";
112 return; 127 return;
113 } 128 }
114 129
130 if (authorized_peers_.find(to) == authorized_peers_.end()) {
131 P2PSocketHost::StunMessageType type;
132 bool stun = GetStunPacketType(&*data.begin(), data.size(), &type);
133 if (!stun || type == STUN_DATA_INDICATION) {
134 LOG(ERROR) << "Page tried to send a data packet to " << to.ToString()
135 << " before STUN binding is finished.";
136 OnError();
137 return;
138 }
139 }
140
115 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(data.size()); 141 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(data.size());
116 memcpy(buffer->data(), &data.begin()[0], data.size()); 142 memcpy(buffer->data(), &data.begin()[0], data.size());
117 int result = socket_->SendTo(buffer, data.size(), socket_address, 143 int result = socket_->SendTo(buffer, data.size(), to, &send_callback_);
118 &send_callback_);
119 if (result == net::ERR_IO_PENDING) { 144 if (result == net::ERR_IO_PENDING) {
120 send_pending_ = true; 145 send_pending_ = true;
121 } else if (result < 0) { 146 } else if (result < 0) {
122 LOG(ERROR) << "Error when sending data in UDP socket: " << result; 147 LOG(ERROR) << "Error when sending data in UDP socket: " << result;
123 OnError(); 148 OnError();
124 } 149 }
125 } 150 }
126 151
127 void P2PSocketHostUdp::OnSend(int result) { 152 void P2PSocketHostUdp::OnSend(int result) {
128 DCHECK(send_pending_); 153 DCHECK(send_pending_);
129 DCHECK_NE(result, net::ERR_IO_PENDING); 154 DCHECK_NE(result, net::ERR_IO_PENDING);
130 155
131 send_pending_ = false; 156 send_pending_ = false;
132 if (result < 0) 157 if (result < 0)
133 OnError(); 158 OnError();
134 } 159 }
OLDNEW
« no previous file with comments | « content/browser/renderer_host/p2p_socket_host_udp.h ('k') | content/browser/renderer_host/p2p_socket_host_udp_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698