Received: by 2002:a25:ca44:0:0:0:0:0 with SMTP id a65csp35783ybg; Mon, 27 Jul 2020 14:51:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxVhbCgxlsvkjTVwvl14R7dNe5dV1I0le4tvqjWQ3VuHu2GGy3xxzRft52XwO0EQdUH9kT5 X-Received: by 2002:a17:906:7709:: with SMTP id q9mr22387728ejm.123.1595886702267; Mon, 27 Jul 2020 14:51:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595886702; cv=none; d=google.com; s=arc-20160816; b=IYV9mFvEGsbrv4Qp6j2jS+XR3db3/jywiAX7TWlfxSWIw2tdD1IJJYk9Qi8ASAW9jk XKAHqfNlUwcMR9qZqwSbdQn7hby+Djn8aWfbzlDQN7F6mMTReprgjtCDzYi4Upj48eNn frBg08qY9T6FWbqP+Rfmfdg7+IcnyxnngC8dalEi5a9OOO17qQo/V6VPOp3pQ9Z+oDjT Qfy8h5ZKn+QHj/QnprcJDbO1jANpLoNyfz6YbPv470YKa6z7J4JA6qYQNQZiEJZj1taw vmflomNNHfHoKmSg0lMk0heE0MFaIQiyxOigM6H9sB1HSaM8dHuZtl2NzegZ1k+Ss8id ZSHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=xf8u112G9B6jocNxpIpSZ5Ma8y1M/w5PTt/UgbiI4/s=; b=ZO1G7I3aG5/kpKibvvJ7oKOm3h1hDPw/kTsygiM99Pkf/hYM4VxtJd7dkm0W5seZ9l Hf/pMxZtx3PfHIkcV7qeYoW2GENooX8pVzMK7X4nDyxDw16Eh1YVnBWzeUxxgIcDitEn WP/2GtuN3BWfB0dbxNYxnuyzKcgFjmF5uVqXV8GcW2zQpBdeDqa5p3RojkIqRSUwTg7H /JEQ8MY3fjDuFnLVNTKBc/gF1cKouQMLo0b1RLQct5m/BWHPaOnhCp+oyNuozZhNT6XN ISWcc/j5liNaBsBlEFNkd/mQaNVjyMhHsCeE0zObaC1lSTL0LdX9OPj+o0yThq/RSj0Z 5N6g== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j20si4577711edr.321.2020.07.27.14.51.20; Mon, 27 Jul 2020 14:51:42 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727020AbgG0VvB (ORCPT + 99 others); Mon, 27 Jul 2020 17:51:01 -0400 Received: from mail-qk1-f194.google.com ([209.85.222.194]:34105 "EHLO mail-qk1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726946AbgG0Vu4 (ORCPT ); Mon, 27 Jul 2020 17:50:56 -0400 Received: by mail-qk1-f194.google.com with SMTP id x69so16842699qkb.1 for ; Mon, 27 Jul 2020 14:50:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xf8u112G9B6jocNxpIpSZ5Ma8y1M/w5PTt/UgbiI4/s=; b=ZMsuDCfx/ThSZIAky4LIzEagGIbw1+E/u0IhdmOr/fR3FNrCqxQmVtUgRlhfYmAEdj QmjukLET29BMQ5E4vNm0TzGWDXlvux4qrKPZO+WwlbLNry0u4e2WLn8XH/GcEZ3hiKZB vNKSHQ8Hw9YutZwytIUNAlU/k946KxdUmVEXJ+c9uyWury7d3QlyGOqLXU6AfQBrjMWe zQ5VSkNio0uHwbcOkIrDzZ8l8vpzrejluxEUVJTcUm67PzQ7RCTZx7PwkOW7t41sJ65H O7DoNwf3ttoAnItL96ISt4Omu4+XlHGIGrQV+Yi1vfBTQ89lNZyOPMhALR89qGOQr1cD dayA== X-Gm-Message-State: AOAM532/a4WFPbPKtd1GvwsE82i/5P/QN0p1CBtyk6gF2fXBjSvID5vD hpAAwPsnFyBHrRjEFB62fsYIl5R9 X-Received: by 2002:a37:6482:: with SMTP id y124mr25625811qkb.470.1595886655245; Mon, 27 Jul 2020 14:50:55 -0700 (PDT) Received: from rani.riverdale.lan ([2001:470:1f07:5f3::b55f]) by smtp.gmail.com with ESMTPSA id n90sm15629338qte.21.2020.07.27.14.50.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jul 2020 14:50:54 -0700 (PDT) From: Arvind Sankar To: Kees Cook , x86@kernel.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH 7/8] x86/kaslr: Clean up slot handling Date: Mon, 27 Jul 2020 17:50:46 -0400 Message-Id: <20200727215047.3341098-8-nivedita@alum.mit.edu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200727215047.3341098-1-nivedita@alum.mit.edu> References: <20200727215047.3341098-1-nivedita@alum.mit.edu> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The number of slots and slot areas can be unsigned int, since on 64-bit, the maximum amount of memory is 2^52, the minimum alignment is 2^21, so the slot number cannot be greater than 2^31. The slot areas are limited by MAX_SLOT_AREA, currently 100. Replace the type used for slot number, which is currently a mix of int and unsigned long, with unsigned int consistently. Drop unnecessary check that number of slots is not zero in store_slot_info, it's guaranteed to be at least 1 by the calculation. Drop unnecessary alignment of image_size to CONFIG_PHYSICAL_ALIGN in find_random_virt_addr, it cannot change the result: the largest valid slot is the largest n that satisfies minimum + n * CONFIG_PHYSICAL_ALIGN + image_size <= KERNEL_IMAGE_SIZE (since minimum is already aligned) and so n is equal to (KERNEL_IMAGE_SIZE - minimum - image_size) / CONFIG_PHYSICAL_ALIGN even if image_size is not aligned to CONFIG_PHYSICAL_ALIGN. Signed-off-by: Arvind Sankar --- arch/x86/boot/compressed/kaslr.c | 36 ++++++++++++-------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c index 29ec964b1330..ffbcc0444f70 100644 --- a/arch/x86/boot/compressed/kaslr.c +++ b/arch/x86/boot/compressed/kaslr.c @@ -511,16 +511,14 @@ static bool mem_avoid_overlap(struct mem_vector *img, struct slot_area { unsigned long addr; - int num; + unsigned int num; }; #define MAX_SLOT_AREA 100 static struct slot_area slot_areas[MAX_SLOT_AREA]; - -static unsigned long slot_max; - -static unsigned long slot_area_index; +static unsigned int slot_area_index; +static unsigned int slot_max; static void store_slot_info(struct mem_vector *region, unsigned long image_size) { @@ -530,13 +528,10 @@ static void store_slot_info(struct mem_vector *region, unsigned long image_size) return; slot_area.addr = region->start; - slot_area.num = (region->size - image_size) / - CONFIG_PHYSICAL_ALIGN + 1; + slot_area.num = 1 + (region->size - image_size) / CONFIG_PHYSICAL_ALIGN; - if (slot_area.num > 0) { - slot_areas[slot_area_index++] = slot_area; - slot_max += slot_area.num; - } + slot_areas[slot_area_index++] = slot_area; + slot_max += slot_area.num; } /* @@ -589,8 +584,7 @@ process_gb_huge_pages(struct mem_vector *region, unsigned long image_size) static unsigned long slots_fetch_random(void) { - unsigned long slot; - int i; + unsigned int slot, i; /* Handle case of no slots stored. */ if (slot_max == 0) @@ -603,7 +597,7 @@ static unsigned long slots_fetch_random(void) slot -= slot_areas[i].num; continue; } - return slot_areas[i].addr + slot * CONFIG_PHYSICAL_ALIGN; + return slot_areas[i].addr + (unsigned long)slot * CONFIG_PHYSICAL_ALIGN; } if (i == slot_area_index) @@ -819,28 +813,24 @@ static unsigned long find_random_phys_addr(unsigned long minimum, return 0; } - if (process_efi_entries(minimum, image_size)) - return slots_fetch_random(); + if (!process_efi_entries(minimum, image_size)) + process_e820_entries(minimum, image_size); - process_e820_entries(minimum, image_size); return slots_fetch_random(); } static unsigned long find_random_virt_addr(unsigned long minimum, unsigned long image_size) { - unsigned long slots, random_addr; - - /* Align image_size for easy slot calculations. */ - image_size = ALIGN(image_size, CONFIG_PHYSICAL_ALIGN); + unsigned int slots; + unsigned long random_addr; /* * There are how many CONFIG_PHYSICAL_ALIGN-sized slots * that can hold image_size within the range of minimum to * KERNEL_IMAGE_SIZE? */ - slots = (KERNEL_IMAGE_SIZE - minimum - image_size) / - CONFIG_PHYSICAL_ALIGN + 1; + slots = 1 + (KERNEL_IMAGE_SIZE - minimum - image_size) / CONFIG_PHYSICAL_ALIGN; random_addr = kaslr_get_random_long("Virtual") % slots; -- 2.26.2