Received: by 2002:a25:ca44:0:0:0:0:0 with SMTP id a65csp666203ybg; Tue, 28 Jul 2020 15:58:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxBjcuCbpKzhsw3safHdZqbPFWcFU8mGtUHzV965cqcVMXprgQDg8+w8NCHTr3sqfJJzCWo X-Received: by 2002:a17:906:5346:: with SMTP id j6mr18746581ejo.311.1595977105702; Tue, 28 Jul 2020 15:58:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595977105; cv=none; d=google.com; s=arc-20160816; b=qhdK/LUQNLcFtX42TmsDOLVw+WugS2Pkn9e1Wsoa+LeoM6mxLwh909I4/BAcofA2pJ gj0CuscGpoYU6VRekB0yavH8bdFHEGPI3c+98kcNn/W3mYjK5qM2JPGmrjcGtZc2sGgu sJwRYeHVaM4J9W28I62Iy/fTGXWHTjXW6XHeRxvJyKjGhLcpBysX8b5WxUifRCxeFkt2 Qbp8FNT8RrnNuwwcwXBtMmtdBN2wEzOAabXCBmy9pBgZPtSU9HnIirjCqaLwXTz52I85 k5qN35jWJpKvTcQOkCgljW0qSuQKAdZ1+tDQzXKa30Ylad/GLvPtwvojtk5CaDwT5TNQ vwnA== 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=frlZNzKjxBW53TThhYCvkj05m4KvzWU3R8lBzdsAfZs=; b=S1R9M8te0SLawSynbqCNTGUsBvcNh5QIQgLAcXv/FkKD+CCEwJ7BRyeo3l+01L0usk 0hJCvKWAHpx9OLJH6YFIAM1kGMU3XsJeB7BnqBIyTaXUvfVYs9dPoM6n8FLCqYcRC2sq 05PWU1rE0AT5sGqeHCQVG3UfY6NprZBMm6iZJdzq1VU07Sbt5KtM7WbzZdSESLydhzHB kDu03MJyhbE/4F74kOD/uWdaNfynadhvQBlPS4WPEF2OByuRBMRBDJDo9Hdan3liMSH0 nfsn6+QtIcVoLv7h/Ud6uWhAJidCbGz6XSbR7pOOs2Tp3Y0Pkg/kFzYVtyzGx8k5d3KE W3YQ== 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 l7si108117edn.510.2020.07.28.15.58.04; Tue, 28 Jul 2020 15:58:25 -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 S1730117AbgG1W5o (ORCPT + 99 others); Tue, 28 Jul 2020 18:57:44 -0400 Received: from mail-qt1-f195.google.com ([209.85.160.195]:36443 "EHLO mail-qt1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729948AbgG1W5c (ORCPT ); Tue, 28 Jul 2020 18:57:32 -0400 Received: by mail-qt1-f195.google.com with SMTP id t23so13116118qto.3 for ; Tue, 28 Jul 2020 15:57:31 -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=frlZNzKjxBW53TThhYCvkj05m4KvzWU3R8lBzdsAfZs=; b=TTxie5HzLiCVSwzdlb1BVZLO4d0nU3p2lMQjfkqiYqKGTmtPitU/cO7o+LwdyXtuLJ gpiXTdurjzWHtm6oK8o59L2K3K5k8zK5/H3oA68zo0+YCRGlafyZGRIANbSljOVWGeze txXtWFNSxNq2IKOjIFnvtT6FIIdIrOMGUR3+clRB20f1I24XaMpUQZij/P9FLJDFuGSM ijcm9mY1MqbL02kM8RVgd2m04BeWxwL36kmivX8i/abpy4BY4NF65Sp6iJuoLGe7v0Fm DbINY4c1Ox1I7SPu6n3bsoZQHqTk2ihVaYFT6utNj70agpLLoF6+BnkVPdNMXoEnJaoZ d9Dw== X-Gm-Message-State: AOAM531pZo8l85hBjG6D5OGK/5RKFl9Xuo3PSltrijIUZ9G1RaLqTLVE Gr64SCJLdNYGtuCNBlJy59ItHDrl X-Received: by 2002:ac8:78d:: with SMTP id l13mr10293908qth.188.1595977050786; Tue, 28 Jul 2020 15:57:30 -0700 (PDT) Received: from rani.riverdale.lan ([2001:470:1f07:5f3::b55f]) by smtp.gmail.com with ESMTPSA id o187sm88118qkd.112.2020.07.28.15.57.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Jul 2020 15:57:30 -0700 (PDT) From: Arvind Sankar To: Kees Cook , x86@kernel.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH v3 09/21] x86/kaslr: Drop some redundant checks from __process_mem_region Date: Tue, 28 Jul 2020 18:57:10 -0400 Message-Id: <20200728225722.67457-10-nivedita@alum.mit.edu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200727230801.3468620-1-nivedita@alum.mit.edu> References: <20200727230801.3468620-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 Clip the start and end of the region to minimum and mem_limit prior to the loop. region.start can only increase during the loop, so raising it to minimum before the loop is enough. A region that becomes empty due to this will get checked in the first iteration of the loop. Drop the check for overlap extending beyond the end of the region. This will get checked in the next loop iteration anyway. Rename end to region_end for symmetry with region.start. Signed-off-by: Arvind Sankar --- arch/x86/boot/compressed/kaslr.c | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c index 8cc47faea56d..d074986e8061 100644 --- a/arch/x86/boot/compressed/kaslr.c +++ b/arch/x86/boot/compressed/kaslr.c @@ -623,34 +623,23 @@ static void __process_mem_region(struct mem_vector *entry, unsigned long image_size) { struct mem_vector region, overlap; - unsigned long end; + unsigned long region_end; - /* Ignore entries entirely below our minimum. */ - if (entry->start + entry->size < minimum) - return; - - /* Ignore entries above memory limit */ - end = min(entry->size + entry->start, mem_limit); - if (entry->start >= end) - return; - - region.start = entry->start; + /* Enforce minimum and memory limit. */ + region.start = max_t(unsigned long long, entry->start, minimum); + region_end = min(entry->start + entry->size, mem_limit); /* Give up if slot area array is full. */ while (slot_area_index < MAX_SLOT_AREA) { - /* Potentially raise address to minimum location. */ - if (region.start < minimum) - region.start = minimum; - /* Potentially raise address to meet alignment needs. */ region.start = ALIGN(region.start, CONFIG_PHYSICAL_ALIGN); /* Did we raise the address above the passed in memory entry? */ - if (region.start > end) + if (region.start > region_end) return; /* Reduce size by any delta from the original address. */ - region.size = end - region.start; + region.size = region_end - region.start; /* Return if region can't contain decompressed kernel */ if (region.size < image_size) @@ -668,10 +657,6 @@ static void __process_mem_region(struct mem_vector *entry, process_gb_huge_pages(®ion, image_size); } - /* Return if overlap extends to or past end of region. */ - if (overlap.start + overlap.size >= region.start + region.size) - return; - /* Clip off the overlapping region and start over. */ region.start = overlap.start + overlap.size; } -- 2.26.2