From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.bugs Subject: bug#31946: 27.0.50; The NSM should warn about more TLS problems Date: Tue, 27 Aug 2019 10:23:55 +0300 Message-ID: <83tva3dqb8.fsf@gnu.org> References: <87h8647de5.fsf@gnus.org> <86tva4dq8h.fsf@gmail.com> <83k1azgbbi.fsf@gnu.org> <86zhjvrfbp.fsf@gmail.com> <83blwbg5i2.fsf@gnu.org> <86tva3rdpv.fsf@gmail.com> <834l23g1mg.fsf@gnu.org> <86d0grfse1.fsf@gmail.com> Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="59265"; mail-complaints-to="usenet@blaine.gmane.org" Cc: 31946@debbugs.gnu.org To: Andy Moreton Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Tue Aug 27 09:25:13 2019 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1i2Vqq-000dtr-A2 for geb-bug-gnu-emacs@m.gmane.org; Tue, 27 Aug 2019 09:25:12 +0200 Original-Received: from localhost ([::1]:47598 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i2Vqo-0003k4-R5 for geb-bug-gnu-emacs@m.gmane.org; Tue, 27 Aug 2019 03:25:10 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:56290) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i2Vqi-0003jt-OE for bug-gnu-emacs@gnu.org; Tue, 27 Aug 2019 03:25:06 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i2Vqh-0007lB-8N for bug-gnu-emacs@gnu.org; Tue, 27 Aug 2019 03:25:04 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:38764) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1i2Vqh-0007l3-4F for bug-gnu-emacs@gnu.org; Tue, 27 Aug 2019 03:25:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1i2Vqg-0002KK-Tf for bug-gnu-emacs@gnu.org; Tue, 27 Aug 2019 03:25:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Eli Zaretskii Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 27 Aug 2019 07:25:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 31946 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: security fixed Original-Received: via spool by 31946-submit@debbugs.gnu.org id=B31946.15668906418861 (code B ref 31946); Tue, 27 Aug 2019 07:25:02 +0000 Original-Received: (at 31946) by debbugs.gnu.org; 27 Aug 2019 07:24:01 +0000 Original-Received: from localhost ([127.0.0.1]:47585 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1i2Vph-0002In-3S for submit@debbugs.gnu.org; Tue, 27 Aug 2019 03:24:01 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:47648) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1i2Vpf-0002IX-Ai for 31946@debbugs.gnu.org; Tue, 27 Aug 2019 03:23:59 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]:53885) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1i2Vpa-0007HT-6Z; Tue, 27 Aug 2019 03:23:54 -0400 Original-Received: from [176.228.60.248] (port=3367 helo=home-c4e4a596f7) by fencepost.gnu.org with esmtpsa (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1i2VpZ-0007Wr-LZ; Tue, 27 Aug 2019 03:23:54 -0400 In-reply-to: <86d0grfse1.fsf@gmail.com> (message from Andy Moreton on Mon, 26 Aug 2019 23:56:06 +0100) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:173343 > From: Andy Moreton > Date: Mon, 26 Aug 2019 23:56:06 +0100 > > > Looks like something is wrong with how I'm using gnutls_free when it > > comes from the DLL. I need to look into that. > > Found it I think. gnutls_free is a pointer of type gnutls_free_function, > so the value returned from GetProcAddress is a data pointer, not a > function address. Something like the following patch appears to work > i.e. no longer crashes with the recipe above. Yes, thanks. I was struggling with the same thing, but evidently there's a limit to what I can do after 11PM at my age ;-) > If this is the right fix then we will need to add e.g. DEF_DLL_DATA and > LOAD_DLL_DATA macros for runtime import of data (and an equivalent to > get_proc_addr to use the right types for runtime data imports). I think there's no need to invent harness for a single user, we can simply do that by hand. Please try the patch below, it's against the current master (it also removes the unnecessary intermediate buffer allocation while at that). > The patch also has "#undef gnutls_free" because gnutls.h also defines > gnutls_free as a macro, if GNUTLS_INTERNAL_BUILD is defined. For some > reason the MSYS2 maintainers add a patch to their build to define > GNUTLS_INTERNAL_BUILD unconditionally: see 0001-add-missing-define.patch > at . I guess they want for some reason not to have the exported functions decorated with __declspec(dllimport). I have no idea why, maybe for reasons of function visibility? It's worth asking them, I think, because this kind of kludge can be a nuisance. diff --git a/src/gnutls.c b/src/gnutls.c index 67d1fb9..fb75eb9 100644 --- a/src/gnutls.c +++ b/src/gnutls.c @@ -140,7 +140,6 @@ DEF_DLL_FN (void, gnutls_dh_set_prime_bits, DEF_DLL_FN (int, gnutls_dh_get_prime_bits, (gnutls_session_t)); DEF_DLL_FN (int, gnutls_error_is_fatal, (int)); DEF_DLL_FN (int, gnutls_global_init, (void)); -DEF_DLL_FN (void, gnutls_free, (void *)); DEF_DLL_FN (void, gnutls_global_set_log_function, (gnutls_log_func)); # ifdef HAVE_GNUTLS3 DEF_DLL_FN (void, gnutls_global_set_audit_log_function, (gnutls_audit_log_func)); @@ -291,6 +290,7 @@ DEF_DLL_FN (const char *, gnutls_ext_get_name, (unsigned int)); # endif # endif /* HAVE_GNUTLS3 */ +static gnutls_free_function *gnutls_free_func; static bool init_gnutls_functions (void) @@ -327,7 +327,6 @@ init_gnutls_functions (void) LOAD_DLL_FN (library, gnutls_dh_get_prime_bits); LOAD_DLL_FN (library, gnutls_error_is_fatal); LOAD_DLL_FN (library, gnutls_global_init); - LOAD_DLL_FN (library, gnutls_free); LOAD_DLL_FN (library, gnutls_global_set_log_function); # ifdef HAVE_GNUTLS3 LOAD_DLL_FN (library, gnutls_global_set_audit_log_function); @@ -430,6 +429,13 @@ init_gnutls_functions (void) # endif # endif /* HAVE_GNUTLS3 */ + /* gnutls_free is a variable inside GnuTLS, whose value is the + "free" function. So it needs special handling. */ + gnutls_free_func = (gnutls_free_function *) GetProcAddress (library, + "gnutls_free"); + if (!gnutls_free_func) + return false; + max_log_level = clip_to_bounds (INT_MIN, global_gnutls_log_level, INT_MAX); { Lisp_Object name = CAR_SAFE (Fget (Qgnutls, QCloaded_from)); @@ -465,7 +471,6 @@ init_gnutls_functions (void) # define gnutls_global_init fn_gnutls_global_init # define gnutls_global_set_audit_log_function fn_gnutls_global_set_audit_log_function # define gnutls_global_set_log_function fn_gnutls_global_set_log_function -# define gnutls_free fn_gnutls_free # define gnutls_global_set_log_level fn_gnutls_global_set_log_level # define gnutls_handshake fn_gnutls_handshake # define gnutls_init fn_gnutls_init @@ -562,6 +567,11 @@ init_gnutls_functions (void) # endif # endif /* HAVE_GNUTLS3 */ +/* gnutls_free is a data pointer to a variable which holds a pointer + to the function. We use #undef because MinGW64 defines gnutls_free + as a macro as well in the GnuTLS headers. */ +# undef gnutls_free +# define gnutls_free (*gnutls_free_func) /* This wrapper is called from fns.c, which doesn't know about the LOAD_DLL_FN stuff above. */ @@ -1612,16 +1622,10 @@ DEFUN ("gnutls-format-certificate", Fgnutls_format_certificate, emacs_gnutls_strerror (err)); } - char *out_buf = xmalloc ((out.size + 1) * sizeof (char)); - memset (out_buf, 0, (out.size + 1) * sizeof (char)); - memcpy (out_buf, out.data, out.size); - + Lisp_Object result = make_string_from_bytes (out.data, out.size, out.size); gnutls_free (out.data); gnutls_x509_crt_deinit (crt); - Lisp_Object result = build_string (out_buf); - xfree (out_buf); - return result; }