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

Do not use arrays in function parameter types #4452

Open
gilles-peskine-arm opened this issue Apr 30, 2021 · 1 comment
Open

Do not use arrays in function parameter types #4452

gilles-peskine-arm opened this issue Apr 30, 2021 · 1 comment
Labels
api-break This issue/PR breaks the API and must wait for a new major version component-crypto Crypto primitives and low-level interfaces size-s Estimated task size: small (~2d)

Comments

@gilles-peskine-arm
Copy link
Contributor

gilles-peskine-arm commented Apr 30, 2021

Context and rationale

A few functions in the API have a parameter of array type. Array types are equivalent to pointers in function parameters as far as the C language is concerned: the array size is purely indicative. However, static analyzers including compilers don't always see it that way and may treat the array size as meaningful.

See #4130 for why this is a problem. Popular compilers are increasingly likely to complain that mbedtls_sha512_finish should not output to a 48-byte buffer, even when it's calculating a SHA-384 hash, because its output is declared as unsigned char[64].

Proposal

Change array types in API function parameters to pointers.

Work items for 3.0

  • At least address sha512_finish, which is the only case I'm aware of that is a problem in practice.
  • If time permits, address all API functions. I think the following command finds them all plus a few false positives:
    egrep '\w+\[[^][]*\] *[,)]' include/mbedtls/*.h
    

Work items for 3.x

Work items for 4.0

Since low-level crypto APIs are becoming private, only one public headers still has array parameters: ssl.h. Let's remove those.

typedef void mbedtls_ssl_export_keys_t(void *p_expkey,
                                       mbedtls_ssl_key_export_type type,
                                       const unsigned char *secret,
                                       size_t secret_len,
                                       const unsigned char client_random[32],
                                       const unsigned char server_random[32],
                                       mbedtls_tls_prf_types tls_prf_type);
int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl,
                            int *enabled,
                            unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
                            size_t *own_cid_len);
int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
                             int *enabled,
                             unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
                             size_t *peer_cid_len);
void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
                                           const unsigned char period[8]);
@gilles-peskine-arm
Copy link
Contributor Author

#4506 has done the critical part, which is the sha512 functions. I'm leaving this task open for all the other array parameters, but this is low-importance since all the remaining ones actually do have a fixed size so they aren't causing trouble, and we may well want to stop here.

@bensze01 bensze01 modified the milestone: Mbed TLS 4.0 Jul 28, 2021
@bensze01 bensze01 removed this from the Mbed TLS 4.0 milestone Aug 11, 2021
@daverodgman daverodgman added api-break This issue/PR breaks the API and must wait for a new major version and removed mbedtls-3 labels May 13, 2022
@gilles-peskine-arm gilles-peskine-arm moved this to Implementation needed in Mbed TLS 4.0 planning Jun 19, 2024
@github-project-automation github-project-automation bot moved this to Mbed TLS 4.0 COULD in Backlog for Mbed TLS Aug 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-break This issue/PR breaks the API and must wait for a new major version component-crypto Crypto primitives and low-level interfaces size-s Estimated task size: small (~2d)
Projects
Status: Mbed TLS 4.0 COULD
Status: Implementation needed
Development

No branches or pull requests

3 participants