From jwestfall@snapper.surrealistic.net  Mon Apr 21 00:33:38 2003
Return-Path: <jwestfall@snapper.surrealistic.net>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 9955037B401
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 21 Apr 2003 00:33:38 -0700 (PDT)
Received: from snapper.surrealistic.net (snapper.surrealistic.net [63.224.43.60])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 955D443F93
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 21 Apr 2003 00:33:37 -0700 (PDT)
	(envelope-from jwestfall@snapper.surrealistic.net)
Received: from snapper.surrealistic.net (jwestfall@localhost [127.0.0.1])
	by snapper.surrealistic.net (8.12.3p2/8.12.3) with ESMTP id h3L7XZgP035671
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 21 Apr 2003 00:33:36 -0700 (PDT)
	(envelope-from jwestfall@snapper.surrealistic.net)
Received: (from jwestfall@localhost)
	by snapper.surrealistic.net (8.12.3p2/8.12.3/Submit) id h3L7XY6Z035670;
	Mon, 21 Apr 2003 00:33:34 -0700 (PDT)
Message-Id: <200304210733.h3L7XY6Z035670@snapper.surrealistic.net>
Date: Mon, 21 Apr 2003 00:33:34 -0700 (PDT)
From: Jim Westfall <jwestfall@surrealistic.net>
Reply-To: Jim Westfall <jwestfall@surrealistic.net>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: openssl in base system is not compiled thread safe
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         51205
>Category:       bin
>Synopsis:       [patch] openssl(1) in base system is not compiled thread safe
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    dwmalone
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          update
>Submitter-Id:   current-users
>Arrival-Date:   Mon Apr 21 00:40:18 PDT 2003
>Closed-Date:    Wed Oct 22 15:17:24 UTC 2008
>Last-Modified:  Wed Oct 22 15:17:24 UTC 2008
>Originator:     Jim Westfall
>Release:        FreeBSD 4.6.2-RELEASE-p13 i386
>Organization:
n/a
>Environment:
System: FreeBSD snapper.surrealistic.net 4.6.2-RELEASE-p13 FreeBSD 4.6.2-RELEASE-p13 #10: Sat Mar 29 13:46:35 PST 2003 root@snapper.surrealistic.net:/usr/obj/usr/src/sys/SMP_46 i386
FreeBSD ultrax.surrealistic.net 4.8-RELEASE FreeBSD 4.8-RELEASE #2: Mon Apr  7 16:01:24 PDT 2003 root@ultrax.surrealistic.net:/usr/obj/usr/src/sys/UP_48  i386

>Description:
openssl thats included in the base system is not being compiled with the necessary CFLAGS to make 
it thread safe.  Because of this its possible to cause an assertion failure within libcrypto if multiple threads 
are accessing RAND_add() function call at the same time.  RAND_add() is called by both SSL_connect() and 
SSL_accept() functions.  So if multiple threads are calling either of these function it can trigger the 
assertion.  I can easily reproduce the assertion with an threaded ftp client i have been working on.  It can 
also be tiggerred with a small mod to mttest.c, which is the thread test program thats include in the openssl 
sources.
>How-To-Repeat:
it can be a bit tricky to reproduce the error, but the following patch applied to mttest.c adds
a RAND_add() call to stress the function in hopes of causing the assertion.  mttest.c is located at
/usr/src/crypto/openssl/crypto/threads/mttest.c

--- mttest.old.c        Sun Apr 20 22:36:45 2003
+++ mttest.c    Mon Apr 21 00:07:02 2003
@@ -354,6 +354,7 @@
        ctx[0]=(char *)ssl_ctx[0];
        ctx[1]=(char *)ssl_ctx[1];
 
+       usleep(1000);
        if (reconnect)
                {
                ctx[2]=(char *)SSL_new(ssl_ctx[0]);
@@ -379,7 +380,7 @@
                        {
                        fprintf(stdout,"error[%d] %lu - %d\n",
                                i,CRYPTO_thread_id(),ret);
-                       return(ret);
+                       //return(ret);
                        }
                }
        fprintf(stdout,"DONE %lu\n",CRYPTO_thread_id());
@@ -407,6 +408,7 @@
        int done=0;
        int c_write,s_write;
        int do_server=0,do_client=0;
+       time_t Time = time(NULL);
 
        s_ctx=(SSL_CTX *)ctx[0];
        c_ctx=(SSL_CTX *)ctx[1];
@@ -448,6 +450,7 @@
        /* We can always do writes */
        for (;;)
                {
+               RAND_add(&Time, sizeof(Time), 0);
                do_server=0;
                do_client=0;
 

[root@ultrax]# patch -p0 < mttest.patch 
Hmm...  Looks like a unified diff to me...
The text leading up to this was:
--------------------------
|--- mttest.old.c       Sun Apr 20 22:36:45 2003
|+++ mttest.c   Mon Apr 21 00:07:02 2003
--------------------------
Patching file mttest.c using Plan A...
Hunk #1 succeeded at 354.
Hunk #2 succeeded at 380.
Hunk #3 succeeded at 408.
Hunk #4 succeeded at 450.
done
[root@ultrax]# gcc -DPTHREADS -I../../include -g mttest.c -o mttest -L../.. -lssl -lcrypto -pthread
[root@ultrax]# ./mttest -CAfile /etc/ssl/certs/ftpd.pem -threads 100 -loops 100 -client_auth -ssl3 2>&1 | grep -vi error | grep -v started
client authentication 
assertion "md_c[1] == md_count[1]" failed: file "/usr/src/secure/lib/libcrypto/../../../crypto/openssl/crypto/rand/md_rand.c", line 312

this is the particular assertion in md_rand.c (0.9.7a)

#if !defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32)
        assert(md_c[1] == md_count[1]);
#endif

>Fix:

I believe its just an oversite.  based on /usr/src/secure/lib/libcrypto/opensslconf-i386.h which is 
auto-generated, it shows that openssl has been configured to have thread support.  It just that the necessary 
CFLAGS where not updated in src/secure/lib/libcrypto/Makefile.inc.  It appears its been like this for a while as 
my 4.6.2 machine is effected too.  I havent looked at 5.0 is see if it is as well.

The fix depends on which openssl version is installed.

if 0.9.7x
-DOPENSSL_THREADS -pthread -D_REENTRANT -D_THREAD_SAFE -D_THREADSAFE

if 0.9.6x
-DTHREADS -pthread -D_REENTRANT -D_THREAD_SAFE -D_THREADSAFE
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->nectar 
Responsible-Changed-By: kris 
Responsible-Changed-When: Sat Jul 12 18:28:33 PDT 2003 
Responsible-Changed-Why:  
Assign to OpenSSL maintainer 

http://www.freebsd.org/cgi/query-pr.cgi?pr=51205 

From: Niall Richard Murphy <niallm-web@enigma.ie>
To: freebsd-gnats-submit@FreeBSD.org
Cc: jwestfall@surrealistic.net, dwmalone@maths.tcd.ie
Subject: Re: bin/51205: openssl in base system is not compiled thread safe
Date: Tue, 03 Feb 2004 11:11:22 +0000

 Folks,
 
 I was recently hit by this issue: our local RADIUS daemon was 
 segfaulting with signal 6 aperiodically, and the messages in the logs 
 indicated it was an openssl rand problem.
 
 Based on advice from David Malone, I applied the following patch:
 
 Index: lib/libcrypto/Makefile.inc
 ===================================================================
 RCS file: /cvs/FreeBSD-CVS/src/secure/lib/libcrypto/Makefile.inc,v
 retrieving revision 1.35
 diff -u -r1.35 Makefile.inc
 --- lib/libcrypto/Makefile.inc  27 Feb 2003 23:07:25 -0000      1.35
 +++ lib/libcrypto/Makefile.inc  7 Jan 2004 13:55:30 -0000
 @@ -5,6 +5,7 @@
  
  CFLAGS+=       -DTERMIOS -DANSI_SOURCE
  CFLAGS+=       -I${LCRYPTO_SRC} -I${LCRYPTO_SRC}/crypto -I${.OBJDIR}
 +CFLAGS+=       -DOPENSSL_THREADS -pthread -D_REENTRANT -D_THREAD_SAFE 
 -D_THREADSAFE
  
  .if !defined(MAKE_IDEA) || ${MAKE_IDEA} != YES
  CFLAGS+=       -DOPENSSL_NO_IDEA
 
 and have suffered no crashes since (1 month ago).
 
 Niall

From: Jon Ribbens <jon+freebsdbugs@unequivocal.co.uk>
To: freebsd-gnats-submit@FreeBSD.org, jwestfall@surrealistic.net
Cc:  
Subject: Re: bin/51205: openssl in base system is not compiled thread safe
Date: Sat, 13 Mar 2004 18:09:38 +0000

 I was just hit by this too:
 
 assertion "md_c[1] == md_count[1]" failed: file
 "/usr/src/secure/lib/libcrypto/../../../crypto/openssl/crypto/rand/md_rand.c",
 line 312
 
 It seems trivial to fix, is there any chance of an official patch?
Responsible-Changed-From-To: nectar->dwmalone 
Responsible-Changed-By: dwmalone 
Responsible-Changed-When: Tue Mar 30 03:30:09 PST 2004 
Responsible-Changed-Why:  
I've committed the patch to -current. I will merge the fix to -stable 
in a month, if all goes according to plan. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=51205 

From: Volker <volker@vwsoft.com>
To: bug-followup@FreeBSD.org, dwmalone@FreeBSD.org
Cc:  
Subject: Re: bin/51205: openssl in base system is not compiled thread safe
Date: Sun, 10 Feb 2008 18:25:43 +0100

 Please check if this PR can be closed. According to CVS, changes have
 been incooperated into RELENG_5/6/7 but never made it to RELENG_4.
 
 Thanks!
State-Changed-From-To: open->patched 
State-Changed-By: vwe 
State-Changed-When: Sat May 24 17:46:08 UTC 2008 
State-Changed-Why:  


http://www.freebsd.org/cgi/query-pr.cgi?pr=51205 
State-Changed-From-To: patched->closed 
State-Changed-By: dwmalone 
State-Changed-When: Wed Oct 22 15:16:51 UTC 2008 
State-Changed-Why:  
Should be fixed in all important branches now. 

David. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=51205 
>Unformatted:
