From nobody@FreeBSD.org  Tue Nov 27 12:20:17 2012
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52])
	by hub.freebsd.org (Postfix) with ESMTP id EC6512E4
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 27 Nov 2012 12:20:17 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from red.freebsd.org (red.freebsd.org [IPv6:2001:4f8:fff6::22])
	by mx1.freebsd.org (Postfix) with ESMTP id D27148FC12
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 27 Nov 2012 12:20:17 +0000 (UTC)
Received: from red.freebsd.org (localhost [127.0.0.1])
	by red.freebsd.org (8.14.5/8.14.5) with ESMTP id qARCKHYC074772
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 27 Nov 2012 12:20:17 GMT
	(envelope-from nobody@red.freebsd.org)
Received: (from nobody@localhost)
	by red.freebsd.org (8.14.5/8.14.5/Submit) id qARCKHNt074770;
	Tue, 27 Nov 2012 12:20:17 GMT
	(envelope-from nobody)
Message-Id: <201211271220.qARCKHNt074770@red.freebsd.org>
Date: Tue, 27 Nov 2012 12:20:17 GMT
From: Jan Mikkelsen <janm@transactionware.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: BN_add_word writes one past its buffer when certain values are added to a single word bignum
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         173952
>Category:       kern
>Synopsis:       [openssl] [patch] BN_add_word writes one past its buffer when certain values are added to a single word bignum
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    delphij
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Nov 27 12:30:00 UTC 2012
>Closed-Date:    Wed Jan 23 15:17:45 UTC 2013
>Last-Modified:  Wed Jan 23 15:17:45 UTC 2013
>Originator:     Jan Mikkelsen
>Release:        9.1-RC3
>Organization:
Transactionware
>Environment:
FreeBSD vanaheim.transactionware.com 9.1-RC3 FreeBSD 9.1-RC3 #0: Sun Nov 25 10:40:45 EST 2012     root@vanaheim.transactionware.com:/home/janm/p4/freebsd-image-std-2011.1/work/base-freebsd/home/janm/p4/freebsd-image-std-2011.1/FreeBSD/src/sys/TW  amd64
>Description:
The function BN_add_word() in crypto/bn/bn_word.c can write one word past the end of the memory pointed to by the 'd' member of the BIGNUM struct when adding a word to a single word bignum.

This has been reported to the OpenSSL people.
>How-To-Repeat:
dc(1) is implemented in terms of BN_*. This will reproduce the problem:

janm@gray: dc $ dc
18446744073709551616 18446744073709551616 / ps
dc: big number failure 306b06b: No such file or directory

>Fix:
My patch below. The OpenSSL people have a different patch; they were more comfortable restructuring the code.


--- //depot/vendor/freebsd/9.1-local/src/crypto/openssl/crypto/bn/bn_word.c	2012-08-13 00:32:35.000000000 1000
+++ /data/scratch/janm/p4/freebsd-image-std-2011.1/FreeBSD/src/crypto/openssl/crypto/bn/bn_word.c	2012-08-13 00:32:35.000000000 1000
@@ -145,9 +145,11 @@
		return(i);
		}
	/* Only expand (and risk failing) if it's possibly necessary */
-	if (((BN_ULONG)(a->d[a->top - 1] + 1) == 0) &&
-			(bn_wexpand(a,a->top+1) == NULL))
-		return(0);
+	if (
+		(((a->top == 1) && (BN_MASK2 - w < a->d[0])) ||
+		 ((a->top > 1) && ((BN_ULONG)(a->d[a->top - 1] + 1) == 0))) &&
+		(bn_wexpand(a,a->top+1) == NULL))
+			return(0);
	i=0;
	for (;;)
		{


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->delphij 
Responsible-Changed-By: delphij 
Responsible-Changed-When: Mon Dec 17 21:00:28 UTC 2012 
Responsible-Changed-Why:  
Take. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=173952 
State-Changed-From-To: open->closed 
State-Changed-By: delphij 
State-Changed-When: Wed Jan 23 15:17:11 UTC 2013 
State-Changed-Why:  
Patch applied to -HEAD and 8,9-STABLE branches.  Thanks for 
your submission! 

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