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

Side by Side Diff: chrome/browser/extensions/api/cast_channel/cast_socket.cc

Issue 408633002: Fix error handling for message parse errors that occur during connection setup. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removed superfluous size check Created 6 years, 5 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 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 "chrome/browser/extensions/api/cast_channel/cast_socket.h" 5 #include "chrome/browser/extensions/api/cast_channel/cast_socket.h"
6 6
7 #include <stdlib.h> 7 #include <stdlib.h>
8 #include <string.h> 8 #include <string.h>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 return false; 151 return false;
152 bool result = net::X509Certificate::GetDEREncoded( 152 bool result = net::X509Certificate::GetDEREncoded(
153 ssl_info.cert->os_cert_handle(), cert); 153 ssl_info.cert->os_cert_handle(), cert);
154 if (result) 154 if (result)
155 VLOG_WITH_CONNECTION(1) << "Successfully extracted peer certificate: " 155 VLOG_WITH_CONNECTION(1) << "Successfully extracted peer certificate: "
156 << *cert; 156 << *cert;
157 return result; 157 return result;
158 } 158 }
159 159
160 bool CastSocket::VerifyChallengeReply() { 160 bool CastSocket::VerifyChallengeReply() {
161 return AuthenticateChallengeReply(*challenge_reply_.get(), peer_cert_); 161 return AuthenticateChallengeReply(*challenge_reply_, peer_cert_);
162 } 162 }
163 163
164 void CastSocket::Connect(const net::CompletionCallback& callback) { 164 void CastSocket::Connect(const net::CompletionCallback& callback) {
165 DCHECK(CalledOnValidThread()); 165 DCHECK(CalledOnValidThread());
166 VLOG_WITH_CONNECTION(1) << "Connect readyState = " << ready_state_; 166 VLOG_WITH_CONNECTION(1) << "Connect readyState = " << ready_state_;
167 if (ready_state_ != READY_STATE_NONE) { 167 if (ready_state_ != READY_STATE_NONE) {
168 callback.Run(net::ERR_CONNECTION_FAILED); 168 callback.Run(net::ERR_CONNECTION_FAILED);
169 return; 169 return;
170 } 170 }
171 ready_state_ = READY_STATE_CONNECTING; 171 ready_state_ = READY_STATE_CONNECTING;
(...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after
525 rv = DoRead(); 525 rv = DoRead();
526 break; 526 break;
527 case READ_STATE_READ_COMPLETE: 527 case READ_STATE_READ_COMPLETE:
528 rv = DoReadComplete(rv); 528 rv = DoReadComplete(rv);
529 break; 529 break;
530 case READ_STATE_DO_CALLBACK: 530 case READ_STATE_DO_CALLBACK:
531 rv = DoReadCallback(); 531 rv = DoReadCallback();
532 break; 532 break;
533 case READ_STATE_ERROR: 533 case READ_STATE_ERROR:
534 rv = DoReadError(rv); 534 rv = DoReadError(rv);
535 DCHECK_EQ(read_state_, READ_STATE_NONE);
535 break; 536 break;
536 default: 537 default:
537 NOTREACHED() << "BUG in read flow. Unknown state: " << state; 538 NOTREACHED() << "BUG in read flow. Unknown state: " << state;
538 break; 539 break;
539 } 540 }
540 } while (rv != net::ERR_IO_PENDING && read_state_ != READ_STATE_NONE); 541 } while (rv != net::ERR_IO_PENDING && read_state_ != READ_STATE_NONE);
541 542
542 // Read loop is done - If the result is ERR_FAILED then close with error. 543 if (rv == net::ERR_FAILED) {
543 if (rv == net::ERR_FAILED) 544 if (ready_state_ == READY_STATE_CONNECTING) {
544 CloseWithError(error_state_); 545 // Read errors during the handshake should notify the caller via
546 // the connect callback, rather than the message event delegate.
547 PostTaskToStartConnectLoop(net::ERR_FAILED);
548 } else {
549 // Connection is already established.
550 // Close and send error status via the message event delegate.
551 CloseWithError(error_state_);
552 }
553 }
545 } 554 }
546 555
547 int CastSocket::DoRead() { 556 int CastSocket::DoRead() {
548 read_state_ = READ_STATE_READ_COMPLETE; 557 read_state_ = READ_STATE_READ_COMPLETE;
549 // Figure out whether to read header or body, and the remaining bytes. 558 // Figure out whether to read header or body, and the remaining bytes.
550 uint32 num_bytes_to_read = 0; 559 uint32 num_bytes_to_read = 0;
551 if (header_read_buffer_->RemainingCapacity() > 0) { 560 if (header_read_buffer_->RemainingCapacity() > 0) {
552 current_read_buffer_ = header_read_buffer_; 561 current_read_buffer_ = header_read_buffer_;
553 num_bytes_to_read = header_read_buffer_->RemainingCapacity(); 562 num_bytes_to_read = header_read_buffer_->RemainingCapacity();
554 DCHECK_LE(num_bytes_to_read, MessageHeader::header_size()); 563 if (num_bytes_to_read > MessageHeader::header_size()) {
564 error_state_ = CHANNEL_ERROR_INVALID_MESSAGE;
mark a. foltz 2014/07/24 00:32:42 This means that header_read_buffer_ was originally
Wez 2014/07/24 00:55:55 If this really indicates a logic error then it sho
Kevin M 2014/07/24 17:45:07 Acknowledged.
Kevin M 2014/07/24 17:45:07 Done.
565 return net::ERR_FAILED;
566 }
555 } else { 567 } else {
556 DCHECK_GT(current_message_size_, 0U); 568 DCHECK_GT(current_message_size_, 0U);
557 num_bytes_to_read = current_message_size_ - body_read_buffer_->offset(); 569 num_bytes_to_read = current_message_size_ - body_read_buffer_->offset();
558 current_read_buffer_ = body_read_buffer_; 570 current_read_buffer_ = body_read_buffer_;
559 DCHECK_LE(num_bytes_to_read, MessageHeader::max_message_size()); 571 if (num_bytes_to_read > MessageHeader::max_message_size()) {
572 error_state_ = CHANNEL_ERROR_INVALID_MESSAGE;
mark a. foltz 2014/07/24 00:32:42 This is also indicative of a bug, as current_messa
Wez 2014/07/24 00:55:55 Again, given that this situation in principle can
Kevin M 2014/07/24 17:45:07 Acknowledged.
Kevin M 2014/07/24 17:45:07 Done.
573 return net::ERR_FAILED;
574 }
560 } 575 }
561 DCHECK_GT(num_bytes_to_read, 0U); 576 if (num_bytes_to_read == 0) {
577 error_state_ = CHANNEL_ERROR_INVALID_MESSAGE;
mark a. foltz 2014/07/24 00:32:42 Ditto.
Kevin M 2014/07/24 17:45:07 Done.
578 return net::ERR_FAILED;
579 }
562 580
563 // Read up to num_bytes_to_read into |current_read_buffer_|. 581 // Read up to num_bytes_to_read into |current_read_buffer_|.
564 return socket_->Read( 582 return socket_->Read(
565 current_read_buffer_.get(), 583 current_read_buffer_.get(),
566 num_bytes_to_read, 584 num_bytes_to_read,
567 base::Bind(&CastSocket::DoReadLoop, AsWeakPtr())); 585 base::Bind(&CastSocket::DoReadLoop, AsWeakPtr()));
568 } 586 }
569 587
570 int CastSocket::DoReadComplete(int result) { 588 int CastSocket::DoReadComplete(int result) {
571 VLOG_WITH_CONNECTION(2) << "DoReadComplete result = " << result 589 VLOG_WITH_CONNECTION(2) << "DoReadComplete result = " << result
572 << " header offset = " 590 << " header offset = "
573 << header_read_buffer_->offset() 591 << header_read_buffer_->offset()
574 << " body offset = " << body_read_buffer_->offset(); 592 << " body offset = " << body_read_buffer_->offset();
575 if (result <= 0) { // 0 means EOF: the peer closed the socket 593 if (result <= 0) { // 0 means EOF: the peer closed the socket
576 VLOG_WITH_CONNECTION(1) << "Read error, peer closed the socket"; 594 VLOG_WITH_CONNECTION(1) << "Read error, peer closed the socket";
577 error_state_ = CHANNEL_ERROR_SOCKET_ERROR; 595 error_state_ = CHANNEL_ERROR_SOCKET_ERROR;
578 read_state_ = READ_STATE_ERROR; 596 read_state_ = READ_STATE_ERROR;
579 return result == 0 ? net::ERR_FAILED : result; 597 return result == 0 ? net::ERR_FAILED : result;
580 } 598 }
581 599
582 // Some data was read. Move the offset in the current buffer forward. 600 // Some data was read. Move the offset in the current buffer forward.
583 DCHECK_LE(current_read_buffer_->offset() + result, 601 CHECK_LE(current_read_buffer_->offset() + result,
584 current_read_buffer_->capacity()); 602 current_read_buffer_->capacity());
585 current_read_buffer_->set_offset(current_read_buffer_->offset() + result); 603 current_read_buffer_->set_offset(current_read_buffer_->offset() + result);
586 read_state_ = READ_STATE_READ; 604 read_state_ = READ_STATE_READ;
587 605
588 if (current_read_buffer_.get() == header_read_buffer_.get() && 606 if (current_read_buffer_.get() == header_read_buffer_.get() &&
589 current_read_buffer_->RemainingCapacity() == 0) { 607 current_read_buffer_->RemainingCapacity() == 0) {
590 // A full header is read, process the contents. 608 // A full header is read, process the contents.
591 if (!ProcessHeader()) { 609 if (!ProcessHeader()) {
592 error_state_ = cast_channel::CHANNEL_ERROR_INVALID_MESSAGE; 610 error_state_ = cast_channel::CHANNEL_ERROR_INVALID_MESSAGE;
593 read_state_ = READ_STATE_ERROR; 611 read_state_ = READ_STATE_ERROR;
594 } 612 }
595 } else if (current_read_buffer_.get() == body_read_buffer_.get() && 613 } else if (current_read_buffer_.get() == body_read_buffer_.get() &&
596 static_cast<uint32>(current_read_buffer_->offset()) == 614 static_cast<uint32>(current_read_buffer_->offset()) ==
597 current_message_size_) { 615 current_message_size_) {
598 // Full body is read, process the contents. 616 // Full body is read, process the contents.
599 if (ProcessBody()) { 617 if (ProcessBody()) {
600 read_state_ = READ_STATE_DO_CALLBACK; 618 read_state_ = READ_STATE_DO_CALLBACK;
601 } else { 619 } else {
602 error_state_ = cast_channel::CHANNEL_ERROR_INVALID_MESSAGE; 620 error_state_ = cast_channel::CHANNEL_ERROR_INVALID_MESSAGE;
603 read_state_ = READ_STATE_ERROR; 621 read_state_ = READ_STATE_ERROR;
604 } 622 }
605 } 623 }
606 624
607 return net::OK; 625 return net::OK;
608 } 626 }
609 627
610 int CastSocket::DoReadCallback() { 628 int CastSocket::DoReadCallback() {
611 read_state_ = READ_STATE_READ; 629 read_state_ = READ_STATE_READ;
612 const CastMessage& message = *(current_message_.get()); 630 const CastMessage& message = *current_message_;
613 if (IsAuthMessage(message)) { 631 if (ready_state_ == READY_STATE_CONNECTING) {
614 // An auth message is received, check that connect flow is running. 632 if (IsAuthMessage(message)) {
615 if (ready_state_ == READY_STATE_CONNECTING) {
616 challenge_reply_.reset(new CastMessage(message)); 633 challenge_reply_.reset(new CastMessage(message));
617 PostTaskToStartConnectLoop(net::OK); 634 PostTaskToStartConnectLoop(net::OK);
635 return net::OK;
618 } else { 636 } else {
637 // Expected an auth message, got something else instead. Handle as error.
619 read_state_ = READ_STATE_ERROR; 638 read_state_ = READ_STATE_ERROR;
639 return net::ERR_INVALID_RESPONSE;
620 } 640 }
621 } else if (delegate_) {
622 MessageInfo message_info;
623 if (CastMessageToMessageInfo(message, &message_info))
624 delegate_->OnMessage(this, message_info);
625 else
626 read_state_ = READ_STATE_ERROR;
627 } 641 }
642
643 MessageInfo message_info;
644 if (!CastMessageToMessageInfo(message, &message_info)) {
645 current_message_->Clear();
646 read_state_ = READ_STATE_ERROR;
647 return net::ERR_INVALID_RESPONSE;
648 }
649 delegate_->OnMessage(this, message_info);
628 current_message_->Clear(); 650 current_message_->Clear();
629 return net::OK; 651 return net::OK;
630 } 652 }
631 653
632 int CastSocket::DoReadError(int result) { 654 int CastSocket::DoReadError(int result) {
633 DCHECK_LE(result, 0); 655 DCHECK_LE(result, 0);
634 // If inside connection flow, then get back to connect loop.
635 if (ready_state_ == READY_STATE_CONNECTING) {
636 PostTaskToStartConnectLoop(result);
637 // does not try to report error also.
638 return net::OK;
639 }
640 return net::ERR_FAILED; 656 return net::ERR_FAILED;
641 } 657 }
642 658
643 bool CastSocket::ProcessHeader() { 659 bool CastSocket::ProcessHeader() {
644 DCHECK_EQ(static_cast<uint32>(header_read_buffer_->offset()), 660 CHECK_EQ(static_cast<uint32>(header_read_buffer_->offset()),
645 MessageHeader::header_size()); 661 MessageHeader::header_size());
646 MessageHeader header; 662 MessageHeader header;
647 MessageHeader::ReadFromIOBuffer(header_read_buffer_.get(), &header); 663 MessageHeader::ReadFromIOBuffer(header_read_buffer_.get(), &header);
648 if (header.message_size > MessageHeader::max_message_size()) 664 if (header.message_size > MessageHeader::max_message_size())
649 return false; 665 return false;
650 666
651 VLOG_WITH_CONNECTION(2) << "Parsed header { message_size: " 667 VLOG_WITH_CONNECTION(2) << "Parsed header { message_size: "
652 << header.message_size << " }"; 668 << header.message_size << " }";
653 current_message_size_ = header.message_size; 669 current_message_size_ = header.message_size;
654 return true; 670 return true;
655 } 671 }
656 672
657 bool CastSocket::ProcessBody() { 673 bool CastSocket::ProcessBody() {
658 DCHECK_EQ(static_cast<uint32>(body_read_buffer_->offset()), 674 if (static_cast<uint32>(body_read_buffer_->offset()) !=
659 current_message_size_); 675 current_message_size_) {
Wez 2014/07/24 00:55:55 This also indicates a logic error; I can't see a w
Kevin M 2014/07/24 17:45:07 Done.
676 return false;
677 }
660 if (!current_message_->ParseFromArray( 678 if (!current_message_->ParseFromArray(
661 body_read_buffer_->StartOfBuffer(), current_message_size_)) { 679 body_read_buffer_->StartOfBuffer(), current_message_size_)) {
662 return false; 680 return false;
663 } 681 }
664 current_message_size_ = 0; 682 current_message_size_ = 0;
665 header_read_buffer_->set_offset(0); 683 header_read_buffer_->set_offset(0);
666 body_read_buffer_->set_offset(0); 684 body_read_buffer_->set_offset(0);
667 current_read_buffer_ = header_read_buffer_; 685 current_read_buffer_ = header_read_buffer_;
668 return true; 686 return true;
669 } 687 }
670 688
671 // static 689 // static
672 bool CastSocket::Serialize(const CastMessage& message_proto, 690 bool CastSocket::Serialize(const CastMessage& message_proto,
673 std::string* message_data) { 691 std::string* message_data) {
674 DCHECK(message_data); 692 DCHECK(message_data);
675 message_proto.SerializeToString(message_data); 693 message_proto.SerializeToString(message_data);
676 size_t message_size = message_data->size(); 694 size_t message_size = message_data->size();
677 if (message_size > MessageHeader::max_message_size()) { 695 if (message_size > MessageHeader::max_message_size()) {
678 message_data->clear(); 696 message_data->clear();
679 return false; 697 return false;
680 } 698 }
681 CastSocket::MessageHeader header; 699 CastSocket::MessageHeader header;
682 header.SetMessageSize(message_size); 700 if (!header.SetMessageSize(message_size)) {
701 return false;
702 }
683 header.PrependToString(message_data); 703 header.PrependToString(message_data);
684 return true; 704 return true;
685 }; 705 }
686 706
687 void CastSocket::CloseWithError(ChannelError error) { 707 void CastSocket::CloseWithError(ChannelError error) {
688 DCHECK(CalledOnValidThread()); 708 DCHECK(CalledOnValidThread());
689 socket_.reset(NULL); 709 socket_.reset(NULL);
690 ready_state_ = READY_STATE_CLOSED; 710 ready_state_ = READY_STATE_CLOSED;
691 error_state_ = error; 711 error_state_ = error;
692 if (delegate_) 712 if (delegate_)
693 delegate_->OnError(this, error); 713 delegate_->OnError(this, error);
694 } 714 }
695 715
696 std::string CastSocket::CastUrl() const { 716 std::string CastSocket::CastUrl() const {
697 return ((channel_auth_ == CHANNEL_AUTH_TYPE_SSL_VERIFIED) ? 717 return ((channel_auth_ == CHANNEL_AUTH_TYPE_SSL_VERIFIED) ?
698 "casts://" : "cast://") + ip_endpoint_.ToString(); 718 "casts://" : "cast://") + ip_endpoint_.ToString();
699 } 719 }
700 720
701 bool CastSocket::CalledOnValidThread() const { 721 bool CastSocket::CalledOnValidThread() const {
702 return thread_checker_.CalledOnValidThread(); 722 return thread_checker_.CalledOnValidThread();
703 } 723 }
704 724
705 CastSocket::MessageHeader::MessageHeader() : message_size(0) { } 725 CastSocket::MessageHeader::MessageHeader() : message_size(0) { }
706 726
707 void CastSocket::MessageHeader::SetMessageSize(size_t size) { 727 bool CastSocket::MessageHeader::SetMessageSize(size_t size) {
708 DCHECK(size < static_cast<size_t>(kuint32max)); 728 message_size = size;
709 DCHECK(size > 0); 729 return true;
mark a. foltz 2014/07/24 00:32:42 Why return a boolean if it's always true?
Kevin M 2014/07/24 17:45:07 Vestiges from validation in a previous patch. Than
710 message_size = static_cast<size_t>(size);
711 } 730 }
712 731
713 // TODO(mfoltz): Investigate replacing header serialization with base::Pickle, 732 // TODO(mfoltz): Investigate replacing header serialization with base::Pickle,
714 // if bit-for-bit compatible. 733 // if bit-for-bit compatible.
715 void CastSocket::MessageHeader::PrependToString(std::string* str) { 734 void CastSocket::MessageHeader::PrependToString(std::string* str) {
716 MessageHeader output = *this; 735 MessageHeader output = *this;
717 output.message_size = base::HostToNet32(message_size); 736 output.message_size = base::HostToNet32(message_size);
718 size_t header_size = base::checked_cast<size_t,uint32>( 737 size_t header_size = base::checked_cast<size_t,uint32>(
719 MessageHeader::header_size()); 738 MessageHeader::header_size());
720 scoped_ptr<char, base::FreeDeleter> char_array( 739 scoped_ptr<char, base::FreeDeleter> char_array(
(...skipping 30 matching lines...) Expand all
751 return true; 770 return true;
752 } 771 }
753 772
754 CastSocket::WriteRequest::~WriteRequest() { } 773 CastSocket::WriteRequest::~WriteRequest() { }
755 774
756 } // namespace cast_channel 775 } // namespace cast_channel
757 } // namespace api 776 } // namespace api
758 } // namespace extensions 777 } // namespace extensions
759 778
760 #undef VLOG_WITH_CONNECTION 779 #undef VLOG_WITH_CONNECTION
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698