diff --git a/MobileGL/MG_Remote/Server/ServerMain.cpp b/MobileGL/MG_Remote/Server/ServerMain.cpp index cd99db9c..b778a4e7 100644 --- a/MobileGL/MG_Remote/Server/ServerMain.cpp +++ b/MobileGL/MG_Remote/Server/ServerMain.cpp @@ -70,7 +70,9 @@ void RefuseBusy(SocketTransport& transport, const char* detail) { // The single-session TCP shape's wait for its data connection's DataBind (ListenerSource). constexpr std::uint32_t kFirstFrameWaitMs = 2000; constexpr std::uint64_t kFirstFrameMaxBytes = Server::kPreAuthFirstFrameMaxBytes; -// The detail a data connection the live session's hand-off will not take is refused with. +// The one detail both ends of the hand-off use for a data connection the live session will not +// take: the supervisor when the send fails, and the child for the ones queued before it closed +// its end (CloseHandoffRefusingQueued). constexpr const char* kHandoffRefused = "data connection could not be handed to the live session"; // Whatever the peer has already sent, read and dropped (non-blocking, at most 1 MiB), so a close @@ -176,6 +178,35 @@ void SendLogAck(void* pointer) { (void)ack.transport->SendFrame({builder.GetBufferPointer(), builder.GetSize()}); } +// What `sourceFd` is, so RunSession knows how to let go of it (ID-P7-44). +enum class DataSource { None, Handoff, Listener }; + +// ID-P7-44. THE HAND-OFF CLOSES WITHOUT A WINDOW. +// +// The F fix round closed the child's end of the hand-off the moment Accept returned, so that a +// DataBind arriving after the session bound fails to send and the supervisor refuses it by name. +// Between BindDataConnection taking the matching descriptor and that close, though, the +// supervisor could still queue one more - and a close with datagrams queued drops their +// descriptors in the kernel: the peer saw its connection end with no frame at all. So the end is +// cut for reading FIRST (after shutdown(SHUT_RD) every further sendmsg to it fails with EPIPE, which +// FdPassing reports as "peer gone" and the supervisor answers by name), and then whatever was +// queued before the cut is received here and refused with the supervisor's own words. No +// DataBind can be in neither place. +void CloseHandoffRefusingQueued(int handoff) { + (void)::shutdown(handoff, SHUT_RD); + for (;;) { + int fd = -1; + std::uint8_t sideband[FdPassing::kMaxSidebandBytes] = {}; + std::uint64_t size = 0; + if (FdPassing::ReceiveFd(handoff, &fd, MobileGLMutableByteSpan{sideband, sizeof(sideband)}, &size, 0) != + MOBILEGL_OK || + fd < 0) + break; + RefuseAndClose(fd, Protocol::RefuseCode::Authentication, kHandoffRefused); + } + ::close(handoff); +} + // Called only after fork (or, in the single-session shape, instead of it): the supervisor never // creates a backend or EGL context. `dataSource` is set on TCP (PH-7 (4)) and empty on a unix // endpoint, whose second connection is the SCM_RIGHTS socket AcceptPair already paired. @@ -188,7 +219,7 @@ void SendLogAck(void* pointer) { // `helloWaitMs`. [[noreturn]] void RunSession(std::unique_ptr control, std::vector helloFrame, Server::ServerSession::DataConnectionSource dataSource, int sourceFd, - std::uint32_t helloWaitMs) { + DataSource sourceKind, std::uint32_t helloWaitMs) { const int selfPid = static_cast(::getpid()); const bool tcp = control->IsTcp(); if (helloFrame.empty()) { @@ -297,9 +328,11 @@ void SendLogAck(void* pointer) { // retry is refused at connect (ECONNREFUSED, as before PH-7 (4)) rather than completing a // TCP handshake this process would never accept and waiting out its whole connect budget. // The source object goes with it, so no later call can poll a descriptor number the kernel - // may have reused. + // may have reused. The hand-off is cut before it is closed, and what was already queued on + // it is refused by name rather than dropped (ID-P7-44, CloseHandoffRefusingQueued). if (sourceFd >= 0) { - ::close(sourceFd); + if (sourceKind == DataSource::Handoff) CloseHandoffRefusingQueued(sourceFd); + else ::close(sourceFd); session.SetDataConnectionSource({}); } if (accepted != MOBILEGL_OK) { @@ -769,9 +802,9 @@ private: Refuse(*connection, Protocol::RefuseCode::Authentication, "data connection names no live session"); return; } - // The send is MSG_DONTWAIT and the child closes its end the moment Accept returns - // (RunSession), so this either queues the descriptor for the child or fails and is - // refused here. + // The send is MSG_DONTWAIT and the child cuts its end the moment its own data + // connection is bound (CloseHandoffRefusingQueued), so this either queues the + // descriptor for a child that will read or refuse it, or fails and is refused here. if (m_handoff >= 0 && FdPassing::SendFd(m_handoff, fd, MobileGLByteSpan{nonce, sizeof(nonce)}, /*dontWait=*/true) == MOBILEGL_OK) { // The child holds its own descriptor now; ours goes without a shutdown(2), which @@ -830,7 +863,8 @@ private: for (const auto& other : m_ready) if (other.fd >= 0) ::close(other.fd); const int controlFd = connection->StreamFd(); - RunSession(std::move(connection), hello, HandoffSource(pair[1], controlFd), pair[1], 0); + RunSession(std::move(connection), hello, HandoffSource(pair[1], controlFd), pair[1], DataSource::Handoff, + 0); } if (child < 0) { ::close(pair[0]); @@ -933,11 +967,11 @@ extern "C" __attribute__((visibility("default"))) int mobilegl_server_main(int a // deadline and the token is still asked first. const int controlFd = control->StreamFd(); RunSession(std::move(control), {}, ListenerSource(listener, controlFd), listener, - knobs.deadlineMs); + DataSource::Listener, knobs.deadlineMs); } ::close(listener); if (endpoint[0] != '@') ::unlink(endpoint.c_str()); - RunSession(std::move(control), {}, {}, -1, 10000); + RunSession(std::move(control), {}, {}, -1, DataSource::None, 10000); } reap(); // exit_group closes files just before waitpid can observe the exit. @@ -955,7 +989,7 @@ extern "C" __attribute__((visibility("default"))) int mobilegl_server_main(int a const pid_t child = ::fork(); if (child == 0) { ::close(listener); - RunSession(std::move(control), {}, {}, -1, 10000); + RunSession(std::move(control), {}, {}, -1, DataSource::None, 10000); } if (child < 0) { RefuseBusy(*control, "could not create session child"); diff --git a/scripts/ci/tcp_supervisor_smoke.py b/scripts/ci/tcp_supervisor_smoke.py index 80ed6e5e..56134bfb 100644 --- a/scripts/ci/tcp_supervisor_smoke.py +++ b/scripts/ci/tcp_supervisor_smoke.py @@ -17,8 +17,9 @@ P7 F2, PH-7 (5): the supervisor now reads and authenticates every first frame be the controls here assert that an unauthenticated peer learns nothing of ours (`unauthenticated_wrong_layout`), that a silent one costs no fork and is answered at the pre-auth deadline (`silent_peer`), that Busy is the answer to an AUTHENTICATED second client (`busy`), the -pending cap and the backoff (`pending_cap`, `auth_backoff`). The five malformed-frame shapes -are fuzz arm 1's own entry, scripts/ci/ph_fuzz_control_frames.py. +pending cap and the backoff (`pending_cap`, `auth_backoff`), and ID-P7-44's queued-DataBind +refusal (`data_bind_queued_before_bound`). The five malformed-frame shapes are fuzz arm 1's own +entry, scripts/ci/ph_fuzz_control_frames.py. It needs flatc, which is deliberately absent from the default build graph (gen_protocol.py's header block says why). Resolution order is gen_protocol.py's, minus the build-it-for-you arm: @@ -723,6 +724,65 @@ def main(): 'fresh_control': still_busy} assert process.poll() is None, 'the refused DataBinds killed the supervisor' + # ---- ID-P7-44: A DataBind QUEUED TO THE CHILD BEFORE IT LET GO IS REFUSED, NOT DROPPED + # + # The F fix round closed the child's end of the hand-off the moment Accept returned; + # between the bind and that close the supervisor could still queue a DataBind, and a + # close with descriptors queued drops them in the kernel - that peer's connection just + # ended, with no frame. The window is microseconds in the wild, so it is held open + # here: the session child is SIGSTOPped right after its Welcome, its own data + # connection and three extra DataBinds are then routed to it (the supervisor forwards + # to a stopped child as readily as to a running one), and it is continued. It binds + # its own connection and lets go of the hand-off with the three still queued. Each + # extra must be answered by name - "nonce mismatch" if the child happened to read it + # before its own, the supervisor's hand-off words if it was still queued when the + # child let go - and the session must come up. RED before the fix: the extras' reads + # end with EOF and no frame ("control channel closed before a complete frame"). + held = hello(schema, flatbuffers, fingerprint=fingerprint) + for _ in range(30): + control = connect_control(port) + control.sendall(held) + welcome = receive(control, schema) + if welcome.get('code') != 7: + break + control.close() + time.sleep(.05) + extras = [] + right = None + try: + assert welcome.get('welcome', 0), welcome + child = welcome['welcome'] + os.kill(child, signal.SIGSTOP) + try: + right = open_data(port, schema, flatbuffers, bytes.fromhex(welcome['nonce'])) + time.sleep(.3) # routed first, so it is first in the child's queue + for _ in range(3): + extras.append(open_data(port, schema, flatbuffers, os.urandom(16))) + time.sleep(.5) # every one of them routed into the stopped child's queue + finally: + os.kill(child, signal.SIGCONT) + queued = [] + for extra in extras: + extra.settimeout(10) + try: + queued.append(receive(extra, schema)) + except (RuntimeError, OSError) as error: + queued.append({'error': repr(error)}) + ready = wait_for_log(serverLog, f'pid={child} transport=spawn role=server ready') + finally: + for extra in extras: + extra.close() + if right is not None: + right.close() + control.close() + for answer in queued: + assert answer.get('code') == 4 and answer.get('detail') in ( + 'data connection nonce mismatch', kDetailHandoff), ( + f'a DataBind queued to session pid={child} before it let go of the hand-off was ' + f'answered {answer}, not refused by name (ID-P7-44); all: {queued}') + assert ready is not None, f'session pid={child} did not come up; see {serverLog}' + evidence['data_bind_queued_before_bound'] = {'refused': queued} + assert process.poll() is None, 'the queued DataBinds killed the supervisor' # ---- F fix round: A GARBAGE FIRST FRAME WHILE BUSY IS MalformedHello, NOT Busy ------ #