Skip to content

Commit

Permalink
In XmppMessageBuilder::GetData() don't return temporary stack variabl…
Browse files Browse the repository at this point in the history
…e address

In XmppMessageBuilder::GetData(), address of a string allocated in stack as
a temporary variable was returned to the caller in some case. This can cause
memory corruption because the the temporary string will get destroyed right
after it goes out of scope, in this case when we return to the caller from
GetData().

Fixed by ensuring that caller of GetData() provides the necessary temporary
storage space for the "temp" std::string

Change-Id: I27e622a34b72717cad90dc9553936182c2a77c6a
Closes-Bug: 1759744
  • Loading branch information
ananth-at-camphor-networks committed Mar 29, 2018
1 parent eb2aef2 commit c9e40b1
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/bgp/bgp_message_builder.cc
Expand Up @@ -261,7 +261,7 @@ void BgpMessage::Finish() {
}

const uint8_t *BgpMessage::GetData(IPeerUpdate *peer, size_t *lenp,
const string **msg_str) {
const string **msg_str, string *temp) {
*lenp = datalen_;
return data_;
}
Expand Down
3 changes: 2 additions & 1 deletion src/bgp/bgp_message_builder.h
Expand Up @@ -21,7 +21,8 @@ class BgpMessage : public Message {
virtual bool AddRoute(const BgpRoute *route, const RibOutAttr *roattr);
virtual void Finish();
virtual const uint8_t *GetData(IPeerUpdate *peer, size_t *lenp,
const std::string **msg_str);
const std::string **msg_str,
std::string *temp);

private:
virtual void Reset();
Expand Down
3 changes: 2 additions & 1 deletion src/bgp/bgp_ribout_updates.cc
Expand Up @@ -464,7 +464,8 @@ void RibOutUpdates::UpdateSend(int queue_id, Message *message,
IPeerUpdate *peer = iter.Next();
size_t msgsize = 0;
const string *msg_str = NULL;
const uint8_t *data = message->GetData(peer, &msgsize, &msg_str);
string temp;
const uint8_t *data = message->GetData(peer, &msgsize, &msg_str, &temp);
if (Sandesh::LoggingLevel() >= Sandesh::LoggingUtLevel()) {
BGP_LOG_PEER(Message, peer, Sandesh::LoggingUtLevel(),
BGP_LOG_FLAG_SYSLOG, BGP_PEER_DIR_OUT,
Expand Down
2 changes: 1 addition & 1 deletion src/bgp/message_builder.h
Expand Up @@ -26,7 +26,7 @@ class Message {
virtual bool AddRoute(const BgpRoute *route, const RibOutAttr *roattr) = 0;
virtual void Finish() = 0;
virtual const uint8_t *GetData(IPeerUpdate *peer_update, size_t *lenp,
const std::string **msg_str) = 0;
const std::string **msg_str, std::string *temp) = 0;
uint64_t num_reach_routes() const { return num_reach_route_; }
uint64_t num_unreach_routes() const { return num_unreach_route_; }

Expand Down
5 changes: 3 additions & 2 deletions src/bgp/test/bgp_msg_builder_test.cc
Expand Up @@ -117,7 +117,8 @@ TEST_F(BgpMsgBuilderTest, Build) {

size_t length;
const string *msg_str;
const uint8_t *data = message.GetData(NULL, &length, &msg_str);
string temp;
const uint8_t *data = message.GetData(NULL, &length, &msg_str, &temp);
for (size_t i = 0; i < length; i++) {
printf("%02x ", data[i]);
}
Expand Down Expand Up @@ -152,7 +153,7 @@ TEST_F(BgpMsgBuilderTest, Build) {
route2.InsertPath(path2);
message.AddRoute(&route2, &rib_out_attr);

data = message.GetData(NULL, &length, &msg_str);
data = message.GetData(NULL, &length, &msg_str, &temp);
for (size_t i = 0; i < length; i++) {
printf("%02x ", data[i]);
}
Expand Down
8 changes: 6 additions & 2 deletions src/bgp/test/xmpp_message_builder_test.cc
Expand Up @@ -214,7 +214,9 @@ TEST_P(XmppMessageBuilderParamTest, Basic) {
XmppTestPeer peer(peer_name);
size_t msgsize;
const string *msg_str = NULL;
const uint8_t *msg = message_->GetData(&peer, &msgsize, &msg_str);
string temp;
const uint8_t *msg = message_->GetData(&peer, &msgsize, &msg_str,
&temp);
peer.SendUpdate(msg, msgsize, reuse_msg_str ? msg_str : NULL);
}
}
Expand Down Expand Up @@ -249,7 +251,9 @@ TEST_P(XmppMvpnMessageBuilderParamTest, Basic) {
XmppTestPeer peer(peer_name);
size_t msgsize;
const string *msg_str = NULL;
const uint8_t *msg = message_->GetData(&peer, &msgsize, &msg_str);
string temp;
const uint8_t *msg = message_->GetData(&peer, &msgsize, &msg_str,
&temp);
peer.SendUpdate(msg, msgsize, reuse_msg_str ? msg_str : NULL);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/bgp/xmpp_message_builder.cc
Expand Up @@ -516,7 +516,7 @@ bool BgpXmppMessage::AddMvpnRoute(const BgpRoute *route,
}

const uint8_t *BgpXmppMessage::GetData(IPeerUpdate *peer, size_t *lenp,
const string **msg_str) {
const string **msg_str, string *temp) {
// Build begin line that contains message opening tag with from and to
// attributes.
msg_begin_.clear();
Expand Down Expand Up @@ -551,10 +551,10 @@ const uint8_t *BgpXmppMessage::GetData(IPeerUpdate *peer, size_t *lenp,
*msg_str = &repr_;
return reinterpret_cast<const uint8_t *>(repr_.c_str()) + extra;
} else {
string temp = msg_begin_ + string(repr_, kMaxFromToLength);
*lenp = temp.size();
*temp = msg_begin_ + string(repr_, kMaxFromToLength);
*lenp = temp->size();
*msg_str = NULL;
return reinterpret_cast<const uint8_t *>(temp.c_str());
return reinterpret_cast<const uint8_t *>(temp->c_str());
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/bgp/xmpp_message_builder.h
Expand Up @@ -40,7 +40,8 @@ class BgpXmppMessage : public Message {
virtual void Finish();
virtual bool AddRoute(const BgpRoute *route, const RibOutAttr *roattr);
virtual const uint8_t *GetData(IPeerUpdate *peer, size_t *lenp,
const std::string **msg_str);
const std::string **msg_str,
std::string *temp);

private:
static const size_t kMaxFromToLength = 192;
Expand Down

0 comments on commit c9e40b1

Please sign in to comment.