summaryrefslogtreecommitdiff
path: root/channels.c
diff options
context:
space:
mode:
authorderaadt@openbsd.org <deraadt@openbsd.org>2019-06-28 13:35:04 +0000
committerDamien Miller <djm@mindrot.org>2019-07-05 11:10:39 +1000
commit4d28fa78abce2890e136281950633fae2066cc29 (patch)
tree33226ec64ced661bb7e40005e30744b68fa59a80 /channels.c
parente8c974043c1648eab0ad67a7ba6a3e444fe79d2d (diff)
upstream: When system calls indicate an error they return -1, not
some arbitrary value < 0. errno is only updated in this case. Change all (most?) callers of syscalls to follow this better, and let's see if this strictness helps us in the future. OpenBSD-Commit-ID: 48081f00db7518e3b712a49dca06efc2a5428075
Diffstat (limited to 'channels.c')
-rw-r--r--channels.c46
1 files changed, 23 insertions, 23 deletions
diff --git a/channels.c b/channels.c
index 30691c82f..e1c7be81f 100644
--- a/channels.c
+++ b/channels.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: channels.c,v 1.392 2019/06/07 14:18:48 dtucker Exp $ */ 1/* $OpenBSD: channels.c,v 1.393 2019/06/28 13:35:04 deraadt Exp $ */
2/* 2/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -1671,7 +1671,7 @@ channel_post_x11_listener(struct ssh *ssh, Channel *c,
1671 chan_mark_dead(ssh, c); 1671 chan_mark_dead(ssh, c);
1672 errno = oerrno; 1672 errno = oerrno;
1673 } 1673 }
1674 if (newsock < 0) { 1674 if (newsock == -1) {
1675 if (errno != EINTR && errno != EWOULDBLOCK && 1675 if (errno != EINTR && errno != EWOULDBLOCK &&
1676 errno != ECONNABORTED) 1676 errno != ECONNABORTED)
1677 error("accept: %.100s", strerror(errno)); 1677 error("accept: %.100s", strerror(errno));
@@ -1814,7 +1814,7 @@ channel_post_port_listener(struct ssh *ssh, Channel *c,
1814 1814
1815 addrlen = sizeof(addr); 1815 addrlen = sizeof(addr);
1816 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen); 1816 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1817 if (newsock < 0) { 1817 if (newsock == -1) {
1818 if (errno != EINTR && errno != EWOULDBLOCK && 1818 if (errno != EINTR && errno != EWOULDBLOCK &&
1819 errno != ECONNABORTED) 1819 errno != ECONNABORTED)
1820 error("accept: %.100s", strerror(errno)); 1820 error("accept: %.100s", strerror(errno));
@@ -1853,7 +1853,7 @@ channel_post_auth_listener(struct ssh *ssh, Channel *c,
1853 1853
1854 addrlen = sizeof(addr); 1854 addrlen = sizeof(addr);
1855 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen); 1855 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1856 if (newsock < 0) { 1856 if (newsock == -1) {
1857 error("accept from auth socket: %.100s", strerror(errno)); 1857 error("accept from auth socket: %.100s", strerror(errno));
1858 if (errno == EMFILE || errno == ENFILE) 1858 if (errno == EMFILE || errno == ENFILE)
1859 c->notbefore = monotime() + 1; 1859 c->notbefore = monotime() + 1;
@@ -1881,7 +1881,7 @@ channel_post_connecting(struct ssh *ssh, Channel *c,
1881 fatal(":%s: channel %d: no remote id", __func__, c->self); 1881 fatal(":%s: channel %d: no remote id", __func__, c->self);
1882 /* for rdynamic the OPEN_CONFIRMATION has been sent already */ 1882 /* for rdynamic the OPEN_CONFIRMATION has been sent already */
1883 isopen = (c->type == SSH_CHANNEL_RDYNAMIC_FINISH); 1883 isopen = (c->type == SSH_CHANNEL_RDYNAMIC_FINISH);
1884 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) { 1884 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) == -1) {
1885 err = errno; 1885 err = errno;
1886 error("getsockopt SO_ERROR failed"); 1886 error("getsockopt SO_ERROR failed");
1887 } 1887 }
@@ -1956,7 +1956,7 @@ channel_handle_rfd(struct ssh *ssh, Channel *c,
1956 1956
1957 errno = 0; 1957 errno = 0;
1958 len = read(c->rfd, buf, sizeof(buf)); 1958 len = read(c->rfd, buf, sizeof(buf));
1959 if (len < 0 && (errno == EINTR || 1959 if (len == -1 && (errno == EINTR ||
1960 ((errno == EAGAIN || errno == EWOULDBLOCK) && !force))) 1960 ((errno == EAGAIN || errno == EWOULDBLOCK) && !force)))
1961 return 1; 1961 return 1;
1962#ifndef PTY_ZEROREAD 1962#ifndef PTY_ZEROREAD
@@ -2030,7 +2030,7 @@ channel_handle_wfd(struct ssh *ssh, Channel *c,
2030 /* ignore truncated writes, datagrams might get lost */ 2030 /* ignore truncated writes, datagrams might get lost */
2031 len = write(c->wfd, buf, dlen); 2031 len = write(c->wfd, buf, dlen);
2032 free(data); 2032 free(data);
2033 if (len < 0 && (errno == EINTR || errno == EAGAIN || 2033 if (len == -1 && (errno == EINTR || errno == EAGAIN ||
2034 errno == EWOULDBLOCK)) 2034 errno == EWOULDBLOCK))
2035 return 1; 2035 return 1;
2036 if (len <= 0) 2036 if (len <= 0)
@@ -2045,7 +2045,7 @@ channel_handle_wfd(struct ssh *ssh, Channel *c,
2045#endif 2045#endif
2046 2046
2047 len = write(c->wfd, buf, dlen); 2047 len = write(c->wfd, buf, dlen);
2048 if (len < 0 && 2048 if (len == -1 &&
2049 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)) 2049 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
2050 return 1; 2050 return 1;
2051 if (len <= 0) { 2051 if (len <= 0) {
@@ -2099,7 +2099,7 @@ channel_handle_efd_write(struct ssh *ssh, Channel *c,
2099 len = write(c->efd, sshbuf_ptr(c->extended), 2099 len = write(c->efd, sshbuf_ptr(c->extended),
2100 sshbuf_len(c->extended)); 2100 sshbuf_len(c->extended));
2101 debug2("channel %d: written %zd to efd %d", c->self, len, c->efd); 2101 debug2("channel %d: written %zd to efd %d", c->self, len, c->efd);
2102 if (len < 0 && (errno == EINTR || errno == EAGAIN || 2102 if (len == -1 && (errno == EINTR || errno == EAGAIN ||
2103 errno == EWOULDBLOCK)) 2103 errno == EWOULDBLOCK))
2104 return 1; 2104 return 1;
2105 if (len <= 0) { 2105 if (len <= 0) {
@@ -2130,7 +2130,7 @@ channel_handle_efd_read(struct ssh *ssh, Channel *c,
2130 2130
2131 len = read(c->efd, buf, sizeof(buf)); 2131 len = read(c->efd, buf, sizeof(buf));
2132 debug2("channel %d: read %zd from efd %d", c->self, len, c->efd); 2132 debug2("channel %d: read %zd from efd %d", c->self, len, c->efd);
2133 if (len < 0 && (errno == EINTR || ((errno == EAGAIN || 2133 if (len == -1 && (errno == EINTR || ((errno == EAGAIN ||
2134 errno == EWOULDBLOCK) && !force))) 2134 errno == EWOULDBLOCK) && !force)))
2135 return 1; 2135 return 1;
2136 if (len <= 0) { 2136 if (len <= 0) {
@@ -2219,7 +2219,7 @@ read_mux(struct ssh *ssh, Channel *c, u_int need)
2219 if (sshbuf_len(c->input) < need) { 2219 if (sshbuf_len(c->input) < need) {
2220 rlen = need - sshbuf_len(c->input); 2220 rlen = need - sshbuf_len(c->input);
2221 len = read(c->rfd, buf, MINIMUM(rlen, CHAN_RBUF)); 2221 len = read(c->rfd, buf, MINIMUM(rlen, CHAN_RBUF));
2222 if (len < 0 && (errno == EINTR || errno == EAGAIN)) 2222 if (len == -1 && (errno == EINTR || errno == EAGAIN))
2223 return sshbuf_len(c->input); 2223 return sshbuf_len(c->input);
2224 if (len <= 0) { 2224 if (len <= 0) {
2225 debug2("channel %d: ctl read<=0 rfd %d len %zd", 2225 debug2("channel %d: ctl read<=0 rfd %d len %zd",
@@ -2283,7 +2283,7 @@ channel_post_mux_client_write(struct ssh *ssh, Channel *c,
2283 return; 2283 return;
2284 2284
2285 len = write(c->wfd, sshbuf_ptr(c->output), sshbuf_len(c->output)); 2285 len = write(c->wfd, sshbuf_ptr(c->output), sshbuf_len(c->output));
2286 if (len < 0 && (errno == EINTR || errno == EAGAIN)) 2286 if (len == -1 && (errno == EINTR || errno == EAGAIN))
2287 return; 2287 return;
2288 if (len <= 0) { 2288 if (len <= 0) {
2289 chan_mark_dead(ssh, c); 2289 chan_mark_dead(ssh, c);
@@ -2331,7 +2331,7 @@ channel_post_mux_listener(struct ssh *ssh, Channel *c,
2331 return; 2331 return;
2332 } 2332 }
2333 2333
2334 if (getpeereid(newsock, &euid, &egid) < 0) { 2334 if (getpeereid(newsock, &euid, &egid) == -1) {
2335 error("%s getpeereid failed: %s", __func__, 2335 error("%s getpeereid failed: %s", __func__,
2336 strerror(errno)); 2336 strerror(errno));
2337 close(newsock); 2337 close(newsock);
@@ -3461,7 +3461,7 @@ channel_setup_fwd_listener_tcpip(struct ssh *ssh, int type,
3461 } 3461 }
3462 /* Create a port to listen for the host. */ 3462 /* Create a port to listen for the host. */
3463 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); 3463 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
3464 if (sock < 0) { 3464 if (sock == -1) {
3465 /* this is no error since kernel may not support ipv6 */ 3465 /* this is no error since kernel may not support ipv6 */
3466 verbose("socket [%s]:%s: %.100s", ntop, strport, 3466 verbose("socket [%s]:%s: %.100s", ntop, strport,
3467 strerror(errno)); 3467 strerror(errno));
@@ -3476,7 +3476,7 @@ channel_setup_fwd_listener_tcpip(struct ssh *ssh, int type,
3476 ntop, strport); 3476 ntop, strport);
3477 3477
3478 /* Bind the socket to the address. */ 3478 /* Bind the socket to the address. */
3479 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { 3479 if (bind(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
3480 /* 3480 /*
3481 * address can be in if use ipv6 address is 3481 * address can be in if use ipv6 address is
3482 * already bound 3482 * already bound
@@ -3492,7 +3492,7 @@ channel_setup_fwd_listener_tcpip(struct ssh *ssh, int type,
3492 continue; 3492 continue;
3493 } 3493 }
3494 /* Start listening for connections on the socket. */ 3494 /* Start listening for connections on the socket. */
3495 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) { 3495 if (listen(sock, SSH_LISTEN_BACKLOG) == -1) {
3496 error("listen: %.100s", strerror(errno)); 3496 error("listen: %.100s", strerror(errno));
3497 error("listen [%s]:%s: %.100s", ntop, strport, 3497 error("listen [%s]:%s: %.100s", ntop, strport,
3498 strerror(errno)); 3498 strerror(errno));
@@ -4512,7 +4512,7 @@ channel_send_window_changes(struct ssh *ssh)
4512 if (sc->channels[i] == NULL || !sc->channels[i]->client_tty || 4512 if (sc->channels[i] == NULL || !sc->channels[i]->client_tty ||
4513 sc->channels[i]->type != SSH_CHANNEL_OPEN) 4513 sc->channels[i]->type != SSH_CHANNEL_OPEN)
4514 continue; 4514 continue;
4515 if (ioctl(sc->channels[i]->rfd, TIOCGWINSZ, &ws) < 0) 4515 if (ioctl(sc->channels[i]->rfd, TIOCGWINSZ, &ws) == -1)
4516 continue; 4516 continue;
4517 channel_request_start(ssh, i, "window-change", 0); 4517 channel_request_start(ssh, i, "window-change", 0);
4518 if ((r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 || 4518 if ((r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 ||
@@ -4615,7 +4615,7 @@ x11_create_display_inet(struct ssh *ssh, int x11_display_offset,
4615 continue; 4615 continue;
4616 sock = socket(ai->ai_family, ai->ai_socktype, 4616 sock = socket(ai->ai_family, ai->ai_socktype,
4617 ai->ai_protocol); 4617 ai->ai_protocol);
4618 if (sock < 0) { 4618 if (sock == -1) {
4619 if ((errno != EINVAL) && (errno != EAFNOSUPPORT) 4619 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)
4620#ifdef EPFNOSUPPORT 4620#ifdef EPFNOSUPPORT
4621 && (errno != EPFNOSUPPORT) 4621 && (errno != EPFNOSUPPORT)
@@ -4634,7 +4634,7 @@ x11_create_display_inet(struct ssh *ssh, int x11_display_offset,
4634 sock_set_v6only(sock); 4634 sock_set_v6only(sock);
4635 if (x11_use_localhost) 4635 if (x11_use_localhost)
4636 set_reuseaddr(sock); 4636 set_reuseaddr(sock);
4637 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { 4637 if (bind(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
4638 debug2("%s: bind port %d: %.100s", __func__, 4638 debug2("%s: bind port %d: %.100s", __func__,
4639 port, strerror(errno)); 4639 port, strerror(errno));
4640 close(sock); 4640 close(sock);
@@ -4658,7 +4658,7 @@ x11_create_display_inet(struct ssh *ssh, int x11_display_offset,
4658 /* Start listening for connections on the socket. */ 4658 /* Start listening for connections on the socket. */
4659 for (n = 0; n < num_socks; n++) { 4659 for (n = 0; n < num_socks; n++) {
4660 sock = socks[n]; 4660 sock = socks[n];
4661 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) { 4661 if (listen(sock, SSH_LISTEN_BACKLOG) == -1) {
4662 error("listen: %.100s", strerror(errno)); 4662 error("listen: %.100s", strerror(errno));
4663 close(sock); 4663 close(sock);
4664 return -1; 4664 return -1;
@@ -4690,7 +4690,7 @@ connect_local_xsocket_path(const char *pathname)
4690 struct sockaddr_un addr; 4690 struct sockaddr_un addr;
4691 4691
4692 sock = socket(AF_UNIX, SOCK_STREAM, 0); 4692 sock = socket(AF_UNIX, SOCK_STREAM, 0);
4693 if (sock < 0) 4693 if (sock == -1)
4694 error("socket: %.100s", strerror(errno)); 4694 error("socket: %.100s", strerror(errno));
4695 memset(&addr, 0, sizeof(addr)); 4695 memset(&addr, 0, sizeof(addr));
4696 addr.sun_family = AF_UNIX; 4696 addr.sun_family = AF_UNIX;
@@ -4831,12 +4831,12 @@ x11_connect_display(struct ssh *ssh)
4831 for (ai = aitop; ai; ai = ai->ai_next) { 4831 for (ai = aitop; ai; ai = ai->ai_next) {
4832 /* Create a socket. */ 4832 /* Create a socket. */
4833 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); 4833 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
4834 if (sock < 0) { 4834 if (sock == -1) {
4835 debug2("socket: %.100s", strerror(errno)); 4835 debug2("socket: %.100s", strerror(errno));
4836 continue; 4836 continue;
4837 } 4837 }
4838 /* Connect it to the display. */ 4838 /* Connect it to the display. */
4839 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) { 4839 if (connect(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
4840 debug2("connect %.100s port %u: %.100s", buf, 4840 debug2("connect %.100s port %u: %.100s", buf,
4841 6000 + display_number, strerror(errno)); 4841 6000 + display_number, strerror(errno));
4842 close(sock); 4842 close(sock);