From owner-freebsd-bugs@FreeBSD.ORG Fri Dec 18 16:50:04 2009 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 894CA106568D for ; Fri, 18 Dec 2009 16:50:04 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id 62DF38FC1D for ; Fri, 18 Dec 2009 16:50:04 +0000 (UTC) Received: from freefall.freebsd.org (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.3/8.14.3) with ESMTP id nBIGo4TR005267 for ; Fri, 18 Dec 2009 16:50:04 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.3/8.14.3/Submit) id nBIGo4jR005266; Fri, 18 Dec 2009 16:50:04 GMT (envelope-from gnats) Resent-Date: Fri, 18 Dec 2009 16:50:04 GMT Resent-Message-Id: <200912181650.nBIGo4jR005266@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Dan Lukes Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 1BC2B106566B for ; Fri, 18 Dec 2009 16:40:57 +0000 (UTC) (envelope-from dan@kulesh.obluda.cz) Received: from kulesh.obluda.cz (kgw.obluda.cz [193.179.199.50]) by mx1.freebsd.org (Postfix) with ESMTP id 94A2C8FC0A for ; Fri, 18 Dec 2009 16:40:55 +0000 (UTC) Received: from kulesh.obluda.cz (localhost. [127.0.0.1]) by kulesh.obluda.cz (8.14.3/8.14.3) with ESMTP id nBIGersQ001356 for ; Fri, 18 Dec 2009 17:40:53 +0100 (CET) (envelope-from dan@kulesh.obluda.cz) Received: (from root@localhost) by kulesh.obluda.cz (8.14.3/8.14.3/Submit) id nBIGerCW001355; Fri, 18 Dec 2009 17:40:53 +0100 (CET) (envelope-from dan) Message-Id: <200912181640.nBIGerCW001355@kulesh.obluda.cz> Date: Fri, 18 Dec 2009 17:40:53 +0100 (CET) From: Dan Lukes To: FreeBSD-gnats-submit@FreeBSD.org X-Send-Pr-Version: 3.113 Cc: Subject: bin/141753: double-free in reallocf() X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: Dan Lukes List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 18 Dec 2009 16:50:04 -0000 >Number: 141753 >Category: bin >Synopsis: double-free in reallocf() >Confidential: no >Severity: non-critical >Priority: medium >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: sw-bug >Submitter-Id: current-users >Arrival-Date: Fri Dec 18 16:50:03 UTC 2009 >Closed-Date: >Last-Modified: >Originator: Dan Lukes >Release: FreeBSD 7.2-STABLE i386 >Organization: Obludarium >Environment: System: FreeBSD 7.2-STABLE i386 lib/libc/stdlib/reallocf.c,v 1.4 2002/03/22 21:53:10 ******** SYS V malloc() compatifility (malloc option 'V' in effect) >Description: Imagine the code: ----------------- _malloc_options = "V"; ... ptr=malloc(5); ... nptr=reallocf(ptr,0); ----------------- Now look into libc's reallocf() implementation: void * reallocf(void *ptr, size_t size) { void *nptr; nptr = realloc(ptr, size); if (!nptr && ptr) free(ptr); return (nptr); } The realloc() is called with non-NULL ptr. Zero-size realloc never fail, so ptr is freed by realloc. nptr is NULL because of size=0 and option V Unfortunatelly, it mean the free(ptr) is called again causing double-free of ptr. It never fail (allocation of >How-To-Repeat: See code in description. >Fix: The free must not be called when size=0 and opt_sysv == true because the pointer is already freed. Unfortunatelly the opt_sysv variable is not avaiable here, it is static variable within malloc.c It sounds to me that better solution is to move reallocf() implementation from reallocf.c to malloc.c (opt_sysv is avaiable here) but there may be other solution. >Release-Note: >Audit-Trail: >Unformatted: