Skip to content

Commit

Permalink
FreeBSD Fix getLoadedLibraries() crash
Browse files Browse the repository at this point in the history
FreeBSD's getLoadedLibraries() uses libprocstat(3) to obtain
the list of mapped files (libraries). However, the list
obtained from procstat_getfiles() does not only return regular files,
but might also return anonymous objects that have no fs->path.

With exiv2 v0.28, I had also observed crashes with a real TTY,
and the entry->fs_path dereference would crash, even if it
was only for a test "non-null", so we need something more thorough.

Add a check to only consider VNODEs (actual file system entries),
and also make sure that entry->fs_path is defined so we don't
pass a nullptr to the std::string(const char *) constructor when
we are reading corrupt data. std::string((const char *)0)
on FreeBSD 13.2 - calls strlen() and crashes there.
  • Loading branch information
mandree committed Jul 1, 2023
1 parent 9215f74 commit 4d1b153
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,18 @@ static std::vector<std::string> getLoadedLibraries() {
pushPath(path, libs, paths);
}
#elif defined(__FreeBSD__)
// this code seg-faults when called from an SSH script! (security?)
if (isatty(STDIN_FILENO)) {
{
unsigned int n;
struct procstat* procstat = procstat_open_sysctl();
struct kinfo_proc* procs = procstat ? procstat_getprocs(procstat, KERN_PROC_PID, getpid(), &n) : nullptr;
struct filestat_list* files = procs ? procstat_getfiles(procstat, procs, true) : nullptr;
if (files) {
filestat* entry;
STAILQ_FOREACH(entry, files, next) {
std::string path(entry->fs_path);
pushPath(path, libs, paths);
if (entry && PS_FST_TYPE_VNODE == entry->fs_type && entry->fs_path) {
std::string path(entry->fs_path);
pushPath(path, libs, paths);
}
}
}
// free resources
Expand Down

0 comments on commit 4d1b153

Please sign in to comment.