Fix crashes in FCGI_PARAMS parsing

This commit is contained in:
flamingcow
2019-05-04 19:42:37 -07:00
parent bea5b76b7c
commit 2faf75e8e4
55 changed files with 24 additions and 4 deletions

1
.gitignore vendored
View File

@@ -3,3 +3,4 @@ example_clock
example_simple
fastcgi_conn_afl
*.o
afl_state/findings

View File

@@ -1 +0,0 @@
<EFBFBD>

View File

@@ -9,7 +9,9 @@ size_t ConstBuffer::ReadMaxLen() const {
}
const char *ConstBuffer::Read(size_t len) {
CHECK_LE(len, ReadMaxLen());
if (ReadMaxLen() < len) {
return nullptr;
}
const auto *ret = &const_buf_[start_];
start_ += len;
return ret;

View File

@@ -82,8 +82,25 @@ int FastCGIConn::Read() {
ConstBuffer param_buf(buf_.Read(header->ContentLength()), header->ContentLength());
while (param_buf.ReadMaxLen() > 0) {
const auto *param_header = param_buf.ReadObj<FastCGIParamHeader>();
std::string_view key(param_buf.Read(param_header->key_length), param_header->key_length);
std::string_view value(param_buf.Read(param_header->value_length), param_header->value_length);
if (!param_header) {
LOG(ERROR) << "FCGI_PARAMS missing header";
return sock_;
}
const auto *key_buf = param_buf.Read(param_header->key_length);
if (!key_buf) {
LOG(ERROR) << "FCGI_PARAMS missing key";
return sock_;
}
std::string_view key(key_buf, param_header->key_length);
const auto *value_buf = param_buf.Read(param_header->value_length);
if (!value_buf) {
LOG(ERROR) << "FCGI_PARAMS missing value";
return sock_;
}
std::string_view value(value_buf, param_header->value_length);
if (headers_.find(key) != headers_.end()) {
request_->AddParam(key, value);
}