Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp593142pxk; Wed, 23 Sep 2020 10:45:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzp0qwjdOnaSMSYEwOD7L72AqCJJZhxP/HcSh7ZjWPFwn9ZQ7pWD/L5oCdLAj3hIaTY3wav X-Received: by 2002:a17:906:1b11:: with SMTP id o17mr813486ejg.67.1600883115032; Wed, 23 Sep 2020 10:45:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600883115; cv=none; d=google.com; s=arc-20160816; b=Lj2TWS+4ALdDIflkRHBTOSHCourfYB4N/WJJZxYLEfvM1IcsYn7At6PsWB46XPCzrC TMsxe33EYibeeV6Ysm80YuoO+ou9W05J27a9/CfECRntiUEWDoyifwQhvR/UwvZnr7Vv jtbx4RPYB4JJZBNmoA76sZduDnt5VTK6kamMcV1s7+ys3HpV5VAL5DjFxmJ6spLGv5ig 6qehojso9plW3wjkC2qMImfDXQuOfiKTXji9A2DYwEzCQZbeRtUSFht7dTyX7R3tygWr jrVRKycJaLUfZq5KnP94Ge3vwjQWZsfCTudtWJPu2DbGxhwe8QRK+Ucm0SDdg9Nl4yjo mmeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :ironport-sdr:ironport-sdr; bh=42bdOxtzNPMSXrd/O5mgR5x9TazkIDtaEYwHPKfVEZY=; b=u3XvUArvy/0G76PQ2aoN4dPPyu3SBvQhaKXxdEfaxpEFkW+oJ8FDvzWpVYIex5lwfH n56Y4I82Q5bHfeZlK4GHPLKNplY7V8wnxONQ18GfOUMCOocaKp3wREahj3WmIxozOCFv x0v+Lbae3JJm+TS0FZ6WAPKxC7or2Up1NLVGhEFt+dVjBVEYonm82RVyYW5PYLV7iBbw bCND/9lPIdT9WlD4IOEFvnqpd4F4uKIcRzQQAzpRfzCENpvVccmJT6+6vO11oNrJwpfW HIxNzVUhjtSRTxs96Vk6rCqle3EAjvkw1f6QjJtusrzJeMVOz+MLcAibZn8IFgNAE14p VYwg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q27si407869ejc.593.2020.09.23.10.44.50; Wed, 23 Sep 2020 10:45:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726746AbgIWRkg (ORCPT + 99 others); Wed, 23 Sep 2020 13:40:36 -0400 Received: from mga04.intel.com ([192.55.52.120]:17211 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726515AbgIWRkf (ORCPT ); Wed, 23 Sep 2020 13:40:35 -0400 IronPort-SDR: ec3qbdePH7oRoVq16kTX2n0xLtXJYxV9h/EHaHOoFcoVYmXSnEOqZeOXBZZ0jVOADbZLv/LOn9 niFRmiGk5+Vg== X-IronPort-AV: E=McAfee;i="6000,8403,9753"; a="158372209" X-IronPort-AV: E=Sophos;i="5.77,293,1596524400"; d="scan'208";a="158372209" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Sep 2020 10:40:35 -0700 IronPort-SDR: fZzUQIEHpQ8Ru67dYVx+/L5ydlgDqSuix7iws7AnqcrQrWWS5G8Hj/7bcwjbQ5i9JyzvGEWPwf Qm9s2ozYM5TQ== X-IronPort-AV: E=Sophos;i="5.77,293,1596524400"; d="scan'208";a="309993115" Received: from kcaccard-mobl.amr.corp.intel.com (HELO kcaccard-mobl1.jf.intel.com) ([10.212.14.213]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Sep 2020 10:40:29 -0700 From: Kristen Carlson Accardi To: keescook@chromium.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, "H. Peter Anvin" Cc: arjan@linux.intel.com, linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com, rick.p.edgecombe@intel.com, Kristen Carlson Accardi Subject: [PATCH v5 06/10] x86/boot/compressed: Avoid duplicate malloc() implementations Date: Wed, 23 Sep 2020 10:39:00 -0700 Message-Id: <20200923173905.11219-7-kristen@linux.intel.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200923173905.11219-1-kristen@linux.intel.com> References: <20200923173905.11219-1-kristen@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kees Cook The preboot malloc() (and free()) implementation in include/linux/decompress/mm.h (which is also included by the static decompressors) is static. This is fine when the only thing interested in using malloc() is the decompression code, but the x86 preboot environment uses malloc() in a couple places, leading to a potential collision when the static copies of the available memory region ("malloc_ptr") gets reset to the global "free_mem_ptr" value. As it happens, the existing usage pattern happened to be safe because each user did 1 malloc() and 1 free() before returning and were not nested: extract_kernel() (misc.c) choose_random_location() (kaslr.c) mem_avoid_init() handle_mem_options() malloc() ... free() ... parse_elf() (misc.c) malloc() ... free() Adding FGKASLR, however, will insert additional malloc() calls local to fgkaslr.c in the middle of parse_elf()'s malloc()/free() pair: parse_elf() (misc.c) malloc() if (...) { layout_randomized_image(output, &ehdr, phdrs); malloc() <- boom ... else layout_image(output, &ehdr, phdrs); free() To avoid collisions, there must be a single implementation of malloc(). Adjust include/linux/decompress/mm.h so that visibility can be controlled, provide prototypes in misc.h, and implement the functions in misc.c. This also results in a small size savings: $ size vmlinux.before vmlinux.after text data bss dec hex filename 8842314 468 178320 9021102 89a6ae vmlinux.before 8842240 468 178320 9021028 89a664 vmlinux.after Signed-off-by: Kees Cook Signed-off-by: Kristen Carlson Accardi --- arch/x86/boot/compressed/kaslr.c | 4 ---- arch/x86/boot/compressed/misc.c | 3 +++ arch/x86/boot/compressed/misc.h | 2 ++ include/linux/decompress/mm.h | 12 ++++++++++-- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c index dde7cb3724df..e811071ce5d2 100644 --- a/arch/x86/boot/compressed/kaslr.c +++ b/arch/x86/boot/compressed/kaslr.c @@ -32,10 +32,6 @@ #include #include -/* Macros used by the included decompressor code below. */ -#define STATIC -#include - #ifdef CONFIG_X86_5LEVEL unsigned int __pgtable_l5_enabled; unsigned int pgdir_shift __ro_after_init = 39; diff --git a/arch/x86/boot/compressed/misc.c b/arch/x86/boot/compressed/misc.c index e478e40fbe5a..dc396321eba8 100644 --- a/arch/x86/boot/compressed/misc.c +++ b/arch/x86/boot/compressed/misc.c @@ -28,6 +28,9 @@ /* Macros used by the included decompressor code below. */ #define STATIC static +/* Define an externally visible malloc()/free(). */ +#define MALLOC_VISIBLE +#include /* * Provide definitions of memzero and memmove as some of the decompressors will diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/misc.h index 726e264410ff..81fbc8d686fa 100644 --- a/arch/x86/boot/compressed/misc.h +++ b/arch/x86/boot/compressed/misc.h @@ -39,6 +39,8 @@ /* misc.c */ extern memptr free_mem_ptr; extern memptr free_mem_end_ptr; +extern void *malloc(int size); +extern void free(void *where); extern struct boot_params *boot_params; void __putstr(const char *s); void __puthex(unsigned long value); diff --git a/include/linux/decompress/mm.h b/include/linux/decompress/mm.h index 868e9eacd69e..9192986b1a73 100644 --- a/include/linux/decompress/mm.h +++ b/include/linux/decompress/mm.h @@ -25,13 +25,21 @@ #define STATIC_RW_DATA static #endif +/* + * When an architecture needs to share the malloc()/free() implementation + * between compilation units, it needs to have non-local visibility. + */ +#ifndef MALLOC_VISIBLE +#define MALLOC_VISIBLE static +#endif + /* A trivial malloc implementation, adapted from * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994 */ STATIC_RW_DATA unsigned long malloc_ptr; STATIC_RW_DATA int malloc_count; -static void *malloc(int size) +MALLOC_VISIBLE void *malloc(int size) { void *p; @@ -52,7 +60,7 @@ static void *malloc(int size) return p; } -static void free(void *where) +MALLOC_VISIBLE void free(void *where) { malloc_count--; if (!malloc_count) -- 2.20.1