Skip to content

Commit 07238f1

Browse files
committed
Don't write to htoprc file if it's not owned by EUID
Because htop writes the new settings in an "atomic" fashion (that is, create a temp file, write content and then rename the temp file to the final name, replacing the old one), the new htoprc file could be owned by a user that's different from the original. This can cause the original user to not be able to access the htoprc file again. This scenario can happen when htop is run with elevated privileges. In Linux, this occurs when htop is run with SUID (`chmod u+s htop`). In macOS/Darwin, this occurs when htop is run with sudo (`sudo htop`) with the default sudoers configuration (specifically, with this `env_keep += "HOME"` line, which is discouraged by sudo upstream). Don't assume the htoprc file opened will be owned by the same effective user ID. If the file's owner is different, don't write to it on htop's exit. Signed-off-by: Kang-Che Sung <[email protected]>
1 parent 70f873e commit 07238f1

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

Settings.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,12 @@ static bool Settings_read(Settings* this, const char* fileName, const Machine* h
331331
return false;
332332
}
333333
} else {
334-
// Check if this is a regular file
334+
// Write the config only if the file is:
335+
// (1) a regular file (not a device file like /dev/null), and
336+
// (2) owned by the effective user ID
335337
struct stat sb;
336338
int err = fstat(fd, &sb);
337-
this->writeConfig = !err && S_ISREG(sb.st_mode);
339+
this->writeConfig = !err && S_ISREG(sb.st_mode) && sb.st_uid == geteuid();
338340
}
339341
}
340342

0 commit comments

Comments
 (0)