mirror of
https://github.com/RetroShare/RetroShare.git
synced 2024-10-01 02:35:48 -04:00
Corrected two harmful bugs in tunnel connexion code:
- added a destructor to RsTunnelDataItem, to remove a memory leak on all data packets. - apparently the code would allow data items of zero size, hence calling malloc(0), which has undetermined behavior. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3191 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
13671d03ea
commit
5045e720b2
@ -82,6 +82,12 @@ RsItem *RsTunnelSerialiser::deserialise(void *data, uint32_t *size)
|
||||
}
|
||||
}
|
||||
|
||||
RsTunnelDataItem::~RsTunnelDataItem()
|
||||
{
|
||||
if(encoded_data != NULL)
|
||||
free(encoded_data) ;
|
||||
}
|
||||
|
||||
bool RsTunnelDataItem::serialize(void *data,uint32_t& pktsize)
|
||||
{
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
@ -112,6 +118,8 @@ bool RsTunnelDataItem::serialize(void *data,uint32_t& pktsize)
|
||||
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, destPeerId);
|
||||
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, encoded_data_len) ;
|
||||
|
||||
if(encoded_data != NULL)
|
||||
memcpy((void*)((unsigned char*)data+offset),encoded_data,encoded_data_len) ;
|
||||
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
@ -125,9 +133,9 @@ bool RsTunnelDataItem::serialize(void *data,uint32_t& pktsize)
|
||||
std::cerr << "RsTunnelDataItem::serialiseTransfer() Size Error! " << std::endl;
|
||||
}
|
||||
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
std::cerr << "RsTunnelDataItem::serialize() packet size inside serialised data : " << getRsItemSize(data) << std::endl ;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
return ok;
|
||||
}
|
||||
@ -215,11 +223,16 @@ RsTunnelDataItem *RsTunnelDataItem::deserialise(void *data,uint32_t pktsize)
|
||||
|
||||
ok &= getRawUInt32(data, pktsize, &offset, &item->encoded_data_len) ;
|
||||
|
||||
if(!ok) // return early to avoid calling malloc with 0 size
|
||||
if(!ok) // return early to avoid calling malloc with invalid size
|
||||
return NULL ;
|
||||
|
||||
if(item->encoded_data_len > 0)
|
||||
{
|
||||
item->encoded_data = (void*)malloc(item->encoded_data_len) ;
|
||||
memcpy(item->encoded_data, (void*)((unsigned char*)data+offset), item->encoded_data_len);
|
||||
}
|
||||
else
|
||||
item->encoded_data = NULL ;
|
||||
|
||||
if ((offset + item->encoded_data_len) != rssize)
|
||||
{
|
||||
|
@ -47,6 +47,8 @@ class RsTunnelItem: public RsItem
|
||||
public:
|
||||
RsTunnelItem(uint8_t tunnel_subtype) : RsItem(RS_PKT_VERSION_SERVICE,RS_SERVICE_TYPE_TUNNEL,tunnel_subtype) {}
|
||||
|
||||
virtual ~RsTunnelItem() {}
|
||||
|
||||
virtual bool serialize(void *data,uint32_t& size) = 0 ; // Isn't it better that items can serialize themselves ?
|
||||
virtual uint32_t serial_size() { return 0;}
|
||||
|
||||
@ -56,8 +58,13 @@ class RsTunnelItem: public RsItem
|
||||
class RsTunnelDataItem: public RsTunnelItem
|
||||
{
|
||||
public:
|
||||
RsTunnelDataItem() : RsTunnelItem(RS_TUNNEL_SUBTYPE_DATA) {}
|
||||
RsTunnelDataItem(void *data,uint32_t size) ; // deserialization
|
||||
RsTunnelDataItem() : RsTunnelItem(RS_TUNNEL_SUBTYPE_DATA)
|
||||
{
|
||||
encoded_data = NULL ; // To comply with the destructor below.
|
||||
}
|
||||
virtual ~RsTunnelDataItem() ;
|
||||
|
||||
static RsTunnelDataItem *deserialise(void *data,uint32_t size) ; // deserialization
|
||||
|
||||
uint32_t encoded_data_len;
|
||||
void *encoded_data;
|
||||
@ -76,7 +83,9 @@ class RsTunnelHandshakeItem: public RsTunnelItem
|
||||
{
|
||||
public:
|
||||
RsTunnelHandshakeItem() : RsTunnelItem(RS_TUNNEL_SUBTYPE_HANDSHAKE) {}
|
||||
RsTunnelHandshakeItem(void *data,uint32_t size) ; // deserialization
|
||||
virtual ~RsTunnelHandshakeItem() {}
|
||||
|
||||
static RsTunnelHandshakeItem *deserialise(void *data,uint32_t size) ; // deserialization
|
||||
|
||||
std::string sourcePeerId ;
|
||||
std::string relayPeerId ;
|
||||
|
@ -44,8 +44,8 @@
|
||||
#define TUNNEL_HANDSHAKE_ACK 2
|
||||
#define TUNNEL_HANDSHAKE_REFUSE 0
|
||||
|
||||
p3tunnel::p3tunnel(p3ConnectMgr *cm, pqipersongrp *perGrp)
|
||||
:p3Service(RS_SERVICE_TYPE_TUNNEL), mConnMgr(cm), mPqiPersonGrp(perGrp)
|
||||
p3tunnel::p3tunnel(p3ConnectMgr *cm, pqipersongrp *perGrp)
|
||||
:p3Service(RS_SERVICE_TYPE_TUNNEL), mConnMgr(cm), mPqiPersonGrp(perGrp)
|
||||
{
|
||||
RsStackMutex stack(mTunnelMtx); /********** STACK LOCKED MTX ******/
|
||||
|
||||
@ -85,13 +85,13 @@ int p3tunnel::handleIncoming()
|
||||
RsTunnelHandshakeItem *thi = NULL;
|
||||
|
||||
{
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
std::ostringstream out;
|
||||
out << "p3tunnel::handleIncoming()";
|
||||
out << " Received Message!" << std::endl;
|
||||
item -> print(out);
|
||||
std::cerr << out.str();
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
if (NULL != (tdi = dynamic_cast<RsTunnelDataItem *> (item))) {
|
||||
@ -124,12 +124,12 @@ void p3tunnel::sendTunnelDataPrivate(std::string to, std::string sourcePeerId, s
|
||||
|
||||
// Then send message.
|
||||
{
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
std::ostringstream out;
|
||||
out << "p3tunnel::sendTunnelDataPrivate() Constructing a RsTunnelItem Message!" << std::endl;
|
||||
out << "Sending to: " << to;
|
||||
std::cerr << out.str() << std::endl;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
// Construct a message
|
||||
@ -139,14 +139,17 @@ void p3tunnel::sendTunnelDataPrivate(std::string to, std::string sourcePeerId, s
|
||||
rdi->relayPeerId = relayPeerId;
|
||||
rdi->encoded_data_len = data_length;
|
||||
|
||||
if(data_length > 0)
|
||||
{
|
||||
rdi->encoded_data = (void*)malloc(data_length);
|
||||
memcpy(rdi->encoded_data, data, data_length);
|
||||
}
|
||||
|
||||
rdi->PeerId(to);
|
||||
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
std::cerr << "p3tunnel::sendTunnelDataPrivate() data_length : "<< data_length << std::endl;
|
||||
#endif
|
||||
#endif
|
||||
/* send msg */
|
||||
sendItem(rdi);
|
||||
}
|
||||
@ -161,11 +164,11 @@ void p3tunnel::pingTunnelConnection(std::string relayPeerId, std::string destPee
|
||||
}
|
||||
|
||||
void p3tunnel::initiateHandshake(std::string relayPeerId, std::string destPeerId) {
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
std::cerr << "p3tunnel::initiateHandshake() initiating handshake with relay id : " << relayPeerId << std::endl;
|
||||
std::cerr << "ownId : " << ownId << std::endl;
|
||||
std::cerr << "destPeerId : " << destPeerId << std::endl;
|
||||
#endif
|
||||
#endif
|
||||
// Construct a message
|
||||
RsTunnelHandshakeItem *rhi = new RsTunnelHandshakeItem();
|
||||
rhi->destPeerId = destPeerId;
|
||||
@ -185,31 +188,31 @@ void p3tunnel::initiateHandshake(std::string relayPeerId, std::string destPeerId
|
||||
/*************************************************************************************/
|
||||
void p3tunnel::recvTunnelHandshake(RsTunnelHandshakeItem *item)
|
||||
{
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
std::cerr << "p3tunnel::recvTunnelHandshake() From: " << item->PeerId() << std::endl;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
RsPeerDetails pd;
|
||||
if (!AuthSSL::getAuthSSL()->LoadDetailsFromStringCert(item->sslCertPEM, pd)) {
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
std::cerr << "p3tunnel::recvTunnelHandshake() cert is not valid. This might be a intrusion attempt." << std::endl;
|
||||
#endif
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
if (item->sourcePeerId != pd.id) {
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
std::cerr << "p3tunnel::recvTunnelHandshake() cert is not issued from the source id of the tunnel. This might be a intrusion attempt." << std::endl;
|
||||
#endif
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
//compare the peer id from the item sender to the ids in the item.
|
||||
if (item->PeerId() == item->sourcePeerId && ownId == item->relayPeerId) {
|
||||
if (mConnMgr->isOnline(item->destPeerId)) {
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
std::cerr << "p3tunnel::recvTunnelHandshake() relaying packet." << std::endl;
|
||||
#endif
|
||||
#endif
|
||||
//relaying the handshake
|
||||
RsTunnelHandshakeItem* forwardItem = new RsTunnelHandshakeItem();
|
||||
forwardItem->sourcePeerId = item->sourcePeerId;
|
||||
@ -222,9 +225,9 @@ void p3tunnel::recvTunnelHandshake(RsTunnelHandshakeItem *item)
|
||||
} else {
|
||||
//sending back refuse
|
||||
//not implemented
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
std::cerr << "p3tunnel::recvTunnelHandshake() not relaying packet because destination is offline." << std::endl;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
} else if (item->PeerId() == item->relayPeerId && ownId == item->destPeerId) {
|
||||
if (item->connection_accepted == TUNNEL_HANDSHAKE_INIT || item->connection_accepted == TUNNEL_HANDSHAKE_ACK) {
|
||||
@ -234,9 +237,9 @@ void p3tunnel::recvTunnelHandshake(RsTunnelHandshakeItem *item)
|
||||
// not implemented
|
||||
} else {
|
||||
if (item->connection_accepted == TUNNEL_HANDSHAKE_INIT) {
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
std::cerr << "p3tunnel::recvTunnelHandshake() sending back acknowledgement to " << item->sourcePeerId << std::endl;
|
||||
#endif
|
||||
#endif
|
||||
//send back acknowledgement
|
||||
RsTunnelHandshakeItem* ack = new RsTunnelHandshakeItem();
|
||||
ack->sourcePeerId = ownId;
|
||||
@ -249,9 +252,9 @@ void p3tunnel::recvTunnelHandshake(RsTunnelHandshakeItem *item)
|
||||
}
|
||||
|
||||
//open the local tunnel connection
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
std::cerr << "p3tunnel::recvTunnelHandshake() opening localy the tunnel connection emulation." << std::endl;
|
||||
#endif
|
||||
#endif
|
||||
pqiperson *pers = mPqiPersonGrp->getPeer(item->sourcePeerId);
|
||||
pqissltunnel *pqicon = (pqissltunnel *)(((pqiconnect *) pers->getKid(PQI_CONNECT_TUNNEL))->ni);
|
||||
pqicon->IncommingHanshakePacket(item->relayPeerId);
|
||||
@ -263,9 +266,9 @@ void p3tunnel::recvTunnelHandshake(RsTunnelHandshakeItem *item)
|
||||
|
||||
void p3tunnel::recvTunnelData(RsTunnelDataItem *item)
|
||||
{
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
std::cerr << "p3tunnel::recvPeerConnectRequest() From: " << item->PeerId() << std::endl;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
//compare the peer id from the item sender to the ids in the item.
|
||||
if (item->PeerId() == item->sourcePeerId && ownId == item->relayPeerId) {
|
||||
@ -276,71 +279,71 @@ void p3tunnel::recvTunnelData(RsTunnelDataItem *item)
|
||||
}
|
||||
|
||||
void p3tunnel::privateRecvTunnelDataRelaying(RsTunnelDataItem *item) {
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
std::cerr << "p3tunnel::privateRecvTunnelDataRelaying() I am relaying, let's see if it's possible to send the packet to destination." << std::endl;
|
||||
#endif
|
||||
#endif
|
||||
if (!mConnMgr->isFriend(item->sourcePeerId) || !mConnMgr->isFriend(item->destPeerId)) {
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() not trusting relay or dest peer. Aborting." << std::endl;
|
||||
#endif
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
if (mConnMgr->isOnline(item->destPeerId)) {
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
std::cerr << "p3tunnel::privateRecvTunnelDataRelaying() I am relaying, relay the packet to destination." << std::endl;
|
||||
#endif
|
||||
#endif
|
||||
sendTunnelDataPrivate(item->destPeerId, item->sourcePeerId, ownId, item->destPeerId, item->encoded_data, item->encoded_data_len);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void p3tunnel::privateRecvTunnelDataDestination(RsTunnelDataItem *item) {
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() I am the destination Id, let's make some checks and read the packet." << std::endl;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if (!mConnMgr->isFriend(item->sourcePeerId) || !mConnMgr->isFriend(item->relayPeerId)) {
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() not trusting relay or source peer. Aborting." << std::endl;
|
||||
#endif
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
if (!mConnMgr->isOnline(item->relayPeerId)) {
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() relay peer is not connected, connection impossible. Aborting." << std::endl;
|
||||
#endif
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
pqiperson *pers = mPqiPersonGrp->getPeer(item->sourcePeerId);
|
||||
if (pers == NULL) {
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() tunnel connection not found. Aborting." << std::endl;
|
||||
#endif
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
pqissltunnel *pqicon = (pqissltunnel *)(((pqiconnect *) pers->getKid(PQI_CONNECT_TUNNEL))->ni);
|
||||
if (pqicon == NULL || !pqicon->isactive()) {
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() tunnel connection not found. Aborting." << std::endl;
|
||||
#endif
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
//send the packet to the net emulation layer
|
||||
if (item->encoded_data_len == 0) {
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() receiving a ping packet, activating connection and sending back acknowlegment." << std::endl;
|
||||
#endif
|
||||
#endif
|
||||
pqissltunnel *pqicon = (pqissltunnel *)(((pqiconnect *) pers->getKid(PQI_CONNECT_TUNNEL))->ni);
|
||||
pqicon->IncommingPingPacket();
|
||||
} else {
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
#ifdef P3TUNNEL_DEBUG
|
||||
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() receiving a data packet, transfer it to the pqissltunnel connection." << std::endl;
|
||||
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() getRsItemSize(item->encoded_data) : " << getRsItemSize(item->encoded_data) << std::endl;
|
||||
#endif
|
||||
#endif
|
||||
pqissltunnel *pqicon = (pqissltunnel *)(((pqiconnect *) pers->getKid(PQI_CONNECT_TUNNEL))->ni);
|
||||
pqicon->addIncomingPacket(item->encoded_data, item->encoded_data_len);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user