Skip to content

Commit d39ac64

Browse files
wwbmmmbrpc-oncall
andauthored
Validate C1 before claiming a connection as rtmp (apache#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>
1 parent 3a285cd commit d39ac64

2 files changed

Lines changed: 193 additions & 7 deletions

File tree

‎src/brpc/policy/rtmp_protocol.cpp‎

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3453,14 +3453,51 @@ bool RtmpChunkStream::OnPause(const RtmpMessageHeader& mh,
34533453

34543454
// ============== protocol handlers =============
34553455

3456+
// Size of C0 plus the magic number of simplified rtmp.
3457+
static const size_t RTMP_SNIFF_SIZE0 = RTMP_HANDSHAKE_SIZE0 + MAGIC_NUMBER_SIZE;
3458+
// Size of C0 plus the time and version fields of C1.
3459+
static const size_t RTMP_SNIFF_SIZE1 = RTMP_HANDSHAKE_SIZE0 + 8;
3460+
34563461
inline ParseResult IsPossiblyRtmp(const butil::IOBuf* source) {
3457-
const char* p = (const char*)source->fetch1();
3458-
if (p == nullptr) {
3462+
// C0 alone is too weak a signature: any payload starting with 0x03(e.g. a
3463+
// thrift request) would be captured by the RTMP handshake and the
3464+
// connection is broken. So validate C1 as well before claiming the
3465+
// connection.
3466+
const size_t len = source->length();
3467+
if (len < RTMP_HANDSHAKE_SIZE0) {
34593468
return MakeParseError(PARSE_ERROR_NOT_ENOUGH_DATA);
34603469
}
3461-
if (*p != RTMP_DEFAULT_VERSION) {
3470+
char buf[RTMP_SNIFF_SIZE1];
3471+
// `fetch' may return the internal buffer instead of copying to `buf'.
3472+
const char* const p = (const char*)source->fetch(
3473+
buf, std::min<size_t>(len, sizeof(buf)));
3474+
if ((uint8_t)p[0] != RTMP_DEFAULT_VERSION) {
34623475
return MakeParseError(PARSE_ERROR_TRY_OTHERS);
34633476
}
3477+
if (len < RTMP_SNIFF_SIZE0) {
3478+
return MakeParseError(PARSE_ERROR_NOT_ENOUGH_DATA);
3479+
}
3480+
if (memcmp(p + RTMP_HANDSHAKE_SIZE0, SIMPLIFIED_RTMP_MAGIC_NUMBER,
3481+
MAGIC_NUMBER_SIZE) == 0) {
3482+
return MakeMessage(nullptr);
3483+
}
3484+
// Reject the connection as early as possible: the version field of C1 is
3485+
// enough to tell whether it can be a handshake supported by brpc. The
3486+
// simple one requires zero, the complex one is verified by
3487+
// adobe_hs::C1::Load and uses FP_VERSION or FMS_VERSION.
3488+
if (len < RTMP_SNIFF_SIZE1) {
3489+
return MakeParseError(PARSE_ERROR_NOT_ENOUGH_DATA);
3490+
}
3491+
const uint32_t version = ReadBigEndian4Bytes(p + RTMP_HANDSHAKE_SIZE0 + 4);
3492+
if (version != 0 &&
3493+
version != adobe_hs::FP_VERSION &&
3494+
version != adobe_hs::FMS_VERSION) {
3495+
return MakeParseError(PARSE_ERROR_TRY_OTHERS);
3496+
}
3497+
// Both supported handshakes need the whole C0+C1.
3498+
if (len < RTMP_HANDSHAKE_SIZE0 + RTMP_HANDSHAKE_SIZE1) {
3499+
return MakeParseError(PARSE_ERROR_NOT_ENOUGH_DATA);
3500+
}
34643501
return MakeMessage(nullptr);
34653502
}
34663503

@@ -3475,9 +3512,8 @@ ParseResult ParseRtmpMessage(butil::IOBuf* source, Socket *socket, bool read_eof
34753512
const Server* server = static_cast<const Server*>(arg);
34763513
RtmpService* service = server->options().rtmp_service;
34773514
if (service == nullptr) {
3478-
// Validating RTMP protocol only checks the first byte, which
3479-
// is very easy to be confused with other protocols. Currently
3480-
// if rtmp_service is not set, the protocol is skipped w/o any
3515+
// RTMP shares the port with other protocols. Currently if
3516+
// rtmp_service is not set, the protocol is skipped w/o any
34813517
// warning. We'll register the protocol on demand in later CI
34823518
// to avoid the confusion from root.
34833519
return MakeParseError(PARSE_ERROR_TRY_OTHERS);

‎test/brpc_rtmp_unittest.cpp‎

Lines changed: 151 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1370,8 +1370,10 @@ TEST(RtmpTest, reject_unauthenticated_connection) {
13701370
// RTMP messages are handled inside the parser and never reach
13711371
// InputMessenger's authentication hook. With ServerOptions.auth set the
13721372
// connection must be rejected before any handshaking happens.
1373+
std::string c0c1(1 + 1536, '\0'); // simple handshaking
1374+
c0c1[0] = '\x03'; // C0 (version)
13731375
butil::IOBuf buf;
1374-
buf.append("\x03", 1); // C0 (version)
1376+
buf.append(c0c1.data(), c0c1.size());
13751377
brpc::ParseResult pr = brpc::policy::ParseRtmpMessage(
13761378
&buf, socket.get(), false, &server);
13771379
ASSERT_EQ(brpc::PARSE_ERROR_ABSOLUTELY_WRONG, pr.error());
@@ -1381,6 +1383,154 @@ TEST(RtmpTest, reject_unauthenticated_connection) {
13811383
server.Join();
13821384
}
13831385

1386+
// Create a socket for ParseRtmpMessage to respond the handshaking on. The
1387+
// returned fd is the peer end which receives the responses.
1388+
static brpc::SocketId CreatePairedSocket(int* peer_fd) {
1389+
int fds[2];
1390+
EXPECT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
1391+
*peer_fd = fds[0];
1392+
brpc::SocketId socket_id;
1393+
brpc::SocketOptions socket_options;
1394+
socket_options.fd = fds[1];
1395+
EXPECT_EQ(0, brpc::Socket::Create(socket_options, &socket_id));
1396+
return socket_id;
1397+
}
1398+
1399+
TEST(RtmpTest, payload_starting_with_c0_version_tries_others) {
1400+
PlayingDummyService rtmp_service;
1401+
brpc::Server server;
1402+
brpc::ServerOptions server_opt;
1403+
server_opt.rtmp_service = &rtmp_service;
1404+
ASSERT_EQ(0, server.Start(0, &server_opt));
1405+
1406+
int peer_fd = -1;
1407+
const brpc::SocketId socket_id = CreatePairedSocket(&peer_fd);
1408+
butil::fd_guard peer_guard(peer_fd);
1409+
brpc::SocketUniquePtr socket;
1410+
ASSERT_EQ(0, brpc::Socket::Address(socket_id, &socket));
1411+
1412+
// A frame of another protocol sharing the port, which starts with the rtmp
1413+
// version by accident, must not be captured by rtmp.
1414+
butil::IOBuf buf;
1415+
std::string payload(1 + 1536, '\x03');
1416+
buf.append(payload.data(), payload.size());
1417+
brpc::ParseResult pr = brpc::policy::ParseRtmpMessage(
1418+
&buf, socket.get(), false, &server);
1419+
ASSERT_EQ(brpc::PARSE_ERROR_TRY_OTHERS, pr.error());
1420+
ASSERT_EQ(nullptr, socket->parsing_context());
1421+
1422+
// A single 0x03 byte is not enough to tell whether it is rtmp.
1423+
butil::IOBuf shorter;
1424+
shorter.append("\x03", 1);
1425+
pr = brpc::policy::ParseRtmpMessage(&shorter, socket.get(), false, &server);
1426+
ASSERT_EQ(brpc::PARSE_ERROR_NOT_ENOUGH_DATA, pr.error());
1427+
ASSERT_EQ(nullptr, socket->parsing_context());
1428+
1429+
// The version field of C1 is checked as soon as it is available, so a
1430+
// non-rtmp payload shorter than C0+C1 falls through without waiting for
1431+
// all of them to arrive.
1432+
butil::IOBuf partial;
1433+
std::string partial_payload(100, '\x03');
1434+
partial.append(partial_payload.data(), partial_payload.size());
1435+
pr = brpc::policy::ParseRtmpMessage(&partial, socket.get(), false, &server);
1436+
ASSERT_EQ(brpc::PARSE_ERROR_TRY_OTHERS, pr.error());
1437+
ASSERT_EQ(nullptr, socket->parsing_context());
1438+
1439+
ASSERT_EQ(0, socket->SetFailed());
1440+
socket.reset();
1441+
server.Stop(0);
1442+
server.Join();
1443+
}
1444+
1445+
TEST(RtmpTest, simple_handshake_accepted_after_c0c1_complete) {
1446+
PlayingDummyService rtmp_service;
1447+
brpc::Server server;
1448+
brpc::ServerOptions server_opt;
1449+
server_opt.rtmp_service = &rtmp_service;
1450+
ASSERT_EQ(0, server.Start(0, &server_opt));
1451+
1452+
int peer_fd = -1;
1453+
const brpc::SocketId socket_id = CreatePairedSocket(&peer_fd);
1454+
butil::fd_guard peer_guard(peer_fd);
1455+
timeval tv = { 2, 0 };
1456+
ASSERT_EQ(0, setsockopt(peer_guard, SOL_SOCKET, SO_RCVTIMEO,
1457+
&tv, sizeof(tv)));
1458+
brpc::SocketUniquePtr socket;
1459+
ASSERT_EQ(0, brpc::Socket::Address(socket_id, &socket));
1460+
1461+
// Simple handshaking: C0(version) + C1(time, zero-version, random)
1462+
std::string c0c1(1 + 1536, '\0');
1463+
c0c1[0] = '\x03';
1464+
1465+
// A fragmented C0+C1 must not claim the connection.
1466+
butil::IOBuf buf;
1467+
buf.append(c0c1.data(), 100);
1468+
brpc::ParseResult pr = brpc::policy::ParseRtmpMessage(
1469+
&buf, socket.get(), false, &server);
1470+
ASSERT_EQ(brpc::PARSE_ERROR_NOT_ENOUGH_DATA, pr.error());
1471+
ASSERT_EQ(nullptr, socket->parsing_context());
1472+
1473+
// The connection is accepted once C0+C1 is completed, and S0+S1+S2 is
1474+
// written back. ParseRtmpMessage still waits for C2 at this moment.
1475+
buf.append(c0c1.data() + 100, c0c1.size() - 100);
1476+
pr = brpc::policy::ParseRtmpMessage(&buf, socket.get(), false, &server);
1477+
ASSERT_EQ(brpc::PARSE_ERROR_NOT_ENOUGH_DATA, pr.error());
1478+
ASSERT_NE(nullptr, socket->parsing_context());
1479+
1480+
char s0s1s2[1 + 1536 + 1536];
1481+
size_t nread = 0;
1482+
while (nread < sizeof(s0s1s2)) {
1483+
const ssize_t nr = HANDLE_EINTR(read(
1484+
peer_guard, s0s1s2 + nread, sizeof(s0s1s2) - nread));
1485+
ASSERT_GT(nr, 0);
1486+
nread += nr;
1487+
}
1488+
ASSERT_EQ(0x03, (uint8_t)s0s1s2[0]);
1489+
1490+
ASSERT_EQ(0, socket->SetFailed());
1491+
socket.reset();
1492+
server.Stop(0);
1493+
server.Join();
1494+
}
1495+
1496+
TEST(RtmpTest, complex_handshake_c1_is_accepted) {
1497+
PlayingDummyService rtmp_service;
1498+
brpc::Server server;
1499+
brpc::ServerOptions server_opt;
1500+
server_opt.rtmp_service = &rtmp_service;
1501+
ASSERT_EQ(0, server.Start(0, &server_opt));
1502+
1503+
int peer_fd = -1;
1504+
const brpc::SocketId socket_id = CreatePairedSocket(&peer_fd);
1505+
butil::fd_guard peer_guard(peer_fd);
1506+
brpc::SocketUniquePtr socket;
1507+
ASSERT_EQ(0, brpc::Socket::Address(socket_id, &socket));
1508+
1509+
// C1 of the complex handshaking is verified by adobe_hs::C1::Load after
1510+
// the connection is claimed, sniffing only checks its version field.
1511+
// The all-zero C1 fails the verification and falls back to the simple
1512+
// handshaking, which is not relevant here.
1513+
std::string c0c1(1 + 1536, '\0');
1514+
c0c1[0] = '\x03'; // C0 (version)
1515+
char* const version = &c0c1[1 + 4]; // C1.version
1516+
version[0] = '\x80';
1517+
version[1] = '\x00';
1518+
version[2] = '\x07';
1519+
version[3] = '\x02'; // adobe_hs::FP_VERSION
1520+
1521+
butil::IOBuf buf;
1522+
buf.append(c0c1.data(), c0c1.size());
1523+
const brpc::ParseResult pr = brpc::policy::ParseRtmpMessage(
1524+
&buf, socket.get(), false, &server);
1525+
ASSERT_NE(nullptr, socket->parsing_context());
1526+
ASSERT_EQ(brpc::PARSE_ERROR_NOT_ENOUGH_DATA, pr.error()); // waiting C2
1527+
1528+
ASSERT_EQ(0, socket->SetFailed());
1529+
socket.reset();
1530+
server.Stop(0);
1531+
server.Join();
1532+
}
1533+
13841534
class RejectAllAuthenticator : public brpc::Authenticator {
13851535
public:
13861536
int GenerateCredential(std::string*) const override { return 0; }

0 commit comments

Comments
 (0)