From: Nikos Mavrogiannopoulos Date: Fri, 3 Oct 2003 09:09:00 +0000 (+0000) Subject: Applied patch by Arne that fixes several possible NULL pointer dereferences. X-Git-Tag: gnutls_0_9_90~21 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=36d366fdd3e1878ea909a64c6c6714d56f7de044;p=thirdparty%2Fgnutls.git Applied patch by Arne that fixes several possible NULL pointer dereferences. --- diff --git a/includes/gnutls/Makefile.am b/includes/gnutls/Makefile.am index d7914a5e2f..647b2bdff7 100644 --- a/includes/gnutls/Makefile.am +++ b/includes/gnutls/Makefile.am @@ -1 +1 @@ -EXTRA_DIST = extra.h openssl.h gnutls.h compat4.h x509.h compat8.h pkcs12.h +EXTRA_DIST = extra.h openssl.h gnutls.h compat4.h x509.h compat8.h pkcs12.h openpgp.h diff --git a/lib/gnutls_alert.c b/lib/gnutls_alert.c index 5350650ead..8476f5b5ae 100644 --- a/lib/gnutls_alert.c +++ b/lib/gnutls_alert.c @@ -108,11 +108,14 @@ int gnutls_alert_send( gnutls_session session, gnutls_alert_level level, gnutls_ { uint8 data[2]; int ret; - + const char *name; + data[0] = (uint8) level; data[1] = (uint8) desc; - _gnutls_record_log( "REC: Sending Alert[%d|%d] - %s\n", data[0], data[1], gnutls_alert_get_name((int)data[1])); + name = gnutls_alert_get_name((int)data[1]); + if (name == NULL) name = "(unknown)"; + _gnutls_record_log( "REC: Sending Alert[%d|%d] - %s\n", data[0], data[1], name); if ( (ret = _gnutls_send_int( session, GNUTLS_ALERT, -1, data, 2)) >= 0) return 0; diff --git a/lib/gnutls_algorithms.c b/lib/gnutls_algorithms.c index 796922ab08..e7759385d6 100644 --- a/lib/gnutls_algorithms.c +++ b/lib/gnutls_algorithms.c @@ -495,8 +495,8 @@ inline int _gnutls_mac_priority(gnutls_session session, gnutls_mac_algorithm alg * gnutls_mac_get_name - Returns a string with the name of the specified mac algorithm * @algorithm: is a MAC algorithm * - * Returns a string that contains the name - * of the specified MAC algorithm. + * Returns a string that contains the name + * of the specified MAC algorithm or NULL. **/ const char *gnutls_mac_get_name( gnutls_mac_algorithm algorithm) { @@ -542,8 +542,8 @@ inline * gnutls_compression_get_name - Returns a string with the name of the specified compression algorithm * @algorithm: is a Compression algorithm * - * Returns a pointer to a string that contains the name - * of the specified compression algorithm. + * Returns a pointer to a string that contains the name + * of the specified compression algorithm or NULL. **/ const char *gnutls_compression_get_name( gnutls_compression_method algorithm) { @@ -691,8 +691,8 @@ int _gnutls_cipher_get_export_flag(gnutls_cipher_algorithm algorithm) * gnutls_cipher_get_name - Returns a string with the name of the specified cipher algorithm * @algorithm: is an encryption algorithm * - * Returns a pointer to a string that contains the name - * of the specified cipher. + * Returns a pointer to a string that contains the name + * of the specified cipher or NULL. **/ const char *gnutls_cipher_get_name( gnutls_cipher_algorithm algorithm) { @@ -744,7 +744,7 @@ inline int _gnutls_kx_priority(gnutls_session session, gnutls_kx_algorithm algor * @algorithm: is a key exchange algorithm * * Returns a pointer to a string that contains the name - * of the specified key exchange algorithm. + * of the specified key exchange algorithm or NULL. **/ const char *gnutls_kx_get_name( gnutls_kx_algorithm algorithm) { @@ -829,8 +829,8 @@ gnutls_protocol_version _gnutls_version_max(gnutls_session session) * gnutls_protocol_get_name - Returns a string with the name of the specified SSL/TLS version * @version: is a (gnutls) version number * - * Returns a string that contains the name - * of the specified TLS version. + * Returns a string that contains the name + * of the specified TLS version or NULL. **/ const char *gnutls_protocol_get_name( gnutls_protocol_version version) { @@ -965,7 +965,7 @@ const char *_gnutls_cipher_suite_get_name(GNUTLS_CipherSuite suite) * @mac_algorithm: is a MAC algorithm * * Returns a string that contains the name of a TLS - * cipher suite, specified by the given algorithms. + * cipher suite, specified by the given algorithms, or NULL. * * Note that the full cipher suite name must be prepended * by TLS or SSL depending of the protocol in use. diff --git a/lib/x509/Makefile.am b/lib/x509/Makefile.am index abc53f3c1b..013fe97877 100644 --- a/lib/x509/Makefile.am +++ b/lib/x509/Makefile.am @@ -1,7 +1,7 @@ INCLUDES = -I../ -I../minitasn1/ -I../../includes/ EXTRA_DIST = dn.h common.h x509.h extensions.h pkcs7.h \ x509-api.tex compat.h verify.h mpi.h crq.h sign.h pkcs5.h \ - privkey.h pkcs12.h rc2.h + privkey.h pkcs12.h rc2.h rfc2818.h noinst_LTLIBRARIES = libx509.la #lib_LTLIBRARIES = libgnutls-x509.la diff --git a/lib/x509/rfc2818_hostname.c b/lib/x509/rfc2818_hostname.c index 6a903b0173..d83ede1083 100644 --- a/lib/x509/rfc2818_hostname.c +++ b/lib/x509/rfc2818_hostname.c @@ -26,8 +26,7 @@ #include #include #include - -static int hostname_compare(const char *certname, const char *hostname); +#include /*- * gnutls_x509_check_certificates_hostname - This function compares the given hostname with the hostname in the certificate @@ -44,7 +43,6 @@ static int hostname_compare(const char *certname, const char *hostname); int gnutls_x509_check_certificates_hostname(const gnutls_datum * cert, const char *hostname) { -#define MAX_CN 256 char dnsname[MAX_CN]; int dnsnamesize; int found_dnsname = 0; @@ -76,7 +74,7 @@ int gnutls_x509_check_certificates_hostname(const gnutls_datum * cert, if (ret == GNUTLS_SAN_DNSNAME) { found_dnsname = 1; - if (hostname_compare(dnsname, hostname)) { + if (_gnutls_hostname_compare(dnsname, hostname)) { return 1; } } @@ -92,7 +90,7 @@ int gnutls_x509_check_certificates_hostname(const gnutls_datum * cert, return 0; } - if (hostname_compare(dn.common_name, hostname)) { + if (_gnutls_hostname_compare(dn.common_name, hostname)) { return 1; } } @@ -105,7 +103,7 @@ int gnutls_x509_check_certificates_hostname(const gnutls_datum * cert, /* compare hostname against certificate, taking account of wildcards * return 1 on success or 0 on error */ -static int hostname_compare(const char *certname, const char *hostname) +int _gnutls_hostname_compare(const char *certname, const char *hostname) { const char *cmpstr1, *cmpstr2; @@ -184,7 +182,7 @@ int gnutls_x509_crt_check_hostname(gnutls_x509_crt cert, if (ret == GNUTLS_SAN_DNSNAME) { found_dnsname = 1; - if (hostname_compare(dnsname, hostname)) { + if (_gnutls_hostname_compare(dnsname, hostname)) { return 1; } } @@ -202,7 +200,7 @@ int gnutls_x509_crt_check_hostname(gnutls_x509_crt cert, return 0; } - if (hostname_compare(dnsname, hostname)) { + if (_gnutls_hostname_compare(dnsname, hostname)) { return 1; } } diff --git a/libextra/Makefile.am b/libextra/Makefile.am index 50f132acf8..c0999fd79a 100644 --- a/libextra/Makefile.am +++ b/libextra/Makefile.am @@ -28,12 +28,12 @@ COBJECTS_EXTRA = ext_srp.c \ libgnutls_extra_la_LDFLAGS = $(libgnutls_extra_version_script_cmd) \ -version-info $(LT_CURRENT):$(LT_REVISION):$(LT_AGE) \ $(LIBOPENCDK_LIBS) $(LZO_LINK) -libgnutls_extra_la_LIBADD = $(LZO_OBJECTS) ../lib/libgnutls.la libgnutls_extra_la_DEPENDENCIES = $(LZO_OBJECTS) libgnutls_extra_la_SOURCES = $(COBJECTS_EXTRA) -libgnutls_extra_la_LIBADD = openpgp/openpgp.lo openpgp/xml.lo openpgp/gnutls_openpgp.lo +libgnutls_extra_la_LIBADD = $(LZO_OBJECTS) ../lib/libgnutls.la \ + openpgp/openpgp.lo openpgp/xml.lo openpgp/gnutls_openpgp.lo EXTRA_libgnutls_extra_la_SOURCES = minilzo.c diff --git a/src/cli.c b/src/cli.c index 1189e5e02c..c5b823a3e9 100644 --- a/src/cli.c +++ b/src/cli.c @@ -35,7 +35,7 @@ #include #include #include -#include +#include "common.h" #include "cli-gaa.h" #ifndef SHUT_WR @@ -222,7 +222,7 @@ static int handle_error(socket_st hd, int err) } str = gnutls_strerror(err); - if (str == NULL) str = "(unknown)"; + if (str == NULL) str = str_unknown; fprintf(stderr, "*** %s error: %s\n", err_type, str); @@ -230,7 +230,7 @@ static int handle_error(socket_st hd, int err) || err == GNUTLS_E_FATAL_ALERT_RECEIVED) { alert = gnutls_alert_get(hd.session); str = gnutls_alert_get_name(alert); - if (str == NULL) str = "(unknown)"; + if (str == NULL) str = str_unknown; printf("*** Received alert [%d]: %s\n", alert, str); } @@ -240,7 +240,7 @@ static int handle_error(socket_st hd, int err) return ret; } -int starttls_alarmed; +int starttls_alarmed = 0; void starttls_alarm (int signum) { diff --git a/src/common.c b/src/common.c index 4fbd6132a8..702136bd2a 100644 --- a/src/common.c +++ b/src/common.c @@ -15,6 +15,8 @@ int xml = 0; #define PRINT_PGP_NAME(X) PRINTX( "NAME:", X.name); \ PRINTX( "EMAIL:", X.email) +const char str_unknown[] = "(unknown)"; + static const char *my_ctime(const time_t * tv) { static char buf[256]; @@ -22,7 +24,7 @@ static const char *my_ctime(const time_t * tv) if ( ( (tp = localtime(tv)) == NULL ) || (!strftime(buf, sizeof buf, "%a %b %e %H:%M:%S %Z %Y\n", tp)) ) - strcpy(buf, "unknown"); /* make sure buf text isn't garbage */ + strcpy(buf, str_unknown);/* make sure buf text isn't garbage */ return buf; @@ -64,7 +66,7 @@ void print_x509_info(gnutls_session session, const char* hostname) GNUTLS_X509_FMT_DER); if (ret < 0) { const char* str = gnutls_strerror(ret); - if (str == NULL) str = "(unknown)"; + if (str == NULL) str = str_unknown; fprintf(stderr, "Decoding error: %s\n", str); return; } @@ -90,7 +92,7 @@ void print_x509_info(gnutls_session session, const char* hostname) ret = gnutls_x509_crt_to_xml( crt, &xml_data, 0); if (ret < 0) { const char* str = gnutls_strerror(ret); - if (str == NULL) str = "(unknown)"; + if (str == NULL) str = str_unknown; fprintf(stderr, "XML encoding error: %s\n", str); return; @@ -127,7 +129,7 @@ void print_x509_info(gnutls_session session, const char* hostname) if ((ret=gnutls_x509_crt_get_fingerprint(crt, GNUTLS_DIG_MD5, digest, &digest_size)) < 0) { const char* str = gnutls_strerror(ret); - if (str == NULL) str = "(unknown)"; + if (str == NULL) str = str_unknown; fprintf(stderr, "Error in fingerprint calculation: %s\n", str); } else { print = printable; @@ -354,19 +356,19 @@ int print_info(gnutls_session session, const char* hostname) tmp = gnutls_protocol_get_name(gnutls_protocol_get_version(session)); - printf("- Version: %s\n", tmp); + if (tmp != NULL) printf("- Version: %s\n", tmp); tmp = gnutls_kx_get_name(kx); - printf("- Key Exchange: %s\n", tmp); + if (tmp != NULL) printf("- Key Exchange: %s\n", tmp); tmp = gnutls_cipher_get_name(gnutls_cipher_get(session)); - printf("- Cipher: %s\n", tmp); + if (tmp != NULL) printf("- Cipher: %s\n", tmp); tmp = gnutls_mac_get_name(gnutls_mac_get(session)); - printf("- MAC: %s\n", tmp); + if (tmp != NULL) printf("- MAC: %s\n", tmp); tmp = gnutls_compression_get_name(gnutls_compression_get(session)); - printf("- Compression: %s\n", tmp); + if (tmp != NULL) printf("- Compression: %s\n", tmp); fflush (stdout); diff --git a/src/common.h b/src/common.h index 594b33ea9e..7def7bde99 100644 --- a/src/common.h +++ b/src/common.h @@ -7,6 +7,8 @@ */ #define PRI_MAX 16 +extern const char str_unknown[]; + int print_info( gnutls_session state, const char* hostname); void print_cert_info( gnutls_session state, const char* hostname); void print_list(void); diff --git a/src/serv.c b/src/serv.c index 74a3a2bb59..0ffebc8f4a 100644 --- a/src/serv.c +++ b/src/serv.c @@ -87,7 +87,7 @@ char *x509_crlfile = NULL; #define SA struct sockaddr #define ERR(err,s) if(err==-1) {perror(s);return(1);} -#define GERR(ret) fprintf(stdout, "Error: %s\n", gnutls_strerror(ret)) +#define GERR(ret) fprintf(stdout, "Error: %s\n", safe_strerror(ret)) #define MAX_BUF 1024 #undef max @@ -134,6 +134,12 @@ LIST_TYPE_DECLARE(listener_item, char *http_request; int http_state; int fd; gnutls_session tls_session; int handshake_ok;); +static const char *safe_strerror(int value) +{ const char *ret = gnutls_strerror(value); + if (ret == NULL) ret = str_unknown; + return ret; +} + static void listener_free(listener_item * j) { if (j->http_request) @@ -213,7 +219,7 @@ static void read_dh_params(void) size = gnutls_dh_params_import_pkcs3( dh_params, ¶ms, GNUTLS_X509_FMT_PEM); if (size < 0) { - fprintf(stderr, "Error parsing dh params: %s\n", gnutls_strerror(size)); + fprintf(stderr, "Error parsing dh params: %s\n", safe_strerror(size)); exit(1); } @@ -385,6 +391,7 @@ char *peer_print_info(gnutls_session session, int *ret_length, strcat(http_buffer, "

\n"); tmp = gnutls_protocol_get_name(gnutls_protocol_get_version(session)); + if (tmp == NULL) tmp = str_unknown; sprintf(tmp2, "\n", tmp); @@ -393,25 +400,31 @@ char *peer_print_info(gnutls_session session, int *ret_length, tmp = gnutls_certificate_type_get_name(gnutls_certificate_type_get (session)); + if (tmp == NULL) tmp = str_unknown; sprintf(tmp2, "\n", tmp); } tmp = gnutls_kx_get_name(kx_alg); + if (tmp == NULL) tmp = str_unknown; sprintf(tmp2, "\n", tmp); tmp = gnutls_compression_get_name(gnutls_compression_get(session)); + if (tmp == NULL) tmp = str_unknown; sprintf(tmp2, "\n", tmp); tmp = gnutls_cipher_get_name(gnutls_cipher_get(session)); + if (tmp == NULL) tmp = str_unknown; sprintf(tmp2, "\n", tmp); tmp = gnutls_mac_get_name(gnutls_mac_get(session)); + if (tmp == NULL) tmp = str_unknown; sprintf(tmp2, "\n", tmp); tmp = gnutls_cipher_suite_get_name(kx_alg, gnutls_cipher_get(session), gnutls_mac_get(session)); + if (tmp == NULL) tmp = str_unknown; sprintf(tmp2, "

Protocol version:%s
Certificate Type:%s
Key Exchange:%s
Compression%s
Cipher%s
MAC%s
Ciphersuite%s
\n", tmp); @@ -537,8 +550,8 @@ int main(int argc, char **argv) signal(SIGPIPE, SIG_IGN); signal(SIGHUP, SIG_IGN); signal(SIGTERM, terminate); - signal(SIGINT, terminate); - /* CHECKME: background processes shouldn't handle SIGINT! */ + if (signal(SIGINT, terminate) == SIG_IGN) + signal(SIGINT, SIG_IGN); /* e.g. background process */ gaa_parser(argc, argv); @@ -1108,7 +1121,7 @@ recv_openpgp_key(gnutls_session session, const unsigned char *keyfpr, unsigned int keyfpr_length, gnutls_datum * key) { static const char hostname[] = "hkp://wwwkeys.pgp.net"; - static const short port = 11371; + static const int port = 11371; int rc; CDK_KBNODE knode = NULL; unsigned int i;