Software /
code /
prosody
File
plugins/mod_debug_stanzas/watcher.lib.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 | 12977:74b9e05af71e |
line wrap: on
line source
local filters = require "prosody.util.filters"; local jid = require "prosody.util.jid"; local set = require "prosody.util.set"; local client_watchers = {}; -- active_filters[session] = { -- filter_func = filter_func; -- downstream = { cb1, cb2, ... }; -- } local active_filters = {}; local function subscribe_session_stanzas(session, handler, reason) if active_filters[session] then table.insert(active_filters[session].downstream, handler); if reason then handler(reason, nil, session); end return; end local downstream = { handler }; active_filters[session] = { filter_in = function (stanza) module:log("debug", "NOTIFY WATCHER %d", #downstream); for i = 1, #downstream do downstream[i]("received", stanza, session); end return stanza; end; filter_out = function (stanza) module:log("debug", "NOTIFY WATCHER %d", #downstream); for i = 1, #downstream do downstream[i]("sent", stanza, session); end return stanza; end; downstream = downstream; }; filters.add_filter(session, "stanzas/in", active_filters[session].filter_in); filters.add_filter(session, "stanzas/out", active_filters[session].filter_out); if reason then handler(reason, nil, session); end end local function unsubscribe_session_stanzas(session, handler, reason) local active_filter = active_filters[session]; if not active_filter then return; end for i = #active_filter.downstream, 1, -1 do if active_filter.downstream[i] == handler then table.remove(active_filter.downstream, i); if reason then handler(reason, nil, session); end end end if #active_filter.downstream == 0 then filters.remove_filter(session, "stanzas/in", active_filter.filter_in); filters.remove_filter(session, "stanzas/out", active_filter.filter_out); end active_filters[session] = nil; end local function unsubscribe_all_from_session(session, reason) local active_filter = active_filters[session]; if not active_filter then return; end for i = #active_filter.downstream, 1, -1 do local handler = table.remove(active_filter.downstream, i); if reason then handler(reason, nil, session); end end filters.remove_filter(session, "stanzas/in", active_filter.filter_in); filters.remove_filter(session, "stanzas/out", active_filter.filter_out); active_filters[session] = nil; end local function unsubscribe_handler_from_all(handler, reason) for session in pairs(active_filters) do unsubscribe_session_stanzas(session, handler, reason); end end local s2s_watchers = {}; module:hook("s2sin-established", function (event) for _, watcher in ipairs(s2s_watchers) do if watcher.target_spec == event.session.from_host then subscribe_session_stanzas(event.session, watcher.handler, "opened"); end end end); module:hook("s2sout-established", function (event) for _, watcher in ipairs(s2s_watchers) do if watcher.target_spec == event.session.to_host then subscribe_session_stanzas(event.session, watcher.handler, "opened"); end end end); module:hook("s2s-closed", function (event) unsubscribe_all_from_session(event.session, "closed"); end); local watched_hosts = set.new(); local handler_map = setmetatable({}, { __mode = "kv" }); local function add_stanza_watcher(spec, orig_handler) local function filtering_handler(event_type, stanza, session) if stanza and spec.filter_spec then if spec.filter_spec.with_jid then if event_type == "sent" and (not stanza.attr.from or not jid.compare(stanza.attr.from, spec.filter_spec.with_jid)) then return; elseif event_type == "received" and (not stanza.attr.to or not jid.compare(stanza.attr.to, spec.filter_spec.with_jid)) then return; end end end return orig_handler(event_type, stanza, session); end handler_map[orig_handler] = filtering_handler; if spec.target_spec.jid then local target_is_remote_host = not jid.node(spec.target_spec.jid) and not prosody.hosts[spec.target_spec.jid]; if target_is_remote_host then -- Watch s2s sessions table.insert(s2s_watchers, { target_spec = spec.target_spec.jid; handler = filtering_handler; orig_handler = orig_handler; }); -- Scan existing s2sin for matches for session in pairs(prosody.incoming_s2s) do if spec.target_spec.jid == session.from_host then subscribe_session_stanzas(session, filtering_handler, "attached"); end end -- Scan existing s2sout for matches for local_host, local_session in pairs(prosody.hosts) do --luacheck: ignore 213/local_host for remote_host, remote_session in pairs(local_session.s2sout) do if spec.target_spec.jid == remote_host then subscribe_session_stanzas(remote_session, filtering_handler, "attached"); end end end else table.insert(client_watchers, { target_spec = spec.target_spec.jid; handler = filtering_handler; orig_handler = orig_handler; }); local host = jid.host(spec.target_spec.jid); if not watched_hosts:contains(host) and prosody.hosts[host] then module:context(host):hook("resource-bind", function (event) for _, watcher in ipairs(client_watchers) do module:log("debug", "NEW CLIENT: %s vs %s", event.session.full_jid, watcher.target_spec); if jid.compare(event.session.full_jid, watcher.target_spec) then module:log("debug", "MATCH"); subscribe_session_stanzas(event.session, watcher.handler, "opened"); else module:log("debug", "NO MATCH"); end end end); module:context(host):hook("resource-unbind", function (event) unsubscribe_all_from_session(event.session, "closed"); end); watched_hosts:add(host); end for full_jid, session in pairs(prosody.full_sessions) do if jid.compare(full_jid, spec.target_spec.jid) then subscribe_session_stanzas(session, filtering_handler, "attached"); end end end else error("No recognized target selector"); end end local function remove_stanza_watcher(orig_handler) local handler = handler_map[orig_handler]; unsubscribe_handler_from_all(handler, "detached"); handler_map[orig_handler] = nil; for i = #client_watchers, 1, -1 do if client_watchers[i].orig_handler == orig_handler then table.remove(client_watchers, i); end end for i = #s2s_watchers, 1, -1 do if s2s_watchers[i].orig_handler == orig_handler then table.remove(s2s_watchers, i); end end end local function cleanup(reason) client_watchers = {}; s2s_watchers = {}; for session in pairs(active_filters) do unsubscribe_all_from_session(session, reason or "cancelled"); end end return { add = add_stanza_watcher; remove = remove_stanza_watcher; cleanup = cleanup; };