of/kexec: Omit kaslr-seed when kexecing with nokaslr

Message ID 20230908104458.775740-1-songshuaishuai@tinylab.org
State New
Headers
Series of/kexec: Omit kaslr-seed when kexecing with nokaslr |

Commit Message

Song Shuai Sept. 8, 2023, 10:44 a.m. UTC
  The chosen node always adds the kaslr-seed property which is needless
for the kernels kexeced with the cmdline contained "nokaslr".

So omit the kaslr-seed when kexecing with nokaslr.

Signed-off-by: Song Shuai <songshuaishuai@tinylab.org>
---
 drivers/of/kexec.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
  

Comments

Rob Herring Sept. 8, 2023, 2:58 p.m. UTC | #1
On Fri, Sep 8, 2023 at 5:45 AM Song Shuai <songshuaishuai@tinylab.org> wrote:
>
> The chosen node always adds the kaslr-seed property which is needless
> for the kernels kexeced with the cmdline contained "nokaslr".
>
> So omit the kaslr-seed when kexecing with nokaslr.

The kexec'ed kernel can ignore bootargs with CONFIG_CMDLINE_FORCE. So
you don't know if kaslr-seed will be used or not.

> Signed-off-by: Song Shuai <songshuaishuai@tinylab.org>
> ---
>  drivers/of/kexec.c | 15 ++++++++++++---
>  1 file changed, 12 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c
> index f26d2ba8a371..c0d53b10cb70 100644
> --- a/drivers/of/kexec.c
> +++ b/drivers/of/kexec.c
> @@ -19,6 +19,7 @@
>  #include <linux/random.h>
>  #include <linux/slab.h>
>  #include <linux/types.h>
> +#include <linux/string.h>
>
>  #define RNG_SEED_SIZE          128
>
> @@ -263,6 +264,14 @@ static inline int setup_ima_buffer(const struct kimage *image, void *fdt,
>  }
>  #endif /* CONFIG_IMA_KEXEC */
>
> +static bool is_nokaslr_cmdline(const char *cmdline)
> +{
> +       char *str;
> +
> +       str = strstr(cmdline, "nokaslr");
> +       return str == cmdline || (str > cmdline && *(str - 1) == ' ');

Yet another copy of the same code. That's a clue to refactor into a
common function.

Rob
  
Song Shuai Sept. 11, 2023, 2:22 a.m. UTC | #2
在 2023/9/8 22:58, Rob Herring 写道:
> On Fri, Sep 8, 2023 at 5:45 AM Song Shuai <songshuaishuai@tinylab.org> wrote:
>>
>> The chosen node always adds the kaslr-seed property which is needless
>> for the kernels kexeced with the cmdline contained "nokaslr".
>>
>> So omit the kaslr-seed when kexecing with nokaslr.
> 
> The kexec'ed kernel can ignore bootargs with CONFIG_CMDLINE_FORCE. So
> you don't know if kaslr-seed will be used or not.
> 
You are correct. In addition, unlike the EFI Stub, Kexec is unable to
determine whether the kexec'ed kernel supports RANDOMIZE_BASE,
hence it is appropriate to emit the kaslr-seed unconditionally like
those out-of-linux bootloaders.

Apologies for bothering you with this incorrect patch.

>> Signed-off-by: Song Shuai <songshuaishuai@tinylab.org>
>> ---
>>   drivers/of/kexec.c | 15 ++++++++++++---
>>   1 file changed, 12 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c
>> index f26d2ba8a371..c0d53b10cb70 100644
>> --- a/drivers/of/kexec.c
>> +++ b/drivers/of/kexec.c
>> @@ -19,6 +19,7 @@
>>   #include <linux/random.h>
>>   #include <linux/slab.h>
>>   #include <linux/types.h>
>> +#include <linux/string.h>
>>
>>   #define RNG_SEED_SIZE          128
>>
>> @@ -263,6 +264,14 @@ static inline int setup_ima_buffer(const struct kimage *image, void *fdt,
>>   }
>>   #endif /* CONFIG_IMA_KEXEC */
>>
>> +static bool is_nokaslr_cmdline(const char *cmdline)
>> +{
>> +       char *str;
>> +
>> +       str = strstr(cmdline, "nokaslr");
>> +       return str == cmdline || (str > cmdline && *(str - 1) == ' ');
> 
> Yet another copy of the same code. That's a clue to refactor into a
> common function.
> 
> Rob
>
  

Patch

diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c
index f26d2ba8a371..c0d53b10cb70 100644
--- a/drivers/of/kexec.c
+++ b/drivers/of/kexec.c
@@ -19,6 +19,7 @@ 
 #include <linux/random.h>
 #include <linux/slab.h>
 #include <linux/types.h>
+#include <linux/string.h>
 
 #define RNG_SEED_SIZE		128
 
@@ -263,6 +264,14 @@  static inline int setup_ima_buffer(const struct kimage *image, void *fdt,
 }
 #endif /* CONFIG_IMA_KEXEC */
 
+static bool is_nokaslr_cmdline(const char *cmdline)
+{
+	char *str;
+
+	str = strstr(cmdline, "nokaslr");
+	return str == cmdline || (str > cmdline && *(str - 1) == ' ');
+}
+
 /*
  * of_kexec_alloc_and_setup_fdt - Alloc and setup a new Flattened Device Tree
  *
@@ -429,15 +438,15 @@  void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
 	else if (ret)
 		goto out;
 
-	if (rng_is_initialized()) {
+	if (!is_nokaslr_cmdline(cmdline) && rng_is_initialized()) {
 		u64 seed = get_random_u64();
 
 		ret = fdt_setprop_u64(fdt, chosen_node, "kaslr-seed", seed);
 		if (ret)
 			goto out;
 	} else {
-		pr_notice("RNG is not initialised: omitting \"%s\" property\n",
-			  "kaslr-seed");
+		pr_notice("RNG is not initialised or Kexec with nokaslr:"
+			  " omitting \"%s\" property\n", "kaslr-seed");
 	}
 
 	/* add rng-seed */