Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp1000035lqt; Fri, 7 Jun 2024 05:28:37 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXfhKs1oUIrf+wZPy57jUVizp1zYuPW/vwQJ265i+7W0+WJk8R4uX1EvACF58fCi1dugxQOo8xToJ9RxCyACNuwclnRkSgXo45KEFU6QQ== X-Google-Smtp-Source: AGHT+IGVdEuq4GJ/GOXMXsw0hOsaYBqGY86nUrctDb6PQ2lP4vOVhEJw47saKn2HKT83irfNDQAU X-Received: by 2002:a05:6a21:78a8:b0:1af:cc48:3e25 with SMTP id adf61e73a8af0-1b2f969bf90mr2612652637.10.1717763317363; Fri, 07 Jun 2024 05:28:37 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717763317; cv=pass; d=google.com; s=arc-20160816; b=h8sWalCIshO0kpiwW7hvu6sRdHvjuUaIgAwLP4DBDakN3ETAKMJqGeMpDtVUshzB1K dDbZf4LVb6s3Bh+mi9iIGhxwcqHE4O+Bcoo/zEcpY3+YjwHNXuEKIP3ATCfTD+eNbn+9 oxKxpMyg+C2MxxyBRyJzMzAcNxHFAHbPqYVYdB3FTZthttfou066STFp1P1FoiYTjQPs zZPXfN+S6y+YhBg1Mj5LbqlfdNeMLTvbJtTclHtQBSBLj+wHcADHdMhbKEnHKAYOMTkJ eHwRnEmz/Z6CpHaWnLbOFbkzEc+FXB4czw32dMz+zM0EcHlLX2ryW/T41uSwZtTu/kc9 fujg== 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=j2P/UHfuOg4ehNZSVTesGfCNw2riSEHvP0JbrY+cOcU=; fh=FeBvRom12i2OcENa+BLEXBQaBP7tGI5egpSn/4iEz3Y=; b=uspxxGsVnblnuMYjgcSN0e+BzwH4Dxd+JFF69RgnYNzBUXqLrYvNy5503OwkIm/cXz 9MEoLMeRWki04R6b5GIlHiInM3j1oGxjju4k2DpX6Wc5UTDKvHMXG2p48YbdDXqmq5Gy X9kz9RiDlbejI9e6FD7cgMFe8+4SykyTIpQdFfKZHKTmn3JEFNcKJ45qGqpoc587puvy QP338nKvZXTDEicnw6URx5GBFZxkh1OcLTOQIHTwjtT7OZIYyvCJjmjh096xubIYQw4F Bhh320SWzA2MxYNPEqcAfabC1IzCYxgIAooPqx5StlUCPgmpP2ESo6a/D94tFeZZbfa6 cB9Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MmAff3dr; 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-206031-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-206031-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-2c2d1182f2csi226687a91.182.2024.06.07.05.28.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 05:28:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-206031-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=MmAff3dr; 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-206031-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-206031-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 318D4284247 for ; Fri, 7 Jun 2024 12:28:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2D79B1946DF; Fri, 7 Jun 2024 12:26:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="MmAff3dr" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 CAC3319414E for ; Fri, 7 Jun 2024 12:26:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717763199; cv=none; b=ss/qZe19MFA75/UFhDIQfx/br7euliIsPDzvlwRdoQl6a/hUJ5N6yb0YO7EKS/icpapo1uMCtZ8mv/eR+FX+3zQh9MIfHRGsv52sIEncPydTODxuUSmJ81NhfH9QPiDN5Bo6oobiQ5pY3i2uwNljzujh9ddNZGke/0r7MXEJ/iw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717763199; c=relaxed/simple; bh=RFJNZU7TLgnGsVmKkHuMKPqdGs2uAqapaQ0gSTvA9Ro=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rqRSBa1w6bGgwm9zCYFFDfdWEhRjB7c1WR5CpaqcJMT4OGtksHl1yl9ID9J//HvZ+hcm9OSzx0QCkGF4sHHN+IFuuntlV6FQ5wb/FcoX1qmD4AYVcjC8wZN4BRhgnnwVxkTkkSLJCp7u/Vp+XlbEQsQe/HEEXjZD4JR+8FSkGDE= 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=MmAff3dr; arc=none smtp.client-ip=170.10.133.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=1717763196; 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=j2P/UHfuOg4ehNZSVTesGfCNw2riSEHvP0JbrY+cOcU=; b=MmAff3drcqdp8NBqxYVK/rMHkzCKNzm3EVrUAInfUN1prucnBtnCaF5IlrWBJc3cLoM0Bw 8uIuSjt8l1sudyuxToHuBn0xn6A8y89sG22nV521QMdM+H0R/8/Y5zlibz9R6XicA28pBw cF/7lw7T0Ay+93E0GEGazo8Tmn7x+kQ= 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-608-X0C3hKA-Ohec7NiucwAHOw-1; Fri, 07 Jun 2024 08:26:35 -0400 X-MC-Unique: X0C3hKA-Ohec7NiucwAHOw-1 Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-1f6174cfcf8so21277435ad.3 for ; Fri, 07 Jun 2024 05:26:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717763194; x=1718367994; 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=j2P/UHfuOg4ehNZSVTesGfCNw2riSEHvP0JbrY+cOcU=; b=AbX6szlHwuYsT5o0F2Y6r377XAqM37owiwiFBMC2drVYUJsQ0PwCiV5oq+6lH+Nm5U jcTh+rqxfF4wJ80/oIiTnJGHpEMbAtaduUXqwts5GRic57XZUdskRsrS1F/GlQoZNRiW vJ+Yvob1njcA2jMBK1g5jTDGJsqp1Iqilf5pe3JwJ2avfvx2l8OpSUMjtmjkVUEonSFB ea6lElUK/8FYu3uc+BabcJcSjgbjSH8PSIloM5s7B4+S7lwVolHiYDZw7rpa/TICsiC6 n8om1vefm/0BALoHUEDE+oSIRUwDB1/WW72r67YuyA9GjdKy0H7YuuhH8Nr5IyB0wAOW TNNw== X-Forwarded-Encrypted: i=1; AJvYcCURYlO5PPokpVMohRzblFSuJZbnZifJ4zuFEvi4igHVj6Z/6qM+gozVpooVUqD+p073J+radcR0fSMma7h4GrhNW9jmMd+EQXG1Gi4U X-Gm-Message-State: AOJu0Yzin799BiNKfYj7bUTUXdFzB5Kze8uwaHUcseQaTn8oM25ib4HN tEyxc9L9b6P9zN49KPpvaUl63J9BnOJa7GThjjh9ldAMURQ3mI2+7IHYKqFe+92svALVQLcJ9Yc SpOegNcYkqGnwynC+TRGfVP4dWRoQ9F210K7YBmn9JY5Fb47/S9OgR6nV3/FKcg== X-Received: by 2002:a17:902:e54c:b0:1f6:5869:df53 with SMTP id d9443c01a7336-1f6d02e647amr28703855ad.20.1717763193987; Fri, 07 Jun 2024 05:26:33 -0700 (PDT) X-Received: by 2002:a17:902:e54c:b0:1f6:5869:df53 with SMTP id d9443c01a7336-1f6d02e647amr28703345ad.20.1717763193274; Fri, 07 Jun 2024 05:26:33 -0700 (PDT) Received: from localhost ([43.228.180.230]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f6bd75f45bsm33029905ad.35.2024.06.07.05.26.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 05:26:32 -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 , Greg KH , Eric Biederman Subject: [PATCH v5 1/7] kexec_file: allow to place kexec_buf randomly Date: Fri, 7 Jun 2024 20:26:11 +0800 Message-ID: <20240607122622.167228-2-coxu@redhat.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240607122622.167228-1-coxu@redhat.com> References: <20240607122622.167228-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 | 4 ++++ kernel/kexec_file.c | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/include/linux/kexec.h b/include/linux/kexec.h index f0e9f8eda7a3..c45bfc727737 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,9 @@ struct kexec_buf { unsigned long buf_min; unsigned long buf_max; bool top_down; +#ifdef CONFIG_CRASH_DUMP + bool random; +#endif }; 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..f7538d8f67e0 100644 --- a/kernel/kexec_file.c +++ b/kernel/kexec_file.c @@ -25,6 +25,9 @@ #include #include #include +#ifdef CONFIG_CRASH_DUMP +#include +#endif #include #include #include "kexec_internal.h" @@ -437,6 +440,18 @@ SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd, return ret; } +#ifdef CONFIG_CRASH_DUMP +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; +} +#endif + static int locate_mem_hole_top_down(unsigned long start, unsigned long end, struct kexec_buf *kbuf) { @@ -445,6 +460,10 @@ 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; +#ifdef CONFIG_CRASH_DUMP + if (kbuf->random) + temp_start = kexec_random_start(temp_start, temp_end); +#endif do { /* align down start */ @@ -482,6 +501,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.1