Received: by 2002:ab2:7855:0:b0:1f9:5764:f03e with SMTP id m21csp773644lqp; Wed, 22 May 2024 22:05:20 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUVPF3zs3K1ESA9A5/Ktj5jrzifEx0WniQLElL0b2Fv12swnV6RLHKRmclXRQjWTaX16zGBC+rkcG2oWS7pfqaRgWFWs7oE45DFuUdFrw== X-Google-Smtp-Source: AGHT+IHaNjR4Vib6YPuDKacAVOuc8NYoiYLR7T1QhJ5yye676DmSZB55n/jAdnFs6PXQbQw9YeCY X-Received: by 2002:a05:6a21:498b:b0:1aa:6167:b6d6 with SMTP id adf61e73a8af0-1b1f8a8bba9mr3828366637.42.1716440720250; Wed, 22 May 2024 22:05:20 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716440720; cv=pass; d=google.com; s=arc-20160816; b=g2UZP7hPJ5NaC0wzM2zNi6rYe7hZESFU8mPd9d9aa4RqlU05k1k42omfmO8egk71ad b5cqgdey+zc+PSX8+eMNa8YJKV7qaKqUhcpIaaNr7YZ7RO+1/apxXAqp+hlhJl/hIlnN IjVzcGvcDQ3EBAvgGm9Y+94lh+e5ygL12tE+yk7NQLm5EMWOgPcX5P5/9yzGx5NER9Bo 4iE7U9A+ln8H+NWf0Qu1x5x8G1Jcr23eqSUiCNUtbz8mJu2LZnYglvWDbTxCfSzVy1Cd 0g1np3kPgKbPYKOqO/kGibmPG5vnY015Yf2icVtWUkHN5xTZRcSY7RmXhLvfErvAasXq XfUA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=9ZynSKfZ+ldOQ3kMeJNfuvuSvRX+IwHDcGck7Xj2nRU=; fh=Cn66P8ts7cdSHikPMcxYQv2Ys/BNdUBKYzxqGlHLHQE=; b=bUB8HtxhdpRNaGVkfSygUvuCUQz7v+249wUOhvCE/OWC65Sg6Zm2H5wgfc96H3iu1L YYE1s+g04/eQ7YJq75+Dc47l1hLzuvYmTmYF7U/IVuGluSLtYHQxRvnYwOroqVmlQttL m1fjfasEGznTHtc++Cb9IRioiyR7vKANAY/jxC8b1m9GbmbYkcrLB7P4rNn9b8ctthT6 /S5YG3cUq0gdA1fpg8DQwWwxmfBcLH/LGsVYJXFAbAwK/qI2ZNjWYoOi9RjhsTT78wEf Y4xdKare33DJ1X5VU35qtP+XVq2RKBpQpIhWMQIrWhP3obdF4GrrwDWpe8hxPeESuHDy Gchg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dVmIxDqr; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-186989-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-186989-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id 98e67ed59e1d1-2bdd9ee61d8si915642a91.30.2024.05.22.22.05.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 May 2024 22:05:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-186989-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dVmIxDqr; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-186989-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-186989-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id C13D42834EC for ; Thu, 23 May 2024 05:05:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A062313B2A5; Thu, 23 May 2024 05:05:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="dVmIxDqr" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0EEB9129A78 for ; Thu, 23 May 2024 05:05:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716440705; cv=none; b=cSWsqqoMhArLKpX9afnqECLS9kkuxkOxle2f4bwxOG7TyHmEAkS46qR1/oLXmrf+FU0HZ8slVsHrAJT8yrJvF4AA2q8jdwviLYGnaqcrVBTvafLD07HIKzyNL2z9czaYNfVN/gOWgvDR9zsKEzkRkNiMUuwy3CmvNh9PsQRaueg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716440705; c=relaxed/simple; bh=dGZmp2vLpOIfcPBTPdNO+OvC6zpXCIslEn4uygnmMgc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jz6Ji2YRtUC52qhjuueRPrNmL+i29VzBuyMrREPmsg4oq1t/d26kKal4Bwo7H7XI75GNproNZYH1URH2tdKtjhpd/P/hxQs2jmZ8S04tZugFTj05PEeThcwGnwmsxOSVSxyY/8rFJFbjjd7KK4M1wYCOMfOb1Vw5HnfJW7BL1Ng= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=dVmIxDqr; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1716440702; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9ZynSKfZ+ldOQ3kMeJNfuvuSvRX+IwHDcGck7Xj2nRU=; b=dVmIxDqrPMG4VFTbMyS37OBLMSIALn63LR9l5dIOdc7gVTp5oVAJuM1iGY/9CsnhojCJWM nEtsGxtT4wFHDJCFJHFH/OarCdd+KkJRsbPQ7SrpUHxTyy6FLrKGKT7lZ/qpI6JkQJb1Bx Tk1DNLAL4S0s71xT4fA3ue0bkKYJT2w= Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-591-O_cr-IqzOBWZ_VVzACwtmg-1; Thu, 23 May 2024 01:05:00 -0400 X-MC-Unique: O_cr-IqzOBWZ_VVzACwtmg-1 Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-1eec5aba2bdso130609475ad.0 for ; Wed, 22 May 2024 22:05:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716440699; x=1717045499; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9ZynSKfZ+ldOQ3kMeJNfuvuSvRX+IwHDcGck7Xj2nRU=; b=km8bz84tTV07bVKPiLMk4OZA9RxItL+BCWVwwNTpzcrWOAfMhUAeQPuJfpmY8OpPCR MKyTLDK6snH4BKhBfP+Zv3xOU9DBQkojhc/1bbvfJzxcuks8cFK9iqfzUTM6tZsKuYmG nRQqm4QL/zksLX/seNI52wfIPV1Xt2QD2biHiweQ0XBIG+bnRTsejS47Gb257KLUoORJ 7sUwhfIqyvrGIgd0YphBwrhhZlHLESI93UZmh8OrBfk/7ExMDINehWZOJgfAc+ce0DWs 0cPdy9cI4o2rL5FufuWyEACDOjKAO83PYMxRBMMDns7kcZs+ojgTrmjhAvof3teUhFHN W+dg== X-Forwarded-Encrypted: i=1; AJvYcCWZbhGG9Kbh1cUnq0WrbrwtL9sBlBK1EgnX6xhxaKPSzs3iV7gV1Ku704jJsua7e2msJwwdajjyxBLDdiNMs1BTeSIq6BSDkG2KTpre X-Gm-Message-State: AOJu0YyGt+aWSRVAlgSwXesVLmRj3TnG/xgTsmkpH4Jh/zRw/cWrKqcl 73/DsBOExswAxiBv7E6wJ93N7ZbHgM/6H4IVzNn7vdN2wscBwyU3miZjllcIlRx+RngzuGR1yxw TfESniiNeqRG+CBTkch+8yFFv/FPnxeXNN12GhMl/yn330Py3jIGcQ2Ft+bWmvA== X-Received: by 2002:a17:902:cf04:b0:1f3:2fa7:6d7b with SMTP id d9443c01a7336-1f32fa76e61mr33104395ad.19.1716440699018; Wed, 22 May 2024 22:04:59 -0700 (PDT) X-Received: by 2002:a17:902:cf04:b0:1f3:2fa7:6d7b with SMTP id d9443c01a7336-1f32fa76e61mr33104035ad.19.1716440698265; Wed, 22 May 2024 22:04:58 -0700 (PDT) Received: from localhost ([43.228.180.230]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f32d91cd9asm15943535ad.267.2024.05.22.22.04.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 May 2024 22:04:57 -0700 (PDT) From: Coiby Xu To: kexec@lists.infradead.org Cc: Ondrej Kozina , Milan Broz , Thomas Staudt , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Kairui Song , Jan Pazdziora , Pingfan Liu , Baoquan He , Dave Young , linux-kernel@vger.kernel.org, x86@kernel.org, Dave Hansen , Vitaly Kuznetsov , Eric Biederman Subject: [PATCH v4 1/7] kexec_file: allow to place kexec_buf randomly Date: Thu, 23 May 2024 13:04:42 +0800 Message-ID: <20240523050451.788754-2-coxu@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240523050451.788754-1-coxu@redhat.com> References: <20240523050451.788754-1-coxu@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Currently, kexec_buf is placed in order which means for the same machine, the info in the kexec_buf is always located at the same position each time the machine is booted. This may cause a risk for sensitive information like LUKS volume key. Now struct kexec_buf has a new field random which indicates it's supposed to be placed in a random position. Suggested-by: Jan Pazdziora Signed-off-by: Coiby Xu --- include/linux/kexec.h | 2 ++ kernel/kexec_file.c | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/linux/kexec.h b/include/linux/kexec.h index f0e9f8eda7a3..cc81b8a903ab 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -171,6 +171,7 @@ int kexec_image_post_load_cleanup_default(struct kimage *image); * @buf_min: The buffer can't be placed below this address. * @buf_max: The buffer can't be placed above this address. * @top_down: Allocate from top of memory. + * @random: Place the buffer at a random position. */ struct kexec_buf { struct kimage *image; @@ -182,6 +183,7 @@ struct kexec_buf { unsigned long buf_min; unsigned long buf_max; bool top_down; + bool random; }; int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf); diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c index 3d64290d24c9..06b77f9ac4cc 100644 --- a/kernel/kexec_file.c +++ b/kernel/kexec_file.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include "kexec_internal.h" @@ -437,6 +438,16 @@ SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd, return ret; } +static unsigned long kexec_random_start(unsigned long start, unsigned long end) +{ + unsigned long temp_start; + unsigned short i; + + get_random_bytes(&i, sizeof(unsigned short)); + temp_start = start + (end - start) / USHRT_MAX * i; + return temp_start; +} + static int locate_mem_hole_top_down(unsigned long start, unsigned long end, struct kexec_buf *kbuf) { @@ -445,6 +456,8 @@ static int locate_mem_hole_top_down(unsigned long start, unsigned long end, temp_end = min(end, kbuf->buf_max); temp_start = temp_end - kbuf->memsz + 1; + if (kbuf->random) + temp_start = kexec_random_start(temp_start, temp_end); do { /* align down start */ @@ -482,6 +495,8 @@ static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end, unsigned long temp_start, temp_end; temp_start = max(start, kbuf->buf_min); + if (kbuf->random) + temp_start = kexec_random_start(temp_start, end); do { temp_start = ALIGN(temp_start, kbuf->buf_align); -- 2.45.0