idx
int64 | func_before
string | Vulnerability Classification
string | vul
int64 | func_after
string | patch
string | CWE ID
string | lines_before
string | lines_after
string |
|---|---|---|---|---|---|---|---|---|
6,800
|
static struct Packet *ssh1_pkt_init(int pkt_type)
{
struct Packet *pkt = ssh_new_packet();
pkt->length = 4 + 8; /* space for length + max padding */
ssh_pkt_addbyte(pkt, pkt_type);
pkt->body = pkt->data + pkt->length;
pkt->type = pkt_type;
pkt->downstream_id = 0;
pkt->additional_log_text = NULL;
return pkt;
}
|
Overflow
| 0
|
static struct Packet *ssh1_pkt_init(int pkt_type)
{
struct Packet *pkt = ssh_new_packet();
pkt->length = 4 + 8; /* space for length + max padding */
ssh_pkt_addbyte(pkt, pkt_type);
pkt->body = pkt->data + pkt->length;
pkt->type = pkt_type;
pkt->downstream_id = 0;
pkt->additional_log_text = NULL;
return pkt;
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,801
|
static struct Packet *ssh1_rdpkt(Ssh ssh, const unsigned char **data,
int *datalen)
{
struct rdpkt1_state_tag *st = &ssh->rdpkt1_state;
crBegin(ssh->ssh1_rdpkt_crstate);
st->pktin = ssh_new_packet();
st->pktin->type = 0;
st->pktin->length = 0;
for (st->i = st->len = 0; st->i < 4; st->i++) {
while ((*datalen) == 0)
crReturn(NULL);
st->len = (st->len << 8) + **data;
(*data)++, (*datalen)--;
}
st->pad = 8 - (st->len % 8);
st->biglen = st->len + st->pad;
st->pktin->length = st->len - 5;
if (st->biglen < 0) {
bombout(("Extremely large packet length from server suggests"
" data stream corruption"));
ssh_free_packet(st->pktin);
crStop(NULL);
}
st->pktin->maxlen = st->biglen;
st->pktin->data = snewn(st->biglen + APIEXTRA, unsigned char);
st->to_read = st->biglen;
st->p = st->pktin->data;
while (st->to_read > 0) {
st->chunk = st->to_read;
while ((*datalen) == 0)
crReturn(NULL);
if (st->chunk > (*datalen))
st->chunk = (*datalen);
memcpy(st->p, *data, st->chunk);
*data += st->chunk;
*datalen -= st->chunk;
st->p += st->chunk;
st->to_read -= st->chunk;
}
if (ssh->cipher && detect_attack(ssh->crcda_ctx, st->pktin->data,
st->biglen, NULL)) {
bombout(("Network attack (CRC compensation) detected!"));
ssh_free_packet(st->pktin);
crStop(NULL);
}
if (ssh->cipher)
ssh->cipher->decrypt(ssh->v1_cipher_ctx, st->pktin->data, st->biglen);
st->realcrc = crc32_compute(st->pktin->data, st->biglen - 4);
st->gotcrc = GET_32BIT(st->pktin->data + st->biglen - 4);
if (st->gotcrc != st->realcrc) {
bombout(("Incorrect CRC received on packet"));
ssh_free_packet(st->pktin);
crStop(NULL);
}
st->pktin->body = st->pktin->data + st->pad + 1;
if (ssh->v1_compressing) {
unsigned char *decompblk;
int decomplen;
if (!zlib_decompress_block(ssh->sc_comp_ctx,
st->pktin->body - 1, st->pktin->length + 1,
&decompblk, &decomplen)) {
bombout(("Zlib decompression encountered invalid data"));
ssh_free_packet(st->pktin);
crStop(NULL);
}
if (st->pktin->maxlen < st->pad + decomplen) {
st->pktin->maxlen = st->pad + decomplen;
st->pktin->data = sresize(st->pktin->data,
st->pktin->maxlen + APIEXTRA,
unsigned char);
st->pktin->body = st->pktin->data + st->pad + 1;
}
memcpy(st->pktin->body - 1, decompblk, decomplen);
sfree(decompblk);
st->pktin->length = decomplen - 1;
}
st->pktin->type = st->pktin->body[-1];
/*
* Now pktin->body and pktin->length identify the semantic content
* of the packet, excluding the initial type byte.
*/
if (ssh->logctx)
ssh1_log_incoming_packet(ssh, st->pktin);
st->pktin->savedpos = 0;
crFinish(st->pktin);
}
|
Overflow
| 0
|
static struct Packet *ssh1_rdpkt(Ssh ssh, const unsigned char **data,
int *datalen)
{
struct rdpkt1_state_tag *st = &ssh->rdpkt1_state;
crBegin(ssh->ssh1_rdpkt_crstate);
st->pktin = ssh_new_packet();
st->pktin->type = 0;
st->pktin->length = 0;
for (st->i = st->len = 0; st->i < 4; st->i++) {
while ((*datalen) == 0)
crReturn(NULL);
st->len = (st->len << 8) + **data;
(*data)++, (*datalen)--;
}
st->pad = 8 - (st->len % 8);
st->biglen = st->len + st->pad;
st->pktin->length = st->len - 5;
if (st->biglen < 0) {
bombout(("Extremely large packet length from server suggests"
" data stream corruption"));
ssh_free_packet(st->pktin);
crStop(NULL);
}
st->pktin->maxlen = st->biglen;
st->pktin->data = snewn(st->biglen + APIEXTRA, unsigned char);
st->to_read = st->biglen;
st->p = st->pktin->data;
while (st->to_read > 0) {
st->chunk = st->to_read;
while ((*datalen) == 0)
crReturn(NULL);
if (st->chunk > (*datalen))
st->chunk = (*datalen);
memcpy(st->p, *data, st->chunk);
*data += st->chunk;
*datalen -= st->chunk;
st->p += st->chunk;
st->to_read -= st->chunk;
}
if (ssh->cipher && detect_attack(ssh->crcda_ctx, st->pktin->data,
st->biglen, NULL)) {
bombout(("Network attack (CRC compensation) detected!"));
ssh_free_packet(st->pktin);
crStop(NULL);
}
if (ssh->cipher)
ssh->cipher->decrypt(ssh->v1_cipher_ctx, st->pktin->data, st->biglen);
st->realcrc = crc32_compute(st->pktin->data, st->biglen - 4);
st->gotcrc = GET_32BIT(st->pktin->data + st->biglen - 4);
if (st->gotcrc != st->realcrc) {
bombout(("Incorrect CRC received on packet"));
ssh_free_packet(st->pktin);
crStop(NULL);
}
st->pktin->body = st->pktin->data + st->pad + 1;
if (ssh->v1_compressing) {
unsigned char *decompblk;
int decomplen;
if (!zlib_decompress_block(ssh->sc_comp_ctx,
st->pktin->body - 1, st->pktin->length + 1,
&decompblk, &decomplen)) {
bombout(("Zlib decompression encountered invalid data"));
ssh_free_packet(st->pktin);
crStop(NULL);
}
if (st->pktin->maxlen < st->pad + decomplen) {
st->pktin->maxlen = st->pad + decomplen;
st->pktin->data = sresize(st->pktin->data,
st->pktin->maxlen + APIEXTRA,
unsigned char);
st->pktin->body = st->pktin->data + st->pad + 1;
}
memcpy(st->pktin->body - 1, decompblk, decomplen);
sfree(decompblk);
st->pktin->length = decomplen - 1;
}
st->pktin->type = st->pktin->body[-1];
/*
* Now pktin->body and pktin->length identify the semantic content
* of the packet, excluding the initial type byte.
*/
if (ssh->logctx)
ssh1_log_incoming_packet(ssh, st->pktin);
st->pktin->savedpos = 0;
crFinish(st->pktin);
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,802
|
static void ssh2_add_sigblob(Ssh ssh, struct Packet *pkt,
void *pkblob_v, int pkblob_len,
void *sigblob_v, int sigblob_len)
{
unsigned char *pkblob = (unsigned char *)pkblob_v;
unsigned char *sigblob = (unsigned char *)sigblob_v;
/* dmemdump(pkblob, pkblob_len); */
/* dmemdump(sigblob, sigblob_len); */
/*
* See if this is in fact an ssh-rsa signature and a buggy
* server; otherwise we can just do this the easy way.
*/
if ((ssh->remote_bugs & BUG_SSH2_RSA_PADDING) && pkblob_len > 4+7+4 &&
(GET_32BIT(pkblob) == 7 && !memcmp(pkblob+4, "ssh-rsa", 7))) {
int pos, len, siglen;
/*
* Find the byte length of the modulus.
*/
pos = 4+7; /* skip over "ssh-rsa" */
len = toint(GET_32BIT(pkblob+pos)); /* get length of exponent */
if (len < 0 || len > pkblob_len - pos - 4)
goto give_up;
pos += 4 + len; /* skip over exponent */
if (pkblob_len - pos < 4)
goto give_up;
len = toint(GET_32BIT(pkblob+pos)); /* find length of modulus */
if (len < 0 || len > pkblob_len - pos - 4)
goto give_up;
pos += 4; /* find modulus itself */
while (len > 0 && pkblob[pos] == 0)
len--, pos++;
/* debug(("modulus length is %d\n", len)); */
/*
* Now find the signature integer.
*/
pos = 4+7; /* skip over "ssh-rsa" */
if (sigblob_len < pos+4)
goto give_up;
siglen = toint(GET_32BIT(sigblob+pos));
if (siglen != sigblob_len - pos - 4)
goto give_up;
/* debug(("signature length is %d\n", siglen)); */
if (len != siglen) {
unsigned char newlen[4];
ssh2_pkt_addstring_start(pkt);
ssh2_pkt_addstring_data(pkt, (char *)sigblob, pos);
/* dmemdump(sigblob, pos); */
pos += 4; /* point to start of actual sig */
PUT_32BIT(newlen, len);
ssh2_pkt_addstring_data(pkt, (char *)newlen, 4);
/* dmemdump(newlen, 4); */
newlen[0] = 0;
while (len-- > siglen) {
ssh2_pkt_addstring_data(pkt, (char *)newlen, 1);
/* dmemdump(newlen, 1); */
}
ssh2_pkt_addstring_data(pkt, (char *)(sigblob+pos), siglen);
/* dmemdump(sigblob+pos, siglen); */
return;
}
/* Otherwise fall through and do it the easy way. We also come
* here as a fallback if we discover above that the key blob
* is misformatted in some way. */
give_up:;
}
ssh2_pkt_addstring_start(pkt);
ssh2_pkt_addstring_data(pkt, (char *)sigblob, sigblob_len);
}
|
Overflow
| 0
|
static void ssh2_add_sigblob(Ssh ssh, struct Packet *pkt,
void *pkblob_v, int pkblob_len,
void *sigblob_v, int sigblob_len)
{
unsigned char *pkblob = (unsigned char *)pkblob_v;
unsigned char *sigblob = (unsigned char *)sigblob_v;
/* dmemdump(pkblob, pkblob_len); */
/* dmemdump(sigblob, sigblob_len); */
/*
* See if this is in fact an ssh-rsa signature and a buggy
* server; otherwise we can just do this the easy way.
*/
if ((ssh->remote_bugs & BUG_SSH2_RSA_PADDING) && pkblob_len > 4+7+4 &&
(GET_32BIT(pkblob) == 7 && !memcmp(pkblob+4, "ssh-rsa", 7))) {
int pos, len, siglen;
/*
* Find the byte length of the modulus.
*/
pos = 4+7; /* skip over "ssh-rsa" */
len = toint(GET_32BIT(pkblob+pos)); /* get length of exponent */
if (len < 0 || len > pkblob_len - pos - 4)
goto give_up;
pos += 4 + len; /* skip over exponent */
if (pkblob_len - pos < 4)
goto give_up;
len = toint(GET_32BIT(pkblob+pos)); /* find length of modulus */
if (len < 0 || len > pkblob_len - pos - 4)
goto give_up;
pos += 4; /* find modulus itself */
while (len > 0 && pkblob[pos] == 0)
len--, pos++;
/* debug(("modulus length is %d\n", len)); */
/*
* Now find the signature integer.
*/
pos = 4+7; /* skip over "ssh-rsa" */
if (sigblob_len < pos+4)
goto give_up;
siglen = toint(GET_32BIT(sigblob+pos));
if (siglen != sigblob_len - pos - 4)
goto give_up;
/* debug(("signature length is %d\n", siglen)); */
if (len != siglen) {
unsigned char newlen[4];
ssh2_pkt_addstring_start(pkt);
ssh2_pkt_addstring_data(pkt, (char *)sigblob, pos);
/* dmemdump(sigblob, pos); */
pos += 4; /* point to start of actual sig */
PUT_32BIT(newlen, len);
ssh2_pkt_addstring_data(pkt, (char *)newlen, 4);
/* dmemdump(newlen, 4); */
newlen[0] = 0;
while (len-- > siglen) {
ssh2_pkt_addstring_data(pkt, (char *)newlen, 1);
/* dmemdump(newlen, 1); */
}
ssh2_pkt_addstring_data(pkt, (char *)(sigblob+pos), siglen);
/* dmemdump(sigblob+pos, siglen); */
return;
}
/* Otherwise fall through and do it the easy way. We also come
* here as a fallback if we discover above that the key blob
* is misformatted in some way. */
give_up:;
}
ssh2_pkt_addstring_start(pkt);
ssh2_pkt_addstring_data(pkt, (char *)sigblob, sigblob_len);
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,803
|
static struct Packet *ssh2_bare_connection_rdpkt(Ssh ssh,
const unsigned char **data,
int *datalen)
{
struct rdpkt2_bare_state_tag *st = &ssh->rdpkt2_bare_state;
crBegin(ssh->ssh2_bare_rdpkt_crstate);
/*
* Read the packet length field.
*/
for (st->i = 0; st->i < 4; st->i++) {
while ((*datalen) == 0)
crReturn(NULL);
st->length[st->i] = *(*data)++;
(*datalen)--;
}
st->packetlen = toint(GET_32BIT_MSB_FIRST(st->length));
if (st->packetlen <= 0 || st->packetlen >= OUR_V2_PACKETLIMIT) {
bombout(("Invalid packet length received"));
crStop(NULL);
}
st->pktin = ssh_new_packet();
st->pktin->data = snewn(st->packetlen, unsigned char);
st->pktin->encrypted_len = st->packetlen;
st->pktin->sequence = st->incoming_sequence++;
/*
* Read the remainder of the packet.
*/
for (st->i = 0; st->i < st->packetlen; st->i++) {
while ((*datalen) == 0)
crReturn(NULL);
st->pktin->data[st->i] = *(*data)++;
(*datalen)--;
}
/*
* pktin->body and pktin->length should identify the semantic
* content of the packet, excluding the initial type byte.
*/
st->pktin->type = st->pktin->data[0];
st->pktin->body = st->pktin->data + 1;
st->pktin->length = st->packetlen - 1;
/*
* Log incoming packet, possibly omitting sensitive fields.
*/
if (ssh->logctx)
ssh2_log_incoming_packet(ssh, st->pktin);
st->pktin->savedpos = 0;
crFinish(st->pktin);
}
|
Overflow
| 0
|
static struct Packet *ssh2_bare_connection_rdpkt(Ssh ssh,
const unsigned char **data,
int *datalen)
{
struct rdpkt2_bare_state_tag *st = &ssh->rdpkt2_bare_state;
crBegin(ssh->ssh2_bare_rdpkt_crstate);
/*
* Read the packet length field.
*/
for (st->i = 0; st->i < 4; st->i++) {
while ((*datalen) == 0)
crReturn(NULL);
st->length[st->i] = *(*data)++;
(*datalen)--;
}
st->packetlen = toint(GET_32BIT_MSB_FIRST(st->length));
if (st->packetlen <= 0 || st->packetlen >= OUR_V2_PACKETLIMIT) {
bombout(("Invalid packet length received"));
crStop(NULL);
}
st->pktin = ssh_new_packet();
st->pktin->data = snewn(st->packetlen, unsigned char);
st->pktin->encrypted_len = st->packetlen;
st->pktin->sequence = st->incoming_sequence++;
/*
* Read the remainder of the packet.
*/
for (st->i = 0; st->i < st->packetlen; st->i++) {
while ((*datalen) == 0)
crReturn(NULL);
st->pktin->data[st->i] = *(*data)++;
(*datalen)--;
}
/*
* pktin->body and pktin->length should identify the semantic
* content of the packet, excluding the initial type byte.
*/
st->pktin->type = st->pktin->data[0];
st->pktin->body = st->pktin->data + 1;
st->pktin->length = st->packetlen - 1;
/*
* Log incoming packet, possibly omitting sensitive fields.
*/
if (ssh->logctx)
ssh2_log_incoming_packet(ssh, st->pktin);
st->pktin->savedpos = 0;
crFinish(st->pktin);
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,804
|
static void ssh2_log_incoming_packet(Ssh ssh, struct Packet *pkt)
{
int nblanks = 0;
struct logblank_t blanks[4];
char *str;
int slen;
pkt->savedpos = 0;
if (ssh->logomitdata &&
(pkt->type == SSH2_MSG_CHANNEL_DATA ||
pkt->type == SSH2_MSG_CHANNEL_EXTENDED_DATA)) {
/* "Session data" packets - omit the data string. */
ssh_pkt_getuint32(pkt); /* skip channel id */
if (pkt->type == SSH2_MSG_CHANNEL_EXTENDED_DATA)
ssh_pkt_getuint32(pkt); /* skip extended data type */
blanks[nblanks].offset = pkt->savedpos + 4;
blanks[nblanks].type = PKTLOG_OMIT;
ssh_pkt_getstring(pkt, &str, &slen);
if (str) {
blanks[nblanks].len = slen;
nblanks++;
}
}
log_packet(ssh->logctx, PKT_INCOMING, pkt->type,
ssh2_pkt_type(ssh->pkt_kctx, ssh->pkt_actx, pkt->type),
pkt->body, pkt->length, nblanks, blanks, &pkt->sequence,
0, NULL);
}
|
Overflow
| 0
|
static void ssh2_log_incoming_packet(Ssh ssh, struct Packet *pkt)
{
int nblanks = 0;
struct logblank_t blanks[4];
char *str;
int slen;
pkt->savedpos = 0;
if (ssh->logomitdata &&
(pkt->type == SSH2_MSG_CHANNEL_DATA ||
pkt->type == SSH2_MSG_CHANNEL_EXTENDED_DATA)) {
/* "Session data" packets - omit the data string. */
ssh_pkt_getuint32(pkt); /* skip channel id */
if (pkt->type == SSH2_MSG_CHANNEL_EXTENDED_DATA)
ssh_pkt_getuint32(pkt); /* skip extended data type */
blanks[nblanks].offset = pkt->savedpos + 4;
blanks[nblanks].type = PKTLOG_OMIT;
ssh_pkt_getstring(pkt, &str, &slen);
if (str) {
blanks[nblanks].len = slen;
nblanks++;
}
}
log_packet(ssh->logctx, PKT_INCOMING, pkt->type,
ssh2_pkt_type(ssh->pkt_kctx, ssh->pkt_actx, pkt->type),
pkt->body, pkt->length, nblanks, blanks, &pkt->sequence,
0, NULL);
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,805
|
static void ssh2_log_outgoing_packet(Ssh ssh, struct Packet *pkt)
{
int nblanks = 0;
struct logblank_t blanks[4];
char *str;
int slen;
/*
* For outgoing packets, pkt->length represents the length of the
* whole packet starting at pkt->data (including some header), and
* pkt->body refers to the point within that where the log-worthy
* payload begins. However, incoming packets expect pkt->length to
* represent only the payload length (that is, it's measured from
* pkt->body not from pkt->data). Temporarily adjust our outgoing
* packet to conform to the incoming-packet semantics, so that we
* can analyse it with the ssh_pkt_get functions.
*/
pkt->length -= (pkt->body - pkt->data);
pkt->savedpos = 0;
if (ssh->logomitdata &&
(pkt->type == SSH2_MSG_CHANNEL_DATA ||
pkt->type == SSH2_MSG_CHANNEL_EXTENDED_DATA)) {
/* "Session data" packets - omit the data string. */
ssh_pkt_getuint32(pkt); /* skip channel id */
if (pkt->type == SSH2_MSG_CHANNEL_EXTENDED_DATA)
ssh_pkt_getuint32(pkt); /* skip extended data type */
blanks[nblanks].offset = pkt->savedpos + 4;
blanks[nblanks].type = PKTLOG_OMIT;
ssh_pkt_getstring(pkt, &str, &slen);
if (str) {
blanks[nblanks].len = slen;
nblanks++;
}
}
if (pkt->type == SSH2_MSG_USERAUTH_REQUEST &&
conf_get_int(ssh->conf, CONF_logomitpass)) {
/* If this is a password packet, blank the password(s). */
pkt->savedpos = 0;
ssh_pkt_getstring(pkt, &str, &slen);
ssh_pkt_getstring(pkt, &str, &slen);
ssh_pkt_getstring(pkt, &str, &slen);
if (slen == 8 && !memcmp(str, "password", 8)) {
ssh2_pkt_getbool(pkt);
/* Blank the password field. */
blanks[nblanks].offset = pkt->savedpos;
blanks[nblanks].type = PKTLOG_BLANK;
ssh_pkt_getstring(pkt, &str, &slen);
if (str) {
blanks[nblanks].len = pkt->savedpos - blanks[nblanks].offset;
nblanks++;
/* If there's another password field beyond it (change of
* password), blank that too. */
ssh_pkt_getstring(pkt, &str, &slen);
if (str)
blanks[nblanks-1].len =
pkt->savedpos - blanks[nblanks].offset;
}
}
} else if (ssh->pkt_actx == SSH2_PKTCTX_KBDINTER &&
pkt->type == SSH2_MSG_USERAUTH_INFO_RESPONSE &&
conf_get_int(ssh->conf, CONF_logomitpass)) {
/* If this is a keyboard-interactive response packet, blank
* the responses. */
pkt->savedpos = 0;
ssh_pkt_getuint32(pkt);
blanks[nblanks].offset = pkt->savedpos;
blanks[nblanks].type = PKTLOG_BLANK;
while (1) {
ssh_pkt_getstring(pkt, &str, &slen);
if (!str)
break;
}
blanks[nblanks].len = pkt->savedpos - blanks[nblanks].offset;
nblanks++;
} else if (pkt->type == SSH2_MSG_CHANNEL_REQUEST &&
conf_get_int(ssh->conf, CONF_logomitpass)) {
/*
* If this is an X forwarding request packet, blank the fake
* auth data.
*
* Note that while we blank the X authentication data here, we
* don't take any special action to blank the start of an X11
* channel, so using MIT-MAGIC-COOKIE-1 and actually opening
* an X connection without having session blanking enabled is
* likely to leak your cookie into the log.
*/
pkt->savedpos = 0;
ssh_pkt_getuint32(pkt);
ssh_pkt_getstring(pkt, &str, &slen);
if (slen == 7 && !memcmp(str, "x11-req", 0)) {
ssh2_pkt_getbool(pkt);
ssh2_pkt_getbool(pkt);
ssh_pkt_getstring(pkt, &str, &slen);
blanks[nblanks].offset = pkt->savedpos;
blanks[nblanks].type = PKTLOG_BLANK;
ssh_pkt_getstring(pkt, &str, &slen);
if (str) {
blanks[nblanks].len = pkt->savedpos - blanks[nblanks].offset;
nblanks++;
}
}
}
log_packet(ssh->logctx, PKT_OUTGOING, pkt->data[5],
ssh2_pkt_type(ssh->pkt_kctx, ssh->pkt_actx, pkt->data[5]),
pkt->body, pkt->length, nblanks, blanks,
&ssh->v2_outgoing_sequence,
pkt->downstream_id, pkt->additional_log_text);
/*
* Undo the above adjustment of pkt->length, to put the packet
* back in the state we found it.
*/
pkt->length += (pkt->body - pkt->data);
}
|
Overflow
| 0
|
static void ssh2_log_outgoing_packet(Ssh ssh, struct Packet *pkt)
{
int nblanks = 0;
struct logblank_t blanks[4];
char *str;
int slen;
/*
* For outgoing packets, pkt->length represents the length of the
* whole packet starting at pkt->data (including some header), and
* pkt->body refers to the point within that where the log-worthy
* payload begins. However, incoming packets expect pkt->length to
* represent only the payload length (that is, it's measured from
* pkt->body not from pkt->data). Temporarily adjust our outgoing
* packet to conform to the incoming-packet semantics, so that we
* can analyse it with the ssh_pkt_get functions.
*/
pkt->length -= (pkt->body - pkt->data);
pkt->savedpos = 0;
if (ssh->logomitdata &&
(pkt->type == SSH2_MSG_CHANNEL_DATA ||
pkt->type == SSH2_MSG_CHANNEL_EXTENDED_DATA)) {
/* "Session data" packets - omit the data string. */
ssh_pkt_getuint32(pkt); /* skip channel id */
if (pkt->type == SSH2_MSG_CHANNEL_EXTENDED_DATA)
ssh_pkt_getuint32(pkt); /* skip extended data type */
blanks[nblanks].offset = pkt->savedpos + 4;
blanks[nblanks].type = PKTLOG_OMIT;
ssh_pkt_getstring(pkt, &str, &slen);
if (str) {
blanks[nblanks].len = slen;
nblanks++;
}
}
if (pkt->type == SSH2_MSG_USERAUTH_REQUEST &&
conf_get_int(ssh->conf, CONF_logomitpass)) {
/* If this is a password packet, blank the password(s). */
pkt->savedpos = 0;
ssh_pkt_getstring(pkt, &str, &slen);
ssh_pkt_getstring(pkt, &str, &slen);
ssh_pkt_getstring(pkt, &str, &slen);
if (slen == 8 && !memcmp(str, "password", 8)) {
ssh2_pkt_getbool(pkt);
/* Blank the password field. */
blanks[nblanks].offset = pkt->savedpos;
blanks[nblanks].type = PKTLOG_BLANK;
ssh_pkt_getstring(pkt, &str, &slen);
if (str) {
blanks[nblanks].len = pkt->savedpos - blanks[nblanks].offset;
nblanks++;
/* If there's another password field beyond it (change of
* password), blank that too. */
ssh_pkt_getstring(pkt, &str, &slen);
if (str)
blanks[nblanks-1].len =
pkt->savedpos - blanks[nblanks].offset;
}
}
} else if (ssh->pkt_actx == SSH2_PKTCTX_KBDINTER &&
pkt->type == SSH2_MSG_USERAUTH_INFO_RESPONSE &&
conf_get_int(ssh->conf, CONF_logomitpass)) {
/* If this is a keyboard-interactive response packet, blank
* the responses. */
pkt->savedpos = 0;
ssh_pkt_getuint32(pkt);
blanks[nblanks].offset = pkt->savedpos;
blanks[nblanks].type = PKTLOG_BLANK;
while (1) {
ssh_pkt_getstring(pkt, &str, &slen);
if (!str)
break;
}
blanks[nblanks].len = pkt->savedpos - blanks[nblanks].offset;
nblanks++;
} else if (pkt->type == SSH2_MSG_CHANNEL_REQUEST &&
conf_get_int(ssh->conf, CONF_logomitpass)) {
/*
* If this is an X forwarding request packet, blank the fake
* auth data.
*
* Note that while we blank the X authentication data here, we
* don't take any special action to blank the start of an X11
* channel, so using MIT-MAGIC-COOKIE-1 and actually opening
* an X connection without having session blanking enabled is
* likely to leak your cookie into the log.
*/
pkt->savedpos = 0;
ssh_pkt_getuint32(pkt);
ssh_pkt_getstring(pkt, &str, &slen);
if (slen == 7 && !memcmp(str, "x11-req", 0)) {
ssh2_pkt_getbool(pkt);
ssh2_pkt_getbool(pkt);
ssh_pkt_getstring(pkt, &str, &slen);
blanks[nblanks].offset = pkt->savedpos;
blanks[nblanks].type = PKTLOG_BLANK;
ssh_pkt_getstring(pkt, &str, &slen);
if (str) {
blanks[nblanks].len = pkt->savedpos - blanks[nblanks].offset;
nblanks++;
}
}
}
log_packet(ssh->logctx, PKT_OUTGOING, pkt->data[5],
ssh2_pkt_type(ssh->pkt_kctx, ssh->pkt_actx, pkt->data[5]),
pkt->body, pkt->length, nblanks, blanks,
&ssh->v2_outgoing_sequence,
pkt->downstream_id, pkt->additional_log_text);
/*
* Undo the above adjustment of pkt->length, to put the packet
* back in the state we found it.
*/
pkt->length += (pkt->body - pkt->data);
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,806
|
static unsigned char *ssh2_mpint_fmt(Bignum b, int *len)
{
unsigned char *p;
int i, n = (bignum_bitcount(b) + 7) / 8;
p = snewn(n + 1, unsigned char);
p[0] = 0;
for (i = 1; i <= n; i++)
p[i] = bignum_byte(b, n - i);
i = 0;
while (i <= n && p[i] == 0 && (p[i + 1] & 0x80) == 0)
i++;
memmove(p, p + i, n + 1 - i);
*len = n + 1 - i;
return p;
}
|
Overflow
| 0
|
static unsigned char *ssh2_mpint_fmt(Bignum b, int *len)
{
unsigned char *p;
int i, n = (bignum_bitcount(b) + 7) / 8;
p = snewn(n + 1, unsigned char);
p[0] = 0;
for (i = 1; i <= n; i++)
p[i] = bignum_byte(b, n - i);
i = 0;
while (i <= n && p[i] == 0 && (p[i + 1] & 0x80) == 0)
i++;
memmove(p, p + i, n + 1 - i);
*len = n + 1 - i;
return p;
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,807
|
static void ssh2_pkt_addmp(struct Packet *pkt, Bignum b)
{
unsigned char *p;
int len;
p = ssh2_mpint_fmt(b, &len);
ssh_pkt_addstring_start(pkt);
ssh_pkt_addstring_data(pkt, (char *)p, len);
sfree(p);
}
|
Overflow
| 0
|
static void ssh2_pkt_addmp(struct Packet *pkt, Bignum b)
{
unsigned char *p;
int len;
p = ssh2_mpint_fmt(b, &len);
ssh_pkt_addstring_start(pkt);
ssh_pkt_addstring_data(pkt, (char *)p, len);
sfree(p);
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,808
|
static int ssh2_pkt_construct(Ssh ssh, struct Packet *pkt)
{
int cipherblk, maclen, padding, unencrypted_prefix, i;
if (ssh->logctx)
ssh2_log_outgoing_packet(ssh, pkt);
if (ssh->bare_connection) {
/*
* Trivial packet construction for the bare connection
* protocol.
*/
PUT_32BIT(pkt->data + 1, pkt->length - 5);
pkt->body = pkt->data + 1;
ssh->v2_outgoing_sequence++; /* only for diagnostics, really */
return pkt->length - 1;
}
/*
* Compress packet payload.
*/
{
unsigned char *newpayload;
int newlen;
if (ssh->cscomp &&
ssh->cscomp->compress(ssh->cs_comp_ctx, pkt->data + 5,
pkt->length - 5,
&newpayload, &newlen)) {
pkt->length = 5;
ssh2_pkt_adddata(pkt, newpayload, newlen);
sfree(newpayload);
}
}
/*
* Add padding. At least four bytes, and must also bring total
* length (minus MAC) up to a multiple of the block size.
* If pkt->forcepad is set, make sure the packet is at least that size
* after padding.
*/
cipherblk = ssh->cscipher ? ssh->cscipher->blksize : 8; /* block size */
cipherblk = cipherblk < 8 ? 8 : cipherblk; /* or 8 if blksize < 8 */
padding = 4;
unencrypted_prefix = (ssh->csmac && ssh->csmac_etm) ? 4 : 0;
if (pkt->length + padding < pkt->forcepad)
padding = pkt->forcepad - pkt->length;
padding +=
(cipherblk - (pkt->length - unencrypted_prefix + padding) % cipherblk)
% cipherblk;
assert(padding <= 255);
maclen = ssh->csmac ? ssh->csmac->len : 0;
ssh2_pkt_ensure(pkt, pkt->length + padding + maclen);
pkt->data[4] = padding;
for (i = 0; i < padding; i++)
pkt->data[pkt->length + i] = random_byte();
PUT_32BIT(pkt->data, pkt->length + padding - 4);
/* Encrypt length if the scheme requires it */
if (ssh->cscipher && (ssh->cscipher->flags & SSH_CIPHER_SEPARATE_LENGTH)) {
ssh->cscipher->encrypt_length(ssh->cs_cipher_ctx, pkt->data, 4,
ssh->v2_outgoing_sequence);
}
if (ssh->csmac && ssh->csmac_etm) {
/*
* OpenSSH-defined encrypt-then-MAC protocol.
*/
if (ssh->cscipher)
ssh->cscipher->encrypt(ssh->cs_cipher_ctx,
pkt->data + 4, pkt->length + padding - 4);
ssh->csmac->generate(ssh->cs_mac_ctx, pkt->data,
pkt->length + padding,
ssh->v2_outgoing_sequence);
} else {
/*
* SSH-2 standard protocol.
*/
if (ssh->csmac)
ssh->csmac->generate(ssh->cs_mac_ctx, pkt->data,
pkt->length + padding,
ssh->v2_outgoing_sequence);
if (ssh->cscipher)
ssh->cscipher->encrypt(ssh->cs_cipher_ctx,
pkt->data, pkt->length + padding);
}
ssh->v2_outgoing_sequence++; /* whether or not we MACed */
pkt->encrypted_len = pkt->length + padding;
/* Ready-to-send packet starts at pkt->data. We return length. */
pkt->body = pkt->data;
return pkt->length + padding + maclen;
}
|
Overflow
| 0
|
static int ssh2_pkt_construct(Ssh ssh, struct Packet *pkt)
{
int cipherblk, maclen, padding, unencrypted_prefix, i;
if (ssh->logctx)
ssh2_log_outgoing_packet(ssh, pkt);
if (ssh->bare_connection) {
/*
* Trivial packet construction for the bare connection
* protocol.
*/
PUT_32BIT(pkt->data + 1, pkt->length - 5);
pkt->body = pkt->data + 1;
ssh->v2_outgoing_sequence++; /* only for diagnostics, really */
return pkt->length - 1;
}
/*
* Compress packet payload.
*/
{
unsigned char *newpayload;
int newlen;
if (ssh->cscomp &&
ssh->cscomp->compress(ssh->cs_comp_ctx, pkt->data + 5,
pkt->length - 5,
&newpayload, &newlen)) {
pkt->length = 5;
ssh2_pkt_adddata(pkt, newpayload, newlen);
sfree(newpayload);
}
}
/*
* Add padding. At least four bytes, and must also bring total
* length (minus MAC) up to a multiple of the block size.
* If pkt->forcepad is set, make sure the packet is at least that size
* after padding.
*/
cipherblk = ssh->cscipher ? ssh->cscipher->blksize : 8; /* block size */
cipherblk = cipherblk < 8 ? 8 : cipherblk; /* or 8 if blksize < 8 */
padding = 4;
unencrypted_prefix = (ssh->csmac && ssh->csmac_etm) ? 4 : 0;
if (pkt->length + padding < pkt->forcepad)
padding = pkt->forcepad - pkt->length;
padding +=
(cipherblk - (pkt->length - unencrypted_prefix + padding) % cipherblk)
% cipherblk;
assert(padding <= 255);
maclen = ssh->csmac ? ssh->csmac->len : 0;
ssh2_pkt_ensure(pkt, pkt->length + padding + maclen);
pkt->data[4] = padding;
for (i = 0; i < padding; i++)
pkt->data[pkt->length + i] = random_byte();
PUT_32BIT(pkt->data, pkt->length + padding - 4);
/* Encrypt length if the scheme requires it */
if (ssh->cscipher && (ssh->cscipher->flags & SSH_CIPHER_SEPARATE_LENGTH)) {
ssh->cscipher->encrypt_length(ssh->cs_cipher_ctx, pkt->data, 4,
ssh->v2_outgoing_sequence);
}
if (ssh->csmac && ssh->csmac_etm) {
/*
* OpenSSH-defined encrypt-then-MAC protocol.
*/
if (ssh->cscipher)
ssh->cscipher->encrypt(ssh->cs_cipher_ctx,
pkt->data + 4, pkt->length + padding - 4);
ssh->csmac->generate(ssh->cs_mac_ctx, pkt->data,
pkt->length + padding,
ssh->v2_outgoing_sequence);
} else {
/*
* SSH-2 standard protocol.
*/
if (ssh->csmac)
ssh->csmac->generate(ssh->cs_mac_ctx, pkt->data,
pkt->length + padding,
ssh->v2_outgoing_sequence);
if (ssh->cscipher)
ssh->cscipher->encrypt(ssh->cs_cipher_ctx,
pkt->data, pkt->length + padding);
}
ssh->v2_outgoing_sequence++; /* whether or not we MACed */
pkt->encrypted_len = pkt->length + padding;
/* Ready-to-send packet starts at pkt->data. We return length. */
pkt->body = pkt->data;
return pkt->length + padding + maclen;
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,809
|
static void ssh2_pkt_defer(Ssh ssh, struct Packet *pkt)
{
if (ssh->queueing)
ssh2_pkt_queue(ssh, pkt);
else
ssh2_pkt_defer_noqueue(ssh, pkt, FALSE);
}
|
Overflow
| 0
|
static void ssh2_pkt_defer(Ssh ssh, struct Packet *pkt)
{
if (ssh->queueing)
ssh2_pkt_queue(ssh, pkt);
else
ssh2_pkt_defer_noqueue(ssh, pkt, FALSE);
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,810
|
static void ssh2_pkt_defer_noqueue(Ssh ssh, struct Packet *pkt, int noignore)
{
int len;
if (ssh->cscipher != NULL && (ssh->cscipher->flags & SSH_CIPHER_IS_CBC) &&
ssh->deferred_len == 0 && !noignore &&
!(ssh->remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE)) {
/*
* Interpose an SSH_MSG_IGNORE to ensure that user data don't
* get encrypted with a known IV.
*/
struct Packet *ipkt = ssh2_pkt_init(SSH2_MSG_IGNORE);
ssh2_pkt_addstring_start(ipkt);
ssh2_pkt_defer_noqueue(ssh, ipkt, TRUE);
}
len = ssh2_pkt_construct(ssh, pkt);
if (ssh->deferred_len + len > ssh->deferred_size) {
ssh->deferred_size = ssh->deferred_len + len + 128;
ssh->deferred_send_data = sresize(ssh->deferred_send_data,
ssh->deferred_size,
unsigned char);
}
memcpy(ssh->deferred_send_data + ssh->deferred_len, pkt->body, len);
ssh->deferred_len += len;
ssh->deferred_data_size += pkt->encrypted_len;
ssh_free_packet(pkt);
}
|
Overflow
| 0
|
static void ssh2_pkt_defer_noqueue(Ssh ssh, struct Packet *pkt, int noignore)
{
int len;
if (ssh->cscipher != NULL && (ssh->cscipher->flags & SSH_CIPHER_IS_CBC) &&
ssh->deferred_len == 0 && !noignore &&
!(ssh->remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE)) {
/*
* Interpose an SSH_MSG_IGNORE to ensure that user data don't
* get encrypted with a known IV.
*/
struct Packet *ipkt = ssh2_pkt_init(SSH2_MSG_IGNORE);
ssh2_pkt_addstring_start(ipkt);
ssh2_pkt_defer_noqueue(ssh, ipkt, TRUE);
}
len = ssh2_pkt_construct(ssh, pkt);
if (ssh->deferred_len + len > ssh->deferred_size) {
ssh->deferred_size = ssh->deferred_len + len + 128;
ssh->deferred_send_data = sresize(ssh->deferred_send_data,
ssh->deferred_size,
unsigned char);
}
memcpy(ssh->deferred_send_data + ssh->deferred_len, pkt->body, len);
ssh->deferred_len += len;
ssh->deferred_data_size += pkt->encrypted_len;
ssh_free_packet(pkt);
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,811
|
static int ssh2_pkt_getbool(struct Packet *pkt)
{
unsigned long value;
if (pkt->length - pkt->savedpos < 1)
return 0; /* arrgh, no way to decline (FIXME?) */
value = pkt->body[pkt->savedpos] != 0;
pkt->savedpos++;
return value;
}
|
Overflow
| 0
|
static int ssh2_pkt_getbool(struct Packet *pkt)
{
unsigned long value;
if (pkt->length - pkt->savedpos < 1)
return 0; /* arrgh, no way to decline (FIXME?) */
value = pkt->body[pkt->savedpos] != 0;
pkt->savedpos++;
return value;
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,812
|
static Bignum ssh2_pkt_getmp(struct Packet *pkt)
{
char *p;
int length;
Bignum b;
ssh_pkt_getstring(pkt, &p, &length);
if (!p)
return NULL;
if (p[0] & 0x80)
return NULL;
b = bignum_from_bytes((unsigned char *)p, length);
return b;
}
|
Overflow
| 0
|
static Bignum ssh2_pkt_getmp(struct Packet *pkt)
{
char *p;
int length;
Bignum b;
ssh_pkt_getstring(pkt, &p, &length);
if (!p)
return NULL;
if (p[0] & 0x80)
return NULL;
b = bignum_from_bytes((unsigned char *)p, length);
return b;
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,813
|
static struct Packet *ssh2_pkt_init(int pkt_type)
{
struct Packet *pkt = ssh_new_packet();
pkt->length = 5; /* space for packet length + padding length */
pkt->forcepad = 0;
pkt->type = pkt_type;
ssh_pkt_addbyte(pkt, (unsigned char) pkt_type);
pkt->body = pkt->data + pkt->length; /* after packet type */
pkt->downstream_id = 0;
pkt->additional_log_text = NULL;
return pkt;
}
|
Overflow
| 0
|
static struct Packet *ssh2_pkt_init(int pkt_type)
{
struct Packet *pkt = ssh_new_packet();
pkt->length = 5; /* space for packet length + padding length */
pkt->forcepad = 0;
pkt->type = pkt_type;
ssh_pkt_addbyte(pkt, (unsigned char) pkt_type);
pkt->body = pkt->data + pkt->length; /* after packet type */
pkt->downstream_id = 0;
pkt->additional_log_text = NULL;
return pkt;
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,814
|
static void ssh2_pkt_queue(Ssh ssh, struct Packet *pkt)
{
assert(ssh->queueing);
if (ssh->queuelen >= ssh->queuesize) {
ssh->queuesize = ssh->queuelen + 32;
ssh->queue = sresize(ssh->queue, ssh->queuesize, struct Packet *);
}
ssh->queue[ssh->queuelen++] = pkt;
}
|
Overflow
| 0
|
static void ssh2_pkt_queue(Ssh ssh, struct Packet *pkt)
{
assert(ssh->queueing);
if (ssh->queuelen >= ssh->queuesize) {
ssh->queuesize = ssh->queuelen + 32;
ssh->queue = sresize(ssh->queue, ssh->queuesize, struct Packet *);
}
ssh->queue[ssh->queuelen++] = pkt;
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,815
|
static void ssh2_pkt_queuesend(Ssh ssh)
{
int i;
assert(!ssh->queueing);
for (i = 0; i < ssh->queuelen; i++)
ssh2_pkt_defer_noqueue(ssh, ssh->queue[i], FALSE);
ssh->queuelen = 0;
ssh_pkt_defersend(ssh);
}
|
Overflow
| 0
|
static void ssh2_pkt_queuesend(Ssh ssh)
{
int i;
assert(!ssh->queueing);
for (i = 0; i < ssh->queuelen; i++)
ssh2_pkt_defer_noqueue(ssh, ssh->queue[i], FALSE);
ssh->queuelen = 0;
ssh_pkt_defersend(ssh);
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,816
|
static void ssh2_pkt_send(Ssh ssh, struct Packet *pkt)
{
if (ssh->queueing)
ssh2_pkt_queue(ssh, pkt);
else
ssh2_pkt_send_noqueue(ssh, pkt);
}
|
Overflow
| 0
|
static void ssh2_pkt_send(Ssh ssh, struct Packet *pkt)
{
if (ssh->queueing)
ssh2_pkt_queue(ssh, pkt);
else
ssh2_pkt_send_noqueue(ssh, pkt);
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,817
|
static void ssh2_pkt_send_noqueue(Ssh ssh, struct Packet *pkt)
{
int len;
int backlog;
if (ssh->cscipher != NULL && (ssh->cscipher->flags & SSH_CIPHER_IS_CBC)) {
/* We need to send two packets, so use the deferral mechanism. */
ssh2_pkt_defer_noqueue(ssh, pkt, FALSE);
ssh_pkt_defersend(ssh);
return;
}
len = ssh2_pkt_construct(ssh, pkt);
backlog = s_write(ssh, pkt->body, len);
if (backlog > SSH_MAX_BACKLOG)
ssh_throttle_all(ssh, 1, backlog);
ssh->outgoing_data_size += pkt->encrypted_len;
if (!ssh->kex_in_progress &&
!ssh->bare_connection &&
ssh->max_data_size != 0 &&
ssh->outgoing_data_size > ssh->max_data_size)
do_ssh2_transport(ssh, "too much data sent", -1, NULL);
ssh_free_packet(pkt);
}
|
Overflow
| 0
|
static void ssh2_pkt_send_noqueue(Ssh ssh, struct Packet *pkt)
{
int len;
int backlog;
if (ssh->cscipher != NULL && (ssh->cscipher->flags & SSH_CIPHER_IS_CBC)) {
/* We need to send two packets, so use the deferral mechanism. */
ssh2_pkt_defer_noqueue(ssh, pkt, FALSE);
ssh_pkt_defersend(ssh);
return;
}
len = ssh2_pkt_construct(ssh, pkt);
backlog = s_write(ssh, pkt->body, len);
if (backlog > SSH_MAX_BACKLOG)
ssh_throttle_all(ssh, 1, backlog);
ssh->outgoing_data_size += pkt->encrypted_len;
if (!ssh->kex_in_progress &&
!ssh->bare_connection &&
ssh->max_data_size != 0 &&
ssh->outgoing_data_size > ssh->max_data_size)
do_ssh2_transport(ssh, "too much data sent", -1, NULL);
ssh_free_packet(pkt);
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,818
|
static void ssh2_pkt_send_with_padding(Ssh ssh, struct Packet *pkt,
int padsize)
{
#if 0
if (0) {
/*
* The simplest way to do this is to adjust the
* variable-length padding field in the outgoing packet.
*
* Currently compiled out, because some Cisco SSH servers
* don't like excessively padded packets (bah, why's it
* always Cisco?)
*/
pkt->forcepad = padsize;
ssh2_pkt_send(ssh, pkt);
} else
#endif
{
/*
* If we can't do that, however, an alternative approach is
* to use the pkt_defer mechanism to bundle the packet
* tightly together with an SSH_MSG_IGNORE such that their
* combined length is a constant. So first we construct the
* final form of this packet and defer its sending.
*/
ssh2_pkt_defer(ssh, pkt);
/*
* Now construct an SSH_MSG_IGNORE which includes a string
* that's an exact multiple of the cipher block size. (If
* the cipher is NULL so that the block size is
* unavailable, we don't do this trick at all, because we
* gain nothing by it.)
*/
if (ssh->cscipher &&
!(ssh->remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE)) {
int stringlen, i;
stringlen = (256 - ssh->deferred_len);
stringlen += ssh->cscipher->blksize - 1;
stringlen -= (stringlen % ssh->cscipher->blksize);
if (ssh->cscomp) {
/*
* Temporarily disable actual compression, so we
* can guarantee to get this string exactly the
* length we want it. The compression-disabling
* routine should return an integer indicating how
* many bytes we should adjust our string length
* by.
*/
stringlen -=
ssh->cscomp->disable_compression(ssh->cs_comp_ctx);
}
pkt = ssh2_pkt_init(SSH2_MSG_IGNORE);
ssh2_pkt_addstring_start(pkt);
for (i = 0; i < stringlen; i++) {
char c = (char) random_byte();
ssh2_pkt_addstring_data(pkt, &c, 1);
}
ssh2_pkt_defer(ssh, pkt);
}
ssh_pkt_defersend(ssh);
}
}
|
Overflow
| 0
|
static void ssh2_pkt_send_with_padding(Ssh ssh, struct Packet *pkt,
int padsize)
{
#if 0
if (0) {
/*
* The simplest way to do this is to adjust the
* variable-length padding field in the outgoing packet.
*
* Currently compiled out, because some Cisco SSH servers
* don't like excessively padded packets (bah, why's it
* always Cisco?)
*/
pkt->forcepad = padsize;
ssh2_pkt_send(ssh, pkt);
} else
#endif
{
/*
* If we can't do that, however, an alternative approach is
* to use the pkt_defer mechanism to bundle the packet
* tightly together with an SSH_MSG_IGNORE such that their
* combined length is a constant. So first we construct the
* final form of this packet and defer its sending.
*/
ssh2_pkt_defer(ssh, pkt);
/*
* Now construct an SSH_MSG_IGNORE which includes a string
* that's an exact multiple of the cipher block size. (If
* the cipher is NULL so that the block size is
* unavailable, we don't do this trick at all, because we
* gain nothing by it.)
*/
if (ssh->cscipher &&
!(ssh->remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE)) {
int stringlen, i;
stringlen = (256 - ssh->deferred_len);
stringlen += ssh->cscipher->blksize - 1;
stringlen -= (stringlen % ssh->cscipher->blksize);
if (ssh->cscomp) {
/*
* Temporarily disable actual compression, so we
* can guarantee to get this string exactly the
* length we want it. The compression-disabling
* routine should return an integer indicating how
* many bytes we should adjust our string length
* by.
*/
stringlen -=
ssh->cscomp->disable_compression(ssh->cs_comp_ctx);
}
pkt = ssh2_pkt_init(SSH2_MSG_IGNORE);
ssh2_pkt_addstring_start(pkt);
for (i = 0; i < stringlen; i++) {
char c = (char) random_byte();
ssh2_pkt_addstring_data(pkt, &c, 1);
}
ssh2_pkt_defer(ssh, pkt);
}
ssh_pkt_defersend(ssh);
}
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,819
|
static struct Packet *ssh2_rdpkt(Ssh ssh, const unsigned char **data,
int *datalen)
{
struct rdpkt2_state_tag *st = &ssh->rdpkt2_state;
crBegin(ssh->ssh2_rdpkt_crstate);
st->pktin = ssh_new_packet();
st->pktin->type = 0;
st->pktin->length = 0;
if (ssh->sccipher)
st->cipherblk = ssh->sccipher->blksize;
else
st->cipherblk = 8;
if (st->cipherblk < 8)
st->cipherblk = 8;
st->maclen = ssh->scmac ? ssh->scmac->len : 0;
if (ssh->sccipher && (ssh->sccipher->flags & SSH_CIPHER_IS_CBC) &&
ssh->scmac && !ssh->scmac_etm) {
/*
* When dealing with a CBC-mode cipher, we want to avoid the
* possibility of an attacker's tweaking the ciphertext stream
* so as to cause us to feed the same block to the block
* cipher more than once and thus leak information
* (VU#958563). The way we do this is not to take any
* decisions on the basis of anything we've decrypted until
* we've verified it with a MAC. That includes the packet
* length, so we just read data and check the MAC repeatedly,
* and when the MAC passes, see if the length we've got is
* plausible.
*
* This defence is unnecessary in OpenSSH ETM mode, because
* the whole point of ETM mode is that the attacker can't
* tweak the ciphertext stream at all without the MAC
* detecting it before we decrypt anything.
*/
/* May as well allocate the whole lot now. */
st->pktin->data = snewn(OUR_V2_PACKETLIMIT + st->maclen + APIEXTRA,
unsigned char);
/* Read an amount corresponding to the MAC. */
for (st->i = 0; st->i < st->maclen; st->i++) {
while ((*datalen) == 0)
crReturn(NULL);
st->pktin->data[st->i] = *(*data)++;
(*datalen)--;
}
st->packetlen = 0;
{
unsigned char seq[4];
ssh->scmac->start(ssh->sc_mac_ctx);
PUT_32BIT(seq, st->incoming_sequence);
ssh->scmac->bytes(ssh->sc_mac_ctx, seq, 4);
}
for (;;) { /* Once around this loop per cipher block. */
/* Read another cipher-block's worth, and tack it onto the end. */
for (st->i = 0; st->i < st->cipherblk; st->i++) {
while ((*datalen) == 0)
crReturn(NULL);
st->pktin->data[st->packetlen+st->maclen+st->i] = *(*data)++;
(*datalen)--;
}
/* Decrypt one more block (a little further back in the stream). */
ssh->sccipher->decrypt(ssh->sc_cipher_ctx,
st->pktin->data + st->packetlen,
st->cipherblk);
/* Feed that block to the MAC. */
ssh->scmac->bytes(ssh->sc_mac_ctx,
st->pktin->data + st->packetlen, st->cipherblk);
st->packetlen += st->cipherblk;
/* See if that gives us a valid packet. */
if (ssh->scmac->verresult(ssh->sc_mac_ctx,
st->pktin->data + st->packetlen) &&
((st->len = toint(GET_32BIT(st->pktin->data))) ==
st->packetlen-4))
break;
if (st->packetlen >= OUR_V2_PACKETLIMIT) {
bombout(("No valid incoming packet found"));
ssh_free_packet(st->pktin);
crStop(NULL);
}
}
st->pktin->maxlen = st->packetlen + st->maclen;
st->pktin->data = sresize(st->pktin->data,
st->pktin->maxlen + APIEXTRA,
unsigned char);
} else if (ssh->scmac && ssh->scmac_etm) {
st->pktin->data = snewn(4 + APIEXTRA, unsigned char);
/*
* OpenSSH encrypt-then-MAC mode: the packet length is
* unencrypted, unless the cipher supports length encryption.
*/
for (st->i = st->len = 0; st->i < 4; st->i++) {
while ((*datalen) == 0)
crReturn(NULL);
st->pktin->data[st->i] = *(*data)++;
(*datalen)--;
}
/* Cipher supports length decryption, so do it */
if (ssh->sccipher && (ssh->sccipher->flags & SSH_CIPHER_SEPARATE_LENGTH)) {
/* Keep the packet the same though, so the MAC passes */
unsigned char len[4];
memcpy(len, st->pktin->data, 4);
ssh->sccipher->decrypt_length(ssh->sc_cipher_ctx, len, 4, st->incoming_sequence);
st->len = toint(GET_32BIT(len));
} else {
st->len = toint(GET_32BIT(st->pktin->data));
}
/*
* _Completely_ silly lengths should be stomped on before they
* do us any more damage.
*/
if (st->len < 0 || st->len > OUR_V2_PACKETLIMIT ||
st->len % st->cipherblk != 0) {
bombout(("Incoming packet length field was garbled"));
ssh_free_packet(st->pktin);
crStop(NULL);
}
/*
* So now we can work out the total packet length.
*/
st->packetlen = st->len + 4;
/*
* Allocate memory for the rest of the packet.
*/
st->pktin->maxlen = st->packetlen + st->maclen;
st->pktin->data = sresize(st->pktin->data,
st->pktin->maxlen + APIEXTRA,
unsigned char);
/*
* Read the remainder of the packet.
*/
for (st->i = 4; st->i < st->packetlen + st->maclen; st->i++) {
while ((*datalen) == 0)
crReturn(NULL);
st->pktin->data[st->i] = *(*data)++;
(*datalen)--;
}
/*
* Check the MAC.
*/
if (ssh->scmac
&& !ssh->scmac->verify(ssh->sc_mac_ctx, st->pktin->data,
st->len + 4, st->incoming_sequence)) {
bombout(("Incorrect MAC received on packet"));
ssh_free_packet(st->pktin);
crStop(NULL);
}
/* Decrypt everything between the length field and the MAC. */
if (ssh->sccipher)
ssh->sccipher->decrypt(ssh->sc_cipher_ctx,
st->pktin->data + 4,
st->packetlen - 4);
} else {
st->pktin->data = snewn(st->cipherblk + APIEXTRA, unsigned char);
/*
* Acquire and decrypt the first block of the packet. This will
* contain the length and padding details.
*/
for (st->i = st->len = 0; st->i < st->cipherblk; st->i++) {
while ((*datalen) == 0)
crReturn(NULL);
st->pktin->data[st->i] = *(*data)++;
(*datalen)--;
}
if (ssh->sccipher)
ssh->sccipher->decrypt(ssh->sc_cipher_ctx,
st->pktin->data, st->cipherblk);
/*
* Now get the length figure.
*/
st->len = toint(GET_32BIT(st->pktin->data));
/*
* _Completely_ silly lengths should be stomped on before they
* do us any more damage.
*/
if (st->len < 0 || st->len > OUR_V2_PACKETLIMIT ||
(st->len + 4) % st->cipherblk != 0) {
bombout(("Incoming packet was garbled on decryption"));
ssh_free_packet(st->pktin);
crStop(NULL);
}
/*
* So now we can work out the total packet length.
*/
st->packetlen = st->len + 4;
/*
* Allocate memory for the rest of the packet.
*/
st->pktin->maxlen = st->packetlen + st->maclen;
st->pktin->data = sresize(st->pktin->data,
st->pktin->maxlen + APIEXTRA,
unsigned char);
/*
* Read and decrypt the remainder of the packet.
*/
for (st->i = st->cipherblk; st->i < st->packetlen + st->maclen;
st->i++) {
while ((*datalen) == 0)
crReturn(NULL);
st->pktin->data[st->i] = *(*data)++;
(*datalen)--;
}
/* Decrypt everything _except_ the MAC. */
if (ssh->sccipher)
ssh->sccipher->decrypt(ssh->sc_cipher_ctx,
st->pktin->data + st->cipherblk,
st->packetlen - st->cipherblk);
/*
* Check the MAC.
*/
if (ssh->scmac
&& !ssh->scmac->verify(ssh->sc_mac_ctx, st->pktin->data,
st->len + 4, st->incoming_sequence)) {
bombout(("Incorrect MAC received on packet"));
ssh_free_packet(st->pktin);
crStop(NULL);
}
}
/* Get and sanity-check the amount of random padding. */
st->pad = st->pktin->data[4];
if (st->pad < 4 || st->len - st->pad < 1) {
bombout(("Invalid padding length on received packet"));
ssh_free_packet(st->pktin);
crStop(NULL);
}
/*
* This enables us to deduce the payload length.
*/
st->payload = st->len - st->pad - 1;
st->pktin->length = st->payload + 5;
st->pktin->encrypted_len = st->packetlen;
st->pktin->sequence = st->incoming_sequence++;
st->pktin->length = st->packetlen - st->pad;
assert(st->pktin->length >= 0);
/*
* Decompress packet payload.
*/
{
unsigned char *newpayload;
int newlen;
if (ssh->sccomp &&
ssh->sccomp->decompress(ssh->sc_comp_ctx,
st->pktin->data + 5, st->pktin->length - 5,
&newpayload, &newlen)) {
if (st->pktin->maxlen < newlen + 5) {
st->pktin->maxlen = newlen + 5;
st->pktin->data = sresize(st->pktin->data,
st->pktin->maxlen + APIEXTRA,
unsigned char);
}
st->pktin->length = 5 + newlen;
memcpy(st->pktin->data + 5, newpayload, newlen);
sfree(newpayload);
}
}
/*
* RFC 4253 doesn't explicitly say that completely empty packets
* with no type byte are forbidden, so treat them as deserving
* an SSH_MSG_UNIMPLEMENTED.
*/
if (st->pktin->length <= 5) { /* == 5 we hope, but robustness */
ssh2_msg_something_unimplemented(ssh, st->pktin);
crStop(NULL);
}
/*
* pktin->body and pktin->length should identify the semantic
* content of the packet, excluding the initial type byte.
*/
st->pktin->type = st->pktin->data[5];
st->pktin->body = st->pktin->data + 6;
st->pktin->length -= 6;
assert(st->pktin->length >= 0); /* one last double-check */
if (ssh->logctx)
ssh2_log_incoming_packet(ssh, st->pktin);
st->pktin->savedpos = 0;
crFinish(st->pktin);
}
|
Overflow
| 0
|
static struct Packet *ssh2_rdpkt(Ssh ssh, const unsigned char **data,
int *datalen)
{
struct rdpkt2_state_tag *st = &ssh->rdpkt2_state;
crBegin(ssh->ssh2_rdpkt_crstate);
st->pktin = ssh_new_packet();
st->pktin->type = 0;
st->pktin->length = 0;
if (ssh->sccipher)
st->cipherblk = ssh->sccipher->blksize;
else
st->cipherblk = 8;
if (st->cipherblk < 8)
st->cipherblk = 8;
st->maclen = ssh->scmac ? ssh->scmac->len : 0;
if (ssh->sccipher && (ssh->sccipher->flags & SSH_CIPHER_IS_CBC) &&
ssh->scmac && !ssh->scmac_etm) {
/*
* When dealing with a CBC-mode cipher, we want to avoid the
* possibility of an attacker's tweaking the ciphertext stream
* so as to cause us to feed the same block to the block
* cipher more than once and thus leak information
* (VU#958563). The way we do this is not to take any
* decisions on the basis of anything we've decrypted until
* we've verified it with a MAC. That includes the packet
* length, so we just read data and check the MAC repeatedly,
* and when the MAC passes, see if the length we've got is
* plausible.
*
* This defence is unnecessary in OpenSSH ETM mode, because
* the whole point of ETM mode is that the attacker can't
* tweak the ciphertext stream at all without the MAC
* detecting it before we decrypt anything.
*/
/* May as well allocate the whole lot now. */
st->pktin->data = snewn(OUR_V2_PACKETLIMIT + st->maclen + APIEXTRA,
unsigned char);
/* Read an amount corresponding to the MAC. */
for (st->i = 0; st->i < st->maclen; st->i++) {
while ((*datalen) == 0)
crReturn(NULL);
st->pktin->data[st->i] = *(*data)++;
(*datalen)--;
}
st->packetlen = 0;
{
unsigned char seq[4];
ssh->scmac->start(ssh->sc_mac_ctx);
PUT_32BIT(seq, st->incoming_sequence);
ssh->scmac->bytes(ssh->sc_mac_ctx, seq, 4);
}
for (;;) { /* Once around this loop per cipher block. */
/* Read another cipher-block's worth, and tack it onto the end. */
for (st->i = 0; st->i < st->cipherblk; st->i++) {
while ((*datalen) == 0)
crReturn(NULL);
st->pktin->data[st->packetlen+st->maclen+st->i] = *(*data)++;
(*datalen)--;
}
/* Decrypt one more block (a little further back in the stream). */
ssh->sccipher->decrypt(ssh->sc_cipher_ctx,
st->pktin->data + st->packetlen,
st->cipherblk);
/* Feed that block to the MAC. */
ssh->scmac->bytes(ssh->sc_mac_ctx,
st->pktin->data + st->packetlen, st->cipherblk);
st->packetlen += st->cipherblk;
/* See if that gives us a valid packet. */
if (ssh->scmac->verresult(ssh->sc_mac_ctx,
st->pktin->data + st->packetlen) &&
((st->len = toint(GET_32BIT(st->pktin->data))) ==
st->packetlen-4))
break;
if (st->packetlen >= OUR_V2_PACKETLIMIT) {
bombout(("No valid incoming packet found"));
ssh_free_packet(st->pktin);
crStop(NULL);
}
}
st->pktin->maxlen = st->packetlen + st->maclen;
st->pktin->data = sresize(st->pktin->data,
st->pktin->maxlen + APIEXTRA,
unsigned char);
} else if (ssh->scmac && ssh->scmac_etm) {
st->pktin->data = snewn(4 + APIEXTRA, unsigned char);
/*
* OpenSSH encrypt-then-MAC mode: the packet length is
* unencrypted, unless the cipher supports length encryption.
*/
for (st->i = st->len = 0; st->i < 4; st->i++) {
while ((*datalen) == 0)
crReturn(NULL);
st->pktin->data[st->i] = *(*data)++;
(*datalen)--;
}
/* Cipher supports length decryption, so do it */
if (ssh->sccipher && (ssh->sccipher->flags & SSH_CIPHER_SEPARATE_LENGTH)) {
/* Keep the packet the same though, so the MAC passes */
unsigned char len[4];
memcpy(len, st->pktin->data, 4);
ssh->sccipher->decrypt_length(ssh->sc_cipher_ctx, len, 4, st->incoming_sequence);
st->len = toint(GET_32BIT(len));
} else {
st->len = toint(GET_32BIT(st->pktin->data));
}
/*
* _Completely_ silly lengths should be stomped on before they
* do us any more damage.
*/
if (st->len < 0 || st->len > OUR_V2_PACKETLIMIT ||
st->len % st->cipherblk != 0) {
bombout(("Incoming packet length field was garbled"));
ssh_free_packet(st->pktin);
crStop(NULL);
}
/*
* So now we can work out the total packet length.
*/
st->packetlen = st->len + 4;
/*
* Allocate memory for the rest of the packet.
*/
st->pktin->maxlen = st->packetlen + st->maclen;
st->pktin->data = sresize(st->pktin->data,
st->pktin->maxlen + APIEXTRA,
unsigned char);
/*
* Read the remainder of the packet.
*/
for (st->i = 4; st->i < st->packetlen + st->maclen; st->i++) {
while ((*datalen) == 0)
crReturn(NULL);
st->pktin->data[st->i] = *(*data)++;
(*datalen)--;
}
/*
* Check the MAC.
*/
if (ssh->scmac
&& !ssh->scmac->verify(ssh->sc_mac_ctx, st->pktin->data,
st->len + 4, st->incoming_sequence)) {
bombout(("Incorrect MAC received on packet"));
ssh_free_packet(st->pktin);
crStop(NULL);
}
/* Decrypt everything between the length field and the MAC. */
if (ssh->sccipher)
ssh->sccipher->decrypt(ssh->sc_cipher_ctx,
st->pktin->data + 4,
st->packetlen - 4);
} else {
st->pktin->data = snewn(st->cipherblk + APIEXTRA, unsigned char);
/*
* Acquire and decrypt the first block of the packet. This will
* contain the length and padding details.
*/
for (st->i = st->len = 0; st->i < st->cipherblk; st->i++) {
while ((*datalen) == 0)
crReturn(NULL);
st->pktin->data[st->i] = *(*data)++;
(*datalen)--;
}
if (ssh->sccipher)
ssh->sccipher->decrypt(ssh->sc_cipher_ctx,
st->pktin->data, st->cipherblk);
/*
* Now get the length figure.
*/
st->len = toint(GET_32BIT(st->pktin->data));
/*
* _Completely_ silly lengths should be stomped on before they
* do us any more damage.
*/
if (st->len < 0 || st->len > OUR_V2_PACKETLIMIT ||
(st->len + 4) % st->cipherblk != 0) {
bombout(("Incoming packet was garbled on decryption"));
ssh_free_packet(st->pktin);
crStop(NULL);
}
/*
* So now we can work out the total packet length.
*/
st->packetlen = st->len + 4;
/*
* Allocate memory for the rest of the packet.
*/
st->pktin->maxlen = st->packetlen + st->maclen;
st->pktin->data = sresize(st->pktin->data,
st->pktin->maxlen + APIEXTRA,
unsigned char);
/*
* Read and decrypt the remainder of the packet.
*/
for (st->i = st->cipherblk; st->i < st->packetlen + st->maclen;
st->i++) {
while ((*datalen) == 0)
crReturn(NULL);
st->pktin->data[st->i] = *(*data)++;
(*datalen)--;
}
/* Decrypt everything _except_ the MAC. */
if (ssh->sccipher)
ssh->sccipher->decrypt(ssh->sc_cipher_ctx,
st->pktin->data + st->cipherblk,
st->packetlen - st->cipherblk);
/*
* Check the MAC.
*/
if (ssh->scmac
&& !ssh->scmac->verify(ssh->sc_mac_ctx, st->pktin->data,
st->len + 4, st->incoming_sequence)) {
bombout(("Incorrect MAC received on packet"));
ssh_free_packet(st->pktin);
crStop(NULL);
}
}
/* Get and sanity-check the amount of random padding. */
st->pad = st->pktin->data[4];
if (st->pad < 4 || st->len - st->pad < 1) {
bombout(("Invalid padding length on received packet"));
ssh_free_packet(st->pktin);
crStop(NULL);
}
/*
* This enables us to deduce the payload length.
*/
st->payload = st->len - st->pad - 1;
st->pktin->length = st->payload + 5;
st->pktin->encrypted_len = st->packetlen;
st->pktin->sequence = st->incoming_sequence++;
st->pktin->length = st->packetlen - st->pad;
assert(st->pktin->length >= 0);
/*
* Decompress packet payload.
*/
{
unsigned char *newpayload;
int newlen;
if (ssh->sccomp &&
ssh->sccomp->decompress(ssh->sc_comp_ctx,
st->pktin->data + 5, st->pktin->length - 5,
&newpayload, &newlen)) {
if (st->pktin->maxlen < newlen + 5) {
st->pktin->maxlen = newlen + 5;
st->pktin->data = sresize(st->pktin->data,
st->pktin->maxlen + APIEXTRA,
unsigned char);
}
st->pktin->length = 5 + newlen;
memcpy(st->pktin->data + 5, newpayload, newlen);
sfree(newpayload);
}
}
/*
* RFC 4253 doesn't explicitly say that completely empty packets
* with no type byte are forbidden, so treat them as deserving
* an SSH_MSG_UNIMPLEMENTED.
*/
if (st->pktin->length <= 5) { /* == 5 we hope, but robustness */
ssh2_msg_something_unimplemented(ssh, st->pktin);
crStop(NULL);
}
/*
* pktin->body and pktin->length should identify the semantic
* content of the packet, excluding the initial type byte.
*/
st->pktin->type = st->pktin->data[5];
st->pktin->body = st->pktin->data + 6;
st->pktin->length -= 6;
assert(st->pktin->length >= 0); /* one last double-check */
if (ssh->logctx)
ssh2_log_incoming_packet(ssh, st->pktin);
st->pktin->savedpos = 0;
crFinish(st->pktin);
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,820
|
static int ssh_channelcmp(void *av, void *bv)
{
struct ssh_channel *a = (struct ssh_channel *) av;
struct ssh_channel *b = (struct ssh_channel *) bv;
if (a->localid < b->localid)
return -1;
if (a->localid > b->localid)
return +1;
return 0;
}
|
Overflow
| 0
|
static int ssh_channelcmp(void *av, void *bv)
{
struct ssh_channel *a = (struct ssh_channel *) av;
struct ssh_channel *b = (struct ssh_channel *) bv;
if (a->localid < b->localid)
return -1;
if (a->localid > b->localid)
return +1;
return 0;
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,821
|
static int ssh_channelfind(void *av, void *bv)
{
unsigned *a = (unsigned *) av;
struct ssh_channel *b = (struct ssh_channel *) bv;
if (*a < b->localid)
return -1;
if (*a > b->localid)
return +1;
return 0;
}
|
Overflow
| 0
|
static int ssh_channelfind(void *av, void *bv)
{
unsigned *a = (unsigned *) av;
struct ssh_channel *b = (struct ssh_channel *) bv;
if (*a < b->localid)
return -1;
if (*a > b->localid)
return +1;
return 0;
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,822
|
static int ssh_comp_none_block(void *handle, unsigned char *block, int len,
unsigned char **outblock, int *outlen)
{
return 0;
}
|
Overflow
| 0
|
static int ssh_comp_none_block(void *handle, unsigned char *block, int len,
unsigned char **outblock, int *outlen)
{
return 0;
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,823
|
static void ssh_comp_none_cleanup(void *handle)
{
}
|
Overflow
| 0
|
static void ssh_comp_none_cleanup(void *handle)
{
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,824
|
static int ssh_comp_none_disable(void *handle)
{
return 0;
}
|
Overflow
| 0
|
static int ssh_comp_none_disable(void *handle)
{
return 0;
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,825
|
static void *ssh_comp_none_init(void)
{
return NULL;
}
|
Overflow
| 0
|
static void *ssh_comp_none_init(void)
{
return NULL;
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,826
|
void ssh_connshare_log(Ssh ssh, int event, const char *logtext,
const char *ds_err, const char *us_err)
{
if (event == SHARE_NONE) {
/* In this case, 'logtext' is an error message indicating a
* reason why connection sharing couldn't be set up _at all_.
* Failing that, ds_err and us_err indicate why we couldn't be
* a downstream and an upstream respectively. */
if (logtext) {
logeventf(ssh, "Could not set up connection sharing: %s", logtext);
} else {
if (ds_err)
logeventf(ssh, "Could not set up connection sharing"
" as downstream: %s", ds_err);
if (us_err)
logeventf(ssh, "Could not set up connection sharing"
" as upstream: %s", us_err);
}
} else if (event == SHARE_DOWNSTREAM) {
/* In this case, 'logtext' is a local endpoint address */
logeventf(ssh, "Using existing shared connection at %s", logtext);
/* Also we should mention this in the console window to avoid
* confusing users as to why this window doesn't behave the
* usual way. */
if ((flags & FLAG_VERBOSE) || (flags & FLAG_INTERACTIVE)) {
c_write_str(ssh,"Reusing a shared connection to this server.\r\n");
}
} else if (event == SHARE_UPSTREAM) {
/* In this case, 'logtext' is a local endpoint address too */
logeventf(ssh, "Sharing this connection at %s", logtext);
}
}
|
Overflow
| 0
|
void ssh_connshare_log(Ssh ssh, int event, const char *logtext,
const char *ds_err, const char *us_err)
{
if (event == SHARE_NONE) {
/* In this case, 'logtext' is an error message indicating a
* reason why connection sharing couldn't be set up _at all_.
* Failing that, ds_err and us_err indicate why we couldn't be
* a downstream and an upstream respectively. */
if (logtext) {
logeventf(ssh, "Could not set up connection sharing: %s", logtext);
} else {
if (ds_err)
logeventf(ssh, "Could not set up connection sharing"
" as downstream: %s", ds_err);
if (us_err)
logeventf(ssh, "Could not set up connection sharing"
" as upstream: %s", us_err);
}
} else if (event == SHARE_DOWNSTREAM) {
/* In this case, 'logtext' is a local endpoint address */
logeventf(ssh, "Using existing shared connection at %s", logtext);
/* Also we should mention this in the console window to avoid
* confusing users as to why this window doesn't behave the
* usual way. */
if ((flags & FLAG_VERBOSE) || (flags & FLAG_INTERACTIVE)) {
c_write_str(ssh,"Reusing a shared connection to this server.\r\n");
}
} else if (event == SHARE_UPSTREAM) {
/* In this case, 'logtext' is a local endpoint address too */
logeventf(ssh, "Sharing this connection at %s", logtext);
}
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,827
|
static int ssh_do_close(Ssh ssh, int notify_exit)
{
int ret = 0;
struct ssh_channel *c;
ssh->state = SSH_STATE_CLOSED;
expire_timer_context(ssh);
if (ssh->s) {
sk_close(ssh->s);
ssh->s = NULL;
if (notify_exit)
notify_remote_exit(ssh->frontend);
else
ret = 1;
}
/*
* Now we must shut down any port- and X-forwarded channels going
* through this connection.
*/
if (ssh->channels) {
while (NULL != (c = index234(ssh->channels, 0))) {
ssh_channel_close_local(c, NULL);
del234(ssh->channels, c); /* moving next one to index 0 */
if (ssh->version == 2)
bufchain_clear(&c->v.v2.outbuffer);
sfree(c);
}
}
/*
* Go through port-forwardings, and close any associated
* listening sockets.
*/
if (ssh->portfwds) {
struct ssh_portfwd *pf;
while (NULL != (pf = index234(ssh->portfwds, 0))) {
/* Dispose of any listening socket. */
if (pf->local)
pfl_terminate(pf->local);
del234(ssh->portfwds, pf); /* moving next one to index 0 */
free_portfwd(pf);
}
freetree234(ssh->portfwds);
ssh->portfwds = NULL;
}
/*
* Also stop attempting to connection-share.
*/
if (ssh->connshare) {
sharestate_free(ssh->connshare);
ssh->connshare = NULL;
}
return ret;
}
|
Overflow
| 0
|
static int ssh_do_close(Ssh ssh, int notify_exit)
{
int ret = 0;
struct ssh_channel *c;
ssh->state = SSH_STATE_CLOSED;
expire_timer_context(ssh);
if (ssh->s) {
sk_close(ssh->s);
ssh->s = NULL;
if (notify_exit)
notify_remote_exit(ssh->frontend);
else
ret = 1;
}
/*
* Now we must shut down any port- and X-forwarded channels going
* through this connection.
*/
if (ssh->channels) {
while (NULL != (c = index234(ssh->channels, 0))) {
ssh_channel_close_local(c, NULL);
del234(ssh->channels, c); /* moving next one to index 0 */
if (ssh->version == 2)
bufchain_clear(&c->v.v2.outbuffer);
sfree(c);
}
}
/*
* Go through port-forwardings, and close any associated
* listening sockets.
*/
if (ssh->portfwds) {
struct ssh_portfwd *pf;
while (NULL != (pf = index234(ssh->portfwds, 0))) {
/* Dispose of any listening socket. */
if (pf->local)
pfl_terminate(pf->local);
del234(ssh->portfwds, pf); /* moving next one to index 0 */
free_portfwd(pf);
}
freetree234(ssh->portfwds);
ssh->portfwds = NULL;
}
/*
* Also stop attempting to connection-share.
*/
if (ssh->connshare) {
sharestate_free(ssh->connshare);
ssh->connshare = NULL;
}
return ret;
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,828
|
static void ssh_fix_verstring(char *str)
{
/* Eat "<protoversion>-". */
while (*str && *str != '-') str++;
assert(*str == '-'); str++;
/* Convert minus signs and spaces in the remaining string into
* underscores. */
while (*str) {
if (*str == '-' || *str == ' ')
*str = '_';
str++;
}
}
|
Overflow
| 0
|
static void ssh_fix_verstring(char *str)
{
/* Eat "<protoversion>-". */
while (*str && *str != '-') str++;
assert(*str == '-'); str++;
/* Convert minus signs and spaces in the remaining string into
* underscores. */
while (*str) {
if (*str == '-' || *str == ' ')
*str = '_';
str++;
}
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,829
|
static void ssh_free_packet(struct Packet *pkt)
{
sfree(pkt->data);
sfree(pkt);
}
|
Overflow
| 0
|
static void ssh_free_packet(struct Packet *pkt)
{
sfree(pkt->data);
sfree(pkt);
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,830
|
static void ssh_gotdata(Ssh ssh, const unsigned char *data, int datalen)
{
/* Log raw data, if we're in that mode. */
if (ssh->logctx)
log_packet(ssh->logctx, PKT_INCOMING, -1, NULL, data, datalen,
0, NULL, NULL, 0, NULL);
crBegin(ssh->ssh_gotdata_crstate);
/*
* To begin with, feed the characters one by one to the
* protocol initialisation / selection function do_ssh_init().
* When that returns 0, we're done with the initial greeting
* exchange and can move on to packet discipline.
*/
while (1) {
int ret; /* need not be kept across crReturn */
if (datalen == 0)
crReturnV; /* more data please */
ret = ssh->do_ssh_init(ssh, *data);
data++;
datalen--;
if (ret == 0)
break;
}
/*
* We emerge from that loop when the initial negotiation is
* over and we have selected an s_rdpkt function. Now pass
* everything to s_rdpkt, and then pass the resulting packets
* to the proper protocol handler.
*/
while (1) {
while (bufchain_size(&ssh->queued_incoming_data) > 0 || datalen > 0) {
if (ssh->frozen) {
ssh_queue_incoming_data(ssh, &data, &datalen);
/* This uses up all data and cannot cause anything interesting
* to happen; indeed, for anything to happen at all, we must
* return, so break out. */
break;
} else if (bufchain_size(&ssh->queued_incoming_data) > 0) {
/* This uses up some or all data, and may freeze the
* session. */
ssh_process_queued_incoming_data(ssh);
} else {
/* This uses up some or all data, and may freeze the
* session. */
ssh_process_incoming_data(ssh, &data, &datalen);
}
/* FIXME this is probably EBW. */
if (ssh->state == SSH_STATE_CLOSED)
return;
}
/* We're out of data. Go and get some more. */
crReturnV;
}
crFinishV;
}
|
Overflow
| 0
|
static void ssh_gotdata(Ssh ssh, const unsigned char *data, int datalen)
{
/* Log raw data, if we're in that mode. */
if (ssh->logctx)
log_packet(ssh->logctx, PKT_INCOMING, -1, NULL, data, datalen,
0, NULL, NULL, 0, NULL);
crBegin(ssh->ssh_gotdata_crstate);
/*
* To begin with, feed the characters one by one to the
* protocol initialisation / selection function do_ssh_init().
* When that returns 0, we're done with the initial greeting
* exchange and can move on to packet discipline.
*/
while (1) {
int ret; /* need not be kept across crReturn */
if (datalen == 0)
crReturnV; /* more data please */
ret = ssh->do_ssh_init(ssh, *data);
data++;
datalen--;
if (ret == 0)
break;
}
/*
* We emerge from that loop when the initial negotiation is
* over and we have selected an s_rdpkt function. Now pass
* everything to s_rdpkt, and then pass the resulting packets
* to the proper protocol handler.
*/
while (1) {
while (bufchain_size(&ssh->queued_incoming_data) > 0 || datalen > 0) {
if (ssh->frozen) {
ssh_queue_incoming_data(ssh, &data, &datalen);
/* This uses up all data and cannot cause anything interesting
* to happen; indeed, for anything to happen at all, we must
* return, so break out. */
break;
} else if (bufchain_size(&ssh->queued_incoming_data) > 0) {
/* This uses up some or all data, and may freeze the
* session. */
ssh_process_queued_incoming_data(ssh);
} else {
/* This uses up some or all data, and may freeze the
* session. */
ssh_process_incoming_data(ssh, &data, &datalen);
}
/* FIXME this is probably EBW. */
if (ssh->state == SSH_STATE_CLOSED)
return;
}
/* We're out of data. Go and get some more. */
crReturnV;
}
crFinishV;
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,831
|
static void ssh_hostport_setup(const char *host, int port, Conf *conf,
char **savedhost, int *savedport,
char **loghost_ret)
{
char *loghost = conf_get_str(conf, CONF_loghost);
if (loghost_ret)
*loghost_ret = loghost;
if (*loghost) {
char *tmphost;
char *colon;
tmphost = dupstr(loghost);
*savedport = 22; /* default ssh port */
/*
* A colon suffix on the hostname string also lets us affect
* savedport. (Unless there are multiple colons, in which case
* we assume this is an unbracketed IPv6 literal.)
*/
colon = host_strrchr(tmphost, ':');
if (colon && colon == host_strchr(tmphost, ':')) {
*colon++ = '\0';
if (*colon)
*savedport = atoi(colon);
}
*savedhost = host_strduptrim(tmphost);
sfree(tmphost);
} else {
*savedhost = host_strduptrim(host);
if (port < 0)
port = 22; /* default ssh port */
*savedport = port;
}
}
|
Overflow
| 0
|
static void ssh_hostport_setup(const char *host, int port, Conf *conf,
char **savedhost, int *savedport,
char **loghost_ret)
{
char *loghost = conf_get_str(conf, CONF_loghost);
if (loghost_ret)
*loghost_ret = loghost;
if (*loghost) {
char *tmphost;
char *colon;
tmphost = dupstr(loghost);
*savedport = 22; /* default ssh port */
/*
* A colon suffix on the hostname string also lets us affect
* savedport. (Unless there are multiple colons, in which case
* we assume this is an unbracketed IPv6 literal.)
*/
colon = host_strrchr(tmphost, ':');
if (colon && colon == host_strchr(tmphost, ':')) {
*colon++ = '\0';
if (*colon)
*savedport = atoi(colon);
}
*savedhost = host_strduptrim(tmphost);
sfree(tmphost);
} else {
*savedhost = host_strduptrim(host);
if (port < 0)
port = 22; /* default ssh port */
*savedport = port;
}
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,832
|
static struct Packet *ssh_new_packet(void)
{
struct Packet *pkt = snew(struct Packet);
pkt->body = pkt->data = NULL;
pkt->maxlen = 0;
return pkt;
}
|
Overflow
| 0
|
static struct Packet *ssh_new_packet(void)
{
struct Packet *pkt = snew(struct Packet);
pkt->body = pkt->data = NULL;
pkt->maxlen = 0;
return pkt;
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,833
|
static void ssh_pkt_addbyte(struct Packet *pkt, unsigned char byte)
{
ssh_pkt_adddata(pkt, &byte, 1);
}
|
Overflow
| 0
|
static void ssh_pkt_addbyte(struct Packet *pkt, unsigned char byte)
{
ssh_pkt_adddata(pkt, &byte, 1);
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,834
|
static void ssh_pkt_adddata(struct Packet *pkt, const void *data, int len)
{
pkt->length += len;
ssh_pkt_ensure(pkt, pkt->length);
memcpy(pkt->data + pkt->length - len, data, len);
}
|
Overflow
| 0
|
static void ssh_pkt_adddata(struct Packet *pkt, const void *data, int len)
{
pkt->length += len;
ssh_pkt_ensure(pkt, pkt->length);
memcpy(pkt->data + pkt->length - len, data, len);
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,835
|
static void ssh_pkt_addstring(struct Packet *pkt, const char *data)
{
ssh_pkt_addstring_start(pkt);
ssh_pkt_addstring_str(pkt, data);
}
|
Overflow
| 0
|
static void ssh_pkt_addstring(struct Packet *pkt, const char *data)
{
ssh_pkt_addstring_start(pkt);
ssh_pkt_addstring_str(pkt, data);
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,836
|
static void ssh_pkt_addstring_str(struct Packet *pkt, const char *data)
{
ssh_pkt_addstring_data(pkt, data, strlen(data));
}
|
Overflow
| 0
|
static void ssh_pkt_addstring_str(struct Packet *pkt, const char *data)
{
ssh_pkt_addstring_data(pkt, data, strlen(data));
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,837
|
static void ssh_pkt_adduint32(struct Packet *pkt, unsigned long value)
{
unsigned char x[4];
PUT_32BIT(x, value);
ssh_pkt_adddata(pkt, x, 4);
}
|
Overflow
| 0
|
static void ssh_pkt_adduint32(struct Packet *pkt, unsigned long value)
{
unsigned char x[4];
PUT_32BIT(x, value);
ssh_pkt_adddata(pkt, x, 4);
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,838
|
static void ssh_pkt_defersend(Ssh ssh)
{
int backlog;
backlog = s_write(ssh, ssh->deferred_send_data, ssh->deferred_len);
ssh->deferred_len = ssh->deferred_size = 0;
sfree(ssh->deferred_send_data);
ssh->deferred_send_data = NULL;
if (backlog > SSH_MAX_BACKLOG)
ssh_throttle_all(ssh, 1, backlog);
if (ssh->version == 2) {
ssh->outgoing_data_size += ssh->deferred_data_size;
ssh->deferred_data_size = 0;
if (!ssh->kex_in_progress &&
!ssh->bare_connection &&
ssh->max_data_size != 0 &&
ssh->outgoing_data_size > ssh->max_data_size)
do_ssh2_transport(ssh, "too much data sent", -1, NULL);
}
}
|
Overflow
| 0
|
static void ssh_pkt_defersend(Ssh ssh)
{
int backlog;
backlog = s_write(ssh, ssh->deferred_send_data, ssh->deferred_len);
ssh->deferred_len = ssh->deferred_size = 0;
sfree(ssh->deferred_send_data);
ssh->deferred_send_data = NULL;
if (backlog > SSH_MAX_BACKLOG)
ssh_throttle_all(ssh, 1, backlog);
if (ssh->version == 2) {
ssh->outgoing_data_size += ssh->deferred_data_size;
ssh->deferred_data_size = 0;
if (!ssh->kex_in_progress &&
!ssh->bare_connection &&
ssh->max_data_size != 0 &&
ssh->outgoing_data_size > ssh->max_data_size)
do_ssh2_transport(ssh, "too much data sent", -1, NULL);
}
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,839
|
static void ssh_pkt_ensure(struct Packet *pkt, int length)
{
if (pkt->maxlen < length) {
unsigned char *body = pkt->body;
int offset = body ? body - pkt->data : 0;
pkt->maxlen = length + 256;
pkt->data = sresize(pkt->data, pkt->maxlen + APIEXTRA, unsigned char);
if (body) pkt->body = pkt->data + offset;
}
}
|
Overflow
| 0
|
static void ssh_pkt_ensure(struct Packet *pkt, int length)
{
if (pkt->maxlen < length) {
unsigned char *body = pkt->body;
int offset = body ? body - pkt->data : 0;
pkt->maxlen = length + 256;
pkt->data = sresize(pkt->data, pkt->maxlen + APIEXTRA, unsigned char);
if (body) pkt->body = pkt->data + offset;
}
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,840
|
static void *ssh_pkt_getdata(struct Packet *pkt, int length)
{
if (pkt->length - pkt->savedpos < length)
return NULL;
pkt->savedpos += length;
return pkt->body + (pkt->savedpos - length);
}
|
Overflow
| 0
|
static void *ssh_pkt_getdata(struct Packet *pkt, int length)
{
if (pkt->length - pkt->savedpos < length)
return NULL;
pkt->savedpos += length;
return pkt->body + (pkt->savedpos - length);
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,841
|
static void ssh_pkt_getstring(struct Packet *pkt, char **p, int *length)
{
int len;
*p = NULL;
*length = 0;
if (pkt->length - pkt->savedpos < 4)
return;
len = toint(GET_32BIT(pkt->body + pkt->savedpos));
if (len < 0)
return;
*length = len;
pkt->savedpos += 4;
if (pkt->length - pkt->savedpos < *length)
return;
*p = (char *)(pkt->body + pkt->savedpos);
pkt->savedpos += *length;
}
|
Overflow
| 0
|
static void ssh_pkt_getstring(struct Packet *pkt, char **p, int *length)
{
int len;
*p = NULL;
*length = 0;
if (pkt->length - pkt->savedpos < 4)
return;
len = toint(GET_32BIT(pkt->body + pkt->savedpos));
if (len < 0)
return;
*length = len;
pkt->savedpos += 4;
if (pkt->length - pkt->savedpos < *length)
return;
*p = (char *)(pkt->body + pkt->savedpos);
pkt->savedpos += *length;
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,842
|
static const char *ssh_pkt_type(Ssh ssh, int type)
{
if (ssh->version == 1)
return ssh1_pkt_type(type);
else
return ssh2_pkt_type(ssh->pkt_kctx, ssh->pkt_actx, type);
}
|
Overflow
| 0
|
static const char *ssh_pkt_type(Ssh ssh, int type)
{
if (ssh->version == 1)
return ssh1_pkt_type(type);
else
return ssh2_pkt_type(ssh->pkt_kctx, ssh->pkt_actx, type);
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,843
|
static int ssh_portcmp(void *av, void *bv)
{
struct ssh_portfwd *a = (struct ssh_portfwd *) av;
struct ssh_portfwd *b = (struct ssh_portfwd *) bv;
int i;
if (a->type > b->type)
return +1;
if (a->type < b->type)
return -1;
if (a->addressfamily > b->addressfamily)
return +1;
if (a->addressfamily < b->addressfamily)
return -1;
if ( (i = nullstrcmp(a->saddr, b->saddr)) != 0)
return i < 0 ? -1 : +1;
if (a->sport > b->sport)
return +1;
if (a->sport < b->sport)
return -1;
if (a->type != 'D') {
if ( (i = nullstrcmp(a->daddr, b->daddr)) != 0)
return i < 0 ? -1 : +1;
if (a->dport > b->dport)
return +1;
if (a->dport < b->dport)
return -1;
}
return 0;
}
|
Overflow
| 0
|
static int ssh_portcmp(void *av, void *bv)
{
struct ssh_portfwd *a = (struct ssh_portfwd *) av;
struct ssh_portfwd *b = (struct ssh_portfwd *) bv;
int i;
if (a->type > b->type)
return +1;
if (a->type < b->type)
return -1;
if (a->addressfamily > b->addressfamily)
return +1;
if (a->addressfamily < b->addressfamily)
return -1;
if ( (i = nullstrcmp(a->saddr, b->saddr)) != 0)
return i < 0 ? -1 : +1;
if (a->sport > b->sport)
return +1;
if (a->sport < b->sport)
return -1;
if (a->type != 'D') {
if ( (i = nullstrcmp(a->daddr, b->daddr)) != 0)
return i < 0 ? -1 : +1;
if (a->dport > b->dport)
return +1;
if (a->dport < b->dport)
return -1;
}
return 0;
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,844
|
static void ssh_process_incoming_data(Ssh ssh,
const unsigned char **data, int *datalen)
{
struct Packet *pktin;
pktin = ssh->s_rdpkt(ssh, data, datalen);
if (pktin) {
ssh->protocol(ssh, NULL, 0, pktin);
ssh_free_packet(pktin);
}
}
|
Overflow
| 0
|
static void ssh_process_incoming_data(Ssh ssh,
const unsigned char **data, int *datalen)
{
struct Packet *pktin;
pktin = ssh->s_rdpkt(ssh, data, datalen);
if (pktin) {
ssh->protocol(ssh, NULL, 0, pktin);
ssh_free_packet(pktin);
}
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,845
|
static void ssh_queue_incoming_data(Ssh ssh,
const unsigned char **data, int *datalen)
{
bufchain_add(&ssh->queued_incoming_data, *data, *datalen);
*data += *datalen;
*datalen = 0;
}
|
Overflow
| 0
|
static void ssh_queue_incoming_data(Ssh ssh,
const unsigned char **data, int *datalen)
{
bufchain_add(&ssh->queued_incoming_data, *data, *datalen);
*data += *datalen;
*datalen = 0;
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,846
|
static int ssh_receive(Plug plug, int urgent, char *data, int len)
{
Ssh ssh = (Ssh) plug;
ssh_gotdata(ssh, (unsigned char *)data, len);
if (ssh->state == SSH_STATE_CLOSED) {
ssh_do_close(ssh, TRUE);
return 0;
}
return 1;
}
|
Overflow
| 0
|
static int ssh_receive(Plug plug, int urgent, char *data, int len)
{
Ssh ssh = (Ssh) plug;
ssh_gotdata(ssh, (unsigned char *)data, len);
if (ssh->state == SSH_STATE_CLOSED) {
ssh_do_close(ssh, TRUE);
return 0;
}
return 1;
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,847
|
static int ssh_rportcmp_ssh1(void *av, void *bv)
{
struct ssh_rportfwd *a = (struct ssh_rportfwd *) av;
struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv;
int i;
if ( (i = strcmp(a->dhost, b->dhost)) != 0)
return i < 0 ? -1 : +1;
if (a->dport > b->dport)
return +1;
if (a->dport < b->dport)
return -1;
return 0;
}
|
Overflow
| 0
|
static int ssh_rportcmp_ssh1(void *av, void *bv)
{
struct ssh_rportfwd *a = (struct ssh_rportfwd *) av;
struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv;
int i;
if ( (i = strcmp(a->dhost, b->dhost)) != 0)
return i < 0 ? -1 : +1;
if (a->dport > b->dport)
return +1;
if (a->dport < b->dport)
return -1;
return 0;
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,848
|
static int ssh_rportcmp_ssh2(void *av, void *bv)
{
struct ssh_rportfwd *a = (struct ssh_rportfwd *) av;
struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv;
int i;
if ( (i = strcmp(a->shost, b->shost)) != 0)
return i < 0 ? -1 : +1;
if (a->sport > b->sport)
return +1;
if (a->sport < b->sport)
return -1;
return 0;
}
|
Overflow
| 0
|
static int ssh_rportcmp_ssh2(void *av, void *bv)
{
struct ssh_rportfwd *a = (struct ssh_rportfwd *) av;
struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv;
int i;
if ( (i = strcmp(a->shost, b->shost)) != 0)
return i < 0 ? -1 : +1;
if (a->sport > b->sport)
return +1;
if (a->sport < b->sport)
return -1;
return 0;
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,849
|
static void ssh_send_verstring(Ssh ssh, const char *protoname, char *svers)
{
char *verstring;
if (ssh->version == 2) {
/*
* Construct a v2 version string.
*/
verstring = dupprintf("%s2.0-%s\015\012", protoname, sshver);
} else {
/*
* Construct a v1 version string.
*/
assert(!strcmp(protoname, "SSH-")); /* no v1 bare connection protocol */
verstring = dupprintf("SSH-%s-%s\012",
(ssh_versioncmp(svers, "1.5") <= 0 ?
svers : "1.5"),
sshver);
}
ssh_fix_verstring(verstring + strlen(protoname));
#ifdef FUZZING
/* FUZZING make PuTTY insecure, so make live use difficult. */
verstring[0] = 'I';
#endif
if (ssh->version == 2) {
size_t len;
/*
* Record our version string.
*/
len = strcspn(verstring, "\015\012");
ssh->v_c = snewn(len + 1, char);
memcpy(ssh->v_c, verstring, len);
ssh->v_c[len] = 0;
}
logeventf(ssh, "We claim version: %.*s",
strcspn(verstring, "\015\012"), verstring);
s_write(ssh, verstring, strlen(verstring));
sfree(verstring);
}
|
Overflow
| 0
|
static void ssh_send_verstring(Ssh ssh, const char *protoname, char *svers)
{
char *verstring;
if (ssh->version == 2) {
/*
* Construct a v2 version string.
*/
verstring = dupprintf("%s2.0-%s\015\012", protoname, sshver);
} else {
/*
* Construct a v1 version string.
*/
assert(!strcmp(protoname, "SSH-")); /* no v1 bare connection protocol */
verstring = dupprintf("SSH-%s-%s\012",
(ssh_versioncmp(svers, "1.5") <= 0 ?
svers : "1.5"),
sshver);
}
ssh_fix_verstring(verstring + strlen(protoname));
#ifdef FUZZING
/* FUZZING make PuTTY insecure, so make live use difficult. */
verstring[0] = 'I';
#endif
if (ssh->version == 2) {
size_t len;
/*
* Record our version string.
*/
len = strcspn(verstring, "\015\012");
ssh->v_c = snewn(len + 1, char);
memcpy(ssh->v_c, verstring, len);
ssh->v_c[len] = 0;
}
logeventf(ssh, "We claim version: %.*s",
strcspn(verstring, "\015\012"), verstring);
s_write(ssh, verstring, strlen(verstring));
sfree(verstring);
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,850
|
static void ssh_sent(Plug plug, int bufsize)
{
Ssh ssh = (Ssh) plug;
/*
* If the send backlog on the SSH socket itself clears, we
* should unthrottle the whole world if it was throttled.
*/
if (bufsize < SSH_MAX_BACKLOG)
ssh_throttle_all(ssh, 0, bufsize);
}
|
Overflow
| 0
|
static void ssh_sent(Plug plug, int bufsize)
{
Ssh ssh = (Ssh) plug;
/*
* If the send backlog on the SSH socket itself clears, we
* should unthrottle the whole world if it was throttled.
*/
if (bufsize < SSH_MAX_BACKLOG)
ssh_throttle_all(ssh, 0, bufsize);
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,851
|
static void ssh_set_frozen(Ssh ssh, int frozen)
{
if (ssh->s)
sk_set_frozen(ssh->s, frozen);
ssh->frozen = frozen;
}
|
Overflow
| 0
|
static void ssh_set_frozen(Ssh ssh, int frozen)
{
if (ssh->s)
sk_set_frozen(ssh->s, frozen);
ssh->frozen = frozen;
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,852
|
static void ssh_socket_log(Plug plug, int type, SockAddr addr, int port,
const char *error_msg, int error_code)
{
Ssh ssh = (Ssh) plug;
/*
* While we're attempting connection sharing, don't loudly log
* everything that happens. Real TCP connections need to be logged
* when we _start_ trying to connect, because it might be ages
* before they respond if something goes wrong; but connection
* sharing is local and quick to respond, and it's sufficient to
* simply wait and see whether it worked afterwards.
*/
if (!ssh->attempting_connshare)
backend_socket_log(ssh->frontend, type, addr, port,
error_msg, error_code, ssh->conf,
ssh->session_started);
}
|
Overflow
| 0
|
static void ssh_socket_log(Plug plug, int type, SockAddr addr, int port,
const char *error_msg, int error_code)
{
Ssh ssh = (Ssh) plug;
/*
* While we're attempting connection sharing, don't loudly log
* everything that happens. Real TCP connections need to be logged
* when we _start_ trying to connect, because it might be ages
* before they respond if something goes wrong; but connection
* sharing is local and quick to respond, and it's sufficient to
* simply wait and see whether it worked afterwards.
*/
if (!ssh->attempting_connshare)
backend_socket_log(ssh->frontend, type, addr, port,
error_msg, error_code, ssh->conf,
ssh->session_started);
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,853
|
static unsigned int ssh_tty_parse_boolean(char *s)
{
if (stricmp(s, "yes") == 0 ||
stricmp(s, "on") == 0 ||
stricmp(s, "true") == 0 ||
stricmp(s, "+") == 0)
return 1; /* true */
else if (stricmp(s, "no") == 0 ||
stricmp(s, "off") == 0 ||
stricmp(s, "false") == 0 ||
stricmp(s, "-") == 0)
return 0; /* false */
else
return (atoi(s) != 0);
}
|
Overflow
| 0
|
static unsigned int ssh_tty_parse_boolean(char *s)
{
if (stricmp(s, "yes") == 0 ||
stricmp(s, "on") == 0 ||
stricmp(s, "true") == 0 ||
stricmp(s, "+") == 0)
return 1; /* true */
else if (stricmp(s, "no") == 0 ||
stricmp(s, "off") == 0 ||
stricmp(s, "false") == 0 ||
stricmp(s, "-") == 0)
return 0; /* false */
else
return (atoi(s) != 0);
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,854
|
static unsigned int ssh_tty_parse_specchar(char *s)
{
unsigned int ret;
if (*s) {
char *next = NULL;
ret = ctrlparse(s, &next);
if (!next) ret = s[0];
} else {
ret = 255; /* special value meaning "don't set" */
}
return ret;
}
|
Overflow
| 0
|
static unsigned int ssh_tty_parse_specchar(char *s)
{
unsigned int ret;
if (*s) {
char *next = NULL;
ret = ctrlparse(s, &next);
if (!next) ret = s[0];
} else {
ret = 255; /* special value meaning "don't set" */
}
return ret;
}
|
@@ -573,10 +573,7 @@ struct ssh_channel {
} v;
union {
struct ssh_agent_channel {
- unsigned char *message;
- unsigned char msglen[4];
- unsigned lensofar, totallen;
- int outstanding_requests;
+ bufchain inbuffer;
agent_pending_query *pending;
} a;
struct ssh_x11_channel {
@@ -3780,6 +3777,8 @@ static void ssh_throttle_conn(Ssh ssh, int adjust)
}
}
+static void ssh_agentf_try_forward(struct ssh_channel *c);
+
/*
* Throttle or unthrottle _all_ local data streams (for when sends
* on the SSH connection itself back up).
@@ -3806,7 +3805,12 @@ static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
x11_override_throttle(c->u.x11.xconn, enable);
break;
case CHAN_AGENT:
- /* Agent channels require no buffer management. */
+ /* Agent forwarding channels are buffer-managed by
+ * checking ssh->throttled_all in ssh_agentf_try_forward.
+ * So at the moment we _un_throttle again, we must make an
+ * attempt to do something. */
+ if (!enable)
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_override_throttle(c->u.pfd.pf, enable);
@@ -3848,29 +3852,113 @@ static void ssh_dialog_callback(void *sshv, int ret)
ssh_process_queued_incoming_data(ssh);
}
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+static void ssh_agentf_got_response(struct ssh_channel *c,
+ void *reply, int replylen)
{
- struct ssh_channel *c = (struct ssh_channel *)cv;
- const void *sentreply = reply;
-
c->u.a.pending = NULL;
- c->u.a.outstanding_requests--;
- if (!sentreply) {
- /* Fake SSH_AGENT_FAILURE. */
- sentreply = "\0\0\0\1\5";
+
+ if (!reply) {
+ /* The real agent didn't send any kind of reply at all for
+ * some reason, so fake an SSH_AGENT_FAILURE. */
+ reply = "\0\0\0\1\5";
replylen = 5;
}
- ssh_send_channel_data(c, sentreply, replylen);
- if (reply)
- sfree(reply);
+
+ ssh_send_channel_data(c, reply, replylen);
+}
+
+static void ssh_agentf_callback(void *cv, void *reply, int replylen);
+
+static void ssh_agentf_try_forward(struct ssh_channel *c)
+{
+ unsigned datalen, lengthfield, messagelen;
+ unsigned char *message;
+ unsigned char msglen[4];
+ void *reply;
+ int replylen;
+
/*
- * If we've already seen an incoming EOF but haven't sent an
- * outgoing one, this may be the moment to send it.
+ * Don't try to parallelise agent requests. Wait for each one to
+ * return before attempting the next.
*/
- if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF))
+ if (c->u.a.pending)
+ return;
+
+ /*
+ * If the outgoing side of the channel connection is currently
+ * throttled (for any reason, either that channel's window size or
+ * the entire SSH connection being throttled), don't submit any
+ * new forwarded requests to the real agent. This causes the input
+ * side of the agent forwarding not to be emptied, exerting the
+ * required back-pressure on the remote client, and encouraging it
+ * to read our responses before sending too many more requests.
+ */
+ if (c->ssh->throttled_all ||
+ (c->ssh->version == 2 && c->v.v2.remwindow == 0))
+ return;
+
+ while (1) {
+ /*
+ * Try to extract a complete message from the input buffer.
+ */
+ datalen = bufchain_size(&c->u.a.inbuffer);
+ if (datalen < 4)
+ break; /* not even a length field available yet */
+
+ bufchain_fetch(&c->u.a.inbuffer, msglen, 4);
+ lengthfield = GET_32BIT(msglen);
+ if (lengthfield > datalen - 4)
+ break; /* a whole message is not yet available */
+
+ messagelen = lengthfield + 4;
+
+ message = snewn(messagelen, unsigned char);
+ bufchain_fetch(&c->u.a.inbuffer, message, messagelen);
+ bufchain_consume(&c->u.a.inbuffer, messagelen);
+ c->u.a.pending = agent_query(
+ message, messagelen, &reply, &replylen, ssh_agentf_callback, c);
+ sfree(message);
+
+ if (c->u.a.pending)
+ return; /* agent_query promised to reply in due course */
+
+ /*
+ * If the agent gave us an answer immediately, pass it
+ * straight on and go round this loop again.
+ */
+ ssh_agentf_got_response(c, reply, replylen);
+ }
+
+ /*
+ * If we get here (i.e. we left the above while loop via 'break'
+ * rather than 'return'), that means we've determined that the
+ * input buffer for the agent forwarding connection doesn't
+ * contain a complete request.
+ *
+ * So if there's potentially more data to come, we can return now,
+ * and wait for the remote client to send it. But if the remote
+ * has sent EOF, it would be a mistake to do that, because we'd be
+ * waiting a long time. So this is the moment to check for EOF,
+ * and respond appropriately.
+ */
+ if (c->closes & CLOSES_RCVD_EOF)
sshfwd_write_eof(c);
}
+static void ssh_agentf_callback(void *cv, void *reply, int replylen)
+{
+ struct ssh_channel *c = (struct ssh_channel *)cv;
+
+ ssh_agentf_got_response(c, reply, replylen);
+ sfree(reply);
+
+ /*
+ * Now try to extract and send further messages from the channel's
+ * input-side buffer.
+ */
+ ssh_agentf_try_forward(c);
+}
+
/*
* Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
* non-NULL, otherwise just close the connection. `client_reason' == NULL
@@ -5553,10 +5641,8 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
c->remoteid = remoteid;
c->halfopen = FALSE;
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
+ bufchain_init(&c->u.a.inbuffer);
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
PKT_INT, c->remoteid, PKT_INT, c->localid,
PKT_END);
@@ -5697,42 +5783,18 @@ static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
static int ssh_agent_channel_data(struct ssh_channel *c, char *data,
int length)
{
- while (length > 0) {
- if (c->u.a.lensofar < 4) {
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)length);
- memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == 4) {
- c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = snewn(c->u.a.totallen, unsigned char);
- memcpy(c->u.a.message, c->u.a.msglen, 4);
- }
- if (c->u.a.lensofar >= 4 && length > 0) {
- unsigned int l = min(c->u.a.totallen - c->u.a.lensofar,
- (unsigned)length);
- memcpy(c->u.a.message + c->u.a.lensofar, data, l);
- data += l;
- length -= l;
- c->u.a.lensofar += l;
- }
- if (c->u.a.lensofar == c->u.a.totallen) {
- void *reply;
- int replylen;
- c->u.a.outstanding_requests++;
- c->u.a.pending = agent_query(
- c->u.a.message, c->u.a.totallen, &reply, &replylen,
- ssh_agentf_callback, c);
- if (!c->u.a.pending)
- ssh_agentf_callback(c, reply, replylen);
- sfree(c->u.a.message);
- c->u.a.message = NULL;
- c->u.a.lensofar = 0;
- }
- }
- return 0; /* agent channels never back up */
+ bufchain_add(&c->u.a.inbuffer, data, length);
+ ssh_agentf_try_forward(c);
+
+ /*
+ * We exert back-pressure on an agent forwarding client if and
+ * only if we're waiting for the response to an asynchronous agent
+ * request. This prevents the client running out of window while
+ * receiving the _first_ message, but means that if any message
+ * takes time to process, the client will be discouraged from
+ * sending an endless stream of further ones after it.
+ */
+ return (c->u.a.pending ? bufchain_size(&c->u.a.inbuffer) : 0);
}
static int ssh_channel_data(struct ssh_channel *c, int is_stderr,
@@ -7733,8 +7795,9 @@ static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
x11_unthrottle(c->u.x11.xconn);
break;
case CHAN_AGENT:
- /* agent sockets are request/response and need no
- * buffer management */
+ /* Now that we've successfully sent all the outgoing
+ * replies we had, try to process more incoming data. */
+ ssh_agentf_try_forward(c);
break;
case CHAN_SOCKDATA:
pfd_unthrottle(c->u.pfd.pf);
@@ -8160,7 +8223,8 @@ static void ssh_channel_close_local(struct ssh_channel *c, char const *reason)
case CHAN_AGENT:
if (c->u.a.pending)
agent_cancel_query(c->u.a.pending);
- sfree(c->u.a.message);
+ bufchain_clear(&c->u.a.inbuffer);
+ msg = "Agent-forwarding connection closed";
break;
case CHAN_SOCKDATA:
assert(c->u.pfd.pf != NULL);
@@ -8248,10 +8312,10 @@ static void ssh_channel_got_eof(struct ssh_channel *c)
assert(c->u.x11.xconn != NULL);
x11_send_eof(c->u.x11.xconn);
} else if (c->type == CHAN_AGENT) {
- if (c->u.a.outstanding_requests == 0) {
- /* Manufacture an outgoing EOF in response to the incoming one. */
- sshfwd_write_eof(c);
- }
+ /* Just call try_forward, which will respond to the EOF now if
+ * appropriate, or wait until the queue of outstanding
+ * requests is dealt with if not */
+ ssh_agentf_try_forward(c);
} else if (c->type == CHAN_SOCKDATA) {
assert(c->u.pfd.pf != NULL);
pfd_send_eof(c->u.pfd.pf);
@@ -8805,10 +8869,8 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
error = "Agent forwarding is not enabled";
else {
c->type = CHAN_AGENT; /* identify channel type */
- c->u.a.lensofar = 0;
- c->u.a.message = NULL;
+ bufchain_init(&c->u.a.inbuffer);
c->u.a.pending = NULL;
- c->u.a.outstanding_requests = 0;
}
} else {
error = "Unsupported channel type requested";
|
CWE-119
| null | null |
6,855
|
void agent_cancel_query(agent_pending_query *q)
{
assert(0 && "Windows agent queries are never asynchronous!");
}
|
Overflow
| 0
|
void agent_cancel_query(agent_pending_query *q)
{
assert(0 && "Windows agent queries are never asynchronous!");
}
|
@@ -4,6 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
+#include <assert.h>
#include "putty.h"
|
CWE-119
| null | null |
6,856
|
int agent_exists(void)
{
HWND hwnd;
hwnd = FindWindow("Pageant", "Pageant");
if (!hwnd)
return FALSE;
else
return TRUE;
}
|
Overflow
| 0
|
int agent_exists(void)
{
HWND hwnd;
hwnd = FindWindow("Pageant", "Pageant");
if (!hwnd)
return FALSE;
else
return TRUE;
}
|
@@ -4,6 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
+#include <assert.h>
#include "putty.h"
|
CWE-119
| null | null |
6,857
|
agent_pending_query *agent_query(
void *in, int inlen, void **out, int *outlen,
void (*callback)(void *, void *, int), void *callback_ctx)
{
HWND hwnd;
char *mapname;
HANDLE filemap;
unsigned char *p, *ret;
int id, retlen;
COPYDATASTRUCT cds;
SECURITY_ATTRIBUTES sa, *psa;
PSECURITY_DESCRIPTOR psd = NULL;
PSID usersid = NULL;
*out = NULL;
*outlen = 0;
hwnd = FindWindow("Pageant", "Pageant");
if (!hwnd)
return NULL; /* *out == NULL, so failure */
mapname = dupprintf("PageantRequest%08x", (unsigned)GetCurrentThreadId());
psa = NULL;
#ifndef NO_SECURITY
if (got_advapi()) {
/*
* Make the file mapping we create for communication with
* Pageant owned by the user SID rather than the default. This
* should make communication between processes with slightly
* different contexts more reliable: in particular, command
* prompts launched as administrator should still be able to
* run PSFTPs which refer back to the owning user's
* unprivileged Pageant.
*/
usersid = get_user_sid();
if (usersid) {
psd = (PSECURITY_DESCRIPTOR)
LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
if (psd) {
if (p_InitializeSecurityDescriptor
(psd, SECURITY_DESCRIPTOR_REVISION) &&
p_SetSecurityDescriptorOwner(psd, usersid, FALSE)) {
sa.nLength = sizeof(sa);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = psd;
psa = &sa;
} else {
LocalFree(psd);
psd = NULL;
}
}
}
}
#endif /* NO_SECURITY */
filemap = CreateFileMapping(INVALID_HANDLE_VALUE, psa, PAGE_READWRITE,
0, AGENT_MAX_MSGLEN, mapname);
if (filemap == NULL || filemap == INVALID_HANDLE_VALUE) {
sfree(mapname);
return NULL; /* *out == NULL, so failure */
}
p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0);
memcpy(p, in, inlen);
cds.dwData = AGENT_COPYDATA_ID;
cds.cbData = 1 + strlen(mapname);
cds.lpData = mapname;
/*
* The user either passed a null callback (indicating that the
* query is required to be synchronous) or CreateThread failed.
* Either way, we need a synchronous request.
*/
id = SendMessage(hwnd, WM_COPYDATA, (WPARAM) NULL, (LPARAM) &cds);
if (id > 0) {
retlen = 4 + GET_32BIT(p);
ret = snewn(retlen, unsigned char);
if (ret) {
memcpy(ret, p, retlen);
*out = ret;
*outlen = retlen;
}
}
UnmapViewOfFile(p);
CloseHandle(filemap);
sfree(mapname);
if (psd)
LocalFree(psd);
return NULL;
}
|
Overflow
| 0
|
agent_pending_query *agent_query(
void *in, int inlen, void **out, int *outlen,
void (*callback)(void *, void *, int), void *callback_ctx)
{
HWND hwnd;
char *mapname;
HANDLE filemap;
unsigned char *p, *ret;
int id, retlen;
COPYDATASTRUCT cds;
SECURITY_ATTRIBUTES sa, *psa;
PSECURITY_DESCRIPTOR psd = NULL;
PSID usersid = NULL;
*out = NULL;
*outlen = 0;
hwnd = FindWindow("Pageant", "Pageant");
if (!hwnd)
return NULL; /* *out == NULL, so failure */
mapname = dupprintf("PageantRequest%08x", (unsigned)GetCurrentThreadId());
psa = NULL;
#ifndef NO_SECURITY
if (got_advapi()) {
/*
* Make the file mapping we create for communication with
* Pageant owned by the user SID rather than the default. This
* should make communication between processes with slightly
* different contexts more reliable: in particular, command
* prompts launched as administrator should still be able to
* run PSFTPs which refer back to the owning user's
* unprivileged Pageant.
*/
usersid = get_user_sid();
if (usersid) {
psd = (PSECURITY_DESCRIPTOR)
LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
if (psd) {
if (p_InitializeSecurityDescriptor
(psd, SECURITY_DESCRIPTOR_REVISION) &&
p_SetSecurityDescriptorOwner(psd, usersid, FALSE)) {
sa.nLength = sizeof(sa);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = psd;
psa = &sa;
} else {
LocalFree(psd);
psd = NULL;
}
}
}
}
#endif /* NO_SECURITY */
filemap = CreateFileMapping(INVALID_HANDLE_VALUE, psa, PAGE_READWRITE,
0, AGENT_MAX_MSGLEN, mapname);
if (filemap == NULL || filemap == INVALID_HANDLE_VALUE) {
sfree(mapname);
return NULL; /* *out == NULL, so failure */
}
p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0);
memcpy(p, in, inlen);
cds.dwData = AGENT_COPYDATA_ID;
cds.cbData = 1 + strlen(mapname);
cds.lpData = mapname;
/*
* The user either passed a null callback (indicating that the
* query is required to be synchronous) or CreateThread failed.
* Either way, we need a synchronous request.
*/
id = SendMessage(hwnd, WM_COPYDATA, (WPARAM) NULL, (LPARAM) &cds);
if (id > 0) {
retlen = 4 + GET_32BIT(p);
ret = snewn(retlen, unsigned char);
if (ret) {
memcpy(ret, p, retlen);
*out = ret;
*outlen = retlen;
}
}
UnmapViewOfFile(p);
CloseHandle(filemap);
sfree(mapname);
if (psd)
LocalFree(psd);
return NULL;
}
|
@@ -4,6 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
+#include <assert.h>
#include "putty.h"
|
CWE-119
| null | null |
6,858
|
static uint32_t vmsvga_bios_read(void *opaque, uint32_t address)
{
printf("%s: what are we supposed to return?\n", __func__);
return 0xcafe;
}
|
DoS
| 0
|
static uint32_t vmsvga_bios_read(void *opaque, uint32_t address)
{
printf("%s: what are we supposed to return?\n", __func__);
return 0xcafe;
}
|
@@ -676,11 +676,13 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
cursor.bpp = vmsvga_fifo_read(s);
args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
- if (cursor.width > 256 ||
- cursor.height > 256 ||
- cursor.bpp > 32 ||
- SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
- SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image) {
+ if (cursor.width > 256
+ || cursor.height > 256
+ || cursor.bpp > 32
+ || SVGA_BITMAP_SIZE(x, y)
+ > sizeof(cursor.mask) / sizeof(cursor.mask[0])
+ || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
+ > sizeof(cursor.image) / sizeof(cursor.image[0])) {
goto badcmd;
}
|
CWE-787
| null | null |
6,859
|
static void vmsvga_bios_write(void *opaque, uint32_t address, uint32_t data)
{
printf("%s: what are we supposed to do with (%08x)?\n", __func__, data);
}
|
DoS
| 0
|
static void vmsvga_bios_write(void *opaque, uint32_t address, uint32_t data)
{
printf("%s: what are we supposed to do with (%08x)?\n", __func__, data);
}
|
@@ -676,11 +676,13 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
cursor.bpp = vmsvga_fifo_read(s);
args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
- if (cursor.width > 256 ||
- cursor.height > 256 ||
- cursor.bpp > 32 ||
- SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
- SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image) {
+ if (cursor.width > 256
+ || cursor.height > 256
+ || cursor.bpp > 32
+ || SVGA_BITMAP_SIZE(x, y)
+ > sizeof(cursor.mask) / sizeof(cursor.mask[0])
+ || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
+ > sizeof(cursor.image) / sizeof(cursor.image[0])) {
goto badcmd;
}
|
CWE-787
| null | null |
6,860
|
static inline void vmsvga_check_size(struct vmsvga_state_s *s)
{
DisplaySurface *surface = qemu_console_surface(s->vga.con);
if (s->new_width != surface_width(surface) ||
s->new_height != surface_height(surface) ||
s->new_depth != surface_bits_per_pixel(surface)) {
int stride = (s->new_depth * s->new_width) / 8;
pixman_format_code_t format =
qemu_default_pixman_format(s->new_depth, true);
trace_vmware_setmode(s->new_width, s->new_height, s->new_depth);
surface = qemu_create_displaysurface_from(s->new_width, s->new_height,
format, stride,
s->vga.vram_ptr);
dpy_gfx_replace_surface(s->vga.con, surface);
s->invalidated = 1;
}
}
|
DoS
| 0
|
static inline void vmsvga_check_size(struct vmsvga_state_s *s)
{
DisplaySurface *surface = qemu_console_surface(s->vga.con);
if (s->new_width != surface_width(surface) ||
s->new_height != surface_height(surface) ||
s->new_depth != surface_bits_per_pixel(surface)) {
int stride = (s->new_depth * s->new_width) / 8;
pixman_format_code_t format =
qemu_default_pixman_format(s->new_depth, true);
trace_vmware_setmode(s->new_width, s->new_height, s->new_depth);
surface = qemu_create_displaysurface_from(s->new_width, s->new_height,
format, stride,
s->vga.vram_ptr);
dpy_gfx_replace_surface(s->vga.con, surface);
s->invalidated = 1;
}
}
|
@@ -676,11 +676,13 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
cursor.bpp = vmsvga_fifo_read(s);
args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
- if (cursor.width > 256 ||
- cursor.height > 256 ||
- cursor.bpp > 32 ||
- SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
- SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image) {
+ if (cursor.width > 256
+ || cursor.height > 256
+ || cursor.bpp > 32
+ || SVGA_BITMAP_SIZE(x, y)
+ > sizeof(cursor.mask) / sizeof(cursor.mask[0])
+ || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
+ > sizeof(cursor.image) / sizeof(cursor.image[0])) {
goto badcmd;
}
|
CWE-787
| null | null |
6,861
|
static inline int vmsvga_copy_rect(struct vmsvga_state_s *s,
int x0, int y0, int x1, int y1, int w, int h)
{
DisplaySurface *surface = qemu_console_surface(s->vga.con);
uint8_t *vram = s->vga.vram_ptr;
int bypl = surface_stride(surface);
int bypp = surface_bytes_per_pixel(surface);
int width = bypp * w;
int line = h;
uint8_t *ptr[2];
if (!vmsvga_verify_rect(surface, "vmsvga_copy_rect/src", x0, y0, w, h)) {
return -1;
}
if (!vmsvga_verify_rect(surface, "vmsvga_copy_rect/dst", x1, y1, w, h)) {
return -1;
}
if (y1 > y0) {
ptr[0] = vram + bypp * x0 + bypl * (y0 + h - 1);
ptr[1] = vram + bypp * x1 + bypl * (y1 + h - 1);
for (; line > 0; line --, ptr[0] -= bypl, ptr[1] -= bypl) {
memmove(ptr[1], ptr[0], width);
}
} else {
ptr[0] = vram + bypp * x0 + bypl * y0;
ptr[1] = vram + bypp * x1 + bypl * y1;
for (; line > 0; line --, ptr[0] += bypl, ptr[1] += bypl) {
memmove(ptr[1], ptr[0], width);
}
}
vmsvga_update_rect_delayed(s, x1, y1, w, h);
return 0;
}
|
DoS
| 0
|
static inline int vmsvga_copy_rect(struct vmsvga_state_s *s,
int x0, int y0, int x1, int y1, int w, int h)
{
DisplaySurface *surface = qemu_console_surface(s->vga.con);
uint8_t *vram = s->vga.vram_ptr;
int bypl = surface_stride(surface);
int bypp = surface_bytes_per_pixel(surface);
int width = bypp * w;
int line = h;
uint8_t *ptr[2];
if (!vmsvga_verify_rect(surface, "vmsvga_copy_rect/src", x0, y0, w, h)) {
return -1;
}
if (!vmsvga_verify_rect(surface, "vmsvga_copy_rect/dst", x1, y1, w, h)) {
return -1;
}
if (y1 > y0) {
ptr[0] = vram + bypp * x0 + bypl * (y0 + h - 1);
ptr[1] = vram + bypp * x1 + bypl * (y1 + h - 1);
for (; line > 0; line --, ptr[0] -= bypl, ptr[1] -= bypl) {
memmove(ptr[1], ptr[0], width);
}
} else {
ptr[0] = vram + bypp * x0 + bypl * y0;
ptr[1] = vram + bypp * x1 + bypl * y1;
for (; line > 0; line --, ptr[0] += bypl, ptr[1] += bypl) {
memmove(ptr[1], ptr[0], width);
}
}
vmsvga_update_rect_delayed(s, x1, y1, w, h);
return 0;
}
|
@@ -676,11 +676,13 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
cursor.bpp = vmsvga_fifo_read(s);
args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
- if (cursor.width > 256 ||
- cursor.height > 256 ||
- cursor.bpp > 32 ||
- SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
- SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image) {
+ if (cursor.width > 256
+ || cursor.height > 256
+ || cursor.bpp > 32
+ || SVGA_BITMAP_SIZE(x, y)
+ > sizeof(cursor.mask) / sizeof(cursor.mask[0])
+ || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
+ > sizeof(cursor.image) / sizeof(cursor.image[0])) {
goto badcmd;
}
|
CWE-787
| null | null |
6,862
|
static inline void vmsvga_cursor_define(struct vmsvga_state_s *s,
struct vmsvga_cursor_definition_s *c)
{
QEMUCursor *qc;
int i, pixels;
qc = cursor_alloc(c->width, c->height);
qc->hot_x = c->hot_x;
qc->hot_y = c->hot_y;
switch (c->bpp) {
case 1:
cursor_set_mono(qc, 0xffffff, 0x000000, (void *)c->image,
1, (void *)c->mask);
#ifdef DEBUG
cursor_print_ascii_art(qc, "vmware/mono");
#endif
break;
case 32:
/* fill alpha channel from mask, set color to zero */
cursor_set_mono(qc, 0x000000, 0x000000, (void *)c->mask,
1, (void *)c->mask);
/* add in rgb values */
pixels = c->width * c->height;
for (i = 0; i < pixels; i++) {
qc->data[i] |= c->image[i] & 0xffffff;
}
#ifdef DEBUG
cursor_print_ascii_art(qc, "vmware/32bit");
#endif
break;
default:
fprintf(stderr, "%s: unhandled bpp %d, using fallback cursor\n",
__func__, c->bpp);
cursor_put(qc);
qc = cursor_builtin_left_ptr();
}
dpy_cursor_define(s->vga.con, qc);
cursor_put(qc);
}
|
DoS
| 0
|
static inline void vmsvga_cursor_define(struct vmsvga_state_s *s,
struct vmsvga_cursor_definition_s *c)
{
QEMUCursor *qc;
int i, pixels;
qc = cursor_alloc(c->width, c->height);
qc->hot_x = c->hot_x;
qc->hot_y = c->hot_y;
switch (c->bpp) {
case 1:
cursor_set_mono(qc, 0xffffff, 0x000000, (void *)c->image,
1, (void *)c->mask);
#ifdef DEBUG
cursor_print_ascii_art(qc, "vmware/mono");
#endif
break;
case 32:
/* fill alpha channel from mask, set color to zero */
cursor_set_mono(qc, 0x000000, 0x000000, (void *)c->mask,
1, (void *)c->mask);
/* add in rgb values */
pixels = c->width * c->height;
for (i = 0; i < pixels; i++) {
qc->data[i] |= c->image[i] & 0xffffff;
}
#ifdef DEBUG
cursor_print_ascii_art(qc, "vmware/32bit");
#endif
break;
default:
fprintf(stderr, "%s: unhandled bpp %d, using fallback cursor\n",
__func__, c->bpp);
cursor_put(qc);
qc = cursor_builtin_left_ptr();
}
dpy_cursor_define(s->vga.con, qc);
cursor_put(qc);
}
|
@@ -676,11 +676,13 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
cursor.bpp = vmsvga_fifo_read(s);
args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
- if (cursor.width > 256 ||
- cursor.height > 256 ||
- cursor.bpp > 32 ||
- SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
- SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image) {
+ if (cursor.width > 256
+ || cursor.height > 256
+ || cursor.bpp > 32
+ || SVGA_BITMAP_SIZE(x, y)
+ > sizeof(cursor.mask) / sizeof(cursor.mask[0])
+ || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
+ > sizeof(cursor.image) / sizeof(cursor.image[0])) {
goto badcmd;
}
|
CWE-787
| null | null |
6,863
|
static inline int vmsvga_fifo_length(struct vmsvga_state_s *s)
{
int num;
if (!s->config || !s->enable) {
return 0;
}
s->fifo_min = le32_to_cpu(s->fifo[SVGA_FIFO_MIN]);
s->fifo_max = le32_to_cpu(s->fifo[SVGA_FIFO_MAX]);
s->fifo_next = le32_to_cpu(s->fifo[SVGA_FIFO_NEXT]);
s->fifo_stop = le32_to_cpu(s->fifo[SVGA_FIFO_STOP]);
/* Check range and alignment. */
if ((s->fifo_min | s->fifo_max | s->fifo_next | s->fifo_stop) & 3) {
return 0;
}
if (s->fifo_min < sizeof(uint32_t) * 4) {
return 0;
}
if (s->fifo_max > SVGA_FIFO_SIZE ||
s->fifo_min >= SVGA_FIFO_SIZE ||
s->fifo_stop >= SVGA_FIFO_SIZE ||
s->fifo_next >= SVGA_FIFO_SIZE) {
return 0;
}
if (s->fifo_max < s->fifo_min + 10 * 1024) {
return 0;
}
num = s->fifo_next - s->fifo_stop;
if (num < 0) {
num += s->fifo_max - s->fifo_min;
}
return num >> 2;
}
|
DoS
| 0
|
static inline int vmsvga_fifo_length(struct vmsvga_state_s *s)
{
int num;
if (!s->config || !s->enable) {
return 0;
}
s->fifo_min = le32_to_cpu(s->fifo[SVGA_FIFO_MIN]);
s->fifo_max = le32_to_cpu(s->fifo[SVGA_FIFO_MAX]);
s->fifo_next = le32_to_cpu(s->fifo[SVGA_FIFO_NEXT]);
s->fifo_stop = le32_to_cpu(s->fifo[SVGA_FIFO_STOP]);
/* Check range and alignment. */
if ((s->fifo_min | s->fifo_max | s->fifo_next | s->fifo_stop) & 3) {
return 0;
}
if (s->fifo_min < sizeof(uint32_t) * 4) {
return 0;
}
if (s->fifo_max > SVGA_FIFO_SIZE ||
s->fifo_min >= SVGA_FIFO_SIZE ||
s->fifo_stop >= SVGA_FIFO_SIZE ||
s->fifo_next >= SVGA_FIFO_SIZE) {
return 0;
}
if (s->fifo_max < s->fifo_min + 10 * 1024) {
return 0;
}
num = s->fifo_next - s->fifo_stop;
if (num < 0) {
num += s->fifo_max - s->fifo_min;
}
return num >> 2;
}
|
@@ -676,11 +676,13 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
cursor.bpp = vmsvga_fifo_read(s);
args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
- if (cursor.width > 256 ||
- cursor.height > 256 ||
- cursor.bpp > 32 ||
- SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
- SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image) {
+ if (cursor.width > 256
+ || cursor.height > 256
+ || cursor.bpp > 32
+ || SVGA_BITMAP_SIZE(x, y)
+ > sizeof(cursor.mask) / sizeof(cursor.mask[0])
+ || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
+ > sizeof(cursor.image) / sizeof(cursor.image[0])) {
goto badcmd;
}
|
CWE-787
| null | null |
6,864
|
static inline uint32_t vmsvga_fifo_read(struct vmsvga_state_s *s)
{
return le32_to_cpu(vmsvga_fifo_read_raw(s));
}
|
DoS
| 0
|
static inline uint32_t vmsvga_fifo_read(struct vmsvga_state_s *s)
{
return le32_to_cpu(vmsvga_fifo_read_raw(s));
}
|
@@ -676,11 +676,13 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
cursor.bpp = vmsvga_fifo_read(s);
args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
- if (cursor.width > 256 ||
- cursor.height > 256 ||
- cursor.bpp > 32 ||
- SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
- SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image) {
+ if (cursor.width > 256
+ || cursor.height > 256
+ || cursor.bpp > 32
+ || SVGA_BITMAP_SIZE(x, y)
+ > sizeof(cursor.mask) / sizeof(cursor.mask[0])
+ || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
+ > sizeof(cursor.image) / sizeof(cursor.image[0])) {
goto badcmd;
}
|
CWE-787
| null | null |
6,865
|
static inline int vmsvga_fill_rect(struct vmsvga_state_s *s,
uint32_t c, int x, int y, int w, int h)
{
DisplaySurface *surface = qemu_console_surface(s->vga.con);
int bypl = surface_stride(surface);
int width = surface_bytes_per_pixel(surface) * w;
int line = h;
int column;
uint8_t *fst;
uint8_t *dst;
uint8_t *src;
uint8_t col[4];
if (!vmsvga_verify_rect(surface, __func__, x, y, w, h)) {
return -1;
}
col[0] = c;
col[1] = c >> 8;
col[2] = c >> 16;
col[3] = c >> 24;
fst = s->vga.vram_ptr + surface_bytes_per_pixel(surface) * x + bypl * y;
if (line--) {
dst = fst;
src = col;
for (column = width; column > 0; column--) {
*(dst++) = *(src++);
if (src - col == surface_bytes_per_pixel(surface)) {
src = col;
}
}
dst = fst;
for (; line > 0; line--) {
dst += bypl;
memcpy(dst, fst, width);
}
}
vmsvga_update_rect_delayed(s, x, y, w, h);
return 0;
}
|
DoS
| 0
|
static inline int vmsvga_fill_rect(struct vmsvga_state_s *s,
uint32_t c, int x, int y, int w, int h)
{
DisplaySurface *surface = qemu_console_surface(s->vga.con);
int bypl = surface_stride(surface);
int width = surface_bytes_per_pixel(surface) * w;
int line = h;
int column;
uint8_t *fst;
uint8_t *dst;
uint8_t *src;
uint8_t col[4];
if (!vmsvga_verify_rect(surface, __func__, x, y, w, h)) {
return -1;
}
col[0] = c;
col[1] = c >> 8;
col[2] = c >> 16;
col[3] = c >> 24;
fst = s->vga.vram_ptr + surface_bytes_per_pixel(surface) * x + bypl * y;
if (line--) {
dst = fst;
src = col;
for (column = width; column > 0; column--) {
*(dst++) = *(src++);
if (src - col == surface_bytes_per_pixel(surface)) {
src = col;
}
}
dst = fst;
for (; line > 0; line--) {
dst += bypl;
memcpy(dst, fst, width);
}
}
vmsvga_update_rect_delayed(s, x, y, w, h);
return 0;
}
|
@@ -676,11 +676,13 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
cursor.bpp = vmsvga_fifo_read(s);
args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
- if (cursor.width > 256 ||
- cursor.height > 256 ||
- cursor.bpp > 32 ||
- SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
- SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image) {
+ if (cursor.width > 256
+ || cursor.height > 256
+ || cursor.bpp > 32
+ || SVGA_BITMAP_SIZE(x, y)
+ > sizeof(cursor.mask) / sizeof(cursor.mask[0])
+ || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
+ > sizeof(cursor.image) / sizeof(cursor.image[0])) {
goto badcmd;
}
|
CWE-787
| null | null |
6,866
|
static uint32_t vmsvga_index_read(void *opaque, uint32_t address)
{
struct vmsvga_state_s *s = opaque;
return s->index;
}
|
DoS
| 0
|
static uint32_t vmsvga_index_read(void *opaque, uint32_t address)
{
struct vmsvga_state_s *s = opaque;
return s->index;
}
|
@@ -676,11 +676,13 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
cursor.bpp = vmsvga_fifo_read(s);
args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
- if (cursor.width > 256 ||
- cursor.height > 256 ||
- cursor.bpp > 32 ||
- SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
- SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image) {
+ if (cursor.width > 256
+ || cursor.height > 256
+ || cursor.bpp > 32
+ || SVGA_BITMAP_SIZE(x, y)
+ > sizeof(cursor.mask) / sizeof(cursor.mask[0])
+ || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
+ > sizeof(cursor.image) / sizeof(cursor.image[0])) {
goto badcmd;
}
|
CWE-787
| null | null |
6,867
|
static void vmsvga_init(DeviceState *dev, struct vmsvga_state_s *s,
MemoryRegion *address_space, MemoryRegion *io)
{
s->scratch_size = SVGA_SCRATCH_SIZE;
s->scratch = g_malloc(s->scratch_size * 4);
s->vga.con = graphic_console_init(dev, 0, &vmsvga_ops, s);
s->fifo_size = SVGA_FIFO_SIZE;
memory_region_init_ram(&s->fifo_ram, NULL, "vmsvga.fifo", s->fifo_size,
&error_fatal);
vmstate_register_ram_global(&s->fifo_ram);
s->fifo_ptr = memory_region_get_ram_ptr(&s->fifo_ram);
vga_common_init(&s->vga, OBJECT(dev), true);
vga_init(&s->vga, OBJECT(dev), address_space, io, true);
vmstate_register(NULL, 0, &vmstate_vga_common, &s->vga);
s->new_depth = 32;
}
|
DoS
| 0
|
static void vmsvga_init(DeviceState *dev, struct vmsvga_state_s *s,
MemoryRegion *address_space, MemoryRegion *io)
{
s->scratch_size = SVGA_SCRATCH_SIZE;
s->scratch = g_malloc(s->scratch_size * 4);
s->vga.con = graphic_console_init(dev, 0, &vmsvga_ops, s);
s->fifo_size = SVGA_FIFO_SIZE;
memory_region_init_ram(&s->fifo_ram, NULL, "vmsvga.fifo", s->fifo_size,
&error_fatal);
vmstate_register_ram_global(&s->fifo_ram);
s->fifo_ptr = memory_region_get_ram_ptr(&s->fifo_ram);
vga_common_init(&s->vga, OBJECT(dev), true);
vga_init(&s->vga, OBJECT(dev), address_space, io, true);
vmstate_register(NULL, 0, &vmstate_vga_common, &s->vga);
s->new_depth = 32;
}
|
@@ -676,11 +676,13 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
cursor.bpp = vmsvga_fifo_read(s);
args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
- if (cursor.width > 256 ||
- cursor.height > 256 ||
- cursor.bpp > 32 ||
- SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
- SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image) {
+ if (cursor.width > 256
+ || cursor.height > 256
+ || cursor.bpp > 32
+ || SVGA_BITMAP_SIZE(x, y)
+ > sizeof(cursor.mask) / sizeof(cursor.mask[0])
+ || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
+ > sizeof(cursor.image) / sizeof(cursor.image[0])) {
goto badcmd;
}
|
CWE-787
| null | null |
6,868
|
static void vmsvga_invalidate_display(void *opaque)
{
struct vmsvga_state_s *s = opaque;
if (!s->enable) {
s->vga.hw_ops->invalidate(&s->vga);
return;
}
s->invalidated = 1;
}
|
DoS
| 0
|
static void vmsvga_invalidate_display(void *opaque)
{
struct vmsvga_state_s *s = opaque;
if (!s->enable) {
s->vga.hw_ops->invalidate(&s->vga);
return;
}
s->invalidated = 1;
}
|
@@ -676,11 +676,13 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
cursor.bpp = vmsvga_fifo_read(s);
args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
- if (cursor.width > 256 ||
- cursor.height > 256 ||
- cursor.bpp > 32 ||
- SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
- SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image) {
+ if (cursor.width > 256
+ || cursor.height > 256
+ || cursor.bpp > 32
+ || SVGA_BITMAP_SIZE(x, y)
+ > sizeof(cursor.mask) / sizeof(cursor.mask[0])
+ || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
+ > sizeof(cursor.image) / sizeof(cursor.image[0])) {
goto badcmd;
}
|
CWE-787
| null | null |
6,869
|
static uint64_t vmsvga_io_read(void *opaque, hwaddr addr, unsigned size)
{
struct vmsvga_state_s *s = opaque;
switch (addr) {
case SVGA_IO_MUL * SVGA_INDEX_PORT: return vmsvga_index_read(s, addr);
case SVGA_IO_MUL * SVGA_VALUE_PORT: return vmsvga_value_read(s, addr);
case SVGA_IO_MUL * SVGA_BIOS_PORT: return vmsvga_bios_read(s, addr);
default: return -1u;
}
}
|
DoS
| 0
|
static uint64_t vmsvga_io_read(void *opaque, hwaddr addr, unsigned size)
{
struct vmsvga_state_s *s = opaque;
switch (addr) {
case SVGA_IO_MUL * SVGA_INDEX_PORT: return vmsvga_index_read(s, addr);
case SVGA_IO_MUL * SVGA_VALUE_PORT: return vmsvga_value_read(s, addr);
case SVGA_IO_MUL * SVGA_BIOS_PORT: return vmsvga_bios_read(s, addr);
default: return -1u;
}
}
|
@@ -676,11 +676,13 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
cursor.bpp = vmsvga_fifo_read(s);
args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
- if (cursor.width > 256 ||
- cursor.height > 256 ||
- cursor.bpp > 32 ||
- SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
- SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image) {
+ if (cursor.width > 256
+ || cursor.height > 256
+ || cursor.bpp > 32
+ || SVGA_BITMAP_SIZE(x, y)
+ > sizeof(cursor.mask) / sizeof(cursor.mask[0])
+ || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
+ > sizeof(cursor.image) / sizeof(cursor.image[0])) {
goto badcmd;
}
|
CWE-787
| null | null |
6,870
|
static void vmsvga_io_write(void *opaque, hwaddr addr,
uint64_t data, unsigned size)
{
struct vmsvga_state_s *s = opaque;
switch (addr) {
case SVGA_IO_MUL * SVGA_INDEX_PORT:
vmsvga_index_write(s, addr, data);
break;
case SVGA_IO_MUL * SVGA_VALUE_PORT:
vmsvga_value_write(s, addr, data);
break;
case SVGA_IO_MUL * SVGA_BIOS_PORT:
vmsvga_bios_write(s, addr, data);
break;
}
}
|
DoS
| 0
|
static void vmsvga_io_write(void *opaque, hwaddr addr,
uint64_t data, unsigned size)
{
struct vmsvga_state_s *s = opaque;
switch (addr) {
case SVGA_IO_MUL * SVGA_INDEX_PORT:
vmsvga_index_write(s, addr, data);
break;
case SVGA_IO_MUL * SVGA_VALUE_PORT:
vmsvga_value_write(s, addr, data);
break;
case SVGA_IO_MUL * SVGA_BIOS_PORT:
vmsvga_bios_write(s, addr, data);
break;
}
}
|
@@ -676,11 +676,13 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
cursor.bpp = vmsvga_fifo_read(s);
args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
- if (cursor.width > 256 ||
- cursor.height > 256 ||
- cursor.bpp > 32 ||
- SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
- SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image) {
+ if (cursor.width > 256
+ || cursor.height > 256
+ || cursor.bpp > 32
+ || SVGA_BITMAP_SIZE(x, y)
+ > sizeof(cursor.mask) / sizeof(cursor.mask[0])
+ || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
+ > sizeof(cursor.image) / sizeof(cursor.image[0])) {
goto badcmd;
}
|
CWE-787
| null | null |
6,871
|
static int vmsvga_post_load(void *opaque, int version_id)
{
struct vmsvga_state_s *s = opaque;
s->invalidated = 1;
if (s->config) {
s->fifo = (uint32_t *) s->fifo_ptr;
}
return 0;
}
|
DoS
| 0
|
static int vmsvga_post_load(void *opaque, int version_id)
{
struct vmsvga_state_s *s = opaque;
s->invalidated = 1;
if (s->config) {
s->fifo = (uint32_t *) s->fifo_ptr;
}
return 0;
}
|
@@ -676,11 +676,13 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
cursor.bpp = vmsvga_fifo_read(s);
args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
- if (cursor.width > 256 ||
- cursor.height > 256 ||
- cursor.bpp > 32 ||
- SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
- SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image) {
+ if (cursor.width > 256
+ || cursor.height > 256
+ || cursor.bpp > 32
+ || SVGA_BITMAP_SIZE(x, y)
+ > sizeof(cursor.mask) / sizeof(cursor.mask[0])
+ || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
+ > sizeof(cursor.image) / sizeof(cursor.image[0])) {
goto badcmd;
}
|
CWE-787
| null | null |
6,872
|
static void vmsvga_register_types(void)
{
type_register_static(&vmsvga_info);
}
|
DoS
| 0
|
static void vmsvga_register_types(void)
{
type_register_static(&vmsvga_info);
}
|
@@ -676,11 +676,13 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
cursor.bpp = vmsvga_fifo_read(s);
args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
- if (cursor.width > 256 ||
- cursor.height > 256 ||
- cursor.bpp > 32 ||
- SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
- SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image) {
+ if (cursor.width > 256
+ || cursor.height > 256
+ || cursor.bpp > 32
+ || SVGA_BITMAP_SIZE(x, y)
+ > sizeof(cursor.mask) / sizeof(cursor.mask[0])
+ || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
+ > sizeof(cursor.image) / sizeof(cursor.image[0])) {
goto badcmd;
}
|
CWE-787
| null | null |
6,873
|
static void vmsvga_text_update(void *opaque, console_ch_t *chardata)
{
struct vmsvga_state_s *s = opaque;
if (s->vga.hw_ops->text_update) {
s->vga.hw_ops->text_update(&s->vga, chardata);
}
}
|
DoS
| 0
|
static void vmsvga_text_update(void *opaque, console_ch_t *chardata)
{
struct vmsvga_state_s *s = opaque;
if (s->vga.hw_ops->text_update) {
s->vga.hw_ops->text_update(&s->vga, chardata);
}
}
|
@@ -676,11 +676,13 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
cursor.bpp = vmsvga_fifo_read(s);
args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
- if (cursor.width > 256 ||
- cursor.height > 256 ||
- cursor.bpp > 32 ||
- SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
- SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image) {
+ if (cursor.width > 256
+ || cursor.height > 256
+ || cursor.bpp > 32
+ || SVGA_BITMAP_SIZE(x, y)
+ > sizeof(cursor.mask) / sizeof(cursor.mask[0])
+ || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
+ > sizeof(cursor.image) / sizeof(cursor.image[0])) {
goto badcmd;
}
|
CWE-787
| null | null |
6,874
|
static inline void vmsvga_update_rect(struct vmsvga_state_s *s,
int x, int y, int w, int h)
{
DisplaySurface *surface = qemu_console_surface(s->vga.con);
int line;
int bypl;
int width;
int start;
uint8_t *src;
uint8_t *dst;
if (!vmsvga_verify_rect(surface, __func__, x, y, w, h)) {
/* go for a fullscreen update as fallback */
x = 0;
y = 0;
w = surface_width(surface);
h = surface_height(surface);
}
bypl = surface_stride(surface);
width = surface_bytes_per_pixel(surface) * w;
start = surface_bytes_per_pixel(surface) * x + bypl * y;
src = s->vga.vram_ptr + start;
dst = surface_data(surface) + start;
for (line = h; line > 0; line--, src += bypl, dst += bypl) {
memcpy(dst, src, width);
}
dpy_gfx_update(s->vga.con, x, y, w, h);
}
|
DoS
| 0
|
static inline void vmsvga_update_rect(struct vmsvga_state_s *s,
int x, int y, int w, int h)
{
DisplaySurface *surface = qemu_console_surface(s->vga.con);
int line;
int bypl;
int width;
int start;
uint8_t *src;
uint8_t *dst;
if (!vmsvga_verify_rect(surface, __func__, x, y, w, h)) {
/* go for a fullscreen update as fallback */
x = 0;
y = 0;
w = surface_width(surface);
h = surface_height(surface);
}
bypl = surface_stride(surface);
width = surface_bytes_per_pixel(surface) * w;
start = surface_bytes_per_pixel(surface) * x + bypl * y;
src = s->vga.vram_ptr + start;
dst = surface_data(surface) + start;
for (line = h; line > 0; line--, src += bypl, dst += bypl) {
memcpy(dst, src, width);
}
dpy_gfx_update(s->vga.con, x, y, w, h);
}
|
@@ -676,11 +676,13 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
cursor.bpp = vmsvga_fifo_read(s);
args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
- if (cursor.width > 256 ||
- cursor.height > 256 ||
- cursor.bpp > 32 ||
- SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
- SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image) {
+ if (cursor.width > 256
+ || cursor.height > 256
+ || cursor.bpp > 32
+ || SVGA_BITMAP_SIZE(x, y)
+ > sizeof(cursor.mask) / sizeof(cursor.mask[0])
+ || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
+ > sizeof(cursor.image) / sizeof(cursor.image[0])) {
goto badcmd;
}
|
CWE-787
| null | null |
6,875
|
static inline void vmsvga_update_rect_delayed(struct vmsvga_state_s *s,
int x, int y, int w, int h)
{
struct vmsvga_rect_s *rect = &s->redraw_fifo[s->redraw_fifo_last++];
s->redraw_fifo_last &= REDRAW_FIFO_LEN - 1;
rect->x = x;
rect->y = y;
rect->w = w;
rect->h = h;
}
|
DoS
| 0
|
static inline void vmsvga_update_rect_delayed(struct vmsvga_state_s *s,
int x, int y, int w, int h)
{
struct vmsvga_rect_s *rect = &s->redraw_fifo[s->redraw_fifo_last++];
s->redraw_fifo_last &= REDRAW_FIFO_LEN - 1;
rect->x = x;
rect->y = y;
rect->w = w;
rect->h = h;
}
|
@@ -676,11 +676,13 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
cursor.bpp = vmsvga_fifo_read(s);
args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
- if (cursor.width > 256 ||
- cursor.height > 256 ||
- cursor.bpp > 32 ||
- SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
- SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image) {
+ if (cursor.width > 256
+ || cursor.height > 256
+ || cursor.bpp > 32
+ || SVGA_BITMAP_SIZE(x, y)
+ > sizeof(cursor.mask) / sizeof(cursor.mask[0])
+ || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
+ > sizeof(cursor.image) / sizeof(cursor.image[0])) {
goto badcmd;
}
|
CWE-787
| null | null |
6,876
|
static inline void vmsvga_update_rect_flush(struct vmsvga_state_s *s)
{
struct vmsvga_rect_s *rect;
if (s->invalidated) {
s->redraw_fifo_first = s->redraw_fifo_last;
return;
}
/* Overlapping region updates can be optimised out here - if someone
* knows a smart algorithm to do that, please share. */
while (s->redraw_fifo_first != s->redraw_fifo_last) {
rect = &s->redraw_fifo[s->redraw_fifo_first++];
s->redraw_fifo_first &= REDRAW_FIFO_LEN - 1;
vmsvga_update_rect(s, rect->x, rect->y, rect->w, rect->h);
}
}
|
DoS
| 0
|
static inline void vmsvga_update_rect_flush(struct vmsvga_state_s *s)
{
struct vmsvga_rect_s *rect;
if (s->invalidated) {
s->redraw_fifo_first = s->redraw_fifo_last;
return;
}
/* Overlapping region updates can be optimised out here - if someone
* knows a smart algorithm to do that, please share. */
while (s->redraw_fifo_first != s->redraw_fifo_last) {
rect = &s->redraw_fifo[s->redraw_fifo_first++];
s->redraw_fifo_first &= REDRAW_FIFO_LEN - 1;
vmsvga_update_rect(s, rect->x, rect->y, rect->w, rect->h);
}
}
|
@@ -676,11 +676,13 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
cursor.bpp = vmsvga_fifo_read(s);
args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
- if (cursor.width > 256 ||
- cursor.height > 256 ||
- cursor.bpp > 32 ||
- SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
- SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image) {
+ if (cursor.width > 256
+ || cursor.height > 256
+ || cursor.bpp > 32
+ || SVGA_BITMAP_SIZE(x, y)
+ > sizeof(cursor.mask) / sizeof(cursor.mask[0])
+ || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
+ > sizeof(cursor.image) / sizeof(cursor.image[0])) {
goto badcmd;
}
|
CWE-787
| null | null |
6,877
|
static inline bool vmsvga_verify_rect(DisplaySurface *surface,
const char *name,
int x, int y, int w, int h)
{
if (x < 0) {
fprintf(stderr, "%s: x was < 0 (%d)\n", name, x);
return false;
}
if (x > SVGA_MAX_WIDTH) {
fprintf(stderr, "%s: x was > %d (%d)\n", name, SVGA_MAX_WIDTH, x);
return false;
}
if (w < 0) {
fprintf(stderr, "%s: w was < 0 (%d)\n", name, w);
return false;
}
if (w > SVGA_MAX_WIDTH) {
fprintf(stderr, "%s: w was > %d (%d)\n", name, SVGA_MAX_WIDTH, w);
return false;
}
if (x + w > surface_width(surface)) {
fprintf(stderr, "%s: width was > %d (x: %d, w: %d)\n",
name, surface_width(surface), x, w);
return false;
}
if (y < 0) {
fprintf(stderr, "%s: y was < 0 (%d)\n", name, y);
return false;
}
if (y > SVGA_MAX_HEIGHT) {
fprintf(stderr, "%s: y was > %d (%d)\n", name, SVGA_MAX_HEIGHT, y);
return false;
}
if (h < 0) {
fprintf(stderr, "%s: h was < 0 (%d)\n", name, h);
return false;
}
if (h > SVGA_MAX_HEIGHT) {
fprintf(stderr, "%s: h was > %d (%d)\n", name, SVGA_MAX_HEIGHT, h);
return false;
}
if (y + h > surface_height(surface)) {
fprintf(stderr, "%s: update height > %d (y: %d, h: %d)\n",
name, surface_height(surface), y, h);
return false;
}
return true;
}
|
DoS
| 0
|
static inline bool vmsvga_verify_rect(DisplaySurface *surface,
const char *name,
int x, int y, int w, int h)
{
if (x < 0) {
fprintf(stderr, "%s: x was < 0 (%d)\n", name, x);
return false;
}
if (x > SVGA_MAX_WIDTH) {
fprintf(stderr, "%s: x was > %d (%d)\n", name, SVGA_MAX_WIDTH, x);
return false;
}
if (w < 0) {
fprintf(stderr, "%s: w was < 0 (%d)\n", name, w);
return false;
}
if (w > SVGA_MAX_WIDTH) {
fprintf(stderr, "%s: w was > %d (%d)\n", name, SVGA_MAX_WIDTH, w);
return false;
}
if (x + w > surface_width(surface)) {
fprintf(stderr, "%s: width was > %d (x: %d, w: %d)\n",
name, surface_width(surface), x, w);
return false;
}
if (y < 0) {
fprintf(stderr, "%s: y was < 0 (%d)\n", name, y);
return false;
}
if (y > SVGA_MAX_HEIGHT) {
fprintf(stderr, "%s: y was > %d (%d)\n", name, SVGA_MAX_HEIGHT, y);
return false;
}
if (h < 0) {
fprintf(stderr, "%s: h was < 0 (%d)\n", name, h);
return false;
}
if (h > SVGA_MAX_HEIGHT) {
fprintf(stderr, "%s: h was > %d (%d)\n", name, SVGA_MAX_HEIGHT, h);
return false;
}
if (y + h > surface_height(surface)) {
fprintf(stderr, "%s: update height > %d (y: %d, h: %d)\n",
name, surface_height(surface), y, h);
return false;
}
return true;
}
|
@@ -676,11 +676,13 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
cursor.bpp = vmsvga_fifo_read(s);
args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
- if (cursor.width > 256 ||
- cursor.height > 256 ||
- cursor.bpp > 32 ||
- SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
- SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image) {
+ if (cursor.width > 256
+ || cursor.height > 256
+ || cursor.bpp > 32
+ || SVGA_BITMAP_SIZE(x, y)
+ > sizeof(cursor.mask) / sizeof(cursor.mask[0])
+ || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
+ > sizeof(cursor.image) / sizeof(cursor.image[0])) {
goto badcmd;
}
|
CWE-787
| null | null |
6,878
|
static int eth_can_rx(NetClientState *nc)
{
struct xlx_ethlite *s = qemu_get_nic_opaque(nc);
unsigned int rxbase = s->rxbuf * (0x800 / 4);
return !(s->regs[rxbase + R_RX_CTRL0] & CTRL_S);
}
|
Exec Code Overflow
| 0
|
static int eth_can_rx(NetClientState *nc)
{
struct xlx_ethlite *s = qemu_get_nic_opaque(nc);
unsigned int rxbase = s->rxbuf * (0x800 / 4);
return !(s->regs[rxbase + R_RX_CTRL0] & CTRL_S);
}
|
@@ -197,6 +197,10 @@ static ssize_t eth_rx(NetClientState *nc, const uint8_t *buf, size_t size)
}
D(qemu_log("%s %zd rxbase=%x\n", __func__, size, rxbase));
+ if (size > (R_MAX - R_RX_BUF0 - rxbase) * 4) {
+ D(qemu_log("ethlite packet is too big, size=%x\n", size));
+ return -1;
+ }
memcpy(&s->regs[rxbase + R_RX_BUF0], buf, size);
s->regs[rxbase + R_RX_CTRL0] |= CTRL_S;
|
CWE-119
| null | null |
6,879
|
static inline void eth_pulse_irq(struct xlx_ethlite *s)
{
/* Only the first gie reg is active. */
if (s->regs[R_TX_GIE0] & GIE_GIE) {
qemu_irq_pulse(s->irq);
}
}
|
Exec Code Overflow
| 0
|
static inline void eth_pulse_irq(struct xlx_ethlite *s)
{
/* Only the first gie reg is active. */
if (s->regs[R_TX_GIE0] & GIE_GIE) {
qemu_irq_pulse(s->irq);
}
}
|
@@ -197,6 +197,10 @@ static ssize_t eth_rx(NetClientState *nc, const uint8_t *buf, size_t size)
}
D(qemu_log("%s %zd rxbase=%x\n", __func__, size, rxbase));
+ if (size > (R_MAX - R_RX_BUF0 - rxbase) * 4) {
+ D(qemu_log("ethlite packet is too big, size=%x\n", size));
+ return -1;
+ }
memcpy(&s->regs[rxbase + R_RX_BUF0], buf, size);
s->regs[rxbase + R_RX_CTRL0] |= CTRL_S;
|
CWE-119
| null | null |
6,880
|
eth_read(void *opaque, hwaddr addr, unsigned int size)
{
struct xlx_ethlite *s = opaque;
uint32_t r = 0;
addr >>= 2;
switch (addr)
{
case R_TX_GIE0:
case R_TX_LEN0:
case R_TX_LEN1:
case R_TX_CTRL1:
case R_TX_CTRL0:
case R_RX_CTRL1:
case R_RX_CTRL0:
r = s->regs[addr];
D(qemu_log("%s " TARGET_FMT_plx "=%x\n", __func__, addr * 4, r));
break;
default:
r = tswap32(s->regs[addr]);
break;
}
return r;
}
|
Exec Code Overflow
| 0
|
eth_read(void *opaque, hwaddr addr, unsigned int size)
{
struct xlx_ethlite *s = opaque;
uint32_t r = 0;
addr >>= 2;
switch (addr)
{
case R_TX_GIE0:
case R_TX_LEN0:
case R_TX_LEN1:
case R_TX_CTRL1:
case R_TX_CTRL0:
case R_RX_CTRL1:
case R_RX_CTRL0:
r = s->regs[addr];
D(qemu_log("%s " TARGET_FMT_plx "=%x\n", __func__, addr * 4, r));
break;
default:
r = tswap32(s->regs[addr]);
break;
}
return r;
}
|
@@ -197,6 +197,10 @@ static ssize_t eth_rx(NetClientState *nc, const uint8_t *buf, size_t size)
}
D(qemu_log("%s %zd rxbase=%x\n", __func__, size, rxbase));
+ if (size > (R_MAX - R_RX_BUF0 - rxbase) * 4) {
+ D(qemu_log("ethlite packet is too big, size=%x\n", size));
+ return -1;
+ }
memcpy(&s->regs[rxbase + R_RX_BUF0], buf, size);
s->regs[rxbase + R_RX_CTRL0] |= CTRL_S;
|
CWE-119
| null | null |
6,881
|
static void xilinx_ethlite_init(Object *obj)
{
struct xlx_ethlite *s = XILINX_ETHLITE(obj);
sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irq);
memory_region_init_io(&s->mmio, obj, ð_ops, s,
"xlnx.xps-ethernetlite", R_MAX * 4);
sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
}
|
Exec Code Overflow
| 0
|
static void xilinx_ethlite_init(Object *obj)
{
struct xlx_ethlite *s = XILINX_ETHLITE(obj);
sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irq);
memory_region_init_io(&s->mmio, obj, ð_ops, s,
"xlnx.xps-ethernetlite", R_MAX * 4);
sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
}
|
@@ -197,6 +197,10 @@ static ssize_t eth_rx(NetClientState *nc, const uint8_t *buf, size_t size)
}
D(qemu_log("%s %zd rxbase=%x\n", __func__, size, rxbase));
+ if (size > (R_MAX - R_RX_BUF0 - rxbase) * 4) {
+ D(qemu_log("ethlite packet is too big, size=%x\n", size));
+ return -1;
+ }
memcpy(&s->regs[rxbase + R_RX_BUF0], buf, size);
s->regs[rxbase + R_RX_CTRL0] |= CTRL_S;
|
CWE-119
| null | null |
6,882
|
static void xilinx_ethlite_realize(DeviceState *dev, Error **errp)
{
struct xlx_ethlite *s = XILINX_ETHLITE(dev);
qemu_macaddr_default_if_unset(&s->conf.macaddr);
s->nic = qemu_new_nic(&net_xilinx_ethlite_info, &s->conf,
object_get_typename(OBJECT(dev)), dev->id, s);
qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
}
|
Exec Code Overflow
| 0
|
static void xilinx_ethlite_realize(DeviceState *dev, Error **errp)
{
struct xlx_ethlite *s = XILINX_ETHLITE(dev);
qemu_macaddr_default_if_unset(&s->conf.macaddr);
s->nic = qemu_new_nic(&net_xilinx_ethlite_info, &s->conf,
object_get_typename(OBJECT(dev)), dev->id, s);
qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
}
|
@@ -197,6 +197,10 @@ static ssize_t eth_rx(NetClientState *nc, const uint8_t *buf, size_t size)
}
D(qemu_log("%s %zd rxbase=%x\n", __func__, size, rxbase));
+ if (size > (R_MAX - R_RX_BUF0 - rxbase) * 4) {
+ D(qemu_log("ethlite packet is too big, size=%x\n", size));
+ return -1;
+ }
memcpy(&s->regs[rxbase + R_RX_BUF0], buf, size);
s->regs[rxbase + R_RX_CTRL0] |= CTRL_S;
|
CWE-119
| null | null |
6,883
|
static void xilinx_ethlite_register_types(void)
{
type_register_static(&xilinx_ethlite_info);
}
|
Exec Code Overflow
| 0
|
static void xilinx_ethlite_register_types(void)
{
type_register_static(&xilinx_ethlite_info);
}
|
@@ -197,6 +197,10 @@ static ssize_t eth_rx(NetClientState *nc, const uint8_t *buf, size_t size)
}
D(qemu_log("%s %zd rxbase=%x\n", __func__, size, rxbase));
+ if (size > (R_MAX - R_RX_BUF0 - rxbase) * 4) {
+ D(qemu_log("ethlite packet is too big, size=%x\n", size));
+ return -1;
+ }
memcpy(&s->regs[rxbase + R_RX_BUF0], buf, size);
s->regs[rxbase + R_RX_CTRL0] |= CTRL_S;
|
CWE-119
| null | null |
6,884
|
static void xilinx_ethlite_reset(DeviceState *dev)
{
struct xlx_ethlite *s = XILINX_ETHLITE(dev);
s->rxbuf = 0;
}
|
Exec Code Overflow
| 0
|
static void xilinx_ethlite_reset(DeviceState *dev)
{
struct xlx_ethlite *s = XILINX_ETHLITE(dev);
s->rxbuf = 0;
}
|
@@ -197,6 +197,10 @@ static ssize_t eth_rx(NetClientState *nc, const uint8_t *buf, size_t size)
}
D(qemu_log("%s %zd rxbase=%x\n", __func__, size, rxbase));
+ if (size > (R_MAX - R_RX_BUF0 - rxbase) * 4) {
+ D(qemu_log("ethlite packet is too big, size=%x\n", size));
+ return -1;
+ }
memcpy(&s->regs[rxbase + R_RX_BUF0], buf, size);
s->regs[rxbase + R_RX_CTRL0] |= CTRL_S;
|
CWE-119
| null | null |
6,885
|
static size_t fill(uint8_t *data, size_t size, const char *fmt, ...)
{
va_list ap;
size_t ret;
va_start(ap, fmt);
ret = vfill(data, size, fmt, ap);
va_end(ap);
return ret;
}
|
DoS
| 0
|
static size_t fill(uint8_t *data, size_t size, const char *fmt, ...)
{
va_list ap;
size_t ret;
va_start(ap, fmt);
ret = vfill(data, size, fmt, ap);
va_end(ap);
return ret;
}
|
@@ -203,7 +203,7 @@ size_t mptsas_config_manufacturing_1(MPTSASState *s, uint8_t **data, int address
{
/* VPD - all zeros */
return MPTSAS_CONFIG_PACK(1, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
- "s256");
+ "*s256");
}
static
@@ -328,7 +328,7 @@ size_t mptsas_config_ioc_0(MPTSASState *s, uint8_t **data, int address)
return MPTSAS_CONFIG_PACK(0, MPI_CONFIG_PAGETYPE_IOC, 0x01,
"*l*lwwb*b*b*blww",
pcic->vendor_id, pcic->device_id, pcic->revision,
- pcic->subsystem_vendor_id,
+ pcic->class_id, pcic->subsystem_vendor_id,
pcic->subsystem_id);
}
|
CWE-20
| null | null |
6,886
|
size_t mptsas_config_io_unit_0(MPTSASState *s, uint8_t **data, int address)
{
PCIDevice *pci = PCI_DEVICE(s);
uint64_t unique_value = 0x53504D554D4551LL; /* "QEMUMPTx" */
unique_value |= (uint64_t)pci->devfn << 56;
return MPTSAS_CONFIG_PACK(0, MPI_CONFIG_PAGETYPE_IO_UNIT, 0x00,
"q", unique_value);
}
|
DoS
| 0
|
size_t mptsas_config_io_unit_0(MPTSASState *s, uint8_t **data, int address)
{
PCIDevice *pci = PCI_DEVICE(s);
uint64_t unique_value = 0x53504D554D4551LL; /* "QEMUMPTx" */
unique_value |= (uint64_t)pci->devfn << 56;
return MPTSAS_CONFIG_PACK(0, MPI_CONFIG_PAGETYPE_IO_UNIT, 0x00,
"q", unique_value);
}
|
@@ -203,7 +203,7 @@ size_t mptsas_config_manufacturing_1(MPTSASState *s, uint8_t **data, int address
{
/* VPD - all zeros */
return MPTSAS_CONFIG_PACK(1, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
- "s256");
+ "*s256");
}
static
@@ -328,7 +328,7 @@ size_t mptsas_config_ioc_0(MPTSASState *s, uint8_t **data, int address)
return MPTSAS_CONFIG_PACK(0, MPI_CONFIG_PAGETYPE_IOC, 0x01,
"*l*lwwb*b*b*blww",
pcic->vendor_id, pcic->device_id, pcic->revision,
- pcic->subsystem_vendor_id,
+ pcic->class_id, pcic->subsystem_vendor_id,
pcic->subsystem_id);
}
|
CWE-20
| null | null |
6,887
|
size_t mptsas_config_io_unit_1(MPTSASState *s, uint8_t **data, int address)
{
return MPTSAS_CONFIG_PACK(1, MPI_CONFIG_PAGETYPE_IO_UNIT, 0x02, "l",
0x41 /* single function, RAID disabled */ );
}
|
DoS
| 0
|
size_t mptsas_config_io_unit_1(MPTSASState *s, uint8_t **data, int address)
{
return MPTSAS_CONFIG_PACK(1, MPI_CONFIG_PAGETYPE_IO_UNIT, 0x02, "l",
0x41 /* single function, RAID disabled */ );
}
|
@@ -203,7 +203,7 @@ size_t mptsas_config_manufacturing_1(MPTSASState *s, uint8_t **data, int address
{
/* VPD - all zeros */
return MPTSAS_CONFIG_PACK(1, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
- "s256");
+ "*s256");
}
static
@@ -328,7 +328,7 @@ size_t mptsas_config_ioc_0(MPTSASState *s, uint8_t **data, int address)
return MPTSAS_CONFIG_PACK(0, MPI_CONFIG_PAGETYPE_IOC, 0x01,
"*l*lwwb*b*b*blww",
pcic->vendor_id, pcic->device_id, pcic->revision,
- pcic->subsystem_vendor_id,
+ pcic->class_id, pcic->subsystem_vendor_id,
pcic->subsystem_id);
}
|
CWE-20
| null | null |
6,888
|
size_t mptsas_config_io_unit_2(MPTSASState *s, uint8_t **data, int address)
{
PCIDevice *pci = PCI_DEVICE(s);
uint8_t devfn = pci->devfn;
return MPTSAS_CONFIG_PACK(2, MPI_CONFIG_PAGETYPE_IO_UNIT, 0x02,
"llbbw*b*b*w*b*b*w*b*b*w*l",
0, 0x100, 0 /* pci bus? */, devfn, 0);
}
|
DoS
| 0
|
size_t mptsas_config_io_unit_2(MPTSASState *s, uint8_t **data, int address)
{
PCIDevice *pci = PCI_DEVICE(s);
uint8_t devfn = pci->devfn;
return MPTSAS_CONFIG_PACK(2, MPI_CONFIG_PAGETYPE_IO_UNIT, 0x02,
"llbbw*b*b*w*b*b*w*b*b*w*l",
0, 0x100, 0 /* pci bus? */, devfn, 0);
}
|
@@ -203,7 +203,7 @@ size_t mptsas_config_manufacturing_1(MPTSASState *s, uint8_t **data, int address
{
/* VPD - all zeros */
return MPTSAS_CONFIG_PACK(1, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
- "s256");
+ "*s256");
}
static
@@ -328,7 +328,7 @@ size_t mptsas_config_ioc_0(MPTSASState *s, uint8_t **data, int address)
return MPTSAS_CONFIG_PACK(0, MPI_CONFIG_PAGETYPE_IOC, 0x01,
"*l*lwwb*b*b*blww",
pcic->vendor_id, pcic->device_id, pcic->revision,
- pcic->subsystem_vendor_id,
+ pcic->class_id, pcic->subsystem_vendor_id,
pcic->subsystem_id);
}
|
CWE-20
| null | null |
6,889
|
size_t mptsas_config_io_unit_4(MPTSASState *s, uint8_t **data, int address)
{
return MPTSAS_CONFIG_PACK(4, MPI_CONFIG_PAGETYPE_IO_UNIT, 0x00, "*l*l*q");
}
|
DoS
| 0
|
size_t mptsas_config_io_unit_4(MPTSASState *s, uint8_t **data, int address)
{
return MPTSAS_CONFIG_PACK(4, MPI_CONFIG_PAGETYPE_IO_UNIT, 0x00, "*l*l*q");
}
|
@@ -203,7 +203,7 @@ size_t mptsas_config_manufacturing_1(MPTSASState *s, uint8_t **data, int address
{
/* VPD - all zeros */
return MPTSAS_CONFIG_PACK(1, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
- "s256");
+ "*s256");
}
static
@@ -328,7 +328,7 @@ size_t mptsas_config_ioc_0(MPTSASState *s, uint8_t **data, int address)
return MPTSAS_CONFIG_PACK(0, MPI_CONFIG_PAGETYPE_IOC, 0x01,
"*l*lwwb*b*b*blww",
pcic->vendor_id, pcic->device_id, pcic->revision,
- pcic->subsystem_vendor_id,
+ pcic->class_id, pcic->subsystem_vendor_id,
pcic->subsystem_id);
}
|
CWE-20
| null | null |
6,890
|
size_t mptsas_config_ioc_1(MPTSASState *s, uint8_t **data, int address)
{
return MPTSAS_CONFIG_PACK(1, MPI_CONFIG_PAGETYPE_IOC, 0x03,
"*l*l*b*b*b*b");
}
|
DoS
| 0
|
size_t mptsas_config_ioc_1(MPTSASState *s, uint8_t **data, int address)
{
return MPTSAS_CONFIG_PACK(1, MPI_CONFIG_PAGETYPE_IOC, 0x03,
"*l*l*b*b*b*b");
}
|
@@ -203,7 +203,7 @@ size_t mptsas_config_manufacturing_1(MPTSASState *s, uint8_t **data, int address
{
/* VPD - all zeros */
return MPTSAS_CONFIG_PACK(1, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
- "s256");
+ "*s256");
}
static
@@ -328,7 +328,7 @@ size_t mptsas_config_ioc_0(MPTSASState *s, uint8_t **data, int address)
return MPTSAS_CONFIG_PACK(0, MPI_CONFIG_PAGETYPE_IOC, 0x01,
"*l*lwwb*b*b*blww",
pcic->vendor_id, pcic->device_id, pcic->revision,
- pcic->subsystem_vendor_id,
+ pcic->class_id, pcic->subsystem_vendor_id,
pcic->subsystem_id);
}
|
CWE-20
| null | null |
6,891
|
size_t mptsas_config_ioc_2(MPTSASState *s, uint8_t **data, int address)
{
return MPTSAS_CONFIG_PACK(2, MPI_CONFIG_PAGETYPE_IOC, 0x04,
"*l*b*b*b*b");
}
|
DoS
| 0
|
size_t mptsas_config_ioc_2(MPTSASState *s, uint8_t **data, int address)
{
return MPTSAS_CONFIG_PACK(2, MPI_CONFIG_PAGETYPE_IOC, 0x04,
"*l*b*b*b*b");
}
|
@@ -203,7 +203,7 @@ size_t mptsas_config_manufacturing_1(MPTSASState *s, uint8_t **data, int address
{
/* VPD - all zeros */
return MPTSAS_CONFIG_PACK(1, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
- "s256");
+ "*s256");
}
static
@@ -328,7 +328,7 @@ size_t mptsas_config_ioc_0(MPTSASState *s, uint8_t **data, int address)
return MPTSAS_CONFIG_PACK(0, MPI_CONFIG_PAGETYPE_IOC, 0x01,
"*l*lwwb*b*b*blww",
pcic->vendor_id, pcic->device_id, pcic->revision,
- pcic->subsystem_vendor_id,
+ pcic->class_id, pcic->subsystem_vendor_id,
pcic->subsystem_id);
}
|
CWE-20
| null | null |
6,892
|
size_t mptsas_config_ioc_3(MPTSASState *s, uint8_t **data, int address)
{
return MPTSAS_CONFIG_PACK(3, MPI_CONFIG_PAGETYPE_IOC, 0x00,
"*b*b*w");
}
|
DoS
| 0
|
size_t mptsas_config_ioc_3(MPTSASState *s, uint8_t **data, int address)
{
return MPTSAS_CONFIG_PACK(3, MPI_CONFIG_PAGETYPE_IOC, 0x00,
"*b*b*w");
}
|
@@ -203,7 +203,7 @@ size_t mptsas_config_manufacturing_1(MPTSASState *s, uint8_t **data, int address
{
/* VPD - all zeros */
return MPTSAS_CONFIG_PACK(1, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
- "s256");
+ "*s256");
}
static
@@ -328,7 +328,7 @@ size_t mptsas_config_ioc_0(MPTSASState *s, uint8_t **data, int address)
return MPTSAS_CONFIG_PACK(0, MPI_CONFIG_PAGETYPE_IOC, 0x01,
"*l*lwwb*b*b*blww",
pcic->vendor_id, pcic->device_id, pcic->revision,
- pcic->subsystem_vendor_id,
+ pcic->class_id, pcic->subsystem_vendor_id,
pcic->subsystem_id);
}
|
CWE-20
| null | null |
6,893
|
size_t mptsas_config_ioc_4(MPTSASState *s, uint8_t **data, int address)
{
return MPTSAS_CONFIG_PACK(4, MPI_CONFIG_PAGETYPE_IOC, 0x00,
"*b*b*w");
}
|
DoS
| 0
|
size_t mptsas_config_ioc_4(MPTSASState *s, uint8_t **data, int address)
{
return MPTSAS_CONFIG_PACK(4, MPI_CONFIG_PAGETYPE_IOC, 0x00,
"*b*b*w");
}
|
@@ -203,7 +203,7 @@ size_t mptsas_config_manufacturing_1(MPTSASState *s, uint8_t **data, int address
{
/* VPD - all zeros */
return MPTSAS_CONFIG_PACK(1, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
- "s256");
+ "*s256");
}
static
@@ -328,7 +328,7 @@ size_t mptsas_config_ioc_0(MPTSASState *s, uint8_t **data, int address)
return MPTSAS_CONFIG_PACK(0, MPI_CONFIG_PAGETYPE_IOC, 0x01,
"*l*lwwb*b*b*blww",
pcic->vendor_id, pcic->device_id, pcic->revision,
- pcic->subsystem_vendor_id,
+ pcic->class_id, pcic->subsystem_vendor_id,
pcic->subsystem_id);
}
|
CWE-20
| null | null |
6,894
|
size_t mptsas_config_ioc_5(MPTSASState *s, uint8_t **data, int address)
{
return MPTSAS_CONFIG_PACK(5, MPI_CONFIG_PAGETYPE_IOC, 0x00,
"*l*b*b*w");
}
|
DoS
| 0
|
size_t mptsas_config_ioc_5(MPTSASState *s, uint8_t **data, int address)
{
return MPTSAS_CONFIG_PACK(5, MPI_CONFIG_PAGETYPE_IOC, 0x00,
"*l*b*b*w");
}
|
@@ -203,7 +203,7 @@ size_t mptsas_config_manufacturing_1(MPTSASState *s, uint8_t **data, int address
{
/* VPD - all zeros */
return MPTSAS_CONFIG_PACK(1, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
- "s256");
+ "*s256");
}
static
@@ -328,7 +328,7 @@ size_t mptsas_config_ioc_0(MPTSASState *s, uint8_t **data, int address)
return MPTSAS_CONFIG_PACK(0, MPI_CONFIG_PAGETYPE_IOC, 0x01,
"*l*lwwb*b*b*blww",
pcic->vendor_id, pcic->device_id, pcic->revision,
- pcic->subsystem_vendor_id,
+ pcic->class_id, pcic->subsystem_vendor_id,
pcic->subsystem_id);
}
|
CWE-20
| null | null |
6,895
|
size_t mptsas_config_ioc_6(MPTSASState *s, uint8_t **data, int address)
{
return MPTSAS_CONFIG_PACK(6, MPI_CONFIG_PAGETYPE_IOC, 0x01,
"*l*b*b*b*b*b*b*b*b*b*b*w*l*l*l*l*b*b*w"
"*w*w*w*w*l*l*l");
}
|
DoS
| 0
|
size_t mptsas_config_ioc_6(MPTSASState *s, uint8_t **data, int address)
{
return MPTSAS_CONFIG_PACK(6, MPI_CONFIG_PAGETYPE_IOC, 0x01,
"*l*b*b*b*b*b*b*b*b*b*b*w*l*l*l*l*b*b*w"
"*w*w*w*w*l*l*l");
}
|
@@ -203,7 +203,7 @@ size_t mptsas_config_manufacturing_1(MPTSASState *s, uint8_t **data, int address
{
/* VPD - all zeros */
return MPTSAS_CONFIG_PACK(1, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
- "s256");
+ "*s256");
}
static
@@ -328,7 +328,7 @@ size_t mptsas_config_ioc_0(MPTSASState *s, uint8_t **data, int address)
return MPTSAS_CONFIG_PACK(0, MPI_CONFIG_PAGETYPE_IOC, 0x01,
"*l*lwwb*b*b*blww",
pcic->vendor_id, pcic->device_id, pcic->revision,
- pcic->subsystem_vendor_id,
+ pcic->class_id, pcic->subsystem_vendor_id,
pcic->subsystem_id);
}
|
CWE-20
| null | null |
6,896
|
size_t mptsas_config_manufacturing_0(MPTSASState *s, uint8_t **data, int address)
{
return MPTSAS_CONFIG_PACK(0, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
"s16s8s16s16s16",
"QEMU MPT Fusion",
"2.5",
"QEMU MPT Fusion",
"QEMU",
"0000111122223333");
}
|
DoS
| 0
|
size_t mptsas_config_manufacturing_0(MPTSASState *s, uint8_t **data, int address)
{
return MPTSAS_CONFIG_PACK(0, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
"s16s8s16s16s16",
"QEMU MPT Fusion",
"2.5",
"QEMU MPT Fusion",
"QEMU",
"0000111122223333");
}
|
@@ -203,7 +203,7 @@ size_t mptsas_config_manufacturing_1(MPTSASState *s, uint8_t **data, int address
{
/* VPD - all zeros */
return MPTSAS_CONFIG_PACK(1, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
- "s256");
+ "*s256");
}
static
@@ -328,7 +328,7 @@ size_t mptsas_config_ioc_0(MPTSASState *s, uint8_t **data, int address)
return MPTSAS_CONFIG_PACK(0, MPI_CONFIG_PAGETYPE_IOC, 0x01,
"*l*lwwb*b*b*blww",
pcic->vendor_id, pcic->device_id, pcic->revision,
- pcic->subsystem_vendor_id,
+ pcic->class_id, pcic->subsystem_vendor_id,
pcic->subsystem_id);
}
|
CWE-20
| null | null |
6,897
|
size_t mptsas_config_manufacturing_10(MPTSASState *s, uint8_t **data, int address)
{
return MPTSAS_CONFIG_PACK(10, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
"*l");
}
|
DoS
| 0
|
size_t mptsas_config_manufacturing_10(MPTSASState *s, uint8_t **data, int address)
{
return MPTSAS_CONFIG_PACK(10, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
"*l");
}
|
@@ -203,7 +203,7 @@ size_t mptsas_config_manufacturing_1(MPTSASState *s, uint8_t **data, int address
{
/* VPD - all zeros */
return MPTSAS_CONFIG_PACK(1, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
- "s256");
+ "*s256");
}
static
@@ -328,7 +328,7 @@ size_t mptsas_config_ioc_0(MPTSASState *s, uint8_t **data, int address)
return MPTSAS_CONFIG_PACK(0, MPI_CONFIG_PAGETYPE_IOC, 0x01,
"*l*lwwb*b*b*blww",
pcic->vendor_id, pcic->device_id, pcic->revision,
- pcic->subsystem_vendor_id,
+ pcic->class_id, pcic->subsystem_vendor_id,
pcic->subsystem_id);
}
|
CWE-20
| null | null |
6,898
|
size_t mptsas_config_manufacturing_2(MPTSASState *s, uint8_t **data, int address)
{
PCIDeviceClass *pcic = PCI_DEVICE_GET_CLASS(s);
return MPTSAS_CONFIG_PACK(2, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
"wb*b*l",
pcic->device_id, pcic->revision);
}
|
DoS
| 0
|
size_t mptsas_config_manufacturing_2(MPTSASState *s, uint8_t **data, int address)
{
PCIDeviceClass *pcic = PCI_DEVICE_GET_CLASS(s);
return MPTSAS_CONFIG_PACK(2, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
"wb*b*l",
pcic->device_id, pcic->revision);
}
|
@@ -203,7 +203,7 @@ size_t mptsas_config_manufacturing_1(MPTSASState *s, uint8_t **data, int address
{
/* VPD - all zeros */
return MPTSAS_CONFIG_PACK(1, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
- "s256");
+ "*s256");
}
static
@@ -328,7 +328,7 @@ size_t mptsas_config_ioc_0(MPTSASState *s, uint8_t **data, int address)
return MPTSAS_CONFIG_PACK(0, MPI_CONFIG_PAGETYPE_IOC, 0x01,
"*l*lwwb*b*b*blww",
pcic->vendor_id, pcic->device_id, pcic->revision,
- pcic->subsystem_vendor_id,
+ pcic->class_id, pcic->subsystem_vendor_id,
pcic->subsystem_id);
}
|
CWE-20
| null | null |
6,899
|
size_t mptsas_config_manufacturing_3(MPTSASState *s, uint8_t **data, int address)
{
PCIDeviceClass *pcic = PCI_DEVICE_GET_CLASS(s);
return MPTSAS_CONFIG_PACK(3, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
"wb*b*l",
pcic->device_id, pcic->revision);
}
|
DoS
| 0
|
size_t mptsas_config_manufacturing_3(MPTSASState *s, uint8_t **data, int address)
{
PCIDeviceClass *pcic = PCI_DEVICE_GET_CLASS(s);
return MPTSAS_CONFIG_PACK(3, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
"wb*b*l",
pcic->device_id, pcic->revision);
}
|
@@ -203,7 +203,7 @@ size_t mptsas_config_manufacturing_1(MPTSASState *s, uint8_t **data, int address
{
/* VPD - all zeros */
return MPTSAS_CONFIG_PACK(1, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
- "s256");
+ "*s256");
}
static
@@ -328,7 +328,7 @@ size_t mptsas_config_ioc_0(MPTSASState *s, uint8_t **data, int address)
return MPTSAS_CONFIG_PACK(0, MPI_CONFIG_PAGETYPE_IOC, 0x01,
"*l*lwwb*b*b*blww",
pcic->vendor_id, pcic->device_id, pcic->revision,
- pcic->subsystem_vendor_id,
+ pcic->class_id, pcic->subsystem_vendor_id,
pcic->subsystem_id);
}
|
CWE-20
| null | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.