From patchwork Thu Mar 31 17:22:19 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jeff Law X-Patchwork-Id: 604223 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3qbWYC6ykLz9sD1 for ; Fri, 1 Apr 2016 04:22:38 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=BO86euFC; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type; q=dns; s=default; b=xGRdoySCgEStgbMxA VTwCRtkTesvDex0q7A5dbyu2A18gxxv9DEjnyxS8JVyV0XkSo7+MFQA1FGVtxuSG 5Nv34sYegJh+hWCdgK3+tow0ENKWqkfJBff13dZnWM9gOdaK+PmWjrdIGsDht3/u dCIwlLUdBlir9l4e3VKBdzxkTo= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type; s=default; bh=NCvuQlSN+nPFFJOQyjfeozD +92s=; b=BO86euFCaD2MvhDLFdc9ysb2+ruKFjW6rG4j3H46PAJRMiY2dZquMbQ zyKmLjx8uXljbKxJXAYkmrjoL9FIwxk3aayg5PgTmpUOAu9nDYntthedr88E7f3B iyRW8D6HG0rS0f3RiCOIJVX8iCZbTWYrs/Q53W64wQaetd3vh4YM= Received: (qmail 124691 invoked by alias); 31 Mar 2016 17:22:31 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 124677 invoked by uid 89); 31 Mar 2016 17:22:31 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=1.0 required=5.0 tests=BAYES_20, RP_MATCHES_RCVD, SPF_HELO_PASS, UNSUBSCRIBE_BODY autolearn=no version=3.3.2 spammy=doubled, useafterfree, use-after-free, reallocation X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Thu, 31 Mar 2016 17:22:21 +0000 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0EF888E743; Thu, 31 Mar 2016 17:22:20 +0000 (UTC) Received: from slagheap.utah.redhat.com (ovpn-113-72.phx2.redhat.com [10.3.113.72]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u2VHMJE0006943; Thu, 31 Mar 2016 13:22:19 -0400 Subject: Re: Fix for PR70481 Libiberty Demangler To: =?UTF-8?Q?Marcel_B=c3=b6hme?= , gcc-patches@gcc.gnu.org References: From: Jeff Law Message-ID: <56FD5CCB.5000002@redhat.com> Date: Thu, 31 Mar 2016 11:22:19 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.7.1 MIME-Version: 1.0 In-Reply-To: X-IsSubscribed: yes On 03/31/2016 08:55 AM, Marcel Böhme wrote: > Hi, > > This fixes the use-after-free detailed in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70481 > > There is a variable ksize storing the amount of allocated memory for the array ktypevec. ksize being zero (0) indicates that some memory must be allocated upon the first write. When more memory is needed, both ksize and the memory are doubled during reallocation. At some point the memory for the array is freed (in squangle_mop_up) but the value of ksize remains. Since ksize is not 0, there is no indication that new memory must be allocated when there is another write to the array. > > The same holds for bsize and btypevec. > > Best regards, > - Marcel > > > Index: libiberty/cplus-dem.c > =================================================================== > --- libiberty/cplus-dem.c (revision 234607) > +++ libiberty/cplus-dem.c (working copy) > @@ -1237,11 +1237,13 @@ squangle_mop_up (struct work_stuff *work) Thanks. I've just installed this patch, along with suitable tests from 70481 and 67394. Jeff commit 6e4dbbe6a1d6dd21e2ae87cabd1eae81312feac2 Author: law Date: Thu Mar 31 17:20:53 2016 +0000 * cplus-dem.c (squangle_mop_up): Zero bsize/ksize after freeing btypevec/ktypevec. * testsuite/demangle-expected: Add coverage tests. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@234645 138bc75d-0d04-0410-961f-82ee72b054a4 diff --git a/libiberty/ChangeLog b/libiberty/ChangeLog index d11f4ce..b4054bd 100644 --- a/libiberty/ChangeLog +++ b/libiberty/ChangeLog @@ -1,3 +1,10 @@ +2016-03-31 Mikhail Maltsev + Marcel Bohme boehme.marcel@gmail.com + + * cplus-dem.c (squangle_mop_up): Zero bsize/ksize after freeing + btypevec/ktypevec. + * testsuite/demangle-expected: Add coverage tests. + 2016-01-27 Iain Buclaw * d-demangle.c (dlang_call_convention): Handle extern Objective-C diff --git a/libiberty/cplus-dem.c b/libiberty/cplus-dem.c index c68b981..7ab46dd 100644 --- a/libiberty/cplus-dem.c +++ b/libiberty/cplus-dem.c @@ -1237,11 +1237,13 @@ squangle_mop_up (struct work_stuff *work) { free ((char *) work -> btypevec); work->btypevec = NULL; + work->bsize = 0; } if (work -> ktypevec != NULL) { free ((char *) work -> ktypevec); work->ktypevec = NULL; + work->ksize = 0; } } diff --git a/libiberty/testsuite/demangle-expected b/libiberty/testsuite/demangle-expected index aebf01b..e214ee5 100644 --- a/libiberty/testsuite/demangle-expected +++ b/libiberty/testsuite/demangle-expected @@ -4421,3 +4421,13 @@ void baz(A*) --format=gnu-v3 _Z3fooI1FEN1XIXszdtcl1PclcvT__EEE5arrayEE4TypeEv X::Type foo() +# +# Tests a use-after-free problem + +_Q.__0 +::Q.(void) +# +# Tests a use-after-free problem + +_Q10-__9cafebabe. +cafebabe.::-(void)