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

refactor: make s2n_array_len constant #4801

Merged
merged 4 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 2 additions & 3 deletions codebuild/bin/grep_simple_mistakes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,10 @@ done
#############################################
S2N_FILES_ARRAY_SIZING_RETURN=$(find "$PWD" -type f -name "s2n*.c" -path "*")
for file in $S2N_FILES_ARRAY_SIZING_RETURN; do
RESULT_ARR_DIV=`grep -Ern 'sizeof\((.*)\) \/ sizeof\(\1\[0\]\)' $file`

RESULT_ARR_DIV=`grep -Ern 'sizeof\(.*?\) / sizeof\(' $file`
if [ "${#RESULT_ARR_DIV}" != "0" ]; then
FAILED=1
printf "\e[1;34mUsage of 'sizeof(array) / sizeof(array[0])' check failed. Use s2n_array_len(array) instead in $file:\e[0m\n$RESULT_ARR_DIV\n\n"
printf "\e[1;34mUsage of 'sizeof(array) / sizeof(T)' check failed. Use s2n_array_len instead in $file:\e[0m\n$RESULT_ARR_DIV\n\n"
fi
done

Expand Down
12 changes: 12 additions & 0 deletions tests/unit/s2n_safety_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,18 @@ int main(int argc, char **argv)
}
}

/* Test: s2n_array_len */
{
/* Must return correct length */
uint16_t test_data[10] = { 0 };
EXPECT_EQUAL(s2n_array_len(test_data), 10);
EXPECT_NOT_EQUAL(s2n_array_len(test_data), sizeof(test_data));

/* Must be usable as an array size / constant expression */
uint16_t test_data_dup[s2n_array_len(test_data)] = { 0 };
EXPECT_EQUAL(sizeof(test_data), sizeof(test_data_dup));
}

END_TEST();
return 0;
}
26 changes: 26 additions & 0 deletions tests/unit/s2n_server_alpn_extension_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,32 @@ int main(int argc, char **argv)
EXPECT_SUCCESS(s2n_connection_free(server_conn));
};

/* Send and receive maximum-sized alpn */
{
const uint8_t max_size = UINT8_MAX;

DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
s2n_connection_ptr_free);
EXPECT_NOT_NULL(client);
EXPECT_NULL(s2n_get_application_protocol(client));

DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
EXPECT_NOT_NULL(server);
memset(server->application_protocol, 'a', sizeof(server->application_protocol) - 1);

DEFER_CLEANUP(struct s2n_stuffer stuffer = { 0 }, s2n_stuffer_free);
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
EXPECT_SUCCESS(s2n_server_alpn_extension.send(server, &stuffer));
EXPECT_SUCCESS(s2n_server_alpn_extension.recv(client, &stuffer));
EXPECT_EQUAL(s2n_stuffer_data_available(&stuffer), 0);

const char *client_alpn = s2n_get_application_protocol(client);
EXPECT_NOT_NULL(client_alpn);
EXPECT_EQUAL(strlen(client_alpn), max_size);
EXPECT_STRING_EQUAL(client_alpn, server->application_protocol);
};

END_TEST();
return 0;
}
2 changes: 1 addition & 1 deletion tests/unit/s2n_tls12_handshake_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ int main(int argc, char **argv)

conn->handshake.handshake_type = test_handshake_type;

for (size_t i = 0; i < sizeof(expected) / sizeof(char *); i++) {
for (size_t i = 0; i < s2n_array_len(expected); i++) {
conn->handshake.message_number = i;
EXPECT_STRING_EQUAL(expected[i], s2n_connection_get_last_message_name(conn));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/s2n_tls13_cert_verify_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ int main(int argc, char **argv)

EXPECT_SUCCESS(s2n_enable_tls13_in_test());

for (size_t i = 0; i < sizeof(test_cases) / sizeof(struct s2n_tls13_cert_verify_test); i++) {
for (size_t i = 0; i < s2n_array_len(test_cases); i++) {
/* Run all tests for server sending and client receiving/verifying cert_verify message */
run_tests(&test_cases[i], S2N_CLIENT);

Expand Down
1 change: 0 additions & 1 deletion tls/extensions/s2n_server_alpn.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ static int s2n_alpn_recv(struct s2n_connection *conn, struct s2n_stuffer *extens

uint8_t protocol_len = 0;
POSIX_GUARD(s2n_stuffer_read_uint8(extension, &protocol_len));
POSIX_ENSURE_LT(protocol_len, s2n_array_len(conn->application_protocol));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now leads to a compiler error:

   /home/runner/work/s2n-tls/s2n-tls/tls/extensions/s2n_server_alpn.c:69:5: error: result of comparison of constant 256 with expression of type 'uint8_t' (aka 'unsigned char') is always true [-Werror,-Wtautological-constant-out-of-range-compare]
     69 |     POSIX_ENSURE_LT(protocol_len, s2n_array_len(conn->application_protocol));
        |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  /home/runner/work/s2n-tls/s2n-tls/utils/s2n_safety_macros.h:261:80: note: expanded from macro 'POSIX_ENSURE_LT'
    261 | #define POSIX_ENSURE_LT(a, b)                                 __S2N_ENSURE((a) < (b), POSIX_BAIL(S2N_ERR_SAFETY))
        |                                                               ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  /home/runner/work/s2n-tls/s2n-tls/utils/s2n_ensure.h:35:15: note: expanded from macro '__S2N_ENSURE'
     35 |         if (!(cond)) {             \
        |               ^~~~
  1 error generated.

I added a test instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm... i wonder how we can preserve this check, though. in theory the conn->application_protocol size could be reduced and then we'd have an overwrite. this check would at least avoid that issue.

Copy link
Contributor Author

@lrstewart lrstewart Sep 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new unit test I added should actually enforce that. The test I wrote sends an alpn based on the size of conn->application_protocol:

DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
EXPECT_NOT_NULL(server);
memset(server->application_protocol, 'a', sizeof(server->application_protocol) - 1);

But then it checks that the final result matches the known max size, independent of the size of conn->application_protocol:
const char *client_alpn = s2n_get_application_protocol(client);
EXPECT_NOT_NULL(client_alpn);
EXPECT_EQUAL(strlen(client_alpn), max_size);

So if conn->application_protocol shrinks, that test fails.

uint8_t *protocol = s2n_stuffer_raw_read(extension, protocol_len);
POSIX_ENSURE_REF(protocol);
Expand Down
2 changes: 1 addition & 1 deletion tls/s2n_cipher_suites.c
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ int s2n_cipher_suites_init(void)
/* Reset any selected record algorithms */
S2N_RESULT s2n_cipher_suites_cleanup(void)
{
const int num_cipher_suites = sizeof(s2n_all_cipher_suites) / sizeof(struct s2n_cipher_suite *);
const int num_cipher_suites = s2n_array_len(s2n_all_cipher_suites);
for (int i = 0; i < num_cipher_suites; i++) {
struct s2n_cipher_suite *cur_suite = s2n_all_cipher_suites[i];
cur_suite->available = 0;
Expand Down
9 changes: 8 additions & 1 deletion utils/s2n_safety.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,14 @@ S2N_CLEANUP_RESULT s2n_connection_apply_error_blinding(struct s2n_connection** c
} \
struct __useless_struct_to_allow_trailing_semicolon__

#define s2n_array_len(array) ((array != NULL) ? (sizeof(array) / sizeof(array[0])) : 0)
/* This method works for ARRAYS, not for POINTERS.
* Calling sizeof on an array declared in the current function correctly returns
* the total size of the array. But once the array is passed to another function,
* it behaves like a pointer. Calling sizeof on a pointer only returns the size
* of the pointer address itself (so usually 8).
* Newer compilers (gcc >= 8.1, clang >= 8.0) will warn if the argument is a pointer.
*/
#define s2n_array_len(array) (sizeof(array) / sizeof(array[0]))

int s2n_mul_overflow(uint32_t a, uint32_t b, uint32_t* out);

Expand Down
Loading