Skip to content

Commit 663e84b

Browse files
committed
upstream: make failures when establishing "Tunnel" forwarding terminate
the connection when ExitOnForwardFailure is enabled; bz3116; ok dtucker OpenBSD-Commit-ID: ef4b4808de0a419c17579b1081da768625c1d735
1 parent ed833da commit 663e84b

File tree

3 files changed

+47
-27
lines changed

3 files changed

+47
-27
lines changed

clientloop.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: clientloop.c,v 1.342 2020/02/26 13:40:09 jsg Exp $ */
1+
/* $OpenBSD: clientloop.c,v 1.343 2020/04/03 02:40:32 djm Exp $ */
22
/*
33
* Author: Tatu Ylonen <[email protected]>
44
* Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland
@@ -1645,7 +1645,7 @@ client_request_agent(struct ssh *ssh, const char *request_type, int rchan)
16451645

16461646
char *
16471647
client_request_tun_fwd(struct ssh *ssh, int tun_mode,
1648-
int local_tun, int remote_tun)
1648+
int local_tun, int remote_tun, channel_open_fn *cb, void *cbctx)
16491649
{
16501650
Channel *c;
16511651
int r, fd;
@@ -1673,6 +1673,9 @@ client_request_tun_fwd(struct ssh *ssh, int tun_mode,
16731673
sys_tun_outfilter, NULL, NULL);
16741674
#endif
16751675

1676+
if (cb != NULL)
1677+
channel_register_open_confirm(ssh, c->self, cb, cbctx);
1678+
16761679
if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
16771680
(r = sshpkt_put_cstring(ssh, "[email protected]")) != 0 ||
16781681
(r = sshpkt_put_u32(ssh, c->self)) != 0 ||

clientloop.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: clientloop.h,v 1.36 2018/07/09 21:03:30 markus Exp $ */
1+
/* $OpenBSD: clientloop.h,v 1.37 2020/04/03 02:40:32 djm Exp $ */
22

33
/*
44
* Author: Tatu Ylonen <[email protected]>
@@ -46,7 +46,8 @@ int client_x11_get_proto(struct ssh *, const char *, const char *,
4646
void client_global_request_reply_fwd(int, u_int32_t, void *);
4747
void client_session2_setup(struct ssh *, int, int, int,
4848
const char *, struct termios *, int, struct sshbuf *, char **);
49-
char *client_request_tun_fwd(struct ssh *, int, int, int);
49+
char *client_request_tun_fwd(struct ssh *, int, int, int,
50+
channel_open_fn *, void *);
5051
void client_stop_mux(void);
5152

5253
/* Escape filter for protocol 2 sessions */

ssh.c

+39-23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: ssh.c,v 1.522 2020/04/03 02:27:12 dtucker Exp $ */
1+
/* $OpenBSD: ssh.c,v 1.523 2020/04/03 02:40:32 djm Exp $ */
22
/*
33
* Author: Tatu Ylonen <[email protected]>
44
* Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland
@@ -197,7 +197,7 @@ struct sshbuf *command;
197197
int subsystem_flag = 0;
198198

199199
/* # of replies received for global requests */
200-
static int remote_forward_confirms_received = 0;
200+
static int forward_confirms_pending = -1;
201201

202202
/* mux.c */
203203
extern int muxserver_sock;
@@ -1673,6 +1673,16 @@ fork_postauth(void)
16731673
fatal("daemon() failed: %.200s", strerror(errno));
16741674
}
16751675

1676+
static void
1677+
forwarding_success(void)
1678+
{
1679+
if (forward_confirms_pending > 0 && --forward_confirms_pending == 0) {
1680+
debug("All forwarding requests processed");
1681+
if (fork_after_authentication_flag)
1682+
fork_postauth();
1683+
}
1684+
}
1685+
16761686
/* Callback for remote forward global requests */
16771687
static void
16781688
ssh_confirm_remote_forward(struct ssh *ssh, int type, u_int32_t seq, void *ctxt)
@@ -1732,11 +1742,7 @@ ssh_confirm_remote_forward(struct ssh *ssh, int type, u_int32_t seq, void *ctxt)
17321742
"for listen port %d", rfwd->listen_port);
17331743
}
17341744
}
1735-
if (++remote_forward_confirms_received == options.num_remote_forwards) {
1736-
debug("All remote forwarding requests processed");
1737-
if (fork_after_authentication_flag)
1738-
fork_postauth();
1739-
}
1745+
forwarding_success();
17401746
}
17411747

17421748
static void
@@ -1753,6 +1759,19 @@ ssh_stdio_confirm(struct ssh *ssh, int id, int success, void *arg)
17531759
fatal("stdio forwarding failed");
17541760
}
17551761

1762+
static void
1763+
ssh_tun_confirm(struct ssh *ssh, int id, int success, void *arg)
1764+
{
1765+
if (!success) {
1766+
error("Tunnel forwarding failed");
1767+
if (options.exit_on_forward_failure)
1768+
cleanup_exit(255);
1769+
}
1770+
1771+
debug("%s: tunnel forward established, id=%d", __func__, id);
1772+
forwarding_success();
1773+
}
1774+
17561775
static void
17571776
ssh_init_stdio_forwarding(struct ssh *ssh)
17581777
{
@@ -1816,32 +1835,29 @@ ssh_init_forwarding(struct ssh *ssh, char **ifname)
18161835
options.remote_forwards[i].connect_path :
18171836
options.remote_forwards[i].connect_host,
18181837
options.remote_forwards[i].connect_port);
1819-
options.remote_forwards[i].handle =
1838+
if ((options.remote_forwards[i].handle =
18201839
channel_request_remote_forwarding(ssh,
1821-
&options.remote_forwards[i]);
1822-
if (options.remote_forwards[i].handle < 0) {
1823-
if (options.exit_on_forward_failure)
1824-
fatal("Could not request remote forwarding.");
1825-
else
1826-
logit("Warning: Could not request remote "
1827-
"forwarding.");
1828-
} else {
1840+
&options.remote_forwards[i])) >= 0) {
18291841
client_register_global_confirm(
18301842
ssh_confirm_remote_forward,
18311843
&options.remote_forwards[i]);
1832-
}
1844+
forward_confirms_pending++;
1845+
} else if (options.exit_on_forward_failure)
1846+
fatal("Could not request remote forwarding.");
1847+
else
1848+
logit("Warning: Could not request remote forwarding.");
18331849
}
18341850

18351851
/* Initiate tunnel forwarding. */
18361852
if (options.tun_open != SSH_TUNMODE_NO) {
18371853
if ((*ifname = client_request_tun_fwd(ssh,
18381854
options.tun_open, options.tun_local,
1839-
options.tun_remote)) == NULL) {
1840-
if (options.exit_on_forward_failure)
1841-
fatal("Could not request tunnel forwarding.");
1842-
else
1843-
error("Could not request tunnel forwarding.");
1844-
}
1855+
options.tun_remote, ssh_tun_confirm, NULL)) != NULL)
1856+
forward_confirms_pending++;
1857+
else if (options.exit_on_forward_failure)
1858+
fatal("Could not request tunnel forwarding.");
1859+
else
1860+
error("Could not request tunnel forwarding.");
18451861
}
18461862
}
18471863

0 commit comments

Comments
 (0)