Skip to content

Commit

Permalink
siptrace: use safety macros to copy values building address buffers
Browse files Browse the repository at this point in the history
(cherry picked from commit ed4a2bc)
  • Loading branch information
miconda committed Jan 19, 2018
1 parent d446a4f commit 67a796c
Showing 1 changed file with 54 additions and 6 deletions.
60 changes: 54 additions & 6 deletions src/modules/siptrace/siptrace.c
Expand Up @@ -1377,13 +1377,52 @@ static void trace_sl_onreply_out(sl_cbp_t *slcbp)
return;
}

#define st_bufcopy_uint(_dbuf, _dsize, _dp, _sival) do { \
str _ls; \
_ls.s = int2str(_sival, &_ls.len); \
if(_ls.s == NULL || _dp + _ls.len >= _dbuf + _dsize) { \
LM_ERR("conversion error or out of bound (%p:%d/%p:%d\n", \
_dbuf, _dsize, _dp, _ls.len); \
goto error; \
} \
memcpy(_dp, _ls.s, _ls.len); \
_dp += _ls.len; \
} while(0)

#define st_bufcopy_ipaddr(_dbuf, _dsize, _dp, _sipaddr) do { \
str _ls; \
_ls.s = ip_addr2a(_sipaddr); \
if(_ls.s == NULL) { \
LM_ERR("conversion error\n"); \
goto error; \
} \
_ls.len = strlen(_ls.s); \
if(_dp + _ls.len >= _dbuf + _dsize) { \
LM_ERR("out of bound (%p:%d/%p:%d\n", \
_dbuf, _dsize, _dp, _ls.len); \
goto error; \
} \
memcpy(_dp, _ls.s, _ls.len); \
_dp += _ls.len; \
} while(0)

#define st_bufcopy_char(_dbuf, _dsize, _dp, _scval) do { \
if(_dp + 1 >= _dbuf + _dsize) { \
LM_ERR("out of bound (%p:%d/%p:1\n", _dbuf, _dsize, _dp); \
goto error; \
} \
*_dp = _scval; \
_dp += 1; \
} while(0)

/**
*
*/
int siptrace_net_data_recv(sr_event_param_t *evp)
{
sr_net_info_t *nd;
siptrace_data_t sto;
char *cp;

if(evp->data == 0)
return -1;
Expand All @@ -1398,23 +1437,32 @@ int siptrace_net_data_recv(sr_event_param_t *evp)
sto.body.len = nd->data.len;

siptrace_copy_proto(nd->rcv->proto, sto.fromip_buff);
strcat(sto.fromip_buff, ip_addr2a(&nd->rcv->src_ip));
strcat(sto.fromip_buff, ":");
strcat(sto.fromip_buff, int2str(nd->rcv->src_port, NULL));
cp = sto.fromip_buff + strlen(sto.fromip_buff);
st_bufcopy_ipaddr(sto.fromip_buff, SIPTRACE_IP_ADDR_MAX, cp,
&nd->rcv->src_ip);
st_bufcopy_char(sto.fromip_buff, SIPTRACE_IP_ADDR_MAX, cp, ':');
st_bufcopy_uint(sto.fromip_buff, SIPTRACE_IP_ADDR_MAX, cp,
nd->rcv->src_port);
sto.fromip.s = sto.fromip_buff;
sto.fromip.len = strlen(sto.fromip_buff);

siptrace_copy_proto(nd->rcv->proto, sto.toip_buff);
strcat(sto.toip_buff, ip_addr2a(&nd->rcv->dst_ip));
strcat(sto.toip_buff, ":");
strcat(sto.toip_buff, int2str(nd->rcv->dst_port, NULL));
cp = sto.toip_buff + strlen(sto.fromip_buff);
st_bufcopy_ipaddr(sto.toip_buff, SIPTRACE_IP_ADDR_MAX, cp,
&nd->rcv->dst_ip);
st_bufcopy_char(sto.toip_buff, SIPTRACE_IP_ADDR_MAX, cp, ':');
st_bufcopy_uint(sto.toip_buff, SIPTRACE_IP_ADDR_MAX, cp,
nd->rcv->dst_port);
sto.toip.s = sto.toip_buff;
sto.toip.len = strlen(sto.toip_buff);

sto.dir = "in";

trace_send_hep_duplicate(&sto.body, &sto.fromip, &sto.toip, NULL, NULL);
return 0;

error:
return -1;
}

/**
Expand Down

0 comments on commit 67a796c

Please sign in to comment.