[PATCH v6 1/7] kexec_file: allow to place kexec_buf randomly

Coiby Xu posted 7 patches 1 year, 3 months ago
There is a newer version of this series
[PATCH v6 1/7] kexec_file: allow to place kexec_buf randomly
Posted by Coiby Xu 1 year, 3 months ago
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 <jpazdziora@redhat.com>
Signed-off-by: Coiby Xu <coxu@redhat.com>
---
 include/linux/kexec.h | 28 ++++++++++++++++++++++++++++
 kernel/kexec_file.c   |  3 +++
 2 files changed, 31 insertions(+)

diff --git a/include/linux/kexec.h b/include/linux/kexec.h
index f0e9f8eda7a3..0dc66ca2506a 100644
--- a/include/linux/kexec.h
+++ b/include/linux/kexec.h
@@ -25,6 +25,10 @@
 
 extern note_buf_t __percpu *crash_notes;
 
+#ifdef CONFIG_CRASH_DUMP
+#include <linux/prandom.h>
+#endif
+
 #ifdef CONFIG_KEXEC_CORE
 #include <linux/list.h>
 #include <linux/compat.h>
@@ -171,6 +175,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,8 +187,31 @@ struct kexec_buf {
 	unsigned long buf_min;
 	unsigned long buf_max;
 	bool top_down;
+#ifdef CONFIG_CRASH_DUMP
+	bool random;
+#endif
 };
 
+
+#ifdef CONFIG_CRASH_DUMP
+static inline void kexec_random_start(unsigned long start, unsigned long end,
+				      struct kexec_buf *kbuf,
+				      unsigned long *temp_start)
+{
+	unsigned short i;
+
+	if (kbuf->random) {
+		get_random_bytes(&i, sizeof(unsigned short));
+		*temp_start = start + (end - start) / USHRT_MAX * i;
+	}
+}
+#else
+static inline void kexec_random_start(unsigned long start, unsigned long end,
+				      struct kexec_buf *kbuf,
+				      unsigned long *temp_start)
+{}
+#endif
+
 int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf);
 int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
 				   void *buf, unsigned int size,
diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
index 3eedb8c226ad..06565d867b69 100644
--- a/kernel/kexec_file.c
+++ b/kernel/kexec_file.c
@@ -445,6 +445,7 @@ 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;
+	kexec_random_start(temp_start, temp_end, kbuf, &temp_start);
 
 	do {
 		/* align down start */
@@ -483,6 +484,8 @@ static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
 
 	temp_start = max(start, kbuf->buf_min);
 
+	kexec_random_start(temp_start, end, kbuf, &temp_start);
+
 	do {
 		temp_start = ALIGN(temp_start, kbuf->buf_align);
 		temp_end = temp_start + kbuf->memsz - 1;
-- 
2.47.0
Re: [PATCH v6 1/7] kexec_file: allow to place kexec_buf randomly
Posted by Baoquan He 1 year, 2 months ago
On 10/29/24 at 01:52pm, Coiby Xu wrote:
> 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.

This change the generic code, but you don't mention this only takes
effect in kdump case, won't impact kexec reboot case. I got this from
code, while this should be mentioned in log.

> 
> Suggested-by: Jan Pazdziora <jpazdziora@redhat.com>
> Signed-off-by: Coiby Xu <coxu@redhat.com>
> ---
>  include/linux/kexec.h | 28 ++++++++++++++++++++++++++++
>  kernel/kexec_file.c   |  3 +++
>  2 files changed, 31 insertions(+)
> 
> diff --git a/include/linux/kexec.h b/include/linux/kexec.h
> index f0e9f8eda7a3..0dc66ca2506a 100644
> --- a/include/linux/kexec.h
> +++ b/include/linux/kexec.h
> @@ -25,6 +25,10 @@
>  
>  extern note_buf_t __percpu *crash_notes;
>  
> +#ifdef CONFIG_CRASH_DUMP
> +#include <linux/prandom.h>
> +#endif
> +
>  #ifdef CONFIG_KEXEC_CORE
>  #include <linux/list.h>
>  #include <linux/compat.h>
> @@ -171,6 +175,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,8 +187,31 @@ struct kexec_buf {
>  	unsigned long buf_min;
>  	unsigned long buf_max;
>  	bool top_down;
> +#ifdef CONFIG_CRASH_DUMP
> +	bool random;
> +#endif
>  };
>  
> +
> +#ifdef CONFIG_CRASH_DUMP
> +static inline void kexec_random_start(unsigned long start, unsigned long end,
                      ~~~~~~~~~~~~~~~~~~
This function name is very confusing. I thought it's a starting to
randomize at the first glance, then realized it's to randomize the
starting position of range.
> +				      struct kexec_buf *kbuf,
> +				      unsigned long *temp_start)
> +{
> +	unsigned short i;
> +
> +	if (kbuf->random) {
> +		get_random_bytes(&i, sizeof(unsigned short));
> +		*temp_start = start + (end - start) / USHRT_MAX * i;
> +	}
> +}
> +#else
> +static inline void kexec_random_start(unsigned long start, unsigned long end,
> +				      struct kexec_buf *kbuf,
> +				      unsigned long *temp_start)
> +{}
> +#endif
> +
>  int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf);
>  int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
>  				   void *buf, unsigned int size,
> diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
> index 3eedb8c226ad..06565d867b69 100644
> --- a/kernel/kexec_file.c
> +++ b/kernel/kexec_file.c
> @@ -445,6 +445,7 @@ 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;
> +	kexec_random_start(temp_start, temp_end, kbuf, &temp_start);
>  
>  	do {
>  		/* align down start */
> @@ -483,6 +484,8 @@ static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
>  
>  	temp_start = max(start, kbuf->buf_min);
>  
> +	kexec_random_start(temp_start, end, kbuf, &temp_start);
> +
>  	do {
>  		temp_start = ALIGN(temp_start, kbuf->buf_align);
>  		temp_end = temp_start + kbuf->memsz - 1;
> -- 
> 2.47.0
>
Re: [PATCH v6 1/7] kexec_file: allow to place kexec_buf randomly
Posted by Coiby Xu 1 year, 2 months ago
On Fri, Nov 29, 2024 at 09:38:58AM +0800, Baoquan He wrote:
>On 10/29/24 at 01:52pm, Coiby Xu wrote:
>> 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.
>
>This change the generic code, but you don't mention this only takes
>effect in kdump case, won't impact kexec reboot case. I got this from
>code, while this should be mentioned in log.

Thanks for the reminder! I'll put a note in the commit msg.

>
[...]
>> +
>> +#ifdef CONFIG_CRASH_DUMP
>> +static inline void kexec_random_start(unsigned long start, unsigned long end,
>                      ~~~~~~~~~~~~~~~~~~
>This function name is very confusing. I thought it's a starting to
>randomize at the first glance, then realized it's to randomize the
>starting position of range.

Thanks for raising this concern! I'll rename it to
kexec_random_range_start.

-- 
Best regards,
Coiby