Skip to content

Commit 8200414

Browse files
committed
BUG/MINOR: ssl: always check for ssl connection before getting its XPRT context
In several SSL functions, the XPRT context is retrieved before any check on the connection. In the function ssl_sock_is_ssl(), a test suggests the connection may be null. So, it is safer to test the ssl connection before retrieving its XPRT context. It removes any ambiguities and prevents possible null pointer dereferences. This patch fixes the issue haproxy#265. It must be backported to 2.0.
1 parent ad6c2ea commit 8200414

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

src/ssl_sock.c

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6142,15 +6142,15 @@ static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
61426142
/* used for ppv2 pkey alog (can be used for logging) */
61436143
int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
61446144
{
6145-
struct ssl_sock_ctx *ctx = conn->xprt_ctx;
6145+
struct ssl_sock_ctx *ctx;
61466146
struct pkey_info *pkinfo;
61476147
int bits = 0;
61486148
int sig = TLSEXT_signature_anonymous;
61496149
int len = -1;
61506150

61516151
if (!ssl_sock_is_ssl(conn))
61526152
return 0;
6153-
6153+
ctx = conn->xprt_ctx;
61546154
pkinfo = SSL_CTX_get_ex_data(SSL_get_SSL_CTX(ctx->ssl), ssl_pkey_info_index);
61556155
if (pkinfo) {
61566156
sig = pkinfo->sig;
@@ -6201,13 +6201,14 @@ int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
62016201
/* used for ppv2 cert signature (can be used for logging) */
62026202
const char *ssl_sock_get_cert_sig(struct connection *conn)
62036203
{
6204-
struct ssl_sock_ctx *ctx = conn->xprt_ctx;
6204+
struct ssl_sock_ctx *ctx;
62056205

62066206
__OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
62076207
X509 *crt;
62086208

62096209
if (!ssl_sock_is_ssl(conn))
62106210
return NULL;
6211+
ctx = conn->xprt_ctx;
62116212
crt = SSL_get_certificate(ctx->ssl);
62126213
if (!crt)
62136214
return NULL;
@@ -6219,10 +6220,11 @@ const char *ssl_sock_get_cert_sig(struct connection *conn)
62196220
const char *ssl_sock_get_sni(struct connection *conn)
62206221
{
62216222
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
6222-
struct ssl_sock_ctx *ctx = conn->xprt_ctx;
6223+
struct ssl_sock_ctx *ctx;
62236224

62246225
if (!ssl_sock_is_ssl(conn))
62256226
return NULL;
6227+
ctx = conn->xprt_ctx;
62266228
return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
62276229
#else
62286230
return NULL;
@@ -6232,22 +6234,22 @@ const char *ssl_sock_get_sni(struct connection *conn)
62326234
/* used for logging/ppv2, may be changed for a sample fetch later */
62336235
const char *ssl_sock_get_cipher_name(struct connection *conn)
62346236
{
6235-
struct ssl_sock_ctx *ctx = conn->xprt_ctx;
6237+
struct ssl_sock_ctx *ctx;
62366238

62376239
if (!ssl_sock_is_ssl(conn))
62386240
return NULL;
6239-
6241+
ctx = conn->xprt_ctx;
62406242
return SSL_get_cipher_name(ctx->ssl);
62416243
}
62426244

62436245
/* used for logging/ppv2, may be changed for a sample fetch later */
62446246
const char *ssl_sock_get_proto_version(struct connection *conn)
62456247
{
6246-
struct ssl_sock_ctx *ctx = conn->xprt_ctx;
6248+
struct ssl_sock_ctx *ctx;
62476249

62486250
if (!ssl_sock_is_ssl(conn))
62496251
return NULL;
6250-
6252+
ctx = conn->xprt_ctx;
62516253
return SSL_get_version(ctx->ssl);
62526254
}
62536255

@@ -6453,11 +6455,11 @@ ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out)
64536455
void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
64546456
{
64556457
#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
6456-
struct ssl_sock_ctx *ctx = conn->xprt_ctx;
6458+
struct ssl_sock_ctx *ctx;
64576459

64586460
if (!ssl_sock_is_ssl(conn))
64596461
return;
6460-
6462+
ctx = conn->xprt_ctx;
64616463
SSL_set_alpn_protos(ctx->ssl, alpn, len);
64626464
#endif
64636465
}
@@ -6468,12 +6470,13 @@ void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int l
64686470
void ssl_sock_set_servername(struct connection *conn, const char *hostname)
64696471
{
64706472
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
6471-
struct ssl_sock_ctx *ctx = conn->xprt_ctx;
6473+
struct ssl_sock_ctx *ctx;
64726474

64736475
char *prev_name;
64746476

64756477
if (!ssl_sock_is_ssl(conn))
64766478
return;
6479+
ctx = conn->xprt_ctx;
64776480

64786481
/* if the SNI changes, we must destroy the reusable context so that a
64796482
* new connection will present a new SNI. As an optimization we could
@@ -6498,7 +6501,7 @@ void ssl_sock_set_servername(struct connection *conn, const char *hostname)
64986501
int ssl_sock_get_remote_common_name(struct connection *conn,
64996502
struct buffer *dest)
65006503
{
6501-
struct ssl_sock_ctx *ctx = conn->xprt_ctx;
6504+
struct ssl_sock_ctx *ctx;
65026505
X509 *crt = NULL;
65036506
X509_NAME *name;
65046507
const char find_cn[] = "CN";
@@ -6510,6 +6513,7 @@ int ssl_sock_get_remote_common_name(struct connection *conn,
65106513

65116514
if (!ssl_sock_is_ssl(conn))
65126515
goto out;
6516+
ctx = conn->xprt_ctx;
65136517

65146518
/* SSL_get_peer_certificate, it increase X509 * ref count */
65156519
crt = SSL_get_peer_certificate(ctx->ssl);
@@ -6531,11 +6535,12 @@ int ssl_sock_get_remote_common_name(struct connection *conn,
65316535
/* returns 1 if client passed a certificate for this session, 0 if not */
65326536
int ssl_sock_get_cert_used_sess(struct connection *conn)
65336537
{
6534-
struct ssl_sock_ctx *ctx = conn->xprt_ctx;
6538+
struct ssl_sock_ctx *ctx;
65356539
X509 *crt = NULL;
65366540

65376541
if (!ssl_sock_is_ssl(conn))
65386542
return 0;
6543+
ctx = conn->xprt_ctx;
65396544

65406545
/* SSL_get_peer_certificate, it increase X509 * ref count */
65416546
crt = SSL_get_peer_certificate(ctx->ssl);
@@ -6549,22 +6554,22 @@ int ssl_sock_get_cert_used_sess(struct connection *conn)
65496554
/* returns 1 if client passed a certificate for this connection, 0 if not */
65506555
int ssl_sock_get_cert_used_conn(struct connection *conn)
65516556
{
6552-
struct ssl_sock_ctx *ctx = conn->xprt_ctx;
6557+
struct ssl_sock_ctx *ctx;
65536558

65546559
if (!ssl_sock_is_ssl(conn))
65556560
return 0;
6556-
6561+
ctx = conn->xprt_ctx;
65576562
return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
65586563
}
65596564

65606565
/* returns result from SSL verify */
65616566
unsigned int ssl_sock_get_verify_result(struct connection *conn)
65626567
{
6563-
struct ssl_sock_ctx *ctx = conn->xprt_ctx;
6568+
struct ssl_sock_ctx *ctx;
65646569

65656570
if (!ssl_sock_is_ssl(conn))
65666571
return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
6567-
6572+
ctx = conn->xprt_ctx;
65686573
return (unsigned int)SSL_get_verify_result(ctx->ssl);
65696574
}
65706575

0 commit comments

Comments
 (0)