Received: by 2002:a25:ca44:0:0:0:0:0 with SMTP id a65csp668308ybg; Tue, 28 Jul 2020 16:01:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwyZZa78KuYllzPcFp9njhp21QgPuRJ5l8hJLo3dosaxaYOael4I+wt1CTBVR1oyrfLG/JE X-Received: by 2002:a05:6402:12d7:: with SMTP id k23mr21000722edx.312.1595977307122; Tue, 28 Jul 2020 16:01:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595977307; cv=none; d=google.com; s=arc-20160816; b=EHzzMN33e6iEFvGukpme66bTiyTdSHznTzNic4wJfiQWZPCyP4ky5hriLxTB9d1ZN6 TtFzykKsulwx0BLi/92zKPwLyHuQCaJuh0ck87waaryJHYSmG915zcR2I4W7AydEdvGo 99kHE/irY3/h0TzEgGa9Fve6fntc89WUq10oRTVB+diZlgeaIvXnOMu/v2F4wY7cUbcl /SVOxwWK6BJpghucSsyDOVHfqwSRwa579503FVqCKVL+rRyQL3JU0mSMZ/DOsgQCwDHV wkBtMH/UR2+zk0S1/nLC+hGJjYvdTJodQDjHNOHAIGnJSV0OjGxVSFU0cqNZJoX6+q1x xniw== 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=qi173Z4bhMQyR0fQT50/DyIWcqMl6dO/gU6zmOPbzuY=; b=PUFH978969++q26jfkLCinq9JuW3seNsrFkHePVbo5fcW4jUuhBVEd5MeBATc3ccqz UzrbfSv4xTL7BRLxVazd8p/cRqn68v7vOCHRlRKXuRuulEgDSDkddA41lSqXQnKZnOFm BFPrlY26y1/iAUrzOmn7xVaXB+hPfTfwVpgVXMcR7O6MmQ/p//uNjIWzKxGbZxHiP14x 7HoWqr5CD8RN1ydYgkKsbSwGfPVYZys/WzyG5ykNGG84HQgU2ukORIlo26KZbEngVh1S wD4jQdcV/36xYYQB/xURTd4hTjBOVEIPcdoh8MqkSi+rf3HnpcR9ET8a8WKErD+LiWFI BHXw== 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 y8si110046edo.253.2020.07.28.16.01.24; Tue, 28 Jul 2020 16:01:47 -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 S1730274AbgG1W6R (ORCPT + 99 others); Tue, 28 Jul 2020 18:58:17 -0400 Received: from mail-qk1-f195.google.com ([209.85.222.195]:37105 "EHLO mail-qk1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730034AbgG1W5h (ORCPT ); Tue, 28 Jul 2020 18:57:37 -0400 Received: by mail-qk1-f195.google.com with SMTP id b14so18749891qkn.4 for ; Tue, 28 Jul 2020 15:57:37 -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=qi173Z4bhMQyR0fQT50/DyIWcqMl6dO/gU6zmOPbzuY=; b=sHZ7OiaHuv1YvVUsUzzGaQMB783U12goumOZ/85sXn8RjgIbRJn7CX3ktzG+MP3cXK xzXgsEYv4xbf/xYlMCHcQ5nC6eBg6DxJ4obRyfFj+qQ7WtYy7sB3Xu/sM67+CoGp2SJG Vxx5xB3T7BZdz2xiZKbcX4KtlDbvGcM2Yg+cIISkI0R36Al1gX/n+PIX9G+cUzPM/Is+ VEtD0lOWaoBCx71XDZbre8OVt+W8zs6av5+qPP7Ul9bO2tapdiEENs42HxLC3MBpVWQH rrCZirsIm4L7y5UVz84wYAawzwLNkYchWZhCIA2RfQPkm1Sh6LrBjiqjgj1sBlbV8Egb TElg== X-Gm-Message-State: AOAM532rGSqxVVQoZGDdXrfTGkdvscBabPTx16qAwRKvW3xMXFywBEVf iAeTvzh2ZNtxFiTkCIJ37tX8fGD5 X-Received: by 2002:a37:b847:: with SMTP id i68mr29531505qkf.126.1595977056552; Tue, 28 Jul 2020 15:57:36 -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.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Jul 2020 15:57:36 -0700 (PDT) From: Arvind Sankar To: Kees Cook , x86@kernel.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH v3 16/21] x86/kaslr: Drop unnecessary alignment in find_random_virt_addr Date: Tue, 28 Jul 2020 18:57:17 -0400 Message-Id: <20200728225722.67457-17-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 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 | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c index 2f43b0d7051e..4fd60eff048f 100644 --- a/arch/x86/boot/compressed/kaslr.c +++ b/arch/x86/boot/compressed/kaslr.c @@ -824,16 +824,12 @@ static unsigned long find_random_virt_addr(unsigned long minimum, { unsigned long slots, random_addr; - /* Align image_size for easy slot calculations. */ - image_size = ALIGN(image_size, CONFIG_PHYSICAL_ALIGN); - /* * 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