Skip to content
Closed
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
23 changes: 22 additions & 1 deletion src/libpsl-native/src/getuserfrompid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#include <sys/sysctl.h>
#endif

#if __FreeBSD__
#include <sys/user.h>
#endif

char* GetUserFromPid(pid_t pid)
{

Expand All @@ -35,7 +39,7 @@ char* GetUserFromPid(pid_t pid)
size_t oldlenp = sizeof(oldp);
int name[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
u_int namelen = sizeof(name)/sizeof(int);

// Read-only query
int ret = sysctl(name, namelen, &oldp, &oldlenp, NULL, 0);
if (ret != 0 || oldlenp == 0)
Expand All @@ -45,6 +49,23 @@ char* GetUserFromPid(pid_t pid)

return GetPwUid(oldp.kp_eproc.e_ucred.cr_uid);

#elif defined(__FreeBSD__)

// Get effective owner of pid from sysctl
struct kinfo_proc oldp;
size_t oldlenp = sizeof(oldp);
int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid };
u_int namelen = sizeof(name)/sizeof(int);

int ret = sysctl(name, namelen, &oldp, &oldlenp, NULL, 0);
if (ret != 0 || oldlenp == 0)
{
return NULL;
}

// TODO: Real of effective user ID?
return GetPwUid(oldp.ki_uid);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ki_uid returns effective user id, where as ki_ruid returns real user id and ki_svuid returns saved effective user id. Which one should we look for in this case?


#else

return NULL;
Expand Down
8 changes: 7 additions & 1 deletion src/libpsl-native/src/isfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,11 @@ bool IsFile(const char* path)
assert(path);

struct stat buf;
return lstat(path, &buf) == 0;
if (lstat(path, &buf) == -1)
{
// TODO: throw error on path doesn't exist?
return false;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If path does not exist, is returning false enough?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unit test test-isfile.cpp currently expects false for a file that does not exit. See https://github.com/PowerShell/PowerShell/blob/master/src/libpsl-native/test/test-isfile.cpp

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I would suggest testing for path before using it; something like the following:

if (path == NULL || lstat(path, &buf) == -1)

}

return S_ISDIR(buf.st_mode) == 0;
}