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(libsinsp): fix some path handling in fs.path #1571

Merged
merged 3 commits into from
Dec 14, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions userspace/libsinsp/parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2326,6 +2326,7 @@ void sinsp_parser::parse_execve_exit(sinsp_evt *evt)
else if(flags & PPM_EXVAT_AT_EMPTY_PATH)
{
/* In this case `sdir` will always be an absolute path.
* concatenate_paths takes care of resolving the path
*/
fullpath = sinsp_utils::concatenate_paths("", sdir);

Expand Down
1 change: 1 addition & 0 deletions userspace/libsinsp/sinsp_filtercheck_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ uint8_t *sinsp_filter_check_event::extract_abspath(sinsp_evt *evt, OUT uint32_t
{
//
// Get the file path directly from the ring buffer.
// concatenate_paths takes care of resolving the path
//
m_strstorage = sinsp_utils::concatenate_paths("", evt->get_param(3)->as<std::string_view>());

Expand Down
29 changes: 13 additions & 16 deletions userspace/libsinsp/sinsp_filtercheck_fspath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.

*/

#include <filesystem>
#include "sinsp_filtercheck_fspath.h"
#include "sinsp_filtercheck_event.h"
#include "sinsp_filtercheck_fd.h"
Expand Down Expand Up @@ -282,7 +283,7 @@ uint8_t* sinsp_filter_check_fspath::extract(sinsp_evt* evt, OUT uint32_t* len, b
{
return NULL;
}
m_tstr.assign((char *) extract_values[0].ptr, extract_values[0].len);
m_tstr.assign((const char*) extract_values[0].ptr, strnlen((const char*) extract_values[0].ptr, extract_values[0].len));
};

break;
Expand All @@ -305,7 +306,7 @@ uint8_t* sinsp_filter_check_fspath::extract(sinsp_evt* evt, OUT uint32_t* len, b
{
return NULL;
}
m_tstr.assign((char *) extract_values[0].ptr, extract_values[0].len);
m_tstr.assign((const char*) extract_values[0].ptr, strnlen((const char*) extract_values[0].ptr, extract_values[0].len));
};
break;
case TYPE_TARGET:
Expand All @@ -328,7 +329,7 @@ uint8_t* sinsp_filter_check_fspath::extract(sinsp_evt* evt, OUT uint32_t* len, b
{
return NULL;
}
m_tstr.assign((char *) extract_values[0].ptr, extract_values[0].len);
m_tstr.assign((const char*) extract_values[0].ptr, strnlen((const char*) extract_values[0].ptr, extract_values[0].len));
};
break;
default:
Expand All @@ -339,8 +340,7 @@ uint8_t* sinsp_filter_check_fspath::extract(sinsp_evt* evt, OUT uint32_t* len, b
// prepend the cwd of the threadinfo to the path.
if((m_field_id == TYPE_NAME ||
m_field_id == TYPE_SOURCE ||
m_field_id == TYPE_TARGET)
&& m_tstr.front() != '/')
m_field_id == TYPE_TARGET))
{
sinsp_threadinfo* tinfo = evt->get_thread_info();

Expand All @@ -349,17 +349,14 @@ uint8_t* sinsp_filter_check_fspath::extract(sinsp_evt* evt, OUT uint32_t* len, b
return NULL;
}

m_tstr = sinsp_utils::concatenate_paths(tinfo->get_cwd(), m_tstr);
}

// If m_tstr ends in a c-style \0, remove it to be
// consistent. Generally, the evt.rawarg fields above *will*
// end in c-style \0 characters, while the ones that extract
// from the enter event or have values populated by parsers
// (e.g. fchmod/fchown) will not.
if(m_tstr.back() == '\0')
{
m_tstr.pop_back();
if(!std::filesystem::path(m_tstr).is_absolute())
{
m_tstr = sinsp_utils::concatenate_paths(tinfo->get_cwd(), m_tstr);
} else
{
// concatenate_paths takes care of resolving the path
m_tstr = sinsp_utils::concatenate_paths("", m_tstr);
}
}

RETURN_EXTRACT_STRING(m_tstr);
Expand Down