Skip to content

Commit

Permalink
Change store/load back to set/clear/test
Browse files Browse the repository at this point in the history
  • Loading branch information
lrstewart committed Jun 15, 2023
1 parent 15445c2 commit 04149ee
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 62 deletions.
8 changes: 4 additions & 4 deletions tests/unit/s2n_alerts_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ int main(int argc, char **argv)
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));

/* Verify state prior to alert */
EXPECT_FALSE(s2n_atomic_load(&conn->close_notify_received));
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));

/* Write and process the alert */
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->in, not_close_notify_alert, sizeof(not_close_notify_alert)));
Expand All @@ -115,7 +115,7 @@ int main(int argc, char **argv)
EXPECT_FAILURE_WITH_ERRNO(s2n_process_alert_fragment(conn), S2N_ERR_ALERT);

/* Verify state after alert */
EXPECT_FALSE(s2n_atomic_load(&conn->close_notify_received));
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));

EXPECT_SUCCESS(s2n_connection_free(conn));
}
Expand All @@ -126,14 +126,14 @@ int main(int argc, char **argv)
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));

/* Verify state prior to alert */
EXPECT_FALSE(s2n_atomic_load(&conn->close_notify_received));
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));

/* Write and process the alert */
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->in, close_notify_alert, sizeof(close_notify_alert)));
EXPECT_SUCCESS(s2n_process_alert_fragment(conn));

/* Verify state after alert */
EXPECT_TRUE(s2n_atomic_load(&conn->close_notify_received));
EXPECT_TRUE(s2n_atomic_flag_test(&conn->close_notify_received));

EXPECT_SUCCESS(s2n_connection_free(conn));
}
Expand Down
24 changes: 12 additions & 12 deletions tests/unit/s2n_connection_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -811,24 +811,24 @@ int main(int argc, char **argv)
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED));

/* Close write */
s2n_atomic_store(&conn->write_closed, true);
s2n_atomic_flag_set(&conn->write_closed);
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE));
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_READABLE));
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX));
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED));
s2n_atomic_store(&conn->write_closed, false);
s2n_atomic_flag_clear(&conn->write_closed);

/* Close read */
s2n_atomic_store(&conn->read_closed, true);
s2n_atomic_flag_set(&conn->read_closed);
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE));
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_READABLE));
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX));
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED));
s2n_atomic_store(&conn->read_closed, false);
s2n_atomic_flag_clear(&conn->read_closed);

/* Close both */
s2n_atomic_store(&conn->read_closed, true);
s2n_atomic_store(&conn->write_closed, true);
s2n_atomic_flag_set(&conn->read_closed);
s2n_atomic_flag_set(&conn->write_closed);
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE));
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_READABLE));
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX));
Expand All @@ -849,24 +849,24 @@ int main(int argc, char **argv)
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED));

/* Close write */
s2n_atomic_store(&conn->write_closed, true);
s2n_atomic_flag_set(&conn->write_closed);
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE));
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_READABLE));
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX));
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED));
s2n_atomic_store(&conn->write_closed, false);
s2n_atomic_flag_clear(&conn->write_closed);

/* Close read */
s2n_atomic_store(&conn->read_closed, true);
s2n_atomic_flag_set(&conn->read_closed);
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE));
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_READABLE));
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX));
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED));
s2n_atomic_store(&conn->read_closed, false);
s2n_atomic_flag_clear(&conn->read_closed);

/* Close both */
s2n_atomic_store(&conn->read_closed, true);
s2n_atomic_store(&conn->write_closed, true);
s2n_atomic_flag_set(&conn->read_closed);
s2n_atomic_flag_set(&conn->write_closed);
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE));
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_READABLE));
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX));
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/s2n_early_data_io_api_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ int main(int argc, char **argv)

/* Pretend we didn't test the above error condition.
* The S2N_ERR_BAD_MESSAGE error triggered S2N to close the connection. */
s2n_atomic_store(&server_conn->write_closed, false);
s2n_atomic_store(&client_conn->write_closed, false);
s2n_atomic_flag_clear(&server_conn->write_closed);
s2n_atomic_flag_clear(&client_conn->write_closed);

/* Read the remaining early data properly */
EXPECT_SUCCESS(s2n_recv_early_data(server_conn, actual_payload, sizeof(actual_payload),
Expand Down
36 changes: 18 additions & 18 deletions tests/unit/s2n_shutdown_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ int main(int argc, char **argv)

/* Verify state prior to alert */
EXPECT_TRUE(s2n_handshake_is_complete(conn));
EXPECT_FALSE(s2n_atomic_load(&conn->close_notify_received));
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
EXPECT_FALSE(conn->alert_sent);
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX));

Expand All @@ -78,7 +78,7 @@ int main(int argc, char **argv)

/* Verify state after shutdown attempt */
EXPECT_TRUE(s2n_handshake_is_complete(conn));
EXPECT_FALSE(s2n_atomic_load(&conn->close_notify_received));
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
EXPECT_TRUE(conn->alert_sent);
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED));

Expand Down Expand Up @@ -109,7 +109,7 @@ int main(int argc, char **argv)

/* Verify state prior to alert */
EXPECT_TRUE(s2n_handshake_is_complete(conn));
EXPECT_FALSE(s2n_atomic_load(&conn->close_notify_received));
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
EXPECT_FALSE(conn->alert_sent);
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX));

Expand All @@ -123,7 +123,7 @@ int main(int argc, char **argv)

/* Verify state after shutdown attempt */
EXPECT_TRUE(s2n_handshake_is_complete(conn));
EXPECT_FALSE(s2n_atomic_load(&conn->close_notify_received));
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
EXPECT_TRUE(conn->alert_sent);
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE));

Expand All @@ -146,7 +146,7 @@ int main(int argc, char **argv)

/* Verify state prior to alert */
EXPECT_TRUE(s2n_handshake_is_complete(conn));
EXPECT_FALSE(s2n_atomic_load(&conn->close_notify_received));
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
EXPECT_FALSE(conn->alert_sent);
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX));

Expand All @@ -173,7 +173,7 @@ int main(int argc, char **argv)

/* Verify state after shutdown attempt */
EXPECT_TRUE(s2n_handshake_is_complete(conn));
EXPECT_FALSE(s2n_atomic_load(&conn->close_notify_received));
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
EXPECT_FALSE(conn->alert_sent);
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED));

Expand All @@ -195,7 +195,7 @@ int main(int argc, char **argv)

/* Verify state prior to alert */
EXPECT_FALSE(s2n_handshake_is_complete(conn));
EXPECT_FALSE(s2n_atomic_load(&conn->close_notify_received));
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
EXPECT_FALSE(conn->alert_sent);
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX));

Expand All @@ -204,7 +204,7 @@ int main(int argc, char **argv)

/* Verify state after shutdown */
EXPECT_FALSE(s2n_handshake_is_complete(conn));
EXPECT_FALSE(s2n_atomic_load(&conn->close_notify_received));
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
EXPECT_TRUE(conn->alert_sent);

/* Fully closed: we don't worry about truncating data */
Expand All @@ -225,7 +225,7 @@ int main(int argc, char **argv)
EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, conn));

/* Verify state prior to alert */
EXPECT_FALSE(s2n_atomic_load(&conn->close_notify_received));
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
EXPECT_FALSE(conn->alert_sent);
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX));

Expand All @@ -234,7 +234,7 @@ int main(int argc, char **argv)
EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ);

/* Verify state after shutdown attempt */
EXPECT_FALSE(s2n_atomic_load(&conn->close_notify_received));
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
EXPECT_TRUE(conn->alert_sent);

/* Half-close: only write closed */
Expand All @@ -257,7 +257,7 @@ int main(int argc, char **argv)
EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, conn));

/* Verify state prior to alert */
EXPECT_FALSE(s2n_atomic_load(&conn->close_notify_received));
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
EXPECT_FALSE(conn->alert_sent);
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX));

Expand All @@ -266,7 +266,7 @@ int main(int argc, char **argv)
EXPECT_SUCCESS(s2n_process_alert_fragment(conn));

/* Verify state after alert */
EXPECT_TRUE(s2n_atomic_load(&conn->close_notify_received));
EXPECT_TRUE(s2n_atomic_flag_test(&conn->close_notify_received));
EXPECT_FALSE(conn->alert_sent);
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE));
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_READABLE));
Expand All @@ -276,7 +276,7 @@ int main(int argc, char **argv)
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);

/* Verify state after shutdown attempt */
EXPECT_TRUE(s2n_atomic_load(&conn->close_notify_received));
EXPECT_TRUE(s2n_atomic_flag_test(&conn->close_notify_received));
EXPECT_TRUE(conn->alert_sent);
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED));
};
Expand Down Expand Up @@ -309,15 +309,15 @@ int main(int argc, char **argv)
EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(conn, &blocked), S2N_ERR_ALERT);

/* Verify state after shutdown attempt */
EXPECT_FALSE(s2n_atomic_load(&conn->close_notify_received));
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
EXPECT_TRUE(conn->alert_sent);
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED));
EXPECT_EQUAL(s2n_stuffer_data_available(&output), alert_record_size);

/* Future calls are no-ops */
for (size_t i = 0; i < 5; i++) {
EXPECT_SUCCESS(s2n_shutdown(conn, &blocked));
EXPECT_FALSE(s2n_atomic_load(&conn->close_notify_received));
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
EXPECT_TRUE(conn->alert_sent);
}
};
Expand Down Expand Up @@ -410,7 +410,7 @@ int main(int argc, char **argv)
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);

/* Verify state after shutdown attempt */
EXPECT_FALSE(s2n_atomic_load(&conn->close_notify_received));
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
EXPECT_FALSE(conn->alert_sent);
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED));
};
Expand Down Expand Up @@ -586,7 +586,7 @@ int main(int argc, char **argv)
/* Full close is no-op */
EXPECT_SUCCESS(s2n_shutdown(conn, &blocked));
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED));
EXPECT_FALSE(s2n_atomic_load(&conn->close_notify_received));
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
};

/* Test: Half close, peer alert, then full close */
Expand Down Expand Up @@ -621,7 +621,7 @@ int main(int argc, char **argv)
/* Full close is no-op */
EXPECT_SUCCESS(s2n_shutdown(conn, &blocked));
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED));
EXPECT_FALSE(s2n_atomic_load(&conn->close_notify_received));
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received));
};
}

Expand Down
6 changes: 3 additions & 3 deletions tls/s2n_alerts.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ int s2n_process_alert_fragment(struct s2n_connection *conn)
if (s2n_stuffer_data_available(&conn->alert_in) == 2) {
/* Close notifications are handled as shutdowns */
if (conn->alert_in_data[1] == S2N_TLS_ALERT_CLOSE_NOTIFY) {
s2n_atomic_store(&conn->read_closed, true);
s2n_atomic_store(&conn->close_notify_received, true);
s2n_atomic_flag_set(&conn->read_closed);
s2n_atomic_flag_set(&conn->close_notify_received);
return 0;
}

Expand All @@ -222,7 +222,7 @@ int s2n_process_alert_fragment(struct s2n_connection *conn)

/* All other alerts are treated as fatal errors */
POSIX_GUARD_RESULT(s2n_connection_set_closed(conn));
s2n_atomic_store(&conn->error_alert_received, true);
s2n_atomic_flag_set(&conn->error_alert_received);
POSIX_BAIL(S2N_ERR_ALERT);
}
}
Expand Down
8 changes: 4 additions & 4 deletions tls/s2n_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -1169,8 +1169,8 @@ S2N_CLEANUP_RESULT s2n_connection_apply_error_blinding(struct s2n_connection **c
S2N_RESULT s2n_connection_set_closed(struct s2n_connection *conn)
{
RESULT_ENSURE_REF(conn);
s2n_atomic_store(&conn->read_closed, true);
s2n_atomic_store(&conn->write_closed, true);
s2n_atomic_flag_set(&conn->read_closed);
s2n_atomic_flag_set(&conn->write_closed);
return S2N_RESULT_OK;
}

Expand Down Expand Up @@ -1548,8 +1548,8 @@ bool s2n_connection_check_io_status(struct s2n_connection *conn, s2n_io_status s
return false;
}

bool read_closed = s2n_atomic_load(&conn->read_closed);
bool write_closed = s2n_atomic_load(&conn->write_closed);
bool read_closed = s2n_atomic_flag_test(&conn->read_closed);
bool write_closed = s2n_atomic_flag_test(&conn->write_closed);
bool full_duplex = !read_closed && !write_closed;

/*
Expand Down
8 changes: 4 additions & 4 deletions tls/s2n_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ struct s2n_connection {
bool alert_sent;

/* Receiving error or close_notify alerts changes the behavior of s2n_shutdown_send */
s2n_atomic_bool error_alert_received;
s2n_atomic_bool close_notify_received;
s2n_atomic_flag error_alert_received;
s2n_atomic_flag close_notify_received;

/* Our handshake state machine */
struct s2n_handshake handshake;
Expand Down Expand Up @@ -313,8 +313,8 @@ struct s2n_connection {
/* Either the reader or the writer can trigger both sides of the connection
* to close in response to a fatal error.
*/
s2n_atomic_bool read_closed;
s2n_atomic_bool write_closed;
s2n_atomic_flag read_closed;
s2n_atomic_flag write_closed;

/* TLS extension data */
char server_name[S2N_MAX_SERVER_NAME + 1];
Expand Down
4 changes: 2 additions & 2 deletions tls/s2n_recv.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ S2N_RESULT s2n_read_in_bytes(struct s2n_connection *conn, struct s2n_stuffer *ou
errno = 0;
int r = s2n_connection_recv_stuffer(output, conn, remaining);
if (r == 0) {
s2n_atomic_store(&conn->read_closed, true);
s2n_atomic_flag_set(&conn->read_closed);
RESULT_BAIL(S2N_ERR_CLOSED);
} else if (r < 0) {
if (errno == EWOULDBLOCK || errno == EAGAIN) {
Expand Down Expand Up @@ -127,7 +127,7 @@ ssize_t s2n_recv_impl(struct s2n_connection *conn, void *buf, ssize_t size, s2n_
*# closed, the TLS implementation MUST receive a "close_notify" alert
*# before indicating end-of-data to the application layer.
*/
POSIX_ENSURE(s2n_atomic_load(&conn->close_notify_received), S2N_ERR_CLOSED);
POSIX_ENSURE(s2n_atomic_flag_test(&conn->close_notify_received), S2N_ERR_CLOSED);
*blocked = S2N_NOT_BLOCKED;
return 0;
}
Expand Down
8 changes: 4 additions & 4 deletions tls/s2n_shutdown.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
static bool s2n_shutdown_expect_close_notify(struct s2n_connection *conn)
{
/* No close_notify expected if we already received an error instead */
if (s2n_atomic_load(&conn->error_alert_received)) {
if (s2n_atomic_flag_test(&conn->error_alert_received)) {
return false;
}

Expand Down Expand Up @@ -67,11 +67,11 @@ int s2n_shutdown_send(struct s2n_connection *conn, s2n_blocked_status *blocked)
}

/* Flush any outstanding data */
s2n_atomic_store(&conn->write_closed, true);
s2n_atomic_flag_set(&conn->write_closed);
POSIX_GUARD(s2n_flush(conn, blocked));

/* For a connection closed due to receiving an alert, we don't send anything. */
if (s2n_atomic_load(&conn->error_alert_received)) {
if (s2n_atomic_flag_test(&conn->error_alert_received)) {
return S2N_SUCCESS;
}

Expand Down Expand Up @@ -120,7 +120,7 @@ int s2n_shutdown(struct s2n_connection *conn, s2n_blocked_status *blocked)
uint8_t record_type = 0;
int isSSLv2 = false;
*blocked = S2N_BLOCKED_ON_READ;
while (!s2n_atomic_load(&conn->close_notify_received)) {
while (!s2n_atomic_flag_test(&conn->close_notify_received)) {
POSIX_GUARD(s2n_read_full_record(conn, &record_type, &isSSLv2));
POSIX_ENSURE(!isSSLv2, S2N_ERR_BAD_MESSAGE);
if (record_type == TLS_ALERT) {
Expand Down
Loading

0 comments on commit 04149ee

Please sign in to comment.