Skip to content

Conversation

@Explorer09
Copy link
Contributor

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.

@BenBE BenBE added enhancement Extension or improvement to existing feature default change Changes a default setting or default UI object security 👮 Issues with security implications labels Jan 19, 2026
@Explorer09 Explorer09 marked this pull request as draft January 20, 2026 01:41
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).

Also, in Linux (see CAP_DAC_OVERRIDE), the root user can bypass access
controls by default, meaning they can open() a file in write mode
without the write permission bit on file.

Check the owner and the write permission bit on the htoprc file. If the
file's owner is different, or if it doesn't have write permission for
owner, don't write to it on htop's exit.

Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
@Explorer09 Explorer09 marked this pull request as ready for review January 20, 2026 02:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

default change Changes a default setting or default UI object enhancement Extension or improvement to existing feature security 👮 Issues with security implications

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants