2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-31 06:15:37 +00:00

Fix potential fd leak when converting trailing newline to cr + nl.

Coverity CID 205872
This commit is contained in:
Todd C. Miller
2019-11-19 18:57:22 -07:00
parent b31b830518
commit e0a4b2d68a

View File

@@ -53,7 +53,6 @@ int
sudo_conversation(int num_msgs, const struct sudo_conv_message msgs[],
struct sudo_conv_reply replies[], struct sudo_conv_callback *callback)
{
int ttyfd = -1;
char *pass;
int n;
const int conv_debug_instance = sudo_debug_get_active_instance();
@@ -96,6 +95,8 @@ sudo_conversation(int num_msgs, const struct sudo_conv_message msgs[],
if (msg->msg != NULL) {
size_t len = strlen(msg->msg);
const char *crnl = NULL;
bool written = false;
int ttyfd = -1;
if (ISSET(msg->msg_type, SUDO_CONV_PREFER_TTY))
ttyfd = open(_PATH_TTY, O_WRONLY);
@@ -111,21 +112,23 @@ sudo_conversation(int num_msgs, const struct sudo_conv_message msgs[],
if (ttyfd != -1) {
/* Try writing to tty but fall back to fp on error. */
if ((len == 0 || write(ttyfd, msg->msg, len) != -1) &&
(crnl == NULL || write(ttyfd, crnl, 2) != -1))
break;
(crnl == NULL || write(ttyfd, crnl, 2) != -1)) {
written = true;
}
close(ttyfd);
}
if (!written) {
if (len != 0 && fwrite(msg->msg, 1, len, fp) == 0)
goto err;
if (crnl != NULL && fwrite(crnl, 1, 2, fp) == 0)
goto err;
}
if (len != 0 && fwrite(msg->msg, 1, len, fp) == 0)
goto err;
if (crnl != NULL && fwrite(crnl, 1, 2, fp) == 0)
goto err;
}
break;
default:
goto err;
}
}
if (ttyfd != -1)
close(ttyfd);
sudo_debug_set_active_instance(conv_debug_instance);
return 0;
@@ -142,8 +145,6 @@ err:
repl->reply = NULL;
} while (n--);
}
if (ttyfd != -1)
close(ttyfd);
sudo_debug_set_active_instance(conv_debug_instance);
return -1;