This is an automated email from the ASF dual-hosted git repository.
chenBright pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git
The following commit(s) were added to refs/heads/master by this push:
new d39ac64c6 Validate C1 before claiming a connection as rtmp (#3766)
d39ac64c6 is described below
commit d39ac64c664b5b71e612362f467085349ff8a77e
Author: Weibing Wang <[email protected]>
AuthorDate: Tue Nov 24 14:03:34 2026 +0800
Validate C1 before claiming a connection as rtmp (#3766)
* Validate C1 before claiming a connection as rtmp
* Decide rtmp match from C1 version before C0+C1 is complete
---------
Co-authored-by: brpc-oncall <brpc-oncall@localhost>
---
src/brpc/policy/rtmp_protocol.cpp | 48 ++++++++++--
test/brpc_rtmp_unittest.cpp | 152 +++++++++++++++++++++++++++++++++++++-
2 files changed, 193 insertions(+), 7 deletions(-)
diff --git a/src/brpc/policy/rtmp_protocol.cpp
b/src/brpc/policy/rtmp_protocol.cpp
index bf46922f7..ecb1a4557 100644
--- a/src/brpc/policy/rtmp_protocol.cpp
+++ b/src/brpc/policy/rtmp_protocol.cpp
@@ -3453,14 +3453,51 @@ bool RtmpChunkStream::OnPause(const RtmpMessageHeader&
mh,
// ============== protocol handlers =============
+// Size of C0 plus the magic number of simplified rtmp.
+static const size_t RTMP_SNIFF_SIZE0 = RTMP_HANDSHAKE_SIZE0 +
MAGIC_NUMBER_SIZE;
+// Size of C0 plus the time and version fields of C1.
+static const size_t RTMP_SNIFF_SIZE1 = RTMP_HANDSHAKE_SIZE0 + 8;
+
inline ParseResult IsPossiblyRtmp(const butil::IOBuf* source) {
- const char* p = (const char*)source->fetch1();
- if (p == nullptr) {
+ // C0 alone is too weak a signature: any payload starting with 0x03(e.g. a
+ // thrift request) would be captured by the RTMP handshake and the
+ // connection is broken. So validate C1 as well before claiming the
+ // connection.
+ const size_t len = source->length();
+ if (len < RTMP_HANDSHAKE_SIZE0) {
return MakeParseError(PARSE_ERROR_NOT_ENOUGH_DATA);
}
- if (*p != RTMP_DEFAULT_VERSION) {
+ char buf[RTMP_SNIFF_SIZE1];
+ // `fetch' may return the internal buffer instead of copying to `buf'.
+ const char* const p = (const char*)source->fetch(
+ buf, std::min<size_t>(len, sizeof(buf)));
+ if ((uint8_t)p[0] != RTMP_DEFAULT_VERSION) {
return MakeParseError(PARSE_ERROR_TRY_OTHERS);
}
+ if (len < RTMP_SNIFF_SIZE0) {
+ return MakeParseError(PARSE_ERROR_NOT_ENOUGH_DATA);
+ }
+ if (memcmp(p + RTMP_HANDSHAKE_SIZE0, SIMPLIFIED_RTMP_MAGIC_NUMBER,
+ MAGIC_NUMBER_SIZE) == 0) {
+ return MakeMessage(nullptr);
+ }
+ // Reject the connection as early as possible: the version field of C1 is
+ // enough to tell whether it can be a handshake supported by brpc. The
+ // simple one requires zero, the complex one is verified by
+ // adobe_hs::C1::Load and uses FP_VERSION or FMS_VERSION.
+ if (len < RTMP_SNIFF_SIZE1) {
+ return MakeParseError(PARSE_ERROR_NOT_ENOUGH_DATA);
+ }
+ const uint32_t version = ReadBigEndian4Bytes(p + RTMP_HANDSHAKE_SIZE0 + 4);
+ if (version != 0 &&
+ version != adobe_hs::FP_VERSION &&
+ version != adobe_hs::FMS_VERSION) {
+ return MakeParseError(PARSE_ERROR_TRY_OTHERS);
+ }
+ // Both supported handshakes need the whole C0+C1.
+ if (len < RTMP_HANDSHAKE_SIZE0 + RTMP_HANDSHAKE_SIZE1) {
+ return MakeParseError(PARSE_ERROR_NOT_ENOUGH_DATA);
+ }
return MakeMessage(nullptr);
}
@@ -3475,9 +3512,8 @@ ParseResult ParseRtmpMessage(butil::IOBuf* source, Socket
*socket, bool read_eof
const Server* server = static_cast<const Server*>(arg);
RtmpService* service = server->options().rtmp_service;
if (service == nullptr) {
- // Validating RTMP protocol only checks the first byte, which
- // is very easy to be confused with other protocols. Currently
- // if rtmp_service is not set, the protocol is skipped w/o any
+ // RTMP shares the port with other protocols. Currently if
+ // rtmp_service is not set, the protocol is skipped w/o any
// warning. We'll register the protocol on demand in later CI
// to avoid the confusion from root.
return MakeParseError(PARSE_ERROR_TRY_OTHERS);
diff --git a/test/brpc_rtmp_unittest.cpp b/test/brpc_rtmp_unittest.cpp
index b0778ab41..f275a79fa 100644
--- a/test/brpc_rtmp_unittest.cpp
+++ b/test/brpc_rtmp_unittest.cpp
@@ -1370,8 +1370,10 @@ TEST(RtmpTest, reject_unauthenticated_connection) {
// RTMP messages are handled inside the parser and never reach
// InputMessenger's authentication hook. With ServerOptions.auth set the
// connection must be rejected before any handshaking happens.
+ std::string c0c1(1 + 1536, '\0'); // simple handshaking
+ c0c1[0] = '\x03'; // C0 (version)
butil::IOBuf buf;
- buf.append("\x03", 1); // C0 (version)
+ buf.append(c0c1.data(), c0c1.size());
brpc::ParseResult pr = brpc::policy::ParseRtmpMessage(
&buf, socket.get(), false, &server);
ASSERT_EQ(brpc::PARSE_ERROR_ABSOLUTELY_WRONG, pr.error());
@@ -1381,6 +1383,154 @@ TEST(RtmpTest, reject_unauthenticated_connection) {
server.Join();
}
+// Create a socket for ParseRtmpMessage to respond the handshaking on. The
+// returned fd is the peer end which receives the responses.
+static brpc::SocketId CreatePairedSocket(int* peer_fd) {
+ int fds[2];
+ EXPECT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
+ *peer_fd = fds[0];
+ brpc::SocketId socket_id;
+ brpc::SocketOptions socket_options;
+ socket_options.fd = fds[1];
+ EXPECT_EQ(0, brpc::Socket::Create(socket_options, &socket_id));
+ return socket_id;
+}
+
+TEST(RtmpTest, payload_starting_with_c0_version_tries_others) {
+ PlayingDummyService rtmp_service;
+ brpc::Server server;
+ brpc::ServerOptions server_opt;
+ server_opt.rtmp_service = &rtmp_service;
+ ASSERT_EQ(0, server.Start(0, &server_opt));
+
+ int peer_fd = -1;
+ const brpc::SocketId socket_id = CreatePairedSocket(&peer_fd);
+ butil::fd_guard peer_guard(peer_fd);
+ brpc::SocketUniquePtr socket;
+ ASSERT_EQ(0, brpc::Socket::Address(socket_id, &socket));
+
+ // A frame of another protocol sharing the port, which starts with the rtmp
+ // version by accident, must not be captured by rtmp.
+ butil::IOBuf buf;
+ std::string payload(1 + 1536, '\x03');
+ buf.append(payload.data(), payload.size());
+ brpc::ParseResult pr = brpc::policy::ParseRtmpMessage(
+ &buf, socket.get(), false, &server);
+ ASSERT_EQ(brpc::PARSE_ERROR_TRY_OTHERS, pr.error());
+ ASSERT_EQ(nullptr, socket->parsing_context());
+
+ // A single 0x03 byte is not enough to tell whether it is rtmp.
+ butil::IOBuf shorter;
+ shorter.append("\x03", 1);
+ pr = brpc::policy::ParseRtmpMessage(&shorter, socket.get(), false,
&server);
+ ASSERT_EQ(brpc::PARSE_ERROR_NOT_ENOUGH_DATA, pr.error());
+ ASSERT_EQ(nullptr, socket->parsing_context());
+
+ // The version field of C1 is checked as soon as it is available, so a
+ // non-rtmp payload shorter than C0+C1 falls through without waiting for
+ // all of them to arrive.
+ butil::IOBuf partial;
+ std::string partial_payload(100, '\x03');
+ partial.append(partial_payload.data(), partial_payload.size());
+ pr = brpc::policy::ParseRtmpMessage(&partial, socket.get(), false,
&server);
+ ASSERT_EQ(brpc::PARSE_ERROR_TRY_OTHERS, pr.error());
+ ASSERT_EQ(nullptr, socket->parsing_context());
+
+ ASSERT_EQ(0, socket->SetFailed());
+ socket.reset();
+ server.Stop(0);
+ server.Join();
+}
+
+TEST(RtmpTest, simple_handshake_accepted_after_c0c1_complete) {
+ PlayingDummyService rtmp_service;
+ brpc::Server server;
+ brpc::ServerOptions server_opt;
+ server_opt.rtmp_service = &rtmp_service;
+ ASSERT_EQ(0, server.Start(0, &server_opt));
+
+ int peer_fd = -1;
+ const brpc::SocketId socket_id = CreatePairedSocket(&peer_fd);
+ butil::fd_guard peer_guard(peer_fd);
+ timeval tv = { 2, 0 };
+ ASSERT_EQ(0, setsockopt(peer_guard, SOL_SOCKET, SO_RCVTIMEO,
+ &tv, sizeof(tv)));
+ brpc::SocketUniquePtr socket;
+ ASSERT_EQ(0, brpc::Socket::Address(socket_id, &socket));
+
+ // Simple handshaking: C0(version) + C1(time, zero-version, random)
+ std::string c0c1(1 + 1536, '\0');
+ c0c1[0] = '\x03';
+
+ // A fragmented C0+C1 must not claim the connection.
+ butil::IOBuf buf;
+ buf.append(c0c1.data(), 100);
+ brpc::ParseResult pr = brpc::policy::ParseRtmpMessage(
+ &buf, socket.get(), false, &server);
+ ASSERT_EQ(brpc::PARSE_ERROR_NOT_ENOUGH_DATA, pr.error());
+ ASSERT_EQ(nullptr, socket->parsing_context());
+
+ // The connection is accepted once C0+C1 is completed, and S0+S1+S2 is
+ // written back. ParseRtmpMessage still waits for C2 at this moment.
+ buf.append(c0c1.data() + 100, c0c1.size() - 100);
+ pr = brpc::policy::ParseRtmpMessage(&buf, socket.get(), false, &server);
+ ASSERT_EQ(brpc::PARSE_ERROR_NOT_ENOUGH_DATA, pr.error());
+ ASSERT_NE(nullptr, socket->parsing_context());
+
+ char s0s1s2[1 + 1536 + 1536];
+ size_t nread = 0;
+ while (nread < sizeof(s0s1s2)) {
+ const ssize_t nr = HANDLE_EINTR(read(
+ peer_guard, s0s1s2 + nread, sizeof(s0s1s2) - nread));
+ ASSERT_GT(nr, 0);
+ nread += nr;
+ }
+ ASSERT_EQ(0x03, (uint8_t)s0s1s2[0]);
+
+ ASSERT_EQ(0, socket->SetFailed());
+ socket.reset();
+ server.Stop(0);
+ server.Join();
+}
+
+TEST(RtmpTest, complex_handshake_c1_is_accepted) {
+ PlayingDummyService rtmp_service;
+ brpc::Server server;
+ brpc::ServerOptions server_opt;
+ server_opt.rtmp_service = &rtmp_service;
+ ASSERT_EQ(0, server.Start(0, &server_opt));
+
+ int peer_fd = -1;
+ const brpc::SocketId socket_id = CreatePairedSocket(&peer_fd);
+ butil::fd_guard peer_guard(peer_fd);
+ brpc::SocketUniquePtr socket;
+ ASSERT_EQ(0, brpc::Socket::Address(socket_id, &socket));
+
+ // C1 of the complex handshaking is verified by adobe_hs::C1::Load after
+ // the connection is claimed, sniffing only checks its version field.
+ // The all-zero C1 fails the verification and falls back to the simple
+ // handshaking, which is not relevant here.
+ std::string c0c1(1 + 1536, '\0');
+ c0c1[0] = '\x03'; // C0 (version)
+ char* const version = &c0c1[1 + 4]; // C1.version
+ version[0] = '\x80';
+ version[1] = '\x00';
+ version[2] = '\x07';
+ version[3] = '\x02'; // adobe_hs::FP_VERSION
+
+ butil::IOBuf buf;
+ buf.append(c0c1.data(), c0c1.size());
+ const brpc::ParseResult pr = brpc::policy::ParseRtmpMessage(
+ &buf, socket.get(), false, &server);
+ ASSERT_NE(nullptr, socket->parsing_context());
+ ASSERT_EQ(brpc::PARSE_ERROR_NOT_ENOUGH_DATA, pr.error()); // waiting C2
+
+ ASSERT_EQ(0, socket->SetFailed());
+ socket.reset();
+ server.Stop(0);
+ server.Join();
+}
+
class RejectAllAuthenticator : public brpc::Authenticator {
public:
int GenerateCredential(std::string*) const override { return 0; }
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]