Skip to content

Commit

Permalink
fix(libsinsp): remove more ub from integer copies
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Guerra <[email protected]>
  • Loading branch information
LucaGuerra authored and poiana committed Nov 13, 2023
1 parent 3284fa8 commit 99f764d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
28 changes: 16 additions & 12 deletions userspace/libsinsp/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1879,10 +1879,10 @@ const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_s
if(payload_len == 1 + 4 + 2 + 4 + 2)
{
ipv4tuple addr;
addr.m_fields.m_sip = *(uint32_t*)(payload + 1);
addr.m_fields.m_sport = *(uint16_t*)(payload+5);
addr.m_fields.m_dip = *(uint32_t*)(payload + 7);
addr.m_fields.m_dport = *(uint16_t*)(payload+11);
memcpy(&addr.m_fields.m_sip, payload + 1, sizeof(uint32_t));
memcpy(&addr.m_fields.m_sport, payload + 5, sizeof(uint16_t));
memcpy(&addr.m_fields.m_dip, payload + 7, sizeof(uint32_t));
memcpy(&addr.m_fields.m_dport, payload + 11, sizeof(uint16_t));
addr.m_fields.m_l4proto = (m_fdinfo != NULL) ? m_fdinfo->get_l4proto() : SCAP_L4_UNKNOWN;
std::string straddr = ipv4tuple_to_string(&addr, m_inspector->m_hostname_and_port_resolution_enabled);
snprintf(&m_paramstr_storage[0],
Expand Down Expand Up @@ -1910,10 +1910,10 @@ const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_s
if(sinsp_utils::is_ipv4_mapped_ipv6(sip6) && sinsp_utils::is_ipv4_mapped_ipv6(dip6))
{
ipv4tuple addr;
addr.m_fields.m_sip = *(uint32_t*)sip;
addr.m_fields.m_sport = *(uint16_t*)(payload+17);
addr.m_fields.m_dip = *(uint32_t*)dip;
addr.m_fields.m_dport = *(uint16_t*)(payload+35);
memcpy(&addr.m_fields.m_sip, sip, sizeof(uint32_t));
memcpy(&addr.m_fields.m_sport, payload + 17, sizeof(uint16_t));
memcpy(&addr.m_fields.m_dip, dip, sizeof(uint32_t));
memcpy(&addr.m_fields.m_dport, payload + 35, sizeof(uint16_t));
addr.m_fields.m_l4proto = (m_fdinfo != NULL) ? m_fdinfo->get_l4proto() : SCAP_L4_UNKNOWN;
std::string straddr = ipv4tuple_to_string(&addr, m_inspector->m_hostname_and_port_resolution_enabled);

Expand Down Expand Up @@ -1954,14 +1954,18 @@ const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_s
//
// Sanitize the file string.
//
std::string sanitized_str = payload + 17;
sanitize_string(sanitized_str);
std::string sanitized_str = payload + 17;
sanitize_string(sanitized_str);

uint64_t src, dst;
memcpy(&src, payload + 1, sizeof(uint64_t));
memcpy(&dst, payload + 9, sizeof(uint64_t));

snprintf(&m_paramstr_storage[0],
m_paramstr_storage.size(),
"%" PRIx64 "->%" PRIx64 " %s",
*(uint64_t*)(payload + 1),
*(uint64_t*)(payload + 9),
src,
dst,
sanitized_str.c_str());
}
else
Expand Down
8 changes: 4 additions & 4 deletions userspace/libsinsp/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,9 @@ bool sinsp_utils::sockinfo_to_str(sinsp_sockinfo* sinfo, scap_fd_type stype, cha
sinfo->m_ipv4info.m_fields.m_l4proto == SCAP_L4_UDP)
{
ipv4tuple addr;
addr.m_fields.m_sip = *(uint32_t*)sb;
addr.m_fields.m_sip = sinfo->m_ipv4info.m_fields.m_sip;
addr.m_fields.m_sport = sinfo->m_ipv4info.m_fields.m_sport;
addr.m_fields.m_dip = *(uint32_t*)db;
addr.m_fields.m_dip = sinfo->m_ipv4info.m_fields.m_dip;
addr.m_fields.m_dport = sinfo->m_ipv4info.m_fields.m_dport;
addr.m_fields.m_l4proto = sinfo->m_ipv4info.m_fields.m_l4proto;
std::string straddr = ipv4tuple_to_string(&addr, resolve);
Expand Down Expand Up @@ -525,9 +525,9 @@ bool sinsp_utils::sockinfo_to_str(sinsp_sockinfo* sinfo, scap_fd_type stype, cha
if(sinsp_utils::is_ipv4_mapped_ipv6(sip6) && sinsp_utils::is_ipv4_mapped_ipv6(dip6))
{
ipv4tuple addr;
addr.m_fields.m_sip = *(uint32_t*)sip;
memcpy(&addr.m_fields.m_sip, sip, sizeof(uint32_t));
addr.m_fields.m_sport = sinfo->m_ipv4info.m_fields.m_sport;
addr.m_fields.m_dip = *(uint32_t*)dip;
memcpy(&addr.m_fields.m_dip, dip, sizeof(uint32_t));
addr.m_fields.m_dport = sinfo->m_ipv4info.m_fields.m_dport;
addr.m_fields.m_l4proto = sinfo->m_ipv4info.m_fields.m_l4proto;
std::string straddr = ipv4tuple_to_string(&addr, resolve);
Expand Down

0 comments on commit 99f764d

Please sign in to comment.