[Feat] (MG_Remote, P11 B1): pairing at hand-off - the fd: endpoint presents no PairBind when MOBILEGL_IPC_FD_PAIRED=1 says the broker did, names the server's refusal when its own PairBinds arrive too late, and a paired unix session waits for its Hello until EOF instead of 10 s

This commit is contained in:
2026-09-29 15:00:30 -04:00
parent 9af4c8aefd
commit 578795c84d
4 changed files with 58 additions and 19 deletions
+19 -3
View File
@@ -883,13 +883,29 @@ namespace MobileGL::MG_Remote::Client {
}
m_connectDial = true;
m_dataEndpoint.clear();
// ID-P11-12: the broker that connected the pair presents its PairBinds at hand-off, and the
// helper says so; then this process must not present a second pair (the server would read
// it where the Hello belongs).
const char* pairedText = std::getenv("MOBILEGL_IPC_FD_PAIRED");
const bool pairedAtHandoff = pairedText != nullptr && std::strcmp(pairedText, "1") == 0;
std::unique_ptr<Transport::SocketTransport> socket;
const auto adopted = Transport::SocketTransport::AdoptConnectedPair(control, aux, socket);
const auto adopted = Transport::SocketTransport::AdoptConnectedPair(control, aux, socket, pairedAtHandoff);
if (adopted == MOBILEGL_ERR_TRANSPORT_CLOSED) {
// The PairBinds could not be sent: the server had already closed the pair. Its reason
// is usually still on the control connection - a pair presented past the pairing budget
// gets Refuse{Authentication} - so it is read and named rather than left as a send error.
Transport::SocketTransport closedPair(control, aux, Transport::TransportRole::Client);
std::vector<Uint8> frame;
if (ReceiveEnvelope(closedPair, frame, 500) == MOBILEGL_OK && LogPeerRefusal(ParseEnvelope(frame)))
return MOBILEGL_ERR_PROTOCOL_MISMATCH;
MGLOG_E("MG_Remote: the server closed the fd: pair before it was presented, and left no refusal");
return adopted;
}
if (adopted != MOBILEGL_OK) return adopted;
const auto started = StartOverSocket(std::move(socket));
if (started == MOBILEGL_OK) {
MGLOG_I("MG_Remote client: control=fd data=shm server=fd:%d,%d pid=%u dial=connect", control, aux,
m_peerServerPid);
MGLOG_I("MG_Remote client: control=fd data=shm server=fd:%d,%d pid=%u dial=connect paired=%s", control,
aux, m_peerServerPid, pairedAtHandoff ? "handoff" : "client");
}
return started;
}
+15 -2
View File
@@ -122,6 +122,19 @@ void RefuseBusy(SocketTransport& transport, const char* detail) {
Refuse(transport, Protocol::RefuseCode::Busy, detail);
}
// P11 B1 (CONTRACT-P11 B1): HOW LONG A PAIRED UNIX SESSION WAITS FOR ITS HELLO - FOR EVER; ITS END
// IS THE PEER'S EOF. The pair's identity is settled before this wait (PairAcceptor paired the two
// connections by the nonce each presented), and a unix endpoint is reachable only by this user / this
// app (SELinux on Android, 0600 on a path). Since B1 the server app's broker presents the PairBinds at
// hand-off, the moment it connects, while the program it is for may still be loading - a Termux program
// can spend many seconds on its assets before its first EGL call sends the Hello. The old 10 s (the
// same budget as TCP's pre-auth) refused such a program by name; there is no bound here that is long
// enough for every program, so there is none: a program that exits (or closes the descriptors) ends
// the wait with EOF ("control peer closed before sending a first frame"), and while it lives it holds
// the one session slot, as a rendering session would. TCP keeps its pre-auth deadline (PreAuthKnobs),
// and the in-process display server keeps 10 s (its stop waits for the session thread).
constexpr std::uint32_t kPairedUnixHelloWaitMs = Transport::kWaitForever;
// 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;
@@ -1521,7 +1534,7 @@ extern "C" __attribute__((visibility("default"))) int mobilegl_server_main(int a
}
::close(listener);
if (endpoint[0] != '@') ::unlink(endpoint.c_str());
ExitSessionProcess(RunSession(std::move(control), {}, {}, -1, DataSource::None, 10000));
ExitSessionProcess(RunSession(std::move(control), {}, {}, -1, DataSource::None, kPairedUnixHelloWaitMs));
}
reap();
// exit_group closes files just before waitpid can observe the exit.
@@ -1546,7 +1559,7 @@ extern "C" __attribute__((visibility("default"))) int mobilegl_server_main(int a
// supervisor's refusal of one would not end it while this session lives (TcpSupervisor's
// fork does the same with its pending peers).
if (pairs) pairs->CloseInForkedChild();
ExitSessionProcess(RunSession(std::move(control), {}, {}, -1, DataSource::None, 10000));
ExitSessionProcess(RunSession(std::move(control), {}, {}, -1, DataSource::None, kPairedUnixHelloWaitMs));
}
if (child < 0) {
RefuseBusy(*control, "could not create session child");
@@ -460,7 +460,8 @@ namespace MobileGL::MG_Remote::Transport {
}
MobileGLResult SocketTransport::AdoptConnectedPair(int controlFd, int auxFd,
std::unique_ptr<SocketTransport>& outClient) {
std::unique_ptr<SocketTransport>& outClient,
bool pairedAtHandoff) {
outClient.reset();
// Checked before a byte is written, so a stale or mistyped `fd:` names what it points at
// instead of writing a PairBind into something that is not the server.
@@ -482,9 +483,16 @@ namespace MobileGL::MG_Remote::Transport {
// from here they are this session's, and a program's own children must not inherit them.
::fcntl(controlFd, F_SETFD, FD_CLOEXEC);
::fcntl(auxFd, F_SETFD, FD_CLOEXEC);
// P11 PAIR: whoever opened the two connections, the CLIENT presents the pair's identity -
// one fresh nonce, control then aux - exactly as ConnectTo does, before the Hello. The
// broker that connected them wrote nothing on them.
// P11 PAIR / ID-P11-12: WHOEVER OPENED THE TWO CONNECTIONS PRESENTS THE PAIR'S IDENTITY - one
// fresh nonce, control then aux, before the Hello. Since B1's follow-up the server app's broker
// does it at hand-off (`pairedAtHandoff`: the helper says so with MOBILEGL_IPC_FD_PAIRED=1), so
// the pairing budget no longer waits on this program reaching its first EGL call; a pair
// somebody connected without presenting (a test's bare connects, an older broker) is presented
// here, exactly as ConnectTo does.
if (pairedAtHandoff) {
outClient = std::make_unique<SocketTransport>(controlFd, auxFd, TransportRole::Client);
return MOBILEGL_OK;
}
std::uint8_t nonce[kPairNonceBytes];
MobileGLResult result = MintNonce(nonce, sizeof(nonce));
if (result == MOBILEGL_OK) {
@@ -496,11 +504,10 @@ namespace MobileGL::MG_Remote::Transport {
const auto auxBind = EncodePairBind(nonce, true);
result = SendFirstFrame(auxFd, auxBind.data(), auxBind.size(), "fd: aux connection's PairBind");
}
if (result != MOBILEGL_OK) {
::close(controlFd);
::close(auxFd);
return result;
}
// A failed send leaves both descriptors open for the caller: the usual cause is a server that
// already refused and closed the pair (a PairBind presented past its pairing budget), and the
// refusal it wrote is still readable on the control connection (ClientSession reads it, by name).
if (result != MOBILEGL_OK) return result;
outClient = std::make_unique<SocketTransport>(controlFd, auxFd, TransportRole::Client);
return MOBILEGL_OK;
}
@@ -893,7 +900,7 @@ namespace MobileGL::MG_Remote::Transport {
return MOBILEGL_ERR_UNSUPPORTED;
}
MobileGLResult SocketTransport::AdoptConnectedPair(int, int, std::unique_ptr<SocketTransport>& outClient) {
MobileGLResult SocketTransport::AdoptConnectedPair(int, int, std::unique_ptr<SocketTransport>& outClient, bool) {
outClient.reset();
WireLogError("MG_Remote SocketTransport: the fd: endpoint is POSIX only (CONTRACT-P11 B1)");
return MOBILEGL_ERR_UNSUPPORTED;
@@ -108,11 +108,14 @@ namespace MobileGL::MG_Remote::Transport {
// endpoint. A broker in the server's own app (the only context SELinux lets connect to its
// endpoint) opened both connections and handed the client ends over; this adopts them. Both
// must be open, connected AF_UNIX stream sockets and distinct (refused by name otherwise,
// nothing written). They become CLOEXEC, and the client then presents one PairBind pair on
// them - a fresh nonce, control then aux - exactly as ConnectTo does: the broker writes
// nothing, so the server's PairAcceptor pairs them by the client's own nonce.
// nothing written). They become CLOEXEC. `pairedAtHandoff`: whoever connected them already
// presented the PairBind pair (ID-P11-12: the server app's broker does, at hand-off), and
// nothing is written; otherwise this presents one - a fresh nonce, control then aux - exactly
// as ConnectTo does, before the Hello. TRANSPORT_CLOSED when that send failed: the descriptors
// are then left open, because the server's refusal may be waiting on the control connection.
static MobileGLResult AdoptConnectedPair(int controlFd, int auxFd,
std::unique_ptr<SocketTransport>& outClient);
std::unique_ptr<SocketTransport>& outClient,
bool pairedAtHandoff = false);
// ---- PH-7 (4), ID-P7-3: a TCP data connection is BOUND, not paired --------
//