add some security checks when running SUID root

on Darwin, htop needs to run with root privileges to display information
about other users processes. This commit makes running htop SUID root a
bit more safe.
This commit is contained in:
Michael Klein 2015-12-02 22:15:46 +01:00
parent 670a2de692
commit d18e9a4895
2 changed files with 12 additions and 6 deletions

View File

@ -513,6 +513,7 @@ void Process_toggleTag(Process* this) {
} }
bool Process_setPriority(Process* this, int priority) { bool Process_setPriority(Process* this, int priority) {
if ( Process_getuid == 0 || Process_getuid == (int) this->st_uid ) {
int old_prio = getpriority(PRIO_PROCESS, this->pid); int old_prio = getpriority(PRIO_PROCESS, this->pid);
int err = setpriority(PRIO_PROCESS, this->pid, priority); int err = setpriority(PRIO_PROCESS, this->pid, priority);
if (err == 0 && old_prio != getpriority(PRIO_PROCESS, this->pid)) { if (err == 0 && old_prio != getpriority(PRIO_PROCESS, this->pid)) {
@ -520,12 +521,16 @@ bool Process_setPriority(Process* this, int priority) {
} }
return (err == 0); return (err == 0);
} }
else
return false;
}
bool Process_changePriorityBy(Process* this, size_t delta) { bool Process_changePriorityBy(Process* this, size_t delta) {
return Process_setPriority(this, this->nice + delta); return Process_setPriority(this, this->nice + delta);
} }
void Process_sendSignal(Process* this, size_t sgn) { void Process_sendSignal(Process* this, size_t sgn) {
if ( Process_getuid == 0 || Process_getuid == (int) this->st_uid )
kill(this->pid, (int) sgn); kill(this->pid, (int) sgn);
} }

View File

@ -86,6 +86,7 @@ void TraceScreen_run(TraceScreen* this) {
int child = fork(); int child = fork();
if (child == -1) return; if (child == -1) return;
if (child == 0) { if (child == 0) {
seteuid(getuid());
dup2(fdpair[1], STDERR_FILENO); dup2(fdpair[1], STDERR_FILENO);
int ok = fcntl(fdpair[1], F_SETFL, O_NONBLOCK); int ok = fcntl(fdpair[1], F_SETFL, O_NONBLOCK);
if (ok != -1) { if (ok != -1) {