robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

zeertzjqfix(channel): stack-buffer-overflow with exit during connection (#39387)4ed2e66d2e

master
4.4 KiB154 linesraw
1#include <assert.h>
2#include <stdbool.h>
3#include <stddef.h>
4#include <uv.h>
5#include <uv/version.h>
6
7#include "nvim/event/defs.h"
8#include "nvim/event/loop.h"
9#include "nvim/event/stream.h"
10#include "nvim/log.h"
11#include "nvim/memory.h"
12#include "nvim/types_defs.h"
13#ifdef MSWIN
14# include "nvim/os/os_win_console.h"
15#endif
16
17#include "event/stream.c.generated.h"
18
19// For compatibility with libuv < 1.19.0 (tested on 1.18.0)
20#if UV_VERSION_MINOR < 19
21# define uv_stream_get_write_queue_size(stream) stream->write_queue_size
22#endif
23
24/// Sets the stream associated with `fd` to "blocking" mode.
25///
26/// @return `0` on success, or libuv error code on failure.
27int stream_set_blocking(int fd, bool blocking)
28{
29  // Private loop to avoid conflict with existing watcher(s):
30  //    uv__io_stop: Assertion `loop->watchers[w->fd] == w' failed.
31  uv_loop_t loop;
32  uv_pipe_t stream;
33  uv_loop_init(&loop);
34  uv_pipe_init(&loop, &stream, 0);
35  uv_pipe_open(&stream, fd);
36  int retval = uv_stream_set_blocking((uv_stream_t *)&stream, blocking);
37  uv_close((uv_handle_t *)&stream, NULL);
38  uv_run(&loop, UV_RUN_NOWAIT);  // not necessary, but couldn't hurt.
39  uv_loop_close(&loop);
40  return retval;
41}
42
43void stream_init(Loop *loop, Stream *stream, int fd, uv_stream_t *uvstream)
44  FUNC_ATTR_NONNULL_ARG(2)
45{
46  // The underlying stream is either a file or an existing uv stream.
47  assert(uvstream == NULL ? fd >= 0 && loop != NULL : fd < 0 && loop == NULL);
48  stream->uvstream = uvstream;
49
50  if (fd >= 0) {
51    uv_handle_type type = uv_guess_handle(fd);
52    stream->fd = fd;
53
54    if (type == UV_FILE) {
55      // Non-blocking file reads are simulated with an idle handle that reads in
56      // chunks of the ring buffer size, giving time for other events to be
57      // processed between reads.
58      uv_idle_init(&loop->uv, &stream->uv.idle);
59      stream->uv.idle.data = stream;
60#ifdef MSWIN
61    } else if (type == UV_TTY) {
62      uv_tty_init(&loop->uv, &stream->uv.tty, fd, 0);
63      uv_tty_set_mode(&stream->uv.tty, UV_TTY_MODE_RAW);
64      DWORD dwMode;
65      if (GetConsoleMode(stream->uv.tty.handle, &dwMode)) {
66        dwMode |= ENABLE_VIRTUAL_TERMINAL_INPUT;
67        SetConsoleMode(stream->uv.tty.handle, dwMode);
68      }
69      stream->uvstream = (uv_stream_t *)&stream->uv.tty;
70#endif
71    } else {
72      assert(type == UV_NAMED_PIPE || type == UV_TTY);
73      uv_pipe_init(&loop->uv, &stream->uv.pipe, 0);
74      uv_pipe_open(&stream->uv.pipe, fd);
75      stream->uvstream = (uv_stream_t *)&stream->uv.pipe;
76    }
77  }
78
79  if (stream->uvstream) {
80    stream->uvstream->data = stream;
81  }
82
83  stream->fpos = 0;
84  stream->internal_data = NULL;
85  stream->curmem = 0;
86  stream->maxmem = 0;
87  stream->pending_reqs = 0;
88  stream->write_cb = NULL;
89  stream->close_cb = NULL;
90  stream->internal_close_cb = NULL;
91  stream->closed = false;
92  stream->events = NULL;
93}
94
95void stream_may_close(Stream *stream)
96  FUNC_ATTR_NONNULL_ARG(1)
97{
98  if (stream->closed) {
99    return;
100  }
101  DLOG("closing Stream: %p", (void *)stream);
102  stream->closed = true;
103
104#ifdef MSWIN
105  if (UV_TTY == uv_guess_handle(stream->fd)) {
106    // Undo UV_TTY_MODE_RAW from stream_init(). #10801
107    uv_tty_set_mode(&stream->uv.tty, UV_TTY_MODE_NORMAL);
108  }
109#endif
110
111  if (!stream->pending_reqs) {
112    stream_close_handle(stream);
113  }  // Else: rstream.c:read_event() or wstream.c:write_cb() will call stream_close_handle().
114}
115
116void stream_close_handle(Stream *stream)
117  FUNC_ATTR_NONNULL_ALL
118{
119  uv_handle_t *handle = NULL;
120  if (stream->uvstream) {
121    if (uv_stream_get_write_queue_size(stream->uvstream) > 0) {
122      WLOG("closed Stream (%p) with %zu unwritten bytes",
123           (void *)stream,
124           uv_stream_get_write_queue_size(stream->uvstream));
125    }
126    handle = (uv_handle_t *)stream->uvstream;
127  } else {
128    handle = (uv_handle_t *)&stream->uv.idle;
129  }
130
131  assert(handle != NULL);
132
133  if (stream->before_close_cb) {
134    stream->pending_reqs++;
135    stream->before_close_cb(stream, stream->close_cb_data);
136    stream->pending_reqs--;
137  }
138  if (!uv_is_closing(handle)) {
139    uv_close(handle, close_cb);
140  }
141}
142
143static void close_cb(uv_handle_t *handle)
144{
145  Stream *stream = handle->data;
146  // Check if handle->data is NULL here, in case this callback is called between
147  // the handle's initialization and stream_init().
148  if (stream && stream->close_cb) {
149    stream->close_cb(stream, stream->close_cb_data);
150  }
151  if (stream && stream->internal_close_cb) {
152    stream->internal_close_cb(stream, stream->internal_data);
153  }
154}