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