Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753257AbcDNWa6 (ORCPT ); Thu, 14 Apr 2016 18:30:58 -0400 Received: from mail-pf0-f171.google.com ([209.85.192.171]:34073 "EHLO mail-pf0-f171.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752617AbcDNW3e (ORCPT ); Thu, 14 Apr 2016 18:29:34 -0400 From: Kees Cook To: Ingo Molnar Cc: Kees Cook , Baoquan He , Yinghai Lu , Ard Biesheuvel , Matt Redfearn , x86@kernel.org, "H. Peter Anvin" , Ingo Molnar , Borislav Petkov , Vivek Goyal , Andy Lutomirski , lasse.collin@tukaani.org, Andrew Morton , Dave Young , kernel-hardening@lists.openwall.com, LKML Subject: [PATCH v5 15/21] x86, KASLR: Add slot_area support functions Date: Thu, 14 Apr 2016 15:29:08 -0700 Message-Id: <1460672954-32567-16-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.6.3 In-Reply-To: <1460672954-32567-1-git-send-email-keescook@chromium.org> References: <1460672954-32567-1-git-send-email-keescook@chromium.org> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2650 Lines: 90 From: Baoquan He The function store_slot_info() is used to calculate the slot info of the passed-in memory region and stores it into slot_areas[] after adjusting for alignment and size requirements. The function mem_min_overlap() is used to iterate over all mem_avoid regions to find the earliest mem_avoid address that conflicts with the given memory region. (For example, with the region [1024M, 2048M), if there is a mem_avoid of [1536M, 1664M), this returns 1536M.) This can be used to split memory regions when building the slot_area array. Signed-off-by: Baoquan He [kees: rewrote changelog] Signed-off-by: Kees Cook --- arch/x86/boot/compressed/aslr.c | 51 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/arch/x86/boot/compressed/aslr.c b/arch/x86/boot/compressed/aslr.c index abe618d489ea..b06618000732 100644 --- a/arch/x86/boot/compressed/aslr.c +++ b/arch/x86/boot/compressed/aslr.c @@ -257,6 +257,40 @@ static bool mem_avoid_overlap(struct mem_vector *img) return false; } +static unsigned long +mem_min_overlap(struct mem_vector *img, struct mem_vector *out) +{ + int i; + struct setup_data *ptr; + unsigned long min = img->start + img->size; + + for (i = 0; i < MEM_AVOID_MAX; i++) { + if (mem_overlaps(img, &mem_avoid[i]) && + (mem_avoid[i].start < min)) { + *out = mem_avoid[i]; + min = mem_avoid[i].start; + } + } + + /* Check all entries in the setup_data linked list. */ + ptr = (struct setup_data *)(unsigned long)real_mode->hdr.setup_data; + while (ptr) { + struct mem_vector avoid; + + avoid.start = (unsigned long)ptr; + avoid.size = sizeof(*ptr) + ptr->len; + + if (mem_overlaps(img, &avoid) && (avoid.start < min)) { + *out = avoid; + min = avoid.start; + } + + ptr = (struct setup_data *)(unsigned long)ptr->next; + } + + return min; +} + static unsigned long slots[KERNEL_IMAGE_SIZE / CONFIG_PHYSICAL_ALIGN]; struct slot_area { @@ -272,6 +306,23 @@ static unsigned long slot_max; static unsigned long slot_area_index; +static void store_slot_info(struct mem_vector *region, unsigned long image_size) +{ + struct slot_area slot_area; + + slot_area.addr = region->start; + if (image_size <= CONFIG_PHYSICAL_ALIGN) + slot_area.num = region->size / CONFIG_PHYSICAL_ALIGN; + else + slot_area.num = (region->size - image_size) / + CONFIG_PHYSICAL_ALIGN + 1; + + if (slot_area.num > 0) { + slot_areas[slot_area_index++] = slot_area; + slot_max += slot_area.num; + } +} + static void slots_append(unsigned long addr) { /* Overflowing the slots list should be impossible. */ -- 2.6.3