From patchwork Wed Sep 25 07:59:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kjetil Oftedal X-Patchwork-Id: 1167030 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=uclibc-ng.org (client-ip=89.238.66.15; helo=helium.openadk.org; envelope-from=devel-bounces@uclibc-ng.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Received: from helium.openadk.org (helium.openadk.org [89.238.66.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46dVpR0rN4z9sNx for ; Wed, 25 Sep 2019 17:59:56 +1000 (AEST) Received: from helium.openadk.org (localhost [IPv6:::1]) by helium.openadk.org (Postfix) with ESMTP id A03A21009B; Wed, 25 Sep 2019 09:59:49 +0200 (CEST) X-Original-To: devel@uclibc-ng.org Delivered-To: devel@helium.openadk.org Received: from rel03.intility.com (rel03.intility.com [137.221.30.10]) by helium.openadk.org (Postfix) with ESMTPS id 8A8021009B for ; Wed, 25 Sep 2019 09:59:46 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A2HHAgDuHYtd/2QKAQpkHAEBAQQBAQwEAQGBZ4Q6nzqFZYtACQEBAQ4FKgEBiA44EwIMAQEFAQEBAQEFBAEBAoYigjoihCZDXAJNgyKBagEDrmEFAReFTIIHChknDWaBRQkBgSqHM4RxgX+DAXRsghqCd4UWBKxPQUaBZoIujluEAQIZmSoBLZgJjyKBaYF6MxoIGxVsgjtQEBSBWhcVjg9xkBUBAQ X-IronPort-AV: E=Sophos;i="5.64,547,1559512800"; d="scan'208";a="12971417" Received: from i2-relay-002.i04.local ([10.1.10.100]) by rel03.intility.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 25 Sep 2019 09:59:46 +0200 Received: from buildhostv2.i04.local ([10.70.47.153]) by I2-RELAY-002.i04.local with Microsoft SMTPSVC(10.0.14393.2608); Wed, 25 Sep 2019 09:59:46 +0200 From: Kjetil Oftedal To: devel@uclibc-ng.org Date: Wed, 25 Sep 2019 09:59:46 +0200 Message-Id: <1569398386-5780-1-git-send-email-oftedal@gmail.com> X-Mailer: git-send-email 1.7.9.5 X-OriginalArrivalTime: 25 Sep 2019 07:59:46.0027 (UTC) FILETIME=[3258A3B0:01D57377] Subject: [uclibc-ng-devel] [PATCH] malloc: Add missing locks for some paths (valloc/memalign/posix_memalign) X-BeenThere: devel@uclibc-ng.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: uClibc-ng Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: devel-bounces@uclibc-ng.org Sender: "devel" The internal heap structures were not protected properly in memalign(). If multiple threads were concurrently allocating memory and one of them were requesting aligned memory via valloc,memalign or posix_memalign the internal heap data structures could be corrupted. Signed-off-by: Kjetil Oftedal --- libc/stdlib/malloc/memalign.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/libc/stdlib/malloc/memalign.c b/libc/stdlib/malloc/memalign.c index 74d5dbd..0d3de67 100644 --- a/libc/stdlib/malloc/memalign.c +++ b/libc/stdlib/malloc/memalign.c @@ -77,7 +77,9 @@ memalign (size_t alignment, size_t size) init_size = addr - tot_addr; } + __heap_lock (&__malloc_heap_lock); __heap_free (heap, base, init_size); + __heap_unlock (&__malloc_heap_lock); /* Remember that we've freed the initial part of MEM. */ base += init_size; @@ -85,9 +87,11 @@ memalign (size_t alignment, size_t size) /* Return the end part of MEM to the heap, unless it's too small. */ end_addr = addr + size; - if (end_addr + MALLOC_REALLOC_MIN_FREE_SIZE < tot_end_addr) + if (end_addr + MALLOC_REALLOC_MIN_FREE_SIZE < tot_end_addr) { + __heap_lock (&__malloc_heap_lock); __heap_free (heap, (void *)end_addr, tot_end_addr - end_addr); - else + __heap_unlock (&__malloc_heap_lock); + } else /* We didn't free the end, so include it in the size. */ end_addr = tot_end_addr;