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

Problem: msg_t leaks/unhandled failures (and bad style). #2486

Merged
merged 6 commits into from
Mar 31, 2017
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
30 changes: 15 additions & 15 deletions src/curve_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ int zmq::curve_server_t::send_zap_request (const uint8_t *key)
msg.set_flags (msg_t::more);
rc = session->write_zap_msg (&msg);
if (rc != 0)
return send_failure (&msg);
return close_and_return (&msg, -1);

// Version frame
rc = msg.init_size (3);
Expand All @@ -589,7 +589,7 @@ int zmq::curve_server_t::send_zap_request (const uint8_t *key)
msg.set_flags (msg_t::more);
rc = session->write_zap_msg (&msg);
if (rc != 0)
return send_failure (&msg);
return close_and_return (&msg, -1);

// Request ID frame
rc = msg.init_size (1);
Expand All @@ -598,7 +598,7 @@ int zmq::curve_server_t::send_zap_request (const uint8_t *key)
msg.set_flags (msg_t::more);
rc = session->write_zap_msg (&msg);
if (rc != 0)
return send_failure (&msg);
return close_and_return (&msg, -1);

// Domain frame
rc = msg.init_size (options.zap_domain.length ());
Expand All @@ -607,7 +607,7 @@ int zmq::curve_server_t::send_zap_request (const uint8_t *key)
msg.set_flags (msg_t::more);
rc = session->write_zap_msg (&msg);
if (rc != 0)
return send_failure (&msg);
return close_and_return (&msg, -1);

// Address frame
rc = msg.init_size (peer_address.length ());
Expand All @@ -616,7 +616,7 @@ int zmq::curve_server_t::send_zap_request (const uint8_t *key)
msg.set_flags (msg_t::more);
rc = session->write_zap_msg (&msg);
if (rc != 0)
return send_failure (&msg);
return close_and_return (&msg, -1);

// Identity frame
rc = msg.init_size (options.identity_size);
Expand All @@ -625,7 +625,7 @@ int zmq::curve_server_t::send_zap_request (const uint8_t *key)
msg.set_flags (msg_t::more);
rc = session->write_zap_msg (&msg);
if (rc != 0)
return send_failure (&msg);
return close_and_return (&msg, -1);

// Mechanism frame
rc = msg.init_size (5);
Expand All @@ -634,15 +634,15 @@ int zmq::curve_server_t::send_zap_request (const uint8_t *key)
msg.set_flags (msg_t::more);
rc = session->write_zap_msg (&msg);
if (rc != 0)
return send_failure (&msg);
return close_and_return (&msg, -1);

// Credentials frame
rc = msg.init_size (crypto_box_PUBLICKEYBYTES);
errno_assert (rc == 0);
memcpy (msg.data (), key, crypto_box_PUBLICKEYBYTES);
rc = session->write_zap_msg (&msg);
if (rc != 0)
return send_failure (&msg);
return close_and_return (&msg, -1);

return 0;
}
Expand All @@ -661,12 +661,12 @@ int zmq::curve_server_t::receive_and_process_zap_reply ()
for (int i = 0; i < 7; i++) {
rc = session->read_zap_msg (&msg [i]);
if (rc == -1)
return send_failure (msg);
return close_and_return (msg, -1);
if ((msg [i].flags () & msg_t::more) == (i < 6? 0: msg_t::more)) {
// Temporary support for security debugging
puts ("CURVE I: ZAP handler sent incomplete reply message");
errno = EPROTO;
return send_failure (msg);
return close_and_return (msg, -1);
}
}

Expand All @@ -675,31 +675,31 @@ int zmq::curve_server_t::receive_and_process_zap_reply ()
// Temporary support for security debugging
puts ("CURVE I: ZAP handler sent malformed reply message");
errno = EPROTO;
return send_failure (msg);
return close_and_return (msg, -1);
}

// Version frame
if (msg [1].size () != 3 || memcmp (msg [1].data (), "1.0", 3)) {
// Temporary support for security debugging
puts ("CURVE I: ZAP handler sent bad version number");
errno = EPROTO;
return send_failure (msg);
return close_and_return (msg, -1);
}

// Request id frame
if (msg [2].size () != 1 || memcmp (msg [2].data (), "1", 1)) {
// Temporary support for security debugging
puts ("CURVE I: ZAP handler sent bad request ID");
errno = EPROTO;
return send_failure (msg);
return close_and_return (msg, -1);
}

// Status code frame
if (msg [3].size () != 3) {
// Temporary support for security debugging
puts ("CURVE I: ZAP handler rejected client authentication");
errno = EACCES;
return send_failure (msg);
return close_and_return (msg, -1);
}

// Save status code
Expand All @@ -713,7 +713,7 @@ int zmq::curve_server_t::receive_and_process_zap_reply ()
msg [6].size (), true);

if (rc != 0)
return send_failure (msg);
return close_and_return (msg, -1);

// Close all reply frames
for (int i = 0; i < 7; i++) {
Expand Down
30 changes: 15 additions & 15 deletions src/gssapi_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ int zmq::gssapi_server_t::send_zap_request ()
msg.set_flags (msg_t::more);
rc = session->write_zap_msg (&msg);
if (rc != 0)
return send_failure (&msg);
return close_and_return (&msg, -1);

// Version frame
rc = msg.init_size (3);
Expand All @@ -170,7 +170,7 @@ int zmq::gssapi_server_t::send_zap_request ()
msg.set_flags (msg_t::more);
rc = session->write_zap_msg (&msg);
if (rc != 0)
return send_failure (&msg);
return close_and_return (&msg, -1);

// Request ID frame
rc = msg.init_size (1);
Expand All @@ -179,7 +179,7 @@ int zmq::gssapi_server_t::send_zap_request ()
msg.set_flags (msg_t::more);
rc = session->write_zap_msg (&msg);
if (rc != 0)
return send_failure (&msg);
return close_and_return (&msg, -1);

// Domain frame
rc = msg.init_size (options.zap_domain.length ());
Expand All @@ -188,7 +188,7 @@ int zmq::gssapi_server_t::send_zap_request ()
msg.set_flags (msg_t::more);
rc = session->write_zap_msg (&msg);
if (rc != 0)
return send_failure (&msg);
return close_and_return (&msg, -1);

// Address frame
rc = msg.init_size (peer_address.length ());
Expand All @@ -197,7 +197,7 @@ int zmq::gssapi_server_t::send_zap_request ()
msg.set_flags (msg_t::more);
rc = session->write_zap_msg (&msg);
if (rc != 0)
return send_failure (&msg);
return close_and_return (&msg, -1);

// Identity frame
rc = msg.init_size (options.identity_size);
Expand All @@ -206,7 +206,7 @@ int zmq::gssapi_server_t::send_zap_request ()
msg.set_flags (msg_t::more);
rc = session->write_zap_msg (&msg);
if (rc != 0)
return send_failure (&msg);
return close_and_return (&msg, -1);

// Mechanism frame
rc = msg.init_size (6);
Expand All @@ -215,7 +215,7 @@ int zmq::gssapi_server_t::send_zap_request ()
msg.set_flags (msg_t::more);
rc = session->write_zap_msg (&msg);
if (rc != 0)
return send_failure (&msg);
return close_and_return (&msg, -1);

// Principal frame
gss_buffer_desc principal;
Expand All @@ -227,7 +227,7 @@ int zmq::gssapi_server_t::send_zap_request ()
rc = session->write_zap_msg (&msg);
gss_release_buffer(&min_stat, &principal);
if (rc != 0)
return send_failure (&msg);
return close_and_return (&msg, -1);

return 0;
}
Expand All @@ -246,35 +246,35 @@ int zmq::gssapi_server_t::receive_and_process_zap_reply ()
for (int i = 0; i < 7; i++) {
rc = session->read_zap_msg (&msg [i]);
if (rc == -1)
return send_failure (msg);
return close_and_return (msg, -1);
if ((msg [i].flags () & msg_t::more) == (i < 6? 0: msg_t::more)) {
errno = EPROTO;
return send_failure (msg);
return close_and_return (msg, -1);
}
}

// Address delimiter frame
if (msg [0].size () > 0) {
errno = EPROTO;
return send_failure (msg);
return close_and_return (msg, -1);
}

// Version frame
if (msg [1].size () != 3 || memcmp (msg [1].data (), "1.0", 3)) {
errno = EPROTO;
return send_failure (msg);
return close_and_return (msg, -1);
}

// Request id frame
if (msg [2].size () != 1 || memcmp (msg [2].data (), "1", 1)) {
errno = EPROTO;
return send_failure (msg);
return close_and_return (msg, -1);
}

// Status code frame
if (msg [3].size () != 3 || memcmp (msg [3].data (), "200", 3)) {
errno = EACCES;
return send_failure (msg);
return close_and_return (msg, -1);
}

// Save user id
Expand All @@ -285,7 +285,7 @@ int zmq::gssapi_server_t::receive_and_process_zap_reply ()
msg [6].size (), true);

if (rc != 0)
return send_failure (msg);
return close_and_return (msg, -1);

// Close all reply frames
for (int i = 0; i < 7; i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ int zmq::msg_t::close ()

if (is_zcmsg())
{
zmq_assert( u.zclmsg.content->ffn );
zmq_assert(u.zclmsg.content->ffn);

// If the content is not shared, or if it is shared and the reference
// count has dropped to zero, deallocate it.
Expand Down
14 changes: 8 additions & 6 deletions src/msg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,19 +247,21 @@ namespace zmq
} u;
};

inline int send_failure (zmq::msg_t *msg)
inline int close_and_return (zmq::msg_t *msg, int echo)
{
// Since we abort on close failure we preserve errno for success case.
int err = errno;
const int rc = msg->close ();
errno_assert (rc == 0);
return -1;
errno = err;
return echo;
}

inline int send_failure (zmq::msg_t msg[], int count)
inline int close_and_return (zmq::msg_t msg [], int count, int echo)
{
for (int i = 0; i < count; i++)
send_failure (&msg [i]);

return -1;
close_and_return (&msg [i], 0);
return echo;
}
}

Expand Down
Loading