From a629688008694ed8022269e66826d4d6ec688b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Witold=20Kr=C4=99cicki?= Date: Sat, 30 Nov 2019 00:53:02 +0100 Subject: [PATCH] pipe: disallow listening on an IPC pipe MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/libuv/libuv/pull/2559 Reviewed-By: Jameson Nash Reviewed-By: Saúl Ibarra Corretgé --- docs/src/pipe.rst | 6 +++++- src/unix/pipe.c | 3 +++ src/win/pipe.c | 4 ++++ test/benchmark-multi-accept.c | 8 ++++++-- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/docs/src/pipe.rst b/docs/src/pipe.rst index 5eac1b6d..6437a9d9 100644 --- a/docs/src/pipe.rst +++ b/docs/src/pipe.rst @@ -24,6 +24,8 @@ Public members .. c:member:: int uv_pipe_t.ipc Whether this pipe is suitable for handle passing between processes. + Only a connected pipe that will be passing the handles should have this flag + set, not the listening pipe that uv_accept is called on. .. seealso:: The :c:type:`uv_stream_t` members also apply. @@ -35,7 +37,9 @@ API Initialize a pipe handle. The `ipc` argument is a boolean to indicate if this pipe will be used for handle passing between processes (which may - change the bytes on the wire). + change the bytes on the wire). Only a connected pipe that will be + passing the handles should have this flag set, not the listening pipe + that uv_accept is called on. .. c:function:: int uv_pipe_open(uv_pipe_t* handle, uv_file file) diff --git a/src/unix/pipe.c b/src/unix/pipe.c index 78ce1542..040d5781 100644 --- a/src/unix/pipe.c +++ b/src/unix/pipe.c @@ -95,6 +95,9 @@ int uv_pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb) { if (uv__stream_fd(handle) == -1) return UV_EINVAL; + if (handle->ipc) + return UV_EINVAL; + #if defined(__MVS__) || defined(__PASE__) /* On zOS, backlog=0 has undefined behaviour */ /* On IBMi PASE, backlog=0 leads to "Connection refused" error */ diff --git a/src/win/pipe.c b/src/win/pipe.c index 277f6497..5d916e9f 100644 --- a/src/win/pipe.c +++ b/src/win/pipe.c @@ -955,6 +955,10 @@ int uv_pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb) { return ERROR_NOT_SUPPORTED; } + if (handle->ipc) { + return WSAEINVAL; + } + handle->flags |= UV_HANDLE_LISTENING; INCREASE_ACTIVE_COUNT(loop, handle); handle->stream.serv.connection_cb = cb; diff --git a/test/benchmark-multi-accept.c b/test/benchmark-multi-accept.c index 2f32c0ca..5a186233 100644 --- a/test/benchmark-multi-accept.c +++ b/test/benchmark-multi-accept.c @@ -218,8 +218,12 @@ static void send_listen_handles(uv_handle_type type, } else ASSERT(0); - - ASSERT(0 == uv_pipe_init(loop, &ctx.ipc_pipe, 1)); + /* We need to initialize this pipe with ipc=0 - this is not a uv_pipe we'll + * be sending handles over, it's just for listening for new connections. + * If we accept a connection then the connected pipe must be initialized + * with ipc=1. + */ + ASSERT(0 == uv_pipe_init(loop, &ctx.ipc_pipe, 0)); ASSERT(0 == uv_pipe_bind(&ctx.ipc_pipe, IPC_PIPE_NAME)); ASSERT(0 == uv_listen((uv_stream_t*) &ctx.ipc_pipe, 128, ipc_connection_cb));