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

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 NOTREACHED 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 // Connection not yet established.
Wez 2014/07/23 21:27:42 nit: No need for this; should be implicit from rea
Kevin M 2014/07/23 23:40:46 Done.
546 // Read errors during the handshake should notify the caller via
547 // the connect callback, rather than the message event delegate.
548 PostTaskToStartConnectLoop(net::ERR_FAILED);
549 } else {
550 // Connection is already established.
551 // Close and send error status via the message event delegate.
552 CloseWithError(error_state_);
553 }
554 }
545 } 555 }
546 556
547 int CastSocket::DoRead() { 557 int CastSocket::DoRead() {
548 read_state_ = READ_STATE_READ_COMPLETE; 558 read_state_ = READ_STATE_READ_COMPLETE;
549 // Figure out whether to read header or body, and the remaining bytes. 559 // Figure out whether to read header or body, and the remaining bytes.
550 uint32 num_bytes_to_read = 0; 560 uint32 num_bytes_to_read = 0;
551 if (header_read_buffer_->RemainingCapacity() > 0) { 561 if (header_read_buffer_->RemainingCapacity() > 0) {
552 current_read_buffer_ = header_read_buffer_; 562 current_read_buffer_ = header_read_buffer_;
553 num_bytes_to_read = header_read_buffer_->RemainingCapacity(); 563 num_bytes_to_read = header_read_buffer_->RemainingCapacity();
554 DCHECK_LE(num_bytes_to_read, MessageHeader::header_size()); 564 if (num_bytes_to_read > MessageHeader::header_size()) {
565 error_state_ = CHANNEL_ERROR_INVALID_MESSAGE;
566 return net::ERR_FAILED;
567 }
555 } else { 568 } else {
556 DCHECK_GT(current_message_size_, 0U); 569 DCHECK_GT(current_message_size_, 0U);
557 num_bytes_to_read = current_message_size_ - body_read_buffer_->offset(); 570 num_bytes_to_read = current_message_size_ - body_read_buffer_->offset();
558 current_read_buffer_ = body_read_buffer_; 571 current_read_buffer_ = body_read_buffer_;
559 DCHECK_LE(num_bytes_to_read, MessageHeader::max_message_size()); 572 if (num_bytes_to_read > MessageHeader::max_message_size()) {
573 error_state_ = CHANNEL_ERROR_INVALID_MESSAGE;
574 return net::ERR_FAILED;
575 }
560 } 576 }
561 DCHECK_GT(num_bytes_to_read, 0U); 577 if (num_bytes_to_read == 0) {
578 error_state_ = CHANNEL_ERROR_INVALID_MESSAGE;
579 return net::ERR_FAILED;
580 }
562 581
563 // Read up to num_bytes_to_read into |current_read_buffer_|. 582 // Read up to num_bytes_to_read into |current_read_buffer_|.
564 return socket_->Read( 583 return socket_->Read(
565 current_read_buffer_.get(), 584 current_read_buffer_.get(),
566 num_bytes_to_read, 585 num_bytes_to_read,
567 base::Bind(&CastSocket::DoReadLoop, AsWeakPtr())); 586 base::Bind(&CastSocket::DoReadLoop, AsWeakPtr()));
568 } 587 }
569 588
570 int CastSocket::DoReadComplete(int result) { 589 int CastSocket::DoReadComplete(int result) {
571 VLOG_WITH_CONNECTION(2) << "DoReadComplete result = " << result 590 VLOG_WITH_CONNECTION(2) << "DoReadComplete result = " << result
572 << " header offset = " 591 << " header offset = "
573 << header_read_buffer_->offset() 592 << header_read_buffer_->offset()
574 << " body offset = " << body_read_buffer_->offset(); 593 << " body offset = " << body_read_buffer_->offset();
575 if (result <= 0) { // 0 means EOF: the peer closed the socket 594 if (result <= 0) { // 0 means EOF: the peer closed the socket
576 VLOG_WITH_CONNECTION(1) << "Read error, peer closed the socket"; 595 VLOG_WITH_CONNECTION(1) << "Read error, peer closed the socket";
577 error_state_ = CHANNEL_ERROR_SOCKET_ERROR; 596 error_state_ = CHANNEL_ERROR_SOCKET_ERROR;
578 read_state_ = READ_STATE_ERROR; 597 read_state_ = READ_STATE_ERROR;
579 return result == 0 ? net::ERR_FAILED : result; 598 return result == 0 ? net::ERR_FAILED : result;
580 } 599 }
581 600
582 // Some data was read. Move the offset in the current buffer forward. 601 // Some data was read. Move the offset in the current buffer forward.
583 DCHECK_LE(current_read_buffer_->offset() + result, 602 CHECK_LE(current_read_buffer_->offset() + result,
584 current_read_buffer_->capacity()); 603 current_read_buffer_->capacity());
585 current_read_buffer_->set_offset(current_read_buffer_->offset() + result); 604 current_read_buffer_->set_offset(current_read_buffer_->offset() + result);
586 read_state_ = READ_STATE_READ; 605 read_state_ = READ_STATE_READ;
587 606
588 if (current_read_buffer_.get() == header_read_buffer_.get() && 607 if (current_read_buffer_.get() == header_read_buffer_.get() &&
589 current_read_buffer_->RemainingCapacity() == 0) { 608 current_read_buffer_->RemainingCapacity() == 0) {
590 // A full header is read, process the contents. 609 // A full header is read, process the contents.
591 if (!ProcessHeader()) { 610 if (!ProcessHeader()) {
592 error_state_ = cast_channel::CHANNEL_ERROR_INVALID_MESSAGE; 611 error_state_ = cast_channel::CHANNEL_ERROR_INVALID_MESSAGE;
593 read_state_ = READ_STATE_ERROR; 612 read_state_ = READ_STATE_ERROR;
594 } 613 }
595 } else if (current_read_buffer_.get() == body_read_buffer_.get() && 614 } else if (current_read_buffer_.get() == body_read_buffer_.get() &&
596 static_cast<uint32>(current_read_buffer_->offset()) == 615 static_cast<uint32>(current_read_buffer_->offset()) ==
597 current_message_size_) { 616 current_message_size_) {
598 // Full body is read, process the contents. 617 // Full body is read, process the contents.
599 if (ProcessBody()) { 618 if (ProcessBody()) {
600 read_state_ = READ_STATE_DO_CALLBACK; 619 read_state_ = READ_STATE_DO_CALLBACK;
601 } else { 620 } else {
602 error_state_ = cast_channel::CHANNEL_ERROR_INVALID_MESSAGE; 621 error_state_ = cast_channel::CHANNEL_ERROR_INVALID_MESSAGE;
603 read_state_ = READ_STATE_ERROR; 622 read_state_ = READ_STATE_ERROR;
604 } 623 }
605 } 624 }
606 625
607 return net::OK; 626 return net::OK;
608 } 627 }
609 628
610 int CastSocket::DoReadCallback() { 629 int CastSocket::DoReadCallback() {
611 read_state_ = READ_STATE_READ; 630 read_state_ = READ_STATE_READ;
612 const CastMessage& message = *(current_message_.get()); 631 const CastMessage& message = *current_message_;
613 if (IsAuthMessage(message)) { 632 if (ready_state_ == READY_STATE_CONNECTING) {
614 // An auth message is received, check that connect flow is running. 633 if (IsAuthMessage(message)) {
615 if (ready_state_ == READY_STATE_CONNECTING) {
616 challenge_reply_.reset(new CastMessage(message)); 634 challenge_reply_.reset(new CastMessage(message));
617 PostTaskToStartConnectLoop(net::OK); 635 PostTaskToStartConnectLoop(net::OK);
636 return net::OK;
618 } else { 637 } else {
638 // Expected an auth message, got something else instead. Handle as error.
619 read_state_ = READ_STATE_ERROR; 639 read_state_ = READ_STATE_ERROR;
640 return net::ERR_INVALID_RESPONSE;
620 } 641 }
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 } 642 }
643
644 MessageInfo message_info;
645 if (!CastMessageToMessageInfo(message, &message_info)) {
646 current_message_->Clear();
647 read_state_ = READ_STATE_ERROR;
648 return net::ERR_INVALID_RESPONSE;
649 }
650 delegate_->OnMessage(this, message_info);
628 current_message_->Clear(); 651 current_message_->Clear();
629 return net::OK; 652 return net::OK;
630 } 653 }
631 654
632 int CastSocket::DoReadError(int result) { 655 int CastSocket::DoReadError(int result) {
633 DCHECK_LE(result, 0); 656 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; 657 return net::ERR_FAILED;
641 } 658 }
642 659
643 bool CastSocket::ProcessHeader() { 660 bool CastSocket::ProcessHeader() {
644 DCHECK_EQ(static_cast<uint32>(header_read_buffer_->offset()), 661 CHECK_EQ(static_cast<uint32>(header_read_buffer_->offset()),
645 MessageHeader::header_size()); 662 MessageHeader::header_size());
646 MessageHeader header; 663 MessageHeader header;
647 MessageHeader::ReadFromIOBuffer(header_read_buffer_.get(), &header); 664 MessageHeader::ReadFromIOBuffer(header_read_buffer_.get(), &header);
648 if (header.message_size > MessageHeader::max_message_size()) 665 if (header.message_size > MessageHeader::max_message_size())
649 return false; 666 return false;
650 667
651 VLOG_WITH_CONNECTION(2) << "Parsed header { message_size: " 668 VLOG_WITH_CONNECTION(2) << "Parsed header { message_size: "
652 << header.message_size << " }"; 669 << header.message_size << " }";
653 current_message_size_ = header.message_size; 670 current_message_size_ = header.message_size;
654 return true; 671 return true;
655 } 672 }
656 673
657 bool CastSocket::ProcessBody() { 674 bool CastSocket::ProcessBody() {
658 DCHECK_EQ(static_cast<uint32>(body_read_buffer_->offset()), 675 if (static_cast<uint32>(body_read_buffer_->offset()) !=
659 current_message_size_); 676 current_message_size_) {
677 return false;
678 }
660 if (!current_message_->ParseFromArray( 679 if (!current_message_->ParseFromArray(
661 body_read_buffer_->StartOfBuffer(), current_message_size_)) { 680 body_read_buffer_->StartOfBuffer(), current_message_size_)) {
662 return false; 681 return false;
663 } 682 }
664 current_message_size_ = 0; 683 current_message_size_ = 0;
665 header_read_buffer_->set_offset(0); 684 header_read_buffer_->set_offset(0);
666 body_read_buffer_->set_offset(0); 685 body_read_buffer_->set_offset(0);
667 current_read_buffer_ = header_read_buffer_; 686 current_read_buffer_ = header_read_buffer_;
668 return true; 687 return true;
669 } 688 }
670 689
671 // static 690 // static
672 bool CastSocket::Serialize(const CastMessage& message_proto, 691 bool CastSocket::Serialize(const CastMessage& message_proto,
673 std::string* message_data) { 692 std::string* message_data) {
674 DCHECK(message_data); 693 DCHECK(message_data);
675 message_proto.SerializeToString(message_data); 694 message_proto.SerializeToString(message_data);
676 size_t message_size = message_data->size(); 695 size_t message_size = message_data->size();
677 if (message_size > MessageHeader::max_message_size()) { 696 if (message_size > MessageHeader::max_message_size()) {
678 message_data->clear(); 697 message_data->clear();
679 return false; 698 return false;
680 } 699 }
681 CastSocket::MessageHeader header; 700 CastSocket::MessageHeader header;
682 header.SetMessageSize(message_size); 701 if (!header.SetMessageSize(message_size)) {
702 return false;
703 }
683 header.PrependToString(message_data); 704 header.PrependToString(message_data);
684 return true; 705 return true;
685 }; 706 }
686 707
687 void CastSocket::CloseWithError(ChannelError error) { 708 void CastSocket::CloseWithError(ChannelError error) {
688 DCHECK(CalledOnValidThread()); 709 DCHECK(CalledOnValidThread());
689 socket_.reset(NULL); 710 socket_.reset(NULL);
690 ready_state_ = READY_STATE_CLOSED; 711 ready_state_ = READY_STATE_CLOSED;
691 error_state_ = error; 712 error_state_ = error;
692 if (delegate_) 713 if (delegate_)
693 delegate_->OnError(this, error); 714 delegate_->OnError(this, error);
694 } 715 }
695 716
696 std::string CastSocket::CastUrl() const { 717 std::string CastSocket::CastUrl() const {
697 return ((channel_auth_ == CHANNEL_AUTH_TYPE_SSL_VERIFIED) ? 718 return ((channel_auth_ == CHANNEL_AUTH_TYPE_SSL_VERIFIED) ?
698 "casts://" : "cast://") + ip_endpoint_.ToString(); 719 "casts://" : "cast://") + ip_endpoint_.ToString();
699 } 720 }
700 721
701 bool CastSocket::CalledOnValidThread() const { 722 bool CastSocket::CalledOnValidThread() const {
702 return thread_checker_.CalledOnValidThread(); 723 return thread_checker_.CalledOnValidThread();
703 } 724 }
704 725
705 CastSocket::MessageHeader::MessageHeader() : message_size(0) { } 726 CastSocket::MessageHeader::MessageHeader() : message_size(0) { }
706 727
707 void CastSocket::MessageHeader::SetMessageSize(size_t size) { 728 bool CastSocket::MessageHeader::SetMessageSize(size_t size) {
708 DCHECK(size < static_cast<size_t>(kuint32max)); 729 if (size >= static_cast<size_t>(kuint32max) ||
Wez 2014/07/23 21:27:42 Don't we already have a check against max_message_
Ryan Sleevi 2014/07/23 22:31:13 It would matter for 64-bit
Wez 2014/07/23 22:33:31 Except that we already check against max_message_s
Kevin M 2014/07/23 23:40:46 True. Removed the check. Thanks.
709 DCHECK(size > 0); 730 size == 0) {
731 return false;
732 }
710 message_size = static_cast<size_t>(size); 733 message_size = static_cast<size_t>(size);
Ryan Sleevi 2014/07/23 22:31:13 Why is this static cast needed? It feels like bot
Kevin M 2014/07/23 23:40:46 Done.
734 return true;
711 } 735 }
712 736
713 // TODO(mfoltz): Investigate replacing header serialization with base::Pickle, 737 // TODO(mfoltz): Investigate replacing header serialization with base::Pickle,
714 // if bit-for-bit compatible. 738 // if bit-for-bit compatible.
715 void CastSocket::MessageHeader::PrependToString(std::string* str) { 739 void CastSocket::MessageHeader::PrependToString(std::string* str) {
716 MessageHeader output = *this; 740 MessageHeader output = *this;
717 output.message_size = base::HostToNet32(message_size); 741 output.message_size = base::HostToNet32(message_size);
718 size_t header_size = base::checked_cast<size_t,uint32>( 742 size_t header_size = base::checked_cast<size_t,uint32>(
719 MessageHeader::header_size()); 743 MessageHeader::header_size());
720 scoped_ptr<char, base::FreeDeleter> char_array( 744 scoped_ptr<char, base::FreeDeleter> char_array(
(...skipping 30 matching lines...) Expand all
751 return true; 775 return true;
752 } 776 }
753 777
754 CastSocket::WriteRequest::~WriteRequest() { } 778 CastSocket::WriteRequest::~WriteRequest() { }
755 779
756 } // namespace cast_channel 780 } // namespace cast_channel
757 } // namespace api 781 } // namespace api
758 } // namespace extensions 782 } // namespace extensions
759 783
760 #undef VLOG_WITH_CONNECTION 784 #undef VLOG_WITH_CONNECTION
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698