Skip to content

Commit

Permalink
Verify $HOME exists and is owned by current user in getHome()
Browse files Browse the repository at this point in the history
Useful because a default `sudo` on darwin doesn't clear `$HOME`, so things like `sudo nix-channel --list`
will surprisingly return the USER'S channels, rather than `root`'s.

Other counterintuitive outcomes can be seen in this PR description:
  #6622
  • Loading branch information
virusdave committed Jun 16, 2022
1 parent 9f58df4 commit 9996f5f
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/libutil/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,14 @@ Path getHome()
static Path homeDir = []()
{
auto homeDir = getEnv("HOME");
if (homeDir) {
// Only use $HOME if it exists and is owned by the current user.
struct stat st = {};
if (stat(homeDir.c_str(), &st) || st.uid_t != geteuid()) {
// Couldn't stat $HOME, or the location wasn't owned by the current user
homeDir.reset();
}
}
if (!homeDir) {
std::vector<char> buf(16384);
struct passwd pwbuf;
Expand Down

0 comments on commit 9996f5f

Please sign in to comment.