Skip to contentSkip to conversation

arm: kexec_file: Avoid temp buffer for RNG seed

RFC PATCH In review George Spelvin 4 participants 6 messages Mar 28, 2020 – Apr 28, 2020 arm64

lore
Share & tools
lore
#0 lore raw

RFC PATCH 39/50 arm: kexec_file: Avoid temp buffer for RNG seed

After using get_random_bytes(), you want to wipe the buffer afterward so the seed remains secret.

In this case, we can eliminate the temporary buffer entirely. fdt_setprop_placeholder returns a pointer to the property value buffer, allowing us to put the random data directy in there without using a temporary buffer at all. Faster and less stack all in one.

Cc Hsin-Yi Wang <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
---
 arch/arm64/kernel/machine_kexec_file.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/arm64/kernel/machine_kexec_file.c +4 -4
index 7b08bf9499b6b..69e25bb96e3fb 100644
@@ -106,12 +106,12 @@ static int setup_dtb(struct kimage *image,
 
 	/* add rng-seed */
 	if (rng_is_initialized()) {
-		u8 rng_seed[RNG_SEED_SIZE];
-		get_random_bytes(rng_seed, RNG_SEED_SIZE);
-		ret = fdt_setprop(dtb, off, FDT_PROP_RNG_SEED, rng_seed,
-				RNG_SEED_SIZE);
+		void *rng_seed;
+		ret = fdt_setprop_placeholder(dtb, off, FDT_PROP_RNG_SEED,
+				RNG_SEED_SIZE, &rng_seed);
 		if (ret)
 			goto out;
+		get_random_bytes(rng_seed, RNG_SEED_SIZE);
 	} else {
 		pr_notice("RNG is not initialised: omitting \"%s\" property\n",
 				FDT_PROP_RNG_SEED);
Signature · 1 line
2.26.0
Acked-by on v1 39/50
#1 lore raw
Quoted 9 lines · George Spelvin

After using get_random_bytes(), you want to wipe the buffer afterward so the seed remains secret.

In this case, we can eliminate the temporary buffer entirely. fdt_setprop_placeholder returns a pointer to the property value buffer, allowing us to put the random data directy in there without using a temporary buffer at all. Faster and less stack all in one.

Quoted 5 lines
Cc Hsin-Yi Wang <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Quoted 26 lines
---
 arch/arm64/kernel/machine_kexec_file.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/arm64/kernel/machine_kexec_file.c +4 -5
index 7b08bf9499b6b..69e25bb96e3fb 100644
@@ -106,12 +106,12 @@ static int setup_dtb(struct kimage *image,
 
        /* add rng-seed */
        if (rng_is_initialized()) {
-               u8 rng_seed[RNG_SEED_SIZE];
-               get_random_bytes(rng_seed, RNG_SEED_SIZE);
-               ret = fdt_setprop(dtb, off, FDT_PROP_RNG_SEED, rng_seed,
-                               RNG_SEED_SIZE);
+               void *rng_seed;
+               ret = fdt_setprop_placeholder(dtb, off, FDT_PROP_RNG_SEED,
+                               RNG_SEED_SIZE, &rng_seed);
                if (ret)
                        goto out;
+               get_random_bytes(rng_seed, RNG_SEED_SIZE);
        } else {
                pr_notice("RNG is not initialised: omitting \"%s\" property\n",
                                FDT_PROP_RNG_SEED);
--
Quoted 2 lines

2.26.0

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

reviewer Acked-by on v1 39/50
#2 lore raw
In reply to George Spelvin

Hi George,

Nit: s/arm/arm64/ in the title

Quoted 25 lines · George Spelvin

After using get_random_bytes(), you want to wipe the buffer afterward so the seed remains secret.

In this case, we can eliminate the temporary buffer entirely. fdt_setprop_placeholder returns a pointer to the property value buffer, allowing us to put the random data directy in there without using a temporary buffer at all. Faster and less stack all in one.

Cc Hsin-Yi Wang <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
---
 arch/arm64/kernel/machine_kexec_file.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/arm64/kernel/machine_kexec_file.c +0 -0
index 7b08bf9499b6b..69e25bb96e3fb 100644
@@ -106,12 +106,12 @@ static int setup_dtb(struct kimage *image,
 
 	/* add rng-seed */
 	if (rng_is_initialized()) {

arch/arm64/kernel/machine_kexec_file.c static int setup_dtb(struct kimage *image, · line 114

Quoted 10 lines
arch/arm64/kernel/machine_kexec_file.c +4 -4
-		u8 rng_seed[RNG_SEED_SIZE];
-		get_random_bytes(rng_seed, RNG_SEED_SIZE);
-		ret = fdt_setprop(dtb, off, FDT_PROP_RNG_SEED, rng_seed,
-				RNG_SEED_SIZE);
+		void *rng_seed;
+		ret = fdt_setprop_placeholder(dtb, off, FDT_PROP_RNG_SEED,
+				RNG_SEED_SIZE, &rng_seed);
 		if (ret)
 			goto out;
+		get_random_bytes(rng_seed, RNG_SEED_SIZE);

This looks sane to me, so FWIW:

Mark.

Quoted 6 lines
arch/arm64/kernel/machine_kexec_file.c +0 -0
 	} else {
 		pr_notice("RNG is not initialised: omitting \"%s\" property\n",
 				FDT_PROP_RNG_SEED);
Signature · 1 line
2.26.0
maintainer Acked-by on v1 39/50
#3 lore raw
In reply to George Spelvin
Quoted 6 lines · George Spelvin

After using get_random_bytes(), you want to wipe the buffer afterward so the seed remains secret.

In this case, we can eliminate the temporary buffer entirely. fdt_setprop_placeholder returns a pointer to the property value buffer, allowing us to put the random data directy in there without

s/directy/directly/

Quoted 10 lines

using a temporary buffer at all. Faster and less stack all in one.

Cc Hsin-Yi Wang <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
---
 arch/arm64/kernel/machine_kexec_file.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Please let me know if you'd like this queued via the arm64 tree, as it appears to be independent of the rest of this series.

Will

#4 lore raw

PATCH v2 1/1 arm64: kexec_file: Avoid temp buffer for RNG seed

After using get_random_bytes(), you want to wipe the buffer afterward so the seed remains secret.

In this case, we can eliminate the temporary buffer entirely. fdt_setprop_placeholder() returns a pointer to the property value buffer, allowing us to put the random data directly in there without using a temporary buffer at all. Faster and less stack all in one.

---
v2: Typos in commit message fixed.

Thank you, I'd be delighted if you'd apply it to the arm64 tree directly! I can take it out of my patch series and off my plate.

Now that I'm looking at it some more, I want to change fdt_setprop_placeholder to return an ERR_PTR. Must. Stop. Scope. Creep.

 arch/arm64/kernel/machine_kexec_file.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/arm64/kernel/machine_kexec_file.c +4 -4
index 7b08bf9499b6b..69e25bb96e3fb 100644
@@ -106,12 +106,12 @@ static int setup_dtb(struct kimage *image,
 
 	/* add rng-seed */
 	if (rng_is_initialized()) {
-		u8 rng_seed[RNG_SEED_SIZE];
-		get_random_bytes(rng_seed, RNG_SEED_SIZE);
-		ret = fdt_setprop(dtb, off, FDT_PROP_RNG_SEED, rng_seed,
-				RNG_SEED_SIZE);
+		void *rng_seed;
+		ret = fdt_setprop_placeholder(dtb, off, FDT_PROP_RNG_SEED,
+				RNG_SEED_SIZE, &rng_seed);
 		if (ret)
 			goto out;
+		get_random_bytes(rng_seed, RNG_SEED_SIZE);
 	} else {
 		pr_notice("RNG is not initialised: omitting \"%s\" property\n",
 				FDT_PROP_RNG_SEED);
Signature · 6 lines
2.26.0

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
maintainer Applied
#5 lore raw

Re: [PATCH v2] arm64: kexec_file: Avoid temp buffer for RNG seed

Quoted 7 lines · George Spelvin

After using get_random_bytes(), you want to wipe the buffer afterward so the seed remains secret.

In this case, we can eliminate the temporary buffer entirely. fdt_setprop_placeholder() returns a pointer to the property value buffer, allowing us to put the random data directly in there without using a temporary buffer at all. Faster and less stack all in one.

Applied to arm64 (for-next/misc), thanks!

[1/1] arm64: kexec_file: Avoid temp buffer for RNG seed

      https://git.kernel.org/arm64/c/99ee28d99607

Cheers,

Signature · 4 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev