Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(userspace/libsinsp): solve fdtables 'type' field returning random data #1903

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion userspace/libsinsp/fdinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,18 @@ sinsp_fdinfo::sinsp_fdinfo(std::shared_ptr<libsinsp::state::dynamic_struct::fiel
libsinsp::state::static_struct::field_infos sinsp_fdinfo::static_fields() const
{
libsinsp::state::static_struct::field_infos ret;
define_static_field(ret, this, (uint32_t) m_openflags, "type");

// the m_type is weird because it's a C-defined non-scoped enum, meaning that it
// should be represented by default as an integer of word-size (e.g. uint32_t in
// most cases). However, the state and plugin API only supports integers, and so
// we need to do some smart casting. Our enemy is the platform/compiler dependent
// integer size with which the enum could be represented, plus the endianess
// of the targeted architecture
auto is_big_endian = htonl(12) == 12; // the chosen number does not matter
size_t type_byte_offset = is_big_endian ? (sizeof(scap_fd_type) - 1) : 0;
define_static_field(ret, this, ((uint8_t*)(&m_type))[type_byte_offset], "type");

// the rest fo the fields are more trivial to expose
define_static_field(ret, this, m_openflags, "open_flags");
define_static_field(ret, this, m_name, "name");
define_static_field(ret, this, m_name_raw, "name_raw");
Expand All @@ -141,6 +152,10 @@ libsinsp::state::static_struct::field_infos sinsp_fdinfo::static_fields() const
define_static_field(ret, this, m_mount_id, "mount_id");
define_static_field(ret, this, m_ino, "ino");
define_static_field(ret, this, m_pid, "pid");
define_static_field(ret, this, m_fd, "fd");

// in this case we have a union, so many of the following exposed fields
// will point to the same memory areas, but this should not be an issue
define_static_field(ret, this, m_sockinfo.m_ipv4info.m_fields.m_sip, "socket_ipv4_src_ip");
define_static_field(ret, this, m_sockinfo.m_ipv4info.m_fields.m_dip, "socket_ipv4_dest_dip");
define_static_field(ret, this, m_sockinfo.m_ipv4info.m_fields.m_sport, "socket_ipv4_src_port");
Expand All @@ -162,6 +177,7 @@ libsinsp::state::static_struct::field_infos sinsp_fdinfo::static_fields() const
define_static_field(ret, this, m_sockinfo.m_ipv6serverinfo.m_l4proto, "socket_ipv6_server_l4_proto");
define_static_field(ret, this, m_sockinfo.m_unixinfo.m_fields.m_source, "socket_unix_src");
define_static_field(ret, this, m_sockinfo.m_unixinfo.m_fields.m_dest, "socket_unix_dest");

return ret;
}

Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/test/state.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ TEST(thread_manager, table_access)
TEST(thread_manager, fdtable_access)
{
// note: used for regression checks, keep this updated as we make new fields available
static const int s_fdinfo_static_fields_count = 31;
static const int s_fdinfo_static_fields_count = 32;

sinsp inspector;
auto& reg = inspector.get_table_registry();
Expand Down
Loading