Skip to content

Commit

Permalink
Enhance code safety and readability in SSL_get_shared_ciphers()
Browse files Browse the repository at this point in the history
This commit introduces two key improvements:

1. Improve code safety by replacing the conditional statement with
`if (n >= size)` and using OPENSSL_strnlen() instead of strlen().
This change ensures proper buffer size handling and adheres to
secure coding practices.

2. Enhance code readability by substituting `strcpy(p, c->name)` with
`memcpy(p, c->name, n)`. This adjustment prioritizes code clarity and
maintenance, even while mitigating a minimal buffer overflow risk.

These enhancements bolster the code's robustness and comprehensibility,
aligning with secure coding principles and best practices.

Fixes #19837

Signed-off-by: Sumitra Sharma <sumitraartsy@gmail.com>

Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #21934)

(cherry picked from commit 2743594)
  • Loading branch information
heygauri authored and t8m committed Sep 18, 2023
1 parent 3558a8c commit 36f1b6e
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions ssl/ssl_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -2809,14 +2809,14 @@ char *SSL_get_shared_ciphers(const SSL *s, char *buf, int size)
if (sk_SSL_CIPHER_find(srvrsk, c) < 0)
continue;

n = strlen(c->name);
if (n + 1 > size) {
n = OPENSSL_strnlen(c->name, size);
if (n >= size) {
if (p != buf)
--p;
*p = '\0';
return buf;
}
strcpy(p, c->name);
memcpy(p, c->name, n);
p += n;
*(p++) = ':';
size -= n + 1;
Expand Down

0 comments on commit 36f1b6e

Please sign in to comment.