Merge pull request #6676 from virusdave/dnicponski/scratch/swap_homedir_check_master

Verify this if `$HOME` exists, it is owned by current user in `getHome()`
This commit is contained in:
Théophane Hufschmitt 2022-06-20 21:17:22 +02:00 committed by GitHub
commit 16c6c6c963
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -574,6 +574,20 @@ Path getHome()
static Path homeDir = []() static Path homeDir = []()
{ {
auto homeDir = getEnv("HOME"); 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) { if (!homeDir) {
std::vector<char> buf(16384); std::vector<char> buf(16384);
struct passwd pwbuf; struct passwd pwbuf;