Skip to content

Commit

Permalink
Merge pull request #6676 from virusdave/dnicponski/scratch/swap_homed…
Browse files Browse the repository at this point in the history
…ir_check_master

Verify this if `$HOME` exists, it is owned by current user in `getHome()`
  • Loading branch information
thufschmitt authored Jun 20, 2022
2 parents 7e301fd + ca2be50 commit 16c6c6c
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/libutil/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,20 @@ Path getHome()
static Path homeDir = []()
{
auto homeDir = getEnv("HOME");
if (homeDir) {
// Only use $HOME if doesn't exist or is owned by the current user.
struct stat st;
int result = stat(homeDir->c_str(), &st);
if (result != 0) {
if (errno != ENOENT) {
warn("Couldn't stat $HOME ('%s') for reason other than not existing ('%d'), falling back to the one defined in the 'passwd' file", *homeDir, errno);
homeDir.reset();
}
} else if (st.st_uid != geteuid()) {
warn("$HOME ('%s') is not owned by you, falling back to the one defined in the 'passwd' file", *homeDir);
homeDir.reset();
}
}
if (!homeDir) {
std::vector<char> buf(16384);
struct passwd pwbuf;
Expand Down

0 comments on commit 16c6c6c

Please sign in to comment.