bpp-bare.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Trivial binary packet protocol for the 'bare' ssh-connection
  3. * protocol used in PuTTY's SSH-2 connection sharing system.
  4. */
  5. #include <assert.h>
  6. #include "putty.h"
  7. #include "ssh.h"
  8. #include "bpp.h"
  9. #include "sshcr.h"
  10. struct ssh2_bare_bpp_state {
  11. int crState;
  12. long packetlen, maxlen;
  13. unsigned char *data;
  14. unsigned long incoming_sequence, outgoing_sequence;
  15. PktIn *pktin;
  16. BinaryPacketProtocol bpp;
  17. };
  18. static void ssh2_bare_bpp_free(BinaryPacketProtocol *bpp);
  19. static void ssh2_bare_bpp_handle_input(BinaryPacketProtocol *bpp);
  20. static void ssh2_bare_bpp_handle_output(BinaryPacketProtocol *bpp);
  21. static PktOut *ssh2_bare_bpp_new_pktout(int type);
  22. static const BinaryPacketProtocolVtable ssh2_bare_bpp_vtable = {
  23. .free = ssh2_bare_bpp_free,
  24. .handle_input = ssh2_bare_bpp_handle_input,
  25. .handle_output = ssh2_bare_bpp_handle_output,
  26. .new_pktout = ssh2_bare_bpp_new_pktout,
  27. .queue_disconnect = ssh2_bpp_queue_disconnect, /* in common.c */
  28. /* packet size limit, per protocol spec in sharing.c comment */
  29. .packet_size_limit = 0x4000,
  30. };
  31. BinaryPacketProtocol *ssh2_bare_bpp_new(LogContext *logctx)
  32. {
  33. struct ssh2_bare_bpp_state *s = snew(struct ssh2_bare_bpp_state);
  34. memset(s, 0, sizeof(*s));
  35. s->bpp.vt = &ssh2_bare_bpp_vtable;
  36. s->bpp.logctx = logctx;
  37. ssh_bpp_common_setup(&s->bpp);
  38. return &s->bpp;
  39. }
  40. static void ssh2_bare_bpp_free(BinaryPacketProtocol *bpp)
  41. {
  42. struct ssh2_bare_bpp_state *s =
  43. container_of(bpp, struct ssh2_bare_bpp_state, bpp);
  44. sfree(s->pktin);
  45. sfree(s);
  46. }
  47. #define BPP_READ(ptr, len) do \
  48. { \
  49. bool success; \
  50. crMaybeWaitUntilV((success = bufchain_try_fetch_consume( \
  51. s->bpp.in_raw, ptr, len)) || \
  52. s->bpp.input_eof); \
  53. if (!success) \
  54. goto eof; \
  55. ssh_check_frozen(s->bpp.ssh); \
  56. } while (0)
  57. static void ssh2_bare_bpp_handle_input(BinaryPacketProtocol *bpp)
  58. {
  59. struct ssh2_bare_bpp_state *s =
  60. container_of(bpp, struct ssh2_bare_bpp_state, bpp);
  61. crBegin(s->crState);
  62. while (1) {
  63. /* Read the length field. */
  64. {
  65. unsigned char lenbuf[4];
  66. BPP_READ(lenbuf, 4);
  67. s->packetlen = toint(GET_32BIT_MSB_FIRST(lenbuf));
  68. }
  69. if (s->packetlen <= 0 || s->packetlen >= (long)OUR_V2_PACKETLIMIT) {
  70. ssh_sw_abort(s->bpp.ssh, "Invalid packet length received");
  71. crStopV;
  72. }
  73. /*
  74. * Allocate the packet to return, now we know its length.
  75. */
  76. s->pktin = snew_plus(PktIn, s->packetlen);
  77. s->pktin->qnode.prev = s->pktin->qnode.next = NULL;
  78. s->pktin->qnode.on_free_queue = false;
  79. s->maxlen = 0;
  80. s->data = snew_plus_get_aux(s->pktin);
  81. s->pktin->sequence = s->incoming_sequence++;
  82. /*
  83. * Read the remainder of the packet.
  84. */
  85. BPP_READ(s->data, s->packetlen);
  86. /*
  87. * The data we just read is precisely the initial type byte
  88. * followed by the packet payload.
  89. */
  90. s->pktin->type = s->data[0];
  91. s->data++;
  92. s->packetlen--;
  93. BinarySource_INIT(s->pktin, s->data, s->packetlen);
  94. if (s->pktin->type == SSH2_MSG_EXT_INFO) {
  95. /*
  96. * Mild layer violation: EXT_INFO is not permitted in the
  97. * bare ssh-connection protocol. Faulting it here means
  98. * that ssh2_common_filter_queue doesn't receive it in the
  99. * first place unless it's legal to have sent it.
  100. */
  101. ssh_proto_error(s->bpp.ssh, "Remote side sent SSH2_MSG_EXT_INFO "
  102. "in bare connection protocol");
  103. return;
  104. }
  105. /*
  106. * Log incoming packet, possibly omitting sensitive fields.
  107. */
  108. if (s->bpp.logctx) {
  109. logblank_t blanks[MAX_BLANKS];
  110. int nblanks = ssh2_censor_packet(
  111. s->bpp.pls, s->pktin->type, false,
  112. make_ptrlen(s->data, s->packetlen), blanks);
  113. log_packet(s->bpp.logctx, PKT_INCOMING, s->pktin->type,
  114. ssh2_pkt_type(s->bpp.pls->kctx, s->bpp.pls->actx,
  115. s->pktin->type),
  116. get_ptr(s->pktin), get_avail(s->pktin), nblanks, blanks,
  117. &s->pktin->sequence, 0, NULL);
  118. }
  119. if (ssh2_bpp_check_unimplemented(&s->bpp, s->pktin)) {
  120. sfree(s->pktin);
  121. s->pktin = NULL;
  122. continue;
  123. }
  124. s->pktin->qnode.formal_size = get_avail(s->pktin);
  125. pq_push(&s->bpp.in_pq, s->pktin);
  126. s->pktin = NULL;
  127. }
  128. eof:
  129. if (!s->bpp.expect_close) {
  130. ssh_remote_error(s->bpp.ssh,
  131. "Remote side unexpectedly closed network connection");
  132. } else {
  133. ssh_remote_eof(s->bpp.ssh, "Remote side closed network connection");
  134. }
  135. return; /* avoid touching s now it's been freed */
  136. crFinishV;
  137. }
  138. static PktOut *ssh2_bare_bpp_new_pktout(int pkt_type)
  139. {
  140. PktOut *pkt = ssh_new_packet();
  141. pkt->length = 4; /* space for packet length */
  142. pkt->type = pkt_type;
  143. put_byte(pkt, pkt_type);
  144. return pkt;
  145. }
  146. static void ssh2_bare_bpp_format_packet(struct ssh2_bare_bpp_state *s,
  147. PktOut *pkt)
  148. {
  149. if (s->bpp.logctx) {
  150. ptrlen pktdata = make_ptrlen(pkt->data + 5, pkt->length - 5);
  151. logblank_t blanks[MAX_BLANKS];
  152. int nblanks = ssh2_censor_packet(
  153. s->bpp.pls, pkt->type, true, pktdata, blanks);
  154. log_packet(s->bpp.logctx, PKT_OUTGOING, pkt->type,
  155. ssh2_pkt_type(s->bpp.pls->kctx, s->bpp.pls->actx,
  156. pkt->type),
  157. pktdata.ptr, pktdata.len, nblanks, blanks,
  158. &s->outgoing_sequence,
  159. pkt->downstream_id, pkt->additional_log_text);
  160. }
  161. s->outgoing_sequence++; /* only for diagnostics, really */
  162. PUT_32BIT_MSB_FIRST(pkt->data, pkt->length - 4);
  163. bufchain_add(s->bpp.out_raw, pkt->data, pkt->length);
  164. }
  165. static void ssh2_bare_bpp_handle_output(BinaryPacketProtocol *bpp)
  166. {
  167. struct ssh2_bare_bpp_state *s =
  168. container_of(bpp, struct ssh2_bare_bpp_state, bpp);
  169. PktOut *pkt;
  170. while ((pkt = pq_pop(&s->bpp.out_pq)) != NULL) {
  171. ssh2_bare_bpp_format_packet(s, pkt);
  172. ssh_free_pktout(pkt);
  173. }
  174. ssh_sendbuffer_changed(bpp->ssh);
  175. }