Paulo Guilherme Silva via dev <[email protected]> writes:

> SSL streams can't send a single message larger than INT_MAX bytes,
> e.g., the initial monitor reply or a snapshot of a big Northbound or
> Southbound database.  The connection is closed instead:
>
>  - ssl_do_tx() passes the size of the whole pending buffer to
>    SSL_write(), which takes the length as an 'int'.  For a buffer
>    larger than INT_MAX bytes the length overflows and becomes negative,
>    SSL_write() fails with a bad length error and the connection is
>    closed with EIO.
>
>  - ssl_send() reports the whole buffer as sent, since it queues what
>    SSL_write() could not send, but stream_send() returns that value
>    as an 'int', so it overflows as well.
>
> TCP and unix streams are not affected in practice, because send() on
> Linux doesn't transfer more than 0x7ffff000 bytes at once.
>
> Fix the first by using SSL_write_ex(), which takes a 'size_t' length
> and returns the number of bytes written separately.  It returns 0 on
> any failure, so keep passing a negative value to interpret_ssl_error()
> to still report system errors, like for SSL_write().  OpenSSL 1.1.1,
> the minimum supported version, provides SSL_write_ex().
>
> Fix the second by making stream_send() return an 'ssize_t', which is
> what the stream providers already return, and by using it in
> jsonrpc_run().  The other callers only send small messages.
>
> We hit this downstream with a Southbound database of 2.7 GiB: the SSL
> connection from ovsdb-server was closed every time it had to send the
> database contents.  A fix based on SSL_write_ex() along the lines of
> this patch has been validated in production.  Compared to it, this
> version also keeps the tx_want handling and the system error reporting
> of the SSL_write() error path unchanged.
>
> No test is included, as reproducing the issue needs a single message
> larger than 2 GiB, which is too much memory for the CI.
>
> Signed-off-by: Paulo Guilherme Silva <[email protected]>
> ---

AI notes the following:

  lib/netdev-dummy.c:294 and tests/test-vconn.c:285 assign the
  now-ssize_t stream_send() return value to 'int'. Harmless for
  packet-sized messages, as the commit message notes, but inconsistent
  with the widened API.

>  lib/jsonrpc.c    |  2 +-
>  lib/stream-ssl.c | 11 ++++++++---
>  lib/stream.c     |  4 ++--
>  lib/stream.h     |  2 +-
>  4 files changed, 12 insertions(+), 7 deletions(-)
>
> diff --git a/lib/jsonrpc.c b/lib/jsonrpc.c
> index f01a2e56f..d359e65d5 100644
> --- a/lib/jsonrpc.c
> +++ b/lib/jsonrpc.c
> @@ -125,7 +125,7 @@ jsonrpc_run(struct jsonrpc *rpc)
>      while (!ovs_list_is_empty(&rpc->output)) {
>          struct ovs_list *head = ovs_list_front(&rpc->output);
>          struct ofpbuf *buf = ofpbuf_from_list(head);
> -        int retval;
> +        ssize_t retval;
>  
>          retval = stream_send(rpc->stream, buf->data, buf->size);
>          if (retval >= 0) {
> diff --git a/lib/stream-ssl.c b/lib/stream-ssl.c
> index 4a35a9525..f1806eb5a 100644
> --- a/lib/stream-ssl.c
> +++ b/lib/stream-ssl.c
> @@ -720,13 +720,15 @@ ssl_do_tx(struct stream *stream)
>  
>      for (;;) {
>          int old_state = SSL_get_state(sslv->ssl);
> -        int ret = SSL_write(sslv->ssl, sslv->txbuf->data, sslv->txbuf->size);
> +        size_t written;
> +        int ret = SSL_write_ex(sslv->ssl, sslv->txbuf->data,
> +                               sslv->txbuf->size, &written);
>          if (old_state != SSL_get_state(sslv->ssl)) {
>              sslv->rx_want = SSL_NOTHING;
>          }
>          sslv->tx_want = SSL_NOTHING;
>          if (ret > 0) {
> -            ofpbuf_pull(sslv->txbuf, ret);
> +            ofpbuf_pull(sslv->txbuf, written);
>              if (sslv->txbuf->size == 0) {
>                  return 0;
>              }
> @@ -736,7 +738,10 @@ ssl_do_tx(struct stream *stream)
>                  VLOG_WARN_RL(&rl, "SSL_write: connection closed");
>                  return EPIPE;
>              } else {
> -                return interpret_ssl_error("SSL_write", ret, ssl_error,
> +                /* SSL_write_ex() returns 0 on any failure, while
> +                 * interpret_ssl_error() expects the SSL_write() convention
> +                 * to report system errors, i.e., a negative value. */
> +                return interpret_ssl_error("SSL_write", -1, ssl_error,
>                                             &sslv->tx_want);
>              }
>          }
> diff --git a/lib/stream.c b/lib/stream.c
> index 0d7e5261c..6ce191b15 100644
> --- a/lib/stream.c
> +++ b/lib/stream.c
> @@ -393,10 +393,10 @@ stream_recv(struct stream *stream, void *buffer, size_t 
> n)
>   *
>   * The send function will not block.  If no bytes can be immediately accepted
>   * for transmission, it returns -EAGAIN immediately. */
> -int
> +ssize_t
>  stream_send(struct stream *stream, const void *buffer, size_t n)
>  {
> -    int retval = stream_connect(stream);
> +    ssize_t retval = stream_connect(stream);

  stream_replay_write() (lib/stream-replay.c:129) and ovs_replay_write()
  still take an `int n`, so the widened retval is implicitly narrowed
  back at line 404. This only matters in OVS_REPLAY_WRITE mode with a >2
  GiB send, where the recorded length would wrap — but that truncation
  existed before this commit (stream_send itself returned int), and the
  replay file format stores lengths as int anyway, so it is not a
  regression introduced here. Worth noting for a follow-up if replay of
  large messages ever matters.

>      retval = retval ? -retval
>               : n == 0 ? 0
>               : (stream->class->send)(stream, buffer, n);
> diff --git a/lib/stream.h b/lib/stream.h
> index e30c51275..7b4195f6a 100644
> --- a/lib/stream.h
> +++ b/lib/stream.h
> @@ -39,7 +39,7 @@ void stream_close(struct stream *);
>  const char *stream_get_name(const struct stream *);
>  int stream_connect(struct stream *);
>  int stream_recv(struct stream *, void *buffer, size_t n);
> -int stream_send(struct stream *, const void *buffer, size_t n);
> +ssize_t stream_send(struct stream *, const void *buffer, size_t n);
>  
>  void stream_run(struct stream *);
>  void stream_run_wait(struct stream *);
> -- 
>
> 2.34.1
_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to