Skip to content

Commit e9ada57

Browse files
Fix fuzz harness blockers (#3257)
* Fix fuzz harness blockers Harden several code paths with increased error handling. The existing fuzzing harneses are running into various blockers stopping them from explore further code. This is an effort to harden the code so the fuzzers will run better without crashing. Signed-off-by: David Korczynski <david@adalogics.com> * Add fatal logging Signed-off-by: David Korczynski <david@adalogics.com> --------- Signed-off-by: David Korczynski <david@adalogics.com>
1 parent f614c81 commit e9ada57

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

src/brpc/policy/mongo_protocol.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ void SendMongoResponse::Run() {
113113
ParseResult ParseMongoMessage(butil::IOBuf* source,
114114
Socket* socket, bool /*read_eof*/, const void *arg) {
115115
const Server* server = static_cast<const Server*>(arg);
116+
// arg may be NULL when the parser is invoked outside of a full Server
117+
// context (e.g. during protocol probing or fuzz testing). Without this
118+
// guard, server->options() dereferences a null pointer and crashes.
119+
if (NULL == server) {
120+
LOG(FATAL) << "Failed creating server";
121+
return MakeParseError(PARSE_ERROR_TRY_OTHERS);
122+
}
116123
const MongoServiceAdaptor* adaptor = server->options().mongo_service_adaptor;
117124
if (NULL == adaptor) {
118125
// The server does not enable mongo adaptor.

src/brpc/policy/streaming_rpc_protocol.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,16 @@ ParseResult ParseStreamingMessage(butil::IOBuf* source,
116116
break;
117117
}
118118
meta_buf.clear(); // to reduce memory resident
119-
((Stream*)ptr->conn())->OnReceived(fm, &payload, socket);
119+
// ptr->conn() returns the connection-level context attached to the
120+
// socket. It may be NULL when the socket was found by ID but has no
121+
// Stream object associated (e.g. during protocol probing or fuzz
122+
// testing). Calling OnReceived on a null pointer would crash.
123+
Stream* stream_conn = (Stream*)ptr->conn();
124+
if (stream_conn == NULL) {
125+
LOG(FATAL) << "No stream object found";
126+
break;
127+
}
128+
stream_conn->OnReceived(fm, &payload, socket);
120129
} while (0);
121130

122131
// Hack input messenger

src/brpc/redis_command.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,12 @@ RedisCommandConsumeState RedisCommandParser::ConsumeImpl(butil::IOBuf& buf,
410410
}
411411
const size_t buf_size = buf.size();
412412
const auto copy_str = static_cast<char *>(arena->allocate(buf_size + 1));
413+
// arena->allocate() may return NULL on allocation failure
414+
if (copy_str == NULL) {
415+
LOG(FATAL) << "Arena failed allocation";
416+
*err = PARSE_ERROR_ABSOLUTELY_WRONG;
417+
return CONSUME_STATE_ERROR;
418+
}
413419
buf.copy_to(copy_str, buf_size);
414420
if (*copy_str == ' ') {
415421
*err = PARSE_ERROR_ABSOLUTELY_WRONG;
@@ -520,6 +526,12 @@ RedisCommandConsumeState RedisCommandParser::ConsumeImpl(butil::IOBuf& buf,
520526
}
521527
buf.pop_front(crlf_pos + 2/*CRLF*/);
522528
char* d = (char*)arena->allocate((len/8 + 1) * 8);
529+
// Guard against allocation failure
530+
if (d == NULL) {
531+
LOG(FATAL) << "Arena failed allocation";
532+
*err = PARSE_ERROR_ABSOLUTELY_WRONG;
533+
return CONSUME_STATE_ERROR;
534+
}
523535
buf.cutn(d, len);
524536
d[len] = '\0';
525537
_args[_index].set(d, len);

0 commit comments

Comments
 (0)