Use dup3() instead of dup2().

This is less error prone since dup3() returns an error if old == new.
Sudo guarantees that fds 0-2 are already open.
This commit is contained in:
Todd C. Miller
2019-11-02 10:55:50 -06:00
parent 43df086186
commit 356287557f
4 changed files with 35 additions and 46 deletions

View File

@@ -311,17 +311,10 @@ sudo_askpass(const char *askpass, const char *prompt)
sudo_fatal(U_("unable to fork"));
if (child == 0) {
/* child, set stdout to write side of the pipe or clear FD_CLOEXEC */
if (pfd[1] == STDOUT_FILENO) {
if (fcntl(pfd[1], F_SETFD, 0) == -1) {
sudo_warn("fcntl");
_exit(255);
}
} else {
if (dup2(pfd[1], STDOUT_FILENO) == -1) {
sudo_warn("dup2");
_exit(255);
}
/* child, set stdout to write side of the pipe */
if (dup3(pfd[1], STDOUT_FILENO, 0) == -1) {
sudo_warn("dup3");
_exit(255);
}
if (setuid(ROOT_UID) == -1)
sudo_warn("setuid(%d)", ROOT_UID);