From 72ab8ad44a386e1aa0cb20ae60379afccffb7d09 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Thu, 2 Nov 2023 12:00:02 +0000 Subject: [PATCH] Reject zero-lengths in psa_crypto_copy_and_free() Zero-length buffers should be represented in the psa_crypto_buffer_copy_t struct as NULL if it was created in psa_crypto_alloc_and_copy(), so reject non-NULL zero-length buffers. Signed-off-by: David Horstmann --- library/psa_crypto.c | 10 ++++++++++ tests/suites/test_suite_psa_crypto.data | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 02aa0eb279..33068af385 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -8521,7 +8521,17 @@ error: psa_status_t psa_crypto_copy_and_free(psa_crypto_buffer_copy_t *buffers) { + if ((buffers->input != NULL) && (buffers->input_len == 0)) { + /* Reject zero-length buffers, these should have been represented by + * NULL in psa_crypto_alloc_and_copy() */ + return PSA_ERROR_INVALID_ARGUMENT; + } if (buffers->output != NULL) { + if (buffers->output_len == 0) { + /* Reject zero-length buffers, these should have been represented + * by NULL in psa_crypto_alloc_and_copy() */ + return PSA_ERROR_INVALID_ARGUMENT; + } if (buffers->output_original == NULL) { /* Output is non-NULL but original output is NULL. The argument * buffers is invalid. Return an error as we have no original to diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data index 81ad333b67..f27a9beab4 100644 --- a/tests/suites/test_suite_psa_crypto.data +++ b/tests/suites/test_suite_psa_crypto.data @@ -7472,3 +7472,9 @@ psa_crypto_copy_and_free:0:20:0:20:1:PSA_ERROR_INVALID_ARGUMENT PSA buffers copy and free, null output_original and null output psa_crypto_copy_and_free:0:20:1:0:1:PSA_SUCCESS + +PSA buffers copy and free, zero-length input +psa_crypto_copy_and_free:0:0:0:20:0:PSA_ERROR_INVALID_ARGUMENT + +PSA buffers copy and free, zero-length output +psa_crypto_copy_and_free:20:0:0:0:0:PSA_ERROR_INVALID_ARGUMENT