Software / code / prosody
File
doc/net.server.lua @ 13652:a08065207ef0
net.server_epoll: Call :shutdown() on TLS sockets when supported
Comment from Matthew:
This fixes a potential issue where the Prosody process gets blocked on sockets
waiting for them to close. Unlike non-TLS sockets, closing a TLS socket sends
layer 7 data, and this can cause problems for sockets which are in the process
of being cleaned up.
This depends on LuaSec changes which are not yet upstream.
From Martijn's original email:
So first my analysis of luasec. in ssl.c the socket is put into blocking
mode right before calling SSL_shutdown() inside meth_destroy(). My best
guess to why this is is because meth_destroy is linked to the __close
and __gc methods, which can't exactly be called multiple times and
luasec does want to make sure that a tls session is shutdown as clean
as possible.
I can't say I disagree with this reasoning and don't want to change this
behaviour. My solution to this without changing the current behaviour is
to introduce a shutdown() method. I am aware that this overlaps in a
conflicting way with tcp's shutdown method, but it stays close to the
OpenSSL name. This method calls SSL_shutdown() in the current
(non)blocking mode of the underlying socket and returns a boolean
whether or not the shutdown is completed (matching SSL_shutdown()'s 0
or 1 return values), and returns the familiar ssl_ioerror() strings on
error with a false for completion. This error can then be used to
determine if we have wantread/wantwrite to finalize things. Once
meth_shutdown() has been called once a shutdown flag will be set, which
indicates to meth_destroy() that the SSL_shutdown() has been handled
by the application and it shouldn't be needed to set the socket to
blocking mode. I've left the SSL_shutdown() call in the
LSEC_STATE_CONNECTED to prevent TOCTOU if the application reaches a
timeout for the shutdown code, which might allow SSL_shutdown() to
clean up anyway at the last possible moment.
Another thing I've changed to luasec is the call to socket_setblocking()
right before calling close(2) in socket_destroy() in usocket.c.
According to the latest POSIX[0]:
Note that the requirement for close() on a socket to block for up to
the current linger interval is not conditional on the O_NONBLOCK
setting.
Which I read to mean that removing O_NONBLOCK on the socket before close
doesn't impact the behaviour and only causes noise in system call
tracers. I didn't touch the windows bits of this, since I don't do
windows.
For the prosody side of things I've made the TLS shutdown bits resemble
interface:onwritable(), and put it under a combined guard of self._tls
and self.conn.shutdown. The self._tls bit is there to prevent getting
stuck on this condition, and self.conn.shutdown is there to prevent the
code being called by instances where the patched luasec isn't deployed.
The destroy() method can be called from various places and is read by
me as the "we give up" error path. To accommodate for these unexpected
entrypoints I've added a single call to self.conn:shutdown() to prevent
the socket being put into blocking mode. I have no expectations that
there is any other use here. Same as previous, the self.conn.shutdown
check is there to make sure it's not called on unpatched luasec
deployments and self._tls is there to make sure we don't call shutdown()
on tcp sockets.
I wouldn't recommend logging of the conn:shutdown() error inside
close(), since a lot of clients simply close the connection before
SSL_shutdown() is done.
| author | Martijn van Duren <martijn@openbsd.org> |
|---|---|
| date | Thu, 06 Feb 2025 15:04:38 +0000 |
| parent | 9846:9a0da809ed4a |
line wrap: on
line source
-- Prosody IM -- Copyright (C) 2014,2016 Daurnimator -- -- This project is MIT/X11 licensed. Please see the -- COPYING file in the source package for more information. --luacheck: ignore --[[ This file is a template for writing a net.server compatible backend. ]] --[[ Read patterns (also called modes) can be one of: - "*a": Read as much as possible - "*l": Read until end of line ]] --- Handle API local handle_mt = {}; local handle_methods = {}; handle_mt.__index = handle_methods; function handle_methods:set_mode(new_pattern) end function handle_methods:setlistener(listeners) end function handle_methods:setoption(option, value) end function handle_methods:ip() end function handle_methods:starttls(sslctx) end function handle_methods:write(data) end function handle_methods:close() end function handle_methods:pause() end function handle_methods:resume() end --[[ Returns - socket: the socket object underlying this handle ]] function handle_methods:socket() end --[[ Returns - boolean: if an ssl context has been set on this handle ]] function handle_methods:ssl() end --- Listeners API local listeners = {} --[[ connect Called when a client socket has established a connection with it's peer ]] function listeners.onconnect(handle) end --[[ incoming Called when data is received If reading data failed this will be called with `nil, "error message"` ]] function listeners.onincoming(handle, buff, err) end --[[ status Known statuses: - "ssl-handshake-complete" ]] function listeners.onstatus(handle, status) end --[[ disconnect Called when the peer has closed the connection ]] function listeners.ondisconnect(handle) end --[[ drain Called when the handle's write buffer is empty ]] function listeners.ondrain(handle) end --[[ readtimeout Called when a socket inactivity timeout occurs ]] function listeners.onreadtimeout(handle) end --[[ detach: Called when other listeners are going to be removed Allows for clean-up ]] function listeners.ondetach(handle) end --- Top level functions --[[ Returns the syscall level event mechanism in use. Returns: - backend: e.g. "select", "epoll" ]] local function get_backend() end --[[ Starts the event loop. Returns: - "quitting" ]] local function loop() end --[[ Stop a running loop() ]] local function setquitting(quit) end --[[ Links to two handles together, so anything written to one is piped to the other Arguments: - sender, receiver: handles to link - buffersize: maximum #bytes until sender will be locked ]] local function link(sender, receiver, buffersize) end --[[ Binds and listens on the given address and port If `sslctx` is given, the connecting clients will have to negotiate an SSL session Arguments: - address: address to bind to, may be "*" to bind all addresses. will be resolved if it is a string. - port: port to bind (as number) - listeners: a table of listeners - pattern: the read pattern - sslctx: is a valid luasec constructor Returns: - handle - nil, "an error message": on failure (e.g. out of file descriptors) ]] local function addserver(address, port, listeners, pattern, sslctx) end --[[ Binds and listens on the given address and port Mostly the same as addserver but with all optional arguments in a table Arguments: - address: address to bind to, may be "*" to bind all addresses. will be resolved if it is a string. - port: port to bind (as number) - listeners: a table of listeners - config: table of extra settings - read_size: the amount of bytes to read or a read pattern - tls_ctx: is a valid luasec constructor - tls_direct: boolean true for direct TLS, false (or nil) for starttls Returns: - handle - nil, "an error message": on failure (e.g. out of file descriptors) ]] local function listen(address, port, listeners, config) end --[[ Wraps a lua-socket socket client socket in a handle. The socket must be already connected to the remote end. If `sslctx` is given, a SSL session will be negotiated before listeners are called. Arguments: - socket: the lua-socket object to wrap - ip: returned by `handle:ip()` - port: - listeners: a table of listeners - pattern: the read pattern - sslctx: is a valid luasec constructor - typ: the socket type, one of: - "tcp" - "tcp6" - "udp" Returns: - handle, socket - nil, "an error message": on failure (e.g. ) ]] local function wrapclient(socket, ip, serverport, listeners, pattern, sslctx) end --[[ Connects to the given address and port If `sslctx` is given, a SSL session will be negotiated before listeners are called. Arguments: - address: address to connect to. will be resolved if it is a string. - port: port to connect to (as number) - listeners: a table of listeners - pattern: the read pattern - sslctx: is a valid luasec constructor - typ: the socket type, one of: - "tcp" - "tcp6" - "udp" Returns: - handle - nil, "an error message": on failure (e.g. out of file descriptors) ]] local function addclient(address, port, listeners, pattern, sslctx, typ) end --[[ Close all handles ]] local function closeall() end --[[ The callback should be called after `delay` seconds. The callback should be called with the time at the point of firing. If the callback returns a number, it should be called again after that many seconds. Arguments: - delay: number of seconds to wait - callback: function to call. ]] local function add_task(delay, callback) end --[[ Adds a handler for when a signal is fired. Optional to implement callback does not take any arguments Arguments: - signal_id: the signal id (as number) to listen for - handler: callback ]] local function hook_signal(signal_id, handler) end --[[ Adds a low-level FD watcher Arguments: - fd_number: A non-negative integer representing a file descriptor or object with a :getfd() method returning one - on_readable: Optional callback for when the FD is readable - on_writable: Optional callback for when the FD is writable Returns: - net.server handle ]] local function watchfd(fd_number, on_readable, on_writable) end return { get_backend = get_backend; loop = loop; setquitting = setquitting; link = link; addserver = addserver; wrapclient = wrapclient; addclient = addclient; closeall = closeall; hook_signal = hook_signal; watchfd = watchfd; listen = listen; }