Skip to content

Commit

Permalink
Merge branch 'master' of github.com:iovisor/bpftrace
Browse files Browse the repository at this point in the history
* 'master' of github.com:iovisor/bpftrace:
  Prevent crash with /proc path canonicalization (bpftrace#1637)
  • Loading branch information
casparant committed Nov 24, 2020
2 parents 61ff216 + ccdb0e7 commit dacbddc
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -116,6 +116,8 @@ and this project adheres to
- [#1622](https://github.com/iovisor/bpftrace/pull/1622)
- Check exponent value can be expressed in uint64_t
- [#1623](https://github.com/iovisor/bpftrace/pull/1623)
- Fix tracing of usdt probes across namespaces
- [#1637](https://github.com/iovisor/bpftrace/pull/1637)

#### Tools
- Hook up execsnoop.bt script onto `execveat` call
Expand Down
15 changes: 13 additions & 2 deletions src/utils.cpp
Expand Up @@ -928,8 +928,19 @@ uint32_t kernel_version(int attempt)

std::string abs_path(const std::string &rel_path)
{
auto p = std_filesystem::path(rel_path);
return std_filesystem::canonical(std_filesystem::absolute(p)).string();
// filesystem::canonical does not work very well with /proc/<pid>/root paths
// of processes in a different mount namespace (than the one bpftrace is
// running in), failing during canonicalization. See iovisor:bpftrace#1595
static auto re = std::regex("^/proc/\\d+/root/.*");
if (!std::regex_match(rel_path, re))
{
auto p = std_filesystem::path(rel_path);
return std_filesystem::canonical(std_filesystem::absolute(p)).string();
}
else
{
return rel_path;
}
}

} // namespace bpftrace
40 changes: 40 additions & 0 deletions tests/utils.cpp
Expand Up @@ -87,6 +87,17 @@ static void symlink_test_binary(const std::string& destination)
}
}

static std::string get_working_path()
{
char cwd_path[PATH_MAX];
if (::getcwd(cwd_path, PATH_MAX) == nullptr)
{
throw std::runtime_error(
"getting current working directory for tests failed");
}
return std::string(cwd_path);
}

TEST(utils, resolve_binary_path)
{
std::string path = "/tmp/bpftrace-test-utils-XXXXXX";
Expand Down Expand Up @@ -131,6 +142,35 @@ TEST(utils, parse_exponent)
EXPECT_EQ(parse_exponent((const char*)"2a9"), 2ULL);
}

TEST(utils, abs_path)
{
std::string path = "/tmp/bpftrace-test-utils-XXXXXX";
std::string rel_file = "bpftrace-test-utils-abs-path";
if (::mkdtemp(&path[0]) == nullptr)
{
throw std::runtime_error("creating temporary path for tests failed");
}

int fd;
fd = open((path + "/somefile").c_str(), O_CREAT, S_IRUSR);
close(fd);
fd = open(rel_file.c_str(), O_CREAT, S_IRUSR);
close(fd);

// Translates absolute path with '../..'
EXPECT_EQ(abs_path(path + "/../.." + path + "/somefile"), path + "/somefile");
// Translates relative path with './'
EXPECT_EQ(abs_path("./" + rel_file), get_working_path() + "/" + rel_file);

// /proc/<pid>/root path returned as is (and doesn't throw)
EXPECT_NO_THROW(
abs_path(std::string("/proc/1/root/usr/local/bin/usdt_test.so")));
EXPECT_EQ(abs_path(std::string("/proc/1/root/usr/local/bin/usdt_test.so")),
std::string("/proc/1/root/usr/local/bin/usdt_test.so"));

remove(rel_file.c_str());
}

} // namespace utils
} // namespace test
} // namespace bpftrace

0 comments on commit dacbddc

Please sign in to comment.