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

udp: properly handle truncated/dropped datagrams #14122

Merged
merged 13 commits into from
Nov 20, 2020
Prev Previous commit
Next Next commit
comments
Signed-off-by: Matt Klein <mklein@lyft.com>
mattklein123 committed Nov 17, 2020
commit e876a47fe13ebb3e57e3693a0a1ad2f481f3b8ff
6 changes: 6 additions & 0 deletions docs/root/version_history/v1.16.1.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1.16.1 (TBD)
============

Changes
-------
* udp: fixed issue in which receiving truncated UDP datagrams would cause Envoy to crash.
1 change: 1 addition & 0 deletions docs/root/version_history/version_history.rst
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ Version history
:titlesonly:

current
v1.16.1
v1.16.0
v1.15.2
v1.15.1
18 changes: 15 additions & 3 deletions source/common/network/io_socket_handle_impl.cc
Original file line number Diff line number Diff line change
@@ -48,6 +48,16 @@ in_addr addressFromMessage(const cmsghdr& cmsg) {
#endif
}

constexpr int messageTruncatedOption() {
#if defined(__APPLE__)
// OSX does not support passing `MSG_TRUNC` to recvmsg and recvmmsg. This does not effect
// functionality and it primarily used for logging.
return 0;
#else
return MSG_TRUNC;
#endif
}

} // namespace

namespace Network {
@@ -296,7 +306,8 @@ Api::IoCallUint64Result IoSocketHandleImpl::recvmsg(Buffer::RawSlice* slices,
hdr.msg_flags = 0;
hdr.msg_control = cbuf.begin();
hdr.msg_controllen = cmsg_space_;
Api::SysCallSizeResult result = Api::OsSysCallsSingleton::get().recvmsg(fd_, &hdr, MSG_TRUNC);
Api::SysCallSizeResult result =
Api::OsSysCallsSingleton::get().recvmsg(fd_, &hdr, messageTruncatedOption());
if (result.rc_ < 0) {
return sysCallResultToIoCallResult(result);
}
@@ -384,8 +395,9 @@ Api::IoCallUint64Result IoSocketHandleImpl::recvmmsg(RawSliceArrays& slices, uin
// Set MSG_WAITFORONE so that recvmmsg will not waiting for
// |num_packets_per_mmsg_call| packets to arrive before returning when the
// socket is a blocking socket.
const Api::SysCallIntResult result = Api::OsSysCallsSingleton::get().recvmmsg(
fd_, mmsg_hdr.data(), num_packets_per_mmsg_call, MSG_TRUNC | MSG_WAITFORONE, nullptr);
const Api::SysCallIntResult result =
Api::OsSysCallsSingleton::get().recvmmsg(fd_, mmsg_hdr.data(), num_packets_per_mmsg_call,
messageTruncatedOption() | MSG_WAITFORONE, nullptr);

if (result.rc_ <= 0) {
return sysCallResultToIoCallResult(result);