From nobody@FreeBSD.org  Wed Mar 15 15:31:42 2006
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id B3B8516A41F
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 15 Mar 2006 15:31:42 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [216.136.204.117])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 8023243D45
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 15 Mar 2006 15:31:42 +0000 (GMT)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.13.1/8.13.1) with ESMTP id k2FFVgXc084190
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 15 Mar 2006 15:31:42 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.13.1/8.13.1/Submit) id k2FFVgYK084178;
	Wed, 15 Mar 2006 15:31:42 GMT
	(envelope-from nobody)
Message-Id: <200603151531.k2FFVgYK084178@www.freebsd.org>
Date: Wed, 15 Mar 2006 15:31:42 GMT
From: Michiel Pelt <m.pelt@xs4all.nl>
To: freebsd-gnats-submit@FreeBSD.org
Subject: bread & bwrite can crash under low memory conditions
X-Send-Pr-Version: www-2.3

>Number:         94480
>Category:       kern
>Synopsis:       [libufs] [patch] bread(3) & bwrite(3) can crash under low memory conditions
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    delphij
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Mar 15 15:40:18 GMT 2006
>Closed-Date:    Thu May 28 21:20:00 UTC 2009
>Last-Modified:  Thu May 28 21:20:03 UTC 2009
>Originator:     Michiel Pelt
>Release:        6.0
>Organization:
Peltin BV
>Environment:
>Description:
I was just examining the kernel sources for the development plans I have
and stumbled upon lib/libufs/block.c rev 1.10. The following code is
incorrect :

	if (((intptr_t)data) & 0x3f) {
		p2 = malloc(size);
		if (p2 == NULL)
			ERROR(disk, "allocate bounce buffer");
	}
	cnt = pread(disk->d_fd, p2, size, (off_t)(blockno * disk->d_bsize));

If the malloc fails, pread will be called with the NULL pointer p2 with
serious consequences. Same problem with the bwrite function:

	if (((intptr_t)data) & 0x3f) {
		p2 = malloc(size);
		if (p2 == NULL)
			ERROR(disk, "allocate bounce buffer");
		memcpy(p2, data, size);
		data = p2;
	}
	cnt = pwrite(disk->d_fd, data, size, (off_t)(blockno * disk->d_bsize));

>How-To-Repeat:
call bread, bwrite with a very large unaligned buffer ...
>Fix:
	if (((intptr_t)data) & 0x3f) {
		p2 = malloc(size);
		if (p2 == NULL) {
			ERROR(disk, "allocate bounce buffer");
                        goto fail;
                }
	}
	cnt = pread(disk->d_fd, p2, size, (off_t)(blockno * disk->d_bsize));


..


	if (((intptr_t)data) & 0x3f) {
		p2 = malloc(size);
		if (p2 == NULL) {
			ERROR(disk, "allocate bounce buffer");
                        return (-1);
                }
		memcpy(p2, data, size);
		data = p2;
	}
	cnt = pwrite(disk->d_fd, data, size, (off_t)(blockno * disk->d_bsize));
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->freebsd-fs 
Responsible-Changed-By: brucec 
Responsible-Changed-When: Wed Mar 25 17:30:41 UTC 2009 
Responsible-Changed-Why:  
Over to maintainer(s). 

http://www.freebsd.org/cgi/query-pr.cgi?pr=94480 
State-Changed-From-To: open->patched 
State-Changed-By: delphij 
State-Changed-When: Thu Apr 2 17:19:12 UTC 2009 
State-Changed-Why:  
A fix has been applied against -HEAD, MFC reminder. 


Responsible-Changed-From-To: freebsd-fs->delphij 
Responsible-Changed-By: delphij 
Responsible-Changed-When: Thu Apr 2 17:19:12 UTC 2009 
Responsible-Changed-Why:  
Take. 

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

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/94480: commit references a PR
Date: Thu,  2 Apr 2009 17:16:56 +0000 (UTC)

 Author: delphij
 Date: Thu Apr  2 17:16:39 2009
 New Revision: 190646
 URL: http://svn.freebsd.org/changeset/base/190646
 
 Log:
   Bail out when memory allocation is failed, rather than referencing
   a NULL pointer.
   
   PR:		kern/94480
   Submitted by:	Michiel Pelt <m.pelt xs4all nl>
 
 Modified:
   head/lib/libufs/block.c
 
 Modified: head/lib/libufs/block.c
 ==============================================================================
 --- head/lib/libufs/block.c	Thu Apr  2 17:15:49 2009	(r190645)
 +++ head/lib/libufs/block.c	Thu Apr  2 17:16:39 2009	(r190646)
 @@ -64,8 +64,10 @@ bread(struct uufsd *disk, ufs2_daddr_t b
  	 */
  	if (((intptr_t)data) & 0x3f) {
  		p2 = malloc(size);
 -		if (p2 == NULL)
 +		if (p2 == NULL) {
  			ERROR(disk, "allocate bounce buffer");
 +			goto fail;
 +		}
  	}
  	cnt = pread(disk->d_fd, p2, size, (off_t)(blockno * disk->d_bsize));
  	if (cnt == -1) {
 @@ -115,8 +117,10 @@ bwrite(struct uufsd *disk, ufs2_daddr_t 
  	 */
  	if (((intptr_t)data) & 0x3f) {
  		p2 = malloc(size);
 -		if (p2 == NULL)
 +		if (p2 == NULL) {
  			ERROR(disk, "allocate bounce buffer");
 +			return (-1);
 +		}
  		memcpy(p2, data, size);
  		data = p2;
  	}
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: patched->closed 
State-Changed-By: delphij 
State-Changed-When: Thu May 28 21:19:32 UTC 2009 
State-Changed-Why:  
Patch applied against -CURRENT, stable/7 and stable/6, thanks 
for your submission! 

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

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/94480: commit references a PR
Date: Thu, 28 May 2009 21:17:37 +0000 (UTC)

 Author: delphij
 Date: Thu May 28 21:17:27 2009
 New Revision: 192995
 URL: http://svn.freebsd.org/changeset/base/192995
 
 Log:
   Merge r190646:
   
   Bail out when memory allocation is failed, rather than referencing
   a NULL pointer.
   
   PR: kern/94480
   Submitted by: Michiel Pelt <m.pelt xs4all nl>
 
 Modified:
   stable/7/lib/libufs/   (props changed)
   stable/7/lib/libufs/block.c
 
 Modified: stable/7/lib/libufs/block.c
 ==============================================================================
 --- stable/7/lib/libufs/block.c	Thu May 28 21:12:43 2009	(r192994)
 +++ stable/7/lib/libufs/block.c	Thu May 28 21:17:27 2009	(r192995)
 @@ -63,8 +63,10 @@ bread(struct uufsd *disk, ufs2_daddr_t b
  	 */
  	if (((intptr_t)data) & 0x3f) {
  		p2 = malloc(size);
 -		if (p2 == NULL)
 +		if (p2 == NULL) {
  			ERROR(disk, "allocate bounce buffer");
 +			goto fail;
 +		}
  	}
  	cnt = pread(disk->d_fd, p2, size, (off_t)(blockno * disk->d_bsize));
  	if (cnt == -1) {
 @@ -114,8 +116,10 @@ bwrite(struct uufsd *disk, ufs2_daddr_t 
  	 */
  	if (((intptr_t)data) & 0x3f) {
  		p2 = malloc(size);
 -		if (p2 == NULL)
 +		if (p2 == NULL) {
  			ERROR(disk, "allocate bounce buffer");
 +			return (-1);
 +		}
  		memcpy(p2, data, size);
  		data = p2;
  	}
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/94480: commit references a PR
Date: Thu, 28 May 2009 21:19:32 +0000 (UTC)

 Author: delphij
 Date: Thu May 28 21:19:21 2009
 New Revision: 192996
 URL: http://svn.freebsd.org/changeset/base/192996
 
 Log:
   Merge r190646:
   
   Bail out when memory allocation is failed, rather than referencing
   a NULL pointer.
   
   PR: kern/94480
   Submitted by: Michiel Pelt <m.pelt xs4all nl>
 
 Modified:
   stable/6/lib/libufs/   (props changed)
   stable/6/lib/libufs/block.c
 
 Modified: stable/6/lib/libufs/block.c
 ==============================================================================
 --- stable/6/lib/libufs/block.c	Thu May 28 21:17:27 2009	(r192995)
 +++ stable/6/lib/libufs/block.c	Thu May 28 21:19:21 2009	(r192996)
 @@ -63,8 +63,10 @@ bread(struct uufsd *disk, ufs2_daddr_t b
  	 */
  	if (((intptr_t)data) & 0x3f) {
  		p2 = malloc(size);
 -		if (p2 == NULL)
 +		if (p2 == NULL) {
  			ERROR(disk, "allocate bounce buffer");
 +			goto fail;
 +		}
  	}
  	cnt = pread(disk->d_fd, p2, size, (off_t)(blockno * disk->d_bsize));
  	if (cnt == -1) {
 @@ -114,8 +116,10 @@ bwrite(struct uufsd *disk, ufs2_daddr_t 
  	 */
  	if (((intptr_t)data) & 0x3f) {
  		p2 = malloc(size);
 -		if (p2 == NULL)
 +		if (p2 == NULL) {
  			ERROR(disk, "allocate bounce buffer");
 +			return (-1);
 +		}
  		memcpy(p2, data, size);
  		data = p2;
  	}
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
>Unformatted:
