aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThéophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com>2022-06-20 21:17:22 +0200
committerGitHub <noreply@github.com>2022-06-20 21:17:22 +0200
commit16c6c6c963e78aff1911054fe4145b891d8e9fc8 (patch)
tree054780609089fc93b18f86198ab8544fbed1d010
parent7e301fd74e929af916116a04d84e130f9e0112de (diff)
parentca2be509b96a10a2035039a825fc2b292ec0ad4d (diff)
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()`
-rw-r--r--src/libutil/util.cc14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 1c19938a8..a368ac844 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -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;