Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tcp recv buffer overflow #30

Merged
merged 1 commit into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/transport/tcp/tcp_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#define NTOHLL(x) ((((uint64_t)ntohl(x)) << 32) + ntohl(x >> 32))
#endif

#define MAX_RECV_BUF_LENGTH 1024 * 1024 * 4

namespace onnxruntime_server::transport::tcp {
struct protocol_header {
int16_t type;
Expand All @@ -29,7 +31,7 @@ namespace onnxruntime_server::transport::tcp {
private:
asio::socket socket;
tcp_server *server;
char chunk[1024];
char chunk[MAX_RECV_BUF_LENGTH];
std::string buffer;

onnxruntime_server::task::benchmark request_time;
Expand Down
16 changes: 1 addition & 15 deletions src/transport/tcp/tcp_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ void onnxruntime_server::transport::tcp::tcp_session::close() {
server->remove_session(shared_from_this());
}

#define MAX_LENGTH 1024 * 16

void Orts::transport::tcp::tcp_session::do_read() {
if (_remote_endpoint.empty())
_remote_endpoint =
socket.remote_endpoint().address().to_string() + ":" + std::to_string(socket.remote_endpoint().port());

socket.async_read_some(
boost::asio::buffer(chunk, MAX_LENGTH),
boost::asio::buffer(chunk, MAX_RECV_BUF_LENGTH),
[self = shared_from_this()](boost::system::error_code ec, std::size_t length) {
if (!ec) {
self->buffer.append(self->chunk, length);
Expand All @@ -42,18 +40,6 @@ void Orts::transport::tcp::tcp_session::do_read() {
header.length = NTOHLL(header.length);
header.json_length = NTOHLL(header.json_length);
header.post_length = NTOHLL(header.post_length);
// assert(header.length == header.json_length + header.post_length);

/*
// check buffer size
if (header.length > MAX_BUFFER_LIMIT) {
PLOG(L_WARNING) << self->get_remote_endpoint()
<< " transport::session::do_read: Buffer size is too large: " << header.length
<< std::endl;
self->close();
return;
}
*/

// continue to read
if (self->buffer.size() < sizeof(protocol_header) + header.length) {
Expand Down