mirror of
https://github.com/apple/swift-nio-ssl
synced 2026-11-28 05:14:29 +00:00
### Motivation When a PSK cipher suite is negotiated on the client, BoringSSL's gives our callback a buffer of length `PSK_MAX_IDENTITY_LEN` + 1, and passes its full size as `max_identity_len`. The callback is expected to populate the identity with a NUL-terminated string, which is relied upon by the subsequent BoringSSL code, which calls `strdup`. However, our code guarded the adopter-provided identity against the `max_identity_len`, without taking into account the space for the NUL, so it permitted identities of length `PSK_MAX_IDENTITY_LEN` and clobbered the NUL, leading to UB. ### Modifications - In `clientPSKCallback`, reject identities whose length is `>= max_identity_len` (was `>`). - NUL-terminate the copied identity explicitly. BoringSSL is zero-initialising the buffer, but it's not documented to behaviour and it doesn't hurt for us to be safe. - Add a test that shows we accept an identity of `PSK_MAX_IDENTITY_LEN` - Add a test that shows we reject an identity of `PSK_MAX_IDENTITY_LEN` + 1 bytes (previously accepted). ### Result An adopter-supplied PSK client identity of exactly `max_identity_len` bytes is now rejected instead of triggering an out-of-bounds read in BoringSSL's `OPENSSL_strdup`. Identities up to `PSK_MAX_IDENTITY_LEN` still work.