=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/ssh/mux.c,v retrieving revision 1.98 retrieving revision 1.99 diff -u -r1.98 -r1.99 --- src/usr.bin/ssh/mux.c 2023/07/26 23:06:00 1.98 +++ src/usr.bin/ssh/mux.c 2023/08/04 06:32:40 1.99 @@ -1,4 +1,4 @@ -/* $OpenBSD: mux.c,v 1.98 2023/07/26 23:06:00 djm Exp $ */ +/* $OpenBSD: mux.c,v 1.99 2023/08/04 06:32:40 dtucker Exp $ */ /* * Copyright (c) 2002-2008 Damien Miller * @@ -55,6 +55,7 @@ #include "readconf.h" #include "clientloop.h" #include "ssherr.h" +#include "misc.h" /* from ssh.c */ extern int tty_flag; @@ -1445,16 +1446,13 @@ } static int -mux_client_read(int fd, struct sshbuf *b, size_t need) +mux_client_read(int fd, struct sshbuf *b, size_t need, int timeout_ms) { size_t have; ssize_t len; u_char *p; - struct pollfd pfd; int r; - pfd.fd = fd; - pfd.events = POLLIN; if ((r = sshbuf_reserve(b, need, &p)) != 0) fatal_fr(r, "reserve"); for (have = 0; have < need; ) { @@ -1466,7 +1464,8 @@ if (len == -1) { switch (errno) { case EAGAIN: - (void)poll(&pfd, 1, -1); + if (waitrfd(fd, &timeout_ms) == -1) + return -1; /* timeout */ /* FALLTHROUGH */ case EINTR: continue; @@ -1535,7 +1534,7 @@ } static int -mux_client_read_packet(int fd, struct sshbuf *m) +mux_client_read_packet_timeout(int fd, struct sshbuf *m, int timeout_ms) { struct sshbuf *queue; size_t need, have; @@ -1544,7 +1543,7 @@ if ((queue = sshbuf_new()) == NULL) fatal_f("sshbuf_new"); - if (mux_client_read(fd, queue, 4) != 0) { + if (mux_client_read(fd, queue, 4, timeout_ms) != 0) { if ((oerrno = errno) == EPIPE) debug3_f("read header failed: %s", strerror(errno)); @@ -1553,7 +1552,7 @@ return -1; } need = PEEK_U32(sshbuf_ptr(queue)); - if (mux_client_read(fd, queue, need) != 0) { + if (mux_client_read(fd, queue, need, timeout_ms) != 0) { oerrno = errno; debug3_f("read body failed: %s", strerror(errno)); sshbuf_free(queue); @@ -1568,8 +1567,14 @@ } static int -mux_client_hello_exchange(int fd) +mux_client_read_packet(int fd, struct sshbuf *m) { + return mux_client_read_packet_timeout(fd, m, -1); +} + +static int +mux_client_hello_exchange(int fd, int timeout_ms) +{ struct sshbuf *m; u_int type, ver; int r, ret = -1; @@ -1589,7 +1594,7 @@ sshbuf_reset(m); /* Read their HELLO */ - if (mux_client_read_packet(fd, m) != 0) { + if (mux_client_read_packet_timeout(fd, m, timeout_ms) != 0) { debug_f("read packet failed"); goto out; } @@ -2237,7 +2242,7 @@ muxclient(const char *path) { struct sockaddr_un addr; - int sock; + int sock, timeout = options.connection_timeout, timeout_ms = -1; u_int pid; if (muxclient_command == 0) { @@ -2293,7 +2298,11 @@ } set_nonblock(sock); - if (mux_client_hello_exchange(sock) != 0) { + /* Timeout on initial connection only. */ + if (timeout > 0 && timeout < INT_MAX / 1000) + timeout_ms = timeout * 1000; + + if (mux_client_hello_exchange(sock, timeout_ms) != 0) { error_f("master hello exchange failed"); close(sock); return -1;