From patchwork Wed Oct 9 13:46:49 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Newton X-Patchwork-Id: 20911 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-vc0-f197.google.com (mail-vc0-f197.google.com [209.85.220.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id A927225E26 for ; Wed, 9 Oct 2013 13:46:55 +0000 (UTC) Received: by mail-vc0-f197.google.com with SMTP id gd11sf1553169vcb.8 for ; Wed, 09 Oct 2013 06:46:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:message-id:date:from:user-agent :mime-version:to:cc:subject:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe:content-type :content-transfer-encoding; bh=00Y8Jw3dcx/pQjBeVRa6g93RtnPihkPmbDGRxId6dDk=; b=PuxJF6Z0Q52hqBxJ8T+c0Nw1s2uscBX2nqfF9Jd+rp940Kpx+ijpWd80XJdutQvjpJ PablPNKOW15o0iweGsDUFJRpuoPloa2bkydD8dzycxAmxk0XVlN86Hh4brMyvGSGp1lE yCMt7cWgRnVQEXfJqKpzse7LU7OPuWz5LnC0UAoNRYY2vL0PVZO70iGbHI9sy1wSpyZx ciCW0Ai0lTWN+rW+S6fdH51lP4b+iPY9NqElKZ6pCypGnwk0wGtqsHu16QD1AI1dtbOX vFYg07anJUW+6pnshNx6Dbv7DH4RPv5sD8IUFP01N+ZWsGAkLPugNPlzyy+35Y1WtxGU 6p6g== X-Received: by 10.236.94.39 with SMTP id m27mr7980572yhf.54.1381326415067; Wed, 09 Oct 2013 06:46:55 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.83.72 with SMTP id o8ls723925qey.0.gmail; Wed, 09 Oct 2013 06:46:54 -0700 (PDT) X-Received: by 10.220.183.199 with SMTP id ch7mr1663417vcb.27.1381326414853; Wed, 09 Oct 2013 06:46:54 -0700 (PDT) Received: from mail-vb0-f46.google.com (mail-vb0-f46.google.com [209.85.212.46]) by mx.google.com with ESMTPS id xt2si11871827vcb.33.1969.12.31.16.00.00 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 09 Oct 2013 06:46:54 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.212.46 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.212.46; Received: by mail-vb0-f46.google.com with SMTP id p13so491284vbe.5 for ; Wed, 09 Oct 2013 06:46:54 -0700 (PDT) X-Gm-Message-State: ALoCoQlWXNPwyraalBdlz9qCHdge1/+WGYIBBmu1+8qdX9OtiXSfU/a1rVLHT46bUKSqFX72bWO1 X-Received: by 10.52.119.198 with SMTP id kw6mr266293vdb.47.1381326414767; Wed, 09 Oct 2013 06:46:54 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.220.174.196 with SMTP id u4csp289393vcz; Wed, 9 Oct 2013 06:46:53 -0700 (PDT) X-Received: by 10.194.11.67 with SMTP id o3mr7245482wjb.0.1381326412654; Wed, 09 Oct 2013 06:46:52 -0700 (PDT) Received: from mail-wi0-f182.google.com (mail-wi0-f182.google.com [209.85.212.182]) by mx.google.com with ESMTPS id ev12si3631425wid.34.1969.12.31.16.00.00 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 09 Oct 2013 06:46:52 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.212.182 is neither permitted nor denied by best guess record for domain of will.newton@linaro.org) client-ip=209.85.212.182; Received: by mail-wi0-f182.google.com with SMTP id ez12so996902wid.3 for ; Wed, 09 Oct 2013 06:46:51 -0700 (PDT) X-Received: by 10.194.109.35 with SMTP id hp3mr2084483wjb.55.1381326411786; Wed, 09 Oct 2013 06:46:51 -0700 (PDT) Received: from localhost.localdomain (cpc6-seac21-2-0-cust453.7-2.cable.virginmedia.com. [82.1.113.198]) by mx.google.com with ESMTPSA id c4sm15535399wiz.0.1969.12.31.16.00.00 (version=TLSv1 cipher=RC4-SHA bits=128/128); Wed, 09 Oct 2013 06:46:50 -0700 (PDT) Message-ID: <52555E49.4050506@linaro.org> Date: Wed, 09 Oct 2013 14:46:49 +0100 From: Will Newton User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130805 Thunderbird/17.0.8 MIME-Version: 1.0 To: libc-alpha@sourceware.org CC: patches@linaro.org Subject: [PATCH] malloc/hooks.c: Correct check for overflow in memalign_check. X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: will.newton@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.212.46 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , A large value of bytes passed to memalign_check can cause an integer overflow in _int_memalign and heap corruption. This issue can be exposed by running tst-memalign with MALLOC_CHECK_=3. ChangeLog: 2013-10-09 Will Newton * malloc/hooks.c (memalign_check): Ensure the value of bytes passed to _int_memalign does not overflow. --- malloc/hooks.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/malloc/hooks.c b/malloc/hooks.c index 8c25846..3f663bb 100644 --- a/malloc/hooks.c +++ b/malloc/hooks.c @@ -361,10 +361,13 @@ memalign_check(size_t alignment, size_t bytes, const void *caller) if (alignment <= MALLOC_ALIGNMENT) return malloc_check(bytes, NULL); if (alignment < MINSIZE) alignment = MINSIZE; - if (bytes+1 == 0) { - __set_errno (ENOMEM); - return NULL; - } + /* Check for overflow. */ + if (bytes > SIZE_MAX - alignment - MINSIZE) + { + __set_errno (ENOMEM); + return 0; + } + (void)mutex_lock(&main_arena.mutex); mem = (top_check() >= 0) ? _int_memalign(&main_arena, alignment, bytes+1) : NULL;