Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 15c21bf

Browse files
committed
Defend against possibility that SSL error reporting mechanism returns
a NULL pointer. Per report from Stephen Pillinger 8-Nov-01.
1 parent f6ee99a commit 15c21bf

File tree

2 files changed

+76
-11
lines changed

2 files changed

+76
-11
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.259 2001/11/10 23:06:12 tgl Exp $
40+
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.260 2001/11/11 02:09:05 tgl Exp $
4141
*
4242
* NOTES
4343
*
@@ -271,6 +271,7 @@ __attribute__((format(printf, 1, 2)));
271271

272272
#ifdef USE_SSL
273273
static void InitSSL(void);
274+
static const char *SSLerrmessage(void);
274275
#endif
275276

276277

@@ -1108,8 +1109,8 @@ ProcessStartupPacket(Port *port, bool SSLdone)
11081109
!SSL_set_fd(port->ssl, port->sock) ||
11091110
SSL_accept(port->ssl) <= 0)
11101111
{
1111-
elog(DEBUG, "failed to initialize SSL connection: %s (%s)",
1112-
ERR_reason_error_string(ERR_get_error()), strerror(errno));
1112+
elog(DEBUG, "failed to initialize SSL connection: %s (%m)",
1113+
SSLerrmessage());
11131114
return STATUS_ERROR;
11141115
}
11151116
}
@@ -2379,6 +2380,7 @@ CountChildren(void)
23792380
}
23802381

23812382
#ifdef USE_SSL
2383+
23822384
/*
23832385
* Initialize SSL library and structures
23842386
*/
@@ -2393,31 +2395,56 @@ InitSSL(void)
23932395
if (!SSL_context)
23942396
{
23952397
postmaster_error("failed to create SSL context: %s",
2396-
ERR_reason_error_string(ERR_get_error()));
2398+
SSLerrmessage());
23972399
ExitPostmaster(1);
23982400
}
23992401
snprintf(fnbuf, sizeof(fnbuf), "%s/server.crt", DataDir);
24002402
if (!SSL_CTX_use_certificate_file(SSL_context, fnbuf, SSL_FILETYPE_PEM))
24012403
{
24022404
postmaster_error("failed to load server certificate (%s): %s",
2403-
fnbuf, ERR_reason_error_string(ERR_get_error()));
2405+
fnbuf, SSLerrmessage());
24042406
ExitPostmaster(1);
24052407
}
24062408
snprintf(fnbuf, sizeof(fnbuf), "%s/server.key", DataDir);
24072409
if (!SSL_CTX_use_PrivateKey_file(SSL_context, fnbuf, SSL_FILETYPE_PEM))
24082410
{
24092411
postmaster_error("failed to load private key file (%s): %s",
2410-
fnbuf, ERR_reason_error_string(ERR_get_error()));
2412+
fnbuf, SSLerrmessage());
24112413
ExitPostmaster(1);
24122414
}
24132415
if (!SSL_CTX_check_private_key(SSL_context))
24142416
{
24152417
postmaster_error("check of private key failed: %s",
2416-
ERR_reason_error_string(ERR_get_error()));
2418+
SSLerrmessage());
24172419
ExitPostmaster(1);
24182420
}
24192421
}
2420-
#endif
2422+
2423+
/*
2424+
* Obtain reason string for last SSL error
2425+
*
2426+
* Some caution is needed here since ERR_reason_error_string will
2427+
* return NULL if it doesn't recognize the error code. We don't
2428+
* want to return NULL ever.
2429+
*/
2430+
static const char *
2431+
SSLerrmessage(void)
2432+
{
2433+
unsigned long errcode;
2434+
const char *errreason;
2435+
static char errbuf[32];
2436+
2437+
errcode = ERR_get_error();
2438+
if (errcode == 0)
2439+
return "No SSL error reported";
2440+
errreason = ERR_reason_error_string(errcode);
2441+
if (errreason != NULL)
2442+
return errreason;
2443+
snprintf(errbuf, sizeof(errbuf), "SSL error code %lu", errcode);
2444+
return errbuf;
2445+
}
2446+
2447+
#endif /* USE_SSL */
24212448

24222449
/*
24232450
* Fire off a subprocess for startup/shutdown/checkpoint.

src/interfaces/libpq/fe-connect.c

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.180 2001/11/05 17:46:37 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.181 2001/11/11 02:09:05 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -186,6 +186,9 @@ static char *conninfo_getval(PQconninfoOption *connOptions,
186186
static void defaultNoticeProcessor(void *arg, const char *message);
187187
static int parseServiceInfo(PQconninfoOption *options,
188188
PQExpBuffer errorMessage);
189+
#ifdef USE_SSL
190+
static const char *SSLerrmessage(void);
191+
#endif
189192

190193

191194
/*
@@ -961,7 +964,7 @@ connectDBStart(PGconn *conn)
961964
{
962965
printfPQExpBuffer(&conn->errorMessage,
963966
libpq_gettext("could not create SSL context: %s\n"),
964-
ERR_reason_error_string(ERR_get_error()));
967+
SSLerrmessage());
965968
goto connect_errReturn;
966969
}
967970
}
@@ -971,7 +974,7 @@ connectDBStart(PGconn *conn)
971974
{
972975
printfPQExpBuffer(&conn->errorMessage,
973976
libpq_gettext("could not establish SSL connection: %s\n"),
974-
ERR_reason_error_string(ERR_get_error()));
977+
SSLerrmessage());
975978
goto connect_errReturn;
976979
}
977980
/* SSL connection finished. Continue to send startup packet */
@@ -981,7 +984,12 @@ connectDBStart(PGconn *conn)
981984
/* Received error - probably protocol mismatch */
982985
if (conn->Pfdebug)
983986
fprintf(conn->Pfdebug, "Postmaster reports error, attempting fallback to pre-7.0.\n");
987+
#ifdef WIN32
988+
closesocket(conn->sock);
989+
#else
984990
close(conn->sock);
991+
#endif
992+
conn->sock = -1;
985993
conn->allow_ssl_try = FALSE;
986994
return connectDBStart(conn);
987995
}
@@ -2610,6 +2618,36 @@ PQconninfoFree(PQconninfoOption *connOptions)
26102618
free(connOptions);
26112619
}
26122620

2621+
2622+
#ifdef USE_SSL
2623+
2624+
/*
2625+
* Obtain reason string for last SSL error
2626+
*
2627+
* Some caution is needed here since ERR_reason_error_string will
2628+
* return NULL if it doesn't recognize the error code. We don't
2629+
* want to return NULL ever.
2630+
*/
2631+
static const char *
2632+
SSLerrmessage(void)
2633+
{
2634+
unsigned long errcode;
2635+
const char *errreason;
2636+
static char errbuf[32];
2637+
2638+
errcode = ERR_get_error();
2639+
if (errcode == 0)
2640+
return "No SSL error reported";
2641+
errreason = ERR_reason_error_string(errcode);
2642+
if (errreason != NULL)
2643+
return errreason;
2644+
snprintf(errbuf, sizeof(errbuf), "SSL error code %lu", errcode);
2645+
return errbuf;
2646+
}
2647+
2648+
#endif /* USE_SSL */
2649+
2650+
26132651
/* =========== accessor functions for PGconn ========= */
26142652
char *
26152653
PQdb(const PGconn *conn)

0 commit comments

Comments
 (0)